motoko 2.0.6 → 2.0.9

Sign up to get free protection for your applications and to get access to all the features.
package/lib/package.js CHANGED
@@ -1,123 +1,129 @@
1
- 'use strict';
1
+ "use strict";
2
2
  // Derived from: https://github.com/dfinity/motoko-playground/blob/main/src/workers/file.ts
3
-
4
- const parse = require('isomorphic-parse-github-url');
5
- const fetch = require('cross-fetch');
6
-
7
- async function fetchPackage(mo, info) {
8
- if (
9
- !info.repo.startsWith('https://github.com/') ||
10
- !info.repo.endsWith('.git')
11
- ) {
12
- return false;
13
- }
14
- const repo = {
15
- repo: info.repo.slice(0, -4).replace(/^(https:\/\/github.com\/)/, ''),
16
- branch: info.version,
17
- dir: info.dir || 'src',
18
- };
19
- const result = await fetchGithub(mo, repo, info.name);
20
- if (result) {
21
- mo.addPackage(info.name, info.name + '/');
22
- }
23
- return result ? true : false;
24
- }
25
-
26
- async function fetchGithub(mo, repo, directory = '') {
27
- const possiblyCDN = !(
28
- (repo.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(repo.branch)) ||
29
- repo.branch === 'master' ||
30
- repo.branch === 'main'
31
- );
32
- if (possiblyCDN) {
33
- const result = await fetchFromCDN(repo, directory);
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ var __importDefault = (this && this.__importDefault) || function (mod) {
13
+ return (mod && mod.__esModule) ? mod : { "default": mod };
14
+ };
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.loadPackages = void 0;
17
+ // @ts-ignore
18
+ const isomorphic_parse_github_url_1 = __importDefault(require("isomorphic-parse-github-url"));
19
+ const cross_fetch_1 = __importDefault(require("cross-fetch"));
20
+ function fetchPackage(mo, info) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ if (!info.repo.startsWith('https://github.com/') ||
23
+ !info.repo.endsWith('.git')) {
24
+ return false;
25
+ }
26
+ const repo = {
27
+ name: info.name,
28
+ version: info.version,
29
+ repo: info.repo.slice(0, -4).replace(/^(https:\/\/github.com\/)/, ''),
30
+ branch: info.version,
31
+ dir: info.dir || 'src',
32
+ };
33
+ const result = yield fetchGithub(mo, repo, info.name);
34
34
  if (result) {
35
- return result;
35
+ mo.addPackage(info.name, info.name + '/');
36
36
  }
37
- }
38
- return await fetchFromGithub(mo, repo, directory);
37
+ return result ? true : false;
38
+ });
39
+ }
40
+ function fetchGithub(mo, info, directory = '') {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const possiblyCDN = !((info.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(info.branch)) ||
43
+ info.branch === 'master' ||
44
+ info.branch === 'main');
45
+ if (possiblyCDN) {
46
+ const result = yield fetchFromCDN(mo, info, directory);
47
+ if (result) {
48
+ return result;
49
+ }
50
+ }
51
+ return yield fetchFromGithub(mo, info, directory);
52
+ });
39
53
  }
40
-
41
54
  // function saveWorkplaceToMotoko(mo, files) {
42
55
  // for (const [name, code] of Object.entries(files)) {
43
56
  // if (!name.endsWith('mo')) continue;
44
57
  // mo.addFile(name, code);
45
58
  // }
46
59
  // }
