wrangler 2.7.0 → 2.8.0

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.
Files changed (34) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/helpers/mock-dialogs.ts +2 -0
  3. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -5
  4. package/src/__tests__/helpers/mock-known-routes.ts +7 -2
  5. package/src/__tests__/helpers/mock-kv.ts +29 -16
  6. package/src/__tests__/helpers/mock-oauth-flow.ts +90 -98
  7. package/src/__tests__/helpers/msw/handlers/deployments.ts +18 -0
  8. package/src/__tests__/helpers/msw/index.ts +30 -1
  9. package/src/__tests__/init.test.ts +3 -14
  10. package/src/__tests__/jest.setup.ts +0 -23
  11. package/src/__tests__/pages-deployment-tail.test.ts +72 -1
  12. package/src/__tests__/pages.test.ts +18 -16
  13. package/src/__tests__/publish.test.ts +744 -522
  14. package/src/__tests__/secret.test.ts +1 -9
  15. package/src/__tests__/tail.test.ts +66 -1
  16. package/src/__tests__/tsconfig.tsbuildinfo +1 -1
  17. package/src/__tests__/user.test.ts +5 -15
  18. package/src/api/dev.ts +17 -5
  19. package/src/cfetch/internal.ts +0 -3
  20. package/src/d1/backups.tsx +1 -5
  21. package/src/dev/remote.tsx +2 -0
  22. package/src/docs/index.ts +2 -1
  23. package/src/init.ts +1 -1
  24. package/src/pages/dev.ts +57 -62
  25. package/src/pages/functions/buildPlugin.ts +2 -20
  26. package/src/pages/functions/buildWorker.ts +136 -20
  27. package/src/pages/functions/tsconfig.tsbuildinfo +1 -1
  28. package/src/pages/publish.tsx +36 -9
  29. package/src/publish/publish.ts +29 -4
  30. package/src/tail/createTail.ts +28 -1
  31. package/src/tail/printing.ts +15 -0
  32. package/templates/checked-fetch.js +1 -3
  33. package/wrangler-dist/cli.js +2935 -2824
  34. package/src/__tests__/helpers/mock-cfetch.ts +0 -278
