]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/js/src/io/whatwg/reader.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / io / whatwg / reader.ts
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18import { DataType } from '../../type';
19import { RecordBatch } from '../../recordbatch';
20import { AsyncByteQueue } from '../../io/stream';
21import { RecordBatchReader } from '../../ipc/reader';
22
23/** @ignore */
24export function recordBatchReaderThroughDOMStream<T extends { [key: string]: DataType } = any>(writableStrategy?: ByteLengthQueuingStrategy, readableStrategy?: { autoDestroy: boolean }) {
25
26 const queue = new AsyncByteQueue();
27 let reader: RecordBatchReader<T> | null = null;
28
29 const readable = new ReadableStream<RecordBatch<T>>({
30 async cancel() { await queue.close(); },
31 async start(controller) { await next(controller, reader || (reader = await open())); },
32 async pull(controller) { reader ? await next(controller, reader) : controller.close(); }
33 });
34
35 return { writable: new WritableStream(queue, { 'highWaterMark': 2 ** 14, ...writableStrategy }), readable };
36
37 async function open() {
38 return await (await RecordBatchReader.from<T>(queue)).open(readableStrategy);
39 }
40
41 async function next(controller: ReadableStreamDefaultController<RecordBatch<T>>, reader: RecordBatchReader<T>) {
42 let size = controller.desiredSize;
43 let r: IteratorResult<RecordBatch<T>> | null = null;
44 while (!(r = await reader.next()).done) {
45 controller.enqueue(r.value);
46 if (size != null && --size <= 0) {
47 return;
48 }
49 }
50 controller.close();
51 }
52}