index.d.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. declare module '@discoveryjs/json-ext' {
  2. export type Chunk = string | Uint8Array | Buffer;
  3. export type Reviver = (this: any, key: string, value: any) => any;
  4. export type ParseChunkedState = {
  5. readonly mode: 'json' | 'jsonl';
  6. readonly returnValue: any;
  7. readonly currentRootValue: any;
  8. readonly rootValuesCount: number;
  9. readonly consumed: number;
  10. readonly parsed: number;
  11. };
  12. export type OnRootValue = (value: any, state: ParseChunkedState) => void;
  13. export type OnChunk = (chunkParsed: number, chunk: string | null, pending: string | null, state: ParseChunkedState) => void;
  14. export type ParseOptions = {
  15. reviver?: Reviver;
  16. mode?: 'json' | 'jsonl' | 'auto';
  17. onRootValue?: OnRootValue;
  18. onChunk?: OnChunk;
  19. };
  20. export type Replacer =
  21. | ((this: any, key: string, value: any) => any)
  22. | (string | number)[]
  23. | null;
  24. export type Space = string | number | null;
  25. export type StringifyOptions = {
  26. replacer?: Replacer;
  27. space?: Space;
  28. mode?: 'json' | 'jsonl';
  29. highWaterMark?: number;
  30. };
  31. export type StringifyInfoOptions = {
  32. replacer?: Replacer;
  33. space?: Space;
  34. mode?: 'json' | 'jsonl';
  35. continueOnCircular?: boolean;
  36. };
  37. export type StringifyInfoResult = {
  38. bytes: number;
  39. spaceBytes: number;
  40. circular: object[];
  41. };
  42. export function parseChunked(input: Iterable<Chunk> | AsyncIterable<Chunk>, reviver?: Reviver): Promise<any>;
  43. export function parseChunked(input: Iterable<Chunk> | AsyncIterable<Chunk>, options: ParseOptions & { onRootValue: OnRootValue }): Promise<number>;
  44. export function parseChunked(input: Iterable<Chunk> | AsyncIterable<Chunk>, options?: ParseOptions): Promise<any>;
  45. export function parseChunked(input: () => (Iterable<Chunk> | AsyncIterable<Chunk>), reviver?: Reviver): Promise<any>;
  46. export function parseChunked(input: () => (Iterable<Chunk> | AsyncIterable<Chunk>), options: ParseOptions & { onRootValue: OnRootValue }): Promise<number>;
  47. export function parseChunked(input: () => (Iterable<Chunk> | AsyncIterable<Chunk>), options?: ParseOptions): Promise<any>;
  48. export function stringifyChunked(value: any, replacer?: Replacer, space?: Space): Generator<string>;
  49. export function stringifyChunked(value: any, options: StringifyOptions): Generator<string>;
  50. export function stringifyInfo(value: any, replacer?: Replacer, space?: Space): StringifyInfoResult;
  51. export function stringifyInfo(value: any, options?: StringifyInfoOptions): StringifyInfoResult;
  52. // Web streams
  53. export function parseFromWebStream(stream: ReadableStream<Chunk>): Promise<any>;
  54. export function createStringifyWebStream(value: any, replacer?: Replacer, space?: Space): ReadableStream<string>;
  55. export function createStringifyWebStream(value: any, options: StringifyOptions): ReadableStream<string>;
  56. }