@prisma/extension-optimize 1.0.2 → 1.1.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/index.js +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`:s},"")}function
|
|
1
|
+
"use strict";var te=Object.create;var S=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var re=Object.getPrototypeOf,ie=Object.prototype.hasOwnProperty;var oe=(t,e)=>{for(var n in e)S(t,n,{get:e[n],enumerable:!0})},v=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of se(e))!ie.call(t,s)&&s!==n&&S(t,s,{get:()=>e[s],enumerable:!(r=ne(e,s))||r.enumerable});return t};var ae=(t,e,n)=>(n=t!=null?te(re(t)):{},v(e||!t||!t.__esModule?S(n,"default",{value:t,enumerable:!0}):n,t)),pe=t=>v(S({},"__esModule",{value:!0}),t);var be={};oe(be,{PROD_DASHBOARD_URL:()=>B,PROD_INGESTION_URL:()=>V,withOptimize:()=>ge});module.exports=pe(be);var J=require("@opentelemetry/api"),Q=require("@prisma/client/extension");function ce(t,e){let n;return async function(r,s,o){let a=new URL(r,t),p=new Headers({"Content-Type":"application/json",Authorization:`Bearer ${e}`});n&&p.set("prisma-optimize-jwt",n);let c=await fetch(a,{method:s,headers:p,body:JSON.stringify(o)});if(!c.ok){let f=await c.clone().text().catch(()=>"<unreadable>");console.error(`[optimize] HTTP ${c.status} ${c.statusText}: ${f}`)}let h=c.headers.get("prisma-optimize-jwt");return h&&(n=h),c}}function w(t,e){let n=ce(t,e);return{request:n,post:(r,s)=>n(r,"POST",s)}}var q=ae(require("kleur"));var{bold:le,underline:me}=q.default;function P(t){let e="See your Optimize dashboard at:",n=e.length,r=`${me(le(t))}`,s=t.length,o=Math.max(n,s)+2;console.log("\u250C"+"\u2500".repeat(o)+"\u2510"),console.log("\u2502 "+e+" ".repeat(o-n-2)+" \u2502"),console.log("\u2502 "+r+" ".repeat(o-s-2)+" \u2502"),console.log("\u2514"+"\u2500".repeat(o)+"\u2518")}var j=require("@opentelemetry/api"),_=require("@opentelemetry/context-async-hooks"),z=require("@opentelemetry/instrumentation"),k=require("@opentelemetry/resources"),F=require("@opentelemetry/sdk-trace-base"),R=require("@opentelemetry/semantic-conventions"),M=require("@prisma/instrumentation");var i=require("zod");async function A(t,e){e.length>0&&await t.post("/ingest",e)}async function E(t,e){return await(await t.post("/schema",e)).text()}function l(t){return t[0]*1e3+t[1]/1e6}var m=require("zod"),g=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=ue(()=>void this.forceFlush(),this.sendInterval)}onStart(e){let{parentSpanId:n,name:r}=e,{spanId:s}=e.spanContext();if(!this.collectedSpans.includes(r))return this.cleanSpan(this.spans[s]);this.spans[s]&&this.spans[s].kind==="future"?Object.assign(this.spans[s],{kind:"present",value:e}):this.spans[s]={spanId:s,kind:"present",children:[],value:e,retries:0},this.collectedRootSpans.includes(r)&&(this.rootSpans[s]={...this.spans[s],done:!1},this.waitSpans[s]=this.spans[s]),n!==void 0&&(this.spans[n]?this.spans[n].children.push(this.spans[s]):(this.spans[n]={spanId:n,kind:"future",children:[this.spans[s]],retries:0},this.waitSpans[n]=this.spans[n]))}cleanSpan(e){if(e===void 0)return;let n=e.spanId;for(let r of e.children)this.cleanSpan(r);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{data:r}=this.treeShape.safeParse(n);return r?(n.done=!0,[{original:n,pruned:r}]):[]});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 ue(t,e){let n=!1;return()=>{n===!1&&(n=!0,setTimeout(()=>(n=!1)||t(),e))}}var u={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 de=i.z.object({value:i.z.object({name:i.z.literal("prisma:engine:db_query"),...u})}),y=i.z.object({value:i.z.object({name:i.z.literal("prisma:engine"),...u}),children:i.z.array(de).nonempty()}),O=i.z.object({value:i.z.object({name:i.z.literal("prisma:client:connect"),...u})}),he=i.z.object({value:i.z.object({name:i.z.literal("prisma:client:operation"),...u}),children:i.z.union([i.z.tuple([y]),i.z.tuple([O,y]),i.z.tuple([y,O])])}),b=class extends g{requests={};errors={};schemaHash;apiClient;schemaRequest;constructor(e){super({treeShape:he,sendTimeout:e.sendTimeout,sendInterval:e.sendInterval,collectedSpans:["prisma:client:operation","prisma:client:connect","prisma:engine","prisma:engine:db_query"],collectedRootSpans:["prisma:client:operation"]}),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??=E(this.apiClient,this.schemaRequest);let n=await this.schemaHash,r=[];for(let{pruned:s}of e){let{value:o}=s,a=o.spanContext().spanId;this.requests[a]&&r.push({hash:n,sql:fe(s),ts:l(o.startTime),connect:Se(s),error:this.errors[a]?.error??null,model:this.requests[a].model??null,operation:this.requests[a].operation,args:this.requests[a].args??{},latency:l(o.endTime)-l(o.startTime)})}await A(this.apiClient,r)}};function fe(t){return t.children.find(({value:s})=>s.name==="prisma:engine").children.filter(({value:s})=>s.name==="prisma:engine:db_query").sort((s,o)=>l(s.value.startTime)-l(o.value.startTime)).reduce((s,o)=>{let a=o.value.attributes["db.statement"];return typeof a=="string"?`${s}${a}
|
|
2
|
+
`:s},"")}function Se(t){let{value:e}=t.children.find(({value:n})=>n.name==="prisma:client:connect")??{};return e?l(e.endTime)-l(e.startTime):!1}var d;function H(t){if(d)return d;let e=new _.AsyncHooksContextManager().enable();j.context.setGlobalContextManager(e);let n=new F.BasicTracerProvider({resource:new k.Resource({[R.SEMRESATTRS_SERVICE_NAME]:"extension-optimize",[R.SEMRESATTRS_SERVICE_VERSION]:"0.0.0"})});return d=new b(t),n.addSpanProcessor(d),(0,z.registerInstrumentations)({tracerProvider:n,instrumentations:[new M.PrismaInstrumentation]}),n.register(),d}function L(t){return t._previewFeatures?.includes("tracing")}function U(t){return t._engineConfig.inlineSchema}function $(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 D=require("@prisma/client/extension"),N=D.Prisma.defineExtension(t=>t.$extends({}));var V="https://optimize-ingestion.datacdn.workers.dev/",B="https://optimize.prisma.io";function ge({enable:t=!0,ingestionUrl:e=V,dashboardUrl:n=B,sendInterval:r=500,sendTimeout:s=1e4,showNotification:o=!0,apiKey:a}){return t?(o&&P(n),Q.Prisma.defineExtension(p=>{let c=U(p),h=Reflect.get(p,"_activeProvider"),f=Reflect.get(p,"_accelerateEngineConfig"),{protocol:G}=new URL(f.accelerateUtils.resolveDatasourceUrl(f)),K={schema:c,provider:h,protocol:G},Z=w(e,a),C=H({schemaRequest:K,apiClient:Z,sendInterval:r,sendTimeout:s});if(!L(p))throw new Error('Please enable the "tracing" preview feature and regenerate the client.');return p.$extends({query:{async $allOperations({query:W,model:X,operation:Y,args:I}){let T=J.trace.getActiveSpan()?.spanContext().spanId;if(!T)throw new Error("prisma:client:operation span is expected to be entered in the client extension when tracing is enabled");C.setRequest({spanId:T,model:X,operation:Y,args:I});try{return await W(I)}catch(x){let ee=$(x);throw C.setError({spanId:T,error:ee}),x}}}})})):N}0&&(module.exports={PROD_DASHBOARD_URL,PROD_INGESTION_URL,withOptimize});
|