@prisma/compute-sdk 0.48.0 → 0.49.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.
Files changed (61) hide show
  1. package/README.md +22 -2
  2. package/dist/service/build/build-service.d.ts +1 -1
  3. package/dist/service/build/build-service.d.ts.map +1 -1
  4. package/dist/service/build/build-service.js +14 -5
  5. package/dist/service/build/build-service.js.map +1 -1
  6. package/dist/service/index.d.ts +1 -1
  7. package/dist/service/index.d.ts.map +1 -1
  8. package/dist/service/index.js.map +1 -1
  9. package/dist/service/output/build-output.d.ts +47 -0
  10. package/dist/service/output/build-output.d.ts.map +1 -0
  11. package/dist/service/output/build-output.js +97 -0
  12. package/dist/service/output/build-output.js.map +1 -0
  13. package/dist/service/output/copy.d.ts +15 -0
  14. package/dist/service/output/copy.d.ts.map +1 -0
  15. package/dist/service/output/copy.js +59 -0
  16. package/dist/service/output/copy.js.map +1 -0
  17. package/dist/service/output/entrypoint.d.ts +20 -0
  18. package/dist/service/output/entrypoint.d.ts.map +1 -0
  19. package/dist/service/output/entrypoint.js +183 -0
  20. package/dist/service/output/entrypoint.js.map +1 -0
  21. package/dist/service/output/source-files.d.ts +12 -0
  22. package/dist/service/output/source-files.d.ts.map +1 -0
  23. package/dist/service/output/source-files.js +46 -0
  24. package/dist/service/output/source-files.js.map +1 -0
  25. package/dist/service/output/trace/bins.d.ts +9 -0
  26. package/dist/service/output/trace/bins.d.ts.map +1 -0
  27. package/dist/service/output/trace/bins.js +26 -0
  28. package/dist/service/output/trace/bins.js.map +1 -0
  29. package/dist/service/output/trace/imports.d.ts +8 -0
  30. package/dist/service/output/trace/imports.d.ts.map +1 -0
  31. package/dist/service/output/trace/imports.js +24 -0
  32. package/dist/service/output/trace/imports.js.map +1 -0
  33. package/dist/service/output/trace/optional-peer-dependencies.d.ts +13 -0
  34. package/dist/service/output/trace/optional-peer-dependencies.d.ts.map +1 -0
  35. package/dist/service/output/trace/optional-peer-dependencies.js +34 -0
  36. package/dist/service/output/trace/optional-peer-dependencies.js.map +1 -0
  37. package/dist/service/output/trace/packages.d.ts +22 -0
  38. package/dist/service/output/trace/packages.d.ts.map +1 -0
  39. package/dist/service/output/trace/packages.js +50 -0
  40. package/dist/service/output/trace/packages.js.map +1 -0
  41. package/dist/service/output/trace/trace.d.ts +32 -0
  42. package/dist/service/output/trace/trace.d.ts.map +1 -0
  43. package/dist/service/output/trace/trace.js +53 -0
  44. package/dist/service/output/trace/trace.js.map +1 -0
  45. package/package.json +1 -1
  46. package/src/service/build/build-service.ts +15 -8
  47. package/src/service/index.ts +1 -1
  48. package/src/service/output/build-output.ts +174 -0
  49. package/src/service/output/copy.ts +69 -0
  50. package/src/service/output/entrypoint.ts +250 -0
  51. package/src/service/output/source-files.ts +64 -0
  52. package/src/service/output/trace/bins.ts +39 -0
  53. package/src/service/output/trace/imports.ts +37 -0
  54. package/src/service/output/trace/optional-peer-dependencies.ts +50 -0
  55. package/src/service/output/trace/packages.ts +81 -0
  56. package/src/service/output/trace/trace.ts +99 -0
  57. package/dist/service/output/write-build-output.d.ts +0 -16
  58. package/dist/service/output/write-build-output.d.ts.map +0 -1
  59. package/dist/service/output/write-build-output.js +0 -24
  60. package/dist/service/output/write-build-output.js.map +0 -1
  61. package/src/service/output/write-build-output.ts +0 -50
