@cdktn/hcl-tools 0.21.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 ADDED
@@ -0,0 +1,19 @@
1
+ # `@cdktn/hcl-tools`
2
+
3
+ This library is meant to house commonly used HCL tools by CDKTN. Currently, it only
4
+ supports the [hclwrite](https://pkg.go.dev/github.com/hashicorp/hcl/v2/hclwrite) Golang library and
5
+ specifically the [`Format`](https://pkg.go.dev/github.com/hashicorp/hcl/v2/hclwrite#Format) function.
6
+
7
+ ## Usage
8
+
9
+ ```
10
+ import { format } from "@cdktn/hcl-tools";
11
+
12
+ const unformattedHcl = `
13
+ resource "random_pet" "my_pet" {
14
+ }
15
+ `
16
+
17
+ const formatted = await format(unformattedHcl);
18
+
19
+ ```
package/main.wasm.gz ADDED
Binary file
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@cdktn/hcl-tools",
3
+ "version": "0.21.0",
4
+ "description": "Interact with HCL helper libraries for output of HCL configuration",
5
+ "keywords": [
6
+ "cdk",
7
+ "cdktf",
8
+ "terraform",
9
+ "hcl"
10
+ ],
11
+ "author": {
12
+ "name": "OpenConstructs",
13
+ "url": "https://github.com/open-constructs"
14
+ },
15
+ "homepage": "https://github.com/open-constructs/cdk-terrain",
16
+ "license": "MPL-2.0",
17
+ "main": "src/index.js",
18
+ "types": "src/index.d.ts",
19
+ "scripts": {
20
+ "build": "tsc && ./build-go.sh",
21
+ "watch": "tsc -w",
22
+ "watch-preserve-output": "tsc -w --preserveWatchOutput",
23
+ "test": "jest",
24
+ "test:ci": "jest --ci",
25
+ "test:update": "jest -u --passWithNoTests",
26
+ "jest-watch": "jest --watch",
27
+ "package": "./package.sh",
28
+ "dist-clean": "rm -rf dist",
29
+ "prebuild": "./prebuild.sh",
30
+ "package:js": "./package.sh"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git://github.com/open-constructs/cdk-terrain.git",
38
+ "directory": "packages/@cdktn/hcl-tools"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/open-constructs/cdk-terrain/issues"
42
+ },
43
+ "dependencies": {
44
+ "fs-extra": "^11.3.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/jest": "29.5.14",
48
+ "@types/node": "20.17.51",
49
+ "jest": "^29.7.0",
50
+ "ts-jest": "29.2.5",
51
+ "typescript": "^5.7.3"
52
+ }
53
+ }
@@ -0,0 +1,2 @@
1
+ export declare function format(contents: string): Promise<string>;
2
+ //# sourceMappingURL=bridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["bridge.ts"],"names":[],"mappings":"AAkFA,wBAAsB,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG9D"}
package/src/bridge.js ADDED
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.format = format;
37
+ // Copyright (c) HashiCorp, Inc
38
+ // SPDX-License-Identifier: MPL-2.0
39
+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
40
+ /// <reference lib="dom" />
41
+ const fs = __importStar(require("fs-extra"));
42
+ const path_1 = require("path");
43
+ const zlib_1 = require("zlib");
44
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
45
+ const jsRoot = {};
46
+ function sleep() {
47
+ return new Promise((resolve) => {
48
+ setTimeout(resolve, 0);
49
+ });
50
+ }
51
+ function goBridge(getBytes) {
52
+ let ready = false;
53
+ async function init() {
54
+ // After: https://github.com/golang/go/commit/680caf15355057ca84857a2a291b6f5c44e73329
55
+ // Go 1.19+ has a different entrypoint
56
+ await Promise.resolve().then(() => __importStar(require(`../wasm/bridge_wasm_exec.js`)));
57
+ const go = new global.Go();
58
+ const bytes = await getBytes;
59
+ const result = await WebAssembly.instantiate(bytes, go.importObject);
60
+ global.__hcl_helpers_config_wasm__ = jsRoot;
61
+ void go.run(result.instance);
62
+ ready = true;
63
+ }
64
+ init().catch((error) => {
65
+ throw error;
66
+ });
67
+ const proxy = new Proxy({}, {
68
+ get: (_, key) => {
69
+ return async (...args) => {
70
+ while (!ready) {
71
+ await sleep();
72
+ }
73
+ if (!(key in jsRoot)) {
74
+ throw new Error(`There is nothing defined with the name "${key.toString()}"`);
75
+ }
76
+ if (typeof jsRoot[key] !== "function") {
77
+ return jsRoot[key];
78
+ }
79
+ return new Promise((resolve, reject) => {
80
+ const cb = (err, ...msg) =>
81
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
82
+ // @ts-ignore
83
+ err ? reject(new Error(err)) : resolve(...msg);
84
+ const run = () => {
85
+ jsRoot[key].apply(undefined, [...args, cb]);
86
+ };
87
+ run();
88
+ });
89
+ };
90
+ },
91
+ });
92
+ return proxy;
93
+ }
94
+ const loadWasm = async () => {
95
+ return (0, zlib_1.gunzipSync)(await fs.readFile((0, path_1.join)(__dirname, "..", "main.wasm.gz")));
96
+ };
97
+ const wasm = goBridge(loadWasm());
98
+ async function format(contents) {
99
+ const res = await wasm.format(contents);
100
+ return res;
101
+ }
102
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnJpZGdlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiYnJpZGdlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBa0ZBLHdCQUdDO0FBckZELCtCQUErQjtBQUMvQixtQ0FBbUM7QUFDbkMscUVBQXFFO0FBQ3JFLDJCQUEyQjtBQUMzQiw2Q0FBK0I7QUFDL0IsK0JBQTRCO0FBQzVCLCtCQUFrQztBQU1sQyxzRUFBc0U7QUFDdEUsTUFBTSxNQUFNLEdBQTZCLEVBQUUsQ0FBQztBQUU1QyxTQUFTLEtBQUs7SUFDWixPQUFPLElBQUksT0FBTyxDQUFDLENBQUMsT0FBTyxFQUFFLEVBQUU7UUFDN0IsVUFBVSxDQUFDLE9BQU8sRUFBRSxDQUFDLENBQUMsQ0FBQztJQUN6QixDQUFDLENBQUMsQ0FBQztBQUNMLENBQUM7QUFFRCxTQUFTLFFBQVEsQ0FBQyxRQUF5QjtJQUN6QyxJQUFJLEtBQUssR0FBRyxLQUFLLENBQUM7SUFFbEIsS0FBSyxVQUFVLElBQUk7UUFDakIsc0ZBQXNGO1FBQ3RGLHNDQUFzQztRQUN0Qyx3REFBYSw2QkFBNkIsR0FBQyxDQUFDO1FBQzVDL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3YWl0IHdhc20uZm9ybWF0KGNvbnRlbnRzKTtcbiAgcmV0dXJuIHJlcztcbn1cbiJdfQ==
package/src/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Copyright (c) HashiCorp, Inc.
3
+ * SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ export { format } from "./bridge";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
package/src/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) HashiCorp, Inc.
4
+ * SPDX-License-Identifier: MPL-2.0
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.format = void 0;
8
+ var bridge_1 = require("./bridge");
9
+ Object.defineProperty(exports, "format", { enumerable: true, get: function () { return bridge_1.format; } });
10
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7OztHQUdHOzs7QUFFSCxtQ0FBa0M7QUFBekIsZ0dBQUEsTUFBTSxPQUFBIiwic291cmNlc0NvbnRlbnQiOlsiLyoqXG4gKiBDb3B5cmlnaHQgKGMpIEhhc2hpQ29ycCwgSW5jLlxuICogU1BEWC1MaWNlbnNlLUlkZW50aWZpZXI6IE1QTC0yLjBcbiAqL1xuXG5leHBvcnQgeyBmb3JtYXQgfSBmcm9tIFwiLi9icmlkZ2VcIjtcbiJdfQ==
@@ -0,0 +1,3 @@
1
+ // Copyright (c) HashiCorp, Inc
2
+ // SPDX-License-Identifier: MPL-2.0
3
+ export {};
@@ -0,0 +1,26 @@
1
+ // Copyright 2021 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // Modified from https://github.com/golang/go/blob/master/misc/wasm/wasm_exec_node.js
6
+
7
+ "use strict";
8
+
9
+ globalThis.require = require;
10
+ globalThis.fs = require("fs");
11
+ globalThis.TextEncoder = require("util").TextEncoder;
12
+ globalThis.TextDecoder = require("util").TextDecoder;
13
+
14
+ globalThis.performance ??= require("performance");
15
+
16
+ // Node >= 19 has a crypto function object, lower node versions need this polyfill
17
+ if (!globalThis.crypto) {
18
+ const crypto = require("crypto");
19
+ globalThis.crypto = {
20
+ getRandomValues(b) {
21
+ return crypto.randomFillSync(b);
22
+ },
23
+ };
24
+ }
25
+
26
+ require("./wasm_exec");
@@ -0,0 +1,3 @@
1
+ // Copyright (c) HashiCorp, Inc
2
+ // Licensed under the Mozilla Public License 2.0
3
+ export {};
@@ -0,0 +1,554 @@
1
+ // Copyright 2018 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ "use strict";
6
+
7
+ (() => {
8
+ const enosys = () => {
9
+ const err = new Error("not implemented");
10
+ err.code = "ENOSYS";
11
+ return err;
12
+ };
13
+
14
+ if (!globalThis.fs) {
15
+ let outputBuf = "";
16
+ globalThis.fs = {
17
+ constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
18
+ writeSync(fd, buf) {
19
+ outputBuf += decoder.decode(buf);
20
+ const nl = outputBuf.lastIndexOf("\n");
21
+ if (nl != -1) {
22
+ console.log(outputBuf.substr(0, nl));
23
+ outputBuf = outputBuf.substr(nl + 1);
24
+ }
25
+ return buf.length;
26
+ },
27
+ write(fd, buf, offset, length, position, callback) {
28
+ if (offset !== 0 || length !== buf.length || position !== null) {
29
+ callback(enosys());
30
+ return;
31
+ }
32
+ const n = this.writeSync(fd, buf);
33
+ callback(null, n);
34
+ },
35
+ chmod(path, mode, callback) { callback(enosys()); },
36
+ chown(path, uid, gid, callback) { callback(enosys()); },
37
+ close(fd, callback) { callback(enosys()); },
38
+ fchmod(fd, mode, callback) { callback(enosys()); },
39
+ fchown(fd, uid, gid, callback) { callback(enosys()); },
40
+ fstat(fd, callback) { callback(enosys()); },
41
+ fsync(fd, callback) { callback(null); },
42
+ ftruncate(fd, length, callback) { callback(enosys()); },
43
+ lchown(path, uid, gid, callback) { callback(enosys()); },
44
+ link(path, link, callback) { callback(enosys()); },
45
+ lstat(path, callback) { callback(enosys()); },
46
+ mkdir(path, perm, callback) { callback(enosys()); },
47
+ open(path, flags, mode, callback) { callback(enosys()); },
48
+ read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
49
+ readdir(path, callback) { callback(enosys()); },
50
+ readlink(path, callback) { callback(enosys()); },
51
+ rename(from, to, callback) { callback(enosys()); },
52
+ rmdir(path, callback) { callback(enosys()); },
53
+ stat(path, callback) { callback(enosys()); },
54
+ symlink(path, link, callback) { callback(enosys()); },
55
+ truncate(path, length, callback) { callback(enosys()); },
56
+ unlink(path, callback) { callback(enosys()); },
57
+ utimes(path, atime, mtime, callback) { callback(enosys()); },
58
+ };
59
+ }
60
+
61
+ if (!globalThis.process) {
62
+ globalThis.process = {
63
+ getuid() { return -1; },
64
+ getgid() { return -1; },
65
+ geteuid() { return -1; },
66
+ getegid() { return -1; },
67
+ getgroups() { throw enosys(); },
68
+ pid: -1,
69
+ ppid: -1,
70
+ umask() { throw enosys(); },
71
+ cwd() { throw enosys(); },
72
+ chdir() { throw enosys(); },
73
+ }
74
+ }
75
+
76
+ if (!globalThis.crypto) {
77
+ throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
78
+ }
79
+
80
+ if (!globalThis.performance) {
81
+ throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
82
+ }
83
+
84
+ if (!globalThis.TextEncoder) {
85
+ throw new Error("globalThis.TextEncoder is not available, polyfill required");
86
+ }
87
+
88
+ if (!globalThis.TextDecoder) {
89
+ throw new Error("globalThis.TextDecoder is not available, polyfill required");
90
+ }
91
+
92
+ const encoder = new TextEncoder("utf-8");
93
+ const decoder = new TextDecoder("utf-8");
94
+
95
+ globalThis.Go = class {
96
+ constructor() {
97
+ this.argv = ["js"];
98
+ this.env = {};
99
+ this.exit = (code) => {
100
+ if (code !== 0) {
101
+ console.warn("exit code:", code);
102
+ }
103
+ };
104
+ this._exitPromise = new Promise((resolve) => {
105
+ this._resolveExitPromise = resolve;
106
+ });
107
+ this._pendingEvent = null;
108
+ this._scheduledTimeouts = new Map();
109
+ this._nextCallbackTimeoutID = 1;
110
+
111
+ const setInt64 = (addr, v) => {
112
+ this.mem.setUint32(addr + 0, v, true);
113
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
114
+ }
115
+
116
+ const getInt64 = (addr) => {
117
+ const low = this.mem.getUint32(addr + 0, true);
118
+ const high = this.mem.getInt32(addr + 4, true);
119
+ return low + high * 4294967296;
120
+ }
121
+
122
+ const loadValue = (addr) => {
123
+ const f = this.mem.getFloat64(addr, true);
124
+ if (f === 0) {
125
+ return undefined;
126
+ }
127
+ if (!isNaN(f)) {
128
+ return f;
129
+ }
130
+
131
+ const id = this.mem.getUint32(addr, true);
132
+ return this._values[id];
133
+ }
134
+
135
+ const storeValue = (addr, v) => {
136
+ const nanHead = 0x7FF80000;
137
+
138
+ if (typeof v === "number" && v !== 0) {
139
+ if (isNaN(v)) {
140
+ this.mem.setUint32(addr + 4, nanHead, true);
141
+ this.mem.setUint32(addr, 0, true);
142
+ return;
143
+ }
144
+ this.mem.setFloat64(addr, v, true);
145
+ return;
146
+ }
147
+
148
+ if (v === undefined) {
149
+ this.mem.setFloat64(addr, 0, true);
150
+ return;
151
+ }
152
+
153
+ let id = this._ids.get(v);
154
+ if (id === undefined) {
155
+ id = this._idPool.pop();
156
+ if (id === undefined) {
157
+ id = this._values.length;
158
+ }
159
+ this._values[id] = v;
160
+ this._goRefCounts[id] = 0;
161
+ this._ids.set(v, id);
162
+ }
163
+ this._goRefCounts[id]++;
164
+ let typeFlag = 0;
165
+ switch (typeof v) {
166
+ case "object":
167
+ if (v !== null) {
168
+ typeFlag = 1;
169
+ }
170
+ break;
171
+ case "string":
172
+ typeFlag = 2;
173
+ break;
174
+ case "symbol":
175
+ typeFlag = 3;
176
+ break;
177
+ case "function":
178
+ typeFlag = 4;
179
+ break;
180
+ }
181
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
182
+ this.mem.setUint32(addr, id, true);
183
+ }
184
+
185
+ const loadSlice = (addr) => {
186
+ const array = getInt64(addr + 0);
187
+ const len = getInt64(addr + 8);
188
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
189
+ }
190
+
191
+ const loadSliceOfValues = (addr) => {
192
+ const array = getInt64(addr + 0);
193
+ const len = getInt64(addr + 8);
194
+ const a = new Array(len);
195
+ for (let i = 0; i < len; i++) {
196
+ a[i] = loadValue(array + i * 8);
197
+ }
198
+ return a;
199
+ }
200
+
201
+ const loadString = (addr) => {
202
+ const saddr = getInt64(addr + 0);
203
+ const len = getInt64(addr + 8);
204
+ return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
205
+ }
206
+
207
+ const timeOrigin = Date.now() - performance.now();
208
+ this.importObject = {
209
+ go: {
210
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
211
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
212
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
213
+ // This changes the SP, thus we have to update the SP used by the imported function.
214
+
215
+ // func wasmExit(code int32)
216
+ "runtime.wasmExit": (sp) => {
217
+ sp >>>= 0;
218
+ const code = this.mem.getInt32(sp + 8, true);
219
+ this.exited = true;
220
+ delete this._inst;
221
+ delete this._values;
222
+ delete this._goRefCounts;
223
+ delete this._ids;
224
+ delete this._idPool;
225
+ this.exit(code);
226
+ },
227
+
228
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
229
+ "runtime.wasmWrite": (sp) => {
230
+ sp >>>= 0;
231
+ const fd = getInt64(sp + 8);
232
+ const p = getInt64(sp + 16);
233
+ const n = this.mem.getInt32(sp + 24, true);
234
+ fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
235
+ },
236
+
237
+ // func resetMemoryDataView()
238
+ "runtime.resetMemoryDataView": (sp) => {
239
+ sp >>>= 0;
240
+ this.mem = new DataView(this._inst.exports.mem.buffer);
241
+ },
242
+
243
+ // func nanotime1() int64
244
+ "runtime.nanotime1": (sp) => {
245
+ sp >>>= 0;
246
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
247
+ },
248
+
249
+ // func walltime() (sec int64, nsec int32)
250
+ "runtime.walltime": (sp) => {
251
+ sp >>>= 0;
252
+ const msec = (new Date).getTime();
253
+ setInt64(sp + 8, msec / 1000);
254
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
255
+ },
256
+
257
+ // func scheduleTimeoutEvent(delay int64) int32
258
+ "runtime.scheduleTimeoutEvent": (sp) => {
259
+ sp >>>= 0;
260
+ const id = this._nextCallbackTimeoutID;
261
+ this._nextCallbackTimeoutID++;
262
+ this._scheduledTimeouts.set(id, setTimeout(
263
+ () => {
264
+ this._resume();
265
+ while (this._scheduledTimeouts.has(id)) {
266
+ // for some reason Go failed to register the timeout event, log and try again
267
+ // (temporary workaround for https://github.com/golang/go/issues/28975)
268
+ console.warn("scheduleTimeoutEvent: missed timeout event");
269
+ this._resume();
270
+ }
271
+ },
272
+ getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
273
+ ));
274
+ this.mem.setInt32(sp + 16, id, true);
275
+ },
276
+
277
+ // func clearTimeoutEvent(id int32)
278
+ "runtime.clearTimeoutEvent": (sp) => {
279
+ sp >>>= 0;
280
+ const id = this.mem.getInt32(sp + 8, true);
281
+ clearTimeout(this._scheduledTimeouts.get(id));
282
+ this._scheduledTimeouts.delete(id);
283
+ },
284
+
285
+ // func getRandomData(r []byte)
286
+ "runtime.getRandomData": (sp) => {
287
+ sp >>>= 0;
288
+ crypto.getRandomValues(loadSlice(sp + 8));
289
+ },
290
+
291
+ // func finalizeRef(v ref)
292
+ "syscall/js.finalizeRef": (sp) => {
293
+ sp >>>= 0;
294
+ const id = this.mem.getUint32(sp + 8, true);
295
+ this._goRefCounts[id]--;
296
+ if (this._goRefCounts[id] === 0) {
297
+ const v = this._values[id];
298
+ this._values[id] = null;
299
+ this._ids.delete(v);
300
+ this._idPool.push(id);
301
+ }
302
+ },
303
+
304
+ // func stringVal(value string) ref
305
+ "syscall/js.stringVal": (sp) => {
306
+ sp >>>= 0;
307
+ storeValue(sp + 24, loadString(sp + 8));
308
+ },
309
+
310
+ // func valueGet(v ref, p string) ref
311
+ "syscall/js.valueGet": (sp) => {
312
+ sp >>>= 0;
313
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
314
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
315
+ storeValue(sp + 32, result);
316
+ },
317
+
318
+ // func valueSet(v ref, p string, x ref)
319
+ "syscall/js.valueSet": (sp) => {
320
+ sp >>>= 0;
321
+ Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
322
+ },
323
+
324
+ // func valueDelete(v ref, p string)
325
+ "syscall/js.valueDelete": (sp) => {
326
+ sp >>>= 0;
327
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
328
+ },
329
+
330
+ // func valueIndex(v ref, i int) ref
331
+ "syscall/js.valueIndex": (sp) => {
332
+ sp >>>= 0;
333
+ storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
334
+ },
335
+
336
+ // valueSetIndex(v ref, i int, x ref)
337
+ "syscall/js.valueSetIndex": (sp) => {
338
+ sp >>>= 0;
339
+ Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
340
+ },
341
+
342
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
343
+ "syscall/js.valueCall": (sp) => {
344
+ sp >>>= 0;
345
+ try {
346
+ const v = loadValue(sp + 8);
347
+ const m = Reflect.get(v, loadString(sp + 16));
348
+ const args = loadSliceOfValues(sp + 32);
349
+ const result = Reflect.apply(m, v, args);
350
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
351
+ storeValue(sp + 56, result);
352
+ this.mem.setUint8(sp + 64, 1);
353
+ } catch (err) {
354
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
355
+ storeValue(sp + 56, err);
356
+ this.mem.setUint8(sp + 64, 0);
357
+ }
358
+ },
359
+
360
+ // func valueInvoke(v ref, args []ref) (ref, bool)
361
+ "syscall/js.valueInvoke": (sp) => {
362
+ sp >>>= 0;
363
+ try {
364
+ const v = loadValue(sp + 8);
365
+ const args = loadSliceOfValues(sp + 16);
366
+ const result = Reflect.apply(v, undefined, args);
367
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
368
+ storeValue(sp + 40, result);
369
+ this.mem.setUint8(sp + 48, 1);
370
+ } catch (err) {
371
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
372
+ storeValue(sp + 40, err);
373
+ this.mem.setUint8(sp + 48, 0);
374
+ }
375
+ },
376
+
377
+ // func valueNew(v ref, args []ref) (ref, bool)
378
+ "syscall/js.valueNew": (sp) => {
379
+ sp >>>= 0;
380
+ try {
381
+ const v = loadValue(sp + 8);
382
+ const args = loadSliceOfValues(sp + 16);
383
+ const result = Reflect.construct(v, args);
384
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
385
+ storeValue(sp + 40, result);
386
+ this.mem.setUint8(sp + 48, 1);
387
+ } catch (err) {
388
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
389
+ storeValue(sp + 40, err);
390
+ this.mem.setUint8(sp + 48, 0);
391
+ }
392
+ },
393
+
394
+ // func valueLength(v ref) int
395
+ "syscall/js.valueLength": (sp) => {
396
+ sp >>>= 0;
397
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
398
+ },
399
+
400
+ // valuePrepareString(v ref) (ref, int)
401
+ "syscall/js.valuePrepareString": (sp) => {
402
+ sp >>>= 0;
403
+ const str = encoder.encode(String(loadValue(sp + 8)));
404
+ storeValue(sp + 16, str);
405
+ setInt64(sp + 24, str.length);
406
+ },
407
+
408
+ // valueLoadString(v ref, b []byte)
409
+ "syscall/js.valueLoadString": (sp) => {
410
+ sp >>>= 0;
411
+ const str = loadValue(sp + 8);
412
+ loadSlice(sp + 16).set(str);
413
+ },
414
+
415
+ // func valueInstanceOf(v ref, t ref) bool
416
+ "syscall/js.valueInstanceOf": (sp) => {
417
+ sp >>>= 0;
418
+ this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
419
+ },
420
+
421
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
422
+ "syscall/js.copyBytesToGo": (sp) => {
423
+ sp >>>= 0;
424
+ const dst = loadSlice(sp + 8);
425
+ const src = loadValue(sp + 32);
426
+ if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
427
+ this.mem.setUint8(sp + 48, 0);
428
+ return;
429
+ }
430
+ const toCopy = src.subarray(0, dst.length);
431
+ dst.set(toCopy);
432
+ setInt64(sp + 40, toCopy.length);
433
+ this.mem.setUint8(sp + 48, 1);
434
+ },
435
+
436
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
437
+ "syscall/js.copyBytesToJS": (sp) => {
438
+ sp >>>= 0;
439
+ const dst = loadValue(sp + 8);
440
+ const src = loadSlice(sp + 16);
441
+ if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
442
+ this.mem.setUint8(sp + 48, 0);
443
+ return;
444
+ }
445
+ const toCopy = src.subarray(0, dst.length);
446
+ dst.set(toCopy);
447
+ setInt64(sp + 40, toCopy.length);
448
+ this.mem.setUint8(sp + 48, 1);
449
+ },
450
+
451
+ "debug": (value) => {
452
+ console.log(value);
453
+ },
454
+ }
455
+ };
456
+ }
457
+
458
+ async run(instance) {
459
+ if (!(instance instanceof WebAssembly.Instance)) {
460
+ throw new Error("Go.run: WebAssembly.Instance expected");
461
+ }
462
+ this._inst = instance;
463
+ this.mem = new DataView(this._inst.exports.mem.buffer);
464
+ this._values = [ // JS values that Go currently has references to, indexed by reference id
465
+ NaN,
466
+ 0,
467
+ null,
468
+ true,
469
+ false,
470
+ globalThis,
471
+ this,
472
+ ];
473
+ this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
474
+ this._ids = new Map([ // mapping from JS values to reference ids
475
+ [0, 1],
476
+ [null, 2],
477
+ [true, 3],
478
+ [false, 4],
479
+ [globalThis, 5],
480
+ [this, 6],
481
+ ]);
482
+ this._idPool = []; // unused ids that have been garbage collected
483
+ this.exited = false; // whether the Go program has exited
484
+
485
+ // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
486
+ let offset = 4096;
487
+
488
+ const strPtr = (str) => {
489
+ const ptr = offset;
490
+ const bytes = encoder.encode(str + "\0");
491
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
492
+ offset += bytes.length;
493
+ if (offset % 8 !== 0) {
494
+ offset += 8 - (offset % 8);
495
+ }
496
+ return ptr;
497
+ };
498
+
499
+ const argc = this.argv.length;
500
+
501
+ const argvPtrs = [];
502
+ this.argv.forEach((arg) => {
503
+ argvPtrs.push(strPtr(arg));
504
+ });
505
+ argvPtrs.push(0);
506
+
507
+ const keys = Object.keys(this.env).sort();
508
+ keys.forEach((key) => {
509
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
510
+ });
511
+ argvPtrs.push(0);
512
+
513
+ const argv = offset;
514
+ argvPtrs.forEach((ptr) => {
515
+ this.mem.setUint32(offset, ptr, true);
516
+ this.mem.setUint32(offset + 4, 0, true);
517
+ offset += 8;
518
+ });
519
+
520
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
521
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
522
+ const wasmMinDataAddr = 4096 + 8192;
523
+ if (offset >= wasmMinDataAddr) {
524
+ throw new Error("total length of command line and environment variables exceeds limit");
525
+ }
526
+
527
+ this._inst.exports.run(argc, argv);
528
+ if (this.exited) {
529
+ this._resolveExitPromise();
530
+ }
531
+ await this._exitPromise;
532
+ }
533
+
534
+ _resume() {
535
+ if (this.exited) {
536
+ throw new Error("Go program has already exited");
537
+ }
538
+ this._inst.exports.resume();
539
+ if (this.exited) {
540
+ this._resolveExitPromise();
541
+ }
542
+ }
543
+
544
+ _makeFuncWrapper(id) {
545
+ const go = this;
546
+ return function () {
547
+ const event = { id: id, this: this, args: arguments };
548
+ go._pendingEvent = event;
549
+ go._resume();
550
+ return event.result;
551
+ };
552
+ }
553
+ }
554
+ })();