stream#Writable TypeScript Examples
The following examples show how to use
stream#Writable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts From next-rpc with MIT License | 7 votes |
async function appReady(stdout: Readable): Promise<void> {
return new Promise<void>((resolve) => {
stdout.pipe(
new Writable({
write(chunk, encoding, callback) {
if (/ready - started server on/i.test(stripAnsi(String(chunk)))) {
resolve();
}
callback();
},
})
);
});
}
Example #2
Source File: runAndLog.ts From task-scheduler with MIT License | 6 votes |
async function runAndCollectLogs(
runner: (stdout: Writable, stderr: Writable) => Promise<boolean>,
globals: Globals
): Promise<TaskResult> {
const stdout = new streams.WritableStream();
const stderr = new streams.WritableStream();
try {
const success = await runner(stdout, stderr);
return { success, stdout: stdout.toString(), stderr: stderr.toString() };
} catch (error) {
if (error) {
const exceptionMessage = globals.errorFormatter(error);
stderr.write(EOL + exceptionMessage);
}
return {
success: false,
stdout: stdout.toString(),
stderr: stderr.toString(),
};
}
}
Example #3
Source File: TestPipeline.ts From backstage with Apache License 2.0 | 6 votes |
/**
* Execute the test pipeline so that you can make assertions about the result
* or behavior of the given test subject.
*/
async execute(): Promise<TestPipelineResult> {
const documents: IndexableDocument[] = [];
if (!this.collator) {
throw new Error(
'Cannot execute pipeline without a collator or documents',
);
}
// If we are here and there is no indexer, we are testing a collator or a
// decorator. Set up a naive writable that captures documents in memory.
if (!this.indexer) {
this.indexer = new Writable({ objectMode: true });
this.indexer._write = (document: IndexableDocument, _, done) => {
documents.push(document);
done();
};
}
return new Promise<TestPipelineResult>(done => {
const pipes: (Readable | Transform | Writable)[] = [this.collator!];
if (this.decorator) {
pipes.push(this.decorator);
}
pipes.push(this.indexer!);
pipeline(pipes, (error: NodeJS.ErrnoException | null) => {
done({
error,
documents,
});
});
});
}
Example #4
Source File: plantuml.ts From tx2uml with MIT License | 6 votes |
pipePuml = (
pumlStream: Readable,
outputStream: Writable,
plantUmlOptions: string[]
): Promise<number> => {
return new Promise<number>(async (resolve, reject) => {
const child = spawn("java", plantUmlOptions)
pumlStream.pipe(child.stdin)
child.stdout.pipe(outputStream)
let error = ""
for await (const chunk of child.stderr) {
error += chunk
}
child.once("exit", code => {
if (code === 0) {
resolve(code)
} else {
reject(
new VError(
{ info: { code } },
`PlantUML process existed with status code ${code}. ${error}`
)
)
}
})
child.once("error", err => {
reject(
new VError(err, `PlantUML process failed with error: ${error}`)
)
})
})
}
Example #5
Source File: cli-tester.ts From devmoji with Apache License 2.0 | 6 votes |
class StringWritable extends Writable {
data = ""
decoder: StringDecoder
constructor(options?: WritableOptions) {
super(options)
this.decoder = new StringDecoder("utf-8")
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_write(chunk: any, encoding: string, callback: () => void) {
this.data += this.decoder.write(chunk)
callback()
}
_destroy(err: Error, callback: () => void) {
this.data += this.decoder.end()
callback()
}
}
Example #6
Source File: import.ts From Hybooru with MIT License | 6 votes |
async importBatch(lastKey: any[], limit: number, input: Statement, output: Writable): Promise<any[] | null> {
const rows = input.all(...lastKey, limit);
let buf = "";
for(const row of rows) {
buf += row.pop();
}
output.write(buf);
if(rows.length > 0) return rows[rows.length - 1];
else return null;
}
Example #7
Source File: runAndLog.ts From task-scheduler with MIT License | 6 votes |
export async function runAndLog(
task: Task,
graph: TopologicalGraph,
p: string,
globals: Globals
): Promise<void> {
const result = await runAndCollectLogs(
(stdout: Writable, stderr: Writable) =>
task.run(path.join(globals.cwd(), graph[p].location), stdout, stderr, p),
globals
);
await wait();
const message = formatOutput(result);
if (result.success) {
outputResult(message, p, task.name, "success", globals.logger);
} else {
throw message;
}
}
Example #8
Source File: TestPipeline.ts From backstage with Apache License 2.0 | 6 votes |
/**
* Provide the collator, decorator, or indexer to be tested.
*/
static withSubject(subject: Readable | Transform | Writable) {
if (subject instanceof Transform) {
return new TestPipeline({ decorator: subject });
}
if (subject instanceof Writable) {
return new TestPipeline({ indexer: subject });
}
if (subject.readable || subject instanceof Readable) {
return new TestPipeline({ collator: subject });
}
throw new Error(
'Unknown test subject: are you passing a readable, writable, or transform stream?',
);
}
Example #9
Source File: TaskLogWritable.ts From lage with MIT License | 6 votes |
export class TaskLogWritable extends Writable {
private buffer: string = "";
constructor(private taskLogger: TaskLogger) {
super();
}
_write(
chunk: Buffer,
_encoding: string,
callback: (error?: Error | null) => void
) {
let prev = 0;
let curr = 0;
while (curr < chunk.byteLength) {
if (chunk[curr] === 13 || (chunk[curr] === 10 && curr - prev > 1)) {
this.buffer =
this.buffer +
chunk
.slice(prev, curr)
.toString()
.replace(/^(\r\n|\n|\r)|(\r\n|\n|\r)$/g, "")
.trimRight();
this.taskLogger.verbose(this.buffer);
this.buffer = "";
prev = curr;
}
curr++;
}
callback();
}
}
Example #10
Source File: stream.ts From flatend with MIT License | 6 votes |
export async function drain(writable: Writable) {
if (writable.destroyed) throw new Error(`premature close`);
await Promise.race([
events.once(writable, "drain"),
events.once(writable, "close").then(() => {
throw new Error(`premature close`);
}),
]);
}
Example #11
Source File: azure_service_bus_queue_impl.ts From Deep-Lynx with MIT License | 6 votes |
Consume(queueName: string, destination: Writable): void {
const receiver = this.client.createReceiver(queueName);
const handler = (message: ServiceBusReceivedMessage) => {
destination.write(JSON.parse(message.body), () => {
receiver.completeMessage(message).catch((e: any) => {
`unable to mark message complete ${JSON.stringify(e)}`;
});
});
};
const errorHandler = (e: any) => {
Logger.error(`unable to read messages from azure service bus queue ${JSON.stringify(e)}`);
};
receiver.subscribe({
processMessage: handler,
processError: errorHandler,
});
}
Example #12
Source File: multipleRangeDownloader.ts From electron-differential-updater with MIT License | 6 votes |
export function executeTasksUsingMultipleRangeRequests(differentialDownloader: DifferentialDownloader, tasks: Array<Operation>, out: Writable, oldFileFd: number, reject: (error: Error) => void): (taskOffset: number) => void {
const w = (taskOffset: number): void => {
if (taskOffset >= tasks.length) {
if (differentialDownloader.fileMetadataBuffer != null) {
out.write(differentialDownloader.fileMetadataBuffer)
}
out.end()
return
}
const nextOffset = taskOffset + 1000
doExecuteTasks(differentialDownloader, {
tasks,
start: taskOffset,
end: Math.min(tasks.length, nextOffset),
oldFileFd,
}, out, () => w(nextOffset), reject)
}
return w
}
Example #13
Source File: destination.ts From pino-lambda with MIT License | 6 votes |
pinoLambdaDestination = (options: PinoLambdaOptions = {}): Writable => {
const writeable = new Writable({
defaultEncoding: 'utf8',
write(chunk, encoding, callback) {
const storageProvider = options.storageProvider || GlobalContextStorageProvider;
const formatter = options?.formatter || new CloudwatchLogFormatter();
const data = JSON.parse(chunk);
const lambdaContext = storageProvider.getContext() || {};
// format the combined request context and log data
let output = formatter.format({ ...lambdaContext, ...data });
// replace characters for proper formatting
output = output.replace(/\n/, '\r');
// final entry must end with carriage return
output += '\n';
if (options.streamWriter) {
options.streamWriter(output);
} else {
process.stdout.write(output);
}
callback();
},
});
return writeable;
}
Example #14
Source File: mavlink.ts From node-mavlink with GNU Lesser General Public License v3.0 | 6 votes |
/**
* Send a signed packet to the stream. Signed packets are always V2 protocol
*
* @param stream Stream to send the data to
* @param msg message to serialize and send
* @param key key to sign the message with
* @param linkId link id for the signature
* @param sysid system id
* @param compid component id
* @param timestamp optional timestamp for packet signing (default: Date.now())
* @returns number of bytes sent
*/
export async function sendSigned(
stream: Writable,
msg: MavLinkData,
key: Buffer,
linkId: uint8_t = 1, sysid: uint8_t = MavLinkProtocol.SYS_ID, compid: uint8_t = MavLinkProtocol.COMP_ID,
timestamp = Date.now()
) {
return new Promise((resolve, reject) => {
const protocol = new MavLinkProtocolV2(sysid, compid, MavLinkProtocolV2.IFLAG_SIGNED)
const b1 = protocol.serialize(msg, seq++)
seq &= 255
const b2 = protocol.sign(b1, linkId, key, timestamp)
stream.write(b2, err => {
if (err) reject(err)
else resolve(b2.length)
})
})
}
Example #15
Source File: ipcInterface.ts From AIPerf with MIT License | 6 votes |
/**
* Construct a IPC proxy
* @param proc the process to wrap
* @param acceptCommandTypes set of accepted commands for this process
*/
constructor(proc: ChildProcess, acceptCommandTypes: Set<string>) {
this.acceptCommandTypes = acceptCommandTypes;
this.outgoingStream = <Writable>proc.stdio[ipcOutgoingFd];
this.incomingStream = <Readable>proc.stdio[ipcIncomingFd];
this.eventEmitter = new EventEmitter();
this.readBuffer = Buffer.alloc(0);
this.incomingStream.on('data', (data: Buffer) => { this.receive(data); });
this.incomingStream.on('error', (error: Error) => { this.eventEmitter.emit('error', error); });
this.outgoingStream.on('error', (error: Error) => { this.eventEmitter.emit('error', error); });
}
Example #16
Source File: mavlink.ts From node-mavlink with GNU Lesser General Public License v3.0 | 6 votes |
/**
* Send a packet to the stream
*
* @param stream Stream to send the data to
* @param msg message to serialize and send
* @param protocol protocol to use (default: MavLinkProtocolV1)
* @returns number of bytes sent
*/
export async function send(stream: Writable, msg: MavLinkData, protocol: MavLinkProtocol = new MavLinkProtocolV1()) {
return new Promise((resolve, reject) => {
const buffer = protocol.serialize(msg, seq++)
seq &= 255
stream.write(buffer, err => {
if (err) reject(err)
else resolve(buffer.length)
})
})
}
Example #17
Source File: log.ts From leo with MIT License | 6 votes |
rewriteDebugTypes = <T extends { [key: string]: any }>(defaultTypes: T): T => {
const writable = new Writable({
write(chunk, encoding, callback): void {
if (process.env.__ISDEBUG === 'true') {
process.stdout.write(chunk);
}
process.nextTick(callback);
},
});
return {
...defaultTypes,
debug: Object.assign({}, defaultTypes.debug, {
stream: writable,
}),
};
}
Example #18
Source File: index.ts From aws-cost-saver with MIT License | 6 votes |
export function createMockTask() {
return {
output: '',
title: '',
newListr(tasks: ListrTask[], options: any) {
return new Listr(tasks, { renderer: 'silent', ...options });
},
prompt: jest.fn(),
report: jest.fn(),
run: jest.fn(),
skip: jest.fn(),
stdout(): NodeJS.WritableStream {
const stream = new Writable();
stream._write = function(chunk, encoding, callback) {
/* do nothing */
};
return stream;
},
};
}
Example #19
Source File: index.ts From electron-request with MIT License | 5 votes |
/**
* Download file to destination
* @param {WriteStream} fileOut Download write stream
* @param {ProgressCallback=} onProgress Download progress callback
*/
public download = async (
fileOut: Writable,
onProgress?: ProgressCallback,
validateOptions?: ValidateOptions,
): Promise<void> => {
const feedStreams: Writable[] = [];
if (typeof onProgress === 'function') {
const contentLength = Number(this.config.headers.get(HEADER_MAP.CONTENT_LENGTH));
feedStreams.push(new ProgressCallbackTransform(contentLength, onProgress));
}
if (validateOptions) {
feedStreams.push(new DigestTransform(validateOptions));
}
feedStreams.push(fileOut);
return new Promise((resolve, reject) => {
let lastStream = this.stream;
for (const stream of feedStreams) {
stream.on('error', (error: Error) => {
reject(error);
});
lastStream = lastStream.pipe(stream);
}
fileOut.once('finish', () => {
if (fileOut instanceof WriteStream && typeof fileOut.close === 'function') {
fileOut.close();
}
resolve();
});
});
};
Example #20
Source File: pdf.ts From SwissQRBill with MIT License | 5 votes |
constructor(data: Data, outputPathOrWritableStream: string | Writable, optionsOrCallback?: PDFOptions | Function, callbackOrUndefined?: Function | undefined) {
let callback: Function | undefined = undefined;
let options: PDFOptions | undefined = undefined;
if(typeof optionsOrCallback === "object"){
options = optionsOrCallback;
if(typeof callbackOrUndefined === "function"){
callback = callbackOrUndefined;
}
} else if(typeof optionsOrCallback === "function"){
callback = optionsOrCallback;
}
super(data, options);
let stream: Writable | undefined;
if(typeof outputPathOrWritableStream === "string"){
stream = createWriteStream(outputPathOrWritableStream);
} else {
stream = outputPathOrWritableStream;
}
super.pipe(stream);
stream.on("finish", ev => {
if(typeof callback === "function"){
callback(this);
}
this.emit("finish", ev);
});
}
Example #21
Source File: index.test.ts From task-scheduler with MIT License | 5 votes |
function makeTestEnvironment(): {
logs: string[];
makeTask: (desiredResult?: TaskResultOverride) => TaskMock;
} {
const logs: string[] = [];
return {
logs,
makeTask(desiredResult?: TaskResultOverride): TaskMock {
const name = Math.random().toString(36);
const defaultResult: TaskResult = {
success: true,
stdout: "",
stderr: "",
};
const result = desiredResult
? { ...defaultResult, ...desiredResult }
: defaultResult;
const messages = {
started(cwd: string): string {
return `called ${name} for ${cwd.replace(/\\/g, "/")}`;
},
finished(cwd: string): string {
return `finished ${name} for ${cwd.replace(/\\/g, "/")}`;
},
};
const run = async (
cwd: string,
stdout: Writable,
stderr: Writable
): Promise<boolean> => {
logs.push(messages.started(cwd));
stdout.write(result.stdout);
stderr.write(result.stderr);
await wait();
if (typeof result.success === "object") {
logs.push(messages.finished(cwd));
throw result.success;
} else {
logs.push(messages.finished(cwd));
return result.success;
}
};
return { run, name, ...messages };
},
};
}
Example #22
Source File: logger.ts From pancake-bot with MIT License | 5 votes |
constructor(writable: Writable, errWritable: Writable) {
this._writable = new PromiseWritable(writable);
this._errWritable = new PromiseWritable(errWritable);
}
Example #23
Source File: BufferedConsole.ts From web with MIT License | 5 votes |
private outStream = new Writable({
write: (chunk, encoding, callback) => {
callback();
this.buffer.push(chunk);
},
});
Example #24
Source File: logger.ts From pancake-bot with MIT License | 5 votes |
private _errWritable: PromiseWritable<Writable>;
Example #25
Source File: stripeDaemon.ts From vscode-stripe with MIT License | 5 votes |
/**
* Start the Stripe daemon process and parse its stdout to get the gRPC server config. Throws
* MalformedConfigError, NoDaemonCommandError, or SyntaxError if the server can't be started.
*/
private startDaemon = async (port?: number): Promise<DaemonConfig> => {
const flags = port ? ['--port', port.toString()] : [];
const cliPath = await this.stripeClient.getCLIPath();
if (!cliPath) {
throw new Error('Failed to get CLI path');
}
const daemonProcess = execa(cliPath, ['daemon', ...flags]);
this.daemonProcess = daemonProcess;
daemonProcess.on('exit', () => {
this.config = undefined;
this.daemonProcess = undefined;
});
return new Promise<DaemonConfig>((resolve, reject) => {
const write = (
chunk: string,
encoding: string,
callback: (error?: Error | null | undefined) => void,
) => {
if (chunk.startsWith('Unknown command "daemon"')) {
return reject(new NoDaemonCommandError());
}
try {
const object = JSON.parse(chunk);
if (isDaemonConfig(object)) {
resolve(object);
} else {
reject(new MalformedConfigError(object));
}
} catch (e) {
reject(e);
} finally {
if (daemonProcess.stdout) {
// We can stop listening after reading the config.
daemonProcess.stdout.removeAllListeners();
}
callback();
}
};
const stdoutStream = new Writable({write, decodeStrings: false});
if (daemonProcess.stdout) {
daemonProcess.stdout.setEncoding('utf8').pipe(new LineStream()).pipe(stdoutStream);
}
});
};
Example #26
Source File: multipleRangeDownloader.ts From electron-differential-updater with MIT License | 5 votes |
function doExecuteTasks(differentialDownloader: DifferentialDownloader, options: PartListDataTask, out: Writable, resolve: () => void, reject: (error: Error) => void): void {
let ranges = "bytes="
let partCount = 0
const partIndexToTaskIndex = new Map<number, number>()
const partIndexToLength: Array<number> = []
for (let i = options.start; i < options.end; i++) {
const task = options.tasks[i]
if (task.kind === OperationKind.DOWNLOAD) {
ranges += `${task.start}-${task.end - 1}, `
partIndexToTaskIndex.set(partCount, i)
partCount++
partIndexToLength.push(task.end - task.start)
}
}
if (partCount <= 1) {
// the only remote range - copy
const w = (index: number): void => {
if (index >= options.end) {
resolve()
return
}
const task = options.tasks[index++]
if (task.kind === OperationKind.COPY) {
copyData(task, out, options.oldFileFd, reject, () => w(index))
}
else {
const requestOptions = differentialDownloader.createRequestOptions()
requestOptions.headers!!.Range = `bytes=${task.start}-${task.end - 1}`
const request = differentialDownloader.httpExecutor.createRequest(requestOptions, response => {
if (!checkIsRangesSupported(response, reject)) {
return
}
response.pipe(out, {
end: false
})
response.once("end", () => w(index))
})
differentialDownloader.httpExecutor.addErrorAndTimeoutHandlers(request, reject)
request.end()
}
}
w(options.start)
return
}
const requestOptions = differentialDownloader.createRequestOptions()
requestOptions.headers!!.Range = ranges.substring(0, ranges.length - 2)
const request = differentialDownloader.httpExecutor.createRequest(requestOptions, response => {
if (!checkIsRangesSupported(response, reject)) {
return
}
const contentType = safeGetHeader(response, "content-type")
const m = /^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i.exec(contentType)
if (m == null) {
reject(new Error(`Content-Type "multipart/byteranges" is expected, but got "${contentType}"`))
return
}
const dicer = new DataSplitter(out, options, partIndexToTaskIndex, m[1] || m[2], partIndexToLength, resolve)
dicer.on("error", reject)
response.pipe(dicer)
})
differentialDownloader.httpExecutor.addErrorAndTimeoutHandlers(request, reject)
request.end()
}
Example #27
Source File: writers.ts From language-tools with MIT License | 5 votes |
constructor(
private stream: Writable,
private isVerbose = true,
private isWatchMode = false,
private diagnosticFilter: DiagnosticFilter = DEFAULT_FILTER
) {}
Example #28
Source File: log.ts From AIPerf with MIT License | 5 votes |
constructor(writable: Writable) {
this.buffer = Buffer.alloc(0);
this.emitting = false;
this.writable = writable;
}
Example #29
Source File: pdf.ts From SwissQRBill with MIT License | 5 votes |
constructor(data: Data, writableStream: Writable, options?: PDFOptions);