@stacksjs/tinker 0.75.568 → 0.75.571
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 +32 -32
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import{appendFileSync as
|
|
3
|
-
`)return n+1;n++}return e.length}function
|
|
4
|
-
`)return-1;if(r==="[")n=!0;else if(r==="]")n=!1;else if(r==="/"&&!n){t++;while(t<e.length&&/[a-z]/i.test(e[t]))t++;return t}t++}return-1}function
|
|
5
|
-
`)r("nl",t+1);else if(s===" "||s==="\t"||s==="\r"){let
|
|
6
|
-
`)
|
|
7
|
-
`)?e.slice(o,r.start):"",
|
|
8
|
-
`)){let n=
|
|
9
|
-
`,
|
|
10
|
-
)`).body;if(n.length===1&&n[0].type==="ExpressionStatement"&&n[0].expression.type==="ObjectExpression")return{root:n[0].expression,shift:1}}catch{}try{return{root:i(e),shift:0}}catch{return}}function
|
|
11
|
-
//# sourceURL=${t}.ts`}function
|
|
12
|
-
`}),r.push(()=>{
|
|
13
|
-
`}function
|
|
14
|
-
`);return e=n.pop()??"",n.map(i)},flush(){if(!e)return[];let t=e;return e="",[{stray:t}]}}}async function
|
|
15
|
-
`);while(
|
|
16
|
-
`)}}if(!
|
|
17
|
-
`).filter(Boolean)}function
|
|
18
|
-
`),n)try{
|
|
2
|
+
import{appendFileSync as ct,chmodSync as ue,existsSync as le,readFileSync as ut,writeFileSync as fe,writeSync as lt}from"fs";import{homedir as ft}from"os";import{join as mt,resolve as dt}from"path";import B from"process";import{writeSync as st}from"fs";import{isAbsolute as Ge,resolve as ae}from"path";import v from"process";var pe=[">>>=","...","===","!==","**=","<<=",">>=",">>>","&&=","||=","??=","=>","==","!=","<=",">=","&&","||","??","?.","++","--","+=","-=","*=","/=","%=","&=","|=","^=","**","<<",">>"],ge=new Set(["return","typeof","instanceof","in","of","new","delete","void","throw","case","do","else","yield","await"]),H=new Set(["if","for","while","do","switch","try","function","class","import","export","throw","break","continue","interface","type","enum","declare","namespace"]);function ye(e){return/[\p{L}_$]/u.test(e)}function be(e){return/[\p{L}\p{N}_$]/u.test(e)}function Y(e,i){let t=i+1;while(t<e.length){let n=e[t];if(n==="\\"){t+=2;continue}if(n==="`")return t+1;if(n==="$"&&e[t+1]==="{"){t+=2;let r=1;while(t<e.length&&r>0){let s=e[t];if(s==="`"){t=Y(e,t);continue}if(s==="'"||s==='"'){t=Q(e,t);continue}if(s==="{")r++;else if(s==="}")r--;t++}continue}t++}return e.length}function Q(e,i){let t=e[i],n=i+1;while(n<e.length){let r=e[n];if(r==="\\"){n+=2;continue}if(r===t||r===`
|
|
3
|
+
`)return n+1;n++}return e.length}function ke(e,i){let t=i+1,n=!1;while(t<e.length){let r=e[t];if(r==="\\"){t+=2;continue}if(r===`
|
|
4
|
+
`)return-1;if(r==="[")n=!0;else if(r==="]")n=!1;else if(r==="/"&&!n){t++;while(t<e.length&&/[a-z]/i.test(e[t]))t++;return t}t++}return-1}function he(e){let i=[],t=0,n,r=(s,a)=>{let u={type:s,start:t,end:a,text:e.slice(t,a)};if(i.push(u),s!=="ws"&&s!=="nl"&&s!=="comment")n=u;t=a};while(t<e.length){let s=e[t];if(s===`
|
|
5
|
+
`)r("nl",t+1);else if(s===" "||s==="\t"||s==="\r"){let a=t+1;while(a<e.length&&(e[a]===" "||e[a]==="\t"||e[a]==="\r"))a++;r("ws",a)}else if(s==="/"&&e[t+1]==="/"){let a=t+2;while(a<e.length&&e[a]!==`
|
|
6
|
+
`)a++;r("comment",a)}else if(s==="/"&&e[t+1]==="*"){let a=e.indexOf("*/",t+2);r("comment",a===-1?e.length:a+2)}else if(s==="'"||s==='"')r("string",Q(e,t));else if(s==="`")r("template",Y(e,t));else if(s==="/"){let a=n,x=!a||a.type==="punct"&&a.text!==")"&&a.text!=="]"&&a.text!=="}"||a.type==="word"&&ge.has(a.text)?ke(e,t):-1;if(x!==-1)r("regex",x);else r("punct",e[t+1]==="="?t+2:t+1)}else if(/\d/.test(s)||s==="."&&/\d/.test(e[t+1]??"")){let a=t+1;while(a<e.length&&/[\w.]/.test(e[a]))a++;r("number",a)}else if(ye(s)||s==="#"){let a=t+1;while(a<e.length&&be(e[a]))a++;r("word",a)}else{let a=pe.find((u)=>e.startsWith(u,t));r("punct",t+(a?.length??1))}}return i}var xe={")":"(","]":"[","}":"{"},X=new Set([")","]","}"]),Z=new Set(["(","[","{"]),we=new Set([".","?.",",","(","[","=","=>","+","-","*","/","%","**","&&","||","??","?",":","<",">","<=",">=","==","===","!=","!==","&","|","^","!","~","+=","-=","*=","/=","??=","||=","&&="]),G=new Set([".","?.",",",")","]","=","=>","+","-","*","/","%","**","&&","||","??","?",":","<",">","<=",">=","==","===","!=","!==","&","|","^"]),Te=new Set(["return","await","typeof","new","in","of","instanceof","yield","void","delete","const","let","var","as"]);function ee(e){return Boolean(e)&&e.type!=="ws"&&e.type!=="nl"&&e.type!=="comment"}function I(e,i){for(let t=i;t>=0;t--)if(ee(e[t]))return t;return-1}function V(e,i){for(let t=i;t<e.length;t++)if(ee(e[t]))return t;return-1}function Se(e,i){let t=0,n=i;for(let r=i;r>=0;r--){let s=e[r];if(s.type==="comment"||s.type==="ws")continue;if(s.type==="nl"){if(t>0)continue;let a=I(e,r-1),u=V(e,r+1);if(a===-1)return n;let x=e[a],k=e[u];if(!(x.type==="punct"&&we.has(x.text)||x.type==="word"&&Te.has(x.text)||k?.type==="punct"&&G.has(k.text)))return n;continue}if(s.type==="punct"){if(X.has(s.text)){if(t===0&&s.text==="}"){let a=V(e,r+1),u=e.slice(r+1,a===-1?void 0:a).some((k)=>k.type==="nl"),x=a===-1?void 0:e[a];if(r!==i&&u&&!(x?.type==="punct"&&G.has(x.text)))return n}t++,n=r;continue}if(Z.has(s.text)){if(t===0)return n;t--,n=r;continue}if(t===0&&(s.text===";"||s.text==="=>"))return n}n=r}return t===0?n:-1}function Ee(e,i){let t=I(e,i-1);if(t===-1||t!==i-1)return-1;let n=-1,r=!0;while(t>=0){let a=e[t];if(a.type==="ws"||a.type==="comment")break;if(r){if(a.type==="punct"&&(a.text===")"||a.text==="]")){let u=0,x=t;for(;x>=0;x--){let h=e[x];if(h.type!=="punct")continue;if(h.text===a.text)u++;else if(h.text===xe[a.text])u--;if(u===0)break}if(x<0)return-1;n=x,t=x-1;let k=e[t];if(k&&(k.type==="word"||k.type==="punct"&&(k.text===")"||k.text==="]"))){r=!0;continue}r=!1;continue}if(a.type==="word"||a.type==="string"||a.type==="template"||a.type==="number"||a.type==="regex"){n=t,t--,r=!1;continue}return-1}if(a.type==="punct"&&(a.text==="."||a.text==="?.")){t--,r=!0;continue}break}if(n===-1||r)return-1;let s=I(e,n-1);if(s!==-1&&e[s].type==="word"&&(e[s].text==="await"||e[s].text==="new"))n=s;return n}function $e(e,i,t){let n=V(e,i+1);if(n===-1||n>t)return;let r=e[n];if(r.type==="word")return r.text;if(r.type!=="punct"||r.text!=="{"&&r.text!=="[")return;let s=[],a=0;for(let u=n;u<=t;u++){let x=e[u];if(x.type==="punct"&&Z.has(x.text))a++;else if(x.type==="punct"&&X.has(x.text))a--;else if(x.type==="word"&&a>0){let k=V(e,u+1),h=I(e,u-1),l=k!==-1&&e[k].text===":",S=h!==-1&&e[h].text==="=";if(!l&&!S)s.push(x.text)}if(a===0&&u>n)break}return s.length?`{ ${s.join(", ")} }`:void 0}var te=/^\/\/\?(\.)?\s*$/,ne=/^\/\*\?(.*)\*\/$/s;function Ne(e){return e.includes("//?")||e.includes("/*?")}function re(e,i={probe:"__tinkerProbe",time:"__tinkerTime"}){if(!Ne(e))return{code:e,probes:[]};let t=[],n=e,r=Number.POSITIVE_INFINITY;for(let s=0;s<500;s++){let a=he(n),u=-1;for(let h=a.length-1;h>=0;h--){let l=a[h];if(l.type==="comment"&&l.start<r&&(te.test(l.text)||ne.test(l.text))){u=h;break}}if(u===-1)break;let x=a[u],k=_e(n,a,u,i);if(!k){r=x.start;continue}n=n.slice(0,k.start)+k.text+n.slice(x.end),r=k.start+k.text.length,t.unshift({line:k.line,kind:k.kind})}return{code:n,probes:t}}function Oe(e,i){let t=0;for(let n=0;n<i;n++)if(e.charCodeAt(n)===10)t++;return t}function _e(e,i,t,n){let r=i[t],s=te.exec(r.text),a=s?null:ne.exec(r.text),u=Oe(e,r.start),x=a?a[1].trim():"",k=Boolean(s?.[1])||x===".",h=x&&!k?`, __v => __v${x.startsWith(".")||x.startsWith("[")||x.startsWith("?.")?"":"."}${x}`:"",l=I(i,t-1);if(l===-1)return;if(a&&!k&&l===t-1){let T=Ee(i,t);if(T===-1)return;let d=e.slice(i[T].start,i[l].end);return{start:i[T].start,text:`${n.probe}(${u}, (${d})${h})`,line:u,kind:"value"}}let S=l;if(i[S].type==="punct"&&i[S].text===";")S=I(i,S-1);if(S===-1)return;let o=i[l].end,g=e.slice(o,r.start).includes(`
|
|
7
|
+
`)?e.slice(o,r.start):"",m=Se(i,S);if(m===-1)return k?{start:o,text:`${g};${n.time}(${u})`,line:u,kind:"time"}:void 0;let y=i[m],w=e.slice(y.start,i[S].end);if(k)return!(y.type==="word"&&(y.text==="const"||y.text==="let"||y.text==="var"||y.text==="return"||H.has(y.text)))?{start:y.start,text:`${n.time}(${u}, (${w}))`,line:u,kind:"time"}:{start:o,text:`${g};${n.time}(${u})`,line:u,kind:"time"};if(y.type==="word"&&(y.text==="const"||y.text==="let"||y.text==="var")){let T=$e(i,m,S);return T?{start:o,text:`;${n.probe}(${u}, ${T}${h})`,line:u,kind:"value"}:void 0}if(y.type==="word"&&y.text==="return"){let T=e.slice(y.end,i[S].end).trim();return T?{start:y.start,text:`return ${n.probe}(${u}, (${T})${h})`,line:u,kind:"value"}:void 0}if(y.type==="word"&&H.has(y.text))return;return{start:y.start,text:`${n.probe}(${u}, (${w})${h})`,line:u,kind:"value"}}var Me={maxDepth:8,maxItems:200,maxString:20000,maxNodes:50000},Ae=/^\s*at\s+(?:(.*?)\s+\()?(.+?):(\d+):(\d+)\)?\s*$/;function Pe(e){if(!e)return[];let i=[];for(let t of e.split(`
|
|
8
|
+
`)){let n=Ae.exec(t);if(!n)continue;i.push({function:n[1]||void 0,file:n[2],line:Number(n[3]),column:Number(n[4])})}return i}function Ie(e){if(Function.prototype.toString.call(e).startsWith("class"))return"class";let t=e.constructor?.name;if(t==="AsyncFunction"||t==="AsyncGeneratorFunction")return"async";if(t==="GeneratorFunction")return"generator";return"function"}function C(e){let i=Object.getPrototypeOf(e);if(i===null)return"null prototype";let t=i.constructor?.name;return typeof t==="string"&&t&&t!=="Object"?t:void 0}var ie=2000000;class R{kind;html;title;text;constructor(e,i,t,n){this.kind=e;this.html=i;this.title=t;this.text=n}}var ve=["hasOne","hasMany","belongsTo","belongsToMany","hasOneThrough","hasManyThrough","morphOne","morphMany","morphTo","morphToMany"];function L(e){let i=[];for(let t of ve){let n=e[t];if(!n)continue;if(Array.isArray(n)){for(let r of n)if(typeof r==="string")i.push({kind:t,model:r});else if(r&&typeof r==="object"&&typeof r.model==="string")i.push({kind:t,model:r.model,...typeof r.relationName==="string"?{name:r.relationName}:{}})}else if(typeof n==="object"){for(let[r,s]of Object.entries(n))if(typeof s==="string")i.push({kind:t,model:s,name:r})}else if(typeof n==="string")i.push({kind:t,model:n})}return i}function Re(e){let i=e._definition;return typeof i==="object"&&i!==null&&typeof i.name==="string"&&typeof e.toJSON==="function"&&typeof e.getAttributes==="function"}function A(e,i={}){let t={...Me,...i.limits},n=i.describers??[],r=new Map,s=0,a=1;function u(l,S){if(s++,l===void 0)return{t:"undefined"};if(l===null)return{t:"null"};switch(typeof l){case"boolean":return{t:"boolean",v:l};case"number":if(Number.isNaN(l))return{t:"number",v:"NaN"};if(l===Number.POSITIVE_INFINITY)return{t:"number",v:"Infinity"};if(l===Number.NEGATIVE_INFINITY)return{t:"number",v:"-Infinity"};if(Object.is(l,-0))return{t:"number",v:"-0"};return{t:"number",v:l};case"bigint":return{t:"bigint",v:l.toString()};case"string":return l.length>t.maxString?{t:"string",v:l.slice(0,t.maxString),truncated:l.length-t.maxString}:{t:"string",v:l};case"symbol":return{t:"symbol",v:l.description??""};case"function":return{t:"function",name:l.name||"(anonymous)",kind:Ie(l)}}let o=l,g=r.get(o);if(g!==void 0)return{t:"ref",id:g};if(o instanceof Date)return{t:"date",v:Number.isNaN(o.getTime())?null:o.toISOString()};if(o instanceof RegExp)return{t:"regexp",v:o.toString()};if(o instanceof URL)return{t:"url",v:o.href};if(o instanceof Promise)return{t:"promise"};if(ArrayBuffer.isView(o)||o instanceof ArrayBuffer){let w=o instanceof ArrayBuffer?new Uint8Array(o):new Uint8Array(o.buffer,o.byteOffset,o.byteLength);return{t:"bytes",ctor:o.constructor?.name??"ArrayBuffer",length:w.byteLength,hex:Buffer.from(w.subarray(0,64)).toString("hex")}}if(S>=t.maxDepth)return{t:"cut",reason:"depth",ctor:C(o)??(Array.isArray(o)?"Array":"Object")};if(s>=t.maxNodes)return{t:"cut",reason:"nodes",ctor:C(o)};let m=a++;r.set(o,m);let y=(w)=>u(w,S+1);if(o instanceof Error)return h(o,y);if(o instanceof R){let w=o.html.length-ie;return{t:"html",kind:o.kind,...o.title?{title:o.title}:{},html:w>0?o.html.slice(0,ie):o.html,...o.text?{text:o.text.slice(0,t.maxString)}:{},...w>0?{truncated:w}:{}}}if(Re(o))return x(o,m,y);for(let w of n){let T;try{T=w(o,y)}catch{T=void 0}if(T)return T}if(Array.isArray(o)){let w=[],T=Math.min(o.length,t.maxItems);for(let c=0;c<T&&s<t.maxNodes;c++)w.push(y(o[c]));let d=o.length-w.length,p=C(o);return{t:"array",id:m,...p&&p!=="Array"?{ctor:p}:{},length:o.length,items:w,...d>0?{more:d}:{}}}if(o instanceof Map){let w=[];for(let[d,p]of o){if(w.length>=t.maxItems||s>=t.maxNodes)break;w.push([y(d),y(p)])}let T=o.size-w.length;return{t:"map",id:m,size:o.size,entries:w,...T>0?{more:T}:{}}}if(o instanceof Set){let w=[];for(let d of o){if(w.length>=t.maxItems||s>=t.maxNodes)break;w.push(y(d))}let T=o.size-w.length;return{t:"set",id:m,size:o.size,items:w,...T>0?{more:T}:{}}}return k(o,m,y)}function x(l,S,o){let g=l._definition,m={};try{let E=l.toJSON();if(E&&typeof E==="object")m=E}catch{}let y={};try{y=l.getLoadedRelations?.()??{}}catch{y={}}let w=Object.keys(m).filter((E)=>!(E in y)),T=[];for(let E of w){if(T.length>=t.maxItems||s>=t.maxNodes)break;T.push([E,o(m[E])])}let d=Object.keys(l).filter((E)=>!(E in m)),p=[];for(let[E,N]of Object.entries(y)){if(p.length>=t.maxItems||s>=t.maxNodes)break;p.push([E,o(N)])}let c=L(g),f=typeof g.primaryKey==="string"?g.primaryKey:"id",b=w.length-T.length;return{t:"model",id:S,model:g.name,...typeof g.table==="string"?{table:g.table}:{},primaryKey:f,...m[f]!==void 0?{key:o(m[f])}:{},attributes:T,...d.length?{hidden:d}:{},...p.length?{loaded:p}:{},...c.length?{relations:c}:{},...b>0?{more:b}:{}}}function k(l,S,o){let g=C(l),m=l,y=Object.keys(l);if(y.length===0&&g&&typeof l.toJSON==="function")try{let d=l.toJSON();if(Array.isArray(d)){let p=o(d);return p.t==="array"?{...p,ctor:g}:p}if(d&&typeof d==="object")m=d,y=Object.keys(m)}catch{}let w=[];for(let d of y){if(w.length>=t.maxItems||s>=t.maxNodes)break;let p;try{p=m[d]}catch(c){p=c}w.push([d,o(p)])}let T=y.length-w.length;return{t:"object",id:S,...g?{ctor:g}:{},entries:w,...T>0?{more:T}:{}}}function h(l,S){let o={t:"error",name:l.name||"Error",message:String(l.message??"")};if(typeof l.stack==="string"){o.stack=l.stack;let m=Pe(l.stack);if(m.length)o.frames=m}if(l.cause!==void 0)o.cause=S(l.cause);let g=[];for(let m of Object.keys(l)){if(m==="name"||m==="message"||m==="stack"||m==="cause")continue;if(g.length>=t.maxItems)break;g.push([m,S(l[m])])}if(g.length)o.entries=g;return o}return u(e,0)}function z(e,i={}){if(e instanceof Error)return A(e,i);return{t:"error",name:"Thrown",message:typeof e==="string"?e:Bun.inspect(e),cause:A(e,i)}}import{parse as Ve}from"@babel/parser";var Ce={open:"(__tinkerPending(",close:") ? await __tinkerTake() : __tinkerTake())"},Et={open:"__tinkerSettled(",close:")"},$t=`declare function __tinkerSettled<T>(value: T | Promise<T>): T
|
|
9
|
+
`,je=["__tinkerProbe","__tinkerTime"],Fe=new Set(["CallExpression","OptionalCallExpression","NewExpression","TaggedTemplateExpression","ImportExpression"]),Be=new Set(["FunctionDeclaration","FunctionExpression","ArrowFunctionExpression","ObjectMethod","ClassMethod","ClassPrivateMethod","ClassDeclaration","ClassExpression"]),se=new Set(["TSAsExpression","TSSatisfiesExpression","TSNonNullExpression","TSTypeAssertion","TSInstantiationExpression"]),Le=new Set(["loc","extra","typeAnnotation","typeArguments","typeParameters","returnType","decorators","leadingComments","trailingComments","innerComments"]),ze=new Set(["then","catch","finally"]),De=new Set(["Program","BlockStatement","SwitchCase","StaticBlock"]),We=new Set(["=","||=","&&=","??="]);function D(e){return Boolean(e)&&typeof e==="object"&&typeof e.type==="string"&&typeof e.start==="number"}function qe(e){if(e.type!=="MemberExpression"&&e.type!=="OptionalMemberExpression"||e.computed)return;let i=e.property;return i.type==="Identifier"?i.name:void 0}function Ue(e){if(e.type!=="CallExpression")return!1;let i=e.callee,t=i.object;return i.type==="MemberExpression"&&!i.computed&&t?.type==="Identifier"&&t.name==="Promise"}function Ke(e){let i=(t)=>Ve(t,{sourceType:"module",plugins:["typescript","decorators"],errorRecovery:!0,allowReturnOutsideFunction:!0,allowUndeclaredExports:!0}).program;if(/^\s*\{/.test(e))try{let n=i(`(${e}
|
|
10
|
+
)`).body;if(n.length===1&&n[0].type==="ExpressionStatement"&&n[0].expression.type==="ObjectExpression")return{root:n[0].expression,shift:1}}catch{}try{return{root:i(e),shift:0}}catch{return}}function oe(e,i={}){let t=i.wrap??Ce,n=new Set(i.transparent??je),r=Ke(e);if(!r)return{code:e,edits:[]};let s=new Set,a=new Set,u=(c)=>{let f=c.callee;return c.type==="CallExpression"&&f?.type==="Identifier"&&n.has(f.name)},x=(c,f,b)=>{if(u(c))return!1;if(!f)return!0;if(f.type==="AwaitExpression")return!1;if(f.type==="UnaryExpression"&&f.operator==="void")return!1;if(b==="object"&&ze.has(qe(f)??""))return!1;if((f.type==="OptionalMemberExpression"||f.type==="OptionalCallExpression")&&f.optional===!1&&(b==="object"||b==="callee"))return!1;if(f.type==="AssignmentExpression"&&b==="left"||f.type==="UpdateExpression"||(f.type==="ForInStatement"||f.type==="ForOfStatement")&&b==="left")return!1;return!0},k=(c)=>{if(!D(c))return;if(se.has(c.type)||c.type==="ParenthesizedExpression")return k(c.expression);switch(c.type){case"Identifier":if(c.name!=="undefined")s.add(c);return;case"MemberExpression":case"OptionalMemberExpression":s.add(c);return;case"AssignmentExpression":if(We.has(c.operator))k(c.right);return;case"ConditionalExpression":k(c.consequent),k(c.alternate);return;case"LogicalExpression":k(c.left),k(c.right);return;case"SequenceExpression":{let f=c.expressions;k(f[f.length-1])}}},h=(c,f,b)=>{if(Be.has(c.type))return;if(c.type.startsWith("TS")){if(se.has(c.type))h(c.expression,c,"expression");return}if(Fe.has(c.type)&&x(c,f,b))s.add(c);if(c.type==="ExpressionStatement")k(c.expression);else if(c.type==="VariableDeclarator")k(c.init);else if(c.type==="ExportDefaultDeclaration")k(c.declaration);else if(u(c))k(c.arguments[1]);let E=Ue(c);for(let N of Object.keys(c)){if(Le.has(N)||E&&N==="arguments")continue;let O=c[N];if(Array.isArray(O)){for(let M of O)if(D(M))h(M,c,N)}else if(D(O))h(O,c,N)}if(c.type==="ExpressionStatement"&&f&&De.has(f.type))a.add(c.start)},{root:l,shift:S}=r;if(h(l,void 0,""),!s.size)return{code:e,edits:[]};let o=[...s],g=o.slice().sort((c,f)=>c.end-f.end||f.start-c.start),m=o.slice().sort((c,f)=>c.start-f.start||f.end-c.end),y=[],w=0,T=0;while(w<g.length||T<m.length){let c=g[w],f=m[T];if(c&&(!f||c.end<=f.start))y.push({at:c.end-S,text:t.close}),w++;else if(f){let b=a.delete(f.start)?";":"";y.push({at:f.start-S,text:b+t.open}),T++}}let d="",p=0;for(let c of y)d+=e.slice(p,c.at)+c.text,p=c.at;return d+=e.slice(p),{code:d,edits:y}}function Nt(e,i){let t=i;for(let n of e){if(n.at>i)break;t+=n.text.length}return t}function Ot(e,i){let t=0;for(let n of e){let r=n.at+t;if(i<r)break;if(i<r+n.text.length)return n.at;t+=n.text.length}return i-t}var Je=/__tinkerPending\(/g,He=/\)\s*\?\s*await __tinkerTake\(\)\s*:\s*__tinkerTake\(\)/g;function W(e){return e.includes("__tinkerPending(")?e.replace(Je,"").replace(He,""):e}function Ye(){let e=[],i=new Set;for(let t of Object.getOwnPropertyNames(globalThis)){let n;try{n=globalThis[t]}catch{continue}if(!n||typeof n!=="object"&&typeof n!=="function")continue;let r=n.getDefinition;if(typeof r!=="function")continue;let s;try{s=r.call(n)}catch{continue}if(!s||typeof s.name!=="string"||i.has(s.name))continue;i.add(s.name),e.push({name:s.name,...typeof s.table==="string"?{table:s.table}:{},...typeof s.primaryKey==="string"?{primaryKey:s.primaryKey}:{},attributes:s.attributes&&typeof s.attributes==="object"?Object.keys(s.attributes):[],relations:L(s)})}return e.sort((t,n)=>t.name.localeCompare(n.name))}function Qe(){return{html(e,i){return new R("html",String(e),i)},async mail(e,i={}){let{templateByName:n}=await import("@stacksjs/email"),r=await n(e,{variables:i});if(!r?.html)throw Error(`No mail template named "${e}" rendered. Templates live in resources/emails.`);return new R("mail",r.html,e,r.text)}}}class j extends Error{constructor(){super("dd");this.name="TinkerHalt"}}var Xe=/\bimport\((["'])(\.{1,2}\/[^"']*)\1\)/g;function q(e,i,t="tinker",n={}){let r=new Bun.Transpiler({loader:"ts",replMode:!0}),s=(u)=>r.transformSync(u).replace(Xe,(x,k,h)=>`import(${k}${ae(i,h)}${k})`),a;if(n.settle){let u=oe(e);if(u.edits.length)try{a=s(u.code)}catch{}}return`${a??s(e)}
|
|
11
|
+
//# sourceURL=${t}.ts`}function Ze(e){if(!(e instanceof Error)||!e.message.includes("__tinkerPending("))return;try{if(e.message=W(e.message),typeof e.stack==="string")e.stack=W(e.stack)}catch{}}function et(e){let i=e instanceof AggregateError?e.errors:[e],t=[];for(let n of i){if(!n||typeof n!=="object")continue;let{message:r,position:s}=n;t.push({message:typeof r==="string"?r:String(n),...s?.line!==void 0?{line:s.line}:{},...s?.column!==void 0?{column:s.column}:{},...s?.lineText?{lineText:s.lineText}:{}})}return t}async function tt(e){let n=!0;try{let{overridesReady:r}=await import("@stacksjs/config");await r}catch(r){n=!1,e.push(`config not loaded: ${r instanceof Error?r.message:String(r)}`)}try{let{injectGlobalAutoImports:r}=await import("@stacksjs/server");await r()}catch(r){n=!1,e.push(`models and framework globals not injected: ${r instanceof Error?r.message:String(r)}`)}return n}var nt=5000,rt=["log","info","debug","dir","table"],it=["warn","error","trace"];async function F(e={}){let i=e.cwd?Ge(e.cwd)?e.cwd:ae(v.cwd(),e.cwd):v.cwd();if(i!==v.cwd())v.chdir(i);let t={limits:e.limits,describers:e.describers},n=[],r=[],s="boot",a,u=(d)=>{a?.push(d);try{e.onFrame?.(d)}catch{}},x=console,k=(d)=>d.map((p)=>typeof p==="string"?p:Bun.inspect(p,{depth:6})).join(" ");for(let[d,p]of[[rt,"stdout"],[it,"stderr"]])for(let c of d){let f=x[c];x[c]=(...b)=>u({type:p,id:s,text:`${k(b)}
|
|
12
|
+
`}),r.push(()=>{x[c]=f})}for(let[d,p]of[[v.stdout,"stdout"],[v.stderr,"stderr"]]){let c=d.write;d.write=(f,...b)=>(u({type:p,id:s,text:typeof f==="string"?f:Buffer.from(f).toString("utf8")}),b.find((N)=>typeof N==="function")?.(),!0),r.push(()=>{d.write=c})}let h=globalThis;h.dump=(...d)=>{for(let p of d)u({type:"dump",id:s,value:A(p,t)})},h.dd=(...d)=>{throw h.dump(...d),new j},h.preview=Qe();let l=performance.now();h.__tinkerProbe=(d,p,c)=>{let f=s,b=(E)=>u({type:"probe",id:f,line:d,kind:"value",value:A(E,t)});if(!c)return b(p),p;try{let E=c(p);if(E instanceof Promise)E.then(b,(N)=>b(N));else b(E)}catch(E){b(E)}return p};let S;if(h.__tinkerPending=(d)=>(S=d,d instanceof Promise),h.__tinkerTake=()=>{let d=S;return S=void 0,d},h.__tinkerTime=(d,p)=>(u({type:"probe",id:s,line:d,kind:"time",ms:Math.round((performance.now()-l)*100)/100}),p),e.globals)Object.assign(h,e.globals);let o=!1;if(typeof e.boot==="function")try{await e.boot(),o=!0}catch(d){n.push(`boot failed: ${d instanceof Error?d.message:String(d)}`)}else if(e.boot!==!1)o=await tt(n);if(o&&e.boot!==!1&&typeof e.boot!=="function"){let d=(...f)=>{for(let b of f)u({type:"dump",id:s,value:A(b,t)})};h.dump=d,h.dd=(...f)=>{throw d(...f),new j};let p="@stacksjs/logging",c="@stacksjs/query-builder";try{let{registerTransport:f}=await import(p);r.push(f({name:"tinker",log(b){u({type:"log",id:s,level:b.level,message:b.message,timestamp:b.timestamp,...b.context?{context:A(b.context,t)}:{}})}}))}catch(f){n.push(`log capture unavailable: ${f instanceof Error?f.message:String(f)}`)}try{let{registerPersistentQueryHooks:f}=await import(c);r.push(f({onQueryEnd(b){u({type:"query",id:s,sql:b.sql,...b.params?.length?{params:A(b.params,t)}:{},...b.kind?{kind:b.kind}:{},durationMs:b.durationMs,...b.rowCount!==void 0?{rowCount:b.rowCount}:{}})},onQueryError(b){u({type:"query",id:s,sql:b.sql,...b.params?.length?{params:A(b.params,t)}:{},...b.kind?{kind:b.kind}:{},durationMs:b.durationMs,error:b.error instanceof Error?b.error.message:String(b.error)})}}))}catch(f){n.push(`query capture unavailable: ${f instanceof Error?f.message:String(f)}`)}}let g=eval,m=0,y=Promise.resolve();async function w(){let d=globalThis[Symbol.for("stacks.orm.modelWorkSettled")];if(typeof d!=="function")return;let p;await Promise.race([d().catch(()=>{}),new Promise((c)=>{p=setTimeout(c,nt)})]),clearTimeout(p)}async function T(d,p,c){let f=[];a=f,s=p;let b=performance.now();l=b;let E=()=>Math.round((performance.now()-b)*100)/100,N;try{N=q(e.magic===!1?d:re(d).code,i,`tinker-${p}`,{settle:c})}catch(O){let M=E();return u({type:"error",id:p,phase:"parse",error:z(O,t),diagnostics:et(O),durationMs:M}),u({type:"done",id:p,ok:!1}),a=void 0,{id:p,ok:!1,thrown:O,frames:f,durationMs:M}}try{let M=(await g(N))?.value,J=E();return await w(),u({type:"result",id:p,value:A(M,t),durationMs:J}),u({type:"done",id:p,ok:!0}),{id:p,ok:!0,value:M,frames:f,durationMs:J}}catch(O){let M=E();if(S=void 0,O instanceof j)return u({type:"result",id:p,value:{t:"undefined"},durationMs:M}),u({type:"done",id:p,ok:!0}),{id:p,ok:!0,frames:f,durationMs:M};return Ze(O),u({type:"error",id:p,phase:"run",error:z(O,t),durationMs:M}),u({type:"done",id:p,ok:!1}),{id:p,ok:!1,thrown:O,frames:f,durationMs:M}}finally{a=void 0}}return{info:{cwd:i,booted:o,bun:Bun.version,warnings:n},models:Ye,evaluate(d,p={}){let c=p.id??String(++m),f=y.then(()=>T(d,c,p.settle??e.settle!==!1));return y=f.catch(()=>{}),f},dispose(){while(r.length)r.pop()?.()}}}var U="\x1E",ot=2;function at(e){return`${U}${JSON.stringify(e)}
|
|
13
|
+
`}function jt(){let e="",i=(t)=>{if(t.startsWith(U))try{return{frame:JSON.parse(t.slice(U.length))}}catch{return{stray:t}}return{stray:t}};return{push(t){e+=t;let n=e.split(`
|
|
14
|
+
`);return e=n.pop()??"",n.map(i)},flush(){if(!e)return[];let t=e;return e="",[{stray:t}]}}}async function ce(e={}){let i=e.write??((g)=>{st(1,g)}),t=(g)=>i(at(g)),{input:n,write:r,once:s,...a}=e,u=await F({...a,onFrame:t});t({type:"ready",session:u.info,protocol:ot});let x=!0,k=!1;if(s!==void 0)return{ok:(await u.evaluate(s)).ok,session:u};let h=new TextDecoder,l="",S=n??Bun.stdin.stream(),o=async(g)=>{if(!g.trim())return!0;let m;try{m=JSON.parse(g)}catch{return t({type:"protocol-error",message:"request is not valid JSON"}),!0}if(m.op==="exit")return!1;if(m.op==="ping")return t({type:"pong",...m.id?{id:m.id}:{}}),!0;if(m.op==="models")return t({type:"models",...m.id?{id:m.id}:{},models:u.models()}),!0;if(m.op==="eval"&&typeof m.code==="string"){if(!(await u.evaluate(m.code,{id:m.id,...typeof m.settle==="boolean"?{settle:m.settle}:{}})).ok)x=!1;return!0}return t({type:"protocol-error",message:`unknown request: ${String(m.op)}`}),!0};e:for await(let g of S){l+=typeof g==="string"?g:h.decode(g,{stream:!0});let m=l.indexOf(`
|
|
15
|
+
`);while(m!==-1){let y=l.slice(0,m);if(l=l.slice(m+1),!await o(y)){k=!0;break e}m=l.indexOf(`
|
|
16
|
+
`)}}if(!k&&l.trim())await o(l);return{ok:x,session:u}}function K(e){return e?.historyFile??mt(ft(),".stacks_tinker_history")}function me(e){let i=K(e);if(!le(i))return[];return ut(i,"utf-8").split(`
|
|
17
|
+
`).filter(Boolean)}function pt(e,i){let t=K(i),n=!le(t);if(ct(t,`${e}
|
|
18
|
+
`),n)try{ue(t,384)}catch{}let r=i?.historySize??5000,s=me(i);if(s.length>r){let a=s.slice(s.length-r);fe(t,a.join(`
|
|
19
19
|
`)+`
|
|
20
|
-
`);try{
|
|
21
|
-
`)}function _(e,i=1){
|
|
20
|
+
`);try{ue(t,384)}catch{}}}function gt(e){let i=K(e);fe(i,"")}function yt(e){let i=["",` \x1B[36mStacks Tinker\x1B[0m (Bun v${e.bun})`,e.booted?" Your app is booted: models, jobs, config and framework helpers are globals.":" Bare session: the app context is not booted."];for(let t of e.warnings)i.push(` \x1B[33m${t}\x1B[0m`);return i.push(""," \x1B[2mDeclarations persist between lines. A query needs no await: User.first() is the row."," dump(value) inspects, dd(value) inspects and stops."," .help for commands, .exit or Ctrl+D to leave.\x1B[0m","",""),i.join(`
|
|
21
|
+
`)}function _(e,i=1){lt(i,e)}function P(e,i=""){let t=`${i} `;switch(e.t){case"undefined":return"undefined";case"null":return"null";case"boolean":return String(e.v);case"number":return String(e.v);case"bigint":return`${e.v}n`;case"string":return JSON.stringify(e.v)+(e.truncated?` ... ${e.truncated} more characters`:"");case"symbol":return`Symbol(${e.v})`;case"function":return`[${e.kind==="class"?"class":"Function"}: ${e.name}]`;case"date":return e.v??"Invalid Date";case"regexp":return e.v;case"url":return e.v;case"promise":return"Promise {}";case"bytes":return`${e.ctor}(${e.length}) <${e.hex}${e.length>64?" ...":""}>`;case"ref":return`[Circular *${e.id}]`;case"cut":return`[${e.ctor??"Object"}]`;case"error":return e.stack??`${e.name}: ${e.message}`;case"array":{if(!e.items.length&&!e.more)return`${e.ctor?`${e.ctor} `:""}[]`;let n=e.items.map((r)=>`${t}${P(r,t)}`);if(e.more)n.push(`${t}... ${e.more} more`);return`${e.ctor?`${e.ctor} `:""}[
|
|
22
22
|
${n.join(`,
|
|
23
23
|
`)}
|
|
24
|
-
${i}]`}case"set":{let n=e.items.map((r)=>`${t}${
|
|
24
|
+
${i}]`}case"set":{let n=e.items.map((r)=>`${t}${P(r,t)}`);if(e.more)n.push(`${t}... ${e.more} more`);return n.length?`Set(${e.size}) {
|
|
25
25
|
${n.join(`,
|
|
26
26
|
`)}
|
|
27
|
-
${i}}`:"Set(0) {}"}case"map":{let n=e.entries.map(([r,s])=>`${t}${
|
|
27
|
+
${i}}`:"Set(0) {}"}case"map":{let n=e.entries.map(([r,s])=>`${t}${P(r,t)} => ${P(s,t)}`);if(e.more)n.push(`${t}... ${e.more} more`);return n.length?`Map(${e.size}) {
|
|
28
28
|
${n.join(`,
|
|
29
29
|
`)}
|
|
30
|
-
${i}}`:"Map(0) {}"}case"html":return`[${e.kind}${e.title?` ${e.title}`:""}: ${e.html.length} characters of markup]`;case"model":{let n=e.attributes.map(([r,s])=>`${t}${r}: ${
|
|
30
|
+
${i}}`:"Map(0) {}"}case"html":return`[${e.kind}${e.title?` ${e.title}`:""}: ${e.html.length} characters of markup]`;case"model":{let n=e.attributes.map(([r,s])=>`${t}${r}: ${P(s,t)}`);for(let r of e.hidden??[])n.push(`${t}${r}: [hidden]`);for(let[r,s]of e.loaded??[])n.push(`${t}${r}: ${P(s,t)}`);if(e.more)n.push(`${t}... ${e.more} more`);return`${e.model} {
|
|
31
31
|
${n.join(`,
|
|
32
32
|
`)}
|
|
33
|
-
${i}}`}case"object":{if(!e.entries.length&&!e.more)return`${e.ctor?`${e.ctor} `:""}{}`;let n=e.entries.map(([r,s])=>`${t}${r}: ${
|
|
33
|
+
${i}}`}case"object":{if(!e.entries.length&&!e.more)return`${e.ctor?`${e.ctor} `:""}{}`;let n=e.entries.map(([r,s])=>`${t}${r}: ${P(s,t)}`);if(e.more)n.push(`${t}... ${e.more} more`);return`${e.ctor?`${e.ctor} `:""}{
|
|
34
34
|
${n.join(`,
|
|
35
35
|
`)}
|
|
36
|
-
${i}}`}}}function
|
|
37
|
-
`);break;case"probe":_(`\x1B[2m line ${e.line+1}:\x1B[0m ${e.kind==="time"?`${e.ms} ms`:
|
|
36
|
+
${i}}`}}}function bt(e,i){switch(e.type){case"stdout":_(e.text);break;case"stderr":_(e.text,2);break;case"dump":_(`${P(e.value)}
|
|
37
|
+
`);break;case"probe":_(`\x1B[2m line ${e.line+1}:\x1B[0m ${e.kind==="time"?`${e.ms} ms`:P(e.value," ")}
|
|
38
38
|
`);break;case"query":if(i)_(`\x1B[2m ${e.durationMs??"?"}ms ${e.sql}\x1B[0m
|
|
39
39
|
`,2);break;case"log":if(i)_(`\x1B[2m [${e.level}] ${e.message}\x1B[0m
|
|
40
|
-
`,2);break}}async function
|
|
41
|
-
`,2);let s=(o,g)=>{if(!o.ok){let
|
|
42
|
-
`,2);return}if(g&&o.value!==void 0)_(`${A(
|
|
40
|
+
`,2);break}}async function kt(e,i){let t=[];for(let n of e??[]){let r=n.replace(/[@/\-.]/g,"_").replace(/^_+/,"");try{let s=n.startsWith(".")?dt(i,n):n;globalThis[r]=await import(s)}catch(s){t.push(`${n}: ${s instanceof Error?s.message:String(s)}`)}}return t}var ht=/Unexpected end of file|Unterminated|Expected "\}" but found end of file|Expected "\)" but found end of file|Expected "\]" but found end of file/;function xt(e,i){try{return q(e,i),!1}catch(t){return(t instanceof AggregateError?t.errors.map((r)=>String(r?.message??r)):[String(t?.message??t)]).some((r)=>ht.test(r))}}async function de(e={}){let i=e.cwd??B.cwd(),t=e.stacksPreload!==!1;if(e.serve){let o=e.eval??e.print,{ok:g}=await ce({cwd:i,boot:t,...o!==void 0?{once:o}:{}});return{exitCode:g?0:1}}let n=e.verbose===!0,r=await F({cwd:i,boot:t,onFrame:(o)=>bt(o,n)});for(let o of await kt(e.preload,r.info.cwd))_(`\x1B[33mpreload failed: ${o}\x1B[0m
|
|
41
|
+
`,2);let s=(o,g)=>{if(!o.ok){let m=o.thrown;_(`${m instanceof Error?Bun.inspect(m,{colors:Boolean(B.stderr.isTTY)}):`Uncaught ${Bun.inspect(m)}`}
|
|
42
|
+
`,2);return}if(g&&o.value!==void 0)_(`${P(A(o.value))}
|
|
43
43
|
`)};if(e.eval!==void 0||e.print!==void 0){for(let g of r.info.warnings)_(`\x1B[33m${g}\x1B[0m
|
|
44
|
-
`,2);let o=await r.evaluate(e.print??e.eval??"");return s(o,e.print!==void 0),r.dispose(),{exitCode:o.ok?0:1}}if(e.banner!==!1)_(
|
|
44
|
+
`,2);let o=await r.evaluate(e.print??e.eval??"");return s(o,e.print!==void 0),r.dispose(),{exitCode:o.ok?0:1}}if(e.banner!==!1)_(yt(r.info));let{createInterface:a}=await import("readline"),{Writable:u}=await import("stream"),x=new u({write(o,g,m){_(typeof o==="string"?o:Buffer.from(o).toString("utf8")),m()}}),k=e.historySize??5000,h=a({input:B.stdin,output:x,terminal:Boolean(B.stdin.isTTY),history:me(e).slice(-k).reverse(),historySize:k,prompt:"> "}),l="",S=0;h.prompt();try{for await(let o of h){let g=o.trim();if(!l&&g.startsWith(".")){if(g===".exit")break;if(g===".help")_(` .exit leave
|
|
45
45
|
.clear forget the history file
|
|
46
46
|
.help this list
|
|
47
47
|
dump(v), dd(v) inspect a value; dd also stops the line
|
|
48
|
-
`);else if(g===".clear")
|
|
49
|
-
`);
|
|
50
|
-
${o}`:o,!
|
|
48
|
+
`);else if(g===".clear")gt(e);else _(` unknown command ${g}
|
|
49
|
+
`);h.prompt();continue}if(l=l?`${l}
|
|
50
|
+
${o}`:o,!l.trim()){l="",h.prompt();continue}if(xt(l,r.info.cwd)){h.setPrompt("... "),h.prompt();continue}let m=l;l="";try{pt(m.replace(/\n/g," "),e)}catch{}let y=await r.evaluate(m);s(y,!0),S=y.ok?0:S,h.setPrompt("> "),h.prompt()}}finally{h.close(),r.dispose()}return{exitCode:S}}async function Kt(e,i={}){return de({...i,eval:e})}async function Jt(e,i={}){return de({...i,print:e})}export{U as FRAME_MARKER,ot as PROTOCOL_VERSION,Ce as SETTLE_RUNTIME_WRAP,je as SETTLE_TRANSPARENT,$t as SETTLE_TYPE_AMBIENT,Et as SETTLE_TYPE_WRAP,R as TinkerPreview,pt as appendHistory,gt as clearHistory,q as compileSnippet,jt as createFrameReader,F as createTinkerSession,Me as defaultSerializeLimits,at as encodeFrame,K as getHistoryPath,Ne as hasMagicComments,re as instrumentMagicComments,xt as isIncompleteSnippet,Re as isModelInstance,Ye as listModels,Ot as originalOffset,Pe as parseStack,me as readHistory,L as relationsOf,P as renderValue,A as serialize,z as serializeThrown,ce as serveTinker,oe as settleTopLevel,Nt as settledOffset,de as startTinker,Kt as tinkerEval,Jt as tinkerPrint,he as tokenize,W as unsettleText};
|
package/package.json
CHANGED