wrangler 0.0.0-e95bea2 → 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.
Files changed (93) hide show
  1. package/README.md +25 -14
  2. package/bin/wrangler.js +13 -1
  3. package/miniflare-dist/index.mjs +4886 -0
  4. package/package.json +33 -25
  5. package/pages/functions/buildPlugin.ts +92 -0
  6. package/pages/functions/buildWorker.ts +80 -4
  7. package/pages/functions/filepath-routing.test.ts +136 -25
  8. package/pages/functions/filepath-routing.ts +14 -3
  9. package/pages/functions/routes.ts +6 -1
  10. package/pages/functions/template-plugin.ts +146 -0
  11. package/pages/functions/template-worker.ts +21 -8
  12. package/src/__tests__/config-cache.test.ts +36 -0
  13. package/src/__tests__/configuration.test.ts +1179 -271
  14. package/src/__tests__/dev.test.tsx +382 -25
  15. package/src/__tests__/guess-worker-format.test.ts +28 -1
  16. package/src/__tests__/helpers/mock-account-id.ts +4 -0
  17. package/src/__tests__/helpers/mock-cfetch.ts +5 -1
  18. package/src/__tests__/helpers/mock-console.ts +20 -3
  19. package/src/__tests__/helpers/mock-dialogs.ts +26 -1
  20. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  21. package/src/__tests__/helpers/mock-istty.ts +27 -0
  22. package/src/__tests__/helpers/mock-kv.ts +9 -16
  23. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  24. package/src/__tests__/helpers/run-in-tmp.ts +10 -0
  25. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  26. package/src/__tests__/https-options.test.ts +5 -2
  27. package/src/__tests__/index.test.ts +27 -769
  28. package/src/__tests__/init.test.ts +1516 -0
  29. package/src/__tests__/jest.setup.ts +4 -4
  30. package/src/__tests__/kv.test.ts +172 -126
  31. package/src/__tests__/logger.test.ts +115 -0
  32. package/src/__tests__/pages.test.ts +311 -26
  33. package/src/__tests__/publish.test.ts +2946 -524
  34. package/src/__tests__/r2.test.ts +26 -10
  35. package/src/__tests__/route.test.ts +4 -4
  36. package/src/__tests__/secret.test.ts +55 -34
  37. package/src/__tests__/tail.test.ts +146 -20
  38. package/src/__tests__/user.test.ts +131 -0
  39. package/src/__tests__/whoami.test.tsx +26 -7
  40. package/src/bundle.ts +30 -3
  41. package/src/cfetch/internal.ts +9 -7
  42. package/src/cli.ts +5 -11
  43. package/src/config/README.md +7 -7
  44. package/src/config/config.ts +33 -78
  45. package/src/config/environment.ts +93 -4
  46. package/src/config/index.ts +15 -4
  47. package/src/config/validation-helpers.ts +202 -15
  48. package/src/config/validation.ts +530 -133
  49. package/src/config-cache.ts +45 -0
  50. package/src/create-worker-preview.ts +22 -13
  51. package/src/create-worker-upload-form.ts +32 -9
  52. package/src/dev/dev-vars.ts +36 -0
  53. package/src/dev/dev.tsx +136 -75
  54. package/src/dev/local.tsx +196 -125
  55. package/src/dev/remote.tsx +25 -13
  56. package/src/dev/use-esbuild.ts +23 -5
  57. package/src/dialogs.tsx +16 -6
  58. package/src/entry.ts +119 -11
  59. package/src/environment-variables.ts +3 -1
  60. package/src/errors.ts +11 -0
  61. package/src/https-options.ts +3 -2
  62. package/src/index.tsx +739 -400
  63. package/src/inspect.ts +17 -11
  64. package/src/intl-polyfill.d.ts +139 -0
  65. package/src/{kv/index.tsx → kv.ts} +8 -28
  66. package/src/logger.ts +66 -0
  67. package/src/miniflare-cli/README.md +30 -0
  68. package/src/miniflare-cli/enum-keys.ts +17 -0
  69. package/src/miniflare-cli/index.ts +41 -0
  70. package/src/miniflare-cli/request-context.ts +40 -0
  71. package/src/module-collection.ts +63 -58
  72. package/src/open-in-browser.ts +5 -4
  73. package/src/package-manager.ts +9 -8
  74. package/src/pages.tsx +883 -62
  75. package/src/parse.ts +1 -1
  76. package/src/proxy.ts +32 -21
  77. package/src/publish.ts +276 -161
  78. package/src/sites.tsx +88 -33
  79. package/src/tail/index.ts +2 -2
  80. package/src/tail/printing.ts +13 -8
  81. package/src/update-check.ts +19 -0
  82. package/src/user.tsx +231 -103
  83. package/src/whoami.tsx +2 -1
  84. package/src/worker.ts +15 -6
  85. package/templates/gitignore +171 -0
  86. package/templates/static-asset-facade.js +1 -1
  87. package/wrangler-dist/cli.js +22087 -49340
  88. package/src/__tests__/dev2.test.tsx +0 -152
  89. package/src/__tests__/helpers/mock-user.ts +0 -27
  90. package/src/__tests__/logout.test.ts +0 -50
  91. package/src/__tests__/sentry.test.ts +0 -146
  92. package/src/reporting.ts +0 -131
  93. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,146 @@
