|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | + |
| 6 | +const { |
| 7 | + readFileSync, |
| 8 | +} = require('fs'); |
| 9 | + |
| 10 | +const { |
| 11 | + open, |
| 12 | +} = require('fs/promises'); |
| 13 | + |
| 14 | +const check = readFileSync(__filename, { encoding: 'utf8' }); |
| 15 | + |
| 16 | +// Make sure the ReadableStream works... |
| 17 | +(async () => { |
| 18 | + const dec = new TextDecoder(); |
| 19 | + const file = await open(__filename); |
| 20 | + let data = ''; |
| 21 | + for await (const chunk of file.readableWebStream()) |
| 22 | + data += dec.decode(chunk); |
| 23 | + |
| 24 | + assert.strictEqual(check, data); |
| 25 | + |
| 26 | + assert.throws(() => file.readableWebStream(), { |
| 27 | + code: 'ERR_INVALID_STATE', |
| 28 | + }); |
| 29 | + |
| 30 | + await file.close(); |
| 31 | +})().then(common.mustCall()); |
| 32 | + |
| 33 | +// Make sure that acquiring a ReadableStream fails if the |
| 34 | +// FileHandle is already closed. |
| 35 | +(async () => { |
| 36 | + const file = await open(__filename); |
| 37 | + await file.close(); |
| 38 | + |
| 39 | + assert.throws(() => file.readableWebStream(), { |
| 40 | + code: 'ERR_INVALID_STATE', |
| 41 | + }); |
| 42 | +})().then(common.mustCall()); |
| 43 | + |
| 44 | +// Make sure that acquiring a ReadableStream fails if the |
| 45 | +// FileHandle is already closing. |
| 46 | +(async () => { |
| 47 | + const file = await open(__filename); |
| 48 | + file.close(); |
| 49 | + |
| 50 | + assert.throws(() => file.readableWebStream(), { |
| 51 | + code: 'ERR_INVALID_STATE', |
| 52 | + }); |
| 53 | +})().then(common.mustCall()); |
| 54 | + |
| 55 | +// Make sure the ReadableStream is closed when the underlying |
| 56 | +// FileHandle is closed. |
| 57 | +(async () => { |
| 58 | + const file = await open(__filename); |
| 59 | + const readable = file.readableWebStream(); |
| 60 | + const reader = readable.getReader(); |
| 61 | + file.close(); |
| 62 | + await reader.closed; |
| 63 | +})().then(common.mustCall()); |
| 64 | + |
| 65 | +// Make sure the ReadableStream is closed when the underlying |
| 66 | +// FileHandle is closed. |
| 67 | +(async () => { |
| 68 | + const file = await open(__filename); |
| 69 | + const readable = file.readableWebStream(); |
| 70 | + file.close(); |
| 71 | + const reader = readable.getReader(); |
| 72 | + await reader.closed; |
| 73 | +})().then(common.mustCall()); |
| 74 | + |
| 75 | +// Make sure that the FileHandle is properly marked "in use" |
| 76 | +// when a ReadableStream has been acquired for it. |
| 77 | +(async () => { |
| 78 | + const file = await open(__filename); |
| 79 | + file.readableWebStream(); |
| 80 | + const mc = new MessageChannel(); |
| 81 | + mc.port1.onmessage = common.mustNotCall(); |
| 82 | + assert.throws(() => mc.port2.postMessage(file, [file]), { |
| 83 | + code: 25 // DataCloneError |
| 84 | + }); |
| 85 | + mc.port1.close(); |
| 86 | + await file.close(); |
| 87 | +})().then(common.mustCall()); |
0 commit comments