wrangler 0.0.0-e1c615f → 0.0.0-e3a458e
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 +2 -2
- package/package.json +34 -15
- package/pages/functions/buildWorker.ts +6 -1
- package/pages/functions/filepath-routing.test.ts +212 -28
- package/pages/functions/filepath-routing.ts +91 -131
- package/pages/functions/routes.ts +11 -18
- package/pages/functions/template-worker.ts +21 -28
- package/src/__tests__/configuration.test.ts +2598 -0
- package/src/__tests__/dev.test.tsx +437 -58
- package/src/__tests__/dev2.test.tsx +154 -0
- package/src/__tests__/guess-worker-format.test.ts +58 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +48 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +55 -19
- package/src/__tests__/helpers/mock-console.ts +70 -0
- package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
- package/src/__tests__/helpers/mock-kv.ts +33 -0
- package/src/__tests__/helpers/mock-stdin.ts +103 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +32 -0
- package/src/__tests__/helpers/run-wrangler.ts +16 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +127 -0
- package/src/__tests__/index.test.ts +779 -75
- package/src/__tests__/jest.setup.ts +53 -6
- package/src/__tests__/kv.test.ts +726 -442
- package/src/__tests__/logout.test.ts +52 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/pages.test.ts +44 -0
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +2611 -0
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +425 -0
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +564 -0
- package/src/__tests__/whoami.test.tsx +100 -0
- package/src/bundle.ts +142 -0
- package/src/cfetch/index.ts +21 -19
- package/src/cfetch/internal.ts +94 -24
- package/src/cli.ts +10 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +279 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +305 -0
- package/src/config/index.ts +52 -0
- package/src/config/validation-helpers.ts +468 -0
- package/src/config/validation.ts +1057 -0
- package/src/{api/preview.ts → create-worker-preview.ts} +61 -23
- package/src/create-worker-upload-form.ts +217 -0
- package/src/dev/dev.tsx +362 -0
- package/src/dev/local.tsx +247 -0
- package/src/dev/remote.tsx +203 -0
- package/src/dev/use-esbuild.ts +100 -0
- package/src/dialogs.tsx +2 -2
- package/src/entry.ts +159 -0
- package/src/environment-variables.ts +33 -0
- package/src/https-options.ts +122 -0
- package/src/index.tsx +1231 -795
- package/src/inspect.ts +617 -0
- package/src/{kv.tsx → kv.ts} +132 -60
- package/src/module-collection.ts +215 -26
- package/src/open-in-browser.ts +25 -0
- package/src/package-manager.ts +126 -0
- package/src/pages.tsx +140 -63
- package/src/parse.ts +183 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +414 -77
- package/src/publish.ts +296 -292
- package/src/r2.ts +50 -0
- package/src/reporting.ts +131 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +217 -58
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +62 -0
- package/src/user.tsx +284 -188
- package/src/whoami.tsx +69 -0
- package/src/{api/worker.ts → worker.ts} +51 -43
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +130585 -112306
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/run-in-tmp.ts +0 -19
- package/src/__tests__/run-wrangler.ts +0 -32
- package/src/api/form_data.ts +0 -131
- package/src/api/inspect.ts +0 -430
- package/src/config.ts +0 -131
- package/src/dev.tsx +0 -874
- package/src/tail.tsx +0 -73
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { execa, execaCommandSync } from "execa";
|
|
4
|
+
|
|
5
|
+
export interface PackageManager {
|
|
6
|
+
cwd: string;
|
|
7
|
+
type: "npm" | "yarn";
|
|
8
|
+
addDevDeps(...packages: string[]): Promise<void>;
|
|
9
|
+
install(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function getPackageManager(cwd: string): Promise<PackageManager> {
|
|
13
|
+
const [hasYarn, hasNpm] = await Promise.all([supportsYarn(), supportsNpm()]);
|
|
14
|
+
const hasYarnLock = existsSync(join(cwd, "yarn.lock"));
|
|
15
|
+
const hasNpmLock = existsSync(join(cwd, "package-lock.json"));
|
|
16
|
+
|
|
17
|
+
if (hasNpmLock) {
|
|
18
|
+
if (hasNpm) {
|
|
19
|
+
console.log(
|
|
20
|
+
"Using npm as package manager, as there is already a package-lock.json file."
|
|
21
|
+
);
|
|
22
|
+
return { ...NpmPackageManager, cwd };
|
|
23
|
+
} else if (hasYarn) {
|
|
24
|
+
console.log("Using yarn as package manager.");
|
|
25
|
+
console.warn(
|
|
26
|
+
"There is already a package-lock.json file but could not find npm on the PATH."
|
|
27
|
+
);
|
|
28
|
+
return { ...YarnPackageManager, cwd };
|
|
29
|
+
}
|
|
30
|
+
} else if (hasYarnLock) {
|
|
31
|
+
if (hasYarn) {
|
|
32
|
+
console.log(
|
|
33
|
+
"Using yarn as package manager, as there is already a yarn.lock file."
|
|
34
|
+
);
|
|
35
|
+
return { ...YarnPackageManager, cwd };
|
|
36
|
+
} else if (hasNpm) {
|
|
37
|
+
console.log("Using npm as package manager.");
|
|
38
|
+
console.warn(
|
|
39
|
+
"There is already a yarn.lock file but could not find yarn on the PATH."
|
|
40
|
+
);
|
|
41
|
+
return { ...NpmPackageManager, cwd };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (hasNpm) {
|
|
46
|
+
console.log("Using npm as package manager.");
|
|
47
|
+
return { ...NpmPackageManager, cwd };
|
|
48
|
+
} else if (hasYarn) {
|
|
49
|
+
console.log("Using yarn as package manager.");
|
|
50
|
+
return { ...YarnPackageManager, cwd };
|
|
51
|
+
} else {
|
|
52
|
+
throw new Error(
|
|
53
|
+
"Unable to find a package manager. Supported managers are: npm and yarn."
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get the name of the given `packageManager`.
|
|
60
|
+
*/
|
|
61
|
+
export function getPackageManagerName(packageManager: PackageManager): string {
|
|
62
|
+
return packageManager.type ?? "unknown";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Manage packages using npm
|
|
67
|
+
*/
|
|
68
|
+
const NpmPackageManager: PackageManager = {
|
|
69
|
+
cwd: process.cwd(),
|
|
70
|
+
type: "npm",
|
|
71
|
+
/** Add and install a new devDependency into the local package.json. */
|
|
72
|
+
async addDevDeps(...packages: string[]): Promise<void> {
|
|
73
|
+
await execa("npm", ["install", ...packages, "--save-dev"], {
|
|
74
|
+
stdio: "inherit",
|
|
75
|
+
cwd: this.cwd,
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
/** Install all the dependencies in the local package.json. */
|
|
80
|
+
async install(): Promise<void> {
|
|
81
|
+
await execa("npm", ["install"], {
|
|
82
|
+
stdio: "inherit",
|
|
83
|
+
cwd: this.cwd,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Manage packages using yarn
|
|
90
|
+
*/
|
|
91
|
+
const YarnPackageManager: PackageManager = {
|
|
92
|
+
cwd: process.cwd(),
|
|
93
|
+
type: "yarn",
|
|
94
|
+
/** Add and install a new devDependency into the local package.json. */
|
|
95
|
+
async addDevDeps(...packages: string[]): Promise<void> {
|
|
96
|
+
await execa("yarn", ["add", ...packages, "--dev"], {
|
|
97
|
+
stdio: "inherit",
|
|
98
|
+
cwd: this.cwd,
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
/** Install all the dependencies in the local package.json. */
|
|
103
|
+
async install(): Promise<void> {
|
|
104
|
+
await execa("yarn", ["install"], {
|
|
105
|
+
stdio: "inherit",
|
|
106
|
+
cwd: this.cwd,
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
async function supports(name: string): Promise<boolean> {
|
|
112
|
+
try {
|
|
113
|
+
execaCommandSync(`${name} --version`, { stdio: "ignore" });
|
|
114
|
+
return true;
|
|
115
|
+
} catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function supportsYarn(): Promise<boolean> {
|
|
121
|
+
return supports("yarn");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function supportsNpm(): Promise<boolean> {
|
|
125
|
+
return supports("npm");
|
|
126
|
+
}
|
package/src/pages.tsx
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
/* eslint-disable no-shadow */
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { execSync, spawn } from "child_process";
|
|
9
|
-
import { URL } from "url";
|
|
10
|
-
import { getType } from "mime";
|
|
11
|
-
import open from "open";
|
|
3
|
+
import { execSync, spawn } from "node:child_process";
|
|
4
|
+
import { existsSync, lstatSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { URL } from "node:url";
|
|
12
8
|
import { watch } from "chokidar";
|
|
13
|
-
import
|
|
9
|
+
import { getType } from "mime";
|
|
14
10
|
import { buildWorker } from "../pages/functions/buildWorker";
|
|
15
|
-
import type { Config } from "../pages/functions/routes";
|
|
16
|
-
import { writeRoutesModule } from "../pages/functions/routes";
|
|
17
11
|
import { generateConfigFromFileTree } from "../pages/functions/filepath-routing";
|
|
12
|
+
import { writeRoutesModule } from "../pages/functions/routes";
|
|
13
|
+
import openInBrowser from "./open-in-browser";
|
|
14
|
+
import { toUrlPath } from "./paths";
|
|
15
|
+
import type { Config } from "../pages/functions/routes";
|
|
16
|
+
import type { Headers, Request, fetch } from "@miniflare/core";
|
|
17
|
+
import type { BuildResult } from "esbuild";
|
|
18
|
+
import type { MiniflareOptions } from "miniflare";
|
|
19
|
+
import type { BuilderCallback } from "yargs";
|
|
18
20
|
|
|
19
21
|
// Defer importing miniflare until we really need it. This takes ~0.5s
|
|
20
22
|
// and also modifies some `stream/web` and `undici` prototypes, so we
|
|
21
23
|
// don't want to do this if pages commands aren't being called.
|
|
22
|
-
import type { Headers, Request, fetch } from "@miniflare/core";
|
|
23
|
-
import type { MiniflareOptions } from "miniflare";
|
|
24
24
|
|
|
25
|
-
const EXIT_CALLBACKS = [];
|
|
25
|
+
const EXIT_CALLBACKS: (() => void)[] = [];
|
|
26
26
|
const EXIT = (message?: string, code?: number) => {
|
|
27
27
|
if (message) console.log(message);
|
|
28
28
|
if (code) process.exitCode = code;
|
|
@@ -122,7 +122,9 @@ async function spawnProxyProcess({
|
|
|
122
122
|
},
|
|
123
123
|
}
|
|
124
124
|
);
|
|
125
|
-
EXIT_CALLBACKS.push(() =>
|
|
125
|
+
EXIT_CALLBACKS.push(() => {
|
|
126
|
+
proxy.kill();
|
|
127
|
+
});
|
|
126
128
|
|
|
127
129
|
proxy.stdout.on("data", (data) => {
|
|
128
130
|
console.log(`[proxy]: ${data}`);
|
|
@@ -642,16 +644,20 @@ const RUNNING_BUILDERS: BuildResult[] = [];
|
|
|
642
644
|
|
|
643
645
|
async function buildFunctions({
|
|
644
646
|
scriptPath,
|
|
647
|
+
outputConfigPath,
|
|
645
648
|
functionsDirectory,
|
|
646
649
|
minify = false,
|
|
647
650
|
sourcemap = false,
|
|
651
|
+
fallbackService = "ASSETS",
|
|
648
652
|
watch = false,
|
|
649
653
|
onEnd,
|
|
650
654
|
}: {
|
|
651
655
|
scriptPath: string;
|
|
656
|
+
outputConfigPath?: string;
|
|
652
657
|
functionsDirectory: string;
|
|
653
658
|
minify?: boolean;
|
|
654
659
|
sourcemap?: boolean;
|
|
660
|
+
fallbackService?: string;
|
|
655
661
|
watch?: boolean;
|
|
656
662
|
onEnd?: () => void;
|
|
657
663
|
}) {
|
|
@@ -660,12 +666,20 @@ async function buildFunctions({
|
|
|
660
666
|
);
|
|
661
667
|
|
|
662
668
|
const routesModule = join(tmpdir(), "./functionsRoutes.mjs");
|
|
669
|
+
const baseURL = toUrlPath("/");
|
|
663
670
|
|
|
664
671
|
const config: Config = await generateConfigFromFileTree({
|
|
665
672
|
baseDir: functionsDirectory,
|
|
666
|
-
baseURL
|
|
673
|
+
baseURL,
|
|
667
674
|
});
|
|
668
675
|
|
|
676
|
+
if (outputConfigPath) {
|
|
677
|
+
writeFileSync(
|
|
678
|
+
outputConfigPath,
|
|
679
|
+
JSON.stringify({ ...config, baseURL }, null, 2)
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
|
|
669
683
|
await writeRoutesModule({
|
|
670
684
|
config,
|
|
671
685
|
srcDir: functionsDirectory,
|
|
@@ -678,6 +692,7 @@ async function buildFunctions({
|
|
|
678
692
|
outfile: scriptPath,
|
|
679
693
|
minify,
|
|
680
694
|
sourcemap,
|
|
695
|
+
fallbackService,
|
|
681
696
|
watch,
|
|
682
697
|
onEnd,
|
|
683
698
|
})
|
|
@@ -738,6 +753,11 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
738
753
|
description: "Durable Object to bind (NAME=CLASS)",
|
|
739
754
|
alias: "o",
|
|
740
755
|
},
|
|
756
|
+
"live-reload": {
|
|
757
|
+
type: "boolean",
|
|
758
|
+
default: false,
|
|
759
|
+
description: "Auto reload HTML pages when change is detected",
|
|
760
|
+
},
|
|
741
761
|
// TODO: Miniflare user options
|
|
742
762
|
});
|
|
743
763
|
},
|
|
@@ -750,7 +770,8 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
750
770
|
binding: bindings = [],
|
|
751
771
|
kv: kvs = [],
|
|
752
772
|
do: durableObjects = [],
|
|
753
|
-
"
|
|
773
|
+
"live-reload": liveReload,
|
|
774
|
+
_: [_pages, _dev, ...remaining],
|
|
754
775
|
}) => {
|
|
755
776
|
if (!local) {
|
|
756
777
|
console.error("Only local mode is supported at the moment.");
|
|
@@ -774,7 +795,10 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
774
795
|
|
|
775
796
|
let miniflareArgs: MiniflareOptions = {};
|
|
776
797
|
|
|
777
|
-
let
|
|
798
|
+
let scriptReadyResolve: () => void;
|
|
799
|
+
const scriptReadyPromise = new Promise<void>(
|
|
800
|
+
(resolve) => (scriptReadyResolve = resolve)
|
|
801
|
+
);
|
|
778
802
|
|
|
779
803
|
if (usingFunctions) {
|
|
780
804
|
const scriptPath = join(tmpdir(), "./functionsWorker.js");
|
|
@@ -786,22 +810,19 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
786
810
|
functionsDirectory,
|
|
787
811
|
sourcemap: true,
|
|
788
812
|
watch: true,
|
|
789
|
-
onEnd: () =>
|
|
790
|
-
scriptReady = true;
|
|
791
|
-
},
|
|
813
|
+
onEnd: () => scriptReadyResolve(),
|
|
792
814
|
});
|
|
793
815
|
|
|
794
816
|
watch([functionsDirectory], {
|
|
795
817
|
persistent: true,
|
|
818
|
+
ignoreInitial: true,
|
|
796
819
|
}).on("all", async () => {
|
|
797
820
|
await buildFunctions({
|
|
798
821
|
scriptPath,
|
|
799
822
|
functionsDirectory,
|
|
800
823
|
sourcemap: true,
|
|
801
824
|
watch: true,
|
|
802
|
-
onEnd: () =>
|
|
803
|
-
scriptReady = true;
|
|
804
|
-
},
|
|
825
|
+
onEnd: () => scriptReadyResolve(),
|
|
805
826
|
});
|
|
806
827
|
});
|
|
807
828
|
|
|
@@ -809,6 +830,9 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
809
830
|
scriptPath,
|
|
810
831
|
};
|
|
811
832
|
} else {
|
|
833
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
834
|
+
scriptReadyResolve!();
|
|
835
|
+
|
|
812
836
|
const scriptPath =
|
|
813
837
|
directory !== undefined
|
|
814
838
|
? join(directory, singleWorkerScriptPath)
|
|
@@ -838,11 +862,19 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
838
862
|
const { Miniflare, Log, LogLevel } = await import("miniflare");
|
|
839
863
|
const { Response, fetch } = await import("@miniflare/core");
|
|
840
864
|
|
|
841
|
-
//
|
|
842
|
-
//
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
865
|
+
// Wait for esbuild to finish building before starting Miniflare.
|
|
866
|
+
// This must be before the call to `new Miniflare`, as that will
|
|
867
|
+
// asynchronously start loading the script. `await startServer()`
|
|
868
|
+
// internally just waits for that promise to resolve.
|
|
869
|
+
await scriptReadyPromise;
|
|
870
|
+
|
|
871
|
+
// `assetsFetch()` will only be called if there is `proxyPort` defined.
|
|
872
|
+
// We only define `proxyPort`, above, when there is no `directory` defined.
|
|
873
|
+
const assetsFetch =
|
|
874
|
+
directory !== undefined
|
|
875
|
+
? await generateAssetsFetch(directory)
|
|
876
|
+
: invalidAssetsFetch;
|
|
877
|
+
|
|
846
878
|
const miniflare = new Miniflare({
|
|
847
879
|
port,
|
|
848
880
|
watch: true,
|
|
@@ -863,17 +895,17 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
863
895
|
// User bindings
|
|
864
896
|
bindings: {
|
|
865
897
|
...Object.fromEntries(
|
|
866
|
-
bindings.map((binding) => binding.toString().split("="))
|
|
898
|
+
bindings.map((binding) => binding.toString().split("=", 1))
|
|
867
899
|
),
|
|
868
900
|
},
|
|
869
901
|
|
|
870
902
|
// env.ASSETS.fetch
|
|
871
903
|
serviceBindings: {
|
|
872
|
-
async ASSETS(request) {
|
|
904
|
+
async ASSETS(request: Request) {
|
|
873
905
|
if (proxyPort) {
|
|
874
906
|
try {
|
|
875
907
|
const url = new URL(request.url);
|
|
876
|
-
url.host = `
|
|
908
|
+
url.host = `localhost:${proxyPort}`;
|
|
877
909
|
return await fetch(url, request);
|
|
878
910
|
} catch (thrown) {
|
|
879
911
|
console.error(`Could not proxy request: ${thrown}`);
|
|
@@ -903,22 +935,27 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
903
935
|
kvPersist: true,
|
|
904
936
|
durableObjectsPersist: true,
|
|
905
937
|
cachePersist: true,
|
|
938
|
+
liveReload,
|
|
906
939
|
|
|
907
940
|
...miniflareArgs,
|
|
908
941
|
});
|
|
909
942
|
|
|
910
|
-
// Wait for esbuild to finish building the script
|
|
911
|
-
while (!scriptReady) {}
|
|
912
|
-
|
|
913
943
|
try {
|
|
914
944
|
// `startServer` might throw if user code contains errors
|
|
915
945
|
const server = await miniflare.startServer();
|
|
916
|
-
console.log(`Serving at http://
|
|
946
|
+
console.log(`Serving at http://localhost:${port}/`);
|
|
917
947
|
|
|
918
948
|
if (process.env.BROWSER !== "none") {
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
949
|
+
await openInBrowser(`http://localhost:${port}/`);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
if (directory !== undefined && liveReload) {
|
|
953
|
+
watch([directory], {
|
|
954
|
+
persistent: true,
|
|
955
|
+
ignoreInitial: true,
|
|
956
|
+
}).on("all", async () => {
|
|
957
|
+
await miniflare.reload();
|
|
958
|
+
});
|
|
922
959
|
}
|
|
923
960
|
|
|
924
961
|
EXIT_CALLBACKS.push(() => {
|
|
@@ -926,43 +963,83 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
926
963
|
miniflare.dispose().catch((err) => miniflare.log.error(err));
|
|
927
964
|
});
|
|
928
965
|
} catch (e) {
|
|
929
|
-
miniflare.log.error(e);
|
|
966
|
+
miniflare.log.error(e as Error);
|
|
930
967
|
EXIT("Could not start Miniflare.", 1);
|
|
931
968
|
}
|
|
932
969
|
}
|
|
933
970
|
)
|
|
934
|
-
.command("functions",
|
|
971
|
+
.command("functions", false, (yargs) =>
|
|
972
|
+
// we hide this command from help output because
|
|
973
|
+
// it's not meant to be used directly right now
|
|
935
974
|
yargs.command(
|
|
936
|
-
"build",
|
|
975
|
+
"build [directory]",
|
|
937
976
|
"Compile a folder of Cloudflare Pages Functions into a single Worker",
|
|
938
977
|
(yargs) =>
|
|
939
|
-
yargs
|
|
940
|
-
"
|
|
978
|
+
yargs
|
|
979
|
+
.positional("directory", {
|
|
941
980
|
type: "string",
|
|
942
|
-
default: "
|
|
943
|
-
description: "The
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
981
|
+
default: "functions",
|
|
982
|
+
description: "The directory of Pages Functions",
|
|
983
|
+
})
|
|
984
|
+
.options({
|
|
985
|
+
"script-path": {
|
|
986
|
+
type: "string",
|
|
987
|
+
default: "_worker.js",
|
|
988
|
+
description: "The location of the output Worker script",
|
|
989
|
+
},
|
|
990
|
+
"output-config-path": {
|
|
991
|
+
type: "string",
|
|
992
|
+
description: "The location for the output config file",
|
|
993
|
+
},
|
|
994
|
+
minify: {
|
|
995
|
+
type: "boolean",
|
|
996
|
+
default: false,
|
|
997
|
+
description: "Minify the output Worker script",
|
|
998
|
+
},
|
|
999
|
+
sourcemap: {
|
|
1000
|
+
type: "boolean",
|
|
1001
|
+
default: false,
|
|
1002
|
+
description:
|
|
1003
|
+
"Generate a sourcemap for the output Worker script",
|
|
1004
|
+
},
|
|
1005
|
+
"fallback-service": {
|
|
1006
|
+
type: "string",
|
|
1007
|
+
default: "ASSETS",
|
|
1008
|
+
description:
|
|
1009
|
+
"The service to fallback to at the end of the `next` chain. Setting to '' will fallback to the global `fetch`.",
|
|
1010
|
+
},
|
|
1011
|
+
watch: {
|
|
1012
|
+
type: "boolean",
|
|
1013
|
+
default: false,
|
|
1014
|
+
description:
|
|
1015
|
+
"Watch for changes to the functions and automatically rebuild the Worker script",
|
|
1016
|
+
},
|
|
1017
|
+
}),
|
|
1018
|
+
async ({
|
|
1019
|
+
directory,
|
|
1020
|
+
"script-path": scriptPath,
|
|
1021
|
+
"output-config-path": outputConfigPath,
|
|
1022
|
+
minify,
|
|
1023
|
+
sourcemap,
|
|
1024
|
+
fallbackService,
|
|
1025
|
+
watch,
|
|
1026
|
+
}) => {
|
|
959
1027
|
await buildFunctions({
|
|
960
1028
|
scriptPath,
|
|
961
|
-
|
|
1029
|
+
outputConfigPath,
|
|
1030
|
+
functionsDirectory: directory,
|
|
962
1031
|
minify,
|
|
963
1032
|
sourcemap,
|
|
1033
|
+
fallbackService,
|
|
1034
|
+
watch,
|
|
964
1035
|
});
|
|
965
1036
|
}
|
|
966
1037
|
)
|
|
967
1038
|
);
|
|
968
1039
|
};
|
|
1040
|
+
|
|
1041
|
+
const invalidAssetsFetch: typeof fetch = () => {
|
|
1042
|
+
throw new Error(
|
|
1043
|
+
"Trying to fetch assets directly when there is no `directory` option specified, and not in `local` mode."
|
|
1044
|
+
);
|
|
1045
|
+
};
|
package/src/parse.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import TOML from "@iarna/toml";
|
|
4
|
+
import { formatMessagesSync } from "esbuild";
|
|
5
|
+
|
|
6
|
+
export type Message = {
|
|
7
|
+
text: string;
|
|
8
|
+
location?: Location;
|
|
9
|
+
notes?: Message[];
|
|
10
|
+
kind?: "warning" | "error";
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type Location = File & {
|
|
14
|
+
line: number;
|
|
15
|
+
column: number;
|
|
16
|
+
length?: number;
|
|
17
|
+
lineText?: string;
|
|
18
|
+
suggestion?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type File = {
|
|
22
|
+
file?: string;
|
|
23
|
+
fileText?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Formats a `Message` using esbuild's pretty-printing algorithm.
|
|
28
|
+
*/
|
|
29
|
+
export function formatMessage(
|
|
30
|
+
{ text, notes, location, kind = "error" }: Message,
|
|
31
|
+
color = true
|
|
32
|
+
): string {
|
|
33
|
+
const input = { text, notes, location };
|
|
34
|
+
delete input.location?.fileText;
|
|
35
|
+
for (const note of notes ?? []) {
|
|
36
|
+
delete note.location?.fileText;
|
|
37
|
+
}
|
|
38
|
+
const lines = formatMessagesSync([input], {
|
|
39
|
+
color,
|
|
40
|
+
kind: kind,
|
|
41
|
+
terminalWidth: process.stderr.columns,
|
|
42
|
+
});
|
|
43
|
+
return lines.join("\n");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* An error that's thrown when something fails to parse.
|
|
48
|
+
*/
|
|
49
|
+
export class ParseError extends Error implements Message {
|
|
50
|
+
readonly text: string;
|
|
51
|
+
readonly notes: Message[];
|
|
52
|
+
readonly location?: Location;
|
|
53
|
+
readonly kind: "warning" | "error";
|
|
54
|
+
|
|
55
|
+
constructor({ text, notes, location, kind }: Message) {
|
|
56
|
+
super(text);
|
|
57
|
+
this.name = this.constructor.name;
|
|
58
|
+
this.text = text;
|
|
59
|
+
this.notes = notes ?? [];
|
|
60
|
+
this.location = location;
|
|
61
|
+
this.kind = kind ?? "error";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
66
|
+
|
|
67
|
+
const TOML_ERROR_NAME = "TomlError";
|
|
68
|
+
const TOML_ERROR_SUFFIX = " at row ";
|
|
69
|
+
|
|
70
|
+
type TomlError = Error & {
|
|
71
|
+
line: number;
|
|
72
|
+
col: number;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A wrapper around `TOML.parse` that throws a `ParseError`.
|
|
77
|
+
*/
|
|
78
|
+
export function parseTOML(input: string, file?: string): object {
|
|
79
|
+
try {
|
|
80
|
+
return TOML.parse(input);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
const { name, message, line, col } = err as TomlError;
|
|
83
|
+
if (name !== TOML_ERROR_NAME) {
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
const text = message.substring(0, message.lastIndexOf(TOML_ERROR_SUFFIX));
|
|
87
|
+
const lineText = input.split("\n")[line];
|
|
88
|
+
const location = {
|
|
89
|
+
lineText,
|
|
90
|
+
line: line + 1,
|
|
91
|
+
column: col - 1,
|
|
92
|
+
file,
|
|
93
|
+
fileText: input,
|
|
94
|
+
};
|
|
95
|
+
throw new ParseError({ text, location });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const JSON_ERROR_SUFFIX = " in JSON at position ";
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A wrapper around `JSON.parse` that throws a `ParseError`.
|
|
103
|
+
*/
|
|
104
|
+
export function parseJSON(input: string, file?: string): any {
|
|
105
|
+
try {
|
|
106
|
+
return JSON.parse(input);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
const { message } = err as Error;
|
|
109
|
+
const index = message.lastIndexOf(JSON_ERROR_SUFFIX);
|
|
110
|
+
if (index < 0) {
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
const text = message.substring(0, index);
|
|
114
|
+
const position = parseInt(
|
|
115
|
+
message.substring(index + JSON_ERROR_SUFFIX.length)
|
|
116
|
+
);
|
|
117
|
+
const location = indexLocation({ file, fileText: input }, position);
|
|
118
|
+
throw new ParseError({ text, location });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Reads a file and parses it based on its type.
|
|
124
|
+
*/
|
|
125
|
+
export function readFileSync(file: string): string {
|
|
126
|
+
try {
|
|
127
|
+
return fs.readFileSync(file, { encoding: "utf-8" });
|
|
128
|
+
} catch (err) {
|
|
129
|
+
const { message } = err as Error;
|
|
130
|
+
throw new ParseError({
|
|
131
|
+
text: `Could not read file: ${file}`,
|
|
132
|
+
notes: [
|
|
133
|
+
{
|
|
134
|
+
text: message.replace(file, resolve(file)),
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Calculates the line and column location from an index.
|
|
143
|
+
*/
|
|
144
|
+
export function indexLocation(file: File, index: number): Location {
|
|
145
|
+
let lineText,
|
|
146
|
+
line = 0,
|
|
147
|
+
column = 0,
|
|
148
|
+
cursor = 0;
|
|
149
|
+
const { fileText = "" } = file;
|
|
150
|
+
for (const row of fileText.split("\n")) {
|
|
151
|
+
line++;
|
|
152
|
+
cursor += row.length + 1;
|
|
153
|
+
if (cursor >= index) {
|
|
154
|
+
lineText = row;
|
|
155
|
+
column = row.length - (cursor - index);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return { lineText, line, column, ...file };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Guesses the line and column location of a search query.
|
|
164
|
+
*/
|
|
165
|
+
export function searchLocation(file: File, query: unknown): Location {
|
|
166
|
+
let lineText,
|
|
167
|
+
length,
|
|
168
|
+
line = 0,
|
|
169
|
+
column = 0;
|
|
170
|
+
const queryText = String(query);
|
|
171
|
+
const { fileText = "" } = file;
|
|
172
|
+
for (const content of fileText.split("\n")) {
|
|
173
|
+
line++;
|
|
174
|
+
const index = content.indexOf(queryText);
|
|
175
|
+
if (index >= 0) {
|
|
176
|
+
lineText = content;
|
|
177
|
+
column = index;
|
|
178
|
+
length = queryText.length;
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return { lineText, line, column, length, ...file };
|
|
183
|
+
}
|
package/src/paths.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { assert } from "console";
|
|
2
|
+
|
|
3
|
+
type DiscriminatedPath<Discriminator extends string> = string & {
|
|
4
|
+
_discriminator: Discriminator;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A branded string that expects to be URL compatible.
|
|
9
|
+
*
|
|
10
|
+
* Require this type when you want callers to ensure that they have converted file-path strings into URL-safe paths.
|
|
11
|
+
*/
|
|
12
|
+
export type UrlPath = DiscriminatedPath<"UrlPath">;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Convert a file-path string to a URL-path string.
|
|
16
|
+
*
|
|
17
|
+
* Use this helper to convert a `string` to a `UrlPath` when it is not clear whether the string needs normalizing.
|
|
18
|
+
* Replaces all back-slashes with forward-slashes, and throws an error if the path contains a drive letter (e.g. `C:`).
|
|
19
|
+
*/
|
|
20
|
+
export function toUrlPath(path: string): UrlPath {
|
|
21
|
+
assert(
|
|
22
|
+
!/^[a-z]:/i.test(path),
|
|
23
|
+
"Tried to convert a Windows file path with a drive to a URL path."
|
|
24
|
+
);
|
|
25
|
+
return path.replace(/\\/g, "/") as UrlPath;
|
|
26
|
+
}
|