wrangler 0.0.0-e3278fa → 0.0.0-e33294b0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -4
- package/bin/wrangler.js +170 -62
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +1499 -34
- package/package.json +193 -153
- package/src/__tests__/access.test.ts +25 -0
- package/src/__tests__/api-dev.test.ts +238 -0
- package/src/__tests__/api-devregistry.test.js +64 -0
- package/src/__tests__/api.test.ts +80 -0
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +30 -24
- package/src/__tests__/configuration.test.ts +4222 -3397
- package/src/__tests__/d1/d1.test.ts +82 -0
- package/src/__tests__/d1/execute.test.ts +64 -0
- package/src/__tests__/d1/migrate.test.ts +155 -0
- package/src/__tests__/d1/splitter.test.ts +255 -0
- package/src/__tests__/delete.test.ts +266 -0
- package/src/__tests__/deployments.test.ts +129 -0
- package/src/__tests__/dev.test.tsx +1560 -956
- package/src/__tests__/generate.test.ts +238 -0
- package/src/__tests__/get-host-from-url.test.ts +16 -0
- package/src/__tests__/guess-worker-format.test.ts +68 -68
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/end-event-loop.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +24 -24
- package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-console.ts +58 -39
- package/src/__tests__/helpers/mock-dialogs.ts +88 -79
- package/src/__tests__/helpers/mock-get-pages-upload-token.ts +25 -0
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +11 -0
- package/src/__tests__/helpers/mock-http-server.ts +30 -30
- package/src/__tests__/helpers/mock-istty.ts +65 -18
- package/src/__tests__/helpers/mock-known-routes.ts +12 -0
- package/src/__tests__/helpers/mock-kv.ts +41 -28
- package/src/__tests__/helpers/mock-oauth-flow.ts +241 -233
- package/src/__tests__/helpers/mock-process.ts +34 -0
- package/src/__tests__/helpers/mock-set-timeout.ts +16 -0
- package/src/__tests__/helpers/mock-stdin.ts +82 -77
- package/src/__tests__/helpers/mock-web-socket.ts +21 -21
- package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
- package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
- package/src/__tests__/helpers/msw/handlers/deployments.ts +93 -0
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +81 -0
- package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
- package/src/__tests__/helpers/msw/handlers/r2.ts +60 -0
- package/src/__tests__/helpers/msw/handlers/script.ts +56 -0
- package/src/__tests__/helpers/msw/handlers/user.ts +52 -0
- package/src/__tests__/helpers/msw/handlers/zones.ts +20 -0
- package/src/__tests__/helpers/msw/index.ts +50 -0
- package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
- package/src/__tests__/helpers/run-in-tmp.ts +27 -28
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/string-dynamic-values-matcher.ts +28 -0
- package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
- package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
- package/src/__tests__/helpers/write-worker-source.ts +16 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +142 -109
- package/src/__tests__/index.test.ts +254 -236
- package/src/__tests__/init.test.ts +3135 -1608
- package/src/__tests__/jest.setup.ts +148 -50
- package/src/__tests__/kv.test.ts +1789 -1471
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/logout.test.ts +47 -0
- package/src/__tests__/metrics.test.ts +493 -0
- package/src/__tests__/middleware.scheduled.test.ts +145 -0
- package/src/__tests__/middleware.test.ts +815 -0
- package/src/__tests__/mtls-certificates.test.ts +585 -0
- package/src/__tests__/package-manager.test.ts +303 -303
- package/src/__tests__/pages/deployment-list.test.ts +78 -0
- package/src/__tests__/pages/functions-build.test.ts +402 -0
- package/src/__tests__/pages/pages.test.ts +81 -0
- package/src/__tests__/pages/project-create.test.ts +63 -0
- package/src/__tests__/pages/project-list.test.ts +108 -0
- package/src/__tests__/pages/project-upload.test.ts +481 -0
- package/src/__tests__/pages/publish.test.ts +2745 -0
- package/src/__tests__/pages-deployment-tail.test.ts +955 -0
- package/src/__tests__/parse.test.ts +358 -252
- package/src/__tests__/paths.test.ts +39 -0
- package/src/__tests__/publish.test.ts +8023 -5629
- package/src/__tests__/pubsub.test.ts +493 -0
- package/src/__tests__/queues.test.ts +529 -0
- package/src/__tests__/r2.test.ts +369 -228
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +681 -469
- package/src/__tests__/tail.test.ts +835 -567
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/tsconfig-sanity.ts +12 -0
- package/src/__tests__/tsconfig.json +8 -0
- package/src/__tests__/tsconfig.tsbuildinfo +1 -0
- package/src/__tests__/type-generation.test.ts +233 -0
- package/src/__tests__/user.test.ts +107 -120
- package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
- package/src/__tests__/validate-dev-props.test.ts +56 -0
- package/src/__tests__/version.test.ts +35 -0
- package/src/__tests__/whoami.test.tsx +165 -132
- package/src/__tests__/worker-namespace.test.ts +339 -0
- package/src/abort.d.ts +1 -1
- package/src/api/dev.ts +328 -0
- package/src/api/index.ts +11 -0
- package/src/api/mtls-certificate.ts +148 -0
- package/src/api/pages/create-worker-bundle-contents.ts +75 -0
- package/src/api/pages/index.ts +5 -0
- package/src/api/pages/publish.tsx +368 -0
- package/src/bundle-reporter.ts +68 -0
- package/src/bundle.ts +839 -160
- package/src/cfetch/index.ts +120 -83
- package/src/cfetch/internal.ts +208 -92
- package/src/cli.ts +23 -8
- package/src/config/config.ts +244 -196
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +523 -357
- package/src/config/index.ts +346 -196
- package/src/config/validation-helpers.ts +379 -366
- package/src/config/validation.ts +2026 -1367
- package/src/config-cache.ts +81 -41
- package/src/create-worker-preview.ts +239 -142
- package/src/create-worker-upload-form.ts +318 -238
- package/src/d1/backups.tsx +215 -0
- package/src/d1/constants.ts +2 -0
- package/src/d1/create.tsx +64 -0
- package/src/d1/delete.ts +53 -0
- package/src/d1/execute.tsx +354 -0
- package/src/d1/formatTimeAgo.ts +14 -0
- package/src/d1/index.ts +100 -0
- package/src/d1/list.tsx +60 -0
- package/src/d1/migrations/apply.tsx +206 -0
- package/src/d1/migrations/create.tsx +76 -0
- package/src/d1/migrations/helpers.ts +172 -0
- package/src/d1/migrations/index.ts +3 -0
- package/src/d1/migrations/list.tsx +92 -0
- package/src/d1/migrations/options.ts +23 -0
- package/src/d1/options.ts +22 -0
- package/src/d1/splitter.ts +161 -0
- package/src/d1/types.ts +25 -0
- package/src/d1/utils.ts +49 -0
- package/src/delete.ts +100 -0
- package/src/deployments.ts +116 -0
- package/src/deprecated/index.ts +144 -0
- package/src/dev/dev-vars.ts +20 -17
- package/src/dev/dev.tsx +533 -345
- package/src/dev/get-local-persistence-path.ts +31 -0
- package/src/dev/local.tsx +903 -302
- package/src/dev/remote.tsx +620 -225
- package/src/dev/start-server.ts +538 -0
- package/src/dev/use-esbuild.ts +184 -97
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.ts +201 -0
- package/src/dev.tsx +914 -0
- package/src/dialogs.ts +136 -0
- package/src/dispatch-namespace.ts +211 -0
- package/src/docs/index.ts +97 -0
- package/src/durable.ts +87 -87
- package/src/entry.ts +236 -230
- package/src/environment-variables/factory.ts +88 -0
- package/src/environment-variables/misc-variables.ts +30 -0
- package/src/errors.ts +6 -6
- package/src/generate/index.ts +298 -0
- package/src/git-client.ts +135 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +84 -80
- package/src/index.ts +705 -0
- package/src/init.ts +1022 -0
- package/src/inspect.ts +698 -593
- package/src/intl-polyfill.d.ts +123 -123
- package/src/is-ci.ts +14 -0
- package/src/is-interactive.ts +16 -0
- package/src/jest.d.ts +4 -0
- package/src/kv/helpers.ts +430 -0
- package/src/kv/index.ts +594 -0
- package/src/logger.ts +80 -42
- package/src/metrics/index.ts +5 -0
- package/src/metrics/metrics-config.ts +239 -0
- package/src/metrics/metrics-dispatcher.ts +96 -0
- package/src/metrics/metrics-usage-headers.ts +24 -0
- package/src/metrics/send-event.ts +98 -0
- package/src/miniflare-cli/assets.ts +250 -0
- package/src/miniflare-cli/index.ts +202 -33
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/miniflare-cli/tsconfig.json +9 -0
- package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
- package/src/miniflare-cli/types.ts +11 -0
- package/src/module-collection.ts +221 -212
- package/src/mtls-certificate/cli.ts +155 -0
- package/src/open-in-browser.ts +4 -6
- package/src/package-manager.ts +173 -128
- package/src/pages/build.ts +232 -0
- package/src/pages/buildFunctions.ts +130 -0
- package/src/pages/constants.ts +18 -0
- package/src/pages/deployment-tails.ts +281 -0
- package/src/pages/deployments.tsx +86 -0
- package/src/pages/dev.ts +716 -0
- package/src/pages/errors.ts +66 -0
- package/src/pages/functions/buildPlugin.ts +90 -0
- package/src/pages/functions/buildWorker.ts +288 -0
- package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes-consolidation.test.ts +250 -0
- package/src/pages/functions/routes-consolidation.ts +73 -0
- package/src/pages/functions/routes-transformation.test.ts +282 -0
- package/src/pages/functions/routes-transformation.ts +115 -0
- package/src/pages/functions/routes-validation.test.ts +403 -0
- package/src/pages/functions/routes-validation.ts +202 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/functions/tsconfig.json +8 -0
- package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
- package/src/pages/functions.ts +86 -0
- package/src/pages/hash.ts +13 -0
- package/src/pages/index.ts +102 -0
- package/src/pages/projects.tsx +162 -0
- package/src/pages/prompt-select-project.tsx +31 -0
- package/src/pages/publish.tsx +276 -0
- package/src/pages/types.ts +46 -0
- package/src/pages/upload.tsx +449 -0
- package/src/pages/utils.ts +23 -0
- package/src/parse.ts +232 -126
- package/src/paths.ts +53 -8
- package/src/proxy.ts +623 -407
- package/src/publish/index.ts +274 -0
- package/src/publish/publish.ts +1065 -0
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.ts +623 -0
- package/src/queues/cli/commands/consumer/add.ts +65 -0
- package/src/queues/cli/commands/consumer/index.ts +19 -0
- package/src/queues/cli/commands/consumer/remove.ts +31 -0
- package/src/queues/cli/commands/create.ts +25 -0
- package/src/queues/cli/commands/delete.ts +26 -0
- package/src/queues/cli/commands/index.ts +35 -0
- package/src/queues/cli/commands/list.ts +25 -0
- package/src/queues/client.ts +135 -0
- package/src/queues/utils.ts +18 -0
- package/src/r2/constants.ts +4 -0
- package/src/r2/helpers.ts +131 -0
- package/src/r2/index.ts +289 -0
- package/src/routes.ts +140 -0
- package/src/secret/index.ts +375 -0
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.ts +372 -0
- package/src/tail/createTail.ts +406 -0
- package/src/tail/filters.ts +109 -111
- package/src/tail/index.ts +208 -296
- package/src/tail/printing.ts +101 -46
- package/src/tsconfig-sanity.ts +16 -0
- package/src/type-generation.ts +181 -0
- package/src/update-check.ts +11 -11
- package/src/user/access.ts +68 -0
- package/src/user/auth-variables.ts +113 -0
- package/src/user/choose-account.tsx +39 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +2 -0
- package/src/user/user.ts +1233 -0
- package/src/utils/collectKeyValues.ts +14 -0
- package/src/whoami.ts +135 -0
- package/src/worker.ts +183 -107
- package/src/yargs-types.ts +37 -0
- package/src/zones.ts +158 -40
- package/templates/__tests__/pages-dev-util.test.ts +128 -0
- package/templates/__tests__/tsconfig-sanity.ts +12 -0
- package/templates/__tests__/tsconfig.json +8 -0
- package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
- package/templates/checked-fetch.js +23 -0
- package/templates/d1-beta-facade.js +241 -0
- package/templates/facade.d.ts +14 -0
- package/templates/first-party-worker-module-facade.ts +19 -0
- package/templates/format-dev-errors.ts +34 -0
- package/templates/init-tests/test-jest-new-worker.js +23 -0
- package/templates/init-tests/test-vitest-new-worker.js +24 -0
- package/templates/init-tests/test-vitest-new-worker.ts +25 -0
- package/templates/middleware/common.ts +62 -0
- package/templates/middleware/loader-modules.ts +84 -0
- package/templates/middleware/loader-sw.ts +233 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +15 -0
- package/templates/new-worker-scheduled.js +3 -3
- package/templates/new-worker-scheduled.ts +15 -15
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +18 -15
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-dev-pipeline.ts +31 -0
- package/templates/pages-dev-util.ts +52 -0
- package/templates/pages-shim.ts +12 -0
- package/templates/pages-template-plugin.ts +176 -0
- package/templates/pages-template-worker.ts +189 -0
- package/templates/serve-static-assets.ts +66 -0
- package/templates/service-bindings-module-facade.js +55 -0
- package/templates/service-bindings-sw-facade.js +42 -0
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +106 -0
- package/templates/tsconfig.json +6 -103
- package/templates/tsconfig.tsbuildinfo +1 -0
- package/wrangler-dist/cli.d.ts +1108 -0
- package/wrangler-dist/cli.js +109524 -90537
- package/wrangler-dist/wasm-sync.wasm +0 -0
- package/pages/functions/buildPlugin.ts +0 -105
- package/pages/functions/buildWorker.ts +0 -151
- package/pages/functions/filepath-routing.ts +0 -189
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -156
- package/pages/functions/template-plugin.ts +0 -147
- package/pages/functions/template-worker.ts +0 -143
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/__tests__/helpers/mock-cfetch.ts +0 -211
- package/src/__tests__/pages.test.ts +0 -754
- package/src/dialogs.tsx +0 -135
- package/src/environment-variables.ts +0 -35
- package/src/index.tsx +0 -2846
- package/src/kv.ts +0 -408
- package/src/miniflare-cli/enum-keys.ts +0 -17
- package/src/pages.tsx +0 -2093
- package/src/publish.ts +0 -716
- package/src/r2.ts +0 -50
- package/src/sites.tsx +0 -326
- package/src/user.tsx +0 -1214
- package/src/whoami.tsx +0 -71
- package/templates/static-asset-facade.js +0 -43
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { createMetadataObject } from "@cloudflare/pages-shared/metadata-generator/createMetadataObject";
|
|
4
|
+
import { parseHeaders } from "@cloudflare/pages-shared/metadata-generator/parseHeaders";
|
|
5
|
+
import { parseRedirects } from "@cloudflare/pages-shared/metadata-generator/parseRedirects";
|
|
6
|
+
import { watch } from "chokidar";
|
|
7
|
+
import { getType } from "mime";
|
|
8
|
+
import { hashFile } from "../pages/hash";
|
|
9
|
+
import type { Metadata } from "@cloudflare/pages-shared/asset-server/metadata";
|
|
10
|
+
import type {
|
|
11
|
+
ParsedRedirects,
|
|
12
|
+
ParsedHeaders,
|
|
13
|
+
} from "@cloudflare/pages-shared/metadata-generator/types";
|
|
14
|
+
import type {
|
|
15
|
+
RequestInfo as TreRequestInfo,
|
|
16
|
+
RequestInit as TreRequestInit,
|
|
17
|
+
} from "@miniflare/tre";
|
|
18
|
+
|
|
19
|
+
interface Logger {
|
|
20
|
+
log: (message: string) => void;
|
|
21
|
+
warn: (message: string) => void;
|
|
22
|
+
error: (error: Error) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Options {
|
|
26
|
+
log: Logger;
|
|
27
|
+
proxyPort?: number;
|
|
28
|
+
directory?: string;
|
|
29
|
+
tre: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default async function generateASSETSBinding(options: Options) {
|
|
33
|
+
const assetsFetch =
|
|
34
|
+
options.directory !== undefined
|
|
35
|
+
? await generateAssetsFetch(options.directory, options.log, options.tre)
|
|
36
|
+
: invalidAssetsFetch;
|
|
37
|
+
|
|
38
|
+
const miniflare = options.tre
|
|
39
|
+
? await import("@miniflare/tre")
|
|
40
|
+
: await import("@miniflare/core");
|
|
41
|
+
|
|
42
|
+
const Request = miniflare.Request;
|
|
43
|
+
const Response = miniflare.Response;
|
|
44
|
+
// WebSockets won't work with `--experimental-local` until we expose something like `upgradingFetch` from `@miniflare/tre`.
|
|
45
|
+
const fetch = (
|
|
46
|
+
options.tre
|
|
47
|
+
? miniflare.fetch
|
|
48
|
+
: (await import("@miniflare/web-sockets")).upgradingFetch
|
|
49
|
+
) as (request: Request) => Promise<Response>;
|
|
50
|
+
|
|
51
|
+
return async function (miniflareRequest: Request) {
|
|
52
|
+
if (options.proxyPort) {
|
|
53
|
+
try {
|
|
54
|
+
const url = new URL(miniflareRequest.url);
|
|
55
|
+
url.host = `localhost:${options.proxyPort}`;
|
|
56
|
+
const proxyRequest = new Request(url, miniflareRequest);
|
|
57
|
+
if (proxyRequest.headers.get("Upgrade") === "websocket") {
|
|
58
|
+
proxyRequest.headers.delete("Sec-WebSocket-Accept");
|
|
59
|
+
proxyRequest.headers.delete("Sec-WebSocket-Key");
|
|
60
|
+
}
|
|
61
|
+
return await fetch(proxyRequest as Request);
|
|
62
|
+
} catch (thrown) {
|
|
63
|
+
options.log.error(new Error(`Could not proxy request: ${thrown}`));
|
|
64
|
+
|
|
65
|
+
// TODO: Pretty error page
|
|
66
|
+
return new Response(`[wrangler] Could not proxy request: ${thrown}`, {
|
|
67
|
+
status: 502,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
try {
|
|
72
|
+
return await assetsFetch(miniflareRequest);
|
|
73
|
+
} catch (thrown) {
|
|
74
|
+
options.log.error(new Error(`Could not serve static asset: ${thrown}`));
|
|
75
|
+
|
|
76
|
+
// TODO: Pretty error page
|
|
77
|
+
return new Response(
|
|
78
|
+
`[wrangler] Could not serve static asset: ${thrown}`,
|
|
79
|
+
{ status: 502 }
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function generateAssetsFetch(
|
|
87
|
+
directory: string,
|
|
88
|
+
log: Logger,
|
|
89
|
+
tre: boolean
|
|
90
|
+
): Promise<typeof fetch> {
|
|
91
|
+
// Defer importing miniflare until we really need it
|
|
92
|
+
|
|
93
|
+
// NOTE: These dynamic imports bring in `global` type augmentations from
|
|
94
|
+
// `@cloudflare/pages-shared/environment-polyfills/types.ts`, allowing us to
|
|
95
|
+
// use `fetch`, `Headers`, `Request` and `Response` as globals in this file
|
|
96
|
+
// and the *entire* `miniflare-cli` TypeScript project.
|
|
97
|
+
const polyfill = tre
|
|
98
|
+
? (
|
|
99
|
+
await import(
|
|
100
|
+
"@cloudflare/pages-shared/environment-polyfills/miniflare-tre"
|
|
101
|
+
)
|
|
102
|
+
).default
|
|
103
|
+
: (await import("@cloudflare/pages-shared/environment-polyfills/miniflare"))
|
|
104
|
+
.default;
|
|
105
|
+
|
|
106
|
+
await polyfill();
|
|
107
|
+
|
|
108
|
+
const miniflare = tre
|
|
109
|
+
? await import("@miniflare/tre")
|
|
110
|
+
: await import("@miniflare/core");
|
|
111
|
+
const Request = miniflare.Request;
|
|
112
|
+
|
|
113
|
+
const { generateHandler, parseQualityWeightedList } = await import(
|
|
114
|
+
"@cloudflare/pages-shared/asset-server/handler"
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const headersFile = join(directory, "_headers");
|
|
118
|
+
const redirectsFile = join(directory, "_redirects");
|
|
119
|
+
const workerFile = join(directory, "_worker.js");
|
|
120
|
+
|
|
121
|
+
const ignoredFiles = [headersFile, redirectsFile, workerFile];
|
|
122
|
+
|
|
123
|
+
let redirects: ParsedRedirects | undefined;
|
|
124
|
+
if (existsSync(redirectsFile)) {
|
|
125
|
+
const contents = readFileSync(redirectsFile, "utf-8");
|
|
126
|
+
redirects = parseRedirects(contents);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let headers: ParsedHeaders | undefined;
|
|
130
|
+
if (existsSync(headersFile)) {
|
|
131
|
+
const contents = readFileSync(headersFile, "utf-8");
|
|
132
|
+
headers = parseHeaders(contents);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let metadata = createMetadataObject({
|
|
136
|
+
redirects,
|
|
137
|
+
headers,
|
|
138
|
+
logger: log.warn.bind(log),
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
watch([headersFile, redirectsFile], { persistent: true }).on(
|
|
142
|
+
"change",
|
|
143
|
+
(path) => {
|
|
144
|
+
switch (path) {
|
|
145
|
+
case headersFile: {
|
|
146
|
+
log.log("_headers modified. Re-evaluating...");
|
|
147
|
+
const contents = readFileSync(headersFile).toString();
|
|
148
|
+
headers = parseHeaders(contents);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case redirectsFile: {
|
|
152
|
+
log.log("_redirects modified. Re-evaluating...");
|
|
153
|
+
const contents = readFileSync(redirectsFile).toString();
|
|
154
|
+
redirects = parseRedirects(contents);
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
metadata = createMetadataObject({
|
|
160
|
+
redirects,
|
|
161
|
+
headers,
|
|
162
|
+
logger: log.warn,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const generateResponse = async (request: Request) => {
|
|
168
|
+
const assetKeyEntryMap = new Map<string, string>();
|
|
169
|
+
|
|
170
|
+
return await generateHandler<string>({
|
|
171
|
+
request,
|
|
172
|
+
metadata: metadata as Metadata,
|
|
173
|
+
xServerEnvHeader: "dev",
|
|
174
|
+
logError: console.error,
|
|
175
|
+
findAssetEntryForPath: async (path) => {
|
|
176
|
+
const filepath = join(directory, path);
|
|
177
|
+
|
|
178
|
+
if (
|
|
179
|
+
existsSync(filepath) &&
|
|
180
|
+
lstatSync(filepath).isFile() &&
|
|
181
|
+
!ignoredFiles.includes(filepath)
|
|
182
|
+
) {
|
|
183
|
+
const hash = hashFile(filepath);
|
|
184
|
+
assetKeyEntryMap.set(hash, filepath);
|
|
185
|
+
return hash;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return null;
|
|
189
|
+
},
|
|
190
|
+
getAssetKey: (assetEntry) => {
|
|
191
|
+
return assetEntry;
|
|
192
|
+
},
|
|
193
|
+
negotiateContent: (contentRequest) => {
|
|
194
|
+
let rawAcceptEncoding: string | undefined;
|
|
195
|
+
if (
|
|
196
|
+
contentRequest.cf &&
|
|
197
|
+
"clientAcceptEncoding" in contentRequest.cf &&
|
|
198
|
+
contentRequest.cf.clientAcceptEncoding
|
|
199
|
+
) {
|
|
200
|
+
rawAcceptEncoding = contentRequest.cf.clientAcceptEncoding as string;
|
|
201
|
+
} else {
|
|
202
|
+
rawAcceptEncoding =
|
|
203
|
+
contentRequest.headers.get("Accept-Encoding") || undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const acceptEncoding = parseQualityWeightedList(rawAcceptEncoding);
|
|
207
|
+
|
|
208
|
+
if (
|
|
209
|
+
acceptEncoding["identity"] === 0 ||
|
|
210
|
+
(acceptEncoding["*"] === 0 &&
|
|
211
|
+
acceptEncoding["identity"] === undefined)
|
|
212
|
+
) {
|
|
213
|
+
throw new Error("No acceptable encodings available");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return { encoding: null };
|
|
217
|
+
},
|
|
218
|
+
fetchAsset: async (assetKey) => {
|
|
219
|
+
const filepath = assetKeyEntryMap.get(assetKey);
|
|
220
|
+
if (!filepath) {
|
|
221
|
+
throw new Error(
|
|
222
|
+
"Could not fetch asset. Please file an issue on GitHub (https://github.com/cloudflare/workers-sdk/issues/new/choose) with reproduction steps."
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
const body = readFileSync(filepath) as unknown as ReadableStream;
|
|
226
|
+
|
|
227
|
+
let contentType = getType(filepath) || "application/octet-stream";
|
|
228
|
+
if (
|
|
229
|
+
contentType.startsWith("text/") &&
|
|
230
|
+
!contentType.includes("charset")
|
|
231
|
+
) {
|
|
232
|
+
contentType = `${contentType}; charset=utf-8`;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return { body, contentType };
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
return (async (input: TreRequestInfo, init?: TreRequestInit) => {
|
|
241
|
+
const request = new Request(input, init);
|
|
242
|
+
return await generateResponse(request as unknown as Request);
|
|
243
|
+
}) as typeof fetch;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const invalidAssetsFetch: typeof fetch = () => {
|
|
247
|
+
throw new Error(
|
|
248
|
+
"Trying to fetch assets directly when there is no `directory` option specified."
|
|
249
|
+
);
|
|
250
|
+
};
|
|
@@ -1,41 +1,210 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { fetch } from "@miniflare/core";
|
|
2
|
+
import {
|
|
3
|
+
DurableObjectNamespace,
|
|
4
|
+
DurableObjectStub,
|
|
5
|
+
} from "@miniflare/durable-objects";
|
|
6
|
+
import {
|
|
7
|
+
Log as MiniflareLog,
|
|
8
|
+
LogLevel as MiniflareLogLevel,
|
|
9
|
+
Miniflare,
|
|
10
|
+
Request as MiniflareRequest,
|
|
11
|
+
Response as MiniflareResponse,
|
|
12
|
+
} from "miniflare";
|
|
2
13
|
import yargs from "yargs";
|
|
3
14
|
import { hideBin } from "yargs/helpers";
|
|
4
|
-
import {
|
|
15
|
+
import { FatalError } from "../errors";
|
|
16
|
+
import generateASSETSBinding from "./assets";
|
|
5
17
|
import { getRequestContextCheckOptions } from "./request-context";
|
|
18
|
+
import type { LoggerLevel } from "../logger";
|
|
19
|
+
import type { Options } from "./assets";
|
|
20
|
+
import type { EnablePagesAssetsServiceBindingOptions } from "./types";
|
|
21
|
+
import type { AddressInfo } from "net";
|
|
22
|
+
|
|
23
|
+
// miniflare defines this but importing it throws:
|
|
24
|
+
// Dynamic require of "path" is not supported
|
|
25
|
+
class MiniflareNoOpLog extends MiniflareLog {
|
|
26
|
+
log(): void {}
|
|
27
|
+
|
|
28
|
+
error(message: Error): void {
|
|
29
|
+
throw message;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
6
32
|
|
|
7
33
|
async function main() {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
const args = await yargs(hideBin(process.argv)).help(false).version(false)
|
|
35
|
+
.argv;
|
|
36
|
+
|
|
37
|
+
const requestContextCheckOptions = await getRequestContextCheckOptions();
|
|
38
|
+
const config = {
|
|
39
|
+
...JSON.parse((args._[0] as string) ?? "{}"),
|
|
40
|
+
...requestContextCheckOptions,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
let logLevelString: Uppercase<LoggerLevel> = config.logLevel.toUpperCase();
|
|
44
|
+
if (logLevelString === "LOG") logLevelString = "INFO";
|
|
45
|
+
const logLevel = MiniflareLogLevel[logLevelString];
|
|
46
|
+
|
|
47
|
+
config.log =
|
|
48
|
+
logLevel === MiniflareLogLevel.NONE
|
|
49
|
+
? new MiniflareNoOpLog()
|
|
50
|
+
: new MiniflareLog(logLevel, config.logOptions);
|
|
51
|
+
|
|
52
|
+
if (logLevel === MiniflareLogLevel.DEBUG) {
|
|
53
|
+
console.log("MINIFLARE OPTIONS:\n", JSON.stringify(config, null, 2));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
config.bindings = {
|
|
57
|
+
...config.bindings,
|
|
58
|
+
...(config.externalDurableObjects &&
|
|
59
|
+
Object.fromEntries(
|
|
60
|
+
Object.entries(
|
|
61
|
+
config.externalDurableObjects as Record<
|
|
62
|
+
string,
|
|
63
|
+
{ name: string; host: string; port: number }
|
|
64
|
+
>
|
|
65
|
+
).map(([binding, { name, host, port }]) => {
|
|
66
|
+
const factory = () => {
|
|
67
|
+
throw new FatalError(
|
|
68
|
+
"An external Durable Object instance's state has somehow been attempted to be accessed.",
|
|
69
|
+
1
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
const namespace = new DurableObjectNamespace(name as string, factory);
|
|
73
|
+
namespace.get = (id) => {
|
|
74
|
+
const stub = new DurableObjectStub(factory, id);
|
|
75
|
+
stub.fetch = (...reqArgs) => {
|
|
76
|
+
const requestFromArgs = new MiniflareRequest(...reqArgs);
|
|
77
|
+
const url = new URL(requestFromArgs.url);
|
|
78
|
+
url.host = host;
|
|
79
|
+
if (port !== undefined) url.port = port.toString();
|
|
80
|
+
const request = new MiniflareRequest(
|
|
81
|
+
url.toString(),
|
|
82
|
+
requestFromArgs
|
|
83
|
+
);
|
|
84
|
+
request.headers.set("x-miniflare-durable-object-name", name);
|
|
85
|
+
request.headers.set(
|
|
86
|
+
"x-miniflare-durable-object-id",
|
|
87
|
+
id.toString()
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
return fetch(request);
|
|
91
|
+
};
|
|
92
|
+
return stub;
|
|
93
|
+
};
|
|
94
|
+
return [binding, namespace];
|
|
95
|
+
})
|
|
96
|
+
)),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
let mf: Miniflare | undefined;
|
|
100
|
+
let durableObjectsMf: Miniflare | undefined = undefined;
|
|
101
|
+
let durableObjectsMfPort: number | undefined = undefined;
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
if (args._[1]) {
|
|
105
|
+
const opts: EnablePagesAssetsServiceBindingOptions = JSON.parse(
|
|
106
|
+
args._[1] as string
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
if (isNaN(opts.proxyPort || NaN) && !opts.directory) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
"MiniflareCLIOptions: built in service bindings set to true, but no port or directory provided"
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const options: Options = {
|
|
116
|
+
log: config.log,
|
|
117
|
+
proxyPort: opts.proxyPort,
|
|
118
|
+
directory: opts.directory,
|
|
119
|
+
tre: false,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
config.serviceBindings = {
|
|
123
|
+
...config.serviceBindings,
|
|
124
|
+
ASSETS: await generateASSETSBinding(options),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
mf = new Miniflare(config);
|
|
128
|
+
// Start Miniflare development server
|
|
129
|
+
const mfServer = await mf.startServer();
|
|
130
|
+
const mfPort = (mfServer.address() as AddressInfo).port;
|
|
131
|
+
await mf.startScheduler();
|
|
132
|
+
|
|
133
|
+
const internalDurableObjectClassNames = Object.values(
|
|
134
|
+
config.durableObjects as Record<string, string>
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
if (internalDurableObjectClassNames.length > 0) {
|
|
138
|
+
durableObjectsMf = new Miniflare({
|
|
139
|
+
host: config.host,
|
|
140
|
+
port: 0,
|
|
141
|
+
script: `
|
|
142
|
+
export default {
|
|
143
|
+
fetch(request, env) {
|
|
144
|
+
return env.DO.fetch(request)
|
|
145
|
+
}
|
|
146
|
+
}`,
|
|
147
|
+
serviceBindings: {
|
|
148
|
+
DO: async (request: MiniflareRequest) => {
|
|
149
|
+
request = new MiniflareRequest(request);
|
|
150
|
+
|
|
151
|
+
const name = request.headers.get("x-miniflare-durable-object-name");
|
|
152
|
+
const idString = request.headers.get(
|
|
153
|
+
"x-miniflare-durable-object-id"
|
|
154
|
+
);
|
|
155
|
+
request.headers.delete("x-miniflare-durable-object-name");
|
|
156
|
+
request.headers.delete("x-miniflare-durable-object-id");
|
|
157
|
+
|
|
158
|
+
if (!name || !idString) {
|
|
159
|
+
return new MiniflareResponse(
|
|
160
|
+
"[durable-object-proxy-err] Missing `x-miniflare-durable-object-name` or `x-miniflare-durable-object-id` headers.",
|
|
161
|
+
{ status: 400 }
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const namespace = await mf?.getDurableObjectNamespace(name);
|
|
166
|
+
const id = namespace?.idFromString(idString);
|
|
167
|
+
|
|
168
|
+
if (!id) {
|
|
169
|
+
return new MiniflareResponse(
|
|
170
|
+
"[durable-object-proxy-err] Could not generate an ID. Possibly due to a mismatched DO name and ID?",
|
|
171
|
+
{ status: 500 }
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const stub = namespace?.get(id);
|
|
176
|
+
|
|
177
|
+
if (!stub) {
|
|
178
|
+
return new MiniflareResponse(
|
|
179
|
+
"[durable-object-proxy-err] Could not generate a stub. Possibly due to a mismatched DO name and ID?",
|
|
180
|
+
{ status: 500 }
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return stub.fetch(request);
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
modules: true,
|
|
188
|
+
});
|
|
189
|
+
const server = await durableObjectsMf.startServer();
|
|
190
|
+
durableObjectsMfPort = (server.address() as AddressInfo).port;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
process.send &&
|
|
194
|
+
process.send(
|
|
195
|
+
JSON.stringify({
|
|
196
|
+
port: mfPort,
|
|
197
|
+
ready: true,
|
|
198
|
+
durableObjectsPort: durableObjectsMfPort,
|
|
199
|
+
})
|
|
200
|
+
);
|
|
201
|
+
} catch (e) {
|
|
202
|
+
mf?.log.error(e as Error);
|
|
203
|
+
process.exitCode = 1;
|
|
204
|
+
// Unmount any mounted workers
|
|
205
|
+
await mf?.dispose();
|
|
206
|
+
await durableObjectsMf?.dispose();
|
|
207
|
+
}
|
|
39
208
|
}
|
|
40
209
|
|
|
41
210
|
await main();
|
|
@@ -16,25 +16,25 @@ import type { MiniflareOptions } from "miniflare";
|
|
|
16
16
|
* that indicate to miniflare whether to run the checks or not.
|
|
17
17
|
*/
|
|
18
18
|
export const getRequestContextCheckOptions = async (): Promise<
|
|
19
|
-
|
|
19
|
+
Pick<MiniflareOptions, "globalAsyncIO" | "globalTimers" | "globalRandom">
|
|
20
20
|
> => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
// check that there's an implementation of AsyncLocalStorage
|
|
22
|
+
let hasAsyncLocalStorage = true;
|
|
23
|
+
try {
|
|
24
|
+
// ripped from the example here https://nodejs.org/api/async_context.html#class-asynclocalstorage
|
|
25
|
+
const { AsyncLocalStorage } = await import("node:async_hooks");
|
|
26
|
+
const storage = new AsyncLocalStorage<string>();
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
hasAsyncLocalStorage = storage.run("some-value", () => {
|
|
29
|
+
return storage.getStore() === "some-value";
|
|
30
|
+
});
|
|
31
|
+
} catch (e) {
|
|
32
|
+
hasAsyncLocalStorage = false;
|
|
33
|
+
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
return {
|
|
36
|
+
globalAsyncIO: hasAsyncLocalStorage,
|
|
37
|
+
globalRandom: hasAsyncLocalStorage,
|
|
38
|
+
globalTimers: hasAsyncLocalStorage,
|
|
39
|
+
};
|
|
40
40
|
};
|