|
3 | 3 | inputBufGrow, |
4 | 4 | encodeFromInputBuf, |
5 | 5 | encodeResultPtr, |
| 6 | + decodeNodeToPacked, |
| 7 | + shrinkBuffers, |
6 | 8 | type BinaryNode, |
7 | 9 | } from "../pkg/whatsapp_rust_bridge.js"; |
8 | 10 |
|
@@ -194,3 +196,111 @@ export function encodeNode(node: BinaryNode): Uint8Array { |
194 | 196 | // Single JS-native slice: one alloc + one memcpy, no FFI crossings |
195 | 197 | return new Uint8Array(wasmMemory.buffer, ptr, len).slice(); |
196 | 198 | } |
| 199 | + |
| 200 | +// ── Fast decodeNode via packed binary protocol ────────────────────────── |
| 201 | +// |
| 202 | +// Rust decodes WA binary → NodeRef → packed LNP buffer in WASM memory. |
| 203 | +// JS reads the buffer and constructs plain { tag, attrs, content } objects. |
| 204 | +// One FFI call, zero wrappers, zero FinalizationRegistry. |
| 205 | + |
| 206 | +const textDecoder = new TextDecoder(); |
| 207 | + |
| 208 | +/** Read a u16-LE-prefixed UTF-8 string. ASCII fast path for short strings. */ |
| 209 | +function readStr16(mem: Uint8Array, p: number[]): string { |
| 210 | + const len = mem[p[0]] | (mem[p[0] + 1] << 8); |
| 211 | + p[0] += 2; |
| 212 | + if (len < 64) { |
| 213 | + const start = p[0]; |
| 214 | + for (let i = 0; i < len; i++) { |
| 215 | + if (mem[start + i] > 0x7f) { |
| 216 | + // Non-ASCII: fall back to TextDecoder |
| 217 | + const str = textDecoder.decode( |
| 218 | + new Uint8Array(mem.buffer, mem.byteOffset + start, len), |
| 219 | + ); |
| 220 | + p[0] += len; |
| 221 | + return str; |
| 222 | + } |
| 223 | + } |
| 224 | + // All ASCII: build string from char codes |
| 225 | + let s = ""; |
| 226 | + for (let i = 0; i < len; i++) { |
| 227 | + s += String.fromCharCode(mem[start + i]); |
| 228 | + } |
| 229 | + p[0] += len; |
| 230 | + return s; |
| 231 | + } |
| 232 | + const str = textDecoder.decode( |
| 233 | + new Uint8Array(mem.buffer, mem.byteOffset + p[0], len), |
| 234 | + ); |
| 235 | + p[0] += len; |
| 236 | + return str; |
| 237 | +} |
| 238 | + |
| 239 | +/** Unpack a single BinaryNode from packed LNP buffer. */ |
| 240 | +function unpackNode(mem: Uint8Array, p: number[]): BinaryNode { |
| 241 | + const tag = readStr16(mem, p); |
| 242 | + |
| 243 | + const attrCount = mem[p[0]] | (mem[p[0] + 1] << 8); |
| 244 | + p[0] += 2; |
| 245 | + const attrs: Record<string, string> = {}; |
| 246 | + for (let i = 0; i < attrCount; i++) { |
| 247 | + const k = readStr16(mem, p); |
| 248 | + attrs[k] = readStr16(mem, p); |
| 249 | + } |
| 250 | + |
| 251 | + const contentType = mem[p[0]++]; |
| 252 | + let content: BinaryNode["content"] = undefined; |
| 253 | + |
| 254 | + if (contentType === 1) { |
| 255 | + // String |
| 256 | + const len = |
| 257 | + mem[p[0]] | |
| 258 | + (mem[p[0] + 1] << 8) | |
| 259 | + (mem[p[0] + 2] << 16) | |
| 260 | + (mem[p[0] + 3] << 24); |
| 261 | + p[0] += 4; |
| 262 | + content = textDecoder.decode( |
| 263 | + new Uint8Array(mem.buffer, mem.byteOffset + p[0], len), |
| 264 | + ); |
| 265 | + p[0] += len; |
| 266 | + } else if (contentType === 2) { |
| 267 | + // Bytes — .slice() to avoid retaining WASM memory |
| 268 | + const len = |
| 269 | + (mem[p[0]] | |
| 270 | + (mem[p[0] + 1] << 8) | |
| 271 | + (mem[p[0] + 2] << 16) | |
| 272 | + (mem[p[0] + 3] << 24)) >>> |
| 273 | + 0; |
| 274 | + p[0] += 4; |
| 275 | + content = mem.slice(p[0], p[0] + len); |
| 276 | + p[0] += len; |
| 277 | + } else if (contentType === 3) { |
| 278 | + // Child nodes |
| 279 | + const count = mem[p[0]] | (mem[p[0] + 1] << 8); |
| 280 | + p[0] += 2; |
| 281 | + const children: BinaryNode[] = new Array(count); |
| 282 | + for (let i = 0; i < count; i++) { |
| 283 | + children[i] = unpackNode(mem, p); |
| 284 | + } |
| 285 | + content = children; |
| 286 | + } |
| 287 | + |
| 288 | + return { tag, attrs, content }; |
| 289 | +} |
| 290 | + |
| 291 | +/** |
| 292 | + * Decode WhatsApp binary format to a plain BinaryNode object. |
| 293 | + * Rust decodes + serializes to packed LNP → JS reads from WASM memory. |
| 294 | + * One FFI call, zero wrappers, zero FinalizationRegistry. |
| 295 | + */ |
| 296 | +export function decodeNode(data: Uint8Array): BinaryNode { |
| 297 | + decodeNodeToPacked(data); |
| 298 | + |
| 299 | + const mem = new Uint8Array(wasmMemory.buffer); |
| 300 | + const a = encResultAddr; |
| 301 | + const ptr = |
| 302 | + (mem[a] | (mem[a + 1] << 8) | (mem[a + 2] << 16) | (mem[a + 3] << 24)) >>> |
| 303 | + 0; |
| 304 | + |
| 305 | + return unpackNode(mem, [ptr]); |
| 306 | +} |
0 commit comments