@prisma/dev 0.2.0 → 0.4.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/dist/chunk-EVE556Q6.js +2 -0
- package/dist/daemon/client.d.ts +24 -0
- package/dist/daemon/client.js +2 -0
- package/dist/daemon/daemon.d.ts +2 -0
- package/dist/daemon/daemon.js +2 -0
- package/dist/index-CW1pktRs.d.ts +83 -0
- package/dist/index.d.ts +1 -83
- package/dist/index.js +7 -7
- package/package.json +9 -2
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as url from 'url';
|
|
2
|
+
import { SockDaemonClient } from 'sock-daemon/client';
|
|
3
|
+
import { S as ServerOptions, a as Server } from '../index-CW1pktRs.js';
|
|
4
|
+
import { MessageBase } from 'sock-daemon/server';
|
|
5
|
+
|
|
6
|
+
type Kind = "START_SERVER" | "STOP_SERVER";
|
|
7
|
+
type RequestMessage = MessageBase & {
|
|
8
|
+
kind: Kind;
|
|
9
|
+
args?: unknown;
|
|
10
|
+
};
|
|
11
|
+
type ResponseMessage = MessageBase & {
|
|
12
|
+
kind: Kind;
|
|
13
|
+
result?: unknown;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
declare class MyServiceClient extends SockDaemonClient<RequestMessage, ResponseMessage> {
|
|
17
|
+
static get serviceName(): string;
|
|
18
|
+
static get daemonScript(): url.URL;
|
|
19
|
+
isResponse(msg: unknown): msg is ResponseMessage;
|
|
20
|
+
stopServer(): Promise<void>;
|
|
21
|
+
startServer(options: ServerOptions): Promise<Server>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { MyServiceClient };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);
|
|
2
|
+
import{a as s}from"../chunk-EVE556Q6.js";import{SockDaemonClient as a}from"sock-daemon/client";var r=class extends a{static get serviceName(){return s}static get daemonScript(){return new URL("./daemon.js",import.meta.url)}isResponse(e){return super.isMessage(e)}async stopServer(){await super.request({kind:"STOP_SERVER"})}async startServer(e){let{result:t}=await super.request({kind:"START_SERVER",args:e});return t}};export{r as MyServiceClient};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);
|
|
2
|
+
import{a as r}from"../chunk-EVE556Q6.js";import{SockDaemonServer as o}from"sock-daemon/server";var s=class extends o{isRequest(e){return super.isMessage(e)}static get serviceName(){return r}handle(e){return console.error("got request",e),Promise.resolve({id:e.id,kind:e.kind,result:null})}};var t=new s({});t.listen();
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
interface ServerOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The port the database server will listen on.
|
|
4
|
+
*
|
|
5
|
+
* Defaults to `51214`.
|
|
6
|
+
*
|
|
7
|
+
* An error is thrown if the port is already in use.
|
|
8
|
+
*/
|
|
9
|
+
databasePort?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to enable debug logging.
|
|
12
|
+
*
|
|
13
|
+
* Defaults to `false`.
|
|
14
|
+
*/
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Whether to run the server in dry run mode.
|
|
18
|
+
*
|
|
19
|
+
* Defaults to `false`.
|
|
20
|
+
*/
|
|
21
|
+
dryRun?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The name of the server.
|
|
24
|
+
*
|
|
25
|
+
* Defaults to `default`.
|
|
26
|
+
*/
|
|
27
|
+
name?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The persistence mode of the server.
|
|
30
|
+
*
|
|
31
|
+
* Default is `stateless`.
|
|
32
|
+
*/
|
|
33
|
+
persistenceMode?: PersistenceMode;
|
|
34
|
+
/**
|
|
35
|
+
* The port the server will listen on.
|
|
36
|
+
*
|
|
37
|
+
* Defaults to `51213`.
|
|
38
|
+
*
|
|
39
|
+
* An error is thrown if the port is already in use.
|
|
40
|
+
*/
|
|
41
|
+
port?: number;
|
|
42
|
+
/**
|
|
43
|
+
* The port the shadow database server will listen on.
|
|
44
|
+
*
|
|
45
|
+
* Defaults to `51215`.
|
|
46
|
+
*
|
|
47
|
+
* An error is thrown if the port is already in use.
|
|
48
|
+
*/
|
|
49
|
+
shadowDatabasePort?: number;
|
|
50
|
+
}
|
|
51
|
+
type PersistenceMode = "stateless" | "stateful";
|
|
52
|
+
|
|
53
|
+
type DBServerPurpose = "database" | "shadow_database";
|
|
54
|
+
|
|
55
|
+
declare const DEFAULT_DATABASE_PORT = 51214;
|
|
56
|
+
declare const DEFAULT_SERVER_PORT = 51213;
|
|
57
|
+
declare const DEFAULT_SHADOW_DATABASE_PORT = 51215;
|
|
58
|
+
type PortAssignableService = DBServerPurpose | "server";
|
|
59
|
+
declare class PortNotAvailableError extends Error {
|
|
60
|
+
port: number;
|
|
61
|
+
service: PortAssignableService;
|
|
62
|
+
name: string;
|
|
63
|
+
constructor(port: number, service: PortAssignableService);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface Server {
|
|
67
|
+
accelerate: {
|
|
68
|
+
url: string;
|
|
69
|
+
};
|
|
70
|
+
close(): Promise<void>;
|
|
71
|
+
database: {
|
|
72
|
+
connectionString: string;
|
|
73
|
+
};
|
|
74
|
+
ppg: {
|
|
75
|
+
url: string;
|
|
76
|
+
};
|
|
77
|
+
shadowDatabase: {
|
|
78
|
+
connectionString: string;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
declare function unstable_startServer(options?: ServerOptions): Promise<Server>;
|
|
82
|
+
|
|
83
|
+
export { DEFAULT_DATABASE_PORT as D, type PortAssignableService as P, type ServerOptions as S, type Server as a, DEFAULT_SERVER_PORT as b, DEFAULT_SHADOW_DATABASE_PORT as c, PortNotAvailableError as d, unstable_startServer as u };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,83 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* The port the database server will listen on.
|
|
4
|
-
*
|
|
5
|
-
* Defaults to `51214`.
|
|
6
|
-
*
|
|
7
|
-
* An error is thrown if the port is already in use.
|
|
8
|
-
*/
|
|
9
|
-
databasePort?: number;
|
|
10
|
-
/**
|
|
11
|
-
* Whether to enable debug logging.
|
|
12
|
-
*
|
|
13
|
-
* Defaults to `false`.
|
|
14
|
-
*/
|
|
15
|
-
debug?: boolean;
|
|
16
|
-
/**
|
|
17
|
-
* Whether to run the server in dry run mode.
|
|
18
|
-
*
|
|
19
|
-
* Defaults to `false`.
|
|
20
|
-
*/
|
|
21
|
-
dryRun?: boolean;
|
|
22
|
-
/**
|
|
23
|
-
* The name of the server.
|
|
24
|
-
*
|
|
25
|
-
* Defaults to `default`.
|
|
26
|
-
*/
|
|
27
|
-
name?: string;
|
|
28
|
-
/**
|
|
29
|
-
* The persistence mode of the server.
|
|
30
|
-
*
|
|
31
|
-
* Default is `stateless`.
|
|
32
|
-
*/
|
|
33
|
-
persistenceMode?: PersistenceMode;
|
|
34
|
-
/**
|
|
35
|
-
* The port the server will listen on.
|
|
36
|
-
*
|
|
37
|
-
* Defaults to `51213`.
|
|
38
|
-
*
|
|
39
|
-
* An error is thrown if the port is already in use.
|
|
40
|
-
*/
|
|
41
|
-
port?: number;
|
|
42
|
-
/**
|
|
43
|
-
* The port the shadow database server will listen on.
|
|
44
|
-
*
|
|
45
|
-
* Defaults to `51215`.
|
|
46
|
-
*
|
|
47
|
-
* An error is thrown if the port is already in use.
|
|
48
|
-
*/
|
|
49
|
-
shadowDatabasePort?: number;
|
|
50
|
-
}
|
|
51
|
-
type PersistenceMode = "stateless" | "stateful";
|
|
52
|
-
|
|
53
|
-
type DBServerPurpose = "database" | "shadow_database";
|
|
54
|
-
|
|
55
|
-
declare const DEFAULT_DATABASE_PORT = 51214;
|
|
56
|
-
declare const DEFAULT_SERVER_PORT = 51213;
|
|
57
|
-
declare const DEFAULT_SHADOW_DATABASE_PORT = 51215;
|
|
58
|
-
type PortAssignableService = DBServerPurpose | "server";
|
|
59
|
-
declare class PortNotAvailableError extends Error {
|
|
60
|
-
port: number;
|
|
61
|
-
service: PortAssignableService;
|
|
62
|
-
name: string;
|
|
63
|
-
constructor(port: number, service: PortAssignableService);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
interface Server {
|
|
67
|
-
accelerate: {
|
|
68
|
-
url: string;
|
|
69
|
-
};
|
|
70
|
-
close(): Promise<void>;
|
|
71
|
-
database: {
|
|
72
|
-
connectionString: string;
|
|
73
|
-
};
|
|
74
|
-
ppg: {
|
|
75
|
-
url: string;
|
|
76
|
-
};
|
|
77
|
-
shadowDatabase: {
|
|
78
|
-
connectionString: string;
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
declare function unstable_startServer(options?: ServerOptions): Promise<Server>;
|
|
82
|
-
|
|
83
|
-
export { DEFAULT_DATABASE_PORT, DEFAULT_SERVER_PORT, DEFAULT_SHADOW_DATABASE_PORT, type PortAssignableService, PortNotAvailableError, type Server, unstable_startServer };
|
|
1
|
+
export { D as DEFAULT_DATABASE_PORT, b as DEFAULT_SERVER_PORT, c as DEFAULT_SHADOW_DATABASE_PORT, P as PortAssignableService, d as PortNotAvailableError, a as Server, u as unstable_startServer } from './index-CW1pktRs.js';
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { createRequire } from 'module'; const require = createRequire(import.meta.url);
|
|
2
|
-
import{createServer as at}from"http";import{promisify as
|
|
3
|
-
`).find(
|
|
4
|
-
Received data: ${
|
|
1
|
+
import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);
|
|
2
|
+
import{createServer as at}from"http";import{promisify as ct}from"util";import{serve as lt}from"@hono/node-server";import dt from"@prisma/get-platform";function E(){let t,e,r=new Promise((s,i)=>{t=s,e=i}),n=s=>{n=o=null,e(s)},o=s=>{o=n=null,t(s)};return{promise:r,reject:s=>n?.(s),resolve:s=>o?.(s)}}import{logger as ut}from"hono/logger";import{Hono as he}from"hono/tiny";import{validator as S}from"hono/validator";import{process as mt}from"std-env";import{HTTPException as A}from"hono/http-exception";import{object as we,optional as Se,parseJson as Pe,pipe as $,regex as U,safeParse as ve,string as I,url as V}from"valibot";var q=/^(postgres|postgresql):\/\//,Ee=$(I(),Pe(),we({databaseUrl:$(I(),V(),U(q)),shadowDatabaseUrl:Se($(I(),V(),U(q)))}));function K(t){return Buffer.from(JSON.stringify(t),"utf8").toString("base64url")}function Ae(t){let e=Buffer.from(t,"base64url").toString("utf8"),{issues:r,output:n,success:o}=ve(Ee,e,{abortEarly:!0});return o?[null,n]:[r]}var g=(t,e)=>{let{authorization:r}=t;if(!r)throw new A(401,{message:"Missing API Key"});let[n,o="",s]=r.split(" ");if(n!=="Bearer"||s)throw new A(401,{message:"Invalid API Key"});let[i,a]=Ae(o);if(i)throw new A(401,{message:"Invalid API Key",cause:i.join(", ")});let{databaseUrl:c}=a;if(c!==e.var.db.connectionString)throw new A(401,{message:"Wrong API Key; Check your Prisma schema's `provider.url` value (probably defined in `.env`'s `DATABASE_URL` environment variable) is aligned with `prisma dev`'s output"});return{decodedAPIKey:a}};import{array as De,literal as Re,minLength as Te,object as Oe,pipe as xe,safeParse as $e,string as Ie,union as ke}from"valibot";var _e=Oe({tags:ke([xe(De(Ie()),Te(1)),Re("all")])});async function Q(t){let{output:e,success:r}=$e(_e,await t.req.json(),{abortEarly:!0});return r?e:t.text("Invalid input",400)}import{spawn as Ke}from"child_process";import{once as Qe}from"events";import{mkdir as Ge}from"fs/promises";import{join as Je}from"path";import{setTimeout as ze}from"timers/promises";import{proxySignals as We}from"foreground-child/proxy-signals";import{process as Ye}from"std-env";import{createWriteStream as Le,WriteStream as Be}from"fs";import{access as He,chmod as Ce,constants as Me,readFile as Fe,stat as je,unlink as Ne,writeFile as Ue}from"fs/promises";import Ve from"env-paths";import{inflate as qe}from"pako";var G=Ve("prisma-dev");function J(t,e){return`${G.cache}/engine/${t}/${e}`}function z(t){return`${G.data}/${t}`}async function D(t){try{return await He(t,Me.F_OK),!0}catch(e){if(_(e))return!1;throw e}}async function W(t,e){let r=qe(t);await Ue(e,r),await Ce(e,"755")}async function Y(t,e){await t.stream().pipeTo(Be.toWeb(Le(e,{encoding:"utf-8"})))}async function X(t){try{return await Ne(t),!0}catch{return!1}}async function k(t){try{return(await je(t)).mtimeMs}catch(e){if(_(e))return-1/0;throw e}}function _(t){return t!=null&&typeof t=="object"&&"code"in t&&t.code==="ENOENT"}async function Z(t){try{return await Fe(t,{encoding:"utf-8"})}catch(e){if(_(e))return null;throw e}}var{PRISMA_DEV_FORCE_ENGINE_BINARY_DOWNLOAD:Xe,PRISMA_DEV_FORCE_ENGINE_BINARY_PATH:Ze,PRISMA_DEV_FORCE_NETWORK_DELAY_MS:ee}=Ye.env,b=class t{static#r=new Map;#e;#t;constructor(e){this.#e=e,this.#t=null}static async get(e){let{debug:r}=e,n=`${e.schemaHash}:${e.clientVersion}`;try{let o=t.#r.get(n);if(o)return o;let s=new t(e);return t.#r.set(n,s),r&&console.debug("[Query Engine] starting...",e),await s.start(),r&&console.debug("[Query Engine] started!"),s}finally{t.stopAll(n)}}static async stopAll(e){let n=(await Promise.allSettled(Array.from(t.#r.entries()).filter(([o])=>o!==e).map(async([o,s])=>{try{await s.stop()}finally{t.#r.delete(o)}}))).filter(o=>o.status==="rejected").map(o=>o.reason);if(n.length>0)throw new AggregateError(n,"Failed to stop engines")}async commitTransaction(e,r){return await this.#s(e,r,"commit")}async request(e,r){let{url:n}=await this.start(),o=this.#n(r),s=await fetch(n,{body:typeof e=="string"?e:JSON.stringify(e),headers:{...o,"Content-Type":"application/json"},method:"POST"});if(!s.ok)throw await h.fromResponse(s);return await s.text()}async rollbackTransaction(e,r){return await this.#s(e,r,"rollback")}async startTransaction(e,r){let{url:n}=await this.start(),o=this.#n(r),s=await fetch(`${n}/transaction/start`,{body:JSON.stringify(e),headers:{...o,"Content-Type":"application/json"},method:"POST"});if(!s.ok)throw await h.fromResponse(s);return await s.json()}async start(){if(this.#t!=null)return await this.#t;let{promise:e,reject:r,resolve:n}=E();this.#t=e;let o=Ze||await this.#i();this.#e.debug&&console.debug("[Query Engine] spinning up at path...",o);let s=Ke(o,["--enable-raw-queries","--enable-telemetry-in-response","--port","0"],{env:{LOG_QUERIES:"y",PRISMA_DML:this.#e.base64Schema,QE_LOG_LEVEL:"TRACE",RUST_BACKTRACE:"1",RUST_LOG:"info"},stdio:["ignore","pipe","pipe"],windowsHide:!0});We(s),s.stderr.setEncoding("utf8"),s.stdout.setEncoding("utf8");let i=d=>{let m=d.split(`
|
|
3
|
+
`).find(be=>be.includes("Started query engine http server"));if(!m)return;s.stdout.removeListener("data",i);let{fields:p}=JSON.parse(m);if(p==null)return r(new Error(`Unexpected data during initialization, "fields" are missing: ${d}`));let{ip:u,port:N}=p;if(u==null||N==null)return r(new Error(`This version of query-engine is not compatible with minippg, "ip" and "port" are missing in the startup log entry.
|
|
4
|
+
Received data: ${d}`));n({childProcess:s,url:`http://${u}:${N}`})},a=d=>{this.#t=null,r(new w(String(d))),s.removeListener("exit",c),s.kill()};s.once("error",a);let c=(d,m)=>{this.#t=null,r(new w(`Query Engine exited with code ${d} and signal ${m}`))};return s.once("exit",c),s.stdout.on("data",i),this.#e.debug&&(s.stderr.on("data",console.error.bind(console,"[Query Engine]")),s.stdout.on("data",console.debug.bind(console,"[Query Engine]"))),await this.#t}async stop(){if(this.#t==null)return;let{childProcess:e}=await this.#t;e.exitCode==null&&e.signalCode==null&&(this.#t=null,e.kill(),await Qe(e,"exit"))}async#i(){this.#e.debug&&console.debug("[Query Engine] getting engine commit hash...");let e=await this.#a();this.#e.debug&&console.debug("[Query Engine] got engine commit hash",e);let r=J(this.#e.clientVersion,e);this.#e.debug&&console.debug("[Query Engine] cache directory path",r),await Ge(r,{recursive:!0});let{platform:n}=this.#e.platform,o=n==="windows"?".exe":"",s=Je(r,`query-engine-${n}${o}`);return this.#e.debug&&console.debug("[Query Engine] binary path",s),(Xe==="1"||await D(s)===!1)&&await this.#o({commitHash:e,extension:o,engineBinaryPath:s}),s}async#a(){let e=await fetch(`https://registry.npmjs.org/@prisma/client/${this.#e.clientVersion}`);if(!e.ok)throw new Error(`Couldn't fetch package.json from npm registry, status code: ${e.status}`);let n=(await e.json()).devDependencies?.["@prisma/engines-version"];if(!n)throw new Error("Couldn't find engines version in package.json");let o=n.split(".").at(-1);if(!o)throw new Error("Couldn't find commit hash in engines version");return o}async#o(e){let{commitHash:r,extension:n,engineBinaryPath:o}=e,{binaryTarget:s}=this.#e.platform,i=`https://binaries.prisma.sh/all_commits/${r}/${s}/query-engine${n}.gz`;this.#e.debug&&console.debug("[Query Engine] downloading engine from url",i);let a=await fetch(i);if(!a.ok)throw new Error(`Couldn't download engine. URL: ${i}, status code: ${a.status}`);ee&&await ze(Number(ee)),await W(await a.arrayBuffer(),o),this.#e.debug&&console.debug("[Query Engine] downloaded and saved at",o)}#n(e){let r={};for(let[n,o]of Object.entries(e))o!=null&&(r[n]=o);return r}async#s(e,r,n){let{url:o}=await this.#t,s=this.#n(r),i=await fetch(`${o}/transaction/${e}/${n}`,{headers:{...s,"Content-Type":"application/json"},method:"POST"});if(!i.ok)throw await h.fromResponse(i);try{return await i.json()}catch{return{}}}};function R(t,e){return console.error(t),t instanceof w?e.json({EngineNotStarted:{reason:{EngineStartupError:{logs:[],msg:t.message}}}},500):t instanceof h?e.text(t.responseBody,t.statusCode):e.body(null,500)}var w=class extends Error{name="EngineStartError"},h=class t extends Error{constructor(r,n,o){super(`${r}: Query Engine response status ${n}, body: ${o}`);this.action=r;this.statusCode=n;this.responseBody=o}name="EngineHttpError";static async fromResponse(r){let n=new URL(r.url),o=await r.text();return new t(n.pathname,r.status,o)}};var te=51214,re=51213,ne=51215,f=class extends Error{constructor(r,n){super(`Port number \`${r}\` is not available for service ${n}.`);this.port=r;this.service=n}name="PortNotAvailableError"};import{Buffer as oe}from"buffer";var T=new Map;async function L(t){let r=new TextEncoder().encode(t),n=await crypto.subtle.digest("SHA-256",r);return Array.from(new Uint8Array(n)).map(i=>i.toString(16).padStart(2,"0")).join("")}function se(t){let e=t.req.param("schemaHash"),r=T.get(e);return r==null?t.json({EngineNotStarted:{reason:"SchemaMissing"}},404):{schemaHash:e,schemas:r}}var et=/datasource\s+db\s+\{\s*provider\s*=\s*"postgres(!?ql)?"\s+url\s*=\s*.+\s*\}/;async function ie(t,e){let r=oe.from(t,"base64").toString("utf8"),n=`datasource db {
|
|
5
5
|
provider = "postgresql"
|
|
6
|
-
url = "${e}"
|
|
7
|
-
}`,o=r.replace(
|
|
8
|
-
`,{encoding:"utf-8"})}async#
|
|
6
|
+
url = "${e.toString()}"
|
|
7
|
+
}`,o=r.replace(et,n),s=await L(o);return{base64Override:oe.from(o,"utf8").toString("base64"),overrideHash:s}}function O(t){let{req:e}=t;return{traceparent:e.header("traceparent"),"X-capture-telemetry":e.header("X-capture-telemetry")}}import{integer as ae,looseObject as tt,minValue as ce,number as B,object as rt,optional as nt,pipe as le,safeParse as de,string as ue,union as ot}from"valibot";var st=rt({isolation_level:nt(ue()),max_wait:le(B(),ae(),ce(0)),timeout:le(B(),ae(),ce(0))});async function me(t){let{issues:e,output:r,success:n}=de(st,await t.req.json(),{abortEarly:!0});return n?r:t.json({EngineNotStarted:{reason:"InvalidRequest",issues:e}},400)}var it=tt({id:ot([ue(),B()])});function pe(t,e){let{output:r,success:n}=de(it,t);return n?r:e.json({EngineMalfunction:{}},500)}async function fe(t,e){let{port:r}=e;if(e.dryRun)return ge(r,null);let n=await pt(r,t,e),{promise:o,reject:s,resolve:i}=E(),a=lt({createServer:at,fetch:n.fetch,port:r},i);return a.on("error",c=>{if(typeof c=="object"&&"code"in c&&c.code==="EADDRINUSE")return s(new f(r,"server"));console.error("[Accelerate]",c)}),await o,ge(r,a)}function ge(t,e){return{async close(){e&&await Promise.allSettled([ct(e.close.bind(e))(),b.stopAll()])},port:t,url:`http://localhost:${t}`}}async function pt(t,e,r){let{debug:n}=r,o=new he,s=await dt.getPlatformInfo();return n&&console.debug("[Accelerate] platform info: %s",JSON.stringify(s)),n&&o.use("*",ut((...i)=>console.log("[Accelerate]",...i))),o.use("*",async(i,a)=>(i.set("db",e),i.set("debug",!!n),i.set("platform",s),i.set("port",t),i.set("protocol","http"),i.set("serverState",r),await a())),o.route("/",P),o}var P=new he;P.get("/health",t=>t.json({name:t.get("serverState").name}));P.post("/invalidate",S("header",g),async t=>{let e=await Q(t);return e instanceof Response?e:t.body(null)});var gt="/:clientVersion/:schemaHash",v=P.basePath(gt);P.route("/",v);var ht=["/graphql","/itx/:transactionId/graphql"];v.on("POST",[...ht],S("header",g),async t=>{let{req:e}=t;try{let r=await H(t);if(r instanceof Response)return r;let n=await e.text(),o=e.param("transactionId"),s=await r.request(n,{...O(t),"X-transaction-id":o});return t.text(s)}catch(r){return R(r,t)}});v.basePath("/itx/:transactionId").on("POST",["/commit","/rollback"],S("header",g),async t=>{let{req:e}=t;try{let r=await H(t);if(r instanceof Response)return r;let o=`${e.routePath.split("/").filter(Boolean).at(-1)}Transaction`,s=e.param("transactionId"),i=await r[o](s,O(t));return t.json(i)}catch(r){return R(r,t)}});v.put("/schema",S("header",g),async t=>{let{req:e}=t,r=await e.text();if(!r)return t.text("Missing schema",400);let n=e.param("schemaHash"),o=T.get(n);if(o==null){if(n!==await L(r))return t.text("Schema hash mismatch",400);let s=new URL(t.get("db").connectionString);s.searchParams.set("single_use_connections","true");let i=await ie(r,s);return T.set(n,{base64Original:r,...i}),t.text(n)}return r!==o.base64Original?t.text("Schema mismatch",400):t.text(n)});v.post("/transaction/start",S("header",g),async t=>{let{req:e}=t,r=await me(t);if(r instanceof Response)return r;try{let n=await H(t);if(n instanceof Response)return n;let o=await n.startTransaction(r,O(t)),s=pe(o,t);if(s instanceof Response)return s;let{id:i}=s,a=e.param("clientVersion"),c=t.get("port"),d=t.get("protocol"),m=e.param("schemaHash");return t.json({...o,"data-proxy":{endpoint:`${d}://localhost:${c}/${a}/${m}/itx/${i}`}})}catch(n){return R(n,t)}});async function H(t){let{req:e}=t,r=se(t);if(r instanceof Response)return r;let{base64Override:n,overrideHash:o}=r.schemas;return await b.get({base64Schema:n,clientVersion:mt.env.PRISMA_DEV_FORCE_CLIENT_VERSION||e.param("clientVersion"),debug:t.get("debug"),platform:t.get("platform"),schemaHash:o})}import{PGlite as ft}from"@electric-sql/pglite";import{PGLiteSocketServer as yt}from"@electric-sql/pglite-socket";var l={connectionLimit:1,connectTimeout:0,database:"template1",maxIdleConnectionLifetime:0,password:"postgres",poolTimeout:0,socketTimeout:0,sslMode:"disable",username:"postgres"},bt=`postgres://${l.username}:${l.password}@localhost`,wt=new URLSearchParams({connection_limit:String(l.connectionLimit),connect_timeout:String(l.connectTimeout),max_idle_connection_lifetime:String(l.maxIdleConnectionLifetime),pool_timeout:String(l.poolTimeout),socket_timeout:String(l.socketTimeout),sslmode:l.sslMode});async function C(t,e){let r=t==="database"?e.databasePort:e.shadowDatabasePort;if(e.dryRun)return ye(t,e,{db:null,port:r,server:null});let{debug:n}=e,o=await ft.create({database:l.database,dataDir:t==="database"?e.pgliteDataDirPath:"memory://",debug:n?5:void 0,defaultDataTransferContainer:"file",relaxedDurability:!1,username:l.username});n&&o.onNotification((i,a)=>{console.debug(`[${t}][${i}] ${a}`)});let s=new yt({db:o,debug:n,inspect:n,port:r});n&&(s.addEventListener("listening",i=>{let{detail:a}=i;console.debug(`[${t}] server listening on ${JSON.stringify(a)}`)}),s.addEventListener("connection",i=>{let{clientAddress:a,clientPort:c}=i.detail;console.debug(`[${t}] client connected from ${a}:${c}`)}),s.addEventListener("error",i=>{let{detail:a}=i;console.error(`[${t}] server error:`,a)}));try{await s.start()}catch(i){throw i instanceof Error&&"code"in i&&i.code==="EADDRINUSE"?new f(r,t):i}return ye(t,e,{db:o,port:r,server:s})}function ye(t,e,r){let{debug:n}=e,{db:o,port:s,server:i}=r||{};return n&&console.debug(`[${t}] server started on port ${s}`),{...l,close:async()=>{let a=[];try{await i?.stop(),n&&console.debug(`[${t}] server stopped on port ${s}`)}catch(c){console.error(`[${t}] server stop error`,c),a.push(c)}if(t==="database")try{await o?.syncToFs(),n&&console.debug(`[${t}] synced to filesystem`)}catch(c){console.error(`[${t}] sync error`,c),a.push(c)}try{await o?.close(),n&&console.debug(`[${t}] closed`)}catch(c){console.error(`[${t}] close error`,c),a.push(c)}if(a.length>0)throw new AggregateError(a,`Failed to close ${t} properly`)},connectionString:St(s),port:s}}function St(t){return`${bt}:${t}/${l.database}?${wt.toString()}`}import{copyFile as Pt,mkdir as vt,writeFile as Et}from"fs/promises";import{join as x}from"pathe";import{lock as At}from"proper-lockfile";import{readLastLines as Dt}from"read-last-lines-ts";import{process as Rt}from"std-env";var j=Symbol("initialize"),y=class{databasePort;debug;dryRun;name;persistenceMode;port;shadowDatabasePort;constructor(e){this.databasePort=e.databasePort??51214,this.debug=e.debug??!1,this.dryRun=e.dryRun??!1,this.name=e.name??"default",this.persistenceMode=e.persistenceMode,this.port=e.port??51213,this.shadowDatabasePort=e.shadowDatabasePort??51215}static async get(e){let r=e?.dryRun!==!0&&e?.persistenceMode!=="stateless"?new F(e):new M(e);return await r[j](),r}},M=class extends y{constructor(e){super({...e,persistenceMode:"stateless"})}get pgliteDataDirPath(){return"memory://"}async[j](){}async close(){}readDatabaseDump(){return Promise.resolve(null)}async writeDatabaseDump(){}async writeServerDump(){}},F=class extends y{#r;#e;#t;#i;#a;#o=null;constructor(e){super({...e,persistenceMode:"stateful"}),this.#r=z(this.name),this.#e=x(this.#r,"ppg.sql"),this.#t=`${this.#e}.bak`,this.#i=x(this.#r,".pglite"),this.#a=x(this.#r,"server.json")}get pgliteDataDirPath(){return this.#i}async[j](){await vt(this.#r,{recursive:!0}),this.debug&&console.debug(`[State] using data directory: ${this.#r}`);try{this.#o=await At(this.#r,{lockfilePath:x(this.#r,".lock")}),this.debug&&console.debug(`[State] obtained lock on: ${this.#r}`),await this.writeServerDump()}catch(e){throw e instanceof Error&&"code"in e&&e.code==="ELOCKED"?new Error(`A server with the name "${this.name}" is already running. `):e}}async close(){this.#o!=null&&(await this.#o(),this.debug&&console.debug(`[State] released lock on: ${this.#r}`))}async readDatabaseDump(){let[e,r]=await Promise.all([k(this.#e),k(this.#t)]),n=Number.isFinite(e)?this.#e:null,o=Number.isFinite(r)?this.#t:null,s=(e>=r?[n,o]:[o,n]).filter(Boolean);for(let i of s){if(!await this.#n(i)){this.debug&&console.debug(`[State] database dump not completed at: ${i}`);continue}let a=await Z(i);if(!a){this.debug&&console.debug(`[State] database dump no longer available at: ${i}`);continue}if(!this.#s(a)){this.debug&&console.debug(`[State] database dump no longer completed at: ${i}`);continue}return this.debug&&console.debug(`using database dump from: ${i}`),a}return this.debug&&console.debug("no completed database dumps found"),null}async writeDatabaseDump(e){let r=!1;await this.#n(this.#e)&&(await Pt(this.#e,this.#t),r=!0,this.debug&&console.debug(`backed up dump to: ${this.#t}`));let n=await e();if(await Y(n,this.#e),this.debug&&console.debug(`dumped database to: ${this.#e}`),!r)return;let o=await X(this.#t);this.debug&&o&&console.debug(`deleted backup dump: ${this.#t}`)}async writeServerDump(e){await Et(this.#a,`${JSON.stringify({name:this.name,version:"1",pid:Rt.pid,port:this.port,databasePort:this.databasePort,shadowDatabasePort:this.shadowDatabasePort,exports:e},null,2)}
|
|
8
|
+
`,{encoding:"utf-8"})}async#n(e){if(!await D(e))return!1;let r=Dt(e,5).toString("utf-8");return this.debug&&console.debug(`last lines of checked dump: ${r}`),this.#s(r)}#s(e){return e.includes("-- PostgreSQL database dump complete")}};async function Lr(t){let e=await y.get(t),[r,n]=await Promise.all([C("database",e),C("shadow_database",e)]),o=await fe(r,e),s=`prisma+postgres://localhost:${o.port}/?${new URLSearchParams({api_key:K({databaseUrl:r.connectionString,shadowDatabaseUrl:n.connectionString})}).toString()}`,i={accelerate:{url:o.url},database:{connectionString:r.connectionString},ppg:{url:s},shadowDatabase:{connectionString:n.connectionString}};return await e.writeServerDump(i),{...i,close:()=>a(e,[o,r,n])};async function a(c,d){let p=(await Promise.allSettled(d.map(u=>u.close()))).filter(u=>u.status==="rejected").map(u=>new Error(u.reason));try{await c.close()}catch(u){p.push(u)}if(p.length>0)throw new AggregateError(p,"Failed to close some servers")}}export{te as DEFAULT_DATABASE_PORT,re as DEFAULT_SERVER_PORT,ne as DEFAULT_SHADOW_DATABASE_PORT,f as PortNotAvailableError,Lr as unstable_startServer};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma/dev",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A local Prisma Postgres server for development and testing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -14,6 +14,11 @@
|
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"import": "./dist/index.js",
|
|
16
16
|
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./daemon": {
|
|
19
|
+
"types": "./dist/daemon/client.d.ts",
|
|
20
|
+
"import": "./dist/daemon/client.js",
|
|
21
|
+
"default": "./dist/daemon/client.js"
|
|
17
22
|
}
|
|
18
23
|
},
|
|
19
24
|
"keywords": [
|
|
@@ -34,7 +39,8 @@
|
|
|
34
39
|
"pkg-types": "2.1.0",
|
|
35
40
|
"tsup": "8.5.0",
|
|
36
41
|
"typescript": "5.8.3",
|
|
37
|
-
"vitest": "3.1.4"
|
|
42
|
+
"vitest": "3.1.4",
|
|
43
|
+
"common-stuff": "^0.0.0"
|
|
38
44
|
},
|
|
39
45
|
"dependencies": {
|
|
40
46
|
"@electric-sql/pglite": "0.3.2",
|
|
@@ -49,6 +55,7 @@
|
|
|
49
55
|
"pathe": "2.0.3",
|
|
50
56
|
"proper-lockfile": "4.1.2",
|
|
51
57
|
"read-last-lines-ts": "1.2.1",
|
|
58
|
+
"sock-daemon": "1.4.2",
|
|
52
59
|
"std-env": "3.9.0",
|
|
53
60
|
"valibot": "1.1.0"
|
|
54
61
|
},
|