1
+ import { match } from "path-to-regexp";
2
+ import type { HTTPMethod } from "./routes";
3
+
4
+ /* TODO: Grab these from @cloudflare/workers-types instead */
5
+ type Params<P extends string = string> = Record<P, string | string[]>;
6
+
7
+ type EventContext<Env, P extends string, Data> = {
8
+ request: Request;
9
+ functionPath: string;
10
+ waitUntil: (promise: Promise<unknown>) => void;
11
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
12
+ env: Env & { ASSETS: { fetch: typeof fetch } };
13
+ params: Params<P>;
14
+ data: Data;
15
+ };
16
+
17
+ type EventPluginContext<Env, P extends string, Data, PluginArgs> = {
18
+ request: Request;
19
+ functionPath: string;
20
+ waitUntil: (promise: Promise<unknown>) => void;
21
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
22
+ env: Env & { ASSETS: { fetch: typeof fetch } };
23
+ params: Params<P>;
24
+ data: Data;
25
+ pluginArgs: PluginArgs;
26
+ };
27
+
28
+ declare type PagesFunction<
29
+ Env = unknown,
30
+ P extends string = string,
31
+ Data extends Record<string, unknown> = Record<string, unknown>
32
+ > = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
33
+
34
+ declare type PagesPluginFunction<
35
+ Env = unknown,
36
+ P extends string = string,
37
+ Data extends Record<string, unknown> = Record<string, unknown>,
38
+ PluginArgs = unknown
39
+ > = (
40
+ context: EventPluginContext<Env, P, Data, PluginArgs>
41
+ ) => Response | Promise<Response>;
42
+ /* end @cloudflare/workers-types */
43
+
44
+ type RouteHandler = {
45
+ routePath: string;
46
+ mountPath: string;
47
+ method?: HTTPMethod;
48
+ modules: PagesFunction[];
49
+ middlewares: PagesFunction[];
50
+ };
51
+
52
+ // inject `routes` via ESBuild
53
+ declare const routes: RouteHandler[];
54
+
55
+ function* executeRequest(request: Request, relativePathname: string) {
56
+ // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
57
+ for (const route of [...routes].reverse()) {
58
+ if (route.method && route.method !== request.method) {
59
+ continue;
60
+ }
61
+
62
+ const routeMatcher = match(route.routePath, { end: false });
63
+ const mountMatcher = match(route.mountPath, { end: false });
64
+ const matchResult = routeMatcher(relativePathname);
65
+ const mountMatchResult = mountMatcher(relativePathname);
66
+ if (matchResult && mountMatchResult) {
67
+ for (const handler of route.middlewares.flat()) {
68
+ yield {
69
+ handler,
70
+ params: matchResult.params as Params,
71
+ path: mountMatchResult.path,
72
+ };
73
+ }
74
+ }
75
+ }
76
+
77
+ // Then look for the first exact route match and execute its "modules"
78
+ for (const route of routes) {
79
+ if (route.method && route.method !== request.method) {
80
+ continue;
81
+ }
82
+
83
+ const routeMatcher = match(route.routePath, { end: true });
84
+ const mountMatcher = match(route.mountPath, { end: false });
85
+ const matchResult = routeMatcher(relativePathname);
86
+ const mountMatchResult = mountMatcher(relativePathname);
87
+ if (matchResult && mountMatchResult && route.modules.length) {
88
+ for (const handler of route.modules.flat()) {
89
+ yield {
90
+ handler,
91
+ params: matchResult.params as Params,
92
+ path: matchResult.path,
93
+ };
94
+ }
95
+ break;
96
+ }
97
+ }
98
+ }
99
+
100
+ export default function (pluginArgs) {
101
+ const onRequest: PagesPluginFunction = async (workerContext) => {
102
+ let { request } = workerContext;
103
+ const { env, next, data } = workerContext;
104
+
105
+ const url = new URL(request.url);
106
+ const relativePathname =
107
+ url.pathname.split(workerContext.functionPath)[1] || "/";
108
+
109
+ const handlerIterator = executeRequest(request, relativePathname);
110
+ const pluginNext = async (input?: RequestInfo, init?: RequestInit) => {
111
+ if (input !== undefined) {
112
+ request = new Request(input, init);
113
+ }
114
+
115
+ const result = handlerIterator.next();
116
+ // Note we can't use `!result.done` because this doesn't narrow to the correct type
117
+ if (result.done === false) {
118
+ const { handler, params, path } = result.value;
119
+ const context = {
120
+ request,
121
+ functionPath: workerContext.functionPath + path,
122
+ next: pluginNext,
123
+ params,
124
+ data,
125
+ pluginArgs,
126
+ env,
127
+ waitUntil: workerContext.waitUntil.bind(workerContext),
128
+ };
129
+
130
+ const response = await handler(context);
131
+
132
+ // https://fetch.spec.whatwg.org/#null-body-status
133
+ return new Response(
134
+ [101, 204, 205, 304].includes(response.status) ? null : response.body,
135
+ { ...response, headers: new Headers(response.headers) }
136
+ );
137
+ } else {
138
+ return next();
139
+ }
140
+ };
141
+
142
+ return pluginNext();
143
+ };
144
+
145
+ return onRequest;
146
+ }
@@ -6,6 +6,7 @@ type Params<P extends string = string> = Record<P, string | string[]>;
6
6
 
