wrangler 0.0.16 → 0.0.19

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 (66) hide show
  1. package/package.json +15 -8
  2. package/pages/functions/filepath-routing.test.ts +103 -46
  3. package/pages/functions/filepath-routing.ts +47 -88
  4. package/src/__tests__/configuration.test.ts +2186 -0
  5. package/src/__tests__/dev.test.tsx +237 -61
  6. package/src/__tests__/guess-worker-format.test.ts +66 -0
  7. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  8. package/src/__tests__/helpers/mock-account-id.ts +6 -6
  9. package/src/__tests__/helpers/mock-bin.ts +4 -1
  10. package/src/__tests__/helpers/mock-cfetch.ts +2 -2
  11. package/src/__tests__/helpers/mock-console.ts +21 -7
  12. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  13. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  14. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  15. package/src/__tests__/index.test.ts +343 -20
  16. package/src/__tests__/jest.setup.ts +37 -5
  17. package/src/__tests__/kv.test.ts +402 -133
  18. package/src/__tests__/package-manager.test.ts +2 -2
  19. package/src/__tests__/parse.test.ts +322 -0
  20. package/src/__tests__/publish.test.ts +1212 -295
  21. package/src/__tests__/r2.test.ts +198 -0
  22. package/src/__tests__/secret.test.ts +179 -66
  23. package/src/__tests__/sentry.test.ts +146 -0
  24. package/src/__tests__/tail.test.ts +564 -0
  25. package/src/api/form_data.ts +106 -23
  26. package/src/api/preview.ts +36 -17
  27. package/src/api/worker.ts +49 -18
  28. package/src/bundle.ts +133 -0
  29. package/src/cfetch/index.ts +8 -6
  30. package/src/cfetch/internal.ts +49 -17
  31. package/src/cli.ts +10 -0
  32. package/src/config/README.md +107 -0
  33. package/src/config/config.ts +270 -0
  34. package/src/config/diagnostics.ts +80 -0
  35. package/src/config/environment.ts +305 -0
  36. package/src/config/index.ts +52 -0
  37. package/src/config/validation-helpers.ts +370 -0
  38. package/src/config/validation.ts +1019 -0
  39. package/src/dev.tsx +288 -179
  40. package/src/entry.ts +57 -0
  41. package/src/environment-variables.ts +33 -0
  42. package/src/guess-worker-format.ts +68 -0
  43. package/src/index.tsx +855 -702
  44. package/src/inspect.ts +16 -3
  45. package/src/kv.tsx +110 -55
  46. package/src/module-collection.ts +141 -34
  47. package/src/open-in-browser.ts +13 -0
  48. package/src/package-manager.ts +21 -15
  49. package/src/pages.tsx +9 -10
  50. package/src/parse.ts +183 -0
  51. package/src/proxy.ts +11 -5
  52. package/src/publish.ts +272 -319
  53. package/src/r2.ts +50 -0
  54. package/src/reporting.ts +115 -0
  55. package/src/sites.tsx +6 -12
  56. package/src/tail/filters.ts +279 -0
  57. package/src/tail/index.ts +299 -0
  58. package/src/tail/printing.ts +62 -0
  59. package/src/user.tsx +75 -45
  60. package/templates/new-worker.js +1 -1
  61. package/templates/new-worker.ts +1 -1
  62. package/templates/tsconfig.json +105 -0
  63. package/wrangler-dist/cli.js +129039 -114255
  64. package/wrangler-dist/cli.js.map +3 -3
  65. package/src/config.ts +0 -641
  66. package/src/tail.tsx +0 -73
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wrangler",
3
- "version": "0.0.16",
3
+ "version": "0.0.19",
4
4
  "author": "wrangler@cloudflare.com",
5
5
  "description": "Command-line interface for all things Cloudflare Workers",
6
6
  "bin": {
@@ -36,8 +36,8 @@
36
36
  "cli"
37
37
  ],
