index.ts (5585B)
1 import { canonicalPublicKey } from '../public-key.ts'; 2 import { boundedUtf8 } from '../food-availability-v1/text.ts'; 3 // Pure projections of Lib189 Message. No signature, participant, relay access, 4 // persistence or sending permission follows from these detached data values. 5 export type MessageRecipient = Readonly<{ 6 public_key: string; 7 relay_url: string | null; 8 }>; 9 export type MessageReply = Readonly<{ id: string; relays: string | null }>; 10 export type MessageV1 = Readonly<{ 11 recipients: readonly MessageRecipient[]; 12 content: string; 13 reply_to: MessageReply | null; 14 subject: string | null; 15 }>; 16 export type MessageWireParts = Readonly<{ 17 kind: 14; 18 tags: readonly (readonly string[])[]; 19 content: string; 20 }>; 21 const bodyBytes = 4096, 22 partsBytes = 8192; 23 // Rust str::trim uses Unicode White_Space, unlike JavaScript trim's BOM rule. 24 const present = (value: string) => /\P{White_Space}/u.test(value); 25 const body = (value: unknown): value is string => 26 typeof value === 'string' && boundedUtf8(value, bodyBytes) && present(value); 27 const text = (value: unknown): value is string => 28 typeof value === 'string' && boundedUtf8(value, partsBytes) && present(value); 29 const hash = (value: unknown): value is string => 30 typeof value === 'string' && /^[0-9a-f]{64}$/.test(value); 31 function fields( 32 value: unknown, 33 required: string[], 34 optional: string[] = [] 35 ): value is Record<string, unknown> { 36 if (typeof value !== 'object' || value === null || Array.isArray(value)) 37 return false; 38 for (const key in value) 39 if (!required.includes(key) && !optional.includes(key)) return false; 40 return required.every((key) => key in value); 41 } 42 function parse(raw: unknown): unknown { 43 if (typeof raw !== 'string' || !boundedUtf8(raw, partsBytes)) 44 return undefined; 45 try { 46 return JSON.parse(raw) as unknown; 47 } catch { 48 return undefined; 49 } 50 } 51 function recipient(value: unknown): MessageRecipient | undefined { 52 if (!fields(value, ['public_key'], ['relay_url'])) return undefined; 53 const key = canonicalPublicKey(value.public_key), 54 hint = value.relay_url ?? null; 55 if (!key || (hint !== null && !text(hint))) return undefined; 56 return { public_key: key, relay_url: hint }; 57 } 58 function reply(value: unknown): MessageReply | null | undefined { 59 if (value === null || value === undefined) return null; 60 if (!fields(value, ['id'], ['relays']) || !hash(value.id)) return undefined; 61 const hint = value.relays ?? null; 62 if (hint !== null && !text(hint)) return undefined; 63 return { id: value.id, relays: hint }; 64 } 65 function subject(value: unknown): string | null | undefined { 66 if (value === null || value === undefined) return null; 67 return text(value) ? value : undefined; 68 } 69 function message(value: unknown): MessageV1 | undefined { 70 if ( 71 !fields(value, ['recipients', 'content'], ['reply_to', 'subject']) || 72 !body(value.content) || 73 !Array.isArray(value.recipients) || 74 !value.recipients.length 75 ) 76 return undefined; 77 const rows: unknown[] = value.recipients; 78 const recipients = rows.map(recipient); 79 if ( 80 !recipients.every( 81 (person): person is MessageRecipient => person !== undefined 82 ) 83 ) 84 return undefined; 85 const parent = reply(value.reply_to), 86 title = subject(value.subject); 87 if (parent === undefined || title === undefined) return undefined; 88 return { 89 recipients, 90 content: value.content, 91 reply_to: parent, 92 subject: title 93 }; 94 } 95 export function messageToWireParts(raw: unknown): MessageWireParts | undefined { 96 const value = message(parse(raw)); 97 if (!value) return undefined; 98 const tags: string[][] = [ 99 ...value.recipients.map((person) => 100 person.relay_url === null 101 ? ['p', person.public_key] 102 : ['p', person.public_key, person.relay_url] 103 ), 104 ...(value.reply_to 105 ? [ 106 value.reply_to.relays === null 107 ? ['e', value.reply_to.id] 108 : ['e', value.reply_to.id, value.reply_to.relays] 109 ] 110 : []), 111 ...(value.subject !== null ? [['subject', value.subject]] : []) 112 ]; 113 const parts = { 114 kind: 14 as const, 115 tags, 116 content: value.content 117 }; 118 return boundedUtf8(JSON.stringify(parts), partsBytes) ? parts : undefined; 119 } 120 function tag(value: unknown): value is string[] { 121 if (!Array.isArray(value)) return false; 122 const values: unknown[] = value; 123 return values.every( 124 (item) => typeof item === 'string' && item.isWellFormed() 125 ); 126 } 127 export function messageFromWireParts(raw: unknown): MessageV1 | undefined { 128 const value = parse(raw); 129 if ( 130 !fields(value, ['kind', 'tags', 'content']) || 131 value.kind !== 14 || 132 !body(value.content) || 133 !Array.isArray(value.tags) 134 ) 135 return undefined; 136 const rows: unknown[] = value.tags; 137 if (!rows.every(tag)) return undefined; 138 const tags = rows; 139 const recipients = tags 140 .filter((row) => row[0] === 'p') 141 .map((row) => 142 recipient({ 143 public_key: row[1], 144 relay_url: row[2] ?? null 145 }) 146 ); 147 if ( 148 !recipients.length || 149 !recipients.every( 150 (person): person is MessageRecipient => person !== undefined 151 ) 152 ) 153 return undefined; 154 const parentTag = tags.find((row) => row[0] === 'e'), 155 titleTag = tags.find((row) => row[0] === 'subject'); 156 const parent = parentTag 157 ? reply({ id: parentTag[1], relays: parentTag[2] ?? null }) 158 : null; 159 const title = titleTag ? subject(titleTag[1] ?? '') : null; 160 if (parent === undefined || title === undefined) return undefined; 161 return { 162 recipients, 163 content: value.content, 164 reply_to: parent, 165 subject: title 166 }; 167 }