javascript-solid-server 0.0.223 → 0.0.224

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-solid-server",
3
- "version": "0.0.223",
3
+ "version": "0.0.224",
4
4
  "description": "A minimal, fast Solid server",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -233,34 +233,11 @@ export async function registerNostrRelay(fastify, options = {}) {
233
233
  await fastify.register(websocket);
234
234
  }
235
235
 
236
- // Register WebSocket route for Nostr relay
237
- fastify.get(path, { websocket: true }, (connection, request) => {
238
- const socket = connection.socket;
239
-
240
- socket.on('message', async (data) => {
241
- try {
242
- const message = JSON.parse(data.toString());
243
- const [type, value, ...rest] = message;
244
- await processMessage(type, value, rest, socket);
245
- } catch (e) {
246
- socket.send(JSON.stringify(['NOTICE', `Error: ${e.message}`]));
247
- }
248
- });
249
-
250
- socket.on('close', () => {
251
- subscribers.delete(socket);
252
- rateLimits.delete(socket);
253
- });
254
-
255
- socket.on('error', () => {
256
- subscribers.delete(socket);
257
- rateLimits.delete(socket);
258
- });
259
- });
260
-
261
- // NIP-11: Relay Information Document at /relay/info
262
- fastify.get(path + '/info', (request, reply) => {
263
- const relayInfo = {
236
+ // NIP-11 relay information document. Served at the relay URL itself when
237
+ // the client sends `Accept: application/nostr+json` (what the spec and
238
+ // tools such as `nak relay` expect), and at <path>/info for older callers.
239
+ function relayInfo() {
240
+ return {
264
241
  name: 'JSS Nostr Relay',
265
242
  description: 'Nostr relay integrated with JavaScript Solid Server',
266
243
  pubkey: '',
@@ -269,11 +246,55 @@ export async function registerNostrRelay(fastify, options = {}) {
269
246
  software: 'https://github.com/JavaScriptSolidServer/JavaScriptSolidServer',
270
247
  version: '0.0.1'
271
248
  };
249
+ }
250
+
251
+ function wantsRelayInfo(request) {
252
+ return (request.headers.accept || '').toLowerCase().includes('application/nostr+json');
253
+ }
254
+
255
+ // Register the relay route: WebSocket upgrades go to wsHandler, plain HTTP
256
+ // GETs to handler (NIP-11 when asked for, 404 otherwise, as before).
257
+ fastify.route({
258
+ method: 'GET',
259
+ url: path,
260
+ handler: (request, reply) => {
261
+ if (!wantsRelayInfo(request)) return reply.code(404).send();
262
+ return reply
263
+ .header('Access-Control-Allow-Origin', '*')
264
+ .header('Content-Type', 'application/nostr+json')
265
+ .send(JSON.stringify(relayInfo()));
266
+ },
267
+ wsHandler: (connection, request) => {
268
+ const socket = connection.socket;
269
+
270
+ socket.on('message', async (data) => {
271
+ try {
272
+ const message = JSON.parse(data.toString());
273
+ const [type, value, ...rest] = message;
274
+ await processMessage(type, value, rest, socket);
275
+ } catch (e) {
276
+ socket.send(JSON.stringify(['NOTICE', `Error: ${e.message}`]));
277
+ }
278
+ });
279
+
280
+ socket.on('close', () => {
281
+ subscribers.delete(socket);
282
+ rateLimits.delete(socket);
283
+ });
272
284
 
285
+ socket.on('error', () => {
286
+ subscribers.delete(socket);
287
+ rateLimits.delete(socket);
288
+ });
289
+ }
290
+ });
291
+
292
+ // NIP-11 at <path>/info, kept for existing callers
293
+ fastify.get(path + '/info', (request, reply) => {
273
294
  return reply
274
295
  .header('Access-Control-Allow-Origin', '*')
275
296
  .header('Content-Type', 'application/json')
276
- .send(relayInfo);
297
+ .send(relayInfo());
277
298
  });
278
299
 
279
300
  return {
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Nostr relay NIP-11 tests
3
+ *
4
+ * NIP-11 says the relay information document is served at the relay's own
5
+ * URL when the client sends `Accept: application/nostr+json`. Tools such as
6
+ * `nak relay` only ask there, so serving it at <path>/info alone made the
7
+ * relay look non-compliant. The WebSocket relay on the same path must keep
8
+ * working.
9
+ */
10
+
11
+ import { describe, it, before, after } from 'node:test';
12
+ import assert from 'node:assert';
13
+ import { mkdtempSync } from 'node:fs';
14
+ import { tmpdir } from 'node:os';
15
+ import { join } from 'node:path';
16
+ import { WebSocket } from 'ws';
17
+ import { createServer } from '../src/server.js';
18
+
19
+ describe('Nostr relay NIP-11', () => {
20
+ let fastify;
21
+ let base;
22
+
23
+ before(async () => {
24
+ const root = mkdtempSync(join(tmpdir(), 'jss-nip11-'));
25
+ fastify = createServer({ root, port: 0, logger: false, nostr: true });
26
+ await fastify.listen({ port: 0, host: '127.0.0.1' });
27
+ base = `127.0.0.1:${fastify.server.address().port}`;
28
+ });
29
+
30
+ after(async () => {
31
+ await fastify.close();
32
+ });
33
+
34
+ it('serves the info document at the relay URL for Accept: application/nostr+json', async () => {
35
+ const res = await fetch(`http://${base}/relay`, { headers: { Accept: 'application/nostr+json' } });
36
+ assert.strictEqual(res.status, 200);
37
+ assert.match(res.headers.get('content-type'), /application\/nostr\+json/);
38
+ assert.strictEqual(res.headers.get('access-control-allow-origin'), '*');
39
+ const info = await res.json();
40
+ assert.ok(info.supported_nips.includes(11));
41
+ });
42
+
43
+ it('still returns 404 for a plain GET of the relay URL', async () => {
44
+ const res = await fetch(`http://${base}/relay`);
45
+ assert.strictEqual(res.status, 404);
46
+ });
47
+
48
+ it('keeps serving the document at /relay/info', async () => {
49
+ const res = await fetch(`http://${base}/relay/info`);
50
+ assert.strictEqual(res.status, 200);
51
+ const info = await res.json();
52
+ assert.ok(info.supported_nips.includes(11));
53
+ });
54
+
55
+ it('still accepts WebSocket subscriptions on the relay URL', async () => {
56
+ const ws = new WebSocket(`ws://${base}/relay`);
57
+ const msg = await new Promise((resolve, reject) => {
58
+ ws.on('open', () => ws.send(JSON.stringify(['REQ', 'sub1', { kinds: [1], limit: 1 }])));
59
+ ws.on('message', (data) => resolve(JSON.parse(data.toString())));
60
+ ws.on('error', reject);
61
+ });
62
+ ws.close();
63
+ assert.deepStrictEqual(msg, ['EOSE', 'sub1']);
64
+ });
65
+ });
@@ -1,38 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(gh issue *)",
5
- "WebFetch(domain:did-nostr.com)",
6
- "WebFetch(domain:nostrcg.github.io)",
7
- "Bash(node --test test/nostr-key-parity.test.js)",
8
- "Bash(node --test test/nostr-cid-vm.test.js test/well-known-did-nostr.test.js test/keys-provision.test.js test/keys-provision-integration.test.js test/nostr-event.test.js test/well-known-nostr-json.test.js)",
9
- "Bash(npm test *)",
10
- "Bash(git checkout *)",
11
- "Bash(git add *)",
12
- "Bash(cd *)",
13
- "Bash(git push *)",
14
- "Bash(gh pr create --base gh-pages --title 'fix\\(nostr\\): reconcile JWK key path to accept both Y-parities \\(#571\\)' --body ' *)",
15
- "Bash(gh pr create --repo JavaScriptSolidServer/JavaScriptSolidServer --base gh-pages --head issue-571-multikey-jwk-parity-consistency --title 'fix\\(nostr\\): reconcile JWK key path to accept both Y-parities \\(#571\\)' --body ' *)",
16
- "Bash(gh pr *)",
17
- "Bash(gh api *)",
18
- "Bash(node --test test/nostr-key-parity.test.js test/nostr-cid-vm.test.js test/well-known-did-nostr.test.js)",
19
- "Bash(git commit *)",
20
- "Bash(git config *)",
21
- "Bash(FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --msg-filter 'grep -v -e \"^Co-Authored-By: Claude\" -e \"Generated with \\\\[Claude Code\\\\]\"' gh-pages..HEAD)",
22
- "Bash(git fetch *)",
23
- "Bash(git ls-remote *)",
24
- "Bash(echo \"exit=$?\")",
25
- "Bash(cd /home/melvin/.claude/projects/-home-melvin-remote-github-com-JavaScriptSolidServer-JavaScriptSolidServer/memory/)",
26
- "Bash(cat MEMORY.md)",
27
- "Bash(FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --env-filter ' *)",
28
- "Bash(git pull *)",
29
- "Bash(git branch *)",
30
- "Bash(git remote *)",
31
- "Bash(node --check src/handlers/container.js)",
32
- "Bash(node --test /tmp/claude-1000/-home-melvin-remote-github-com-JavaScriptSolidServer-JavaScriptSolidServer/47629faf-bc8f-4e26-9878-091c550812d5/scratchpad/repro.test.js)",
33
- "Bash(node --test test/auth.test.js)",
34
- "Bash(git stash *)",
35
- "Bash(node --check src/wac/checker.js)"
36
- ]
37
- }
38
- }