38
38
  "dependencies": {
39
- "esbuild": "0.14.14",
40
- "miniflare": "2.2.0",
39
+ "esbuild": "0.14.23",
40
+ "miniflare": "2.3.0",
41
41
  "path-to-regexp": "^6.2.0",
42
42
  "semiver": "^1.1.0",
43
43
  "xxhash-wasm": "^1.0.1"
@@ -46,17 +46,19 @@
46
46
  "fsevents": "~2.3.2"
47
47
  },
48
48
  "devDependencies": {
49
- "@babel/types": "^7.16.0",
50
49
  "@iarna/toml": "^2.2.5",
51
- "@types/estree": "^0.0.50",
50
+ "@sentry/cli": "^1.71.0",
51
+ "@sentry/integrations": "^6.17.6",
52
+ "@sentry/node": "^6.17.6",
53
+ "@types/command-exists": "^1.2.0",
54
+ "@types/glob-to-regexp": "0.4.1",
52
55
  "@types/mime": "^2.0.3",
56
+ "@types/prompts": "^2.0.14",
53
57
  "@types/react": "^17.0.37",
54
58
  "@types/serve-static": "^1.13.10",
55
59
  "@types/signal-exit": "^3.0.1",
56
60
  "@types/ws": "^8.2.1",
57
61
  "@types/yargs": "^17.0.7",
58
- "acorn": "^8.6.0",
59
- "acorn-walk": "^8.2.0",
60
62
  "chokidar": "^3.5.2",
61
63
  "clipboardy": "^3.0.0",
62
64
  "cmd-shim": "^4.1.0",
@@ -66,6 +68,8 @@
66
68
  "faye-websocket": "^0.11.4",
67
69
  "finalhandler": "^1.1.2",
68
70
  "find-up": "^6.2.0",
71
+ "get-port": "^6.1.2",
72
+ "glob-to-regexp": "0.4.1",
69
73
  "ignore": "^5.2.0",
70
74
  "ink": "^3.2.0",
71
75
  "ink-select-input": "^4.2.1",
@@ -73,8 +77,10 @@
73
77
  "ink-testing-library": "^2.1.0",
74
78
  "ink-text-input": "^4.0.2",
75
79
  "jest-fetch-mock": "^3.0.3",
80
+ "jest-websocket-mock": "^2.3.0",
76
81
  "mime": "^3.0.0",
77
82
  "open": "^8.4.0",
83
+ "prompts": "^2.4.2",
78
84
  "react": "^17.0.2",
79
85
  "react-error-boundary": "^3.1.4",
80
86
  "serve-static": "^1.14.1",
@@ -99,6 +105,7 @@
99
105
  "check:type": "tsc",
100
106
  "bundle": "node -r esbuild-register scripts/bundle.ts",
101
107
  "build": "npm run clean && npm run bundle",
108
+ "prepublishOnly": "npm run build",
102
109
  "start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
103
110
  "test": "jest --silent=false --verbose=true",
104
111
  "test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"
@@ -111,7 +118,7 @@
111
118
  "testTimeout": 30000,
112
119
  "testRegex": ".*.(test|spec)\\.[jt]sx?$",
113
120
  "transformIgnorePatterns": [
114
- "node_modules/(?!find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)"
121
+ "node_modules/(?!find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream|get-port)"
115
122
  ],
116
123
  "moduleNameMapper": {
117
124
  "clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
@@ -1,60 +1,117 @@
1
+ import { writeFileSync } from "fs";
2
+ import { runInTempDir } from "../../src/__tests__/helpers/run-in-tmp";
1
3
  import { toUrlPath } from "../../src/paths";
2
- import { compareRoutes } from "./filepath-routing";
4
+ import { compareRoutes, generateConfigFromFileTree } from "./filepath-routing";
5
+ import type { UrlPath } from "../../src/paths";
3
6
  import type { HTTPMethod, RouteConfig } from "./routes";
4
7
 
5
- describe("compareRoutes()", () => {
6
- test("routes / last", () => {
7
- expect(
8
- compareRoutes(routeConfig("/"), routeConfig("/foo"))
9
- ).toBeGreaterThanOrEqual(1);
10
- expect(
11
- compareRoutes(routeConfig("/"), routeConfig("/:foo"))
12
- ).toBeGreaterThanOrEqual(1);
13
- expect(
14
- compareRoutes(routeConfig("/"), routeConfig("/:foo*"))
15
- ).toBeGreaterThanOrEqual(1);
16
- });
8
+ describe("filepath-routing", () => {
9
+ describe("compareRoutes()", () => {
10
+ test("routes / last", () => {
11
+ expect(
12
+ compareRoutes(routeConfig("/"), routeConfig("/foo"))
13
+ ).toBeGreaterThanOrEqual(1);
14
+ expect(
15
+ compareRoutes(routeConfig("/"), routeConfig("/:foo"))
16
+ ).toBeGreaterThanOrEqual(1);
17
+ expect(
18
+ compareRoutes(routeConfig("/"), routeConfig("/:foo*"))
19
+ ).toBeGreaterThanOrEqual(1);
20
+ });
17
21
 
18
- test("routes with fewer segments come after those with more segments", () => {
19
- expect(
20
- compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar"))
21
- ).toBeGreaterThanOrEqual(1);
22
- expect(
23
- compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat"))
24
- ).toBeGreaterThanOrEqual(1);
25
- });
22
+ test("routes with fewer segments come after those with more segments", () => {
23
+ expect(
24
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar"))
25
+ ).toBeGreaterThanOrEqual(1);
26
+ expect(
27
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat"))
28
+ ).toBeGreaterThanOrEqual(1);
29
+ });
26
30
 
27
- test("routes with wildcard segments come after those without", () => {
28
- expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1);
29
- expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(1);
30
- });
31
+ test("routes with wildcard segments come after those without", () => {
32
+ expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1);
33
+ expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(
34
+ 1
35
+ );
36
+ });
31
37
 
32
- test("routes with dynamic segments come after those without", () => {
33
- expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
34
- });
38
+ test("routes with dynamic segments come after those without", () => {
39
+ expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
40
+ });
35
41
 
36
- test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
37
- expect(
38
- compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id"))
39
- ).toBe(1);
40
- });
42
+ test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
43
+ expect(
44
+ compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id"))
45
+ ).toBe(1);
46
+ });
41
47
 
42
- test("routes with no HTTP method come after those specifying a method", () => {
43
- expect(compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))).toBe(
44
- 1
45
- );
46
- });
48
+ test("routes with no HTTP method come after those specifying a method", () => {
49
+ expect(
50
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))
51
+ ).toBe(1);
52
+ });
53
+
54
+ test("two equal routes are sorted according to their original position in the list", () => {
55
+ expect(
56
+ compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET"))
57
+ ).toBe(0);
58
+ });
47
59
 
