-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from EthianWong/feat/disposeable
feat: allow proactively eliminate side effects
- Loading branch information
Showing
5 changed files
with
83 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,73 @@ | ||
import { RequestDetail } from "../common"; | ||
import { headersToObject } from "../utils/map"; | ||
import { MainProcess } from "./fork"; | ||
import { RequestDetail } from '../common' | ||
import { headersToObject } from '../utils/map' | ||
import { MainProcess } from './fork' | ||
|
||
export function proxyFetch(mainProcess: MainProcess) { | ||
if (!globalThis.fetch) { | ||
return; | ||
return | ||
} | ||
const originalFetch = globalThis.fetch; | ||
const originalFetch = globalThis.fetch | ||
|
||
globalThis["fetch"] = fetchProxyFactory(originalFetch, mainProcess); | ||
globalThis['fetch'] = fetchProxyFactory(originalFetch, mainProcess) | ||
|
||
return () => { | ||
globalThis['fetch'] = originalFetch | ||
} | ||
} | ||
|
||
function fetchProxyFactory(fetchFn: typeof fetch, mainProcess: MainProcess) { | ||
return function (request: string | URL | Request, options?: RequestInit) { | ||
const requestDetail = new RequestDetail(); | ||
requestDetail.requestStartTime = new Date().getTime(); | ||
const requestDetail = new RequestDetail() | ||
requestDetail.requestStartTime = new Date().getTime() | ||
|
||
if (typeof request === "string") { | ||
requestDetail.url = request; | ||
if (typeof request === 'string') { | ||
requestDetail.url = request | ||
} else if (request instanceof URL) { | ||
requestDetail.url = request.toString(); | ||
requestDetail.url = request.toString() | ||
} | ||
|
||
requestDetail.method = options?.method ?? "GET"; | ||
requestDetail.requestHeaders = options?.headers ?? {}; | ||
requestDetail.requestData = options?.body; | ||
requestDetail.method = options?.method ?? 'GET' | ||
requestDetail.requestHeaders = options?.headers ?? {} | ||
requestDetail.requestData = options?.body | ||
|
||
mainProcess.registerRequest(requestDetail); | ||
mainProcess.registerRequest(requestDetail) | ||
return fetchFn(request as string | Request, options) | ||
.then(fetchResponseHandlerFactory(requestDetail, mainProcess)) | ||
.catch(fetchErrorHandlerFactory(requestDetail, mainProcess)); | ||
}; | ||
.catch(fetchErrorHandlerFactory(requestDetail, mainProcess)) | ||
} | ||
} | ||
|
||
function fetchResponseHandlerFactory( | ||
requestDetail: RequestDetail, | ||
mainProcess: MainProcess | ||
) { | ||
function fetchResponseHandlerFactory(requestDetail: RequestDetail, mainProcess: MainProcess) { | ||
return (response: Response) => { | ||
requestDetail.requestEndTime = new Date().getTime(); | ||
requestDetail.responseHeaders = headersToObject(response.headers); | ||
requestDetail.responseStatusCode = response.status || 0; | ||
|
||
requestDetail.requestEndTime = new Date().getTime() | ||
requestDetail.responseHeaders = headersToObject(response.headers) | ||
requestDetail.responseStatusCode = response.status || 0 | ||
|
||
response | ||
.clone() | ||
.arrayBuffer() | ||
.then((buffer) => { | ||
const responseData = Buffer.from(buffer); | ||
requestDetail.responseData = responseData; | ||
requestDetail.responseInfo.dataLength = responseData.length; | ||
const responseData = Buffer.from(buffer) | ||
requestDetail.responseData = responseData | ||
requestDetail.responseInfo.dataLength = responseData.length | ||
// TODO: use content-encoding to determine the actual length | ||
requestDetail.responseInfo.encodedDataLength = responseData.length; | ||
requestDetail.responseInfo.encodedDataLength = responseData.length | ||
}) | ||
.finally(() => { | ||
mainProcess.updateRequest(requestDetail); | ||
mainProcess.endRequest(requestDetail); | ||
}); | ||
mainProcess.updateRequest(requestDetail) | ||
mainProcess.endRequest(requestDetail) | ||
}) | ||
|
||
return response; | ||
}; | ||
return response | ||
} | ||
} | ||
|
||
function fetchErrorHandlerFactory( | ||
requestDetail: RequestDetail, | ||
mainProcess: MainProcess | ||
) { | ||
function fetchErrorHandlerFactory(requestDetail: RequestDetail, mainProcess: MainProcess) { | ||
return (err: unknown) => { | ||
requestDetail.requestEndTime = Date.now(); | ||
requestDetail.responseStatusCode = 0; | ||
mainProcess.updateRequest(requestDetail); | ||
mainProcess.endRequest(requestDetail); | ||
throw err; | ||
}; | ||
requestDetail.requestEndTime = Date.now() | ||
requestDetail.responseStatusCode = 0 | ||
mainProcess.updateRequest(requestDetail) | ||
mainProcess.endRequest(requestDetail) | ||
throw err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters