@prisma/extension-optimize 0.0.0-dev.202412162344 → 0.0.0-dev.202412162349
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.js +2 -2
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var Se=Object.create;var x=Object.defineProperty;var de=Object.getOwnPropertyDescriptor;var he=Object.getOwnPropertyNames;var fe=Object.getPrototypeOf,ge=Object.prototype.hasOwnProperty;var xe=(
|
|
2
|
-
`:
|
|
1
|
+
"use strict";var Se=Object.create;var x=Object.defineProperty;var de=Object.getOwnPropertyDescriptor;var he=Object.getOwnPropertyNames;var fe=Object.getPrototypeOf,ge=Object.prototype.hasOwnProperty;var xe=(t,e)=>{for(var n in e)x(t,n,{get:e[n],enumerable:!0})},F=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of he(e))!ge.call(t,r)&&r!==n&&x(t,r,{get:()=>e[r],enumerable:!(s=de(e,r))||s.enumerable});return t};var Te=(t,e,n)=>(n=t!=null?Se(fe(t)):{},F(e||!t||!t.__esModule?x(n,"default",{value:t,enumerable:!0}):n,t)),ye=t=>F(x({},"__esModule",{value:!0}),t);var Fe={};xe(Fe,{PROD_DASHBOARD_URL:()=>se,PROD_INGESTION_URL:()=>ae,withOptimize:()=>ze});module.exports=ye(Fe);var ne=require("@opentelemetry/api"),re=require("@prisma/client/extension");function Ie(t,e){let n;return async function(s,r,i){let p=new URL(s,t),c=new Headers({"Content-Type":"application/json",Authorization:`Bearer ${e}`});n&&c.set("prisma-optimize-jwt",n);let l=await fetch(p,{method:r,headers:c,body:JSON.stringify(i)});if(!l.ok){let g=await l.clone().text().catch(()=>"<unreadable>");console.error(`[optimize] HTTP ${l.status} ${l.statusText}: ${g}`)}let f=l.headers.get("prisma-optimize-jwt");return f&&(n=f),l}}function k(t,e){let n=Ie(t,e);return{request:n,post:(s,r)=>n(s,"POST",r)}}var H=Te(require("kleur"));var{bold:be,underline:Re}=H.default;function M(t){let e="See your Optimize dashboard at:",n=e.length,s=`${Re(be(t))}`,r=t.length,i=Math.max(n,r)+2;console.log("\u250C"+"\u2500".repeat(i)+"\u2510"),console.log("\u2502 "+e+" ".repeat(i-n-2)+" \u2502"),console.log("\u2502 "+s+" ".repeat(i-r-2)+" \u2502"),console.log("\u2514"+"\u2500".repeat(i)+"\u2518")}var D=require("@opentelemetry/api"),N=require("@opentelemetry/context-async-hooks"),J=require("@opentelemetry/instrumentation"),V=require("@opentelemetry/resources"),G=require("@opentelemetry/sdk-trace-base"),R=require("@opentelemetry/semantic-conventions"),Z=require("@prisma/instrumentation");async function B(t,e){e.length>0&&await t.post("/ingest",e)}async function $(t,e){return await(await t.post("/schema",e)).text()}function u(t){return t[0]*1e3+t[1]/1e6}var a=require("zod");var m=require("zod"),T=class{spans={};rootSpans={};waitSpans={};treeShape;collectedSpans;collectedRootSpans;sendTimeout;sendInterval;pendingSend;constructor(e){this.treeShape=e.treeShape,this.sendTimeout=e.sendTimeout,this.sendInterval=e.sendInterval,this.collectedSpans=e.collectedSpans,this.collectedRootSpans=e.collectedRootSpans,this.onEnd=ve(()=>void this.forceFlush(),this.sendInterval)}onStart(e){let{parentSpanId:n,name:s}=e,{spanId:r}=e.spanContext();if(!this.collectedSpans.includes(s))return this.cleanSpan(this.spans[r]);this.spans[r]&&this.spans[r].kind==="future"?Object.assign(this.spans[r],{kind:"present",value:e}):this.spans[r]={spanId:r,kind:"present",children:[],value:e,retries:0},this.collectedRootSpans.includes(s)&&(this.rootSpans[r]={...this.spans[r],done:!1},this.waitSpans[r]=this.spans[r]),n!==void 0&&(this.spans[n]?this.spans[n].children.push(this.spans[r]):(this.spans[n]={spanId:n,kind:"future",children:[this.spans[r]],retries:0},this.waitSpans[n]=this.spans[n]))}cleanSpan(e){if(e===void 0)return;let n=e.spanId;for(let s of e.children)this.cleanSpan(s);delete this.spans[n],delete this.rootSpans[n],delete this.waitSpans[n],this.onClean(e)}async forceFlush(){await this.pendingSend;let e=Object.values(this.rootSpans).flatMap(n=>{if(n.done)return[];let s=this.treeShape.safeParse(n);return s.data?(n.done=!0,[{original:n,pruned:s.data}]):[]});e.length>0&&(this.pendingSend=this.onFlush(e).finally(()=>{e.forEach(({original:n})=>this.cleanSpan(n))}));for(let n of Object.values(this.waitSpans))++n.retries*this.sendInterval>this.sendTimeout&&this.cleanSpan(n)}async shutdown(){await this.forceFlush(),this.spans={},this.rootSpans={},this.waitSpans={}}onEnd(){}};function ve(t,e){let n=!1;return()=>{n===!1&&(n=!0,setTimeout(()=>(n=!1)||t(),e))}}var o={ended:m.z.literal(!0),_spanContext:m.z.custom(),spanContext:m.z.custom(),startTime:m.z.custom(),endTime:m.z.custom(),attributes:m.z.custom()};var L=a.z.object({value:a.z.object({name:a.z.literal("prisma:engine:db_query"),...o})}),qe=a.z.object({value:a.z.object({name:a.z.literal("prisma:engine:itx_execute_single"),...o}),children:S([L])}),y=a.z.object({value:a.z.object({name:a.z.union([a.z.literal("prisma:engine"),a.z.literal("prisma:engine:query")]),...o}),children:S([L])}),Ce=a.z.object({value:a.z.object({name:a.z.union([a.z.literal("prisma:engine"),a.z.literal("prisma:engine:query")]),...o}),children:S([qe])}),I=a.z.object({value:a.z.object({name:a.z.literal("prisma:client:connect"),...o})}),q=a.z.object({value:a.z.object({name:a.z.literal("prisma:client:operation"),...o}),children:a.z.union([a.z.tuple([y]),a.z.tuple([I,y]),a.z.tuple([y,I])])}),C=a.z.object({value:a.z.object({name:a.z.literal("prisma:client:operation"),...o}),children:S([Ce])}),we=a.z.object({value:a.z.object({name:a.z.union([a.z.literal("prisma:engine:start_transaction"),a.z.literal("prisma:engine:commit_transaction")]),...o})}),w=a.z.object({value:a.z.object({name:a.z.literal("prisma:client:operation"),...o}),children:a.z.array(I)}),O=a.z.object({value:a.z.object({name:a.z.literal("prisma:client:transaction"),...o}),children:S([we,C,I])}),P=a.z.object({value:a.z.object({name:a.z.literal("prisma:client:transaction"),...o}),children:S([w,y])}),U=a.z.union([q,O,P]);function S(t){return a.z.array(a.z.custom()).nonempty().superRefine((e,n)=>{t.every(r=>e.some(i=>r.safeParse(i).success))||n.addIssue({code:"custom",message:"Array does not contain all the required items"})})}var b=class extends T{requests={};errors={};schemaHash;resolvedSchemaHash;apiClient;schemaRequest;constructor(e){super({treeShape:U,sendTimeout:e.sendTimeout,sendInterval:e.sendInterval,collectedSpans:["prisma:client:transaction","prisma:client:operation","prisma:client:connect","prisma:engine","prisma:engine:query","prisma:engine:db_query","prisma:engine:itx_execute_single","prisma:engine:start_transaction","prisma:engine:commit_transaction"],collectedRootSpans:["prisma:client:operation","prisma:client:transaction"]}),this.apiClient=e.apiClient,this.schemaRequest=e.schemaRequest}setRequest(e){this.requests[e.spanId]=e}setError(e){this.errors[e.spanId]=e}onClean({spanId:e}){delete this.requests[e],delete this.errors[e]}async onFlush(e){this.schemaHash??=$(this.apiClient,this.schemaRequest),this.resolvedSchemaHash??=await this.schemaHash;let n=e.flatMap(({pruned:s})=>Oe(s,this));await B(this.apiClient,n)}};function Oe(t,e){return O.safeParse(t).success?Pe(t,e):P.safeParse(t).success?Ee(t,e):q.safeParse(t).success?je(t,e):[]}function Pe(t,e){let n=[],s=t.children.filter(r=>C.safeParse(r).success);for(let r of s){if(j({span:r,ip:e})===!1)continue;let i=_e(r);n.push(d({ip:e,span:r,sql:i,connect:!1}))}if(n.length){let r=E(t);n.unshift(d({ip:e,span:t,sql:null,connect:r}))}return n}function Ee(t,e){let n=[],s=t.children.filter(i=>w.safeParse(i).success),r=!1;for(let i of s)j({span:i,ip:e})!==!1&&(r||=E(i),n.push(d({ip:e,span:i,sql:null,connect:!1})));return n.length&&n.unshift(d({ip:e,span:t,sql:null,connect:r})),n}function je(t,e){let n=E(t),s=Ae(t);return j({span:t,ip:e})===!1?[]:[d({ip:e,span:t,sql:s,connect:n})]}function d(t){let{span:e,sql:n,connect:s,ip:r}=t,i=e.value.spanContext().spanId,{value:p}=e;return{hash:r.resolvedSchemaHash,sql:n??null,connect:s??!1,error:r.errors[i]?.error??null,ts:u(p.startTime),model:r.requests[i]?.model??p.attributes.model??null,operation:r.requests[i]?.operation??p.attributes.method,args:r.requests[i]?.args??{},latency:u(p.endTime)-u(p.startTime)}}function Q(t){return t.sort((n,s)=>u(n.value.startTime)-u(s.value.startTime)).reduce((n,s)=>{let r=s.value.attributes["db.statement"];return typeof r=="string"?`${n}${r}
|
|
2
|
+
`:n},"")}function Ae(t){let{children:e}=t.children.find(({value:s})=>s.name==="prisma:engine"||s.name==="prisma:engine:query"),n=e.filter(({value:s})=>s.name==="prisma:engine:db_query");return Q(n)}function _e(t){let{children:e}=t.children.find(({value:r})=>r.name==="prisma:engine"||r.name==="prisma:engine:query"),s=e.filter(({value:r})=>r.name==="prisma:engine:itx_execute_single").flatMap(({children:r})=>r.filter(({value:i})=>i.name==="prisma:engine:db_query"));return Q(s)}function E(t){let{value:e}=t.children.find(({value:n})=>n.name==="prisma:client:connect")??{};return e?u(e.endTime)-u(e.startTime):!1}function j(t){let{span:e,ip:n}=t;return!!n.requests[e.value.spanContext().spanId]}var h;function K(t){if(h)return h;let e=new N.AsyncHooksContextManager().enable();D.context.setGlobalContextManager(e);let n=new G.BasicTracerProvider({resource:new V.Resource({[R.SEMRESATTRS_SERVICE_NAME]:"extension-optimize",[R.SEMRESATTRS_SERVICE_VERSION]:"0.0.0"})});return h=new b(t),n.addSpanProcessor(h),(0,J.registerInstrumentations)({tracerProvider:n,instrumentations:[new Z.PrismaInstrumentation]}),n.register(),h}function W(t){return t._previewFeatures?.includes("tracing")}function X(t){return t._engineConfig.inlineSchema}function Y(t){if(t instanceof Error)return t.stack??t.message;switch(typeof t){case"undefined":return"undefined";case"object":{let e;return t!==null&&typeof t.toString=="function"&&(e=t.toString()),typeof e=="string"&&e!=="[object Object]"?e:JSON.stringify(t)}default:return String(t)}}var ee=require("@prisma/client/extension"),te=ee.Prisma.defineExtension(t=>t.$extends({}));var ae="https://optimize-ingestion.datacdn.workers.dev/",se="https://optimize.prisma.io";function ze({enable:t=!0,ingestionUrl:e=ae,dashboardUrl:n=se,sendInterval:s=500,sendTimeout:r=1e4,showNotification:i=!0,apiKey:p}){return t?(i&&M(n),re.Prisma.defineExtension(c=>{let l=X(c),f=Reflect.get(c,"_activeProvider"),g=Reflect.get(c,"_accelerateEngineConfig"),{protocol:ie}=new URL(g.accelerateUtils.resolveDatasourceUrl(g)),oe={schema:l,provider:f,protocol:ie},pe=k(e,p),A=K({schemaRequest:oe,apiClient:pe,sendInterval:s,sendTimeout:r});if(!W(c))throw new Error('Please enable the "tracing" preview feature and regenerate the client.');return c.$extends({query:{async $allOperations({query:ce,model:le,operation:ue,args:_}){let v=ne.trace.getActiveSpan()?.spanContext().spanId;if(!v)throw new Error("prisma:client:operation span is expected to be entered in the client extension when tracing is enabled");A.setRequest({spanId:v,model:le,operation:ue,args:_});try{return await ce(_)}catch(z){let me=Y(z);throw A.setError({spanId:v,error:me}),z}}}})})):te}0&&(module.exports={PROD_DASHBOARD_URL,PROD_INGESTION_URL,withOptimize});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma/extension-optimize",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.202412162349",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"hono": "4.4.6",
|
|
26
26
|
"tsup": "8.0.2",
|
|
27
27
|
"vitest": "1.4.0",
|
|
28
|
-
"common": "0.0.0-dev.
|
|
28
|
+
"common": "0.0.0-dev.202412162349"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@prisma/client": "5.x"
|