wrangler 0.0.0-e2a26a0 → 0.0.0-e3cab74
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/package.json +16 -7
- package/pages/functions/filepath-routing.test.ts +112 -36
- package/pages/functions/filepath-routing.ts +66 -119
- package/pages/functions/routes.ts +8 -17
- package/pages/functions/template-worker.ts +3 -9
- package/src/__tests__/configuration.test.ts +2185 -0
- package/src/__tests__/dev.test.tsx +48 -6
- package/src/__tests__/guess-worker-format.test.ts +66 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +6 -6
- package/src/__tests__/helpers/mock-bin.ts +4 -1
- package/src/__tests__/helpers/mock-cfetch.ts +2 -2
- package/src/__tests__/helpers/mock-console.ts +11 -5
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +5 -3
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/index.test.ts +343 -20
- package/src/__tests__/jest.setup.ts +21 -1
- package/src/__tests__/kv.test.ts +82 -80
- package/src/__tests__/package-manager.test.ts +2 -2
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +1428 -244
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/secret.test.ts +176 -60
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +564 -0
- package/src/api/form_data.ts +106 -23
- package/src/api/preview.ts +32 -13
- package/src/api/worker.ts +49 -18
- package/src/bundle.ts +133 -0
- package/src/cfetch/index.ts +8 -6
- package/src/cfetch/internal.ts +49 -17
- package/src/cli.ts +10 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +265 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +305 -0
- package/src/config/index.ts +52 -0
- package/src/config/validation-helpers.ts +370 -0
- package/src/config/validation.ts +1017 -0
- package/src/dev.tsx +338 -211
- package/src/entry.ts +57 -0
- package/src/environment-variables.ts +33 -0
- package/src/guess-worker-format.ts +68 -0
- package/src/index.tsx +860 -634
- package/src/inspect.ts +16 -3
- package/src/kv.tsx +20 -29
- package/src/module-collection.ts +141 -34
- package/src/open-in-browser.ts +13 -0
- package/src/package-manager.ts +21 -15
- package/src/pages.tsx +9 -10
- package/src/parse.ts +183 -0
- package/src/proxy.ts +11 -5
- package/src/publish.ts +272 -314
- package/src/r2.ts +50 -0
- package/src/reporting.ts +115 -0
- package/src/sites.tsx +16 -15
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +62 -0
- package/src/user.tsx +74 -44
- package/templates/new-worker.js +1 -1
- package/templates/new-worker.ts +1 -1
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +51308 -36440
- package/wrangler-dist/cli.js.map +3 -3
- package/src/config.ts +0 -641
- package/src/tail.tsx +0 -73
package/src/entry.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { Config } from "./config";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An entry point for the Worker.
|
|
7
|
+
*
|
|
8
|
+
* It consists not just of a `file`, but also of a `directory` that is used to resolve relative paths.
|
|
9
|
+
*/
|
|
10
|
+
export type Entry = { file: string; directory: string };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Compute the entry-point for the Worker.
|
|
14
|
+
*/
|
|
15
|
+
export function getEntry(
|
|
16
|
+
config: Config,
|
|
17
|
+
command: string,
|
|
18
|
+
script?: string
|
|
19
|
+
): Entry {
|
|
20
|
+
let file: string;
|
|
21
|
+
let directory = process.cwd();
|
|
22
|
+
if (script) {
|
|
23
|
+
// If the script name comes from the command line it is relative to the current working directory.
|
|
24
|
+
file = path.resolve(script);
|
|
25
|
+
} else if (config.main === undefined) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler ${command} path/to/script\`) or the \`main\` config field.`
|
|
28
|
+
);
|
|
29
|
+
} else {
|
|
30
|
+
directory = path.resolve(path.dirname(config.configPath ?? "."));
|
|
31
|
+
file = path.resolve(directory, config.main);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!config.build?.command && fileExists(file) === false) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Could not resolve "${path.relative(process.cwd(), file)}".`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return { file, directory };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns true if the given `filePath` exists as-is,
|
|
44
|
+
* or if some version of it (by appending a common extension) exists.
|
|
45
|
+
*/
|
|
46
|
+
export function fileExists(filePath: string): boolean {
|
|
47
|
+
if (path.extname(filePath) !== "") {
|
|
48
|
+
return existsSync(filePath);
|
|
49
|
+
}
|
|
50
|
+
const base = path.join(path.dirname(filePath), path.basename(filePath));
|
|
51
|
+
for (const ext of [".ts", ".tsx", ".js", ".jsx"]) {
|
|
52
|
+
if (existsSync(base + ext)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a function used to access an environment variable.
|
|
3
|
+
*
|
|
4
|
+
* This is not memoized to allow us to change the value at runtime, such as in testing.
|
|
5
|
+
* A warning is shown if the client is using a deprecated version - but only once.
|
|
6
|
+
*/
|
|
7
|
+
export function getEnvironmentVariableFactory({
|
|
8
|
+
variableName,
|
|
9
|
+
deprecatedName,
|
|
10
|
+
defaultValue,
|
|
11
|
+
}: {
|
|
12
|
+
variableName: string;
|
|
13
|
+
deprecatedName?: string;
|
|
14
|
+
defaultValue?: string;
|
|
15
|
+
}) {
|
|
16
|
+
let hasWarned = false;
|
|
17
|
+
return () => {
|
|
18
|
+
if (process.env[variableName]) {
|
|
19
|
+
return process.env[variableName];
|
|
20
|
+
} else if (deprecatedName && process.env[deprecatedName]) {
|
|
21
|
+
if (!hasWarned) {
|
|
22
|
+
// Only show the warning once.
|
|
23
|
+
hasWarned = true;
|
|
24
|
+
console.warn(
|
|
25
|
+
`Using "${deprecatedName}" environment variable. This is deprecated. Please use "${variableName}", instead.`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return process.env[deprecatedName];
|
|
29
|
+
} else {
|
|
30
|
+
return defaultValue;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { build } from "esbuild";
|
|
3
|
+
import type { CfScriptFormat } from "./api/worker";
|
|
4
|
+
import type { Entry } from "./entry";
|
|
5
|
+
import type { Metafile } from "esbuild";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A function to "guess" the type of worker.
|
|
9
|
+
* We do this by running a lightweight build of the actual script,
|
|
10
|
+
* and looking at the metafile generated by esbuild. If it has a default
|
|
11
|
+
* export (or really, any exports), that means it's a "modules" worker.
|
|
12
|
+
* Else, it's a "service-worker" worker. This seems hacky, but works remarkably
|
|
13
|
+
* well in practice.
|
|
14
|
+
*/
|
|
15
|
+
export default async function guessWorkerFormat(
|
|
16
|
+
entry: Entry,
|
|
17
|
+
hint: CfScriptFormat | undefined
|
|
18
|
+
): Promise<CfScriptFormat> {
|
|
19
|
+
const result = await build({
|
|
20
|
+
entryPoints: [entry.file],
|
|
21
|
+
absWorkingDir: entry.directory,
|
|
22
|
+
metafile: true,
|
|
23
|
+
bundle: false,
|
|
24
|
+
format: "esm",
|
|
25
|
+
write: false,
|
|
26
|
+
});
|
|
27
|
+
// result.metafile is defined because of the `metafile: true` option above.
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
29
|
+
const metafile = result.metafile!;
|
|
30
|
+
const entryPoints = Object.entries(metafile.outputs).filter(
|
|
31
|
+
([_path, output]) => output.entryPoint !== undefined
|
|
32
|
+
);
|
|
33
|
+
assert(
|
|
34
|
+
entryPoints.length > 0,
|
|
35
|
+
`Cannot find entry-point "${entry.file}" in generated bundle.` +
|
|
36
|
+
listEntryPoints(entryPoints)
|
|
37
|
+
);
|
|
38
|
+
assert(
|
|
39
|
+
entryPoints.length < 2,
|
|
40
|
+
"More than one entry-point found for generated bundle." +
|
|
41
|
+
listEntryPoints(entryPoints)
|
|
42
|
+
);
|
|
43
|
+
const guessedWorkerFormat =
|
|
44
|
+
entryPoints[0][1].exports.length > 0 ? "modules" : "service-worker";
|
|
45
|
+
|
|
46
|
+
if (hint) {
|
|
47
|
+
if (hint !== guessedWorkerFormat) {
|
|
48
|
+
if (hint === "service-worker") {
|
|
49
|
+
throw new Error(
|
|
50
|
+
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have ES module exports. Please pass `--format modules`, or simply remove the configuration."
|
|
51
|
+
);
|
|
52
|
+
} else {
|
|
53
|
+
throw new Error(
|
|
54
|
+
"You configured this worker to be 'modules', but the file you are trying to build doesn't export a handler. Please pass `--format service-worker`, or simply remove the configuration."
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return guessedWorkerFormat;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function listEntryPoints(
|
|
63
|
+
outputs: [string, ValueOf<Metafile["outputs"]>][]
|
|
64
|
+
): string {
|
|
65
|
+
return outputs.map(([_input, output]) => output.entryPoint).join("\n");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type ValueOf<T> = T[keyof T];
|