Files
dbgate/packages/tools/src/createAsyncWriteStream.ts
2023-02-25 20:25:27 +01:00

31 lines
703 B
TypeScript

import _intersection from 'lodash/intersection';
import _isArray from 'lodash/isArray';
import { getLogger } from './getLogger';
const logger = getLogger('asyncWriteStream');
export interface AsyncWriteStreamOptions {
processItem: (chunk: any) => Promise<void>;
}
export function createAsyncWriteStream(stream, options: AsyncWriteStreamOptions): any {
const writable = new stream.Writable({
objectMode: true,
});
writable._write = async (chunk, encoding, callback) => {
try {
await options.processItem(chunk);
callback(null);
} catch (err) {
callback(err);
}
};
// writable._final = async callback => {
// callback();
// };
return writable;
}