Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/release-samples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release samples index

on:
push:
branches: [main]
paths:
- "src/edgeactions-js/**"
- "scripts/build-samples-index.mjs"
workflow_dispatch:

permissions:
contents: write

concurrency:
group: release-samples

jobs:
release:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
with:
node-version: 22
- run: node scripts/build-samples-index.mjs samples-index.json
- name: Publish release
env:
GH_TOKEN: ${{ github.token }}
run: gh release create "samples-${GITHUB_SHA::7}" samples-index.json --target "$GITHUB_SHA" --notes "Samples index built from $GITHUB_SHA."
20 changes: 20 additions & 0 deletions .github/workflows/validate-samples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Validate samples

on:
pull_request:
paths:
- "src/edgeactions-js/**"
- "scripts/build-samples-index.mjs"

permissions:
contents: read

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
with:
node-version: 22
- run: node scripts/build-samples-index.mjs
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ Edge actions can be setup and consumed using Azure portal.

This repo hosts sample code for Edge Actions at https://github.com/Azure/EdgeActionsSamples/tree/main/src/edgeactions-js

## Adding a sample

1. Create `src/edgeactions-js/<sample-id>/` with `edge_action.js` and a `test_input.json` (or `test_cases.json`).
2. Add a `sample.json`:
```json
{
"$schema": "../sample.schema.json",
"title": "Add a response header",
"description": "Sets the response status to 200 and adds a custom response header.",
"category": "headers",
"tags": ["response-headers"]
}
```
`category` is the gallery filter and must be one of the values in `src/edgeactions-js/sample.schema.json`. Omit it if none fits. `tags` are optional search keywords. Reuse existing tags where possible.
3. Run `node scripts/build-samples-index.mjs` to validate, then open a PR.

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Expand Down
62 changes: 62 additions & 0 deletions scripts/build-samples-index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Builds one index of all samples from src/edgeactions-js/*/sample.json.
// node scripts/build-samples-index.mjs validate only
// node scripts/build-samples-index.mjs index.json validate and write the index

import fs from "node:fs";
import path from "node:path";

const root = path.join(import.meta.dirname, "..", "src", "edgeactions-js");
const read = (...p) => fs.readFileSync(path.join(root, ...p), "utf8").replace(/^\uFEFF/, "").replace(/\r\n/g, "\n");
const exists = (...p) => fs.existsSync(path.join(root, ...p));

const categories = JSON.parse(read("sample.schema.json")).properties.category.enum;
const errors = [];
const samples = [];

for (const id of fs.readdirSync(root).sort()) {
if (!exists(id, "edge_action.js")) continue;
const fail = (msg) => errors.push(`${id}: ${msg}`);
if (!exists(id, "sample.json")) { fail("missing sample.json"); continue; }

let meta, testInput;
try {
meta = JSON.parse(read(id, "sample.json"));
testInput = exists(id, "test_input.json")
? JSON.parse(read(id, "test_input.json"))
: JSON.parse(read(id, "test_cases.json")).test_cases[0].input;
} catch (e) { fail(e.message); continue; }

if (!meta.title?.trim()) fail("title is required");
if (!meta.description?.trim()) fail("description is required");
if (meta.category !== undefined && !categories.includes(meta.category)) {
fail(`category must be one of: ${categories.join(", ")}`);
}
const tags = meta.tags ?? [];
if (!Array.isArray(tags) || tags.some((t) => typeof t !== "string" || !/^[a-z0-9-]+$/.test(t)) || new Set(tags).size !== tags.length) {
fail("tags must be unique lowercase strings (a-z, 0-9, -)");
}
if (typeof testInput !== "object" || testInput === null || Array.isArray(testInput)) {
fail("test input must be a JSON object");
}

const code = read(id, "edge_action.js");
samples.push({
id,
title: meta.title,
description: meta.description,
category: meta.category ?? null,
tags,
// Drop editor-only type hints that reference the local edge_actions.d.ts.
code: code.replace(/^\/\/ @ts-check\n+/, "").replaceAll('import("./edge_actions").', ""),
testInput,
});
}

if (errors.length) {
console.error(errors.join("\n"));
process.exit(1);
}
if (process.argv[2]) {
fs.writeFileSync(process.argv[2], JSON.stringify({ categories, samples }, null, 2) + "\n");
}
console.log(`${samples.length} samples OK`);
6 changes: 6 additions & 0 deletions src/edgeactions-js/a-b-experimentation/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../sample.schema.json",
"title": "A/B experimentation",
"description": "Routes opted-in mobile requests to an experimental origin.",
"tags": ["a-b-testing", "origin-selection"]
}
6 changes: 6 additions & 0 deletions src/edgeactions-js/header-add/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../sample.schema.json",
"title": "Add a response header",
"description": "Sets the response status to 200 and adds a custom response header.",
"tags": ["response-headers"]
}
6 changes: 6 additions & 0 deletions src/edgeactions-js/origin-select/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../sample.schema.json",
"title": "Select an origin by country",
"description": "Routes US requests to a US origin and falls back to the first available origin.",
"tags": ["geolocation", "origin-selection"]
}
6 changes: 6 additions & 0 deletions src/edgeactions-js/request-rejection/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../sample.schema.json",
"title": "Block requests by country",
"description": "Rejects requests from the United Kingdom with a 403 response.",
"tags": ["geolocation", "access-control"]
}
31 changes: 31 additions & 0 deletions src/edgeactions-js/sample.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Edge Actions sample metadata",
"type": "object",
"additionalProperties": false,
"required": ["title", "description"],
"properties": {
"$schema": { "type": "string" },
"title": {
"type": "string",
"minLength": 1,
"description": "Short title shown in the samples gallery."
},
"description": {
"type": "string",
"minLength": 1,
"description": "One sentence describing what the sample does."
},
"category": {
"type": "string",
"enum": ["headers", "routing", "security"],
"description": "Gallery filter. Omit if no category fits. To add one, extend this list."
},
"tags": {
"type": "array",
"uniqueItems": true,
"items": { "type": "string", "pattern": "^[a-z0-9-]+$" },
"description": "Optional search keywords. Reuse existing tags where possible."
}
}
}
6 changes: 6 additions & 0 deletions src/edgeactions-js/url-rewrite/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../sample.schema.json",
"title": "Rewrite URLs",
"description": "Rewrites legacy paths, removes trailing slashes, and strips tracking query parameters.",
"tags": ["url-rewrite", "query-string"]
}