Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142import assert from "node:assert/strict";import { createHash } from "node:crypto";import { resolve } from "node:path";import { test } from "node:test";import { build } from "esbuild";import { Miniflare, convertV4MiniflareOptions } from "miniflare";
test( "deployment adapter uses native Workers fetch for origin and content", { timeout: 30_000 }, async (t) => { const accountId = "a".repeat(32); const workerName = "flarebot-network-fixture"; const module = "export default { fetch() { return new Response('fixture'); } };"; const artifact = { files: [ { path: "worker/index.js", size: Buffer.byteLength(module), sha256: createHash("sha256").update(module).digest("hex"), }, ], }; const bundled = await build({ stdin: { contents: ` import { Deployment } from './control-plane/deployment.ts'; import { runDeploymentEffect } from './control-plane/workflow-boundary.ts'; export default { async fetch(request, env) { // Deliberately omit the injected network argument: this must exercise // the receiver-sensitive native Workers fetch, not a JS replacement. const api = new Deployment('fixture-token', JSON.parse(env.RECORD)); try { if (new URL(request.url).pathname === '/origin') { return Response.json({ origin: await runDeploymentEffect(api.worker.origin()) }); } await runDeploymentEffect(api.worker.verifyContent(JSON.parse(env.ARTIFACT))); return Response.json({ verified: true }); } catch (error) { return Response.json({ error: error.message }, { status: 500 }); } }}; `, resolveDir: resolve("."), loader: "js", }, bundle: true, format: "esm", platform: "browser", write: false, }); const requests = []; let deployedModule = module; const mf = new Miniflare( convertV4MiniflareOptions({ name: "flarebot-deployment-network-test", modules: true, script: bundled.outputFiles[0].text, compatibilityDate: "2026-09-04", compatibilityFlags: ["nodejs_compat"], bindings: { RECORD: JSON.stringify({ accountId, resources: { workerName } }), ARTIFACT: JSON.stringify(artifact), }, // Every outbound request is intercepted outside the Worker. The Worker // still calls native fetch and never receives a replacement function. outboundService(request) { assert.equal(request.method, "GET"); assert.equal( request.headers.get("Authorization"), "Bearer fixture-token", ); const url = new URL(request.url); assert.equal(url.origin, "https://api.cloudflare.com"); requests.push(url.pathname); if ( url.pathname === `/client/v4/accounts/${accountId}/workers/subdomain` ) { return Response.json({ success: true, result: { subdomain: "fixture" }, }); } assert.equal( url.pathname, `/client/v4/accounts/${accountId}/workers/scripts/${workerName}/content/v2`, ); const form = new FormData(); form.append( "index.js", new Blob([deployedModule], { type: "application/javascript+module", }), "index.js", ); return new Response(form, { headers: { "cf-entrypoint": "index.js" }, }); }, }), ); try { await t.test( "resolves the customer origin through native fetch", async () => { const before = requests.length; const response = await mf.dispatchFetch("http://localhost/origin"); const result = await response.json(); assert.equal(response.status, 200, JSON.stringify(result)); assert.deepEqual(result, { origin: `https://${workerName}.fixture.workers.dev`, }); assert.equal(requests.length, before + 1); }, ); await t.test( "verifies the actual multipart module bytes through native fetch", async () => { const before = requests.length; const response = await mf.dispatchFetch("http://localhost/content"); const result = await response.json(); assert.equal(response.status, 200, JSON.stringify(result)); assert.deepEqual(result, { verified: true }); assert.equal(requests.length, before + 1); }, ); await t.test("rejects changed deployed module bytes", async () => { deployedModule = module.replace("fixture", "changed"); const before = requests.length; const response = await mf.dispatchFetch("http://localhost/content"); assert.equal(response.status, 500); assert.deepEqual(await response.json(), { error: "resource_conflict" }); assert.equal(requests.length, before + 1); }); } finally { await mf.dispose(); } },);