wrangler 0.0.0-e043479 → 0.0.0-e09abbc
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/Cloudflare_CA.pem +18 -0
- package/README.md +33 -14
- package/bin/wrangler.js +160 -28
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +5354 -0
- package/package.json +166 -136
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +42 -0
- package/src/__tests__/configuration.test.ts +3942 -2178
- package/src/__tests__/dev.test.tsx +1191 -235
- package/src/__tests__/guess-worker-format.test.ts +76 -57
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/mock-account-id.ts +26 -22
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-cfetch.ts +96 -92
- package/src/__tests__/helpers/mock-console.ts +59 -32
- package/src/__tests__/helpers/mock-dialogs.ts +121 -33
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +74 -0
- package/src/__tests__/helpers/mock-kv.ts +28 -35
- package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
- 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 +29 -20
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +163 -0
- package/src/__tests__/index.test.ts +246 -850
- package/src/__tests__/init.test.ts +1992 -0
- package/src/__tests__/jest.setup.ts +85 -34
- package/src/__tests__/kv.test.ts +1402 -1139
- package/src/__tests__/logger.test.ts +141 -0
- package/src/__tests__/metrics.test.ts +445 -0
- package/src/__tests__/package-manager.test.ts +312 -165
- package/src/__tests__/pages.test.ts +1261 -0
- package/src/__tests__/parse.test.ts +253 -245
- package/src/__tests__/publish.test.ts +6611 -2079
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +184 -149
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +474 -407
- package/src/__tests__/tail.test.ts +565 -431
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/user.test.ts +143 -0
- package/src/__tests__/whoami.test.tsx +219 -75
- package/src/__tests__/worker-namespace.test.ts +327 -0
- package/src/abort.d.ts +3 -0
- package/src/api/dev.ts +86 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +196 -93
- package/src/cfetch/index.ts +83 -66
- package/src/cfetch/internal.ts +100 -84
- package/src/cli.ts +22 -14
- package/src/config/README.md +7 -7
- package/src/config/config.ts +223 -242
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +404 -256
- package/src/config/index.ts +218 -32
- package/src/config/validation-helpers.ts +436 -222
- package/src/config/validation.ts +1642 -907
- package/src/config-cache.ts +84 -0
- package/src/create-worker-preview.ts +255 -0
- package/src/create-worker-upload-form.ts +263 -0
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +452 -0
- package/src/dev/local.tsx +373 -0
- package/src/dev/remote.tsx +386 -0
- package/src/dev/use-esbuild.ts +150 -0
- package/src/dev.tsx +576 -943
- package/src/dialogs.tsx +115 -57
- package/src/durable.ts +102 -0
- package/src/entry.ts +308 -37
- package/src/environment-variables.ts +25 -23
- package/src/errors.ts +11 -0
- package/src/faye-websocket.d.ts +6 -0
- package/src/generate.ts +33 -0
- package/src/git-client.ts +42 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +127 -0
- package/src/index.tsx +1910 -2241
- package/src/init.ts +549 -0
- package/src/inspect.ts +603 -550
- package/src/intl-polyfill.d.ts +139 -0
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +430 -0
- package/src/logger.ts +73 -0
- package/src/metrics/index.ts +4 -0
- package/src/metrics/is-ci.ts +14 -0
- package/src/metrics/metrics-config.ts +239 -0
- package/src/metrics/metrics-dispatcher.ts +95 -0
- package/src/metrics/send-event.ts +92 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/assets.ts +543 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +85 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +234 -146
- package/src/open-in-browser.ts +10 -6
- package/src/package-manager.ts +183 -90
- package/src/pages/build.tsx +204 -0
- package/src/pages/constants.ts +7 -0
- package/src/pages/deployments.tsx +106 -0
- package/src/pages/dev.tsx +401 -0
- package/src/pages/functions/buildPlugin.ts +105 -0
- package/src/pages/functions/buildWorker.ts +151 -0
- package/src/pages/functions/filepath-routing.test.ts +234 -0
- 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 +163 -0
- package/src/pages/publish.tsx +338 -0
- package/src/pages/types.ts +40 -0
- package/src/pages/upload.tsx +396 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +224 -121
- package/src/paths.ts +6 -6
- package/src/preview.ts +31 -0
- package/src/proxy.ts +413 -313
- package/src/publish.ts +773 -343
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +623 -0
- package/src/r2.ts +19 -19
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +285 -151
- package/src/tail/filters.ts +108 -108
- package/src/tail/index.ts +217 -217
- package/src/tail/printing.ts +51 -46
- package/src/update-check.ts +19 -0
- package/src/user/choose-account.tsx +69 -0
- package/src/user/env-vars.ts +46 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +3 -0
- package/src/user/user.tsx +1171 -0
- package/src/whoami.tsx +63 -41
- package/src/worker-namespace.ts +206 -0
- package/src/worker.ts +191 -0
- package/src/zones.ts +76 -0
- package/templates/checked-fetch.js +17 -0
- package/templates/gitignore +171 -0
- package/templates/new-worker-scheduled.js +17 -0
- package/templates/new-worker-scheduled.ts +32 -0
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +18 -3
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +155 -0
- package/templates/pages-template-worker.ts +161 -0
- package/templates/static-asset-facade.js +38 -36
- package/templates/tsconfig.json +95 -95
- package/wrangler-dist/cli.d.ts +327 -0
- package/wrangler-dist/cli.js +94947 -111199
- package/pages/functions/buildWorker.ts +0 -62
- package/pages/functions/filepath-routing.test.ts +0 -123
- package/pages/functions/filepath-routing.ts +0 -178
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -151
- package/pages/functions/template-worker.ts +0 -130
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/__tests__/helpers/mock-user.ts +0 -27
- package/src/__tests__/logout.test.ts +0 -50
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/api/form_data.ts +0 -217
- package/src/api/preview.ts +0 -147
- package/src/api/worker.ts +0 -186
- package/src/guess-worker-format.ts +0 -68
- package/src/kv.tsx +0 -339
- package/src/pages.tsx +0 -1045
- package/src/reporting.ts +0 -115
- package/src/user.tsx +0 -1063
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
- package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
- package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
- package/vendor/wrangler-mime/CHANGELOG.md +0 -289
- package/vendor/wrangler-mime/LICENSE +0 -21
- package/vendor/wrangler-mime/Mime.js +0 -97
- package/vendor/wrangler-mime/README.md +0 -187
- package/vendor/wrangler-mime/cli.js +0 -46
- package/vendor/wrangler-mime/index.js +0 -4
- package/vendor/wrangler-mime/lite.js +0 -4
- package/vendor/wrangler-mime/package.json +0 -52
- package/vendor/wrangler-mime/types/other.js +0 -1
- package/vendor/wrangler-mime/types/standard.js +0 -1
- package/wrangler-dist/cli.js.map +0 -7
package/src/bundle.ts
CHANGED
|
@@ -1,95 +1,192 @@
|
|
|
1
1
|
import assert from "node:assert";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
|
+
import { builtinModules } from "node:module";
|
|
3
4
|
import * as path from "node:path";
|
|
5
|
+
import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
|
|
6
|
+
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
|
|
4
7
|
import * as esbuild from "esbuild";
|
|
5
|
-
import
|
|
6
|
-
import type { CfModule, CfScriptFormat } from "./api/worker";
|
|
8
|
+
import createModuleCollector from "./module-collection";
|
|
7
9
|
import type { Config } from "./config";
|
|
8
10
|
import type { Entry } from "./entry";
|
|
11
|
+
import type { CfModule } from "./worker";
|
|
9
12
|
|
|
10
13
|
type BundleResult = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
modules: CfModule[];
|
|
15
|
+
resolvedEntryPointPath: string;
|
|
16
|
+
bundleType: "esm" | "commonjs";
|
|
17
|
+
stop: (() => void) | undefined;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Searches for any uses of node's builtin modules, and throws an error if it
|
|
22
|
+
* finds anything. This plugin is only used when nodeCompat is not enabled.
|
|
23
|
+
* Supports both regular node builtins, and the new "node:<MODULE>" format.
|
|
24
|
+
*/
|
|
25
|
+
const checkForNodeBuiltinsPlugin = {
|
|
26
|
+
name: "checkForNodeBuiltins",
|
|
27
|
+
setup(build: esbuild.PluginBuild) {
|
|
28
|
+
build.onResolve(
|
|
29
|
+
{
|
|
30
|
+
filter: new RegExp(
|
|
31
|
+
"^(" +
|
|
32
|
+
builtinModules.join("|") +
|
|
33
|
+
"|" +
|
|
34
|
+
builtinModules.map((module) => "node:" + module).join("|") +
|
|
35
|
+
")$"
|
|
36
|
+
),
|
|
37
|
+
},
|
|
38
|
+
() => {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`Detected a Node builtin module import while Node compatibility is disabled.\nAdd node_compat = true to your wrangler.toml file to enable Node compatibility.`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
},
|
|
15
45
|
};
|
|
16
46
|
|
|
17
47
|
/**
|
|
18
48
|
* Generate a bundle for the worker identified by the arguments passed in.
|
|
19
49
|
*/
|
|
20
50
|
export async function bundleWorker(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
51
|
+
entry: Entry,
|
|
52
|
+
destination: string,
|
|
53
|
+
options: {
|
|
54
|
+
serveAssetsFromWorker: boolean;
|
|
55
|
+
jsxFactory: string | undefined;
|
|
56
|
+
jsxFragment: string | undefined;
|
|
57
|
+
rules: Config["rules"];
|
|
58
|
+
watch?: esbuild.WatchMode;
|
|
59
|
+
tsconfig: string | undefined;
|
|
60
|
+
minify: boolean | undefined;
|
|
61
|
+
nodeCompat: boolean | undefined;
|
|
62
|
+
define: Config["define"];
|
|
63
|
+
checkFetch: boolean;
|
|
64
|
+
}
|
|
31
65
|
): Promise<BundleResult> {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
66
|
+
const {
|
|
67
|
+
serveAssetsFromWorker,
|
|
68
|
+
jsxFactory,
|
|
69
|
+
jsxFragment,
|
|
70
|
+
rules,
|
|
71
|
+
watch,
|
|
72
|
+
tsconfig,
|
|
73
|
+
minify,
|
|
74
|
+
nodeCompat,
|
|
75
|
+
checkFetch,
|
|
76
|
+
} = options;
|
|
77
|
+
const entryDirectory = path.dirname(entry.file);
|
|
78
|
+
const moduleCollector = createModuleCollector({
|
|
79
|
+
wrangler1xlegacyModuleReferences: {
|
|
80
|
+
rootDirectory: entryDirectory,
|
|
81
|
+
fileNames: new Set(
|
|
82
|
+
fs
|
|
83
|
+
.readdirSync(entryDirectory, { withFileTypes: true })
|
|
84
|
+
.filter(
|
|
85
|
+
(dirEntry) =>
|
|
86
|
+
dirEntry.isFile() && dirEntry.name !== path.basename(entry.file)
|
|
87
|
+
)
|
|
88
|
+
.map((dirEnt) => dirEnt.name)
|
|
89
|
+
),
|
|
90
|
+
},
|
|
91
|
+
format: entry.format,
|
|
92
|
+
rules,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// In dev, we want to patch `fetch()` with a special version that looks
|
|
96
|
+
// for bad usages and can warn the user about them; so we inject
|
|
97
|
+
// `checked-fetch.js` to do so. However, with yarn 3 style pnp,
|
|
98
|
+
// we need to extract that file to an accessible place before injecting
|
|
99
|
+
// it in, hence this code here.
|
|
100
|
+
const checkedFetchFileToInject = path.join(
|
|
101
|
+
destination,
|
|
102
|
+
"--temp-wrangler-files--",
|
|
103
|
+
"checked-fetch.js"
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
if (checkFetch) {
|
|
107
|
+
fs.mkdirSync(path.join(destination, "--temp-wrangler-files--"), {
|
|
108
|
+
recursive: true,
|
|
109
|
+
});
|
|
110
|
+
fs.writeFileSync(
|
|
111
|
+
checkedFetchFileToInject,
|
|
112
|
+
fs.readFileSync(path.resolve(__dirname, "../templates/checked-fetch.js"))
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
// TODO: we need to have similar logic like above for other files
|
|
116
|
+
// like the static asset facade, and other middleware that we
|
|
117
|
+
// plan on injecting/referencing.
|
|
118
|
+
|
|
119
|
+
const result = await esbuild.build({
|
|
120
|
+
...getEntryPoint(entry.file, serveAssetsFromWorker),
|
|
121
|
+
bundle: true,
|
|
122
|
+
absWorkingDir: entry.directory,
|
|
123
|
+
outdir: destination,
|
|
124
|
+
inject: checkFetch ? [checkedFetchFileToInject] : [],
|
|
125
|
+
external: ["__STATIC_CONTENT_MANIFEST"],
|
|
126
|
+
format: entry.format === "modules" ? "esm" : "iife",
|
|
127
|
+
target: "es2020",
|
|
128
|
+
sourcemap: true,
|
|
129
|
+
minify,
|
|
130
|
+
metafile: true,
|
|
131
|
+
conditions: ["worker", "browser"],
|
|
132
|
+
...(process.env.NODE_ENV && {
|
|
133
|
+
define: {
|
|
134
|
+
// use process.env["NODE_ENV" + ""] so that esbuild doesn't replace it
|
|
135
|
+
// when we do a build of wrangler. (re: https://github.com/cloudflare/wrangler2/issues/1477)
|
|
136
|
+
"process.env.NODE_ENV": `"${process.env["NODE_ENV" + ""]}"`,
|
|
137
|
+
...(nodeCompat ? { global: "globalThis" } : {}),
|
|
138
|
+
...(checkFetch ? { fetch: "checkedFetch" } : {}),
|
|
139
|
+
...options.define,
|
|
140
|
+
},
|
|
141
|
+
}),
|
|
142
|
+
loader: {
|
|
143
|
+
".js": "jsx",
|
|
144
|
+
".mjs": "jsx",
|
|
145
|
+
".cjs": "jsx",
|
|
146
|
+
},
|
|
147
|
+
plugins: [
|
|
148
|
+
moduleCollector.plugin,
|
|
149
|
+
...(nodeCompat
|
|
150
|
+
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
|
|
151
|
+
: // we use checkForNodeBuiltinsPlugin to throw a nicer error
|
|
152
|
+
// if we find node builtins when nodeCompat isn't turned on
|
|
153
|
+
[checkForNodeBuiltinsPlugin]),
|
|
154
|
+
],
|
|
155
|
+
...(jsxFactory && { jsxFactory }),
|
|
156
|
+
...(jsxFragment && { jsxFragment }),
|
|
157
|
+
...(tsconfig && { tsconfig }),
|
|
158
|
+
watch,
|
|
159
|
+
});
|
|
61
160
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
161
|
+
const entryPointOutputs = Object.entries(result.metafile.outputs).filter(
|
|
162
|
+
([_path, output]) => output.entryPoint !== undefined
|
|
163
|
+
);
|
|
164
|
+
assert(
|
|
165
|
+
entryPointOutputs.length > 0,
|
|
166
|
+
`Cannot find entry-point "${entry.file}" in generated bundle.` +
|
|
167
|
+
listEntryPoints(entryPointOutputs)
|
|
168
|
+
);
|
|
169
|
+
assert(
|
|
170
|
+
entryPointOutputs.length < 2,
|
|
171
|
+
"More than one entry-point found for generated bundle." +
|
|
172
|
+
listEntryPoints(entryPointOutputs)
|
|
173
|
+
);
|
|
75
174
|
|
|
76
|
-
|
|
77
|
-
|
|
175
|
+
const entryPointExports = entryPointOutputs[0][1].exports;
|
|
176
|
+
const bundleType = entryPointExports.length > 0 ? "esm" : "commonjs";
|
|
78
177
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
178
|
+
return {
|
|
179
|
+
modules: moduleCollector.modules,
|
|
180
|
+
resolvedEntryPointPath: path.resolve(
|
|
181
|
+
entry.directory,
|
|
182
|
+
entryPointOutputs[0][0]
|
|
183
|
+
),
|
|
184
|
+
bundleType,
|
|
185
|
+
stop: result.stop,
|
|
186
|
+
};
|
|
88
187
|
}
|
|
89
188
|
|
|
90
|
-
type EntryPoint =
|
|
91
|
-
| { stdin: esbuild.StdinOptions; nodePaths: string[] }
|
|
92
|
-
| { entryPoints: string[] };
|
|
189
|
+
type EntryPoint = { stdin: esbuild.StdinOptions } | { entryPoints: string[] };
|
|
93
190
|
|
|
94
191
|
/**
|
|
95
192
|
* Create an object that describes the entry point for esbuild.
|
|
@@ -99,35 +196,41 @@ type EntryPoint =
|
|
|
99
196
|
* or delegate to the actual worker.
|
|
100
197
|
*/
|
|
101
198
|
function getEntryPoint(
|
|
102
|
-
|
|
103
|
-
|
|
199
|
+
entryFile: string,
|
|
200
|
+
serveAssetsFromWorker: boolean
|
|
104
201
|
): EntryPoint {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
202
|
+
if (serveAssetsFromWorker) {
|
|
203
|
+
return {
|
|
204
|
+
stdin: {
|
|
205
|
+
contents: fs
|
|
206
|
+
.readFileSync(
|
|
207
|
+
path.join(__dirname, "../templates/static-asset-facade.js"),
|
|
208
|
+
"utf8"
|
|
209
|
+
)
|
|
210
|
+
// on windows, escape backslashes in the path (`\`)
|
|
211
|
+
.replace("__ENTRY_POINT__", entryFile.replaceAll("\\", "\\\\"))
|
|
212
|
+
.replace(
|
|
213
|
+
"__KV_ASSET_HANDLER__",
|
|
214
|
+
path
|
|
215
|
+
.join(__dirname, "../kv-asset-handler.js")
|
|
216
|
+
.replaceAll("\\", "\\\\")
|
|
217
|
+
),
|
|
218
|
+
sourcefile: "static-asset-facade.js",
|
|
219
|
+
resolveDir: path.dirname(entryFile),
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
} else {
|
|
223
|
+
return { entryPoints: [entryFile] };
|
|
224
|
+
}
|
|
122
225
|
}
|
|
123
226
|
|
|
124
227
|
/**
|
|
125
228
|
* Generate a string that describes the entry-points that were identified by esbuild.
|
|
126
229
|
*/
|
|
127
230
|
function listEntryPoints(
|
|
128
|
-
|
|
231
|
+
outputs: [string, ValueOf<esbuild.Metafile["outputs"]>][]
|
|
129
232
|
): string {
|
|
130
|
-
|
|
233
|
+
return outputs.map(([_input, output]) => output.entryPoint).join("\n");
|
|
131
234
|
}
|
|
132
235
|
|
|
133
236
|
type ValueOf<T> = T[keyof T];
|
package/src/cfetch/index.ts
CHANGED
|
@@ -8,15 +8,16 @@ import type { RequestInit } from "undici";
|
|
|
8
8
|
export { getCloudflareAPIBaseURL as getCloudflareApiBaseUrl } from "./internal";
|
|
9
9
|
|
|
10
10
|
export interface FetchError {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
code: number;
|
|
12
|
+
message: string;
|
|
13
|
+
error_chain?: FetchError[];
|
|
13
14
|
}
|
|
14
15
|
export interface FetchResult<ResponseType = unknown> {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
success: boolean;
|
|
17
|
+
result: ResponseType;
|
|
18
|
+
errors: FetchError[];
|
|
19
|
+
messages: string[];
|
|
20
|
+
result_info?: unknown;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
export { fetchKVGetValue } from "./internal";
|
|
@@ -25,20 +26,22 @@ export { fetchKVGetValue } from "./internal";
|
|
|
25
26
|
* Make a fetch request, and extract the `result` from the JSON response.
|
|
26
27
|
*/
|
|
27
28
|
export async function fetchResult<ResponseType>(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
resource: string,
|
|
30
|
+
init: RequestInit = {},
|
|
31
|
+
queryParams?: URLSearchParams,
|
|
32
|
+
abortSignal?: AbortSignal
|
|
31
33
|
): Promise<ResponseType> {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
const json = await fetchInternal<FetchResult<ResponseType>>(
|
|
35
|
+
resource,
|
|
36
|
+
init,
|
|
37
|
+
queryParams,
|
|
38
|
+
abortSignal
|
|
39
|
+
);
|
|
40
|
+
if (json.success) {
|
|
41
|
+
return json.result;
|
|
42
|
+
} else {
|
|
43
|
+
throwFetchError(resource, json);
|
|
44
|
+
}
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
/**
|
|
@@ -47,58 +50,72 @@ export async function fetchResult<ResponseType>(
|
|
|
47
50
|
* and repeating the request if the results are paginated.
|
|
48
51
|
*/
|
|
49
52
|
export async function fetchListResult<ResponseType>(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
resource: string,
|
|
54
|
+
init: RequestInit = {},
|
|
55
|
+
queryParams?: URLSearchParams
|
|
53
56
|
): Promise<ResponseType[]> {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
57
|
+
const results: ResponseType[] = [];
|
|
58
|
+
let getMoreResults = true;
|
|
59
|
+
let cursor: string | undefined;
|
|
60
|
+
while (getMoreResults) {
|
|
61
|
+
if (cursor) {
|
|
62
|
+
queryParams = new URLSearchParams(queryParams);
|
|
63
|
+
queryParams.set("cursor", cursor);
|
|
64
|
+
}
|
|
65
|
+
const json = await fetchInternal<FetchResult<ResponseType[]>>(
|
|
66
|
+
resource,
|
|
67
|
+
init,
|
|
68
|
+
queryParams
|
|
69
|
+
);
|
|
70
|
+
if (json.success) {
|
|
71
|
+
results.push(...json.result);
|
|
72
|
+
if (hasCursor(json.result_info)) {
|
|
73
|
+
cursor = json.result_info?.cursor;
|
|
74
|
+
} else {
|
|
75
|
+
getMoreResults = false;
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
throwFetchError(resource, json);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return results;
|
|
79
82
|
}
|
|
80
83
|
|
|
81
84
|
function throwFetchError(
|
|
82
|
-
|
|
83
|
-
|
|
85
|
+
resource: string,
|
|
86
|
+
response: FetchResult<unknown>
|
|
84
87
|
): never {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
88
|
+
const error = new ParseError({
|
|
89
|
+
text: `A request to the Cloudflare API (${resource}) failed.`,
|
|
90
|
+
notes: response.errors.map((err) => ({
|
|
91
|
+
text: renderError(err),
|
|
92
|
+
})),
|
|
93
|
+
});
|
|
94
|
+
// add the first error code directly to this error
|
|
95
|
+
// so consumers can use it for specific behaviour
|
|
96
|
+
const code = response.errors[0]?.code;
|
|
97
|
+
if (code) {
|
|
98
|
+
//@ts-expect-error non-standard property on Error
|
|
99
|
+
error.code = code;
|
|
100
|
+
}
|
|
101
|
+
throw error;
|
|
99
102
|
}
|
|
100
103
|
|
|
101
104
|
function hasCursor(result_info: unknown): result_info is { cursor: string } {
|
|
102
|
-
|
|
103
|
-
|
|
105
|
+
const cursor = (result_info as { cursor: string } | undefined)?.cursor;
|
|
106
|
+
return cursor !== undefined && cursor !== null && cursor !== "";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function renderError(err: FetchError, level = 0): string {
|
|
110
|
+
const chainedMessages =
|
|
111
|
+
err.error_chain
|
|
112
|
+
?.map(
|
|
113
|
+
(chainedError) =>
|
|
114
|
+
`\n${" ".repeat(level)}- ${renderError(chainedError, level + 1)}`
|
|
115
|
+
)
|
|
116
|
+
.join("\n") ?? "";
|
|
117
|
+
return (
|
|
118
|
+
(err.code ? `${err.message} [code: ${err.code}]` : err.message) +
|
|
119
|
+
chainedMessages
|
|
120
|
+
);
|
|
104
121
|
}
|