diff --git a/src/create-context.ts b/src/create-context.ts index 8a6542e..fa6bb9a 100644 --- a/src/create-context.ts +++ b/src/create-context.ts @@ -73,10 +73,9 @@ export async function createContext(node: T, options?: Options & ].map(() => { const worker = new Worker(workerUrl!) worker.onmessage = async event => { - const { url, stream } = event.data - if (stream) { - const data = await (stream as ReadableStream).getReader().read() - requests.get(url)?.resovle?.(data.value) + const { url, result } = event.data + if (result) { + requests.get(url)?.resovle?.(result) } else { requests.get(url)?.reject?.(new Error(`Error receiving message from worker: ${ url }`)) } diff --git a/src/utils.ts b/src/utils.ts index f399e7e..c6e9823 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -93,7 +93,10 @@ export function createSvg(width: number, height: number, ownerDocument?: Documen } export function svgToDataUrl(svg: SVGElement) { - const xhtml = new XMLSerializer().serializeToString(svg) + const xhtml = new XMLSerializer() + .serializeToString(svg) + // eslint-disable-next-line no-control-regex + .replace(/[\u0000-\u001F\u007F-\u009F]/g, '') return `data:image/svg+xml;charset=utf-8,${ encodeURIComponent(xhtml) }` } diff --git a/src/worker.ts b/src/worker.ts index d3843c7..d12dc00 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,25 +1,18 @@ import { baseFetch } from './fetch' +import { consoleWarn } from './utils' import type { BaseFetchOptions } from './fetch' const worker = self as unknown as Worker worker.onmessage = async event => { const options = event.data as BaseFetchOptions & { rawUrl: string } - - const result = await baseFetch(options) - - const stream = new ReadableStream({ - pull(controller) { - controller.enqueue(result) - controller.close() - }, - }) - const url = options.rawUrl || options.url - if (stream) { - worker.postMessage({ url, stream }, [stream]) - } else { + try { + const result = await baseFetch(options) + worker.postMessage({ url, result }) + } catch (error) { + consoleWarn(error) worker.postMessage({ url }) } }