From 3d1873c38b51e597eecb5b1dd188791dd8312e45 Mon Sep 17 00:00:00 2001 From: Nikolay Zhidenko Date: Thu, 19 Nov 2026 15:48:54 +0100 Subject: [PATCH] ci: run the packed SDK on Deno 2.1.4 and current Deno 2 Supabase Edge Functions import npm:@proofage/node, and customers already integrate from there, but nothing tested it. scripts/deno-smoke.sh packs the package, installs the tarball into deno/ and runs deno/smoke.ts: the shared HMAC vectors, a signed create against a local stand-in for the API (keys explicit and from the environment, SDK header checked) and webhookHandler accepting a signed delivery and refusing a tampered one. The README shows the npm: import for Deno. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 40 +++++++++++ CLAUDE.md | 9 +++ README.md | 13 +++- deno/package.json | 5 ++ deno/smoke.ts | 140 +++++++++++++++++++++++++++++++++++++++ scripts/deno-smoke.sh | 18 +++++ 6 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 deno/package.json create mode 100644 deno/smoke.ts create mode 100755 scripts/deno-smoke.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0d560a1..579aaac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,3 +51,43 @@ jobs: - name: Build package run: npm run build + + deno: + runs-on: ubuntu-latest + timeout-minutes: 10 + + # Deno is a supported runtime: Supabase Edge Functions load the package as + # npm:@proofage/node, and customers integrate from there. scripts/deno-smoke.sh + # packs the SDK and runs deno/smoke.ts against the tarball (HMAC vectors, a + # signed create, the webhook handler). + # + # 2.1.4 the Deno that Supabase Edge Runtime 1.77-1.79 reported in production + # User-Agents (Nov 2026); raise it when Supabase moves on + # v2.x the current Deno 2 release + strategy: + fail-fast: false + matrix: + deno-version: ['2.1.4', 'v2.x'] + + name: Deno ${{ matrix.deno-version }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 24 + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Setup Deno + uses: denoland/setup-deno@v2 + with: + deno-version: ${{ matrix.deno-version }} + + - name: Deno smoke test + run: scripts/deno-smoke.sh diff --git a/CLAUDE.md b/CLAUDE.md index bfe455e..701cd1d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -34,3 +34,12 @@ does not ship (see `files` in package.json), so maintainer notes belong here. `proofage/php-sdk` — the file the app also runs through its real `VerifyHmacSignature` middleware. Refresh it by copying when that file changes; never edit it by hand. A signing change here that breaks a vector breaks real requests. + +## Deno + +Deno 2 (and Supabase Edge Functions, which import `npm:@proofage/node`) is a supported runtime. +CI's `deno` job runs `scripts/deno-smoke.sh`: it packs the package and runs `deno/smoke.ts` +against the tarball. Keep the SDK to what Deno's Node compatibility provides (`node:crypto`, +`Buffer`, `process.env`, global `fetch`); anything else fails that job. Without a local Deno, run +the smoke test in Docker after building, packing and installing the tarball into `deno/`: +`docker run --rm -v "$PWD":/repo -w /repo/deno denoland/deno:2.1.4 run --allow-net=127.0.0.1 --allow-env --allow-read smoke.ts`. diff --git a/README.md b/README.md index 903f575..a43b3f6 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This package provides a first-class Node.js integration: auto-configuration from ## Requirements -- Node.js 22+ (see `engines` in package.json) +- Node.js 22+ (see `engines` in package.json), or Deno 2, including Supabase Edge Functions ## Installation @@ -20,6 +20,17 @@ This package provides a first-class Node.js integration: auto-configuration from npm install @proofage/node ``` +On Deno and in Supabase Edge Functions, import it with the `npm:` specifier; there is nothing to install: + +```ts +import { ProofAgeClient } from 'npm:@proofage/node'; + +const client = new ProofAgeClient({ + apiKey: Deno.env.get('PROOFAGE_API_KEY'), + secretKey: Deno.env.get('PROOFAGE_SECRET_KEY'), +}); +``` + ## Quick Start Set your environment variables: diff --git a/deno/package.json b/deno/package.json new file mode 100644 index 0000000..b0c34cb --- /dev/null +++ b/deno/package.json @@ -0,0 +1,5 @@ +{ + "private": true, + "type": "module", + "description": "Deno smoke test for the packed @proofage/node tarball. scripts/deno-smoke.sh installs the tarball here and runs smoke.ts." +} diff --git a/deno/smoke.ts b/deno/smoke.ts new file mode 100644 index 0000000..0a70af6 --- /dev/null +++ b/deno/smoke.ts @@ -0,0 +1,140 @@ +/** + * Runs the packed @proofage/node under Deno, the way a Supabase Edge Function loads it: + * as npm package code, not as source. It checks what the SDK needs from the runtime — + * node:crypto, Buffer, process.env and fetch — on real calls: + * + * 1. the shared HMAC vectors (the server accepts these exact signatures); + * 2. a signed create against a local stand-in for the API, keys from the environment + * and passed explicitly, with the SDK identification header; + * 3. webhookHandler accepting a signed delivery and refusing a tampered one. + * + * Run by scripts/deno-smoke.sh; it is not part of the npm package. + */ +import { + generateHmacSignature, + generateWebhookSignature, + ProofAgeClient, + SDK_VERSION, + webhookHandler, +} from '@proofage/node'; + +function check(condition: unknown, message: string): asserts condition { + if (!condition) { + throw new Error(`Deno smoke test failed: ${message}`); + } +} + +const API_KEY = 'pk_test_deno_smoke'; +const SECRET_KEY = 'sk_test_deno_smoke'; + +// 1. Shared HMAC vectors. +interface Vectors { + secret: string; + json: Array<{ name: string; method: string; path: string; query?: string; body: string; expected: string }>; +} + +const vectors = JSON.parse( + await Deno.readTextFile(new URL('../tests/fixtures/hmac-vectors.json', import.meta.url)), +) as Vectors; + +let checkedVectors = 0; +for (const vector of vectors.json) { + if (vector.query) { + continue; // the vitest suite covers query normalisation; the crypto is the same call + } + const [, version, ...rest] = vector.path.split('/'); + const signature = generateHmacSignature(vectors.secret, vector.method, version!, rest.join('/'), vector.body); + check(signature === vector.expected, `HMAC vector "${vector.name}"`); + checkedVectors++; +} +check(checkedVectors > 0, 'no HMAC vector was checked'); + +// 2. A signed create against a local stand-in for the API. +const seen: Array<{ apiKey: string | null; sdk: string | null; signatureValid: boolean }> = []; + +const server = Deno.serve({ hostname: '127.0.0.1', port: 0, onListen: () => {} }, async (request) => { + const url = new URL(request.url); + const body = await request.text(); + const [, version, ...rest] = url.pathname.split('/'); + const expected = generateHmacSignature(SECRET_KEY, request.method, version!, rest.join('/'), body); + + seen.push({ + apiKey: request.headers.get('x-api-key'), + sdk: request.headers.get('x-proofage-sdk'), + signatureValid: request.headers.get('x-hmac-signature') === expected, + }); + + if (request.method !== 'POST' || url.pathname !== '/v1/verifications') { + return new Response(JSON.stringify({ message: 'Not found' }), { status: 404 }); + } + + const payload = JSON.parse(body) as { external_id: string }; + + return Response.json( + { + id: '550e8400-e29b-41d4-a716-446655440000', + external_id: payload.external_id, + status: 'created', + url: 'https://idv.proofage.net/q/AB12cd34Ef', + }, + { status: 201, headers: { Date: new Date().toUTCString() } }, + ); +}); + +const baseUrl = `http://127.0.0.1:${server.addr.port}`; + +try { + // Keys passed explicitly, as the docs show for Deno. + const explicit = new ProofAgeClient({ apiKey: API_KEY, secretKey: SECRET_KEY, baseUrl }); + const created = await explicit.verifications().create({ external_id: 'user_1' }); + check(created?.url === 'https://idv.proofage.net/q/AB12cd34Ef', 'create returned the verification url'); + + // Keys read from the environment, as on Node. + Deno.env.set('PROOFAGE_API_KEY', API_KEY); + Deno.env.set('PROOFAGE_SECRET_KEY', SECRET_KEY); + Deno.env.set('PROOFAGE_BASE_URL', `${baseUrl}/v1`); // a trailing /v1 is accepted + const fromEnv = new ProofAgeClient(); + const createdFromEnv = await fromEnv.verifications().create({ external_id: 'user_2' }); + check(createdFromEnv?.external_id === 'user_2', 'create with keys from the environment'); + + check(seen.length === 2, `expected 2 requests, the stand-in saw ${seen.length}`); + for (const request of seen) { + check(request.apiKey === API_KEY, 'X-API-Key is the public key'); + check(request.signatureValid, 'X-HMAC-Signature matches the request'); + check(request.sdk?.startsWith(`node/${SDK_VERSION}`), `X-ProofAge-Sdk names node/${SDK_VERSION}`); + } +} finally { + await server.shutdown(); +} + +// 3. Webhook verification. +const delivery = { event: 'verification.updated', data: { id: '550e8400-e29b-41d4-a716-446655440000', status: 'approved' } }; +const timestamp = Math.floor(Date.now() / 1000); +const deliverySignature = generateWebhookSignature(delivery, SECRET_KEY, timestamp); + +const received: unknown[] = []; +const handler = webhookHandler((payload) => { + received.push(payload); +}, { apiKey: API_KEY, secretKey: SECRET_KEY }); + +const webhookRequest = (signature: string) => + new Request('http://localhost/webhooks/proofage', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Auth-Client': API_KEY, + 'X-HMAC-Signature': signature, + 'X-Timestamp': String(timestamp), + }, + body: JSON.stringify(delivery), + }); + +const accepted = await handler(webhookRequest(deliverySignature)); +check(accepted.status === 200, `a signed webhook is accepted (got ${accepted.status})`); +check(received.length === 1, 'the handler received the payload'); + +const refused = await handler(webhookRequest('0'.repeat(64))); +check(refused.status === 401, `a tampered webhook is refused (got ${refused.status})`); +check(received.length === 1, 'the handler did not run for a tampered webhook'); + +console.log(`@proofage/node ${SDK_VERSION} works on Deno ${Deno.version.deno}: ${checkedVectors} HMAC vectors, signed create, webhook handler.`); diff --git a/scripts/deno-smoke.sh b/scripts/deno-smoke.sh new file mode 100755 index 0000000..5606acc --- /dev/null +++ b/scripts/deno-smoke.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Packs the SDK and runs deno/smoke.ts against the tarball, the way a Deno or Supabase +# Edge Function project loads it (npm package code from node_modules, not the sources). +# Needs node/npm (to build and pack) and deno on PATH. Usage: scripts/deno-smoke.sh +set -euo pipefail + +root="$(cd "$(dirname "$0")/.." && pwd)" +cd "$root" + +npm run build >/dev/null +tarball="$root/$(npm pack --silent | tail -n 1)" +trap 'rm -f "$tarball"; rm -rf "$root/deno/node_modules"' EXIT + +(cd deno && npm install --no-save --no-package-lock --silent "$tarball") + +# Run from deno/ so Deno resolves @proofage/node from deno/node_modules, not the repo itself. +cd deno +deno run --allow-net=127.0.0.1 --allow-env --allow-read --no-prompt smoke.ts