Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use old cf TextEncoderStream #6373

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/qwik-city/middleware/bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik-city/middleware/cloudflare-pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions packages/qwik-city/middleware/request-handler/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ export class _TextEncoderStream_polyfill {
get writable(): WritableStream<string>;
}

// @internal (undocumented)
export class _TextEncoderStream_polyfill2 {
constructor();
// (undocumented)
readable: any;
// (undocumented)
writable: any;
// (undocumented)
_writer: any;
}

// (No @packageDocumentation comment for this package)

```
2 changes: 1 addition & 1 deletion packages/qwik-city/middleware/request-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions packages/qwik-city/middleware/request-handler/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
},
};
}
}
Loading