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. /* global Bun */
    
  11. 
    
  12. type BunReadableStreamController = ReadableStreamController & {
    
  13.   end(): mixed,
    
  14.   write(data: Chunk | BinaryChunk): void,
    
  15.   error(error: Error): void,
    
  16. };
    
  17. export type Destination = BunReadableStreamController;
    
  18. 
    
  19. export type PrecomputedChunk = string;
    
  20. export opaque type Chunk = string;
    
  21. export type BinaryChunk = $ArrayBufferView;
    
  22. 
    
  23. export function scheduleWork(callback: () => void) {
    
  24.   callback();
    
  25. }
    
  26. 
    
  27. export function flushBuffered(destination: Destination) {
    
  28.   // WHATWG Streams do not yet have a way to flush the underlying
    
  29.   // transform streams. https://github.com/whatwg/streams/issues/960
    
  30. }
    
  31. 
    
  32. export function beginWriting(destination: Destination) {}
    
  33. 
    
  34. export function writeChunk(
    
  35.   destination: Destination,
    
  36.   chunk: PrecomputedChunk | Chunk | BinaryChunk,
    
  37. ): void {
    
  38.   if (chunk.length === 0) {
    
  39.     return;
    
  40.   }
    
  41. 
    
  42.   destination.write(chunk);
    
  43. }
    
  44. 
    
  45. export function writeChunkAndReturn(
    
  46.   destination: Destination,
    
  47.   chunk: PrecomputedChunk | Chunk | BinaryChunk,
    
  48. ): boolean {
    
  49.   return !!destination.write(chunk);
    
  50. }
    
  51. 
    
  52. export function completeWriting(destination: Destination) {}
    
  53. 
    
  54. export function close(destination: Destination) {
    
  55.   destination.end();
    
  56. }
    
  57. 
    
  58. export function stringToChunk(content: string): Chunk {
    
  59.   return content;
    
  60. }
    
  61. 
    
  62. export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
    
  63.   return content;
    
  64. }
    
  65. 
    
  66. export function typedArrayToBinaryChunk(
    
  67.   content: $ArrayBufferView,
    
  68. ): BinaryChunk {
    
  69.   // TODO: Does this needs to be cloned if it's transferred in enqueue()?
    
  70.   return content;
    
  71. }
    
  72. 
    
  73. export function clonePrecomputedChunk(
    
  74.   chunk: PrecomputedChunk,
    
  75. ): PrecomputedChunk {
    
  76.   return chunk;
    
  77. }
    
  78. 
    
  79. export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
    
  80.   return Buffer.byteLength(chunk, 'utf8');
    
  81. }
    
  82. 
    
  83. export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
    
  84.   return chunk.byteLength;
    
  85. }
    
  86. 
    
  87. export function closeWithError(destination: Destination, error: mixed): void {
    
  88.   if (typeof destination.error === 'function') {
    
  89.     // $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
    
  90.     destination.error(error);
    
  91.   } else {
    
  92.     // Earlier implementations doesn't support this method. In that environment you're
    
  93.     // supposed to throw from a promise returned but we don't return a promise in our
    
  94.     // approach. We could fork this implementation but this is environment is an edge
    
  95.     // case to begin with. It's even less common to run this in an older environment.
    
  96.     // Even then, this is not where errors are supposed to happen and they get reported
    
  97.     // to a global callback in addition to this anyway. So it's fine just to close this.
    
  98.     destination.close();
    
  99.   }
    
  100. }
    
  101. 
    
  102. export function createFastHash(input: string): string | number {
    
  103.   return Bun.hash(input);
    
  104. }