diff --git a/scripts/test-typescript-package-closure.mjs b/scripts/test-typescript-package-closure.mjs index 58a40ae7..914cf8cf 100644 --- a/scripts/test-typescript-package-closure.mjs +++ b/scripts/test-typescript-package-closure.mjs @@ -84,14 +84,51 @@ const mesh = require("cultmesh-ts"); assert.equal(typeof cache.defineDocumentType, "function"); // The document owner is also an entry of its own, which the browser client imports without the Node-only cache. assert.equal(documentEntry.decodeDocumentPayload, cache.decodeDocumentPayload); -// No entry exports a decoder or formatter that reads a payload around its schema: by shape, every packed export whose name -// says it decodes, reads a wire or formats is enumerated, and the only one allowed is the schema-checked owner. +// No entry exports a function that reads a payload around its schema, whatever it is called, wherever it is mounted: the +// package-internal readers are loaded from the packed dist files by path and looked for by identity in every packed +// entry (own names, 'default', and the members of any namespace or function export), and every exported function is +// shown a MessagePack map and must not hand it back decoded. The only reader allowed is the schema-checked owner. +const path = require("node:path"); +const msgpack = require("@msgpack/msgpack"); +const dist = path.dirname(require.resolve("@gamecult/cultcache-ts")); +const internalReaders = new Map([ + ["readWire", require(path.join(dist, "wire.js")).readWire], + ["resolveDocumentFormatter", require(path.join(dist, "document-formatter.js")).resolveDocumentFormatter], + ["@msgpack/msgpack decode", msgpack.decode], + ["@msgpack/msgpack decodeMulti", msgpack.decodeMulti], +]); +for (const [name, reader] of internalReaders) assert.equal(typeof reader, "function", name + " was found in the packed package"); const inspection = require("@gamecult/cultcache-ts/inspection"); -const decoderShaped = (entry) => Object.keys(entry).filter((name) => /decode|formatter|readwire/i.test(name)).sort(); -for (const [entryName, entry] of Object.entries({ root: cache, codecs: cache.codecs, document: documentEntry, inspection })) { - const found = decoderShaped(entry); - const allowed = entryName === "root" || entryName === "document" ? ["decodeDocumentPayload"] : []; - assert.deepEqual(found, allowed, "decoder-shaped exports of the " + entryName + " entry"); +const entries = { root: cache, codecs: cache.codecs, document: documentEntry, inspection }; +const probePayload = msgpack.encode({ probe: 7 }); +const exportsOf = (entry) => { + const found = []; + const seen = new Set(); + const visit = (value, where, depth) => { + if ((typeof value !== "function" && (typeof value !== "object" || value === null)) || seen.has(value) || depth > 4) return; + seen.add(value); + found.push([where, value]); + for (const key of Object.getOwnPropertyNames(value)) { + if (typeof value === "function" && ["length", "name", "prototype", "caller", "arguments"].includes(key)) continue; + let member; + try { member = value[key]; } catch { continue; } + visit(member, where + "." + key, depth + 1); + } + }; + for (const key of Object.getOwnPropertyNames(entry)) visit(entry[key], key, 0); + return found; +}; +for (const [entryName, entry] of Object.entries(entries)) { + for (const [where, value] of exportsOf(entry)) { + for (const [name, reader] of internalReaders) { + assert.notEqual(value, reader, entryName + " entry exports " + name + " as " + where); + } + if (typeof value !== "function" || value === cache.decodeDocumentPayload) continue; + let returned; + try { returned = value(probePayload); } catch { continue; } + assert.ok(!JSON.stringify(returned, (_key, member) => (member instanceof Map ? Object.fromEntries(member) : member))?.includes('"probe":7'), + entryName + " entry exports " + where + ", which reads a payload without its schema"); + } } assert.equal(typeof documentEntry.checkPayloadDepth, "undefined"); assert.equal(documentEntry.MAX_PAYLOAD_DEPTH, 256);