@zktx.io/sui-move-builder 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -0
- package/dist/index.cjs +542 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +100 -0
- package/dist/index.d.ts +100 -0
- package/dist/index.js +494 -0
- package/dist/index.js.map +1 -0
- package/dist/sui_move_wasm.d.ts +55 -0
- package/dist/sui_move_wasm.js +314 -0
- package/dist/sui_move_wasm_bg.wasm +0 -0
- package/dist/sui_move_wasm_bg.wasm.d.ts +14 -0
- package/package.json +54 -0
package/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# @zktx.io/sui-move-builder
|
|
2
|
+
|
|
3
|
+
Build Move packages in web or Node.js with dependency fetching and dump outputs.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @zktx.io/sui-move-builder
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start (Node.js or browser)
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { initMoveCompiler, buildMovePackage } from "@zktx.io/sui-move-builder";
|
|
15
|
+
|
|
16
|
+
// 1) Load the WASM once
|
|
17
|
+
await initMoveCompiler();
|
|
18
|
+
|
|
19
|
+
// 2) Prepare files as an in-memory folder (Move.toml + sources/*)
|
|
20
|
+
const files = {
|
|
21
|
+
"Move.toml": `
|
|
22
|
+
[package]
|
|
23
|
+
name = "hello_world"
|
|
24
|
+
version = "0.0.1"
|
|
25
|
+
|
|
26
|
+
[addresses]
|
|
27
|
+
hello_world = "0x0"
|
|
28
|
+
`,
|
|
29
|
+
"sources/hello_world.move": `
|
|
30
|
+
module hello_world::hello_world {
|
|
31
|
+
// your code...
|
|
32
|
+
}
|
|
33
|
+
`,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// 3) Compile
|
|
37
|
+
const result = await buildMovePackage({ files, dependencies: {} });
|
|
38
|
+
|
|
39
|
+
if (result.success) {
|
|
40
|
+
console.log(result.digest);
|
|
41
|
+
console.log(result.modules); // Base64-encoded Move modules
|
|
42
|
+
} else {
|
|
43
|
+
console.error(result.error);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Resolving dependencies from GitHub (optional)
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { resolve, GitHubFetcher } from "@zktx.io/sui-move-builder";
|
|
51
|
+
|
|
52
|
+
const resolution = await resolve(
|
|
53
|
+
files["Move.toml"],
|
|
54
|
+
files,
|
|
55
|
+
new GitHubFetcher()
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const filesJson =
|
|
59
|
+
typeof resolution.files === "string"
|
|
60
|
+
? JSON.parse(resolution.files)
|
|
61
|
+
: resolution.files;
|
|
62
|
+
const depsJson =
|
|
63
|
+
typeof resolution.dependencies === "string"
|
|
64
|
+
? JSON.parse(resolution.dependencies)
|
|
65
|
+
: resolution.dependencies;
|
|
66
|
+
|
|
67
|
+
const result = await buildMovePackage({
|
|
68
|
+
files: filesJson,
|
|
69
|
+
dependencies: depsJson,
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`buildMovePackage` returns:
|
|
74
|
+
|
|
75
|
+
- `success: true | false`
|
|
76
|
+
- on success: `modules` (Base64), `dependencies`, `digest`
|
|
77
|
+
- on failure: `error` with compiler logs
|
|
78
|
+
|
|
79
|
+
## Local test page
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
npm run serve:test # serves ./test via python -m http.server
|
|
83
|
+
# open http://localhost:8000/test/index.html
|
|
84
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Fetcher: () => Fetcher,
|
|
34
|
+
GitHubFetcher: () => GitHubFetcher,
|
|
35
|
+
Resolver: () => Resolver,
|
|
36
|
+
buildMovePackage: () => buildMovePackage,
|
|
37
|
+
compileRaw: () => compileRaw,
|
|
38
|
+
getSuiMoveVersion: () => getSuiMoveVersion,
|
|
39
|
+
getSuiVersion: () => getSuiVersion,
|
|
40
|
+
getWasmBindings: () => getWasmBindings,
|
|
41
|
+
initMoveCompiler: () => initMoveCompiler,
|
|
42
|
+
parseToml: () => parseToml,
|
|
43
|
+
resolve: () => resolve
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
46
|
+
|
|
47
|
+
// src/tomlParser.ts
|
|
48
|
+
function stripInlineComment(line) {
|
|
49
|
+
let inQuote = false;
|
|
50
|
+
let quoteChar = "";
|
|
51
|
+
for (let i = 0; i < line.length; i++) {
|
|
52
|
+
const ch = line[i];
|
|
53
|
+
if ((ch === '"' || ch === "'") && (!inQuote || ch === quoteChar)) {
|
|
54
|
+
inQuote = !inQuote;
|
|
55
|
+
quoteChar = ch;
|
|
56
|
+
}
|
|
57
|
+
if (!inQuote && ch === "#") {
|
|
58
|
+
return line.slice(0, i);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return line;
|
|
62
|
+
}
|
|
63
|
+
function parseScalar(value) {
|
|
64
|
+
const trimmed = value.trim();
|
|
65
|
+
if (!trimmed) return "";
|
|
66
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
67
|
+
return trimmed.slice(1, -1);
|
|
68
|
+
}
|
|
69
|
+
if (trimmed === "true") return true;
|
|
70
|
+
if (trimmed === "false") return false;
|
|
71
|
+
const num = Number(trimmed);
|
|
72
|
+
if (!Number.isNaN(num)) return num;
|
|
73
|
+
return trimmed;
|
|
74
|
+
}
|
|
75
|
+
function parseInlineTable(value) {
|
|
76
|
+
const result = {};
|
|
77
|
+
const inner = value.trim().replace(/^\{/, "").replace(/\}$/, "");
|
|
78
|
+
let current = "";
|
|
79
|
+
let inQuote = false;
|
|
80
|
+
let quoteChar = "";
|
|
81
|
+
const parts = [];
|
|
82
|
+
for (let i = 0; i < inner.length; i++) {
|
|
83
|
+
const ch = inner[i];
|
|
84
|
+
if ((ch === '"' || ch === "'") && (!inQuote || ch === quoteChar)) {
|
|
85
|
+
inQuote = !inQuote;
|
|
86
|
+
quoteChar = ch;
|
|
87
|
+
}
|
|
88
|
+
if (!inQuote && ch === ",") {
|
|
89
|
+
parts.push(current);
|
|
90
|
+
current = "";
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
current += ch;
|
|
94
|
+
}
|
|
95
|
+
if (current.trim()) {
|
|
96
|
+
parts.push(current);
|
|
97
|
+
}
|
|
98
|
+
for (const part of parts) {
|
|
99
|
+
const eq = part.indexOf("=");
|
|
100
|
+
if (eq === -1) continue;
|
|
101
|
+
const key = part.slice(0, eq).trim();
|
|
102
|
+
const val = part.slice(eq + 1).trim();
|
|
103
|
+
result[key] = parseScalar(val);
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
function parseToml(content) {
|
|
108
|
+
const result = {
|
|
109
|
+
package: {},
|
|
110
|
+
dependencies: {},
|
|
111
|
+
addresses: {}
|
|
112
|
+
};
|
|
113
|
+
let section = null;
|
|
114
|
+
const lines = content.split(/\r?\n/);
|
|
115
|
+
for (const rawLine of lines) {
|
|
116
|
+
const line = stripInlineComment(rawLine).trim();
|
|
117
|
+
if (!line) continue;
|
|
118
|
+
const sectionMatch = line.match(/^\[([^\]]+)\]$/);
|
|
119
|
+
if (sectionMatch) {
|
|
120
|
+
section = sectionMatch[1].trim();
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const eq = line.indexOf("=");
|
|
124
|
+
if (eq === -1 || !section) continue;
|
|
125
|
+
const key = line.slice(0, eq).trim();
|
|
126
|
+
const value = line.slice(eq + 1).trim();
|
|
127
|
+
if (section === "package") {
|
|
128
|
+
result.package[key.replace(/-/g, "_")] = parseScalar(value);
|
|
129
|
+
} else if (section === "dependencies") {
|
|
130
|
+
if (value.startsWith("{")) {
|
|
131
|
+
result.dependencies[key] = parseInlineTable(value);
|
|
132
|
+
} else {
|
|
133
|
+
result.dependencies[key] = parseScalar(value);
|
|
134
|
+
}
|
|
135
|
+
} else if (section === "addresses") {
|
|
136
|
+
result.addresses[key] = parseScalar(value);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/resolver.ts
|
|
143
|
+
var Resolver = class {
|
|
144
|
+
constructor(fetcher) {
|
|
145
|
+
this.fetcher = fetcher;
|
|
146
|
+
this.globalAddresses = {};
|
|
147
|
+
this.visited = /* @__PURE__ */ new Set();
|
|
148
|
+
this.dependencyFiles = {};
|
|
149
|
+
this.systemDepsLoaded = /* @__PURE__ */ new Set();
|
|
150
|
+
}
|
|
151
|
+
async resolve(rootMoveToml, rootFiles) {
|
|
152
|
+
const parsedToml = parseToml(rootMoveToml);
|
|
153
|
+
if (parsedToml.addresses) {
|
|
154
|
+
this.mergeAddresses(parsedToml.addresses);
|
|
155
|
+
}
|
|
156
|
+
if (parsedToml.dependencies) {
|
|
157
|
+
await this.resolveDeps(parsedToml.dependencies);
|
|
158
|
+
await this.injectSystemDepsFromRoot(parsedToml.dependencies);
|
|
159
|
+
}
|
|
160
|
+
const finalMoveToml = this.reconstructMoveToml(parsedToml, this.globalAddresses);
|
|
161
|
+
const finalFiles = { ...rootFiles, "Move.toml": finalMoveToml };
|
|
162
|
+
return {
|
|
163
|
+
files: JSON.stringify(finalFiles),
|
|
164
|
+
dependencies: JSON.stringify(this.dependencyFiles)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
mergeAddresses(addresses) {
|
|
168
|
+
for (const [name, val] of Object.entries(addresses)) {
|
|
169
|
+
this.globalAddresses[name] = this.normalizeAddress(val);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
normalizeAddress(addr) {
|
|
173
|
+
if (!addr) return addr;
|
|
174
|
+
let clean = addr;
|
|
175
|
+
if (clean.startsWith("0x")) clean = clean.slice(2);
|
|
176
|
+
if (/^[0-9a-fA-F]+$/.test(clean)) {
|
|
177
|
+
return "0x" + clean.padStart(64, "0");
|
|
178
|
+
}
|
|
179
|
+
return addr;
|
|
180
|
+
}
|
|
181
|
+
async resolveDeps(depsObj, parentContext = null) {
|
|
182
|
+
for (const [name, depInfo] of Object.entries(depsObj)) {
|
|
183
|
+
let gitUrl;
|
|
184
|
+
let rev;
|
|
185
|
+
let subdir;
|
|
186
|
+
if (depInfo.git) {
|
|
187
|
+
gitUrl = depInfo.git;
|
|
188
|
+
rev = depInfo.rev;
|
|
189
|
+
subdir = depInfo.subdir;
|
|
190
|
+
} else if (depInfo.local) {
|
|
191
|
+
if (!parentContext) continue;
|
|
192
|
+
gitUrl = parentContext.git;
|
|
193
|
+
rev = parentContext.rev;
|
|
194
|
+
subdir = this.resolvePath(parentContext.subdir || "", depInfo.local);
|
|
195
|
+
} else {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const cacheKey = `${gitUrl}|${rev}|${subdir || ""}`;
|
|
199
|
+
if (this.visited.has(cacheKey)) continue;
|
|
200
|
+
this.visited.add(cacheKey);
|
|
201
|
+
const files = await this.fetcher.fetch(gitUrl, rev, subdir);
|
|
202
|
+
let pkgMoveToml = null;
|
|
203
|
+
for (const [path, content] of Object.entries(files)) {
|
|
204
|
+
if (path.endsWith("Move.toml")) {
|
|
205
|
+
pkgMoveToml = content;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (pkgMoveToml) {
|
|
210
|
+
const parsed = parseToml(pkgMoveToml);
|
|
211
|
+
if (parsed.addresses) {
|
|
212
|
+
this.mergeAddresses(parsed.addresses);
|
|
213
|
+
}
|
|
214
|
+
if (parsed.dependencies) {
|
|
215
|
+
await this.resolveDeps(parsed.dependencies, { git: gitUrl, rev, subdir });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const [path, content] of Object.entries(files)) {
|
|
219
|
+
if (path.endsWith(".move") || path.endsWith("Move.toml")) {
|
|
220
|
+
const targetPath = `dependencies/${name}/${path}`;
|
|
221
|
+
this.dependencyFiles[targetPath] = content;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
resolvePath(base, relative) {
|
|
227
|
+
const stack = base.split("/").filter((p) => p && p !== ".");
|
|
228
|
+
const parts = relative.split("/").filter((p) => p && p !== ".");
|
|
229
|
+
for (const part of parts) {
|
|
230
|
+
if (part === "..") {
|
|
231
|
+
stack.pop();
|
|
232
|
+
} else {
|
|
233
|
+
stack.push(part);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return stack.join("/");
|
|
237
|
+
}
|
|
238
|
+
reconstructMoveToml(originalParsed, addresses) {
|
|
239
|
+
let newToml = `[package]
|
|
240
|
+
name = "${originalParsed.package.name}"
|
|
241
|
+
version = "${originalParsed.package.version}"
|
|
242
|
+
`;
|
|
243
|
+
if (originalParsed.package.edition) {
|
|
244
|
+
newToml += `edition = "${originalParsed.package.edition}"
|
|
245
|
+
`;
|
|
246
|
+
}
|
|
247
|
+
newToml += `
|
|
248
|
+
[dependencies]
|
|
249
|
+
`;
|
|
250
|
+
if (originalParsed.dependencies) {
|
|
251
|
+
for (const [name, info] of Object.entries(originalParsed.dependencies)) {
|
|
252
|
+
newToml += `${name} = { git = "${info.git}", rev = "${info.rev}" }
|
|
253
|
+
`;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
newToml += `
|
|
257
|
+
[addresses]
|
|
258
|
+
`;
|
|
259
|
+
for (const [addrName, addrVal] of Object.entries(addresses)) {
|
|
260
|
+
newToml += `${addrName} = "${addrVal}"
|
|
261
|
+
`;
|
|
262
|
+
}
|
|
263
|
+
return newToml;
|
|
264
|
+
}
|
|
265
|
+
async injectSystemDepsFromRoot(depsObj) {
|
|
266
|
+
for (const depInfo of Object.values(depsObj)) {
|
|
267
|
+
if (!depInfo || !depInfo.git || !depInfo.rev) continue;
|
|
268
|
+
if (!this.isSuiRepo(depInfo.git)) continue;
|
|
269
|
+
await this.addImplicitSystemDepsForRepo(depInfo.git, depInfo.rev);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
async addImplicitSystemDepsForRepo(gitUrl, rev) {
|
|
274
|
+
if (!this.isSuiRepo(gitUrl)) return;
|
|
275
|
+
const cacheKey = `${gitUrl}|${rev}`;
|
|
276
|
+
if (this.systemDepsLoaded.has(cacheKey)) return;
|
|
277
|
+
this.systemDepsLoaded.add(cacheKey);
|
|
278
|
+
const manifestPath = "crates/sui-framework-snapshot/manifest.json";
|
|
279
|
+
if (!this.fetcher.fetchFile) return;
|
|
280
|
+
let packages = null;
|
|
281
|
+
try {
|
|
282
|
+
const manifestText = await this.fetcher.fetchFile(gitUrl, rev, manifestPath);
|
|
283
|
+
if (manifestText) {
|
|
284
|
+
const manifest = JSON.parse(manifestText);
|
|
285
|
+
const versions = Object.keys(manifest).map((v) => Number(v)).filter((v) => !Number.isNaN(v)).sort((a, b) => a - b);
|
|
286
|
+
const latestVersion = versions[versions.length - 1];
|
|
287
|
+
const latest = manifest[String(latestVersion)];
|
|
288
|
+
if (latest && latest.packages) {
|
|
289
|
+
packages = latest.packages;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
} catch (e) {
|
|
293
|
+
}
|
|
294
|
+
if (!packages) {
|
|
295
|
+
packages = [
|
|
296
|
+
{ name: "MoveStdlib", id: "0x1" },
|
|
297
|
+
{ name: "Sui", id: "0x2" },
|
|
298
|
+
{ name: "SuiSystem", id: "0x3" },
|
|
299
|
+
{ name: "Bridge", id: "0xb" }
|
|
300
|
+
];
|
|
301
|
+
}
|
|
302
|
+
for (const pkg of packages) {
|
|
303
|
+
if (!pkg || !pkg.name || !pkg.id) continue;
|
|
304
|
+
if (pkg.name === "DeepBook") continue;
|
|
305
|
+
const targetPath = `dependencies/${pkg.name}/Move.toml`;
|
|
306
|
+
if (this.dependencyFiles[targetPath]) continue;
|
|
307
|
+
const moveToml = [
|
|
308
|
+
"[package]",
|
|
309
|
+
`name = "${pkg.name}"`,
|
|
310
|
+
'version = "0.0.0"',
|
|
311
|
+
`published-at = "${pkg.id}"`,
|
|
312
|
+
""
|
|
313
|
+
].join("\n");
|
|
314
|
+
this.dependencyFiles[targetPath] = moveToml;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
isSuiRepo(gitUrl) {
|
|
318
|
+
return gitUrl.includes("github.com/MystenLabs/sui");
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
async function resolve(rootMoveTomlContent, rootSourceFiles, fetcher) {
|
|
322
|
+
const resolver = new Resolver(fetcher);
|
|
323
|
+
return resolver.resolve(rootMoveTomlContent, rootSourceFiles);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/fetcher.ts
|
|
327
|
+
var Fetcher = class {
|
|
328
|
+
/** Fetch a package. Return map of path -> content. */
|
|
329
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
330
|
+
async fetch(_gitUrl, _rev, _subdir) {
|
|
331
|
+
throw new Error("Not implemented");
|
|
332
|
+
}
|
|
333
|
+
/** Fetch a single file from a repository. */
|
|
334
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
335
|
+
async fetchFile(_gitUrl, _rev, _path) {
|
|
336
|
+
throw new Error("Not implemented");
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
var GitHubFetcher = class extends Fetcher {
|
|
340
|
+
constructor() {
|
|
341
|
+
super();
|
|
342
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
343
|
+
}
|
|
344
|
+
async fetch(gitUrl, rev, subdir) {
|
|
345
|
+
const { owner, repo } = this.parseGitUrl(gitUrl);
|
|
346
|
+
if (!owner || !repo) {
|
|
347
|
+
throw new Error(`Invalid git URL: ${gitUrl}`);
|
|
348
|
+
}
|
|
349
|
+
const treeUrl = `https://api.github.com/repos/${owner}/${repo}/git/trees/${rev}?recursive=1`;
|
|
350
|
+
let treeData;
|
|
351
|
+
try {
|
|
352
|
+
const resp = await fetch(treeUrl);
|
|
353
|
+
if (!resp.ok) {
|
|
354
|
+
if (resp.status === 403 || resp.status === 429) {
|
|
355
|
+
throw new Error("GitHub API rate limit exceeded.");
|
|
356
|
+
}
|
|
357
|
+
throw new Error(`Failed to fetch tree: ${resp.statusText}`);
|
|
358
|
+
}
|
|
359
|
+
treeData = await resp.json();
|
|
360
|
+
} catch (e) {
|
|
361
|
+
return {};
|
|
362
|
+
}
|
|
363
|
+
const files = {};
|
|
364
|
+
const fetchPromises = [];
|
|
365
|
+
for (const item of treeData.tree) {
|
|
366
|
+
if (item.type !== "blob") continue;
|
|
367
|
+
let relativePath = item.path;
|
|
368
|
+
if (subdir) {
|
|
369
|
+
if (!item.path.startsWith(subdir)) continue;
|
|
370
|
+
relativePath = item.path.slice(subdir.length);
|
|
371
|
+
if (relativePath.startsWith("/")) {
|
|
372
|
+
relativePath = relativePath.slice(1);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (!relativePath.endsWith(".move") && relativePath !== "Move.toml") {
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
const rawUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${rev}/${item.path}`;
|
|
379
|
+
const p = this.fetchContent(rawUrl).then((content) => {
|
|
380
|
+
if (content) {
|
|
381
|
+
files[relativePath] = content;
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
fetchPromises.push(p);
|
|
385
|
+
}
|
|
386
|
+
await Promise.all(fetchPromises);
|
|
387
|
+
return files;
|
|
388
|
+
}
|
|
389
|
+
async fetchFile(gitUrl, rev, path) {
|
|
390
|
+
const { owner, repo } = this.parseGitUrl(gitUrl);
|
|
391
|
+
if (!owner || !repo) {
|
|
392
|
+
throw new Error(`Invalid git URL: ${gitUrl}`);
|
|
393
|
+
}
|
|
394
|
+
const rawUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${rev}/${path}`;
|
|
395
|
+
return this.fetchContent(rawUrl);
|
|
396
|
+
}
|
|
397
|
+
async fetchContent(url) {
|
|
398
|
+
if (this.cache.has(url)) {
|
|
399
|
+
return this.cache.get(url) ?? null;
|
|
400
|
+
}
|
|
401
|
+
try {
|
|
402
|
+
const resp = await fetch(url);
|
|
403
|
+
if (!resp.ok) return null;
|
|
404
|
+
const text = await resp.text();
|
|
405
|
+
this.cache.set(url, text);
|
|
406
|
+
return text;
|
|
407
|
+
} catch (e) {
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
parseGitUrl(url) {
|
|
412
|
+
try {
|
|
413
|
+
const urlObj = new URL(url);
|
|
414
|
+
const parts = urlObj.pathname.split("/").filter((p) => p);
|
|
415
|
+
if (parts.length >= 2) {
|
|
416
|
+
let repo = parts[1];
|
|
417
|
+
if (repo.endsWith(".git")) {
|
|
418
|
+
repo = repo.slice(0, -4);
|
|
419
|
+
}
|
|
420
|
+
return { owner: parts[0], repo };
|
|
421
|
+
}
|
|
422
|
+
} catch (e) {
|
|
423
|
+
}
|
|
424
|
+
return { owner: null, repo: null };
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// src/index.ts
|
|
429
|
+
var import_meta = {};
|
|
430
|
+
var wasmUrl = (() => {
|
|
431
|
+
try {
|
|
432
|
+
return new URL("./sui_move_wasm_bg.wasm", import_meta.url);
|
|
433
|
+
} catch {
|
|
434
|
+
return "./sui_move_wasm_bg.wasm";
|
|
435
|
+
}
|
|
436
|
+
})();
|
|
437
|
+
var wasmReady;
|
|
438
|
+
async function loadWasm(customWasm) {
|
|
439
|
+
if (!wasmReady) {
|
|
440
|
+
wasmReady = import("./sui_move_wasm.js").then(async (mod) => {
|
|
441
|
+
await mod.default(customWasm ?? wasmUrl);
|
|
442
|
+
return mod;
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
return wasmReady;
|
|
446
|
+
}
|
|
447
|
+
function toJson(value) {
|
|
448
|
+
return JSON.stringify(value ?? {});
|
|
449
|
+
}
|
|
450
|
+
function asFailure(err) {
|
|
451
|
+
const msg = err instanceof Error ? err.message : typeof err === "string" ? err : "Unknown error";
|
|
452
|
+
return { success: false, error: msg };
|
|
453
|
+
}
|
|
454
|
+
function ensureCompileResult(result) {
|
|
455
|
+
if (typeof result !== "object" || result === null) {
|
|
456
|
+
throw new Error("Unexpected compile result shape from wasm");
|
|
457
|
+
}
|
|
458
|
+
const asAny = result;
|
|
459
|
+
if (typeof asAny.success === "function" && typeof asAny.output === "function") {
|
|
460
|
+
return asAny;
|
|
461
|
+
}
|
|
462
|
+
if (typeof asAny.success === "boolean" && typeof asAny.output === "string") {
|
|
463
|
+
return {
|
|
464
|
+
success: () => asAny.success,
|
|
465
|
+
output: () => asAny.output
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
throw new Error("Unexpected compile result shape from wasm");
|
|
469
|
+
}
|
|
470
|
+
function parseCompileResult(output) {
|
|
471
|
+
const toHex = (bytes) => bytes.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
472
|
+
try {
|
|
473
|
+
const parsed = JSON.parse(output);
|
|
474
|
+
if (!parsed.modules || !parsed.dependencies || !parsed.digest) {
|
|
475
|
+
throw new Error("missing fields in compiler output");
|
|
476
|
+
}
|
|
477
|
+
const digestHex = typeof parsed.digest === "string" ? parsed.digest : toHex(parsed.digest);
|
|
478
|
+
return {
|
|
479
|
+
success: true,
|
|
480
|
+
modules: parsed.modules,
|
|
481
|
+
dependencies: parsed.dependencies,
|
|
482
|
+
digest: digestHex
|
|
483
|
+
};
|
|
484
|
+
} catch (error) {
|
|
485
|
+
return asFailure(error);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
async function initMoveCompiler(options) {
|
|
489
|
+
await loadWasm(options?.wasm);
|
|
490
|
+
}
|
|
491
|
+
async function buildMovePackage(input) {
|
|
492
|
+
try {
|
|
493
|
+
const mod = await loadWasm(input.wasm);
|
|
494
|
+
const raw = mod.compile(
|
|
495
|
+
toJson(input.files),
|
|
496
|
+
toJson(input.dependencies ?? {})
|
|
497
|
+
);
|
|
498
|
+
const result = ensureCompileResult(raw);
|
|
499
|
+
const ok = result.success();
|
|
500
|
+
const output = result.output();
|
|
501
|
+
if (!ok) {
|
|
502
|
+
return asFailure(output);
|
|
503
|
+
}
|
|
504
|
+
return parseCompileResult(output);
|
|
505
|
+
} catch (error) {
|
|
506
|
+
return asFailure(error);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
async function getSuiMoveVersion(options) {
|
|
510
|
+
const mod = await loadWasm(options?.wasm);
|
|
511
|
+
return mod.sui_move_version();
|
|
512
|
+
}
|
|
513
|
+
async function getSuiVersion(options) {
|
|
514
|
+
const mod = await loadWasm(options?.wasm);
|
|
515
|
+
return mod.sui_version();
|
|
516
|
+
}
|
|
517
|
+
async function getWasmBindings(options) {
|
|
518
|
+
return loadWasm(options?.wasm);
|
|
519
|
+
}
|
|
520
|
+
async function compileRaw(filesJson, depsJson, options) {
|
|
521
|
+
const mod = await loadWasm(options?.wasm);
|
|
522
|
+
const result = ensureCompileResult(mod.compile(filesJson, depsJson));
|
|
523
|
+
return {
|
|
524
|
+
success: result.success(),
|
|
525
|
+
output: result.output()
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
529
|
+
0 && (module.exports = {
|
|
530
|
+
Fetcher,
|
|
531
|
+
GitHubFetcher,
|
|
532
|
+
Resolver,
|
|
533
|
+
buildMovePackage,
|
|
534
|
+
compileRaw,
|
|
535
|
+
getSuiMoveVersion,
|
|
536
|
+
getSuiVersion,
|
|
537
|
+
getWasmBindings,
|
|
538
|
+
initMoveCompiler,
|
|
539
|
+
parseToml,
|
|
540
|
+
resolve
|
|
541
|
+
});
|
|
542
|
+
//# sourceMappingURL=index.cjs.map
|