-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: fix missing createWritable in safari #62
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import FileSystemHandle from './FileSystemHandle.js' | ||
import FileSystemWritableFileStream from './FileSystemWritableFileStream.js' | ||
import { errors } from './util.js' | ||
|
||
const { INVALID, SYNTAX, GONE } = errors | ||
|
||
const kAdapter = Symbol('adapter') | ||
|
||
|
@@ -43,5 +46,183 @@ Object.defineProperties(FileSystemFileHandle.prototype, { | |
getFile: { enumerable: true } | ||
}) | ||
|
||
// Safari doesn't support async createWritable streams yet. | ||
if ( | ||
globalThis.FileSystemFileHandle && | ||
!globalThis.FileSystemFileHandle.prototype.createWritable | ||
) { | ||
const wm = new WeakMap() | ||
|
||
let workerUrl | ||
|
||
// Worker code that should be inlined (can't use any external functions) | ||
function code () { | ||
let fileHandle, handle | ||
|
||
onmessage = async evt => { | ||
const port = evt.ports[0] | ||
const cmd = evt.data | ||
switch (cmd.type) { | ||
case 'open': | ||
const file = cmd.name | ||
|
||
let dir = await navigator.storage.getDirectory() | ||
|
||
for (const folder of cmd.path) { | ||
dir = await dir.getDirectoryHandle(folder) | ||
} | ||
|
||
fileHandle = await dir.getFileHandle(file) | ||
handle = await fileHandle.createSyncAccessHandle() | ||
break | ||
case 'write': | ||
handle.write(cmd.data, { at: cmd.position }) | ||
handle.flush() | ||
break | ||
case 'truncate': | ||
handle.truncate(cmd.size) | ||
break | ||
case 'abort': | ||
case 'close': | ||
handle.close() | ||
break | ||
} | ||
|
||
port.postMessage(0) | ||
} | ||
} | ||
|
||
|
||
globalThis.FileSystemFileHandle.prototype.createWritable = async function (options) { | ||
// Safari only support writing data in a worker with sync access handle. | ||
if (!workerUrl) { | ||
const blob = new Blob([code.toString() + `;${code.name}();`], { | ||
type: 'text/javascript' | ||
}) | ||
workerUrl = URL.createObjectURL(blob) | ||
} | ||
const worker = new Worker(workerUrl, { type: 'module' }) | ||
|
||
let position = 0 | ||
const textEncoder = new TextEncoder() | ||
let size = await this.getFile().then(file => file.size) | ||
|
||
const send = message => new Promise((resolve, reject) => { | ||
const mc = new MessageChannel() | ||
mc.port1.onmessage = evt => { | ||
if (evt.data instanceof Error) reject(evt.data) | ||
else resolve(evt.data) | ||
mc.port1.close() | ||
mc.port2.close() | ||
mc.port1.onmessage = null | ||
} | ||
worker.postMessage(message, [mc.port2]) | ||
}) | ||
|
||
// Safari also don't support transferable file system handles. | ||
// So we need to pass the path to the worker. This is a bit hacky and ugly. | ||
const root = await navigator.storage.getDirectory() | ||
const parent = await wm.get(this) | ||
const path = await parent.resolve(root) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jimmywarting path was null for me. I had to swap these two. const path = await root.resolve(parent) |
||
|
||
// Should likely never happen, but just in case... | ||
if (path === null) throw new DOMException(...GONE) | ||
|
||
let controller | ||
await send({ type: 'open', path, name: this.name }) | ||
|
||
if (options?.keepExistingData === false) { | ||
await send({ type: 'truncate', size: 0 }) | ||
size = 0 | ||
} | ||
|
||
const ws = new FileSystemWritableFileStream({ | ||
start: ctrl => { | ||
controller = ctrl | ||
}, | ||
async write(chunk) { | ||
const isPlainObject = chunk?.constructor === Object | ||
|
||
if (isPlainObject) { | ||
chunk = { ...chunk } | ||
} else { | ||
chunk = { type: 'write', data: chunk, position } | ||
} | ||
|
||
if (chunk.type === 'write') { | ||
if (!('data' in chunk)) { | ||
await send({ type: 'close' }) | ||
throw new DOMException(...SYNTAX('write requires a data argument')) | ||
} | ||
|
||
chunk.position ??= position | ||
|
||
if (typeof chunk.data === 'string') { | ||
chunk.data = textEncoder.encode(chunk.data) | ||
} | ||
|
||
else if (chunk.data instanceof ArrayBuffer) { | ||
chunk.data = new Uint8Array(chunk.data) | ||
} | ||
|
||
else if (!(chunk.data instanceof Uint8Array) && ArrayBuffer.isView(chunk.data)) { | ||
chunk.data = new Uint8Array(chunk.data.buffer, chunk.data.byteOffset, chunk.data.byteLength) | ||
} | ||
|
||
else if (!(chunk.data instanceof Uint8Array)) { | ||
const ab = await new Response(chunk.data).arrayBuffer() | ||
chunk.data = new Uint8Array(ab) | ||
} | ||
|
||
if (Number.isInteger(chunk.position) && chunk.position >= 0) { | ||
position = chunk.position | ||
} | ||
position += chunk.data.byteLength | ||
size += chunk.data.byteLength | ||
} else if (chunk.type === 'seek') { | ||
if (Number.isInteger(chunk.position) && chunk.position >= 0) { | ||
if (size < chunk.position) { | ||
throw new DOMException(...INVALID) | ||
} | ||
console.log('seeking', chunk) | ||
position = chunk.position | ||
return // Don't need to enqueue seek... | ||
} else { | ||
await send({ type: 'close' }) | ||
throw new DOMException(...SYNTAX('seek requires a position argument')) | ||
} | ||
} else if (chunk.type === 'truncate') { | ||
if (Number.isInteger(chunk.size) && chunk.size >= 0) { | ||
size = chunk.size | ||
if (position > size) { position = size } | ||
} else { | ||
await send({ type: 'close' }) | ||
throw new DOMException(...SYNTAX('truncate requires a size argument')) | ||
} | ||
} | ||
|
||
await send(chunk) | ||
}, | ||
async close () { | ||
await send({ type: 'close' }) | ||
worker.terminate() | ||
}, | ||
async abort (reason) { | ||
await send({ type: 'abort', reason }) | ||
worker.terminate() | ||
}, | ||
}) | ||
|
||
return ws | ||
} | ||
|
||
const orig = FileSystemDirectoryHandle.prototype.getFileHandle | ||
FileSystemDirectoryHandle.prototype.getFileHandle = async function (...args) { | ||
const handle = await orig.call(this, ...args) | ||
wm.set(handle, this) | ||
return handle | ||
} | ||
} | ||
|
||
export default FileSystemFileHandle | ||
export { FileSystemFileHandle } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another issue here...
not sure why but
produces:
function() {....}
which throws a syntax error because function doesnt have name.
To solve this, i change code to be arrow function:
and then here I have:
which produces:
And this works.