7
7
  type EventContext<Env, P extends string, Data> = {
8
8
  request: Request;
9
+ functionPath: string;
9
10
  waitUntil: (promise: Promise<unknown>) => void;
10
11
  next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
11
12
  env: Env & { ASSETS: { fetch: typeof fetch } };
@@ -22,6 +23,7 @@ declare type PagesFunction<
22
23
 
23
24
  type RouteHandler = {
24
25
  routePath: string;
26
+ mountPath: string;
25
27
  method?: HTTPMethod;
26
28
  modules: PagesFunction[];
27
29
  middlewares: PagesFunction[];
@@ -42,7 +44,7 @@ type WorkerContext = {
42
44
  waitUntil: (promise: Promise<unknown>) => void;
43
45
  };
44
46
 
45
- function* executeRequest(request: Request, _env: FetchEnv) {
47
+ function* executeRequest(request: Request) {
46
48
  const requestPath = new URL(request.url).pathname;
47
49
 
48
50
  // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
@@ -52,12 +54,15 @@ function* executeRequest(request: Request, _env: FetchEnv) {
52
54
  }
53
55
 
54
56
  const routeMatcher = match(route.routePath, { end: false });
57
+ const mountMatcher = match(route.mountPath, { end: false });
55
58
  const matchResult = routeMatcher(requestPath);
56
- if (matchResult) {
59
+ const mountMatchResult = mountMatcher(requestPath);
60
+ if (matchResult && mountMatchResult) {
57
61
  for (const handler of route.middlewares.flat()) {
58
62
  yield {
59
63
  handler,
60
64
  params: matchResult.params as Params,
65
+ path: mountMatchResult.path,
61
66
  };
62
67
  }
63
68
  }
@@ -70,12 +75,15 @@ function* executeRequest(request: Request, _env: FetchEnv) {
70
75
  }
71
76
 
72
77
  const routeMatcher = match(route.routePath, { end: true });
78
+ const mountMatcher = match(route.mountPath, { end: false });
73
79
  const matchResult = routeMatcher(requestPath);
74
- if (matchResult && route.modules.length) {
80
+ const mountMatchResult = mountMatcher(requestPath);
81
+ if (matchResult && mountMatchResult && route.modules.length) {
75
82
  for (const handler of route.modules.flat()) {
76
83
  yield {
77
84
  handler,
78
85
  params: matchResult.params as Params,
86
+ path: matchResult.path,
79
87
  };
80
88
  }
81
89
  break;
@@ -85,19 +93,24 @@ function* executeRequest(request: Request, _env: FetchEnv) {
85
93
 
86
94
  export default {
87
95
  async fetch(request: Request, env: FetchEnv, workerContext: WorkerContext) {
88
- const handlerIterator = executeRequest(request, env);
96
+ const handlerIterator = executeRequest(request);
89
97
  const data = {}; // arbitrary data the user can set between functions
90
98
  const next = async (input?: RequestInfo, init?: RequestInit) => {
91
99
  if (input !== undefined) {
92
- request = new Request(input, init);
100
+ let url = input;
101
+ if (typeof input === "string") {
102
+ url = new URL(input, request.url).toString();
103
+ }
104
+ request = new Request(url, init);
93
105
  }
94
106
 
95
107
  const result = handlerIterator.next();
96
108
  // Note we can't use `!result.done` because this doesn't narrow to the correct type
97
- if (result.done == false) {
98
- const { handler, params } = result.value;
109
+ if (result.done === false) {
110
+ const { handler, params, path } = result.value;
99
111
  const context = {
100
112
  request: new Request(request.clone()),
113
+ functionPath: path,
101
114
  next,
102
115
  params,
103
116
  data,
@@ -110,7 +123,7 @@ export default {
110
123
  // https://fetch.spec.whatwg.org/#null-body-status
111
124
  return new Response(
112
125
  [101, 204, 205, 304].includes(response.status) ? null : response.body,
113
- response
126
+ { ...response, headers: new Headers(response.headers) }
114
127
  );
115
128
  } else if (__FALLBACK_SERVICE__) {
116
129
  // There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
@@ -0,0 +1,36 @@
1
+ import { getConfigCache, saveToConfigCache } from "../config-cache";
2
+ import { runInTempDir } from "./helpers/run-in-tmp";
3
+
4
+ interface PagesConfigCache {
5
+ account_id: string;
6
+ pages_project_name: string;
7
+ }
8
+
9
+ describe("config cache", () => {
10
+ runInTempDir();
11
+
12
+ const pagesConfigCacheFilename = "pages-config-cache.json";
13
+
14
+ it("should return an empty config if no file exists", () => {
15
+ expect(
16
+ getConfigCache<PagesConfigCache>(pagesConfigCacheFilename)
17
+ ).toMatchInlineSnapshot(`Object {}`);
18
+ });
19
+
20
+ it("should read and write values without overriding old ones", () => {
21
+ saveToConfigCache<PagesConfigCache>(pagesConfigCacheFilename, {
22
+ account_id: "some-account-id",
23
+ pages_project_name: "foo",
24
+ });
25
+ expect(
26
+ getConfigCache<PagesConfigCache>(pagesConfigCacheFilename).account_id
27
+ ).toEqual("some-account-id");
28
+
29
+ saveToConfigCache<PagesConfigCache>(pagesConfigCacheFilename, {
30
+ pages_project_name: "bar",
31
+ });
32
+ expect(
33
+ getConfigCache<PagesConfigCache>(pagesConfigCacheFilename).account_id
34
+ ).toEqual("some-account-id");
35
+ });
36
+ });