From 060d959996665043f7245834a6b817ebfc0fb607 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Sun, 15 Nov 2026 01:35:47 +0200 Subject: [PATCH 1/3] Read a source written into the build page's link as readable JSON --- src/h5p/build/build-source.ts | 45 ++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/h5p/build/build-source.ts b/src/h5p/build/build-source.ts index f335f82..27c1866 100644 --- a/src/h5p/build/build-source.ts +++ b/src/h5p/build/build-source.ts @@ -25,14 +25,31 @@ export interface TypeFile { } /** - * The source a link carries in its fragment, `#source=`, which the browser never - * sends to a server. The bytes are the source's JSON, as UTF-8, or that JSON compressed with - * raw deflate, which a chat's sandbox writes with Node's `zlib.deflateRawSync` or Python's - * `zlib.compressobj(wbits=-15)` to keep the link short. Returns `null` for a fragment that - * carries none, and throws for one that carries something else. + * The source a link carries in its fragment, `#source=`, which the browser never sends to a + * server, in either of two forms: + * + * - `#source=`: the source's JSON, as UTF-8, or that JSON compressed with raw deflate, + * which a chat's sandbox writes with Node's `zlib.deflateRawSync` or Python's + * `zlib.compressobj(wbits=-15)` to keep the link short. + * - `#source={…}`: the JSON itself, which a chat with no code tool writes into the link, since a + * model can write its own JSON but cannot compress it (h5p-offline-player#206). The value runs to + * the end of the fragment, since JSON text holds `&` and `+`, and it is percent-decoded as far as + * it can be (`percentDecoded`). Base64url never holds `{` or `%`, so the forms cannot be mistaken. + * + * Returns `null` for a fragment that carries none, and throws for one that carries something else. */ export async function sourceFromFragment(hash: string): Promise { - const value = new URLSearchParams(hash.replace(/^#/, '')).get('source') + const fragment = hash.replace(/^#/, '') + const readable = /(?:^|&)source=(?=\{|%7B)/i.exec(fragment) + if (readable) { + const text = percentDecoded(fragment.slice(readable.index + readable[0].length)) + if (isJson(text)) return text + // A link set in a sentence may end in what follows it, as `}.`: the JSON is up to its last brace. + const upToBrace = text.slice(0, text.lastIndexOf('}') + 1) + if (isJson(upToBrace)) return upToBrace + throw new Error('The link’s #source= holds JSON that does not parse') + } + const value = new URLSearchParams(fragment).get('source') if (!value) return null let bytes: Uint8Array try { @@ -53,6 +70,22 @@ export async function sourceFromFragment(hash: string): Promise { throw new Error('The link’s #source= is neither JSON nor deflated JSON in base64url') } +/** + * Percent-decodes JSON a model wrote into a link, as far as it can: each run of escapes is decoded + * where it is UTF-8 and kept as written where it is not, and a `%` that starts no escape is a `%`. + * The browser escapes some characters itself as it opens the link (a space, `"`, `<`, `>`, and any + * letter outside ASCII), and the model escapes the few a Markdown link cannot hold. + */ +function percentDecoded(text: string): string { + return text.replace(/(?:%[0-9A-Fa-f]{2})+/g, (run) => { + try { + return decodeURIComponent(run) + } catch { + return run + } + }) +} + function isJson(text: string): boolean { if (!/^\s*\{/.test(text)) return false try { From 0612d9f2c97c615e295bd9966ce0a05d86ba0f05 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Sun, 15 Nov 2026 01:38:12 +0200 Subject: [PATCH 2/3] A readable link whose JSON breaks puts its text in the box, with where it breaks, for the chat to fix --- src/h5p/build/build-source.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/h5p/build/build-source.ts b/src/h5p/build/build-source.ts index 27c1866..f5853e2 100644 --- a/src/h5p/build/build-source.ts +++ b/src/h5p/build/build-source.ts @@ -35,6 +35,9 @@ export interface TypeFile { * model can write its own JSON but cannot compress it (h5p-offline-player#206). The value runs to * the end of the fragment, since JSON text holds `&` and `+`, and it is percent-decoded as far as * it can be (`percentDecoded`). Base64url never holds `{` or `%`, so the forms cannot be mistaken. + * Its text is returned even where it is not JSON: a weak model's JSON slips, and text that can + * be read goes in the box, where the build lists where the JSON breaks, for the chat to fix, as + * for a source pasted in. Only a compressed source that cannot be read is a damaged link. * * Returns `null` for a fragment that carries none, and throws for one that carries something else. */ @@ -46,8 +49,7 @@ export async function sourceFromFragment(hash: string): Promise { if (isJson(text)) return text // A link set in a sentence may end in what follows it, as `}.`: the JSON is up to its last brace. const upToBrace = text.slice(0, text.lastIndexOf('}') + 1) - if (isJson(upToBrace)) return upToBrace - throw new Error('The link’s #source= holds JSON that does not parse') + return isJson(upToBrace) ? upToBrace : text } const value = new URLSearchParams(fragment).get('source') if (!value) return null From 2b26f89515ab7f716a417532b62dadd2376d95ec Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Sun, 15 Nov 2026 01:44:29 +0200 Subject: [PATCH 3/3] Close up to three braces or brackets a readable link's JSON leaves open at its end --- src/h5p/build/build-source.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/h5p/build/build-source.ts b/src/h5p/build/build-source.ts index f5853e2..e302a54 100644 --- a/src/h5p/build/build-source.ts +++ b/src/h5p/build/build-source.ts @@ -35,7 +35,8 @@ export interface TypeFile { * model can write its own JSON but cannot compress it (h5p-offline-player#206). The value runs to * the end of the fragment, since JSON text holds `&` and `+`, and it is percent-decoded as far as * it can be (`percentDecoded`). Base64url never holds `{` or `%`, so the forms cannot be mistaken. - * Its text is returned even where it is not JSON: a weak model's JSON slips, and text that can + * Up to three braces or brackets left open at its end are closed (`withClosers`). Its text is + * returned even where it is still not JSON: a weak model's JSON slips, and text that can * be read goes in the box, where the build lists where the JSON breaks, for the chat to fix, as * for a source pasted in. Only a compressed source that cannot be read is a damaged link. * @@ -49,7 +50,9 @@ export async function sourceFromFragment(hash: string): Promise { if (isJson(text)) return text // A link set in a sentence may end in what follows it, as `}.`: the JSON is up to its last brace. const upToBrace = text.slice(0, text.lastIndexOf('}') + 1) - return isJson(upToBrace) ? upToBrace : text + if (isJson(upToBrace)) return upToBrace + const closed = withClosers(text) + return closed !== null && isJson(closed) ? closed : text } const value = new URLSearchParams(fragment).get('source') if (!value) return null @@ -72,6 +75,33 @@ export async function sourceFromFragment(hash: string): Promise { throw new Error('The link’s #source= is neither JSON nor deflated JSON in base64url') } +/** + * The JSON with the braces and brackets still open at its end closed, up to three of them, or + * `null` where it ends inside a string or has more open. A model that writes a source on one line + * loses count of its closers: Haiku left one `}` out of a True/False's 600 characters + * (h5p-offline-player#206). What is closed at the end is only where the source ends, and the + * build still checks every field of it. + */ +function withClosers(text: string): string | null { + const open: string[] = [] + let inString = false + let escaped = false + for (const char of text) { + if (inString) { + if (escaped) escaped = false + else if (char === '\\') escaped = true + else if (char === '"') inString = false + } else if (char === '"') inString = true + else if (char === '{') open.push('}') + else if (char === '[') open.push(']') + else if (char === '}' || char === ']') { + if (open.pop() !== char) return null + } + } + if (inString || !open.length || open.length > 3) return null + return text.trimEnd() + open.reverse().join('') +} + /** * Percent-decodes JSON a model wrote into a link, as far as it can: each run of escapes is decoded * where it is UTF-8 and kept as written where it is not, and a `%` that starts no escape is a `%`.