wrangler 0.0.0-e88a54e → 0.0.0-e95e5a0
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 +25 -14
- package/bin/wrangler.js +13 -1
- package/miniflare-dist/index.mjs +4828 -21
- package/package.json +29 -24
- package/pages/functions/buildPlugin.ts +92 -0
- package/pages/functions/buildWorker.ts +80 -4
- package/pages/functions/filepath-routing.test.ts +11 -0
- package/pages/functions/filepath-routing.ts +8 -0
- package/pages/functions/routes.ts +6 -1
- package/pages/functions/template-plugin.ts +146 -0
- package/pages/functions/template-worker.ts +21 -8
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +530 -201
- package/src/__tests__/dev.test.tsx +284 -18
- package/src/__tests__/guess-worker-format.test.ts +28 -1
- package/src/__tests__/helpers/mock-cfetch.ts +5 -1
- package/src/__tests__/helpers/mock-console.ts +20 -3
- package/src/__tests__/helpers/mock-dialogs.ts +26 -1
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +27 -0
- package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
- package/src/__tests__/helpers/run-in-tmp.ts +7 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/https-options.test.ts +5 -2
- package/src/__tests__/index.test.ts +27 -769
- package/src/__tests__/init.test.ts +1516 -0
- package/src/__tests__/jest.setup.ts +4 -4
- package/src/__tests__/kv.test.ts +133 -61
- package/src/__tests__/logger.test.ts +115 -0
- package/src/__tests__/pages.test.ts +311 -26
- package/src/__tests__/publish.test.ts +2789 -618
- package/src/__tests__/r2.test.ts +26 -10
- package/src/__tests__/route.test.ts +4 -4
- package/src/__tests__/secret.test.ts +55 -34
- package/src/__tests__/tail.test.ts +146 -20
- package/src/__tests__/user.test.ts +131 -0
- package/src/__tests__/whoami.test.tsx +14 -9
- package/src/bundle.ts +30 -3
- package/src/cfetch/internal.ts +0 -2
- package/src/cli.ts +5 -11
- package/src/config/README.md +1 -1
- package/src/config/config.ts +20 -4
- package/src/config/environment.ts +32 -4
- package/src/config/index.ts +4 -3
- package/src/config/validation-helpers.ts +96 -7
- package/src/config/validation.ts +388 -54
- package/src/config-cache.ts +45 -0
- package/src/create-worker-preview.ts +22 -13
- package/src/create-worker-upload-form.ts +32 -9
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +132 -78
- package/src/dev/local.tsx +101 -21
- package/src/dev/remote.tsx +23 -13
- package/src/dev/use-esbuild.ts +23 -5
- package/src/dialogs.tsx +16 -6
- package/src/entry.ts +83 -24
- package/src/environment-variables.ts +3 -1
- package/src/errors.ts +11 -0
- package/src/https-options.ts +3 -2
- package/src/index.tsx +689 -394
- package/src/inspect.ts +17 -11
- package/src/intl-polyfill.d.ts +139 -0
- package/src/logger.ts +66 -0
- package/src/miniflare-cli/index.ts +4 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +7 -7
- package/src/open-in-browser.ts +5 -4
- package/src/package-manager.ts +9 -8
- package/src/pages.tsx +875 -56
- package/src/parse.ts +1 -1
- package/src/proxy.ts +28 -19
- package/src/publish.ts +263 -141
- package/src/sites.tsx +27 -14
- package/src/tail/index.ts +2 -2
- package/src/tail/printing.ts +13 -8
- package/src/update-check.ts +19 -0
- package/src/user.tsx +116 -41
- package/src/whoami.tsx +2 -1
- package/src/worker.ts +15 -6
- package/templates/gitignore +171 -0
- package/templates/static-asset-facade.js +1 -1
- package/wrangler-dist/cli.js +21162 -50227
- package/miniflare-dist/index.mjs.map +0 -7
- package/src/__tests__/dev2.test.tsx +0 -154
- package/src/__tests__/logout.test.ts +0 -52
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/reporting.ts +0 -131
- package/wrangler-dist/cli.js.map +0 -7
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import fetchMock from "jest-fetch-mock";
|
|
2
|
+
import { Request } from "undici";
|
|
3
|
+
import { getCloudflareApiBaseUrl } from "../../cfetch";
|
|
4
|
+
import openInBrowser from "../../open-in-browser";
|
|
5
|
+
import { mockHttpServer } from "./mock-http-server";
|
|
6
|
+
|
|
7
|
+
// the response to send when wrangler wants an oauth grant
|
|
8
|
+
let oauthGrantResponse: GrantResponseOptions | "timeout" = {};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Functions to help with mocking various parts of the OAuth Flow
|
|
12
|
+
*/
|
|
13
|
+
export const mockOAuthFlow = () => {
|
|
14
|
+
const fetchHttp = mockHttpServer();
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
fetchMock.resetMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Mock out the callback from a browser to our HttpServer.
|
|
22
|
+
*
|
|
23
|
+
* This function will override `openInBrowser()` so that instead of opening a browser window
|
|
24
|
+
* at the OAuth URL, it will automatically trigger the callback URL on the mock HttpServer that
|
|
25
|
+
* we have created as part of the call to `mockOAuthFlow()`.
|
|
26
|
+
*/
|
|
27
|
+
const mockOAuthServerCallback = () => {
|
|
28
|
+
(
|
|
29
|
+
openInBrowser as jest.MockedFunction<typeof openInBrowser>
|
|
30
|
+
).mockImplementation(async (url: string) => {
|
|
31
|
+
// We don't support the grant response timing out.
|
|
32
|
+
if (oauthGrantResponse === "timeout") {
|
|
33
|
+
throw "unimplemented";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Create a fake callback request that would be created by the OAuth server
|
|
37
|
+
const { searchParams } = new URL(url);
|
|
38
|
+
const queryParams = toQueryParams(oauthGrantResponse, searchParams);
|
|
39
|
+
const request = new Request(
|
|
40
|
+
`${searchParams.get("redirect_uri")}?${queryParams}`
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
// Trigger the mock HttpServer to handle this fake request to continue the OAuth flow.
|
|
44
|
+
fetchHttp(request).catch((e) => {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"Failed to send OAuth Grant to wrangler, maybe the server was closed?",
|
|
47
|
+
e as Error
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const mockGrantAuthorization = ({
|
|
54
|
+
respondWith,
|
|
55
|
+
}: {
|
|
56
|
+
respondWith: "timeout" | "success" | "failure" | GrantResponseOptions;
|
|
57
|
+
}) => {
|
|
58
|
+
if (respondWith === "failure") {
|
|
59
|
+
oauthGrantResponse = {
|
|
60
|
+
error: "access_denied",
|
|
61
|
+
};
|
|
62
|
+
} else if (respondWith === "success") {
|
|
63
|
+
oauthGrantResponse = {
|
|
64
|
+
code: "test-oauth-code",
|
|
65
|
+
};
|
|
66
|
+
} else if (respondWith === "timeout") {
|
|
67
|
+
oauthGrantResponse = "timeout";
|
|
68
|
+
} else {
|
|
69
|
+
oauthGrantResponse = respondWith;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const mockRevokeAuthorization = () => {
|
|
74
|
+
const outcome = {
|
|
75
|
+
actual: new Request("https://example.org"),
|
|
76
|
+
expected: new Request("https://dash.cloudflare.com/oauth2/revoke", {
|
|
77
|
+
method: "POST",
|
|
78
|
+
}),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
fetchMock.mockIf(outcome.expected.url, async (req) => {
|
|
82
|
+
outcome.actual = req;
|
|
83
|
+
return "";
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return outcome;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const mockGetMemberships = (args: {
|
|
90
|
+
success: boolean;
|
|
91
|
+
result: { id: string; account: { id: string; name: string } }[];
|
|
92
|
+
}) => {
|
|
93
|
+
const outcome = {
|
|
94
|
+
actual: new Request("https://example.org"),
|
|
95
|
+
expected: new Request(`${getCloudflareApiBaseUrl()}/memberships`, {
|
|
96
|
+
method: "GET",
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
fetchMock.mockIf(outcome.expected.url, async (req) => {
|
|
101
|
+
outcome.actual = req;
|
|
102
|
+
return {
|
|
103
|
+
status: 200,
|
|
104
|
+
body: JSON.stringify(args),
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return outcome;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const mockGrantAccessToken = ({
|
|
112
|
+
respondWith,
|
|
113
|
+
}: {
|
|
114
|
+
respondWith: MockTokenResponse;
|
|
115
|
+
}) => {
|
|
116
|
+
const outcome = {
|
|
117
|
+
actual: new Request("https://example.org"),
|
|
118
|
+
expected: new Request("https://dash.cloudflare.com/oauth2/token", {
|
|
119
|
+
method: "POST",
|
|
120
|
+
}),
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
fetchMock.mockOnceIf(outcome.expected.url, async (req) => {
|
|
124
|
+
outcome.actual = req;
|
|
125
|
+
return makeTokenResponse(respondWith);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return outcome;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const mockExchangeRefreshTokenForAccessToken = ({
|
|
132
|
+
respondWith,
|
|
133
|
+
}: {
|
|
134
|
+
respondWith: "refreshSuccess" | "refreshError" | "badResponse";
|
|
135
|
+
}) => {
|
|
136
|
+
fetchMock.mockOnceIf(
|
|
137
|
+
"https://dash.cloudflare.com/oauth2/token",
|
|
138
|
+
async () => {
|
|
139
|
+
switch (respondWith) {
|
|
140
|
+
case "refreshSuccess":
|
|
141
|
+
return {
|
|
142
|
+
status: 200,
|
|
143
|
+
body: JSON.stringify({
|
|
144
|
+
access_token: "access_token_success_mock",
|
|
145
|
+
expires_in: 1701,
|
|
146
|
+
refresh_token: "refresh_token_sucess_mock",
|
|
147
|
+
scope: "scope_success_mock",
|
|
148
|
+
token_type: "bearer",
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
case "refreshError":
|
|
152
|
+
return {
|
|
153
|
+
status: 400,
|
|
154
|
+
body: JSON.stringify({
|
|
155
|
+
error: "invalid_request",
|
|
156
|
+
error_description: "error_description_mock",
|
|
157
|
+
error_hint: "error_hint_mock",
|
|
158
|
+
error_verbose: "error_verbose_mock",
|
|
159
|
+
status_code: 400,
|
|
160
|
+
}),
|
|
161
|
+
};
|
|
162
|
+
case "badResponse":
|
|
163
|
+
return {
|
|
164
|
+
status: 400,
|
|
165
|
+
body: `<html> <body> This shouldn't be sent, but should be handled </body> </html>`,
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
default:
|
|
169
|
+
return "Not a respondWith option for `mockExchangeRefreshTokenForAccessToken`";
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
mockGetMemberships,
|
|
177
|
+
mockGrantAccessToken,
|
|
178
|
+
mockGrantAuthorization,
|
|
179
|
+
mockOAuthServerCallback,
|
|
180
|
+
mockRevokeAuthorization,
|
|
181
|
+
mockExchangeRefreshTokenForAccessToken,
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
type GrantResponseOptions = {
|
|
186
|
+
code?: string;
|
|
187
|
+
error?: ErrorType | ErrorType[];
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const toQueryParams = (
|
|
191
|
+
{ code, error }: GrantResponseOptions,
|
|
192
|
+
wranglerRequestParams: URLSearchParams
|
|
193
|
+
): string => {
|
|
194
|
+
const queryParams = [];
|
|
195
|
+
if (code) {
|
|
196
|
+
queryParams.push(`code=${code}`);
|
|
197
|
+
}
|
|
198
|
+
if (error) {
|
|
199
|
+
const stringifiedErr = Array.isArray(error) ? error.join(",") : error;
|
|
200
|
+
queryParams.push(`error=${stringifiedErr}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
queryParams.push(`state=${wranglerRequestParams.get("state")}`);
|
|
204
|
+
|
|
205
|
+
return queryParams.join("&");
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
type ErrorType =
|
|
209
|
+
| "invalid_request"
|
|
210
|
+
| "invalid_grant"
|
|
211
|
+
| "unauthorized_client"
|
|
212
|
+
| "access_denied"
|
|
213
|
+
| "unsupported_response_type"
|
|
214
|
+
| "invalid_scope"
|
|
215
|
+
| "server_error"
|
|
216
|
+
| "temporarily_unavailable"
|
|
217
|
+
| "invalid_client"
|
|
218
|
+
| "unsupported_grant_type"
|
|
219
|
+
| "invalid_json"
|
|
220
|
+
| "invalid_token"
|
|
221
|
+
| "test-token-grant-error";
|
|
222
|
+
|
|
223
|
+
type MockTokenResponse =
|
|
224
|
+
| "ok"
|
|
225
|
+
| "error"
|
|
226
|
+
| {
|
|
227
|
+
access_token: string;
|
|
228
|
+
expires_in: number;
|
|
229
|
+
refresh_token: string;
|
|
230
|
+
scope: string;
|
|
231
|
+
}
|
|
232
|
+
| {
|
|
233
|
+
error: ErrorType;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const makeTokenResponse = (partialResponse: MockTokenResponse): string => {
|
|
237
|
+
let fullResponse: MockTokenResponse;
|
|
238
|
+
|
|
239
|
+
if (partialResponse === "ok") {
|
|
240
|
+
fullResponse = {
|
|
241
|
+
access_token: "test-access-token",
|
|
242
|
+
expires_in: 100000,
|
|
243
|
+
refresh_token: "test-refresh-token",
|
|
244
|
+
scope: "account:read",
|
|
245
|
+
};
|
|
246
|
+
} else if (partialResponse === "error") {
|
|
247
|
+
fullResponse = {
|
|
248
|
+
error: "test-token-grant-error",
|
|
249
|
+
};
|
|
250
|
+
} else {
|
|
251
|
+
fullResponse = partialResponse;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return JSON.stringify(fullResponse);
|
|
255
|
+
};
|
|
@@ -8,6 +8,13 @@ const originalCwd = process.cwd();
|
|
|
8
8
|
export function runInTempDir({ homedir }: { homedir?: string } = {}) {
|
|
9
9
|
let tmpDir: string;
|
|
10
10
|
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
if (tmpDir !== undefined) {
|
|
13
|
+
process.chdir(originalCwd);
|
|
14
|
+
fs.rmSync(tmpDir, { recursive: true });
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
11
18
|
beforeEach(() => {
|
|
12
19
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "wrangler-tests"));
|
|
13
20
|
process.chdir(tmpDir);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
|
|
3
|
+
/** Write a mock Worker script to disk. */
|
|
4
|
+
export function writeWorkerSource({
|
|
5
|
+
basePath = ".",
|
|
6
|
+
format = "js",
|
|
7
|
+
type = "esm",
|
|
8
|
+
}: {
|
|
9
|
+
basePath?: string;
|
|
10
|
+
format?: "js" | "ts" | "jsx" | "tsx" | "mjs";
|
|
11
|
+
type?: "esm" | "sw";
|
|
12
|
+
} = {}) {
|
|
13
|
+
if (basePath !== ".") {
|
|
14
|
+
fs.mkdirSync(basePath, { recursive: true });
|
|
15
|
+
}
|
|
16
|
+
fs.writeFileSync(
|
|
17
|
+
`${basePath}/index.${format}`,
|
|
18
|
+
type === "esm"
|
|
19
|
+
? `import { foo } from "./another";
|
|
20
|
+
export default {
|
|
21
|
+
async fetch(request) {
|
|
22
|
+
return new Response('Hello' + foo);
|
|
23
|
+
},
|
|
24
|
+
};`
|
|
25
|
+
: `import { foo } from "./another";
|
|
26
|
+
addEventListener('fetch', event => {
|
|
27
|
+
event.respondWith(new Response('Hello' + foo));
|
|
28
|
+
})`
|
|
29
|
+
);
|
|
30
|
+
fs.writeFileSync(`${basePath}/another.${format}`, `export const foo = 100;`);
|
|
31
|
+
}
|
|
@@ -97,8 +97,11 @@ describe("getHttpsOptions()", () => {
|
|
|
97
97
|
`"Generating new self-signed certificate..."`
|
|
98
98
|
);
|
|
99
99
|
expect(std.warn).toMatchInlineSnapshot(`
|
|
100
|
-
"
|
|
101
|
-
|
|
100
|
+
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mUnable to cache generated self-signed certificate in home/.wrangler/local-cert.[0m
|
|
101
|
+
|
|
102
|
+
ERROR: Cannot write file
|
|
103
|
+
|
|
104
|
+
"
|
|
102
105
|
`);
|
|
103
106
|
expect(std.err).toMatchInlineSnapshot(`""`);
|
|
104
107
|
});
|