aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/lnurl-cors-fallback-test.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/lnurl-cors-fallback-test.js b/tools/lnurl-cors-fallback-test.js
new file mode 100644
index 0000000..bc3923c
--- /dev/null
+++ b/tools/lnurl-cors-fallback-test.js
@@ -0,0 +1,44 @@
+// A Lightning address whose host the browser can't fetch (speed.app answers
+// with a redirect that has no CORS header, so fetch throws a TypeError) is
+// fetched through the registrar's /lnurlp-fetch instead; any other failure
+// stays as it was. Run: bun tools/lnurl-cors-fallback-test.js
+import assert from 'node:assert/strict';
+import { fetchPayParams } from '../src/lnurl.js';
+
+const pay = { tag: 'payRequest', callback: 'https://api.example/cb', minSendable: 1000, maxSendable: 1e9, metadata: '[]' };
+let calls = [];
+const install = (handler) => { calls = []; globalThis.fetch = async (url) => { calls.push(String(url)); return handler(String(url)); }; };
+
+// the browser can't follow the host's redirect → the registrar's copy
+install((u) => {
+ if (u.startsWith('https://speed.example/')) throw new TypeError('Failed to fetch');
+ if (u.startsWith('https://names.coinos.io/lnurlp-fetch?url=')) {
+ assert.equal(decodeURIComponent(u.split('url=')[1]), 'https://speed.example/.well-known/lnurlp/bob');
+ return Response.json(pay);
+ }
+ throw new Error('unexpected ' + u);
+});
+const p = await fetchPayParams('https://speed.example/.well-known/lnurlp/bob');
+assert.equal(p.callback, 'https://api.example/cb');
+assert.equal(calls.length, 2);
+
+// a host that works is fetched directly, once
+install(() => Response.json(pay));
+await fetchPayParams('https://ok.example/.well-known/lnurlp/bob');
+assert.deepEqual(calls, ['https://ok.example/.well-known/lnurlp/bob']);
+
+// the host's own error isn't retried elsewhere
+install(() => Response.json({ status: 'ERROR', reason: 'no such user' }, { status: 404 }));
+await assert.rejects(fetchPayParams('https://ok.example/.well-known/lnurlp/nobody'), /no such user/);
+assert.equal(calls.length, 1);
+
+// unreachable both ways: the original "couldn't reach" stands
+install((u) => { if (u.includes('names.coinos.io')) return Response.json({ status: 'ERROR', reason: "couldn't reach that address" }, { status: 502 }); throw new TypeError('Failed to fetch'); });
+await assert.rejects(fetchPayParams('https://down.example/.well-known/lnurlp/bob'), /couldn’t reach down\.example/);
+
+// an lnurl1 that isn't a Lightning address path isn't sent to the registrar
+install(() => { throw new TypeError('Failed to fetch'); });
+await assert.rejects(fetchPayParams('https://down.example/lnurl/pay/abc'), /couldn’t reach/);
+assert.equal(calls.length, 1);
+
+console.log('lnurl-cors-fallback-test: ok');