-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Native File System] Writable File Stream API
This change adds a writable stream output to the `FileSystemFileHandle`. This follows some [discussion](WICG/file-system-access#19 (comment)) on github. While this writable stream does not yet feature stream-like behavior, it carries over some of the interface from `NativeFileSystemWriter`, namely `truncate` and `write`. Bug: 955189 Change-Id: I8b8c9ff1ef36adfb30501251563699c6c7973889
- Loading branch information
1 parent
fabbe20
commit 4df0a4a
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
native-file-system/NativeFileSystemWritableFileStream.tentative.window.js
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// META: script=resources/test-helpers.js | ||
promise_test(async t => cleanupSandboxedFileSystem(), | ||
'Cleanup to setup test environment'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'empty_blob'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, new Blob([])); | ||
|
||
assert_equals(await getFileContents(handle), ''); | ||
assert_equals(await getFileSize(handle), 0); | ||
}, 'write() with an empty blob to an empty file'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'valid_blob'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, new Blob(['1234567890'])); | ||
|
||
assert_equals(await getFileContents(handle), '1234567890'); | ||
assert_equals(await getFileSize(handle), 10); | ||
}, 'write() a blob to an empty file'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'blob_with_offset'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, new Blob(['1234567890'])); | ||
await stream.write(4, new Blob(['abc'])); | ||
|
||
assert_equals(await getFileContents(handle), '1234abc890'); | ||
assert_equals(await getFileSize(handle), 10); | ||
}, 'write() called with a blob and a valid offset'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'bad_offset'); | ||
const stream = await handle.createWritable(); | ||
|
||
await promise_rejects(t, 'InvalidStateError', stream.write(4, new Blob(['abc']))); | ||
|
||
assert_equals(await getFileContents(handle), ''); | ||
assert_equals(await getFileSize(handle), 0); | ||
}, 'write() called with an invalid offset'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'empty_string'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, ''); | ||
assert_equals(await getFileContents(handle), ''); | ||
assert_equals(await getFileSize(handle), 0); | ||
}, 'write() with an empty string to an empty file'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'valid_utf8_string'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, 'foo🤘'); | ||
assert_equals(await getFileContents(handle), 'foo🤘'); | ||
assert_equals(await getFileSize(handle), 7); | ||
}, 'write() with a valid utf-8 string'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'string_with_unix_line_ending'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, 'foo\n'); | ||
assert_equals(await getFileContents(handle), 'foo\n'); | ||
assert_equals(await getFileSize(handle), 4); | ||
}, 'write() with a string with unix line ending preserved'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'string_with_windows_line_ending'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, 'foo\r\n'); | ||
assert_equals(await getFileContents(handle), 'foo\r\n'); | ||
assert_equals(await getFileSize(handle), 5); | ||
}, 'write() with a string with windows line ending preserved'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'empty_array_buffer'); | ||
const stream = await handle.createWritable(); | ||
|
||
let buf = new ArrayBuffer(0); | ||
await stream.write(0, buf); | ||
assert_equals(await getFileContents(handle), ''); | ||
assert_equals(await getFileSize(handle), 0); | ||
}, 'write() with an empty array buffer to an empty file'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'valid_string_typed_byte_array'); | ||
const stream = await handle.createWritable(); | ||
|
||
let buf = new ArrayBuffer(3); | ||
let intView = new Uint8Array(buf); | ||
intView[0] = 0x66; | ||
intView[1] = 0x6f; | ||
intView[2] = 0x6f; | ||
await stream.write(0, buf); | ||
assert_equals(await getFileContents(handle), 'foo'); | ||
assert_equals(await getFileSize(handle), 3); | ||
}, 'write() with a valid typed array buffer'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'trunc_shrink'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, new Blob(['1234567890'])); | ||
await stream.truncate(5); | ||
|
||
assert_equals(await getFileContents(handle), '12345'); | ||
assert_equals(await getFileSize(handle), 5); | ||
}, 'truncate() to shrink a file'); | ||
|
||
promise_test(async t => { | ||
const handle = await createEmptyFile(t, 'trunc_grow'); | ||
const stream = await handle.createWritable(); | ||
|
||
await stream.write(0, new Blob(['abc'])); | ||
await stream.truncate(5); | ||
|
||
assert_equals(await getFileContents(handle), 'abc\0\0'); | ||
assert_equals(await getFileSize(handle), 5); | ||
}, 'truncate() to grow a file'); |