summaryrefslogtreecommitdiff
path: root/test/parallel/test-filehandle-readablestream.js
blob: 70cd1814b2fe1b68c7265f13db9771c9a2b642e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';

const common = require('../common');
const assert = require('assert');

const {
  readFileSync,
} = require('fs');

const {
  open,
} = require('fs/promises');

const check = readFileSync(__filename, { encoding: 'utf8' });

// Make sure the ReadableStream works...
(async () => {
  const dec = new TextDecoder();
  const file = await open(__filename);
  let data = '';
  for await (const chunk of file.readableWebStream())
    data += dec.decode(chunk);

  assert.strictEqual(check, data);

  assert.throws(() => file.readableWebStream(), {
    code: 'ERR_INVALID_STATE',
  });

  await file.close();
})().then(common.mustCall());

// Make sure that acquiring a ReadableStream fails if the
// FileHandle is already closed.
(async () => {
  const file = await open(__filename);
  await file.close();

  assert.throws(() => file.readableWebStream(), {
    code: 'ERR_INVALID_STATE',
  });
})().then(common.mustCall());

// Make sure that acquiring a ReadableStream fails if the
// FileHandle is already closing.
(async () => {
  const file = await open(__filename);
  file.close();

  assert.throws(() => file.readableWebStream(), {
    code: 'ERR_INVALID_STATE',
  });
})().then(common.mustCall());

// Make sure the ReadableStream is closed when the underlying
// FileHandle is closed.
(async () => {
  const file = await open(__filename);
  const readable = file.readableWebStream();
  const reader = readable.getReader();
  file.close();
  await reader.closed;
})().then(common.mustCall());

// Make sure the ReadableStream is closed when the underlying
// FileHandle is closed.
(async () => {
  const file = await open(__filename);
  const readable = file.readableWebStream();
  file.close();
  const reader = readable.getReader();
  await reader.closed;
})().then(common.mustCall());

// Make sure that the FileHandle is properly marked "in use"
// when a ReadableStream has been acquired for it.
(async () => {
  const file = await open(__filename);
  file.readableWebStream();
  const mc = new MessageChannel();
  mc.port1.onmessage = common.mustNotCall();
  assert.throws(() => mc.port2.postMessage(file, [file]), {
    code: 25,
    name: 'DataCloneError',
  });
  mc.port1.close();
  await file.close();
})().then(common.mustCall());

// Make sure 'bytes' stream works
(async () => {
  const file = await open(__filename);
  const dec = new TextDecoder();
  const readable = file.readableWebStream({ type: 'bytes' });
  const reader = readable.getReader({ mode: 'byob' });

  let data = '';
  let result;
  do {
    const buff = new ArrayBuffer(100);
    result = await reader.read(new DataView(buff));
    if (result.value !== undefined) {
      data += dec.decode(result.value);
      assert.ok(result.value.byteLength <= 100);
    }
  } while (!result.done);

  assert.strictEqual(check, data);

  assert.throws(() => file.readableWebStream(), {
    code: 'ERR_INVALID_STATE',
  });

  await file.close();
})().then(common.mustCall());

// Make sure that acquiring a ReadableStream 'bytes' stream
// fails if the FileHandle is already closed.
(async () => {
  const file = await open(__filename);
  await file.close();

  assert.throws(() => file.readableWebStream({ type: 'bytes' }), {
    code: 'ERR_INVALID_STATE',
  });
})().then(common.mustCall());

// Make sure that acquiring a ReadableStream 'bytes' stream
// fails if the FileHandle is already closing.
(async () => {
  const file = await open(__filename);
  file.close();

  assert.throws(() => file.readableWebStream({ type: 'bytes' }), {
    code: 'ERR_INVALID_STATE',
  });
})().then(common.mustCall());

// Make sure the 'bytes' ReadableStream is closed when the underlying
// FileHandle is closed.
(async () => {
  const file = await open(__filename);
  const readable = file.readableWebStream({ type: 'bytes' });
  const reader = readable.getReader({ mode: 'byob' });
  file.close();
  await reader.closed;
})().then(common.mustCall());

// Make sure the 'bytes' ReadableStream is closed when the underlying
// FileHandle is closed.
(async () => {
  const file = await open(__filename);
  const readable = file.readableWebStream({ type: 'bytes' });
  file.close();
  const reader = readable.getReader({ mode: 'byob' });
  await reader.closed;
})().then(common.mustCall());

// Make sure that the FileHandle is properly marked "in use"
// when a 'bytes' ReadableStream has been acquired for it.
(async () => {
  const file = await open(__filename);
  file.readableWebStream({ type: 'bytes' });
  const mc = new MessageChannel();
  mc.port1.onmessage = common.mustNotCall();
  assert.throws(() => mc.port2.postMessage(file, [file]), {
    code: 25,
    name: 'DataCloneError',
  });
  mc.port1.close();
  await file.close();
})().then(common.mustCall());