wrangler 0.0.0-e178d6fb → 0.0.0-e1c2f5b9
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/README.md +16 -2
- package/bin/wrangler.js +31 -11
- package/miniflare-dist/index.mjs +1340 -330
- package/package.json +40 -13
- package/src/__tests__/access.test.ts +25 -0
- package/src/__tests__/api-dev.test.ts +238 -0
- package/src/__tests__/api-devregistry.test.js +64 -0
- package/src/__tests__/api.test.ts +80 -0
- package/src/__tests__/configuration.test.ts +683 -470
- package/src/__tests__/d1.test.ts +123 -0
- package/src/__tests__/delete.test.ts +263 -0
- package/src/__tests__/deployments.test.ts +105 -0
- package/src/__tests__/dev.test.tsx +453 -70
- package/src/__tests__/dialogs.test.tsx +40 -0
- package/src/__tests__/generate.test.ts +235 -0
- package/src/__tests__/get-host-from-url.test.ts +16 -0
- package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
- package/src/__tests__/helpers/mock-cfetch.ts +92 -26
- package/src/__tests__/helpers/mock-console.ts +11 -2
- package/src/__tests__/helpers/mock-dialogs.ts +1 -1
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
- package/src/__tests__/helpers/mock-known-routes.ts +7 -0
- package/src/__tests__/helpers/mock-kv.ts +1 -1
- package/src/__tests__/helpers/mock-oauth-flow.ts +74 -53
- package/src/__tests__/helpers/mock-process.ts +8 -13
- package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
- package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
- package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
- package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
- package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
- package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
- package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
- package/src/__tests__/helpers/msw/index.ts +21 -0
- package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
- package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
- package/src/__tests__/index.test.ts +103 -97
- package/src/__tests__/init.test.ts +1484 -382
- package/src/__tests__/jest.setup.ts +88 -5
- package/src/__tests__/kv.test.ts +655 -540
- package/src/__tests__/logout.test.ts +47 -0
- package/src/__tests__/metrics.test.ts +95 -45
- package/src/__tests__/middleware.scheduled.test.ts +145 -0
- package/src/__tests__/middleware.test.ts +750 -0
- package/src/__tests__/pages-deployment-tail.test.ts +884 -0
- package/src/__tests__/pages.test.ts +1301 -119
- package/src/__tests__/paths.test.ts +39 -0
- package/src/__tests__/publish.test.ts +2266 -1180
- package/src/__tests__/pubsub.test.ts +211 -88
- package/src/__tests__/queues.test.ts +522 -0
- package/src/__tests__/r2.test.ts +363 -203
- package/src/__tests__/secret.test.ts +261 -59
- package/src/__tests__/tail.test.ts +352 -151
- package/src/__tests__/test-old-node-version.js +3 -3
- package/src/__tests__/tsconfig-sanity.ts +12 -0
- package/src/__tests__/tsconfig.json +8 -0
- package/src/__tests__/tsconfig.tsbuildinfo +1 -0
- package/src/__tests__/type-generation.test.ts +233 -0
- package/src/__tests__/user.test.ts +59 -73
- package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
- package/src/__tests__/validate-dev-props.test.ts +56 -0
- package/src/__tests__/whoami.test.tsx +114 -66
- package/src/__tests__/worker-namespace.test.ts +154 -155
- package/src/api/dev.ts +170 -38
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +41 -2
- package/src/bundle.ts +668 -81
- package/src/cfetch/index.ts +40 -3
- package/src/cfetch/internal.ts +144 -32
- package/src/cli.ts +4 -3
- package/src/config/config.ts +27 -3
- package/src/config/environment.ts +131 -7
- package/src/config/index.ts +128 -8
- package/src/config/validation-helpers.ts +19 -6
- package/src/config/validation.ts +411 -14
- package/src/config-cache.ts +3 -2
- package/src/create-worker-preview.ts +31 -6
- package/src/create-worker-upload-form.ts +84 -22
- package/src/d1/backups.tsx +223 -0
- package/src/d1/constants.ts +2 -0
- package/src/d1/create.tsx +63 -0
- package/src/d1/delete.tsx +56 -0
- package/src/d1/execute.tsx +352 -0
- package/src/d1/formatTimeAgo.ts +14 -0
- package/src/d1/index.ts +101 -0
- package/src/d1/list.tsx +48 -0
- package/src/d1/migrations/apply.tsx +196 -0
- package/src/d1/migrations/create.tsx +77 -0
- package/src/d1/migrations/helpers.ts +147 -0
- package/src/d1/migrations/index.tsx +3 -0
- package/src/d1/migrations/list.tsx +79 -0
- package/src/d1/migrations/options.ts +18 -0
- package/src/d1/options.ts +22 -0
- package/src/d1/types.tsx +24 -0
- package/src/d1/utils.ts +48 -0
- package/src/delete.ts +108 -0
- package/src/deployments.ts +83 -0
- package/src/deprecated/index.ts +145 -0
- package/src/dev/dev-vars.ts +11 -8
- package/src/dev/dev.tsx +220 -58
- package/src/dev/get-local-persistence-path.tsx +31 -0
- package/src/dev/local.tsx +740 -181
- package/src/dev/remote.tsx +340 -123
- package/src/dev/start-server.ts +543 -0
- package/src/dev/use-esbuild.ts +61 -7
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +201 -0
- package/src/dev.tsx +614 -258
- package/src/dialogs.tsx +41 -8
- package/src/{worker-namespace.ts → dispatch-namespace.ts} +23 -22
- package/src/docs/index.ts +97 -0
- package/src/entry.ts +3 -2
- 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/git-client.ts +100 -7
- package/src/index.tsx +249 -1560
- package/src/init.ts +626 -150
- package/src/inspect.ts +137 -32
- package/src/{metrics/is-ci.ts → is-ci.ts} +0 -0
- package/src/is-interactive.ts +4 -0
- package/src/jest.d.ts +4 -0
- package/src/{kv.ts → kv/helpers.ts} +3 -3
- package/src/kv/index.ts +596 -0
- package/src/logger.ts +27 -8
- package/src/metrics/index.ts +1 -0
- package/src/metrics/metrics-config.ts +4 -4
- package/src/metrics/metrics-dispatcher.ts +1 -0
- package/src/metrics/metrics-usage-headers.ts +24 -0
- package/src/metrics/send-event.ts +13 -8
- package/src/miniflare-cli/assets.ts +172 -465
- package/src/miniflare-cli/index.ts +149 -24
- 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/module-collection.ts +12 -3
- package/src/pages/build.tsx +108 -42
- package/src/pages/constants.ts +11 -0
- package/src/pages/deployment-tails.tsx +284 -0
- package/src/pages/deployments.tsx +9 -31
- package/src/pages/dev.tsx +417 -108
- package/src/pages/errors.ts +66 -0
- package/src/pages/functions/buildPlugin.ts +86 -84
- package/src/pages/functions/buildWorker.ts +121 -106
- package/src/pages/functions/routes-consolidation.test.ts +250 -0
- package/src/pages/functions/routes-consolidation.ts +73 -0
- package/src/pages/functions/routes-transformation.test.ts +282 -0
- package/src/pages/functions/routes-transformation.ts +115 -0
- package/src/pages/functions/routes-validation.test.ts +403 -0
- package/src/pages/functions/routes-validation.ts +202 -0
- package/src/pages/functions/tsconfig.json +8 -0
- package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
- package/src/pages/functions.tsx +82 -0
- package/src/pages/hash.tsx +13 -0
- package/src/pages/index.tsx +73 -55
- package/src/pages/projects.tsx +9 -3
- package/src/pages/prompt-select-project.tsx +31 -0
- package/src/pages/publish.tsx +183 -60
- package/src/pages/types.ts +1 -0
- package/src/pages/upload.tsx +107 -56
- package/src/pages/utils.ts +13 -2
- package/src/paths.ts +49 -4
- package/src/proxy.ts +375 -157
- package/src/publish/index.ts +270 -0
- package/src/{publish.ts → publish/publish.ts} +431 -188
- package/src/pubsub/pubsub-commands.tsx +4 -3
- package/src/queues/cli/commands/consumer/add.ts +71 -0
- package/src/queues/cli/commands/consumer/index.ts +22 -0
- package/src/queues/cli/commands/consumer/remove.ts +38 -0
- package/src/queues/cli/commands/create.ts +25 -0
- package/src/queues/cli/commands/delete.ts +26 -0
- package/src/queues/cli/commands/index.ts +36 -0
- package/src/queues/cli/commands/list.ts +25 -0
- package/src/queues/client.ts +135 -0
- package/src/queues/utils.ts +18 -0
- package/src/r2/constants.ts +4 -0
- package/src/r2/helpers.ts +131 -0
- package/src/r2/index.ts +289 -0
- package/src/routes.ts +140 -0
- package/src/secret/index.ts +379 -0
- package/src/sites.tsx +3 -3
- package/src/tail/createTail.ts +379 -0
- package/src/tail/filters.ts +4 -6
- package/src/tail/index.ts +199 -284
- package/src/tail/printing.ts +43 -3
- package/src/tsconfig-sanity.ts +16 -0
- package/src/type-generation.ts +181 -0
- package/src/user/access.ts +68 -0
- 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 +116 -49
- package/src/utils/collectKeyValues.ts +14 -0
- package/src/whoami.tsx +80 -2
- package/src/worker.ts +69 -9
- package/src/yargs-types.ts +24 -0
- package/src/zones.ts +124 -9
- package/templates/__tests__/pages-dev-util.test.ts +128 -0
- package/templates/__tests__/tsconfig-sanity.ts +12 -0
- package/templates/__tests__/tsconfig.json +8 -0
- package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
- package/templates/d1-beta-facade.js +205 -0
- package/templates/facade.d.ts +14 -0
- package/templates/first-party-worker-module-facade.ts +19 -0
- package/templates/format-dev-errors.ts +33 -0
- package/templates/init-tests/test-jest-new-worker.js +23 -0
- package/templates/init-tests/test-vitest-new-worker.js +24 -0
- package/templates/init-tests/test-vitest-new-worker.ts +25 -0
- package/templates/middleware/common.ts +62 -0
- package/templates/middleware/loader-modules.ts +82 -0
- package/templates/middleware/loader-sw.ts +233 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +15 -0
- package/templates/new-worker.js +1 -1
- package/templates/pages-dev-pipeline.ts +31 -0
- package/templates/pages-dev-util.ts +52 -0
- package/templates/pages-shim.ts +4 -1
- package/templates/pages-template-plugin.ts +21 -5
- package/templates/pages-template-worker.ts +37 -9
- package/templates/{static-asset-facade.js → serve-static-assets.ts} +23 -7
- package/templates/service-bindings-module-facade.js +54 -0
- package/templates/service-bindings-sw-facade.js +42 -0
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +106 -0
- package/templates/tsconfig.json +5 -102
- package/templates/tsconfig.tsbuildinfo +1 -0
- package/wrangler-dist/cli.d.ts +621 -42
- package/wrangler-dist/cli.js +144441 -102606
- package/src/environment-variables.ts +0 -35
- package/src/faye-websocket.d.ts +0 -6
- package/src/generate.ts +0 -33
- package/src/miniflare-cli/enum-keys.ts +0 -17
- package/src/preview.ts +0 -31
- package/src/r2.ts +0 -50
- package/src/user/env-vars.ts +0 -46
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import { findUpSync } from "find-up";
|
|
3
|
+
import { getEntry } from "./entry";
|
|
4
|
+
import { logger } from "./logger";
|
|
5
|
+
import type { Config } from "./config";
|
|
6
|
+
|
|
7
|
+
// Currently includes bindings & rules for declaring modules
|
|
8
|
+
|
|
9
|
+
export async function generateTypes(
|
|
10
|
+
configToDTS: Partial<Config>,
|
|
11
|
+
config: Config
|
|
12
|
+
) {
|
|
13
|
+
const entry = await getEntry({}, config, "types");
|
|
14
|
+
const envTypeStructure: string[] = [];
|
|
15
|
+
|
|
16
|
+
if (configToDTS.kv_namespaces) {
|
|
17
|
+
for (const kvNamespace of configToDTS.kv_namespaces) {
|
|
18
|
+
envTypeStructure.push(`${kvNamespace.binding}: KVNamespace;`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (configToDTS.vars) {
|
|
23
|
+
for (const varName in configToDTS.vars) {
|
|
24
|
+
const varValue = configToDTS.vars[varName];
|
|
25
|
+
if (
|
|
26
|
+
typeof varValue === "string" ||
|
|
27
|
+
typeof varValue === "number" ||
|
|
28
|
+
typeof varValue === "boolean"
|
|
29
|
+
) {
|
|
30
|
+
envTypeStructure.push(`${varName}: "${varValue}";`);
|
|
31
|
+
}
|
|
32
|
+
if (typeof varValue === "object" && varValue !== null) {
|
|
33
|
+
envTypeStructure.push(`${varName}: ${JSON.stringify(varValue)};`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (configToDTS.durable_objects?.bindings) {
|
|
39
|
+
for (const durableObject of configToDTS.durable_objects.bindings) {
|
|
40
|
+
envTypeStructure.push(`${durableObject.name}: DurableObjectNamespace;`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (configToDTS.r2_buckets) {
|
|
45
|
+
for (const R2Bucket of configToDTS.r2_buckets) {
|
|
46
|
+
envTypeStructure.push(`${R2Bucket.binding}: R2Bucket;`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (configToDTS.d1_databases) {
|
|
51
|
+
for (const d1 of configToDTS.d1_databases) {
|
|
52
|
+
envTypeStructure.push(`${d1.binding}: D1Database;`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (configToDTS.services) {
|
|
57
|
+
for (const service of configToDTS.services) {
|
|
58
|
+
envTypeStructure.push(`${service.binding}: Fetcher;`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (configToDTS.analytics_engine_datasets) {
|
|
63
|
+
for (const analyticsEngine of configToDTS.analytics_engine_datasets) {
|
|
64
|
+
envTypeStructure.push(
|
|
65
|
+
`${analyticsEngine.binding}: AnalyticsEngineDataset;`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (configToDTS.dispatch_namespaces) {
|
|
71
|
+
for (const namespace of configToDTS.dispatch_namespaces) {
|
|
72
|
+
envTypeStructure.push(`${namespace.binding}: any;`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (configToDTS.logfwdr?.schema) {
|
|
77
|
+
envTypeStructure.push(`LOGFWDR_SCHEMA: any;`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (configToDTS.data_blobs) {
|
|
81
|
+
for (const dataBlobs in configToDTS.data_blobs) {
|
|
82
|
+
envTypeStructure.push(`${dataBlobs}: ArrayBuffer;`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (configToDTS.text_blobs) {
|
|
87
|
+
for (const textBlobs in configToDTS.text_blobs) {
|
|
88
|
+
envTypeStructure.push(`${textBlobs}: string;`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (configToDTS.unsafe) {
|
|
93
|
+
for (const unsafe of configToDTS.unsafe.bindings) {
|
|
94
|
+
envTypeStructure.push(`${unsafe.name}: any;`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (configToDTS.queues) {
|
|
99
|
+
if (configToDTS.queues.producers) {
|
|
100
|
+
for (const queue of configToDTS.queues.producers) {
|
|
101
|
+
envTypeStructure.push(`${queue.binding}: Queue;`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const modulesTypeStructure: string[] = [];
|
|
107
|
+
if (configToDTS.rules) {
|
|
108
|
+
const moduleTypeMap = {
|
|
109
|
+
Text: "string",
|
|
110
|
+
Data: "ArrayBuffer",
|
|
111
|
+
CompiledWasm: "WebAssembly.Module",
|
|
112
|
+
};
|
|
113
|
+
for (const ruleObject of configToDTS.rules) {
|
|
114
|
+
const typeScriptType =
|
|
115
|
+
moduleTypeMap[ruleObject.type as keyof typeof moduleTypeMap];
|
|
116
|
+
if (typeScriptType !== undefined) {
|
|
117
|
+
ruleObject.globs.forEach((glob) => {
|
|
118
|
+
modulesTypeStructure.push(`declare module "*.${glob
|
|
119
|
+
.split(".")
|
|
120
|
+
.at(-1)}" {
|
|
121
|
+
const value: ${typeScriptType};
|
|
122
|
+
export default value;
|
|
123
|
+
}`);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
writeDTSFile({
|
|
130
|
+
envTypeStructure,
|
|
131
|
+
modulesTypeStructure,
|
|
132
|
+
formatType: entry.format,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function writeDTSFile({
|
|
137
|
+
envTypeStructure,
|
|
138
|
+
modulesTypeStructure,
|
|
139
|
+
formatType,
|
|
140
|
+
}: {
|
|
141
|
+
envTypeStructure: string[];
|
|
142
|
+
modulesTypeStructure: string[];
|
|
143
|
+
formatType: "modules" | "service-worker";
|
|
144
|
+
}) {
|
|
145
|
+
const wranglerOverrideDTSPath = findUpSync("worker-configuration.d.ts");
|
|
146
|
+
try {
|
|
147
|
+
if (
|
|
148
|
+
wranglerOverrideDTSPath !== undefined &&
|
|
149
|
+
!fs
|
|
150
|
+
.readFileSync(wranglerOverrideDTSPath, "utf8")
|
|
151
|
+
.includes("Generated by Wrangler")
|
|
152
|
+
) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
"A non-wrangler worker-configuration.d.ts already exists, please rename and try again."
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
} catch (error) {
|
|
158
|
+
if (error instanceof Error && !error.message.includes("not found")) {
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let combinedTypeStrings = "";
|
|
164
|
+
if (formatType === "modules") {
|
|
165
|
+
combinedTypeStrings += `interface Env {\n${envTypeStructure
|
|
166
|
+
.map((value) => `\t${value}`)
|
|
167
|
+
.join("\n")}\n}\n${modulesTypeStructure.join("\n")}`;
|
|
168
|
+
} else {
|
|
169
|
+
combinedTypeStrings += `export {};\ndeclare global {\n${envTypeStructure
|
|
170
|
+
.map((value) => `\tconst ${value}`)
|
|
171
|
+
.join("\n")}\n}\n${modulesTypeStructure.join("\n")}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (envTypeStructure.length || modulesTypeStructure.length) {
|
|
175
|
+
fs.writeFileSync(
|
|
176
|
+
"worker-configuration.d.ts",
|
|
177
|
+
`// Generated by Wrangler on ${new Date()}` + "\n" + combinedTypeStrings
|
|
178
|
+
);
|
|
179
|
+
logger.log(combinedTypeStrings);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { spawnSync } from "child_process";
|
|
2
|
+
import { fetch } from "undici";
|
|
3
|
+
import { logger } from "../logger";
|
|
4
|
+
|
|
5
|
+
const cache: Record<string, string> = {};
|
|
6
|
+
|
|
7
|
+
const usesAccessCache = new Map();
|
|
8
|
+
|
|
9
|
+
export async function domainUsesAccess(domain: string): Promise<boolean> {
|
|
10
|
+
logger.debug("Checking if domain has Access enabled:", domain);
|
|
11
|
+
|
|
12
|
+
if (usesAccessCache.has(domain)) {
|
|
13
|
+
logger.debug(
|
|
14
|
+
"Using cached Access switch for:",
|
|
15
|
+
domain,
|
|
16
|
+
usesAccessCache.get(domain)
|
|
17
|
+
);
|
|
18
|
+
return usesAccessCache.get(domain);
|
|
19
|
+
}
|
|
20
|
+
logger.debug("Access switch not cached for:", domain);
|
|
21
|
+
try {
|
|
22
|
+
const controller = new AbortController();
|
|
23
|
+
const cancel = setTimeout(() => {
|
|
24
|
+
controller.abort();
|
|
25
|
+
}, 1000);
|
|
26
|
+
|
|
27
|
+
const output = await fetch(`https://${domain}`, {
|
|
28
|
+
redirect: "manual",
|
|
29
|
+
signal: controller.signal,
|
|
30
|
+
});
|
|
31
|
+
clearTimeout(cancel);
|
|
32
|
+
const usesAccess = !!(
|
|
33
|
+
output.status === 302 &&
|
|
34
|
+
output.headers.get("location")?.includes("cloudflareaccess.com")
|
|
35
|
+
);
|
|
36
|
+
logger.debug("Caching access switch for:", domain);
|
|
37
|
+
|
|
38
|
+
usesAccessCache.set(domain, usesAccess);
|
|
39
|
+
return usesAccess;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
usesAccessCache.set(domain, false);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export async function getAccessToken(
|
|
46
|
+
domain: string
|
|
47
|
+
): Promise<string | undefined> {
|
|
48
|
+
if (!(await domainUsesAccess(domain))) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
if (cache[domain]) {
|
|
52
|
+
return cache[domain];
|
|
53
|
+
}
|
|
54
|
+
const output = await spawnSync("cloudflared", ["access", "login", domain]);
|
|
55
|
+
if (output.error) {
|
|
56
|
+
// The cloudflared binary is not installed
|
|
57
|
+
throw new Error(
|
|
58
|
+
"To use Wrangler with Cloudflare Access, please install `cloudflared` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation"
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const stringOutput = output.stdout.toString();
|
|
62
|
+
const matches = stringOutput.match(/fetched your token:\n\n(.*)/m);
|
|
63
|
+
if (matches && matches.length >= 2) {
|
|
64
|
+
cache[domain] = matches[1];
|
|
65
|
+
return matches[1];
|
|
66
|
+
}
|
|
67
|
+
throw new Error("Failed to authenticate with Cloudflare Access");
|
|
68
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { getEnvironmentVariableFactory } from "../environment-variables/factory";
|
|
2
|
+
import { getCloudflareApiEnvironmentFromEnv } from "../environment-variables/misc-variables";
|
|
3
|
+
import { getAccessToken } from "./access";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `CLOUDFLARE_ACCOUNT_ID` overrides the account inferred from the current user.
|
|
7
|
+
*/
|
|
8
|
+
export const getCloudflareAccountIdFromEnv = getEnvironmentVariableFactory({
|
|
9
|
+
variableName: "CLOUDFLARE_ACCOUNT_ID",
|
|
10
|
+
deprecatedName: "CF_ACCOUNT_ID",
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export const getCloudflareAPITokenFromEnv = getEnvironmentVariableFactory({
|
|
14
|
+
variableName: "CLOUDFLARE_API_TOKEN",
|
|
15
|
+
deprecatedName: "CF_API_TOKEN",
|
|
16
|
+
});
|
|
17
|
+
export const getCloudflareGlobalAuthKeyFromEnv = getEnvironmentVariableFactory({
|
|
18
|
+
variableName: "CLOUDFLARE_API_KEY",
|
|
19
|
+
deprecatedName: "CF_API_KEY",
|
|
20
|
+
});
|
|
21
|
+
export const getCloudflareGlobalAuthEmailFromEnv =
|
|
22
|
+
getEnvironmentVariableFactory({
|
|
23
|
+
variableName: "CLOUDFLARE_EMAIL",
|
|
24
|
+
deprecatedName: "CF_EMAIL",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* `WRANGLER_CLIENT_ID` is a UUID that is used to identify Wrangler
|
|
29
|
+
* to the Cloudflare APIs.
|
|
30
|
+
*
|
|
31
|
+
* Normally you should not need to set this explicitly.
|
|
32
|
+
* If you want to switch to the staging environment set the
|
|
33
|
+
* `WRANGLER_USE_STAGING` environment variable instead.
|
|
34
|
+
*/
|
|
35
|
+
export const getClientIdFromEnv = getEnvironmentVariableFactory({
|
|
36
|
+
variableName: "WRANGLER_CLIENT_ID",
|
|
37
|
+
defaultValue: () =>
|
|
38
|
+
getCloudflareApiEnvironmentFromEnv() === "staging"
|
|
39
|
+
? "4b2ea6cc-9421-4761-874b-ce550e0e3def"
|
|
40
|
+
: "54d11594-84e4-41aa-b438-e81b8fa78ee7",
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `WRANGLER_AUTH_DOMAIN` is the URL base domain that is used
|
|
45
|
+
* to access OAuth URLs for the Cloudflare APIs.
|
|
46
|
+
*
|
|
47
|
+
* Normally you should not need to set this explicitly.
|
|
48
|
+
* If you want to switch to the staging environment set the
|
|
49
|
+
* `WRANGLER_USE_STAGING` environment variable instead.
|
|
50
|
+
*/
|
|
51
|
+
export const getAuthDomainFromEnv = getEnvironmentVariableFactory({
|
|
52
|
+
variableName: "WRANGLER_AUTH_DOMAIN",
|
|
53
|
+
defaultValue: () =>
|
|
54
|
+
getCloudflareApiEnvironmentFromEnv() === "staging"
|
|
55
|
+
? "dash.staging.cloudflare.com"
|
|
56
|
+
: "dash.cloudflare.com",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `WRANGLER_AUTH_URL` is the path that is used to access OAuth
|
|
61
|
+
* for the Cloudflare APIs.
|
|
62
|
+
*
|
|
63
|
+
* Normally you should not need to set this explicitly.
|
|
64
|
+
* If you want to switch to the staging environment set the
|
|
65
|
+
* `WRANGLER_USE_STAGING` environment variable instead.
|
|
66
|
+
*/
|
|
67
|
+
export const getAuthUrlFromEnv = getEnvironmentVariableFactory({
|
|
68
|
+
variableName: "WRANGLER_AUTH_URL",
|
|
69
|
+
defaultValue: () => `https://${getAuthDomainFromEnv()}/oauth2/auth`,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* `WRANGLER_TOKEN_URL` is the path that is used to exchange an OAuth
|
|
74
|
+
* token for an API token.
|
|
75
|
+
*
|
|
76
|
+
* Normally you should not need to set this explicitly.
|
|
77
|
+
* If you want to switch to the staging environment set the
|
|
78
|
+
* `WRANGLER_USE_STAGING` environment variable instead.
|
|
79
|
+
*/
|
|
80
|
+
export const getTokenUrlFromEnv = getEnvironmentVariableFactory({
|
|
81
|
+
variableName: "WRANGLER_TOKEN_URL",
|
|
82
|
+
defaultValue: () => `https://${getAuthDomainFromEnv()}/oauth2/token`,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* `WRANGLER_REVOKE_URL` is the path that is used to exchange an OAuth
|
|
87
|
+
* refresh token for a new OAuth token.
|
|
88
|
+
*
|
|
89
|
+
* Normally you should not need to set this explicitly.
|
|
90
|
+
* If you want to switch to the staging environment set the
|
|
91
|
+
* `WRANGLER_USE_STAGING` environment variable instead.
|
|
92
|
+
*/
|
|
93
|
+
export const getRevokeUrlFromEnv = getEnvironmentVariableFactory({
|
|
94
|
+
variableName: "WRANGLER_REVOKE_URL",
|
|
95
|
+
defaultValue: () => `https://${getAuthDomainFromEnv()}/oauth2/revoke`,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Set the `WRANGLER_CF_AUTHORIZATION_TOKEN` to the CF_Authorization token found at https://dash.staging.cloudflare.com/bypass-limits
|
|
100
|
+
* if you want to access the staging environment, triggered by `WRANGLER_API_ENVIRONMENT=staging`.
|
|
101
|
+
*/
|
|
102
|
+
export const getCloudflareAccessToken = async () => {
|
|
103
|
+
const env = getEnvironmentVariableFactory({
|
|
104
|
+
variableName: "WRANGLER_CF_AUTHORIZATION_TOKEN",
|
|
105
|
+
})();
|
|
106
|
+
|
|
107
|
+
// If the environment variable is defined, go ahead and use it.
|
|
108
|
+
if (env !== undefined) {
|
|
109
|
+
return env;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return getAccessToken(getAuthDomainFromEnv());
|
|
113
|
+
};
|
|
@@ -3,7 +3,7 @@ import SelectInput from "ink-select-input";
|
|
|
3
3
|
import React from "react";
|
|
4
4
|
import { fetchListResult } from "../cfetch";
|
|
5
5
|
import { logger } from "../logger";
|
|
6
|
-
import { getCloudflareAccountIdFromEnv } from "./
|
|
6
|
+
import { getCloudflareAccountIdFromEnv } from "./auth-variables";
|
|
7
7
|
|
|
8
8
|
export type ChooseAccountItem = {
|
|
9
9
|
id: string;
|
package/src/user/index.ts
CHANGED