@prisma/compute-sdk 0.43.0 → 0.45.0
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 +199 -1
- package/dist/api-client.d.ts.map +1 -1
- package/dist/api-client.js +18 -1
- package/dist/api-client.js.map +1 -1
- package/dist/archive.d.ts +2 -1
- package/dist/archive.d.ts.map +1 -1
- package/dist/archive.js +8 -4
- package/dist/archive.js.map +1 -1
- package/dist/build-strategy.d.ts +7 -0
- package/dist/build-strategy.d.ts.map +1 -1
- package/dist/build-strategy.js +3 -0
- package/dist/build-strategy.js.map +1 -1
- package/dist/compute-client.d.ts.map +1 -1
- package/dist/compute-client.js +1 -1
- package/dist/compute-client.js.map +1 -1
- package/dist/config/frameworks.d.ts +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js.map +1 -1
- package/dist/config/json-schema.d.ts +9 -3
- package/dist/config/json-schema.d.ts.map +1 -1
- package/dist/config/json-schema.js +7 -2
- package/dist/config/json-schema.js.map +1 -1
- package/dist/config/normalize.d.ts +2 -2
- package/dist/config/normalize.d.ts.map +1 -1
- package/dist/config/normalize.js +3 -14
- package/dist/config/normalize.js.map +1 -1
- package/dist/config/types.d.ts +12 -3
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js +5 -1
- package/dist/config/types.js.map +1 -1
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +3 -0
- package/dist/errors.js.map +1 -1
- package/dist/package-manager.d.ts +6 -0
- package/dist/package-manager.d.ts.map +1 -0
- package/dist/package-manager.js +21 -0
- package/dist/package-manager.js.map +1 -0
- package/dist/service/artifact/exclude.d.ts +6 -0
- package/dist/service/artifact/exclude.d.ts.map +1 -0
- package/dist/service/artifact/exclude.js +8 -0
- package/dist/service/artifact/exclude.js.map +1 -0
- package/dist/service/artifact/launcher-file.d.ts +3 -0
- package/dist/service/artifact/launcher-file.d.ts.map +1 -0
- package/dist/service/artifact/launcher-file.js +13 -0
- package/dist/service/artifact/launcher-file.js.map +1 -0
- package/dist/service/artifact/write-artifact.d.ts +16 -0
- package/dist/service/artifact/write-artifact.d.ts.map +1 -0
- package/dist/service/artifact/write-artifact.js +24 -0
- package/dist/service/artifact/write-artifact.js.map +1 -0
- package/dist/service/build/build-output.d.ts +15 -0
- package/dist/service/build/build-output.d.ts.map +1 -0
- package/dist/service/build/build-output.js +63 -0
- package/dist/service/build/build-output.js.map +1 -0
- package/dist/service/build/build-service.d.ts +30 -0
- package/dist/service/build/build-service.d.ts.map +1 -0
- package/dist/service/build/build-service.js +95 -0
- package/dist/service/build/build-service.js.map +1 -0
- package/dist/service/build/run-step.d.ts +32 -0
- package/dist/service/build/run-step.d.ts.map +1 -0
- package/dist/service/build/run-step.js +29 -0
- package/dist/service/build/run-step.js.map +1 -0
- package/dist/service/index.d.ts +7 -0
- package/dist/service/index.d.ts.map +1 -0
- package/dist/service/index.js +7 -0
- package/dist/service/index.js.map +1 -0
- package/dist/service/launcher-contract/events.d.ts +22 -0
- package/dist/service/launcher-contract/events.d.ts.map +1 -0
- package/dist/service/launcher-contract/events.js +39 -0
- package/dist/service/launcher-contract/events.js.map +1 -0
- package/dist/service/launcher-contract/start-plan.d.ts +20 -0
- package/dist/service/launcher-contract/start-plan.d.ts.map +1 -0
- package/dist/service/launcher-contract/start-plan.js +10 -0
- package/dist/service/launcher-contract/start-plan.js.map +1 -0
- package/dist/service/launcher.mjs +482 -0
- package/dist/service/settings/dev-command.d.ts +6 -0
- package/dist/service/settings/dev-command.d.ts.map +1 -0
- package/dist/service/settings/dev-command.js +46 -0
- package/dist/service/settings/dev-command.js.map +1 -0
- package/dist/service/settings/index.d.ts +5 -0
- package/dist/service/settings/index.d.ts.map +1 -0
- package/dist/service/settings/index.js +3 -0
- package/dist/service/settings/index.js.map +1 -0
- package/dist/service/settings/install.d.ts +15 -0
- package/dist/service/settings/install.d.ts.map +1 -0
- package/dist/service/settings/install.js +58 -0
- package/dist/service/settings/install.js.map +1 -0
- package/dist/service/settings/node-version.d.ts +9 -0
- package/dist/service/settings/node-version.d.ts.map +1 -0
- package/dist/service/settings/node-version.js +18 -0
- package/dist/service/settings/node-version.js.map +1 -0
- package/dist/service/settings/package-json.d.ts +14 -0
- package/dist/service/settings/package-json.d.ts.map +1 -0
- package/dist/service/settings/package-json.js +51 -0
- package/dist/service/settings/package-json.js.map +1 -0
- package/dist/service/settings/resolve.d.ts +9 -0
- package/dist/service/settings/resolve.d.ts.map +1 -0
- package/dist/service/settings/resolve.js +49 -0
- package/dist/service/settings/resolve.js.map +1 -0
- package/dist/service/settings/start-order.d.ts +21 -0
- package/dist/service/settings/start-order.d.ts.map +1 -0
- package/dist/service/settings/start-order.js +138 -0
- package/dist/service/settings/start-order.js.map +1 -0
- package/dist/service/settings/types.d.ts +66 -0
- package/dist/service/settings/types.d.ts.map +1 -0
- package/dist/service/settings/types.js +2 -0
- package/dist/service/settings/types.js.map +1 -0
- package/dist/workspace.d.ts +4 -1
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +12 -23
- package/dist/workspace.js.map +1 -1
- package/package.json +16 -2
- package/src/api-client.ts +27 -1
- package/src/archive.ts +9 -2
- package/src/build-strategy.ts +15 -1
- package/src/compute-client.ts +1 -0
- package/src/config/index.ts +1 -0
- package/src/config/json-schema.ts +8 -2
- package/src/config/normalize.ts +6 -29
- package/src/config/types.ts +13 -3
- package/src/errors.ts +13 -0
- package/src/package-manager.ts +27 -0
- package/src/service/artifact/exclude.ts +7 -0
- package/src/service/artifact/launcher-file.ts +18 -0
- package/src/service/artifact/write-artifact.ts +50 -0
- package/src/service/build/build-output.ts +95 -0
- package/src/service/build/build-service.ts +171 -0
- package/src/service/build/run-step.ts +65 -0
- package/src/service/index.ts +24 -0
- package/src/service/launcher/bin-targets.ts +143 -0
- package/src/service/launcher/bin-wrappers.ts +48 -0
- package/src/service/launcher/bun-shell.ts +17 -0
- package/src/service/launcher/listening-ports.ts +112 -0
- package/src/service/launcher/main.ts +54 -0
- package/src/service/launcher/package-manager-shims.ts +147 -0
- package/src/service/launcher/proc.ts +112 -0
- package/src/service/launcher/run-command.ts +82 -0
- package/src/service/launcher/serve-static.ts +102 -0
- package/src/service/launcher-contract/events.ts +53 -0
- package/src/service/launcher-contract/start-plan.ts +18 -0
- package/src/service/settings/dev-command.ts +65 -0
- package/src/service/settings/index.ts +17 -0
- package/src/service/settings/install.ts +105 -0
- package/src/service/settings/node-version.ts +26 -0
- package/src/service/settings/package-json.ts +71 -0
- package/src/service/settings/resolve.ts +80 -0
- package/src/service/settings/start-order.ts +213 -0
- package/src/service/settings/types.ts +73 -0
- package/src/workspace.ts +20 -35
package/src/archive.ts
CHANGED
|
@@ -11,6 +11,7 @@ const COMPUTE_MANIFEST_VERSION = "1" as const;
|
|
|
11
11
|
* - Files are added under the `bundle/` prefix.
|
|
12
12
|
* - Symlinks are preserved as symlink entries.
|
|
13
13
|
* - A synthetic `compute.manifest.json` entry is injected at the tar root.
|
|
14
|
+
* - Entries whose file or folder name `exclude` accepts are left out.
|
|
14
15
|
*
|
|
15
16
|
* Returns the gzipped archive as a Uint8Array.
|
|
16
17
|
*/
|
|
@@ -18,13 +19,14 @@ export async function createArchive(
|
|
|
18
19
|
directory: string,
|
|
19
20
|
entrypoint: string,
|
|
20
21
|
signal?: AbortSignal,
|
|
22
|
+
exclude?: (name: string) => boolean,
|
|
21
23
|
): Promise<Uint8Array> {
|
|
22
24
|
signal?.throwIfAborted();
|
|
23
25
|
const packer = pack();
|
|
24
26
|
const rootPath = path.resolve(directory);
|
|
25
27
|
|
|
26
28
|
// Walk the directory and add all files
|
|
27
|
-
await addDirectory(packer, rootPath, rootPath, "bundle", signal);
|
|
29
|
+
await addDirectory(packer, rootPath, rootPath, "bundle", signal, exclude);
|
|
28
30
|
|
|
29
31
|
// Inject the manifest as a synthetic entry
|
|
30
32
|
const manifest = JSON.stringify(
|
|
@@ -50,6 +52,7 @@ async function addDirectory(
|
|
|
50
52
|
fsPath: string,
|
|
51
53
|
tarPrefix: string,
|
|
52
54
|
signal?: AbortSignal,
|
|
55
|
+
exclude?: (name: string) => boolean,
|
|
53
56
|
): Promise<void> {
|
|
54
57
|
signal?.throwIfAborted();
|
|
55
58
|
const directoryPath = resolvePathWithinRoot(rootPath, fsPath);
|
|
@@ -57,6 +60,10 @@ async function addDirectory(
|
|
|
57
60
|
|
|
58
61
|
for (const entry of entries) {
|
|
59
62
|
signal?.throwIfAborted();
|
|
63
|
+
if (exclude?.(entry.name)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
60
67
|
const fullPath = path.join(directoryPath, entry.name);
|
|
61
68
|
const tarPath = `${tarPrefix}/${entry.name}`;
|
|
62
69
|
|
|
@@ -64,7 +71,7 @@ async function addDirectory(
|
|
|
64
71
|
const linkStat = await lstat(fullPath);
|
|
65
72
|
await addSymlink(packer, rootPath, fullPath, tarPath, linkStat, signal);
|
|
66
73
|
} else if (entry.isDirectory()) {
|
|
67
|
-
await addDirectory(packer, rootPath, fullPath, tarPath, signal);
|
|
74
|
+
await addDirectory(packer, rootPath, fullPath, tarPath, signal, exclude);
|
|
68
75
|
} else if (entry.isFile()) {
|
|
69
76
|
const fileStat = await lstat(fullPath);
|
|
70
77
|
await addFile(packer, rootPath, fullPath, tarPath, fileStat, signal);
|
package/src/build-strategy.ts
CHANGED
|
@@ -26,6 +26,12 @@ export interface BuildArtifact {
|
|
|
26
26
|
*/
|
|
27
27
|
defaultPortMapping?: PortMapping;
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Leaves a file or folder out of the uploaded archive when it returns true
|
|
31
|
+
* for the entry's name. Applies at every depth.
|
|
32
|
+
*/
|
|
33
|
+
exclude?(name: string): boolean;
|
|
34
|
+
|
|
29
35
|
/**
|
|
30
36
|
* Optional cleanup callback. Called by the core after the archive has been
|
|
31
37
|
* created and the artifact directory is no longer needed. Strategies that
|
|
@@ -49,10 +55,17 @@ export interface BuildStrategy {
|
|
|
49
55
|
export class PreBuilt implements BuildStrategy {
|
|
50
56
|
readonly #appPath: string;
|
|
51
57
|
readonly #entrypoint: string;
|
|
58
|
+
readonly #exclude: ((name: string) => boolean) | undefined;
|
|
52
59
|
|
|
53
|
-
constructor(options: {
|
|
60
|
+
constructor(options: {
|
|
61
|
+
appPath: string;
|
|
62
|
+
entrypoint: string;
|
|
63
|
+
/** Leaves out every file or folder whose name this accepts, at every depth. */
|
|
64
|
+
exclude?: (name: string) => boolean;
|
|
65
|
+
}) {
|
|
54
66
|
this.#appPath = options.appPath;
|
|
55
67
|
this.#entrypoint = options.entrypoint;
|
|
68
|
+
this.#exclude = options.exclude;
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
async canBuild(_signal?: AbortSignal): Promise<boolean> {
|
|
@@ -82,6 +95,7 @@ export class PreBuilt implements BuildStrategy {
|
|
|
82
95
|
return {
|
|
83
96
|
directory: this.#appPath,
|
|
84
97
|
entrypoint: normalized.split(path.sep).join("/"),
|
|
98
|
+
...(this.#exclude ? { exclude: this.#exclude } : {}),
|
|
85
99
|
};
|
|
86
100
|
}
|
|
87
101
|
}
|
package/src/compute-client.ts
CHANGED
package/src/config/index.ts
CHANGED
|
@@ -19,6 +19,12 @@ import { COMPUTE_FRAMEWORKS, COMPUTE_REGIONS } from "./types.ts";
|
|
|
19
19
|
export const COMPUTE_CONFIG_JSON_SCHEMA_URL =
|
|
20
20
|
"https://schemas.prisma.io/compute.schema.json";
|
|
21
21
|
|
|
22
|
+
const REGION_SCHEMA = {
|
|
23
|
+
type: "string",
|
|
24
|
+
minLength: 1,
|
|
25
|
+
examples: COMPUTE_REGIONS,
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
22
28
|
const ENV_SCHEMA = {
|
|
23
29
|
description:
|
|
24
30
|
"Environment variables for the deploy. A string is shorthand for a dotenv file path.",
|
|
@@ -84,7 +90,7 @@ const APP_SCHEMA = {
|
|
|
84
90
|
},
|
|
85
91
|
region: {
|
|
86
92
|
description: "App region override. Defaults to the project region.",
|
|
87
|
-
|
|
93
|
+
...REGION_SCHEMA,
|
|
88
94
|
},
|
|
89
95
|
root: {
|
|
90
96
|
description:
|
|
@@ -132,7 +138,7 @@ export const COMPUTE_CONFIG_JSON_SCHEMA = {
|
|
|
132
138
|
region: {
|
|
133
139
|
description:
|
|
134
140
|
"Default region used when creating new apps. Existing apps keep their current region.",
|
|
135
|
-
|
|
141
|
+
...REGION_SCHEMA,
|
|
136
142
|
},
|
|
137
143
|
app: APP_SCHEMA,
|
|
138
144
|
apps: {
|
package/src/config/normalize.ts
CHANGED
|
@@ -3,12 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import { Result, TaggedError } from "better-result";
|
|
4
4
|
|
|
5
5
|
import { frameworkByKey } from "./frameworks.ts";
|
|
6
|
-
import {
|
|
7
|
-
COMPUTE_FRAMEWORKS,
|
|
8
|
-
COMPUTE_REGIONS,
|
|
9
|
-
type ComputeFramework,
|
|
10
|
-
type ComputeRegion,
|
|
11
|
-
} from "./types.ts";
|
|
6
|
+
import { COMPUTE_FRAMEWORKS, type ComputeFramework } from "./types.ts";
|
|
12
7
|
|
|
13
8
|
export interface ComputeDeployTargetBuild {
|
|
14
9
|
/** Build command, null to skip the build step, undefined when not configured. */
|
|
@@ -27,7 +22,7 @@ export interface ComputeDeployTarget {
|
|
|
27
22
|
key: string | null;
|
|
28
23
|
name: string | null;
|
|
29
24
|
/** Region used when creating a new app, or null to use the consumer default. */
|
|
30
|
-
region:
|
|
25
|
+
region: string | null;
|
|
31
26
|
/** Normalized app directory relative to the config file, or null for the config directory. */
|
|
32
27
|
root: string | null;
|
|
33
28
|
framework: ComputeFramework | null;
|
|
@@ -137,7 +132,7 @@ export function normalizeComputeConfig(
|
|
|
137
132
|
const issues: string[] = [];
|
|
138
133
|
const targets: ComputeDeployTarget[] = [];
|
|
139
134
|
let kind: LoadedComputeConfig["kind"] = "single";
|
|
140
|
-
let projectRegion:
|
|
135
|
+
let projectRegion: string | null = null;
|
|
141
136
|
|
|
142
137
|
if (!isPlainObject(exported)) {
|
|
143
138
|
issues.push(
|
|
@@ -146,7 +141,7 @@ export function normalizeComputeConfig(
|
|
|
146
141
|
} else {
|
|
147
142
|
const hasApp = exported.app !== undefined;
|
|
148
143
|
const hasApps = exported.apps !== undefined;
|
|
149
|
-
projectRegion =
|
|
144
|
+
projectRegion = readOptionalNonEmptyString(
|
|
150
145
|
exported.region,
|
|
151
146
|
"region",
|
|
152
147
|
issues,
|
|
@@ -326,7 +321,7 @@ function normalizeAppEntry(
|
|
|
326
321
|
value: unknown,
|
|
327
322
|
label: string,
|
|
328
323
|
key: string | null,
|
|
329
|
-
projectRegion:
|
|
324
|
+
projectRegion: string | null,
|
|
330
325
|
issues: string[],
|
|
331
326
|
): ComputeDeployTarget | null {
|
|
332
327
|
if (!isPlainObject(value)) {
|
|
@@ -343,7 +338,7 @@ function normalizeAppEntry(
|
|
|
343
338
|
}
|
|
344
339
|
|
|
345
340
|
const name = readOptionalNonEmptyString(value.name, `${label}.name`, issues);
|
|
346
|
-
const appRegion =
|
|
341
|
+
const appRegion = readOptionalNonEmptyString(
|
|
347
342
|
value.region,
|
|
348
343
|
`${label}.region`,
|
|
349
344
|
issues,
|
|
@@ -641,24 +636,6 @@ function readOptionalNonEmptyString(
|
|
|
641
636
|
return value.trim();
|
|
642
637
|
}
|
|
643
638
|
|
|
644
|
-
function readOptionalComputeRegion(
|
|
645
|
-
value: unknown,
|
|
646
|
-
label: string,
|
|
647
|
-
issues: string[],
|
|
648
|
-
): ComputeRegion | null {
|
|
649
|
-
if (value === undefined) {
|
|
650
|
-
return null;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
const region = typeof value === "string" ? value.trim() : "";
|
|
654
|
-
if (!(COMPUTE_REGIONS as readonly string[]).includes(region)) {
|
|
655
|
-
issues.push(`\`${label}\` must be one of: ${COMPUTE_REGIONS.join(", ")}.`);
|
|
656
|
-
return null;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
return region as ComputeRegion;
|
|
660
|
-
}
|
|
661
|
-
|
|
662
639
|
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
663
640
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
664
641
|
}
|
package/src/config/types.ts
CHANGED
|
@@ -23,7 +23,11 @@ export const COMPUTE_FRAMEWORKS = [
|
|
|
23
23
|
|
|
24
24
|
export type ComputeFramework = (typeof COMPUTE_FRAMEWORKS)[number];
|
|
25
25
|
|
|
26
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Compute region identifiers that editors, prompts, and help text suggest.
|
|
28
|
+
* A config may name any region: the Management API decides which regions a
|
|
29
|
+
* workspace can use.
|
|
30
|
+
*/
|
|
27
31
|
export const COMPUTE_REGIONS = Object.freeze([
|
|
28
32
|
"us-east-1",
|
|
29
33
|
"us-west-1",
|
|
@@ -35,6 +39,12 @@ export const COMPUTE_REGIONS = Object.freeze([
|
|
|
35
39
|
|
|
36
40
|
export type ComputeRegion = (typeof COMPUTE_REGIONS)[number];
|
|
37
41
|
|
|
42
|
+
/**
|
|
43
|
+
* A region in a config: any string. The union with `ComputeRegion` keeps
|
|
44
|
+
* editor completion for the suggested regions.
|
|
45
|
+
*/
|
|
46
|
+
export type ComputeRegionId = ComputeRegion | (string & {});
|
|
47
|
+
|
|
38
48
|
export interface ComputeEnvConfig {
|
|
39
49
|
/** Dotenv file path(s) resolved relative to the config file directory. */
|
|
40
50
|
file?: string | string[];
|
|
@@ -59,7 +69,7 @@ export interface ComputeAppConfig {
|
|
|
59
69
|
/** Deployed app name. Defaults to the `apps` key, then consumer-side inference. */
|
|
60
70
|
name?: string;
|
|
61
71
|
/** App region override. Defaults to the project region, then consumer-side inference. */
|
|
62
|
-
region?:
|
|
72
|
+
region?: ComputeRegionId;
|
|
63
73
|
/** App directory relative to the config file. Defaults to the config file directory. */
|
|
64
74
|
root?: string;
|
|
65
75
|
/** Framework to deploy. Defaults to detection from the app directory. */
|
|
@@ -79,7 +89,7 @@ export interface ComputeAppConfig {
|
|
|
79
89
|
*/
|
|
80
90
|
export interface ComputeConfigDefaults {
|
|
81
91
|
/** Default region used when creating new apps. Existing apps keep their current region. */
|
|
82
|
-
region?:
|
|
92
|
+
region?: ComputeRegionId;
|
|
83
93
|
}
|
|
84
94
|
|
|
85
95
|
/**
|
package/src/errors.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { TaggedError } from "better-result";
|
|
2
|
+
import type { BuildStep } from "./service/build/run-step.ts";
|
|
3
|
+
import type { ServiceBuildSettings } from "./service/settings/types.ts";
|
|
2
4
|
|
|
3
5
|
export class AuthenticationError extends TaggedError("AuthenticationError")<{
|
|
4
6
|
statusCode: 401;
|
|
@@ -187,3 +189,14 @@ export type DeploymentOperationError =
|
|
|
187
189
|
| ApiError
|
|
188
190
|
| TimeoutError
|
|
189
191
|
| DeploymentFailedError;
|
|
192
|
+
|
|
193
|
+
/** A build of one Service that produced no Artifact. `code` is stable across releases. */
|
|
194
|
+
export class ServiceBuildError extends TaggedError("ServiceBuildError")<{
|
|
195
|
+
code: "install_failed" | "build_failed" | "no_start";
|
|
196
|
+
/** For the user. */
|
|
197
|
+
message: string;
|
|
198
|
+
settings: ServiceBuildSettings;
|
|
199
|
+
steps: BuildStep[];
|
|
200
|
+
/** The end of the failed command's output, under a line that gives its exit code. */
|
|
201
|
+
output?: string;
|
|
202
|
+
}>() {}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// The package manager facts every part of the SDK shares. This file imports nothing,
|
|
2
|
+
// so the Build rules can use it in a browser or a Worker.
|
|
3
|
+
|
|
4
|
+
export type PackageManager = "bun" | "pnpm" | "yarn" | "npm";
|
|
5
|
+
|
|
6
|
+
/** Each lockfile and the package manager that writes it. When a folder holds several, the first one wins. */
|
|
7
|
+
export const LOCKFILES: ReadonlyArray<
|
|
8
|
+
readonly [file: string, packageManager: PackageManager]
|
|
9
|
+
> = [
|
|
10
|
+
["bun.lock", "bun"],
|
|
11
|
+
["bun.lockb", "bun"],
|
|
12
|
+
["pnpm-lock.yaml", "pnpm"],
|
|
13
|
+
["yarn.lock", "yarn"],
|
|
14
|
+
["package-lock.json", "npm"],
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
/** The package manager a `packageManager` field such as `pnpm@10.1.0` names, or null when it names none. */
|
|
18
|
+
export function packageManagerFromField(value: unknown): PackageManager | null {
|
|
19
|
+
if (typeof value !== "string") {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const name = value.split("@")[0];
|
|
24
|
+
return name === "bun" || name === "pnpm" || name === "yarn" || name === "npm"
|
|
25
|
+
? name
|
|
26
|
+
: null;
|
|
27
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True for a file or folder that never goes into an Artifact, at any depth.
|
|
3
|
+
* A CI checkout keeps a token in `.git`, and dotenv files hold local secrets.
|
|
4
|
+
*/
|
|
5
|
+
export function excludedFromArtifact(name: string): boolean {
|
|
6
|
+
return name === ".git" || name === ".env" || name.startsWith(".env.");
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
/** The one-file Launcher, relative to this folder. `src/` and `dist/` hold this folder at the same depth, so one path fits both. */
|
|
5
|
+
const BUNDLED_LAUNCHER = "../../../dist/service/launcher.mjs";
|
|
6
|
+
|
|
7
|
+
/** The path of the Launcher as one file, ready to copy into an Artifact. */
|
|
8
|
+
export function launcherFile(): string {
|
|
9
|
+
const file = fileURLToPath(new URL(BUNDLED_LAUNCHER, import.meta.url));
|
|
10
|
+
|
|
11
|
+
if (!existsSync(file)) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`The Launcher is not built: ${file} does not exist. Run "bun scripts/bundle-launcher.ts" in the SDK package to build it.`,
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return file;
|
|
18
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { copyFileSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
LAUNCHER_FILE,
|
|
5
|
+
START_PLAN_FILE,
|
|
6
|
+
type StartPlan,
|
|
7
|
+
} from "../launcher-contract/start-plan.ts";
|
|
8
|
+
import type { Start } from "../settings/types.ts";
|
|
9
|
+
import { launcherFile } from "./launcher-file.ts";
|
|
10
|
+
|
|
11
|
+
/** What a build hands to the caller: a folder and the file Compute starts in it. */
|
|
12
|
+
export interface ServiceArtifact {
|
|
13
|
+
/** The folder to copy or upload, without what `excludedFromArtifact` names. */
|
|
14
|
+
dir: string;
|
|
15
|
+
/** The file Compute starts, relative to `dir`. */
|
|
16
|
+
entrypoint: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Picks the Artifact folder, and writes the Launcher and the Start plan into it. */
|
|
20
|
+
export function writeArtifact(options: {
|
|
21
|
+
serviceDir: string;
|
|
22
|
+
start: Start;
|
|
23
|
+
/** The port the user stated, or null. */
|
|
24
|
+
port: number | null;
|
|
25
|
+
}): ServiceArtifact {
|
|
26
|
+
const dir = artifactDir(options.serviceDir, options.start);
|
|
27
|
+
|
|
28
|
+
mkdirSync(join(dir, dirname(LAUNCHER_FILE)), { recursive: true });
|
|
29
|
+
copyFileSync(launcherFile(), join(dir, LAUNCHER_FILE));
|
|
30
|
+
writeFileSync(
|
|
31
|
+
join(dir, START_PLAN_FILE),
|
|
32
|
+
JSON.stringify(startPlan(options.start, options.port)),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return { dir, entrypoint: LAUNCHER_FILE };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A static site needs no source and no `node_modules`, so its Artifact is the built folder alone. */
|
|
39
|
+
function artifactDir(serviceDir: string, start: Start): string {
|
|
40
|
+
return start.kind === "static" ? join(serviceDir, start.dir) : serviceDir;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function startPlan(start: Start, port: number | null): StartPlan {
|
|
44
|
+
if (start.kind === "static") {
|
|
45
|
+
// The Artifact of a static site is the site's own folder, so the plan serves the Artifact folder itself.
|
|
46
|
+
return { kind: "static", dir: ".", port };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { kind: "command", command: start.command, port };
|
|
50
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Finds what the build created by comparing the Service folder before and after the build.
|
|
2
|
+
// The Build rules must not guess output folders by framework.
|
|
3
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import type { BuildOutput } from "../settings/types.ts";
|
|
6
|
+
|
|
7
|
+
/** The folders of the Service folder at one moment. Taken before the build, it tells what the build created. */
|
|
8
|
+
export interface FolderSnapshot {
|
|
9
|
+
topLevelDirs: Set<string>;
|
|
10
|
+
staticDirs: Set<string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Folders that never hold build output. */
|
|
14
|
+
const SKIPPED_DIRS = new Set(["node_modules", ".git"]);
|
|
15
|
+
|
|
16
|
+
/** How many levels below the Service folder a static site is looked for. */
|
|
17
|
+
const STATIC_SITE_DEPTH = 3;
|
|
18
|
+
|
|
19
|
+
/** How many of the scripts and styles a page links to are checked. */
|
|
20
|
+
const CHECKED_LINKS = 5;
|
|
21
|
+
|
|
22
|
+
/** A script or style the page loads from its own site, such as `src="/assets/app.js"`. The group is the path. */
|
|
23
|
+
const LOCAL_SCRIPT_OR_STYLE =
|
|
24
|
+
/(?:src|href)="(\/[^"/][^"?#]*\.(?:js|mjs|css))["?#]/g;
|
|
25
|
+
|
|
26
|
+
export function snapshotFolders(serviceDir: string): FolderSnapshot {
|
|
27
|
+
return {
|
|
28
|
+
topLevelDirs: new Set(topLevelDirs(serviceDir)),
|
|
29
|
+
staticDirs: new Set(dirsWithIndexHtml(serviceDir, STATIC_SITE_DEPTH)),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What the build created: the folders that are in the Service folder now and were not in the snapshot. */
|
|
34
|
+
export function buildOutputSince(
|
|
35
|
+
serviceDir: string,
|
|
36
|
+
before: FolderSnapshot,
|
|
37
|
+
): BuildOutput {
|
|
38
|
+
return {
|
|
39
|
+
newDirs: topLevelDirs(serviceDir).filter(
|
|
40
|
+
(dir) => !before.topLevelDirs.has(dir),
|
|
41
|
+
),
|
|
42
|
+
newStaticDirs: dirsWithIndexHtml(serviceDir, STATIC_SITE_DEPTH)
|
|
43
|
+
.filter((dir) => !before.staticDirs.has(dir))
|
|
44
|
+
.filter((dir) => isSelfContained(join(serviceDir, dir)))
|
|
45
|
+
.sort(shallowestFirst),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* True when the site in `dir` holds the first scripts and styles its `index.html` links to.
|
|
51
|
+
* A page copied out of a server build does not, and cannot be served alone.
|
|
52
|
+
*/
|
|
53
|
+
export function isSelfContained(dir: string): boolean {
|
|
54
|
+
const html = readFileSync(join(dir, "index.html"), "utf8");
|
|
55
|
+
const links = [...html.matchAll(LOCAL_SCRIPT_OR_STYLE)]
|
|
56
|
+
.map((match) => match[1] ?? "")
|
|
57
|
+
.slice(0, CHECKED_LINKS);
|
|
58
|
+
|
|
59
|
+
return links.every((link) => existsSync(join(dir, link)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function topLevelDirs(serviceDir: string): string[] {
|
|
63
|
+
return readdirSync(serviceDir, { withFileTypes: true })
|
|
64
|
+
.filter((entry) => entry.isDirectory() && !SKIPPED_DIRS.has(entry.name))
|
|
65
|
+
.map((entry) => entry.name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Folders that hold an `index.html`, relative to `serviceDir`, at most `depth` levels below `dir`. */
|
|
69
|
+
function dirsWithIndexHtml(
|
|
70
|
+
serviceDir: string,
|
|
71
|
+
depth: number,
|
|
72
|
+
dir = ".",
|
|
73
|
+
): string[] {
|
|
74
|
+
const found: string[] = [];
|
|
75
|
+
|
|
76
|
+
for (const entry of readdirSync(join(serviceDir, dir), {
|
|
77
|
+
withFileTypes: true,
|
|
78
|
+
})) {
|
|
79
|
+
if (entry.isFile() && entry.name === "index.html") {
|
|
80
|
+
found.push(dir);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (entry.isDirectory() && !SKIPPED_DIRS.has(entry.name) && depth > 0) {
|
|
84
|
+
found.push(
|
|
85
|
+
...dirsWithIndexHtml(serviceDir, depth - 1, join(dir, entry.name)),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return found;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function shallowestFirst(a: string, b: string): number {
|
|
94
|
+
return a.split("/").length - b.split("/").length || a.localeCompare(b);
|
|
95
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Builds one Service folder into an Artifact. Runs on the Build runner: it needs a shell,
|
|
2
|
+
// the repository's package manager, and the checkout. It never starts the Service.
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { Result } from "better-result";
|
|
6
|
+
import { ServiceBuildError } from "../../errors.ts";
|
|
7
|
+
import {
|
|
8
|
+
type ServiceArtifact,
|
|
9
|
+
writeArtifact,
|
|
10
|
+
} from "../artifact/write-artifact.ts";
|
|
11
|
+
import {
|
|
12
|
+
resolveServiceBuildSettings,
|
|
13
|
+
resolveStartAfterBuild,
|
|
14
|
+
} from "../settings/resolve.ts";
|
|
15
|
+
import type {
|
|
16
|
+
BuildOutput,
|
|
17
|
+
ReadFile,
|
|
18
|
+
Resolved,
|
|
19
|
+
ServiceBuildSettings,
|
|
20
|
+
ServiceConfig,
|
|
21
|
+
Start,
|
|
22
|
+
} from "../settings/types.ts";
|
|
23
|
+
import { buildOutputSince, snapshotFolders } from "./build-output.ts";
|
|
24
|
+
import {
|
|
25
|
+
type BuildStep,
|
|
26
|
+
runStep,
|
|
27
|
+
type StepRun,
|
|
28
|
+
type StepToRun,
|
|
29
|
+
} from "./run-step.ts";
|
|
30
|
+
|
|
31
|
+
export interface BuildServiceOptions {
|
|
32
|
+
/** The Service folder. */
|
|
33
|
+
dir: string;
|
|
34
|
+
config?: ServiceConfig;
|
|
35
|
+
/** Extra variables for the install and build commands. */
|
|
36
|
+
env?: Record<string, string>;
|
|
37
|
+
/** Receives each command as it starts, and its output. */
|
|
38
|
+
log?: (text: string) => void;
|
|
39
|
+
signal?: AbortSignal;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface BuiltService {
|
|
43
|
+
/** The Build settings, with the start resolved. */
|
|
44
|
+
settings: ServiceBuildSettings;
|
|
45
|
+
start: Resolved<Start>;
|
|
46
|
+
steps: BuildStep[];
|
|
47
|
+
artifact: ServiceArtifact;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** What makes a `ServiceBuildError`, beside the Build settings and the steps. */
|
|
51
|
+
type Failure = Pick<ServiceBuildError, "code" | "message" | "output">;
|
|
52
|
+
|
|
53
|
+
/** The build failures have stable codes: the CLI, Console and the fix task key on them. */
|
|
54
|
+
const STEP_FAILURES = {
|
|
55
|
+
install: { code: "install_failed", label: "Install" },
|
|
56
|
+
build: { code: "build_failed", label: "Build" },
|
|
57
|
+
} as const;
|
|
58
|
+
|
|
59
|
+
const NO_START: Failure = {
|
|
60
|
+
code: "no_start",
|
|
61
|
+
message:
|
|
62
|
+
"No start command. Add a start script to package.json, or set start or static in prisma.config.ts.",
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Runs the Service's install and build commands, decides how the Service starts, and writes the Artifact.
|
|
67
|
+
*
|
|
68
|
+
* A failed install, a failed build and a missing start are expected outcomes:
|
|
69
|
+
* they come back as a `ServiceBuildError`, not as a thrown error.
|
|
70
|
+
*/
|
|
71
|
+
export async function buildService(
|
|
72
|
+
options: BuildServiceOptions,
|
|
73
|
+
): Promise<Result<BuiltService, ServiceBuildError>> {
|
|
74
|
+
const { dir } = options;
|
|
75
|
+
const read = readFrom(dir);
|
|
76
|
+
const settings = await resolveServiceBuildSettings(
|
|
77
|
+
read,
|
|
78
|
+
options.config,
|
|
79
|
+
options.signal,
|
|
80
|
+
);
|
|
81
|
+
const steps: BuildStep[] = [];
|
|
82
|
+
const fail = (failure: Failure) =>
|
|
83
|
+
Result.err(new ServiceBuildError({ ...failure, settings, steps }));
|
|
84
|
+
|
|
85
|
+
const output = await installAndBuild(settings, options, steps);
|
|
86
|
+
if (output.isErr()) {
|
|
87
|
+
return fail(output.error);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const start =
|
|
91
|
+
settings.start === "after build"
|
|
92
|
+
? await resolveStartAfterBuild(read, output.value, options.signal)
|
|
93
|
+
: settings.start;
|
|
94
|
+
if (start === null) {
|
|
95
|
+
return fail(NO_START);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const port = settings.port?.value ?? null;
|
|
99
|
+
const artifact = writeArtifact({ serviceDir: dir, start: start.value, port });
|
|
100
|
+
|
|
101
|
+
return Result.ok({
|
|
102
|
+
settings: { ...settings, start },
|
|
103
|
+
start,
|
|
104
|
+
steps,
|
|
105
|
+
artifact,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Runs the install, then the build. Returns what the build created, or the step that failed. */
|
|
110
|
+
async function installAndBuild(
|
|
111
|
+
settings: ServiceBuildSettings,
|
|
112
|
+
options: BuildServiceOptions,
|
|
113
|
+
steps: BuildStep[],
|
|
114
|
+
): Promise<Result<BuildOutput, Failure>> {
|
|
115
|
+
const { dir, env, signal } = options;
|
|
116
|
+
const run: StepRun = { dir, steps, log: options.log ?? (() => {}), signal };
|
|
117
|
+
|
|
118
|
+
const install = installStep(settings, env);
|
|
119
|
+
const installed = await runStep(install, run);
|
|
120
|
+
if (!installed.ok) {
|
|
121
|
+
return Result.err(stepFailed(install, installed.output));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const beforeBuild = snapshotFolders(dir);
|
|
125
|
+
const build = buildStep(settings, env);
|
|
126
|
+
const built = await runStep(build, run);
|
|
127
|
+
if (!built.ok) {
|
|
128
|
+
return Result.err(stepFailed(build, built.output));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Result.ok(buildOutputSince(dir, beforeBuild));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The build runs in production mode. The caller's variables win over these. */
|
|
135
|
+
function buildStep(
|
|
136
|
+
settings: ServiceBuildSettings,
|
|
137
|
+
extraEnv: Record<string, string> = {},
|
|
138
|
+
): StepToRun {
|
|
139
|
+
return {
|
|
140
|
+
name: "build",
|
|
141
|
+
command: settings.build.value,
|
|
142
|
+
env: { ...process.env, CI: "true", NODE_ENV: "production", ...extraEnv },
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The install never runs in production mode: dev dependencies hold the build tools.
|
|
148
|
+
* The caller's variables do not change that, because a package manager in production mode skips those tools.
|
|
149
|
+
*/
|
|
150
|
+
function installStep(
|
|
151
|
+
settings: ServiceBuildSettings,
|
|
152
|
+
extraEnv: Record<string, string> = {},
|
|
153
|
+
): StepToRun {
|
|
154
|
+
return {
|
|
155
|
+
name: "install",
|
|
156
|
+
command: settings.install.value,
|
|
157
|
+
env: { ...process.env, CI: "true", ...extraEnv, NODE_ENV: "development" },
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function stepFailed(step: StepToRun, output: string): Failure {
|
|
162
|
+
const { code, label } = STEP_FAILURES[step.name];
|
|
163
|
+
|
|
164
|
+
return { code, message: `${label} failed: ${step.command}`, output };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Reads the files of the Service folder for the Build rules. */
|
|
168
|
+
function readFrom(dir: string): ReadFile {
|
|
169
|
+
return (path) =>
|
|
170
|
+
existsSync(join(dir, path)) ? readFileSync(join(dir, path), "utf8") : null;
|
|
171
|
+
}
|