Skip to content

Commit 14e675d

Browse files
authored
Merge pull request #54 from missing-elements/feat/readable-source-link
Build page: read a source written into the link as readable JSON
2 parents e6aab10 + 2b26f89 commit 14e675d

1 file changed

Lines changed: 71 additions & 6 deletions

File tree

‎src/h5p/build/build-source.ts‎

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,36 @@ export interface TypeFile {
2525
}
2626

2727
/**
28-
* The source a link carries in its fragment, `#source=<base64url>`, which the browser never
29-
* sends to a server. The bytes are the source's JSON, as UTF-8, or that JSON compressed with
30-
* raw deflate, which a chat's sandbox writes with Node's `zlib.deflateRawSync` or Python's
31-
* `zlib.compressobj(wbits=-15)` to keep the link short. Returns `null` for a fragment that
32-
* carries none, and throws for one that carries something else.
28+
* The source a link carries in its fragment, `#source=`, which the browser never sends to a
29+
* server, in either of two forms:
30+
*
31+
* - `#source=<base64url>`: the source's JSON, as UTF-8, or that JSON compressed with raw deflate,
32+
* which a chat's sandbox writes with Node's `zlib.deflateRawSync` or Python's
33+
* `zlib.compressobj(wbits=-15)` to keep the link short.
34+
* - `#source={…}`: the JSON itself, which a chat with no code tool writes into the link, since a
35+
* model can write its own JSON but cannot compress it (h5p-offline-player#206). The value runs to
36+
* the end of the fragment, since JSON text holds `&` and `+`, and it is percent-decoded as far as
37+
* it can be (`percentDecoded`). Base64url never holds `{` or `%`, so the forms cannot be mistaken.
38+
* Up to three braces or brackets left open at its end are closed (`withClosers`). Its text is
39+
* returned even where it is still not JSON: a weak model's JSON slips, and text that can
40+
* be read goes in the box, where the build lists where the JSON breaks, for the chat to fix, as
41+
* for a source pasted in. Only a compressed source that cannot be read is a damaged link.
42+
*
43+
* Returns `null` for a fragment that carries none, and throws for one that carries something else.
3344
*/
3445
export async function sourceFromFragment(hash: string): Promise<string | null> {
35-
const value = new URLSearchParams(hash.replace(/^#/, '')).get('source')
46+
const fragment = hash.replace(/^#/, '')
47+
const readable = /(?:^|&)source=(?=\{|%7B)/i.exec(fragment)
48+
if (readable) {
49+
const text = percentDecoded(fragment.slice(readable.index + readable[0].length))
50+
if (isJson(text)) return text
51+
// A link set in a sentence may end in what follows it, as `}.`: the JSON is up to its last brace.
52+
const upToBrace = text.slice(0, text.lastIndexOf('}') + 1)
53+
if (isJson(upToBrace)) return upToBrace
54+
const closed = withClosers(text)
55+
return closed !== null && isJson(closed) ? closed : text
56+
}
57+
const value = new URLSearchParams(fragment).get('source')
3658
if (!value) return null
3759
let bytes: Uint8Array<ArrayBuffer>
3860
try {
@@ -53,6 +75,49 @@ export async function sourceFromFragment(hash: string): Promise<string | null> {
5375
throw new Error('The link’s #source= is neither JSON nor deflated JSON in base64url')
5476
}
5577

78+
/**
79+
* The JSON with the braces and brackets still open at its end closed, up to three of them, or
80+
* `null` where it ends inside a string or has more open. A model that writes a source on one line
81+
* loses count of its closers: Haiku left one `}` out of a True/False's 600 characters
82+
* (h5p-offline-player#206). What is closed at the end is only where the source ends, and the
83+
* build still checks every field of it.
84+
*/
85+
function withClosers(text: string): string | null {
86+
const open: string[] = []
87+
let inString = false
88+
let escaped = false
89+
for (const char of text) {
90+
if (inString) {
91+
if (escaped) escaped = false
92+
else if (char === '\\') escaped = true
93+
else if (char === '"') inString = false
94+
} else if (char === '"') inString = true
95+
else if (char === '{') open.push('}')
96+
else if (char === '[') open.push(']')
97+
else if (char === '}' || char === ']') {
98+
if (open.pop() !== char) return null
99+
}
100+
}
101+
if (inString || !open.length || open.length > 3) return null
102+
return text.trimEnd() + open.reverse().join('')
103+
}
104+
105+
/**
106+
* Percent-decodes JSON a model wrote into a link, as far as it can: each run of escapes is decoded
107+
* where it is UTF-8 and kept as written where it is not, and a `%` that starts no escape is a `%`.
108+
* The browser escapes some characters itself as it opens the link (a space, `"`, `<`, `>`, and any
109+
* letter outside ASCII), and the model escapes the few a Markdown link cannot hold.
110+
*/
111+
function percentDecoded(text: string): string {
112+
return text.replace(/(?:%[0-9A-Fa-f]{2})+/g, (run) => {
113+
try {
114+
return decodeURIComponent(run)
115+
} catch {
116+
return run
117+
}
118+
})
119+
}
120+
56121
function isJson(text: string): boolean {
57122
if (!/^\s*\{/.test(text)) return false
58123
try {

0 commit comments

Comments
 (0)