wrangler 0.0.0-fb43821 → 0.0.0-fb53fda
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 +1 -1
- package/bin/wrangler.js +12 -3
- package/miniflare-dist/index.mjs +79 -0
- package/miniflare-dist/index.mjs.map +7 -0
- package/package.json +46 -17
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +223 -0
- package/pages/functions/filepath-routing.ts +181 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +151 -0
- package/pages/functions/template-worker.ts +130 -0
- package/src/__tests__/configuration.test.ts +2790 -0
- package/src/__tests__/dev.test.tsx +532 -0
- package/src/__tests__/guess-worker-format.test.ts +68 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +48 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +70 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +33 -0
- package/src/__tests__/helpers/mock-stdin.ts +103 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +32 -0
- package/src/__tests__/helpers/run-wrangler.ts +16 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +127 -0
- package/src/__tests__/index.test.ts +770 -209
- package/src/__tests__/jest.setup.ts +69 -0
- package/src/__tests__/kv.test.ts +1380 -0
- package/src/__tests__/logout.test.ts +52 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/pages.test.ts +82 -0
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +3431 -0
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +425 -0
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +562 -0
- package/src/__tests__/whoami.test.tsx +100 -0
- package/src/bundle.ts +142 -0
- package/src/cfetch/index.ts +104 -0
- package/src/cfetch/internal.ts +139 -0
- package/src/cli.ts +16 -7
- package/src/config/README.md +107 -0
- package/src/config/config.ts +218 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +366 -0
- package/src/config/index.ts +62 -0
- package/src/config/validation-helpers.ts +554 -0
- package/src/config/validation.ts +1191 -0
- package/src/{api/preview.ts → create-worker-preview.ts} +63 -25
- package/src/create-worker-upload-form.ts +217 -0
- package/src/dev/dev.tsx +390 -0
- package/src/dev/local.tsx +232 -0
- package/src/dev/remote.tsx +203 -0
- package/src/dev/use-esbuild.ts +100 -0
- package/src/dialogs.tsx +4 -4
- package/src/entry.ts +237 -0
- package/src/environment-variables.ts +33 -0
- package/src/https-options.ts +122 -0
- package/src/index.tsx +1417 -915
- package/src/inspect.ts +617 -0
- package/src/intl-polyfill.d.ts +139 -0
- package/src/kv.ts +319 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +37 -0
- package/src/module-collection.ts +229 -35
- package/src/open-in-browser.ts +25 -0
- package/src/package-manager.ts +126 -0
- package/src/pages.tsx +470 -227
- package/src/parse.ts +183 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +440 -0
- package/src/publish.ts +292 -290
- package/src/r2.ts +50 -0
- package/src/reporting.ts +131 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +224 -59
- 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 +301 -203
- package/src/whoami.tsx +69 -0
- package/src/worker.ts +163 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +134241 -116339
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/mock-cfetch.js +0 -42
- package/src/api/form_data.ts +0 -141
- package/src/api/inspect.ts +0 -430
- package/src/api/worker.ts +0 -161
- package/src/cfetch.ts +0 -72
- package/src/config.ts +0 -122
- package/src/dev.tsx +0 -776
- package/src/kv.tsx +0 -211
- package/src/tail.tsx +0 -71
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { useState, useEffect, useRef } from "react";
|
|
5
|
+
import { createWorkerPreview } from "../create-worker-preview";
|
|
6
|
+
import useInspector from "../inspect";
|
|
7
|
+
import { usePreviewServer } from "../proxy";
|
|
8
|
+
import { syncAssets } from "../sites";
|
|
9
|
+
import type { CfPreviewToken } from "../create-worker-preview";
|
|
10
|
+
import type { AssetPaths } from "../sites";
|
|
11
|
+
import type { CfModule, CfWorkerInit, CfScriptFormat } from "../worker";
|
|
12
|
+
import type { EsbuildBundle } from "./use-esbuild";
|
|
13
|
+
|
|
14
|
+
export function Remote(props: {
|
|
15
|
+
name: string | undefined;
|
|
16
|
+
bundle: EsbuildBundle | undefined;
|
|
17
|
+
format: CfScriptFormat | undefined;
|
|
18
|
+
public: undefined | string;
|
|
19
|
+
assetPaths: undefined | AssetPaths;
|
|
20
|
+
port: number;
|
|
21
|
+
ip: string;
|
|
22
|
+
localProtocol: "https" | "http";
|
|
23
|
+
inspectorPort: number;
|
|
24
|
+
accountId: undefined | string;
|
|
25
|
+
apiToken: undefined | string;
|
|
26
|
+
bindings: CfWorkerInit["bindings"];
|
|
27
|
+
compatibilityDate: string | undefined;
|
|
28
|
+
compatibilityFlags: undefined | string[];
|
|
29
|
+
usageModel: undefined | "bundled" | "unbound";
|
|
30
|
+
env: string | undefined;
|
|
31
|
+
legacyEnv: boolean | undefined;
|
|
32
|
+
zone: { id: string; host: string } | undefined;
|
|
33
|
+
}) {
|
|
34
|
+
assert(props.accountId, "accountId is required");
|
|
35
|
+
assert(props.apiToken, "apiToken is required");
|
|
36
|
+
const previewToken = useWorker({
|
|
37
|
+
name: props.name,
|
|
38
|
+
bundle: props.bundle,
|
|
39
|
+
format: props.format,
|
|
40
|
+
modules: props.bundle ? props.bundle.modules : [],
|
|
41
|
+
accountId: props.accountId,
|
|
42
|
+
apiToken: props.apiToken,
|
|
43
|
+
bindings: props.bindings,
|
|
44
|
+
assetPaths: props.assetPaths,
|
|
45
|
+
port: props.port,
|
|
46
|
+
compatibilityDate: props.compatibilityDate,
|
|
47
|
+
compatibilityFlags: props.compatibilityFlags,
|
|
48
|
+
usageModel: props.usageModel,
|
|
49
|
+
env: props.env,
|
|
50
|
+
legacyEnv: props.legacyEnv,
|
|
51
|
+
zone: props.zone,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
usePreviewServer({
|
|
55
|
+
previewToken,
|
|
56
|
+
publicRoot: props.public,
|
|
57
|
+
localProtocol: props.localProtocol,
|
|
58
|
+
localPort: props.port,
|
|
59
|
+
ip: props.ip,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
useInspector({
|
|
63
|
+
inspectorUrl: previewToken ? previewToken.inspectorUrl.href : undefined,
|
|
64
|
+
port: props.inspectorPort,
|
|
65
|
+
logToTerminal: true,
|
|
66
|
+
});
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function useWorker(props: {
|
|
71
|
+
name: string | undefined;
|
|
72
|
+
bundle: EsbuildBundle | undefined;
|
|
73
|
+
format: CfScriptFormat | undefined;
|
|
74
|
+
modules: CfModule[];
|
|
75
|
+
accountId: string;
|
|
76
|
+
apiToken: string;
|
|
77
|
+
bindings: CfWorkerInit["bindings"];
|
|
78
|
+
assetPaths: undefined | AssetPaths;
|
|
79
|
+
port: number;
|
|
80
|
+
compatibilityDate: string | undefined;
|
|
81
|
+
compatibilityFlags: string[] | undefined;
|
|
82
|
+
usageModel: undefined | "bundled" | "unbound";
|
|
83
|
+
env: string | undefined;
|
|
84
|
+
legacyEnv: boolean | undefined;
|
|
85
|
+
zone: { id: string; host: string } | undefined;
|
|
86
|
+
}): CfPreviewToken | undefined {
|
|
87
|
+
const {
|
|
88
|
+
name,
|
|
89
|
+
bundle,
|
|
90
|
+
format,
|
|
91
|
+
modules,
|
|
92
|
+
accountId,
|
|
93
|
+
apiToken,
|
|
94
|
+
bindings,
|
|
95
|
+
assetPaths,
|
|
96
|
+
compatibilityDate,
|
|
97
|
+
compatibilityFlags,
|
|
98
|
+
usageModel,
|
|
99
|
+
port,
|
|
100
|
+
} = props;
|
|
101
|
+
const [token, setToken] = useState<CfPreviewToken | undefined>();
|
|
102
|
+
|
|
103
|
+
// This is the most reliable way to detect whether
|
|
104
|
+
// something's "happened" in our system; We make a ref and
|
|
105
|
+
// mark it once we log our initial message. Refs are vars!
|
|
106
|
+
const startedRef = useRef(false);
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
async function start() {
|
|
110
|
+
setToken(undefined); // reset token in case we're re-running
|
|
111
|
+
|
|
112
|
+
if (!bundle || !format) return;
|
|
113
|
+
|
|
114
|
+
if (!startedRef.current) {
|
|
115
|
+
startedRef.current = true;
|
|
116
|
+
} else {
|
|
117
|
+
console.log("⎔ Detected changes, restarted server.");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const assets = await syncAssets(
|
|
121
|
+
accountId,
|
|
122
|
+
// When we're using the newer service environments, we wouldn't
|
|
123
|
+
// have added the env name on to the script name. However, we must
|
|
124
|
+
// include it in the kv namespace name regardless (since there's no
|
|
125
|
+
// concept of service environments for kv namespaces yet).
|
|
126
|
+
name + (!props.legacyEnv && props.env ? `-${props.env}` : ""),
|
|
127
|
+
assetPaths,
|
|
128
|
+
true
|
|
129
|
+
); // TODO: cancellable?
|
|
130
|
+
|
|
131
|
+
const content = await readFile(bundle.path, "utf-8");
|
|
132
|
+
|
|
133
|
+
const init: CfWorkerInit = {
|
|
134
|
+
name,
|
|
135
|
+
main: {
|
|
136
|
+
name: path.basename(bundle.path),
|
|
137
|
+
type: format === "modules" ? "esm" : "commonjs",
|
|
138
|
+
content,
|
|
139
|
+
},
|
|
140
|
+
modules: modules.concat(
|
|
141
|
+
assets.manifest
|
|
142
|
+
? {
|
|
143
|
+
name: "__STATIC_CONTENT_MANIFEST",
|
|
144
|
+
content: JSON.stringify(assets.manifest),
|
|
145
|
+
type: "text",
|
|
146
|
+
}
|
|
147
|
+
: []
|
|
148
|
+
),
|
|
149
|
+
bindings: {
|
|
150
|
+
...bindings,
|
|
151
|
+
kv_namespaces: (bindings.kv_namespaces || []).concat(
|
|
152
|
+
assets.namespace
|
|
153
|
+
? { binding: "__STATIC_CONTENT", id: assets.namespace }
|
|
154
|
+
: []
|
|
155
|
+
),
|
|
156
|
+
text_blobs: {
|
|
157
|
+
...bindings.text_blobs,
|
|
158
|
+
...(assets.manifest &&
|
|
159
|
+
format === "service-worker" && {
|
|
160
|
+
__STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
|
|
161
|
+
}),
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
migrations: undefined, // no migrations in dev
|
|
165
|
+
compatibility_date: compatibilityDate,
|
|
166
|
+
compatibility_flags: compatibilityFlags,
|
|
167
|
+
usage_model: usageModel,
|
|
168
|
+
};
|
|
169
|
+
setToken(
|
|
170
|
+
await createWorkerPreview(
|
|
171
|
+
init,
|
|
172
|
+
{
|
|
173
|
+
accountId,
|
|
174
|
+
apiToken,
|
|
175
|
+
},
|
|
176
|
+
{ env: props.env, legacyEnv: props.legacyEnv, zone: props.zone }
|
|
177
|
+
)
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
start().catch((err) => {
|
|
181
|
+
// we want to log the error, but not end the process
|
|
182
|
+
// since it could recover after the developer fixes whatever's wrong
|
|
183
|
+
console.error("remote worker:", err);
|
|
184
|
+
});
|
|
185
|
+
}, [
|
|
186
|
+
name,
|
|
187
|
+
bundle,
|
|
188
|
+
format,
|
|
189
|
+
accountId,
|
|
190
|
+
apiToken,
|
|
191
|
+
port,
|
|
192
|
+
assetPaths,
|
|
193
|
+
compatibilityDate,
|
|
194
|
+
compatibilityFlags,
|
|
195
|
+
usageModel,
|
|
196
|
+
bindings,
|
|
197
|
+
modules,
|
|
198
|
+
props.env,
|
|
199
|
+
props.legacyEnv,
|
|
200
|
+
props.zone,
|
|
201
|
+
]);
|
|
202
|
+
return token;
|
|
203
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { useState, useEffect } from "react";
|
|
3
|
+
import { bundleWorker } from "../bundle";
|
|
4
|
+
import type { Config } from "../config";
|
|
5
|
+
import type { Entry } from "../entry";
|
|
6
|
+
import type { CfModule } from "../worker";
|
|
7
|
+
import type { WatchMode } from "esbuild";
|
|
8
|
+
|
|
9
|
+
export type EsbuildBundle = {
|
|
10
|
+
id: number;
|
|
11
|
+
path: string;
|
|
12
|
+
entry: Entry;
|
|
13
|
+
type: "esm" | "commonjs";
|
|
14
|
+
modules: CfModule[];
|
|
15
|
+
serveAssetsFromWorker: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function useEsbuild({
|
|
19
|
+
entry,
|
|
20
|
+
destination,
|
|
21
|
+
staticRoot,
|
|
22
|
+
jsxFactory,
|
|
23
|
+
jsxFragment,
|
|
24
|
+
rules,
|
|
25
|
+
serveAssetsFromWorker,
|
|
26
|
+
}: {
|
|
27
|
+
entry: Entry;
|
|
28
|
+
destination: string | undefined;
|
|
29
|
+
staticRoot: undefined | string;
|
|
30
|
+
jsxFactory: string | undefined;
|
|
31
|
+
jsxFragment: string | undefined;
|
|
32
|
+
rules: Config["rules"];
|
|
33
|
+
serveAssetsFromWorker: boolean;
|
|
34
|
+
}): EsbuildBundle | undefined {
|
|
35
|
+
const [bundle, setBundle] = useState<EsbuildBundle>();
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
let stopWatching: (() => void) | undefined = undefined;
|
|
38
|
+
|
|
39
|
+
const watchMode: WatchMode = {
|
|
40
|
+
async onRebuild(error) {
|
|
41
|
+
if (error) console.error("watch build failed:", error);
|
|
42
|
+
else {
|
|
43
|
+
// nothing really changes here, so let's increment the id
|
|
44
|
+
// to change the return object's identity
|
|
45
|
+
setBundle((previousBundle) => {
|
|
46
|
+
assert(
|
|
47
|
+
previousBundle,
|
|
48
|
+
"Rebuild triggered with no previous build available"
|
|
49
|
+
);
|
|
50
|
+
return { ...previousBundle, id: previousBundle.id + 1 };
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
async function build() {
|
|
57
|
+
if (!destination) return;
|
|
58
|
+
|
|
59
|
+
const { resolvedEntryPointPath, bundleType, modules, stop } =
|
|
60
|
+
await bundleWorker(entry, destination, {
|
|
61
|
+
// In dev, we serve assets from the local proxy before we send the request to the worker.
|
|
62
|
+
serveAssetsFromWorker: false,
|
|
63
|
+
jsxFactory,
|
|
64
|
+
jsxFragment,
|
|
65
|
+
rules,
|
|
66
|
+
watch: watchMode,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Capture the `stop()` method to use as the `useEffect()` destructor.
|
|
70
|
+
stopWatching = stop;
|
|
71
|
+
|
|
72
|
+
setBundle({
|
|
73
|
+
id: 0,
|
|
74
|
+
entry,
|
|
75
|
+
path: resolvedEntryPointPath,
|
|
76
|
+
type: bundleType,
|
|
77
|
+
modules,
|
|
78
|
+
serveAssetsFromWorker,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
build().catch(() => {
|
|
83
|
+
// esbuild already logs errors to stderr and we don't want to end the process
|
|
84
|
+
// on build errors anyway so this is a no-op error handler
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return () => {
|
|
88
|
+
stopWatching?.();
|
|
89
|
+
};
|
|
90
|
+
}, [
|
|
91
|
+
entry,
|
|
92
|
+
destination,
|
|
93
|
+
staticRoot,
|
|
94
|
+
jsxFactory,
|
|
95
|
+
jsxFragment,
|
|
96
|
+
serveAssetsFromWorker,
|
|
97
|
+
rules,
|
|
98
|
+
]);
|
|
99
|
+
return bundle;
|
|
100
|
+
}
|
package/src/dialogs.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { useState } from "react";
|
|
3
1
|
import { Box, Text, useInput, render } from "ink";
|
|
4
2
|
import TextInput from "ink-text-input";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { useState } from "react";
|
|
5
5
|
type ConfirmProps = {
|
|
6
6
|
text: string;
|
|
7
7
|
onConfirm: (answer: boolean) => void;
|
|
@@ -67,9 +67,9 @@ export async function prompt(text: string, type: "text" | "password" = "text") {
|
|
|
67
67
|
<Prompt
|
|
68
68
|
text={text}
|
|
69
69
|
type={type}
|
|
70
|
-
onSubmit={(
|
|
70
|
+
onSubmit={(inputText) => {
|
|
71
71
|
unmount();
|
|
72
|
-
resolve(
|
|
72
|
+
resolve(inputText);
|
|
73
73
|
}}
|
|
74
74
|
/>
|
|
75
75
|
);
|
package/src/entry.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import * as esbuild from "esbuild";
|
|
5
|
+
import { execaCommand } from "execa";
|
|
6
|
+
import type { Config } from "./config";
|
|
7
|
+
import type { CfScriptFormat } from "./worker";
|
|
8
|
+
import type { Metafile } from "esbuild";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An entry point for the Worker.
|
|
12
|
+
*
|
|
13
|
+
* It consists not just of a `file`, but also of a `directory` that is used to resolve relative paths.
|
|
14
|
+
*/
|
|
15
|
+
export type Entry = { file: string; directory: string; format: CfScriptFormat };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Compute the entry-point for the Worker.
|
|
19
|
+
*/
|
|
20
|
+
export async function getEntry(
|
|
21
|
+
args: { script?: string; format?: CfScriptFormat | undefined },
|
|
22
|
+
config: Config,
|
|
23
|
+
command: "dev" | "publish"
|
|
24
|
+
): Promise<Entry> {
|
|
25
|
+
let file: string;
|
|
26
|
+
let directory = process.cwd();
|
|
27
|
+
if (args.script) {
|
|
28
|
+
// If the script name comes from the command line it is relative to the current working directory.
|
|
29
|
+
file = path.resolve(args.script);
|
|
30
|
+
} else if (config.main === undefined) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`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.`
|
|
33
|
+
);
|
|
34
|
+
} else {
|
|
35
|
+
directory = path.resolve(path.dirname(config.configPath ?? "."));
|
|
36
|
+
file = path.resolve(directory, config.main);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
await runCustomBuild(file, config.build);
|
|
40
|
+
|
|
41
|
+
if (fileExists(file) === false) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Could not resolve "${path.relative(process.cwd(), file)}".`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const format = await guessWorkerFormat(
|
|
47
|
+
file,
|
|
48
|
+
directory,
|
|
49
|
+
args.format ?? config.build?.upload?.format
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const { localBindings, remoteBindings } =
|
|
53
|
+
partitionDurableObjectBindings(config);
|
|
54
|
+
|
|
55
|
+
if (command === "dev" && remoteBindings.length > 0) {
|
|
56
|
+
console.warn(
|
|
57
|
+
"WARNING: You have Durable Object bindings, which are not defined locally in the worker being developed.\n" +
|
|
58
|
+
"Be aware that changes to the data stored in these Durable Objects will be permanent and affect the live instances.\n" +
|
|
59
|
+
"Remote Durable Objects that are affected:\n" +
|
|
60
|
+
remoteBindings.map((b) => `- ${JSON.stringify(b)}`).join("\n")
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (format === "service-worker" && localBindings.length > 0) {
|
|
65
|
+
const errorMessage =
|
|
66
|
+
"You seem to be trying to use Durable Objects in a Worker written with Service Worker syntax.";
|
|
67
|
+
const addScriptName =
|
|
68
|
+
"You can use Durable Objects defined in other Workers by specifying a `script_name` in your wrangler.toml, where `script_name` is the name of the Worker that implements that Durable Object. For example:";
|
|
69
|
+
const addScriptNameExamples = generateAddScriptNameExamples(localBindings);
|
|
70
|
+
const migrateText =
|
|
71
|
+
"Alternatively, migrate your worker to ES Module syntax to implement a Durable Object in this Worker:";
|
|
72
|
+
const migrateUrl =
|
|
73
|
+
"https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/";
|
|
74
|
+
throw new Error(
|
|
75
|
+
`${errorMessage}\n${addScriptName}\n${addScriptNameExamples}\n${migrateText}\n${migrateUrl}`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { file, directory, format };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function runCustomBuild(
|
|
83
|
+
expectedEntry: string,
|
|
84
|
+
build: Config["build"]
|
|
85
|
+
) {
|
|
86
|
+
if (build?.command) {
|
|
87
|
+
// TODO: add a deprecation message here?
|
|
88
|
+
console.log("running:", build.command);
|
|
89
|
+
await execaCommand(build.command, {
|
|
90
|
+
shell: true,
|
|
91
|
+
stdout: "inherit",
|
|
92
|
+
stderr: "inherit",
|
|
93
|
+
timeout: 1000 * 30,
|
|
94
|
+
...(build.cwd && { cwd: build.cwd }),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (fileExists(expectedEntry) === false) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Could not resolve "${path.relative(
|
|
100
|
+
process.cwd(),
|
|
101
|
+
expectedEntry
|
|
102
|
+
)}" after running custom build: ${build.command}`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A function to "guess" the type of worker.
|
|
110
|
+
* We do this by running a lightweight build of the actual script,
|
|
111
|
+
* and looking at the metafile generated by esbuild. If it has a default
|
|
112
|
+
* export (or really, any exports), that means it's a "modules" worker.
|
|
113
|
+
* Else, it's a "service-worker" worker. This seems hacky, but works remarkably
|
|
114
|
+
* well in practice.
|
|
115
|
+
*/
|
|
116
|
+
export default async function guessWorkerFormat(
|
|
117
|
+
entryFile: string,
|
|
118
|
+
entryWorkingDirectory: string,
|
|
119
|
+
hint: CfScriptFormat | undefined
|
|
120
|
+
): Promise<CfScriptFormat> {
|
|
121
|
+
const result = await esbuild.build({
|
|
122
|
+
entryPoints: [entryFile],
|
|
123
|
+
absWorkingDir: entryWorkingDirectory,
|
|
124
|
+
metafile: true,
|
|
125
|
+
bundle: false,
|
|
126
|
+
format: "esm",
|
|
127
|
+
write: false,
|
|
128
|
+
loader: {
|
|
129
|
+
".js": "jsx",
|
|
130
|
+
".mjs": "jsx",
|
|
131
|
+
".cjs": "jsx",
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
// result.metafile is defined because of the `metafile: true` option above.
|
|
135
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
136
|
+
const metafile = result.metafile!;
|
|
137
|
+
const entryPoints = Object.entries(metafile.outputs).filter(
|
|
138
|
+
([_path, output]) => output.entryPoint !== undefined
|
|
139
|
+
);
|
|
140
|
+
assert(
|
|
141
|
+
entryPoints.length > 0,
|
|
142
|
+
`Cannot find entry-point "${entryFile}" in generated bundle.` +
|
|
143
|
+
listEntryPoints(entryPoints)
|
|
144
|
+
);
|
|
145
|
+
assert(
|
|
146
|
+
entryPoints.length < 2,
|
|
147
|
+
"More than one entry-point found for generated bundle." +
|
|
148
|
+
listEntryPoints(entryPoints)
|
|
149
|
+
);
|
|
150
|
+
const guessedWorkerFormat =
|
|
151
|
+
entryPoints[0][1].exports.length > 0 ? "modules" : "service-worker";
|
|
152
|
+
|
|
153
|
+
if (hint) {
|
|
154
|
+
if (hint !== guessedWorkerFormat) {
|
|
155
|
+
if (hint === "service-worker") {
|
|
156
|
+
throw new Error(
|
|
157
|
+
"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."
|
|
158
|
+
);
|
|
159
|
+
} else {
|
|
160
|
+
throw new Error(
|
|
161
|
+
"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."
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return guessedWorkerFormat;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function listEntryPoints(
|
|
170
|
+
outputs: [string, ValueOf<Metafile["outputs"]>][]
|
|
171
|
+
): string {
|
|
172
|
+
return outputs.map(([_input, output]) => output.entryPoint).join("\n");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
type ValueOf<T> = T[keyof T];
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Returns true if the given `filePath` exists as-is,
|
|
179
|
+
* or if some version of it (by appending a common extension) exists.
|
|
180
|
+
*/
|
|
181
|
+
export function fileExists(filePath: string): boolean {
|
|
182
|
+
if (path.extname(filePath) !== "") {
|
|
183
|
+
return existsSync(filePath);
|
|
184
|
+
}
|
|
185
|
+
const base = path.join(path.dirname(filePath), path.basename(filePath));
|
|
186
|
+
for (const ext of [".ts", ".tsx", ".js", ".jsx"]) {
|
|
187
|
+
if (existsSync(base + ext)) {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
type DurableObjectBindings = Config["durable_objects"]["bindings"];
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Groups the durable object bindings into two lists:
|
|
198
|
+
* those that are defined locally and those that refer to a durable object defined in another script.
|
|
199
|
+
*/
|
|
200
|
+
function partitionDurableObjectBindings(config: Config): {
|
|
201
|
+
localBindings: DurableObjectBindings;
|
|
202
|
+
remoteBindings: DurableObjectBindings;
|
|
203
|
+
} {
|
|
204
|
+
const localBindings: DurableObjectBindings = [];
|
|
205
|
+
const remoteBindings: DurableObjectBindings = [];
|
|
206
|
+
for (const binding of config.durable_objects.bindings) {
|
|
207
|
+
if (binding.script_name === undefined) {
|
|
208
|
+
localBindings.push(binding);
|
|
209
|
+
} else {
|
|
210
|
+
remoteBindings.push(binding);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return { localBindings, remoteBindings };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Generates some help text based on the Durable Object bindings in a given
|
|
218
|
+
* config indicating how the user can add a `script_name` field to bind an
|
|
219
|
+
* externally defined Durable Object.
|
|
220
|
+
*/
|
|
221
|
+
function generateAddScriptNameExamples(
|
|
222
|
+
localBindings: DurableObjectBindings
|
|
223
|
+
): string {
|
|
224
|
+
function exampleScriptName(binding_name: string): string {
|
|
225
|
+
return `${binding_name.toLowerCase().replaceAll("_", "-")}-worker`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return localBindings
|
|
229
|
+
.map(({ name, class_name }) => {
|
|
230
|
+
const script_name = exampleScriptName(name);
|
|
231
|
+
const currentBinding = `{ name = ${name}, class_name = ${class_name} }`;
|
|
232
|
+
const fixedBinding = `{ name = ${name}, class_name = ${class_name}, script_name = ${script_name} }`;
|
|
233
|
+
|
|
234
|
+
return `${currentBinding} ==> ${fixedBinding}`;
|
|
235
|
+
})
|
|
236
|
+
.join("\n");
|
|
237
|
+
}
|
|
@@ -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
|
+
}
|