@prisma/compute-sdk 0.48.0 → 0.50.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 (81) hide show
  1. package/README.md +47 -2
  2. package/dist/nextjs-build.d.ts +14 -0
  3. package/dist/nextjs-build.d.ts.map +1 -1
  4. package/dist/nextjs-build.js +2 -2
  5. package/dist/nextjs-build.js.map +1 -1
  6. package/dist/service/build/build-service.d.ts +2 -1
  7. package/dist/service/build/build-service.d.ts.map +1 -1
  8. package/dist/service/build/build-service.js +58 -15
  9. package/dist/service/build/build-service.js.map +1 -1
  10. package/dist/service/frameworks/framework.d.ts +26 -0
  11. package/dist/service/frameworks/framework.d.ts.map +1 -0
  12. package/dist/service/frameworks/framework.js +2 -0
  13. package/dist/service/frameworks/framework.js.map +1 -0
  14. package/dist/service/frameworks/index.d.ts +7 -0
  15. package/dist/service/frameworks/index.d.ts.map +1 -0
  16. package/dist/service/frameworks/index.js +8 -0
  17. package/dist/service/frameworks/index.js.map +1 -0
  18. package/dist/service/frameworks/nextjs.d.ts +3 -0
  19. package/dist/service/frameworks/nextjs.d.ts.map +1 -0
  20. package/dist/service/frameworks/nextjs.js +77 -0
  21. package/dist/service/frameworks/nextjs.js.map +1 -0
  22. package/dist/service/index.d.ts +1 -1
  23. package/dist/service/index.d.ts.map +1 -1
  24. package/dist/service/index.js.map +1 -1
  25. package/dist/service/output/build-output.d.ts +55 -0
  26. package/dist/service/output/build-output.d.ts.map +1 -0
  27. package/dist/service/output/build-output.js +98 -0
  28. package/dist/service/output/build-output.js.map +1 -0
  29. package/dist/service/output/copy.d.ts +15 -0
  30. package/dist/service/output/copy.d.ts.map +1 -0
  31. package/dist/service/output/copy.js +59 -0
  32. package/dist/service/output/copy.js.map +1 -0
  33. package/dist/service/output/entrypoint.d.ts +20 -0
  34. package/dist/service/output/entrypoint.d.ts.map +1 -0
  35. package/dist/service/output/entrypoint.js +183 -0
  36. package/dist/service/output/entrypoint.js.map +1 -0
  37. package/dist/service/output/source-files.d.ts +12 -0
  38. package/dist/service/output/source-files.d.ts.map +1 -0
  39. package/dist/service/output/source-files.js +46 -0
  40. package/dist/service/output/source-files.js.map +1 -0
  41. package/dist/service/output/trace/bins.d.ts +9 -0
  42. package/dist/service/output/trace/bins.d.ts.map +1 -0
  43. package/dist/service/output/trace/bins.js +26 -0
  44. package/dist/service/output/trace/bins.js.map +1 -0
  45. package/dist/service/output/trace/imports.d.ts +8 -0
  46. package/dist/service/output/trace/imports.d.ts.map +1 -0
  47. package/dist/service/output/trace/imports.js +24 -0
  48. package/dist/service/output/trace/imports.js.map +1 -0
  49. package/dist/service/output/trace/optional-peer-dependencies.d.ts +13 -0
  50. package/dist/service/output/trace/optional-peer-dependencies.d.ts.map +1 -0
  51. package/dist/service/output/trace/optional-peer-dependencies.js +34 -0
  52. package/dist/service/output/trace/optional-peer-dependencies.js.map +1 -0
  53. package/dist/service/output/trace/packages.d.ts +22 -0
  54. package/dist/service/output/trace/packages.d.ts.map +1 -0
  55. package/dist/service/output/trace/packages.js +50 -0
  56. package/dist/service/output/trace/packages.js.map +1 -0
  57. package/dist/service/output/trace/trace.d.ts +32 -0
  58. package/dist/service/output/trace/trace.d.ts.map +1 -0
  59. package/dist/service/output/trace/trace.js +53 -0
  60. package/dist/service/output/trace/trace.js.map +1 -0
  61. package/package.json +1 -1
  62. package/src/nextjs-build.ts +4 -2
  63. package/src/service/build/build-service.ts +74 -17
  64. package/src/service/frameworks/framework.ts +28 -0
  65. package/src/service/frameworks/index.ts +12 -0
  66. package/src/service/frameworks/nextjs.ts +98 -0
  67. package/src/service/index.ts +1 -1
  68. package/src/service/output/build-output.ts +181 -0
  69. package/src/service/output/copy.ts +69 -0
  70. package/src/service/output/entrypoint.ts +250 -0
  71. package/src/service/output/source-files.ts +64 -0
  72. package/src/service/output/trace/bins.ts +39 -0
  73. package/src/service/output/trace/imports.ts +37 -0
  74. package/src/service/output/trace/optional-peer-dependencies.ts +50 -0
  75. package/src/service/output/trace/packages.ts +81 -0
  76. package/src/service/output/trace/trace.ts +99 -0
  77. package/dist/service/output/write-build-output.d.ts +0 -16
  78. package/dist/service/output/write-build-output.d.ts.map +0 -1
  79. package/dist/service/output/write-build-output.js +0 -24
  80. package/dist/service/output/write-build-output.js.map +0 -1
  81. package/src/service/output/write-build-output.ts +0 -50