@@ -1,278 +0,0 @@
1
- import { Readable } from "node:stream";
2
- import { URL, URLSearchParams } from "node:url";
3
- import { pathToRegexp } from "path-to-regexp";
4
- import { Response } from "undici";
5
- import { getCloudflareApiBaseUrl } from "../../environment-variables/misc-variables";
6
- import type { FetchResult, FetchError } from "../../cfetch";
7
- import type {
8
- fetchInternal,
9
- fetchR2Objects,
10
- performApiFetch,
11
- } from "../../cfetch/internal";
12
- import type { RequestInit, BodyInit, HeadersInit } from "undici";
13
-
14
- /**
15
- * When the custom mocks fallthrough in tests because they aren't set, instead of throwing an error,
16
- * we use real fetch to make the request which will use Mock Service Workers.
17
- */
18
- const {
19
- fetchInternal: realFetchInternal,
20
- }: { fetchInternal: typeof fetchInternal } = jest.requireActual(
21
- "../../cfetch/internal"
22
- );
23
- const {
24
- fetchR2Objects: realFetchR2Objects,
25
- }: { fetchR2Objects: typeof fetchR2Objects } = jest.requireActual(
26
- "../../cfetch/internal"
27
- );
28
-
29
- const {
30
- performApiFetch: realPerformApiFetch,
31
- }: { performApiFetch: typeof performApiFetch } = jest.requireActual(
32
- "../../cfetch/internal"
33
- );
34
-
35
- /**
36
- * The signature of the function that will handle a mock request.
37
- */
38
- export type MockHandler<ResponseType> = (
39
- uri: RegExpExecArray,
40
- init: RequestInit,
41
- queryParams: URLSearchParams
42
- ) => ResponseType | Promise<ResponseType>;
43
-
44
- type RemoveMockFn = () => void;
45
-
46
- interface MockFetch<ResponseType> {
47
- regexp: RegExp;
48
- method: string | undefined;
49
- handler: MockHandler<ResponseType>;
50
- }
51
- const mocks: MockFetch<unknown>[] = [];
52
-
53
- /**
54
- * The mock implementation of `cfApi.fetch()`.
55
- *
56
- * This function will attempt to match the given request to one of the mock handlers configured by calls to `setMock`.
57
- *
58
- * Once found the handler will be used to generate a mock response.
59
- */
60
- export async function mockFetchInternal(
61
- resource: string,
62
- init: RequestInit = {},
63
- queryParams: URLSearchParams | undefined
64
- ) {
65
- for (const { regexp, method, handler } of mocks) {
66
- const resourcePath = new URL(resource, getCloudflareApiBaseUrl()).pathname;
67
- const uri = regexp.exec(resourcePath);
68
- // Do the resource path and (if specified) the HTTP method match?
69
- if (uri !== null && (!method || method === (init.method ?? "GET"))) {
70
- // The `resource` regular expression will extract the labelled groups from the URL.
71
- // These are passed through to the `handler` call, to allow it to do additional checks or behaviour.
72
- return await handler(uri, init, queryParams ?? new URLSearchParams()); // TODO: should we have some kind of fallthrough system? we'll see.
73
- }
74
- }
75
-
76
- // let it fall through to mock-service-worker
77
- // (do a real, unmocked fetch)
78
- return await realFetchInternal(resource, init, queryParams);
79
- }
80
-
81
- /**
82
- * We don't have loads of infrastructure around performApiFetch
83
- * the same way we do for cfetch, so we'll just pass off the real
84
- * deal to avoid getting it auto-mocked, so that msw can catch it.
85
- */
86
- export const mockPerformApiFetch = realPerformApiFetch;
87
-
88
- /**
89
- * Specify an expected resource path that is to be handled, resulting in a raw JSON response.
90
- *
91
- * @param resource The path of the resource to be matched.
92
- * This can include wildcards whose value will be passed to the `handler`.
93
- * @param handler The function that will generate the mock response for this request.
94
- */
95
- export function setMockRawResponse<ResponseType>(
96
- resource: string,
97
- handler: MockHandler<ResponseType>
98
- ): RemoveMockFn;
99
- /**
100
- * Specify an expected resource path that is to be handled, resulting in a raw JSON response.
101
- *
102
- * @param resource The path of the resource to be matched.
103
- * This can include wildcards whose value will be passed to the `handler`.
104
- * @param method The HTTP method (e.g. GET, POST, etc) that the request must have to match this mock handler.
105
- * @param handler The function that will generate the mock response for this request.
106
- */
107
- export function setMockRawResponse<ResponseType>(
108
- resource: string,
109
- method: string,
110
- handler: MockHandler<ResponseType>
111
- ): RemoveMockFn;
112
- /**
113
- * Specify an expected resource path that is to be handled, resulting in a raw JSON response.
114
- */
115
- export function setMockRawResponse<ResponseType>(
116
- resource: string,
117
- ...args: [string, MockHandler<ResponseType>] | [MockHandler<ResponseType>]
118
- ): RemoveMockFn {
119
- const handler = args.pop() as MockHandler<ResponseType>;
120
- const method = args.pop() as string;
121
- const mock = {
122
- resource,
123
- method,
124
- handler,
125
- regexp: pathToRegexp(resource),
126
- };
127
- mocks.push(mock);
128
- return () => {
129
- const mockIndex = mocks.indexOf(mock);
130
- if (mockIndex !== -1) {
131
- mocks.splice(mockIndex, 1);
132
- }
133
- };
134
- }
135
-
136
- /**
137
- * Specify an expected resource path that is to be handled, resulting in a `FetchRequest`.
138
- *
139
- * The mock `handler` should return the `result`, which will then be wrapped in a `FetchRequest` object.
140
- *
141
- * @param resource The path of the resource to be matched.
142
- * This can include wildcards whose value will be passed to the `handler`.
143
- * @param handler The function that will generate the mock response for this request.
144
- */
145
- export function setMockResponse<ResponseType>(
146
- resource: string,
147
- handler: MockHandler<ResponseType>
148
- ): RemoveMockFn;
149
- /**
150
- * Specify an expected resource path that is to be handled, resulting in a FetchRequest..
151
- *
152
- * @param resource The path of the resource to be matched.
153
- * This can include wildcards whose value will be passed to the `handler`.
154
- * @param method The HTTP method (e.g. GET, POST, etc) that the request must have to match this mock handler.
155
- * @param handler The function that will generate the mock response for this request.
156
- */
157
- export function setMockResponse<ResponseType>(
158
- resource: string,
159
- method: string,
160
- handler: MockHandler<ResponseType>
161
- ): RemoveMockFn;
162
- /**
163
- * Specify an expected resource path that is to be handled, resulting in a FetchRequest.
164
- */
165
- export function setMockResponse<ResponseType>(
166
- resource: string,
167
- ...args: [string, MockHandler<ResponseType>] | [MockHandler<ResponseType>]
168
- ): RemoveMockFn {
169
- const handler = args.pop() as MockHandler<ResponseType>;
170
- const method = args.pop() as string;
171
- return setMockRawResponse(resource, method, (...handlerArgs) =>
172
- createFetchResult(handler(...handlerArgs))
173
- );
174
- }
175
-
176
- /**
177
- * A helper to make it easier to create `FetchResult` objects in tests.
178
- * TODO: Hijack this for MSW response objects. - JACOB
179
- */
180
- export async function createFetchResult<ResponseType>(
181
- result: ResponseType | Promise<ResponseType>,
182
- success = true,
183
- errors: FetchError[] = [],
184
- messages: string[] = [],
185
- result_info?: unknown
186
- ): Promise<FetchResult<ResponseType>> {
187
- return result_info
188
- ? {
189
- result: await result,
190
- success,
191
- errors,
192
- messages,
193
- result_info,
194
- }
195
- : {
196
- result: await result,
197
- success,
198
- errors,
199
- messages,
200
- };
201
- }
202
-
203
- /**
204
- * Remove all the configured mock handlers.
205
- *
206
- * This should be called in an `afterEach()` block to ensure that mock handlers do not leak between tests.
207
- */
208
- export function unsetAllMocks() {
209
- mocks.length = 0;
210
- }
211
-
212
- /**
213
- * We special-case fetching the request for `kv:key get`, because it's
214
- * the only cloudflare API endpoint that returns a plain string as the
215
- * value, and not as the "standard" FetchResult-style json. Hence, we also
216
- * special-case mocking it here.
217
- */
218
-
219
- const kvGetMocks = new Map<string, string | Buffer>();
220
- const r2GetMocks = new Map<string, string | undefined>();
221
- const dashScriptMocks = new Map<string, string | undefined>();
222
-
223
- /**
224
- * @mocked typeof fetchR2Objects
225
- */
226
- export async function mockFetchR2Objects(
227
- resource: string,
228
- bodyInit: {
229
- body: BodyInit | Readable;
230
- headers: HeadersInit | undefined;
231
- method: "PUT" | "GET" | "DELETE";
232
- }
233
- ): Promise<Response> {
234
- if (r2GetMocks.has(resource)) {
235
- /**
236
- * Here we destroy & removeListeners to "drain" the stream, for testing purposes
237
- * mimicking the fetch request taking in the stream and draining it.
238
- */
239
- if (bodyInit.body instanceof Readable) {
240
- bodyInit.body.destroy();
241
- bodyInit.body.removeAllListeners();
242
- }
243
-
244
- const value = r2GetMocks.get(resource);
245
-
246
- return new Response(value);
247
- }
248
-
249
- // No mocks found for ${init.method ?? "any HTTP"} request to ${resource}
250
- // let it fall through to Mock Service Worker with a real fetch.
251
- return await realFetchR2Objects(resource, bodyInit);
252
- }
253
-
254
- /**
255
- * Mock setter for usage within test blocks, companion helper to `mockFetchR2Objects`
256
- */
257
- export function setMockFetchR2Objects({
258
- accountId,
259
- bucketName,
260
- objectName,
261
- mockResponse,
262
- }: {
263
- accountId: string;
264
- bucketName: string;
265
- objectName: string;
266
- mockResponse?: string;
267
- }) {
268
- r2GetMocks.set(
269
- `/accounts/${accountId}/r2/buckets/${bucketName}/objects/${objectName}`,
270
- mockResponse
271
- );
272
- }
273
-
274
- export function unsetSpecialMockFns() {
275
- kvGetMocks.clear();
276
- r2GetMocks.clear();
277
- dashScriptMocks.clear();
278
- }