wrangler 2.0.29 → 2.1.2

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 (45) hide show
  1. package/miniflare-dist/index.mjs +1136 -372
  2. package/package.json +3 -2
  3. package/src/__tests__/helpers/mock-cfetch.ts +39 -19
  4. package/src/__tests__/helpers/mock-console.ts +11 -2
  5. package/src/__tests__/helpers/msw/handlers/index.ts +13 -0
  6. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  7. package/src/__tests__/helpers/msw/handlers/oauth.ts +36 -0
  8. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  9. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  10. package/src/__tests__/helpers/msw/index.ts +4 -0
  11. package/src/__tests__/index.test.ts +9 -7
  12. package/src/__tests__/init.test.ts +356 -5
  13. package/src/__tests__/jest.setup.ts +16 -0
  14. package/src/__tests__/middleware.test.ts +768 -0
  15. package/src/__tests__/pages.test.ts +11 -12
  16. package/src/__tests__/publish.test.ts +516 -438
  17. package/src/__tests__/r2.test.ts +128 -93
  18. package/src/__tests__/secret.test.ts +78 -0
  19. package/src/__tests__/tail.test.ts +47 -74
  20. package/src/__tests__/whoami.test.tsx +49 -64
  21. package/src/api/dev.ts +23 -4
  22. package/src/bundle.ts +225 -1
  23. package/src/dev/dev.tsx +3 -1
  24. package/src/dev/local.tsx +2 -2
  25. package/src/dev/remote.tsx +6 -3
  26. package/src/dev/start-server.ts +11 -7
  27. package/src/dev/use-esbuild.ts +4 -0
  28. package/src/dev.tsx +6 -16
  29. package/src/dialogs.tsx +12 -0
  30. package/src/index.tsx +95 -4
  31. package/src/init.ts +286 -11
  32. package/src/miniflare-cli/assets.ts +130 -415
  33. package/src/miniflare-cli/index.ts +3 -1
  34. package/src/pages/dev.tsx +5 -1
  35. package/src/pages/hash.tsx +13 -0
  36. package/src/pages/upload.tsx +3 -18
  37. package/src/publish.ts +38 -4
  38. package/src/tail/filters.ts +1 -5
  39. package/src/tail/index.ts +6 -3
  40. package/templates/middleware/common.ts +62 -0
  41. package/templates/middleware/loader-modules.ts +84 -0
  42. package/templates/middleware/loader-sw.ts +213 -0
  43. package/templates/middleware/middleware-pretty-error.ts +40 -0
  44. package/templates/middleware/middleware-scheduled.ts +14 -0
  45. package/wrangler-dist/cli.js +65900 -65432
@@ -6,7 +6,12 @@ import { parseConfigFileTextToJson } from "typescript";
6
6
  import { version as wranglerVersion } from "../../package.json";
7
7
  import { getPackageManager } from "../package-manager";
8
8
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
9
- import { setMockFetchDashScript, unsetAllMocks } from "./helpers/mock-cfetch";
9
+ import {
10
+ setMockFetchDashScript,
11
+ setMockResponse,
12
+ unsetAllMocks,
13
+ unsetSpecialMockFns,
14
+ } from "./helpers/mock-cfetch";
10
15
  import { mockConsoleMethods } from "./helpers/mock-console";
