wrangler 2.0.29 → 2.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wrangler",
3
- "version": "2.0.29",
3
+ "version": "2.1.0",
4
4
  "description": "Command-line interface for all things Cloudflare Workers",
5
5
  "keywords": [
6
6
  "wrangler",
@@ -97,7 +97,7 @@
97
97
  "blake3-wasm": "^2.1.5",
98
98
  "chokidar": "^3.5.3",
99
99
  "esbuild": "0.14.51",
100
- "miniflare": "^2.7.1",
100
+ "miniflare": "^2.8.1",
101
101
  "nanoid": "^3.3.3",
102
102
  "path-to-regexp": "^6.2.0",
103
103
  "selfsigned": "^2.0.1",
@@ -270,9 +270,7 @@ export function unsetSpecialMockFns() {
270
270
  */
271
271
  export async function mockFetchDashScript(resource: string): Promise<string> {
272
272
  if (dashScriptMocks.has(resource)) {
273
- const value = dashScriptMocks.get(resource) ?? "";
274
-
275
- return value;
273
+ return dashScriptMocks.get(resource) ?? "";
276
274
  }
277
275
  throw new Error(`no mock found for \`init from-dash\` - ${resource}`);
278
276
  }
@@ -283,14 +281,16 @@ export async function mockFetchDashScript(resource: string): Promise<string> {
283
281
  export function setMockFetchDashScript({
284
282
  accountId,
285
283
  fromDashScriptName,
284
+ environment,
286
285
  mockResponse,
287
286
  }: {
288
287
  accountId: string;
289
288
  fromDashScriptName: string;
289
+ environment: string;
290
290
  mockResponse?: string;
291
291
  }) {
292
292
  dashScriptMocks.set(
293
- `/accounts/${accountId}/workers/scripts/${fromDashScriptName}`,
293
+ `/accounts/${accountId}/workers/services/${fromDashScriptName}/environments/${environment}/content`,
294
294
  mockResponse
295
295
  );
296
296
  }
@@ -29,7 +29,9 @@ const std = {
29
29
  function normalizeOutput(spy: jest.SpyInstance): string {
30
30
  return normalizeErrorMarkers(
31
31
  replaceByte(
32
- stripTrailingWhitespace(normalizeSlashes(stripTimings(captureCalls(spy))))
32
+ stripTrailingWhitespace(
33
+ normalizeSlashes(normalizeTempDirs(stripTimings(captureCalls(spy))))
34
+ )
33
35
  )
34
36
  );
35
37
  }
@@ -93,5 +95,12 @@ export function stripTrailingWhitespace(str: string): string {
93
95
  * variation causing every few tests the value to change by ± .01
94
96
  */
95
97
  function replaceByte(stdout: string): string {
96
- return stdout.replaceAll(/.[0-9][0-9] KiB/g, "xx KiB");
98
+ return stdout.replaceAll(/\d+\.\d+ KiB/g, "xx KiB");
99
+ }
100
+
101
+ /**
102
+ * Temp directories are created with random names, so we replace all comments temp dirs in them
103
+ */
104
+ export function normalizeTempDirs(stdout: string): string {
105
+ return stdout.replaceAll(/\/\/.+\/tmp.+/g, "//tmpdir");
97
106
  }
@@ -227,16 +227,16 @@ describe("wrangler", () => {
227
227
  await runWrangler("build");
228
228
  await endEventLoop();
229
229
  expect(std.out).toMatchInlineSnapshot(`
230
- "▲ [WARNING] Deprecation: \`wrangler build\` has been deprecated.
230
+ "▲ [WARNING] Deprecation: \`wrangler build\` has been deprecated.
231
231
 
232
- Please refer to https://developers.cloudflare.com/workers/wrangler/migration/deprecations/#build
233
- for more information.
234
- Attempting to run \`wrangler publish --dry-run --outdir=dist\` for you instead:
232
+ Please refer to https://developers.cloudflare.com/workers/wrangler/migration/deprecations/#build
233
+ for more information.
234
+ Attempting to run \`wrangler publish --dry-run --outdir=dist\` for you instead:
235
235
 
236
236
 
237
- --dry-run: exiting now.
238
- Total Upload: 0xx KiB / gzip: 0xx KiB"
239
- `);
237
+ Total Upload: xx KiB / gzip: xx KiB
238
+ --dry-run: exiting now."
239
+ `);
240
240
  });
241
241
  });
242
242
 
@@ -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(