@@ -0,0 +1,81 @@
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
+
5
+ /** A package as it is found: `link` is the path it is reached by, a link under pnpm. `dir` holds its files. */
6
+ export interface InstalledPackage {
7
+ link: string;
8
+ dir: string;
9
+ }
10
+
11
+ /** The fields of a dependency's `package.json` that tracing reads. */
12
+ export interface PackageManifest {
13
+ name?: string;
14
+ dependencies?: Record<string, string>;
15
+ optionalDependencies?: Record<string, string>;
16
+ peerDependenciesMeta?: Record<string, { optional?: boolean }>;
17
+ }
18
+
19
+ export function readManifest(packageDir: string): PackageManifest {
20
+ try {
21
+ return JSON.parse(
22
+ readFileSync(path.join(packageDir, "package.json"), "utf8"),
23
+ );
24
+ } catch {
25
+ return {};
26
+ }
27
+ }
28
+
29
+ /** Finds a package the way Node.js does: in the `node_modules` of `from` and of each folder above it. */
30
+ export function findPackage(
31
+ from: string,
32
+ name: string,
33
+ ): InstalledPackage | null {
34
+ for (let dir = from; dir !== path.dirname(dir); dir = path.dirname(dir)) {
35
+ const link = path.join(dir, "node_modules", name);
36
+ if (existsSync(path.join(link, "package.json"))) {
37
+ return { link, dir: realpathSync(link) };
38
+ }
39
+ }
40
+
41
+ return null;
42
+ }
43
+
44
+ /** The folder of the package that holds `file`: the nearest folder with a `package.json` that sits in a `node_modules`. */
45
+ export function packageDirOf(file: string): string | null {
46
+ for (
47
+ let dir = path.dirname(file);
48
+ dir !== path.dirname(dir);
49
+ dir = path.dirname(dir)
50
+ ) {
51
+ const parent = path.basename(path.dirname(dir));
52
+ const inNodeModules = parent === "node_modules" || parent.startsWith("@");
53
+ if (inNodeModules && existsSync(path.join(dir, "package.json"))) {
54
+ return dir;
55
+ }
56
+ }
57
+
58
+ return null;
59
+ }
60
+
61
+ /** Adds a package and, in turn, every package it depends on. */
62
+ export function addWithDependencies(
63
+ installed: InstalledPackage,
64
+ found: Map<string, InstalledPackage>,
65
+ ): void {
66
+ if (found.has(installed.link)) {
67
+ return;
68
+ }
69
+
70
+ found.set(installed.link, installed);
71
+ const manifest = readManifest(installed.dir);
72
+ for (const name of Object.keys({
73
+ ...manifest.dependencies,
74
+ ...manifest.optionalDependencies,
75
+ })) {
76
+ const dependency = findPackage(installed.dir, name);
77
+ if (dependency !== null) {
78
+ addWithDependencies(dependency, found);
79
+ }
80
+ }
81
+ }
@@ -0,0 +1,99 @@
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 type { PackageJson } from "../../settings/types.ts";
5
+ import { readStartCommand } from "../entrypoint.ts";
6
+ import { binPackages } from "./bins.ts";
7
+ import { traceImports } from "./imports.ts";
8
+ import { optionalPeerDependencies } from "./optional-peer-dependencies.ts";
9
+ import {
10
+ addWithDependencies,
11
+ findPackage,
12
+ type InstalledPackage,
13
+ } from "./packages.ts";
14
+
15
+ /** What a server loads, found from its start command. */
16
+ export interface ServerTrace {
17
+ /** The files the server starts from. */
18
+ entrypoints: string[];
19
+ /** The bins the start command runs. */
20
+ bins: string[];
21
+ /** Every file the entrypoints and the built scripts import. */
22
+ files: string[];
23
+ /** Packages that go in whole: those of the bins, those the start command preloads, and the optional peer dependencies. */
24
+ packages: InstalledPackage[];
25
+ /** The names of the optional peer dependencies among `packages`. */
26
+ optionalPeerDependencies: string[];
27
+ }
28
+
29
+ /** Why a server could not be traced. */
30
+ export type Untraceable =
31
+ | "the start command runs no file and no bin that can be followed"
32
+ | "the server imports files from outside the Service folder";
33
+
34
+ /**
35
+ * Traces the server that `command` starts:
36
+ * 1. the imports of its entrypoints, and of the scripts the build wrote;
37
+ * 2. the packages of the bins it runs, and the packages it preloads;
38
+ * 3. the optional peer dependencies of what was traced.
39
+ */
40
+ export async function traceServer(options: {
41
+ serviceDir: string;
42
+ command: string;
43
+ packageJson: PackageJson;
44
+ /** Scripts in the output directories. A framework's server loads them by path, and they import packages. */
45
+ builtScripts: string[];
46
+ signal?: AbortSignal;
47
+ }): Promise<ServerTrace | Untraceable> {
48
+ const { packageJson } = options;
49
+ // Traced files are real paths, so the Service folder they are compared with must be its real path too.
50
+ const serviceDir = realpathSync(options.serviceDir);
51
+ const { entrypoints, bins, preloadedPackages } = readStartCommand(
52
+ serviceDir,
53
+ options.command,
54
+ packageJson,
55
+ );
56
+ if (entrypoints.length === 0 && bins.length === 0) {
57
+ return "the start command runs no file and no bin that can be followed";
58
+ }
59
+
60
+ options.signal?.throwIfAborted();
61
+ const files = await traceImports(
62
+ [...entrypoints, ...options.builtScripts],
63
+ serviceDir,
64
+ );
65
+
66
+ const packages = binPackages(serviceDir, bins);
67
+ // A preloaded package goes in whole, as a bin's package does: `tsx` loads its own files by path.
68
+ for (const name of preloadedPackages) {
69
+ const preloaded = findPackage(serviceDir, name);
70
+ if (preloaded !== null) {
71
+ addWithDependencies(preloaded, packages);
72
+ }
73
+ }
74
+
75
+ const peers = optionalPeerDependencies(serviceDir, packageJson, files);
76
+ for (const peer of peers) {
77
+ addWithDependencies(peer, packages);
78
+ }
79
+
80
+ const inside = (file: string) => {
81
+ const relative = path.relative(serviceDir, file);
82
+
83
+ return !relative.startsWith("..") && !path.isAbsolute(relative);
84
+ };
85
+ if (
86
+ !files.every(inside) ||
87
+ ![...packages.values()].every((found) => inside(found.dir))
88
+ ) {
89
+ return "the server imports files from outside the Service folder";
90
+ }
91
+
92
+ return {
93
+ entrypoints,
94
+ bins,
95
+ files,
96
+ packages: [...packages.values()],
97
+ optionalPeerDependencies: peers.map((peer) => peer.name),
98
+ };
99
+ }
@@ -1,16 +0,0 @@
1
- import type { Start } from "../settings/types.ts";
2
- /** What a build hands to the caller: a folder and the file Compute starts in it. */
3
- export interface BuildOutput {
4
- /** The folder to copy or upload, without what `excludedFromBuildOutput` names. */
5
- dir: string;
6
- /** The file Compute starts, relative to `dir`. */
7
- entrypoint: string;
8
- }
9
- /** Picks the build output folder, and writes the Launcher and the Start plan into it. */
10
- export declare function writeBuildOutput(options: {
11
- serviceDir: string;
12
- start: Start;
13
- /** The port the user stated, or null. */
14
- port: number | null;
15
- }): BuildOutput;
16
- //# sourceMappingURL=write-build-output.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"write-build-output.d.ts","sourceRoot":"","sources":["../../../src/service/output/write-build-output.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAGlD,oFAAoF;AACpF,MAAM,WAAW,WAAW;IAC1B,kFAAkF;IAClF,GAAG,EAAE,MAAM,CAAC;IACZ,kDAAkD;IAClD,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,yFAAyF;AACzF,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IACxC,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC;IACb,yCAAyC;IACzC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CACrB,GAAG,WAAW,CAWd"}
@@ -1,24 +0,0 @@
1
- import { copyFileSync, mkdirSync, writeFileSync } from "node:fs";
2
- import { dirname, join } from "node:path";
3
- import { LAUNCHER_FILE, START_PLAN_FILE, } from "../launcher-contract/start-plan.js";
4
- import { launcherFile } from "./launcher-file.js";
5
- /** Picks the build output folder, and writes the Launcher and the Start plan into it. */
6
- export function writeBuildOutput(options) {
7
- const dir = outputDir(options.serviceDir, options.start);
8
- mkdirSync(join(dir, dirname(LAUNCHER_FILE)), { recursive: true });
9
- copyFileSync(launcherFile(), join(dir, LAUNCHER_FILE));
10
- writeFileSync(join(dir, START_PLAN_FILE), JSON.stringify(startPlan(options.start, options.port)));
11
- return { dir, entrypoint: LAUNCHER_FILE };
12
- }
13
- /** A static site needs no source and no `node_modules`, so its build output is the built folder alone. */
14
- function outputDir(serviceDir, start) {
15
- return start.kind === "static" ? join(serviceDir, start.dir) : serviceDir;
16
- }
17
- function startPlan(start, port) {
18
- if (start.kind === "static") {
19
- // The build output of a static site is the site's own folder, so the plan serves the build output folder itself.
20
- return { kind: "static", dir: ".", port };
21
- }
22
- return { kind: "command", command: start.command, port };
23
- }
24
- //# sourceMappingURL=write-build-output.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"write-build-output.js","sourceRoot":"","sources":["../../../src/service/output/write-build-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EACL,aAAa,EACb,eAAe,GAEhB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAUlD,yFAAyF;AACzF,MAAM,UAAU,gBAAgB,CAAC,OAKhC;IACC,MAAM,GAAG,GAAG,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IAEzD,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAClE,YAAY,CAAC,YAAY,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC;IACvD,aAAa,CACX,IAAI,CAAC,GAAG,EAAE,eAAe,CAAC,EAC1B,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CACvD,CAAC;IAEF,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC;AAC5C,CAAC;AAED,0GAA0G;AAC1G,SAAS,SAAS,CAAC,UAAkB,EAAE,KAAY;IACjD,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;AAC5E,CAAC;AAED,SAAS,SAAS,CAAC,KAAY,EAAE,IAAmB;IAClD,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC5B,iHAAiH;QACjH,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;IAC5C,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC;AAC3D,CAAC"}
@@ -1,50 +0,0 @@
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 BuildOutput {
13
- /** The folder to copy or upload, without what `excludedFromBuildOutput` names. */
14
- dir: string;
15
- /** The file Compute starts, relative to `dir`. */
16
- entrypoint: string;
17
- }
18
-
19
- /** Picks the build output folder, and writes the Launcher and the Start plan into it. */
20
- export function writeBuildOutput(options: {
21
- serviceDir: string;
22
- start: Start;
23
- /** The port the user stated, or null. */
24
- port: number | null;
25
- }): BuildOutput {
26
- const dir = outputDir(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 build output is the built folder alone. */
39
- function outputDir(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 build output of a static site is the site's own folder, so the plan serves the build output folder itself.
46
- return { kind: "static", dir: ".", port };
47
- }
48
-
49
- return { kind: "command", command: start.command, port };
50
- }