wrangler 0.0.0-ecc8b172 → 0.0.0-ecded1af1
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 +17 -19
- package/bin/wrangler.js +24 -93
- package/config-schema.json +3115 -0
- package/package.json +197 -190
- 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/checked-fetch.js +19 -6
- package/templates/facade.d.ts +19 -0
- package/templates/gitignore +2 -3
- package/templates/init-tests/test-jest-new-worker.js +3 -5
- package/templates/init-tests/test-vitest-new-worker.js +4 -6
- package/templates/init-tests/test-vitest-new-worker.ts +25 -0
- package/templates/middleware/common.ts +9 -4
- package/templates/middleware/loader-modules.ts +94 -44
- package/templates/middleware/loader-sw.ts +48 -32
- package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
- package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
- package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
- package/templates/middleware/middleware-scheduled.ts +15 -1
- package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
- package/templates/middleware/middleware-serve-static-assets.ts +56 -0
- package/templates/modules-watch-stub.js +4 -0
- package/templates/new-worker-scheduled.ts +1 -1
- package/templates/new-worker.ts +4 -1
- package/templates/pages-dev-pipeline.ts +10 -13
- package/templates/pages-dev-util.ts +55 -0
- package/templates/pages-shim.ts +1 -1
- package/templates/pages-template-plugin.ts +37 -14
- package/templates/pages-template-worker.ts +33 -7
- package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
- package/templates/startDevWorker/ProxyWorker.ts +336 -0
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +22 -0
- package/templates/tsconfig.json +5 -102
- package/wrangler-dist/InspectorProxyWorker.js +464 -0
- package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
- package/wrangler-dist/ProxyWorker.js +241 -0
- package/wrangler-dist/ProxyWorker.js.map +6 -0
- package/wrangler-dist/cli.d.ts +26095 -471
- package/wrangler-dist/cli.js +178062 -122008
- package/wrangler-dist/wasm-sync.wasm +0 -0
- package/Cloudflare_CA.pem +0 -18
- package/import_meta_url.js +0 -3
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +0 -1
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/miniflare-dist/index.mjs +0 -6310
- package/src/__tests__/api-dev.test.ts +0 -209
- package/src/__tests__/api-devregistry.test.js +0 -56
- package/src/__tests__/api.test.ts +0 -80
- package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
- package/src/__tests__/config-cache.test.ts +0 -42
- package/src/__tests__/configuration.test.ts +0 -3864
- package/src/__tests__/d1.test.ts +0 -70
- package/src/__tests__/delete.test.ts +0 -116
- package/src/__tests__/dev.test.tsx +0 -1357
- package/src/__tests__/dialogs.test.tsx +0 -40
- package/src/__tests__/generate.test.ts +0 -235
- package/src/__tests__/get-host-from-url.test.ts +0 -16
- package/src/__tests__/guess-worker-format.test.ts +0 -85
- package/src/__tests__/helpers/clipboardy-mock.js +0 -4
- package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
- package/src/__tests__/helpers/mock-account-id.ts +0 -48
- package/src/__tests__/helpers/mock-bin.ts +0 -36
- package/src/__tests__/helpers/mock-cfetch.ts +0 -333
- package/src/__tests__/helpers/mock-console.ts +0 -106
- package/src/__tests__/helpers/mock-dialogs.ts +0 -153
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -8
- package/src/__tests__/helpers/mock-http-server.ts +0 -46
- package/src/__tests__/helpers/mock-istty.ts +0 -74
- package/src/__tests__/helpers/mock-known-routes.ts +0 -7
- package/src/__tests__/helpers/mock-kv.ts +0 -33
- package/src/__tests__/helpers/mock-oauth-flow.ts +0 -252
- package/src/__tests__/helpers/mock-process.ts +0 -39
- package/src/__tests__/helpers/mock-stdin.ts +0 -108
- package/src/__tests__/helpers/mock-web-socket.ts +0 -29
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -104
- package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -36
- package/src/__tests__/helpers/msw/handlers/r2.ts +0 -80
- package/src/__tests__/helpers/msw/handlers/script.ts +0 -75
- package/src/__tests__/helpers/msw/handlers/user.ts +0 -63
- package/src/__tests__/helpers/msw/index.ts +0 -15
- package/src/__tests__/helpers/run-in-tmp.ts +0 -38
- package/src/__tests__/helpers/run-wrangler.ts +0 -16
- package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
- package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
- package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
- package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
- package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
- package/src/__tests__/helpers/write-worker-source.ts +0 -31
- package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
- package/src/__tests__/https-options.test.ts +0 -163
- package/src/__tests__/index.test.ts +0 -251
- package/src/__tests__/init.test.ts +0 -2816
- package/src/__tests__/jest.setup.ts +0 -191
- package/src/__tests__/kv.test.ts +0 -1675
- package/src/__tests__/logger.test.ts +0 -141
- package/src/__tests__/metrics.test.ts +0 -450
- package/src/__tests__/middleware.scheduled.test.ts +0 -135
- package/src/__tests__/middleware.test.ts +0 -726
- package/src/__tests__/package-manager.test.ts +0 -353
- package/src/__tests__/pages.test.ts +0 -2111
- package/src/__tests__/parse.test.ts +0 -330
- package/src/__tests__/paths.test.ts +0 -39
- package/src/__tests__/publish.test.ts +0 -7119
- package/src/__tests__/pubsub.test.ts +0 -367
- package/src/__tests__/r2.test.ts +0 -371
- package/src/__tests__/route.test.ts +0 -45
- package/src/__tests__/secret.test.ts +0 -644
- package/src/__tests__/tail.test.ts +0 -800
- package/src/__tests__/test-old-node-version.js +0 -31
- package/src/__tests__/user.test.ts +0 -155
- package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
- package/src/__tests__/validate-dev-props.test.ts +0 -56
- package/src/__tests__/version.test.ts +0 -35
- package/src/__tests__/whoami.test.tsx +0 -278
- package/src/__tests__/worker-namespace.test.ts +0 -321
- package/src/abort.d.ts +0 -3
- package/src/api/dev.ts +0 -208
- package/src/api/index.ts +0 -2
- package/src/bundle-reporter.tsx +0 -29
- package/src/bundle.ts +0 -756
- package/src/cfetch/index.ts +0 -160
- package/src/cfetch/internal.ts +0 -263
- package/src/cli.ts +0 -29
- package/src/config/README.md +0 -107
- package/src/config/config.ts +0 -275
- package/src/config/diagnostics.ts +0 -80
- package/src/config/environment.ts +0 -505
- package/src/config/index.ts +0 -293
- package/src/config/validation-helpers.ts +0 -597
- package/src/config/validation.ts +0 -1930
- package/src/config-cache.ts +0 -85
- package/src/create-worker-preview.ts +0 -275
- package/src/create-worker-upload-form.ts +0 -301
- package/src/d1/backups.tsx +0 -212
- package/src/d1/create.tsx +0 -54
- package/src/d1/delete.tsx +0 -56
- package/src/d1/execute.tsx +0 -294
- package/src/d1/formatTimeAgo.ts +0 -14
- package/src/d1/index.ts +0 -75
- package/src/d1/list.tsx +0 -48
- package/src/d1/options.ts +0 -12
- package/src/d1/types.tsx +0 -14
- package/src/d1/utils.ts +0 -39
- package/src/delete.ts +0 -86
- package/src/deprecated/index.ts +0 -453
- package/src/dev/dev-vars.ts +0 -36
- package/src/dev/dev.tsx +0 -575
- package/src/dev/get-local-persistence-path.tsx +0 -31
- package/src/dev/local.tsx +0 -686
- package/src/dev/remote.tsx +0 -601
- package/src/dev/start-server.ts +0 -506
- package/src/dev/use-esbuild.ts +0 -197
- package/src/dev/validate-dev-props.ts +0 -31
- package/src/dev-registry.tsx +0 -201
- package/src/dev.tsx +0 -865
- package/src/dialogs.tsx +0 -163
- package/src/dispatch-namespace.ts +0 -206
- package/src/durable.ts +0 -102
- package/src/entry.ts +0 -329
- package/src/environment-variables.ts +0 -50
- package/src/errors.ts +0 -11
- package/src/git-client.ts +0 -124
- package/src/global-wrangler-config-path.ts +0 -26
- package/src/https-options.ts +0 -127
- package/src/index.tsx +0 -582
- package/src/init.ts +0 -970
- package/src/inspect.ts +0 -747
- package/src/intl-polyfill.d.ts +0 -139
- package/src/is-ci.ts +0 -14
- package/src/is-interactive.ts +0 -12
- package/src/kv/helpers.ts +0 -430
- package/src/kv/index.ts +0 -645
- package/src/logger.ts +0 -80
- package/src/metrics/index.ts +0 -5
- package/src/metrics/metrics-config.ts +0 -239
- package/src/metrics/metrics-dispatcher.ts +0 -96
- package/src/metrics/metrics-usage-headers.ts +0 -24
- package/src/metrics/send-event.ts +0 -94
- package/src/miniflare-cli/README.md +0 -30
- package/src/miniflare-cli/assets.ts +0 -215
- package/src/miniflare-cli/index.ts +0 -208
- package/src/miniflare-cli/request-context.ts +0 -40
- package/src/module-collection.ts +0 -267
- package/src/open-in-browser.ts +0 -17
- package/src/package-manager.ts +0 -219
- package/src/pages/build.tsx +0 -238
- package/src/pages/constants.ts +0 -16
- package/src/pages/deployments.tsx +0 -106
- package/src/pages/dev.tsx +0 -689
- package/src/pages/errors.ts +0 -66
- package/src/pages/functions/buildPlugin.ts +0 -110
- package/src/pages/functions/buildWorker.ts +0 -156
- package/src/pages/functions/filepath-routing.test.ts +0 -234
- package/src/pages/functions/filepath-routing.ts +0 -189
- package/src/pages/functions/identifiers.ts +0 -78
- package/src/pages/functions/routes-consolidation.test.ts +0 -250
- package/src/pages/functions/routes-consolidation.ts +0 -73
- package/src/pages/functions/routes-transformation.test.ts +0 -282
- package/src/pages/functions/routes-transformation.ts +0 -115
- package/src/pages/functions/routes-validation.test.ts +0 -403
- package/src/pages/functions/routes-validation.ts +0 -202
- package/src/pages/functions/routes.ts +0 -151
- package/src/pages/functions.tsx +0 -82
- package/src/pages/hash.tsx +0 -13
- package/src/pages/index.tsx +0 -94
- package/src/pages/projects.tsx +0 -169
- package/src/pages/publish.tsx +0 -436
- package/src/pages/types.ts +0 -49
- package/src/pages/upload.tsx +0 -398
- package/src/pages/utils.ts +0 -12
- package/src/parse.ts +0 -286
- package/src/paths.ts +0 -71
- package/src/proxy.ts +0 -657
- package/src/publish/index.ts +0 -267
- package/src/publish/publish.ts +0 -913
- package/src/pubsub/index.ts +0 -286
- package/src/pubsub/pubsub-commands.tsx +0 -623
- package/src/r2/helpers.ts +0 -131
- package/src/r2/index.ts +0 -277
- package/src/routes.ts +0 -140
- package/src/secret/index.ts +0 -403
- package/src/selfsigned.d.ts +0 -29
- package/src/sites.tsx +0 -372
- package/src/tail/createTail.ts +0 -315
- package/src/tail/filters.ts +0 -277
- package/src/tail/index.ts +0 -217
- package/src/tail/printing.ts +0 -107
- package/src/update-check.ts +0 -19
- package/src/user/choose-account.tsx +0 -69
- package/src/user/env-vars.ts +0 -46
- package/src/user/generate-auth-url.ts +0 -33
- package/src/user/generate-random-state.ts +0 -16
- package/src/user/index.ts +0 -3
- package/src/user/user.tsx +0 -1192
- package/src/utils/collectKeyValues.ts +0 -14
- package/src/whoami.tsx +0 -169
- package/src/worker.ts +0 -234
- package/src/yargs-types.ts +0 -7
- package/src/zones.ts +0 -165
- package/templates/d1-beta-facade.js +0 -174
- package/templates/experimental-local-cache-stubs.js +0 -27
- package/templates/first-party-worker-module-facade.ts +0 -18
- package/templates/format-dev-errors.ts +0 -32
- package/templates/serve-static-assets.ts +0 -63
- package/templates/service-bindings-module-facade.js +0 -54
- package/templates/service-bindings-sw-facade.js +0 -42
|
@@ -1,215 +0,0 @@
|
|
|
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 { fetch as miniflareFetch } from "@miniflare/core";
|
|
7
|
-
import {
|
|
8
|
-
Response as MiniflareResponse,
|
|
9
|
-
Request as MiniflareRequest,
|
|
10
|
-
} from "@miniflare/core";
|
|
11
|
-
import { watch } from "chokidar";
|
|
12
|
-
import { getType } from "mime";
|
|
13
|
-
import { hashFile } from "../pages/hash";
|
|
14
|
-
import type { Metadata } from "@cloudflare/pages-shared/asset-server/metadata";
|
|
15
|
-
import type {
|
|
16
|
-
fetch,
|
|
17
|
-
Request,
|
|
18
|
-
} from "@cloudflare/pages-shared/environment-polyfills/types";
|
|
19
|
-
import type {
|
|
20
|
-
ParsedRedirects,
|
|
21
|
-
ParsedHeaders,
|
|
22
|
-
} from "@cloudflare/pages-shared/metadata-generator/types";
|
|
23
|
-
import type { RequestInfo, RequestInit, FetcherFetch } from "@miniflare/core";
|
|
24
|
-
import type { Log } from "miniflare";
|
|
25
|
-
|
|
26
|
-
export interface Options {
|
|
27
|
-
log: Log;
|
|
28
|
-
proxyPort?: number;
|
|
29
|
-
directory?: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export default async function generateASSETSBinding(options: Options) {
|
|
33
|
-
const assetsFetch =
|
|
34
|
-
options.directory !== undefined
|
|
35
|
-
? await generateAssetsFetch(options.directory, options.log)
|
|
36
|
-
: invalidAssetsFetch;
|
|
37
|
-
|
|
38
|
-
return async function (miniflareRequest: MiniflareRequest) {
|
|
39
|
-
if (options.proxyPort) {
|
|
40
|
-
try {
|
|
41
|
-
const url = new URL(miniflareRequest.url);
|
|
42
|
-
url.host = `localhost:${options.proxyPort}`;
|
|
43
|
-
return await miniflareFetch(url, miniflareRequest);
|
|
44
|
-
} catch (thrown) {
|
|
45
|
-
options.log.error(new Error(`Could not proxy request: ${thrown}`));
|
|
46
|
-
|
|
47
|
-
// TODO: Pretty error page
|
|
48
|
-
return new MiniflareResponse(
|
|
49
|
-
`[wrangler] Could not proxy request: ${thrown}`,
|
|
50
|
-
{
|
|
51
|
-
status: 502,
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
try {
|
|
57
|
-
return await assetsFetch(miniflareRequest);
|
|
58
|
-
} catch (thrown) {
|
|
59
|
-
options.log.error(new Error(`Could not serve static asset: ${thrown}`));
|
|
60
|
-
|
|
61
|
-
// TODO: Pretty error page
|
|
62
|
-
return new MiniflareResponse(
|
|
63
|
-
`[wrangler] Could not serve static asset: ${thrown}`,
|
|
64
|
-
{ status: 502 }
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
} as FetcherFetch;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function generateAssetsFetch(
|
|
72
|
-
directory: string,
|
|
73
|
-
log: Log
|
|
74
|
-
): Promise<typeof fetch> {
|
|
75
|
-
// Defer importing miniflare until we really need it
|
|
76
|
-
await import("@cloudflare/pages-shared/environment-polyfills/miniflare");
|
|
77
|
-
|
|
78
|
-
const { generateHandler, parseQualityWeightedList } = await import(
|
|
79
|
-
"@cloudflare/pages-shared/asset-server/handler"
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
const headersFile = join(directory, "_headers");
|
|
83
|
-
const redirectsFile = join(directory, "_redirects");
|
|
84
|
-
const workerFile = join(directory, "_worker.js");
|
|
85
|
-
|
|
86
|
-
const ignoredFiles = [headersFile, redirectsFile, workerFile];
|
|
87
|
-
|
|
88
|
-
let redirects: ParsedRedirects | undefined;
|
|
89
|
-
if (existsSync(redirectsFile)) {
|
|
90
|
-
const contents = readFileSync(redirectsFile, "utf-8");
|
|
91
|
-
redirects = parseRedirects(contents);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
let headers: ParsedHeaders | undefined;
|
|
95
|
-
if (existsSync(headersFile)) {
|
|
96
|
-
const contents = readFileSync(headersFile, "utf-8");
|
|
97
|
-
headers = parseHeaders(contents);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
let metadata = createMetadataObject({
|
|
101
|
-
redirects,
|
|
102
|
-
headers,
|
|
103
|
-
logger: log.warn.bind(log),
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
watch([headersFile, redirectsFile], { persistent: true }).on(
|
|
107
|
-
"change",
|
|
108
|
-
(path) => {
|
|
109
|
-
switch (path) {
|
|
110
|
-
case headersFile: {
|
|
111
|
-
log.log("_headers modified. Re-evaluating...");
|
|
112
|
-
const contents = readFileSync(headersFile).toString();
|
|
113
|
-
headers = parseHeaders(contents);
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
case redirectsFile: {
|
|
117
|
-
log.log("_redirects modified. Re-evaluating...");
|
|
118
|
-
const contents = readFileSync(redirectsFile).toString();
|
|
119
|
-
redirects = parseRedirects(contents);
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
metadata = createMetadataObject({
|
|
125
|
-
redirects,
|
|
126
|
-
headers,
|
|
127
|
-
logger: log.warn,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
const generateResponse = async (request: Request) => {
|
|
133
|
-
const assetKeyEntryMap = new Map<string, string>();
|
|
134
|
-
|
|
135
|
-
return await generateHandler<string>({
|
|
136
|
-
request,
|
|
137
|
-
metadata: metadata as Metadata,
|
|
138
|
-
xServerEnvHeader: "dev",
|
|
139
|
-
logError: console.error,
|
|
140
|
-
findAssetEntryForPath: async (path) => {
|
|
141
|
-
const filepath = join(directory, path);
|
|
142
|
-
|
|
143
|
-
if (
|
|
144
|
-
existsSync(filepath) &&
|
|
145
|
-
lstatSync(filepath).isFile() &&
|
|
146
|
-
!ignoredFiles.includes(filepath)
|
|
147
|
-
) {
|
|
148
|
-
const hash = hashFile(filepath);
|
|
149
|
-
assetKeyEntryMap.set(hash, filepath);
|
|
150
|
-
return hash;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return null;
|
|
154
|
-
},
|
|
155
|
-
getAssetKey: (assetEntry) => {
|
|
156
|
-
return assetEntry;
|
|
157
|
-
},
|
|
158
|
-
negotiateContent: (contentRequest) => {
|
|
159
|
-
let rawAcceptEncoding: string | undefined;
|
|
160
|
-
if (
|
|
161
|
-
contentRequest.cf &&
|
|
162
|
-
"clientAcceptEncoding" in contentRequest.cf &&
|
|
163
|
-
contentRequest.cf.clientAcceptEncoding
|
|
164
|
-
) {
|
|
165
|
-
rawAcceptEncoding = contentRequest.cf.clientAcceptEncoding as string;
|
|
166
|
-
} else {
|
|
167
|
-
rawAcceptEncoding =
|
|
168
|
-
contentRequest.headers.get("Accept-Encoding") || undefined;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const acceptEncoding = parseQualityWeightedList(rawAcceptEncoding);
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
acceptEncoding["identity"] === 0 ||
|
|
175
|
-
(acceptEncoding["*"] === 0 &&
|
|
176
|
-
acceptEncoding["identity"] === undefined)
|
|
177
|
-
) {
|
|
178
|
-
throw new Error("No acceptable encodings available");
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return { encoding: null };
|
|
182
|
-
},
|
|
183
|
-
fetchAsset: async (assetKey) => {
|
|
184
|
-
const filepath = assetKeyEntryMap.get(assetKey);
|
|
185
|
-
if (!filepath) {
|
|
186
|
-
throw new Error(
|
|
187
|
-
"Could not fetch asset. Please file an issue on GitHub (https://github.com/cloudflare/wrangler2/issues/new/choose) with reproduction steps."
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
const body = readFileSync(filepath) as unknown as ReadableStream;
|
|
191
|
-
|
|
192
|
-
let contentType = getType(filepath) || "application/octet-stream";
|
|
193
|
-
if (
|
|
194
|
-
contentType.startsWith("text/") &&
|
|
195
|
-
!contentType.includes("charset")
|
|
196
|
-
) {
|
|
197
|
-
contentType = `${contentType}; charset=utf-8`;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return { body, contentType };
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
return async (input: RequestInfo, init?: RequestInit) => {
|
|
206
|
-
const request = new MiniflareRequest(input, init);
|
|
207
|
-
return await generateResponse(request as unknown as Request);
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const invalidAssetsFetch: typeof fetch = () => {
|
|
212
|
-
throw new Error(
|
|
213
|
-
"Trying to fetch assets directly when there is no `directory` option specified."
|
|
214
|
-
);
|
|
215
|
-
};
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { fetch } from "@miniflare/core";
|
|
2
|
-
import {
|
|
3
|
-
DurableObjectNamespace,
|
|
4
|
-
DurableObjectStub,
|
|
5
|
-
} from "@miniflare/durable-objects";
|
|
6
|
-
import {
|
|
7
|
-
Log,
|
|
8
|
-
Miniflare,
|
|
9
|
-
Response as MiniflareResponse,
|
|
10
|
-
Request as MiniflareRequest,
|
|
11
|
-
} from "miniflare";
|
|
12
|
-
import yargs from "yargs";
|
|
13
|
-
import { hideBin } from "yargs/helpers";
|
|
14
|
-
import { FatalError } from "../errors";
|
|
15
|
-
import generateASSETSBinding from "./assets";
|
|
16
|
-
import { getRequestContextCheckOptions } from "./request-context";
|
|
17
|
-
import type { Options } from "./assets";
|
|
18
|
-
import type { AddressInfo } from "net";
|
|
19
|
-
|
|
20
|
-
export interface EnablePagesAssetsServiceBindingOptions {
|
|
21
|
-
proxyPort?: number;
|
|
22
|
-
directory?: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// miniflare defines this but importing it throws:
|
|
26
|
-
// Dynamic require of "path" is not supported
|
|
27
|
-
class NoOpLog extends Log {
|
|
28
|
-
log(): void {}
|
|
29
|
-
|
|
30
|
-
error(message: Error): void {
|
|
31
|
-
throw message;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async function main() {
|
|
36
|
-
const args = await yargs(hideBin(process.argv)).help(false).version(false)
|
|
37
|
-
.argv;
|
|
38
|
-
|
|
39
|
-
const requestContextCheckOptions = await getRequestContextCheckOptions();
|
|
40
|
-
const config = {
|
|
41
|
-
...JSON.parse((args._[0] as string) ?? "{}"),
|
|
42
|
-
...requestContextCheckOptions,
|
|
43
|
-
};
|
|
44
|
-
const logLevel = config.logLevel.toUpperCase();
|
|
45
|
-
|
|
46
|
-
config.log =
|
|
47
|
-
config.logLevel === "none"
|
|
48
|
-
? new NoOpLog()
|
|
49
|
-
: new Log(logLevel, config.logOptions);
|
|
50
|
-
|
|
51
|
-
if (logLevel === "DEBUG" || logLevel === "VERBOSE") {
|
|
52
|
-
console.log("MINIFLARE OPTIONS:\n", JSON.stringify(config, null, 2));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
config.bindings = {
|
|
56
|
-
...config.bindings,
|
|
57
|
-
...(config.externalDurableObjects &&
|
|
58
|
-
Object.fromEntries(
|
|
59
|
-
Object.entries(
|
|
60
|
-
config.externalDurableObjects as Record<
|
|
61
|
-
string,
|
|
62
|
-
{ name: string; host: string; port: number }
|
|
63
|
-
>
|
|
64
|
-
).map(([binding, { name, host, port }]) => {
|
|
65
|
-
const factory = () => {
|
|
66
|
-
throw new FatalError(
|
|
67
|
-
"An external Durable Object instance's state has somehow been attempted to be accessed.",
|
|
68
|
-
1
|
|
69
|
-
);
|
|
70
|
-
};
|
|
71
|
-
const namespace = new DurableObjectNamespace(name as string, factory);
|
|
72
|
-
namespace.get = (id) => {
|
|
73
|
-
const stub = new DurableObjectStub(factory, id);
|
|
74
|
-
stub.fetch = (...reqArgs) => {
|
|
75
|
-
const requestFromArgs = new MiniflareRequest(...reqArgs);
|
|
76
|
-
const url = new URL(requestFromArgs.url);
|
|
77
|
-
url.host = host;
|
|
78
|
-
if (port !== undefined) url.port = port.toString();
|
|
79
|
-
const request = new MiniflareRequest(
|
|
80
|
-
url.toString(),
|
|
81
|
-
requestFromArgs
|
|
82
|
-
);
|
|
83
|
-
request.headers.set("x-miniflare-durable-object-name", name);
|
|
84
|
-
request.headers.set(
|
|
85
|
-
"x-miniflare-durable-object-id",
|
|
86
|
-
id.toString()
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
return fetch(request);
|
|
90
|
-
};
|
|
91
|
-
return stub;
|
|
92
|
-
};
|
|
93
|
-
return [binding, namespace];
|
|
94
|
-
})
|
|
95
|
-
)),
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
let mf: Miniflare | undefined;
|
|
99
|
-
let durableObjectsMf: Miniflare | undefined = undefined;
|
|
100
|
-
let durableObjectsMfPort: number | undefined = undefined;
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
if (args._[1]) {
|
|
104
|
-
const opts: EnablePagesAssetsServiceBindingOptions = JSON.parse(
|
|
105
|
-
args._[1] as string
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
if (isNaN(opts.proxyPort || NaN) && !opts.directory) {
|
|
109
|
-
throw new Error(
|
|
110
|
-
"MiniflareCLIOptions: built in service bindings set to true, but no port or directory provided"
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const options: Options = {
|
|
115
|
-
log: config.log,
|
|
116
|
-
proxyPort: opts.proxyPort,
|
|
117
|
-
directory: opts.directory,
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
config.serviceBindings = {
|
|
121
|
-
...config.serviceBindings,
|
|
122
|
-
ASSETS: await generateASSETSBinding(options),
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
mf = new Miniflare(config);
|
|
126
|
-
// Start Miniflare development server
|
|
127
|
-
const mfServer = await mf.startServer();
|
|
128
|
-
const mfPort = (mfServer.address() as AddressInfo).port;
|
|
129
|
-
await mf.startScheduler();
|
|
130
|
-
|
|
131
|
-
const internalDurableObjectClassNames = Object.values(
|
|
132
|
-
config.durableObjects as Record<string, string>
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
if (internalDurableObjectClassNames.length > 0) {
|
|
136
|
-
durableObjectsMf = new Miniflare({
|
|
137
|
-
host: config.host,
|
|
138
|
-
port: 0,
|
|
139
|
-
script: `
|
|
140
|
-
export default {
|
|
141
|
-
fetch(request, env) {
|
|
142
|
-
return env.DO.fetch(request)
|
|
143
|
-
}
|
|
144
|
-
}`,
|
|
145
|
-
serviceBindings: {
|
|
146
|
-
DO: async (request: MiniflareRequest) => {
|
|
147
|
-
request = new MiniflareRequest(request);
|
|
148
|
-
|
|
149
|
-
const name = request.headers.get("x-miniflare-durable-object-name");
|
|
150
|
-
const idString = request.headers.get(
|
|
151
|
-
"x-miniflare-durable-object-id"
|
|
152
|
-
);
|
|
153
|
-
request.headers.delete("x-miniflare-durable-object-name");
|
|
154
|
-
request.headers.delete("x-miniflare-durable-object-id");
|
|
155
|
-
|
|
156
|
-
if (!name || !idString) {
|
|
157
|
-
return new MiniflareResponse(
|
|
158
|
-
"[durable-object-proxy-err] Missing `x-miniflare-durable-object-name` or `x-miniflare-durable-object-id` headers.",
|
|
159
|
-
{ status: 400 }
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const namespace = await mf?.getDurableObjectNamespace(name);
|
|
164
|
-
const id = namespace?.idFromString(idString);
|
|
165
|
-
|
|
166
|
-
if (!id) {
|
|
167
|
-
return new MiniflareResponse(
|
|
168
|
-
"[durable-object-proxy-err] Could not generate an ID. Possibly due to a mismatched DO name and ID?",
|
|
169
|
-
{ status: 500 }
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const stub = namespace?.get(id);
|
|
174
|
-
|
|
175
|
-
if (!stub) {
|
|
176
|
-
return new MiniflareResponse(
|
|
177
|
-
"[durable-object-proxy-err] Could not generate a stub. Possibly due to a mismatched DO name and ID?",
|
|
178
|
-
{ status: 500 }
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return stub.fetch(request);
|
|
183
|
-
},
|
|
184
|
-
},
|
|
185
|
-
modules: true,
|
|
186
|
-
});
|
|
187
|
-
const server = await durableObjectsMf.startServer();
|
|
188
|
-
durableObjectsMfPort = (server.address() as AddressInfo).port;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
process.send &&
|
|
192
|
-
process.send(
|
|
193
|
-
JSON.stringify({
|
|
194
|
-
mfPort: mfPort,
|
|
195
|
-
ready: true,
|
|
196
|
-
durableObjectsPort: durableObjectsMfPort,
|
|
197
|
-
})
|
|
198
|
-
);
|
|
199
|
-
} catch (e) {
|
|
200
|
-
mf?.log.error(e as Error);
|
|
201
|
-
process.exitCode = 1;
|
|
202
|
-
// Unmount any mounted workers
|
|
203
|
-
await mf?.dispose();
|
|
204
|
-
await durableObjectsMf?.dispose();
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
await main();
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { MiniflareOptions } from "miniflare";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Certain runtime APIs are only available to workers during the "request context",
|
|
5
|
-
* which is any code that returns after receiving a request and before returning
|
|
6
|
-
* a response.
|
|
7
|
-
*
|
|
8
|
-
* Miniflare emulates this behavior by using an [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) and
|
|
9
|
-
* [checking at runtime](https://github.com/cloudflare/miniflare/blob/master/packages/shared/src/context.ts#L21-L36)
|
|
10
|
-
* to see if you're using those APIs during the request context.
|
|
11
|
-
*
|
|
12
|
-
* In certain environments `AsyncLocalStorage` is unavailable, such as in a
|
|
13
|
-
* [webcontainer](https://github.com/stackblitz/webcontainer-core).
|
|
14
|
-
* This function figures out if we're able to run those "request context" checks
|
|
15
|
-
* and returns [a set of options](https://miniflare.dev/core/standards#global-functionality-limits)
|
|
16
|
-
* that indicate to miniflare whether to run the checks or not.
|
|
17
|
-
*/
|
|
18
|
-
export const getRequestContextCheckOptions = async (): Promise<
|
|
19
|
-
Pick<MiniflareOptions, "globalAsyncIO" | "globalTimers" | "globalRandom">
|
|
20
|
-
> => {
|
|
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
|
-
|
|
28
|
-
hasAsyncLocalStorage = storage.run("some-value", () => {
|
|
29
|
-
return storage.getStore() === "some-value";
|
|
30
|
-
});
|
|
31
|
-
} catch (e) {
|
|
32
|
-
hasAsyncLocalStorage = false;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
globalAsyncIO: hasAsyncLocalStorage,
|
|
37
|
-
globalRandom: hasAsyncLocalStorage,
|
|
38
|
-
globalTimers: hasAsyncLocalStorage,
|
|
39
|
-
};
|
|
40
|
-
};
|