48
- test("two equal routes are sorted according to their original position in the list", () => {
49
- expect(
50
- compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET"))
51
- ).toBe(0);
60
+ test("it returns -1 if the first argument should appear first in the list", () => {
61
+ expect(
62
+ compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))
63
+ ).toBe(-1);
64
+ });
52
65
  });
53
66
 
54
- test("it returns -1 if the first argument should appear first in the list", () => {
55
- expect(compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))).toBe(
56
- -1
57
- );
67
+ describe("generateConfigFromFileTree", () => {
68
+ runInTempDir();
69
+
70
+ it("should generate a route entry for each file in the tree", async () => {
71
+ writeFileSync(
72
+ "foo.ts",
73
+ `
74
+ export function onRequestGet() {}
75
+ export function onRequestPost() {}
76
+ `
77
+ );
78
+ writeFileSync(
79
+ "bar.ts",
80
+ `
81
+ export function onRequestPut() {}
82
+ export function onRequestDelete() {}
83
+ `
84
+ );
85
+
86
+ const entries = await generateConfigFromFileTree({
87
+ baseDir: ".",
88
+ baseURL: "/base" as UrlPath,
89
+ });
90
+ expect(entries).toEqual({
91
+ routes: [
92
+ {
93
+ method: "DELETE",
94
+ module: ["bar.ts:onRequestDelete"],
95
+ routePath: "/base/bar",
96
+ },
97
+ {
98
+ method: "PUT",
99
+ module: ["bar.ts:onRequestPut"],
100
+ routePath: "/base/bar",
101
+ },
102
+ {
103
+ method: "GET",
104
+ module: ["foo.ts:onRequestGet"],
105
+ routePath: "/base/foo",
106
+ },
107
+ {
108
+ method: "POST",
109
+ module: ["foo.ts:onRequestPost"],
110
+ routePath: "/base/foo",
111
+ },
112
+ ],
113
+ });
114
+ });
58
115
  });