@@ -0,0 +1,46 @@
1
+ // The files of a Service that are its own: the repository's files and the output directories.
2
+ import { readdir } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { excludedFromBuildOutput } from "./exclude.js";
5
+ /** Build caches inside an output directory. Nothing reads them at run time. */
6
+ const CACHE_FOLDERS = new Set(["cache", ".cache"]);
7
+ /**
8
+ * Every file of the Service folder except the installed `node_modules`, `.prisma`,
9
+ * and what `excludedFromBuildOutput` names.
10
+ *
11
+ * An output directory is kept with a `node_modules` folder of its own: Nuxt and
12
+ * Next.js put the packages their server needs there. Its cache folders are left out.
13
+ */
14
+ export async function sourceFiles(serviceDir, outputDirectories, signal) {
15
+ const outputDirs = new Set(outputDirectories.newDirs);
16
+ const found = [];
17
+ async function walk(dir, inOutputDir) {
18
+ signal?.throwIfAborted();
19
+ for (const entry of await readdir(dir, { withFileTypes: true })) {
20
+ const full = path.join(dir, entry.name);
21
+ if (excludedFromBuildOutput(entry.name)) {
22
+ continue;
23
+ }
24
+ if (!entry.isDirectory()) {
25
+ found.push(full);
26
+ continue;
27
+ }
28
+ const atRoot = dir === serviceDir;
29
+ const output = inOutputDir || (atRoot && outputDirs.has(entry.name));
30
+ const left = (entry.name === "node_modules" && !output) ||
31
+ (atRoot && entry.name === ".prisma") ||
32
+ (inOutputDir && CACHE_FOLDERS.has(entry.name));
33
+ if (!left) {
34
+ await walk(full, output);
35
+ }
36
+ }
37
+ }
38
+ await walk(serviceDir, false);
39
+ return found;
40
+ }
41
+ /** True when `file` is in one of the output directories. */
42
+ export function inOutputDirectory(serviceDir, outputDirectories, file) {
43
+ const [top] = path.relative(serviceDir, file).split(path.sep);
44
+ return top !== undefined && outputDirectories.newDirs.includes(top);
45
+ }
46
+ //# sourceMappingURL=source-files.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source-files.js","sourceRoot":"","sources":["../../../src/service/output/source-files.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEvD,+EAA+E;AAC/E,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;AAEnD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,UAAkB,EAClB,iBAAoC,EACpC,MAAoB;IAEpB,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACtD,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,KA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,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAE9D,OAAO,GAAG,KAAK,SAAS,IAAI,iBAAiB,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AACtE,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { type InstalledPackage } from "./packages.ts";
2
+ /**
3
+ * The packages of the bins, each with the packages it depends on.
4
+ *
5
+ * A bin loads its own files by path, which imports do not show: tracing `tsx` misses
6
+ * a file it needs. So the package of a bin goes into the build output whole.
7
+ */
8
+ export declare function binPackages(serviceDir: string, bins: string[]): Map<string, InstalledPackage>;
9
+ //# sourceMappingURL=bins.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bins.d.ts","sourceRoot":"","sources":["../../../../src/service/output/trace/bins.ts"],"names":[],"mappings":"AAGA,OAAO,EAGL,KAAK,gBAAgB,EAGtB,MAAM,eAAe,CAAC;AAEvB;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EAAE,GACb,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAkB/B"}
@@ -0,0 +1,26 @@
1
+ // A bin the start command runs, such as `tsx` or `react-router-serve`.
2
+ import { realpathSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { addWithDependencies, findPackage, packageDirOf, readManifest, } from "./packages.js";
5
+ /**
6
+ * The packages of the bins, each with the packages it depends on.
7
+ *
8
+ * A bin loads its own files by path, which imports do not show: tracing `tsx` misses
9
+ * a file it needs. So the package of a bin goes into the build output whole.
10
+ */
11
+ export function binPackages(serviceDir, bins) {
12
+ const found = new Map();
13
+ for (const name of bins) {
14
+ const bin = path.join(serviceDir, "node_modules", ".bin", name);
15
+ const dir = packageDirOf(realpathSync(bin));
16
+ if (dir === null) {
17
+ continue;
18
+ }
19
+ // Under pnpm the package is reached through a link in `node_modules`, which the bin's own link points at.
20
+ const packageName = readManifest(dir).name;
21
+ const installed = packageName === undefined ? null : findPackage(serviceDir, packageName);
22
+ addWithDependencies(installed ?? { link: dir, dir }, found);
23
+ }
24
+ return found;
25
+ }
26
+ //# sourceMappingURL=bins.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bins.js","sourceRoot":"","sources":["../../../../src/service/output/trace/bins.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,mBAAmB,EACnB,WAAW,EAEX,YAAY,EACZ,YAAY,GACb,MAAM,eAAe,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACzB,UAAkB,EAClB,IAAc;IAEd,MAAM,KAAK,GAAG,IAAI,GAAG,EAA4B,CAAC;IAElD,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,cAAc,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QAChE,MAAM,GAAG,GAAG,YAAY,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC;QAC5C,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;YACjB,SAAS;QACX,CAAC;QAED,0GAA0G;QAC1G,MAAM,WAAW,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;QAC3C,MAAM,SAAS,GACb,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;QAC1E,mBAAmB,CAAC,SAAS,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Every file that `files` import, directly or through other files. Absolute paths.
3
+ *
4
+ * Compute runs a Service with Bun, which reads a package's `bun` export condition.
5
+ * nft's `conditions` replaces the default set, so the imports are followed twice.
6
+ */
7
+ export declare function traceImports(files: string[], serviceDir: string): Promise<string[]>;
8
+ //# sourceMappingURL=imports.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imports.d.ts","sourceRoot":"","sources":["../../../../src/service/output/trace/imports.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,KAAK,EAAE,MAAM,EAAE,EACf,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,EAAE,CAAC,CAuBnB"}
@@ -0,0 +1,24 @@
1
+ // Follows imports from files with `@vercel/nft`.
2
+ import path from "node:path";
3
+ import { nodeFileTrace } from "@vercel/nft";
4
+ /**
5
+ * Every file that `files` import, directly or through other files. Absolute paths.
6
+ *
7
+ * Compute runs a Service with Bun, which reads a package's `bun` export condition.
8
+ * nft's `conditions` replaces the default set, so the imports are followed twice.
9
+ */
10
+ export async function traceImports(files, serviceDir) {
11
+ if (files.length === 0) {
12
+ return [];
13
+ }
14
+ const base = path.parse(serviceDir).root;
15
+ const traces = await Promise.all([undefined, ["bun"]].map((conditions) => nodeFileTrace(files, {
16
+ base,
17
+ processCwd: serviceDir,
18
+ ...(conditions ? { conditions } : {}),
19
+ })));
20
+ return [
21
+ ...new Set(traces.flatMap((trace) => [...trace.fileList].map((file) => path.join(base, file)))),
22
+ ];
23
+ }
24
+ //# sourceMappingURL=imports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imports.js","sourceRoot":"","sources":["../../../../src/service/output/trace/imports.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,KAAe,EACf,UAAkB;IAElB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC;IACzC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CAC9B,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CACtC,aAAa,CAAC,KAAK,EAAE;QACnB,IAAI;QACJ,UAAU,EAAE,UAAU;QACtB,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACtC,CAAC,CACH,CACF,CAAC;IAEF,OAAO;QACL,GAAG,IAAI,GAAG,CACR,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CACvB,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CACzD,CACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { PackageJson } from "../../settings/types.ts";
2
+ import { type InstalledPackage } from "./packages.ts";
3
+ /**
4
+ * A package loaded by a name held in a variable does not show as an import:
5
+ * sequelize loads `pg` that way. Such packages are declared as optional peer dependencies.
6
+ *
7
+ * When a traced package declares one and the Service lists it in `dependencies`,
8
+ * the Service installed it for that package, and it belongs in the build output.
9
+ */
10
+ export declare function optionalPeerDependencies(serviceDir: string, packageJson: PackageJson, tracedFiles: string[]): Array<InstalledPackage & {
11
+ name: string;
12
+ }>;
13
+ //# sourceMappingURL=optional-peer-dependencies.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optional-peer-dependencies.d.ts","sourceRoot":"","sources":["../../../../src/service/output/trace/optional-peer-dependencies.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAEL,KAAK,gBAAgB,EAGtB,MAAM,eAAe,CAAC;AAEvB;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,WAAW,EACxB,WAAW,EAAE,MAAM,EAAE,GACpB,KAAK,CAAC,gBAAgB,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CA6B5C"}
@@ -0,0 +1,34 @@
1
+ import { findPackage, packageDirOf, readManifest, } from "./packages.js";
2
+ /**
3
+ * A package loaded by a name held in a variable does not show as an import:
4
+ * sequelize loads `pg` that way. Such packages are declared as optional peer dependencies.
5
+ *
6
+ * When a traced package declares one and the Service lists it in `dependencies`,
7
+ * the Service installed it for that package, and it belongs in the build output.
8
+ */
9
+ export function optionalPeerDependencies(serviceDir, packageJson, tracedFiles) {
10
+ const listed = new Set(Object.keys(packageJson.dependencies ?? {}));
11
+ const tracedPackages = new Set();
12
+ for (const file of tracedFiles) {
13
+ const dir = packageDirOf(file);
14
+ if (dir !== null) {
15
+ tracedPackages.add(dir);
16
+ }
17
+ }
18
+ const tracedNames = new Set([...tracedPackages].map((dir) => readManifest(dir).name));
19
+ const found = new Map();
20
+ for (const dir of tracedPackages) {
21
+ const declared = readManifest(dir).peerDependenciesMeta ?? {};
22
+ for (const [name, { optional }] of Object.entries(declared)) {
23
+ if (!optional || !listed.has(name) || tracedNames.has(name)) {
24
+ continue;
25
+ }
26
+ const installed = findPackage(serviceDir, name);
27
+ if (installed !== null) {
28
+ found.set(name, installed);
29
+ }
30
+ }
31
+ }
32
+ return [...found].map(([name, installed]) => ({ name, ...installed }));
33
+ }
34
+ //# sourceMappingURL=optional-peer-dependencies.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optional-peer-dependencies.js","sourceRoot":"","sources":["../../../../src/service/output/trace/optional-peer-dependencies.ts"],"names":[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IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;gBACvB,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;YAC7B,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,SAAS,EAAE,CAAC,CAAC,CAAC;AACzE,CAAC"}
@@ -0,0 +1,22 @@
1
+ /** A package as it is found: `link` is the path it is reached by, a link under pnpm. `dir` holds its files. */
2
+ export interface InstalledPackage {
3
+ link: string;
4
+ dir: string;
5
+ }
6
+ /** The fields of a dependency's `package.json` that tracing reads. */
7
+ export interface PackageManifest {
8
+ name?: string;
9
+ dependencies?: Record<string, string>;
10
+ optionalDependencies?: Record<string, string>;
11
+ peerDependenciesMeta?: Record<string, {
12
+ optional?: boolean;
13
+ }>;
14
+ }
15
+ export declare function readManifest(packageDir: string): PackageManifest;
16
+ /** Finds a package the way Node.js does: in the `node_modules` of `from` and of each folder above it. */
17
+ export declare function findPackage(from: string, name: string): InstalledPackage | null;
18
+ /** The folder of the package that holds `file`: the nearest folder with a `package.json` that sits in a `node_modules`. */
19
+ export declare function packageDirOf(file: string): string | null;
20
+ /** Adds a package and, in turn, every package it depends on. */
21
+ export declare function addWithDependencies(installed: InstalledPackage, found: Map<string, InstalledPackage>): void;
22
+ //# sourceMappingURL=packages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packages.d.ts","sourceRoot":"","sources":["../../../../src/service/output/trace/packages.ts"],"names":[],"mappings":"AAIA,+GAA+G;AAC/G,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;CACb;AAED,sEAAsE;AACtE,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,oBAAoB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,oBAAoB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CAC/D;AAED,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,eAAe,CAQhE;AAED,yGAAyG;AACzG,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,gBAAgB,GAAG,IAAI,CASzB;AAED,2HAA2H;AAC3H,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAcxD;AAED,gEAAgE;AAChE,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,gBAAgB,EAC3B,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,GACnC,IAAI,CAgBN"}
@@ -0,0 +1,50 @@
1
+ // Finds packages in `node_modules`, for the parts of a trace that take whole packages.
2
+ import { existsSync, readFileSync, realpathSync } from "node:fs";
3
+ import path from "node:path";
4
+ export function readManifest(packageDir) {
5
+ try {
6
+ return JSON.parse(readFileSync(path.join(packageDir, "package.json"), "utf8"));
7
+ }
8
+ catch {
9
+ return {};
10
+ }
11
+ }
12
+ /** Finds a package the way Node.js does: in the `node_modules` of `from` and of each folder above it. */
13
+ export function findPackage(from, name) {
14
+ for (let dir = from; dir !== path.dirname(dir); dir = path.dirname(dir)) {
15
+ const link = path.join(dir, "node_modules", name);
16
+ if (existsSync(path.join(link, "package.json"))) {
17
+ return { link, dir: realpathSync(link) };
18
+ }
19
+ }
20
+ return null;
21
+ }
22
+ /** The folder of the package that holds `file`: the nearest folder with a `package.json` that sits in a `node_modules`. */
23
+ export function packageDirOf(file) {
24
+ for (let dir = path.dirname(file); dir !== path.dirname(dir); dir = path.dirname(dir)) {
25
+ const parent = path.basename(path.dirname(dir));
26
+ const inNodeModules = parent === "node_modules" || parent.startsWith("@");
27
+ if (inNodeModules && existsSync(path.join(dir, "package.json"))) {
28
+ return dir;
29
+ }
30
+ }
31
+ return null;
32
+ }
33
+ /** Adds a package and, in turn, every package it depends on. */
34
+ export function addWithDependencies(installed, found) {
35
+ if (found.has(installed.link)) {
36
+ return;
37
+ }
38
+ found.set(installed.link, installed);
39
+ const manifest = readManifest(installed.dir);
40
+ for (const name of Object.keys({
41
+ ...manifest.dependencies,
42
+ ...manifest.optionalDependencies,
43
+ })) {
44
+ const dependency = findPackage(installed.dir, name);
45
+ if (dependency !== null) {
46
+ addWithDependencies(dependency, found);
47
+ }
48
+ }
49
+ }
50
+ //# sourceMappingURL=packages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packages.js","sourceRoot":"","sources":["../../../../src/service/output/trace/packages.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,IAAI,MAAM,WAAW,CAAC;AAgB7B,MAAM,UAAU,YAAY,CAAC,UAAkB;IAC7C,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CACf,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,cAAc,CAAC,EAAE,MAAM,CAAC,CAC5D,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,yGAAyG;AACzG,MAAM,UAAU,WAAW,CACzB,IAAY,EACZ,IAAY;IAEZ,KAAK,IAAI,GAAG,GAAG,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACxE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,c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,QAAQ,CAAC,YAAY;QACxB,GAAG,QAAQ,CAAC,oBAAoB;KACjC,CAAC,EAAE,CAAC;QACH,MAAM,UAAU,GAAG,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QACpD,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;YACxB,mBAAmB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { PackageJson } from "../../settings/types.ts";
2
+ import { type InstalledPackage } from "./packages.ts";
3
+ /** What a server loads, found from its start command. */
4
+ export interface ServerTrace {
5
+ /** The files the server starts from. */
6
+ entrypoints: string[];
7
+ /** The bins the start command runs. */
8
+ bins: string[];
9
+ /** Every file the entrypoints and the built scripts import. */
10
+ files: string[];
11
+ /** Packages that go in whole: those of the bins, those the start command preloads, and the optional peer dependencies. */
12
+ packages: InstalledPackage[];
13
+ /** The names of the optional peer dependencies among `packages`. */
14
+ optionalPeerDependencies: string[];
15
+ }
16
+ /** Why a server could not be traced. */
17
+ export type Untraceable = "the start command runs no file and no bin that can be followed" | "the server imports files from outside the Service folder";
18
+ /**
19
+ * Traces the server that `command` starts:
20
+ * 1. the imports of its entrypoints, and of the scripts the build wrote;
21
+ * 2. the packages of the bins it runs, and the packages it preloads;
22
+ * 3. the optional peer dependencies of what was traced.
23
+ */
24
+ export declare function traceServer(options: {
25
+ serviceDir: string;
26
+ command: string;
27
+ packageJson: PackageJson;
28
+ /** Scripts in the output directories. A framework's server loads them by path, and they import packages. */
29
+ builtScripts: string[];
30
+ signal?: AbortSignal;
31
+ }): Promise<ServerTrace | Untraceable>;
32
+ //# sourceMappingURL=trace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../../../../src/service/output/trace/trace.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAK3D,OAAO,EAGL,KAAK,gBAAgB,EACtB,MAAM,eAAe,CAAC;AAEvB,yDAAyD;AACzD,MAAM,WAAW,WAAW;IAC1B,wCAAwC;IACxC,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,uCAAuC;IACvC,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,+DAA+D;IAC/D,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,0HAA0H;IAC1H,QAAQ,EAAE,gBAAgB,EAAE,CAAC;IAC7B,oEAAoE;IACpE,wBAAwB,EAAE,MAAM,EAAE,CAAC;CACpC;AAED,wCAAwC;AACxC,MAAM,MAAM,WAAW,GACnB,gEAAgE,GAChE,0DAA0D,CAAC;AAE/D;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,WAAW,CAAC;IACzB,4GAA4G;IAC5G,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB,GAAG,OAAO,CAAC,WAAW,GAAG,WAAW,CAAC,CAoDrC"}
@@ -0,0 +1,53 @@
1
+ // The trace of a server: every file it loads from `node_modules`.
2
+ import { realpathSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { readStartCommand } from "../entrypoint.js";
5
+ import { binPackages } from "./bins.js";
6
+ import { traceImports } from "./imports.js";
7
+ import { optionalPeerDependencies } from "./optional-peer-dependencies.js";
8
+ import { addWithDependencies, findPackage, } from "./packages.js";
9
+ /**
10
+ * Traces the server that `command` starts:
11
+ * 1. the imports of its entrypoints, and of the scripts the build wrote;
12
+ * 2. the packages of the bins it runs, and the packages it preloads;
13
+ * 3. the optional peer dependencies of what was traced.
14
+ */
15
+ export async function traceServer(options) {
16
+ const { packageJson } = options;
17
+ // Traced files are real paths, so the Service folder they are compared with must be its real path too.
18
+ const serviceDir = realpathSync(options.serviceDir);
19
+ const { entrypoints, bins, preloadedPackages } = readStartCommand(serviceDir, options.command, packageJson);
20
+ if (entrypoints.length === 0 && bins.length === 0) {
21
+ return "the start command runs no file and no bin that can be followed";
22
+ }
23
+ options.signal?.throwIfAborted();
24
+ const files = await traceImports([...entrypoints, ...options.builtScripts], serviceDir);
25
+ const packages = binPackages(serviceDir, bins);
26
+ // A preloaded package goes in whole, as a bin's package does: `tsx` loads its own files by path.
27
+ for (const name of preloadedPackages) {
28
+ const preloaded = findPackage(serviceDir, name);
29
+ if (preloaded !== null) {
30
+ addWithDependencies(preloaded, packages);
31
+ }
32
+ }
33
+ const peers = optionalPeerDependencies(serviceDir, packageJson, files);
34
+ for (const peer of peers) {
35
+ addWithDependencies(peer, packages);
36
+ }
37
+ const inside = (file) => {
38
+ const relative = path.relative(serviceDir, file);
39
+ return !relative.startsWith("..") && !path.isAbsolute(relative);
40
+ };
41
+ if (!files.every(inside) ||
42
+ ![...packages.values()].every((found) => inside(found.dir))) {
43
+ return "the server imports files from outside the Service folder";
44
+ }
45
+ return {
46
+ entrypoints,
47
+ bins,
48
+ files,
49
+ packages: [...packages.values()],
50
+ optionalPeerDependencies: peers.map((peer) => peer.name),
51
+ };
52
+ }
53
+ //# sourceMappingURL=trace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace.js","sourceRoot":"","sources":["../../../../src/service/output/trace/trace.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EACL,mBAAmB,EACnB,WAAW,GAEZ,MAAM,eAAe,CAAC;AAqBvB;;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AAI,CAAC,CAAC;QAEjD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;IAClE,CAAC,CAAC;IACF,IACE,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC;QACpB,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAC3D,CAAC;QACD,OAAO,0DAA0D,CAAC;IACpE,CAAC;IAED,OAAO;QACL,WAAW;QACX,IAAI;QACJ,KAAK;QACL,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;QAChC,wBAAwB,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC;KACzD,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/compute-sdk",
3
- "version": "0.48.0",
3
+ "version": "0.49.0",
4
4
  "description": "TypeScript SDK for deploying and managing applications on Prisma Compute",
5
5
  "license": "Apache-2.0",
6
6
  "repository": "prisma/project-compute",
@@ -4,10 +4,8 @@ import { existsSync, readFileSync } from "node:fs";
4
4
  import { join } from "node:path";
5
5
  import { Result } from "better-result";
6
6
  import { ServiceBuildError } from "../../errors.ts";
7
- import {
8
- type BuildOutput,
9
- writeBuildOutput,
10
- } from "../output/write-build-output.ts";
7
+ import { type BuildOutput, writeBuildOutput } from "../output/build-output.ts";
8
+ import { readPackageJson } from "../settings/package-json.ts";
11
9
  import {
12
10
  resolveServiceBuildSettings,
13
11
  resolveStartAfterBuild,
@@ -102,15 +100,24 @@ export async function buildService(
102
100
  return fail(NO_START);
103
101
  }
104
102
 
105
- const port = settings.port?.value ?? null;
106
- const output = writeBuildOutput({
103
+ const output = await writeBuildOutput({
107
104
  serviceDir: dir,
108
105
  start: start.value,
109
- port,
106
+ port: settings.port?.value ?? null,
107
+ packageJson: (await readPackageJson(read)).packageJson,
108
+ outputDirectories: outputDirectories.value,
109
+ signal: options.signal,
110
110
  });
111
+ const notes =
112
+ output.kind === "whole folder"
113
+ ? [
114
+ ...settings.notes,
115
+ `The build output is the whole folder, with all of node_modules: ${output.reason}.`,
116
+ ]
117
+ : settings.notes;
111
118
 
112
119
  return Result.ok({
113
- settings: { ...settings, start },
120
+ settings: { ...settings, start, notes },
114
121
  start,
115
122
  steps,
116
123
  output,
@@ -7,8 +7,8 @@ export {
7
7
  buildService,
8
8
  } from "./build/build-service.ts";
9
9
  export type { BuildStep } from "./build/run-step.ts";
10
+ export type { BuildOutput, TraceSummary } from "./output/build-output.ts";
10
11
  export { excludedFromBuildOutput } from "./output/exclude.ts";
11
- export type { BuildOutput } from "./output/write-build-output.ts";
12
12
  export {
13
13
  isDevCommand,
14
14
  type OutputDirectories,
@@ -0,0 +1,174 @@
1
+ // The build output of a Service: the folder a build hands to the caller, and the file Compute starts in it.
2
+ import { copyFileSync, mkdirSync, realpathSync, writeFileSync } from "node:fs";
3
+ import { rm } from "node:fs/promises";
4
+ import path from "node:path";
5
+ import {
6
+ LAUNCHER_FILE,
7
+ START_PLAN_FILE,
8
+ type StartPlan,
9
+ } from "../launcher-contract/start-plan.ts";
10
+ import type {
11
+ OutputDirectories,
12
+ PackageJson,
13
+ Start,
14
+ } from "../settings/types.ts";
15
+ import { Copy } from "./copy.ts";
16
+ import { excludedFromBuildOutput } from "./exclude.ts";
17
+ import { launcherFile } from "./launcher-file.ts";
18
+ import { inOutputDirectory, sourceFiles } from "./source-files.ts";
19
+ import {
20
+ type ServerTrace,
21
+ traceServer,
22
+ type Untraceable,
23
+ } from "./trace/trace.ts";
24
+
25
+ /** Where the build output of a traced server is written, relative to the Service folder. */
26
+ export const BUILD_OUTPUT_FOLDER = path.join(".prisma", "output");
27
+
28
+ /** What a build hands to the caller: a folder and the file Compute starts in it. */
29
+ export type BuildOutput = {
30
+ /** The folder to copy or upload, without what `excludedFromBuildOutput` names. A real path: no part of it is a symbolic link. */
31
+ dir: string;
32
+ /** The file Compute starts, relative to `dir`. */
33
+ entrypoint: string;
34
+ } & (
35
+ | {
36
+ /** A folder of static files that the build wrote. Nothing else is needed to serve it. */
37
+ kind: "static site";
38
+ }
39
+ | {
40
+ /** The source files and the trace of the server: the files of `node_modules` that it loads. */
41
+ kind: "traced server";
42
+ trace: TraceSummary;
43
+ }
44
+ | {
45
+ /** The whole Service folder, with all of `node_modules`, because the server could not be traced. */
46
+ kind: "whole folder";
47
+ reason: Untraceable;
48
+ }
49
+ );
50
+
51
+ /** What the build output of a traced server holds, for the caller to show. Paths are relative to the Service folder. */
52
+ export interface TraceSummary {
53
+ files: number;
54
+ bytes: number;
55
+ entrypoints: string[];
56
+ bins: string[];
57
+ optionalPeerDependencies: string[];
58
+ }
59
+
60
+ const SCRIPT_FILE = /\.(c|m)?js$/;
61
+
62
+ /**
63
+ * Writes the build output for how the Service starts, with the Launcher and the Start plan in it.
64
+ *
65
+ * - Static files: the folder the build wrote.
66
+ * - A start command: the source files and the trace of the server it starts, in `.prisma/output`.
67
+ * - A start command whose server cannot be traced: the whole Service folder.
68
+ */
69
+ export async function writeBuildOutput(options: {
70
+ serviceDir: string;
71
+ start: Start;
72
+ /** The port the user stated, or null. */
73
+ port: number | null;
74
+ packageJson: PackageJson;
75
+ outputDirectories: OutputDirectories;
76
+ signal?: AbortSignal;
77
+ }): Promise<BuildOutput> {
78
+ const { start } = options;
79
+ // The trace holds real paths, so every path here starts from the real path of the Service folder.
80
+ const serviceDir = realpathSync(options.serviceDir);
81
+ if (start.kind === "static") {
82
+ const dir = path.join(serviceDir, start.dir);
83
+ writeLauncher(dir, { kind: "static", dir: ".", port: options.port });
84
+
85
+ return { dir, entrypoint: LAUNCHER_FILE, kind: "static site" };
86
+ }
87
+
88
+ const plan: StartPlan = {
89
+ kind: "command",
90
+ command: start.command,
91
+ port: options.port,
92
+ };
93
+ const source = await sourceFiles(
94
+ serviceDir,
95
+ options.outputDirectories,
96
+ options.signal,
97
+ );
98
+ const trace = await traceServer({
99
+ serviceDir,
100
+ command: start.command,
101
+ packageJson: options.packageJson,
102
+ builtScripts: source.filter(
103
+ (file) =>
104
+ SCRIPT_FILE.test(file) &&
105
+ inOutputDirectory(serviceDir, options.outputDirectories, file),
106
+ ),
107
+ signal: options.signal,
108
+ });
109
+ const dir = path.join(serviceDir, BUILD_OUTPUT_FOLDER);
110
+ // An earlier build may have written this folder. It must not stay in a whole-folder build output.
111
+ await rm(dir, { recursive: true, force: true });
112
+ if (typeof trace === "string") {
113
+ writeLauncher(serviceDir, plan);
114
+
115
+ return {
116
+ dir: serviceDir,
117
+ entrypoint: LAUNCHER_FILE,
118
+ kind: "whole folder",
119
+ reason: trace,
120
+ };
121
+ }
122
+
123
+ const copied = await copyTracedServer(serviceDir, dir, source, trace);
124
+ writeLauncher(dir, plan);
125
+
126
+ const relative = (file: string) => path.relative(serviceDir, file);
127
+
128
+ return {
129
+ dir,
130
+ entrypoint: LAUNCHER_FILE,
131
+ kind: "traced server",
132
+ trace: {
133
+ files: copied.files,
134
+ bytes: copied.bytes,
135
+ entrypoints: trace.entrypoints.map(relative),
136
+ bins: trace.bins,
137
+ optionalPeerDependencies: trace.optionalPeerDependencies,
138
+ },
139
+ };
140
+ }
141
+
142
+ /** Copies the source files and the trace into the build output folder, each to the place it has in the Service folder. */
143
+ async function copyTracedServer(
144
+ serviceDir: string,
145
+ dir: string,
146
+ source: string[],
147
+ trace: ServerTrace,
148
+ ): Promise<Copy> {
149
+ const copy = new Copy(serviceDir, dir);
150
+
151
+ for (const file of source) {
152
+ await copy.file(file);
153
+ }
154
+ for (const file of trace.files) {
155
+ if (!excludedFromBuildOutput(path.basename(file))) {
156
+ await copy.file(file);
157
+ }
158
+ }
159
+ for (const installed of trace.packages) {
160
+ await copy.file(installed.link);
161
+ await copy.packageFolder(installed.dir);
162
+ }
163
+ for (const bin of trace.bins) {
164
+ await copy.file(path.join(serviceDir, "node_modules", ".bin", bin));
165
+ }
166
+
167
+ return copy;
168
+ }
169
+
170
+ function writeLauncher(dir: string, plan: StartPlan): void {
171
+ mkdirSync(path.join(dir, path.dirname(LAUNCHER_FILE)), { recursive: true });
172
+ copyFileSync(launcherFile(), path.join(dir, LAUNCHER_FILE));
173
+ writeFileSync(path.join(dir, START_PLAN_FILE), JSON.stringify(plan));
174
+ }