wrangler 0.0.0-e3e3243 → 0.0.0-e4047cab
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 +47 -14
- package/bin/wrangler.js +166 -31
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +6263 -21
- package/package.json +191 -141
- package/src/__tests__/access.test.ts +25 -0
- package/src/__tests__/api-dev.test.ts +209 -0
- package/src/__tests__/api-devregistry.test.js +56 -0
- package/src/__tests__/api.test.ts +80 -0
- 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 +3971 -2810
- package/src/__tests__/d1.test.ts +80 -0
- package/src/__tests__/delete.test.ts +263 -0
- package/src/__tests__/deployments.test.ts +91 -0
- package/src/__tests__/dev.test.tsx +1521 -526
- package/src/__tests__/dialogs.test.tsx +40 -0
- package/src/__tests__/generate.test.ts +235 -0
- package/src/__tests__/get-host-from-url.test.ts +16 -0
- package/src/__tests__/guess-worker-format.test.ts +74 -57
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- 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 +219 -93
- package/src/__tests__/helpers/mock-console.ts +68 -32
- package/src/__tests__/helpers/mock-dialogs.ts +121 -33
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +74 -0
- package/src/__tests__/helpers/mock-known-routes.ts +7 -0
- package/src/__tests__/helpers/mock-kv.ts +27 -27
- package/src/__tests__/helpers/mock-oauth-flow.ts +256 -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/msw/handlers/access.ts +13 -0
- package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
- package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
- package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
- package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
- package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
- package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
- package/src/__tests__/helpers/msw/index.ts +21 -0
- package/src/__tests__/helpers/run-in-tmp.ts +28 -22
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
- package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
- 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 +144 -108
- package/src/__tests__/index.test.ts +248 -967
- package/src/__tests__/init.test.ts +2900 -0
- package/src/__tests__/jest.setup.ts +167 -34
- package/src/__tests__/kv.test.ts +1661 -1366
- package/src/__tests__/logger.test.ts +141 -0
- package/src/__tests__/logout.test.ts +36 -41
- package/src/__tests__/metrics.test.ts +450 -0
- package/src/__tests__/middleware.scheduled.test.ts +135 -0
- package/src/__tests__/middleware.test.ts +726 -0
- package/src/__tests__/package-manager.test.ts +312 -165
- package/src/__tests__/pages-deployment-tail.test.ts +820 -0
- package/src/__tests__/pages.test.ts +2437 -77
- package/src/__tests__/parse.test.ts +253 -245
- package/src/__tests__/paths.test.ts +39 -0
- package/src/__tests__/publish.test.ts +7569 -3445
- package/src/__tests__/pubsub.test.ts +370 -0
- package/src/__tests__/queues.test.ts +522 -0
- package/src/__tests__/r2.test.ts +388 -193
- package/src/__tests__/route.test.ts +22 -22
- package/src/__tests__/secret.test.ts +626 -407
- package/src/__tests__/tail.test.ts +779 -442
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/type-generation.test.ts +226 -0
- package/src/__tests__/user.test.ts +129 -0
- package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
- package/src/__tests__/validate-dev-props.test.ts +56 -0
- package/src/__tests__/version.test.ts +35 -0
- package/src/__tests__/whoami.test.tsx +266 -88
- package/src/__tests__/worker-namespace.test.ts +326 -0
- package/src/abort.d.ts +3 -0
- package/src/api/dev.ts +210 -0
- package/src/api/index.ts +2 -0
- package/src/bundle-reporter.tsx +68 -0
- package/src/bundle.ts +791 -123
- package/src/cfetch/index.ts +123 -67
- package/src/cfetch/internal.ts +216 -92
- package/src/cli.ts +23 -14
- package/src/config/README.md +1 -1
- package/src/config/config.ts +236 -179
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +506 -316
- package/src/config/index.ts +310 -38
- package/src/config/validation-helpers.ts +385 -342
- package/src/config/validation.ts +1970 -1099
- package/src/config-cache.ts +85 -0
- package/src/create-worker-preview.ts +240 -124
- package/src/create-worker-upload-form.ts +300 -201
- package/src/d1/backups.tsx +223 -0
- package/src/d1/constants.ts +2 -0
- package/src/d1/create.tsx +63 -0
- package/src/d1/delete.tsx +56 -0
- package/src/d1/execute.tsx +352 -0
- package/src/d1/formatTimeAgo.ts +14 -0
- package/src/d1/index.ts +101 -0
- package/src/d1/list.tsx +48 -0
- package/src/d1/migrations/apply.tsx +184 -0
- package/src/d1/migrations/create.tsx +77 -0
- package/src/d1/migrations/helpers.ts +147 -0
- package/src/d1/migrations/index.tsx +3 -0
- package/src/d1/migrations/list.tsx +79 -0
- package/src/d1/migrations/options.ts +18 -0
- package/src/d1/options.ts +22 -0
- package/src/d1/types.tsx +24 -0
- package/src/d1/utils.ts +48 -0
- package/src/delete.ts +108 -0
- package/src/deployments.ts +77 -0
- package/src/deprecated/index.ts +438 -0
- package/src/dev/dev-vars.ts +39 -0
- package/src/dev/dev.tsx +550 -328
- package/src/dev/get-local-persistence-path.tsx +31 -0
- package/src/dev/local.tsx +871 -213
- package/src/dev/remote.tsx +592 -192
- package/src/dev/start-server.ts +539 -0
- package/src/dev/use-esbuild.ts +184 -84
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +201 -0
- package/src/dev.tsx +904 -0
- package/src/dialogs.tsx +150 -59
- package/src/dispatch-namespace.ts +207 -0
- package/src/durable.ts +102 -0
- package/src/entry.ts +260 -171
- package/src/environment-variables.ts +40 -23
- package/src/errors.ts +11 -0
- package/src/git-client.ts +124 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +85 -80
- package/src/index.tsx +639 -2341
- package/src/init.ts +970 -0
- package/src/inspect.ts +707 -557
- package/src/intl-polyfill.d.ts +123 -123
- package/src/is-ci.ts +14 -0
- package/src/is-interactive.ts +16 -0
- package/src/kv/helpers.ts +430 -0
- package/src/kv/index.ts +596 -0
- package/src/logger.ts +92 -0
- package/src/metrics/index.ts +5 -0
- package/src/metrics/metrics-config.ts +239 -0
- package/src/metrics/metrics-dispatcher.ts +96 -0
- package/src/metrics/metrics-usage-headers.ts +24 -0
- package/src/metrics/send-event.ts +96 -0
- package/src/miniflare-cli/assets.ts +220 -0
- package/src/miniflare-cli/index.ts +206 -30
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +222 -213
- package/src/open-in-browser.ts +8 -16
- package/src/package-manager.ts +183 -90
- package/src/pages/build.tsx +270 -0
- package/src/pages/constants.ts +18 -0
- package/src/pages/deployment-tails.tsx +284 -0
- package/src/pages/deployments.tsx +84 -0
- package/src/pages/dev.tsx +706 -0
- package/src/pages/errors.ts +66 -0
- package/src/pages/functions/buildPlugin.ts +107 -0
- package/src/pages/functions/buildWorker.ts +166 -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-consolidation.test.ts +250 -0
- package/src/pages/functions/routes-consolidation.ts +73 -0
- package/src/pages/functions/routes-transformation.test.ts +282 -0
- package/src/pages/functions/routes-transformation.ts +115 -0
- package/src/pages/functions/routes-validation.test.ts +403 -0
- package/src/pages/functions/routes-validation.ts +202 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/functions.tsx +82 -0
- package/src/pages/hash.tsx +13 -0
- package/src/pages/index.tsx +102 -0
- package/src/pages/projects.tsx +169 -0
- package/src/pages/prompt-select-project.tsx +31 -0
- package/src/pages/publish.tsx +451 -0
- package/src/pages/types.ts +41 -0
- package/src/pages/upload.tsx +426 -0
- package/src/pages/utils.ts +23 -0
- package/src/parse.ts +224 -121
- package/src/paths.ts +53 -8
- package/src/proxy.ts +629 -375
- package/src/publish/index.ts +270 -0
- package/src/publish/publish.ts +1035 -0
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +624 -0
- package/src/queues/cli/commands/consumer/add.ts +71 -0
- package/src/queues/cli/commands/consumer/index.ts +22 -0
- package/src/queues/cli/commands/consumer/remove.ts +38 -0
- package/src/queues/cli/commands/create.ts +25 -0
- package/src/queues/cli/commands/delete.ts +26 -0
- package/src/queues/cli/commands/index.ts +36 -0
- package/src/queues/cli/commands/list.ts +25 -0
- package/src/queues/client.ts +135 -0
- package/src/queues/utils.ts +18 -0
- package/src/r2/constants.ts +4 -0
- package/src/r2/helpers.ts +131 -0
- package/src/r2/index.ts +289 -0
- package/src/routes.ts +140 -0
- package/src/secret/index.ts +378 -0
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +283 -191
- package/src/tail/createTail.ts +379 -0
- package/src/tail/filters.ts +108 -110
- package/src/tail/index.ts +211 -296
- package/src/tail/printing.ts +92 -47
- package/src/type-generation.ts +173 -0
- package/src/update-check.ts +19 -0
- package/src/user/access.ts +69 -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 +1198 -0
- package/src/utils/collectKeyValues.ts +14 -0
- package/src/whoami.tsx +142 -42
- package/src/worker.ts +182 -100
- package/src/yargs-types.ts +24 -0
- package/src/zones.ts +191 -0
- package/templates/__tests__/pages-dev-util.test.ts +128 -0
- package/templates/checked-fetch.js +17 -0
- package/templates/d1-beta-facade.js +205 -0
- package/templates/first-party-worker-module-facade.ts +18 -0
- package/templates/format-dev-errors.ts +32 -0
- package/templates/gitignore +171 -0
- package/templates/init-tests/test-jest-new-worker.js +25 -0
- package/templates/init-tests/test-vitest-new-worker.js +26 -0
- package/templates/middleware/common.ts +62 -0
- package/templates/middleware/loader-modules.ts +84 -0
- package/templates/middleware/loader-sw.ts +227 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +15 -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-dev-pipeline.ts +28 -0
- package/templates/pages-dev-util.ts +52 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +171 -0
- package/templates/pages-template-worker.ts +189 -0
- package/templates/serve-static-assets.ts +63 -0
- package/templates/service-bindings-module-facade.js +54 -0
- package/templates/service-bindings-sw-facade.js +42 -0
- package/templates/tsconfig.json +96 -95
- package/wrangler-dist/cli.d.ts +893 -0
- package/wrangler-dist/cli.js +116816 -94969
- package/miniflare-dist/index.mjs.map +0 -7
- package/pages/functions/buildWorker.ts +0 -62
- package/pages/functions/filepath-routing.test.ts +0 -223
- package/pages/functions/filepath-routing.ts +0 -181
- 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__/sentry.test.ts +0 -146
- package/src/kv.ts +0 -319
- package/src/miniflare-cli/enum-keys.ts +0 -17
- package/src/pages.tsx +0 -1061
- package/src/publish.ts +0 -350
- package/src/r2.ts +0 -50
- package/src/reporting.ts +0 -131
- package/src/user.tsx +0 -1127
- package/templates/static-asset-facade.js +0 -47
- 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/cfetch/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { URLSearchParams } from "node:url";
|
|
2
|
+
import { logger } from "../logger";
|
|
2
3
|
import { ParseError } from "../parse";
|
|
3
|
-
import { fetchInternal } from "./internal";
|
|
4
|
+
import { fetchInternal, performApiFetch } from "./internal";
|
|
4
5
|
import type { RequestInit } from "undici";
|
|
5
6
|
|
|
6
7
|
// Check out https://api.cloudflare.com/ for API docs.
|
|
@@ -8,15 +9,16 @@ import type { RequestInit } from "undici";
|
|
|
8
9
|
export { getCloudflareAPIBaseURL as getCloudflareApiBaseUrl } from "./internal";
|
|
9
10
|
|
|
10
11
|
export interface FetchError {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
code: number;
|
|
13
|
+
message: string;
|
|
14
|
+
error_chain?: FetchError[];
|
|
13
15
|
}
|
|
14
16
|
export interface FetchResult<ResponseType = unknown> {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
success: boolean;
|
|
18
|
+
result: ResponseType;
|
|
19
|
+
errors: FetchError[];
|
|
20
|
+
messages: string[];
|
|
21
|
+
result_info?: unknown;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export { fetchKVGetValue } from "./internal";
|
|
@@ -25,20 +27,22 @@ export { fetchKVGetValue } from "./internal";
|
|
|
25
27
|
* Make a fetch request, and extract the `result` from the JSON response.
|
|
26
28
|
*/
|
|
27
29
|
export async function fetchResult<ResponseType>(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
resource: string,
|
|
31
|
+
init: RequestInit = {},
|
|
32
|
+
queryParams?: URLSearchParams,
|
|
33
|
+
abortSignal?: AbortSignal
|
|
31
34
|
): Promise<ResponseType> {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
35
|
+
const json = await fetchInternal<FetchResult<ResponseType>>(
|
|
36
|
+
resource,
|
|
37
|
+
init,
|
|
38
|
+
queryParams,
|
|
39
|
+
abortSignal
|
|
40
|
+
);
|
|
41
|
+
if (json.success) {
|
|
42
|
+
return json.result;
|
|
43
|
+
} else {
|
|
44
|
+
throwFetchError(resource, json);
|
|
45
|
+
}
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
/**
|
|
@@ -47,58 +51,110 @@ export async function fetchResult<ResponseType>(
|
|
|
47
51
|
* and repeating the request if the results are paginated.
|
|
48
52
|
*/
|
|
49
53
|
export async function fetchListResult<ResponseType>(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
resource: string,
|
|
55
|
+
init: RequestInit = {},
|
|
56
|
+
queryParams?: URLSearchParams
|
|
53
57
|
): 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
|
-
|
|
58
|
+
const results: ResponseType[] = [];
|
|
59
|
+
let getMoreResults = true;
|
|
60
|
+
let cursor: string | undefined;
|
|
61
|
+
while (getMoreResults) {
|
|
62
|
+
if (cursor) {
|
|
63
|
+
queryParams = new URLSearchParams(queryParams);
|
|
64
|
+
queryParams.set("cursor", cursor);
|
|
65
|
+
}
|
|
66
|
+
const json = await fetchInternal<FetchResult<ResponseType[]>>(
|
|
67
|
+
resource,
|
|
68
|
+
init,
|
|
69
|
+
queryParams
|
|
70
|
+
);
|
|
71
|
+
if (json.success) {
|
|
72
|
+
results.push(...json.result);
|
|
73
|
+
if (hasCursor(json.result_info)) {
|
|
74
|
+
cursor = json.result_info?.cursor;
|
|
75
|
+
} else {
|
|
76
|
+
getMoreResults = false;
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
throwFetchError(resource, json);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return results;
|
|
79
83
|
}
|
|
80
84
|
|
|
81
85
|
function throwFetchError(
|
|
82
|
-
|
|
83
|
-
|
|
86
|
+
resource: string,
|
|
87
|
+
response: FetchResult<unknown>
|
|
84
88
|
): never {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
89
|
+
const error = new ParseError({
|
|
90
|
+
text: `A request to the Cloudflare API (${resource}) failed.`,
|
|
91
|
+
notes: response.errors.map((err) => ({
|
|
92
|
+
text: renderError(err),
|
|
93
|
+
})),
|
|
94
|
+
});
|
|
95
|
+
// add the first error code directly to this error
|
|
96
|
+
// so consumers can use it for specific behaviour
|
|
97
|
+
const code = response.errors[0]?.code;
|
|
98
|
+
if (code) {
|
|
99
|
+
//@ts-expect-error non-standard property on Error
|
|
100
|
+
error.code = code;
|
|
101
|
+
}
|
|
102
|
+
throw error;
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
function hasCursor(result_info: unknown): result_info is { cursor: string } {
|
|
102
|
-
|
|
103
|
-
|
|
106
|
+
const cursor = (result_info as { cursor: string } | undefined)?.cursor;
|
|
107
|
+
return cursor !== undefined && cursor !== null && cursor !== "";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function renderError(err: FetchError, level = 0): string {
|
|
111
|
+
const chainedMessages =
|
|
112
|
+
err.error_chain
|
|
113
|
+
?.map(
|
|
114
|
+
(chainedError) =>
|
|
115
|
+
`\n${" ".repeat(level)}- ${renderError(chainedError, level + 1)}`
|
|
116
|
+
)
|
|
117
|
+
.join("\n") ?? "";
|
|
118
|
+
return (
|
|
119
|
+
(err.code ? `${err.message} [code: ${err.code}]` : err.message) +
|
|
120
|
+
chainedMessages
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Fetch the raw script content of a Worker
|
|
126
|
+
* Note, this will concatenate the files of multi-module workers
|
|
127
|
+
*/
|
|
128
|
+
export async function fetchScriptContent(
|
|
129
|
+
resource: string,
|
|
130
|
+
init: RequestInit = {},
|
|
131
|
+
queryParams?: URLSearchParams,
|
|
132
|
+
abortSignal?: AbortSignal
|
|
133
|
+
): Promise<string> {
|
|
134
|
+
const response = await performApiFetch(
|
|
135
|
+
resource,
|
|
136
|
+
init,
|
|
137
|
+
queryParams,
|
|
138
|
+
abortSignal
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
logger.debug(
|
|
142
|
+
"-- START CF API RESPONSE:",
|
|
143
|
+
response.statusText,
|
|
144
|
+
response.status
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
logger.debug("HEADERS:", { ...response.headers });
|
|
148
|
+
// logger.debug("RESPONSE:", text);
|
|
149
|
+
logger.debug("-- END CF API RESPONSE");
|
|
150
|
+
const contentType = response.headers.get("content-type");
|
|
151
|
+
|
|
152
|
+
const usesModules = contentType?.startsWith("multipart");
|
|
153
|
+
if (usesModules && contentType) {
|
|
154
|
+
const form = await response.formData();
|
|
155
|
+
const entries = Array.from(form.entries());
|
|
156
|
+
return entries.map((e) => e[1]).join("\n");
|
|
157
|
+
} else {
|
|
158
|
+
return await response.text();
|
|
159
|
+
}
|
|
104
160
|
}
|
package/src/cfetch/internal.ts
CHANGED
|
@@ -1,20 +1,60 @@
|
|
|
1
|
-
import
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { fetch, File, Headers } from "undici";
|
|
3
|
+
import { version as wranglerVersion } from "../../package.json";
|
|
2
4
|
import { getEnvironmentVariableFactory } from "../environment-variables";
|
|
5
|
+
import { logger } from "../logger";
|
|
3
6
|
import { ParseError, parseJSON } from "../parse";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
7
|
+
import { loginOrRefreshIfRequired, requireApiToken } from "../user";
|
|
8
|
+
import type { ApiCredentials } from "../user";
|
|
6
9
|
import type { URLSearchParams } from "node:url";
|
|
7
|
-
import type { RequestInit, HeadersInit } from "undici";
|
|
10
|
+
import type { RequestInit, HeadersInit, Response } from "undici";
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Get the URL to use to access the Cloudflare API.
|
|
11
14
|
*/
|
|
12
15
|
export const getCloudflareAPIBaseURL = getEnvironmentVariableFactory({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
variableName: "CLOUDFLARE_API_BASE_URL",
|
|
17
|
+
deprecatedName: "CF_API_BASE_URL",
|
|
18
|
+
defaultValue: "https://api.cloudflare.com/client/v4",
|
|
16
19
|
});
|
|
17
20
|
|
|
21
|
+
/*
|
|
22
|
+
* performApiFetch does everything required to make a CF API request,
|
|
23
|
+
* but doesn't parse the response as JSON. For normal V4 API responses,
|
|
24
|
+
* use `fetchInternal`
|
|
25
|
+
* */
|
|
26
|
+
export async function performApiFetch(
|
|
27
|
+
resource: string,
|
|
28
|
+
init: RequestInit = {},
|
|
29
|
+
queryParams?: URLSearchParams,
|
|
30
|
+
abortSignal?: AbortSignal
|
|
31
|
+
) {
|
|
32
|
+
const method = init.method ?? "GET";
|
|
33
|
+
assert(
|
|
34
|
+
resource.startsWith("/"),
|
|
35
|
+
`CF API fetch - resource path must start with a "/" but got "${resource}"`
|
|
36
|
+
);
|
|
37
|
+
await requireLoggedIn();
|
|
38
|
+
const apiToken = requireApiToken();
|
|
39
|
+
const headers = cloneHeaders(init.headers);
|
|
40
|
+
addAuthorizationHeaderIfUnspecified(headers, apiToken);
|
|
41
|
+
addUserAgent(headers);
|
|
42
|
+
|
|
43
|
+
const queryString = queryParams ? `?${queryParams.toString()}` : "";
|
|
44
|
+
logger.debug(
|
|
45
|
+
`-- START CF API REQUEST: ${method} ${getCloudflareAPIBaseURL()}${resource}${queryString}`
|
|
46
|
+
);
|
|
47
|
+
logger.debug("HEADERS:", JSON.stringify(headers, null, 2));
|
|
48
|
+
logger.debug("INIT:", JSON.stringify(init, null, 2));
|
|
49
|
+
logger.debug("-- END CF API REQUEST");
|
|
50
|
+
return await fetch(`${getCloudflareAPIBaseURL()}${resource}${queryString}`, {
|
|
51
|
+
method,
|
|
52
|
+
...init,
|
|
53
|
+
headers,
|
|
54
|
+
signal: abortSignal,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
18
58
|
/**
|
|
19
59
|
* Make a fetch request to the Cloudflare API.
|
|
20
60
|
*
|
|
@@ -25,87 +65,86 @@ export const getCloudflareAPIBaseURL = getEnvironmentVariableFactory({
|
|
|
25
65
|
* This function should not be used directly, instead use the functions in `cfetch/index.ts`.
|
|
26
66
|
*/
|
|
27
67
|
export async function fetchInternal<ResponseType>(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
68
|
+
resource: string,
|
|
69
|
+
init: RequestInit = {},
|
|
70
|
+
queryParams?: URLSearchParams,
|
|
71
|
+
abortSignal?: AbortSignal
|
|
31
72
|
): Promise<ResponseType> {
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
const method = init.method ?? "GET";
|
|
74
|
+
const response = await performApiFetch(
|
|
75
|
+
resource,
|
|
76
|
+
init,
|
|
77
|
+
queryParams,
|
|
78
|
+
abortSignal
|
|
79
|
+
);
|
|
80
|
+
const jsonText = await response.text();
|
|
81
|
+
logger.debug(
|
|
82
|
+
"-- START CF API RESPONSE:",
|
|
83
|
+
response.statusText,
|
|
84
|
+
response.status
|
|
85
|
+
);
|
|
86
|
+
logger.debug("HEADERS:", JSON.stringify(response.headers, null, 2));
|
|
87
|
+
logger.debug("RESPONSE:", jsonText);
|
|
88
|
+
logger.debug("-- END CF API RESPONSE");
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
return parseJSON<ResponseType>(jsonText);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
throw new ParseError({
|
|
94
|
+
text: "Received a malformed response from the API",
|
|
95
|
+
notes: [
|
|
96
|
+
{
|
|
97
|
+
text: truncate(jsonText, 100),
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
text: `${method} ${resource} -> ${response.status} ${response.statusText}`,
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
}
|
|
64
105
|
}
|
|
65
106
|
|
|
66
107
|
function truncate(text: string, maxLength: number): string {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
108
|
+
const { length } = text;
|
|
109
|
+
if (length <= maxLength) {
|
|
110
|
+
return text;
|
|
111
|
+
}
|
|
112
|
+
return `${text.substring(0, maxLength)}... (length = ${length})`;
|
|
72
113
|
}
|
|
73
114
|
|
|
74
115
|
function cloneHeaders(
|
|
75
|
-
|
|
116
|
+
headers: HeadersInit | undefined
|
|
76
117
|
): Record<string, string> {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
118
|
+
return headers instanceof Headers
|
|
119
|
+
? Object.fromEntries(headers.entries())
|
|
120
|
+
: Array.isArray(headers)
|
|
121
|
+
? Object.fromEntries(headers)
|
|
122
|
+
: { ...headers };
|
|
82
123
|
}
|
|
83
124
|
|
|
84
125
|
async function requireLoggedIn(): Promise<void> {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
126
|
+
const loggedIn = await loginOrRefreshIfRequired();
|
|
127
|
+
if (!loggedIn) {
|
|
128
|
+
throw new Error("Not logged in.");
|
|
129
|
+
}
|
|
89
130
|
}
|
|
90
131
|
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
132
|
+
function addAuthorizationHeaderIfUnspecified(
|
|
133
|
+
headers: Record<string, string>,
|
|
134
|
+
auth: ApiCredentials
|
|
135
|
+
): void {
|
|
136
|
+
if (!("Authorization" in headers)) {
|
|
137
|
+
if ("apiToken" in auth) {
|
|
138
|
+
headers["Authorization"] = `Bearer ${auth.apiToken}`;
|
|
139
|
+
} else {
|
|
140
|
+
headers["X-Auth-Key"] = auth.authKey;
|
|
141
|
+
headers["X-Auth-Email"] = auth.authEmail;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
97
144
|
}
|
|
98
145
|
|
|
99
|
-
function
|
|
100
|
-
|
|
101
|
-
apiToken: string
|
|
102
|
-
): void {
|
|
103
|
-
if ("Authorization" in headers) {
|
|
104
|
-
throw new Error(
|
|
105
|
-
"The request already specifies an authorisation header - cannot add a new one."
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
headers["Authorization"] = `Bearer ${apiToken}`;
|
|
146
|
+
function addUserAgent(headers: Record<string, string>): void {
|
|
147
|
+
headers["User-Agent"] = `wrangler/${wranglerVersion}`;
|
|
109
148
|
}
|
|
110
149
|
|
|
111
150
|
/**
|
|
@@ -114,26 +153,111 @@ function addAuthorizationHeader(
|
|
|
114
153
|
* doesn't return json. We inline the implementation and try not to share
|
|
115
154
|
* any code with the other calls. We should push back on any new APIs that
|
|
116
155
|
* try to introduce non-"standard" response structures.
|
|
156
|
+
*
|
|
157
|
+
* Note: any calls to fetchKVGetValue must call encodeURIComponent on key
|
|
158
|
+
* before passing it
|
|
117
159
|
*/
|
|
118
|
-
|
|
119
160
|
export async function fetchKVGetValue(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
161
|
+
accountId: string,
|
|
162
|
+
namespaceId: string,
|
|
163
|
+
key: string
|
|
164
|
+
): Promise<ArrayBuffer> {
|
|
165
|
+
await requireLoggedIn();
|
|
166
|
+
const auth = requireApiToken();
|
|
167
|
+
const headers: Record<string, string> = {};
|
|
168
|
+
addAuthorizationHeaderIfUnspecified(headers, auth);
|
|
169
|
+
const resource = `${getCloudflareAPIBaseURL()}/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`;
|
|
170
|
+
const response = await fetch(resource, {
|
|
171
|
+
method: "GET",
|
|
172
|
+
headers,
|
|
173
|
+
});
|
|
174
|
+
if (response.ok) {
|
|
175
|
+
return await response.arrayBuffer();
|
|
176
|
+
} else {
|
|
177
|
+
throw new Error(
|
|
178
|
+
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The implementation for fetching a R2 object from Cloudflare API.
|
|
185
|
+
* We have a special implementation to handle the non-standard API response
|
|
186
|
+
* that doesn't return JSON, likely due to the streaming nature.
|
|
187
|
+
*
|
|
188
|
+
* note: The implementation should be called from light wrappers for
|
|
189
|
+
* different methods (GET, PUT)
|
|
190
|
+
*/
|
|
191
|
+
type ResponseWithBody = Response & { body: NonNullable<Response["body"]> };
|
|
192
|
+
export async function fetchR2Objects(
|
|
193
|
+
resource: string,
|
|
194
|
+
bodyInit: RequestInit = {}
|
|
195
|
+
): Promise<ResponseWithBody> {
|
|
196
|
+
await requireLoggedIn();
|
|
197
|
+
const auth = requireApiToken();
|
|
198
|
+
const headers = cloneHeaders(bodyInit.headers);
|
|
199
|
+
addAuthorizationHeaderIfUnspecified(headers, auth);
|
|
200
|
+
addUserAgent(headers);
|
|
201
|
+
|
|
202
|
+
const response = await fetch(`${getCloudflareAPIBaseURL()}${resource}`, {
|
|
203
|
+
...bodyInit,
|
|
204
|
+
headers,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (response.ok && response.body) {
|
|
208
|
+
return response as ResponseWithBody;
|
|
209
|
+
} else {
|
|
210
|
+
throw new Error(
|
|
211
|
+
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* This is a wrapper STOPGAP for getting the script which returns a raw text response.
|
|
218
|
+
*/
|
|
219
|
+
export async function fetchDashboardScript(
|
|
220
|
+
resource: string,
|
|
221
|
+
bodyInit: RequestInit = {}
|
|
123
222
|
): Promise<string> {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
223
|
+
await requireLoggedIn();
|
|
224
|
+
const auth = requireApiToken();
|
|
225
|
+
const headers = cloneHeaders(bodyInit.headers);
|
|
226
|
+
addAuthorizationHeaderIfUnspecified(headers, auth);
|
|
227
|
+
addUserAgent(headers);
|
|
228
|
+
|
|
229
|
+
const response = await fetch(`${getCloudflareAPIBaseURL()}${resource}`, {
|
|
230
|
+
...bodyInit,
|
|
231
|
+
headers,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
if (!response.ok || !response.body) {
|
|
235
|
+
throw new Error(
|
|
236
|
+
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const usesModules = response.headers
|
|
241
|
+
.get("content-type")
|
|
242
|
+
?.startsWith("multipart");
|
|
243
|
+
|
|
244
|
+
if (usesModules) {
|
|
245
|
+
// Response from edge contains generic "name = worker.js" for dashboard created scripts
|
|
246
|
+
const form = await response.formData();
|
|
247
|
+
const entries = Array.from(form.entries());
|
|
248
|
+
if (entries.length > 1)
|
|
249
|
+
throw new RangeError("Expected only one entry in multipart response");
|
|
250
|
+
const [_, file] = entries[0];
|
|
251
|
+
|
|
252
|
+
if (file instanceof File) {
|
|
253
|
+
return await file.text();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return file ?? "";
|
|
257
|
+
|
|
258
|
+
// Follow up on issue in Undici about multipart/form-data support & replace the workaround: https://github.com/nodejs/undici/issues/974
|
|
259
|
+
// This should be using a builtin formData() parser pattern.
|
|
260
|
+
} else {
|
|
261
|
+
return response.text();
|
|
262
|
+
}
|
|
139
263
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import process from "process";
|
|
2
2
|
import { hideBin } from "yargs/helpers";
|
|
3
|
-
import {
|
|
3
|
+
import { unstable_dev } from "./api";
|
|
4
|
+
import { FatalError } from "./errors";
|
|
4
5
|
import { main } from ".";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
import type { UnstableDevWorker } from "./api";
|
|
8
|
+
/**
|
|
9
|
+
* The main entrypoint for the CLI.
|
|
10
|
+
* main only gets called when the script is run directly, not when it's imported as a module.
|
|
11
|
+
*/
|
|
12
|
+
if (typeof jest === "undefined" && require.main === module) {
|
|
13
|
+
main(hideBin(process.argv)).catch((e) => {
|
|
14
|
+
// The logging of any error that was thrown from `main()` is handled in the `yargs.fail()` handler.
|
|
15
|
+
// Here we just want to ensure that the process exits with a non-zero code.
|
|
16
|
+
// We don't want to do this inside the `main()` function, since that would kill the process when running our tests.
|
|
17
|
+
const exitCode = (e instanceof FatalError && e.code) || 1;
|
|
18
|
+
process.exit(exitCode);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
9
21
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// We don't want to do this inside the `main()` function, since that would kill the process when running our tests.
|
|
18
|
-
process.exit(1);
|
|
19
|
-
});
|
|
22
|
+
/**
|
|
23
|
+
* This is how we're exporting the API.
|
|
24
|
+
* It makes it possible to import wrangler from 'wrangler',
|
|
25
|
+
* and call wrangler.unstable_dev().
|
|
26
|
+
*/
|
|
27
|
+
export { unstable_dev };
|
|
28
|
+
export type { UnstableDevWorker };
|
package/src/config/README.md
CHANGED
|
@@ -48,7 +48,7 @@ This function will return:
|
|
|
48
48
|
- a `Diagnostics` object, which contains any errors or warnings from the validation process
|
|
49
49
|
|
|
50
50
|
The field values may have been parsed directly from the `RawConfig`, inherited into a named environment from the top-level environment, or given a default value.
|
|
51
|
-
Generally, if there are any warnings they should be presented to the user via `
|
|
51
|
+
Generally, if there are any warnings they should be presented to the user via `logger.warn()` messages,
|
|
52
52
|
and if there are any errors then an `Error` should be thrown describing these errors.
|
|
53
53
|
|
|
54
54
|
The `Diagnostics` object is hierarchical: each `Diagnostics` instance can contain a collection of child `Diagnostics` instance.
|