| 881 | } |
| 882 | |
| 883 | class MockFetchResponse { |
| 884 | public url?: string; |
| 885 | public headers: Record<string, string> = {}; |
| 886 | |
| 887 | public progress: number[] = []; |
| 888 | |
| 889 | private sub$ = new Subject<any>(); |
| 890 | public stream = new ReadableStream({ |
| 891 | start: (controller) => { |
| 892 | this.sub$.subscribe({ |
| 893 | next: (val) => { |
| 894 | controller.enqueue(val instanceof Uint8Array ? val : new TextEncoder().encode(val)); |
| 895 | }, |
| 896 | complete: () => { |
| 897 | controller.close(); |
| 898 | }, |
| 899 | }); |
| 900 | }, |
| 901 | }); |
| 902 | |
| 903 | public setBody(body: any) { |
| 904 | this.sub$.next(body); |
| 905 | this.sub$.complete(); |
| 906 | } |
| 907 | |
| 908 | public setupBodyStream(body?: string) { |
| 909 | if (body && this.progress.length) { |
| 910 | this.headers['content-length'] = `${body.length}`; |
| 911 | let shift = 0; |
| 912 | this.progress.forEach((loaded) => { |
| 913 | this.sub$.next(body.substring(shift, loaded)); |
| 914 | shift = loaded; |
| 915 | }); |
| 916 | this.sub$.next(body.substring(shift, body.length)); |
| 917 | } else { |
| 918 | this.sub$.next(body); |
| 919 | } |
| 920 | |
| 921 | this.sub$.complete(); |
| 922 | } |
| 923 | } |