2022-12-17 21:08:20 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
2022-12-17 21:58:26 +00:00
|
|
|
const {
|
|
|
|
strictEqual,
|
|
|
|
rejects,
|
2023-04-15 18:21:16 +00:00
|
|
|
throws,
|
2022-12-17 21:58:26 +00:00
|
|
|
} = require('assert');
|
|
|
|
const { TextDecoder } = require('util');
|
|
|
|
const {
|
|
|
|
writeFileSync,
|
|
|
|
openAsBlob,
|
|
|
|
} = require('fs');
|
|
|
|
|
|
|
|
const {
|
|
|
|
unlink
|
|
|
|
} = require('fs/promises');
|
2022-12-17 21:08:20 +00:00
|
|
|
const path = require('path');
|
2022-12-17 21:58:26 +00:00
|
|
|
const { Blob } = require('buffer');
|
2022-12-17 21:08:20 +00:00
|
|
|
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
2022-12-17 21:58:26 +00:00
|
|
|
const testfile = path.join(tmpdir.path, 'test-file-backed-blob.txt');
|
|
|
|
const testfile2 = path.join(tmpdir.path, 'test-file-backed-blob2.txt');
|
2023-03-23 18:28:43 +00:00
|
|
|
const testfile3 = path.join(tmpdir.path, 'test-file-backed-blob3.txt');
|
2022-12-17 21:08:20 +00:00
|
|
|
tmpdir.refresh();
|
|
|
|
|
|
|
|
const data = `${'a'.repeat(1000)}${'b'.repeat(2000)}`;
|
|
|
|
|
2022-12-17 21:58:26 +00:00
|
|
|
writeFileSync(testfile, data);
|
|
|
|
writeFileSync(testfile2, data.repeat(100));
|
2023-03-23 18:28:43 +00:00
|
|
|
writeFileSync(testfile3, '');
|
2022-12-17 21:08:20 +00:00
|
|
|
|
|
|
|
(async () => {
|
2022-12-17 21:58:26 +00:00
|
|
|
const blob = await openAsBlob(testfile);
|
2022-12-17 21:08:20 +00:00
|
|
|
|
|
|
|
const ab = await blob.arrayBuffer();
|
|
|
|
const dec = new TextDecoder();
|
|
|
|
|
|
|
|
strictEqual(dec.decode(new Uint8Array(ab)), data);
|
|
|
|
strictEqual(await blob.text(), data);
|
|
|
|
|
2022-12-17 21:58:26 +00:00
|
|
|
// Can be read multiple times
|
2022-12-17 21:08:20 +00:00
|
|
|
let stream = blob.stream();
|
|
|
|
let check = '';
|
|
|
|
for await (const chunk of stream)
|
|
|
|
check = dec.decode(chunk);
|
|
|
|
strictEqual(check, data);
|
|
|
|
|
2022-12-17 21:58:26 +00:00
|
|
|
// Can be combined with other Blob's and read
|
|
|
|
const combined = new Blob(['hello', blob, 'world']);
|
|
|
|
const ab2 = await combined.arrayBuffer();
|
|
|
|
strictEqual(dec.decode(ab2.slice(0, 5)), 'hello');
|
|
|
|
strictEqual(dec.decode(ab2.slice(5, -5)), data);
|
|
|
|
strictEqual(dec.decode(ab2.slice(-5)), 'world');
|
|
|
|
|
2022-12-17 21:08:20 +00:00
|
|
|
// If the file is modified tho, the stream errors.
|
2022-12-17 21:58:26 +00:00
|
|
|
writeFileSync(testfile, data + 'abc');
|
2022-12-17 21:08:20 +00:00
|
|
|
|
|
|
|
stream = blob.stream();
|
|
|
|
|
2022-12-17 21:58:26 +00:00
|
|
|
const read = async () => {
|
|
|
|
// eslint-disable-next-line no-unused-vars, no-empty
|
|
|
|
for await (const _ of stream) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
await rejects(read(), { name: 'NotReadableError' });
|
|
|
|
|
|
|
|
await unlink(testfile);
|
|
|
|
})().then(common.mustCall());
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const blob = await openAsBlob(testfile2);
|
|
|
|
const stream = blob.stream();
|
|
|
|
const read = async () => {
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
for await (const _ of stream) {
|
|
|
|
writeFileSync(testfile2, data + 'abc');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
await rejects(read(), { name: 'NotReadableError' });
|
|
|
|
|
|
|
|
await unlink(testfile2);
|
2022-12-17 21:08:20 +00:00
|
|
|
})().then(common.mustCall());
|
2023-03-23 18:28:43 +00:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const blob = await openAsBlob(testfile3);
|
|
|
|
strictEqual(blob.size, 0);
|
|
|
|
strictEqual(await blob.text(), '');
|
|
|
|
writeFileSync(testfile3, 'abc');
|
|
|
|
await rejects(blob.text(), { name: 'NotReadableError' });
|
|
|
|
await unlink(testfile3);
|
|
|
|
})().then(common.mustCall());
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const blob = await openAsBlob(testfile3);
|
|
|
|
strictEqual(blob.size, 0);
|
|
|
|
writeFileSync(testfile3, 'abc');
|
|
|
|
const stream = blob.stream();
|
|
|
|
const reader = stream.getReader();
|
|
|
|
await rejects(() => reader.read(), { name: 'NotReadableError' });
|
|
|
|
})().then(common.mustCall());
|
2023-04-15 18:21:16 +00:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
// We currently do not allow File-backed blobs to be cloned or transfered
|
|
|
|
// across worker threads. This is largely because the underlying FdEntry
|
|
|
|
// is bound to the Environment/Realm under which is was created.
|
|
|
|
const blob = await openAsBlob(__filename);
|
|
|
|
throws(() => structuredClone(blob), {
|
|
|
|
code: 'ERR_INVALID_STATE',
|
|
|
|
message: 'Invalid state: File-backed Blobs are not cloneable'
|
|
|
|
});
|
|
|
|
})().then(common.mustCall());
|