main
js 93 lines 2.68 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 export interface Destination {
11 push(chunk: string | null): boolean;
12 destroy(error: Error): mixed;
13 }
14
15 export opaque type PrecomputedChunk = string;
16 export opaque type Chunk = string;
17 export opaque type BinaryChunk = string;
18
19 export function scheduleWork(callback: () => void) {
20 callback();
21 }
22
23 export function scheduleMicrotask(callback: () => void) {
24 // While this defies the method name the legacy builds have special
25 // overrides that make work scheduling sync. At the moment scheduleMicrotask
26 // isn't used by any legacy APIs so this is somewhat academic but if they
27 // did in the future we'd probably want to have this be in sync with scheduleWork
28 callback();
29 }
30
31 export function flushBuffered(destination: Destination) {}
32
33 export function beginWriting(destination: Destination) {}
34
35 export function writeChunk(
36 destination: Destination,
37 chunk: Chunk | PrecomputedChunk | BinaryChunk,
38 ): void {
39 writeChunkAndReturn(destination, chunk);
40 }
41
42 export function writeChunkAndReturn(
43 destination: Destination,
44 chunk: Chunk | PrecomputedChunk | BinaryChunk,
45 ): boolean {
46 return destination.push(chunk);
47 }
48
49 export function completeWriting(destination: Destination) {}
50
51 export function close(destination: Destination) {
52 destination.push(null);
53 }
54
55 export function stringToChunk(content: string): Chunk {
56 return content;
57 }
58
59 export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
60 return content;
61 }
62
63 export function typedArrayToBinaryChunk(
64 content: $ArrayBufferView,
65 ): BinaryChunk {
66 throw new Error('Not implemented.');
67 }
68
69 export const byteLengthOfChunk:
70 | null
71 | ((chunk: Chunk | PrecomputedChunk) => number) = null;
72
73 export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
74 throw new Error('Not implemented.');
75 }
76
77 export function closeWithError(destination: Destination, error: mixed): void {
78 // $FlowFixMe[incompatible-type]: This is an Error object or the destination accepts other types.
79 destination.destroy(error);
80 }
81
82 export {createFastHashJS as createFastHash} from 'react-server/src/createFastHashJS';
83
84 export function readAsDataURL(blob: Blob): Promise<string> {
85 return blob.arrayBuffer().then(arrayBuffer => {
86 const encoded =
87 typeof Buffer === 'function' && typeof Buffer.from === 'function'
88 ? Buffer.from(arrayBuffer).toString('base64')
89 : btoa(String.fromCharCode.apply(String, new Uint8Array(arrayBuffer)));
90 const mimeType = blob.type || 'application/octet-stream';
91 return 'data:' + mimeType + ';base64,' + encoded;
92 });
93 }