@prisma/dev 0.0.0-dev.202505211033 → 0.0.0-dev.202505211457
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/index.d.ts +8 -0
- package/dist/index.js +4 -4
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
type DBPersistenceMode = "dump_on_close" | "memory";
|
|
2
|
+
|
|
1
3
|
declare const DEFAULT_ACCELERATE_PORT = 51213;
|
|
2
4
|
declare const DEFAULT_DATABASE_PORT = 51214;
|
|
3
5
|
declare const DEFAULT_SHADOW_DATABASE_PORT = 51215;
|
|
@@ -38,6 +40,12 @@ interface StartServerOptions {
|
|
|
38
40
|
* Defaults to `false`.
|
|
39
41
|
*/
|
|
40
42
|
dryRun?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* TODO: Add description.
|
|
45
|
+
*
|
|
46
|
+
* Default is `memory`.
|
|
47
|
+
*/
|
|
48
|
+
persistenceMode?: DBPersistenceMode;
|
|
41
49
|
/**
|
|
42
50
|
* The port the shadow database server will listen on.
|
|
43
51
|
*
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createRequire } from 'module'; const require = createRequire(import.meta.url);
|
|
2
|
-
import{createServer as
|
|
3
|
-
`).find(
|
|
4
|
-
Received data: ${i}`));s({childProcess:
|
|
2
|
+
import{createServer as qe}from"http";import{promisify as Fe}from"util";import{serve as Ne}from"@hono/node-server";import Ve from"@prisma/get-platform";import{logger as Ke}from"hono/logger";import{Hono as oe}from"hono/tiny";import{validator as S}from"hono/validator";import{HTTPException as P}from"hono/http-exception";import{object as ie,optional as ce,parseJson as pe,pipe as O,regex as $,safeParse as ue,string as D,url as k}from"valibot";var L=/^(postgres|postgresql):\/\//,le=O(D(),pe(),ie({databaseUrl:O(D(),k(),$(L)),shadowDatabaseUrl:ce(O(D(),k(),$(L)))}));function j(t){return Buffer.from(JSON.stringify(t),"utf8").toString("base64url")}function de(t){let e=Buffer.from(t,"base64url").toString("utf8"),{issues:r,output:n,success:o}=ue(le,e,{abortEarly:!0});return o?[null,n]:[r]}var d=(t,e)=>{let{authorization:r}=t;if(!r)throw new P(401,{message:"Missing API Key"});let[n,o="",s]=r.split(" ");if(n!=="Bearer"||s)throw new P(401,{message:"Invalid API Key"});let[a,i]=de(o);if(a)throw new P(401,{message:"Invalid API Key",cause:a.join(", ")});let{databaseUrl:c}=i;if(c!==e.var.db.connectionString)throw new P(401,{message:"Unauthorized"});return{decodedAPIKey:i}};import{array as me,literal as ge,minLength as fe,object as he,pipe as ye,safeParse as Se,string as we,union as be}from"valibot";var ve=he({tags:be([ye(me(we()),fe(1)),ge("all")])});async function M(t){let{output:e,success:r}=Se(ve,await t.req.json(),{abortEarly:!0});return r?e:t.text("Invalid input",400)}import{spawn as He}from"child_process";import{once as _e}from"events";import{mkdir as Ie}from"fs/promises";import{join as Ce}from"path";import{createWriteStream as Pe,WriteStream as Ee}from"fs";import{chmod as Ae,stat as Te,writeFile as Re}from"fs/promises";import xe from"env-paths";import{inflate as Oe}from"pako";var De=xe("prisma-dev").cache;function U(t,e){return`${De}/${t}/${e}`}async function q(t){try{return await Te(t),!0}catch(e){if(e!=null&&typeof e=="object"&&"code"in e&&e.code==="ENOENT")return!1;throw e}}async function F(t,e){let r=Oe(t);await Re(e,r),await Ae(e,"755")}async function N(t,e){await t.stream().pipeTo(Ee.toWeb(Pe(e,{encoding:"utf-8"})))}function E(){let t,e,r=new Promise((n,o)=>{t=n,e=o});return{fail:e,promise:r,succeed:t}}var h=class t{static#r=new Map;#e;#t;constructor(e){this.#e=e,this.#t=null}static async get(e){let r=`${e.schemaHash}:${e.clientVersion}`;try{let n=t.#r.get(r);if(n)return n;let o=new t(e);return t.#r.set(r,o),e.debug&&console.debug("starting engine...",e),await o.start(),e.debug&&console.debug("engine started!"),o}finally{t.stopAll(r)}}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.#o(e,r,"commit")}async request(e,r){let{url:n}=await this.#t,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 m.fromResponse(s);return await s.text()}async rollbackTransaction(e,r){return await this.#o(e,r,"rollback")}async startTransaction(e,r){let{url:n}=await this.#t,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 m.fromResponse(s);return await s.json()}async start(){if(this.#t!=null)return;let e=await this.#s();this.#e.debug&&console.debug("spinning up engine at path...",e);let r=He(e,["--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});r.stderr.setEncoding("utf8"),r.stdout.setEncoding("utf8");let{fail:n,promise:o,succeed:s}=E();this.#t=o;let a=i=>{let c=i.split(`
|
|
3
|
+
`).find(x=>x.includes("Started query engine http server"));if(!c)return;r.stdout.removeListener("data",a);let{fields:u}=JSON.parse(c);if(u==null)return n(new Error(`Unexpected data during initialization, "fields" are missing: ${i}`));let{ip:l,port:v}=u;if(l==null||v==null)return n(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: ${i}`));s({childProcess:r,url:`http://${l}:${v}`})};r.once("error",i=>{this.#t=null,n(new y(String(i)))}),r.once("exit",(i,c)=>{this.#t=null,n(new y(`Query Engine exited with code ${i} and signal ${c}`))}),r.stdout.on("data",a),r.stderr.on("data",i=>{console.error(i)}),await this.#t}async stop(){if(this.#t==null)return;let{childProcess:e}=await this.#t;e.exitCode==null&&e.signalCode==null&&(e.kill(),await _e(e,"exit"))}async#s(){this.#e.debug&&console.debug("getting engine commit hash...");let e=await this.#a();this.#e.debug&&console.debug("got engine commit hash",e);let r=U(this.#e.clientVersion,e);this.#e.debug&&console.debug("cache directory path",r),await Ie(r,{recursive:!0});let{platform:n}=this.#e.platform,o=n==="windows"?".exe":"",s=Ce(r,`query-engine-${n}${o}`);return this.#e.debug&&console.debug("engine binary path",s),(process.env.PRISMA_DEV_FORCE_ENGINE_DOWNLOAD==="1"||await q(s)===!1)&&await this.#i({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#i(e){let{commitHash:r,extension:n,engineBinaryPath:o}=e,{binaryTarget:s}=this.#e.platform,a=`https://binaries.prisma.sh/all_commits/${r}/${s}/query-engine${n}.gz`;this.#e.debug&&console.debug("downloading engine from url",a);let i=await fetch(a);if(!i.ok)throw new Error(`Couldn't download engine. URL: ${a}, status code: ${i.status}`);await F(await i.arrayBuffer(),o),this.#e.debug&&console.debug("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#o(e,r,n){let{url:o}=await this.#t,s=this.#n(r),a=await fetch(`${o}/transaction/${e}/${n}`,{headers:{...s,"Content-Type":"application/json"},method:"POST"});if(!a.ok)throw await m.fromResponse(a);try{return await a.json()}catch{return{}}}};function A(t,e){return console.error(t),t instanceof y?e.json({EngineNotStarted:{reason:{EngineStartupError:{logs:[],msg:t.message}}}},500):t instanceof m?e.text(t.responseBody,t.statusCode):e.body(null,500)}var y=class extends Error{name="EngineStartError"},m=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 V=51213,K=51214,G=51215,g=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 z}from"buffer";var T=new Map;async function H(t){let r=new TextEncoder().encode(t),n=await crypto.subtle.digest("SHA-256",r);return Array.from(new Uint8Array(n)).map(a=>a.toString(16).padStart(2,"0")).join("")}function J(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 Be=/datasource\s+db\s+\{\s*provider\s*=\s*"postgres(!?ql)?"\s+url\s*=\s*.+\s*\}/;async function W(t,e){let r=z.from(t,"base64").toString("utf8"),n=`datasource db {
|
|
5
5
|
provider = "postgresql"
|
|
6
6
|
url = "${e}"
|
|
7
|
-
}`,o=
|
|
7
|
+
}`,o=r.replace(Be,n),s=await H(o);return{base64Override:z.from(o,"utf8").toString("base64"),overrideHash:s}}function R(t){let{req:e}=t;return{traceparent:e.header("traceparent"),"X-capture-telemetry":e.header("X-capture-telemetry")}}import{integer as Q,looseObject as $e,minValue as X,number as _,object as ke,optional as Le,pipe as Y,safeParse as Z,string as ee,union as je}from"valibot";var Me=ke({isolation_level:Le(ee()),max_wait:Y(_(),Q(),X(0)),timeout:Y(_(),Q(),X(0))});async function te(t){let{issues:e,output:r,success:n}=Z(Me,await t.req.json(),{abortEarly:!0});return n?r:t.json({EngineNotStarted:{reason:"InvalidRequest",issues:e}},400)}var Ue=$e({id:je([ee(),_()])});function re(t,e){let{output:r,success:n}=Z(Ue,t);return n?r:e.json({EngineMalfunction:{}},500)}async function se(t){let{dbServer:e,debug:r,dryRun:n,port:o}=t;if(n)return ne(o,null);let s=await Ge(o,e,r),{fail:a,promise:i,succeed:c}=E(),u=Ne({createServer:qe,fetch:s.fetch,port:o},()=>{c()});return u.on("error",l=>{if(typeof l=="object"&&"code"in l&&l.code==="EADDRINUSE")return a(new g(o,"accelerate"));console.error(l)}),await i,ne(o,u)}function ne(t,e){return{async close(){e&&await Promise.allSettled([Fe(e.close.bind(e))(),h.stopAll()])},port:t,url:`http://localhost:${t}`}}async function Ge(t,e,r){let n=new oe,o=await Ve.getPlatformInfo();return r&&console.debug("platform info: %s",JSON.stringify(o)),r&&n.use("*",Ke()),n.use("*",async(s,a)=>(s.set("db",e),s.set("debug",!!r),s.set("platform",o),s.set("port",t),s.set("protocol","http"),await a())),n.route("/",w),n}var w=new oe;w.get("/health",t=>t.text("ok"));w.post("/invalidate",S("header",d),async t=>{let e=await M(t);return e instanceof Response?e:t.body(null)});var ze="/:clientVersion/:schemaHash",b=w.basePath(ze);w.route("/",b);var Je=["/graphql","/itx/:transactionId/graphql"];b.on("POST",[...Je],S("header",d),async t=>{let{req:e}=t;try{let r=await I(t);if(r instanceof Response)return r;let n=await e.text(),o=e.param("transactionId"),s=await r.request(n,{...R(t),"X-transaction-id":o});return t.text(s)}catch(r){return A(r,t)}});b.basePath("/itx/:transactionId").on("POST",["/commit","/rollback"],S("header",d),async t=>{let{req:e}=t;try{let r=await I(t);if(r instanceof Response)return r;let o=`${e.routePath.split("/").filter(Boolean).at(-1)}Transaction`,s=e.param("transactionId"),a=await r[o](s,R(t));return t.json(a)}catch(r){return A(r,t)}});b.put("/schema",S("header",d),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 H(r))return t.text("Schema hash mismatch",400);let s=await W(r,t.get("db").connectionString);return T.set(n,{base64Original:r,...s}),t.text(n)}return r!==o.base64Original?t.text("Schema mismatch",400):t.text(n)});b.post("/transaction/start",S("header",d),async t=>{let{req:e}=t,r=await te(t);if(r instanceof Response)return r;try{let n=await I(t);if(n instanceof Response)return n;let o=await n.startTransaction(r,R(t)),s=re(o,t);if(s instanceof Response)return s;let{id:a}=s,i=e.param("clientVersion"),c=t.get("port"),u=t.get("protocol"),l=e.param("schemaHash");return t.json({...o,"data-proxy":{endpoint:`${u}://localhost:${c}/${i}/${l}/itx/${a}`}})}catch(n){return A(n,t)}});async function I(t){let{req:e}=t,r=J(t);if(r instanceof Response)return r;let{base64Override:n,overrideHash:o}=r.schemas;return await h.get({base64Schema:n,clientVersion:process.env.PRISMA_DEV_FORCE_CLIENT_VERSION||e.param("clientVersion"),debug:t.get("debug"),platform:t.get("platform"),schemaHash:o})}import{readFile as We}from"fs/promises";import{PGlite as Qe}from"@electric-sql/pglite";import{PGLiteSocketServer as Xe}from"@electric-sql/pglite-socket";import{pgDump as Ye}from"@electric-sql/pglite-tools/pg_dump";var p={connectionLimit:1,connectTimeout:0,database:"postgres",maxIdleConnectionLifetime:0,password:"postgres",poolTimeout:0,socketTimeout:0,sslMode:"disable",username:"postgres"},Ze=`postgres://${p.username}:${p.password}@localhost`,et=new URLSearchParams({connection_limit:String(p.connectionLimit),connect_timeout:String(p.connectTimeout),max_idle_connection_lifetime:String(p.maxIdleConnectionLifetime),pool_timeout:String(p.poolTimeout),socket_timeout:String(p.socketTimeout),sslmode:p.sslMode});async function C(t){let{debug:e,dryRun:r,persistenceMode:n,port:o,purpose:s}=t;if(r)return ae({...t,db:null,server:null});let a=await Qe.create({database:p.database,dataDir:"memory://",debug:e?5:void 0,defaultDataTransferContainer:"file",username:p.username});await a.waitReady,n==="dump_on_close"&&await tt(a);let i=new Xe({db:a,inspect:e,port:o});try{await i.start()}catch(c){throw c instanceof Error&&"code"in c&&c.code==="EADDRINUSE"?new g(o,s):c}return e&&console.debug(`${s} server started on port %i`,o),ae({...t,db:a,server:i})}function ae(t){let{db:e,persistenceMode:r,port:n,purpose:o,server:s}=t;return{...p,close:async()=>{if(!e||!s)return;let a=[];try{await s.stop()}catch(i){a.push(i)}if(r==="dump_on_close")try{await rt(e,o)}catch(i){a.push(i)}try{await e.close()}catch(i){a.push(i)}if(a.length>0)throw new AggregateError(a,`Failed to close ${o} properly`)},connectionString:nt(n),port:n}}async function tt(t){try{let e=await We("./dump.sql",{encoding:"utf-8"});if(!e.length)return;await t.exec(e)}catch(e){if(e instanceof Error&&"code"in e&&e.code==="ENOENT")return;throw e}}async function rt(t,e){if(e!=="database")return;let r=await Ye({pg:t});await N(r,"./dump.sql")}function nt(t){return`${Ze}:${t}/${p.database}?${et.toString()}`}async function or(t){let{acceleratePort:e=51213,databasePort:r=51214,debug:n=!1,dryRun:o=!1,persistenceMode:s="memory",shadowDatabasePort:a=51215}=t||{},[i,c]=await Promise.all([C({debug:n,dryRun:o,persistenceMode:s,port:r,purpose:"database"}),C({debug:n,dryRun:o,port:a,purpose:"shadow_database"})]),u=await se({dbServer:i,debug:n,dryRun:o,port:e}),l=`prisma+postgres://localhost:${u.port}/?${new URLSearchParams({api_key:j({databaseUrl:i.connectionString,shadowDatabaseUrl:c.connectionString})}).toString()}`;return{accelerate:{url:u.url},close:()=>v([u,i,c]),database:{connectionString:i.connectionString},ppg:{url:l},shadowDatabase:{connectionString:c.connectionString}};async function v(x){let B=(await Promise.allSettled(x.map(f=>f.close()))).filter(f=>f.status==="rejected").map(f=>new Error(f.reason));if(B.length>0)throw new AggregateError(B,"Failed to close some servers")}}export{V as DEFAULT_ACCELERATE_PORT,K as DEFAULT_DATABASE_PORT,G as DEFAULT_SHADOW_DATABASE_PORT,g as PortNotAvailableError,or as unstable_startServer};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma/dev",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.202505211457",
|
|
4
4
|
"description": "A local Prisma Postgres server for development and testing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@electric-sql/pglite": "0.3.1",
|
|
40
40
|
"@electric-sql/pglite-socket": "0.0.4",
|
|
41
|
+
"@electric-sql/pglite-tools": "0.2.6",
|
|
41
42
|
"@hono/node-server": "1.14.1",
|
|
42
43
|
"@prisma/get-platform": "6.7.0",
|
|
43
44
|
"env-paths": "3.0.0",
|