Skip to content

Commit 7a932df

Browse files
committed
Support gzipped files in single file mode (curiousdannii/parchment#146)
1 parent 3b5b811 commit 7a932df

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"base32768": "^3.0.1",
1717
"body-scroll-lock": "^4.0.0-beta.0",
18+
"fflate": "^0.8.2",
1819
"file-saver": "^2.0.5",
1920
"glkaudio": "file:src/glkaudio/pkg",
2021
"lodash-es": "^4.17.21",

src/common/file.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ https://github.com/curiousdannii/asyncglk
99
1010
*/
1111

12+
import {gunzipSync} from 'fflate'
13+
1214
export type ProgressCallback = (bytes: number) => void
1315

1416
export type TruthyOption = boolean | number
@@ -48,14 +50,19 @@ export async function fetch_resource(options: DownloadOptions, path: string, pro
4850
async function fetch_resource_inner(options: DownloadOptions, path: string, progress_callback?: ProgressCallback) {
4951
// Handle embedded resources in single file mode
5052
if (options.single_file) {
51-
const data = (document.getElementById(path) as HTMLScriptElement).text
53+
const node = document.getElementById(path) as HTMLScriptElement
54+
const data_base64 = node.text
5255
if (path.endsWith('.js')) {
53-
return import(`data:text/javascript,${encodeURIComponent(data)}`)
56+
return import(`data:text/javascript,${encodeURIComponent(data_base64)}`)
5457
}
5558
if (!path.endsWith('.wasm')) {
5659
throw new Error(`Can't load ${path} in single file mode`)
5760
}
58-
return parse_base64(data)
61+
let data = await parse_base64(data_base64)
62+
if (node.type.endsWith(';gzip')) {
63+
data = gunzipSync(data)
64+
}
65+
return data
5966
}
6067

6168
// Handle when lib_path is a proper URL (such as import.meta.url), as well as the old style path fragment

0 commit comments

Comments
 (0)