Skip to content

Commit 5caf63b

Browse files
authored
Merge pull request #104 from lightninglabs/2024-02-update_to_v0.3.0-alpha
Bump version to v0.3.0-alpha
2 parents f6436b7 + 7ffde3a commit 5caf63b

File tree

3 files changed

+33
-108
lines changed

3 files changed

+33
-108
lines changed

lib/lnc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { wasmLog as log } from './util/log';
1414

1515
/** The default values for the LncConfig options */
1616
const DEFAULT_CONFIG = {
17-
wasmClientCode: 'https://lightning.engineering/lnc-v0.2.8-alpha.wasm',
17+
wasmClientCode: 'https://lightning.engineering/lnc-v0.3.0-alpha.wasm',
1818
namespace: 'default',
1919
serverHost: 'mailbox.terminal.lightning.today:443'
2020
} as Required<LncConfig>;
@@ -172,7 +172,7 @@ export default class LNC {
172172

173173
this.go.argv = [
174174
'wasm-client',
175-
'--debuglevel=trace',
175+
'--debuglevel=debug,GOBN=info,GRPC=info',
176176
'--namespace=' + this._namespace,
177177
`--onlocalprivcreate=${this._namespace}.onLocalPrivCreate`,
178178
`--onremotekeyreceive=${this._namespace}.onRemoteKeyReceive`,

lib/wasm_exec.js

Lines changed: 30 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,25 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
(() => {
6-
// Map multiple JavaScript environments to a single common API,
7-
// preferring web standards over Node.js API.
8-
//
9-
// Environments considered:
10-
// - Browsers
11-
// - Node.js
12-
// - Electron
13-
// - Parcel
14-
// - Webpack
15-
16-
if (typeof global !== "undefined") {
17-
// global already exists
18-
} else if (typeof window !== "undefined") {
19-
window.global = window;
20-
} else if (typeof self !== "undefined") {
21-
self.global = self;
22-
} else {
23-
throw new Error("cannot export Go (neither global, window nor self is defined)");
24-
}
25-
26-
if (!global.require && typeof require !== "undefined") {
27-
global.require = require;
28-
}
29-
30-
if (!global.fs && global.require) {
31-
const fs = require("fs");
32-
if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) {
33-
global.fs = fs;
34-
}
35-
}
5+
"use strict";
366

7+
(() => {
378
const enosys = () => {
389
const err = new Error("not implemented");
3910
err.code = "ENOSYS";
4011
return err;
4112
};
4213

43-
if (!global.fs) {
14+
if (!globalThis.fs) {
4415
let outputBuf = "";
45-
global.fs = {
16+
globalThis.fs = {
4617
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
4718
writeSync(fd, buf) {
4819
outputBuf += decoder.decode(buf);
4920
const nl = outputBuf.lastIndexOf("\n");
5021
if (nl != -1) {
51-
console.log(outputBuf.substr(0, nl));
52-
outputBuf = outputBuf.substr(nl + 1);
22+
console.log(outputBuf.substring(0, nl));
23+
outputBuf = outputBuf.substring(nl + 1);
5324
}
5425
return buf.length;
5526
},
@@ -87,8 +58,8 @@
8758
};
8859
}
8960

90-
if (!global.process) {
91-
global.process = {
61+
if (!globalThis.process) {
62+
globalThis.process = {
9263
getuid() { return -1; },
9364
getgid() { return -1; },
9465
geteuid() { return -1; },
@@ -102,47 +73,26 @@
10273
}
10374
}
10475

105-
if (!global.crypto && global.require) {
106-
const nodeCrypto = require("crypto");
107-
global.crypto = {
108-
getRandomValues(b) {
109-
nodeCrypto.randomFillSync(b);
110-
},
111-
};
112-
}
113-
if (!global.crypto) {
114-
throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
76+
if (!globalThis.crypto) {
77+
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
11578
}
11679

117-
if (!global.performance) {
118-
global.performance = {
119-
now() {
120-
const [sec, nsec] = process.hrtime();
121-
return sec * 1000 + nsec / 1000000;
122-
},
123-
};
80+
if (!globalThis.performance) {
81+
throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
12482
}
12583

126-
if (!global.TextEncoder && global.require) {
127-
global.TextEncoder = require("util").TextEncoder;
128-
}
129-
if (!global.TextEncoder) {
130-
throw new Error("global.TextEncoder is not available, polyfill required");
84+
if (!globalThis.TextEncoder) {
85+
throw new Error("globalThis.TextEncoder is not available, polyfill required");
13186
}
13287

133-
if (!global.TextDecoder && global.require) {
134-
global.TextDecoder = require("util").TextDecoder;
135-
}
136-
if (!global.TextDecoder) {
137-
throw new Error("global.TextDecoder is not available, polyfill required");
88+
if (!globalThis.TextDecoder) {
89+
throw new Error("globalThis.TextDecoder is not available, polyfill required");
13890
}
13991

140-
// End of polyfills for common API.
141-
14292
const encoder = new TextEncoder("utf-8");
14393
const decoder = new TextDecoder("utf-8");
14494

145-
global.Go = class {
95+
globalThis.Go = class {
14696
constructor() {
14797
this.argv = ["js"];
14898
this.env = {};
@@ -163,6 +113,10 @@
163113
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
164114
}
165115

116+
const setInt32 = (addr, v) => {
117+
this.mem.setUint32(addr + 0, v, true);
118+
}
119+
166120
const getInt64 = (addr) => {
167121
const low = this.mem.getUint32(addr + 0, true);
168122
const high = this.mem.getInt32(addr + 4, true);
@@ -256,7 +210,10 @@
256210

257211
const timeOrigin = Date.now() - performance.now();
258212
this.importObject = {
259-
go: {
213+
_gotest: {
214+
add: (a, b) => a + b,
215+
},
216+
gojs: {
260217
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
261218
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
262219
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
@@ -319,7 +276,7 @@
319276
this._resume();
320277
}
321278
},
322-
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
279+
getInt64(sp + 8),
323280
));
324281
this.mem.setInt32(sp + 16, id, true);
325282
},
@@ -517,7 +474,7 @@
517474
null,
518475
true,
519476
false,
520-
global,
477+
globalThis,
521478
this,
522479
];
523480
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
@@ -526,7 +483,7 @@
526483
[null, 2],
527484
[true, 3],
528485
[false, 4],
529-
[global, 5],
486+
[globalThis, 5],
530487
[this, 6],
531488
]);
532489
this._idPool = []; // unused ids that have been garbage collected
@@ -569,9 +526,9 @@
569526

570527
// The linker guarantees global data starts from at least wasmMinDataAddr.
571528
// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
572-
const wasmMinDataAddr = 4096 + 4096;
529+
const wasmMinDataAddr = 4096 + 8192;
573530
if (offset >= wasmMinDataAddr) {
574-
throw new Error("command line too long");
531+
throw new Error("total length of command line and environment variables exceeds limit");
575532
}
576533

577534
this._inst.exports.run(argc, argv);
@@ -601,36 +558,4 @@
601558
};
602559
}
603560
}
604-
605-
if (
606-
typeof module !== "undefined" &&
607-
global.require &&
608-
global.require.main === module &&
609-
global.process &&
610-
global.process.versions &&
611-
!global.process.versions.electron
612-
) {
613-
if (process.argv.length < 3) {
614-
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
615-
process.exit(1);
616-
}
617-
618-
const go = new Go();
619-
go.argv = process.argv.slice(2);
620-
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
621-
go.exit = process.exit;
622-
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
623-
process.on("exit", (code) => { // Node.js exits if no event handler is pending
624-
if (code === 0 && !go.exited) {
625-
// deadlock, make Go print error and stack traces
626-
go._pendingEvent = { id: 0 };
627-
go._resume();
628-
}
629-
});
630-
return go.run(result.instance);
631-
}).catch((err) => {
632-
console.error(err);
633-
process.exit(1);
634-
});
635-
}
636561
})();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lightninglabs/lnc-web",
3-
"version": "0.2.8-alpha",
3+
"version": "0.3.0-alpha",
44
"description": "Lightning Node Connect npm module for web",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

0 commit comments

Comments
 (0)