@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/store.js ADDED
@@ -0,0 +1,69 @@
1
+ import { isNode, encoder, decoder } from "./util.js"
2
+
3
+ async function swallow(fn) {
4
+ try {
5
+ return await fn()
6
+ } catch {
7
+ return undefined
8
+ }
9
+ }
10
+
11
+ function toBytes(value) {
12
+ if (value instanceof Uint8Array) return value
13
+ if (value instanceof ArrayBuffer) return new Uint8Array(value)
14
+ if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
15
+ throw new TypeError("cacheAPI.read must return bytes")
16
+ }
17
+
18
+ function apiBackend(api) {
19
+ return {
20
+ async get(store, key) {
21
+ const value = await api.read?.(store + "/" + key)
22
+ if (value == null) return undefined
23
+ const bytes = toBytes(value)
24
+ return store === "meta" ? JSON.parse(decoder.decode(bytes)) : bytes
25
+ },
26
+ async set(store, key, value) {
27
+ await api.write?.(store + "/" + key, store === "meta" ? encoder.encode(JSON.stringify(value)) : value)
28
+ },
29
+ async delete(store, key) {
30
+ await api.delete?.(store + "/" + key)
31
+ },
32
+ async list() {
33
+ const out = await api.list?.()
34
+ return out == null ? undefined : Array.from(out, f => ({ key: String(f.key), size: Number(f.size) || 0 }))
35
+ },
36
+ async clear() {
37
+ await api.clear?.()
38
+ },
39
+ async setMaxSize() {}
40
+ }
41
+ }
42
+
43
+ function builtinBackend(dir, maxSize, key) {
44
+ let impl
45
+ const load = () => impl ??= isNode
46
+ ? import("./cache.js").then(m => new m.FileCache(dir, { maxSize, key }))
47
+ : import("./opfs-cache.js").then(m => new m.OpfsCache(dir, { maxSize, key }))
48
+ load().catch(() => {})
49
+ return {
50
+ get: async (store, key) => (await load()).get(store, key),
51
+ set: async (store, key, value) => (await load()).set(store, key, value),
52
+ delete: async (store, key) => (await load()).delete(store, key),
53
+ list: async () => (await load()).list(),
54
+ clear: async () => (await load()).clear(),
55
+ setMaxSize: async size => (await load()).setMaxSize(size)
56
+ }
57
+ }
58
+
59
+ export function createStore({ cacheAPI, cacheDir, cacheSize, cacheKey } = {}) {
60
+ const backend = cacheAPI ? apiBackend(cacheAPI) : builtinBackend(cacheDir, cacheSize, cacheKey)
61
+ return {
62
+ get: (store, key) => swallow(() => backend.get(store, key)),
63
+ set: (store, key, value) => swallow(() => backend.set(store, key, value)),
64
+ delete: (store, key) => swallow(() => backend.delete(store, key)),
65
+ list: async () => (await swallow(() => backend.list())) ?? null,
66
+ clear: () => swallow(() => backend.clear()),
67
+ setMaxSize: size => swallow(() => backend.setMaxSize(size))
68
+ }
69
+ }
package/src/util.js ADDED
@@ -0,0 +1,105 @@
1
+ export const isNode = typeof process !== "undefined" && typeof process.versions?.node === "string"
2
+
3
+ export const collator = new Intl.Collator()
4
+ export const encoder = new TextEncoder()
5
+ export const decoder = new TextDecoder()
6
+
7
+ export async function pool(items, limit, fn) {
8
+ const arr = Array.from(items)
9
+ let next = 0
10
+ const workers = Array.from({ length: Math.max(1, Math.min(limit, arr.length)) }, async () => {
11
+ while (next < arr.length) {
12
+ const i = next++
13
+ await fn(arr[i], i)
14
+ }
15
+ })
16
+ await Promise.all(workers)
17
+ }
18
+
19
+ export function budget(ms = 8) {
20
+ let deadline = performance.now() + ms
21
+ return async () => {
22
+ if (performance.now() < deadline) return
23
+ await new Promise(resolve => setTimeout(resolve, 0))
24
+ deadline = performance.now() + ms
25
+ }
26
+ }
27
+
28
+ export function hashFromUrl(url) {
29
+ const parts = String(url).split("/")
30
+ return parts.length >= 2 ? parts[parts.length - 2] : String(url)
31
+ }
32
+
33
+ export function pathFilter(filter) {
34
+ if (filter == null) return () => true
35
+ if (typeof filter === "function") return p => !!filter(p)
36
+ if (Array.isArray(filter)) {
37
+ const set = new Set(filter.map(f => typeof f === "string" ? f : f?.path))
38
+ return p => set.has(p)
39
+ }
40
+ throw new TypeError("filter must be a function or an array of paths/entries")
41
+ }
42
+
43
+ export function memo(obj, key, factory) {
44
+ if (obj[key]) return obj[key]
45
+ const p = Promise.resolve().then(factory)
46
+ obj[key] = p
47
+ p.catch(() => { if (obj[key] === p) obj[key] = null })
48
+ return p
49
+ }
50
+
51
+ export function memoMap(map, key, factory) {
52
+ let p = map.get(key)
53
+ if (!p) {
54
+ p = Promise.resolve().then(factory)
55
+ map.set(key, p)
56
+ p.catch(() => { if (map.get(key) === p) map.delete(key) })
57
+ }
58
+ return p
59
+ }
60
+
61
+ export async function readBody(res, tick, expected) {
62
+ if (!res.body) {
63
+ const bytes = new Uint8Array(await res.arrayBuffer())
64
+ tick?.(bytes.length)
65
+ return bytes
66
+ }
67
+ const reader = res.body.getReader()
68
+ if (expected != null) {
69
+ let out = new Uint8Array(expected)
70
+ let at = 0
71
+ while (true) {
72
+ const { done, value } = await reader.read()
73
+ if (done) break
74
+ if (at + value.length > out.length) {
75
+ const bigger = new Uint8Array(Math.max(out.length * 2, at + value.length))
76
+ bigger.set(out.subarray(0, at))
77
+ out = bigger
78
+ }
79
+ out.set(value, at)
80
+ at += value.length
81
+ tick?.(value.length)
82
+ }
83
+ return at === out.length ? out : out.subarray(0, at)
84
+ }
85
+ const parts = []
86
+ let size = 0
87
+ while (true) {
88
+ const { done, value } = await reader.read()
89
+ if (done) break
90
+ parts.push(value)
91
+ size += value.length
92
+ tick?.(value.length)
93
+ }
94
+ const out = new Uint8Array(size)
95
+ let at = 0
96
+ for (const part of parts) {
97
+ out.set(part, at)
98
+ at += part.length
99
+ }
100
+ return out
101
+ }
102
+
103
+ export function define(obj, name, value) {
104
+ Object.defineProperty(obj, name, { value, enumerable: false, configurable: true, writable: true })
105
+ }
package/src/version.js ADDED
@@ -0,0 +1,160 @@
1
+ import { Jar } from "./jar.js"
2
+ import { BedrockZip } from "./bedrock.js"
3
+ import { rootIndex } from "./objects.js"
4
+ import { hashFromUrl, collator, memo, memoMap } from "./util.js"
5
+ import { versionKey } from "./manifest.js"
6
+
7
+ export class VersionContext {
8
+ constructor(mc, entry) {
9
+ this.mc = mc
10
+ this.entry = entry
11
+ this.key = versionKey(entry)
12
+ this._details = null
13
+ this._jar = null
14
+ this._jarValue = null
15
+ this._index = null
16
+ this._listings = new Map()
17
+ }
18
+
19
+ details() {
20
+ return memo(this, "_details", () => this.mc.manifest.details(this.entry))
21
+ }
22
+
23
+ jar() {
24
+ return memo(this, "_jar", async () => this._jarValue = await this._openJar())
25
+ }
26
+
27
+ async _openJar() {
28
+ if (this.mc._type === "assets") throw new Error("Asset index versions have no jar")
29
+ if (this.mc._type === "bedrock") {
30
+ const zip = this.entry.zip
31
+ if (!zip?.url) throw new Error(`Version "${this.entry.id}" has no download`)
32
+ return new BedrockZip({
33
+ url: zip.url,
34
+ size: zip.size,
35
+ archive: zip.archive,
36
+ key: this.entry.tag ?? this.entry.id,
37
+ request: (url, init) => this.mc._request(url, init),
38
+ store: this.mc._store
39
+ })
40
+ }
41
+ const d = await this.details()
42
+ const client = d.downloads?.client
43
+ if (!client?.url) throw new Error(`Version "${this.entry.id}" has no client jar`)
44
+ const local = await this.mc._local()
45
+ return new Jar({
46
+ url: client.url,
47
+ size: client.size,
48
+ sha1: client.sha1 ?? hashFromUrl(client.url),
49
+ local: local ? await local.jar(this.entry.id, client) : null,
50
+ legacyLayout: this.entry.legacyLayout,
51
+ request: (url, init) => this.mc._request(url, init),
52
+ store: this.mc._store
53
+ })
54
+ }
55
+
56
+ index() {
57
+ return memo(this, "_index", async () => {
58
+ if (this.mc._type === "bedrock") return null
59
+ const meta = this.mc._type === "assets" ? this.entry : (await this.details()).assetIndex
60
+ const url = meta?.url
61
+ if (!url) return null
62
+ const store = this.mc._store
63
+ const key = "index_" + (meta.sha1 ?? hashFromUrl(url))
64
+ let json = await store.get("meta", key)
65
+ if (!json?.objects) {
66
+ const local = await this.mc._local()
67
+ json = local ? await local.index(meta.id, meta.sha1) : null
68
+ if (!json?.objects) {
69
+ json = await (await this.mc._request(url)).json()
70
+ store.set("meta", key, json)
71
+ }
72
+ }
73
+ return rootIndex(json)
74
+ })
75
+ }
76
+
77
+ listing(objects) {
78
+ if (this.mc._type !== "java") objects = false
79
+ const key = objects ? "on" : "off"
80
+ return memoMap(this._listings, key, () => this._buildListing(!!objects))
81
+ }
82
+
83
+ async _buildListing(objects) {
84
+ if (this.mc._type === "assets") {
85
+ const index = await this.index()
86
+ const byPath = new Map()
87
+ for (const [path, obj] of index) byPath.set(path, this._fileEntry(path, null, obj))
88
+ const files = Object.freeze(Array.from(byPath.values()).sort((a, b) => collator.compare(a.path, b.path)))
89
+ let paths
90
+ return { files, byPath, get paths() { return paths ??= Object.freeze(files.map(f => f.path)) } }
91
+ }
92
+ const jar = await this.jar()
93
+ const [listing, index] = await Promise.all([jar.listing(), objects ? this.index() : null])
94
+ if (!listing.sorted && listing.sorting) await listing.sorting
95
+ const { entries } = listing
96
+ const byPath = new Map()
97
+ for (const [path, jarEntry] of entries) byPath.set(path, this._fileEntry(path, jarEntry, index?.get(path)))
98
+ if (index) for (const [path, obj] of index) if (!byPath.has(path)) byPath.set(path, this._fileEntry(path, null, obj))
99
+ let list
100
+ if (!index && listing.order) list = listing.order.map(path => byPath.get(path))
101
+ else {
102
+ list = Array.from(byPath.values())
103
+ if (!listing.sorted || index) list.sort((a, b) => collator.compare(a.path, b.path))
104
+ }
105
+ const files = Object.freeze(list)
106
+ let paths
107
+ return { files, byPath, get paths() { return paths ??= Object.freeze(files.map(f => f.path)) } }
108
+ }
109
+
110
+ _fileEntry(path, jarEntry, obj) {
111
+ return new FileEntry(this, path, jarEntry, obj)
112
+ }
113
+
114
+ readJar(path) {
115
+ const jar = this._jarValue
116
+ return jar ? jar.read(path) : this.jar().then(j => j.read(path))
117
+ }
118
+
119
+ async file(path, objects) {
120
+ const { byPath } = await this.listing(objects)
121
+ return byPath.get(path) ?? null
122
+ }
123
+
124
+ async read(path, objects, prefer) {
125
+ const entry = await this.file(path, objects)
126
+ return entry ? entry.read({ prefer }) : null
127
+ }
128
+ }
129
+
130
+ const STATE = Symbol("state")
131
+
132
+ class FileEntry {
133
+ constructor(ctx, path, jarEntry, obj) {
134
+ this.path = path
135
+ if (ctx.mc._type === "java") this.source = obj ? "object" : "jar"
136
+ this.size = (obj ?? jarEntry).size
137
+ if (obj) this.hash = obj.hash
138
+ else this.crc = jarEntry.crc
139
+ this[STATE] = Object.freeze({ ctx, jar: jarEntry, obj })
140
+ }
141
+
142
+ read({ prefer } = {}) {
143
+ const { ctx, jar, obj } = this[STATE]
144
+ const useJar = jar && (!obj || prefer === "jar")
145
+ return useJar ? ctx.readJar(this.path) : ctx.mc._readObject(obj.hash)
146
+ }
147
+
148
+ raw({ prefer } = {}) {
149
+ const { ctx, jar: jarEntry, obj } = this[STATE]
150
+ const useJar = jarEntry && (!obj || prefer === "jar")
151
+ if (!useJar) return ctx.mc._readObject(obj.hash).then(bytes => ({ compression: null, bytes }))
152
+ const jar = ctx._jarValue
153
+ if (jar?._list && jar._held) return Promise.resolve(rawResult(jar.rawSync(this.path)))
154
+ return ctx.jar().then(j => j.raw(this.path)).then(rawResult)
155
+ }
156
+ }
157
+
158
+ function rawResult(hit) {
159
+ return hit ? { compression: hit.entry.method === 8 ? "deflate-raw" : null, bytes: hit.data } : null
160
+ }
package/src/zip.js ADDED
@@ -0,0 +1,253 @@
1
+ import { isNode, encoder, decoder, define, pool, budget } from "./util.js"
2
+ import { inflateSync } from "./inflate.js"
3
+
4
+ const INLINE_INFLATE_MAX = 1 << 20
5
+
6
+ export const SIG_LOCAL = 0x04034b50
7
+ export const SIG_CENTRAL = 0x02014b50
8
+ export const SIG_EOCD = 0x06054b50
9
+
10
+ const u16 = (b, i) => b[i] | (b[i + 1] << 8)
11
+ const u32 = (b, i) => (b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24)) >>> 0
12
+ const u64 = (b, i) => u32(b, i) + u32(b, i + 4) * 0x100000000
13
+
14
+ let nodeZlib
15
+ const zlib = () => nodeZlib ??= import("./zlib-node.js")
16
+
17
+ async function transform(bytes, stream) {
18
+ const writer = stream.writable.getWriter()
19
+ writer.write(bytes).catch(() => {})
20
+ writer.close().catch(() => {})
21
+ return new Uint8Array(await new Response(stream.readable).arrayBuffer())
22
+ }
23
+
24
+ export async function inflateRaw(bytes) {
25
+ if (isNode) return (await zlib()).inflateRaw(bytes)
26
+ return transform(bytes, new DecompressionStream("deflate-raw"))
27
+ }
28
+
29
+ export async function deflateRaw(bytes, level) {
30
+ if (isNode) return (await zlib()).deflateRaw(bytes, level)
31
+ return transform(bytes, new CompressionStream("deflate-raw"))
32
+ }
33
+
34
+ const CRC_TABLE = new Int32Array(256)
35
+ for (let n = 0; n < 256; n++) {
36
+ let c = n
37
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
38
+ CRC_TABLE[n] = c
39
+ }
40
+
41
+ export function crc32(bytes) {
42
+ let c = -1
43
+ for (let i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 0xff] ^ (c >>> 8)
44
+ return (c ^ -1) >>> 0
45
+ }
46
+
47
+ export function findEocd(buf) {
48
+ for (let i = buf.length - 22; i >= 0; i--) if (u32(buf, i) === SIG_EOCD) return i
49
+ return -1
50
+ }
51
+
52
+ export function parseEocd(buf, at) {
53
+ return { count: u16(buf, at + 10), cdSize: u32(buf, at + 12), cdOffset: u32(buf, at + 16) }
54
+ }
55
+
56
+ export function parseCentralDirectory(cd) {
57
+ const entries = []
58
+ let i = 0
59
+ while (i + 46 <= cd.length && u32(cd, i) === SIG_CENTRAL) {
60
+ const nameLen = u16(cd, i + 28)
61
+ const extraLen = u16(cd, i + 30)
62
+ const commentLen = u16(cd, i + 32)
63
+ const entry = {
64
+ path: decoder.decode(cd.subarray(i + 46, i + 46 + nameLen)),
65
+ method: u16(cd, i + 10),
66
+ crc: u32(cd, i + 16),
67
+ compressedSize: u32(cd, i + 20),
68
+ size: u32(cd, i + 24),
69
+ offset: u32(cd, i + 42),
70
+ nameLen
71
+ }
72
+ if (entry.size === 0xffffffff || entry.compressedSize === 0xffffffff || entry.offset === 0xffffffff) {
73
+ let at = i + 46 + nameLen
74
+ const end = at + extraLen
75
+ while (at + 4 <= end) {
76
+ const id = u16(cd, at)
77
+ const len = u16(cd, at + 2)
78
+ if (id === 1) {
79
+ let field = at + 4
80
+ if (entry.size === 0xffffffff) {
81
+ entry.size = u64(cd, field)
82
+ field += 8
83
+ }
84
+ if (entry.compressedSize === 0xffffffff) {
85
+ entry.compressedSize = u64(cd, field)
86
+ field += 8
87
+ }
88
+ if (entry.offset === 0xffffffff) entry.offset = u64(cd, field)
89
+ break
90
+ }
91
+ at += 4 + len
92
+ }
93
+ }
94
+ entries.push(entry)
95
+ i += 46 + nameLen + extraLen + commentLen
96
+ }
97
+ return entries
98
+ }
99
+
100
+ export function parseZip(buf) {
101
+ const at = findEocd(buf)
102
+ if (at < 0) throw new Error("Not a zip file")
103
+ let eocd = parseEocd(buf, at)
104
+ if ((eocd.count === 0xffff || eocd.cdSize === 0xffffffff || eocd.cdOffset === 0xffffffff) && at >= 20 && u32(buf, at - 20) === 0x07064b50) {
105
+ const at64 = u64(buf, at - 12)
106
+ if (at64 + 56 <= buf.length && u32(buf, at64) === 0x06064b50) {
107
+ eocd = { count: u64(buf, at64 + 32), cdSize: u64(buf, at64 + 40), cdOffset: u64(buf, at64 + 48) }
108
+ }
109
+ }
110
+ return { ...eocd, entries: parseCentralDirectory(buf.subarray(eocd.cdOffset, eocd.cdOffset + eocd.cdSize)) }
111
+ }
112
+
113
+ export function listBuffer(buf) {
114
+ return parseZip(buf).entries
115
+ }
116
+
117
+ export function localDataOffset(buf, entry) {
118
+ const o = entry.offset
119
+ return o + 30 + u16(buf, o + 26) + u16(buf, o + 28)
120
+ }
121
+
122
+ export function rawFromBuffer(buf, entry) {
123
+ const at = localDataOffset(buf, entry)
124
+ return buf.subarray(at, at + entry.compressedSize)
125
+ }
126
+
127
+ export function decodeEntrySync(raw, entry) {
128
+ if (entry.method === 0) return raw.slice()
129
+ return entry.size <= INLINE_INFLATE_MAX ? inflateSync(raw, entry.size) : null
130
+ }
131
+
132
+ export async function decodeEntry(raw, entry) {
133
+ return decodeEntrySync(raw, entry) ?? inflateRaw(raw)
134
+ }
135
+
136
+ export function entryFromBuffer(buf, entry) {
137
+ return decodeEntry(rawFromBuffer(buf, entry), entry)
138
+ }
139
+
140
+ const DOS_TIME = 0
141
+ const DOS_DATE = 0x21
142
+
143
+ export async function buildZip(items, pause = budget()) {
144
+ if (items.length > 0xffff) throw new Error("Too many entries for a plain zip")
145
+ const parts = []
146
+ const centrals = []
147
+ let offset = 0
148
+ for (const it of items) {
149
+ await pause()
150
+ const name = encoder.encode(it.path)
151
+ const local = new Uint8Array(30 + name.length)
152
+ const lv = new DataView(local.buffer)
153
+ lv.setUint32(0, SIG_LOCAL, true)
154
+ lv.setUint16(4, 20, true)
155
+ lv.setUint16(8, it.method, true)
156
+ lv.setUint16(10, DOS_TIME, true)
157
+ lv.setUint16(12, DOS_DATE, true)
158
+ lv.setUint32(14, it.crc, true)
159
+ lv.setUint32(18, it.compressedSize, true)
160
+ lv.setUint32(22, it.size, true)
161
+ lv.setUint16(26, name.length, true)
162
+ lv.setUint16(28, 0, true)
163
+ local.set(name, 30)
164
+
165
+ const central = new Uint8Array(46 + name.length)
166
+ const cv = new DataView(central.buffer)
167
+ cv.setUint32(0, SIG_CENTRAL, true)
168
+ cv.setUint16(4, 20, true)
169
+ cv.setUint16(6, 20, true)
170
+ cv.setUint16(10, it.method, true)
171
+ cv.setUint16(12, DOS_TIME, true)
172
+ cv.setUint16(14, DOS_DATE, true)
173
+ cv.setUint32(16, it.crc, true)
174
+ cv.setUint32(20, it.compressedSize, true)
175
+ cv.setUint32(24, it.size, true)
176
+ cv.setUint16(28, name.length, true)
177
+ cv.setUint32(42, offset, true)
178
+ central.set(name, 46)
179
+
180
+ parts.push(local, it.data)
181
+ centrals.push(central)
182
+ offset += local.length + it.data.length
183
+ }
184
+ const cdSize = centrals.reduce((n, c) => n + c.length, 0)
185
+ if (offset + cdSize > 0xffffffff) throw new Error("Archive too large for a plain zip")
186
+ const eocd = new Uint8Array(22)
187
+ const ev = new DataView(eocd.buffer)
188
+ ev.setUint32(0, SIG_EOCD, true)
189
+ ev.setUint16(8, items.length, true)
190
+ ev.setUint16(10, items.length, true)
191
+ ev.setUint32(12, cdSize, true)
192
+ ev.setUint32(16, offset, true)
193
+
194
+ const out = new Uint8Array(offset + cdSize + 22)
195
+ let at = 0
196
+ for (const p of parts.concat(centrals, [eocd])) {
197
+ out.set(p, at)
198
+ at += p.length
199
+ await pause()
200
+ }
201
+ return out
202
+ }
203
+
204
+ export async function packEntry(path, bytes, compress = true) {
205
+ const crc = crc32(bytes)
206
+ let data = bytes
207
+ let method = 0
208
+ if (compress && bytes.length > 0) {
209
+ const packed = await deflateRaw(bytes)
210
+ if (packed.length < bytes.length) {
211
+ data = packed
212
+ method = 8
213
+ }
214
+ }
215
+ return { path, method, crc, size: bytes.length, compressedSize: data.length, data }
216
+ }
217
+
218
+ export function readZip(bytes) {
219
+ const buf = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)
220
+ return parseZip(buf).entries.filter(e => !e.path.endsWith("/")).map(entry => {
221
+ const out = { path: entry.path, size: entry.size, crc: entry.crc }
222
+ let memo
223
+ define(out, "read", () => memo ??= entryFromBuffer(buf, entry))
224
+ define(out, "raw", async () => ({ compression: entry.method === 8 ? "deflate-raw" : null, bytes: rawFromBuffer(buf, entry) }))
225
+ return out
226
+ })
227
+ }
228
+
229
+ export async function copyEntry(path, entry) {
230
+ const { compression, bytes } = await entry.raw()
231
+ return { path, method: compression ? 8 : 0, crc: entry.crc, size: entry.size, compressedSize: bytes.length, data: bytes }
232
+ }
233
+
234
+ function copyable(entry) {
235
+ return typeof entry.raw === "function" && typeof entry.crc === "number" && typeof entry.size === "number"
236
+ }
237
+
238
+ export async function writeZip(files, { compress = true, concurrency = 32, onProgress } = {}) {
239
+ const list = files instanceof Map ? Array.from(files)
240
+ : Array.isArray(files) ? files.map(f => [f.path, f])
241
+ : Object.entries(files)
242
+ const items = new Array(list.length)
243
+ const pause = budget()
244
+ let done = 0
245
+ await pool(list, concurrency, async ([path, data], i) => {
246
+ items[i] = data instanceof Uint8Array ? await packEntry(path, data, compress)
247
+ : copyable(data) ? await copyEntry(path, data)
248
+ : await packEntry(path, await data.read(), compress)
249
+ onProgress?.(++done, list.length)
250
+ await pause()
251
+ })
252
+ return buildZip(items, pause)
253
+ }
@@ -0,0 +1,15 @@
1
+ import zlib from "node:zlib"
2
+ import { promisify } from "node:util"
3
+
4
+ const inflate = promisify(zlib.inflateRaw)
5
+ const deflate = promisify(zlib.deflateRaw)
6
+
7
+ const plain = buf => new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
8
+
9
+ export async function inflateRaw(bytes) {
10
+ return plain(await inflate(bytes, { chunkSize: 1 << 20 }))
11
+ }
12
+
13
+ export async function deflateRaw(bytes, level) {
14
+ return plain(await deflate(bytes, { level: level ?? zlib.constants.Z_DEFAULT_COMPRESSION, chunkSize: 1 << 20 }))
15
+ }