wrangler 0.0.0-e978986 → 0.0.0-e98613f8
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 +23 -3
- package/bin/wrangler.js +162 -62
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +1456 -21
- package/package.json +191 -152
- package/src/__tests__/access.test.ts +25 -0
- package/src/__tests__/api-dev.test.ts +209 -0
- package/src/__tests__/api-devregistry.test.js +56 -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 +3970 -3363
- package/src/__tests__/d1.test.ts +80 -0
- package/src/__tests__/delete.test.ts +263 -0
- package/src/__tests__/deployments.test.ts +91 -0
- package/src/__tests__/dev.test.tsx +1524 -951
- package/src/__tests__/dialogs.test.tsx +40 -0
- package/src/__tests__/generate.test.ts +235 -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/mock-account-id.ts +24 -24
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-cfetch.ts +216 -94
- package/src/__tests__/helpers/mock-console.ts +58 -39
- package/src/__tests__/helpers/mock-dialogs.ts +94 -71
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -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 +7 -0
- package/src/__tests__/helpers/mock-kv.ts +27 -27
- package/src/__tests__/helpers/mock-oauth-flow.ts +229 -228
- package/src/__tests__/helpers/mock-process.ts +39 -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/handlers/access.ts +13 -0
- package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
- package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
- package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
- package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
- package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
- package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
- package/src/__tests__/helpers/msw/index.ts +21 -0
- package/src/__tests__/helpers/run-in-tmp.ts +27 -28
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- 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 +241 -233
- package/src/__tests__/init.test.ts +2884 -1536
- package/src/__tests__/jest.setup.ts +159 -34
- package/src/__tests__/kv.test.ts +1660 -1466
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/logout.test.ts +47 -0
- package/src/__tests__/metrics.test.ts +450 -0
- package/src/__tests__/middleware.scheduled.test.ts +135 -0
- package/src/__tests__/middleware.test.ts +726 -0
- package/src/__tests__/package-manager.test.ts +303 -303
- package/src/__tests__/pages-deployment-tail.test.ts +820 -0
- package/src/__tests__/pages.test.ts +2423 -638
- package/src/__tests__/parse.test.ts +252 -252
- package/src/__tests__/paths.test.ts +39 -0
- package/src/__tests__/publish.test.ts +7516 -5351
- package/src/__tests__/pubsub.test.ts +370 -0
- package/src/__tests__/queues.test.ts +522 -0
- package/src/__tests__/r2.test.ts +388 -228
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +622 -459
- package/src/__tests__/tail.test.ts +776 -567
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/type-generation.test.ts +226 -0
- package/src/__tests__/user.test.ts +117 -119
- 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 +264 -91
- package/src/__tests__/worker-namespace.test.ts +326 -0
- package/src/abort.d.ts +1 -1
- package/src/api/dev.ts +210 -0
- package/src/api/index.ts +2 -0
- package/src/bundle-reporter.tsx +68 -0
- package/src/bundle.ts +785 -162
- package/src/cfetch/index.ts +120 -81
- package/src/cfetch/internal.ts +209 -91
- package/src/cli.ts +23 -8
- package/src/config/config.ts +236 -195
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +498 -357
- package/src/config/index.ts +302 -193
- package/src/config/validation-helpers.ts +379 -357
- package/src/config/validation.ts +1931 -1367
- package/src/config-cache.ts +81 -41
- package/src/create-worker-preview.ts +239 -142
- package/src/create-worker-upload-form.ts +300 -238
- package/src/d1/backups.tsx +223 -0
- package/src/d1/constants.ts +2 -0
- package/src/d1/create.tsx +63 -0
- package/src/d1/delete.tsx +56 -0
- package/src/d1/execute.tsx +352 -0
- package/src/d1/formatTimeAgo.ts +14 -0
- package/src/d1/index.ts +101 -0
- package/src/d1/list.tsx +48 -0
- package/src/d1/migrations/apply.tsx +184 -0
- package/src/d1/migrations/create.tsx +77 -0
- package/src/d1/migrations/helpers.ts +147 -0
- package/src/d1/migrations/index.tsx +3 -0
- package/src/d1/migrations/list.tsx +79 -0
- package/src/d1/migrations/options.ts +18 -0
- package/src/d1/options.ts +22 -0
- package/src/d1/types.tsx +24 -0
- package/src/d1/utils.ts +48 -0
- package/src/delete.ts +108 -0
- package/src/deployments.ts +77 -0
- package/src/deprecated/index.ts +438 -0
- package/src/dev/dev-vars.ts +20 -17
- package/src/dev/dev.tsx +547 -352
- package/src/dev/get-local-persistence-path.tsx +31 -0
- package/src/dev/local.tsx +870 -298
- package/src/dev/remote.tsx +591 -215
- package/src/dev/start-server.ts +539 -0
- package/src/dev/use-esbuild.ts +183 -97
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +201 -0
- package/src/dev.tsx +913 -0
- package/src/dialogs.tsx +132 -99
- package/src/dispatch-namespace.ts +207 -0
- package/src/durable.ts +87 -87
- package/src/entry.ts +235 -228
- package/src/environment-variables.ts +38 -23
- package/src/errors.ts +6 -6
- 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.tsx +630 -2791
- package/src/init.ts +970 -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/kv/helpers.ts +430 -0
- package/src/kv/index.ts +596 -0
- package/src/logger.ts +67 -41
- 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 +96 -0
- package/src/miniflare-cli/assets.ts +220 -0
- package/src/miniflare-cli/index.ts +205 -33
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/module-collection.ts +221 -212
- package/src/open-in-browser.ts +4 -6
- package/src/package-manager.ts +173 -128
- package/src/pages/build.tsx +270 -0
- package/src/pages/constants.ts +18 -0
- package/src/pages/deployment-tails.tsx +284 -0
- package/src/pages/deployments.tsx +84 -0
- package/src/pages/dev.tsx +706 -0
- package/src/pages/errors.ts +66 -0
- package/src/pages/functions/buildPlugin.ts +107 -0
- package/src/pages/functions/buildWorker.ts +166 -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.tsx +82 -0
- package/src/pages/hash.tsx +13 -0
- package/src/pages/index.tsx +102 -0
- package/src/pages/projects.tsx +169 -0
- package/src/pages/prompt-select-project.tsx +31 -0
- package/src/pages/publish.tsx +451 -0
- package/src/pages/types.ts +41 -0
- package/src/pages/upload.tsx +426 -0
- package/src/pages/utils.ts +23 -0
- package/src/parse.ts +209 -125
- package/src/paths.ts +53 -8
- package/src/proxy.ts +623 -394
- package/src/publish/index.ts +270 -0
- package/src/publish/publish.ts +1035 -0
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +624 -0
- package/src/queues/cli/commands/consumer/add.ts +71 -0
- package/src/queues/cli/commands/consumer/index.ts +22 -0
- package/src/queues/cli/commands/consumer/remove.ts +38 -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 +36 -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 +378 -0
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +280 -203
- package/src/tail/createTail.ts +379 -0
- package/src/tail/filters.ts +108 -110
- package/src/tail/index.ts +211 -296
- package/src/tail/printing.ts +86 -46
- package/src/type-generation.ts +173 -0
- package/src/update-check.ts +11 -11
- package/src/user/access.ts +69 -0
- package/src/user/choose-account.tsx +69 -0
- package/src/user/env-vars.ts +46 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +3 -0
- package/src/user/user.tsx +1198 -0
- package/src/utils/collectKeyValues.ts +14 -0
- package/src/whoami.tsx +141 -42
- package/src/worker.ts +171 -106
- package/src/yargs-types.ts +24 -0
- package/src/zones.ts +191 -0
- package/templates/__tests__/pages-dev-util.test.ts +128 -0
- package/templates/checked-fetch.js +17 -0
- package/templates/d1-beta-facade.js +205 -0
- package/templates/first-party-worker-module-facade.ts +18 -0
- package/templates/format-dev-errors.ts +32 -0
- package/templates/init-tests/test-jest-new-worker.js +25 -0
- package/templates/init-tests/test-vitest-new-worker.js +26 -0
- package/templates/middleware/common.ts +62 -0
- package/templates/middleware/loader-modules.ts +84 -0
- package/templates/middleware/loader-sw.ts +227 -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 +4 -4
- package/templates/new-worker-scheduled.ts +16 -16
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +15 -15
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-dev-pipeline.ts +28 -0
- package/templates/pages-dev-util.ts +52 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +171 -0
- package/templates/pages-template-worker.ts +189 -0
- package/templates/serve-static-assets.ts +63 -0
- package/templates/service-bindings-module-facade.js +54 -0
- package/templates/service-bindings-sw-facade.js +42 -0
- package/templates/tsconfig.json +96 -95
- package/wrangler-dist/cli.d.ts +893 -0
- package/wrangler-dist/cli.js +146463 -108710
- 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/kv.ts +0 -408
- package/src/miniflare-cli/enum-keys.ts +0 -17
- package/src/pages.tsx +0 -2027
- package/src/publish.ts +0 -583
- package/src/r2.ts +0 -50
- package/src/user.tsx +0 -1203
- package/templates/static-asset-facade.js +0 -47
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
- package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
- package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
- package/vendor/wrangler-mime/CHANGELOG.md +0 -289
- package/vendor/wrangler-mime/LICENSE +0 -21
- package/vendor/wrangler-mime/Mime.js +0 -97
- package/vendor/wrangler-mime/README.md +0 -187
- package/vendor/wrangler-mime/cli.js +0 -46
- package/vendor/wrangler-mime/index.js +0 -4
- package/vendor/wrangler-mime/lite.js +0 -4
- package/vendor/wrangler-mime/package.json +0 -52
- package/vendor/wrangler-mime/types/other.js +0 -1
- package/vendor/wrangler-mime/types/standard.js +0 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export type Awaitable<T> = T | Promise<T>;
|
|
2
|
+
// TODO: allow dispatching more events?
|
|
3
|
+
export type Dispatcher = (
|
|
4
|
+
type: "scheduled",
|
|
5
|
+
init: { cron?: string }
|
|
6
|
+
) => Awaitable<void>;
|
|
7
|
+
|
|
8
|
+
export interface MiddlewareContext {
|
|
9
|
+
dispatch: Dispatcher;
|
|
10
|
+
next(request: Request, env: any): Awaitable<Response>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type Middleware = (
|
|
14
|
+
request: Request,
|
|
15
|
+
env: any,
|
|
16
|
+
ctx: ExecutionContext,
|
|
17
|
+
middlewareCtx: MiddlewareContext
|
|
18
|
+
) => Awaitable<Response>;
|
|
19
|
+
|
|
20
|
+
const __facade_middleware__: Middleware[] = [];
|
|
21
|
+
|
|
22
|
+
// The register functions allow for the insertion of one or many middleware,
|
|
23
|
+
// We register internal middleware first in the stack, but have no way of controlling
|
|
24
|
+
// the order that addMiddleware is run in service workers so need an internal function.
|
|
25
|
+
export function __facade_register__(...args: (Middleware | Middleware[])[]) {
|
|
26
|
+
__facade_middleware__.push(...args.flat());
|
|
27
|
+
}
|
|
28
|
+
export function __facade_registerInternal__(
|
|
29
|
+
...args: (Middleware | Middleware[])[]
|
|
30
|
+
) {
|
|
31
|
+
__facade_middleware__.unshift(...args.flat());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function __facade_invokeChain__(
|
|
35
|
+
request: Request,
|
|
36
|
+
env: any,
|
|
37
|
+
ctx: ExecutionContext,
|
|
38
|
+
dispatch: Dispatcher,
|
|
39
|
+
middlewareChain: Middleware[]
|
|
40
|
+
): Awaitable<Response> {
|
|
41
|
+
const [head, ...tail] = middlewareChain;
|
|
42
|
+
const middlewareCtx: MiddlewareContext = {
|
|
43
|
+
dispatch,
|
|
44
|
+
next(newRequest, newEnv) {
|
|
45
|
+
return __facade_invokeChain__(newRequest, newEnv, ctx, dispatch, tail);
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
return head(request, env, ctx, middlewareCtx);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function __facade_invoke__(
|
|
52
|
+
request: Request,
|
|
53
|
+
env: any,
|
|
54
|
+
ctx: ExecutionContext,
|
|
55
|
+
dispatch: Dispatcher,
|
|
56
|
+
finalMiddleware: Middleware
|
|
57
|
+
): Awaitable<Response> {
|
|
58
|
+
return __facade_invokeChain__(request, env, ctx, dispatch, [
|
|
59
|
+
...__facade_middleware__,
|
|
60
|
+
finalMiddleware,
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// // This loads all middlewares exposed on the middleware object
|
|
2
|
+
// // and then starts the invocation chain.
|
|
3
|
+
// // The big idea is that we can add these to the middleware export dynamically
|
|
4
|
+
// // through wrangler, or we can potentially let users directly add them as a sort
|
|
5
|
+
// // of "plugin" system.
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
Dispatcher,
|
|
9
|
+
Middleware,
|
|
10
|
+
__facade_invoke__,
|
|
11
|
+
__facade_register__,
|
|
12
|
+
} from "./common";
|
|
13
|
+
|
|
14
|
+
// @ts-expect-error We'll swap in the entry point during build
|
|
15
|
+
import worker from "__ENTRY_POINT__";
|
|
16
|
+
|
|
17
|
+
// We need to preserve all of the exports from the worker
|
|
18
|
+
// @ts-expect-error We'll swap in the entry point during build
|
|
19
|
+
export * from "__ENTRY_POINT__";
|
|
20
|
+
|
|
21
|
+
class __Facade_ScheduledController__ implements ScheduledController {
|
|
22
|
+
#noRetry: ScheduledController["noRetry"];
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
readonly scheduledTime: number,
|
|
26
|
+
readonly cron: string,
|
|
27
|
+
noRetry: ScheduledController["noRetry"]
|
|
28
|
+
) {
|
|
29
|
+
this.#noRetry = noRetry;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
noRetry() {
|
|
33
|
+
if (!(this instanceof __Facade_ScheduledController__)) {
|
|
34
|
+
throw new TypeError("Illegal invocation");
|
|
35
|
+
}
|
|
36
|
+
// Need to call native method immediately in case uncaught error thrown
|
|
37
|
+
this.#noRetry();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const __facade_modules_fetch__: Middleware = function (request, env, ctx) {
|
|
42
|
+
if (worker.fetch === undefined) throw new Error("No fetch handler!"); // TODO: proper error message
|
|
43
|
+
return worker.fetch(request, env, ctx);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const facade: ExportedHandler<unknown> = {
|
|
47
|
+
fetch(request, env, ctx) {
|
|
48
|
+
// Get the chain of middleware from the worker object
|
|
49
|
+
if (worker.middleware && worker.middleware.length > 0) {
|
|
50
|
+
for (const middleware of worker.middleware) {
|
|
51
|
+
__facade_register__(middleware);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const __facade_modules_dispatch__: Dispatcher = function (type, init) {
|
|
55
|
+
if (type === "scheduled" && worker.scheduled !== undefined) {
|
|
56
|
+
const controller = new __Facade_ScheduledController__(
|
|
57
|
+
Date.now(),
|
|
58
|
+
init.cron ?? "",
|
|
59
|
+
() => {}
|
|
60
|
+
);
|
|
61
|
+
return worker.scheduled(controller, env, ctx);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return __facade_invoke__(
|
|
66
|
+
request,
|
|
67
|
+
env,
|
|
68
|
+
ctx,
|
|
69
|
+
__facade_modules_dispatch__,
|
|
70
|
+
__facade_modules_fetch__
|
|
71
|
+
);
|
|
72
|
+
} else {
|
|
73
|
+
// We didn't have any middleware so we can skip the invocation chain,
|
|
74
|
+
// and just call the fetch handler directly
|
|
75
|
+
|
|
76
|
+
// We "don't care" if this is undefined as we want to have the same behaviour
|
|
77
|
+
// as if the worker completely bypassed middleware.
|
|
78
|
+
return worker.fetch!(request, env, ctx);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
scheduled: worker.scheduled,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export default facade;
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Awaitable,
|
|
3
|
+
Dispatcher,
|
|
4
|
+
Middleware,
|
|
5
|
+
__facade_invoke__,
|
|
6
|
+
__facade_register__,
|
|
7
|
+
__facade_registerInternal__,
|
|
8
|
+
} from "./common";
|
|
9
|
+
export { __facade_register__, __facade_registerInternal__ };
|
|
10
|
+
|
|
11
|
+
// Miniflare 2's `EventTarget` follows the spec and doesn't allow exceptions to
|
|
12
|
+
// be caught by `dispatchEvent`. Instead it has a custom `ThrowingEventTarget`
|
|
13
|
+
// class that rethrows errors from event listeners in `dispatchEvent`.
|
|
14
|
+
// We'd like errors to be propagated to the top-level `addEventListener`, so
|
|
15
|
+
// we'd like to use `ThrowingEventTarget`. Unfortunately, `ThrowingEventTarget`
|
|
16
|
+
// isn't exposed on the global scope, but `WorkerGlobalScope` (which extends
|
|
17
|
+
// `ThrowingEventTarget`) is. Therefore, we get at it in this nasty way.
|
|
18
|
+
let __FACADE_EVENT_TARGET__: EventTarget;
|
|
19
|
+
if ((globalThis as any).MINIFLARE) {
|
|
20
|
+
__FACADE_EVENT_TARGET__ = new (Object.getPrototypeOf(WorkerGlobalScope))();
|
|
21
|
+
} else {
|
|
22
|
+
__FACADE_EVENT_TARGET__ = new EventTarget();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function __facade_isSpecialEvent__(
|
|
26
|
+
type: string
|
|
27
|
+
): type is "fetch" | "scheduled" {
|
|
28
|
+
return type === "fetch" || type === "scheduled";
|
|
29
|
+
}
|
|
30
|
+
const __facade__originalAddEventListener__ = globalThis.addEventListener;
|
|
31
|
+
const __facade__originalRemoveEventListener__ = globalThis.removeEventListener;
|
|
32
|
+
const __facade__originalDispatchEvent__ = globalThis.dispatchEvent;
|
|
33
|
+
|
|
34
|
+
globalThis.addEventListener = function (type, listener, options) {
|
|
35
|
+
if (__facade_isSpecialEvent__(type)) {
|
|
36
|
+
__FACADE_EVENT_TARGET__.addEventListener(
|
|
37
|
+
type,
|
|
38
|
+
listener as EventListenerOrEventListenerObject,
|
|
39
|
+
options
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
__facade__originalAddEventListener__(type, listener, options);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
globalThis.removeEventListener = function (type, listener, options) {
|
|
46
|
+
if (__facade_isSpecialEvent__(type)) {
|
|
47
|
+
__FACADE_EVENT_TARGET__.removeEventListener(
|
|
48
|
+
type,
|
|
49
|
+
listener as EventListenerOrEventListenerObject,
|
|
50
|
+
options
|
|
51
|
+
);
|
|
52
|
+
} else {
|
|
53
|
+
__facade__originalRemoveEventListener__(type, listener, options);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
globalThis.dispatchEvent = function (event) {
|
|
57
|
+
if (__facade_isSpecialEvent__(event.type)) {
|
|
58
|
+
return __FACADE_EVENT_TARGET__.dispatchEvent(event);
|
|
59
|
+
} else {
|
|
60
|
+
return __facade__originalDispatchEvent__(event);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
declare global {
|
|
65
|
+
var addMiddleware: typeof __facade_register__;
|
|
66
|
+
var addMiddlewareInternal: typeof __facade_registerInternal__;
|
|
67
|
+
}
|
|
68
|
+
globalThis.addMiddleware = __facade_register__;
|
|
69
|
+
globalThis.addMiddlewareInternal = __facade_registerInternal__;
|
|
70
|
+
|
|
71
|
+
const __facade_waitUntil__ = Symbol("__facade_waitUntil__");
|
|
72
|
+
const __facade_response__ = Symbol("__facade_response__");
|
|
73
|
+
const __facade_dispatched__ = Symbol("__facade_dispatched__");
|
|
74
|
+
|
|
75
|
+
class __Facade_ExtendableEvent__ extends Event {
|
|
76
|
+
[__facade_waitUntil__]: Awaitable<unknown>[] = [];
|
|
77
|
+
|
|
78
|
+
waitUntil(promise: Awaitable<any>) {
|
|
79
|
+
if (!(this instanceof __Facade_ExtendableEvent__)) {
|
|
80
|
+
throw new TypeError("Illegal invocation");
|
|
81
|
+
}
|
|
82
|
+
this[__facade_waitUntil__].push(promise);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface FetchEventInit extends EventInit {
|
|
87
|
+
request: Request;
|
|
88
|
+
passThroughOnException: FetchEvent["passThroughOnException"];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
class __Facade_FetchEvent__ extends __Facade_ExtendableEvent__ {
|
|
92
|
+
#request: Request;
|
|
93
|
+
#passThroughOnException: FetchEvent["passThroughOnException"];
|
|
94
|
+
[__facade_response__]?: Awaitable<Response>;
|
|
95
|
+
[__facade_dispatched__] = false;
|
|
96
|
+
|
|
97
|
+
constructor(type: "fetch", init: FetchEventInit) {
|
|
98
|
+
super(type);
|
|
99
|
+
this.#request = init.request;
|
|
100
|
+
this.#passThroughOnException = init.passThroughOnException;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get request() {
|
|
104
|
+
return this.#request;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
respondWith(response: Awaitable<Response>) {
|
|
108
|
+
if (!(this instanceof __Facade_FetchEvent__)) {
|
|
109
|
+
throw new TypeError("Illegal invocation");
|
|
110
|
+
}
|
|
111
|
+
if (this[__facade_response__] !== undefined) {
|
|
112
|
+
throw new DOMException(
|
|
113
|
+
"FetchEvent.respondWith() has already been called; it can only be called once.",
|
|
114
|
+
"InvalidStateError"
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
if (this[__facade_dispatched__]) {
|
|
118
|
+
throw new DOMException(
|
|
119
|
+
"Too late to call FetchEvent.respondWith(). It must be called synchronously in the event handler.",
|
|
120
|
+
"InvalidStateError"
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
this.stopImmediatePropagation();
|
|
124
|
+
this[__facade_response__] = response;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
passThroughOnException() {
|
|
128
|
+
if (!(this instanceof __Facade_FetchEvent__)) {
|
|
129
|
+
throw new TypeError("Illegal invocation");
|
|
130
|
+
}
|
|
131
|
+
// Need to call native method immediately in case uncaught error thrown
|
|
132
|
+
this.#passThroughOnException();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
interface ScheduledEventInit extends EventInit {
|
|
137
|
+
scheduledTime: number;
|
|
138
|
+
cron: string;
|
|
139
|
+
noRetry: ScheduledEvent["noRetry"];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
class __Facade_ScheduledEvent__ extends __Facade_ExtendableEvent__ {
|
|
143
|
+
#scheduledTime: number;
|
|
144
|
+
#cron: string;
|
|
145
|
+
#noRetry: ScheduledEvent["noRetry"];
|
|
146
|
+
|
|
147
|
+
constructor(type: "scheduled", init: ScheduledEventInit) {
|
|
148
|
+
super(type);
|
|
149
|
+
this.#scheduledTime = init.scheduledTime;
|
|
150
|
+
this.#cron = init.cron;
|
|
151
|
+
this.#noRetry = init.noRetry;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
get scheduledTime() {
|
|
155
|
+
return this.#scheduledTime;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get cron() {
|
|
159
|
+
return this.#cron;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
noRetry() {
|
|
163
|
+
if (!(this instanceof __Facade_ScheduledEvent__)) {
|
|
164
|
+
throw new TypeError("Illegal invocation");
|
|
165
|
+
}
|
|
166
|
+
// Need to call native method immediately in case uncaught error thrown
|
|
167
|
+
this.#noRetry();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
__facade__originalAddEventListener__("fetch", (event) => {
|
|
172
|
+
const ctx: ExecutionContext = {
|
|
173
|
+
waitUntil: event.waitUntil.bind(event),
|
|
174
|
+
passThroughOnException: event.passThroughOnException.bind(event),
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const __facade_sw_dispatch__: Dispatcher = function (type, init) {
|
|
178
|
+
if (type === "scheduled") {
|
|
179
|
+
const facadeEvent = new __Facade_ScheduledEvent__("scheduled", {
|
|
180
|
+
scheduledTime: Date.now(),
|
|
181
|
+
cron: init.cron ?? "",
|
|
182
|
+
noRetry() {},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
__FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
|
|
186
|
+
event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const __facade_sw_fetch__: Middleware = function (request, _env, ctx) {
|
|
191
|
+
const facadeEvent = new __Facade_FetchEvent__("fetch", {
|
|
192
|
+
request,
|
|
193
|
+
passThroughOnException: ctx.passThroughOnException,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
__FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
|
|
197
|
+
facadeEvent[__facade_dispatched__] = true;
|
|
198
|
+
event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
|
|
199
|
+
|
|
200
|
+
const response = facadeEvent[__facade_response__];
|
|
201
|
+
if (response === undefined) {
|
|
202
|
+
throw new Error("No response!"); // TODO: proper error message
|
|
203
|
+
}
|
|
204
|
+
return response;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
event.respondWith(
|
|
208
|
+
__facade_invoke__(
|
|
209
|
+
event.request,
|
|
210
|
+
globalThis,
|
|
211
|
+
ctx,
|
|
212
|
+
__facade_sw_dispatch__,
|
|
213
|
+
__facade_sw_fetch__
|
|
214
|
+
)
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
__facade__originalAddEventListener__("scheduled", (event) => {
|
|
219
|
+
const facadeEvent = new __Facade_ScheduledEvent__("scheduled", {
|
|
220
|
+
scheduledTime: event.scheduledTime,
|
|
221
|
+
cron: event.cron,
|
|
222
|
+
noRetry: event.noRetry.bind(event),
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
__FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
|
|
226
|
+
event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
|
|
227
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Middleware } from "./common";
|
|
2
|
+
|
|
3
|
+
// See comment in `bundle.ts` for details on why this is needed
|
|
4
|
+
const jsonError: Middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
5
|
+
try {
|
|
6
|
+
return await middlewareCtx.next(request, env);
|
|
7
|
+
} catch (e: any) {
|
|
8
|
+
const error = {
|
|
9
|
+
name: e?.name,
|
|
10
|
+
message: e?.message ?? String(e),
|
|
11
|
+
stack: e?.stack,
|
|
12
|
+
};
|
|
13
|
+
return Response.json(error, {
|
|
14
|
+
status: 500,
|
|
15
|
+
headers: { "MF-Experimental-Error-Stack": "true" },
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default jsonError;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Middleware } from "./common";
|
|
2
|
+
|
|
3
|
+
// A middleware has to be a function of type Middleware
|
|
4
|
+
const prettyError: Middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
5
|
+
try {
|
|
6
|
+
const response = await middlewareCtx.next(request, env);
|
|
7
|
+
return response;
|
|
8
|
+
} catch (e: any) {
|
|
9
|
+
const html = `
|
|
10
|
+
<!DOCTYPE html>
|
|
11
|
+
<html lang="en">
|
|
12
|
+
<head>
|
|
13
|
+
<meta charset="UTF-8">
|
|
14
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
15
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
16
|
+
<title>Error 🚨</title>
|
|
17
|
+
<style>
|
|
18
|
+
pre {
|
|
19
|
+
margin: 16px auto;
|
|
20
|
+
max-width: 600px;
|
|
21
|
+
background-color: #eeeeee;
|
|
22
|
+
border-radius: 4px;
|
|
23
|
+
padding: 16px;
|
|
24
|
+
}
|
|
25
|
+
</style>
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
<pre>${e.stack}</pre>
|
|
29
|
+
</body>
|
|
30
|
+
</html>
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
return new Response(html, {
|
|
34
|
+
status: 500,
|
|
35
|
+
headers: { "Content-Type": "text/html;charset=utf-8" },
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export default prettyError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Middleware } from "./common";
|
|
2
|
+
|
|
3
|
+
// A middleware has to be a function of type Middleware
|
|
4
|
+
const scheduled: Middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
5
|
+
const url = new URL(request.url);
|
|
6
|
+
if (url.pathname === "/__scheduled") {
|
|
7
|
+
const cron = url.searchParams.get("cron") ?? "";
|
|
8
|
+
await middlewareCtx.dispatch("scheduled", { cron });
|
|
9
|
+
|
|
10
|
+
return new Response("Ran scheduled event");
|
|
11
|
+
}
|
|
12
|
+
return middlewareCtx.next(request, env);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default scheduled;
|
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
* - Run `wrangler dev --local` in your terminal to start a development server
|
|
5
5
|
* - Run `curl "http://localhost:8787/cdn-cgi/mf/scheduled"` to trigger the scheduled event
|
|
6
6
|
* - Go back to the console to see what your worker has logged
|
|
7
|
-
* -
|
|
7
|
+
* - Update the Cron trigger in wrangler.toml (see https://developers.cloudflare.com/workers/wrangler/configuration/#triggers)
|
|
8
8
|
* - Run `wrangler publish --name my-worker` to publish your worker
|
|
9
9
|
*
|
|
10
10
|
* Learn more at https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
export default {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
async scheduled(controller, env, ctx) {
|
|
15
|
+
console.log(`Hello World!`);
|
|
16
|
+
},
|
|
17
17
|
};
|
|
@@ -4,29 +4,29 @@
|
|
|
4
4
|
* - Run `wrangler dev --local` in your terminal to start a development server
|
|
5
5
|
* - Run `curl "http://localhost:8787/cdn-cgi/mf/scheduled"` to trigger the scheduled event
|
|
6
6
|
* - Go back to the console to see what your worker has logged
|
|
7
|
-
* -
|
|
7
|
+
* - Update the Cron trigger in wrangler.toml (see https://developers.cloudflare.com/workers/wrangler/configuration/#triggers)
|
|
8
8
|
* - Run `wrangler publish --name my-worker` to publish your worker
|
|
9
9
|
*
|
|
10
10
|
* Learn more at https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
export interface Env {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
|
15
|
+
// MY_KV_NAMESPACE: KVNamespace;
|
|
16
|
+
//
|
|
17
|
+
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
|
|
18
|
+
// MY_DURABLE_OBJECT: DurableObjectNamespace;
|
|
19
|
+
//
|
|
20
|
+
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
|
21
|
+
// MY_BUCKET: R2Bucket;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export default {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
async scheduled(
|
|
26
|
+
controller: ScheduledController,
|
|
27
|
+
env: Env,
|
|
28
|
+
ctx: ExecutionContext
|
|
29
|
+
): Promise<void> {
|
|
30
|
+
console.log(`Hello World!`);
|
|
31
|
+
},
|
|
32
32
|
};
|
package/templates/new-worker.js
CHANGED
package/templates/new-worker.ts
CHANGED
|
@@ -9,22 +9,22 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
export interface Env {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
|
13
|
+
// MY_KV_NAMESPACE: KVNamespace;
|
|
14
|
+
//
|
|
15
|
+
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
|
|
16
|
+
// MY_DURABLE_OBJECT: DurableObjectNamespace;
|
|
17
|
+
//
|
|
18
|
+
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
|
19
|
+
// MY_BUCKET: R2Bucket;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export default {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
async fetch(
|
|
24
|
+
request: Request,
|
|
25
|
+
env: Env,
|
|
26
|
+
ctx: ExecutionContext
|
|
27
|
+
): Promise<Response> {
|
|
28
|
+
return new Response("Hello World!");
|
|
29
|
+
},
|
|
30
30
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// @ts-ignore entry point will get replaced
|
|
2
|
+
import worker from "__ENTRY_POINT__";
|
|
3
|
+
// @ts-ignore entry point will get replaced
|
|
4
|
+
export * from "__ENTRY_POINT__";
|
|
5
|
+
import { isRoutingRuleMatch } from "./pages-dev-util";
|
|
6
|
+
|
|
7
|
+
// @ts-ignore routes are injected
|
|
8
|
+
const routes = __ROUTES__;
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
fetch(request, env, context) {
|
|
12
|
+
const { pathname } = new URL(request.url);
|
|
13
|
+
|
|
14
|
+
for (const exclude of routes.exclude) {
|
|
15
|
+
if (isRoutingRuleMatch(pathname, exclude)) {
|
|
16
|
+
return env.ASSETS.fetch(request);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
for (const include of routes.include) {
|
|
21
|
+
if (isRoutingRuleMatch(pathname, include)) {
|
|
22
|
+
return worker.fetch(request, env, context);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return env.ASSETS.fetch(request);
|
|
27
|
+
},
|
|
28
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param pathname A pathname string, such as `/foo` or `/foo/bar`
|
|
3
|
+
* @param routingRule The routing rule, such as `/foo/*`
|
|
4
|
+
* @returns True if pathname matches the routing rule
|
|
5
|
+
*
|
|
6
|
+
* / -> /
|
|
7
|
+
* /* -> /*
|
|
8
|
+
* /foo -> /foo
|
|
9
|
+
* /foo* -> /foo, /foo-bar, /foo/*
|
|
10
|
+
* /foo/* -> /foo, /foo/bar
|
|
11
|
+
*/
|
|
12
|
+
export function isRoutingRuleMatch(
|
|
13
|
+
pathname: string,
|
|
14
|
+
routingRule: string
|
|
15
|
+
): boolean {
|
|
16
|
+
// sanity checks
|
|
17
|
+
if (!pathname) {
|
|
18
|
+
throw new Error("Pathname is undefined.");
|
|
19
|
+
}
|
|
20
|
+
if (!routingRule) {
|
|
21
|
+
throw new Error("Routing rule is undefined.");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const ruleRegExp = transformRoutingRuleToRegExp(routingRule);
|
|
25
|
+
return pathname.match(ruleRegExp) !== null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function transformRoutingRuleToRegExp(rule: string): RegExp {
|
|
29
|
+
let transformedRule;
|
|
30
|
+
|
|
31
|
+
if (rule === "/" || rule === "/*") {
|
|
32
|
+
transformedRule = rule;
|
|
33
|
+
} else if (rule.endsWith("/*")) {
|
|
34
|
+
// make `/*` an optional group so we can match both /foo/* and /foo
|
|
35
|
+
// /foo/* => /foo(/*)?
|
|
36
|
+
transformedRule = `${rule.substring(0, rule.length - 2)}(/*)?`;
|
|
37
|
+
} else if (rule.endsWith("/")) {
|
|
38
|
+
// make `/` an optional group so we can match both /foo/ and /foo
|
|
39
|
+
// /foo/ => /foo(/)?
|
|
40
|
+
transformedRule = `${rule.substring(0, rule.length - 1)}(/)?`;
|
|
41
|
+
} else if (rule.endsWith("*")) {
|
|
42
|
+
transformedRule = rule;
|
|
43
|
+
} else {
|
|
44
|
+
transformedRule = `${rule}(/)?`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// /foo* => /foo.* => ^/foo.*$
|
|
48
|
+
transformedRule = `^${transformedRule.replace("*", ".*")}$`;
|
|
49
|
+
|
|
50
|
+
// ^/foo.*$ => /^\/foo.*$/
|
|
51
|
+
return new RegExp(transformedRule);
|
|
52
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// This Worker is used as a default when no Pages Functions are present.
|
|
2
|
+
// It proxies the request directly on to the asset server binding.
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
async fetch(request, env, context) {
|
|
6
|
+
const response = await env.ASSETS.fetch(request.url, request);
|
|
7
|
+
return new Response(response.body, response);
|
|
8
|
+
},
|
|
9
|
+
};
|