| 819 | } |
| 820 | |
| 821 | class MockFetchResponse { |
| 822 | public url?: string; |
| 823 | public headers: Record<string, string> = {}; |
| 824 | |
| 825 | public progress: number[] = []; |
| 826 | |
| 827 | private sub$ = new Subject<any>(); |
| 828 | public stream = new ReadableStream({ |
| 829 | start: (controller) => { |
| 830 | this.sub$.subscribe({ |
| 831 | next: (val) => { |
| 832 | controller.enqueue(new TextEncoder().encode(val)); |
| 833 | }, |
| 834 | complete: () => { |
| 835 | controller.close(); |
| 836 | }, |
| 837 | }); |
| 838 | }, |
| 839 | }); |
| 840 | |
| 841 | public setBody(body: any) { |
| 842 | this.sub$.next(body); |
| 843 | this.sub$.complete(); |
| 844 | } |
| 845 | |
| 846 | public setupBodyStream(body?: string) { |
| 847 | if (body && this.progress.length) { |
| 848 | this.headers['content-length'] = `${body.length}`; |
| 849 | let shift = 0; |
| 850 | this.progress.forEach((loaded) => { |
| 851 | this.sub$.next(body.substring(shift, loaded)); |
| 852 | shift = loaded; |
| 853 | }); |
| 854 | this.sub$.next(body.substring(shift, body.length)); |
| 855 | } else { |
| 856 | this.sub$.next(body); |
| 857 | } |
| 858 | |
| 859 | this.sub$.complete(); |
| 860 | } |
| 861 | } |