59
116
  });
60
117
 
@@ -1,12 +1,9 @@
1
1
  import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
- import * as acorn from "acorn";
4
- import * as acornWalk from "acorn-walk";
5
- import { transform } from "esbuild";
3
+ import { build } from "esbuild";
6
4
  import { toUrlPath } from "../../src/paths";
7
5
  import type { UrlPath } from "../../src/paths";
8
6
  import type { HTTPMethod, RouteConfig } from "./routes";
9
- import type { ExportNamedDeclaration, Identifier } from "estree";
10
7
 
11
8
  export async function generateConfigFromFileTree({
12
9
  baseDir,
@@ -28,106 +25,68 @@ export async function generateConfigFromFileTree({
28
25
  await forEachFile(baseDir, async (filepath) => {
29
26
  const ext = path.extname(filepath);
30
27
  if (/^\.(mjs|js|ts|tsx|jsx)$/.test(ext)) {
31
- // transform the code to ensure we're working with vanilla JS + ESM
32
- const { code } = await transform(await fs.readFile(filepath, "utf-8"), {
33
- loader: ext === ".ts" ? "ts" : "js",
28
+ const { metafile } = await build({
29
+ metafile: true,
30
+ write: false,
31
+ bundle: false,
32
+ entryPoints: [path.resolve(filepath)],
34
33
  });
34
+ const exportNames: string[] = [];
35
+ if (metafile) {
36
+ for (const output in metafile?.outputs) {
37
+ exportNames.push(...metafile.outputs[output].exports);
38
+ }
39
+ }
40
+ for (const exportName of exportNames) {
41
+ const [match, method = ""] = (exportName.match(
42
+ /^onRequest(Get|Post|Put|Patch|Delete|Options|Head)?$/
43
+ ) ?? []) as (string | undefined)[];
35
44
 
36
- // parse each file into an AST and search for module exports that match "onRequest" and friends
37
- const ast = acorn.parse(code, {
38
- ecmaVersion: "latest",
39
- sourceType: "module",
40
- });
41
- acornWalk.simple(ast, {
42
- ExportNamedDeclaration(_node: unknown) {
43
- // This dynamic cast assumes that the AST generated by acornWalk will generate nodes that
44
- // are compatible with the eslint AST nodes.
45
- const node = _node as ExportNamedDeclaration;
46
-
47
- // this is an array because multiple things can be exported from a single statement
48
- // i.e. `export {foo, bar}` or `export const foo = "f", bar = "b"`
49
- const exportNames: string[] = [];
50
-
51
- if (node.declaration) {
52
- const declaration = node.declaration;
53
-
54
- // `export async function onRequest() {...}`
55
- if (declaration.type === "FunctionDeclaration" && declaration.id) {
56
- exportNames.push(declaration.id.name);
57
- }
58
-
59
- // `export const onRequestGet = () => {}, onRequestPost = () => {}`
60
- if (declaration.type === "VariableDeclaration") {
61
- exportNames.push(
62
- ...declaration.declarations.map(
63
- (variableDeclarator) =>
64
- (variableDeclarator.id as unknown as Identifier).name
65
- )
66
- );
67
- }
68
- }
69
-
70
- // `export {foo, bar}`
71
- if (node.specifiers.length) {
72
- exportNames.push(
73
- ...node.specifiers.map(
74
- (exportSpecifier) =>
75
- (exportSpecifier.exported as unknown as Identifier).name
76
- )
77
- );
78
- }
79
-
80
- for (const exportName of exportNames) {
81
- const [match, method = ""] = (exportName.match(
82
- /^onRequest(Get|Post|Put|Patch|Delete|Options|Head)?$/
83
- ) ?? []) as (string | undefined)[];
84
-
85
- if (match) {
86
- const basename = path.basename(filepath).slice(0, -ext.length);
45
+ if (match) {
46
+ const basename = path.basename(filepath).slice(0, -ext.length);
87
47
 
88
- const isIndexFile = basename === "index";
89
- // TODO: deprecate _middleware_ in favor of _middleware
90
- const isMiddlewareFile =
91
- basename === "_middleware" || basename === "_middleware_";
48
+ const isIndexFile = basename === "index";
49
+ // TODO: deprecate _middleware_ in favor of _middleware
50
+ const isMiddlewareFile =
51
+ basename === "_middleware" || basename === "_middleware_";
92
52
 
93
- let routePath = path
94
- .relative(baseDir, filepath)
95
- .slice(0, -ext.length);
53
+ let routePath = path
54
+ .relative(baseDir, filepath)
55
+ .slice(0, -ext.length);
96
56
 
97
- if (isIndexFile || isMiddlewareFile) {
98
- routePath = path.dirname(routePath);
99
- }
57
+ if (isIndexFile || isMiddlewareFile) {
58
+ routePath = path.dirname(routePath);
59
+ }
100
60
 
101
- if (routePath === ".") {
102
- routePath = "";
103
- }
61
+ if (routePath === ".") {
62
+ routePath = "";
63
+ }
104
64
 
105
- routePath = `${baseURL}/${routePath}`;
65
+ routePath = `${baseURL}/${routePath}`;
106
66
 
107
- routePath = routePath.replace(/\[\[(.+)]]/g, ":$1*"); // transform [[id]] => :id*
108
- routePath = routePath.replace(/\[(.+)]/g, ":$1"); // transform [id] => :id
67
+ routePath = routePath.replace(/\[\[(.+)]]/g, ":$1*"); // transform [[id]] => :id*
68
+ routePath = routePath.replace(/\[(.+)]/g, ":$1"); // transform [id] => :id
109
69
 
110
- const routeEntry: RouteConfig = {
111
- routePath: toUrlPath(routePath),
112
- method: method.toUpperCase() as HTTPMethod,
113
- [isMiddlewareFile ? "middleware" : "module"]: [
114
- `${path.relative(baseDir, filepath)}:${exportName}`,
115
- ],
116
- };
70
+ const routeEntry: RouteConfig = {
71
+ routePath: toUrlPath(routePath),
72
+ method: method.toUpperCase() as HTTPMethod,
73
+ [isMiddlewareFile ? "middleware" : "module"]: [
74
+ `${path.relative(baseDir, filepath)}:${exportName}`,
75
+ ],
76
+ };
117
77
 
118
- routeEntries.push(routeEntry);
119
- }
120
- }
121
- },
122
- });
78
+ routeEntries.push(routeEntry);
79
+ }
80
+ }
123
81
  }
124
82
  });
125
-
126
83
  // Combine together any routes (index routes) which contain both a module and a middleware
127
84
  routeEntries = routeEntries.reduce(
128
85
  (acc: typeof routeEntries, { routePath, ...rest }) => {
129
86
  const existingRouteEntry = acc.find(
130
- (routeEntry) => routeEntry.routePath === routePath
87
+ (routeEntry) =>
88
+ routeEntry.routePath === routePath &&
89
+ routeEntry.method === rest.method
131
90
  );
132
91
  if (existingRouteEntry !== undefined) {
133
92
  Object.assign(existingRouteEntry, rest);