-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for reading streaming format via node streams
- Loading branch information
Showing
7 changed files
with
180 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import { PADDING } from '../magic'; | ||
import { flatbuffers } from 'flatbuffers'; | ||
import * as Message_ from '../../fb/Message'; | ||
import ByteBuffer = flatbuffers.ByteBuffer; | ||
import _Message = Message_.org.apache.arrow.flatbuf.Message; | ||
|
||
export async function* fromNodeStream(stream: NodeJS.ReadableStream) { | ||
|
||
let bb: ByteBuffer; | ||
let bytesRead = 0, bytes = new Uint8Array(0); | ||
let messageLength = 0, message: _Message | null = null; | ||
|
||
for await (let chunk of (stream as any as AsyncIterable<Uint8Array | Buffer | string>)) { | ||
|
||
const grown = new Uint8Array(bytes.length + chunk.length); | ||
|
||
if (typeof chunk !== 'string') { | ||
grown.set(bytes, 0) || grown.set(chunk, bytes.length); | ||
} else { | ||
for (let i = -1, j = bytes.length, n = chunk.length; ++i < n;) { | ||
grown[i + j] = chunk.charCodeAt(i); | ||
} | ||
} | ||
|
||
bytes = grown; | ||
|
||
if (messageLength <= 0) { | ||
messageLength = new DataView(bytes.buffer).getInt32(0, true); | ||
} | ||
|
||
while (messageLength < bytes.length) { | ||
if (!message) { | ||
(bb = new ByteBuffer(bytes)).setPosition(4); | ||
if (message = _Message.getRootAsMessage(bb)) { | ||
messageLength += message.bodyLength().low; | ||
continue; | ||
} | ||
throw new Error(`Invalid message at position ${bytesRead}`); | ||
} | ||
bytesRead += messageLength + PADDING; | ||
yield bytes.subarray(0, messageLength); | ||
bytes = bytes.subarray(messageLength + PADDING); | ||
messageLength = bytes.length <= 0 ? 0 : | ||
new DataView(bytes.buffer).getInt32(bytes.byteOffset, true); | ||
message = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
export class PipeIterator<T> implements IterableIterator<T> { | ||
constructor(protected iterator: IterableIterator<T>, protected encoding?: any) {} | ||
[Symbol.iterator]() { return this.iterator; } | ||
next(value?: any) { return this.iterator.next(value); } | ||
throw(error?: any) { | ||
if (typeof this.iterator.throw === 'function') { | ||
return this.iterator.throw(error); | ||
} | ||
return { done: true, value: null as any }; | ||
} | ||
return(value?: any) { | ||
if (typeof this.iterator.return === 'function') { | ||
return this.iterator.return(value); | ||
} | ||
return { done: true, value: null as any }; | ||
} | ||
pipe(stream: NodeJS.WritableStream) { | ||
let { encoding } = this; | ||
let res: IteratorResult<T>; | ||
let write = (err?: any) => { | ||
stream['removeListener']('error', write); | ||
stream['removeListener']('drain', write); | ||
if (err) return this.throw(err); | ||
if (stream['writable']) { | ||
do { | ||
if ((res = this.next()).done) break; | ||
} while (emit(stream, encoding, res.value)); | ||
} | ||
return wait(stream, encoding, res && res.done, write); | ||
}; | ||
write(); | ||
return stream; | ||
} | ||
} | ||
|
||
export class AsyncPipeIterator<T> implements AsyncIterableIterator<T> { | ||
constructor(protected iterator: AsyncIterableIterator<T>, protected encoding?: any) {} | ||
[Symbol.asyncIterator]() { return this.iterator; } | ||
next(value?: any) { return this.iterator.next(value); } | ||
async throw(error?: any) { | ||
if (typeof this.iterator.throw === 'function') { | ||
return this.iterator.throw(error); | ||
} | ||
return { done: true, value: null as any }; | ||
} | ||
async return(value?: any) { | ||
if (typeof this.iterator.return === 'function') { | ||
return this.iterator.return(value); | ||
} | ||
return { done: true, value: null as any }; | ||
} | ||
pipe(stream: NodeJS.WritableStream) { | ||
let { encoding } = this; | ||
let res: IteratorResult<T>; | ||
let write = async (err?: any) => { | ||
stream['removeListener']('error', write); | ||
stream['removeListener']('drain', write); | ||
if (err) return this.throw(err); | ||
if (stream['writable']) { | ||
do { | ||
if ((res = await this.next()).done) break; | ||
} while (emit(stream, encoding, res.value)); | ||
} | ||
return wait(stream, encoding, res && res.done, write); | ||
}; | ||
write(); | ||
return stream; | ||
} | ||
} | ||
|
||
function emit(stream: NodeJS.WritableStream, encoding: string, value: any) { | ||
return stream['write']((encoding === 'utf8' ? value + '\n' : value) as any, encoding); | ||
} | ||
|
||
function wait(stream: NodeJS.WritableStream, encoding: string, done: boolean, write: (x?: any) => void) { | ||
const p = eval('process'); // defeat closure compiler | ||
if (!done) { | ||
stream['once']('error', write); | ||
stream['once']('drain', write); | ||
} else if (!(!p || stream === p.stdout) && !(stream as any)['isTTY']) { | ||
stream['end'](<any> (encoding === 'utf8' ? '\n' : new Uint8Array(0))); | ||
} | ||
} |