fix(tracker-fetch,tracker-assist): async sanitization & message sending
This commit is contained in:
parent
f19a7df354
commit
c80b8c34c8
2 changed files with 70 additions and 68 deletions
|
|
@ -61,7 +61,7 @@ export default function(opts: Partial<Options> = {}) {
|
||||||
? name => ihOpt.includes(name)
|
? name => ihOpt.includes(name)
|
||||||
: () => ihOpt
|
: () => ihOpt
|
||||||
|
|
||||||
const sendFetchMessage = (res: AxiosResponse) => {
|
const sendFetchMessage = async (res: AxiosResponse) => {
|
||||||
// ?? TODO: why config is undeined sometimes?
|
// ?? TODO: why config is undeined sometimes?
|
||||||
if (!isAxiosResponse(res)) { return }
|
if (!isAxiosResponse(res)) { return }
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|
|
||||||
|
|
@ -87,82 +87,84 @@ export default function(opts: Partial<Options> = {}): (app: App | null) => Windo
|
||||||
if (options.failuresOnly && response.status < 400) {
|
if (options.failuresOnly && response.status < 400) {
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
const r = response.clone();
|
(async () => {
|
||||||
|
const r = response.clone();
|
||||||
|
|
||||||
r.text().then(text => {
|
r.text().then(text => {
|
||||||
// Headers prepearing
|
// Headers prepearing
|
||||||
const reqHs: Record<string, string> = {}
|
const reqHs: Record<string, string> = {}
|
||||||
const resHs: Record<string, string> = {}
|
const resHs: Record<string, string> = {}
|
||||||
if (ihOpt !== true) {
|
if (ihOpt !== true) {
|
||||||
function writeReqHeader([n, v]) {
|
function writeReqHeader([n, v]) {
|
||||||
if (!isHIgnoring(n)) { reqHs[n] = v }
|
if (!isHIgnoring(n)) { reqHs[n] = v }
|
||||||
}
|
}
|
||||||
if (init.headers instanceof Headers) {
|
if (init.headers instanceof Headers) {
|
||||||
init.headers.forEach((v, n) => writeReqHeader([n, v]))
|
init.headers.forEach((v, n) => writeReqHeader([n, v]))
|
||||||
} else if (Array.isArray(init.headers)) {
|
} else if (Array.isArray(init.headers)) {
|
||||||
init.headers.forEach(writeReqHeader);
|
init.headers.forEach(writeReqHeader);
|
||||||
} else if (typeof init.headers === 'object') {
|
} else if (typeof init.headers === 'object') {
|
||||||
Object.entries(init.headers).forEach(writeReqHeader)
|
Object.entries(init.headers).forEach(writeReqHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.headers.forEach((v, n) => { if (!isHIgnoring(n)) resHs[n] = v })
|
||||||
}
|
}
|
||||||
|
|
||||||
r.headers.forEach((v, n) => { if (!isHIgnoring(n)) resHs[n] = v })
|
const req: RequestData = {
|
||||||
}
|
headers: reqHs,
|
||||||
|
body: init.body,
|
||||||
const req: RequestData = {
|
|
||||||
headers: reqHs,
|
|
||||||
body: init.body,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Response forming
|
|
||||||
const res: ResponseData = {
|
|
||||||
headers: resHs,
|
|
||||||
body: text,
|
|
||||||
}
|
|
||||||
|
|
||||||
const method = typeof init.method === 'string'
|
|
||||||
? init.method.toUpperCase()
|
|
||||||
: 'GET'
|
|
||||||
let reqResInfo: RequestResponseData | null = {
|
|
||||||
url: input,
|
|
||||||
method,
|
|
||||||
status: r.status,
|
|
||||||
request: req,
|
|
||||||
response: res,
|
|
||||||
}
|
|
||||||
if (options.sanitiser) {
|
|
||||||
try {
|
|
||||||
reqResInfo.response.body = JSON.parse(text) as Object // Why the returning type is "any"?
|
|
||||||
} catch {}
|
|
||||||
reqResInfo = options.sanitiser(reqResInfo)
|
|
||||||
if (!reqResInfo) {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const getStj = (r: RequestData | ResponseData): string => {
|
// Response forming
|
||||||
if (r && typeof r.body !== 'string') {
|
const res: ResponseData = {
|
||||||
|
headers: resHs,
|
||||||
|
body: text,
|
||||||
|
}
|
||||||
|
|
||||||
|
const method = typeof init.method === 'string'
|
||||||
|
? init.method.toUpperCase()
|
||||||
|
: 'GET'
|
||||||
|
let reqResInfo: RequestResponseData | null = {
|
||||||
|
url: input,
|
||||||
|
method,
|
||||||
|
status: r.status,
|
||||||
|
request: req,
|
||||||
|
response: res,
|
||||||
|
}
|
||||||
|
if (options.sanitiser) {
|
||||||
try {
|
try {
|
||||||
r.body = JSON.stringify(r.body)
|
reqResInfo.response.body = JSON.parse(text) as Object // Why the returning type is "any"?
|
||||||
} catch {
|
} catch {}
|
||||||
r.body = "<unable to stringify>"
|
reqResInfo = options.sanitiser(reqResInfo)
|
||||||
//app.log.warn("Openreplay fetch") // TODO: version check
|
if (!reqResInfo) {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return JSON.stringify(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.send(
|
const getStj = (r: RequestData | ResponseData): string => {
|
||||||
Messages.Fetch(
|
if (r && typeof r.body !== 'string') {
|
||||||
method,
|
try {
|
||||||
String(reqResInfo.url),
|
r.body = JSON.stringify(r.body)
|
||||||
getStj(reqResInfo.request),
|
} catch {
|
||||||
getStj(reqResInfo.response),
|
r.body = "<unable to stringify>"
|
||||||
r.status,
|
//app.log.warn("Openreplay fetch") // TODO: version check
|
||||||
startTime + performance.timing.navigationStart,
|
}
|
||||||
duration,
|
}
|
||||||
),
|
return JSON.stringify(r)
|
||||||
)
|
}
|
||||||
});
|
|
||||||
|
app.send(
|
||||||
|
Messages.Fetch(
|
||||||
|
method,
|
||||||
|
String(reqResInfo.url),
|
||||||
|
getStj(reqResInfo.request),
|
||||||
|
getStj(reqResInfo.response),
|
||||||
|
r.status,
|
||||||
|
startTime + performance.timing.navigationStart,
|
||||||
|
duration,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})()
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue