|
@ -184,42 +184,59 @@ async function serialize(src: any): Promise<any> { |
|
|
return ret; |
|
|
return ret; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function cleanupSerialized(src: any) { |
|
|
async function braveserialize(root: any): Promise<any> { |
|
|
if (typeof src != "object") |
|
|
const transfer: Transferable[] = []; |
|
|
return src; |
|
|
const ser = async (src: any): Promise<any> => { |
|
|
switch (src.cls) { |
|
|
if (src instanceof FormData) { |
|
|
case 'FormData': { |
|
|
const value = []; |
|
|
for (const [key, items] of src.value) { |
|
|
for (const kv of src) |
|
|
for (const item of items) { |
|
|
value.push([kv[0], await Promise.all(src.getAll(kv[0]).map(ser))]); |
|
|
cleanupSerialized(item); |
|
|
return { |
|
|
} |
|
|
cls: 'FormData', value, |
|
|
} |
|
|
}; |
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
case 'File': { |
|
|
if (src instanceof File) { |
|
|
URL.revokeObjectURL(src.value); |
|
|
const { name, type, lastModified } = src; |
|
|
break; |
|
|
const value = await src.arrayBuffer(); |
|
|
|
|
|
transfer.push(value); |
|
|
|
|
|
return { |
|
|
|
|
|
cls: 'File', |
|
|
|
|
|
name, type, lastModified, value, |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
case 'Blob': { |
|
|
if (src instanceof Blob) { |
|
|
URL.revokeObjectURL(src.value); |
|
|
const { type } = src; |
|
|
break; |
|
|
const value = await src.arrayBuffer(); |
|
|
|
|
|
transfer.push(value); |
|
|
|
|
|
return { |
|
|
|
|
|
cls: 'Blob', type, value, |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
case 'Object': { |
|
|
if (src === null || src === undefined || typeof src != "object") |
|
|
for (const prop in src.value) { |
|
|
return src; |
|
|
cleanupSerialized(src.value[prop]); |
|
|
const ret = { |
|
|
} |
|
|
cls: 'Object', |
|
|
|
|
|
value: {} |
|
|
|
|
|
} as any; |
|
|
|
|
|
for (const prop in src) { |
|
|
|
|
|
ret.value[prop] = await ser(src[prop]); |
|
|
} |
|
|
} |
|
|
} |
|
|
return ret; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return [await ser(root), transfer]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export const corsFetch = async (input: string, init?: RequestInit, lsn?: EventTarget) => { |
|
|
export const corsFetch = async (input: string, init?: RequestInit, lsn?: EventTarget) => { |
|
|
const id = gid++; |
|
|
const id = gid++; |
|
|
|
|
|
|
|
|
|
|
|
let transfer: Transferable[] = []; |
|
|
if (init?.body) { |
|
|
if (init?.body) { |
|
|
// Chrom* can't pass around FormData and File/Blobs between
|
|
|
// Chrom* can't pass around FormData and File/Blobs between
|
|
|
// the content and bg scripts, so the data is passed through bloburls
|
|
|
// the content and bg scripts, so the data is passed through bloburls
|
|
|
if (execution_mode == "chrome_api") |
|
|
if (execution_mode == "chrome_api") { |
|
|
init.body = await serialize(init.body); |
|
|
[init.body, transfer] = await braveserialize(init.body); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const prom = new Promise<Awaited<ReturnType<typeof fetch>>>((_, rej) => { |
|
|
const prom = new Promise<Awaited<ReturnType<typeof fetch>>>((_, rej) => { |
|
@ -247,13 +264,7 @@ export const corsFetch = async (input: string, init?: RequestInit, lsn?: EventTa |
|
|
s = 0; |
|
|
s = 0; |
|
|
const cmdbuff: any[] = []; |
|
|
const cmdbuff: any[] = []; |
|
|
|
|
|
|
|
|
let cleaned = false; |
|
|
|
|
|
lqueue[id] = (async (e: any) => { |
|
|
lqueue[id] = (async (e: any) => { |
|
|
if (!cleaned && execution_mode == "chrome_api" && init?.body) { |
|
|
|
|
|
cleanupSerialized(init.body); |
|
|
|
|
|
cleaned = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// this is computed from the background script because the content script may
|
|
|
// this is computed from the background script because the content script may
|
|
|
// request everything to be delivered in one chunk, defeating the purpose
|
|
|
// request everything to be delivered in one chunk, defeating the purpose
|
|
|
if (e.progress) { |
|
|
if (e.progress) { |
|
@ -360,7 +371,7 @@ export const corsFetch = async (input: string, init?: RequestInit, lsn?: EventTa |
|
|
|
|
|
|
|
|
port1.postMessage({ |
|
|
port1.postMessage({ |
|
|
id, name: 'corsFetch', args: [input, init] |
|
|
id, name: 'corsFetch', args: [input, init] |
|
|
}); |
|
|
}, transfer); |
|
|
}); |
|
|
}); |
|
|
return prom; |
|
|
return prom; |
|
|
}; |
|
|
}; |
|
|