wrangler 0.0.0-ece06ea → 0.0.0-ecfbb0c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cloudflare_CA.pem +18 -0
- package/README.md +33 -14
- package/bin/wrangler.js +123 -25
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +4893 -0
- package/package.json +157 -116
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +3829 -0
- package/src/__tests__/dev.test.tsx +1159 -0
- package/src/__tests__/guess-worker-format.test.ts +85 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +48 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/helpers/mock-cfetch.ts +211 -0
- package/src/__tests__/helpers/mock-console.ts +97 -0
- package/src/__tests__/helpers/mock-dialogs.ts +153 -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-kv.ts +33 -0
- 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 +108 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +39 -0
- package/src/__tests__/helpers/run-wrangler.ts +16 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +130 -0
- package/src/__tests__/index.test.ts +255 -148
- package/src/__tests__/init.test.ts +1992 -0
- package/src/__tests__/jest.setup.ts +107 -0
- package/src/__tests__/kv.test.ts +1619 -0
- package/src/__tests__/logger.test.ts +141 -0
- package/src/__tests__/package-manager.test.ts +353 -0
- package/src/__tests__/pages.test.ts +1254 -0
- package/src/__tests__/parse.test.ts +330 -0
- package/src/__tests__/publish.test.ts +6513 -0
- package/src/__tests__/pubsub.test.ts +276 -0
- package/src/__tests__/r2.test.ts +233 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +486 -0
- package/src/__tests__/tail.test.ts +690 -0
- package/src/__tests__/user.test.ts +143 -0
- package/src/__tests__/whoami.test.tsx +220 -0
- package/src/abort.d.ts +3 -0
- package/src/api/dev.ts +39 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +212 -0
- package/src/cfetch/index.ts +121 -0
- package/src/cfetch/internal.ts +149 -0
- package/src/cli.ts +25 -8
- package/src/config/README.md +107 -0
- package/src/config/config.ts +243 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +430 -0
- package/src/config/index.ts +225 -0
- package/src/config/validation-helpers.ts +584 -0
- package/src/config/validation.ts +1693 -0
- package/src/config-cache.ts +44 -0
- package/src/create-worker-preview.ts +199 -0
- package/src/create-worker-upload-form.ts +254 -0
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +417 -0
- package/src/dev/local.tsx +350 -0
- package/src/dev/remote.tsx +294 -0
- package/src/dev/use-esbuild.ts +150 -0
- package/src/dev.tsx +517 -727
- package/src/dialogs.tsx +116 -66
- package/src/durable.ts +102 -0
- package/src/entry.ts +328 -0
- package/src/environment-variables.ts +35 -0
- package/src/errors.ts +11 -0
- package/src/generate.ts +33 -0
- package/src/git-client.ts +42 -0
- package/src/https-options.ts +123 -0
- package/src/index.tsx +1845 -1828
- package/src/init.ts +549 -0
- package/src/inspect.ts +662 -0
- package/src/intl-polyfill.d.ts +139 -0
- package/src/kv.ts +408 -0
- package/src/logger.ts +73 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +52 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +258 -0
- package/src/open-in-browser.ts +17 -0
- package/src/package-manager.ts +174 -0
- package/src/pages/build.tsx +202 -0
- package/src/pages/constants.ts +7 -0
- package/src/pages/deployments.tsx +101 -0
- package/src/pages/dev.tsx +964 -0
- package/src/pages/functions/buildPlugin.ts +105 -0
- package/src/pages/functions/buildWorker.ts +151 -0
- package/src/pages/functions/filepath-routing.test.ts +234 -0
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/index.tsx +84 -0
- package/src/pages/projects.tsx +157 -0
- package/src/pages/publish.tsx +335 -0
- package/src/pages/types.ts +40 -0
- package/src/pages/upload.tsx +384 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +286 -0
- package/src/paths.ts +26 -0
- package/src/preview.ts +31 -0
- package/src/proxy.ts +476 -0
- package/src/publish.ts +755 -343
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +594 -0
- package/src/r2.ts +50 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +357 -100
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +67 -0
- package/src/update-check.ts +19 -0
- package/src/user/choose-account.tsx +60 -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 +1130 -0
- package/src/whoami.tsx +90 -0
- package/src/worker.ts +185 -0
- package/src/zones.ts +76 -0
- package/templates/checked-fetch.js +17 -0
- package/templates/gitignore +171 -0
- package/templates/new-worker-scheduled.js +17 -0
- package/templates/new-worker-scheduled.ts +32 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +30 -0
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-template-plugin.ts +155 -0
- package/templates/pages-template-worker.ts +161 -0
- package/templates/static-asset-facade.js +43 -0
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +104118 -113381
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/form_data.ts +0 -158
- package/src/api/inspect.ts +0 -441
- package/src/api/preview.ts +0 -123
- package/src/api/worker.ts +0 -161
- package/src/cfetch.ts +0 -72
- package/src/config.ts +0 -124
- package/src/kv.tsx +0 -211
- package/src/pages.tsx +0 -344
- package/src/tail.tsx +0 -71
- package/src/user.tsx +0 -1029
- package/src/util/fetch.ts +0 -74
- package/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/api/preview.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import cfetch from "../cfetch";
|
|
2
|
-
import { fetchJson } from "../util/fetch";
|
|
3
|
-
import { toFormData } from "./form_data";
|
|
4
|
-
import type { CfAccount, CfWorkerInit } from "./worker";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A preview mode.
|
|
8
|
-
*
|
|
9
|
-
* * If true, then using a `workers.dev` subdomain.
|
|
10
|
-
* * Otherwise, a list of routes under a single zone.
|
|
11
|
-
*/
|
|
12
|
-
export type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A preview token.
|
|
16
|
-
*/
|
|
17
|
-
export interface CfPreviewToken {
|
|
18
|
-
/**
|
|
19
|
-
* The header value required to trigger a preview.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* const headers = { 'cf-workers-preview-token': value }
|
|
23
|
-
* const response = await fetch('https://' + host, { headers })
|
|
24
|
-
*/
|
|
25
|
-
value: string;
|
|
26
|
-
/**
|
|
27
|
-
* The host where the preview is available.
|
|
28
|
-
*/
|
|
29
|
-
host: string;
|
|
30
|
-
/**
|
|
31
|
-
* A websocket url to a DevTools inspector.
|
|
32
|
-
*
|
|
33
|
-
* Workers does not have a fully-featured implementation
|
|
34
|
-
* of the Chrome DevTools protocol, but supports the following:
|
|
35
|
-
* * `console.log()` output.
|
|
36
|
-
* * `Error` stack traces.
|
|
37
|
-
* * `fetch()` events.
|
|
38
|
-
*
|
|
39
|
-
* There is no support for breakpoints, but we want to implement
|
|
40
|
-
* this eventually.
|
|
41
|
-
*
|
|
42
|
-
* @link https://chromedevtools.github.io/devtools-protocol/
|
|
43
|
-
*/
|
|
44
|
-
inspectorUrl: URL;
|
|
45
|
-
/**
|
|
46
|
-
* A url to prewarm the preview session.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* fetch(prewarmUrl, { method: 'POST' })
|
|
50
|
-
*/
|
|
51
|
-
prewarmUrl: URL;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Generates a preview session token.
|
|
56
|
-
*/
|
|
57
|
-
async function sessionToken(account: CfAccount): Promise<CfPreviewToken> {
|
|
58
|
-
const { accountId, zoneId } = account;
|
|
59
|
-
const initUrl = zoneId
|
|
60
|
-
? `/zones/${zoneId}/workers/edge-preview`
|
|
61
|
-
: `/accounts/${accountId}/workers/subdomain/edge-preview`;
|
|
62
|
-
|
|
63
|
-
const { exchange_url: tokenUrl } = await cfetch<{ exchange_url: string }>(
|
|
64
|
-
initUrl
|
|
65
|
-
);
|
|
66
|
-
const { inspector_websocket: url, token } = await fetchJson()(tokenUrl);
|
|
67
|
-
const { host } = new URL(url);
|
|
68
|
-
const query = `cf_workers_preview_token=${token}`;
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
value: token,
|
|
72
|
-
host,
|
|
73
|
-
inspectorUrl: new URL(`${url}?${query}`),
|
|
74
|
-
prewarmUrl: new URL(
|
|
75
|
-
`https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
|
|
76
|
-
),
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Credit: https://stackoverflow.com/a/2117523
|
|
81
|
-
function randomId(): string {
|
|
82
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
83
|
-
const r = (Math.random() * 16) | 0,
|
|
84
|
-
v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
85
|
-
return v.toString(16);
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Creates a preview token.
|
|
91
|
-
*/
|
|
92
|
-
export async function previewToken(
|
|
93
|
-
account: CfAccount,
|
|
94
|
-
worker: CfWorkerInit
|
|
95
|
-
): Promise<CfPreviewToken> {
|
|
96
|
-
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(account);
|
|
97
|
-
|
|
98
|
-
const { accountId, zoneId } = account;
|
|
99
|
-
const scriptId = zoneId ? randomId() : host.split(".")[0];
|
|
100
|
-
const url = `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
|
|
101
|
-
|
|
102
|
-
const mode = zoneId ? { routes: ["*/*"] } : { workers_dev: true };
|
|
103
|
-
const formData = toFormData(worker);
|
|
104
|
-
formData.set("wrangler-session-config", JSON.stringify(mode));
|
|
105
|
-
|
|
106
|
-
const { preview_token: token } = await cfetch<{ preview_token: string }>(
|
|
107
|
-
url,
|
|
108
|
-
{
|
|
109
|
-
method: "POST",
|
|
110
|
-
// @ts-expect-error TODO: fix this
|
|
111
|
-
body: formData,
|
|
112
|
-
headers: {
|
|
113
|
-
"cf-preview-upload-config-token": value,
|
|
114
|
-
},
|
|
115
|
-
}
|
|
116
|
-
);
|
|
117
|
-
return {
|
|
118
|
-
value: token,
|
|
119
|
-
host,
|
|
120
|
-
inspectorUrl,
|
|
121
|
-
prewarmUrl,
|
|
122
|
-
};
|
|
123
|
-
}
|
package/src/api/worker.ts
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import type { CfPreviewToken } from "./preview";
|
|
2
|
-
import { previewToken } from "./preview";
|
|
3
|
-
import fetch from "node-fetch";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A Cloudflare account.
|
|
7
|
-
*/
|
|
8
|
-
export interface CfAccount {
|
|
9
|
-
/**
|
|
10
|
-
* An API token.
|
|
11
|
-
*
|
|
12
|
-
* @link https://api.cloudflare.com/#user-api-tokens-properties
|
|
13
|
-
*/
|
|
14
|
-
apiToken: string;
|
|
15
|
-
/**
|
|
16
|
-
* An account ID.
|
|
17
|
-
*/
|
|
18
|
-
accountId: string;
|
|
19
|
-
/**
|
|
20
|
-
* A zone ID, only required if not using `workers.dev`.
|
|
21
|
-
*/
|
|
22
|
-
zoneId?: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* A module type.
|
|
27
|
-
*/
|
|
28
|
-
export type CfModuleType =
|
|
29
|
-
| "esm"
|
|
30
|
-
| "commonjs"
|
|
31
|
-
| "compiled-wasm"
|
|
32
|
-
| "text"
|
|
33
|
-
| "buffer";
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* An imported module.
|
|
37
|
-
*/
|
|
38
|
-
export interface CfModule {
|
|
39
|
-
/**
|
|
40
|
-
* The module name.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* './src/index.js'
|
|
44
|
-
*/
|
|
45
|
-
name: string;
|
|
46
|
-
/**
|
|
47
|
-
* The module content, usually JavaScript or WASM code.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* export default {
|
|
51
|
-
* async fetch(request) {
|
|
52
|
-
* return new Response('Ok')
|
|
53
|
-
* }
|
|
54
|
-
* }
|
|
55
|
-
*/
|
|
56
|
-
content: string | BufferSource;
|
|
57
|
-
/**
|
|
58
|
-
* The module type.
|
|
59
|
-
*
|
|
60
|
-
* If absent, will default to the main module's type.
|
|
61
|
-
*/
|
|
62
|
-
type?: CfModuleType;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* A KV namespace.
|
|
67
|
-
*/
|
|
68
|
-
export interface CfKvNamespace {
|
|
69
|
-
/**
|
|
70
|
-
* The namespace ID.
|
|
71
|
-
*/
|
|
72
|
-
namespaceId: string;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface CfDurableObject {
|
|
76
|
-
class_name: string;
|
|
77
|
-
script_name?: string;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
interface CfDOMigrations {
|
|
81
|
-
old_tag?: string;
|
|
82
|
-
new_tag: string;
|
|
83
|
-
steps: {
|
|
84
|
-
new_classes?: string[];
|
|
85
|
-
renamed_classes?: string[];
|
|
86
|
-
deleted_classes?: string[];
|
|
87
|
-
}[];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* A `WebCrypto` key.
|
|
92
|
-
*
|
|
93
|
-
* @link https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
|
|
94
|
-
*/
|
|
95
|
-
export interface CfCryptoKey {
|
|
96
|
-
/**
|
|
97
|
-
* The format.
|
|
98
|
-
*/
|
|
99
|
-
format: string;
|
|
100
|
-
/**
|
|
101
|
-
* The algorithm.
|
|
102
|
-
*/
|
|
103
|
-
algorithm: string;
|
|
104
|
-
/**
|
|
105
|
-
* The usages.
|
|
106
|
-
*/
|
|
107
|
-
usages: string[];
|
|
108
|
-
/**
|
|
109
|
-
* The data.
|
|
110
|
-
*/
|
|
111
|
-
data: BufferSource | JsonWebKey;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* A variable (aka. environment variable).
|
|
116
|
-
*/
|
|
117
|
-
export type CfVariable = string | CfKvNamespace | CfCryptoKey | CfDurableObject;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Options for creating a `CfWorker`.
|
|
121
|
-
*/
|
|
122
|
-
export interface CfWorkerInit {
|
|
123
|
-
/**
|
|
124
|
-
* The name of the worker.
|
|
125
|
-
*/
|
|
126
|
-
name: string;
|
|
127
|
-
/**
|
|
128
|
-
* The entrypoint module.
|
|
129
|
-
*/
|
|
130
|
-
main: CfModule;
|
|
131
|
-
/**
|
|
132
|
-
* The list of additional modules.
|
|
133
|
-
*/
|
|
134
|
-
modules: void | CfModule[];
|
|
135
|
-
/**
|
|
136
|
-
* The map of names to variables. (aka. environment variables)
|
|
137
|
-
*/
|
|
138
|
-
variables?: { [name: string]: CfVariable };
|
|
139
|
-
migrations: void | CfDOMigrations;
|
|
140
|
-
compatibility_date: string | void;
|
|
141
|
-
compatibility_flags: void | string[];
|
|
142
|
-
usage_model: void | "bundled" | "unbound";
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* A stub to create a Cloudflare Worker preview.
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* const {value, host} = await createWorker(init, acct);
|
|
150
|
-
*/
|
|
151
|
-
export async function createWorker(
|
|
152
|
-
init: CfWorkerInit,
|
|
153
|
-
account: CfAccount
|
|
154
|
-
): Promise<CfPreviewToken> {
|
|
155
|
-
const token = await previewToken(account, init);
|
|
156
|
-
const response = await fetch(token.prewarmUrl.href, { method: "POST" });
|
|
157
|
-
if (!response.ok) {
|
|
158
|
-
// console.error("worker failed to prewarm: ", response.statusText);
|
|
159
|
-
}
|
|
160
|
-
return token;
|
|
161
|
-
}
|
package/src/cfetch.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import fetch from "node-fetch";
|
|
2
|
-
import type { RequestInit } from "node-fetch";
|
|
3
|
-
import { getAPIToken } from "./user";
|
|
4
|
-
import { loginOrRefreshIfRequired } from "./user";
|
|
5
|
-
|
|
6
|
-
export const CF_API_BASE_URL =
|
|
7
|
-
process.env.CF_API_BASE_URL || "https://api.cloudflare.com/client/v4";
|
|
8
|
-
|
|
9
|
-
export default async function fetchWithAuthAndLoginIfRequired<ResponseType>(
|
|
10
|
-
resource: string,
|
|
11
|
-
init: RequestInit = {}
|
|
12
|
-
): Promise<ResponseType> {
|
|
13
|
-
const loggedIn = await loginOrRefreshIfRequired();
|
|
14
|
-
if (!loggedIn) {
|
|
15
|
-
throw new Error("Not logged in");
|
|
16
|
-
}
|
|
17
|
-
const apiToken = getAPIToken();
|
|
18
|
-
if (!apiToken) {
|
|
19
|
-
throw new Error("No API token found");
|
|
20
|
-
}
|
|
21
|
-
// @ts-expect-error Authorization isn't non HeadersInit, annoyingly
|
|
22
|
-
if (init.headers?.Authorization) {
|
|
23
|
-
throw new Error(
|
|
24
|
-
"fetchWithAuthAndLoginIfRequired will not add an authorisation header a request that already specifies it"
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
// should I bother with response code?
|
|
28
|
-
// maybe I can just throw the json error?
|
|
29
|
-
|
|
30
|
-
const response = await fetch(`${CF_API_BASE_URL}${resource}`, {
|
|
31
|
-
method: "GET",
|
|
32
|
-
...init,
|
|
33
|
-
headers: {
|
|
34
|
-
...(init.headers || {}),
|
|
35
|
-
Authorization: `Bearer ${apiToken}`,
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
const text = await response.text();
|
|
39
|
-
let json;
|
|
40
|
-
try {
|
|
41
|
-
json = JSON.parse(text);
|
|
42
|
-
} catch (parseError) {
|
|
43
|
-
// hate this edge case
|
|
44
|
-
// the only api call I know that doesn't return json is kv:key get <key>
|
|
45
|
-
|
|
46
|
-
// maybe it's a plain response
|
|
47
|
-
if (response.ok) {
|
|
48
|
-
// @ts-expect-error bleh
|
|
49
|
-
return text;
|
|
50
|
-
} else {
|
|
51
|
-
// UGH.
|
|
52
|
-
throw new Error(`${response.status}: ${response.statusText}`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (json.success) {
|
|
57
|
-
return json.result;
|
|
58
|
-
} else {
|
|
59
|
-
const errorDesc = json.errors?.[0];
|
|
60
|
-
if (errorDesc) {
|
|
61
|
-
// TODO: map .message to real human readable strings
|
|
62
|
-
const error = new Error(`${errorDesc.code}: ${errorDesc.message}`);
|
|
63
|
-
// @ts-expect-error hacksss
|
|
64
|
-
error.code = errorDesc.code;
|
|
65
|
-
throw error;
|
|
66
|
-
} else {
|
|
67
|
-
// This should almost never happen.
|
|
68
|
-
// ... which means it'll probably happen, lol.
|
|
69
|
-
throw new Error(`${response.status}: ${response.statusText}`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
package/src/config.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
// we're going to manually write both the type definition AND
|
|
2
|
-
// the validator for the config, so that we can give better error messages
|
|
3
|
-
|
|
4
|
-
type DOMigration = {
|
|
5
|
-
tag: string;
|
|
6
|
-
new_classes?: string[];
|
|
7
|
-
renamed_classes?: string[];
|
|
8
|
-
deleted_classes?: string[];
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
type Project = "webpack" | "javascript" | "rust";
|
|
12
|
-
|
|
13
|
-
type Site = {
|
|
14
|
-
// inherited
|
|
15
|
-
bucket: string;
|
|
16
|
-
"entry-point": string;
|
|
17
|
-
include?: string[];
|
|
18
|
-
exclude?: string[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
type Dev = {
|
|
22
|
-
ip?: string;
|
|
23
|
-
port?: number;
|
|
24
|
-
local_protocol?: string;
|
|
25
|
-
upstream_protocol?: string;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
type Vars = { [key: string]: string };
|
|
29
|
-
|
|
30
|
-
type Cron = string; // TODO: we should be able to parse a cron pattern with ts
|
|
31
|
-
|
|
32
|
-
type KVNamespace = {
|
|
33
|
-
binding?: string;
|
|
34
|
-
preview_id: string;
|
|
35
|
-
id: string;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
type DurableObject = {
|
|
39
|
-
name: string;
|
|
40
|
-
class_name: string;
|
|
41
|
-
script_name?: string;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
type Build = {
|
|
45
|
-
command?: string;
|
|
46
|
-
cwd?: string;
|
|
47
|
-
watch_dir?: string;
|
|
48
|
-
} & (
|
|
49
|
-
| {
|
|
50
|
-
upload?: {
|
|
51
|
-
format: "service-worker";
|
|
52
|
-
main: string;
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
| {
|
|
56
|
-
upload?: {
|
|
57
|
-
format: "modules";
|
|
58
|
-
dir?: string;
|
|
59
|
-
main?: string;
|
|
60
|
-
rules?: {
|
|
61
|
-
type: "ESModule" | "CommonJS" | "Text" | "Data" | "CompiledWasm";
|
|
62
|
-
globs: string[]; // can we use typescript for these patterns?
|
|
63
|
-
fallthrough?: boolean;
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
type UsageModel = "bundled" | "unbound";
|
|
70
|
-
|
|
71
|
-
type Env = {
|
|
72
|
-
name?: string; // inherited
|
|
73
|
-
account_id?: string; // inherited
|
|
74
|
-
workers_dev?: boolean; // inherited
|
|
75
|
-
compatibility_date?: string; // inherited
|
|
76
|
-
compatibility_flags?: string[]; // inherited
|
|
77
|
-
zone_id?: string; // inherited
|
|
78
|
-
routes?: string[]; // inherited
|
|
79
|
-
route?: string; // inherited
|
|
80
|
-
webpack_config?: string; // inherited
|
|
81
|
-
site?: Site;
|
|
82
|
-
jsx_factory?: string; // inherited
|
|
83
|
-
jsx_fragment?: string; // inherited
|
|
84
|
-
polyfill_node?: boolean; //inherited
|
|
85
|
-
// we should use typescript to parse cron patterns
|
|
86
|
-
triggers?: { crons: Cron[] }; // inherited
|
|
87
|
-
vars?: Vars;
|
|
88
|
-
durable_objects?: { bindings: DurableObject[] };
|
|
89
|
-
kv_namespaces?: KVNamespace[];
|
|
90
|
-
usage_model?: UsageModel; // inherited
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export type Config = {
|
|
94
|
-
name?: string; // inherited
|
|
95
|
-
account_id?: string; // inherited
|
|
96
|
-
// @deprecated Don't use this
|
|
97
|
-
type?: Project; // top level
|
|
98
|
-
compatibility_date?: string; // inherited
|
|
99
|
-
compatibility_flags?: string[]; // inherited
|
|
100
|
-
// -- there's some mutually exclusive logic for this next block,
|
|
101
|
-
// but I didn't bother for now
|
|
102
|
-
workers_dev?: boolean; // inherited
|
|
103
|
-
zone_id?: string; // inherited
|
|
104
|
-
routes?: string[]; // inherited
|
|
105
|
-
route?: string; // inherited
|
|
106
|
-
// -- end mutually exclusive stuff
|
|
107
|
-
// @deprecated Don't use this
|
|
108
|
-
webpack_config?: string; // inherited
|
|
109
|
-
jsx_factory?: string; // inherited
|
|
110
|
-
jsx_fragment?: string; // inherited
|
|
111
|
-
polyfill_node?: boolean; //inherited
|
|
112
|
-
vars?: Vars;
|
|
113
|
-
migrations?: DOMigration[];
|
|
114
|
-
durable_objects?: { bindings: DurableObject[] };
|
|
115
|
-
kv_namespaces?: KVNamespace[];
|
|
116
|
-
site?: Site; // inherited
|
|
117
|
-
// we should use typescript to parse cron patterns
|
|
118
|
-
triggers?: { crons: Cron[] }; // inherited
|
|
119
|
-
dev?: Dev;
|
|
120
|
-
usage_model?: UsageModel; // inherited
|
|
121
|
-
// top level
|
|
122
|
-
build?: Build;
|
|
123
|
-
env?: { [envName: string]: void | Env };
|
|
124
|
-
};
|
package/src/kv.tsx
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import type { Config } from "./config";
|
|
2
|
-
import cfetch from "./cfetch";
|
|
3
|
-
import qs from "node:querystring";
|
|
4
|
-
|
|
5
|
-
type KvArgs = {
|
|
6
|
-
binding?: string;
|
|
7
|
-
"namespace-id"?: string;
|
|
8
|
-
env?: string;
|
|
9
|
-
preview?: boolean;
|
|
10
|
-
config?: Config;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export async function listNamespaces(accountId: string) {
|
|
14
|
-
let page = 1,
|
|
15
|
-
done = false,
|
|
16
|
-
results = [];
|
|
17
|
-
while (!(done || results.length % 100 !== 0)) {
|
|
18
|
-
const json = await cfetch<
|
|
19
|
-
{ id: string; title: string; supports_url_encoding: boolean }[]
|
|
20
|
-
>(
|
|
21
|
-
`/accounts/${accountId}/storage/kv/namespaces?per_page=100&order=title&direction=asc&page=${page}`
|
|
22
|
-
);
|
|
23
|
-
page++;
|
|
24
|
-
results = [...results, ...json];
|
|
25
|
-
if (json.length === 0) {
|
|
26
|
-
done = true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return results;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export async function listNamespaceKeys(
|
|
33
|
-
accountId: string,
|
|
34
|
-
namespaceId: string,
|
|
35
|
-
prefix?: string,
|
|
36
|
-
limit?: number
|
|
37
|
-
) {
|
|
38
|
-
// TODO: this doesn't appear to do pagination
|
|
39
|
-
return await cfetch<
|
|
40
|
-
{ name: string; expiration: number; metadata: { [key: string]: unknown } }[]
|
|
41
|
-
>(
|
|
42
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys?${qs.stringify(
|
|
43
|
-
{ prefix, limit }
|
|
44
|
-
)}`
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function putKeyValue(
|
|
49
|
-
accountId: string,
|
|
50
|
-
namespaceId: string,
|
|
51
|
-
key: string,
|
|
52
|
-
value: string,
|
|
53
|
-
args?: { expiration?: number; expiration_ttl?: number }
|
|
54
|
-
) {
|
|
55
|
-
return await cfetch(
|
|
56
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}?${
|
|
57
|
-
args
|
|
58
|
-
? qs.stringify({
|
|
59
|
-
expiration: args.expiration,
|
|
60
|
-
expiration_ttl: args.expiration_ttl,
|
|
61
|
-
})
|
|
62
|
-
: ""
|
|
63
|
-
}`,
|
|
64
|
-
{ method: "PUT", body: value }
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export async function putBulkKeyValue(
|
|
69
|
-
accountId: string,
|
|
70
|
-
namespaceId: string,
|
|
71
|
-
keyvalueStr: string
|
|
72
|
-
) {
|
|
73
|
-
return await cfetch(
|
|
74
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
75
|
-
{
|
|
76
|
-
method: "PUT",
|
|
77
|
-
body: keyvalueStr,
|
|
78
|
-
headers: { "Content-Type": "application/json" },
|
|
79
|
-
}
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export async function deleteBulkKeyValue(
|
|
84
|
-
accountId: string,
|
|
85
|
-
namespaceId: string,
|
|
86
|
-
keyStr: string
|
|
87
|
-
) {
|
|
88
|
-
return await cfetch(
|
|
89
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
90
|
-
{
|
|
91
|
-
method: "DELETE",
|
|
92
|
-
body: keyStr,
|
|
93
|
-
headers: { "Content-Type": "application/json" },
|
|
94
|
-
}
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function getNamespaceId({
|
|
99
|
-
preview,
|
|
100
|
-
binding,
|
|
101
|
-
config,
|
|
102
|
-
"namespace-id": namespaceId,
|
|
103
|
-
env,
|
|
104
|
-
}: KvArgs): string {
|
|
105
|
-
// nice
|
|
106
|
-
if (namespaceId) {
|
|
107
|
-
return namespaceId;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// begin pre-flight checks
|
|
111
|
-
|
|
112
|
-
// `--binding` is only valid if there's a wrangler configuration file.
|
|
113
|
-
if (binding && !config) {
|
|
114
|
-
throw new Error("--binding specified, but no config file was found.");
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// there's no config. abort here
|
|
118
|
-
if (!config) {
|
|
119
|
-
throw new Error(
|
|
120
|
-
"Failed to find a config file.\n" +
|
|
121
|
-
"Either use --namespace to upload directly or create a configuration file with a binding."
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// they want to use an environment, actually
|
|
126
|
-
if (env) {
|
|
127
|
-
if (!config.env || !config.env[env]) {
|
|
128
|
-
throw new Error(
|
|
129
|
-
`Failed to find environment "${env}" in configuration file!`
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// TODO: either a bespoke arg type for this function to avoid undefineds or a EnvOrConfig type
|
|
134
|
-
return getNamespaceId({
|
|
135
|
-
binding,
|
|
136
|
-
"namespace-id": namespaceId,
|
|
137
|
-
env: undefined,
|
|
138
|
-
preview,
|
|
139
|
-
config: {
|
|
140
|
-
env: undefined,
|
|
141
|
-
build: undefined,
|
|
142
|
-
name: undefined,
|
|
143
|
-
account_id: undefined,
|
|
144
|
-
...config.env[env],
|
|
145
|
-
},
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// there's no KV namespaces
|
|
150
|
-
if (!config.kv_namespaces || config.kv_namespaces.length === 0) {
|
|
151
|
-
throw new Error(
|
|
152
|
-
"No KV Namespace to upload to! Either use --namespace to upload directly or add a KV namespace to your wrangler config file."
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const namespace = config.kv_namespaces.find(
|
|
157
|
-
(namespace) => namespace.binding === binding
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
// we couldn't find a namespace with that binding
|
|
161
|
-
if (!namespace) {
|
|
162
|
-
throw new Error(`No KV Namespaces found with binding ${binding}!`);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// end pre-flight checks
|
|
166
|
-
|
|
167
|
-
// we're in preview mode, `--preview true` or `--preivew` was passed
|
|
168
|
-
if (preview && namespace.preview_id) {
|
|
169
|
-
namespaceId = namespace.preview_id;
|
|
170
|
-
} else {
|
|
171
|
-
throw new Error(
|
|
172
|
-
`No preview ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// either `--preview false`, or preview wasn't passed
|
|
177
|
-
// TODO: should we care? or should we just treat false and undefined the same
|
|
178
|
-
const previewIsDefined = typeof preview !== "undefined";
|
|
179
|
-
|
|
180
|
-
// --preview false was passed
|
|
181
|
-
if (previewIsDefined && namespace.id) {
|
|
182
|
-
namespaceId = namespace.id;
|
|
183
|
-
} else {
|
|
184
|
-
throw new Error(
|
|
185
|
-
`No namespace ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// `--preview` wasn't passed
|
|
190
|
-
const bindingHasOnlyOneId =
|
|
191
|
-
(namespace.id && !namespace.preview_id) ||
|
|
192
|
-
(!namespace.id && namespace.preview_id);
|
|
193
|
-
if (bindingHasOnlyOneId) {
|
|
194
|
-
namespaceId = namespace.id || namespace.preview_id;
|
|
195
|
-
} else {
|
|
196
|
-
throw new Error(
|
|
197
|
-
`${binding} has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.`
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// shouldn't happen. we should be able to prove this with strong typing.
|
|
202
|
-
// TODO: when we add strongly typed commands, rewrite these checks so they're exhaustive
|
|
203
|
-
if (!namespaceId) {
|
|
204
|
-
throw Error(
|
|
205
|
-
"Something went wrong trying to determine which namespace to upload to.\n" +
|
|
206
|
-
"Please create a github issue with the command you just ran along with your wrangler configuration."
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return namespaceId;
|
|
211
|
-
}
|