47
-
48
- async function fetchFromCDN(mo, repo, directory = '') {
49
- const meta_url = `https://data.jsdelivr.com/v1/package/gh/${repo.repo}@${repo.branch}/flat`;
50
- const base_url = `https://cdn.jsdelivr.net/gh/${repo.repo}@${repo.branch}`;
51
- const response = await fetch(meta_url);
52
- const json = await response.json();
53
- if (!json.hasOwnProperty('files')) {
54
- throw new Error(json.message || `Could not fetch from CDN: ${repo}`);
55
- }
56
- const promises = [];
57
- const files = {};
58
- for (const f of json.files) {
59
- if (f.name.startsWith(`/${repo.dir}/`) && /\.mo$/.test(f.name)) {
60
- const promise = (async () => {
61
- const content = await (await fetch(base_url + f.name)).text();
62
- const stripped =
63
- directory +
64
- f.name.slice(repo.dir ? repo.dir.length + 1 : 0);
65
- mo.write(stripped, content);
66
- files[stripped] = content;
67
- })();
68
- promises.push(promise);
60
+ function fetchFromCDN(mo, info, directory = '') {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const meta_url = `https://data.jsdelivr.com/v1/package/gh/${info.repo}@${info.branch}/flat`;
63
+ const base_url = `https://cdn.jsdelivr.net/gh/${info.repo}@${info.branch}`;
64
+ const response = yield (0, cross_fetch_1.default)(meta_url);
65
+ const json = yield response.json();
66
+ if (!json.hasOwnProperty('files')) {
67
+ throw new Error(json.message || `Could not fetch from CDN: ${info}`);
69
68
  }
70
- }
71
- if (!promises.length) {
72
- return;
73
- }
74
- return Promise.all(promises).then(() => {
75
- return files;
69
+ const promises = [];
70
+ const files = {};
71
+ for (const f of json.files) {
72
+ if (f.name.startsWith(`/${info.dir}/`) && /\.mo$/.test(f.name)) {
73
+ const promise = (() => __awaiter(this, void 0, void 0, function* () {
74
+ const content = yield (yield (0, cross_fetch_1.default)(base_url + f.name)).text();
75
+ const stripped = directory +
76
+ f.name.slice(info.dir ? info.dir.length + 1 : 0);
77
+ mo.write(stripped, content);
78
+ files[stripped] = content;
79
+ }))();
80
+ promises.push(promise);
81
+ }
82
+ }
83
+ if (!promises.length) {
84
+ return;
85
+ }
86
+ return Promise.all(promises).then(() => {
87
+ return files;
88
+ });
76
89
  });
77
90
  }
78
-
79
- async function fetchFromGithub(mo, repo, directory = '') {
80
- const meta_url = `https://api.github.com/repos/${repo.repo}/git/trees/${repo.branch}?recursive=1`;
81
- const base_url = `https://raw.githubusercontent.com/${repo.repo}/${repo.branch}/`;
82
- const response = await fetch(meta_url);
83
- const json = await response.json();
84
- if (!json.hasOwnProperty('tree')) {
85
- throw new Error(
86
- json.message || `Could not fetch from GitHub repository: ${repo}`,
87
- );
88
- }
89
- const promises = [];
90
- const files = {};
91
- for (const f of json.tree) {
92
- if (
93
- f.path.startsWith(repo.dir ? `${repo.dir}/` : '') &&
94
- f.type === 'blob' &&
95
- /\.mo$/.test(f.path)
96
- ) {
97
- const promise = (async () => {
98
- const content = await (await fetch(base_url + f.path)).text();
99
- const stripped =
100
- directory +
101
- (directory ? '/' : '') +
102
- f.path.slice(repo.dir ? repo.dir.length + 1 : 0);
103
- mo.write(stripped, content);
104
- files[stripped] = content;
105
- })();
106
- promises.push(promise);
91
+ function fetchFromGithub(mo, info, directory = '') {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ const meta_url = `https://api.github.com/repos/${info.repo}/git/trees/${info.branch}?recursive=1`;
94
+ const base_url = `https://raw.githubusercontent.com/${info.repo}/${info.branch}/`;
95
+ const response = yield (0, cross_fetch_1.default)(meta_url);
96
+ const json = yield response.json();
97
+ if (!json.hasOwnProperty('tree')) {
98
+ throw new Error(json.message || `Could not fetch from GitHub repository: ${info}`);
107
99
  }
108
- }
109
- if (!promises.length) {
110
- return;
111
- }
112
- return Promise.all(promises).then(() => {
113
- return files;
100
+ const promises = [];
101
+ const files = {};
102
+ for (const f of json.tree) {
103
+ if (f.path.startsWith(info.dir ? `${info.dir}/` : '') &&
104
+ f.type === 'blob' &&
105
+ /\.mo$/.test(f.path)) {
106
+ const promise = (() => __awaiter(this, void 0, void 0, function* () {
107
+ const content = yield (yield (0, cross_fetch_1.default)(base_url + f.path)).text();
108
+ const stripped = directory +
109
+ (directory ? '/' : '') +
110
+ f.path.slice(info.dir ? info.dir.length + 1 : 0);
111
+ mo.write(stripped, content);
112
+ files[stripped] = content;
113
+ }))();
114
+ promises.push(promise);
115
+ }
116
+ }
117
+ if (!promises.length) {
118
+ return;
119
+ }
120
+ return Promise.all(promises).then(() => {
121
+ return files;
122
+ });
114
123
  });
115
124
  }
116
-
117
125
  // async function resolve(path) {
118
-
119
126
  // }
120
-
121
127
  function parseGithubPackage(path, name) {
122
128
  if (!path) {
123
129
  return;
@@ -125,19 +131,17 @@ function parseGithubPackage(path, name) {
125
131
  if (typeof path === 'object') {
126
132
  return path;
127
133
  }
128
-
129
134
  let result;
130
135
  try {
131
- result = parse(path);
136
+ result = (0, isomorphic_parse_github_url_1.default)(path);
132
137
  if (!result) {
133
138
  return;
134
139
  }
135
- } catch (err) {
140
+ }
141
+ catch (err) {
136
142
  console.warn(err);
137
143
  }
138
-
139
144
  const { name: repoName, filepath, branch, owner } = result;
140
-
141
145
  return {
142
146
  name: name || repoName,
143
147
  repo: `https://github.com/${owner}/${repoName}.git`,
@@ -146,18 +150,19 @@ function parseGithubPackage(path, name) {
146
150
  // homepage: ,
147
151
  };
148
152
  }
149
-
150
- module.exports = {
151
- // async findPackage(package) {
152
- // if (typeof package === 'string') {
153
- // return resolve(package);
154
- // }
155
- // return package;
156
- // },
157
- loadPackages: async (mo, packages) => {
158
- for (const [name, path] of Object.entries(packages)) {
153
+ function loadPackages(mo, packages) {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ yield Promise.all(Object.entries(packages).map(([name, path]) => {
159
156
  const info = parseGithubPackage(path, name);
160
157
  return fetchPackage(mo, info);
161
- }
162
- },
163
- };
158
+ }));
159
+ });
160
+ }
161
+ exports.loadPackages = loadPackages;
162
+ // export async function findPackage(package) {
163
+ // if (typeof package === 'string') {
164
+ // return resolve(package);
165
+ // }
166
+ // return package;
167
+ // },
168
+ //# sourceMappingURL=package.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.js","sourceRoot":"","sources":["../src/package.ts"],"names":[],"mappings":";AAAA,2FAA2F;;;;;;;;;;;;;;;AAE3F,aAAa;AACb,8FAA+D;AAC/D,8DAAgC;AAWhC,SAAe,YAAY,CAAC,EAAU,EAAE,IAAiB;;QACrD,IACI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC;YAC5C,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,C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C;QACrB,IAAI,CAAC,MAAM,EAAE;YACT,OAAO;SACV;KACJ;IAAC,OAAO,GAAG,EAAE;QACV,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACrB;IAED,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IAE3D,OAAO;QACH,IAAI,EAAE,IAAI,IAAI,QAAQ;QACtB,IAAI,EAAE,sBAAsB,KAAK,IAAI,QAAQ,MAAM;QACnD,OAAO,EAAE,MAAM;QACf,GAAG,EAAE,QAAQ;QACb,cAAc;KACjB,CAAC;AACN,CAAC;AAED,SAAsB,YAAY,CAC9B,EAAU,EACV,QAA8C;;QAE9C,MAAM,OAAO,CAAC,GAAG,CACb,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE;YAC1C,MAAM,IAAI,GAAG,kBAAkB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAC5C,OAAO,YAAY,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC,CAAC,CACL,CAAC;IACN,CAAC;CAAA;AAVD,oCAUC;AAED,+CAA+C;AAC/C,yCAAyC;AACzC,mCAAmC;AACnC,QAAQ;AACR,sBAAsB;AACtB,KAAK"}
@@ -0,0 +1,45 @@
1
+ declare const _default: {
2
+ version: string;
3
+ compiler: any;
4
+ file(path: string): {
5
+ readonly path: string;
6
+ clone(): any;
7
+ read(): string;
8
+ write(content: string): void;
9
+ rename(newPath: string): void;
10
+ delete(): void;
11
+ list(): string[];
12
+ check(): import("..").Diagnostic[];
13
+ run(): {
14
+ stdout: string;
15
+ stderr: string;
16
+ result: string | number;
17
+ };
18
+ candid(): string;
19
+ wasm(mode: import("..").WasmMode): any;
20
+ parseMotoko(): object;
21
+ parseCandid(): object;
22
+ };
23
+ loadPackages(packages: Record<string, string | import("../package").PackageInfo>): Promise<void>;
24
+ read(path: string): string;
25
+ write(path: string, content?: string): void;
26
+ rename(path: string, newPath: string): void;
27
+ delete(path: string): void;
28
+ list(directory: string): string[];
29
+ addPackage(name: string, directory: string): void;
30
+ clearPackages(): void;
31
+ setAliases(aliases: string): void;
32
+ setMetadata(values: string): void;
33
+ check(path: string): import("..").Diagnostic[];
34
+ run(path: string, libPaths?: string[]): {
35
+ stdout: string;
36
+ stderr: string;
37
+ result: string | number;
38
+ };
39
+ candid(path: string): string;
40
+ wasm(path: string, mode: import("..").WasmMode): any;
41
+ parseMotoko(content: string): object;
42
+ parseCandid(content: string): object;
43
+ };
44
+ export default _default;
45
+ //# sourceMappingURL=interpreter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpreter.d.ts","sourceRoot":"","sources":["../../src/versions/interpreter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wBAAuD"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const __1 = __importDefault(require(".."));
7
+ const { Motoko } = require('../../versions/latest/moc_interpreter.min');
8
+ exports.default = (0, __1.default)(Motoko, 'latest/interpreter');
9
+ //# sourceMappingURL=interpreter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpreter.js","sourceRoot":"","sources":["../../src/versions/interpreter.ts"],"names":[],"mappings":";;;;;AAAA,2CAA2B;AAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,2CAA2C,CAAC,CAAC;AAExE,kBAAe,IAAA,WAAS,EAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC"}
@@ -0,0 +1,45 @@
1
+ declare const _default: {
2
+ version: string;
3
+ compiler: any;
4
+ file(path: string): {
5
+ readonly path: string;
6
+ clone(): any;
7
+ read(): string;
8
+ write(content: string): void;
9
+ rename(newPath: string): void;
10
+ delete(): void;
11
+ list(): string[];
12
+ check(): import("..").Diagnostic[];
13
+ run(): {
14
+ stdout: string;
15
+ stderr: string;
16
+ result: string | number;
17
+ };
18
+ candid(): string;
19
+ wasm(mode: import("..").WasmMode): any;
20
+ parseMotoko(): object;
21
+ parseCandid(): object;
22
+ };
23
+ loadPackages(packages: Record<string, string | import("../package").PackageInfo>): Promise<void>;
24
+ read(path: string): string;
25
+ write(path: string, content?: string): void;
26
+ rename(path: string, newPath: string): void;
27
+ delete(path: string): void;
28
+ list(directory: string): string[];
29
+ addPackage(name: string, directory: string): void;
30
+ clearPackages(): void;
31
+ setAliases(aliases: string): void;
32
+ setMetadata(values: string): void;
33
+ check(path: string): import("..").Diagnostic[];
34
+ run(path: string, libPaths?: string[]): {
35
+ stdout: string;
36
+ stderr: string;
37
+ result: string | number;
38
+ };
39
+ candid(path: string): string;
40
+ wasm(path: string, mode: import("..").WasmMode): any;
41
+ parseMotoko(content: string): object;
42
+ parseCandid(content: string): object;
43
+ };
44
+ export default _default;
45
+ //# sourceMappingURL=moc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moc.d.ts","sourceRoot":"","sources":["../../src/versions/moc.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wBAA2C"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const __1 = __importDefault(require(".."));
7
+ const { Motoko } = require('../../versions/latest/moc.min');
8
+ exports.default = (0, __1.default)(Motoko, 'latest');
9
+ //# sourceMappingURL=moc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moc.js","sourceRoot":"","sources":["../../src/versions/moc.ts"],"names":[],"mappings":";;;;;AAAA,2CAA2B;AAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,+BAA+B,CAAC,CAAC;AAE5D,kBAAe,IAAA,WAAS,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,32 +1,40 @@
1
1
  {
2
2
  "name": "motoko",
3
- "version": "2.0.6",
4
- "description": "Compile Motoko smart contracts in Node.js and the browser.",
3
+ "version": "2.0.9",
4
+ "description": "Compile and run Motoko smart contracts in Node.js or the browser.",
5
5
  "author": "Ryan Vandersmith (https://github.com/rvanasa)",
6
6
  "license": "Apache-2.0",
7
7
  "main": "./index.js",
8
+ "types": "lib/versions/moc.d.ts",
8
9
  "repository": {
9
10
  "type": "git",
10
11
  "url": "https://github.com/dfinity/node-motoko.git"
11
12
  },
12
13
  "scripts": {
14
+ "build": "rimraf ./lib && tsc -p .",
13
15
  "prepare": "husky install",
14
16
  "generate": "node utils/generate",
15
17
  "test": "jest",
16
- "precommit": "lint-staged"
18
+ "precommit": "lint-staged",
19
+ "prepublishOnly": "run-s build test"
17
20
  },
18
21
  "dependencies": {
19
22
  "cross-fetch": "3.1.5",
20
23
  "isomorphic-parse-github-url": "1.0.2"
21
24
  },
22
25
  "devDependencies": {
26
+ "@types/jest": "^28.1.3",
23
27
  "@wasmer/wasi": "^1.0.2",
24
28
  "cross-env": "^7.0.3",
25
29
  "eslint-config-prettier": "^8.5.0",
26
30
  "husky": "^8.0.1",
27
31
  "jest": "^28.1.3",
28
32
  "lint-staged": "^13.0.3",
29
- "prettier": "^2.7.1"
33
+ "npm-run-all": "^4.1.5",
34
+ "prettier": "^2.7.1",
35
+ "ts-jest": "^28.0.8",
36
+ "ts-node": "^10.9.1",
37
+ "typescript": "^4.8.2"
30
38
  },
31
39
  "lint-staged": {
32
40
  "{lib,contrib,utils}/**/*.{js,ts,jsx,tsx}": [
@@ -40,9 +48,10 @@
40
48
  "files": [
41
49
  "index.js",
42
50
  "interpreter.js",
43
- "lib",
44
- "contrib",
45
- "versions/latest"
51
+ "src/**/*",
52
+ "lib/**/*",
53
+ "contrib/**/*",
54
+ "versions/latest/**/*"
46
55
  ],
47
56
  "keywords": [
48
57
  "motoko",
package/src/file.ts ADDED
@@ -0,0 +1,68 @@
1
+ import { Motoko, WasmMode } from '.';
2
+
3
+ function getValidPath(path: string): string {
4
+ if (typeof path !== 'string') {
5
+ throw new Error('File path must be a string');
6
+ }
7
+ if (path.startsWith('/')) {
8
+ path = path.slice(1);
9
+ }
10
+ if (path.endsWith('/')) {
11
+ path = path.slice(0, -1);
12
+ }
13
+ return path;
14
+ }
15
+
16
+ export type MotokoFile = ReturnType<typeof file>;
17
+
18
+ export const file = (mo: Motoko, path: string) => {
19
+ path = getValidPath(path);
20
+ const result = {
21
+ get path(): string {
22
+ return path;
23
+ },
24
+ // file(subPath) {
25
+ // subPath = getValidPath(subPath);
26
+ // return exports.file(`${path}/${subPath}`);
27
+ // },
28
+ clone() {
29
+ return exports.file(path);
30
+ },
31
+ read(): string {
32
+ return mo.read(path);
33
+ },
34
+ write(content: string) {
35
+ return mo.write(path, content);
36
+ },
37
+ rename(newPath: string) {
38
+ let result = mo.rename(path, newPath);
39
+ path = newPath;
40
+ return result;
41
+ },
42
+ delete() {
43
+ return mo.delete(path);
44
+ },
45
+ list() {
46
+ return mo.list(path);
47
+ },
48
+ check() {
49
+ return mo.check(path);
50
+ },
51
+ run() {
52
+ return mo.run(path);
53
+ },
54
+ candid() {
55
+ return mo.candid(path);
56
+ },
57
+ wasm(mode: WasmMode) {
58
+ return mo.wasm(path, mode);
59
+ },
60
+ parseMotoko() {
61
+ return mo.parseMotoko(result.read());
62
+ },
63
+ parseCandid() {
64
+ return mo.parseCandid(result.read());
65
+ },
66
+ };
67
+ return result;
68
+ };