libp2r2p 0.10.7 → 0.10.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/url/app-url.js +50 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2r2p",
3
- "version": "0.10.7",
3
+ "version": "0.10.8",
4
4
  "description": "Peer-to-relay-to-peer",
5
5
  "keywords": [
6
6
  "p2r2p",
package/url/app-url.js CHANGED
@@ -9,11 +9,25 @@ import {
9
9
  } from '../nip27/helpers/user-reference.js'
10
10
  import {
11
11
  NAPP_ENTITY_REGEX,
12
- appDecode
12
+ appDecode,
13
+ appEncode,
14
+ naddrDecode
13
15
  } from '../nip19/index.js'
16
+ import {
17
+ DRAFT_SITE_MANIFEST,
18
+ MAIN_SITE_MANIFEST,
19
+ NEXT_SITE_MANIFEST
20
+ } from '../kind/index.js'
14
21
 
15
22
  export const APP_URL_MIN_ENTITY_BODY_LENGTH = 48
16
23
 
24
+ const SITE_MANIFEST_KINDS = new Set([
25
+ MAIN_SITE_MANIFEST,
26
+ NEXT_SITE_MANIFEST,
27
+ DRAFT_SITE_MANIFEST
28
+ ])
29
+ const NADDR_PREFIX = 'naddr1'
30
+
17
31
  const APP_NAME_MAX_LENGTH = 260
18
32
  const CHANNEL_BY_PREFIX = { '+': 'main', '++': 'next', '+++': 'draft' }
19
33
  const PREFIX_BY_CHANNEL = { main: '+', next: '++', draft: '+++' }
@@ -38,6 +52,38 @@ function isValidAppName (appName) {
38
52
  !CONTROL_CHARS.test(appName)
39
53
  }
40
54
 
55
+ // `naddr` already carries the event kind, so it does not need the `+`/`++`/
56
+ // `+++` channel prefix (a leading prefix is still tolerated). Only site
57
+ // manifests are app URLs; the result is canonicalized to the `appEncode`
58
+ // entity so downstream code keeps working unchanged.
59
+ function tryDecodeNaddr (value) {
60
+ if (typeof value !== 'string' || !value) return null
61
+ const body = value.replace(/^\+{1,3}/, '')
62
+ if (!body.startsWith(NADDR_PREFIX)) return null
63
+
64
+ let decoded
65
+ try {
66
+ decoded = naddrDecode(body)
67
+ } catch {
68
+ return null
69
+ }
70
+ if (!SITE_MANIFEST_KINDS.has(decoded.kind)) return null
71
+
72
+ try {
73
+ return {
74
+ type: 'entity',
75
+ entity: appEncode({
76
+ dTag: decoded.identifier,
77
+ pubkey: decoded.pubkey,
78
+ kind: decoded.kind,
79
+ relays: decoded.relays
80
+ })
81
+ }
82
+ } catch {
83
+ return null
84
+ }
85
+ }
86
+
41
87
  // Decodes a raw (still percent-encoded) first path segment such as
42
88
  // `+apps` or `+caf%C3%A9@bob@example.com` or `+3swFhu...`.
43
89
  // Returns:
@@ -47,6 +93,9 @@ function isValidAppName (appName) {
47
93
  // - `null` when the segment is not a valid app URL.
48
94
  export function decodeAppUrl (segment) {
49
95
  if (typeof segment !== 'string' || !segment) return null
96
+ const decodedNaddr = tryDecodeNaddr(segment)
97
+ if (decodedNaddr) return decodedNaddr
98
+
50
99
  const prefixMatch = segment.match(/^\+{1,3}/)
51
100
  if (!prefixMatch) return null
52
101
  const prefix = prefixMatch[0]