|
| 1 | +import type {EmulatorChunkedFileSpec} from "./emulator-common"; |
| 2 | +import {generateChunkUrl} from "./emulator-common"; |
| 3 | + |
| 4 | +export function createChunkedFile( |
| 5 | + parent: string | FS.FSNode, |
| 6 | + name: string, |
| 7 | + spec: EmulatorChunkedFileSpec, |
| 8 | + canRead: boolean, |
| 9 | + canWrite: boolean |
| 10 | +): FS.FSNode { |
| 11 | + const contents = new Uint8Array(spec.totalSize); |
| 12 | + const file = FS.createDataFile( |
| 13 | + parent, |
| 14 | + name, |
| 15 | + contents, |
| 16 | + canRead, |
| 17 | + canWrite, |
| 18 | + // Set canOwn so that MEMFS uses `contents` directly, and we can mutate |
| 19 | + // it later when loading the contents on demand. |
| 20 | + true |
| 21 | + ); |
| 22 | + |
| 23 | + const {chunkSize} = spec; |
| 24 | + const loadedChunks = new Set<number>(); |
| 25 | + loadedChunksBySpec.set(spec, loadedChunks); |
| 26 | + |
| 27 | + function loadChunk(chunkIndex: number) { |
| 28 | + const chunkUrl = generateChunkUrl(spec, chunkIndex); |
| 29 | + const xhr = new XMLHttpRequest(); |
| 30 | + xhr.responseType = "arraybuffer"; |
| 31 | + xhr.open("GET", chunkUrl, false); |
| 32 | + xhr.send(); |
| 33 | + const data = new Uint8Array(xhr.response as ArrayBuffer); |
| 34 | + contents.set(data, chunkIndex * chunkSize); |
| 35 | + } |
| 36 | + |
| 37 | + const defaultStreamOps = file.stream_ops; |
| 38 | + file.stream_ops = { |
| 39 | + ...defaultStreamOps, |
| 40 | + read( |
| 41 | + stream: FS.FSStream, |
| 42 | + buffer: Uint8Array, |
| 43 | + offset: number, |
| 44 | + length: number, |
| 45 | + position: number |
| 46 | + ) { |
| 47 | + if (position >= contents.length) { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + if (stream.node.contents.buffer !== contents.buffer) { |
| 51 | + // If Emscripten somehow changes the contents out from under us, |
| 52 | + // we should go back to normal reading. |
| 53 | + return defaultStreamOps.read.call( |
| 54 | + this, |
| 55 | + stream, |
| 56 | + buffer, |
| 57 | + offset, |
| 58 | + length, |
| 59 | + position |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + const readSize = Math.min(contents.length - position, length); |
| 64 | + |
| 65 | + const startChunk = Math.floor(position / chunkSize); |
| 66 | + const endChunk = Math.floor((position + readSize - 1) / chunkSize); |
| 67 | + for ( |
| 68 | + let chunkIndex = startChunk; |
| 69 | + chunkIndex <= endChunk; |
| 70 | + chunkIndex++ |
| 71 | + ) { |
| 72 | + if (!loadedChunks.has(chunkIndex)) { |
| 73 | + loadChunk(chunkIndex); |
| 74 | + loadedChunks.add(chunkIndex); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + buffer.set(contents.slice(position, position + readSize), offset); |
| 79 | + return readSize; |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + return file; |
| 84 | +} |
| 85 | + |
| 86 | +export function validateSpecPrefetchChunks(spec: EmulatorChunkedFileSpec) { |
| 87 | + const loadedChunks = loadedChunksBySpec.get(spec); |
| 88 | + if (!loadedChunks) { |
| 89 | + console.warn( |
| 90 | + `Chunked file ${spec.baseUrl} never had any chunks loaded` |
| 91 | + ); |
| 92 | + return; |
| 93 | + } |
| 94 | + const prefetchedChunks = new Set(spec.prefetchChunks); |
| 95 | + const needsPrefetch = []; |
| 96 | + const extraPrefetch = []; |
| 97 | + for (const chunk of loadedChunks) { |
| 98 | + if (!prefetchedChunks.has(chunk)) { |
| 99 | + needsPrefetch.push(chunk); |
| 100 | + } |
| 101 | + } |
| 102 | + for (const chunk of prefetchedChunks) { |
| 103 | + if (!loadedChunks.has(chunk)) { |
| 104 | + extraPrefetch.push(chunk); |
| 105 | + } |
| 106 | + } |
| 107 | + if (extraPrefetch.length) { |
| 108 | + console.warn( |
| 109 | + `Chunked file ${spec.baseUrl} had unncessary chunks prefetched:`, |
| 110 | + extraPrefetch |
| 111 | + ); |
| 112 | + } |
| 113 | + if (needsPrefetch.length) { |
| 114 | + console.warn( |
| 115 | + `Chunked file ${spec.baseUrl} had needs more chunks prefetched:`, |
| 116 | + needsPrefetch |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +const loadedChunksBySpec = new Map<EmulatorChunkedFileSpec, Set<number>>(); |
0 commit comments