commit af11f9c7b0a4d3b8aab11bdfc0f711ee7823ee71
parent f78d736727aae7a7a9d2e2946fbe67f90f1a96e8
Author: William Casarin <jb55@jb55.com>
Date: Wed, 23 Mar 2022 10:04:41 -0700
make it work in the browser
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
7 files changed, 40 insertions(+), 19 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -7,7 +7,7 @@
/config.h
/configurator.out.dSYM
/configurator
-/test
+/ctest
/target
TODO.bak
*.a
diff --git a/Makefile b/Makefile
@@ -173,13 +173,13 @@ dist-js: js
install-all: install install-js
-check: test
- @./test
+check: ctest
+ @./ctest
gocheck:
go test ./lnsocket.go
-test: test.o $(DEPS)
+ctest: test.o $(DEPS)
@echo "ld test"
@$(CC) $(CFLAGS) test.o $(OBJS) $(ARS) $(LDFLAGS) -o $@
diff --git a/examples/node.js b/examples/node.js
@@ -1,4 +1,4 @@
-const LNSocket = require('lnsocket')
+const LNSocket = require('../')
async function go() {
const ln = await LNSocket()
@@ -6,19 +6,12 @@ async function go() {
ln.genkey()
await ln.connect_and_init("03f3c108ccd536b8526841f0a5c58212bb9e6584a1eb493080e7c1cc34f82dad71", "24.84.152.187")
- const rune = "b3Xsg2AS2cknHYa6H94so7FAVQVdnRSP6Pv-1WOQEBc9NCZtZXRob2Q9b2ZmZXItc3VtbWFyeQ=="
- const summary = await ln.rpc({
- rune,
- method: "offer-summary",
- params: {
- offerid: "22db2cbdb2d6e1f4d727d099e2ea987c05212d6b4af56d92497e093b82360db7",
- limit: 10
- }
- })
+ const rune = "uQux-hID66AX5rFUpkt1p9CU_7DsTMyUC4G5yq7-dcw9MTMmbWV0aG9kPWdldGluZm8="
+ const res = await ln.rpc({ method: "getinfo", rune })
ln.destroy()
- console.log(summary.result)
- return summary.result
+ console.log(res)
+ return res
}
go()
diff --git a/index.js b/index.js
@@ -1,5 +1,13 @@
-const Module = require("./dist/node/lnsocket.js")
+const is_browser = new Function("try {return this===window;}catch(e){ return false;}");
+
+let Module
+if (is_browser()) {
+ Module = require("./dist/js/lnsocket.js")
+} else {
+ Module = require("./dist/node/lnsocket.js")
+}
+
const LNSocketReady = Module.lnsocket_init()
async function load_lnsocket(opts)
@@ -9,3 +17,4 @@ async function load_lnsocket(opts)
}
module.exports = load_lnsocket
+
diff --git a/lnsocket_pre.js b/lnsocket_pre.js
@@ -3,6 +3,7 @@ Module.getRandomValue = (function() {
const crypto_ = typeof window_.crypto !== "undefined" ? window_.crypto : window_.msCrypto;
let randomBytesNode
+ let fn
if (!crypto_) {
randomBytesNode = require('crypto').randomBytes
fn = randomValuesNode
diff --git a/package.json b/package.json
@@ -6,8 +6,10 @@
"url": "https://github.com/jb55/lnsocket"
},
"main": "index.js",
- "dependencies": {},
+ "scripts": {
+ "test": "tap test/*.js"
+ },
"devDependencies": {
- "tap": "~0.2.5"
+ "tap": "^0.2.6"
}
}
diff --git a/test/connection.js b/test/connection.js
@@ -0,0 +1,16 @@
+
+
+const t = require('tap')
+const LNSocket = require('../')
+
+t.test('connection works', async (t) => {
+ const ln = await LNSocket()
+
+ ln.genkey()
+ await ln.connect_and_init("03f3c108ccd536b8526841f0a5c58212bb9e6584a1eb493080e7c1cc34f82dad71", "24.84.152.187")
+
+ t.ok(ln.connected, "connection works")
+
+ ln.destroy()
+ t.end()
+})