Skip to content

Commit

Permalink
fix(api/{fs/web.js,internal/pickers.js}): fix save file picker and we…
Browse files Browse the repository at this point in the history
…b handle writes
  • Loading branch information
jwerle committed Dec 5, 2024
1 parent 89b3d77 commit c741b35
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 9 additions & 4 deletions api/fs/web.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global ReadableStream, WritableStream, Blob */
import { isBufferLike, toBuffer, splitBuffer } from '../util.js'
import { DEFAULT_STREAM_HIGH_WATER_MARK } from './stream.js'
import { isBufferLike, toBuffer } from '../util.js'
import { NotAllowedError } from '../errors.js'
import { readFileSync } from './index.js'
import mime from '../mime.js'
Expand Down Expand Up @@ -282,7 +282,7 @@ export async function createFileSystemWritableFileStream (handle, options) {

const file = await handle.getFile()
let offset = 0
let fd = null
let fd = file?.[kFileDescriptor] || null

if (options?.keepExistingData === true) {
try {
Expand Down Expand Up @@ -350,7 +350,12 @@ export async function createFileSystemWritableFileStream (handle, options) {
}

const buffer = toBuffer(data)
await fd.write(buffer, 0, buffer.byteLength, offset)
const buffers = splitBuffer(buffer, 16 * 1024)
while (buffers.length) {
const buffer = buffers.shift()
await fd.write(buffer, 0, buffer.byteLength, offset)
offset += buffer.byteLength
}
}
}

Expand Down Expand Up @@ -384,7 +389,7 @@ export async function createFileSystemFileHandle (file, options = null) {
return path.basename(file)
}

return file.name
return file?.name ?? ''
}

get kind () {
Expand Down
6 changes: 5 additions & 1 deletion api/internal/pickers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SecurityError } from '../errors.js'
import application from '../application.js'
import path from '../path.js'
import fs from '../fs/promises.js'

import {
createFileSystemDirectoryHandle,
createFileSystemFileHandle,
createFile,
FileSystemHandle
} from '../fs/web.js'

Expand Down Expand Up @@ -281,8 +283,10 @@ export async function showSaveFilePicker (options = null) {
}

bookmarks.temporary.set(filename, null)
const fd = await fs.open(filename, 'w+')
const file = await createFile(filename, { fd })

return await createFileSystemFileHandle(filename)
return await createFileSystemFileHandle(file)
}

export default {
Expand Down

0 comments on commit c741b35

Please sign in to comment.