@zktx.io/sui-move-builder 0.1.6 → 0.1.7
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.cjs +14 -14
- package/dist/index.js +14 -14
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
"use strict";var W=Object.create;var S=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var z=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var H=(d,e)=>{for(var t in e)S(d,t,{get:e[t],enumerable:!0})},L=(d,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of E(e))!V.call(d,n)&&n!==t&&S(d,n,{get:()=>e[n],enumerable:!(i=U(e,n))||i.enumerable});return d};var q=(d,e,t)=>(t=d!=null?W(z(d)):{},L(e||!d||!d.__esModule?S(t,"default",{value:d,enumerable:!0}):t,d)),K=d=>L(S({},"__esModule",{value:!0}),d);var re={};H(re,{buildMovePackage:()=>ee,compileRaw:()=>se,fetchPackageFromGitHub:()=>B,getSuiMoveVersion:()=>te,getSuiVersion:()=>ne,getWasmBindings:()=>ie,initMoveCompiler:()=>Z,resolveDependencies:()=>j});module.exports=K(re);var I=class{async fetch(e,t,i){throw new Error("Not implemented")}async fetchFile(e,t,i){throw new Error("Not implemented")}},P=class extends I{constructor(t){super();this.rateLimitRemaining=60;this.rateLimitReset=0;this.cache=new Map,this.treeCache=new Map,this.token=t}updateRateLimit(t){let i=t.headers.get("x-ratelimit-remaining"),n=t.headers.get("x-ratelimit-reset");i&&(this.rateLimitRemaining=parseInt(i,10)),n&&(this.rateLimitReset=parseInt(n,10)*1e3)}async fetch(t,i,n,o){let{owner:s,repo:c}=this.parseGitUrl(t);if(!s||!c)throw new Error(`Invalid git URL: ${t}`);let a=`${s}/${c}@${i}`,l=`https://api.github.com/repos/${s}/${c}/git/trees/${i}?recursive=1`,r;if(this.treeCache.has(a))r=this.treeCache.get(a);else{let p=null;for(let m=1;m<=3;m++)try{if(m>1){let k=Math.pow(2,m-1)*1e3;await new Promise(v=>setTimeout(v,k))}let y={};this.token&&(y.Authorization=`Bearer ${this.token}`);let h=await fetch(l,{headers:y});if(this.updateRateLimit(h),!h.ok){if(h.status===403||h.status===429){let k=new Date(this.rateLimitReset);throw new Error(`GitHub API rate limit exceeded. Resets at ${k.toLocaleTimeString()}`)}if(h.status>=500&&h.status<600&&m<3){p=new Error(`Failed to fetch tree: ${h.statusText}`);continue}throw new Error(`Failed to fetch tree: ${h.statusText}`)}r=await h.json(),this.treeCache.set(a,r);break}catch(y){if(p=y instanceof Error?y:new Error(String(y)),m===3)return{}}if(p)return{}}let g={},f=[];for(let u of r.tree){if(u.type!=="blob")continue;let p=u.path;if(n){if(!u.path.startsWith(n))continue;p=u.path.slice(n.length),p.startsWith("/")&&(p=p.slice(1))}if(!p.endsWith(".move")&&p!=="Move.toml"&&p!=="Move.lock"&&!p.match(/^Move\.(mainnet|testnet|devnet)\.toml$/))continue;let m=`https://raw.githubusercontent.com/${s}/${c}/${i}/${u.path}`,y=this.fetchContent(m).then(h=>{h&&(g[p]=h)});f.push(y)}if(await Promise.all(f),g["Move.toml"]){let u=g["Move.toml"].trim();if(u.match(/^Move\.(mainnet|testnet|devnet)\.toml$/)&&!u.includes("[")&&!u.includes("=")){let p=u,m=n?`${n}/${p}`.replace(/\/+/g,"/"):p,y=`https://raw.githubusercontent.com/${s}/${c}/${i}/${m}`,h=await this.fetchContent(y);h&&(g["Move.toml"]=h,g[p]=h)}}return g}async fetchFile(t,i,n){let{owner:o,repo:s}=this.parseGitUrl(t);if(!o||!s)throw new Error(`Invalid git URL: ${t}`);let c=`https://raw.githubusercontent.com/${o}/${s}/${i}/${n}`;return this.fetchContent(c)}async fetchContent(t){if(this.cache.has(t))return this.cache.get(t)??null;try{let i={},n=typeof window<"u",o=t.startsWith("https://api.github.com/");this.token&&(!n||o)&&(i.Authorization=`Bearer ${this.token}`);let s=await fetch(t,{headers:i});if(!s.ok)return null;let c=await s.text();return this.cache.set(t,c),c}catch{return null}}parseGitUrl(t){try{let n=new URL(t).pathname.split("/").filter(o=>o);if(n.length>=2){let o=n[1];return o.endsWith(".git")&&(o=o.slice(0,-4)),{owner:n[0],repo:o}}}catch{}return{owner:null,repo:null}}};function M(d){let e=!1,t="";for(let i=0;i<d.length;i++){let n=d[i];if((n==='"'||n==="'")&&(!e||n===t)&&(e=!e,t=n),!e&&n==="#")return d.slice(0,i)}return d}function x(d){let e=d.trim();if(!e)return"";if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))return e.slice(1,-1);if(e==="true")return!0;if(e==="false")return!1;let t=Number(e);return Number.isNaN(t)?e:t}function N(d){let e={},t=d.trim().replace(/^\{/,"").replace(/\}$/,""),i="",n=!1,o="",s=[];for(let c=0;c<t.length;c++){let a=t[c];if((a==='"'||a==="'")&&(!n||a===o)&&(n=!n,o=a),!n&&a===","){s.push(i),i="";continue}i+=a}i.trim()&&s.push(i);for(let c of s){let a=c.indexOf("=");if(a===-1)continue;let l=c.slice(0,a).trim(),r=c.slice(a+1).trim();e[l]=x(r)}return e}function J(d){let e=[],t=d.trim().replace(/^\[/,"").replace(/\]$/,""),i="",n=!1,o="",s=0;for(let c=0;c<t.length;c++){let a=t[c];if((a==='"'||a==="'")&&(!n||a===o)&&(n=!n,o=n?a:""),!n&&(a==="{"&&s++,a==="}"&&s--,a===","&&s===0)){i.trim()&&e.push(G(i.trim())),i="";continue}i+=a}return i.trim()&&e.push(G(i.trim())),e}function G(d){return d.startsWith("{")?N(d):x(d)}function b(d){let e={},t=null,i=!1,n=d.split(/\r?\n/),o=[],s=0;for(;s<n.length;){let a=M(n[s]);if(a.match(/=\s*\[\s*$/)||a.includes("=")&&a.includes("[")&&!a.includes("]")){let l=a;for(s++;s<n.length&&!l.includes("]");)l+=" "+M(n[s]).trim(),s++;s<n.length&&l.includes("[")&&!l.includes("]")&&(l+=" "+M(n[s]).trim(),s++),o.push(l)}else o.push(a),s++}function c(a,l){let r=a;for(let g of l){if(!(g in r))return;r=r[g]}return r}for(let a of o){let l=M(a).trim();if(!l)continue;let r=l.match(/^\[\[([^\]]+)\]\]$/);if(r){t=r[1].trim(),i=!0;let y=t.split("."),h=e;for(let v=0;v<y.length-1;v++){let w=y[v];w in h||(h[w]={}),h=h[w]}let k=y[y.length-1];Array.isArray(h[k])||(h[k]=[]),h[k].push({});continue}let g=l.match(/^\[([^\]]+)\]$/);if(g){t=g[1].trim(),i=!1;continue}let f=l.indexOf("=");if(f===-1||!t)continue;let u=l.slice(0,f).trim(),p=l.slice(f+1).trim(),m;if(p.startsWith("{")?m=N(p):p.startsWith("[")?m=J(p):m=x(p),i){let y=t.split("."),h=c(e,y);if(Array.isArray(h)&&h.length>0){let k=h[h.length-1];k[u]=m}}else{let y=t.split("."),h=e;for(let v of y)v in h||(h[v]={}),h=h[v];let k=t==="package"?u.replace(/-/g,"_"):u;h[k]=m}}return e}var D=class{constructor(e){this.packageTable=new Map;this.graph=new Map;this.alwaysDeps=new Set(["Sui","MoveStdlib","SuiSystem","Bridge"]);this.lockfileOrder=[];this.root=e}setLockfileOrder(e){this.lockfileOrder=e}addPackage(e){this.packageTable.set(e.id.name,e),this.graph.has(e.id.name)||this.graph.set(e.id.name,new Set)}addDependency(e,t,i){this.graph.has(e)||this.graph.set(e,new Set),this.graph.get(e).add(t)}getPackage(e){return this.packageTable.get(e)}getAllPackages(){return Array.from(this.packageTable.values())}getImmediateDependencies(e){return this.graph.get(e)||new Set}getTransitiveDependencies(e){let t=new Set,i=new Set,n=o=>{if(i.has(o))return;i.add(o),t.add(o);let s=this.graph.get(o);if(s)for(let c of s)n(c)};return n(e),t.delete(e),t}topologicalOrder(){if(!this.lockfileOrder.length)return this.topologicalOrderDFS().filter(s=>s!==this.root);let e=new Set,t=new Set,i=o=>{if(t.has(o))return;t.add(o),e.add(o);let s=this.graph.get(o);if(s)for(let c of s)i(c)};i(this.root);let n=[];for(let o of this.lockfileOrder)o!==this.root&&e.has(o)&&n.push(o);return n}topologicalOrderDFS(){let e=new Set,t=[],i=n=>{if(e.has(n))return;e.add(n);let o=this.graph.get(n);if(o)for(let s of Array.from(o))i(s);t.push(n)};i(this.root);for(let n of this.packageTable.keys())i(n);return t}detectCycle(){let e=new Set,t=new Set,i=new Map,n=o=>{e.add(o),t.add(o);let s=this.graph.get(o);if(s)for(let c of s)if(e.has(c)){if(t.has(c)){let a=[c],l=o;for(;l!==c;)a.unshift(l),l=i.get(l);return a.unshift(c),a}}else{i.set(c,o);let a=n(c);if(a)return a}return t.delete(o),null};for(let o of this.packageTable.keys())if(!e.has(o)){let s=n(o);if(s)return s}return null}getRootPackage(){return this.packageTable.get(this.root)}getRootName(){return this.root}isAlwaysDep(e){return this.alwaysDeps.has(e)}};var $=class{constructor(e,t={}){this.unifiedAddressTable=new Map;this.packageResolvedTables=new Map;this.graph=e,this.buildConfig=t}async resolve(){let t=[this.graph.getRootName(),...this.graph.topologicalOrder()];for(let i of t){let n=this.graph.getPackage(i);if(n)for(let[o,s]of Object.entries(n.manifest.addresses)){let c=this.normalizeAddress(s);this.unifiedAddressTable.has(o)&&this.unifiedAddressTable.get(o)!==c||this.unifiedAddressTable.set(o,c)}}for(let i of this.graph.getAllPackages()){let n={};for(let[o,s]of this.unifiedAddressTable.entries())n[o]=s;this.packageResolvedTables.set(i.id.name,n),i.resolvedTable=n}}normalizeAddress(e){if(!e)return e;let t=e.trim();return t.startsWith("0x")&&(t=t.slice(2)),/^[0-9a-fA-F]+$/.test(t)?"0x"+t.padStart(64,"0"):e}getUnifiedAddressTable(){let e={};for(let[t,i]of this.unifiedAddressTable.entries())e[t]=i;return e}getPackageResolvedTable(e){return this.packageResolvedTables.get(e)}getGraph(){return this.graph}topologicalOrder(){return this.graph.topologicalOrder()}getRootName(){return this.graph.getRootName()}getPackage(e){return this.graph.getPackage(e)}getImmediateDependencies(e){return this.graph.getImmediateDependencies(e)}};var A=class{constructor(e){this.dependencies=[];this.resolvedGraph=e,this.rootPackageName=e.getRootName()}async compute(e){if(!this.resolvedGraph.getPackage(this.rootPackageName))throw new Error(`Root package '${this.rootPackageName}' not found`);let i=this.resolvedGraph.getImmediateDependencies(this.rootPackageName),n=this.resolvedGraph.topologicalOrder(),o=new Set(["Bridge","SuiSystem"]);for(let s of n){if(s===this.rootPackageName)continue;let c=this.resolvedGraph.getPackage(s);if(!c||o.has(s))continue;let a=e.get(s)||{},l=this.extractSourcePaths(s,a),r=c.manifest.edition||"legacy",g=c.manifest.latestPublishedId||c.manifest.originalId||c.manifest.publishedAt||c.resolvedTable?.[s],f={name:s,isImmediate:i.has(s),sourcePaths:l,addressMapping:c.resolvedTable||{},compilerConfig:{edition:r,flavor:"sui"},moduleFormat:l.length>0?"Source":"Bytecode",edition:r,publishedIdForOutput:g};this.dependencies.push(f)}}extractSourcePaths(e,t){let i=Object.keys(t).filter(s=>s.endsWith("Move.toml")||s.endsWith("Move.lock")?!1:s.endsWith(".move")),n=new TextEncoder,o=(s,c)=>{let a=n.encode(`/vfs/deps/${e}/${s}`),l=n.encode(`/vfs/deps/${e}/${c}`),r=Math.min(a.length,l.length);for(let g=0;g<r;g++)if(a[g]!==l[g])return a[g]-l[g];return a.length-l.length};return i.sort(o),i}toPackageGroupedFormat(e){let t=[];for(let i of this.dependencies){let n=e.get(i.name)||{},o={};for(let[s,c]of Object.entries(n)){if(s.endsWith("Move.lock"))continue;let a=`dependencies/${i.name}/${s}`;s.endsWith("Move.toml")?o[a]=this.reconstructDependencyMoveToml(i.name,c,i.edition,i.addressMapping):o[a]=c}t.push({name:i.name,files:o,edition:i.edition,addressMapping:i.addressMapping,publishedIdForOutput:i.publishedIdForOutput})}return t}reconstructDependencyMoveToml(e,t,i,n){let o=(t||"").split(`
|
|
2
|
-
`),
|
|
3
|
-
`,g=!1,f=!1;for(let u of
|
|
1
|
+
"use strict";var W=Object.create;var S=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var z=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var H=(d,e)=>{for(var t in e)S(d,t,{get:e[t],enumerable:!0})},L=(d,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of E(e))!V.call(d,n)&&n!==t&&S(d,n,{get:()=>e[n],enumerable:!(s=U(e,n))||s.enumerable});return d};var q=(d,e,t)=>(t=d!=null?W(z(d)):{},L(e||!d||!d.__esModule?S(t,"default",{value:d,enumerable:!0}):t,d)),K=d=>L(S({},"__esModule",{value:!0}),d);var re={};H(re,{buildMovePackage:()=>ee,compileRaw:()=>se,fetchPackageFromGitHub:()=>B,getSuiMoveVersion:()=>te,getSuiVersion:()=>ne,getWasmBindings:()=>ie,initMoveCompiler:()=>Z,resolveDependencies:()=>j});module.exports=K(re);var I=class{async fetch(e,t,s){throw new Error("Not implemented")}async fetchFile(e,t,s){throw new Error("Not implemented")}},P=class extends I{constructor(t){super();this.rateLimitRemaining=60;this.rateLimitReset=0;this.cache=new Map,this.treeCache=new Map,this.token=t}updateRateLimit(t){let s=t.headers.get("x-ratelimit-remaining"),n=t.headers.get("x-ratelimit-reset");s&&(this.rateLimitRemaining=parseInt(s,10)),n&&(this.rateLimitReset=parseInt(n,10)*1e3)}async fetch(t,s,n,o){let{owner:i,repo:c}=this.parseGitUrl(t);if(!i||!c)throw new Error(`Invalid git URL: ${t}`);let a=`${i}/${c}@${s}`,l=`https://api.github.com/repos/${i}/${c}/git/trees/${s}?recursive=1`,r;if(this.treeCache.has(a))r=this.treeCache.get(a);else{let p=null;for(let m=1;m<=3;m++)try{if(m>1){let k=Math.pow(2,m-1)*1e3;await new Promise(v=>setTimeout(v,k))}let y={};this.token&&(y.Authorization=`Bearer ${this.token}`);let h=await fetch(l,{headers:y});if(this.updateRateLimit(h),!h.ok){if(h.status===403||h.status===429){let k=new Date(this.rateLimitReset);throw new Error(`GitHub API rate limit exceeded. Resets at ${k.toLocaleTimeString()}`)}if(h.status>=500&&h.status<600&&m<3){p=new Error(`Failed to fetch tree: ${h.statusText}`);continue}throw new Error(`Failed to fetch tree: ${h.statusText}`)}r=await h.json(),this.treeCache.set(a,r);break}catch(y){if(p=y instanceof Error?y:new Error(String(y)),m===3)return{}}if(p)return{}}let g={},f=[];for(let u of r.tree){if(u.type!=="blob")continue;let p=u.path;if(n){if(!u.path.startsWith(n))continue;p=u.path.slice(n.length),p.startsWith("/")&&(p=p.slice(1))}if(!p.endsWith(".move")&&p!=="Move.toml"&&p!=="Move.lock"&&!p.match(/^Move\.(mainnet|testnet|devnet)\.toml$/))continue;let m=`https://raw.githubusercontent.com/${i}/${c}/${s}/${u.path}`,y=this.fetchContent(m).then(h=>{h&&(g[p]=h)});f.push(y)}if(await Promise.all(f),g["Move.toml"]){let u=g["Move.toml"].trim();if(u.match(/^Move\.(mainnet|testnet|devnet)\.toml$/)&&!u.includes("[")&&!u.includes("=")){let p=u,m=n?`${n}/${p}`.replace(/\/+/g,"/"):p,y=`https://raw.githubusercontent.com/${i}/${c}/${s}/${m}`,h=await this.fetchContent(y);h&&(g["Move.toml"]=h,g[p]=h)}}return g}async fetchFile(t,s,n){let{owner:o,repo:i}=this.parseGitUrl(t);if(!o||!i)throw new Error(`Invalid git URL: ${t}`);let c=`https://raw.githubusercontent.com/${o}/${i}/${s}/${n}`;return this.fetchContent(c)}async fetchContent(t){if(this.cache.has(t))return this.cache.get(t)??null;try{let s={},n=typeof window<"u",o=t.startsWith("https://api.github.com/");this.token&&(!n||o)&&(s.Authorization=`Bearer ${this.token}`);let i=await fetch(t,{headers:s});if(!i.ok)return null;let c=await i.text();return this.cache.set(t,c),c}catch{return null}}parseGitUrl(t){try{let n=new URL(t).pathname.split("/").filter(o=>o);if(n.length>=2){let o=n[1];return o.endsWith(".git")&&(o=o.slice(0,-4)),{owner:n[0],repo:o}}}catch{}return{owner:null,repo:null}}};function D(d){let e=!1,t="";for(let s=0;s<d.length;s++){let n=d[s];if((n==='"'||n==="'")&&(!e||n===t)&&(e=!e,t=n),!e&&n==="#")return d.slice(0,s)}return d}function x(d){let e=d.trim();if(!e)return"";if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))return e.slice(1,-1);if(e==="true")return!0;if(e==="false")return!1;let t=Number(e);return Number.isNaN(t)?e:t}function N(d){let e={},t=d.trim().replace(/^\{/,"").replace(/\}$/,""),s="",n=!1,o="",i=[];for(let c=0;c<t.length;c++){let a=t[c];if((a==='"'||a==="'")&&(!n||a===o)&&(n=!n,o=a),!n&&a===","){i.push(s),s="";continue}s+=a}s.trim()&&i.push(s);for(let c of i){let a=c.indexOf("=");if(a===-1)continue;let l=c.slice(0,a).trim(),r=c.slice(a+1).trim();e[l]=x(r)}return e}function J(d){let e=[],t=d.trim().replace(/^\[/,"").replace(/\]$/,""),s="",n=!1,o="",i=0;for(let c=0;c<t.length;c++){let a=t[c];if((a==='"'||a==="'")&&(!n||a===o)&&(n=!n,o=n?a:""),!n&&(a==="{"&&i++,a==="}"&&i--,a===","&&i===0)){s.trim()&&e.push(G(s.trim())),s="";continue}s+=a}return s.trim()&&e.push(G(s.trim())),e}function G(d){return d.startsWith("{")?N(d):x(d)}function w(d){let e={},t=null,s=!1,n=d.split(/\r?\n/),o=[],i=0;for(;i<n.length;){let a=D(n[i]);if(a.match(/=\s*\[\s*$/)||a.includes("=")&&a.includes("[")&&!a.includes("]")){let l=a;for(i++;i<n.length&&!l.includes("]");)l+=" "+D(n[i]).trim(),i++;i<n.length&&l.includes("[")&&!l.includes("]")&&(l+=" "+D(n[i]).trim(),i++),o.push(l)}else o.push(a),i++}function c(a,l){let r=a;for(let g of l){if(!(g in r))return;r=r[g]}return r}for(let a of o){let l=D(a).trim();if(!l)continue;let r=l.match(/^\[\[([^\]]+)\]\]$/);if(r){t=r[1].trim(),s=!0;let y=t.split("."),h=e;for(let v=0;v<y.length-1;v++){let b=y[v];b in h||(h[b]={}),h=h[b]}let k=y[y.length-1];Array.isArray(h[k])||(h[k]=[]),h[k].push({});continue}let g=l.match(/^\[([^\]]+)\]$/);if(g){t=g[1].trim(),s=!1;continue}let f=l.indexOf("=");if(f===-1||!t)continue;let u=l.slice(0,f).trim(),p=l.slice(f+1).trim(),m;if(p.startsWith("{")?m=N(p):p.startsWith("[")?m=J(p):m=x(p),s){let y=t.split("."),h=c(e,y);if(Array.isArray(h)&&h.length>0){let k=h[h.length-1];k[u]=m}}else{let y=t.split("."),h=e;for(let v of y)v in h||(h[v]={}),h=h[v];let k=t==="package"?u.replace(/-/g,"_"):u;h[k]=m}}return e}var M=class{constructor(e){this.packageTable=new Map;this.graph=new Map;this.alwaysDeps=new Set(["Sui","MoveStdlib","SuiSystem","Bridge"]);this.lockfileOrder=[];this.root=e}setLockfileOrder(e){this.lockfileOrder=e}addPackage(e){this.packageTable.set(e.id.name,e),this.graph.has(e.id.name)||this.graph.set(e.id.name,new Set)}addDependency(e,t,s){this.graph.has(e)||this.graph.set(e,new Set),this.graph.get(e).add(t)}getPackage(e){return this.packageTable.get(e)}getAllPackages(){return Array.from(this.packageTable.values())}getImmediateDependencies(e){return this.graph.get(e)||new Set}getTransitiveDependencies(e){let t=new Set,s=new Set,n=o=>{if(s.has(o))return;s.add(o),t.add(o);let i=this.graph.get(o);if(i)for(let c of i)n(c)};return n(e),t.delete(e),t}topologicalOrder(){if(!this.lockfileOrder.length)return this.topologicalOrderDFS().filter(i=>i!==this.root);let e=new Set,t=new Set,s=o=>{if(t.has(o))return;t.add(o),e.add(o);let i=this.graph.get(o);if(i)for(let c of i)s(c)};s(this.root);let n=[];for(let o of this.lockfileOrder)o!==this.root&&e.has(o)&&n.push(o);return n}topologicalOrderDFS(){let e=new Set,t=[],s=n=>{if(e.has(n))return;e.add(n);let o=this.graph.get(n);if(o)for(let i of Array.from(o))s(i);t.push(n)};s(this.root);for(let n of this.packageTable.keys())s(n);return t}detectCycle(){let e=new Set,t=new Set,s=new Map,n=o=>{e.add(o),t.add(o);let i=this.graph.get(o);if(i)for(let c of i)if(e.has(c)){if(t.has(c)){let a=[c],l=o;for(;l!==c;)a.unshift(l),l=s.get(l);return a.unshift(c),a}}else{s.set(c,o);let a=n(c);if(a)return a}return t.delete(o),null};for(let o of this.packageTable.keys())if(!e.has(o)){let i=n(o);if(i)return i}return null}getRootPackage(){return this.packageTable.get(this.root)}getRootName(){return this.root}isAlwaysDep(e){return this.alwaysDeps.has(e)}};var $=class{constructor(e,t={}){this.unifiedAddressTable=new Map;this.packageResolvedTables=new Map;this.graph=e,this.buildConfig=t}async resolve(){let t=[this.graph.getRootName(),...this.graph.topologicalOrder()];for(let s of t){let n=this.graph.getPackage(s);if(n)for(let[o,i]of Object.entries(n.manifest.addresses)){let c=this.normalizeAddress(i);this.unifiedAddressTable.has(o)&&this.unifiedAddressTable.get(o)!==c||this.unifiedAddressTable.set(o,c)}}for(let s of this.graph.getAllPackages()){let n={};for(let[o,i]of this.unifiedAddressTable.entries())n[o]=i;this.packageResolvedTables.set(s.id.name,n),s.resolvedTable=n}}normalizeAddress(e){if(!e)return e;let t=e.trim();return t.startsWith("0x")&&(t=t.slice(2)),/^[0-9a-fA-F]+$/.test(t)?"0x"+t.padStart(64,"0"):e}getUnifiedAddressTable(){let e={};for(let[t,s]of this.unifiedAddressTable.entries())e[t]=s;return e}getPackageResolvedTable(e){return this.packageResolvedTables.get(e)}getGraph(){return this.graph}topologicalOrder(){return this.graph.topologicalOrder()}getRootName(){return this.graph.getRootName()}getPackage(e){return this.graph.getPackage(e)}getImmediateDependencies(e){return this.graph.getImmediateDependencies(e)}};var A=class{constructor(e){this.dependencies=[];this.resolvedGraph=e,this.rootPackageName=e.getRootName()}async compute(e){if(!this.resolvedGraph.getPackage(this.rootPackageName))throw new Error(`Root package '${this.rootPackageName}' not found`);let s=this.resolvedGraph.getImmediateDependencies(this.rootPackageName),n=this.resolvedGraph.topologicalOrder(),o=new Set(["Bridge","SuiSystem"]);for(let i of n){if(i===this.rootPackageName)continue;let c=this.resolvedGraph.getPackage(i);if(!c||o.has(i))continue;let a=e.get(i)||{},l=this.extractSourcePaths(i,a),r=c.manifest.edition||"legacy",g=c.manifest.latestPublishedId||c.manifest.originalId||c.manifest.publishedAt||c.resolvedTable?.[i],f={name:i,isImmediate:s.has(i),sourcePaths:l,addressMapping:c.resolvedTable||{},compilerConfig:{edition:r,flavor:"sui"},moduleFormat:l.length>0?"Source":"Bytecode",edition:r,publishedIdForOutput:g};this.dependencies.push(f)}}extractSourcePaths(e,t){let s=Object.keys(t).filter(i=>i.endsWith("Move.toml")||i.endsWith("Move.lock")?!1:i.endsWith(".move")),n=new TextEncoder,o=(i,c)=>{let a=n.encode(`/vfs/deps/${e}/${i}`),l=n.encode(`/vfs/deps/${e}/${c}`),r=Math.min(a.length,l.length);for(let g=0;g<r;g++)if(a[g]!==l[g])return a[g]-l[g];return a.length-l.length};return s.sort(o),s}toPackageGroupedFormat(e){let t=[];for(let s of this.dependencies){let n=e.get(s.name)||{},o={};for(let[i,c]of Object.entries(n)){if(i.endsWith("Move.lock"))continue;let a=`dependencies/${s.name}/${i}`;i.endsWith("Move.toml")?o[a]=this.reconstructDependencyMoveToml(s.name,c,s.edition,s.addressMapping):o[a]=c}t.push({name:s.name,files:o,edition:s.edition,addressMapping:s.addressMapping,publishedIdForOutput:s.publishedIdForOutput})}return t}reconstructDependencyMoveToml(e,t,s,n){let o=(t||"").split(`
|
|
2
|
+
`),i=[],c=[],a=!1,l=!1;for(let u of o){let p=u.trim();if(p.startsWith("[package]")){a=!0,l=!1;continue}if(p.startsWith("[dependencies]")){a=!1,l=!0;continue}if(p.startsWith("[")){a=!1,l=!1;continue}a&&p&&i.push(u),l&&p&&c.push(u)}let r=`[package]
|
|
3
|
+
`,g=!1,f=!1;for(let u of i)if(u.includes("name ="))r+=u+`
|
|
4
4
|
`,g=!0;else if(u.includes("version ="))r+=u+`
|
|
5
5
|
`,f=!0;else{if(u.includes("edition ="))continue;r+=u+`
|
|
6
6
|
`}g||(r+=`name = "${e}"
|
|
7
7
|
`),f||(r+=`version = "0.0.0"
|
|
8
|
-
`),r+=`edition = "${
|
|
8
|
+
`),r+=`edition = "${s}"
|
|
9
9
|
`,r+=`
|
|
10
10
|
[dependencies]
|
|
11
11
|
`;for(let u of c)r+=u+`
|
|
12
12
|
`;r+=`
|
|
13
13
|
[addresses]
|
|
14
14
|
`;for(let[u,p]of Object.entries(n))r+=`${u} = "${p}"
|
|
15
|
-
`;return r}};var F=class{constructor(e,t="mainnet",
|
|
16
|
-
`))}this.packageNameCache.set(r.manifest.name,n.source)}r.manifest.publishedAt&&(r.manifest.addresses[r.manifest.name]=this.normalizeAddress(r.manifest.publishedAt)),r.manifest.edition||(r.manifest.edition="legacy"),e.addPackage(r),e.addDependency(t.id.name,r.id.name,n),this.packageFiles.set(r.id.name,c),await this.buildDependencyGraph(e,r)}}getChainIdForNetwork(e){return{mainnet:"35834a8a",testnet:"4c78adac",devnet:"2",localnet:"localnet"}[e]||e}resolvePublishedAt(e,t,
|
|
15
|
+
`;return r}};var F=class{constructor(e,t="mainnet",s=null){this.visited=new Set;this.packageNameCache=new Map;this.packageFiles=new Map;this.fetcher=e,this.network=t,this.rootSource=s}async resolve(e,t){let s=w(e),n=s.package?.name||"RootPackage",o=s.package?.edition,i=new M(n),c=await this.buildPackage(n,this.rootSource,e,t);o&&(c.manifest.edition=o);let a=c.manifest.addresses[n];this.normalizeAddress(a||"")==="0x0000000000000000000000000000000000000000000000000000000000000000"&&c.manifest.originalId&&(c.manifest.addresses[n]=this.normalizeAddress(c.manifest.originalId)),i.addPackage(c),this.packageFiles.set(n,t);let r=await this.loadFromLockfile(i,c,t),g=Array.from(c.dependencies.keys()).filter(k=>!i.getPackage(k));(!r||g.length>0)&&await this.buildDependencyGraph(i,c);let f=i.detectCycle();if(f)throw new Error(`Dependency cycle detected: ${f.join(" \u2192 ")}`);let u=new $(i,{});await u.resolve();let p=new A(u);await p.compute(this.packageFiles);let m=this.reconstructMoveToml(s,u.getUnifiedAddressTable(),!0,o),y={...t};delete y["Move.lock"],y["Move.toml"]=m;let h=p.toPackageGroupedFormat(this.packageFiles);return{files:JSON.stringify(y),dependencies:JSON.stringify(h)}}async buildPackage(e,t,s,n){let o=w(s),i=n["Move.lock"],c=this.getChainIdForNetwork(this.network),a=this.resolvePublishedAt(s,i,c),l=a.latestId?this.normalizeAddress(a.latestId):void 0;a.error;let r={name:o.package?.name||e,version:o.package?.version||"0.0.0",edition:o.package?.edition,publishedAt:a.publishedAt,originalId:a.originalId,latestPublishedId:l,addresses:o.addresses||{},dependencies:o.dependencies||{},devDependencies:o["dev-dependencies"]},g=r.publishedAt&&r.publishedAt!=="0x0"?this.normalizeAddress(r.publishedAt):void 0,f=r.addresses[r.name],u=f?this.normalizeAddress(f):void 0;g?r.addresses[r.name]=g:u?r.addresses[r.name]=u:r.addresses[r.name]="0x0";let p=new Map;if(r.dependencies)for(let[y,h]of Object.entries(r.dependencies)){let k=this.parseDependencyInfo(h);k&&p.set(y,k)}return{id:{name:r.name,version:r.version,source:t||{type:"local"}},manifest:r,dependencies:p,devDependencies:new Map}}parseDependencyInfo(e){if(!e)return null;let t={source:{type:"local"}};if(e.git&&e.rev)t.source={type:"git",git:e.git,rev:e.rev,subdir:e.subdir};else if(e.local)t.source={type:"local",local:e.local};else return null;if(e["addr-subst"]||e.addr_subst){let s=e["addr-subst"]||e.addr_subst,n={};for(let[o,i]of Object.entries(s))typeof i=="string"&&(i.startsWith("0x")||/^[0-9a-fA-F]+$/.test(i)?n[o]={type:"assign",address:i}:n[o]={type:"renameFrom",name:i});Object.keys(n).length>0&&(t.subst=n)}return t}async buildDependencyGraph(e,t){for(let[s,n]of t.dependencies.entries()){if(n.source.type==="local")if(t.id.source.type==="git"&&n.source.local){let g=t.id.source.subdir||"",f=n.source.local,u=this.resolveRelativePath(g,f);n.source={type:"git",git:t.id.source.git,rev:t.id.source.rev,subdir:u}}else continue;if(n.source.type!=="git")continue;let o=`${n.source.git}|${n.source.rev}|${n.source.subdir||""}`;if(this.visited.has(o)){let g=this.findPackageBySource(e,n.source);g&&e.addDependency(t.id.name,g.id.name,n);continue}this.visited.add(o);let i=n.source.subdir;!i&&n.source.git&&this.isSuiRepo(n.source.git)&&(i=this.inferSuiFrameworkSubdir(s),i&&(n.source.subdir=i));let c=await this.fetcher.fetch(n.source.git,n.source.rev,i,`${t.id.name} -> ${s}`),a=null,l=`Move.${this.network}.toml`;for(let[g,f]of Object.entries(c))if(g.endsWith(l)){a=f;break}if(!a){for(let[g,f]of Object.entries(c))if(g.endsWith("Move.toml")){a=f;break}}if(!a)continue;let r=await this.buildPackage(s,n.source,a,c);if(!this.lockfileVersion||this.lockfileVersion<4){let g=this.packageNameCache.get(r.manifest.name);if(g){let f=u=>JSON.stringify(u);throw new Error([`Conflicting versions of package '${r.manifest.name}' found`,`Existing: ${f(g)}`,`New: ${f(n.source)}`,`When resolving dependencies for '${t.id.name}' -> '${s}'`].join(`
|
|
16
|
+
`))}this.packageNameCache.set(r.manifest.name,n.source)}r.manifest.publishedAt&&(r.manifest.addresses[r.manifest.name]=this.normalizeAddress(r.manifest.publishedAt)),r.manifest.edition||(r.manifest.edition="legacy"),e.addPackage(r),e.addDependency(t.id.name,r.id.name,n),this.packageFiles.set(r.id.name,c),await this.buildDependencyGraph(e,r)}}getChainIdForNetwork(e){return{mainnet:"35834a8a",testnet:"4c78adac",devnet:"2",localnet:"localnet"}[e]||e}resolvePublishedAt(e,t,s){let n=w(e),o=n.package?.published_at||n.package?.["published-at"],i=o&&o!=="0x0"?o:void 0,c=i?this.normalizeAddress(i):void 0,a=n.package?.["original-id"];if(!t||!s)return{publishedAt:c,originalId:a};let l=w(t),r,g,f;if(l.env)for(let[,u]of Object.entries(l.env)){let p=u["latest-published-id"],m=u["original-published-id"],y=u["chain-id"];if(y===s||!y){let h=p&&p!=="0x0"?this.normalizeAddress(p):void 0,k=m&&m!=="0x0"?this.normalizeAddress(m):void 0;g=h,f=k,r=h||k;break}}return!f&&r&&(f=r),r&&c&&r!==c?{error:`Conflicting 'published-at' addresses between Move.toml (${c}) and Move.lock (${r})`}:{publishedAt:f||c||r,originalId:f||a,latestId:g||r||c}}findPackageBySource(e,t){for(let s of e.getAllPackages()){let n=s.id.source;if(n.type===t.type&&n.git===t.git&&n.rev===t.rev&&n.subdir===t.subdir)return s}}resolveRelativePath(e,t){let s=e?e.split("/").filter(Boolean):[],n=t.split("/").filter(Boolean),o=[...s];for(let i of n)i===".."?o.length>0&&o.pop():i!=="."&&o.push(i);return o.join("/")}async computeManifestDigest(e){let s=new TextEncoder().encode(e),n=await crypto.subtle.digest("SHA-256",s);return Array.from(new Uint8Array(n)).map(c=>c.toString(16).padStart(2,"0")).join("").toUpperCase()}async loadFromLockfile(e,t,s){let n=s["Move.lock"];if(!n)return!1;let o=w(n);this.lockfileVersion=o.move?.version;let i=o.move?.version;return i===3?await this.loadFromLockfileV3(e,o,t):i&&i>=4?await this.loadFromLockfileV4(e,o,s):await this.loadFromLockfileV0(e,o,t)}async loadFromLockfileV0(e,t,s){let n=t.move?.package;if(!n||!Array.isArray(n))return!1;let o=Array.isArray(t.move?.dependencies)?t.move.dependencies.map(r=>r.name||r.id||r).filter(Boolean):[],i=n.map(r=>r.name||r.id).filter(Boolean),c=[...o,...i.filter(r=>!o.includes(r))],a=new Map,l=new Map;for(let r of n){let g=r.id||r.name,f=r.source;if(!g||!f)continue;let u=null;if(f.git&&f.rev)u={type:"git",git:f.git,rev:f.rev,subdir:f.subdir};else if(f.local&&this.rootSource?.type==="git"){let h=this.resolveRelativePath(this.rootSource.subdir||"",f.local);u={type:"git",git:this.rootSource.git,rev:this.rootSource.rev,subdir:h}}else continue;let p=await this.fetcher.fetch(u.git,u.rev,u.subdir,`lockfile:${g}`);if(Object.keys(p).length===0)continue;let m=p["Move.toml"];if(!m)continue;let y=await this.buildPackage(g,u,m,p);a.set(g,y),l.set(y.manifest.name,y),this.packageFiles.set(y.manifest.name,p),e.addPackage(y)}c.length&&e.setLockfileOrder(c);for(let r of n){let g=r.id||r.name,f=a.get(g);if(!f)continue;let u=r.dependencies;if(u&&Array.isArray(u))for(let p of u){let m=p.id||p.name,y=a.get(m)||l.get(m);if(y){let h={source:y.id.source};e.addDependency(f.id.name,y.id.name,h)}}}for(let r of s.dependencies.keys()){let g=l.get(r);if(g){let f=s.dependencies.get(r);e.addDependency(s.id.name,g.id.name,f)}}return a.size>0}async loadFromLockfileV3(e,t,s){let n=t.move?.package;if(!n||!Array.isArray(n))return!1;let o=new Map,i=new Map,c=[];for(let a of n)a.id&&i.set(a.id,a);for(let a of n){let l=a.id,r=a.source;c.push(l);let g=null;if(r?.git&&r.rev)g={type:"git",git:r.git,rev:r.rev,subdir:r.subdir};else if(r?.local&&this.rootSource?.type==="git"){let m=this.resolveRelativePath(this.rootSource.subdir||"",r.local);g={type:"git",git:this.rootSource.git,rev:this.rootSource.rev,subdir:m}}else continue;let f=await this.fetcher.fetch(r.git,r.rev,r.subdir,`lockfile:${l}`);if(Object.keys(f).length===0)continue;let u=f["Move.toml"];if(!u)continue;let p=await this.buildPackage(l,g,u,f);o.set(l,p),this.packageFiles.set(p.manifest.name,f),e.addPackage(p)}e.setLockfileOrder(c);for(let a of n){let l=a.id,r=o.get(l);if(!r)continue;let g=a.dependencies;if(!(!g||!Array.isArray(g)))for(let f of g){let u=f.id,p=o.get(u);if(!p){let m=i.get(u);if(m?.source?.local&&r.id.source.type==="git"){let y=this.resolveRelativePath(r.id.source.subdir||"",m.source.local),h={type:"git",git:r.id.source.git,rev:r.id.source.rev,subdir:y},k=await this.fetcher.fetch(h.git,h.rev,h.subdir,`lockfile:${u}`),v=k["Move.toml"];if(v){let b=await this.buildPackage(u,h,v,k);o.set(u,b),this.packageFiles.set(b.manifest.name,k),e.addPackage(b),p=b}}}if(p){let m={source:p.id.source};e.addDependency(r.id.name,p.id.name,m)}}}for(let a of s.dependencies.keys()){let l=o.get(a);if(l){let r=s.dependencies.get(a);e.addDependency(s.id.name,l.id.name,r)}}return!0}async loadFromLockfileV4(e,t,s){let n=t.pinned?.[this.network];if(!n)return!1;let o=rootFiles["Move.toml"];if(o&&t.move?.manifest_digest&&await this.computeManifestDigest(o)!==t.move.manifest_digest)return!1;let i=new Map,c=new Map,a=[];for(let[l,r]of Object.entries(n)){a.push(l);let g=this.lockfileSourceToDependencySource(r.source);if(!g)continue;let f=await this.fetchFromSource(g);if(!f)return!1;let u=f["Move.toml"];if(!u||r["manifest-digest"]&&await this.computeManifestDigest(u)!==r["manifest-digest"])return!1;let p=await this.buildPackage(l,g,u,f);i.set(l,p),c.set(p.manifest.name,p),this.packageFiles.set(p.manifest.name,f),(g.type!=="local"||!("root"in r.source))&&e.addPackage(p)}a.length>0&&e.setLockfileOrder(a);for(let[l,r]of Object.entries(n)){let g=i.get(l);if(g&&r.deps)for(let[f,u]of Object.entries(r.deps)){let p=i.get(u);if(p){let m=g.dependencies.get(f);m&&e.addDependency(g.id.name,p.id.name,m)}}}for(let l of rootPackage.dependencies.keys()){let r=c.get(l)||i.get(l);if(r){let g=rootPackage.dependencies.get(l);e.addDependency(rootPackage.id.name,r.id.name,g)}}return!0}lockfileSourceToDependencySource(e){return"git"in e?{type:"git",git:e.git,rev:e.rev,subdir:e.subdir}:"local"in e?{type:"local",local:e.local}:"root"in e?{type:"local"}:null}async fetchFromSource(e){if(e.type==="git"&&e.git&&e.rev)try{return await this.fetcher.fetch(e.git,e.rev,e.subdir,"lockfile:dependency")}catch{return null}return null}reconstructMoveToml(e,t,s,n){let i=`[package]
|
|
17
17
|
name = "${e.package.name}"
|
|
18
18
|
version = "${e.package.version}"
|
|
19
|
-
`,c=n||e.package.edition;if(c&&(
|
|
20
|
-
`),
|
|
19
|
+
`,c=n||e.package.edition;if(c&&(i+=`edition = "${c}"
|
|
20
|
+
`),i+=`
|
|
21
21
|
[dependencies]
|
|
22
|
-
`,e.dependencies)for(let[a,l]of Object.entries(e.dependencies)){let r=l;r.local?
|
|
23
|
-
`:r.git&&r.rev&&(r.subdir?
|
|
24
|
-
`:
|
|
25
|
-
`)}
|
|
22
|
+
`,e.dependencies)for(let[a,l]of Object.entries(e.dependencies)){let r=l;r.local?i+=`${a} = { local = "${r.local}" }
|
|
23
|
+
`:r.git&&r.rev&&(r.subdir?i+=`${a} = { git = "${r.git}", subdir = "${r.subdir}", rev = "${r.rev}" }
|
|
24
|
+
`:i+=`${a} = { git = "${r.git}", rev = "${r.rev}" }
|
|
25
|
+
`)}i+=`
|
|
26
26
|
[addresses]
|
|
27
|
-
`;for(let[a,l]of Object.entries(t))
|
|
28
|
-
`;return
|
|
27
|
+
`;for(let[a,l]of Object.entries(t))i+=`${a} = "${l}"
|
|
28
|
+
`;return i}normalizeAddress(e){if(!e)return e;let t=e.trim();return t.startsWith("0x")&&(t=t.slice(2)),/^[0-9a-fA-F]+$/.test(t)?"0x"+t.padStart(64,"0"):e}isSuiRepo(e){return e.includes("github.com/MystenLabs/sui")}inferSuiFrameworkSubdir(e){let t={Sui:"crates/sui-framework/packages/sui-framework",MoveStdlib:"crates/sui-framework/packages/move-stdlib",SuiSystem:"crates/sui-framework/packages/sui-system",Bridge:"crates/sui-framework/packages/bridge",DeepBook:"crates/sui-framework/packages/deepbook",SuiFramework:"crates/sui-framework/packages/sui-framework"};return t[e]||t[e.toLowerCase()]}};async function C(d,e,t,s="mainnet",n){return new F(t,s,n||null).resolve(d,e)}function Q(d){try{let e=new URL(d);if(e.hostname!=="github.com")return null;let t=e.pathname.split("/").filter(Boolean);if(t.length<2)return null;let s=t[0],n=t[1],o="main",i;return t.length>=4&&t[2]==="tree"&&(o=t[3],t.length>4&&(i=t.slice(4).join("/"))),{owner:s,repo:n,ref:o,subdir:i}}catch{return null}}async function B(d,e){let t=Q(d);if(!t)throw new Error(`Invalid GitHub URL: ${d}`);let s=e?.fetcher||new P(e?.githubToken),n=e?.includeLock!==!1,o=`https://github.com/${t.owner}/${t.repo}.git`,i=await s.fetch(o,t.ref,t.subdir,`root:${t.owner}/${t.repo}`);if(Object.defineProperty(i,"__rootGit",{value:{git:o,rev:t.ref,subdir:t.subdir},enumerable:!1}),!n&&i["Move.lock"]){let{"Move.lock":c,...a}=i;return a}return i}var O;async function R(d){return O||(O=import("./sui_move_wasm.js").then(async e=>(d?await e.default({module_or_path:d}):await e.default(),e))),O}function T(d){return{error:d instanceof Error?d.message:typeof d=="string"?d:"Unknown error"}}function _(d){if(typeof d!="object"||d===null)throw new Error("Unexpected compile result shape from wasm");let e=d;if(typeof e.success=="function"&&typeof e.output=="function")return e;if(typeof e.success=="boolean"&&typeof e.output=="string")return{success:()=>e.success,output:()=>e.output};throw new Error("Unexpected compile result shape from wasm")}function X(d){let e=t=>{let s=t.startsWith("0x")?t.slice(2):t,n=s.length%2===0?s:`0${s}`,o=[];for(let i=0;i<n.length;i+=2){let c=parseInt(n.slice(i,i+2),16);if(Number.isNaN(c))throw new Error("invalid hex digest");o.push(c)}return o};try{let t=JSON.parse(d);if(!t.modules||!t.dependencies||!t.digest)throw new Error("missing fields in compiler output");let s=typeof t.digest=="string"?e(t.digest):Array.from(t.digest);return{modules:t.modules,dependencies:t.dependencies,digest:s}}catch(t){return T(t)}}function Y(d){try{let e=JSON.parse(d);for(let t of e){let s=t.addressMapping?.[t.name]??(()=>{let n=Object.entries(t.files).find(([i])=>i.endsWith("Move.toml"));if(!n)return;let o=w(n[1]);return o.addresses&&o.addresses[t.name]||o.package?.published_at||o.package?.["published-at"]})()}}catch{}}async function Z(d){await R(d?.wasm)}async function j(d){let e=d.files["Move.toml"]||"",t=d.rootGit||d.files.__rootGit,s=await C(e,{...d.files,"Move.toml":e},new P(d.githubToken),d.network,t?{type:"git",git:t.git,rev:t.rev,subdir:t.subdir}:void 0);return{files:s.files,dependencies:s.dependencies}}async function ee(d){try{let e=d.resolvedDependencies?d.resolvedDependencies:await j(d),t=await R(d.wasm);Y(e.dependencies);let s=d.ansiColor&&typeof t.compile_with_color=="function"?t.compile_with_color(e.files,e.dependencies,!0):t.compile(e.files,e.dependencies),n=_(s),o=n.success(),i=n.output();return o?X(i):T(i)}catch(e){return T(e)}}async function te(d){return(await R(d?.wasm)).sui_move_version()}async function ne(d){return(await R(d?.wasm)).sui_version()}async function ie(d){return R(d?.wasm)}async function se(d,e,t){let s=await R(t?.wasm),n=t?.ansiColor&&typeof s.compile_with_color=="function"?s.compile_with_color(d,e,!0):s.compile(d,e),o=_(n);return{success:o.success(),output:o.output()}}0&&(module.exports={buildMovePackage,compileRaw,fetchPackageFromGitHub,getSuiMoveVersion,getSuiVersion,getWasmBindings,initMoveCompiler,resolveDependencies});
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
var A=class{async fetch(e,t,
|
|
2
|
-
`),
|
|
3
|
-
`,l=!1,f=!1;for(let g of
|
|
1
|
+
var A=class{async fetch(e,t,s){throw new Error("Not implemented")}async fetchFile(e,t,s){throw new Error("Not implemented")}},P=class extends A{constructor(t){super();this.rateLimitRemaining=60;this.rateLimitReset=0;this.cache=new Map,this.treeCache=new Map,this.token=t}updateRateLimit(t){let s=t.headers.get("x-ratelimit-remaining"),n=t.headers.get("x-ratelimit-reset");s&&(this.rateLimitRemaining=parseInt(s,10)),n&&(this.rateLimitReset=parseInt(n,10)*1e3)}async fetch(t,s,n,o){let{owner:i,repo:c}=this.parseGitUrl(t);if(!i||!c)throw new Error(`Invalid git URL: ${t}`);let a=`${i}/${c}@${s}`,d=`https://api.github.com/repos/${i}/${c}/git/trees/${s}?recursive=1`,r;if(this.treeCache.has(a))r=this.treeCache.get(a);else{let p=null;for(let m=1;m<=3;m++)try{if(m>1){let k=Math.pow(2,m-1)*1e3;await new Promise(v=>setTimeout(v,k))}let y={};this.token&&(y.Authorization=`Bearer ${this.token}`);let h=await fetch(d,{headers:y});if(this.updateRateLimit(h),!h.ok){if(h.status===403||h.status===429){let k=new Date(this.rateLimitReset);throw new Error(`GitHub API rate limit exceeded. Resets at ${k.toLocaleTimeString()}`)}if(h.status>=500&&h.status<600&&m<3){p=new Error(`Failed to fetch tree: ${h.statusText}`);continue}throw new Error(`Failed to fetch tree: ${h.statusText}`)}r=await h.json(),this.treeCache.set(a,r);break}catch(y){if(p=y instanceof Error?y:new Error(String(y)),m===3)return{}}if(p)return{}}let l={},f=[];for(let g of r.tree){if(g.type!=="blob")continue;let p=g.path;if(n){if(!g.path.startsWith(n))continue;p=g.path.slice(n.length),p.startsWith("/")&&(p=p.slice(1))}if(!p.endsWith(".move")&&p!=="Move.toml"&&p!=="Move.lock"&&!p.match(/^Move\.(mainnet|testnet|devnet)\.toml$/))continue;let m=`https://raw.githubusercontent.com/${i}/${c}/${s}/${g.path}`,y=this.fetchContent(m).then(h=>{h&&(l[p]=h)});f.push(y)}if(await Promise.all(f),l["Move.toml"]){let g=l["Move.toml"].trim();if(g.match(/^Move\.(mainnet|testnet|devnet)\.toml$/)&&!g.includes("[")&&!g.includes("=")){let p=g,m=n?`${n}/${p}`.replace(/\/+/g,"/"):p,y=`https://raw.githubusercontent.com/${i}/${c}/${s}/${m}`,h=await this.fetchContent(y);h&&(l["Move.toml"]=h,l[p]=h)}}return l}async fetchFile(t,s,n){let{owner:o,repo:i}=this.parseGitUrl(t);if(!o||!i)throw new Error(`Invalid git URL: ${t}`);let c=`https://raw.githubusercontent.com/${o}/${i}/${s}/${n}`;return this.fetchContent(c)}async fetchContent(t){if(this.cache.has(t))return this.cache.get(t)??null;try{let s={},n=typeof window<"u",o=t.startsWith("https://api.github.com/");this.token&&(!n||o)&&(s.Authorization=`Bearer ${this.token}`);let i=await fetch(t,{headers:s});if(!i.ok)return null;let c=await i.text();return this.cache.set(t,c),c}catch{return null}}parseGitUrl(t){try{let n=new URL(t).pathname.split("/").filter(o=>o);if(n.length>=2){let o=n[1];return o.endsWith(".git")&&(o=o.slice(0,-4)),{owner:n[0],repo:o}}}catch{}return{owner:null,repo:null}}};function S(u){let e=!1,t="";for(let s=0;s<u.length;s++){let n=u[s];if((n==='"'||n==="'")&&(!e||n===t)&&(e=!e,t=n),!e&&n==="#")return u.slice(0,s)}return u}function I(u){let e=u.trim();if(!e)return"";if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))return e.slice(1,-1);if(e==="true")return!0;if(e==="false")return!1;let t=Number(e);return Number.isNaN(t)?e:t}function L(u){let e={},t=u.trim().replace(/^\{/,"").replace(/\}$/,""),s="",n=!1,o="",i=[];for(let c=0;c<t.length;c++){let a=t[c];if((a==='"'||a==="'")&&(!n||a===o)&&(n=!n,o=a),!n&&a===","){i.push(s),s="";continue}s+=a}s.trim()&&i.push(s);for(let c of i){let a=c.indexOf("=");if(a===-1)continue;let d=c.slice(0,a).trim(),r=c.slice(a+1).trim();e[d]=I(r)}return e}function C(u){let e=[],t=u.trim().replace(/^\[/,"").replace(/\]$/,""),s="",n=!1,o="",i=0;for(let c=0;c<t.length;c++){let a=t[c];if((a==='"'||a==="'")&&(!n||a===o)&&(n=!n,o=n?a:""),!n&&(a==="{"&&i++,a==="}"&&i--,a===","&&i===0)){s.trim()&&e.push(T(s.trim())),s="";continue}s+=a}return s.trim()&&e.push(T(s.trim())),e}function T(u){return u.startsWith("{")?L(u):I(u)}function w(u){let e={},t=null,s=!1,n=u.split(/\r?\n/),o=[],i=0;for(;i<n.length;){let a=S(n[i]);if(a.match(/=\s*\[\s*$/)||a.includes("=")&&a.includes("[")&&!a.includes("]")){let d=a;for(i++;i<n.length&&!d.includes("]");)d+=" "+S(n[i]).trim(),i++;i<n.length&&d.includes("[")&&!d.includes("]")&&(d+=" "+S(n[i]).trim(),i++),o.push(d)}else o.push(a),i++}function c(a,d){let r=a;for(let l of d){if(!(l in r))return;r=r[l]}return r}for(let a of o){let d=S(a).trim();if(!d)continue;let r=d.match(/^\[\[([^\]]+)\]\]$/);if(r){t=r[1].trim(),s=!0;let y=t.split("."),h=e;for(let v=0;v<y.length-1;v++){let b=y[v];b in h||(h[b]={}),h=h[b]}let k=y[y.length-1];Array.isArray(h[k])||(h[k]=[]),h[k].push({});continue}let l=d.match(/^\[([^\]]+)\]$/);if(l){t=l[1].trim(),s=!1;continue}let f=d.indexOf("=");if(f===-1||!t)continue;let g=d.slice(0,f).trim(),p=d.slice(f+1).trim(),m;if(p.startsWith("{")?m=L(p):p.startsWith("[")?m=C(p):m=I(p),s){let y=t.split("."),h=c(e,y);if(Array.isArray(h)&&h.length>0){let k=h[h.length-1];k[g]=m}}else{let y=t.split("."),h=e;for(let v of y)v in h||(h[v]={}),h=h[v];let k=t==="package"?g.replace(/-/g,"_"):g;h[k]=m}}return e}var D=class{constructor(e){this.packageTable=new Map;this.graph=new Map;this.alwaysDeps=new Set(["Sui","MoveStdlib","SuiSystem","Bridge"]);this.lockfileOrder=[];this.root=e}setLockfileOrder(e){this.lockfileOrder=e}addPackage(e){this.packageTable.set(e.id.name,e),this.graph.has(e.id.name)||this.graph.set(e.id.name,new Set)}addDependency(e,t,s){this.graph.has(e)||this.graph.set(e,new Set),this.graph.get(e).add(t)}getPackage(e){return this.packageTable.get(e)}getAllPackages(){return Array.from(this.packageTable.values())}getImmediateDependencies(e){return this.graph.get(e)||new Set}getTransitiveDependencies(e){let t=new Set,s=new Set,n=o=>{if(s.has(o))return;s.add(o),t.add(o);let i=this.graph.get(o);if(i)for(let c of i)n(c)};return n(e),t.delete(e),t}topologicalOrder(){if(!this.lockfileOrder.length)return this.topologicalOrderDFS().filter(i=>i!==this.root);let e=new Set,t=new Set,s=o=>{if(t.has(o))return;t.add(o),e.add(o);let i=this.graph.get(o);if(i)for(let c of i)s(c)};s(this.root);let n=[];for(let o of this.lockfileOrder)o!==this.root&&e.has(o)&&n.push(o);return n}topologicalOrderDFS(){let e=new Set,t=[],s=n=>{if(e.has(n))return;e.add(n);let o=this.graph.get(n);if(o)for(let i of Array.from(o))s(i);t.push(n)};s(this.root);for(let n of this.packageTable.keys())s(n);return t}detectCycle(){let e=new Set,t=new Set,s=new Map,n=o=>{e.add(o),t.add(o);let i=this.graph.get(o);if(i)for(let c of i)if(e.has(c)){if(t.has(c)){let a=[c],d=o;for(;d!==c;)a.unshift(d),d=s.get(d);return a.unshift(c),a}}else{s.set(c,o);let a=n(c);if(a)return a}return t.delete(o),null};for(let o of this.packageTable.keys())if(!e.has(o)){let i=n(o);if(i)return i}return null}getRootPackage(){return this.packageTable.get(this.root)}getRootName(){return this.root}isAlwaysDep(e){return this.alwaysDeps.has(e)}};var M=class{constructor(e,t={}){this.unifiedAddressTable=new Map;this.packageResolvedTables=new Map;this.graph=e,this.buildConfig=t}async resolve(){let t=[this.graph.getRootName(),...this.graph.topologicalOrder()];for(let s of t){let n=this.graph.getPackage(s);if(n)for(let[o,i]of Object.entries(n.manifest.addresses)){let c=this.normalizeAddress(i);this.unifiedAddressTable.has(o)&&this.unifiedAddressTable.get(o)!==c||this.unifiedAddressTable.set(o,c)}}for(let s of this.graph.getAllPackages()){let n={};for(let[o,i]of this.unifiedAddressTable.entries())n[o]=i;this.packageResolvedTables.set(s.id.name,n),s.resolvedTable=n}}normalizeAddress(e){if(!e)return e;let t=e.trim();return t.startsWith("0x")&&(t=t.slice(2)),/^[0-9a-fA-F]+$/.test(t)?"0x"+t.padStart(64,"0"):e}getUnifiedAddressTable(){let e={};for(let[t,s]of this.unifiedAddressTable.entries())e[t]=s;return e}getPackageResolvedTable(e){return this.packageResolvedTables.get(e)}getGraph(){return this.graph}topologicalOrder(){return this.graph.topologicalOrder()}getRootName(){return this.graph.getRootName()}getPackage(e){return this.graph.getPackage(e)}getImmediateDependencies(e){return this.graph.getImmediateDependencies(e)}};var $=class{constructor(e){this.dependencies=[];this.resolvedGraph=e,this.rootPackageName=e.getRootName()}async compute(e){if(!this.resolvedGraph.getPackage(this.rootPackageName))throw new Error(`Root package '${this.rootPackageName}' not found`);let s=this.resolvedGraph.getImmediateDependencies(this.rootPackageName),n=this.resolvedGraph.topologicalOrder(),o=new Set(["Bridge","SuiSystem"]);for(let i of n){if(i===this.rootPackageName)continue;let c=this.resolvedGraph.getPackage(i);if(!c||o.has(i))continue;let a=e.get(i)||{},d=this.extractSourcePaths(i,a),r=c.manifest.edition||"legacy",l=c.manifest.latestPublishedId||c.manifest.originalId||c.manifest.publishedAt||c.resolvedTable?.[i],f={name:i,isImmediate:s.has(i),sourcePaths:d,addressMapping:c.resolvedTable||{},compilerConfig:{edition:r,flavor:"sui"},moduleFormat:d.length>0?"Source":"Bytecode",edition:r,publishedIdForOutput:l};this.dependencies.push(f)}}extractSourcePaths(e,t){let s=Object.keys(t).filter(i=>i.endsWith("Move.toml")||i.endsWith("Move.lock")?!1:i.endsWith(".move")),n=new TextEncoder,o=(i,c)=>{let a=n.encode(`/vfs/deps/${e}/${i}`),d=n.encode(`/vfs/deps/${e}/${c}`),r=Math.min(a.length,d.length);for(let l=0;l<r;l++)if(a[l]!==d[l])return a[l]-d[l];return a.length-d.length};return s.sort(o),s}toPackageGroupedFormat(e){let t=[];for(let s of this.dependencies){let n=e.get(s.name)||{},o={};for(let[i,c]of Object.entries(n)){if(i.endsWith("Move.lock"))continue;let a=`dependencies/${s.name}/${i}`;i.endsWith("Move.toml")?o[a]=this.reconstructDependencyMoveToml(s.name,c,s.edition,s.addressMapping):o[a]=c}t.push({name:s.name,files:o,edition:s.edition,addressMapping:s.addressMapping,publishedIdForOutput:s.publishedIdForOutput})}return t}reconstructDependencyMoveToml(e,t,s,n){let o=(t||"").split(`
|
|
2
|
+
`),i=[],c=[],a=!1,d=!1;for(let g of o){let p=g.trim();if(p.startsWith("[package]")){a=!0,d=!1;continue}if(p.startsWith("[dependencies]")){a=!1,d=!0;continue}if(p.startsWith("[")){a=!1,d=!1;continue}a&&p&&i.push(g),d&&p&&c.push(g)}let r=`[package]
|
|
3
|
+
`,l=!1,f=!1;for(let g of i)if(g.includes("name ="))r+=g+`
|
|
4
4
|
`,l=!0;else if(g.includes("version ="))r+=g+`
|
|
5
5
|
`,f=!0;else{if(g.includes("edition ="))continue;r+=g+`
|
|
6
6
|
`}l||(r+=`name = "${e}"
|
|
7
7
|
`),f||(r+=`version = "0.0.0"
|
|
8
|
-
`),r+=`edition = "${
|
|
8
|
+
`),r+=`edition = "${s}"
|
|
9
9
|
`,r+=`
|
|
10
10
|
[dependencies]
|
|
11
11
|
`;for(let g of c)r+=g+`
|
|
12
12
|
`;r+=`
|
|
13
13
|
[addresses]
|
|
14
14
|
`;for(let[g,p]of Object.entries(n))r+=`${g} = "${p}"
|
|
15
|
-
`;return r}};var x=class{constructor(e,t="mainnet",
|
|
16
|
-
`))}this.packageNameCache.set(r.manifest.name,n.source)}r.manifest.publishedAt&&(r.manifest.addresses[r.manifest.name]=this.normalizeAddress(r.manifest.publishedAt)),r.manifest.edition||(r.manifest.edition="legacy"),e.addPackage(r),e.addDependency(t.id.name,r.id.name,n),this.packageFiles.set(r.id.name,c),await this.buildDependencyGraph(e,r)}}getChainIdForNetwork(e){return{mainnet:"35834a8a",testnet:"4c78adac",devnet:"2",localnet:"localnet"}[e]||e}resolvePublishedAt(e,t,
|
|
15
|
+
`;return r}};var x=class{constructor(e,t="mainnet",s=null){this.visited=new Set;this.packageNameCache=new Map;this.packageFiles=new Map;this.fetcher=e,this.network=t,this.rootSource=s}async resolve(e,t){let s=w(e),n=s.package?.name||"RootPackage",o=s.package?.edition,i=new D(n),c=await this.buildPackage(n,this.rootSource,e,t);o&&(c.manifest.edition=o);let a=c.manifest.addresses[n];this.normalizeAddress(a||"")==="0x0000000000000000000000000000000000000000000000000000000000000000"&&c.manifest.originalId&&(c.manifest.addresses[n]=this.normalizeAddress(c.manifest.originalId)),i.addPackage(c),this.packageFiles.set(n,t);let r=await this.loadFromLockfile(i,c,t),l=Array.from(c.dependencies.keys()).filter(k=>!i.getPackage(k));(!r||l.length>0)&&await this.buildDependencyGraph(i,c);let f=i.detectCycle();if(f)throw new Error(`Dependency cycle detected: ${f.join(" \u2192 ")}`);let g=new M(i,{});await g.resolve();let p=new $(g);await p.compute(this.packageFiles);let m=this.reconstructMoveToml(s,g.getUnifiedAddressTable(),!0,o),y={...t};delete y["Move.lock"],y["Move.toml"]=m;let h=p.toPackageGroupedFormat(this.packageFiles);return{files:JSON.stringify(y),dependencies:JSON.stringify(h)}}async buildPackage(e,t,s,n){let o=w(s),i=n["Move.lock"],c=this.getChainIdForNetwork(this.network),a=this.resolvePublishedAt(s,i,c),d=a.latestId?this.normalizeAddress(a.latestId):void 0;a.error;let r={name:o.package?.name||e,version:o.package?.version||"0.0.0",edition:o.package?.edition,publishedAt:a.publishedAt,originalId:a.originalId,latestPublishedId:d,addresses:o.addresses||{},dependencies:o.dependencies||{},devDependencies:o["dev-dependencies"]},l=r.publishedAt&&r.publishedAt!=="0x0"?this.normalizeAddress(r.publishedAt):void 0,f=r.addresses[r.name],g=f?this.normalizeAddress(f):void 0;l?r.addresses[r.name]=l:g?r.addresses[r.name]=g:r.addresses[r.name]="0x0";let p=new Map;if(r.dependencies)for(let[y,h]of Object.entries(r.dependencies)){let k=this.parseDependencyInfo(h);k&&p.set(y,k)}return{id:{name:r.name,version:r.version,source:t||{type:"local"}},manifest:r,dependencies:p,devDependencies:new Map}}parseDependencyInfo(e){if(!e)return null;let t={source:{type:"local"}};if(e.git&&e.rev)t.source={type:"git",git:e.git,rev:e.rev,subdir:e.subdir};else if(e.local)t.source={type:"local",local:e.local};else return null;if(e["addr-subst"]||e.addr_subst){let s=e["addr-subst"]||e.addr_subst,n={};for(let[o,i]of Object.entries(s))typeof i=="string"&&(i.startsWith("0x")||/^[0-9a-fA-F]+$/.test(i)?n[o]={type:"assign",address:i}:n[o]={type:"renameFrom",name:i});Object.keys(n).length>0&&(t.subst=n)}return t}async buildDependencyGraph(e,t){for(let[s,n]of t.dependencies.entries()){if(n.source.type==="local")if(t.id.source.type==="git"&&n.source.local){let l=t.id.source.subdir||"",f=n.source.local,g=this.resolveRelativePath(l,f);n.source={type:"git",git:t.id.source.git,rev:t.id.source.rev,subdir:g}}else continue;if(n.source.type!=="git")continue;let o=`${n.source.git}|${n.source.rev}|${n.source.subdir||""}`;if(this.visited.has(o)){let l=this.findPackageBySource(e,n.source);l&&e.addDependency(t.id.name,l.id.name,n);continue}this.visited.add(o);let i=n.source.subdir;!i&&n.source.git&&this.isSuiRepo(n.source.git)&&(i=this.inferSuiFrameworkSubdir(s),i&&(n.source.subdir=i));let c=await this.fetcher.fetch(n.source.git,n.source.rev,i,`${t.id.name} -> ${s}`),a=null,d=`Move.${this.network}.toml`;for(let[l,f]of Object.entries(c))if(l.endsWith(d)){a=f;break}if(!a){for(let[l,f]of Object.entries(c))if(l.endsWith("Move.toml")){a=f;break}}if(!a)continue;let r=await this.buildPackage(s,n.source,a,c);if(!this.lockfileVersion||this.lockfileVersion<4){let l=this.packageNameCache.get(r.manifest.name);if(l){let f=g=>JSON.stringify(g);throw new Error([`Conflicting versions of package '${r.manifest.name}' found`,`Existing: ${f(l)}`,`New: ${f(n.source)}`,`When resolving dependencies for '${t.id.name}' -> '${s}'`].join(`
|
|
16
|
+
`))}this.packageNameCache.set(r.manifest.name,n.source)}r.manifest.publishedAt&&(r.manifest.addresses[r.manifest.name]=this.normalizeAddress(r.manifest.publishedAt)),r.manifest.edition||(r.manifest.edition="legacy"),e.addPackage(r),e.addDependency(t.id.name,r.id.name,n),this.packageFiles.set(r.id.name,c),await this.buildDependencyGraph(e,r)}}getChainIdForNetwork(e){return{mainnet:"35834a8a",testnet:"4c78adac",devnet:"2",localnet:"localnet"}[e]||e}resolvePublishedAt(e,t,s){let n=w(e),o=n.package?.published_at||n.package?.["published-at"],i=o&&o!=="0x0"?o:void 0,c=i?this.normalizeAddress(i):void 0,a=n.package?.["original-id"];if(!t||!s)return{publishedAt:c,originalId:a};let d=w(t),r,l,f;if(d.env)for(let[,g]of Object.entries(d.env)){let p=g["latest-published-id"],m=g["original-published-id"],y=g["chain-id"];if(y===s||!y){let h=p&&p!=="0x0"?this.normalizeAddress(p):void 0,k=m&&m!=="0x0"?this.normalizeAddress(m):void 0;l=h,f=k,r=h||k;break}}return!f&&r&&(f=r),r&&c&&r!==c?{error:`Conflicting 'published-at' addresses between Move.toml (${c}) and Move.lock (${r})`}:{publishedAt:f||c||r,originalId:f||a,latestId:l||r||c}}findPackageBySource(e,t){for(let s of e.getAllPackages()){let n=s.id.source;if(n.type===t.type&&n.git===t.git&&n.rev===t.rev&&n.subdir===t.subdir)return s}}resolveRelativePath(e,t){let s=e?e.split("/").filter(Boolean):[],n=t.split("/").filter(Boolean),o=[...s];for(let i of n)i===".."?o.length>0&&o.pop():i!=="."&&o.push(i);return o.join("/")}async computeManifestDigest(e){let s=new TextEncoder().encode(e),n=await crypto.subtle.digest("SHA-256",s);return Array.from(new Uint8Array(n)).map(c=>c.toString(16).padStart(2,"0")).join("").toUpperCase()}async loadFromLockfile(e,t,s){let n=s["Move.lock"];if(!n)return!1;let o=w(n);this.lockfileVersion=o.move?.version;let i=o.move?.version;return i===3?await this.loadFromLockfileV3(e,o,t):i&&i>=4?await this.loadFromLockfileV4(e,o,s):await this.loadFromLockfileV0(e,o,t)}async loadFromLockfileV0(e,t,s){let n=t.move?.package;if(!n||!Array.isArray(n))return!1;let o=Array.isArray(t.move?.dependencies)?t.move.dependencies.map(r=>r.name||r.id||r).filter(Boolean):[],i=n.map(r=>r.name||r.id).filter(Boolean),c=[...o,...i.filter(r=>!o.includes(r))],a=new Map,d=new Map;for(let r of n){let l=r.id||r.name,f=r.source;if(!l||!f)continue;let g=null;if(f.git&&f.rev)g={type:"git",git:f.git,rev:f.rev,subdir:f.subdir};else if(f.local&&this.rootSource?.type==="git"){let h=this.resolveRelativePath(this.rootSource.subdir||"",f.local);g={type:"git",git:this.rootSource.git,rev:this.rootSource.rev,subdir:h}}else continue;let p=await this.fetcher.fetch(g.git,g.rev,g.subdir,`lockfile:${l}`);if(Object.keys(p).length===0)continue;let m=p["Move.toml"];if(!m)continue;let y=await this.buildPackage(l,g,m,p);a.set(l,y),d.set(y.manifest.name,y),this.packageFiles.set(y.manifest.name,p),e.addPackage(y)}c.length&&e.setLockfileOrder(c);for(let r of n){let l=r.id||r.name,f=a.get(l);if(!f)continue;let g=r.dependencies;if(g&&Array.isArray(g))for(let p of g){let m=p.id||p.name,y=a.get(m)||d.get(m);if(y){let h={source:y.id.source};e.addDependency(f.id.name,y.id.name,h)}}}for(let r of s.dependencies.keys()){let l=d.get(r);if(l){let f=s.dependencies.get(r);e.addDependency(s.id.name,l.id.name,f)}}return a.size>0}async loadFromLockfileV3(e,t,s){let n=t.move?.package;if(!n||!Array.isArray(n))return!1;let o=new Map,i=new Map,c=[];for(let a of n)a.id&&i.set(a.id,a);for(let a of n){let d=a.id,r=a.source;c.push(d);let l=null;if(r?.git&&r.rev)l={type:"git",git:r.git,rev:r.rev,subdir:r.subdir};else if(r?.local&&this.rootSource?.type==="git"){let m=this.resolveRelativePath(this.rootSource.subdir||"",r.local);l={type:"git",git:this.rootSource.git,rev:this.rootSource.rev,subdir:m}}else continue;let f=await this.fetcher.fetch(r.git,r.rev,r.subdir,`lockfile:${d}`);if(Object.keys(f).length===0)continue;let g=f["Move.toml"];if(!g)continue;let p=await this.buildPackage(d,l,g,f);o.set(d,p),this.packageFiles.set(p.manifest.name,f),e.addPackage(p)}e.setLockfileOrder(c);for(let a of n){let d=a.id,r=o.get(d);if(!r)continue;let l=a.dependencies;if(!(!l||!Array.isArray(l)))for(let f of l){let g=f.id,p=o.get(g);if(!p){let m=i.get(g);if(m?.source?.local&&r.id.source.type==="git"){let y=this.resolveRelativePath(r.id.source.subdir||"",m.source.local),h={type:"git",git:r.id.source.git,rev:r.id.source.rev,subdir:y},k=await this.fetcher.fetch(h.git,h.rev,h.subdir,`lockfile:${g}`),v=k["Move.toml"];if(v){let b=await this.buildPackage(g,h,v,k);o.set(g,b),this.packageFiles.set(b.manifest.name,k),e.addPackage(b),p=b}}}if(p){let m={source:p.id.source};e.addDependency(r.id.name,p.id.name,m)}}}for(let a of s.dependencies.keys()){let d=o.get(a);if(d){let r=s.dependencies.get(a);e.addDependency(s.id.name,d.id.name,r)}}return!0}async loadFromLockfileV4(e,t,s){let n=t.pinned?.[this.network];if(!n)return!1;let o=rootFiles["Move.toml"];if(o&&t.move?.manifest_digest&&await this.computeManifestDigest(o)!==t.move.manifest_digest)return!1;let i=new Map,c=new Map,a=[];for(let[d,r]of Object.entries(n)){a.push(d);let l=this.lockfileSourceToDependencySource(r.source);if(!l)continue;let f=await this.fetchFromSource(l);if(!f)return!1;let g=f["Move.toml"];if(!g||r["manifest-digest"]&&await this.computeManifestDigest(g)!==r["manifest-digest"])return!1;let p=await this.buildPackage(d,l,g,f);i.set(d,p),c.set(p.manifest.name,p),this.packageFiles.set(p.manifest.name,f),(l.type!=="local"||!("root"in r.source))&&e.addPackage(p)}a.length>0&&e.setLockfileOrder(a);for(let[d,r]of Object.entries(n)){let l=i.get(d);if(l&&r.deps)for(let[f,g]of Object.entries(r.deps)){let p=i.get(g);if(p){let m=l.dependencies.get(f);m&&e.addDependency(l.id.name,p.id.name,m)}}}for(let d of rootPackage.dependencies.keys()){let r=c.get(d)||i.get(d);if(r){let l=rootPackage.dependencies.get(d);e.addDependency(rootPackage.id.name,r.id.name,l)}}return!0}lockfileSourceToDependencySource(e){return"git"in e?{type:"git",git:e.git,rev:e.rev,subdir:e.subdir}:"local"in e?{type:"local",local:e.local}:"root"in e?{type:"local"}:null}async fetchFromSource(e){if(e.type==="git"&&e.git&&e.rev)try{return await this.fetcher.fetch(e.git,e.rev,e.subdir,"lockfile:dependency")}catch{return null}return null}reconstructMoveToml(e,t,s,n){let i=`[package]
|
|
17
17
|
name = "${e.package.name}"
|
|
18
18
|
version = "${e.package.version}"
|
|
19
|
-
`,c=n||e.package.edition;if(c&&(
|
|
20
|
-
`),
|
|
19
|
+
`,c=n||e.package.edition;if(c&&(i+=`edition = "${c}"
|
|
20
|
+
`),i+=`
|
|
21
21
|
[dependencies]
|
|
22
|
-
`,e.dependencies)for(let[a,d]of Object.entries(e.dependencies)){let r=d;r.local?
|
|
23
|
-
`:r.git&&r.rev&&(r.subdir?
|
|
24
|
-
`:
|
|
25
|
-
`)}
|
|
22
|
+
`,e.dependencies)for(let[a,d]of Object.entries(e.dependencies)){let r=d;r.local?i+=`${a} = { local = "${r.local}" }
|
|
23
|
+
`:r.git&&r.rev&&(r.subdir?i+=`${a} = { git = "${r.git}", subdir = "${r.subdir}", rev = "${r.rev}" }
|
|
24
|
+
`:i+=`${a} = { git = "${r.git}", rev = "${r.rev}" }
|
|
25
|
+
`)}i+=`
|
|
26
26
|
[addresses]
|
|
27
|
-
`;for(let[a,d]of Object.entries(t))
|
|
28
|
-
`;return
|
|
27
|
+
`;for(let[a,d]of Object.entries(t))i+=`${a} = "${d}"
|
|
28
|
+
`;return i}normalizeAddress(e){if(!e)return e;let t=e.trim();return t.startsWith("0x")&&(t=t.slice(2)),/^[0-9a-fA-F]+$/.test(t)?"0x"+t.padStart(64,"0"):e}isSuiRepo(e){return e.includes("github.com/MystenLabs/sui")}inferSuiFrameworkSubdir(e){let t={Sui:"crates/sui-framework/packages/sui-framework",MoveStdlib:"crates/sui-framework/packages/move-stdlib",SuiSystem:"crates/sui-framework/packages/sui-system",Bridge:"crates/sui-framework/packages/bridge",DeepBook:"crates/sui-framework/packages/deepbook",SuiFramework:"crates/sui-framework/packages/sui-framework"};return t[e]||t[e.toLowerCase()]}};async function G(u,e,t,s="mainnet",n){return new x(t,s,n||null).resolve(u,e)}function B(u){try{let e=new URL(u);if(e.hostname!=="github.com")return null;let t=e.pathname.split("/").filter(Boolean);if(t.length<2)return null;let s=t[0],n=t[1],o="main",i;return t.length>=4&&t[2]==="tree"&&(o=t[3],t.length>4&&(i=t.slice(4).join("/"))),{owner:s,repo:n,ref:o,subdir:i}}catch{return null}}async function _(u,e){let t=B(u);if(!t)throw new Error(`Invalid GitHub URL: ${u}`);let s=e?.fetcher||new P(e?.githubToken),n=e?.includeLock!==!1,o=`https://github.com/${t.owner}/${t.repo}.git`,i=await s.fetch(o,t.ref,t.subdir,`root:${t.owner}/${t.repo}`);if(Object.defineProperty(i,"__rootGit",{value:{git:o,rev:t.ref,subdir:t.subdir},enumerable:!1}),!n&&i["Move.lock"]){let{"Move.lock":c,...a}=i;return a}return i}var F;async function R(u){return F||(F=import("./sui_move_wasm.js").then(async e=>(u?await e.default({module_or_path:u}):await e.default(),e))),F}function O(u){return{error:u instanceof Error?u.message:typeof u=="string"?u:"Unknown error"}}function N(u){if(typeof u!="object"||u===null)throw new Error("Unexpected compile result shape from wasm");let e=u;if(typeof e.success=="function"&&typeof e.output=="function")return e;if(typeof e.success=="boolean"&&typeof e.output=="string")return{success:()=>e.success,output:()=>e.output};throw new Error("Unexpected compile result shape from wasm")}function j(u){let e=t=>{let s=t.startsWith("0x")?t.slice(2):t,n=s.length%2===0?s:`0${s}`,o=[];for(let i=0;i<n.length;i+=2){let c=parseInt(n.slice(i,i+2),16);if(Number.isNaN(c))throw new Error("invalid hex digest");o.push(c)}return o};try{let t=JSON.parse(u);if(!t.modules||!t.dependencies||!t.digest)throw new Error("missing fields in compiler output");let s=typeof t.digest=="string"?e(t.digest):Array.from(t.digest);return{modules:t.modules,dependencies:t.dependencies,digest:s}}catch(t){return O(t)}}function W(u){try{let e=JSON.parse(u);for(let t of e){let s=t.addressMapping?.[t.name]??(()=>{let n=Object.entries(t.files).find(([i])=>i.endsWith("Move.toml"));if(!n)return;let o=w(n[1]);return o.addresses&&o.addresses[t.name]||o.package?.published_at||o.package?.["published-at"]})()}}catch{}}async function le(u){await R(u?.wasm)}async function U(u){let e=u.files["Move.toml"]||"",t=u.rootGit||u.files.__rootGit,s=await G(e,{...u.files,"Move.toml":e},new P(u.githubToken),u.network,t?{type:"git",git:t.git,rev:t.rev,subdir:t.subdir}:void 0);return{files:s.files,dependencies:s.dependencies}}async function ge(u){try{let e=u.resolvedDependencies?u.resolvedDependencies:await U(u),t=await R(u.wasm);W(e.dependencies);let s=u.ansiColor&&typeof t.compile_with_color=="function"?t.compile_with_color(e.files,e.dependencies,!0):t.compile(e.files,e.dependencies),n=N(s),o=n.success(),i=n.output();return o?j(i):O(i)}catch(e){return O(e)}}async function ue(u){return(await R(u?.wasm)).sui_move_version()}async function pe(u){return(await R(u?.wasm)).sui_version()}async function fe(u){return R(u?.wasm)}async function he(u,e,t){let s=await R(t?.wasm),n=t?.ansiColor&&typeof s.compile_with_color=="function"?s.compile_with_color(u,e,!0):s.compile(u,e),o=N(n);return{success:o.success(),output:o.output()}}export{ge as buildMovePackage,he as compileRaw,_ as fetchPackageFromGitHub,ue as getSuiMoveVersion,pe as getSuiVersion,fe as getWasmBindings,le as initMoveCompiler,U as resolveDependencies};
|