Skip to content

Commit

Permalink
fix: xml parsing exception caused by ascii control character
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Mar 5, 2023
1 parent f33d008 commit a48cb89
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
7 changes: 3 additions & 4 deletions src/create-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ export async function createContext<T extends Node>(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 }`))
}
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) }`
}

Expand Down
19 changes: 6 additions & 13 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -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<any>({
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 })
}
}

1 comment on commit a48cb89

@vercel
Copy link

@vercel vercel bot commented on a48cb89 Mar 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

modern-screenshot – ./

modern-screenshot.vercel.app
modern-screenshot-git-main-qq15725.vercel.app
modern-screenshot-qq15725.vercel.app

Please sign in to comment.