summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fs/script-tests/FileSystemWritableFileStream-piped.js
blob: 79e4cfb4f22be6fe99c901b5bb435119a9e78bd6 (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
'use strict';

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'foo_string.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue('foo_string');
      controller.close();
    }
  });

  await rs.pipeTo(wfs, { preventCancel: true });
  assert_equals(await getFileContents(handle), 'foo_string');
  assert_equals(await getFileSize(handle), 10);
}, 'can be piped to with a string');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'foo_arraybuf.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());
  const buf = new ArrayBuffer(3);
  const intView = new Uint8Array(buf);
  intView[0] = 0x66;
  intView[1] = 0x6f;
  intView[2] = 0x6f;

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue(buf);
      controller.close();
    }
  });

  await rs.pipeTo(wfs, { preventCancel: true });
  assert_equals(await getFileContents(handle), 'foo');
  assert_equals(await getFileSize(handle), 3);
}, 'can be piped to with an ArrayBuffer');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'foo_blob.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue(new Blob(['foo']));
      controller.close();
    }
  });

  await rs.pipeTo(wfs, { preventCancel: true });
  assert_equals(await getFileContents(handle), 'foo');
  assert_equals(await getFileSize(handle), 3);
}, 'can be piped to with a Blob');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'foo_write_param.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue({type: 'write', data: 'foobar'});
      controller.close();
    }
  });

  await rs.pipeTo(wfs, { preventCancel: true });
  assert_equals(await getFileContents(handle), 'foobar');
  assert_equals(await getFileSize(handle), 6);
}, 'can be piped to with a param object with write command');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'foo_write_param.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue({type: 'write', data: 'foobar'});
      controller.enqueue({type: 'truncate', size: 10});
      controller.enqueue({type: 'write', position: 0, data: 'baz'});
      controller.close();
    }
  });

  await rs.pipeTo(wfs, { preventCancel: true });
  assert_equals(await getFileContents(handle), 'bazbar\0\0\0\0');
  assert_equals(await getFileSize(handle), 10);
}, 'can be piped to with a param object with multiple commands');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'foo_write_queued.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue('foo');
      controller.enqueue('bar');
      controller.enqueue('baz');
      controller.close();
    }
  });

  await rs.pipeTo(wfs, { preventCancel: true });
  assert_equals(await getFileContents(handle), 'foobarbaz');
  assert_equals(await getFileSize(handle), 9);
}, 'multiple operations can be queued');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'fetched.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());

  const response = await fetch('data:text/plain,fetched from far');
  const body = await response.body;
  await body.pipeTo(wfs, { preventCancel: true });
  assert_equals(await getFileContents(handle), 'fetched from far');
  assert_equals(await getFileSize(handle), 16);
}, 'plays well with fetch');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'aborted should_be_empty.txt', root);
  const wfs = await cleanup_writable(t, await handle.createWritable());

  const response = await fetch('data:text/plain,fetched from far');
  const body = await response.body;

  const abortController = new AbortController();
  const signal = abortController.signal;

  const promise = body.pipeTo(wfs, { signal });
  await abortController.abort();

  await promise_rejects_dom(t, 'AbortError', promise, 'stream is aborted');
  await promise_rejects_js(t, TypeError, wfs.close(), 'stream cannot be closed to flush writes');

  assert_equals(await getFileContents(handle), '');
  assert_equals(await getFileSize(handle), 0);
}, 'abort() aborts write');