@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/LICENSE +373 -0
- package/README.md +453 -2
- package/dist/mc-asset-loader.min.js +11 -0
- package/index.d.ts +344 -0
- package/package.json +52 -4
- package/src/bedrock.js +54 -0
- package/src/cache.js +127 -0
- package/src/index.js +471 -0
- package/src/inflate.js +168 -0
- package/src/jar.js +267 -0
- package/src/local.js +62 -0
- package/src/manifest.js +442 -0
- package/src/objects.js +18 -0
- package/src/opfs-cache.js +153 -0
- package/src/source.js +68 -0
- package/src/store.js +69 -0
- package/src/util.js +105 -0
- package/src/version.js +160 -0
- package/src/zip.js +253 -0
- package/src/zlib-node.js +15 -0
package/src/jar.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { findEocd, parseEocd, parseCentralDirectory, rawFromBuffer } from "./zip.js"
|
|
2
|
+
import { ZipSource } from "./source.js"
|
|
3
|
+
import { encoder, pool, readBody, memo, collator } from "./util.js"
|
|
4
|
+
|
|
5
|
+
const TAIL_PROBE = 65536
|
|
6
|
+
const TAIL_PROBE_MAX = 4 << 20
|
|
7
|
+
const LOCAL_PAD = 4096
|
|
8
|
+
const MERGE_GAP = 65536
|
|
9
|
+
const RANGE_CONCURRENCY = 6
|
|
10
|
+
|
|
11
|
+
const PACK_MAGIC = 0x4a41434d
|
|
12
|
+
const PACK_HEADER = 8
|
|
13
|
+
const PACK_ENTRY = 16
|
|
14
|
+
|
|
15
|
+
const isDirectory = path => path.endsWith("/")
|
|
16
|
+
|
|
17
|
+
export function isData(path, legacyLayout) {
|
|
18
|
+
if (isDirectory(path)) return false
|
|
19
|
+
if (legacyLayout) return !path.endsWith(".class") && !path.startsWith("META-INF/")
|
|
20
|
+
if (path.startsWith("assets/") || path.startsWith("data/")) return !path.endsWith("/.mcassetsroot")
|
|
21
|
+
return path === "pack.png" || path === "pack.mcmeta" || path === "version.json"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function packChunks(chunks) {
|
|
25
|
+
const total = chunks.reduce((n, c) => n + c.bytes.length, 0)
|
|
26
|
+
const out = new Uint8Array(PACK_HEADER + chunks.length * PACK_ENTRY + total)
|
|
27
|
+
const view = new DataView(out.buffer)
|
|
28
|
+
view.setUint32(0, PACK_MAGIC, true)
|
|
29
|
+
view.setUint32(4, chunks.length, true)
|
|
30
|
+
let at = PACK_HEADER + chunks.length * PACK_ENTRY
|
|
31
|
+
chunks.forEach((c, i) => {
|
|
32
|
+
view.setFloat64(PACK_HEADER + i * PACK_ENTRY, c.start, true)
|
|
33
|
+
view.setFloat64(PACK_HEADER + i * PACK_ENTRY + 8, c.bytes.length, true)
|
|
34
|
+
out.set(c.bytes, at)
|
|
35
|
+
at += c.bytes.length
|
|
36
|
+
})
|
|
37
|
+
return out
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function unpackChunks(packed, size) {
|
|
41
|
+
if (packed.length < PACK_HEADER) return null
|
|
42
|
+
const view = new DataView(packed.buffer, packed.byteOffset, packed.byteLength)
|
|
43
|
+
if (view.getUint32(0, true) !== PACK_MAGIC) return null
|
|
44
|
+
const count = view.getUint32(4, true)
|
|
45
|
+
let at = PACK_HEADER + count * PACK_ENTRY
|
|
46
|
+
if (count === 0 || at > packed.length) return null
|
|
47
|
+
const chunks = []
|
|
48
|
+
for (let i = 0; i < count; i++) {
|
|
49
|
+
const start = view.getFloat64(PACK_HEADER + i * PACK_ENTRY, true)
|
|
50
|
+
const length = view.getFloat64(PACK_HEADER + i * PACK_ENTRY + 8, true)
|
|
51
|
+
if (at + length > packed.length || start + length > size) return null
|
|
52
|
+
chunks.push({ start, bytes: packed.subarray(at, at + length) })
|
|
53
|
+
at += length
|
|
54
|
+
}
|
|
55
|
+
const last = chunks[chunks.length - 1]
|
|
56
|
+
return last.start + last.bytes.length === size ? chunks : null
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class Jar extends ZipSource {
|
|
60
|
+
constructor(options) {
|
|
61
|
+
super(options)
|
|
62
|
+
this.sha1 = options.sha1
|
|
63
|
+
this.legacyLayout = options.legacyLayout
|
|
64
|
+
this.local = options.local ?? null
|
|
65
|
+
this._cached = null
|
|
66
|
+
this._tail = null
|
|
67
|
+
this._whole = null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get metaKey() { return "jar_" + this.sha1 }
|
|
71
|
+
get blobKey() { return "jar_" + this.sha1 }
|
|
72
|
+
|
|
73
|
+
async _range(start, end, tick) {
|
|
74
|
+
if (this.local) {
|
|
75
|
+
try {
|
|
76
|
+
return await this._readLocal(start, end, tick)
|
|
77
|
+
} catch {
|
|
78
|
+
this.local = null
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const wanted = end - start
|
|
82
|
+
if (this._whole) {
|
|
83
|
+
tick?.(wanted)
|
|
84
|
+
return this._whole.subarray(start, end)
|
|
85
|
+
}
|
|
86
|
+
// a browser's HTTP cache can answer a range with only the part of it an earlier range already fetched
|
|
87
|
+
const res = await this.request(this.url, { headers: { Range: `bytes=${start}-${end - 1}` }, cache: "no-store" })
|
|
88
|
+
if (res.status === 200) {
|
|
89
|
+
const whole = new Uint8Array(await res.arrayBuffer())
|
|
90
|
+
if (whole.length !== this.size) throw new Error(`Ranged request refused (200, ${whole.length} bytes for the whole jar) by ${this.url}`)
|
|
91
|
+
this._whole = whole
|
|
92
|
+
tick?.(wanted)
|
|
93
|
+
return whole.subarray(start, end)
|
|
94
|
+
}
|
|
95
|
+
const bytes = tick && res.body ? await readBody(res, tick, wanted) : new Uint8Array(await res.arrayBuffer())
|
|
96
|
+
if (res.status !== 206 || bytes.length !== wanted) throw new Error(`Ranged request refused (${res.status}, ${bytes.length} bytes for ${wanted}) by ${this.url}`)
|
|
97
|
+
if (tick && !res.body) tick(wanted)
|
|
98
|
+
return bytes
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async _readLocal(start, end, tick) {
|
|
102
|
+
const fs = await import("node:fs/promises")
|
|
103
|
+
const handle = await fs.open(this.local, "r")
|
|
104
|
+
try {
|
|
105
|
+
const bytes = new Uint8Array(end - start)
|
|
106
|
+
const { bytesRead } = await handle.read(bytes, 0, bytes.length, start)
|
|
107
|
+
if (bytesRead !== bytes.length) throw new Error("Short read")
|
|
108
|
+
tick?.(bytes.length)
|
|
109
|
+
return bytes
|
|
110
|
+
} finally {
|
|
111
|
+
await handle.close()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
cached() {
|
|
116
|
+
return memo(this, "_cached", () => this._loadCached())
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async _loadCached() {
|
|
120
|
+
const packed = await this.store.get("blobs", this.blobKey)
|
|
121
|
+
if (!packed) return null
|
|
122
|
+
const chunks = unpackChunks(packed, this.size)
|
|
123
|
+
if (!chunks) await this.store.delete("blobs", this.blobKey)
|
|
124
|
+
return chunks
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async _loadListing() {
|
|
128
|
+
const cached = await this.store.get("meta", this.metaKey)
|
|
129
|
+
if (cached?.entries && cached.size === this.size) return this._inflateListing(cached)
|
|
130
|
+
const chunks = await this.cached()
|
|
131
|
+
if (chunks) return this._listingFromZip(this._parseTail(chunks[chunks.length - 1]))
|
|
132
|
+
return this._probe()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
_parseTail(chunk) {
|
|
136
|
+
const at = findEocd(chunk.bytes)
|
|
137
|
+
if (at < 0) throw new Error("No zip end record found in " + this.url)
|
|
138
|
+
const eocd = parseEocd(chunk.bytes, at)
|
|
139
|
+
return { ...eocd, entries: parseCentralDirectory(chunk.bytes.subarray(eocd.cdOffset - chunk.start, eocd.cdOffset - chunk.start + eocd.cdSize)) }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
_inflateListing({ entries, cdOffset, cdSize, sorted }) {
|
|
143
|
+
const map = new Map()
|
|
144
|
+
for (const [path, offset, compressedSize, size, method, crc] of entries) {
|
|
145
|
+
map.set(path, { path, offset, compressedSize, size, method, crc })
|
|
146
|
+
}
|
|
147
|
+
return { entries: map, cdOffset, cdSize, sorted: !!sorted }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
_listingFromZip({ entries, cdOffset, cdSize }) {
|
|
151
|
+
const map = new Map()
|
|
152
|
+
for (const e of entries) if (isData(e.path, this.legacyLayout)) map.set(e.path, e)
|
|
153
|
+
const listing = { entries: map, cdOffset, cdSize, sorted: false }
|
|
154
|
+
listing.sorting = new Promise(resolve => setTimeout(resolve, 0)).then(() => {
|
|
155
|
+
const data = Array.from(map.values()).sort((a, b) => collator.compare(a.path, b.path))
|
|
156
|
+
listing.order = data.map(e => e.path)
|
|
157
|
+
listing.sorted = true
|
|
158
|
+
this._persistedMeta = this.store.set("meta", this.metaKey, {
|
|
159
|
+
size: this.size,
|
|
160
|
+
cdOffset,
|
|
161
|
+
cdSize,
|
|
162
|
+
sorted: true,
|
|
163
|
+
entries: data.map(e => [e.path, e.offset, e.compressedSize, e.size, e.method, e.crc])
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
return listing
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async _probe() {
|
|
170
|
+
const tailStart = Math.max(0, this.size - Math.min(TAIL_PROBE_MAX, Math.max(TAIL_PROBE, Math.round(this.size / 10))))
|
|
171
|
+
const tail = await this._range(tailStart, this.size)
|
|
172
|
+
const at = findEocd(tail)
|
|
173
|
+
if (at < 0) throw new Error("No zip end record found in " + this.url)
|
|
174
|
+
const eocd = parseEocd(tail, at)
|
|
175
|
+
let chunk = { start: tailStart, bytes: tail }
|
|
176
|
+
if (eocd.cdOffset < tailStart) {
|
|
177
|
+
const head = await this._range(eocd.cdOffset, tailStart)
|
|
178
|
+
const bytes = new Uint8Array(this.size - eocd.cdOffset)
|
|
179
|
+
bytes.set(head, 0)
|
|
180
|
+
bytes.set(tail, tailStart - eocd.cdOffset)
|
|
181
|
+
chunk = { start: eocd.cdOffset, bytes }
|
|
182
|
+
}
|
|
183
|
+
this._tail = chunk
|
|
184
|
+
return this._listingFromZip(this._parseTail(chunk))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async _loadBuffer() {
|
|
188
|
+
const cached = await this.cached()
|
|
189
|
+
const { entries, cdOffset } = await this.listing()
|
|
190
|
+
if (cached) {
|
|
191
|
+
if (this._covers(cached, entries)) return cached
|
|
192
|
+
await this.store.delete("blobs", this.blobKey).catch(() => {})
|
|
193
|
+
}
|
|
194
|
+
const tail = this._tail
|
|
195
|
+
const ranges = Array.from(entries.values())
|
|
196
|
+
.filter(e => !tail || e.offset < tail.start)
|
|
197
|
+
.map(e => this._entryRange(e, this.size))
|
|
198
|
+
if (!tail) ranges.push([cdOffset, this.size])
|
|
199
|
+
const merged = mergeRanges(ranges, MERGE_GAP)
|
|
200
|
+
const onProgress = this._progress
|
|
201
|
+
let tick
|
|
202
|
+
if (onProgress) {
|
|
203
|
+
const total = merged.reduce((n, [start, end]) => n + end - start, 0)
|
|
204
|
+
let done = 0
|
|
205
|
+
onProgress(0, total)
|
|
206
|
+
tick = n => onProgress(done += n, total)
|
|
207
|
+
}
|
|
208
|
+
const chunks = []
|
|
209
|
+
await pool(merged, RANGE_CONCURRENCY, async ([start, end]) => {
|
|
210
|
+
chunks.push({ start, bytes: await this._range(start, end, tick) })
|
|
211
|
+
})
|
|
212
|
+
if (tail) chunks.push(tail)
|
|
213
|
+
chunks.sort((a, b) => a.start - b.start)
|
|
214
|
+
if (!this.local) this._persisted = this._persist(chunks)
|
|
215
|
+
return chunks
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async _persist(chunks) {
|
|
219
|
+
try {
|
|
220
|
+
await this.store.set("blobs", this.blobKey, packChunks(chunks))
|
|
221
|
+
} catch {}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
_entryRange(e, limit) {
|
|
225
|
+
const end = e.offset + 30 + (e.nameLen ?? encoder.encode(e.path).length) + e.compressedSize + LOCAL_PAD
|
|
226
|
+
return [e.offset, Math.min(limit, end)]
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
_covers(chunks, entries) {
|
|
230
|
+
for (const e of entries.values()) {
|
|
231
|
+
const chunk = chunks.findLast(c => c.start <= e.offset)
|
|
232
|
+
if (!chunk) return false
|
|
233
|
+
const at = e.offset - chunk.start
|
|
234
|
+
const bytes = chunk.bytes
|
|
235
|
+
if (at + 30 > bytes.length) return false
|
|
236
|
+
const nameLen = bytes[at + 26] | (bytes[at + 27] << 8)
|
|
237
|
+
const extraLen = bytes[at + 28] | (bytes[at + 29] << 8)
|
|
238
|
+
if (at + 30 + nameLen + extraLen + e.compressedSize > bytes.length) return false
|
|
239
|
+
}
|
|
240
|
+
return true
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async raw(path) {
|
|
244
|
+
if (this._list && this._held) return this.rawSync(path)
|
|
245
|
+
await Promise.all([this.listing(), this.buffer()])
|
|
246
|
+
return this.rawSync(path)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
rawSync(path) {
|
|
250
|
+
const entry = this._list.entries.get(path)
|
|
251
|
+
if (!entry) return null
|
|
252
|
+
const chunks = this._held
|
|
253
|
+
const chunk = chunks.findLast(c => c.start <= entry.offset)
|
|
254
|
+
return { entry, data: rawFromBuffer(chunk.bytes, { ...entry, offset: entry.offset - chunk.start }) }
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function mergeRanges(ranges, gap) {
|
|
259
|
+
const sorted = ranges.filter(r => r[1] > r[0]).sort((a, b) => a[0] - b[0])
|
|
260
|
+
const out = []
|
|
261
|
+
for (const [start, end] of sorted) {
|
|
262
|
+
const last = out[out.length - 1]
|
|
263
|
+
if (last && start - last[1] <= gap) last[1] = Math.max(last[1], end)
|
|
264
|
+
else out.push([start, end])
|
|
265
|
+
}
|
|
266
|
+
return out
|
|
267
|
+
}
|
package/src/local.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import os from "node:os"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import crypto from "node:crypto"
|
|
5
|
+
import { decoder } from "./util.js"
|
|
6
|
+
|
|
7
|
+
export function defaultMinecraftDir() {
|
|
8
|
+
if (process.platform === "win32") return path.join(process.env.APPDATA ?? path.join(os.homedir(), "AppData", "Roaming"), ".minecraft")
|
|
9
|
+
if (process.platform === "darwin") return path.join(os.homedir(), "Library", "Application Support", "minecraft")
|
|
10
|
+
return path.join(os.homedir(), ".minecraft")
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function sha1(bytes) {
|
|
14
|
+
return crypto.createHash("sha1").update(bytes).digest("hex")
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function sha1File(file) {
|
|
18
|
+
const hash = crypto.createHash("sha1")
|
|
19
|
+
for await (const chunk of fs.createReadStream(file)) hash.update(chunk)
|
|
20
|
+
return hash.digest("hex")
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class LocalInstall {
|
|
24
|
+
constructor(dir, store) {
|
|
25
|
+
this.dir = dir
|
|
26
|
+
this.store = store
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async object(hash) {
|
|
30
|
+
try {
|
|
31
|
+
const bytes = await fs.promises.readFile(path.join(this.dir, "assets", "objects", hash.slice(0, 2), hash))
|
|
32
|
+
if (sha1(bytes) === hash) return bytes
|
|
33
|
+
} catch {}
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async index(id, hash) {
|
|
38
|
+
if (!hash) return null
|
|
39
|
+
try {
|
|
40
|
+
const bytes = await fs.promises.readFile(path.join(this.dir, "assets", "indexes", id + ".json"))
|
|
41
|
+
if (sha1(bytes) === hash) return JSON.parse(decoder.decode(bytes))
|
|
42
|
+
} catch {}
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async jar(id, client) {
|
|
47
|
+
if (!client?.sha1) return null
|
|
48
|
+
const file = path.join(this.dir, "versions", id, id + ".jar")
|
|
49
|
+
try {
|
|
50
|
+
const stat = await fs.promises.stat(file)
|
|
51
|
+
if (client.size != null && stat.size !== client.size) return null
|
|
52
|
+
const key = "local_jar_" + client.sha1
|
|
53
|
+
const note = await this.store.get("meta", key)
|
|
54
|
+
if (note?.size === stat.size && note.mtime === stat.mtimeMs) return file
|
|
55
|
+
if (await sha1File(file) !== client.sha1) return null
|
|
56
|
+
await this.store.set("meta", key, { size: stat.size, mtime: stat.mtimeMs })
|
|
57
|
+
return file
|
|
58
|
+
} catch {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|