wrangler 0.0.0-e88a54e → 0.0.0-e95e5a0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -14
- package/bin/wrangler.js +13 -1
- package/miniflare-dist/index.mjs +4828 -21
- package/package.json +29 -24
- package/pages/functions/buildPlugin.ts +92 -0
- package/pages/functions/buildWorker.ts +80 -4
- package/pages/functions/filepath-routing.test.ts +11 -0
- package/pages/functions/filepath-routing.ts +8 -0
- package/pages/functions/routes.ts +6 -1
- package/pages/functions/template-plugin.ts +146 -0
- package/pages/functions/template-worker.ts +21 -8
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +530 -201
- package/src/__tests__/dev.test.tsx +284 -18
- package/src/__tests__/guess-worker-format.test.ts +28 -1
- package/src/__tests__/helpers/mock-cfetch.ts +5 -1
- package/src/__tests__/helpers/mock-console.ts +20 -3
- package/src/__tests__/helpers/mock-dialogs.ts +26 -1
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +27 -0
- package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
- package/src/__tests__/helpers/run-in-tmp.ts +7 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/https-options.test.ts +5 -2
- package/src/__tests__/index.test.ts +27 -769
- package/src/__tests__/init.test.ts +1516 -0
- package/src/__tests__/jest.setup.ts +4 -4
- package/src/__tests__/kv.test.ts +133 -61
- package/src/__tests__/logger.test.ts +115 -0
- package/src/__tests__/pages.test.ts +311 -26
- package/src/__tests__/publish.test.ts +2789 -618
- package/src/__tests__/r2.test.ts +26 -10
- package/src/__tests__/route.test.ts +4 -4
- package/src/__tests__/secret.test.ts +55 -34
- package/src/__tests__/tail.test.ts +146 -20
- package/src/__tests__/user.test.ts +131 -0
- package/src/__tests__/whoami.test.tsx +14 -9
- package/src/bundle.ts +30 -3
- package/src/cfetch/internal.ts +0 -2
- package/src/cli.ts +5 -11
- package/src/config/README.md +1 -1
- package/src/config/config.ts +20 -4
- package/src/config/environment.ts +32 -4
- package/src/config/index.ts +4 -3
- package/src/config/validation-helpers.ts +96 -7
- package/src/config/validation.ts +388 -54
- package/src/config-cache.ts +45 -0
- package/src/create-worker-preview.ts +22 -13
- package/src/create-worker-upload-form.ts +32 -9
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +132 -78
- package/src/dev/local.tsx +101 -21
- package/src/dev/remote.tsx +23 -13
- package/src/dev/use-esbuild.ts +23 -5
- package/src/dialogs.tsx +16 -6
- package/src/entry.ts +83 -24
- package/src/environment-variables.ts +3 -1
- package/src/errors.ts +11 -0
- package/src/https-options.ts +3 -2
- package/src/index.tsx +689 -394
- package/src/inspect.ts +17 -11
- package/src/intl-polyfill.d.ts +139 -0
- package/src/logger.ts +66 -0
- package/src/miniflare-cli/index.ts +4 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +7 -7
- package/src/open-in-browser.ts +5 -4
- package/src/package-manager.ts +9 -8
- package/src/pages.tsx +875 -56
- package/src/parse.ts +1 -1
- package/src/proxy.ts +28 -19
- package/src/publish.ts +263 -141
- package/src/sites.tsx +27 -14
- package/src/tail/index.ts +2 -2
- package/src/tail/printing.ts +13 -8
- package/src/update-check.ts +19 -0
- package/src/user.tsx +116 -41
- package/src/whoami.tsx +2 -1
- package/src/worker.ts +15 -6
- package/templates/gitignore +171 -0
- package/templates/static-asset-facade.js +1 -1
- package/wrangler-dist/cli.js +21162 -50227
- package/miniflare-dist/index.mjs.map +0 -7
- package/src/__tests__/dev2.test.tsx +0 -154
- package/src/__tests__/logout.test.ts +0 -52
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/reporting.ts +0 -131
- package/wrangler-dist/cli.js.map +0 -7
|
@@ -2,6 +2,7 @@ import { URL } from "node:url";
|
|
|
2
2
|
import { fetch } from "undici";
|
|
3
3
|
import { fetchResult } from "./cfetch";
|
|
4
4
|
import { createWorkerUploadForm } from "./create-worker-upload-form";
|
|
5
|
+
import { logger } from "./logger";
|
|
5
6
|
import type { CfAccount, CfWorkerContext, CfWorkerInit } from "./worker";
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -57,7 +58,8 @@ export interface CfPreviewToken {
|
|
|
57
58
|
*/
|
|
58
59
|
async function sessionToken(
|
|
59
60
|
account: CfAccount,
|
|
60
|
-
ctx: CfWorkerContext
|
|
61
|
+
ctx: CfWorkerContext,
|
|
62
|
+
abortSignal: AbortSignal
|
|
61
63
|
): Promise<CfPreviewToken> {
|
|
62
64
|
const { accountId } = account;
|
|
63
65
|
const initUrl = ctx.zone
|
|
@@ -65,9 +67,9 @@ async function sessionToken(
|
|
|
65
67
|
: `/accounts/${accountId}/workers/subdomain/edge-preview`;
|
|
66
68
|
|
|
67
69
|
const { exchange_url } = await fetchResult<{ exchange_url: string }>(initUrl);
|
|
68
|
-
const { inspector_websocket, token } = (await (
|
|
69
|
-
await fetch(exchange_url)
|
|
70
|
-
).json()) as { inspector_websocket: string; token: string };
|
|
70
|
+
const { inspector_websocket, prewarm, token } = (await (
|
|
71
|
+
await fetch(exchange_url, { signal: abortSignal })
|
|
72
|
+
).json()) as { inspector_websocket: string; token: string; prewarm: string };
|
|
71
73
|
const { host } = new URL(inspector_websocket);
|
|
72
74
|
const query = `cf_workers_preview_token=${token}`;
|
|
73
75
|
|
|
@@ -75,9 +77,7 @@ async function sessionToken(
|
|
|
75
77
|
value: token,
|
|
76
78
|
host,
|
|
77
79
|
inspectorUrl: new URL(`${inspector_websocket}?${query}`),
|
|
78
|
-
prewarmUrl: new URL(
|
|
79
|
-
`https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
|
|
80
|
-
),
|
|
80
|
+
prewarmUrl: new URL(prewarm),
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
83
|
|
|
@@ -96,11 +96,13 @@ function randomId(): string {
|
|
|
96
96
|
async function createPreviewToken(
|
|
97
97
|
account: CfAccount,
|
|
98
98
|
worker: CfWorkerInit,
|
|
99
|
-
ctx: CfWorkerContext
|
|
99
|
+
ctx: CfWorkerContext,
|
|
100
|
+
abortSignal: AbortSignal
|
|
100
101
|
): Promise<CfPreviewToken> {
|
|
101
102
|
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(
|
|
102
103
|
account,
|
|
103
|
-
ctx
|
|
104
|
+
ctx,
|
|
105
|
+
abortSignal
|
|
104
106
|
);
|
|
105
107
|
|
|
106
108
|
const { accountId } = account;
|
|
@@ -155,12 +157,19 @@ async function createPreviewToken(
|
|
|
155
157
|
export async function createWorkerPreview(
|
|
156
158
|
init: CfWorkerInit,
|
|
157
159
|
account: CfAccount,
|
|
158
|
-
ctx: CfWorkerContext
|
|
160
|
+
ctx: CfWorkerContext,
|
|
161
|
+
abortSignal: AbortSignal
|
|
159
162
|
): Promise<CfPreviewToken> {
|
|
160
|
-
const token = await createPreviewToken(account, init, ctx);
|
|
161
|
-
const response = await fetch(token.prewarmUrl.href, {
|
|
163
|
+
const token = await createPreviewToken(account, init, ctx, abortSignal);
|
|
164
|
+
const response = await fetch(token.prewarmUrl.href, {
|
|
165
|
+
method: "POST",
|
|
166
|
+
signal: abortSignal,
|
|
167
|
+
headers: {
|
|
168
|
+
"cf-workers-preview-token": token.value,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
162
171
|
if (!response.ok) {
|
|
163
|
-
|
|
172
|
+
logger.warn("worker failed to prewarm: ", response.statusText);
|
|
164
173
|
}
|
|
165
174
|
return token;
|
|
166
175
|
}
|
|
@@ -26,7 +26,7 @@ export function toMimeType(type: CfModuleType): string {
|
|
|
26
26
|
export interface WorkerMetadata {
|
|
27
27
|
/** The name of the entry point module. Only exists when the worker is in the ES module format */
|
|
28
28
|
main_module?: string;
|
|
29
|
-
/** The name of the entry point module. Only exists when the worker is in the
|
|
29
|
+
/** The name of the entry point module. Only exists when the worker is in the service-worker format */
|
|
30
30
|
body_part?: string;
|
|
31
31
|
compatibility_date?: string;
|
|
32
32
|
compatibility_flags?: string[];
|
|
@@ -38,6 +38,7 @@ export interface WorkerMetadata {
|
|
|
38
38
|
| { type: "json"; name: string; json: unknown }
|
|
39
39
|
| { type: "wasm_module"; name: string; part: string }
|
|
40
40
|
| { type: "text_blob"; name: string; part: string }
|
|
41
|
+
| { type: "data_blob"; name: string; part: string }
|
|
41
42
|
| {
|
|
42
43
|
type: "durable_object_namespace";
|
|
43
44
|
name: string;
|
|
@@ -133,6 +134,21 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
|
|
|
133
134
|
}
|
|
134
135
|
}
|
|
135
136
|
|
|
137
|
+
for (const [name, filePath] of Object.entries(bindings.data_blobs || {})) {
|
|
138
|
+
metadataBindings.push({
|
|
139
|
+
name,
|
|
140
|
+
type: "data_blob",
|
|
141
|
+
part: name,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
formData.set(
|
|
145
|
+
name,
|
|
146
|
+
new File([readFileSync(filePath)], filePath, {
|
|
147
|
+
type: "application/octet-stream",
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
136
152
|
if (main.type === "commonjs") {
|
|
137
153
|
// This is a service-worker format worker.
|
|
138
154
|
for (const module of Object.values([...(modules || [])])) {
|
|
@@ -146,8 +162,12 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
|
|
|
146
162
|
);
|
|
147
163
|
// And then remove it from the modules collection
|
|
148
164
|
modules = modules?.filter((m) => m !== module);
|
|
149
|
-
} else if (
|
|
150
|
-
|
|
165
|
+
} else if (
|
|
166
|
+
module.type === "compiled-wasm" ||
|
|
167
|
+
module.type === "text" ||
|
|
168
|
+
module.type === "buffer"
|
|
169
|
+
) {
|
|
170
|
+
// Convert all wasm/text/data modules into `wasm_module`/`text_blob`/`data_blob` bindings.
|
|
151
171
|
// The "name" of the module is a file path. We use it
|
|
152
172
|
// to instead be a "part" of the body, and a reference
|
|
153
173
|
// that we can use inside our source. This identifier has to be a valid
|
|
@@ -156,7 +176,12 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
|
|
|
156
176
|
const name = module.name.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
157
177
|
metadataBindings.push({
|
|
158
178
|
name,
|
|
159
|
-
type:
|
|
179
|
+
type:
|
|
180
|
+
module.type === "compiled-wasm"
|
|
181
|
+
? "wasm_module"
|
|
182
|
+
: module.type === "text"
|
|
183
|
+
? "text_blob"
|
|
184
|
+
: "data_blob",
|
|
160
185
|
part: name,
|
|
161
186
|
});
|
|
162
187
|
|
|
@@ -167,15 +192,13 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
|
|
|
167
192
|
type:
|
|
168
193
|
module.type === "compiled-wasm"
|
|
169
194
|
? "application/wasm"
|
|
170
|
-
: "text
|
|
195
|
+
: module.type === "text"
|
|
196
|
+
? "text/plain"
|
|
197
|
+
: "application/octet-stream",
|
|
171
198
|
})
|
|
172
199
|
);
|
|
173
200
|
// And then remove it from the modules collection
|
|
174
201
|
modules = modules?.filter((m) => m !== module);
|
|
175
|
-
} else if (module.type === "buffer") {
|
|
176
|
-
throw new Error(
|
|
177
|
-
'The "buffer" module type is not yet supported in service worker format workers'
|
|
178
|
-
);
|
|
179
202
|
}
|
|
180
203
|
}
|
|
181
204
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import dotenv from "dotenv";
|
|
4
|
+
import { logger } from "../logger";
|
|
5
|
+
import type { Config } from "../config";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get the Worker `vars` bindings for a `wrangler dev` instance of a Worker.
|
|
9
|
+
*
|
|
10
|
+
* The `vars` bindings can be specified in the `wrangler.toml` configuration file.
|
|
11
|
+
* But "secret" `vars` are usually only provided at the server -
|
|
12
|
+
* either by creating them in the Dashboard UI, or using the `wrangler secret` command.
|
|
13
|
+
*
|
|
14
|
+
* It is useful during development, to provide these types of variable locally.
|
|
15
|
+
* When running `wrangler dev` we will look for a file called `.dev.vars`, situated
|
|
16
|
+
* next to the `wrangler.toml` file (or in the current working directory if there is no
|
|
17
|
+
* `wrangler.toml`).
|
|
18
|
+
*
|
|
19
|
+
* Any values in this file, formatted like a `dotenv` file, will add to or override `vars`
|
|
20
|
+
* bindings provided in the `wrangler.toml`.
|
|
21
|
+
*/
|
|
22
|
+
export function getVarsForDev(config: Config): Config["vars"] {
|
|
23
|
+
const configDir = path.resolve(path.dirname(config.configPath ?? "."));
|
|
24
|
+
const devVarsPath = path.resolve(configDir, ".dev.vars");
|
|
25
|
+
if (fs.existsSync(devVarsPath)) {
|
|
26
|
+
const devVarsRelativePath = path.relative(process.cwd(), devVarsPath);
|
|
27
|
+
logger.log(`Using vars defined in ${devVarsRelativePath}`);
|
|
28
|
+
const devVars = dotenv.parse(fs.readFileSync(devVarsPath, "utf8"));
|
|
29
|
+
return {
|
|
30
|
+
...config.vars,
|
|
31
|
+
...devVars,
|
|
32
|
+
};
|
|
33
|
+
} else {
|
|
34
|
+
return config.vars;
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/dev/dev.tsx
CHANGED
|
@@ -2,15 +2,15 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import { watch } from "chokidar";
|
|
3
3
|
import clipboardy from "clipboardy";
|
|
4
4
|
import commandExists from "command-exists";
|
|
5
|
-
import { Box, Text, useApp, useInput } from "ink";
|
|
5
|
+
import { Box, Text, useApp, useInput, useStdin } from "ink";
|
|
6
6
|
import React, { useState, useEffect, useRef } from "react";
|
|
7
7
|
import { withErrorBoundary, useErrorHandler } from "react-error-boundary";
|
|
8
8
|
import onExit from "signal-exit";
|
|
9
9
|
import tmp from "tmp-promise";
|
|
10
10
|
import { fetch } from "undici";
|
|
11
11
|
import { runCustomBuild } from "../entry";
|
|
12
|
+
import { logger } from "../logger";
|
|
12
13
|
import openInBrowser from "../open-in-browser";
|
|
13
|
-
import { reportError } from "../reporting";
|
|
14
14
|
import { getAPIToken } from "../user";
|
|
15
15
|
import { Local } from "./local";
|
|
16
16
|
import { Remote } from "./remote";
|
|
@@ -19,31 +19,36 @@ import type { Config } from "../config";
|
|
|
19
19
|
import type { Entry } from "../entry";
|
|
20
20
|
import type { AssetPaths } from "../sites";
|
|
21
21
|
import type { CfWorkerInit } from "../worker";
|
|
22
|
+
import type { EsbuildBundle } from "./use-esbuild";
|
|
22
23
|
|
|
23
24
|
export type DevProps = {
|
|
24
25
|
name?: string;
|
|
25
26
|
entry: Entry;
|
|
26
|
-
port
|
|
27
|
+
port: number;
|
|
27
28
|
ip: string;
|
|
28
29
|
inspectorPort: number;
|
|
29
30
|
rules: Config["rules"];
|
|
30
|
-
accountId:
|
|
31
|
+
accountId: string | undefined;
|
|
31
32
|
initialMode: "local" | "remote";
|
|
32
|
-
jsxFactory:
|
|
33
|
-
jsxFragment:
|
|
33
|
+
jsxFactory: string | undefined;
|
|
34
|
+
jsxFragment: string | undefined;
|
|
35
|
+
tsconfig: string | undefined;
|
|
34
36
|
upstreamProtocol: "https" | "http";
|
|
35
37
|
localProtocol: "https" | "http";
|
|
36
38
|
enableLocalPersistence: boolean;
|
|
37
39
|
bindings: CfWorkerInit["bindings"];
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
crons: Config["triggers"]["crons"];
|
|
41
|
+
public: string | undefined;
|
|
42
|
+
assetPaths: AssetPaths | undefined;
|
|
43
|
+
compatibilityDate: string;
|
|
44
|
+
compatibilityFlags: string[] | undefined;
|
|
45
|
+
usageModel: "bundled" | "unbound" | undefined;
|
|
46
|
+
minify: boolean | undefined;
|
|
47
|
+
nodeCompat: boolean | undefined;
|
|
48
|
+
build: {
|
|
49
|
+
command?: string | undefined;
|
|
50
|
+
cwd?: string | undefined;
|
|
51
|
+
watch_dir?: string | undefined;
|
|
47
52
|
};
|
|
48
53
|
env: string | undefined;
|
|
49
54
|
legacyEnv: boolean;
|
|
@@ -56,15 +61,14 @@ export type DevProps = {
|
|
|
56
61
|
};
|
|
57
62
|
|
|
58
63
|
export function DevImplementation(props: DevProps): JSX.Element {
|
|
59
|
-
const port = props.port ?? 8787;
|
|
60
64
|
const apiToken = props.initialMode === "remote" ? getAPIToken() : undefined;
|
|
61
65
|
const directory = useTmpDir();
|
|
62
66
|
|
|
63
|
-
useCustomBuild(props.entry, props.
|
|
67
|
+
useCustomBuild(props.entry, props.build);
|
|
64
68
|
|
|
65
69
|
if (props.public && props.entry.format === "service-worker") {
|
|
66
70
|
throw new Error(
|
|
67
|
-
"You cannot use the service
|
|
71
|
+
"You cannot use the service-worker format with a `public` directory."
|
|
68
72
|
);
|
|
69
73
|
}
|
|
70
74
|
|
|
@@ -76,7 +80,13 @@ export function DevImplementation(props: DevProps): JSX.Element {
|
|
|
76
80
|
|
|
77
81
|
if (props.bindings.text_blobs && props.entry.format === "modules") {
|
|
78
82
|
throw new Error(
|
|
79
|
-
"You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[
|
|
83
|
+
"You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (props.bindings.data_blobs && props.entry.format === "modules") {
|
|
88
|
+
throw new Error(
|
|
89
|
+
"You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
|
|
80
90
|
);
|
|
81
91
|
}
|
|
82
92
|
|
|
@@ -88,14 +98,37 @@ export function DevImplementation(props: DevProps): JSX.Element {
|
|
|
88
98
|
rules: props.rules,
|
|
89
99
|
jsxFragment: props.jsxFragment,
|
|
90
100
|
serveAssetsFromWorker: !!props.public,
|
|
101
|
+
tsconfig: props.tsconfig,
|
|
102
|
+
minify: props.minify,
|
|
103
|
+
nodeCompat: props.nodeCompat,
|
|
91
104
|
});
|
|
92
105
|
|
|
106
|
+
// only load the UI if we're running in a supported environment
|
|
107
|
+
const { isRawModeSupported } = useStdin();
|
|
108
|
+
return isRawModeSupported ? (
|
|
109
|
+
<InteractiveDevSession {...props} bundle={bundle} apiToken={apiToken} />
|
|
110
|
+
) : (
|
|
111
|
+
<DevSession
|
|
112
|
+
{...props}
|
|
113
|
+
bundle={bundle}
|
|
114
|
+
apiToken={apiToken}
|
|
115
|
+
local={props.initialMode === "local"}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
type InteractiveDevSessionProps = DevProps & {
|
|
121
|
+
apiToken: string | undefined;
|
|
122
|
+
bundle: EsbuildBundle | undefined;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
function InteractiveDevSession(props: InteractiveDevSessionProps) {
|
|
93
126
|
const toggles = useHotkeys(
|
|
94
127
|
{
|
|
95
128
|
local: props.initialMode === "local",
|
|
96
129
|
tunnel: false,
|
|
97
130
|
},
|
|
98
|
-
port,
|
|
131
|
+
props.port,
|
|
99
132
|
props.ip,
|
|
100
133
|
props.inspectorPort,
|
|
101
134
|
props.localProtocol
|
|
@@ -105,55 +138,67 @@ export function DevImplementation(props: DevProps): JSX.Element {
|
|
|
105
138
|
|
|
106
139
|
return (
|
|
107
140
|
<>
|
|
108
|
-
{toggles.local
|
|
109
|
-
<Local
|
|
110
|
-
name={props.name}
|
|
111
|
-
bundle={bundle}
|
|
112
|
-
format={props.entry.format}
|
|
113
|
-
compatibilityDate={props.compatibilityDate}
|
|
114
|
-
compatibilityFlags={props.compatibilityFlags}
|
|
115
|
-
bindings={props.bindings}
|
|
116
|
-
assetPaths={props.assetPaths}
|
|
117
|
-
public={props.public}
|
|
118
|
-
port={port}
|
|
119
|
-
ip={props.ip}
|
|
120
|
-
rules={props.rules}
|
|
121
|
-
inspectorPort={props.inspectorPort}
|
|
122
|
-
enableLocalPersistence={props.enableLocalPersistence}
|
|
123
|
-
/>
|
|
124
|
-
) : (
|
|
125
|
-
<Remote
|
|
126
|
-
name={props.name}
|
|
127
|
-
bundle={bundle}
|
|
128
|
-
format={props.entry.format}
|
|
129
|
-
accountId={props.accountId}
|
|
130
|
-
apiToken={apiToken}
|
|
131
|
-
bindings={props.bindings}
|
|
132
|
-
assetPaths={props.assetPaths}
|
|
133
|
-
public={props.public}
|
|
134
|
-
port={port}
|
|
135
|
-
ip={props.ip}
|
|
136
|
-
localProtocol={props.localProtocol}
|
|
137
|
-
inspectorPort={props.inspectorPort}
|
|
138
|
-
compatibilityDate={props.compatibilityDate}
|
|
139
|
-
compatibilityFlags={props.compatibilityFlags}
|
|
140
|
-
usageModel={props.usageModel}
|
|
141
|
-
env={props.env}
|
|
142
|
-
legacyEnv={props.legacyEnv}
|
|
143
|
-
zone={props.zone}
|
|
144
|
-
/>
|
|
145
|
-
)}
|
|
141
|
+
<DevSession {...props} local={toggles.local} />
|
|
146
142
|
<Box borderStyle="round" paddingLeft={1} paddingRight={1}>
|
|
147
|
-
<Text>
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
</Text>
|
|
143
|
+
<Text bold={true}>[b]</Text>
|
|
144
|
+
<Text> open a browser, </Text>
|
|
145
|
+
<Text bold={true}>[d]</Text>
|
|
146
|
+
<Text> open Devtools, </Text>
|
|
147
|
+
<Text bold={true}>[l]</Text>
|
|
148
|
+
<Text> {toggles.local ? "turn off" : "turn on"} local mode, </Text>
|
|
149
|
+
<Text bold={true}>[c]</Text>
|
|
150
|
+
<Text> clear console, </Text>
|
|
151
|
+
<Text bold={true}>[x]</Text>
|
|
152
|
+
<Text> to exit</Text>
|
|
152
153
|
</Box>
|
|
153
154
|
</>
|
|
154
155
|
);
|
|
155
156
|
}
|
|
156
157
|
|
|
158
|
+
type DevSessionProps = InteractiveDevSessionProps & { local: boolean };
|
|
159
|
+
|
|
160
|
+
function DevSession(props: DevSessionProps) {
|
|
161
|
+
return props.local ? (
|
|
162
|
+
<Local
|
|
163
|
+
name={props.name}
|
|
164
|
+
bundle={props.bundle}
|
|
165
|
+
format={props.entry.format}
|
|
166
|
+
compatibilityDate={props.compatibilityDate}
|
|
167
|
+
compatibilityFlags={props.compatibilityFlags}
|
|
168
|
+
bindings={props.bindings}
|
|
169
|
+
assetPaths={props.assetPaths}
|
|
170
|
+
public={props.public}
|
|
171
|
+
port={props.port}
|
|
172
|
+
ip={props.ip}
|
|
173
|
+
rules={props.rules}
|
|
174
|
+
inspectorPort={props.inspectorPort}
|
|
175
|
+
enableLocalPersistence={props.enableLocalPersistence}
|
|
176
|
+
crons={props.crons}
|
|
177
|
+
/>
|
|
178
|
+
) : (
|
|
179
|
+
<Remote
|
|
180
|
+
name={props.name}
|
|
181
|
+
bundle={props.bundle}
|
|
182
|
+
format={props.entry.format}
|
|
183
|
+
accountId={props.accountId}
|
|
184
|
+
apiToken={props.apiToken}
|
|
185
|
+
bindings={props.bindings}
|
|
186
|
+
assetPaths={props.assetPaths}
|
|
187
|
+
public={props.public}
|
|
188
|
+
port={props.port}
|
|
189
|
+
ip={props.ip}
|
|
190
|
+
localProtocol={props.localProtocol}
|
|
191
|
+
inspectorPort={props.inspectorPort}
|
|
192
|
+
compatibilityDate={props.compatibilityDate}
|
|
193
|
+
compatibilityFlags={props.compatibilityFlags}
|
|
194
|
+
usageModel={props.usageModel}
|
|
195
|
+
env={props.env}
|
|
196
|
+
legacyEnv={props.legacyEnv}
|
|
197
|
+
zone={props.zone}
|
|
198
|
+
/>
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
157
202
|
export interface DirectorySyncResult {
|
|
158
203
|
name: string;
|
|
159
204
|
removeCallback: () => void;
|
|
@@ -169,7 +214,9 @@ function useTmpDir(): string | undefined {
|
|
|
169
214
|
setDirectory(dir);
|
|
170
215
|
return;
|
|
171
216
|
} catch (err) {
|
|
172
|
-
|
|
217
|
+
logger.error(
|
|
218
|
+
"Failed to create temporary directory to store built files."
|
|
219
|
+
);
|
|
173
220
|
handleError(err);
|
|
174
221
|
}
|
|
175
222
|
return () => {
|
|
@@ -182,9 +229,9 @@ function useTmpDir(): string | undefined {
|
|
|
182
229
|
function useCustomBuild(
|
|
183
230
|
expectedEntry: Entry,
|
|
184
231
|
build: {
|
|
185
|
-
command?:
|
|
186
|
-
cwd?:
|
|
187
|
-
watch_dir?:
|
|
232
|
+
command?: string | undefined;
|
|
233
|
+
cwd?: string | undefined;
|
|
234
|
+
watch_dir?: string | undefined;
|
|
188
235
|
}
|
|
189
236
|
): void {
|
|
190
237
|
useEffect(() => {
|
|
@@ -196,9 +243,9 @@ function useCustomBuild(
|
|
|
196
243
|
ignoreInitial: true,
|
|
197
244
|
}).on("all", (_event, filePath) => {
|
|
198
245
|
//TODO: we should buffer requests to the proxy until this completes
|
|
199
|
-
|
|
246
|
+
logger.log(`The file ${filePath} changed, restarting build...`);
|
|
200
247
|
runCustomBuild(expectedEntry.file, build).catch((err) => {
|
|
201
|
-
|
|
248
|
+
logger.error("Custom build failed:", err);
|
|
202
249
|
});
|
|
203
250
|
});
|
|
204
251
|
}
|
|
@@ -245,12 +292,12 @@ function useTunnel(toggle: boolean) {
|
|
|
245
292
|
try {
|
|
246
293
|
await commandExists("cloudflared");
|
|
247
294
|
} catch (e) {
|
|
248
|
-
|
|
295
|
+
logger.warn(
|
|
249
296
|
"To share your worker on the Internet, please install `cloudflared` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation"
|
|
250
297
|
);
|
|
251
298
|
return;
|
|
252
299
|
}
|
|
253
|
-
|
|
300
|
+
logger.log("⎔ Starting a tunnel...");
|
|
254
301
|
tunnel.current = spawn("cloudflared", [
|
|
255
302
|
"tunnel",
|
|
256
303
|
"--url",
|
|
@@ -261,30 +308,29 @@ function useTunnel(toggle: boolean) {
|
|
|
261
308
|
|
|
262
309
|
tunnel.current.on("close", (code) => {
|
|
263
310
|
if (code) {
|
|
264
|
-
|
|
311
|
+
logger.log(`Tunnel process exited with code ${code}`);
|
|
265
312
|
}
|
|
266
313
|
});
|
|
267
314
|
|
|
268
315
|
removeSignalExitListener.current = onExit((_code, _signal) => {
|
|
269
|
-
|
|
316
|
+
logger.log("⎔ Shutting down local tunnel.");
|
|
270
317
|
tunnel.current?.kill();
|
|
271
318
|
tunnel.current = undefined;
|
|
272
319
|
});
|
|
273
320
|
|
|
274
321
|
const hostName = await findTunnelHostname();
|
|
275
322
|
await clipboardy.write(hostName);
|
|
276
|
-
|
|
323
|
+
logger.log(`⬣ Sharing at ${hostName}, copied to clipboard.`);
|
|
277
324
|
}
|
|
278
325
|
}
|
|
279
326
|
|
|
280
327
|
startTunnel().catch(async (err) => {
|
|
281
|
-
|
|
282
|
-
await reportError(err);
|
|
328
|
+
logger.error("tunnel:", err);
|
|
283
329
|
});
|
|
284
330
|
|
|
285
331
|
return () => {
|
|
286
332
|
if (tunnel.current) {
|
|
287
|
-
|
|
333
|
+
logger.log("⎔ Shutting down tunnel.");
|
|
288
334
|
tunnel.current?.kill();
|
|
289
335
|
tunnel.current = undefined;
|
|
290
336
|
removeSignalExitListener.current && removeSignalExitListener.current();
|
|
@@ -307,6 +353,7 @@ function useHotkeys(
|
|
|
307
353
|
) {
|
|
308
354
|
// UGH, we should put port in context instead
|
|
309
355
|
const [toggles, setToggles] = useState(initial);
|
|
356
|
+
const { exit } = useApp();
|
|
310
357
|
useInput(
|
|
311
358
|
async (
|
|
312
359
|
input,
|
|
@@ -314,6 +361,13 @@ function useHotkeys(
|
|
|
314
361
|
key
|
|
315
362
|
) => {
|
|
316
363
|
switch (input.toLowerCase()) {
|
|
364
|
+
// clear console
|
|
365
|
+
case "c":
|
|
366
|
+
console.clear();
|
|
367
|
+
// This console.log causes Ink to re-render the `DevSession` component.
|
|
368
|
+
// Couldn't find a better way to tell it to do so...
|
|
369
|
+
console.log();
|
|
370
|
+
break;
|
|
317
371
|
// open browser
|
|
318
372
|
case "b": {
|
|
319
373
|
await openInBrowser(`${localProtocol}://${ip}:${port}`);
|
|
@@ -337,7 +391,7 @@ function useHotkeys(
|
|
|
337
391
|
// shut down
|
|
338
392
|
case "q":
|
|
339
393
|
case "x":
|
|
340
|
-
|
|
394
|
+
exit();
|
|
341
395
|
break;
|
|
342
396
|
default:
|
|
343
397
|
// nothing?
|