wrangler 2.8.0 → 2.8.1

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 (44) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/d1/d1.test.ts +12 -8
  3. package/src/__tests__/deployments.test.ts +4 -4
  4. package/src/__tests__/helpers/msw/handlers/deployments.ts +10 -18
  5. package/src/__tests__/helpers/msw/handlers/namespaces.ts +18 -41
  6. package/src/__tests__/helpers/msw/handlers/r2.ts +14 -34
  7. package/src/__tests__/helpers/msw/handlers/script.ts +9 -28
  8. package/src/__tests__/helpers/msw/handlers/user.ts +13 -24
  9. package/src/__tests__/helpers/msw/handlers/zones.ts +6 -8
  10. package/src/__tests__/pages.test.ts +34 -37
  11. package/src/__tests__/publish.test.ts +126 -0
  12. package/src/__tests__/r2.test.ts +11 -35
  13. package/src/__tests__/tail.test.ts +6 -18
  14. package/src/__tests__/tsconfig.tsbuildinfo +1 -1
  15. package/src/__tests__/user.test.ts +0 -1
  16. package/src/__tests__/whoami.test.tsx +6 -17
  17. package/src/__tests__/worker-namespace.test.ts +56 -48
  18. package/src/api/index.ts +1 -0
  19. package/src/api/pages/index.ts +5 -0
  20. package/src/api/pages/publish.tsx +321 -0
  21. package/src/bundle.ts +62 -10
  22. package/src/cli.ts +2 -2
  23. package/src/config/environment.ts +12 -10
  24. package/src/d1/utils.ts +1 -1
  25. package/src/deployments.ts +16 -6
  26. package/src/dev/local.tsx +1 -10
  27. package/src/dev/start-server.ts +5 -10
  28. package/src/dev/use-esbuild.ts +1 -0
  29. package/src/entry.ts +1 -2
  30. package/src/index.ts +1 -1
  31. package/src/metrics/send-event.ts +2 -1
  32. package/src/pages/build.ts +4 -124
  33. package/src/pages/buildFunctions.ts +129 -0
  34. package/src/pages/dev.ts +12 -2
  35. package/src/pages/functions/buildPlugin.ts +1 -0
  36. package/src/pages/functions/buildWorker.ts +8 -2
  37. package/src/pages/functions/tsconfig.tsbuildinfo +1 -1
  38. package/src/pages/publish.tsx +9 -235
  39. package/src/publish/publish.ts +1 -0
  40. package/templates/d1-beta-facade.js +1 -1
  41. package/templates/middleware/loader-modules.ts +2 -0
  42. package/templates/tsconfig.tsbuildinfo +1 -1
  43. package/wrangler-dist/cli.d.ts +132 -10
  44. package/wrangler-dist/cli.js +486 -388
