wrangler 0.0.0-c18fbaf5 → 0.0.0-c2b2dfb8
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/bin/wrangler.js +9 -1
- package/miniflare-dist/index.mjs +36 -37
- package/package.json +4 -2
- package/src/__tests__/api-dev.test.ts +1 -1
- package/src/__tests__/api-devregistry.test.js +2 -2
- package/src/__tests__/d1.test.ts +46 -3
- package/src/__tests__/deployments.test.ts +20 -6
- package/src/__tests__/dev.test.tsx +52 -19
- package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
- package/src/__tests__/helpers/mock-cfetch.ts +1 -57
- package/src/__tests__/helpers/mock-oauth-flow.ts +64 -49
- package/src/__tests__/helpers/mock-process.ts +8 -13
- package/src/__tests__/index.test.ts +46 -42
- package/src/__tests__/init.test.ts +277 -158
- package/src/__tests__/jest.setup.ts +8 -7
- package/src/__tests__/kv.test.ts +250 -139
- package/src/__tests__/logout.test.ts +1 -1
- package/src/__tests__/metrics.test.ts +88 -43
- package/src/__tests__/pages.test.ts +1 -0
- package/src/__tests__/publish.test.ts +43 -0
- package/src/__tests__/r2.test.ts +2 -2
- package/src/__tests__/secret.test.ts +151 -101
- package/src/__tests__/tsconfig-sanity.ts +12 -0
- package/src/__tests__/tsconfig.json +8 -0
- package/src/__tests__/tsconfig.tsbuildinfo +1 -0
- package/src/api/dev.ts +1 -1
- package/src/cfetch/index.ts +0 -2
- package/src/cfetch/internal.ts +16 -18
- package/src/config/index.ts +2 -1
- package/src/d1/migrations/apply.tsx +20 -8
- package/src/deployments.ts +6 -0
- package/src/deprecated/index.ts +2 -295
- package/src/dev/dev.tsx +2 -2
- package/src/dev/local.tsx +46 -22
- package/src/dev/start-server.ts +17 -17
- package/src/dev/use-esbuild.ts +1 -1
- package/src/dev.tsx +33 -11
- package/src/dispatch-namespace.ts +1 -1
- package/src/docs/index.ts +97 -0
- package/src/environment-variables/factory.ts +88 -0
- package/src/environment-variables/misc-variables.ts +30 -0
- package/src/generate/index.ts +300 -0
- package/src/index.tsx +16 -10
- package/src/init.ts +46 -34
- package/src/jest.d.ts +4 -0
- package/src/logger.ts +2 -2
- package/src/metrics/metrics-config.ts +1 -1
- package/src/metrics/send-event.ts +2 -1
- package/src/miniflare-cli/assets.ts +15 -7
- package/src/miniflare-cli/index.ts +1 -5
- package/src/miniflare-cli/tsconfig.json +9 -0
- package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
- package/src/miniflare-cli/types.ts +11 -0
- package/src/pages/dev.tsx +1 -1
- package/src/pages/functions/tsconfig.json +8 -0
- package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
- package/src/pages/publish.tsx +11 -1
- package/src/pages/upload.tsx +27 -6
- package/src/pubsub/pubsub-commands.tsx +1 -1
- package/src/tsconfig-sanity.ts +16 -0
- package/src/user/access.ts +0 -1
- package/src/user/auth-variables.ts +113 -0
- package/src/user/choose-account.tsx +1 -1
- package/src/user/index.ts +0 -1
- package/src/user/user.tsx +78 -38
- package/templates/__tests__/tsconfig-sanity.ts +12 -0
- package/templates/__tests__/tsconfig.json +8 -0
- package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
- package/templates/facade.d.ts +14 -0
- package/templates/first-party-worker-module-facade.ts +4 -3
- package/templates/format-dev-errors.ts +7 -6
- package/templates/middleware/loader-modules.ts +0 -2
- package/templates/middleware/loader-sw.ts +6 -0
- package/templates/pages-dev-pipeline.ts +4 -1
- package/templates/pages-shim.ts +4 -1
- package/templates/pages-template-plugin.ts +1 -1
- package/templates/serve-static-assets.ts +16 -14
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +106 -0
- package/templates/tsconfig.json +5 -103
- package/templates/tsconfig.tsbuildinfo +1 -0
- package/wrangler-dist/cli.js +1579 -1327
- package/src/environment-variables.ts +0 -50
- package/src/user/env-vars.ts +0 -46
package/bin/wrangler.js
CHANGED
|
@@ -93,7 +93,15 @@ function runDelegatedWrangler() {
|
|
|
93
93
|
} = JSON.parse(fs.readFileSync(packageJsonPath));
|
|
94
94
|
const resolvedBinaryPath = path.resolve(packageJsonPath, "..", binaryPath);
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
// Make sure the user knows we're delegating to a different installation
|
|
97
|
+
const currentPackageJsonPath = path.resolve(__dirname, "..", "package.json");
|
|
98
|
+
const currentPackage = JSON.parse(fs.readFileSync(currentPackageJsonPath));
|
|
99
|
+
const argv = process.argv.slice(2).join(" ");
|
|
100
|
+
console.log(
|
|
101
|
+
`Delegating to locally-installed wrangler@${version} over global wrangler@${currentPackage.version}...
|
|
102
|
+
Run \`npx wrangler ${argv}\` to use the local version directly.
|
|
103
|
+
`
|
|
104
|
+
);
|
|
97
105
|
|
|
98
106
|
// this call to `spawn` is simpler because the delegated version will do all
|
|
99
107
|
// of the other work.
|
package/miniflare-dist/index.mjs
CHANGED
|
@@ -130,41 +130,43 @@ var init_environment_polyfills = __esm({
|
|
|
130
130
|
|
|
131
131
|
// ../pages-shared/environment-polyfills/miniflare-tre.ts
|
|
132
132
|
var miniflare_tre_exports = {};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
Response as MiniflareResponse
|
|
138
|
-
} from "@miniflare/tre";
|
|
133
|
+
__export(miniflare_tre_exports, {
|
|
134
|
+
default: () => miniflare_tre_default
|
|
135
|
+
});
|
|
136
|
+
var miniflare_tre_default;
|
|
139
137
|
var init_miniflare_tre = __esm({
|
|
140
138
|
"../pages-shared/environment-polyfills/miniflare-tre.ts"() {
|
|
141
139
|
init_environment_polyfills();
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
140
|
+
miniflare_tre_default = async () => {
|
|
141
|
+
const mf = await import("@miniflare/tre");
|
|
142
|
+
polyfill({
|
|
143
|
+
fetch: mf.fetch,
|
|
144
|
+
Headers: mf.Headers,
|
|
145
|
+
Request: mf.Request,
|
|
146
|
+
Response: mf.Response
|
|
147
|
+
});
|
|
148
|
+
};
|
|
148
149
|
}
|
|
149
150
|
});
|
|
150
151
|
|
|
151
152
|
// ../pages-shared/environment-polyfills/miniflare.ts
|
|
152
153
|
var miniflare_exports = {};
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
Response as MiniflareResponse2
|
|
158
|
-
} from "@miniflare/core";
|
|
154
|
+
__export(miniflare_exports, {
|
|
155
|
+
default: () => miniflare_default
|
|
156
|
+
});
|
|
157
|
+
var miniflare_default;
|
|
159
158
|
var init_miniflare = __esm({
|
|
160
159
|
"../pages-shared/environment-polyfills/miniflare.ts"() {
|
|
161
160
|
init_environment_polyfills();
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
161
|
+
miniflare_default = async () => {
|
|
162
|
+
const mf = await import("@miniflare/core");
|
|
163
|
+
polyfill({
|
|
164
|
+
fetch: mf.fetch,
|
|
165
|
+
Headers: mf.Headers,
|
|
166
|
+
Request: mf.Request,
|
|
167
|
+
Response: mf.Response
|
|
168
|
+
});
|
|
169
|
+
};
|
|
168
170
|
}
|
|
169
171
|
});
|
|
170
172
|
|
|
@@ -786,8 +788,8 @@ import {
|
|
|
786
788
|
Log as MiniflareLog,
|
|
787
789
|
LogLevel as MiniflareLogLevel,
|
|
788
790
|
Miniflare,
|
|
789
|
-
Request as
|
|
790
|
-
Response as
|
|
791
|
+
Request as MiniflareRequest,
|
|
792
|
+
Response as MiniflareResponse
|
|
791
793
|
} from "miniflare";
|
|
792
794
|
|
|
793
795
|
// ../../node_modules/yargs/lib/platform-shims/esm.mjs
|
|
@@ -6066,11 +6068,8 @@ async function generateASSETSBinding(options) {
|
|
|
6066
6068
|
};
|
|
6067
6069
|
}
|
|
6068
6070
|
async function generateAssetsFetch(directory, log, tre) {
|
|
6069
|
-
|
|
6070
|
-
|
|
6071
|
-
} else {
|
|
6072
|
-
await Promise.resolve().then(() => (init_miniflare(), miniflare_exports));
|
|
6073
|
-
}
|
|
6071
|
+
const polyfill2 = tre ? (await Promise.resolve().then(() => (init_miniflare_tre(), miniflare_tre_exports))).default : (await Promise.resolve().then(() => (init_miniflare(), miniflare_exports))).default;
|
|
6072
|
+
await polyfill2();
|
|
6074
6073
|
const miniflare = tre ? await import("@miniflare/tre") : await import("@miniflare/core");
|
|
6075
6074
|
const Request = miniflare.Request;
|
|
6076
6075
|
const { generateHandler: generateHandler2, parseQualityWeightedList: parseQualityWeightedList2 } = await Promise.resolve().then(() => (init_handler(), handler_exports));
|
|
@@ -6234,12 +6233,12 @@ async function main() {
|
|
|
6234
6233
|
namespace.get = (id) => {
|
|
6235
6234
|
const stub = new DurableObjectStub(factory, id);
|
|
6236
6235
|
stub.fetch = (...reqArgs) => {
|
|
6237
|
-
const requestFromArgs = new
|
|
6236
|
+
const requestFromArgs = new MiniflareRequest(...reqArgs);
|
|
6238
6237
|
const url = new URL(requestFromArgs.url);
|
|
6239
6238
|
url.host = host;
|
|
6240
6239
|
if (port !== void 0)
|
|
6241
6240
|
url.port = port.toString();
|
|
6242
|
-
const request = new
|
|
6241
|
+
const request = new MiniflareRequest(
|
|
6243
6242
|
url.toString(),
|
|
6244
6243
|
requestFromArgs
|
|
6245
6244
|
);
|
|
@@ -6299,7 +6298,7 @@ async function main() {
|
|
|
6299
6298
|
}`,
|
|
6300
6299
|
serviceBindings: {
|
|
6301
6300
|
DO: async (request) => {
|
|
6302
|
-
request = new
|
|
6301
|
+
request = new MiniflareRequest(request);
|
|
6303
6302
|
const name = request.headers.get("x-miniflare-durable-object-name");
|
|
6304
6303
|
const idString = request.headers.get(
|
|
6305
6304
|
"x-miniflare-durable-object-id"
|
|
@@ -6307,7 +6306,7 @@ async function main() {
|
|
|
6307
6306
|
request.headers.delete("x-miniflare-durable-object-name");
|
|
6308
6307
|
request.headers.delete("x-miniflare-durable-object-id");
|
|
6309
6308
|
if (!name || !idString) {
|
|
6310
|
-
return new
|
|
6309
|
+
return new MiniflareResponse(
|
|
6311
6310
|
"[durable-object-proxy-err] Missing `x-miniflare-durable-object-name` or `x-miniflare-durable-object-id` headers.",
|
|
6312
6311
|
{ status: 400 }
|
|
6313
6312
|
);
|
|
@@ -6315,14 +6314,14 @@ async function main() {
|
|
|
6315
6314
|
const namespace = await mf?.getDurableObjectNamespace(name);
|
|
6316
6315
|
const id = namespace?.idFromString(idString);
|
|
6317
6316
|
if (!id) {
|
|
6318
|
-
return new
|
|
6317
|
+
return new MiniflareResponse(
|
|
6319
6318
|
"[durable-object-proxy-err] Could not generate an ID. Possibly due to a mismatched DO name and ID?",
|
|
6320
6319
|
{ status: 500 }
|
|
6321
6320
|
);
|
|
6322
6321
|
}
|
|
6323
6322
|
const stub = namespace?.get(id);
|
|
6324
6323
|
if (!stub) {
|
|
6325
|
-
return new
|
|
6324
|
+
return new MiniflareResponse(
|
|
6326
6325
|
"[durable-object-proxy-err] Could not generate a stub. Possibly due to a mismatched DO name and ID?",
|
|
6327
6326
|
{ status: 500 }
|
|
6328
6327
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrangler",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-c2b2dfb8",
|
|
4
4
|
"description": "Command-line interface for all things Cloudflare Workers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"wrangler",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"assert-git-version": "node -r esbuild-register scripts/assert-git-version.ts",
|
|
58
58
|
"build": "npm run clean && npm run bundle && npm run emit-types",
|
|
59
59
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
60
|
-
"check:type": "tsc",
|
|
60
|
+
"check:type": "node -r esbuild-register scripts/tsc-all.ts",
|
|
61
61
|
"clean": "rimraf wrangler-dist miniflare-dist emitted-types",
|
|
62
62
|
"dev": "npm run clean && concurrently -c black,blue --kill-others-on-fail false 'npm run bundle -- --watch' 'npm run check:type -- --watch --preserveWatchOutput'",
|
|
63
63
|
"emit-types": "tsc -p tsconfig.emit.json && node -r esbuild-register scripts/emit-types.ts",
|
|
@@ -115,6 +115,7 @@
|
|
|
115
115
|
},
|
|
116
116
|
"devDependencies": {
|
|
117
117
|
"@cloudflare/types": "^6.18.4",
|
|
118
|
+
"@cloudflare/workers-types": "^4.20221111.1",
|
|
118
119
|
"@databases/split-sql-query": "^1.0.3",
|
|
119
120
|
"@databases/sql": "^3.2.0",
|
|
120
121
|
"@iarna/toml": "^3.0.0",
|
|
@@ -136,6 +137,7 @@
|
|
|
136
137
|
"@types/yargs": "^17.0.10",
|
|
137
138
|
"@webcontainer/env": "^1.0.1",
|
|
138
139
|
"body-parser": "^1.20.0",
|
|
140
|
+
"chalk": "^2.4.2",
|
|
139
141
|
"clipboardy": "^3.0.0",
|
|
140
142
|
"cmd-shim": "^4.1.0",
|
|
141
143
|
"command-exists": "^1.2.9",
|
|
@@ -5,7 +5,7 @@ import { runInTempDir } from "./helpers/run-in-tmp";
|
|
|
5
5
|
|
|
6
6
|
jest.unmock("undici");
|
|
7
7
|
|
|
8
|
-
describe
|
|
8
|
+
describe("unstable_dev", () => {
|
|
9
9
|
it("should return Hello World", async () => {
|
|
10
10
|
const worker = await unstable_dev(
|
|
11
11
|
"src/__tests__/helpers/worker-scripts/hello-world-worker.js",
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { unstable_dev } from "../api";
|
|
2
2
|
import { fetch } from "undici";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
jest.unmock("undici");
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* a huge caveat to how testing multi-worker scripts works:
|
|
8
8
|
* you can't shutdown the first worker you spun up, or it'll kill the devRegistry
|
|
9
9
|
*/
|
|
10
|
-
describe
|
|
10
|
+
describe("multi-worker testing", () => {
|
|
11
11
|
let childWorker;
|
|
12
12
|
let parentWorker;
|
|
13
13
|
|
package/src/__tests__/d1.test.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cwd } from "process";
|
|
2
2
|
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
3
|
+
import { useMockIsTTY } from "./helpers/mock-istty";
|
|
4
|
+
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
3
5
|
import { runWrangler } from "./helpers/run-wrangler";
|
|
6
|
+
import writeWranglerToml from "./helpers/write-wrangler-toml";
|
|
4
7
|
|
|
5
8
|
function endEventLoop() {
|
|
6
9
|
return new Promise((resolve) => setImmediate(resolve));
|
|
@@ -8,8 +11,8 @@ function endEventLoop() {
|
|
|
8
11
|
|
|
9
12
|
describe("d1", () => {
|
|
10
13
|
const std = mockConsoleMethods();
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
runInTempDir();
|
|
15
|
+
const { setIsTTY } = useMockIsTTY();
|
|
13
16
|
|
|
14
17
|
it("should show help when no argument is passed", async () => {
|
|
15
18
|
await runWrangler("d1");
|
|
@@ -77,4 +80,44 @@ describe("d1", () => {
|
|
|
77
80
|
To give feedback, visit https://discord.gg/cloudflaredev"
|
|
78
81
|
`);
|
|
79
82
|
});
|
|
83
|
+
|
|
84
|
+
describe("migrate", () => {
|
|
85
|
+
describe("apply", () => {
|
|
86
|
+
it("should not attempt to login in local mode", async () => {
|
|
87
|
+
setIsTTY(false);
|
|
88
|
+
writeWranglerToml({
|
|
89
|
+
d1_databases: [
|
|
90
|
+
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
// If we get to the point where we are checking for migrations then we have not been asked to log in.
|
|
94
|
+
await expect(
|
|
95
|
+
runWrangler("d1 migrations apply --local DATABASE")
|
|
96
|
+
).rejects.toThrowError(
|
|
97
|
+
`No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.`
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should try to read D1 config from wrangler.toml", async () => {
|
|
102
|
+
setIsTTY(false);
|
|
103
|
+
writeWranglerToml();
|
|
104
|
+
await expect(
|
|
105
|
+
runWrangler("d1 migrations apply DATABASE")
|
|
106
|
+
).rejects.toThrowError(
|
|
107
|
+
"Can't find a DB with name/binding 'DATABASE' in local config. Check info in wrangler.toml..."
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should not try to read wrangler.toml in local mode", async () => {
|
|
112
|
+
setIsTTY(false);
|
|
113
|
+
writeWranglerToml();
|
|
114
|
+
// If we get to the point where we are checking for migrations then we have not checked wrangler.toml.
|
|
115
|
+
await expect(
|
|
116
|
+
runWrangler("d1 migrations apply --local DATABASE")
|
|
117
|
+
).rejects.toThrowError(
|
|
118
|
+
`No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.`
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
|
80
123
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// import * as fs from "fs";
|
|
2
2
|
// import * as TOML from "@iarna/toml";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as TOML from "@iarna/toml";
|
|
3
5
|
import { rest } from "msw";
|
|
4
6
|
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
|
|
5
7
|
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
@@ -26,11 +28,7 @@ describe("deployments", () => {
|
|
|
26
28
|
...mswSuccessUserHandlers,
|
|
27
29
|
rest.get(
|
|
28
30
|
"*/accounts/:accountId/workers/services/:scriptName",
|
|
29
|
-
(
|
|
30
|
-
expect(["undefined", "somethingElse"]).toContain(
|
|
31
|
-
request.params.scriptName
|
|
32
|
-
);
|
|
33
|
-
|
|
31
|
+
(_, response, context) => {
|
|
34
32
|
return response.once(
|
|
35
33
|
context.status(200),
|
|
36
34
|
context.json({
|
|
@@ -52,6 +50,16 @@ describe("deployments", () => {
|
|
|
52
50
|
});
|
|
53
51
|
|
|
54
52
|
it("should log deployments", async () => {
|
|
53
|
+
fs.writeFileSync(
|
|
54
|
+
"./wrangler.toml",
|
|
55
|
+
TOML.stringify({
|
|
56
|
+
compatibility_date: "2022-01-12",
|
|
57
|
+
name: "test-script-name",
|
|
58
|
+
first_party_worker: true,
|
|
59
|
+
}),
|
|
60
|
+
"utf-8"
|
|
61
|
+
);
|
|
62
|
+
|
|
55
63
|
await runWrangler("deployments");
|
|
56
64
|
expect(std.out).toMatchInlineSnapshot(`
|
|
57
65
|
"🚧\`wrangler deployments\` is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
|
|
@@ -71,7 +79,7 @@ describe("deployments", () => {
|
|
|
71
79
|
});
|
|
72
80
|
|
|
73
81
|
it("should log deployments for script with passed in name option", async () => {
|
|
74
|
-
await runWrangler("deployments --name
|
|
82
|
+
await runWrangler("deployments --name something-else");
|
|
75
83
|
expect(std.out).toMatchInlineSnapshot(`
|
|
76
84
|
"🚧\`wrangler deployments\` is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
|
|
77
85
|
|
|
@@ -88,4 +96,10 @@ describe("deployments", () => {
|
|
|
88
96
|
🟩 Active"
|
|
89
97
|
`);
|
|
90
98
|
});
|
|
99
|
+
|
|
100
|
+
it("should error on missing script name", async () => {
|
|
101
|
+
await expect(runWrangler("deployments")).rejects.toMatchInlineSnapshot(
|
|
102
|
+
`[Error: Required Worker name missing. Please specify the Worker name in wrangler.toml, or pass it as an argument with \`--name\`]`
|
|
103
|
+
);
|
|
104
|
+
});
|
|
91
105
|
});
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import getPort from "get-port";
|
|
3
|
+
import { rest } from "msw";
|
|
3
4
|
import patchConsole from "patch-console";
|
|
4
5
|
import dedent from "ts-dedent";
|
|
5
6
|
import Dev from "../dev/dev";
|
|
6
7
|
import { CI } from "../is-ci";
|
|
7
8
|
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
|
|
8
|
-
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
|
|
9
9
|
import { mockConsoleMethods } from "./helpers/mock-console";
|
|
10
10
|
import {
|
|
11
11
|
msw,
|
|
@@ -33,7 +33,7 @@ describe("wrangler dev", () => {
|
|
|
33
33
|
afterEach(() => {
|
|
34
34
|
(Dev as jest.Mock).mockClear();
|
|
35
35
|
patchConsole(() => {});
|
|
36
|
-
|
|
36
|
+
msw.resetHandlers();
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
describe("authorization", () => {
|
|
@@ -439,16 +439,44 @@ describe("wrangler dev", () => {
|
|
|
439
439
|
main: "index.js",
|
|
440
440
|
});
|
|
441
441
|
fs.writeFileSync("index.js", `export default {};`);
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
442
|
+
|
|
443
|
+
msw.use(
|
|
444
|
+
rest.get("*/zones", (req, res, ctx) => {
|
|
445
|
+
let zone: [] | [{ id: "some-zone-id" }] = [];
|
|
446
|
+
if (
|
|
447
|
+
req.url.searchParams.get("name") === "111.222.333.some-host.com"
|
|
448
|
+
) {
|
|
449
|
+
zone = [];
|
|
450
|
+
} else if (
|
|
451
|
+
req.url.searchParams.get("name") === "222.333.some-host.com"
|
|
452
|
+
) {
|
|
453
|
+
zone = [];
|
|
454
|
+
} else if (req.url.searchParams.get("name") === "333.some-host.com") {
|
|
455
|
+
zone = [{ id: "some-zone-id" }];
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
return res(
|
|
459
|
+
ctx.status(200),
|
|
460
|
+
ctx.json({
|
|
461
|
+
success: true,
|
|
462
|
+
errors: [],
|
|
463
|
+
messages: [],
|
|
464
|
+
result: zone,
|
|
465
|
+
})
|
|
466
|
+
);
|
|
450
467
|
})
|
|
451
468
|
);
|
|
469
|
+
|
|
470
|
+
await runWrangler("dev --host 111.222.333.some-host.com");
|
|
471
|
+
|
|
472
|
+
const devMockCall = (Dev as jest.Mock).mock.calls[0][0];
|
|
473
|
+
|
|
474
|
+
expect(devMockCall).toHaveProperty("host", "111.222.333.some-host.com");
|
|
475
|
+
expect(devMockCall).toHaveProperty(
|
|
476
|
+
"localUpstream",
|
|
477
|
+
"111.222.333.some-host.com"
|
|
478
|
+
);
|
|
479
|
+
expect(devMockCall).toHaveProperty("zone", "some-zone-id");
|
|
452
480
|
});
|
|
453
481
|
|
|
454
482
|
it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
|
|
@@ -1540,14 +1568,19 @@ describe("wrangler dev", () => {
|
|
|
1540
1568
|
});
|
|
1541
1569
|
|
|
1542
1570
|
function mockGetZones(domain: string, zones: { id: string }[] = []) {
|
|
1543
|
-
|
|
1544
|
-
"
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1571
|
+
msw.use(
|
|
1572
|
+
rest.get("*/zones", (req, res, ctx) => {
|
|
1573
|
+
expect([...req.url.searchParams.entries()]).toEqual([["name", domain]]);
|
|
1574
|
+
|
|
1575
|
+
return res(
|
|
1576
|
+
ctx.status(200),
|
|
1577
|
+
ctx.json({
|
|
1578
|
+
success: true,
|
|
1579
|
+
errors: [],
|
|
1580
|
+
messages: [],
|
|
1581
|
+
result: zones,
|
|
1582
|
+
})
|
|
1583
|
+
);
|
|
1584
|
+
})
|
|
1552
1585
|
);
|
|
1553
1586
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const ORIGINAL_WRANGLER_AUTH_DOMAIN = process.env.WRANGLER_AUTH_DOMAIN;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mock the Auth URL domain so that we can control where we attempt to login.
|
|
5
|
+
*
|
|
6
|
+
* Note that you can remove any API token from the environment by setting the value to `null`.
|
|
7
|
+
* This is useful if a higher `describe()` block has already called `mockAuthDomain()`.
|
|
8
|
+
*/
|
|
9
|
+
export function mockAuthDomain({ domain }: { domain: string | null }) {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
if (domain === null) {
|
|
12
|
+
delete process.env.WRANGLER_AUTH_DOMAIN;
|
|
13
|
+
} else {
|
|
14
|
+
process.env.WRANGLER_AUTH_DOMAIN = domain;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
process.env.WRANGLER_AUTH_DOMAIN = ORIGINAL_WRANGLER_AUTH_DOMAIN;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -2,7 +2,7 @@ import { Readable } from "node:stream";
|
|
|
2
2
|
import { URL, URLSearchParams } from "node:url";
|
|
3
3
|
import { pathToRegexp } from "path-to-regexp";
|
|
4
4
|
import { Response } from "undici";
|
|
5
|
-
import { getCloudflareApiBaseUrl } from "../../
|
|
5
|
+
import { getCloudflareApiBaseUrl } from "../../environment-variables/misc-variables";
|
|
6
6
|
import type { FetchResult, FetchError } from "../../cfetch";
|
|
7
7
|
import type {
|
|
8
8
|
fetchInternal,
|
|
@@ -219,31 +219,6 @@ const kvGetMocks = new Map<string, string | Buffer>();
|
|
|
219
219
|
const r2GetMocks = new Map<string, string | undefined>();
|
|
220
220
|
const dashScriptMocks = new Map<string, string | undefined>();
|
|
221
221
|
|
|
222
|
-
/**
|
|
223
|
-
* @mocked typeof fetchKVGetValue
|
|
224
|
-
*/
|
|
225
|
-
export function mockFetchKVGetValue(
|
|
226
|
-
accountId: string,
|
|
227
|
-
namespaceId: string,
|
|
228
|
-
key: string
|
|
229
|
-
) {
|
|
230
|
-
const mapKey = `${accountId}/${namespaceId}/${key}`;
|
|
231
|
-
if (kvGetMocks.has(mapKey)) {
|
|
232
|
-
const value = kvGetMocks.get(mapKey);
|
|
233
|
-
if (value !== undefined) return Promise.resolve(value);
|
|
234
|
-
}
|
|
235
|
-
throw new Error(`no mock value found for \`kv:key get\` - ${mapKey}`);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export function setMockFetchKVGetValue(
|
|
239
|
-
accountId: string,
|
|
240
|
-
namespaceId: string,
|
|
241
|
-
key: string,
|
|
242
|
-
value: string | Buffer
|
|
243
|
-
) {
|
|
244
|
-
kvGetMocks.set(`${accountId}/${namespaceId}/${key}`, value);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
222
|
/**
|
|
248
223
|
* @mocked typeof fetchR2Objects
|
|
249
224
|
*/
|
|
@@ -300,34 +275,3 @@ export function unsetSpecialMockFns() {
|
|
|
300
275
|
r2GetMocks.clear();
|
|
301
276
|
dashScriptMocks.clear();
|
|
302
277
|
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* @mocked typeof fetchDashScript
|
|
306
|
-
* multipart/form-data is the response for modules and raw text for the Script endpoint.
|
|
307
|
-
*/
|
|
308
|
-
export async function mockFetchDashScript(resource: string): Promise<string> {
|
|
309
|
-
if (dashScriptMocks.has(resource)) {
|
|
310
|
-
return dashScriptMocks.get(resource) ?? "";
|
|
311
|
-
}
|
|
312
|
-
throw new Error(`no mock found for \`init from-dash\` - ${resource}`);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Mock setter for usage within test blocks, companion helper to `mockFetchDashScript`
|
|
317
|
-
*/
|
|
318
|
-
export function setMockFetchDashScript({
|
|
319
|
-
accountId,
|
|
320
|
-
fromDashScriptName,
|
|
321
|
-
environment,
|
|
322
|
-
mockResponse,
|
|
323
|
-
}: {
|
|
324
|
-
accountId: string;
|
|
325
|
-
fromDashScriptName: string;
|
|
326
|
-
environment: string;
|
|
327
|
-
mockResponse?: string;
|
|
328
|
-
}) {
|
|
329
|
-
dashScriptMocks.set(
|
|
330
|
-
`/accounts/${accountId}/workers/services/${fromDashScriptName}/environments/${environment}/content`,
|
|
331
|
-
mockResponse
|
|
332
|
-
);
|
|
333
|
-
}
|