| 1078 | } |
| 1079 | |
| 1080 | class MockFetchResponse { |
| 1081 | public url?: string; |
| 1082 | public headers: Record<string, string> = {}; |
| 1083 | |
| 1084 | public progress: number[] = []; |
| 1085 | |
| 1086 | private sub$ = new Subject<any>(); |
| 1087 | public stream = new ReadableStream({ |
| 1088 | start: (controller) => { |
| 1089 | this.sub$.subscribe({ |
| 1090 | next: (val) => { |
| 1091 | controller.enqueue(val instanceof Uint8Array ? val : new TextEncoder().encode(val)); |
| 1092 | }, |
| 1093 | complete: () => { |
| 1094 | controller.close(); |
| 1095 | }, |
| 1096 | }); |
| 1097 | }, |
| 1098 | }); |
| 1099 | |
| 1100 | public setBody(body: any) { |
| 1101 | this.sub$.next(body); |
| 1102 | this.sub$.complete(); |
| 1103 | } |
| 1104 | |
| 1105 | public setupBodyStream(body?: string) { |
| 1106 | if (body && this.progress.length) { |
| 1107 | this.headers['content-length'] = `${body.length}`; |
| 1108 | let shift = 0; |
| 1109 | this.progress.forEach((loaded) => { |
| 1110 | this.sub$.next(body.substring(shift, loaded)); |
| 1111 | shift = loaded; |
| 1112 | }); |
| 1113 | this.sub$.next(body.substring(shift, body.length)); |
| 1114 | } else { |
| 1115 | this.sub$.next(body); |
| 1116 | } |
| 1117 | |
| 1118 | this.sub$.complete(); |
| 1119 | } |
| 1120 | } |