wrangler 0.0.0-e2a26a0 → 0.0.0-e3e3243
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/README.md +1 -1
- package/bin/wrangler.js +10 -1
- package/miniflare-dist/index.mjs +79 -0
- package/miniflare-dist/index.mjs.map +7 -0
- package/package.json +24 -12
- package/pages/functions/filepath-routing.test.ts +212 -36
- package/pages/functions/filepath-routing.ts +69 -119
- package/pages/functions/routes.ts +8 -17
- package/pages/functions/template-worker.ts +3 -9
- package/src/__tests__/configuration.test.ts +2820 -0
- package/src/__tests__/dev.test.tsx +535 -65
- package/src/__tests__/guess-worker-format.test.ts +68 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +26 -8
- package/src/__tests__/helpers/mock-bin.ts +4 -1
- package/src/__tests__/helpers/mock-cfetch.ts +5 -5
- package/src/__tests__/helpers/mock-console.ts +21 -7
- package/src/__tests__/helpers/mock-kv.ts +9 -16
- package/src/__tests__/helpers/mock-stdin.ts +103 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +3 -0
- package/src/__tests__/helpers/run-wrangler.ts +5 -3
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +127 -0
- package/src/__tests__/index.test.ts +467 -27
- package/src/__tests__/jest.setup.ts +37 -5
- package/src/__tests__/kv.test.ts +442 -201
- package/src/__tests__/logout.test.ts +15 -13
- package/src/__tests__/package-manager.test.ts +2 -2
- package/src/__tests__/pages.test.ts +82 -0
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +2717 -430
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +301 -86
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +562 -0
- package/src/__tests__/whoami.test.tsx +21 -7
- package/src/bundle.ts +155 -0
- package/src/cfetch/index.ts +8 -6
- package/src/cfetch/internal.ts +60 -22
- package/src/cli.ts +10 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +218 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +371 -0
- package/src/config/index.ts +62 -0
- package/src/config/validation-helpers.ts +554 -0
- package/src/config/validation.ts +1232 -0
- package/src/{api/preview.ts → create-worker-preview.ts} +59 -21
- package/src/create-worker-upload-form.ts +217 -0
- package/src/dev/dev.tsx +392 -0
- package/src/dev/local.tsx +232 -0
- package/src/dev/remote.tsx +203 -0
- package/src/dev/use-esbuild.ts +104 -0
- package/src/entry.ts +240 -0
- package/src/environment-variables.ts +33 -0
- package/src/https-options.ts +122 -0
- package/src/index.tsx +1067 -779
- package/src/inspect.ts +25 -4
- package/src/intl-polyfill.d.ts +139 -0
- package/src/{kv.tsx → kv.ts} +95 -60
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +37 -0
- package/src/module-collection.ts +226 -31
- package/src/open-in-browser.ts +25 -0
- package/src/package-manager.ts +21 -15
- package/src/pages.tsx +35 -20
- package/src/parse.ts +183 -0
- package/src/proxy.ts +115 -45
- package/src/publish.ts +268 -328
- package/src/r2.ts +50 -0
- package/src/reporting.ts +131 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +83 -43
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +62 -0
- package/src/user.tsx +234 -126
- package/src/{api/worker.ts → worker.ts} +44 -36
- package/templates/new-worker.js +1 -1
- package/templates/new-worker.ts +1 -1
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +131201 -114242
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/helpers/mock-user.ts +0 -27
- package/src/api/form_data.ts +0 -134
- package/src/config.ts +0 -641
- package/src/dev.tsx +0 -834
- package/src/tail.tsx +0 -73
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { URL } from "node:url";
|
|
2
2
|
import { fetch } from "undici";
|
|
3
|
-
import { fetchResult } from "
|
|
4
|
-
import {
|
|
5
|
-
import type { CfAccount, CfWorkerInit } from "./worker";
|
|
3
|
+
import { fetchResult } from "./cfetch";
|
|
4
|
+
import { createWorkerUploadForm } from "./create-worker-upload-form";
|
|
5
|
+
import type { CfAccount, CfWorkerContext, CfWorkerInit } from "./worker";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* A preview mode.
|
|
@@ -55,10 +55,13 @@ export interface CfPreviewToken {
|
|
|
55
55
|
/**
|
|
56
56
|
* Generates a preview session token.
|
|
57
57
|
*/
|
|
58
|
-
async function sessionToken(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
async function sessionToken(
|
|
59
|
+
account: CfAccount,
|
|
60
|
+
ctx: CfWorkerContext
|
|
61
|
+
): Promise<CfPreviewToken> {
|
|
62
|
+
const { accountId } = account;
|
|
63
|
+
const initUrl = ctx.zone
|
|
64
|
+
? `/zones/${ctx.zone.id}/workers/edge-preview`
|
|
62
65
|
: `/accounts/${accountId}/workers/subdomain/edge-preview`;
|
|
63
66
|
|
|
64
67
|
const { exchange_url } = await fetchResult<{ exchange_url: string }>(initUrl);
|
|
@@ -90,21 +93,28 @@ function randomId(): string {
|
|
|
90
93
|
/**
|
|
91
94
|
* Creates a preview token.
|
|
92
95
|
*/
|
|
93
|
-
|
|
96
|
+
async function createPreviewToken(
|
|
94
97
|
account: CfAccount,
|
|
95
|
-
worker: CfWorkerInit
|
|
98
|
+
worker: CfWorkerInit,
|
|
99
|
+
ctx: CfWorkerContext
|
|
96
100
|
): Promise<CfPreviewToken> {
|
|
97
|
-
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(
|
|
101
|
+
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(
|
|
102
|
+
account,
|
|
103
|
+
ctx
|
|
104
|
+
);
|
|
98
105
|
|
|
99
|
-
const { accountId
|
|
100
|
-
const scriptId =
|
|
101
|
-
const url =
|
|
106
|
+
const { accountId } = account;
|
|
107
|
+
const scriptId = ctx.zone ? randomId() : worker.name || host.split(".")[0];
|
|
108
|
+
const url =
|
|
109
|
+
ctx.env && !ctx.legacyEnv
|
|
110
|
+
? `/accounts/${accountId}/workers/services/${scriptId}/environments/${ctx.env}/edge-preview`
|
|
111
|
+
: `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
|
|
102
112
|
|
|
103
|
-
const mode: CfPreviewMode =
|
|
104
|
-
? { routes: ["*/*"] }
|
|
113
|
+
const mode: CfPreviewMode = ctx.zone
|
|
114
|
+
? { routes: ["*/*"] } // TODO: should we support routes here? how?
|
|
105
115
|
: { workers_dev: true };
|
|
106
116
|
|
|
107
|
-
const formData =
|
|
117
|
+
const formData = createWorkerUploadForm(worker);
|
|
108
118
|
formData.set("wrangler-session-config", JSON.stringify(mode));
|
|
109
119
|
|
|
110
120
|
const { preview_token } = await fetchResult<{ preview_token: string }>(url, {
|
|
@@ -117,12 +127,40 @@ export async function previewToken(
|
|
|
117
127
|
|
|
118
128
|
return {
|
|
119
129
|
value: preview_token,
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
worker.name
|
|
123
|
-
|
|
124
|
-
|
|
130
|
+
host: ctx.zone
|
|
131
|
+
? ctx.zone.host
|
|
132
|
+
: worker.name
|
|
133
|
+
? `${
|
|
134
|
+
worker.name
|
|
135
|
+
// TODO: this should also probably have the env prefix
|
|
136
|
+
// but it doesn't appear to work yet, instead giving us the
|
|
137
|
+
// "There is nothing here yet" screen
|
|
138
|
+
// ctx.env && !ctx.legacyEnv
|
|
139
|
+
// ? `${ctx.env}.${worker.name}`
|
|
140
|
+
// : worker.name
|
|
141
|
+
}.${host.split(".").slice(1).join(".")}`
|
|
142
|
+
: host,
|
|
143
|
+
|
|
125
144
|
inspectorUrl,
|
|
126
145
|
prewarmUrl,
|
|
127
146
|
};
|
|
128
147
|
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A stub to create a Cloudflare Worker preview.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* const {value, host} = await createWorker(init, acct);
|
|
154
|
+
*/
|
|
155
|
+
export async function createWorkerPreview(
|
|
156
|
+
init: CfWorkerInit,
|
|
157
|
+
account: CfAccount,
|
|
158
|
+
ctx: CfWorkerContext
|
|
159
|
+
): Promise<CfPreviewToken> {
|
|
160
|
+
const token = await createPreviewToken(account, init, ctx);
|
|
161
|
+
const response = await fetch(token.prewarmUrl.href, { method: "POST" });
|
|
162
|
+
if (!response.ok) {
|
|
163
|
+
// console.error("worker failed to prewarm: ", response.statusText);
|
|
164
|
+
}
|
|
165
|
+
return token;
|
|
166
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { FormData, File } from "undici";
|
|
3
|
+
import type {
|
|
4
|
+
CfWorkerInit,
|
|
5
|
+
CfModuleType,
|
|
6
|
+
CfDurableObjectMigrations,
|
|
7
|
+
} from "./worker.js";
|
|
8
|
+
|
|
9
|
+
export function toMimeType(type: CfModuleType): string {
|
|
10
|
+
switch (type) {
|
|
11
|
+
case "esm":
|
|
12
|
+
return "application/javascript+module";
|
|
13
|
+
case "commonjs":
|
|
14
|
+
return "application/javascript";
|
|
15
|
+
case "compiled-wasm":
|
|
16
|
+
return "application/wasm";
|
|
17
|
+
case "buffer":
|
|
18
|
+
return "application/octet-stream";
|
|
19
|
+
case "text":
|
|
20
|
+
return "text/plain";
|
|
21
|
+
default:
|
|
22
|
+
throw new TypeError("Unsupported module: " + type);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface WorkerMetadata {
|
|
27
|
+
/** The name of the entry point module. Only exists when the worker is in the ES module format */
|
|
28
|
+
main_module?: string;
|
|
29
|
+
/** The name of the entry point module. Only exists when the worker is in the Service Worker format */
|
|
30
|
+
body_part?: string;
|
|
31
|
+
compatibility_date?: string;
|
|
32
|
+
compatibility_flags?: string[];
|
|
33
|
+
usage_model?: "bundled" | "unbound";
|
|
34
|
+
migrations?: CfDurableObjectMigrations;
|
|
35
|
+
bindings: (
|
|
36
|
+
| { type: "kv_namespace"; name: string; namespace_id: string }
|
|
37
|
+
| { type: "plain_text"; name: string; text: string }
|
|
38
|
+
| { type: "json"; name: string; json: unknown }
|
|
39
|
+
| { type: "wasm_module"; name: string; part: string }
|
|
40
|
+
| { type: "text_blob"; name: string; part: string }
|
|
41
|
+
| {
|
|
42
|
+
type: "durable_object_namespace";
|
|
43
|
+
name: string;
|
|
44
|
+
class_name: string;
|
|
45
|
+
script_name?: string;
|
|
46
|
+
}
|
|
47
|
+
| { type: "r2_bucket"; name: string; bucket_name: string }
|
|
48
|
+
)[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Creates a `FormData` upload from a `CfWorkerInit`.
|
|
53
|
+
*/
|
|
54
|
+
export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
|
|
55
|
+
const formData = new FormData();
|
|
56
|
+
const {
|
|
57
|
+
main,
|
|
58
|
+
bindings,
|
|
59
|
+
migrations,
|
|
60
|
+
usage_model,
|
|
61
|
+
compatibility_date,
|
|
62
|
+
compatibility_flags,
|
|
63
|
+
} = worker;
|
|
64
|
+
|
|
65
|
+
let { modules } = worker;
|
|
66
|
+
|
|
67
|
+
const metadataBindings: WorkerMetadata["bindings"] = [];
|
|
68
|
+
|
|
69
|
+
bindings.kv_namespaces?.forEach(({ id, binding }) => {
|
|
70
|
+
metadataBindings.push({
|
|
71
|
+
name: binding,
|
|
72
|
+
type: "kv_namespace",
|
|
73
|
+
namespace_id: id,
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
bindings.durable_objects?.bindings.forEach(
|
|
78
|
+
({ name, class_name, script_name }) => {
|
|
79
|
+
metadataBindings.push({
|
|
80
|
+
name,
|
|
81
|
+
type: "durable_object_namespace",
|
|
82
|
+
class_name: class_name,
|
|
83
|
+
...(script_name && { script_name }),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
bindings.r2_buckets?.forEach(({ binding, bucket_name }) => {
|
|
89
|
+
metadataBindings.push({
|
|
90
|
+
name: binding,
|
|
91
|
+
type: "r2_bucket",
|
|
92
|
+
bucket_name,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
Object.entries(bindings.vars || {})?.forEach(([key, value]) => {
|
|
97
|
+
if (typeof value === "string") {
|
|
98
|
+
metadataBindings.push({ name: key, type: "plain_text", text: value });
|
|
99
|
+
} else {
|
|
100
|
+
metadataBindings.push({ name: key, type: "json", json: value });
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
for (const [name, filePath] of Object.entries(bindings.wasm_modules || {})) {
|
|
105
|
+
metadataBindings.push({
|
|
106
|
+
name,
|
|
107
|
+
type: "wasm_module",
|
|
108
|
+
part: name,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
formData.set(
|
|
112
|
+
name,
|
|
113
|
+
new File([readFileSync(filePath)], filePath, {
|
|
114
|
+
type: "application/wasm",
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const [name, filePath] of Object.entries(bindings.text_blobs || {})) {
|
|
120
|
+
metadataBindings.push({
|
|
121
|
+
name,
|
|
122
|
+
type: "text_blob",
|
|
123
|
+
part: name,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
if (name !== "__STATIC_CONTENT_MANIFEST") {
|
|
127
|
+
formData.set(
|
|
128
|
+
name,
|
|
129
|
+
new File([readFileSync(filePath)], filePath, {
|
|
130
|
+
type: "text/plain",
|
|
131
|
+
})
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (main.type === "commonjs") {
|
|
137
|
+
// This is a service-worker format worker.
|
|
138
|
+
for (const module of Object.values([...(modules || [])])) {
|
|
139
|
+
if (module.name === "__STATIC_CONTENT_MANIFEST") {
|
|
140
|
+
// Add the manifest to the form data.
|
|
141
|
+
formData.set(
|
|
142
|
+
module.name,
|
|
143
|
+
new File([module.content], module.name, {
|
|
144
|
+
type: "text/plain",
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
// And then remove it from the modules collection
|
|
148
|
+
modules = modules?.filter((m) => m !== module);
|
|
149
|
+
} else if (module.type === "compiled-wasm" || module.type === "text") {
|
|
150
|
+
// Convert all wasm/text modules into `wasm_module`/`text_blob` bindings.
|
|
151
|
+
// The "name" of the module is a file path. We use it
|
|
152
|
+
// to instead be a "part" of the body, and a reference
|
|
153
|
+
// that we can use inside our source. This identifier has to be a valid
|
|
154
|
+
// JS identifier, so we replace all non alphanumeric characters
|
|
155
|
+
// with an underscore.
|
|
156
|
+
const name = module.name.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
157
|
+
metadataBindings.push({
|
|
158
|
+
name,
|
|
159
|
+
type: module.type === "compiled-wasm" ? "wasm_module" : "text_blob",
|
|
160
|
+
part: name,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Add the module to the form data.
|
|
164
|
+
formData.set(
|
|
165
|
+
name,
|
|
166
|
+
new File([module.content], module.name, {
|
|
167
|
+
type:
|
|
168
|
+
module.type === "compiled-wasm"
|
|
169
|
+
? "application/wasm"
|
|
170
|
+
: "text/plain",
|
|
171
|
+
})
|
|
172
|
+
);
|
|
173
|
+
// And then remove it from the modules collection
|
|
174
|
+
modules = modules?.filter((m) => m !== module);
|
|
175
|
+
} else if (module.type === "buffer") {
|
|
176
|
+
throw new Error(
|
|
177
|
+
'The "buffer" module type is not yet supported in service worker format workers'
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (bindings.unsafe) {
|
|
184
|
+
// @ts-expect-error unsafe bindings don't need to match a specific type here
|
|
185
|
+
metadataBindings.push(...bindings.unsafe);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const metadata: WorkerMetadata = {
|
|
189
|
+
...(main.type !== "commonjs"
|
|
190
|
+
? { main_module: main.name }
|
|
191
|
+
: { body_part: main.name }),
|
|
192
|
+
bindings: metadataBindings,
|
|
193
|
+
...(compatibility_date && { compatibility_date }),
|
|
194
|
+
...(compatibility_flags && { compatibility_flags }),
|
|
195
|
+
...(usage_model && { usage_model }),
|
|
196
|
+
...(migrations && { migrations }),
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
formData.set("metadata", JSON.stringify(metadata));
|
|
200
|
+
|
|
201
|
+
if (main.type === "commonjs" && modules && modules.length > 0) {
|
|
202
|
+
throw new TypeError(
|
|
203
|
+
"More than one module can only be specified when type = 'esm'"
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
for (const module of [main].concat(modules || [])) {
|
|
208
|
+
formData.set(
|
|
209
|
+
module.name,
|
|
210
|
+
new File([module.content], module.name, {
|
|
211
|
+
type: toMimeType(module.type ?? main.type ?? "esm"),
|
|
212
|
+
})
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return formData;
|
|
217
|
+
}
|