@nkduy/mc-asset-loader 0.0.0-stage → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js ADDED
@@ -0,0 +1,471 @@
1
+ import pathSearch from "@nkduy/mc-path-search-sort"
2
+ import { Manifest, VersionType, LEGACY_ASSETS_BEFORE, versionKey } from "./manifest.js"
3
+ import { VersionContext } from "./version.js"
4
+ import { createStore } from "./store.js"
5
+ import { objectUrl } from "./objects.js"
6
+ import { buildZip, packEntry, decodeEntry, readZip, writeZip } from "./zip.js"
7
+ import { isNode, decoder, pool, budget, pathFilter, define, memo, memoMap } from "./util.js"
8
+
9
+ export { VersionType, LEGACY_ASSETS_BEFORE, readZip, writeZip }
10
+
11
+ const normalisePath = p => String(p).replace(/\\/g, "/").replace(/^(\.\/)+/, "").replace(/^\/+|\/+$/g, "")
12
+ const nameOf = path => path.slice(path.lastIndexOf("/") + 1)
13
+
14
+ function stripNamespace(query) {
15
+ if (typeof query !== "string") return query
16
+ const q = query.trim()
17
+ const colon = q.indexOf(":")
18
+ return colon >= 0 && !q.slice(0, colon).includes("/") ? q.slice(colon + 1) : q
19
+ }
20
+
21
+ function assetPath(id, kind, ext, namespace) {
22
+ let p = String(id).trim().replace(/\\/g, "/").replace(/^\/+/, "")
23
+ if (!p.startsWith("assets/")) {
24
+ let ns = namespace ?? "minecraft"
25
+ let rest = p
26
+ const colon = p.indexOf(":")
27
+ if (colon >= 0 && !p.slice(0, colon).includes("/")) {
28
+ ns = p.slice(0, colon) || ns
29
+ rest = p.slice(colon + 1)
30
+ }
31
+ if (rest.startsWith(kind + "/")) rest = rest.slice(kind.length + 1)
32
+ p = `assets/${ns}/${kind}/${rest}`
33
+ }
34
+ if (!p.toLowerCase().endsWith(ext)) p += ext
35
+ return p
36
+ }
37
+
38
+ function parseLang(text) {
39
+ const out = {}
40
+ for (const raw of text.split(/\r?\n/)) {
41
+ const line = raw.trim()
42
+ if (!line || line.startsWith("#")) continue
43
+ const eq = line.indexOf("=")
44
+ if (eq < 0) continue
45
+ out[line.slice(0, eq).trim()] = line.slice(eq + 1)
46
+ }
47
+ return out
48
+ }
49
+
50
+ function bedrockId(id, namespace) {
51
+ if (namespace != null && namespace !== "minecraft") return null
52
+ let p = String(id).trim().replace(/\\/g, "/").replace(/^\/+/, "")
53
+ const colon = p.indexOf(":")
54
+ if (colon >= 0 && !p.slice(0, colon).includes("/")) {
55
+ if (p.slice(0, colon) !== "minecraft") return null
56
+ p = p.slice(colon + 1)
57
+ }
58
+ return p
59
+ }
60
+
61
+ function bedrockPaths(p, base, kind, exts) {
62
+ if (p == null) return []
63
+ if (p.startsWith("resource_pack/") || p.startsWith("behavior_pack/")) return [p]
64
+ if (p.startsWith(kind + "/")) p = p.slice(kind.length + 1)
65
+ const lower = p.toLowerCase()
66
+ if (exts.some(e => lower.endsWith(e))) return [`${base}/${kind}/${p}`]
67
+ return exts.map(e => `${base}/${kind}/${p}${e}`)
68
+ }
69
+
70
+ const isEntry = x => x != null && typeof x === "object" && typeof x.path === "string"
71
+
72
+ export default class MinecraftAssets {
73
+ constructor({ type = "java", cacheDir, cacheSize, cacheKey, cacheAPI, proxy, version, manifest, manifestExpiry, onManifestProgress, onManifestUpdate, objects, minecraftDir } = {}) {
74
+ if (type !== "java" && type !== "assets" && type !== "bedrock") throw new TypeError(`Unknown type "${type}"`)
75
+ this._type = type
76
+ this._version = version ?? "release"
77
+ this._objects = !!objects
78
+ this._proxy = proxy
79
+ this._minecraftDir = minecraftDir
80
+ this._localInstall = null
81
+ this._store = createStore({ cacheAPI, cacheDir, cacheSize, cacheKey })
82
+ this.manifest = new Manifest(this, { manifest, manifestExpiry, onProgress: onManifestProgress, onUpdate: onManifestUpdate })
83
+ this._contexts = new Map()
84
+ this._objectReads = new Map()
85
+ this._objectFetches = new Map()
86
+ }
87
+
88
+ _local() {
89
+ return memo(this, "_localInstall", async () => {
90
+ if (!isNode || this._minecraftDir === false || this._type === "bedrock") return null
91
+ const { LocalInstall, defaultMinecraftDir } = await import("./local.js")
92
+ return new LocalInstall(typeof this._minecraftDir === "string" ? this._minecraftDir : defaultMinecraftDir(), this._store)
93
+ })
94
+ }
95
+
96
+ async _request(url, init) {
97
+ let target = url
98
+ if (typeof this._proxy === "function") target = this._proxy(url) || url
99
+ else if (this._proxy) target = this._proxy + url
100
+ let res
101
+ try {
102
+ res = await fetch(target, init)
103
+ } catch (e) {
104
+ if (isNode) throw e
105
+ throw new Error(`Fetch failed for ${target}. If this is a cross-origin request, the host may not allow it and a proxy is needed (see the proxy option)`, { cause: e })
106
+ }
107
+ if (!res.ok) throw new Error(`HTTP ${res.status} fetching ${url}`)
108
+ return res
109
+ }
110
+
111
+ _fetchObject(hash, cache = true) {
112
+ if (!cache) return this._loadObject(hash, false)
113
+ const pending = memoMap(this._objectFetches, hash, () => this._loadObject(hash, true))
114
+ pending.finally(() => { if (this._objectFetches.get(hash) === pending) this._objectFetches.delete(hash) })
115
+ return pending
116
+ }
117
+
118
+ async _loadObject(hash, cache) {
119
+ if (cache) {
120
+ const hit = await this._store.get("blobs", hash)
121
+ if (hit) return hit
122
+ }
123
+ const local = await this._local()
124
+ if (local) {
125
+ const bytes = await local.object(hash)
126
+ if (bytes) return bytes
127
+ }
128
+ const res = await this._request(objectUrl(hash))
129
+ const bytes = new Uint8Array(await res.arrayBuffer())
130
+ if (cache) this._store.set("blobs", hash, bytes)
131
+ return bytes
132
+ }
133
+
134
+ _readObject(hash) {
135
+ return memoMap(this._objectReads, hash, () => this._fetchObject(hash))
136
+ }
137
+
138
+ get version() {
139
+ return this.manifest.peek(this._version)?.id ?? null
140
+ }
141
+
142
+ async setVersion(value) {
143
+ const v = value ?? "release"
144
+ const entry = await this.manifest.resolve(v)
145
+ this._version = v
146
+ return entry
147
+ }
148
+
149
+ get channel() {
150
+ return this.manifest.peek(this._version)?.type ?? null
151
+ }
152
+
153
+ async _ctx(version) {
154
+ const entry = await this.manifest.resolve(version ?? this._version)
155
+ const key = versionKey(entry)
156
+ let ctx = this._contexts.get(key)
157
+ if (!ctx) {
158
+ ctx = new VersionContext(this, entry)
159
+ this._contexts.set(key, ctx)
160
+ }
161
+ return ctx
162
+ }
163
+
164
+ _isFolderArg(a) {
165
+ return typeof a === "string" || isEntry(a)
166
+ }
167
+
168
+ list(a, b) {
169
+ const folder = this._isFolderArg(a) ? a : null
170
+ const opts = (folder ? b : a) ?? {}
171
+ if (opts.folders) return this._browse(folder ?? "", opts)
172
+ return this._list(folder, opts)
173
+ }
174
+
175
+ async _list(folder, { version, objects } = {}) {
176
+ const inherited = folder && typeof folder === "object" ? folder._context : null
177
+ const ctx = await this._ctx(version ?? inherited?.version)
178
+ const { files } = await ctx.listing(objects ?? inherited?.objects ?? this._objects)
179
+ const prefix = folder ? normalisePath(typeof folder === "string" ? folder : folder.path) : ""
180
+ return prefix ? files.filter(f => f.path.startsWith(prefix + "/")) : files
181
+ }
182
+
183
+ async _browse(folder, { version, objects } = {}) {
184
+ const folderPath = normalisePath(typeof folder === "string" ? folder : folder.path)
185
+ const inherited = typeof folder === "object" ? folder._context : null
186
+ const ctx = await this._ctx(version ?? inherited?.version)
187
+ const withObjects = objects ?? inherited?.objects ?? this._objects
188
+ const { files: all } = await ctx.listing(withObjects)
189
+ const prefix = folderPath ? folderPath + "/" : ""
190
+ const files = []
191
+ const dirs = new Map()
192
+ for (const f of all) {
193
+ if (!f.path.startsWith(prefix)) continue
194
+ const rest = f.path.slice(prefix.length)
195
+ const slash = rest.indexOf("/")
196
+ if (slash < 0) {
197
+ files.push(f)
198
+ continue
199
+ }
200
+ const name = rest.slice(0, slash)
201
+ let d = dirs.get(name)
202
+ if (!d) dirs.set(name, d = { jar: false, object: false })
203
+ if (f.source) d[f.source] = true
204
+ }
205
+ const folders = Array.from(dirs).map(([name, d]) => this._folderEntry(prefix + name, this._type === "java" ? (d.jar && d.object ? "both" : d.jar ? "jar" : "object") : undefined, ctx, withObjects))
206
+ return { files, folders }
207
+ }
208
+
209
+ _folderEntry(path, source, ctx, objects) {
210
+ const entry = source ? { path, source, objects } : { path, objects }
211
+ const context = { version: ctx.entry, objects }
212
+ function join(rel) {
213
+ const r = normalisePath(rel)
214
+ return r === path || r.startsWith(path + "/") ? r : path + "/" + r
215
+ }
216
+ define(entry, "_context", context)
217
+ define(entry, "list", (a, b) => {
218
+ const target = this._isFolderArg(a) ? join(typeof a === "string" ? a : a.path) : path
219
+ const opts = (this._isFolderArg(a) ? b : a) ?? {}
220
+ return this.list(target, { ...opts, ...context, objects: opts.objects ?? objects })
221
+ })
222
+ define(entry, "search", (query, opts) => this.search(query, { ...opts, ...context, objects: opts?.objects ?? objects, root: path }))
223
+ define(entry, "file", rel => this.file(join(rel), context))
224
+ define(entry, "read", (x, opts) => isEntry(x) ? this.read(x, { ...opts, ...context }) : this.read(join(x), { ...opts, ...context }))
225
+ return entry
226
+ }
227
+
228
+ async search(query, { version, objects, spaces = "_", ...options } = {}) {
229
+ const ctx = await this._ctx(version)
230
+ const { paths, byPath } = await ctx.listing(objects ?? this._objects)
231
+ return pathSearch(paths, stripNamespace(query ?? ""), { ...options, spaces }).map(p => byPath.get(p))
232
+ }
233
+
234
+ async file(path, { version, objects } = {}) {
235
+ const ctx = await this._ctx(version)
236
+ return ctx.file(normalisePath(path), objects ?? this._objects)
237
+ }
238
+
239
+ async read(pathOrEntry, { version, objects, prefer } = {}) {
240
+ if (isEntry(pathOrEntry) && typeof pathOrEntry.read === "function") return pathOrEntry.read({ prefer })
241
+ const path = isEntry(pathOrEntry) ? pathOrEntry.path : pathOrEntry
242
+ const ctx = await this._ctx(version)
243
+ return ctx.read(normalisePath(path), objects ?? this._objects, prefer)
244
+ }
245
+
246
+ async _readFirst(ctx, candidates) {
247
+ for (const path of candidates) {
248
+ const bytes = await ctx.read(path, false)
249
+ if (bytes) return { path, bytes }
250
+ }
251
+ return null
252
+ }
253
+
254
+ async getTexture(id, { version, meta, prefer, namespace } = {}) {
255
+ const ctx = await this._ctx(version)
256
+ if (this._type === "bedrock") {
257
+ const hit = await this._readFirst(ctx, bedrockPaths(bedrockId(id, namespace), "resource_pack", "textures", [".png", ".tga"]))
258
+ if (!hit) return null
259
+ if (!meta) return hit.bytes
260
+ const raw = await ctx.read(hit.path.replace(/\.(png|tga)$/i, "") + ".texture_set.json", false)
261
+ return { data: hit.bytes, meta: raw ? JSON.parse(decoder.decode(raw)) : null }
262
+ }
263
+ const path = assetPath(id, "textures", ".png", namespace)
264
+ const data = await ctx.read(path, this._objects, prefer)
265
+ if (!data) return null
266
+ if (!meta) return data
267
+ const raw = await ctx.read(path + ".mcmeta", this._objects, prefer)
268
+ return { data, meta: raw ? JSON.parse(decoder.decode(raw)) : null }
269
+ }
270
+
271
+ async _json(id, kind, { version, namespace } = {}, bedrock) {
272
+ const ctx = await this._ctx(version)
273
+ if (this._type === "bedrock") {
274
+ const hit = await this._readFirst(ctx, bedrockPaths(bedrockId(id, namespace), bedrock.base, bedrock.kind, bedrock.exts))
275
+ return hit ? JSON.parse(decoder.decode(hit.bytes)) : null
276
+ }
277
+ const bytes = await ctx.read(assetPath(id, kind, ".json", namespace), this._objects)
278
+ return bytes ? JSON.parse(decoder.decode(bytes)) : null
279
+ }
280
+
281
+ getModel(id, options) {
282
+ return this._json(id, "models", options, { base: "resource_pack", kind: "models", exts: [".geo.json", ".json"] })
283
+ }
284
+
285
+ getBlockstate(id, options) {
286
+ return this._json(id, "blockstates", options, { base: "behavior_pack", kind: "blocks", exts: [".block.json", ".json"] })
287
+ }
288
+
289
+ getItemDefinition(id, options) {
290
+ return this._json(id, "items", options, { base: "behavior_pack", kind: "items", exts: [".json"] })
291
+ }
292
+
293
+ async getStructure(id, { version, namespace } = {}) {
294
+ if (this._type === "bedrock") return null
295
+ const ctx = await this._ctx(version)
296
+ let p = String(id).trim().replace(/\\/g, "/").replace(/^\/+/, "")
297
+ if (!p.toLowerCase().endsWith(".nbt")) p += ".nbt"
298
+ if (p.startsWith("data/") || p.startsWith("assets/")) return ctx.read(p, this._objects)
299
+ let ns = namespace ?? "minecraft"
300
+ const colon = p.indexOf(":")
301
+ if (colon >= 0 && !p.slice(0, colon).includes("/")) {
302
+ ns = p.slice(0, colon) || ns
303
+ p = p.slice(colon + 1)
304
+ }
305
+ p = p.replace(/^structures?\//, "")
306
+ for (const base of [`data/${ns}/structure/`, `data/${ns}/structures/`, `assets/${ns}/structures/`]) {
307
+ const bytes = await ctx.read(base + p, this._objects)
308
+ if (bytes) return bytes
309
+ }
310
+ return null
311
+ }
312
+
313
+ async getSound(id, { version, namespace } = {}) {
314
+ const ctx = await this._ctx(version)
315
+ if (this._type === "bedrock") {
316
+ const hit = await this._readFirst(ctx, bedrockPaths(bedrockId(id, namespace), "resource_pack", "sounds", [".fsb", ".ogg"]))
317
+ return hit ? hit.bytes : null
318
+ }
319
+ return ctx.read(assetPath(id, "sounds", ".ogg", namespace), true)
320
+ }
321
+
322
+ async _bedrockLang(ctx, code, namespace) {
323
+ if (namespace != null && namespace !== "minecraft") return null
324
+ const { files } = await ctx.listing(false)
325
+ const want = nameOf(String(code).replace(/\\/g, "/")).replace(/\.lang$/i, "").toLowerCase()
326
+ for (const f of files) {
327
+ if (!f.path.startsWith("resource_pack/texts/")) continue
328
+ const m = /^([^/]*)\.lang$/i.exec(f.path.slice("resource_pack/texts/".length))
329
+ if (m && m[1].toLowerCase() === want) return parseLang(decoder.decode(await f.read()))
330
+ }
331
+ return null
332
+ }
333
+
334
+ async getLang(code, { version, namespace = "minecraft" } = {}) {
335
+ const ctx = await this._ctx(version)
336
+ if (this._type === "bedrock") return this._bedrockLang(ctx, code, namespace)
337
+ const { files } = await ctx.listing(true)
338
+ let c = String(code).replace(/\\/g, "/")
339
+ const colon = c.indexOf(":")
340
+ if (colon >= 0 && !c.slice(0, colon).includes("/")) {
341
+ namespace = c.slice(0, colon) || namespace
342
+ c = c.slice(colon + 1)
343
+ }
344
+ const want = nameOf(c).replace(/\.(json|lang)$/i, "").toLowerCase()
345
+ const dirs = [`assets/${namespace}/lang/`]
346
+ if (ctx.entry.legacyLayout) dirs.push("lang/")
347
+ let json = null
348
+ let lang = null
349
+ for (const f of files) {
350
+ for (const dir of dirs) {
351
+ if (!f.path.startsWith(dir)) continue
352
+ const name = f.path.slice(dir.length)
353
+ const m = /^([^/]*)\.(json|lang)$/i.exec(name)
354
+ if (!m || m[1].toLowerCase() !== want) continue
355
+ if (m[2].toLowerCase() === "json") json ??= f
356
+ else lang ??= f
357
+ }
358
+ }
359
+ const entry = json ?? lang
360
+ if (!entry) return null
361
+ const text = decoder.decode(await entry.read())
362
+ return entry === json ? JSON.parse(text) : parseLang(text)
363
+ }
364
+
365
+ async loadJar({ version, onProgress } = {}) {
366
+ const ctx = await this._ctx(version)
367
+ if (this._type === "assets") {
368
+ const { files } = await ctx.listing(false)
369
+ const total = files.reduce((n, f) => n + f.size, 0)
370
+ let done = 0
371
+ onProgress?.(0, total)
372
+ await pool(files, 32, async f => {
373
+ try { await this._fetchObject(f.hash) } catch {}
374
+ onProgress?.(done += f.size, total)
375
+ })
376
+ return
377
+ }
378
+ const jar = await ctx.jar()
379
+ ctx.listing(this._objects).catch(() => {})
380
+ await jar.load(onProgress)
381
+ }
382
+
383
+ async loadObjects({ version, filter, concurrency = 32, onProgress, cache = true } = {}) {
384
+ const ctx = await this._ctx(version)
385
+ const index = await ctx.index()
386
+ const keep = pathFilter(filter)
387
+ const wanted = index ? Array.from(index).filter(([path]) => keep(path)) : []
388
+ const results = new Array(wanted.length)
389
+ let done = 0
390
+ await pool(wanted, concurrency, async ([, { hash }], i) => {
391
+ try {
392
+ results[i] = await this._fetchObject(hash, cache)
393
+ } catch {}
394
+ onProgress?.(++done, wanted.length)
395
+ })
396
+ const out = new Map()
397
+ wanted.forEach(([path], i) => { if (results[i]) out.set(path, results[i]) })
398
+ return out
399
+ }
400
+
401
+ async export({ version, filter, dir, objects, concurrency = 32, onProgress } = {}) {
402
+ const ctx = await this._ctx(version)
403
+ const { files } = await ctx.listing(objects ?? this._objects)
404
+ const keep = pathFilter(filter)
405
+ const wanted = files.filter(f => keep(f.path))
406
+ const jar = wanted.some(f => !f.hash) ? await ctx.jar() : null
407
+ if (jar) await jar.buffer()
408
+ let done = 0
409
+ const tick = () => onProgress?.(++done, wanted.length)
410
+
411
+ if (dir == null) {
412
+ const items = new Array(wanted.length)
413
+ const pause = budget()
414
+ await pool(wanted, concurrency, async (f, i) => {
415
+ if (!f.hash) {
416
+ const { entry, data } = await jar.raw(f.path)
417
+ items[i] = { path: f.path, method: entry.method, crc: entry.crc, size: entry.size, compressedSize: entry.compressedSize, data }
418
+ } else {
419
+ items[i] = await packEntry(f.path, await this._fetchObject(f.hash))
420
+ }
421
+ tick()
422
+ await pause()
423
+ })
424
+ return buildZip(items, pause)
425
+ }
426
+
427
+ const [fs, nodePath] = await Promise.all([import("node:fs/promises"), import("node:path")])
428
+ await pool(wanted, concurrency, async f => {
429
+ let bytes
430
+ if (!f.hash) {
431
+ const { entry, data } = await jar.raw(f.path)
432
+ bytes = await decodeEntry(data, entry)
433
+ } else {
434
+ bytes = await this._fetchObject(f.hash)
435
+ }
436
+ const target = nodePath.join(dir, ...f.path.split("/"))
437
+ await fs.mkdir(nodePath.dirname(target), { recursive: true })
438
+ await fs.writeFile(target, bytes)
439
+ tick()
440
+ })
441
+ return wanted.length
442
+ }
443
+
444
+ async cacheStats() {
445
+ const list = await this._store.list()
446
+ if (!list) return null
447
+ let size = 0
448
+ for (const f of list) size += f.size
449
+ return { files: list.length, size }
450
+ }
451
+
452
+ async listCache() {
453
+ const list = await this._store.list()
454
+ return list ? list.sort((a, b) => b.size - a.size) : null
455
+ }
456
+
457
+ setCacheSize(size) {
458
+ return this._store.setMaxSize(size)
459
+ }
460
+
461
+ async clearCache(key) {
462
+ for (const ctx of this._contexts.values()) {
463
+ const jar = await ctx._jar?.catch(() => null)
464
+ await jar?.settled()
465
+ }
466
+ if (key == null) return this._store.clear()
467
+ const k = String(key)
468
+ const slash = k.indexOf("/")
469
+ return this._store.delete(k.slice(0, slash), k.slice(slash + 1))
470
+ }
471
+ }
package/src/inflate.js ADDED
@@ -0,0 +1,168 @@
1
+ const LEN_BASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]
2
+ const LEN_EXTRA = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]
3
+ const DIST_BASE = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]
4
+ const DIST_EXTRA = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]
5
+ const CL_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]
6
+
7
+ const FAST_BITS = 9
8
+ const FAST_SIZE = 1 << FAST_BITS
9
+ const FAST_MASK = FAST_SIZE - 1
10
+
11
+ function huffman(lengths, n) {
12
+ const count = new Uint16Array(16)
13
+ for (let i = 0; i < n; i++) count[lengths[i]]++
14
+ count[0] = 0
15
+ const offsets = new Uint16Array(16)
16
+ const next = new Uint16Array(16)
17
+ let code = 0
18
+ for (let len = 1; len < 16; len++) {
19
+ offsets[len] = offsets[len - 1] + count[len - 1]
20
+ code = (code + count[len - 1]) << 1
21
+ next[len] = code
22
+ }
23
+ const symbol = new Uint16Array(n)
24
+ const fast = new Uint16Array(FAST_SIZE)
25
+ for (let i = 0; i < n; i++) {
26
+ const len = lengths[i]
27
+ if (!len) continue
28
+ symbol[offsets[len]++] = i
29
+ const c = next[len]++
30
+ if (len > FAST_BITS) continue
31
+ let rev = 0
32
+ for (let b = 0; b < len; b++) rev |= ((c >> b) & 1) << (len - 1 - b)
33
+ const packed = (len << 9) | i
34
+ for (let j = rev; j < FAST_SIZE; j += 1 << len) fast[j] = packed
35
+ }
36
+ return { count, symbol, fast }
37
+ }
38
+
39
+ const fixedLengths = new Uint8Array(288)
40
+ fixedLengths.fill(8, 0, 144)
41
+ fixedLengths.fill(9, 144, 256)
42
+ fixedLengths.fill(7, 256, 280)
43
+ fixedLengths.fill(8, 280, 288)
44
+ const FIXED_LIT = huffman(fixedLengths, 288)
45
+ const FIXED_DIST = huffman(new Uint8Array(30).fill(5), 30)
46
+
47
+ export function inflateSync(src, size) {
48
+ let out = new Uint8Array(size ?? Math.max(1024, src.length * 4))
49
+ let pos = 0
50
+ let ip = 0
51
+ let bitBuf = 0
52
+ let bitCnt = 0
53
+ const end = src.length
54
+
55
+ function fill(n) {
56
+ while (bitCnt < n) {
57
+ if (ip > end + 4) throw new Error("Truncated deflate data")
58
+ bitBuf |= (ip < end ? src[ip] : 0) << bitCnt
59
+ ip++
60
+ bitCnt += 8
61
+ }
62
+ }
63
+
64
+ function bits(n) {
65
+ if (bitCnt < n) fill(n)
66
+ const v = bitBuf & ((1 << n) - 1)
67
+ bitBuf >>>= n
68
+ bitCnt -= n
69
+ return v
70
+ }
71
+
72
+ function decode(h) {
73
+ if (bitCnt < FAST_BITS) fill(FAST_BITS)
74
+ const hit = h.fast[bitBuf & FAST_MASK]
75
+ if (hit) {
76
+ const len = hit >> 9
77
+ bitBuf >>>= len
78
+ bitCnt -= len
79
+ return hit & 511
80
+ }
81
+ let code = 0
82
+ let first = 0
83
+ let index = 0
84
+ const count = h.count
85
+ for (let len = 1; len <= 15; len++) {
86
+ code |= bits(1)
87
+ const c = count[len]
88
+ if (code - c < first) return h.symbol[index + (code - first)]
89
+ index += c
90
+ first += c
91
+ first <<= 1
92
+ code <<= 1
93
+ }
94
+ throw new Error("Invalid deflate data")
95
+ }
96
+
97
+ function grow(need) {
98
+ if (need <= out.length) return
99
+ const bigger = new Uint8Array(Math.max(need, out.length * 2))
100
+ bigger.set(out.subarray(0, pos))
101
+ out = bigger
102
+ }
103
+
104
+ while (true) {
105
+ const last = bits(1)
106
+ const type = bits(2)
107
+ if (type === 0) {
108
+ ip -= bitCnt >> 3
109
+ bitBuf = 0
110
+ bitCnt = 0
111
+ const len = src[ip] | (src[ip + 1] << 8)
112
+ ip += 4
113
+ if (ip + len > end) throw new Error("Truncated deflate data")
114
+ grow(pos + len)
115
+ out.set(src.subarray(ip, ip + len), pos)
116
+ ip += len
117
+ pos += len
118
+ } else if (type === 1 || type === 2) {
119
+ let lit = FIXED_LIT
120
+ let dist = FIXED_DIST
121
+ if (type === 2) {
122
+ const nlen = bits(5) + 257
123
+ const ndist = bits(5) + 1
124
+ const ncode = bits(4) + 4
125
+ const lengths = new Uint8Array(320)
126
+ for (let i = 0; i < ncode; i++) lengths[CL_ORDER[i]] = bits(3)
127
+ const lencode = huffman(lengths, 19)
128
+ lengths.fill(0)
129
+ let i = 0
130
+ while (i < nlen + ndist) {
131
+ const sym = decode(lencode)
132
+ if (sym < 16) lengths[i++] = sym
133
+ else {
134
+ let repeat
135
+ let value = 0
136
+ if (sym === 16) {
137
+ value = lengths[i - 1]
138
+ repeat = 3 + bits(2)
139
+ } else if (sym === 17) repeat = 3 + bits(3)
140
+ else repeat = 11 + bits(7)
141
+ lengths.fill(value, i, i + repeat)
142
+ i += repeat
143
+ }
144
+ }
145
+ lit = huffman(lengths, nlen)
146
+ dist = huffman(lengths.subarray(nlen), ndist)
147
+ }
148
+ while (true) {
149
+ const sym = decode(lit)
150
+ if (sym < 256) {
151
+ if (pos >= out.length) grow(pos + 1)
152
+ out[pos++] = sym
153
+ } else if (sym === 256) break
154
+ else {
155
+ const li = sym - 257
156
+ const len = LEN_BASE[li] + bits(LEN_EXTRA[li])
157
+ const di = decode(dist)
158
+ const d = DIST_BASE[di] + bits(DIST_EXTRA[di])
159
+ grow(pos + len)
160
+ let from = pos - d
161
+ for (let k = 0; k < len; k++) out[pos++] = out[from++]
162
+ }
163
+ }
164
+ } else throw new Error("Invalid deflate block type")
165
+ if (last) break
166
+ }
167
+ return pos === out.length ? out : out.subarray(0, pos)
168
+ }