2018-12-01 03:38:22 +08:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type {Writable} from 'stream';
|
|
|
|
|
|
2019-11-07 01:10:26 +08:00
|
|
|
type MightBeFlushable = {
|
|
|
|
|
flush?: () => void,
|
2020-01-09 22:50:44 +08:00
|
|
|
...
|
2019-11-07 01:10:26 +08:00
|
|
|
};
|
2018-12-01 03:38:22 +08:00
|
|
|
|
|
|
|
|
export type Destination = Writable & MightBeFlushable;
|
|
|
|
|
|
2021-03-16 01:36:23 +08:00
|
|
|
export type PrecomputedChunk = Uint8Array;
|
|
|
|
|
export type Chunk = string;
|
|
|
|
|
|
2018-12-01 03:38:22 +08:00
|
|
|
export function scheduleWork(callback: () => void) {
|
|
|
|
|
setImmediate(callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function flushBuffered(destination: Destination) {
|
|
|
|
|
// If we don't have any more data to send right now.
|
|
|
|
|
// Flush whatever is in the buffer to the wire.
|
|
|
|
|
if (typeof destination.flush === 'function') {
|
2021-06-05 03:17:57 +08:00
|
|
|
// By convention the Zlib streams provide a flush function for this purpose.
|
|
|
|
|
// For Express, compression middleware adds this method.
|
|
|
|
|
destination.flush();
|
2018-12-01 03:38:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function beginWriting(destination: Destination) {
|
2019-11-07 01:10:26 +08:00
|
|
|
// Older Node streams like http.createServer don't have this.
|
|
|
|
|
if (typeof destination.cork === 'function') {
|
|
|
|
|
destination.cork();
|
|
|
|
|
}
|
2018-12-01 03:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-07 01:48:34 +08:00
|
|
|
export function writeChunk(
|
|
|
|
|
destination: Destination,
|
2021-03-16 01:36:23 +08:00
|
|
|
chunk: Chunk | PrecomputedChunk,
|
2019-11-07 01:48:34 +08:00
|
|
|
): boolean {
|
2021-03-16 01:36:23 +08:00
|
|
|
const nodeBuffer = ((chunk: any): Buffer | string); // close enough
|
2019-11-07 01:48:34 +08:00
|
|
|
return destination.write(nodeBuffer);
|
2018-12-01 03:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function completeWriting(destination: Destination) {
|
2019-11-07 01:10:26 +08:00
|
|
|
// Older Node streams like http.createServer don't have this.
|
|
|
|
|
if (typeof destination.uncork === 'function') {
|
|
|
|
|
destination.uncork();
|
|
|
|
|
}
|
2018-12-01 03:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function close(destination: Destination) {
|
|
|
|
|
destination.end();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 01:36:23 +08:00
|
|
|
export function stringToChunk(content: string): Chunk {
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
2018-12-01 03:38:22 +08:00
|
|
|
return Buffer.from(content, 'utf8');
|
|
|
|
|
}
|
2021-03-13 07:21:02 +08:00
|
|
|
|
|
|
|
|
export function closeWithError(destination: Destination, error: mixed): void {
|
|
|
|
|
// $FlowFixMe: This is an Error object or the destination accepts other types.
|
|
|
|
|
destination.destroy(error);
|
|
|
|
|
}
|