| 910 | } |
| 911 | |
| 912 | class MockFetchResponse { |
| 913 | public url?: string; |
| 914 | public headers: Record<string, string> = {}; |
| 915 | |
| 916 | public progress: number[] = []; |
| 917 | |
| 918 | private sub$ = new Subject<any>(); |
| 919 | public stream = new ReadableStream({ |
| 920 | start: (controller) => { |
| 921 | this.sub$.subscribe({ |
| 922 | next: (val) => { |
| 923 | controller.enqueue(val instanceof Uint8Array ? val : new TextEncoder().encode(val)); |
| 924 | }, |
| 925 | complete: () => { |
| 926 | controller.close(); |
| 927 | }, |
| 928 | }); |
| 929 | }, |
| 930 | }); |
| 931 | |
| 932 | public setBody(body: any) { |
| 933 | this.sub$.next(body); |
| 934 | this.sub$.complete(); |
| 935 | } |
| 936 | |
| 937 | public setupBodyStream(body?: string) { |
| 938 | if (body && this.progress.length) { |
| 939 | this.headers['content-length'] = `${body.length}`; |
| 940 | let shift = 0; |
| 941 | this.progress.forEach((loaded) => { |
| 942 | this.sub$.next(body.substring(shift, loaded)); |
| 943 | shift = loaded; |
| 944 | }); |
| 945 | this.sub$.next(body.substring(shift, body.length)); |
| 946 | } else { |
| 947 | this.sub$.next(body); |
| 948 | } |
| 949 | |
| 950 | this.sub$.complete(); |
| 951 | } |
| 952 | } |