Comparing version 2.0.7 to 2.0.8
'use strict'; | ||
module.exports = require('./lib')( | ||
require('./versions/latest/moc.min').Motoko, | ||
'latest', | ||
); | ||
module.exports = require('./lib/versions/moc').default; |
'use strict'; | ||
module.exports = require('./lib')( | ||
require('./versions/latest/moc.min').Motoko, | ||
'latest/interpreter', | ||
); | ||
module.exports = require('./lib/versions/interpreter').default; |
@@ -1,3 +0,4 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.file = void 0; | ||
function getValidPath(path) { | ||
@@ -15,4 +16,3 @@ if (typeof path !== 'string') { | ||
} | ||
exports.file = (mo, path) => { | ||
const file = (mo, path) => { | ||
path = getValidPath(path); | ||
@@ -48,3 +48,3 @@ const result = { | ||
check() { | ||
return mo.check(path, ...args); | ||
return mo.check(path); | ||
}, | ||
@@ -69,1 +69,3 @@ run() { | ||
}; | ||
exports.file = file; | ||
//# sourceMappingURL=file.js.map |
@@ -1,14 +0,19 @@ | ||
'use strict'; | ||
const { file } = require('./file'); | ||
const { /* findPackage, */ loadPackages } = require('./package'); | ||
module.exports = (compiler, version) => { | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const file_1 = require("./file"); | ||
const package_1 = require("./package"); | ||
function getMotoko(compiler, version) { | ||
const debug = require('debug')(version ? `motoko:${version}` : 'motoko'); | ||
const invoke = (key, unwrap, args) => { | ||
if (!compiler) { | ||
throw new Error( | ||
'Please load a Motoko compiler before running this function', | ||
); | ||
throw new Error('Please load a Motoko compiler before running this function'); | ||
} | ||
@@ -21,11 +26,10 @@ if (typeof compiler[key] !== 'function') { | ||
result = compiler[key](...args); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
if (err instanceof Error) { | ||
throw err; | ||
} | ||
throw new Error( | ||
`Unable to execute ${key}(${[...args] | ||
.map((x) => typeof x) | ||
.join(', ')}):\n${JSON.stringify(err)}`, | ||
); | ||
throw new Error(`Unable to execute ${key}(${[...args] | ||
.map((x) => typeof x) | ||
.join(', ')}):\n${JSON.stringify(err)}`); | ||
} | ||
@@ -36,13 +40,10 @@ if (!unwrap) { | ||
if (!result.code) { | ||
throw new Error( | ||
result.diagnostics | ||
? result.diagnostics | ||
.map(({ message }) => message) | ||
.join('; ') | ||
: '(no diagnostics)', | ||
); | ||
throw new Error(result.diagnostics | ||
? result.diagnostics | ||
.map(({ message }) => message) | ||
.join('; ') | ||
: '(no diagnostics)'); | ||
} | ||
return result.code; | ||
}; | ||
const mo = { | ||
@@ -52,7 +53,9 @@ version, | ||
file(path) { | ||
return file(mo, path); | ||
return (0, file_1.file)(mo, path); | ||
}, | ||
// findPackage, | ||
async loadPackages(packages) { | ||
return loadPackages(mo, packages); | ||
loadPackages(packages) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (0, package_1.loadPackages)(mo, packages); | ||
}); | ||
}, | ||
@@ -107,3 +110,4 @@ read(path) { | ||
mode = 'ic'; | ||
} else if (mode !== 'ic' && mode !== 'wasi') { | ||
} | ||
else if (mode !== 'ic' && mode !== 'wasi') { | ||
throw new Error(`Invalid WASM format: ${mode}`); | ||
@@ -114,12 +118,13 @@ } | ||
parseMotoko(content) { | ||
const ast = invoke('parseMotoko', true, [content]); | ||
return ast; | ||
return invoke('parseMotoko', true, [content]); | ||
}, | ||
parseCandid(content) { | ||
const ast = invoke('parseCandid', true, [content]); | ||
return ast; | ||
return invoke('parseCandid', true, [content]); | ||
}, | ||
}; | ||
// @ts-ignore | ||
mo.default = mo; | ||
return mo; | ||
}; | ||
exports.default = exports; | ||
} | ||
exports.default = getMotoko; | ||
//# sourceMappingURL=index.js.map |
@@ -1,41 +0,54 @@ | ||
'use strict'; | ||
"use strict"; | ||
// Derived from: https://github.com/dfinity/motoko-playground/blob/main/src/workers/file.ts | ||
const parse = require('isomorphic-parse-github-url'); | ||
const fetch = require('cross-fetch'); | ||
async function fetchPackage(mo, info) { | ||
if ( | ||
!info.repo.startsWith('https://github.com/') || | ||
!info.repo.endsWith('.git') | ||
) { | ||
return false; | ||
} | ||
const repo = { | ||
repo: info.repo.slice(0, -4).replace(/^(https:\/\/github.com\/)/, ''), | ||
branch: info.version, | ||
dir: info.dir || 'src', | ||
}; | ||
const result = await fetchGithub(mo, repo, info.name); | ||
if (result) { | ||
mo.addPackage(info.name, info.name + '/'); | ||
} | ||
return result ? true : false; | ||
} | ||
async function fetchGithub(mo, repo, directory = '') { | ||
const possiblyCDN = !( | ||
(repo.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(repo.branch)) || | ||
repo.branch === 'master' || | ||
repo.branch === 'main' | ||
); | ||
if (possiblyCDN) { | ||
const result = await fetchFromCDN(repo, directory); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.loadPackages = void 0; | ||
// @ts-ignore | ||
const isomorphic_parse_github_url_1 = __importDefault(require("isomorphic-parse-github-url")); | ||
const cross_fetch_1 = __importDefault(require("cross-fetch")); | ||
function fetchPackage(mo, info) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!info.repo.startsWith('https://github.com/') || | ||
!info.repo.endsWith('.git')) { | ||
return false; | ||
} | ||
const repo = { | ||
name: info.name, | ||
version: info.version, | ||
repo: info.repo.slice(0, -4).replace(/^(https:\/\/github.com\/)/, ''), | ||
branch: info.version, | ||
dir: info.dir || 'src', | ||
}; | ||
const result = yield fetchGithub(mo, repo, info.name); | ||
if (result) { | ||
return result; | ||
mo.addPackage(info.name, info.name + '/'); | ||
} | ||
} | ||
return await fetchFromGithub(mo, repo, directory); | ||
return result ? true : false; | ||
}); | ||
} | ||
function fetchGithub(mo, info, directory = '') { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const possiblyCDN = !((info.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(info.branch)) || | ||
info.branch === 'master' || | ||
info.branch === 'main'); | ||
if (possiblyCDN) { | ||
const result = yield fetchFromCDN(mo, info, directory); | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
return yield fetchFromGithub(mo, info, directory); | ||
}); | ||
} | ||
// function saveWorkplaceToMotoko(mo, files) { | ||
@@ -47,76 +60,69 @@ // for (const [name, code] of Object.entries(files)) { | ||
// } | ||
async function fetchFromCDN(mo, repo, directory = '') { | ||
const meta_url = `https://data.jsdelivr.com/v1/package/gh/${repo.repo}@${repo.branch}/flat`; | ||
const base_url = `https://cdn.jsdelivr.net/gh/${repo.repo}@${repo.branch}`; | ||
const response = await fetch(meta_url); | ||
const json = await response.json(); | ||
if (!json.hasOwnProperty('files')) { | ||
throw new Error(json.message || `Could not fetch from CDN: ${repo}`); | ||
} | ||
const promises = []; | ||
const files = {}; | ||
for (const f of json.files) { | ||
if (f.name.startsWith(`/${repo.dir}/`) && /\.mo$/.test(f.name)) { | ||
const promise = (async () => { | ||
const content = await (await fetch(base_url + f.name)).text(); | ||
const stripped = | ||
directory + | ||
f.name.slice(repo.dir ? repo.dir.length + 1 : 0); | ||
mo.write(stripped, content); | ||
files[stripped] = content; | ||
})(); | ||
promises.push(promise); | ||
function fetchFromCDN(mo, info, directory = '') { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const meta_url = `https://data.jsdelivr.com/v1/package/gh/${info.repo}@${info.branch}/flat`; | ||
const base_url = `https://cdn.jsdelivr.net/gh/${info.repo}@${info.branch}`; | ||
const response = yield (0, cross_fetch_1.default)(meta_url); | ||
const json = yield response.json(); | ||
if (!json.hasOwnProperty('files')) { | ||
throw new Error(json.message || `Could not fetch from CDN: ${info}`); | ||
} | ||
} | ||
if (!promises.length) { | ||
return; | ||
} | ||
return Promise.all(promises).then(() => { | ||
return files; | ||
const promises = []; | ||
const files = {}; | ||
for (const f of json.files) { | ||
if (f.name.startsWith(`/${info.dir}/`) && /\.mo$/.test(f.name)) { | ||
const promise = (() => __awaiter(this, void 0, void 0, function* () { | ||
const content = yield (yield (0, cross_fetch_1.default)(base_url + f.name)).text(); | ||
const stripped = directory + | ||
f.name.slice(info.dir ? info.dir.length + 1 : 0); | ||
mo.write(stripped, content); | ||
files[stripped] = content; | ||
}))(); | ||
promises.push(promise); | ||
} | ||
} | ||
if (!promises.length) { | ||
return; | ||
} | ||
return Promise.all(promises).then(() => { | ||
return files; | ||
}); | ||
}); | ||
} | ||
async function fetchFromGithub(mo, repo, directory = '') { | ||
const meta_url = `https://api.github.com/repos/${repo.repo}/git/trees/${repo.branch}?recursive=1`; | ||
const base_url = `https://raw.githubusercontent.com/${repo.repo}/${repo.branch}/`; | ||
const response = await fetch(meta_url); | ||
const json = await response.json(); | ||
if (!json.hasOwnProperty('tree')) { | ||
throw new Error( | ||
json.message || `Could not fetch from GitHub repository: ${repo}`, | ||
); | ||
} | ||
const promises = []; | ||
const files = {}; | ||
for (const f of json.tree) { | ||
if ( | ||
f.path.startsWith(repo.dir ? `${repo.dir}/` : '') && | ||
f.type === 'blob' && | ||
/\.mo$/.test(f.path) | ||
) { | ||
const promise = (async () => { | ||
const content = await (await fetch(base_url + f.path)).text(); | ||
const stripped = | ||
directory + | ||
(directory ? '/' : '') + | ||
f.path.slice(repo.dir ? repo.dir.length + 1 : 0); | ||
mo.write(stripped, content); | ||
files[stripped] = content; | ||
})(); | ||
promises.push(promise); | ||
function fetchFromGithub(mo, info, directory = '') { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const meta_url = `https://api.github.com/repos/${info.repo}/git/trees/${info.branch}?recursive=1`; | ||
const base_url = `https://raw.githubusercontent.com/${info.repo}/${info.branch}/`; | ||
const response = yield (0, cross_fetch_1.default)(meta_url); | ||
const json = yield response.json(); | ||
if (!json.hasOwnProperty('tree')) { | ||
throw new Error(json.message || `Could not fetch from GitHub repository: ${info}`); | ||
} | ||
} | ||
if (!promises.length) { | ||
return; | ||
} | ||
return Promise.all(promises).then(() => { | ||
return files; | ||
const promises = []; | ||
const files = {}; | ||
for (const f of json.tree) { | ||
if (f.path.startsWith(info.dir ? `${info.dir}/` : '') && | ||
f.type === 'blob' && | ||
/\.mo$/.test(f.path)) { | ||
const promise = (() => __awaiter(this, void 0, void 0, function* () { | ||
const content = yield (yield (0, cross_fetch_1.default)(base_url + f.path)).text(); | ||
const stripped = directory + | ||
(directory ? '/' : '') + | ||
f.path.slice(info.dir ? info.dir.length + 1 : 0); | ||
mo.write(stripped, content); | ||
files[stripped] = content; | ||
}))(); | ||
promises.push(promise); | ||
} | ||
} | ||
if (!promises.length) { | ||
return; | ||
} | ||
return Promise.all(promises).then(() => { | ||
return files; | ||
}); | ||
}); | ||
} | ||
// async function resolve(path) { | ||
// } | ||
function parseGithubPackage(path, name) { | ||
@@ -129,15 +135,13 @@ if (!path) { | ||
} | ||
let result; | ||
try { | ||
result = parse(path); | ||
result = (0, isomorphic_parse_github_url_1.default)(path); | ||
if (!result) { | ||
return; | ||
} | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
console.warn(err); | ||
} | ||
const { name: repoName, filepath, branch, owner } = result; | ||
return { | ||
@@ -151,16 +155,17 @@ name: name || repoName, | ||
} | ||
module.exports = { | ||
// async findPackage(package) { | ||
// if (typeof package === 'string') { | ||
// return resolve(package); | ||
// } | ||
// return package; | ||
// }, | ||
loadPackages: async (mo, packages) => { | ||
for (const [name, path] of Object.entries(packages)) { | ||
function loadPackages(mo, packages) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield Promise.all(Object.entries(packages).map(([name, path]) => { | ||
const info = parseGithubPackage(path, name); | ||
return fetchPackage(mo, info); | ||
} | ||
}, | ||
}; | ||
})); | ||
}); | ||
} | ||
exports.loadPackages = loadPackages; | ||
// export async function findPackage(package) { | ||
// if (typeof package === 'string') { | ||
// return resolve(package); | ||
// } | ||
// return package; | ||
// }, | ||
//# sourceMappingURL=package.js.map |
{ | ||
"name": "motoko", | ||
"version": "2.0.7", | ||
"version": "2.0.8", | ||
"description": "Compile Motoko smart contracts in Node.js and the browser.", | ||
@@ -8,2 +8,3 @@ "author": "Ryan Vandersmith (https://github.com/rvanasa)", | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"repository": { | ||
@@ -14,6 +15,8 @@ "type": "git", | ||
"scripts": { | ||
"build": "rimraf ./lib && tsc -p .", | ||
"prepare": "husky install", | ||
"generate": "node utils/generate", | ||
"test": "jest", | ||
"precommit": "lint-staged" | ||
"precommit": "lint-staged", | ||
"prepublishOnly": "run-s build test" | ||
}, | ||
@@ -25,2 +28,3 @@ "dependencies": { | ||
"devDependencies": { | ||
"@types/jest": "^28.1.3", | ||
"@wasmer/wasi": "^1.0.2", | ||
@@ -32,3 +36,7 @@ "cross-env": "^7.0.3", | ||
"lint-staged": "^13.0.3", | ||
"prettier": "^2.7.1" | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.7.1", | ||
"ts-jest": "^28.0.8", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.2" | ||
}, | ||
@@ -47,5 +55,6 @@ "lint-staged": { | ||
"interpreter.js", | ||
"lib", | ||
"contrib", | ||
"versions/latest" | ||
"src/**/*", | ||
"lib/**/*", | ||
"contrib/**/*", | ||
"versions/latest/**/*" | ||
], | ||
@@ -52,0 +61,0 @@ "keywords": [ |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
3423181
35
1181
0
12