diff --git a/.github/workflows/release-samples.yml b/.github/workflows/release-samples.yml new file mode 100644 index 0000000..329a71f --- /dev/null +++ b/.github/workflows/release-samples.yml @@ -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." diff --git a/.github/workflows/validate-samples.yml b/.github/workflows/validate-samples.yml new file mode 100644 index 0000000..df737e0 --- /dev/null +++ b/.github/workflows/validate-samples.yml @@ -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 diff --git a/README.md b/README.md index 97048d9..23f8962 100644 --- a/README.md +++ b/README.md @@ -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//` 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 diff --git a/scripts/build-samples-index.mjs b/scripts/build-samples-index.mjs new file mode 100644 index 0000000..7c1e1db --- /dev/null +++ b/scripts/build-samples-index.mjs @@ -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`); diff --git a/src/edgeactions-js/a-b-experimentation/sample.json b/src/edgeactions-js/a-b-experimentation/sample.json new file mode 100644 index 0000000..13e7ed8 --- /dev/null +++ b/src/edgeactions-js/a-b-experimentation/sample.json @@ -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"] +} diff --git a/src/edgeactions-js/header-add/sample.json b/src/edgeactions-js/header-add/sample.json new file mode 100644 index 0000000..8237955 --- /dev/null +++ b/src/edgeactions-js/header-add/sample.json @@ -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"] +} diff --git a/src/edgeactions-js/origin-select/sample.json b/src/edgeactions-js/origin-select/sample.json new file mode 100644 index 0000000..7642485 --- /dev/null +++ b/src/edgeactions-js/origin-select/sample.json @@ -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"] +} diff --git a/src/edgeactions-js/request-rejection/sample.json b/src/edgeactions-js/request-rejection/sample.json new file mode 100644 index 0000000..f04e217 --- /dev/null +++ b/src/edgeactions-js/request-rejection/sample.json @@ -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"] +} diff --git a/src/edgeactions-js/sample.schema.json b/src/edgeactions-js/sample.schema.json new file mode 100644 index 0000000..9ef671f --- /dev/null +++ b/src/edgeactions-js/sample.schema.json @@ -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." + } + } +} diff --git a/src/edgeactions-js/url-rewrite/sample.json b/src/edgeactions-js/url-rewrite/sample.json new file mode 100644 index 0000000..66e9781 --- /dev/null +++ b/src/edgeactions-js/url-rewrite/sample.json @@ -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"] +}