wrangler 2.7.1 → 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.
- package/package.json +1 -1
- package/src/__tests__/d1/d1.test.ts +12 -8
- package/src/__tests__/deployments.test.ts +4 -4
- package/src/__tests__/helpers/mock-dialogs.ts +2 -0
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -5
- package/src/__tests__/helpers/mock-known-routes.ts +7 -2
- package/src/__tests__/helpers/mock-kv.ts +29 -16
- package/src/__tests__/helpers/mock-oauth-flow.ts +90 -98
- package/src/__tests__/helpers/msw/handlers/deployments.ts +20 -10
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +18 -41
- package/src/__tests__/helpers/msw/handlers/r2.ts +14 -34
- package/src/__tests__/helpers/msw/handlers/script.ts +9 -28
- package/src/__tests__/helpers/msw/handlers/user.ts +13 -24
- package/src/__tests__/helpers/msw/handlers/zones.ts +6 -8
- package/src/__tests__/helpers/msw/index.ts +30 -1
- package/src/__tests__/init.test.ts +3 -14
- package/src/__tests__/jest.setup.ts +0 -23
- package/src/__tests__/pages-deployment-tail.test.ts +72 -1
- package/src/__tests__/pages.test.ts +52 -53
- package/src/__tests__/publish.test.ts +870 -522
- package/src/__tests__/r2.test.ts +11 -35
- package/src/__tests__/secret.test.ts +1 -9
- package/src/__tests__/tail.test.ts +72 -19
- package/src/__tests__/tsconfig.tsbuildinfo +1 -1
- package/src/__tests__/user.test.ts +5 -16
- package/src/__tests__/whoami.test.tsx +6 -17
- package/src/__tests__/worker-namespace.test.ts +56 -48
- package/src/api/index.ts +1 -0
- package/src/api/pages/index.ts +5 -0
- package/src/api/pages/publish.tsx +321 -0
- package/src/bundle.ts +62 -10
- package/src/cfetch/internal.ts +0 -3
- package/src/cli.ts +2 -2
- package/src/config/environment.ts +12 -10
- package/src/d1/backups.tsx +1 -5
- package/src/d1/utils.ts +1 -1
- package/src/deployments.ts +16 -6
- package/src/dev/local.tsx +1 -10
- package/src/dev/remote.tsx +2 -0
- package/src/dev/start-server.ts +5 -10
- package/src/dev/use-esbuild.ts +1 -0
- package/src/docs/index.ts +2 -1
- package/src/entry.ts +1 -2
- package/src/index.ts +1 -1
- package/src/init.ts +1 -1
- package/src/metrics/send-event.ts +2 -1
- package/src/pages/build.ts +4 -124
- package/src/pages/buildFunctions.ts +129 -0
- package/src/pages/dev.ts +68 -63
- package/src/pages/functions/buildPlugin.ts +3 -20
- package/src/pages/functions/buildWorker.ts +143 -21
- package/src/pages/functions/tsconfig.tsbuildinfo +1 -1
- package/src/pages/publish.tsx +21 -220
- package/src/publish/publish.ts +30 -4
- package/src/tail/createTail.ts +28 -1
- package/src/tail/printing.ts +15 -0
- package/templates/checked-fetch.js +1 -3
- package/templates/d1-beta-facade.js +1 -1
- package/templates/middleware/loader-modules.ts +2 -0
- package/templates/tsconfig.tsbuildinfo +1 -1
- package/wrangler-dist/cli.d.ts +132 -10
- package/wrangler-dist/cli.js +3532 -3330
- package/src/__tests__/helpers/mock-cfetch.ts +0 -278
|
@@ -8,7 +8,10 @@ import {
|
|
|
8
8
|
} from "../user";
|
|
9
9
|
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
10
10
|
import { useMockIsTTY } from "./helpers/mock-istty";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
mockExchangeRefreshTokenForAccessToken,
|
|
13
|
+
mockOAuthFlow,
|
|
14
|
+
} from "./helpers/mock-oauth-flow";
|
|
12
15
|
import {
|
|
13
16
|
msw,
|
|
14
17
|
mswSuccessOauthHandlers,
|
|
@@ -42,7 +45,6 @@ describe("User", () => {
|
|
|
42
45
|
counter += 1;
|
|
43
46
|
|
|
44
47
|
return response.once(
|
|
45
|
-
context.status(200),
|
|
46
48
|
context.json({
|
|
47
49
|
access_token: "test-access-token",
|
|
48
50
|
expires_in: 100000,
|
|
@@ -77,19 +79,7 @@ describe("User", () => {
|
|
|
77
79
|
oauth_token: "hunter2",
|
|
78
80
|
refresh_token: "Order 66",
|
|
79
81
|
});
|
|
80
|
-
|
|
81
|
-
let counter = 0;
|
|
82
|
-
msw.use(
|
|
83
|
-
rest.post("*/oauth2/token", async (request, response, context) => {
|
|
84
|
-
counter += 1;
|
|
85
|
-
return response.once(
|
|
86
|
-
context.status(400),
|
|
87
|
-
context.body(
|
|
88
|
-
`<html> <body> This shouldn't be sent, but should be handled </body> </html>`
|
|
89
|
-
)
|
|
90
|
-
);
|
|
91
|
-
})
|
|
92
|
-
);
|
|
82
|
+
mockExchangeRefreshTokenForAccessToken({ respondWith: "refreshError" });
|
|
93
83
|
|
|
94
84
|
// Handles the requireAuth error throw from failed login that is unhandled due to directly calling it here
|
|
95
85
|
await expect(
|
|
@@ -97,7 +87,6 @@ describe("User", () => {
|
|
|
97
87
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
98
88
|
`"In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work. Please go to https://developers.cloudflare.com/api/tokens/create/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN."`
|
|
99
89
|
);
|
|
100
|
-
expect(counter).toBe(0);
|
|
101
90
|
});
|
|
102
91
|
|
|
103
92
|
it("should confirm no error message when refresh is successful", async () => {
|
|
@@ -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.
|
|
48
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
-
"
|
|
53
|
-
(req, res,
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
"
|
|
112
|
-
(req, res,
|
|
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(
|
|
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
|
-
"
|
|
161
|
-
(req, res,
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
"
|
|
227
|
-
(req, res,
|
|
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
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
"
|
|
270
|
-
(req, res,
|
|
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
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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
|
@@ -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
|
+
}
|