wrangler 0.0.0-e14e589 → 0.0.0-e2a26a0
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 +51 -0
- package/bin/wrangler.js +2 -2
- package/package.json +49 -30
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +47 -0
- package/pages/functions/filepath-routing.ts +231 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +160 -0
- package/pages/functions/template-worker.ts +136 -0
- package/src/__tests__/dev.test.tsx +71 -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/mock-account-id.ts +30 -0
- package/src/__tests__/helpers/mock-bin.ts +33 -0
- package/src/__tests__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +56 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +40 -0
- package/src/__tests__/helpers/mock-user.ts +27 -0
- package/src/__tests__/helpers/run-in-tmp.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +14 -0
- package/src/__tests__/index.test.ts +509 -119
- package/src/__tests__/jest.setup.ts +37 -0
- package/src/__tests__/kv.test.ts +1139 -0
- package/src/__tests__/logout.test.ts +50 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/publish.test.ts +1222 -0
- package/src/__tests__/secret.test.ts +210 -0
- package/src/__tests__/whoami.test.tsx +86 -0
- package/src/api/form_data.ts +85 -76
- package/src/api/preview.ts +29 -24
- package/src/api/worker.ts +50 -35
- package/src/cfetch/index.ts +102 -0
- package/src/cfetch/internal.ts +101 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +630 -85
- package/src/dev.tsx +446 -282
- package/src/dialogs.tsx +6 -14
- package/src/index.tsx +773 -426
- package/src/inspect.ts +596 -0
- package/src/kv.tsx +118 -45
- package/src/module-collection.ts +63 -0
- package/src/package-manager.ts +120 -0
- package/src/pages.tsx +917 -211
- package/src/paths.ts +26 -0
- package/src/proxy.ts +370 -0
- package/src/publish.ts +220 -88
- package/src/sites.tsx +180 -55
- package/src/tail.tsx +6 -4
- package/src/user.tsx +77 -87
- package/src/whoami.tsx +69 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
- package/wrangler-dist/cli.js +125330 -557
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/inspect.ts +0 -441
- package/src/cfetch.ts +0 -72
- package/src/util/fetch.ts +0 -74
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
|
|
2
|
+
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
|
|
3
|
+
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
4
|
+
import { mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
|
|
5
|
+
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
6
|
+
import { runWrangler } from "./helpers/run-wrangler";
|
|
7
|
+
|
|
8
|
+
describe("wrangler secret", () => {
|
|
9
|
+
const std = mockConsoleMethods();
|
|
10
|
+
runInTempDir();
|
|
11
|
+
mockAccountId();
|
|
12
|
+
mockApiToken();
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
unsetAllMocks();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("put", () => {
|
|
19
|
+
function mockPutRequest(input: { name: string; text: string }) {
|
|
20
|
+
setMockResponse(
|
|
21
|
+
"/accounts/:accountId/workers/scripts/:scriptName/secrets",
|
|
22
|
+
"PUT",
|
|
23
|
+
([_url, accountId], { body }) => {
|
|
24
|
+
expect(accountId).toEqual("some-account-id");
|
|
25
|
+
const { name, text, type } = JSON.parse(body as string);
|
|
26
|
+
expect(type).toEqual("secret_text");
|
|
27
|
+
expect(name).toEqual(input.name);
|
|
28
|
+
expect(text).toEqual(input.text);
|
|
29
|
+
|
|
30
|
+
return { name, type };
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
it("should create a secret", async () => {
|
|
36
|
+
mockPrompt({
|
|
37
|
+
text: "Enter a secret value:",
|
|
38
|
+
type: "password",
|
|
39
|
+
result: "the-secret",
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
mockPutRequest({ name: "the-secret-name", text: "the-secret" });
|
|
43
|
+
await runWrangler("secret put the-key --name script-name");
|
|
44
|
+
|
|
45
|
+
expect(std.out).toMatchInlineSnapshot(`
|
|
46
|
+
"🌀 Creating the secret for script script-name
|
|
47
|
+
✨ Success! Uploaded secret the-key"
|
|
48
|
+
`);
|
|
49
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should error without a script name", async () => {
|
|
53
|
+
let error: Error | undefined;
|
|
54
|
+
try {
|
|
55
|
+
await runWrangler("secret put the-key");
|
|
56
|
+
} catch (e) {
|
|
57
|
+
error = e;
|
|
58
|
+
}
|
|
59
|
+
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
60
|
+
expect(std.err).toMatchInlineSnapshot(`
|
|
61
|
+
"Missing script name
|
|
62
|
+
|
|
63
|
+
[32m%s[0m
|
|
64
|
+
If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
|
|
65
|
+
`);
|
|
66
|
+
expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("warns about being a no-op in local mode", async () => {
|
|
70
|
+
mockPrompt({
|
|
71
|
+
text: "Enter a secret value:",
|
|
72
|
+
type: "password",
|
|
73
|
+
result: "the-secret",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
mockPutRequest({ name: "the-secret-name", text: "the-secret" });
|
|
77
|
+
await runWrangler("secret put the-key --name script-name --local");
|
|
78
|
+
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
79
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
80
|
+
expect(std.warn).toMatchInlineSnapshot(
|
|
81
|
+
`"\`wrangler secret put\` is a no-op in --local mode"`
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("delete", () => {
|
|
87
|
+
function mockDeleteRequest(input: {
|
|
88
|
+
scriptName: string;
|
|
89
|
+
secretName: string;
|
|
90
|
+
}) {
|
|
91
|
+
setMockResponse(
|
|
92
|
+
"/accounts/:accountId/workers/scripts/:scriptName/secrets/:secretName",
|
|
93
|
+
"DELETE",
|
|
94
|
+
([_url, accountId, scriptName, secretName]) => {
|
|
95
|
+
expect(accountId).toEqual("some-account-id");
|
|
96
|
+
expect(scriptName).toEqual(input.scriptName);
|
|
97
|
+
expect(secretName).toEqual(input.secretName);
|
|
98
|
+
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
it("should delete a secret", async () => {
|
|
104
|
+
mockDeleteRequest({ scriptName: "script-name", secretName: "the-key" });
|
|
105
|
+
mockConfirm({
|
|
106
|
+
text: "Are you sure you want to permanently delete the variable the-key on the script script-name?",
|
|
107
|
+
result: true,
|
|
108
|
+
});
|
|
109
|
+
await runWrangler("secret delete the-key --name script-name");
|
|
110
|
+
expect(std.out).toMatchInlineSnapshot(`
|
|
111
|
+
"🌀 Deleting the secret the-key on script script-name.
|
|
112
|
+
✨ Success! Deleted secret the-key"
|
|
113
|
+
`);
|
|
114
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("should error without a script name", async () => {
|
|
118
|
+
let error: Error | undefined;
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
await runWrangler("secret delete the-key");
|
|
122
|
+
} catch (e) {
|
|
123
|
+
error = e;
|
|
124
|
+
}
|
|
125
|
+
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
126
|
+
expect(std.err).toMatchInlineSnapshot(`
|
|
127
|
+
"Missing script name
|
|
128
|
+
|
|
129
|
+
[32m%s[0m
|
|
130
|
+
If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
|
|
131
|
+
`);
|
|
132
|
+
expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("warns about being a no-op in local mode", async () => {
|
|
136
|
+
mockConfirm({
|
|
137
|
+
text: "Are you sure you want to permanently delete the variable the-key on the script script-name?",
|
|
138
|
+
result: true,
|
|
139
|
+
});
|
|
140
|
+
await runWrangler("secret delete the-key --name script-name --local");
|
|
141
|
+
expect(std.out).toMatchInlineSnapshot(
|
|
142
|
+
`"🌀 Deleting the secret the-key on script script-name."`
|
|
143
|
+
);
|
|
144
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
145
|
+
expect(std.warn).toMatchInlineSnapshot(
|
|
146
|
+
`"\`wrangler secret delete\` is a no-op in --local mode"`
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe("list", () => {
|
|
152
|
+
function mockListRequest(input: { scriptName: string }) {
|
|
153
|
+
setMockResponse(
|
|
154
|
+
"/accounts/:accountId/workers/scripts/:scriptName/secrets",
|
|
155
|
+
"GET",
|
|
156
|
+
([_url, accountId, scriptName]) => {
|
|
157
|
+
expect(accountId).toEqual("some-account-id");
|
|
158
|
+
expect(scriptName).toEqual(input.scriptName);
|
|
159
|
+
|
|
160
|
+
return [
|
|
161
|
+
{
|
|
162
|
+
name: "the-secret-name",
|
|
163
|
+
type: "secret_text",
|
|
164
|
+
},
|
|
165
|
+
];
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
it("should list secrets", async () => {
|
|
171
|
+
mockListRequest({ scriptName: "script-name" });
|
|
172
|
+
await runWrangler("secret list --name script-name");
|
|
173
|
+
expect(std.out).toMatchInlineSnapshot(`
|
|
174
|
+
"[
|
|
175
|
+
{
|
|
176
|
+
\\"name\\": \\"the-secret-name\\",
|
|
177
|
+
\\"type\\": \\"secret_text\\"
|
|
178
|
+
}
|
|
179
|
+
]"
|
|
180
|
+
`);
|
|
181
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("should error without a script name", async () => {
|
|
185
|
+
let error: Error | undefined;
|
|
186
|
+
try {
|
|
187
|
+
await runWrangler("secret list");
|
|
188
|
+
} catch (e) {
|
|
189
|
+
error = e;
|
|
190
|
+
}
|
|
191
|
+
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
192
|
+
expect(std.err).toMatchInlineSnapshot(`
|
|
193
|
+
"Missing script name
|
|
194
|
+
|
|
195
|
+
[32m%s[0m
|
|
196
|
+
If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
|
|
197
|
+
`);
|
|
198
|
+
expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("warns about being a no-op in local mode", async () => {
|
|
202
|
+
await runWrangler("secret list --name script-name --local");
|
|
203
|
+
expect(std.out).toMatchInlineSnapshot(`""`);
|
|
204
|
+
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
205
|
+
expect(std.warn).toMatchInlineSnapshot(
|
|
206
|
+
`"\`wrangler secret list\` is a no-op in --local mode"`
|
|
207
|
+
);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { render } from "ink-testing-library";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { initialise } from "../user";
|
|
4
|
+
import { getUserInfo, WhoAmI } from "../whoami";
|
|
5
|
+
import { setMockResponse } from "./helpers/mock-cfetch";
|
|
6
|
+
import { writeUserConfig } from "./helpers/mock-user";
|
|
7
|
+
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
8
|
+
import type { UserInfo } from "../whoami";
|
|
9
|
+
|
|
10
|
+
describe("getUserInfo()", () => {
|
|
11
|
+
runInTempDir({ homedir: "./home" });
|
|
12
|
+
|
|
13
|
+
it("should return undefined if there is no config file", async () => {
|
|
14
|
+
await initialise();
|
|
15
|
+
const userInfo = await getUserInfo();
|
|
16
|
+
expect(userInfo).toBeUndefined();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should return undefined if there is an empty config file", async () => {
|
|
20
|
+
writeUserConfig();
|
|
21
|
+
await initialise();
|
|
22
|
+
const userInfo = await getUserInfo();
|
|
23
|
+
expect(userInfo).toBeUndefined();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return the user's email and accounts if authenticated via config token", async () => {
|
|
27
|
+
writeUserConfig("some-oauth-token");
|
|
28
|
+
setMockResponse("/user", () => {
|
|
29
|
+
return { email: "user@example.com" };
|
|
30
|
+
});
|
|
31
|
+
setMockResponse("/accounts", () => {
|
|
32
|
+
return [
|
|
33
|
+
{ name: "Account One", id: "account-1" },
|
|
34
|
+
{ name: "Account Two", id: "account-2" },
|
|
35
|
+
{ name: "Account Three", id: "account-3" },
|
|
36
|
+
];
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
await initialise();
|
|
40
|
+
const userInfo = await getUserInfo();
|
|
41
|
+
|
|
42
|
+
expect(userInfo).toEqual({
|
|
43
|
+
authType: "OAuth",
|
|
44
|
+
apiToken: "some-oauth-token",
|
|
45
|
+
email: "user@example.com",
|
|
46
|
+
accounts: [
|
|
47
|
+
{ name: "Account One", id: "account-1" },
|
|
48
|
+
{ name: "Account Two", id: "account-2" },
|
|
49
|
+
{ name: "Account Three", id: "account-3" },
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("WhoAmI component", () => {
|
|
56
|
+
it("should return undefined if there is no user", async () => {
|
|
57
|
+
const { lastFrame } = render(<WhoAmI user={undefined}></WhoAmI>);
|
|
58
|
+
|
|
59
|
+
expect(lastFrame()).toMatchInlineSnapshot(
|
|
60
|
+
`"You are not authenticated. Please run \`wrangler login\`."`
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should display the user's email and accounts", async () => {
|
|
65
|
+
const user: UserInfo = {
|
|
66
|
+
authType: "OAuth",
|
|
67
|
+
apiToken: "some-oauth-token",
|
|
68
|
+
email: "user@example.com",
|
|
69
|
+
accounts: [
|
|
70
|
+
{ name: "Account One", id: "account-1" },
|
|
71
|
+
{ name: "Account Two", id: "account-2" },
|
|
72
|
+
{ name: "Account Three", id: "account-3" },
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const { lastFrame } = render(<WhoAmI user={user}></WhoAmI>);
|
|
77
|
+
|
|
78
|
+
expect(lastFrame()).toContain(
|
|
79
|
+
"You are logged in with an OAuth Token, associated with the email 'user@example.com'!"
|
|
80
|
+
);
|
|
81
|
+
expect(lastFrame()).toMatch(/Account Name .+ Account ID/);
|
|
82
|
+
expect(lastFrame()).toMatch(/Account One .+ account-1/);
|
|
83
|
+
expect(lastFrame()).toMatch(/Account Two .+ account-2/);
|
|
84
|
+
expect(lastFrame()).toMatch(/Account Three .+ account-3/);
|
|
85
|
+
});
|
|
86
|
+
});
|
package/src/api/form_data.ts
CHANGED
|
@@ -1,59 +1,10 @@
|
|
|
1
|
+
import { FormData, File } from "undici";
|
|
1
2
|
import type {
|
|
2
3
|
CfWorkerInit,
|
|
3
4
|
CfModuleType,
|
|
4
|
-
CfVariable,
|
|
5
5
|
CfModule,
|
|
6
|
-
|
|
7
|
-
CfCryptoKey,
|
|
6
|
+
CfDurableObjectMigrations,
|
|
8
7
|
} from "./worker.js";
|
|
9
|
-
import { FormData, Blob } from "formdata-node";
|
|
10
|
-
|
|
11
|
-
// Credit: https://stackoverflow.com/a/9458996
|
|
12
|
-
function toBase64(source: BufferSource): string {
|
|
13
|
-
let result = "";
|
|
14
|
-
const buffer = source instanceof ArrayBuffer ? source : source.buffer;
|
|
15
|
-
const bytes = new Uint8Array(buffer);
|
|
16
|
-
for (let i = 0; i < bytes.byteLength; i++) {
|
|
17
|
-
result += String.fromCharCode(bytes[i]);
|
|
18
|
-
}
|
|
19
|
-
return btoa(result);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function toBinding(
|
|
23
|
-
name: string,
|
|
24
|
-
variable: CfVariable
|
|
25
|
-
): Record<string, unknown> {
|
|
26
|
-
if (typeof variable === "string") {
|
|
27
|
-
return { name, type: "plain_text", text: variable };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const { namespaceId } = variable as CfKvNamespace;
|
|
31
|
-
if (namespaceId) {
|
|
32
|
-
return { name, type: "kv_namespace", namespace_id: namespaceId };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const { format, algorithm, usages, data } = variable as CfCryptoKey;
|
|
36
|
-
if (format) {
|
|
37
|
-
let key_base64;
|
|
38
|
-
let key_jwk;
|
|
39
|
-
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
40
|
-
key_base64 = toBase64(data);
|
|
41
|
-
} else {
|
|
42
|
-
key_jwk = data;
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
name,
|
|
46
|
-
type: "secret_key",
|
|
47
|
-
format,
|
|
48
|
-
algorithm,
|
|
49
|
-
usages,
|
|
50
|
-
key_base64,
|
|
51
|
-
key_jwk,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
throw new TypeError("Unsupported variable: " + variable);
|
|
56
|
-
}
|
|
57
8
|
|
|
58
9
|
export function toMimeType(type: CfModuleType): string {
|
|
59
10
|
switch (type) {
|
|
@@ -72,11 +23,37 @@ export function toMimeType(type: CfModuleType): string {
|
|
|
72
23
|
}
|
|
73
24
|
}
|
|
74
25
|
|
|
75
|
-
function toModule(module: CfModule, entryType
|
|
26
|
+
function toModule(module: CfModule, entryType: CfModuleType): File {
|
|
76
27
|
const { type: moduleType, content } = module;
|
|
77
28
|
const type = toMimeType(moduleType ?? entryType);
|
|
29
|
+
return new File([content], module.name, { type });
|
|
30
|
+
}
|
|
78
31
|
|
|
79
|
-
|
|
32
|
+
export interface WorkerMetadata {
|
|
33
|
+
/** The name of the entry point module. Only exists when the worker is in the ES module format */
|
|
34
|
+
main_module?: string;
|
|
35
|
+
/** The name of the entry point module. Only exists when the worker is in the Service Worker format */
|
|
36
|
+
body_part?: string;
|
|
37
|
+
compatibility_date?: string;
|
|
38
|
+
compatibility_flags?: string[];
|
|
39
|
+
usage_model?: "bundled" | "unbound";
|
|
40
|
+
migrations?: CfDurableObjectMigrations;
|
|
41
|
+
bindings: (
|
|
42
|
+
| { type: "kv_namespace"; name: string; namespace_id: string }
|
|
43
|
+
| { type: "plain_text"; name: string; text: string }
|
|
44
|
+
| {
|
|
45
|
+
type: "durable_object_namespace";
|
|
46
|
+
name: string;
|
|
47
|
+
class_name: string;
|
|
48
|
+
script_name?: string;
|
|
49
|
+
}
|
|
50
|
+
| {
|
|
51
|
+
type: "service";
|
|
52
|
+
name: string;
|
|
53
|
+
service: string;
|
|
54
|
+
environment: string;
|
|
55
|
+
}
|
|
56
|
+
)[];
|
|
80
57
|
}
|
|
81
58
|
|
|
82
59
|
/**
|
|
@@ -84,32 +61,64 @@ function toModule(module: CfModule, entryType?: CfModuleType): Blob {
|
|
|
84
61
|
*/
|
|
85
62
|
export function toFormData(worker: CfWorkerInit): FormData {
|
|
86
63
|
const formData = new FormData();
|
|
87
|
-
const {
|
|
88
|
-
|
|
64
|
+
const {
|
|
65
|
+
main,
|
|
66
|
+
modules,
|
|
67
|
+
bindings,
|
|
68
|
+
migrations,
|
|
69
|
+
usage_model,
|
|
70
|
+
compatibility_date,
|
|
71
|
+
compatibility_flags,
|
|
72
|
+
} = worker;
|
|
89
73
|
|
|
90
|
-
const bindings = [];
|
|
91
|
-
for (const [name, variable] of Object.entries(variables ?? {})) {
|
|
92
|
-
const binding = toBinding(name, variable);
|
|
93
|
-
bindings.push(binding);
|
|
94
|
-
}
|
|
74
|
+
const metadataBindings: WorkerMetadata["bindings"] = [];
|
|
95
75
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
76
|
+
bindings.kv_namespaces?.forEach(({ id, binding }) => {
|
|
77
|
+
metadataBindings.push({
|
|
78
|
+
name: binding,
|
|
79
|
+
type: "kv_namespace",
|
|
80
|
+
namespace_id: id,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
bindings.durable_objects?.bindings.forEach(
|
|
85
|
+
({ name, class_name, script_name }) => {
|
|
86
|
+
metadataBindings.push({
|
|
87
|
+
name,
|
|
88
|
+
type: "durable_object_namespace",
|
|
89
|
+
class_name: class_name,
|
|
90
|
+
...(script_name && { script_name }),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
Object.entries(bindings.vars || {})?.forEach(([key, value]) => {
|
|
96
|
+
metadataBindings.push({ name: key, type: "plain_text", text: value });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
bindings.services?.forEach(({ name, service, environment }) => {
|
|
100
|
+
metadataBindings.push({
|
|
101
|
+
name,
|
|
102
|
+
type: "service",
|
|
103
|
+
service,
|
|
104
|
+
environment,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const metadata: WorkerMetadata = {
|
|
109
|
+
...(main.type !== "commonjs"
|
|
110
|
+
? { main_module: main.name }
|
|
111
|
+
: { body_part: main.name }),
|
|
112
|
+
bindings: metadataBindings,
|
|
113
|
+
...(compatibility_date && { compatibility_date }),
|
|
114
|
+
...(compatibility_flags && { compatibility_flags }),
|
|
115
|
+
...(usage_model && { usage_model }),
|
|
116
|
+
...(migrations && { migrations }),
|
|
101
117
|
};
|
|
102
|
-
|
|
103
|
-
// @ts-expect-error - we should type metadata
|
|
104
|
-
metadata.compatibility_date = compatibility_date;
|
|
105
|
-
}
|
|
106
|
-
if (usage_model) {
|
|
107
|
-
// @ts-expect-error - we should type metadata
|
|
108
|
-
metadata.usage_model = usage_model;
|
|
109
|
-
}
|
|
118
|
+
|
|
110
119
|
formData.set("metadata", JSON.stringify(metadata));
|
|
111
120
|
|
|
112
|
-
if (
|
|
121
|
+
if (main.type === "commonjs" && modules && modules.length > 0) {
|
|
113
122
|
throw new TypeError(
|
|
114
123
|
"More than one module can only be specified when type = 'esm'"
|
|
115
124
|
);
|
|
@@ -117,8 +126,8 @@ export function toFormData(worker: CfWorkerInit): FormData {
|
|
|
117
126
|
|
|
118
127
|
for (const module of [main].concat(modules || [])) {
|
|
119
128
|
const { name } = module;
|
|
120
|
-
const
|
|
121
|
-
formData.set(name,
|
|
129
|
+
const file = toModule(module, main.type ?? "esm");
|
|
130
|
+
formData.set(name, file);
|
|
122
131
|
}
|
|
123
132
|
|
|
124
133
|
return formData;
|
package/src/api/preview.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { URL } from "node:url";
|
|
2
|
+
import { fetch } from "undici";
|
|
3
|
+
import { fetchResult } from "../cfetch";
|
|
3
4
|
import { toFormData } from "./form_data";
|
|
4
5
|
import type { CfAccount, CfWorkerInit } from "./worker";
|
|
5
6
|
|
|
@@ -9,7 +10,7 @@ import type { CfAccount, CfWorkerInit } from "./worker";
|
|
|
9
10
|
* * If true, then using a `workers.dev` subdomain.
|
|
10
11
|
* * Otherwise, a list of routes under a single zone.
|
|
11
12
|
*/
|
|
12
|
-
|
|
13
|
+
type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* A preview token.
|
|
@@ -60,17 +61,17 @@ async function sessionToken(account: CfAccount): Promise<CfPreviewToken> {
|
|
|
60
61
|
? `/zones/${zoneId}/workers/edge-preview`
|
|
61
62
|
: `/accounts/${accountId}/workers/subdomain/edge-preview`;
|
|
62
63
|
|
|
63
|
-
const { exchange_url
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const { host } = new URL(
|
|
64
|
+
const { exchange_url } = await fetchResult<{ exchange_url: string }>(initUrl);
|
|
65
|
+
const { inspector_websocket, token } = (await (
|
|
66
|
+
await fetch(exchange_url)
|
|
67
|
+
).json()) as { inspector_websocket: string; token: string };
|
|
68
|
+
const { host } = new URL(inspector_websocket);
|
|
68
69
|
const query = `cf_workers_preview_token=${token}`;
|
|
69
70
|
|
|
70
71
|
return {
|
|
71
72
|
value: token,
|
|
72
73
|
host,
|
|
73
|
-
inspectorUrl: new URL(`${
|
|
74
|
+
inspectorUrl: new URL(`${inspector_websocket}?${query}`),
|
|
74
75
|
prewarmUrl: new URL(
|
|
75
76
|
`https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
|
|
76
77
|
),
|
|
@@ -96,27 +97,31 @@ export async function previewToken(
|
|
|
96
97
|
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(account);
|
|
97
98
|
|
|
98
99
|
const { accountId, zoneId } = account;
|
|
99
|
-
const scriptId = zoneId ? randomId() : host.split(".")[0];
|
|
100
|
+
const scriptId = zoneId ? randomId() : worker.name || host.split(".")[0];
|
|
100
101
|
const url = `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
|
|
101
102
|
|
|
102
|
-
const mode = zoneId
|
|
103
|
+
const mode: CfPreviewMode = zoneId
|
|
104
|
+
? { routes: ["*/*"] }
|
|
105
|
+
: { workers_dev: true };
|
|
106
|
+
|
|
103
107
|
const formData = toFormData(worker);
|
|
104
108
|
formData.set("wrangler-session-config", JSON.stringify(mode));
|
|
105
109
|
|
|
106
|
-
const { preview_token
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
},
|
|
115
|
-
}
|
|
116
|
-
);
|
|
110
|
+
const { preview_token } = await fetchResult<{ preview_token: string }>(url, {
|
|
111
|
+
method: "POST",
|
|
112
|
+
body: formData,
|
|
113
|
+
headers: {
|
|
114
|
+
"cf-preview-upload-config-token": value,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
117
118
|
return {
|
|
118
|
-
value:
|
|
119
|
-
|
|
119
|
+
value: preview_token,
|
|
120
|
+
// TODO: verify this works with zoned workers
|
|
121
|
+
host:
|
|
122
|
+
worker.name && !zoneId
|
|
123
|
+
? `${worker.name}.${host.split(".").slice(1).join(".")}`
|
|
124
|
+
: host,
|
|
120
125
|
inspectorUrl,
|
|
121
126
|
prewarmUrl,
|
|
122
127
|
};
|