diff --git a/packages/qwik-city/middleware/bun/index.ts b/packages/qwik-city/middleware/bun/index.ts index 3cc057523b4..195f88f09f1 100644 --- a/packages/qwik-city/middleware/bun/index.ts +++ b/packages/qwik-city/middleware/bun/index.ts @@ -20,7 +20,7 @@ import { join, extname } from 'node:path'; export function createQwikCity(opts: QwikCityBunOptions) { // @builder.io/qwik-city/middleware/bun // still missing from bun: last check was bun version 1.1.8 - globalThis.TextEncoderStream ||= class TextEncoderStream extends _TextEncoderStream_polyfill {}; + globalThis.TextEncoderStream ||= _TextEncoderStream_polyfill; const qwikSerializer = { _deserializeData, diff --git a/packages/qwik-city/middleware/cloudflare-pages/index.ts b/packages/qwik-city/middleware/cloudflare-pages/index.ts index 22e3d04047a..eee224c1e9b 100644 --- a/packages/qwik-city/middleware/cloudflare-pages/index.ts +++ b/packages/qwik-city/middleware/cloudflare-pages/index.ts @@ -5,7 +5,7 @@ import type { import { mergeHeadersCookies, requestHandler, - _TextEncoderStream_polyfill, + _TextEncoderStream_polyfill2, } from '@builder.io/qwik-city/middleware/request-handler'; import { getNotFound } from '@qwik-city-not-found-paths'; import { isStaticPath } from '@qwik-city-static-paths'; @@ -22,7 +22,7 @@ export function createQwikCity(opts: QwikCityCloudflarePagesOptions) { new globalThis.TextEncoderStream(); } catch (e) { // @ts-ignore - globalThis.TextEncoderStream = class TextEncoderStream extends _TextEncoderStream_polyfill {}; + globalThis.TextEncoderStream = _TextEncoderStream_polyfill2; } const qwikSerializer = { _deserializeData, diff --git a/packages/qwik-city/middleware/request-handler/api.md b/packages/qwik-city/middleware/request-handler/api.md index 91fb8b63b50..e130f662b74 100644 --- a/packages/qwik-city/middleware/request-handler/api.md +++ b/packages/qwik-city/middleware/request-handler/api.md @@ -233,6 +233,17 @@ export class _TextEncoderStream_polyfill { get writable(): WritableStream; } +// @internal (undocumented) +export class _TextEncoderStream_polyfill2 { + constructor(); + // (undocumented) + readable: any; + // (undocumented) + writable: any; + // (undocumented) + _writer: any; +} + // (No @packageDocumentation comment for this package) ``` diff --git a/packages/qwik-city/middleware/request-handler/index.ts b/packages/qwik-city/middleware/request-handler/index.ts index a5d10b3195a..34790151783 100644 --- a/packages/qwik-city/middleware/request-handler/index.ts +++ b/packages/qwik-city/middleware/request-handler/index.ts @@ -2,7 +2,7 @@ export { getErrorHtml, ServerError } from './error-handler'; export { mergeHeadersCookies } from './cookie'; export { AbortMessage, RedirectMessage } from './redirect-handler'; export { requestHandler } from './request-handler'; -export { _TextEncoderStream_polyfill } from './polyfill'; +export { _TextEncoderStream_polyfill, _TextEncoderStream_polyfill2 } from './polyfill'; export type { CacheControl, Cookie, diff --git a/packages/qwik-city/middleware/request-handler/polyfill.ts b/packages/qwik-city/middleware/request-handler/polyfill.ts index c51e6185b17..485bae077f7 100644 --- a/packages/qwik-city/middleware/request-handler/polyfill.ts +++ b/packages/qwik-city/middleware/request-handler/polyfill.ts @@ -73,3 +73,39 @@ export class _TextEncoderStream_polyfill { return 'TextEncoderStream'; } } + +const resolved = Promise.resolve(); +/** @internal */ +export class _TextEncoderStream_polyfill2 { + // minimal polyfill implementation of TextEncoderStream + // since Cloudflare Pages doesn't support readable.pipeTo() + _writer: any; + readable: any; + writable: any; + + constructor() { + this._writer = null; + this.readable = { + pipeTo: (writableStream: any) => { + this._writer = writableStream.getWriter(); + }, + }; + this.writable = { + getWriter: () => { + if (!this._writer) { + throw new Error('No writable stream'); + } + const encoder = new TextEncoder(); + return { + write: async (chunk: any) => { + if (chunk != null) { + await this._writer.write(encoder.encode(chunk)); + } + }, + close: () => this._writer.close(), + ready: resolved, + }; + }, + }; + } +}