@@ -45,7 +45,6 @@ describe("User", () => {
45
45
  counter += 1;
46
46
 
47
47
  return response.once(
48
- context.status(200),
49
48
  context.json({
50
49
  access_token: "test-access-token",
51
50
  expires_in: 100000,
@@ -4,6 +4,7 @@ import { getUserInfo } from "../whoami";
4
4
  import { mockConsoleMethods } from "./helpers/mock-console";
5
5
  import { useMockIsTTY } from "./helpers/mock-istty";
6
6
  import {
7
+ createFetchResult,
7
8
  msw,
8
9
  mswSuccessOauthHandlers,
9
10
  mswSuccessUserHandlers,
@@ -44,18 +45,14 @@ describe("getUserInfo()", () => {
44
45
  msw.use(
45
46
  rest.get("*/user", (_, res, ctx) => {
46
47
  return res.once(
47
- ctx.status(200),
48
- ctx.json({
49
- success: false,
50
- errors: [
48
+ ctx.json(
49
+ createFetchResult({}, false, [
51
50
  {
52
51
  code: 9109,
53
52
  message: "Uauthorized to access requested resource",
54
53
  },
55
- ],
56
- messages: [],
57
- result: {},
58
- })
54
+ ])
55
+ )
59
56
  );
60
57
  }),
61
58
  rest.get("*/accounts", (request, res, ctx) => {
@@ -71,15 +68,7 @@ describe("getUserInfo()", () => {
71
68
  "host": "api.cloudflare.com",
72
69
  }
73
70
  `);
74
- return res.once(
75
- ctx.status(200),
76
- ctx.json({
77
- success: true,
78
- errors: [],
79
- messages: [],
80
- result: [],
81
- })
82
- );
71
+ return res.once(ctx.json(createFetchResult([])));
83
72
  })
84
73
  );
85
74
  const userInfo = await getUserInfo();
@@ -1,7 +1,11 @@
1
1
  import { rest } from "msw";
2
2
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
3
3
  import { mockConsoleMethods } from "./helpers/mock-console";
4
- import { msw, mswSuccessNamespacesHandlers } from "./helpers/msw";
4
+ import {
5
+ createFetchResult,
6
+ msw,
7
+ mswSuccessNamespacesHandlers,
8
+ } from "./helpers/msw";
5
9
  import { runInTempDir } from "./helpers/run-in-tmp";
6
10
  import { runWrangler } from "./helpers/run-wrangler";
7
11
 
@@ -49,22 +53,23 @@ describe("dispatch-namespace", () => {
49
53
  let counter = 0;
50
54
  msw.use(
51
55
  rest.post(
52
- "/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
53
- (req, res, cxt) => {
56
+ "*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
57
+ (req, res, ctx) => {
54
58
  counter++;
55
59
  const { namespaceNameParam } = req.params;
56
60
  expect(counter).toBe(1);
57
61
  expect(namespaceNameParam).toBe(namespaceName);
58
62
  return res.once(
59
- cxt.status(200),
60
- cxt.json({
61
- namespace_id: "some-namespace-id",
62
- namespace_name: "namespace-name",
63
- created_on: "2022-06-29T14:30:08.16152Z",
64
- created_by: "1fc1df98cc4420fe00367c3ab68c1639",
65
- modified_on: "2022-06-29T14:30:08.16152Z",
66
- modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
67
- })
63
+ ctx.json(
64
+ createFetchResult({
65
+ namespace_id: "some-namespace-id",
66
+ namespace_name: "namespace-name",
67
+ created_on: "2022-06-29T14:30:08.16152Z",
68
+ created_by: "1fc1df98cc4420fe00367c3ab68c1639",
69
+ modified_on: "2022-06-29T14:30:08.16152Z",
70
+ modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
71
+ })
72
+ )
68
73
  );
69
74
  }
70
75
  )
@@ -108,13 +113,13 @@ describe("dispatch-namespace", () => {
108
113
  let counter = 0;
109
114
  msw.use(
110
115
  rest.delete(
111
- "/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
112
- (req, res, cxt) => {
116
+ "*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
117
+ (req, res, ctx) => {
113
118
  counter++;
114
119
  const { namespaceNameParam } = req.params;
115
120
  expect(counter).toBe(1);
116
121
  expect(namespaceNameParam).toBe(namespaceName);
117
- return res.once(cxt.status(200), cxt.json(null));
122
+ return res.once(ctx.json(null));
118
123
  }
119
124
  )
120
125
  );
@@ -157,22 +162,23 @@ describe("dispatch-namespace", () => {
157
162
  let counter = 0;
158
163
  msw.use(
159
164
  rest.get(
160
- "/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
161
- (req, res, cxt) => {
165
+ "*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
166
+ (req, res, ctx) => {
162
167
  counter++;
163
168
  const { namespaceNameParam } = req.params;
164
169
  expect(counter).toBe(1);
165
170
  expect(namespaceNameParam).toBe(namespaceName);
166
171
  return res.once(
167
- cxt.status(200),
168
- cxt.json({
169
- namespace_id: "some-namespace-id",
170
- namespace_name: "namespace-name",
171
- created_on: "2022-06-29T14:30:08.16152Z",
172
- created_by: "1fc1df98cc4420fe00367c3ab68c1639",
173
- modified_on: "2022-06-29T14:30:08.16152Z",
174
- modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
175
- })
172
+ ctx.json(
173
+ createFetchResult({
174
+ namespace_id: "some-namespace-id",
175
+ namespace_name: "namespace-name",
176
+ created_on: "2022-06-29T14:30:08.16152Z",
177
+ created_by: "1fc1df98cc4420fe00367c3ab68c1639",
178
+ modified_on: "2022-06-29T14:30:08.16152Z",
179
+ modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
180
+ })
181
+ )
176
182
  );
177
183
  }
178
184
  )
@@ -223,22 +229,23 @@ describe("dispatch-namespace", () => {
223
229
  let counter = 0;
224
230
  msw.use(
225
231
  rest.get(
226
- "/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
227
- (req, res, cxt) => {
232
+ "*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
233
+ (req, res, ctx) => {
228
234
  counter++;
229
235
  const { namespaceNameParam } = req.params;
230
236
  expect(counter).toBe(1);
231
237
  expect(namespaceNameParam).toBe(namespaceName);
232
238
  return res.once(
233
- cxt.status(200),
234
- cxt.json({
235
- namespace_id: "some-namespace-id",
236
- namespace_name: "namespace-name",
237
- created_on: "2022-06-29T14:30:08.16152Z",
238
- created_by: "1fc1df98cc4420fe00367c3ab68c1639",
239
- modified_on: "2022-06-29T14:30:08.16152Z",
240
- modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
241
- })
239
+ ctx.json(
240
+ createFetchResult({
241
+ namespace_id: "some-namespace-id",
242
+ namespace_name: "namespace-name",
243
+ created_on: "2022-06-29T14:30:08.16152Z",
244
+ created_by: "1fc1df98cc4420fe00367c3ab68c1639",
245
+ modified_on: "2022-06-29T14:30:08.16152Z",
246
+ modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
247
+ })
248
+ )
242
249
  );
243
250
  }
244
251
  )
@@ -266,22 +273,23 @@ describe("dispatch-namespace", () => {
266
273
  let counter = 0;
267
274
  msw.use(
268
275
  rest.put(
269
- "/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
270
- (req, res, cxt) => {
276
+ "*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
277
+ (req, res, ctx) => {
271
278
  counter++;
272
279
  const { namespaceNameParam } = req.params;
273
280
  expect(counter).toBe(1);
274
281
  expect(namespaceNameParam).toBe(namespaceName);
275
282
  return res.once(
276
- cxt.status(200),
277
- cxt.json({
278
- namespace_id: "some-namespace-id",
279
- namespace_name: "namespace-name",
280
- created_on: "2022-06-29T14:30:08.16152Z",
281
- created_by: "1fc1df98cc4420fe00367c3ab68c1639",
282
- modified_on: "2022-06-29T14:30:08.16152Z",
283
- modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
284
- })
283
+ ctx.json(
284
+ createFetchResult({
285
+ namespace_id: "some-namespace-id",
286
+ namespace_name: "namespace-name",
287
+ created_on: "2022-06-29T14:30:08.16152Z",
288
+ created_by: "1fc1df98cc4420fe00367c3ab68c1639",
289
+ modified_on: "2022-06-29T14:30:08.16152Z",
290
+ modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
291
+ })
292
+ )
285
293
  );
286
294
  }
287
295
  )
package/src/api/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { unstable_dev } from "./dev";
2
2
  export type { UnstableDevWorker, UnstableDevOptions } from "./dev";
3
+ export { unstable_pages } from "./pages";
@@ -0,0 +1,5 @@
1
+ import { publish } from "./publish";
2
+
3
+ export const unstable_pages = {
4
+ publish,
5
+ };
@@ -0,0 +1,321 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { dirname, join, resolve as resolvePath } from "node:path";
4
+ import { cwd } from "node:process";
5
+ import { File, FormData } from "undici";
6
+ import { fetchResult } from "../../cfetch";
7
+ import { FatalError } from "../../errors";
8
+ import { logger } from "../../logger";
9
+ import { buildFunctions } from "../../pages/buildFunctions";
10
+ import {
11
+ FunctionsNoRoutesError,
12
+ getFunctionsNoRoutesWarning,
13
+ } from "../../pages/errors";
14
+ import {
15
+ buildRawWorker,
16
+ checkRawWorker,
17
+ } from "../../pages/functions/buildWorker";
18
+ import { validateRoutes } from "../../pages/functions/routes-validation";
19
+ import { upload } from "../../pages/upload";
20
+ import type { Project, Deployment } from "@cloudflare/types";
21
+
22
+ interface PagesPublishOptions {
23
+ /**
24
+ * Path to static assets to publish to Pages
25
+ */
26
+ directory: string;
27
+ /**
28
+ * The Cloudflare Account ID that owns the project that's
29
+ * being published
30
+ */
31
+ accountId: string;
32
+ /**
33
+ * The name of the project to be published
34
+ */
35
+ projectName: string;
36
+ /**
37
+ * Branch name to use. Defaults to production branch
38
+ */
39
+ branch?: string;
40
+ /**
41
+ * Whether or not to skip local file upload result caching
42
+ */
43
+ skipCaching?: boolean;
44
+ /**
45
+ * Commit message associated to deployment
46
+ */
47
+ commitMessage?: string;
48
+ /**
49
+ * Commit hash associated to deployment
50
+ */
51
+ commitHash?: string;
52
+ /**
53
+ * Whether or not the deployment should be considered to be
54
+ * in a dirty commit state
55
+ */
56
+ commitDirty?: boolean;
57
+ /**
58
+ * Path to the project's functions directory. Default uses
59
+ * the current working directory + /functions since this is
60
+ * typically called in a CLI
61
+ */
62
+ functionsDirectory?: string;
63
+
64
+ /**
65
+ * Whether to run bundling on `_worker.js` before deploying.
66
+ * Default: false
67
+ */
68
+ bundle?: boolean;
69
+
70
+ // TODO: Allow passing in the API key and plumb it through
71
+ // to the API calls so that the publish function does not
72
+ // rely on the `CLOUDFLARE_API_KEY` environment variable
73
+ }
74
+
75
+ /**
76
+ * Publish a directory to an account/project.
77
+ * NOTE: You will need the `CLOUDFLARE_API_KEY` environment
78
+ * variable set
79
+ */
80
+ export async function publish({
81
+ directory,
82
+ accountId,
83
+ projectName,
84
+ branch,
85
+ skipCaching,
86
+ commitMessage,
87
+ commitHash,
88
+ commitDirty,
89
+ functionsDirectory: customFunctionsDirectory,
90
+ bundle,
91
+ }: PagesPublishOptions) {
92
+ let _headers: string | undefined,
93
+ _redirects: string | undefined,
94
+ _routesGenerated: string | undefined,
95
+ _routesCustom: string | undefined,
96
+ _workerJS: string | undefined;
97
+
98
+ const workerScriptPath = resolvePath(directory, "_worker.js");
99
+
100
+ try {
101
+ _headers = readFileSync(join(directory, "_headers"), "utf-8");
102
+ } catch {}
103
+
104
+ try {
105
+ _redirects = readFileSync(join(directory, "_redirects"), "utf-8");
106
+ } catch {}
107
+
108
+ try {
109
+ /**
110
+ * Developers can specify a custom _routes.json file, for projects with Pages
111
+ * Functions or projects in Advanced Mode
112
+ */
113
+ _routesCustom = readFileSync(join(directory, "_routes.json"), "utf-8");
114
+ } catch {}
115
+
116
+ try {
117
+ _workerJS = readFileSync(workerScriptPath, "utf-8");
118
+ } catch {}
119
+
120
+ // Grab the bindings from the API, we need these for shims and other such hacky inserts
121
+ const project = await fetchResult<Project>(
122
+ `/accounts/${accountId}/pages/projects/${projectName}`
123
+ );
124
+ let isProduction = true;
125
+ if (branch) {
126
+ isProduction = project.production_branch === branch;
127
+ }
128
+
129
+ /**
130
+ * Evaluate if this is an Advanced Mode or Pages Functions project. If Advanced Mode, we'll
131
+ * go ahead and upload `_worker.js` as is, but if Pages Functions, we need to attempt to build
132
+ * Functions first and exit if it failed
133
+ */
134
+ let builtFunctions: string | undefined = undefined;
135
+ const functionsDirectory =
136
+ customFunctionsDirectory || join(cwd(), "functions");
137
+ const routesOutputPath = !existsSync(join(directory, "_routes.json"))
138
+ ? join(tmpdir(), `_routes-${Math.random()}.json`)
139
+ : undefined;
140
+
141
+ // Routing configuration displayed in the Functions tab of a deployment in Dash
142
+ let filepathRoutingConfig: string | undefined;
143
+
144
+ if (!_workerJS && existsSync(functionsDirectory)) {
145
+ const outfile = join(tmpdir(), `./functionsWorker-${Math.random()}.js`);
146
+ const outputConfigPath = join(
147
+ tmpdir(),
148
+ `functions-filepath-routing-config-${Math.random()}.json`
149
+ );
150
+
151
+ try {
152
+ await buildFunctions({
153
+ outfile,
154
+ outputConfigPath,
155
+ functionsDirectory,
156
+ onEnd: () => {},
157
+ buildOutputDirectory: dirname(outfile),
158
+ routesOutputPath,
159
+ local: false,
160
+ d1Databases: Object.keys(
161
+ project.deployment_configs[isProduction ? "production" : "preview"]
162
+ .d1_databases ?? {}
163
+ ),
164
+ });
165
+
166
+ builtFunctions = readFileSync(outfile, "utf-8");
167
+ filepathRoutingConfig = readFileSync(outputConfigPath, "utf-8");
168
+ } catch (e) {
169
+ if (e instanceof FunctionsNoRoutesError) {
170
+ logger.warn(
171
+ getFunctionsNoRoutesWarning(functionsDirectory, "skipping")
172
+ );
173
+ } else {
174
+ throw e;
175
+ }
176
+ }
177
+ }
178
+
179
+ const manifest = await upload({
180
+ directory,
181
+ accountId,
182
+ projectName,
183
+ skipCaching: skipCaching ?? false,
184
+ });
185
+
186
+ const formData = new FormData();
187
+
188
+ formData.append("manifest", JSON.stringify(manifest));
189
+
190
+ if (branch) {
191
+ formData.append("branch", branch);
192
+ }
193
+
194
+ if (commitMessage) {
195
+ formData.append("commit_message", commitMessage);
196
+ }
197
+
198
+ if (commitHash) {
199
+ formData.append("commit_hash", commitHash);
200
+ }
201
+
202
+ if (commitDirty !== undefined) {
203
+ formData.append("commit_dirty", commitDirty);
204
+ }
205
+
206
+ if (_headers) {
207
+ formData.append("_headers", new File([_headers], "_headers"));
208
+ logger.log(`✨ Uploading _headers`);
209
+ }
210
+
211
+ if (_redirects) {
212
+ formData.append("_redirects", new File([_redirects], "_redirects"));
213
+ logger.log(`✨ Uploading _redirects`);
214
+ }
215
+
216
+ if (filepathRoutingConfig) {
217
+ formData.append(
218
+ "functions-filepath-routing-config.json",
219
+ new File(
220
+ [filepathRoutingConfig],
221
+ "functions-filepath-routing-config.json"
222
+ )
223
+ );
224
+ }
225
+
226
+ /**
227
+ * Advanced Mode
228
+ * https://developers.cloudflare.com/pages/platform/functions/#advanced-mode
229
+ *
230
+ * When using a _worker.js file, the entire /functions directory is ignored
231
+ * – this includes its routing and middleware characteristics.
232
+ */
233
+ if (_workerJS) {
234
+ let workerFileContents = _workerJS;
235
+ if (bundle) {
236
+ const outfile = join(tmpdir(), `./bundledWorker-${Math.random()}.mjs`);
237
+ await buildRawWorker({
238
+ workerScriptPath,
239
+ outfile,
240
+ directory: directory ?? ".",
241
+ local: false,
242
+ sourcemap: true,
243
+ watch: false,
244
+ onEnd: () => {},
245
+ });
246
+ workerFileContents = readFileSync(outfile, "utf8");
247
+ } else {
248
+ await checkRawWorker(workerScriptPath, () => {});
249
+ }
250
+
251
+ formData.append("_worker.js", new File([workerFileContents], "_worker.js"));
252
+ logger.log(`✨ Uploading _worker.js`);
253
+
254
+ if (_routesCustom) {
255
+ // user provided a custom _routes.json file
256
+ try {
257
+ const routesCustomJSON = JSON.parse(_routesCustom);
258
+ validateRoutes(routesCustomJSON, join(directory, "_routes.json"));
259
+
260
+ formData.append(
261
+ "_routes.json",
262
+ new File([_routesCustom], "_routes.json")
263
+ );
264
+ logger.log(`✨ Uploading _routes.json`);
265
+ } catch (err) {
266
+ if (err instanceof FatalError) {
267
+ throw err;
268
+ }
269
+ }
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Pages Functions
275
+ * https://developers.cloudflare.com/pages/platform/functions/
276
+ */
277
+ if (builtFunctions && !_workerJS) {
278
+ // if Functions were build successfully, proceed to uploading the build file
279
+ formData.append("_worker.js", new File([builtFunctions], "_worker.js"));
280
+ logger.log(`✨ Uploading Functions`);
281
+
282
+ if (_routesCustom) {
283
+ // user provided a custom _routes.json file
284
+ try {
285
+ const routesCustomJSON = JSON.parse(_routesCustom);
286
+ validateRoutes(routesCustomJSON, join(directory, "_routes.json"));
287
+
288
+ formData.append(
289
+ "_routes.json",
290
+ new File([_routesCustom], "_routes.json")
291
+ );
292
+ logger.log(`✨ Uploading _routes.json`);
293
+ } catch (err) {
294
+ if (err instanceof FatalError) {
295
+ throw err;
296
+ }
297
+ }
298
+ } else if (routesOutputPath) {
299
+ // no custom _routes.json file found, so fallback to the generated one
300
+ try {
301
+ _routesGenerated = readFileSync(routesOutputPath, "utf-8");
302
+
303
+ if (_routesGenerated) {
304
+ formData.append(
305
+ "_routes.json",
306
+ new File([_routesGenerated], "_routes.json")
307
+ );
308
+ }
309
+ } catch {}
310
+ }
311
+ }
312
+
313
+ const deploymentResponse = await fetchResult<Deployment>(
314
+ `/accounts/${accountId}/pages/projects/${projectName}/deployments`,
315
+ {
316
+ method: "POST",
317
+ body: formData,
318
+ }
319
+ );
320
+ return deploymentResponse;
321
+ }
package/src/bundle.ts CHANGED
@@ -9,10 +9,10 @@ import tmp from "tmp-promise";
9
9
  import createModuleCollector from "./module-collection";
10
10
  import { getBasePath, toUrlPath } from "./paths";
11
11
  import type { Config } from "./config";
12
+ import type { DurableObjectBindings } from "./config/environment";
12
13
  import type { WorkerRegistry } from "./dev-registry";
13
14
  import type { Entry } from "./entry";
14
15
  import type { CfModule } from "./worker";
15
-
16
16
  export type BundleResult = {
17
17
  modules: CfModule[];
18
18
  dependencies: esbuild.Metafile["outputs"][string]["inputs"];
@@ -95,6 +95,7 @@ export async function bundleWorker(
95
95
  serveAssetsFromWorker: boolean;
96
96
  assets?: StaticAssetsConfig;
97
97
  betaD1Shims?: string[];
98
+ doBindings: DurableObjectBindings;
98
99
  jsxFactory?: string;
99
100
  jsxFragment?: string;
100
101
  rules: Config["rules"];
@@ -123,6 +124,7 @@ export async function bundleWorker(
123
124
  const {
124
125
  serveAssetsFromWorker,
125
126
  betaD1Shims,
127
+ doBindings,
126
128
  jsxFactory,
127
129
  jsxFragment,
128
130
  rules,
@@ -271,7 +273,13 @@ export async function bundleWorker(
271
273
  Array.isArray(betaD1Shims) &&
272
274
  betaD1Shims.length > 0 &&
273
275
  ((currentEntry: Entry) => {
274
- return applyD1BetaFacade(currentEntry, tmpDir.path, betaD1Shims, local);
276
+ return applyD1BetaFacade(
277
+ currentEntry,
278
+ tmpDir.path,
279
+ betaD1Shims,
280
+ local,
281
+ doBindings
282
+ );
275
283
  }),
276
284
 
277
285
  // Middleware loader: to add middleware, we add the path to the middleware
@@ -395,13 +403,23 @@ export async function bundleWorker(
395
403
  _path.includes(".map")
396
404
  )[0];
397
405
 
406
+ const resolvedEntryPointPath = path.resolve(
407
+ entry.directory,
408
+ entryPointOutputs[0][0]
409
+ );
410
+
411
+ // copy all referenced modules into the output bundle directory
412
+ for (const module of moduleCollector.modules) {
413
+ fs.writeFileSync(
414
+ path.join(path.dirname(resolvedEntryPointPath), module.name),
415
+ module.content
416
+ );
417
+ }
418
+
398
419
  return {
399
420
  modules: moduleCollector.modules,
400
421
  dependencies,
401
- resolvedEntryPointPath: path.resolve(
402
- entry.directory,
403
- entryPointOutputs[0][0]
404
- ),
422
+ resolvedEntryPointPath,
405
423
  bundleType,
406
424
  stop: result.stop,
407
425
  sourceMapPath,
@@ -779,17 +797,51 @@ async function applyFirstPartyWorkerDevFacade(
779
797
  * This code be removed from here when the API is in Workers core,
780
798
  * but moved inside Miniflare for simulating D1.
781
799
  */
782
-
783
800
  async function applyD1BetaFacade(
784
801
  entry: Entry,
785
802
  tmpDirPath: string,
786
803
  betaD1Shims: string[],
787
- local: boolean
804
+ local: boolean,
805
+ doBindings: DurableObjectBindings
788
806
  ): Promise<Entry> {
807
+ let entrypointPath = path.resolve(
808
+ getBasePath(),
809
+ "templates/d1-beta-facade.js"
810
+ );
811
+ if (Array.isArray(doBindings) && doBindings.length > 0) {
812
+ //we have DO bindings, so we need to shim them
813
+ const maskedDoBindings = doBindings
814
+ // Don't shim anything not local to this worker
815
+ .filter((b) => !b.script_name)
816
+ // Reexport the DO classnames
817
+ .map(
818
+ (b) =>
819
+ `export const ${b.class_name} = maskDurableObjectDefinition(OTHER_EXPORTS.${b.class_name});`
820
+ )
821
+ .join("\n");
822
+ const baseFile = fs.readFileSync(
823
+ path.resolve(getBasePath(), "templates/d1-beta-facade.js"),
824
+ "utf8"
825
+ );
826
+ //getMaskedEnv is already used to shim regular Workers
827
+ const contents = `
828
+ ${baseFile}
829
+
830
+ var maskDurableObjectDefinition = (cls) =>
831
+ class extends cls {
832
+ constructor(state, env) {
833
+ super(state, getMaskedEnv(env));
834
+ }
835
+ };
836
+ ${maskedDoBindings}`;
837
+ const doD1FacadePath = path.join(tmpDirPath, "d1-do-facade.js");
838
+ //write our shim so we can build it
839
+ fs.writeFileSync(doD1FacadePath, contents);
840
+ entrypointPath = doD1FacadePath;
841
+ }
789
842
  const targetPath = path.join(tmpDirPath, "d1-beta-facade.entry.js");
790
-
791
843
  await esbuild.build({
792
- entryPoints: [path.resolve(getBasePath(), "templates/d1-beta-facade.js")],
844
+ entryPoints: [entrypointPath],
793
845
  bundle: true,
794
846
  format: "esm",
795
847
  sourcemap: true,
package/src/cli.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import process from "process";
2
2
  import { hideBin } from "yargs/helpers";
3
- import { unstable_dev } from "./api";
3
+ import { unstable_dev, unstable_pages } from "./api";
4
4
  import { FatalError } from "./errors";
5
5
  import { main } from ".";
6
6
 
@@ -24,5 +24,5 @@ if (typeof jest === "undefined" && require.main === module) {
24
24
  * It makes it possible to import wrangler from 'wrangler',
25
25
  * and call wrangler.unstable_dev().
26
26
  */
27
- export { unstable_dev };
27
+ export { unstable_dev, unstable_pages };
28
28
  export type { UnstableDevWorker, UnstableDevOptions };