wrangler 0.0.0-e88a54e → 0.0.0-e8e684c7
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 +51 -14
- package/bin/wrangler.js +161 -29
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +5484 -21
- package/package.json +173 -143
- 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 +4016 -2786
- package/src/__tests__/dev.test.tsx +1305 -483
- package/src/__tests__/generate.test.ts +91 -0
- package/src/__tests__/guess-worker-format.test.ts +75 -48
- 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 +182 -93
- package/src/__tests__/helpers/mock-console.ts +59 -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 +26 -26
- 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 +28 -22
- 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 +144 -108
- package/src/__tests__/index.test.ts +229 -975
- package/src/__tests__/init.test.ts +2170 -0
- package/src/__tests__/jest.setup.ts +122 -34
- package/src/__tests__/kv.test.ts +1376 -1087
- package/src/__tests__/logger.test.ts +141 -0
- package/src/__tests__/metrics.test.ts +450 -0
- package/src/__tests__/package-manager.test.ts +312 -165
- package/src/__tests__/pages.test.ts +1270 -39
- package/src/__tests__/parse.test.ts +253 -245
- package/src/__tests__/publish.test.ts +6878 -2627
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +328 -190
- package/src/__tests__/route.test.ts +22 -22
- package/src/__tests__/secret.test.ts +474 -407
- package/src/__tests__/tail.test.ts +679 -441
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/user.test.ts +155 -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 +276 -87
- package/src/__tests__/worker-namespace.test.ts +329 -0
- package/src/abort.d.ts +3 -0
- package/src/api/dev.ts +93 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +441 -110
- package/src/cfetch/index.ts +83 -66
- package/src/cfetch/internal.ts +251 -92
- package/src/cli.ts +22 -14
- package/src/config/README.md +1 -1
- package/src/config/config.ts +224 -179
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +430 -311
- package/src/config/index.ts +227 -38
- package/src/config/validation-helpers.ts +414 -303
- package/src/config/validation.ts +1745 -973
- package/src/config-cache.ts +85 -0
- package/src/create-worker-preview.ts +209 -118
- package/src/create-worker-upload-form.ts +275 -201
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +488 -307
- package/src/dev/local.tsx +427 -210
- package/src/dev/remote.tsx +407 -188
- package/src/dev/use-esbuild.ts +156 -78
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +160 -0
- package/src/dev.tsx +696 -0
- package/src/dialogs.tsx +115 -57
- package/src/dispatch-namespace.ts +206 -0
- package/src/durable.ts +102 -0
- package/src/entry.ts +267 -147
- 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 +131 -0
- package/src/git-client.ts +42 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +85 -80
- package/src/index.tsx +2191 -2304
- package/src/init.ts +622 -0
- package/src/inspect.ts +687 -557
- package/src/intl-polyfill.d.ts +139 -0
- package/src/is-ci.ts +14 -0
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +353 -242
- package/src/logger.ts +73 -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 +92 -0
- package/src/miniflare-cli/assets.ts +554 -0
- package/src/miniflare-cli/enum-keys.ts +8 -8
- package/src/miniflare-cli/index.ts +199 -29
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +213 -213
- package/src/open-in-browser.ts +8 -16
- package/src/package-manager.ts +183 -90
- package/src/pages/build.tsx +238 -0
- package/src/pages/constants.ts +12 -0
- package/src/pages/deployments.tsx +106 -0
- package/src/pages/dev.tsx +528 -0
- package/src/pages/errors.ts +22 -0
- package/src/pages/functions/buildPlugin.ts +109 -0
- package/src/pages/functions/buildWorker.ts +155 -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 +271 -0
- package/src/pages/functions/routes-transformation.ts +122 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/functions.tsx +96 -0
- package/src/pages/index.tsx +94 -0
- package/src/pages/projects.tsx +169 -0
- package/src/pages/publish.tsx +391 -0
- package/src/pages/types.ts +49 -0
- package/src/pages/upload.tsx +413 -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 +416 -370
- package/src/publish.ts +796 -321
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +623 -0
- package/src/r2.ts +100 -19
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +281 -189
- package/src/tail/filters.ts +111 -109
- package/src/tail/index.ts +230 -217
- package/src/tail/printing.ts +92 -47
- 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 +1191 -0
- package/src/whoami.tsx +142 -42
- package/src/worker.ts +135 -94
- package/src/zones.ts +167 -0
- package/templates/checked-fetch.js +17 -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/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 +167 -0
- package/templates/pages-template-worker.ts +172 -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 +95 -95
- package/wrangler-dist/cli.d.ts +338 -0
- package/wrangler-dist/cli.js +124611 -112403
- 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__/dev2.test.tsx +0 -154
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/__tests__/logout.test.ts +0 -52
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/pages.tsx +0 -1047
- package/src/reporting.ts +0 -131
- package/src/user.tsx +0 -1118
- 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
|
@@ -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
|
}
|
package/src/cfetch/internal.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import Busboy from "busboy";
|
|
3
|
+
import { fetch, File, FormData, Headers } from "undici";
|
|
4
|
+
import { version as wranglerVersion } from "../../package.json";
|
|
2
5
|
import { getEnvironmentVariableFactory } from "../environment-variables";
|
|
6
|
+
import { logger } from "../logger";
|
|
3
7
|
import { ParseError, parseJSON } from "../parse";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
8
|
+
import { loginOrRefreshIfRequired, requireApiToken } from "../user";
|
|
9
|
+
import type { ApiCredentials } from "../user";
|
|
6
10
|
import type { URLSearchParams } from "node:url";
|
|
7
|
-
import type { RequestInit, HeadersInit } from "undici";
|
|
11
|
+
import type { RequestInit, HeadersInit, Response } from "undici";
|
|
8
12
|
|
|
9
13
|
/**
|
|
10
14
|
* Get the URL to use to access the Cloudflare API.
|
|
11
15
|
*/
|
|
12
16
|
export const getCloudflareAPIBaseURL = getEnvironmentVariableFactory({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
variableName: "CLOUDFLARE_API_BASE_URL",
|
|
18
|
+
deprecatedName: "CF_API_BASE_URL",
|
|
19
|
+
defaultValue: "https://api.cloudflare.com/client/v4",
|
|
16
20
|
});
|
|
17
21
|
|
|
18
22
|
/**
|
|
@@ -25,87 +29,107 @@ export const getCloudflareAPIBaseURL = getEnvironmentVariableFactory({
|
|
|
25
29
|
* This function should not be used directly, instead use the functions in `cfetch/index.ts`.
|
|
26
30
|
*/
|
|
27
31
|
export async function fetchInternal<ResponseType>(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
resource: string,
|
|
33
|
+
init: RequestInit = {},
|
|
34
|
+
queryParams?: URLSearchParams,
|
|
35
|
+
abortSignal?: AbortSignal
|
|
31
36
|
): 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
|
-
|
|
37
|
+
assert(
|
|
38
|
+
resource.startsWith("/"),
|
|
39
|
+
`CF API fetch - resource path must start with a "/" but got "${resource}"`
|
|
40
|
+
);
|
|
41
|
+
await requireLoggedIn();
|
|
42
|
+
const apiToken = requireApiToken();
|
|
43
|
+
const headers = cloneHeaders(init.headers);
|
|
44
|
+
addAuthorizationHeaderIfUnspecified(headers, apiToken);
|
|
45
|
+
addUserAgent(headers);
|
|
46
|
+
|
|
47
|
+
const queryString = queryParams ? `?${queryParams.toString()}` : "";
|
|
48
|
+
const method = init.method ?? "GET";
|
|
49
|
+
|
|
50
|
+
logger.debug(
|
|
51
|
+
`-- START CF API REQUEST: ${method} ${getCloudflareAPIBaseURL()}${resource}${queryString}`
|
|
52
|
+
);
|
|
53
|
+
logger.debug("HEADERS:", JSON.stringify(headers, null, 2));
|
|
54
|
+
logger.debug("INIT:", JSON.stringify(init, null, 2));
|
|
55
|
+
logger.debug("-- END CF API REQUEST");
|
|
56
|
+
const response = await fetch(
|
|
57
|
+
`${getCloudflareAPIBaseURL()}${resource}${queryString}`,
|
|
58
|
+
{
|
|
59
|
+
method,
|
|
60
|
+
...init,
|
|
61
|
+
headers,
|
|
62
|
+
signal: abortSignal,
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
const jsonText = await response.text();
|
|
66
|
+
logger.debug(
|
|
67
|
+
"-- START CF API RESPONSE:",
|
|
68
|
+
response.statusText,
|
|
69
|
+
response.status
|
|
70
|
+
);
|
|
71
|
+
logger.debug("HEADERS:", JSON.stringify(response.headers, null, 2));
|
|
72
|
+
logger.debug("RESPONSE:", jsonText);
|
|
73
|
+
logger.debug("-- END CF API RESPONSE");
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
return parseJSON<ResponseType>(jsonText);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
throw new ParseError({
|
|
79
|
+
text: "Received a malformed response from the API",
|
|
80
|
+
notes: [
|
|
81
|
+
{
|
|
82
|
+
text: truncate(jsonText, 100),
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
text: `${method} ${resource} -> ${response.status} ${response.statusText}`,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
});
|
|
89
|
+
}
|
|
64
90
|
}
|
|
65
91
|
|
|
66
92
|
function truncate(text: string, maxLength: number): string {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
93
|
+
const { length } = text;
|
|
94
|
+
if (length <= maxLength) {
|
|
95
|
+
return text;
|
|
96
|
+
}
|
|
97
|
+
return `${text.substring(0, maxLength)}... (length = ${length})`;
|
|
72
98
|
}
|
|
73
99
|
|
|
74
100
|
function cloneHeaders(
|
|
75
|
-
|
|
101
|
+
headers: HeadersInit | undefined
|
|
76
102
|
): Record<string, string> {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
103
|
+
return headers instanceof Headers
|
|
104
|
+
? Object.fromEntries(headers.entries())
|
|
105
|
+
: Array.isArray(headers)
|
|
106
|
+
? Object.fromEntries(headers)
|
|
107
|
+
: { ...headers };
|
|
82
108
|
}
|
|
83
109
|
|
|
84
110
|
async function requireLoggedIn(): Promise<void> {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
111
|
+
const loggedIn = await loginOrRefreshIfRequired();
|
|
112
|
+
if (!loggedIn) {
|
|
113
|
+
throw new Error("Not logged in.");
|
|
114
|
+
}
|
|
89
115
|
}
|
|
90
116
|
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
117
|
+
function addAuthorizationHeaderIfUnspecified(
|
|
118
|
+
headers: Record<string, string>,
|
|
119
|
+
auth: ApiCredentials
|
|
120
|
+
): void {
|
|
121
|
+
if (!("Authorization" in headers)) {
|
|
122
|
+
if ("apiToken" in auth) {
|
|
123
|
+
headers["Authorization"] = `Bearer ${auth.apiToken}`;
|
|
124
|
+
} else {
|
|
125
|
+
headers["X-Auth-Key"] = auth.authKey;
|
|
126
|
+
headers["X-Auth-Email"] = auth.authEmail;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
97
129
|
}
|
|
98
130
|
|
|
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}`;
|
|
131
|
+
function addUserAgent(headers: Record<string, string>): void {
|
|
132
|
+
headers["User-Agent"] = `wrangler/${wranglerVersion}`;
|
|
109
133
|
}
|
|
110
134
|
|
|
111
135
|
/**
|
|
@@ -114,26 +138,161 @@ function addAuthorizationHeader(
|
|
|
114
138
|
* doesn't return json. We inline the implementation and try not to share
|
|
115
139
|
* any code with the other calls. We should push back on any new APIs that
|
|
116
140
|
* try to introduce non-"standard" response structures.
|
|
141
|
+
*
|
|
142
|
+
* Note: any calls to fetchKVGetValue must call encodeURIComponent on key
|
|
143
|
+
* before passing it
|
|
117
144
|
*/
|
|
118
|
-
|
|
119
145
|
export async function fetchKVGetValue(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
146
|
+
accountId: string,
|
|
147
|
+
namespaceId: string,
|
|
148
|
+
key: string
|
|
149
|
+
): Promise<ArrayBuffer> {
|
|
150
|
+
await requireLoggedIn();
|
|
151
|
+
const auth = requireApiToken();
|
|
152
|
+
const headers: Record<string, string> = {};
|
|
153
|
+
addAuthorizationHeaderIfUnspecified(headers, auth);
|
|
154
|
+
const resource = `${getCloudflareAPIBaseURL()}/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`;
|
|
155
|
+
const response = await fetch(resource, {
|
|
156
|
+
method: "GET",
|
|
157
|
+
headers,
|
|
158
|
+
});
|
|
159
|
+
if (response.ok) {
|
|
160
|
+
return await response.arrayBuffer();
|
|
161
|
+
} else {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The implementation for fetching a R2 object from Cloudflare API.
|
|
170
|
+
* We have a special implementation to handle the non-standard API response
|
|
171
|
+
* that doesn't return JSON, likely due to the streaming nature.
|
|
172
|
+
*
|
|
173
|
+
* note: The implementation should be called from light wrappers for
|
|
174
|
+
* different methods (GET, PUT)
|
|
175
|
+
*/
|
|
176
|
+
type ResponseWithBody = Response & { body: NonNullable<Response["body"]> };
|
|
177
|
+
export async function fetchR2Objects(
|
|
178
|
+
resource: string,
|
|
179
|
+
bodyInit: RequestInit = {}
|
|
180
|
+
): Promise<ResponseWithBody> {
|
|
181
|
+
await requireLoggedIn();
|
|
182
|
+
const auth = requireApiToken();
|
|
183
|
+
const headers = cloneHeaders(bodyInit.headers);
|
|
184
|
+
addAuthorizationHeaderIfUnspecified(headers, auth);
|
|
185
|
+
addUserAgent(headers);
|
|
186
|
+
|
|
187
|
+
const response = await fetch(`${getCloudflareAPIBaseURL()}${resource}`, {
|
|
188
|
+
...bodyInit,
|
|
189
|
+
headers,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
if (response.ok && response.body) {
|
|
193
|
+
return response as ResponseWithBody;
|
|
194
|
+
} else {
|
|
195
|
+
throw new Error(
|
|
196
|
+
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* This is a wrapper STOPGAP for getting the script which returns a raw text response.
|
|
203
|
+
*/
|
|
204
|
+
export async function fetchDashboardScript(
|
|
205
|
+
resource: string,
|
|
206
|
+
bodyInit: RequestInit = {}
|
|
123
207
|
): Promise<string> {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
208
|
+
await requireLoggedIn();
|
|
209
|
+
const auth = requireApiToken();
|
|
210
|
+
const headers = cloneHeaders(bodyInit.headers);
|
|
211
|
+
addAuthorizationHeaderIfUnspecified(headers, auth);
|
|
212
|
+
addUserAgent(headers);
|
|
213
|
+
|
|
214
|
+
const response = await fetch(`${getCloudflareAPIBaseURL()}${resource}`, {
|
|
215
|
+
...bodyInit,
|
|
216
|
+
headers,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
if (!response.ok || !response.body) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const usesModules = response.headers
|
|
226
|
+
.get("content-type")
|
|
227
|
+
?.startsWith("multipart");
|
|
228
|
+
|
|
229
|
+
if (usesModules) {
|
|
230
|
+
// Response from edge contains generic "name = worker.js" for dashboard created scripts
|
|
231
|
+
const form = await formData(response);
|
|
232
|
+
const entries = Array.from(form.entries());
|
|
233
|
+
if (entries.length > 1)
|
|
234
|
+
throw new RangeError("Expected only one entry in multipart response");
|
|
235
|
+
const [_, file] = entries[0];
|
|
236
|
+
|
|
237
|
+
if (file instanceof File) {
|
|
238
|
+
return await file.text();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return file ?? "";
|
|
242
|
+
|
|
243
|
+
// Follow up on issue in Undici about multipart/form-data support & replace the workaround: https://github.com/nodejs/undici/issues/974
|
|
244
|
+
// This should be using a builtin formData() parser pattern.
|
|
245
|
+
} else {
|
|
246
|
+
return response.text();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function formData({ headers, body }: Response): Promise<FormData> {
|
|
251
|
+
// undici doesn't include a multipart/form-data parser yet, so we parse
|
|
252
|
+
// form data with busboy instead
|
|
253
|
+
const contentType = headers.get("Content-Type") ?? "";
|
|
254
|
+
if (!/multipart\/form-data/.test(contentType))
|
|
255
|
+
throw Error("Need Content-Type for multipart/form-data");
|
|
256
|
+
|
|
257
|
+
const responseFormData = new FormData();
|
|
258
|
+
|
|
259
|
+
let busboy: Busboy.Busboy;
|
|
260
|
+
|
|
261
|
+
const parsedHeaders = Object.fromEntries(
|
|
262
|
+
Array.from(headers).map(([header, value]) => [header.toLowerCase(), value])
|
|
263
|
+
);
|
|
264
|
+
try {
|
|
265
|
+
busboy = Busboy({ headers: parsedHeaders });
|
|
266
|
+
} catch (err) {
|
|
267
|
+
// Error due to headers:
|
|
268
|
+
throw Object.assign(new TypeError(), { cause: err });
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
busboy.on("field", (name, value) => {
|
|
272
|
+
responseFormData.append(name, value);
|
|
273
|
+
});
|
|
274
|
+
busboy.on("file", (name, value, info) => {
|
|
275
|
+
const { filename, encoding, mimeType } = info;
|
|
276
|
+
const base64 = encoding.toLowerCase() === "base64";
|
|
277
|
+
const chunks: Buffer[] = [];
|
|
278
|
+
value.on("data", (chunk) => {
|
|
279
|
+
if (base64) chunk = Buffer.from(chunk.toString(), "base64");
|
|
280
|
+
chunks.push(chunk);
|
|
281
|
+
});
|
|
282
|
+
value.on("end", () => {
|
|
283
|
+
const file = new File(chunks, filename, { type: mimeType });
|
|
284
|
+
responseFormData.append(name, file);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
const busboyResolve = new Promise((resolve, reject) => {
|
|
289
|
+
busboy.on("finish", resolve);
|
|
290
|
+
busboy.on("error", (err) => reject(err));
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
if (body !== null) for await (const chunk of body) busboy.write(chunk);
|
|
294
|
+
busboy.end();
|
|
295
|
+
await busboyResolve;
|
|
296
|
+
|
|
297
|
+
return responseFormData;
|
|
139
298
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
|
+
import "dotenv/config"; // Grab locally specified env params from a `.env` file.
|
|
1
2
|
import process from "process";
|
|
2
3
|
import { hideBin } from "yargs/helpers";
|
|
3
|
-
import {
|
|
4
|
+
import { unstable_dev } from "./api";
|
|
5
|
+
import { FatalError } from "./errors";
|
|
4
6
|
import { main } from ".";
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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) {
|
|
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
|
-
// Here we just want to ensure that the process exits with a non-zero code.
|
|
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 default { unstable_dev };
|
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.
|