-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathReactServerStreamConfigNode.js
More file actions
73 lines (60 loc) · 1.96 KB
/
ReactServerStreamConfigNode.js
File metadata and controls
73 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* 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';
type MightBeFlushable = {
flush?: () => void,
...
};
export type Destination = Writable & MightBeFlushable;
export type PrecomputedChunk = Uint8Array;
export type Chunk = string;
export function scheduleWork(destination: Destination, 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') {
// By convention the Zlib streams provide a flush function for this purpose.
// For Express, compression middleware adds this method.
destination.flush();
}
}
export function beginWriting(destination: Destination) {
// Older Node streams like http.createServer don't have this.
if (typeof destination.cork === 'function') {
destination.cork();
}
}
export function writeChunk(
destination: Destination,
chunk: Chunk | PrecomputedChunk,
): boolean {
const nodeBuffer = ((chunk: any): Buffer | string); // close enough
return destination.write(nodeBuffer);
}
export function completeWriting(destination: Destination) {
// Older Node streams like http.createServer don't have this.
if (typeof destination.uncork === 'function') {
destination.uncork();
}
}
export function close(destination: Destination) {
destination.end();
}
export function stringToChunk(content: string): Chunk {
return content;
}
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
return Buffer.from(content, 'utf8');
}
export function closeWithError(destination: Destination, error: mixed): void {
// $FlowFixMe: This is an Error object or the destination accepts other types.
destination.destroy(error);
}