wrangler 2.0.12 → 2.0.16
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 +7 -1
- package/bin/wrangler.js +111 -57
- package/miniflare-dist/index.mjs +9 -2
- package/package.json +156 -154
- 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 +3935 -3476
- package/src/__tests__/dev.test.tsx +1128 -979
- package/src/__tests__/guess-worker-format.test.ts +68 -68
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/faye-websocket.d.ts +4 -4
- 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 +92 -92
- package/src/__tests__/helpers/mock-console.ts +49 -39
- package/src/__tests__/helpers/mock-dialogs.ts +94 -71
- package/src/__tests__/helpers/mock-http-server.ts +30 -30
- package/src/__tests__/helpers/mock-istty.ts +65 -18
- package/src/__tests__/helpers/mock-kv.ts +26 -26
- package/src/__tests__/helpers/mock-oauth-flow.ts +223 -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/run-in-tmp.ts +27 -27
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- 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 +104 -104
- package/src/__tests__/index.test.ts +239 -234
- package/src/__tests__/init.test.ts +1605 -1250
- package/src/__tests__/jest.setup.ts +63 -33
- package/src/__tests__/kv.test.ts +1128 -1011
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/package-manager.test.ts +303 -303
- package/src/__tests__/pages.test.ts +1152 -652
- package/src/__tests__/parse.test.ts +252 -252
- package/src/__tests__/publish.test.ts +6371 -5622
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +133 -133
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +382 -377
- package/src/__tests__/tail.test.ts +530 -530
- package/src/__tests__/user.test.ts +123 -111
- package/src/__tests__/whoami.test.tsx +198 -117
- package/src/__tests__/worker-namespace.test.ts +327 -0
- package/src/abort.d.ts +1 -1
- package/src/api/dev.ts +49 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +157 -149
- package/src/cfetch/index.ts +80 -80
- package/src/cfetch/internal.ts +90 -83
- package/src/cli.ts +21 -7
- package/src/config/config.ts +204 -195
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +390 -357
- package/src/config/index.ts +206 -193
- package/src/config/validation-helpers.ts +366 -366
- package/src/config/validation.ts +1573 -1376
- package/src/config-cache.ts +79 -41
- package/src/create-worker-preview.ts +206 -136
- package/src/create-worker-upload-form.ts +247 -238
- package/src/dev/dev-vars.ts +13 -13
- package/src/dev/dev.tsx +329 -307
- package/src/dev/local.tsx +304 -275
- package/src/dev/remote.tsx +366 -224
- package/src/dev/use-esbuild.ts +126 -91
- package/src/dev.tsx +538 -0
- package/src/dialogs.tsx +97 -97
- package/src/durable.ts +87 -87
- package/src/entry.ts +234 -228
- package/src/environment-variables.ts +23 -23
- package/src/errors.ts +6 -6
- package/src/generate.ts +33 -0
- package/src/git-client.ts +42 -0
- package/src/https-options.ts +79 -79
- package/src/index.tsx +1775 -2763
- package/src/init.ts +549 -0
- package/src/inspect.ts +593 -593
- package/src/intl-polyfill.d.ts +123 -123
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +277 -277
- package/src/logger.ts +46 -39
- package/src/miniflare-cli/enum-keys.ts +8 -8
- package/src/miniflare-cli/index.ts +42 -31
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/module-collection.ts +212 -212
- package/src/open-in-browser.ts +4 -6
- package/src/package-manager.ts +123 -123
- package/src/pages/build.tsx +202 -0
- package/src/pages/constants.ts +7 -0
- package/src/pages/deployments.tsx +101 -0
- package/src/pages/dev.tsx +964 -0
- package/src/pages/functions/buildPlugin.ts +105 -0
- package/src/pages/functions/buildWorker.ts +151 -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.ts +151 -0
- package/src/pages/index.tsx +84 -0
- package/src/pages/projects.tsx +157 -0
- package/src/pages/publish.tsx +335 -0
- package/src/pages/types.ts +40 -0
- package/src/pages/upload.tsx +384 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +202 -138
- package/src/paths.ts +6 -6
- package/src/preview.ts +31 -0
- package/src/proxy.ts +400 -402
- package/src/publish.ts +667 -621
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +577 -0
- package/src/r2.ts +19 -19
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +271 -225
- package/src/tail/filters.ts +108 -108
- package/src/tail/index.ts +217 -217
- package/src/tail/printing.ts +45 -45
- package/src/update-check.ts +11 -11
- package/src/user/choose-account.tsx +60 -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 +1161 -0
- package/src/whoami.tsx +61 -42
- package/src/worker-namespace.ts +190 -0
- package/src/worker.ts +110 -100
- package/src/zones.ts +39 -36
- package/templates/checked-fetch.js +17 -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 +15 -15
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-template-plugin.ts +155 -0
- package/templates/pages-template-worker.ts +161 -0
- package/templates/static-asset-facade.js +31 -31
- package/templates/tsconfig.json +95 -95
- package/wrangler-dist/cli.js +55383 -54138
- 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/pages.tsx +0 -2093
- package/src/user.tsx +0 -1214
|
@@ -0,0 +1,964 @@
|
|
|
1
|
+
import { execSync, spawn } from "node:child_process";
|
|
2
|
+
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join, resolve as resolvePath } from "node:path";
|
|
5
|
+
import { URL } from "node:url";
|
|
6
|
+
import { watch } from "chokidar";
|
|
7
|
+
import { getType } from "mime";
|
|
8
|
+
import { getVarsForDev } from "../dev/dev-vars";
|
|
9
|
+
import { FatalError } from "../errors";
|
|
10
|
+
import { logger } from "../logger";
|
|
11
|
+
import { getRequestContextCheckOptions } from "../miniflare-cli/request-context";
|
|
12
|
+
import openInBrowser from "../open-in-browser";
|
|
13
|
+
import { buildFunctions } from "./build";
|
|
14
|
+
import { SECONDS_TO_WAIT_FOR_PROXY } from "./constants";
|
|
15
|
+
import { CLEANUP, CLEANUP_CALLBACKS, pagesBetaWarning } from "./utils";
|
|
16
|
+
import type { Config } from "../config";
|
|
17
|
+
import type {
|
|
18
|
+
fetch as miniflareFetch,
|
|
19
|
+
Headers as MiniflareHeaders,
|
|
20
|
+
} from "@miniflare/core";
|
|
21
|
+
import type { MiniflareOptions, Request as MiniflareRequest } from "miniflare";
|
|
22
|
+
import type { Argv, ArgumentsCamelCase } from "yargs";
|
|
23
|
+
|
|
24
|
+
type PagesDevArgs = {
|
|
25
|
+
directory?: string;
|
|
26
|
+
command?: string;
|
|
27
|
+
local: boolean;
|
|
28
|
+
port: number;
|
|
29
|
+
proxy?: number;
|
|
30
|
+
"script-path": string;
|
|
31
|
+
binding?: (string | number)[];
|
|
32
|
+
kv?: (string | number)[];
|
|
33
|
+
do?: (string | number)[];
|
|
34
|
+
"live-reload": boolean;
|
|
35
|
+
"node-compat": boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export function Options(yargs: Argv): Argv<PagesDevArgs> {
|
|
39
|
+
return yargs
|
|
40
|
+
.positional("directory", {
|
|
41
|
+
type: "string",
|
|
42
|
+
demandOption: undefined,
|
|
43
|
+
description: "The directory of static assets to serve",
|
|
44
|
+
})
|
|
45
|
+
.positional("command", {
|
|
46
|
+
type: "string",
|
|
47
|
+
demandOption: undefined,
|
|
48
|
+
description: "The proxy command to run",
|
|
49
|
+
})
|
|
50
|
+
.options({
|
|
51
|
+
local: {
|
|
52
|
+
type: "boolean",
|
|
53
|
+
default: true,
|
|
54
|
+
description: "Run on my machine",
|
|
55
|
+
},
|
|
56
|
+
port: {
|
|
57
|
+
type: "number",
|
|
58
|
+
default: 8788,
|
|
59
|
+
description: "The port to listen on (serve from)",
|
|
60
|
+
},
|
|
61
|
+
proxy: {
|
|
62
|
+
type: "number",
|
|
63
|
+
description: "The port to proxy (where the static assets are served)",
|
|
64
|
+
},
|
|
65
|
+
"script-path": {
|
|
66
|
+
type: "string",
|
|
67
|
+
default: "_worker.js",
|
|
68
|
+
description:
|
|
69
|
+
"The location of the single Worker script if not using functions",
|
|
70
|
+
},
|
|
71
|
+
binding: {
|
|
72
|
+
type: "array",
|
|
73
|
+
description: "Bind variable/secret (KEY=VALUE)",
|
|
74
|
+
alias: "b",
|
|
75
|
+
},
|
|
76
|
+
kv: {
|
|
77
|
+
type: "array",
|
|
78
|
+
description: "KV namespace to bind",
|
|
79
|
+
alias: "k",
|
|
80
|
+
},
|
|
81
|
+
do: {
|
|
82
|
+
type: "array",
|
|
83
|
+
description: "Durable Object to bind (NAME=CLASS)",
|
|
84
|
+
alias: "o",
|
|
85
|
+
},
|
|
86
|
+
"live-reload": {
|
|
87
|
+
type: "boolean",
|
|
88
|
+
default: false,
|
|
89
|
+
description: "Auto reload HTML pages when change is detected",
|
|
90
|
+
},
|
|
91
|
+
"node-compat": {
|
|
92
|
+
describe: "Enable node.js compatibility",
|
|
93
|
+
default: false,
|
|
94
|
+
type: "boolean",
|
|
95
|
+
hidden: true,
|
|
96
|
+
},
|
|
97
|
+
config: {
|
|
98
|
+
describe: "Pages does not support wrangler.toml",
|
|
99
|
+
type: "string",
|
|
100
|
+
hidden: true,
|
|
101
|
+
},
|
|
102
|
+
// // TODO: Miniflare user options
|
|
103
|
+
})
|
|
104
|
+
.epilogue(pagesBetaWarning);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const Handler = async ({
|
|
108
|
+
local,
|
|
109
|
+
directory,
|
|
110
|
+
port,
|
|
111
|
+
proxy: requestedProxyPort,
|
|
112
|
+
"script-path": singleWorkerScriptPath,
|
|
113
|
+
binding: bindings = [],
|
|
114
|
+
kv: kvs = [],
|
|
115
|
+
do: durableObjects = [],
|
|
116
|
+
"live-reload": liveReload,
|
|
117
|
+
"node-compat": nodeCompat,
|
|
118
|
+
config: config,
|
|
119
|
+
_: [_pages, _dev, ...remaining],
|
|
120
|
+
}: ArgumentsCamelCase<PagesDevArgs>) => {
|
|
121
|
+
// Beta message for `wrangler pages <commands>` usage
|
|
122
|
+
logger.log(pagesBetaWarning);
|
|
123
|
+
|
|
124
|
+
if (!local) {
|
|
125
|
+
throw new FatalError("Only local mode is supported at the moment.", 1);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (config) {
|
|
129
|
+
throw new FatalError("Pages does not support wrangler.toml", 1);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const functionsDirectory = "./functions";
|
|
133
|
+
const usingFunctions = existsSync(functionsDirectory);
|
|
134
|
+
|
|
135
|
+
const command = remaining as (string | number)[];
|
|
136
|
+
|
|
137
|
+
let proxyPort: number | void;
|
|
138
|
+
|
|
139
|
+
if (directory === undefined) {
|
|
140
|
+
proxyPort = await spawnProxyProcess({
|
|
141
|
+
port: requestedProxyPort,
|
|
142
|
+
command,
|
|
143
|
+
});
|
|
144
|
+
if (proxyPort === undefined) return undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let miniflareArgs: MiniflareOptions = {};
|
|
148
|
+
|
|
149
|
+
let scriptReadyResolve: () => void;
|
|
150
|
+
const scriptReadyPromise = new Promise<void>(
|
|
151
|
+
(resolve) => (scriptReadyResolve = resolve)
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
if (usingFunctions) {
|
|
155
|
+
const outfile = join(tmpdir(), `./functionsWorker-${Math.random()}.js`);
|
|
156
|
+
|
|
157
|
+
if (nodeCompat) {
|
|
158
|
+
console.warn(
|
|
159
|
+
"Enabling node.js compatibility mode for builtins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
logger.log(`Compiling worker to "${outfile}"...`);
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
await buildFunctions({
|
|
167
|
+
outfile,
|
|
168
|
+
functionsDirectory,
|
|
169
|
+
sourcemap: true,
|
|
170
|
+
watch: true,
|
|
171
|
+
onEnd: () => scriptReadyResolve(),
|
|
172
|
+
buildOutputDirectory: directory,
|
|
173
|
+
nodeCompat,
|
|
174
|
+
});
|
|
175
|
+
} catch {}
|
|
176
|
+
|
|
177
|
+
watch([functionsDirectory], {
|
|
178
|
+
persistent: true,
|
|
179
|
+
ignoreInitial: true,
|
|
180
|
+
}).on("all", async () => {
|
|
181
|
+
await buildFunctions({
|
|
182
|
+
outfile,
|
|
183
|
+
functionsDirectory,
|
|
184
|
+
sourcemap: true,
|
|
185
|
+
watch: true,
|
|
186
|
+
onEnd: () => scriptReadyResolve(),
|
|
187
|
+
buildOutputDirectory: directory,
|
|
188
|
+
nodeCompat,
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
miniflareArgs = {
|
|
193
|
+
scriptPath: outfile,
|
|
194
|
+
};
|
|
195
|
+
} else {
|
|
196
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
197
|
+
scriptReadyResolve!();
|
|
198
|
+
|
|
199
|
+
const scriptPath =
|
|
200
|
+
directory !== undefined
|
|
201
|
+
? join(directory, singleWorkerScriptPath)
|
|
202
|
+
: singleWorkerScriptPath;
|
|
203
|
+
|
|
204
|
+
if (existsSync(scriptPath)) {
|
|
205
|
+
miniflareArgs = {
|
|
206
|
+
scriptPath,
|
|
207
|
+
};
|
|
208
|
+
} else {
|
|
209
|
+
logger.log("No functions. Shimming...");
|
|
210
|
+
miniflareArgs = {
|
|
211
|
+
// cfFetch sets the `cf` object that a function could expect
|
|
212
|
+
// If there are no functions, there's no reason to set this up (and not make that network call)
|
|
213
|
+
cfFetch: false,
|
|
214
|
+
// TODO: The fact that these request/response hacks are necessary is ridiculous.
|
|
215
|
+
// We need to eliminate them from env.ASSETS.fetch (not sure if just local or prod as well)
|
|
216
|
+
script: `
|
|
217
|
+
export default {
|
|
218
|
+
async fetch(request, env, context) {
|
|
219
|
+
const response = await env.ASSETS.fetch(request.url, request)
|
|
220
|
+
return new Response(response.body, response)
|
|
221
|
+
}
|
|
222
|
+
}`,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Defer importing miniflare until we really need it
|
|
228
|
+
const { Miniflare, Log, LogLevel } = await import("miniflare");
|
|
229
|
+
const { Response, fetch } = await import("@miniflare/core");
|
|
230
|
+
|
|
231
|
+
// Wait for esbuild to finish building before starting Miniflare.
|
|
232
|
+
// This must be before the call to `new Miniflare`, as that will
|
|
233
|
+
// asynchronously start loading the script. `await startServer()`
|
|
234
|
+
// internally just waits for that promise to resolve.
|
|
235
|
+
await scriptReadyPromise;
|
|
236
|
+
|
|
237
|
+
// `assetsFetch()` will only be called if there is `proxyPort` defined.
|
|
238
|
+
// We only define `proxyPort`, above, when there is no `directory` defined.
|
|
239
|
+
const assetsFetch =
|
|
240
|
+
directory !== undefined
|
|
241
|
+
? await generateAssetsFetch(directory)
|
|
242
|
+
: invalidAssetsFetch;
|
|
243
|
+
|
|
244
|
+
const requestContextCheckOptions = await getRequestContextCheckOptions();
|
|
245
|
+
|
|
246
|
+
const vars = getVarsForDev({
|
|
247
|
+
configPath: resolvePath(".dev.vars"),
|
|
248
|
+
} as Config);
|
|
249
|
+
|
|
250
|
+
const miniflare = new Miniflare({
|
|
251
|
+
port,
|
|
252
|
+
watch: true,
|
|
253
|
+
modules: true,
|
|
254
|
+
|
|
255
|
+
log: new Log(LogLevel.ERROR, { prefix: "pages" }),
|
|
256
|
+
logUnhandledRejections: true,
|
|
257
|
+
sourceMap: true,
|
|
258
|
+
|
|
259
|
+
kvNamespaces: kvs.map((kv) => kv.toString()),
|
|
260
|
+
|
|
261
|
+
durableObjects: Object.fromEntries(
|
|
262
|
+
durableObjects.map((durableObject) => durableObject.toString().split("="))
|
|
263
|
+
),
|
|
264
|
+
|
|
265
|
+
// User bindings
|
|
266
|
+
bindings: {
|
|
267
|
+
...vars,
|
|
268
|
+
...Object.fromEntries(
|
|
269
|
+
bindings
|
|
270
|
+
.map((binding) => binding.toString().split("="))
|
|
271
|
+
.map(([key, ...values]) => [key, values.join("=")])
|
|
272
|
+
),
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
// env.ASSETS.fetch
|
|
276
|
+
serviceBindings: {
|
|
277
|
+
async ASSETS(request: MiniflareRequest) {
|
|
278
|
+
if (proxyPort) {
|
|
279
|
+
try {
|
|
280
|
+
const url = new URL(request.url);
|
|
281
|
+
url.host = `localhost:${proxyPort}`;
|
|
282
|
+
return await fetch(url, request);
|
|
283
|
+
} catch (thrown) {
|
|
284
|
+
logger.error(`Could not proxy request: ${thrown}`);
|
|
285
|
+
|
|
286
|
+
// TODO: Pretty error page
|
|
287
|
+
return new Response(
|
|
288
|
+
`[wrangler] Could not proxy request: ${thrown}`,
|
|
289
|
+
{ status: 502 }
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
try {
|
|
294
|
+
return await assetsFetch(request);
|
|
295
|
+
} catch (thrown) {
|
|
296
|
+
logger.error(`Could not serve static asset: ${thrown}`);
|
|
297
|
+
|
|
298
|
+
// TODO: Pretty error page
|
|
299
|
+
return new Response(
|
|
300
|
+
`[wrangler] Could not serve static asset: ${thrown}`,
|
|
301
|
+
{ status: 502 }
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
kvPersist: true,
|
|
309
|
+
durableObjectsPersist: true,
|
|
310
|
+
cachePersist: true,
|
|
311
|
+
liveReload,
|
|
312
|
+
|
|
313
|
+
...requestContextCheckOptions,
|
|
314
|
+
...miniflareArgs,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
// `startServer` might throw if user code contains errors
|
|
319
|
+
const server = await miniflare.startServer();
|
|
320
|
+
logger.log(`Serving at http://localhost:${port}/`);
|
|
321
|
+
|
|
322
|
+
if (process.env.BROWSER !== "none") {
|
|
323
|
+
await openInBrowser(`http://localhost:${port}/`);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (directory !== undefined && liveReload) {
|
|
327
|
+
watch([directory], {
|
|
328
|
+
persistent: true,
|
|
329
|
+
ignoreInitial: true,
|
|
330
|
+
}).on("all", async () => {
|
|
331
|
+
await miniflare.reload();
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
CLEANUP_CALLBACKS.push(() => {
|
|
336
|
+
server.close();
|
|
337
|
+
miniflare.dispose().catch((err) => miniflare.log.error(err));
|
|
338
|
+
});
|
|
339
|
+
} catch (e) {
|
|
340
|
+
miniflare.log.error(e as Error);
|
|
341
|
+
CLEANUP();
|
|
342
|
+
throw new FatalError("Could not start Miniflare.", 1);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
function isWindows() {
|
|
347
|
+
return process.platform === "win32";
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async function sleep(ms: number) {
|
|
351
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function getPids(pid: number) {
|
|
355
|
+
const pids: number[] = [pid];
|
|
356
|
+
let command: string, regExp: RegExp;
|
|
357
|
+
|
|
358
|
+
if (isWindows()) {
|
|
359
|
+
command = `wmic process where (ParentProcessId=${pid}) get ProcessId`;
|
|
360
|
+
regExp = new RegExp(/(\d+)/);
|
|
361
|
+
} else {
|
|
362
|
+
command = `pgrep -P ${pid}`;
|
|
363
|
+
regExp = new RegExp(/(\d+)/);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
try {
|
|
367
|
+
const newPids = (
|
|
368
|
+
execSync(command)
|
|
369
|
+
.toString()
|
|
370
|
+
.split("\n")
|
|
371
|
+
.map((line) => line.match(regExp))
|
|
372
|
+
.filter((line) => line !== null) as RegExpExecArray[]
|
|
373
|
+
).map((match) => parseInt(match[1]));
|
|
374
|
+
|
|
375
|
+
pids.push(...newPids.map(getPids).flat());
|
|
376
|
+
} catch {}
|
|
377
|
+
|
|
378
|
+
return pids;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function getPort(pid: number) {
|
|
382
|
+
let command: string, regExp: RegExp;
|
|
383
|
+
|
|
384
|
+
if (isWindows()) {
|
|
385
|
+
command = "\\windows\\system32\\netstat.exe -nao";
|
|
386
|
+
regExp = new RegExp(`TCP\\s+.*:(\\d+)\\s+.*:\\d+\\s+LISTENING\\s+${pid}`);
|
|
387
|
+
} else {
|
|
388
|
+
command = "lsof -nPi";
|
|
389
|
+
regExp = new RegExp(`${pid}\\s+.*TCP\\s+.*:(\\d+)\\s+\\(LISTEN\\)`);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
try {
|
|
393
|
+
const matches = execSync(command)
|
|
394
|
+
.toString()
|
|
395
|
+
.split("\n")
|
|
396
|
+
.map((line) => line.match(regExp))
|
|
397
|
+
.filter((line) => line !== null) as RegExpExecArray[];
|
|
398
|
+
|
|
399
|
+
const match = matches[0];
|
|
400
|
+
if (match) return parseInt(match[1]);
|
|
401
|
+
} catch (thrown) {
|
|
402
|
+
logger.error(
|
|
403
|
+
`Error scanning for ports of process with PID ${pid}: ${thrown}`
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
async function spawnProxyProcess({
|
|
409
|
+
port,
|
|
410
|
+
command,
|
|
411
|
+
}: {
|
|
412
|
+
port?: number;
|
|
413
|
+
command: (string | number)[];
|
|
414
|
+
}): Promise<void | number> {
|
|
415
|
+
if (command.length === 0) {
|
|
416
|
+
CLEANUP();
|
|
417
|
+
throw new FatalError(
|
|
418
|
+
"Must specify a directory of static assets to serve or a command to run.",
|
|
419
|
+
1
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
logger.log(`Running ${command.join(" ")}...`);
|
|
424
|
+
const proxy = spawn(
|
|
425
|
+
command[0].toString(),
|
|
426
|
+
command.slice(1).map((value) => value.toString()),
|
|
427
|
+
{
|
|
428
|
+
shell: isWindows(),
|
|
429
|
+
env: {
|
|
430
|
+
BROWSER: "none",
|
|
431
|
+
...process.env,
|
|
432
|
+
},
|
|
433
|
+
}
|
|
434
|
+
);
|
|
435
|
+
CLEANUP_CALLBACKS.push(() => {
|
|
436
|
+
proxy.kill();
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
proxy.stdout.on("data", (data) => {
|
|
440
|
+
logger.log(`[proxy]: ${data}`);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
proxy.stderr.on("data", (data) => {
|
|
444
|
+
logger.error(`[proxy]: ${data}`);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
proxy.on("close", (code) => {
|
|
448
|
+
logger.error(`Proxy exited with status ${code}.`);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
// Wait for proxy process to start...
|
|
452
|
+
while (!proxy.pid) {}
|
|
453
|
+
|
|
454
|
+
if (port === undefined) {
|
|
455
|
+
logger.log(
|
|
456
|
+
`Sleeping ${SECONDS_TO_WAIT_FOR_PROXY} seconds to allow proxy process to start before attempting to automatically determine port...`
|
|
457
|
+
);
|
|
458
|
+
logger.log("To skip, specify the proxy port with --proxy.");
|
|
459
|
+
await sleep(SECONDS_TO_WAIT_FOR_PROXY * 1000);
|
|
460
|
+
|
|
461
|
+
port = getPids(proxy.pid)
|
|
462
|
+
.map(getPort)
|
|
463
|
+
.filter((nr) => nr !== undefined)[0];
|
|
464
|
+
|
|
465
|
+
if (port === undefined) {
|
|
466
|
+
CLEANUP();
|
|
467
|
+
throw new FatalError(
|
|
468
|
+
"Could not automatically determine proxy port. Please specify the proxy port with --proxy.",
|
|
469
|
+
1
|
|
470
|
+
);
|
|
471
|
+
} else {
|
|
472
|
+
logger.log(`Automatically determined the proxy port to be ${port}.`);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return port;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function escapeRegex(str: string) {
|
|
480
|
+
return str.replace(/[-/\\^$*+?.()|[]{}]/g, "\\$&");
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
type Replacements = Record<string, string>;
|
|
484
|
+
|
|
485
|
+
function replacer(str: string, replacements: Replacements) {
|
|
486
|
+
for (const [replacement, value] of Object.entries(replacements)) {
|
|
487
|
+
str = str.replace(`:${replacement}`, value);
|
|
488
|
+
}
|
|
489
|
+
return str;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function generateRulesMatcher<T>(
|
|
493
|
+
rules?: Record<string, T>,
|
|
494
|
+
replacerFn: (match: T, replacements: Replacements) => T = (match) => match
|
|
495
|
+
) {
|
|
496
|
+
// TODO: How can you test cross-host rules?
|
|
497
|
+
if (!rules) return () => [];
|
|
498
|
+
|
|
499
|
+
const compiledRules = Object.entries(rules)
|
|
500
|
+
.map(([rule, match]) => {
|
|
501
|
+
const crossHost = rule.startsWith("https://");
|
|
502
|
+
|
|
503
|
+
rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
|
|
504
|
+
|
|
505
|
+
const host_matches = rule.matchAll(
|
|
506
|
+
/(?<=^https:\\\/\\\/[^/]*?):([^\\]+)(?=\\)/g
|
|
507
|
+
);
|
|
508
|
+
for (const hostMatch of host_matches) {
|
|
509
|
+
rule = rule.split(hostMatch[0]).join(`(?<${hostMatch[1]}>[^/.]+)`);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const path_matches = rule.matchAll(/:(\w+)/g);
|
|
513
|
+
for (const pathMatch of path_matches) {
|
|
514
|
+
rule = rule.split(pathMatch[0]).join(`(?<${pathMatch[1]}>[^/]+)`);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
rule = "^" + rule + "$";
|
|
518
|
+
|
|
519
|
+
try {
|
|
520
|
+
const regExp = new RegExp(rule);
|
|
521
|
+
return [{ crossHost, regExp }, match];
|
|
522
|
+
} catch {}
|
|
523
|
+
})
|
|
524
|
+
.filter((value) => value !== undefined) as [
|
|
525
|
+
{ crossHost: boolean; regExp: RegExp },
|
|
526
|
+
T
|
|
527
|
+
][];
|
|
528
|
+
|
|
529
|
+
return ({ request }: { request: MiniflareRequest }) => {
|
|
530
|
+
const { pathname, host } = new URL(request.url);
|
|
531
|
+
|
|
532
|
+
return compiledRules
|
|
533
|
+
.map(([{ crossHost, regExp }, match]) => {
|
|
534
|
+
const test = crossHost ? `https://${host}${pathname}` : pathname;
|
|
535
|
+
const result = regExp.exec(test);
|
|
536
|
+
if (result) {
|
|
537
|
+
return replacerFn(match, result.groups || {});
|
|
538
|
+
}
|
|
539
|
+
})
|
|
540
|
+
.filter((value) => value !== undefined) as T[];
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function generateHeadersMatcher(headersFile: string) {
|
|
545
|
+
if (existsSync(headersFile)) {
|
|
546
|
+
const contents = readFileSync(headersFile).toString();
|
|
547
|
+
|
|
548
|
+
// TODO: Log errors
|
|
549
|
+
const lines = contents
|
|
550
|
+
.split("\n")
|
|
551
|
+
.map((line) => line.trim())
|
|
552
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
553
|
+
|
|
554
|
+
const rules: Record<string, Record<string, string>> = {};
|
|
555
|
+
let rule: { path: string; headers: Record<string, string> } | undefined =
|
|
556
|
+
undefined;
|
|
557
|
+
|
|
558
|
+
for (const line of lines) {
|
|
559
|
+
if (/^([^\s]+:\/\/|^\/)/.test(line)) {
|
|
560
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
561
|
+
rules[rule.path] = rule.headers;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const path = validateURL(line);
|
|
565
|
+
if (path) {
|
|
566
|
+
rule = {
|
|
567
|
+
path,
|
|
568
|
+
headers: {},
|
|
569
|
+
};
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (!line.includes(":")) continue;
|
|
575
|
+
|
|
576
|
+
const [rawName, ...rawValue] = line.split(":");
|
|
577
|
+
const name = rawName.trim().toLowerCase();
|
|
578
|
+
const value = rawValue.join(":").trim();
|
|
579
|
+
|
|
580
|
+
if (name === "") continue;
|
|
581
|
+
if (!rule) continue;
|
|
582
|
+
|
|
583
|
+
const existingValues = rule.headers[name];
|
|
584
|
+
rule.headers[name] = existingValues
|
|
585
|
+
? `${existingValues}, ${value}`
|
|
586
|
+
: value;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
590
|
+
rules[rule.path] = rule.headers;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const rulesMatcher = generateRulesMatcher(rules, (match, replacements) =>
|
|
594
|
+
Object.fromEntries(
|
|
595
|
+
Object.entries(match).map(([name, value]) => [
|
|
596
|
+
name,
|
|
597
|
+
replacer(value, replacements),
|
|
598
|
+
])
|
|
599
|
+
)
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
return (request: MiniflareRequest) => {
|
|
603
|
+
const matches = rulesMatcher({
|
|
604
|
+
request,
|
|
605
|
+
});
|
|
606
|
+
if (matches) return matches;
|
|
607
|
+
};
|
|
608
|
+
} else {
|
|
609
|
+
return () => undefined;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function generateRedirectsMatcher(redirectsFile: string) {
|
|
614
|
+
if (existsSync(redirectsFile)) {
|
|
615
|
+
const contents = readFileSync(redirectsFile).toString();
|
|
616
|
+
|
|
617
|
+
// TODO: Log errors
|
|
618
|
+
const lines = contents
|
|
619
|
+
.split("\n")
|
|
620
|
+
.map((line) => line.trim())
|
|
621
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
622
|
+
|
|
623
|
+
const rules = Object.fromEntries(
|
|
624
|
+
lines
|
|
625
|
+
.map((line) => line.split(" "))
|
|
626
|
+
.filter((tokens) => tokens.length === 2 || tokens.length === 3)
|
|
627
|
+
.map((tokens) => {
|
|
628
|
+
const from = validateURL(tokens[0], true, false, false);
|
|
629
|
+
const to = validateURL(tokens[1], false, true, true);
|
|
630
|
+
let status: number | undefined = parseInt(tokens[2]) || 302;
|
|
631
|
+
status = [301, 302, 303, 307, 308].includes(status)
|
|
632
|
+
? status
|
|
633
|
+
: undefined;
|
|
634
|
+
|
|
635
|
+
return from && to && status ? [from, { to, status }] : undefined;
|
|
636
|
+
})
|
|
637
|
+
.filter((rule) => rule !== undefined) as [
|
|
638
|
+
string,
|
|
639
|
+
{ to: string; status?: number }
|
|
640
|
+
][]
|
|
641
|
+
);
|
|
642
|
+
|
|
643
|
+
const rulesMatcher = generateRulesMatcher(
|
|
644
|
+
rules,
|
|
645
|
+
({ status, to }, replacements) => ({
|
|
646
|
+
status,
|
|
647
|
+
to: replacer(to, replacements),
|
|
648
|
+
})
|
|
649
|
+
);
|
|
650
|
+
|
|
651
|
+
return (request: MiniflareRequest) => {
|
|
652
|
+
const match = rulesMatcher({
|
|
653
|
+
request,
|
|
654
|
+
})[0];
|
|
655
|
+
if (match) return match;
|
|
656
|
+
};
|
|
657
|
+
} else {
|
|
658
|
+
return () => undefined;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function extractPathname(
|
|
663
|
+
path = "/",
|
|
664
|
+
includeSearch: boolean,
|
|
665
|
+
includeHash: boolean
|
|
666
|
+
) {
|
|
667
|
+
if (!path.startsWith("/")) path = `/${path}`;
|
|
668
|
+
const url = new URL(`//${path}`, "relative://");
|
|
669
|
+
return `${url.pathname}${includeSearch ? url.search : ""}${
|
|
670
|
+
includeHash ? url.hash : ""
|
|
671
|
+
}`;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
function validateURL(
|
|
675
|
+
token: string,
|
|
676
|
+
onlyRelative = false,
|
|
677
|
+
includeSearch = false,
|
|
678
|
+
includeHash = false
|
|
679
|
+
) {
|
|
680
|
+
const host = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/.exec(token);
|
|
681
|
+
if (host && host.groups && host.groups.host) {
|
|
682
|
+
if (onlyRelative) return;
|
|
683
|
+
|
|
684
|
+
return `https://${host.groups.host}${extractPathname(
|
|
685
|
+
host.groups.path,
|
|
686
|
+
includeSearch,
|
|
687
|
+
includeHash
|
|
688
|
+
)}`;
|
|
689
|
+
} else {
|
|
690
|
+
if (!token.startsWith("/") && onlyRelative) token = `/${token}`;
|
|
691
|
+
|
|
692
|
+
const path = /^\//.exec(token);
|
|
693
|
+
if (path) {
|
|
694
|
+
try {
|
|
695
|
+
return extractPathname(token, includeSearch, includeHash);
|
|
696
|
+
} catch {}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return "";
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function hasFileExtension(pathname: string) {
|
|
703
|
+
return /\/.+\.[a-z0-9]+$/i.test(pathname);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
async function generateAssetsFetch(
|
|
707
|
+
directory: string
|
|
708
|
+
): Promise<typeof miniflareFetch> {
|
|
709
|
+
// Defer importing miniflare until we really need it
|
|
710
|
+
const { Headers, Request, Response } = await import("@miniflare/core");
|
|
711
|
+
|
|
712
|
+
const headersFile = join(directory, "_headers");
|
|
713
|
+
const redirectsFile = join(directory, "_redirects");
|
|
714
|
+
const workerFile = join(directory, "_worker.js");
|
|
715
|
+
|
|
716
|
+
const ignoredFiles = [headersFile, redirectsFile, workerFile];
|
|
717
|
+
|
|
718
|
+
const assetExists = (path: string) => {
|
|
719
|
+
path = join(directory, path);
|
|
720
|
+
return (
|
|
721
|
+
existsSync(path) &&
|
|
722
|
+
lstatSync(path).isFile() &&
|
|
723
|
+
!ignoredFiles.includes(path)
|
|
724
|
+
);
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
const getAsset = (path: string) => {
|
|
728
|
+
if (assetExists(path)) {
|
|
729
|
+
return join(directory, path);
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
let redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
734
|
+
let headersMatcher = generateHeadersMatcher(headersFile);
|
|
735
|
+
|
|
736
|
+
watch([headersFile, redirectsFile], {
|
|
737
|
+
persistent: true,
|
|
738
|
+
}).on("change", (path) => {
|
|
739
|
+
switch (path) {
|
|
740
|
+
case headersFile: {
|
|
741
|
+
logger.log("_headers modified. Re-evaluating...");
|
|
742
|
+
headersMatcher = generateHeadersMatcher(headersFile);
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
case redirectsFile: {
|
|
746
|
+
logger.log("_redirects modified. Re-evaluating...");
|
|
747
|
+
redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
const serveAsset = (file: string) => {
|
|
754
|
+
return readFileSync(file);
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
const generateResponse = (request: MiniflareRequest) => {
|
|
758
|
+
const url = new URL(request.url);
|
|
759
|
+
|
|
760
|
+
const deconstructedResponse: {
|
|
761
|
+
status: number;
|
|
762
|
+
headers: MiniflareHeaders;
|
|
763
|
+
body?: Buffer;
|
|
764
|
+
} = {
|
|
765
|
+
status: 200,
|
|
766
|
+
headers: new Headers(),
|
|
767
|
+
body: undefined,
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
const match = redirectsMatcher(request);
|
|
771
|
+
if (match) {
|
|
772
|
+
const { status, to } = match;
|
|
773
|
+
|
|
774
|
+
let location = to;
|
|
775
|
+
let search;
|
|
776
|
+
|
|
777
|
+
if (to.startsWith("/")) {
|
|
778
|
+
search = new URL(location, "http://fakehost").search;
|
|
779
|
+
} else {
|
|
780
|
+
search = new URL(location).search;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
location = `${location}${search ? "" : url.search}`;
|
|
784
|
+
|
|
785
|
+
if (status && [301, 302, 303, 307, 308].includes(status)) {
|
|
786
|
+
deconstructedResponse.status = status;
|
|
787
|
+
} else {
|
|
788
|
+
deconstructedResponse.status = 302;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
deconstructedResponse.headers.set("Location", location);
|
|
792
|
+
return deconstructedResponse;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
if (!request.method?.match(/^(get|head)$/i)) {
|
|
796
|
+
deconstructedResponse.status = 405;
|
|
797
|
+
return deconstructedResponse;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
const notFound = () => {
|
|
801
|
+
let cwd = url.pathname;
|
|
802
|
+
while (cwd) {
|
|
803
|
+
cwd = cwd.slice(0, cwd.lastIndexOf("/"));
|
|
804
|
+
|
|
805
|
+
if ((asset = getAsset(`${cwd}/404.html`))) {
|
|
806
|
+
deconstructedResponse.status = 404;
|
|
807
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
808
|
+
deconstructedResponse.headers.set(
|
|
809
|
+
"Content-Type",
|
|
810
|
+
getType(asset) || "application/octet-stream"
|
|
811
|
+
);
|
|
812
|
+
return deconstructedResponse;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
if ((asset = getAsset(`/index.html`))) {
|
|
817
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
818
|
+
deconstructedResponse.headers.set(
|
|
819
|
+
"Content-Type",
|
|
820
|
+
getType(asset) || "application/octet-stream"
|
|
821
|
+
);
|
|
822
|
+
return deconstructedResponse;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
deconstructedResponse.status = 404;
|
|
826
|
+
return deconstructedResponse;
|
|
827
|
+
};
|
|
828
|
+
|
|
829
|
+
let asset;
|
|
830
|
+
|
|
831
|
+
if (url.pathname.endsWith("/")) {
|
|
832
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
833
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
834
|
+
deconstructedResponse.headers.set(
|
|
835
|
+
"Content-Type",
|
|
836
|
+
getType(asset) || "application/octet-stream"
|
|
837
|
+
);
|
|
838
|
+
return deconstructedResponse;
|
|
839
|
+
} else if (
|
|
840
|
+
(asset = getAsset(`${url.pathname.replace(/\/$/, ".html")}`))
|
|
841
|
+
) {
|
|
842
|
+
deconstructedResponse.status = 301;
|
|
843
|
+
deconstructedResponse.headers.set(
|
|
844
|
+
"Location",
|
|
845
|
+
`${url.pathname.slice(0, -1)}${url.search}`
|
|
846
|
+
);
|
|
847
|
+
return deconstructedResponse;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
if (url.pathname.endsWith("/index")) {
|
|
852
|
+
deconstructedResponse.status = 301;
|
|
853
|
+
deconstructedResponse.headers.set(
|
|
854
|
+
"Location",
|
|
855
|
+
`${url.pathname.slice(0, -"index".length)}${url.search}`
|
|
856
|
+
);
|
|
857
|
+
return deconstructedResponse;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
if ((asset = getAsset(url.pathname))) {
|
|
861
|
+
if (url.pathname.endsWith(".html")) {
|
|
862
|
+
const extensionlessPath = url.pathname.slice(0, -".html".length);
|
|
863
|
+
if (getAsset(extensionlessPath) || extensionlessPath === "/") {
|
|
864
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
865
|
+
deconstructedResponse.headers.set(
|
|
866
|
+
"Content-Type",
|
|
867
|
+
getType(asset) || "application/octet-stream"
|
|
868
|
+
);
|
|
869
|
+
return deconstructedResponse;
|
|
870
|
+
} else {
|
|
871
|
+
deconstructedResponse.status = 301;
|
|
872
|
+
deconstructedResponse.headers.set(
|
|
873
|
+
"Location",
|
|
874
|
+
`${extensionlessPath}${url.search}`
|
|
875
|
+
);
|
|
876
|
+
return deconstructedResponse;
|
|
877
|
+
}
|
|
878
|
+
} else {
|
|
879
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
880
|
+
deconstructedResponse.headers.set(
|
|
881
|
+
"Content-Type",
|
|
882
|
+
getType(asset) || "application/octet-stream"
|
|
883
|
+
);
|
|
884
|
+
return deconstructedResponse;
|
|
885
|
+
}
|
|
886
|
+
} else if (hasFileExtension(url.pathname)) {
|
|
887
|
+
notFound();
|
|
888
|
+
return deconstructedResponse;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
if ((asset = getAsset(`${url.pathname}.html`))) {
|
|
892
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
893
|
+
deconstructedResponse.headers.set(
|
|
894
|
+
"Content-Type",
|
|
895
|
+
getType(asset) || "application/octet-stream"
|
|
896
|
+
);
|
|
897
|
+
return deconstructedResponse;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
901
|
+
deconstructedResponse.status = 301;
|
|
902
|
+
deconstructedResponse.headers.set(
|
|
903
|
+
"Location",
|
|
904
|
+
`${url.pathname}/${url.search}`
|
|
905
|
+
);
|
|
906
|
+
return deconstructedResponse;
|
|
907
|
+
} else {
|
|
908
|
+
notFound();
|
|
909
|
+
return deconstructedResponse;
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
|
|
913
|
+
const attachHeaders = (
|
|
914
|
+
request: MiniflareRequest,
|
|
915
|
+
deconstructedResponse: {
|
|
916
|
+
status: number;
|
|
917
|
+
headers: MiniflareHeaders;
|
|
918
|
+
body?: Buffer;
|
|
919
|
+
}
|
|
920
|
+
) => {
|
|
921
|
+
const headers = deconstructedResponse.headers;
|
|
922
|
+
const newHeaders = new Headers({});
|
|
923
|
+
const matches = headersMatcher(request) || [];
|
|
924
|
+
|
|
925
|
+
matches.forEach((match) => {
|
|
926
|
+
Object.entries(match).forEach(([name, value]) => {
|
|
927
|
+
newHeaders.append(name, `${value}`);
|
|
928
|
+
});
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
const combinedHeaders = {
|
|
932
|
+
...Object.fromEntries(headers.entries()),
|
|
933
|
+
...Object.fromEntries(newHeaders.entries()),
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
deconstructedResponse.headers = new Headers({});
|
|
937
|
+
Object.entries(combinedHeaders).forEach(([name, value]) => {
|
|
938
|
+
if (value) deconstructedResponse.headers.set(name, value);
|
|
939
|
+
});
|
|
940
|
+
};
|
|
941
|
+
|
|
942
|
+
return async (input, init) => {
|
|
943
|
+
const request = new Request(input, init);
|
|
944
|
+
const deconstructedResponse = generateResponse(request);
|
|
945
|
+
attachHeaders(request, deconstructedResponse);
|
|
946
|
+
|
|
947
|
+
const headers = new Headers();
|
|
948
|
+
|
|
949
|
+
[...deconstructedResponse.headers.entries()].forEach(([name, value]) => {
|
|
950
|
+
if (value) headers.set(name, value);
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
return new Response(deconstructedResponse.body, {
|
|
954
|
+
headers,
|
|
955
|
+
status: deconstructedResponse.status,
|
|
956
|
+
});
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
const invalidAssetsFetch: typeof miniflareFetch = () => {
|
|
961
|
+
throw new Error(
|
|
962
|
+
"Trying to fetch assets directly when there is no `directory` option specified, and not in `local` mode."
|
|
963
|
+
);
|
|
964
|
+
};
|