11
16
  import {
12
17
  mockConfirm,
@@ -1908,6 +1913,7 @@ describe("init", () => {
1908
1913
  mockAccountId({ accountId: "LCARS" });
1909
1914
  afterEach(() => {
1910
1915
  unsetAllMocks();
1916
+ unsetSpecialMockFns();
1911
1917
  });
1912
1918
  const mockDashboardScript = `
1913
1919
  export default {
@@ -1916,12 +1922,305 @@ describe("init", () => {
1916
1922
  },
1917
1923
  };
1918
1924
  `;
1925
+ const mockServiceMetadata = {
1926
+ id: "memory-crystal",
1927
+ default_environment: {
1928
+ environment: "test",
1929
+ created_on: "1987-9-27",
1930
+ modified_on: "1987-9-27",
1931
+ script: {
1932
+ id: "memory-crystal",
1933
+ tag: "test-tag",
1934
+ etag: "some-etag",
1935
+ handlers: [],
1936
+ modified_on: "1987-9-27",
1937
+ created_on: "1987-9-27",
1938
+ migration_tag: "some-migration-tag",
1939
+ usage_model: "bundled",
1940
+ compatibility_date: "1987-9-27",
1941
+ },
1942
+ },
1943
+ created_on: "1987-9-27",
1944
+ modified_on: "1987-9-27",
1945
+ usage_model: "bundled",
1946
+ environments: [
1947
+ {
1948
+ environment: "test",
1949
+ created_on: "1987-9-27",
1950
+ modified_on: "1987-9-27",
1951
+ },
1952
+ {
1953
+ environment: "staging",
1954
+ created_on: "1987-9-27",
1955
+ modified_on: "1987-9-27",
1956
+ },
1957
+ ],
1958
+ };
1959
+ const mockBindingsRes = [
1960
+ {
1961
+ type: "secret_text",
1962
+ name: "ABC",
1963
+ },
1964
+ {
1965
+ type: "plain_text",
1966
+ name: "ANOTHER",
1967
+ text: "thing",
1968
+ },
1969
+ {
1970
+ type: "durable_object_namespace",
1971
+ name: "DURABLE_TEST",
1972
+ class_name: "Durability",
1973
+ script_name: "another-durable-object-worker",
1974
+ environment: "production",
1975
+ },
1976
+ {
1977
+ type: "kv_namespace",
1978
+ name: "kv_testing",
1979
+ namespace_id: "some-namespace-id",
1980
+ },
1981
+ {
1982
+ type: "r2_bucket",
1983
+ bucket_name: "test-bucket",
1984
+ name: "test-bucket",
1985
+ },
1986
+ {
1987
+ environment: "production",
1988
+ name: "website",
1989
+ service: "website",
1990
+ type: "service",
1991
+ },
1992
+ {
1993
+ type: "namespace",
1994
+ name: "name-namespace-mock",
1995
+ namespace: "namespace-mock",
1996
+ },
1997
+ {
1998
+ name: "httplogs",
1999
+ type: "logfwdr",
2000
+ destination: "httplogs",
2001
+ },
2002
+ {
2003
+ name: "trace",
2004
+ type: "logfwdr",
2005
+ destination: "trace",
2006
+ },
2007
+ {
2008
+ type: "wasm_module",
2009
+ name: "WASM_MODULE_ONE",
2010
+ part: "./some_wasm.wasm",
2011
+ },
2012
+ {
2013
+ type: "wasm_module",
2014
+ name: "WASM_MODULE_TWO",
2015
+ part: "./more_wasm.wasm",
2016
+ },
2017
+ {
2018
+ type: "text_blob",
2019
+ name: "TEXT_BLOB_ONE",
2020
+ part: "./my-entire-app-depends-on-this.cfg",
2021
+ },
2022
+ {
2023
+ type: "text_blob",
2024
+ name: "TEXT_BLOB_TWO",
2025
+ part: "./the-entirety-of-human-knowledge.txt",
2026
+ },
2027
+ { type: "data_blob", name: "DATA_BLOB_ONE", part: "DATA_BLOB_ONE" },
2028
+ { type: "data_blob", name: "DATA_BLOB_TWO", part: "DATA_BLOB_TWO" },
2029
+ {
2030
+ type: "some unsafe thing",
2031
+ name: "UNSAFE_BINDING_ONE",
2032
+ data: { some: { unsafe: "thing" } },
2033
+ },
2034
+ {
2035
+ type: "another unsafe thing",
2036
+ name: "UNSAFE_BINDING_TWO",
2037
+ data: 1337,
2038
+ },
2039
+ ];
2040
+ const mockRoutesRes = [
2041
+ {
2042
+ id: "some-route-id",
2043
+ pattern: "delta.quadrant",
2044
+ },
2045
+ ];
2046
+ const mockConfigExpected = {
2047
+ main: "src/index.ts",
2048
+ compatibility_date: "1987-9-27",
2049
+ name: "isolinear-optical-chip",
2050
+ migrations: [
2051
+ {
2052
+ new_classes: ["Durability"],
2053
+ tag: "some-migration-tag",
2054
+ },
2055
+ ],
2056
+ durable_objects: {
2057
+ bindings: [
2058
+ {
2059
+ class_name: "Durability",
2060
+ name: "DURABLE_TEST",
2061
+ script_name: "another-durable-object-worker",
2062
+ environment: "production",
2063
+ },
2064
+ ],
2065
+ },
2066
+ kv_namespaces: [
2067
+ {
2068
+ binding: "kv_testing",
2069
+ id: "some-namespace-id",
2070
+ },
2071
+ ],
2072
+ r2_buckets: [
2073
+ {
2074
+ bucket_name: "test-bucket",
2075
+ binding: "test-bucket",
2076
+ },
2077
+ ],
2078
+ dispatch_namespaces: [
2079
+ {
2080
+ binding: "name-namespace-mock",
2081
+ namespace: "namespace-mock",
2082
+ },
2083
+ ],
2084
+ route: "delta.quadrant",
2085
+ services: [
2086
+ {
2087
+ environment: "production",
2088
+ binding: "website",
2089
+ service: "website",
2090
+ },
2091
+ ],
2092
+ triggers: {
2093
+ crons: ["0 0 0 * * *"],
2094
+ },
2095
+ usage_model: "bundled",
2096
+ vars: {
2097
+ name: "ANOTHER",
2098
+ text: "thing",
2099
+ },
2100
+ env: {
2101
+ test: {},
2102
+ staging: {},
2103
+ },
2104
+ unsafe: {
2105
+ bindings: [
2106
+ {
2107
+ name: "UNSAFE_BINDING_ONE",
2108
+ type: "some unsafe thing",
2109
+ data: { some: { unsafe: "thing" } },
2110
+ },
2111
+ {
2112
+ name: "UNSAFE_BINDING_TWO",
2113
+ type: "another unsafe thing",
2114
+ data: 1337,
2115
+ },
2116
+ ],
2117
+ },
2118
+ wasm_modules: {
2119
+ WASM_MODULE_ONE: "./some_wasm.wasm",
2120
+ WASM_MODULE_TWO: "./more_wasm.wasm",
2121
+ },
2122
+ text_blobs: {
2123
+ TEXT_BLOB_ONE: "./my-entire-app-depends-on-this.cfg",
2124
+ TEXT_BLOB_TWO: "./the-entirety-of-human-knowledge.txt",
2125
+ },
2126
+ data_blobs: {
2127
+ DATA_BLOB_ONE: "DATA_BLOB_ONE",
2128
+ DATA_BLOB_TWO: "DATA_BLOB_TWO",
2129
+ },
2130
+ logfwdr: {
2131
+ schema: "",
2132
+ bindings: [
2133
+ {
2134
+ name: "httplogs",
2135
+ destination: "httplogs",
2136
+ },
2137
+ {
2138
+ name: "trace",
2139
+ destination: "trace",
2140
+ },
2141
+ ],
2142
+ },
2143
+ };
2144
+
2145
+ function mockSupportingDashRequests({
2146
+ expectedAccountId = "",
2147
+ expectedScriptName = "",
2148
+ expectedEnvironment = "",
2149
+ }) {
2150
+ setMockResponse(
2151
+ `/accounts/:accountId/workers/services/:scriptName`,
2152
+ "GET",
2153
+ ([_url, accountId, scriptName]) => {
2154
+ expect(accountId).toEqual(expectedAccountId);
2155
+ expect(scriptName).toEqual(expectedScriptName);
2156
+
2157
+ return mockServiceMetadata;
2158
+ }
2159
+ );
2160
+ setMockResponse(
2161
+ `/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2162
+ "GET",
2163
+ ([_url, accountId, scriptName, environment]) => {
2164
+ expect(accountId).toEqual(expectedAccountId);
2165
+ expect(scriptName).toEqual(expectedScriptName);
2166
+ expect(environment).toEqual(expectedEnvironment);
2167
+
2168
+ return mockBindingsRes;
2169
+ }
2170
+ );
2171
+ setMockResponse(
2172
+ `/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2173
+ "GET",
2174
+ ([_url, accountId, scriptName, environment]) => {
2175
+ expect(accountId).toEqual(expectedAccountId);
2176
+ expect(scriptName).toEqual(expectedScriptName);
2177
+ expect(environment).toEqual(expectedEnvironment);
2178
+
2179
+ return mockRoutesRes;
2180
+ }
2181
+ );
2182
+ setMockResponse(
2183
+ `/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2184
+ "GET",
2185
+ ([_url, accountId, scriptName, environment]) => {
2186
+ expect(accountId).toEqual(expectedAccountId);
2187
+ expect(scriptName).toEqual(expectedScriptName);
2188
+ expect(environment).toEqual(expectedEnvironment);
2189
+
2190
+ return mockServiceMetadata.default_environment;
2191
+ }
2192
+ );
2193
+ setMockResponse(
2194
+ `/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2195
+ "GET",
2196
+ ([_url, accountId, scriptName]) => {
2197
+ expect(accountId).toEqual(expectedAccountId);
2198
+ expect(scriptName).toEqual(expectedScriptName);
2199
+
2200
+ return {
2201
+ schedules: [
2202
+ {
2203
+ cron: "0 0 0 * * *",
2204
+ created_on: new Date(1987, 9, 27),
2205
+ modified_on: new Date(1987, 9, 27),
2206
+ },
2207
+ ],
2208
+ };
2209
+ }
2210
+ );
2211
+ }
1919
2212
 
1920
2213
  //TODO: Tests for a case when a worker name doesn't exist - JACOB & CASS
1921
2214
  it("should download source script from dashboard w/ positional <name> in TypeScript project", async () => {
2215
+ mockSupportingDashRequests({
2216
+ expectedAccountId: "LCARS",
2217
+ expectedScriptName: "memory-crystal",
2218
+ expectedEnvironment: "test",
2219
+ });
1922
2220
  setMockFetchDashScript({
1923
2221
  accountId: "LCARS",
1924
2222
  fromDashScriptName: "memory-crystal",
2223
+ environment: mockServiceMetadata.default_environment.environment,
1925
2224
  mockResponse: mockDashboardScript,
1926
2225
  });
1927
2226
  mockConfirm(
@@ -1960,17 +2259,22 @@ describe("init", () => {
1960
2259
  },
1961
2260
  "isolinear-optical-chip/tsconfig.json": true,
1962
2261
  "isolinear-optical-chip/wrangler.toml": wranglerToml({
1963
- ...MINIMAL_WRANGLER_TOML,
1964
- name: "isolinear-optical-chip",
2262
+ ...mockConfigExpected,
1965
2263
  }),
1966
2264
  },
1967
2265
  });
1968
2266
  });
1969
2267
 
1970
2268
  it("should download source script from dashboard w/ out positional <name>", async () => {
2269
+ mockSupportingDashRequests({
2270
+ expectedAccountId: "LCARS",
2271
+ expectedScriptName: "isolinear-optical-chip",
2272
+ expectedEnvironment: "test",
2273
+ });
1971
2274
  setMockFetchDashScript({
1972
2275
  accountId: "LCARS",
1973
2276
  fromDashScriptName: "isolinear-optical-chip",
2277
+ environment: mockServiceMetadata.default_environment.environment,
1974
2278
  mockResponse: mockDashboardScript,
1975
2279
  });
1976
2280
  mockConfirm(
@@ -2007,7 +2311,7 @@ describe("init", () => {
2007
2311
  },
2008
2312
  "isolinear-optical-chip/tsconfig.json": true,
2009
2313
  "isolinear-optical-chip/wrangler.toml": wranglerToml({
2010
- ...MINIMAL_WRANGLER_TOML,
2314
+ ...mockConfigExpected,
2011
2315
  name: "isolinear-optical-chip",
2012
2316
  }),
2013
2317
  },
@@ -2015,9 +2319,15 @@ describe("init", () => {
2015
2319
  });
2016
2320
 
2017
2321
  it("should download source script from dashboard as plain JavaScript", async () => {
2322
+ mockSupportingDashRequests({
2323
+ expectedAccountId: "LCARS",
2324
+ expectedScriptName: "isolinear-optical-chip",
2325
+ expectedEnvironment: "test",
2326
+ });
2018
2327
  setMockFetchDashScript({
2019
2328
  accountId: "LCARS",
2020
2329
  fromDashScriptName: "isolinear-optical-chip",
2330
+ environment: mockServiceMetadata.default_environment.environment,
2021
2331
  mockResponse: mockDashboardScript,
2022
2332
  });
2023
2333
  mockConfirm(
@@ -2054,12 +2364,53 @@ describe("init", () => {
2054
2364
  },
2055
2365
  "isolinear-optical-chip/tsconfig.json": false,
2056
2366
  "isolinear-optical-chip/wrangler.toml": wranglerToml({
2057
- ...MINIMAL_WRANGLER_TOML,
2367
+ ...mockConfigExpected,
2058
2368
  name: "isolinear-optical-chip",
2059
2369
  }),
2060
2370
  },
2061
2371
  });
2062
2372
  });
2373
+ it("should throw an error to retry if a request fails", async () => {
2374
+ setMockResponse(
2375
+ `/accounts/:accountId/workers/services/:scriptName`,
2376
+ "GET",
2377
+ () => mockServiceMetadata
2378
+ );
2379
+ setMockResponse(
2380
+ `/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2381
+ "GET",
2382
+ () => Promise.reject()
2383
+ );
2384
+
2385
+ setMockFetchDashScript({
2386
+ accountId: "LCARS",
2387
+ fromDashScriptName: "isolinear-optical-chip",
2388
+ environment: mockServiceMetadata.default_environment.environment,
2389
+ mockResponse: mockDashboardScript,
2390
+ });
2391
+ mockConfirm(
2392
+ {
2393
+ text: "Would you like to use git to manage this Worker?",
2394
+ result: false,
2395
+ },
2396
+ {
2397
+ text: "Would you like to use TypeScript?",
2398
+ result: false,
2399
+ },
2400
+ {
2401
+ text: "No package.json found. Would you like to create one?",
2402
+ result: true,
2403
+ },
2404
+ {
2405
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2406
+ result: true,
2407
+ }
2408
+ );
2409
+
2410
+ await expect(
2411
+ runWrangler("init --from-dash isolinear-optical-chip")
2412
+ ).rejects.toThrowError();
2413
+ });
2063
2414
 
2064
2415
  it("should not continue if no worker name is provided", async () => {
2065
2416
  await expect(
@@ -14,6 +14,7 @@ import {
14
14
  mockFetchR2Objects,
15
15
  } from "./helpers/mock-cfetch";
16
16
  import { MockWebSocket } from "./helpers/mock-web-socket";
17
+ import { msw } from "./helpers/msw";
17
18
 
18
19
  /**
19
20
  * The relative path between the bundled code and the Wrangler package.
@@ -57,6 +58,21 @@ fetchMock.doMock(() => {
57
58
 
58
59
  jest.mock("../package-manager");
59
60
 
61
+ // requests not mocked with `jest-fetch-mock` fall through
62
+ // to `mock-service-worker`
63
+ fetchMock.dontMock();
64
+ beforeAll(() => {
65
+ msw.listen({
66
+ onUnhandledRequest: (request) => {
67
+ throw new Error(
68
+ `No mock found for ${request.method} ${request.url.href}`
69
+ );
70
+ },
71
+ });
72
+ });
73
+ afterEach(() => msw.resetHandlers());
74
+ afterAll(() => msw.close());
75
+
60
76
  jest.mock("../cfetch/internal");
61
77
  (fetchInternal as jest.Mock).mockImplementation(mockFetchInternal);
62
78
  (fetchKVGetValue as jest.Mock).mockImplementation(mockFetchKVGetValue);