javascript - ReferenceError: TransformStream is not defined - Stack Overflow

I'm attempting to test the implementation of Fast Google Fonts with Cloudflare Workers, from Cloud

I'm attempting to test the implementation of Fast Google Fonts with Cloudflare Workers, from Cloudflare's blog for inlining the Google Fonts stylesheet directly into the HTML. The implementation itself seems to be working fine when running via the Cloudflare worker. But I wrote some tests, and when running the tests, I get this error that says TransformStream is not defined.

Error when running my Jest test via npm run test:

ReferenceError: TransformStream is not defined

This is the code that the test is hitting:

async function processHtmlResponse(request, response) {
  ...

  // This is where the reference error es up 
  const { readable, writable } = new TransformStream();

  const newResponse = new Response(readable, response);
  modifyHtmlStream(response.body, writable, request, embedStylesheet);

  return newResponse;
}

This test looks something like this, where we basically expect that the stylesheet link will be replaced by the stylesheet itself, containing all the @font-face styles.

describe('inlineGoogleFontsHtml', () => {
  it('inlines the response of a Google Fonts stylesheet link within an HTML response', async () => {
    const request = new Request('')
    const response = new Response(
        '<html><head><link rel="stylesheet" media="screen" href=":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')
})

I'm not really sure what the issue is here. Does TransformStream work in Node? Is there some polyfill that's needed?

Related: Cloudflare streams

I'm attempting to test the implementation of Fast Google Fonts with Cloudflare Workers, from Cloudflare's blog for inlining the Google Fonts stylesheet directly into the HTML. The implementation itself seems to be working fine when running via the Cloudflare worker. But I wrote some tests, and when running the tests, I get this error that says TransformStream is not defined.

Error when running my Jest test via npm run test:

ReferenceError: TransformStream is not defined

This is the code that the test is hitting:

async function processHtmlResponse(request, response) {
  ...

  // This is where the reference error es up 
  const { readable, writable } = new TransformStream();

  const newResponse = new Response(readable, response);
  modifyHtmlStream(response.body, writable, request, embedStylesheet);

  return newResponse;
}

This test looks something like this, where we basically expect that the stylesheet link will be replaced by the stylesheet itself, containing all the @font-face styles.

describe('inlineGoogleFontsHtml', () => {
  it('inlines the response of a Google Fonts stylesheet link within an HTML response', async () => {
    const request = new Request('https://example.')
    const response = new Response(
        '<html><head><link rel="stylesheet" media="screen" href="https://fonts.googleapis./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')
})

I'm not really sure what the issue is here. Does TransformStream work in Node? Is there some polyfill that's needed?

Related: Cloudflare streams

Share asked May 7, 2020 at 22:55 orangemilkteaorangemilktea 1251 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

TransformStream is part of the Streams API, a browser-side standard. It is not implemented by Node (because they had their own streams long before this spec existed), so you will need a polyfill when testing your code in Node.

Incidentally, the example you're following is fairly old. These days, it would be better to use HTMLRewriter to implement this kind of transformation -- it is much more efficient for rewriting HTML specifically. (However, it is a Cloudflare-specific feature, so you wouldn't be able to test it under Node at all.)

Also if you are running code on node which requires TransformStream object, it didn't work for me on v14.20, but seems to work on version 18, i.e. this can be checked via node --version

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744916031a4600840.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信