Skip to content

Commit 060d959

Browse files
committed
Read a source written into the build page's link as readable JSON
1 parent e6aab10 commit 060d959

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,31 @@ 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+
*
39+
* Returns `null` for a fragment that carries none, and throws for one that carries something else.
3340
*/
3441
export async function sourceFromFragment(hash: string): Promise<string | null> {
35-
const value = new URLSearchParams(hash.replace(/^#/, '')).get('source')
42+
const fragment = hash.replace(/^#/, '')
43+
const readable = /(?:^|&)source=(?=\{|%7B)/i.exec(fragment)
44+
if (readable) {
45+
const text = percentDecoded(fragment.slice(readable.index + readable[0].length))
46+
if (isJson(text)) return text
47+
// A link set in a sentence may end in what follows it, as `}.`: the JSON is up to its last brace.
48+
const upToBrace = text.slice(0, text.lastIndexOf('}') + 1)
49+
if (isJson(upToBrace)) return upToBrace
50+
throw new Error('The link’s #source= holds JSON that does not parse')
51+
}
52+
const value = new URLSearchParams(fragment).get('source')
3653
if (!value) return null
3754
let bytes: Uint8Array<ArrayBuffer>
3855
try {
@@ -53,6 +70,22 @@ export async function sourceFromFragment(hash: string): Promise<string | null> {
5370
throw new Error('The link’s #source= is neither JSON nor deflated JSON in base64url')
5471
}
5572

73+
/**
74+
* Percent-decodes JSON a model wrote into a link, as far as it can: each run of escapes is decoded
75+
* where it is UTF-8 and kept as written where it is not, and a `%` that starts no escape is a `%`.
76+
* The browser escapes some characters itself as it opens the link (a space, `"`, `<`, `>`, and any
77+
* letter outside ASCII), and the model escapes the few a Markdown link cannot hold.
78+
*/
79+
function percentDecoded(text: string): string {
80+
return text.replace(/(?:%[0-9A-Fa-f]{2})+/g, (run) => {
81+
try {
82+
return decodeURIComponent(run)
83+
} catch {
84+
return run
85+
}
86+
})
87+
}
88+
5689
function isJson(text: string): boolean {
5790
if (!/^\s*\{/.test(text)) return false
5891
try {

0 commit comments

Comments
 (0)