wrangler 0.0.0-e95e5a0 → 0.0.0-e98613f8
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 +26 -4
- package/bin/wrangler.js +166 -34
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +1456 -21
- package/package.json +191 -148
- 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 +30 -24
- package/src/__tests__/configuration.test.ts +3970 -3114
- 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 +1528 -737
- 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 +68 -68
- 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 +216 -94
- package/src/__tests__/helpers/mock-console.ts +58 -39
- package/src/__tests__/helpers/mock-dialogs.ts +114 -51
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
- package/src/__tests__/helpers/mock-http-server.ts +30 -30
- package/src/__tests__/helpers/mock-istty.ts +65 -18
- 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 +229 -228
- package/src/__tests__/helpers/mock-process.ts +39 -0
- package/src/__tests__/helpers/mock-stdin.ts +82 -77
- package/src/__tests__/helpers/mock-web-socket.ts +21 -21
- package/src/__tests__/helpers/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 +27 -28
- 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 +16 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +142 -109
- package/src/__tests__/index.test.ts +246 -231
- package/src/__tests__/init.test.ts +2887 -1488
- package/src/__tests__/jest.setup.ts +163 -30
- package/src/__tests__/kv.test.ts +1660 -1439
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/logout.test.ts +47 -0
- 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 +2435 -322
- package/src/__tests__/parse.test.ts +253 -245
- package/src/__tests__/paths.test.ts +39 -0
- package/src/__tests__/publish.test.ts +7536 -4778
- package/src/__tests__/pubsub.test.ts +370 -0
- package/src/__tests__/queues.test.ts +522 -0
- package/src/__tests__/r2.test.ts +388 -209
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +624 -426
- package/src/__tests__/tail.test.ts +776 -567
- 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 +117 -119
- 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 +264 -91
- 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 +789 -135
- package/src/cfetch/index.ts +123 -67
- package/src/cfetch/internal.ts +216 -90
- package/src/cli.ts +23 -8
- package/src/config/config.ts +236 -195
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +506 -339
- package/src/config/index.ts +309 -38
- package/src/config/validation-helpers.ts +379 -357
- package/src/config/validation.ts +1942 -1263
- package/src/config-cache.ts +81 -41
- package/src/create-worker-preview.ts +239 -132
- package/src/create-worker-upload-form.ts +300 -224
- 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 +20 -17
- package/src/dev/dev.tsx +549 -353
- package/src/dev/get-local-persistence-path.tsx +31 -0
- package/src/dev/local.tsx +870 -292
- package/src/dev/remote.tsx +591 -201
- package/src/dev/start-server.ts +539 -0
- package/src/dev/use-esbuild.ts +183 -97
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +201 -0
- package/src/dev.tsx +913 -0
- package/src/dialogs.tsx +146 -65
- package/src/dispatch-namespace.ts +207 -0
- package/src/durable.ts +102 -0
- package/src/entry.ts +248 -186
- package/src/environment-variables.ts +38 -23
- package/src/errors.ts +6 -6
- package/src/git-client.ts +135 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +84 -80
- package/src/index.tsx +636 -2617
- package/src/init.ts +970 -0
- package/src/inspect.ts +706 -562
- 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 +67 -41
- 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 +205 -33
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/module-collection.ts +221 -212
- package/src/open-in-browser.ts +5 -14
- package/src/package-manager.ts +182 -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/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes-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 +223 -120
- package/src/paths.ts +53 -8
- package/src/proxy.ts +628 -383
- 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 +281 -202
- 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 +86 -46
- package/src/type-generation.ts +173 -0
- package/src/update-check.ts +11 -11
- 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 +141 -42
- package/src/worker.ts +174 -101
- 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/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 +95201 -44118
- package/pages/functions/buildPlugin.ts +0 -92
- package/pages/functions/buildWorker.ts +0 -138
- package/pages/functions/filepath-routing.ts +0 -189
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -156
- package/pages/functions/template-plugin.ts +0 -146
- package/pages/functions/template-worker.ts +0 -143
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/kv.ts +0 -319
- package/src/miniflare-cli/enum-keys.ts +0 -17
- package/src/pages.tsx +0 -1866
- package/src/publish.ts +0 -469
- package/src/r2.ts +0 -50
- package/src/user.tsx +0 -1193
- 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/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,19 +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 {
|
|
7
|
+
import { loginOrRefreshIfRequired, requireApiToken } from "../user";
|
|
8
|
+
import type { ApiCredentials } from "../user";
|
|
5
9
|
import type { URLSearchParams } from "node:url";
|
|
6
|
-
import type { RequestInit, HeadersInit } from "undici";
|
|
10
|
+
import type { RequestInit, HeadersInit, Response } from "undici";
|
|
7
11
|
|
|
8
12
|
/**
|
|
9
13
|
* Get the URL to use to access the Cloudflare API.
|
|
10
14
|
*/
|
|
11
15
|
export const getCloudflareAPIBaseURL = getEnvironmentVariableFactory({
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
variableName: "CLOUDFLARE_API_BASE_URL",
|
|
17
|
+
deprecatedName: "CF_API_BASE_URL",
|
|
18
|
+
defaultValue: "https://api.cloudflare.com/client/v4",
|
|
15
19
|
});
|
|
16
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
|
+
|
|
17
58
|
/**
|
|
18
59
|
* Make a fetch request to the Cloudflare API.
|
|
19
60
|
*
|
|
@@ -24,86 +65,86 @@ export const getCloudflareAPIBaseURL = getEnvironmentVariableFactory({
|
|
|
24
65
|
* This function should not be used directly, instead use the functions in `cfetch/index.ts`.
|
|
25
66
|
*/
|
|
26
67
|
export async function fetchInternal<ResponseType>(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
68
|
+
resource: string,
|
|
69
|
+
init: RequestInit = {},
|
|
70
|
+
queryParams?: URLSearchParams,
|
|
71
|
+
abortSignal?: AbortSignal
|
|
30
72
|
): Promise<ResponseType> {
|
|
31
|
-
|
|
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
|
-
|
|
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
|
+
}
|
|
62
105
|
}
|
|
63
106
|
|
|
64
107
|
function truncate(text: string, maxLength: number): string {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
108
|
+
const { length } = text;
|
|
109
|
+
if (length <= maxLength) {
|
|
110
|
+
return text;
|
|
111
|
+
}
|
|
112
|
+
return `${text.substring(0, maxLength)}... (length = ${length})`;
|
|
70
113
|
}
|
|
71
114
|
|
|
72
115
|
function cloneHeaders(
|
|
73
|
-
|
|
116
|
+
headers: HeadersInit | undefined
|
|
74
117
|
): Record<string, string> {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
118
|
+
return headers instanceof Headers
|
|
119
|
+
? Object.fromEntries(headers.entries())
|
|
120
|
+
: Array.isArray(headers)
|
|
121
|
+
? Object.fromEntries(headers)
|
|
122
|
+
: { ...headers };
|
|
80
123
|
}
|
|
81
124
|
|
|
82
125
|
async function requireLoggedIn(): Promise<void> {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
126
|
+
const loggedIn = await loginOrRefreshIfRequired();
|
|
127
|
+
if (!loggedIn) {
|
|
128
|
+
throw new Error("Not logged in.");
|
|
129
|
+
}
|
|
87
130
|
}
|
|
88
131
|
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
+
}
|
|
95
144
|
}
|
|
96
145
|
|
|
97
|
-
function
|
|
98
|
-
|
|
99
|
-
apiToken: string
|
|
100
|
-
): void {
|
|
101
|
-
if ("Authorization" in headers) {
|
|
102
|
-
throw new Error(
|
|
103
|
-
"The request already specifies an authorisation header - cannot add a new one."
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
headers["Authorization"] = `Bearer ${apiToken}`;
|
|
146
|
+
function addUserAgent(headers: Record<string, string>): void {
|
|
147
|
+
headers["User-Agent"] = `wrangler/${wranglerVersion}`;
|
|
107
148
|
}
|
|
108
149
|
|
|
109
150
|
/**
|
|
@@ -112,26 +153,111 @@ function addAuthorizationHeader(
|
|
|
112
153
|
* doesn't return json. We inline the implementation and try not to share
|
|
113
154
|
* any code with the other calls. We should push back on any new APIs that
|
|
114
155
|
* try to introduce non-"standard" response structures.
|
|
156
|
+
*
|
|
157
|
+
* Note: any calls to fetchKVGetValue must call encodeURIComponent on key
|
|
158
|
+
* before passing it
|
|
115
159
|
*/
|
|
116
|
-
|
|
117
160
|
export async function fetchKVGetValue(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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 = {}
|
|
121
222
|
): Promise<string> {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
+
}
|
|
137
263
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
|
-
import "dotenv/config"; // Grab locally specified env params from a `.env` file.
|
|
2
1
|
import process from "process";
|
|
3
2
|
import { hideBin } from "yargs/helpers";
|
|
3
|
+
import { unstable_dev } from "./api";
|
|
4
4
|
import { FatalError } from "./errors";
|
|
5
5
|
import { main } from ".";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
+
}
|
|
21
|
+
|
|
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 };
|