我正在尝试测试使用Cloudflare Workers的快速Google字体的实现,从Cloudflare的博客中直接将Google字体样式表嵌入到HTML中。当通过Cloudflare工作人员运行时,实现本身似乎运行良好。但是我编写了一些测试,在运行测试时,我得到了这样一个错误,即没有定义TransformStream
。
通过npm run test
运行Jest测试时出错:
ReferenceError: TransformStream is not defined
这是测试正在命中的代码:
async function processHtmlResponse(request, response) {
...
// This is where the reference error comes up
const { readable, writable } = new TransformStream();
const newResponse = new Response(readable, response);
modifyHtmlStream(response.body, writable, request, embedStylesheet);
return newResponse;
}
这个测试如下所示,我们基本上预期样式表链接将被样式表本身所取代,其中包含所有的@font-face
样式。
describe('inlineGoogleFontsHtml', () => {
it('inlines the response of a Google Fonts stylesheet link within an HTML response', async () => {
const request = new Request('https://example.com')
const response = new Response(
'<html><head><link rel="stylesheet" media="screen" href="https://fonts.googleapis.com/css?family=Lato:300"></head><body></body></html>',
{
headers: {
'Content-Type': 'text/html'
}
}
)
const rewrittenResponse = processHtmlResponse(request, response)
const rewrittenResponseText = await rewrittenResponse.text()
expect(rewrittenResponseText).toContain('@font-face')
})
我不太清楚这到底是什么问题。TransformStream
在Node工作吗?有需要填充物吗?
相关:云焰流
发布于 2020-05-07 23:59:58
TransformStream
是浏览器端标准流API的一部分.它不是由Node实现的(因为在这个规范存在之前很久他们就有自己的流),所以在Node中测试您的代码时,您需要一个多填充。
顺便说一句,您所遵循的示例相当古老。现在,更好的做法是使用HTMLRewriter来实现这种转换--特别是重写HTML的效率要高得多。(然而,这是一个特定于Cloudflare的特性,因此您根本无法在Node下测试它。)
https://stackoverflow.com/questions/61669151
复制相似问题