Repository navigation
Write binary files given to ready() as bytes in both Workers #65
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Summary
- Goal: Binary files (
ArrayBuffers) passed toready({ files })reach the virtual filesystems of both Workers with their bytes, as the type and documentation ofVrowzerConfig.filespromise. - Current gap: The runtime sends the initial files to both Workers as text, and both Workers write them with
{ encoding: 'utf8' }.- The Web Worker receives them in
V_WW_SETUP'sfiles. - The Service Worker receives them in
V_FS_INIT'sfiles. - An
ArrayBufferwritten this way becomes the string[object ArrayBuffer].
- The Web Worker receives them in
- Effect:
- Every binary file given to
ready()is corrupted: images, fonts, WASM, and so on. - Public images given to
ready()are now served (since #64), but with that content. - Binary files added later with
addFile()orupdateFile()are not affected.
- Every binary file given to
- Request: Send binary initial files to both Workers as binary, write them as bytes, and keep the caller's buffers usable.
- Verification status: Reproduced in Node.js with the built
@vrowzer/fssubscriber, which is what the Service Worker uses (see Details).- The Web Worker side was found by reading the code. It writes the files the same way.
- Not checked in a browser.
- Related: Found while working on #62. A fix may use
FileSystemPublisher.initFiles(), which has its own bug (#63).
Details
Current implementation, verification, requested behavior, and acceptance criteria
Current implementation
Reviewed at 750be3a.
VrowzerConfig.filesisRecord<string, string | ArrayBuffer>. Its documentation says that a value "can be either a string or an ArrayBuffer".initialize()spreadsconfig.filesintoallFilesasRecord<string, string>and sendsallFilesto both Workers:- to the Web Worker, as
V_WW_SETUP'sfiles; - to the Service Worker, as
V_FS_INIT's text fieldfiles. The binary fieldbinaryFilesis not used.
- to the Web Worker, as
- The Web Worker writes these files through
setupVirtualFiles(),updateFiles()andupdateFile(), withfs.writeFileSync(path, content, { encoding: 'utf8' })(setupVirtualFiles(),updateFile()). - The Service Worker's subscriber writes
V_FS_INIT.filesas text, withwriteToVol(path, 'text', content). ItsbinaryFilesbranch writes bytes correctly, but the runtime does not use it.
Verification
I ran the following with Node.js v24.21.0. It used the built @vrowzer/fs (packages/fs/dist), built from ba60d7b. @vrowzer/fs is unchanged in 750be3a.
const subscriber = createFileSystemSubscriber(fs)
const bytes = [0x89, 0x50, 0x4e, 0x47, 0x00, 0xff, 0x10]
// What the runtime sends to the Service Worker for ready({ files })
subscriber.handleMessage({ type: 'V_FS_INIT', files: { '/public/init.png': new Uint8Array(bytes).buffer } })
;[...fs.readFileSync('/public/init.png')]
// [91, 111, 98, 106, 101, 99, 116, 32, 65, 114, 114, 97, 121, 66, 117, 102, 102, 101, 114, 93]
// = "[object ArrayBuffer]"
// What addFile() sends
subscriber.handleMessage({ type: 'V_FS_WRITE', path: '/public/write.png', encoding: 'binary', content: new Uint8Array(bytes).buffer })
;[...fs.readFileSync('/public/write.png')]
// [137, 80, 78, 71, 0, 255, 16]Requested behavior
- Binary files given to
ready()are written as bytes in both Workers. - The caller's
ArrayBuffers stay usable. Each Worker gets its own copy, asaddFile()does since #61. - Text files are sent as before.
One possible approach:
- Split
config.filesinto text and binary files ininitialize(). - For the Service Worker, send the binary files in
V_FS_INIT.binaryFiles, with a copy for each target. Either send them directly, or fix Copy binary files for each target in FileSystemPublisher.initFiles() #63 first and useinitFiles(). - For the Web Worker, either:
- send the binary files in
V_WW_SETUPand write them as bytes insetupVirtualFiles(), or - send them with
V_FS_WRITEafter setup.
- send the binary files in
Acceptance criteria
- A binary file given to
ready()is served by the Service Worker with the same bytes. This includes a public image, and a file served by the static middleware. - The Web Worker reads the same bytes, for example when it inlines the file with
?inline. - After
ready(), the caller'sArrayBuffers keep their size and content. - A test covers this in the browser.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity