Skip to content

Commit

Permalink
Revert "fix: remove cf pages stream polyfill (#5352)"
Browse files Browse the repository at this point in the history
This reverts commit a2c2bc9.
  • Loading branch information
gioboa authored Nov 29, 2023
1 parent bc31d78 commit 1cf3a25
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/qwik-city/middleware/cloudflare-pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { setServerPlatform } from '@builder.io/qwik/server';

/** @public */
export function createQwikCity(opts: QwikCityCloudflarePagesOptions) {
(globalThis as any).TextEncoderStream = TextEncoderStream;
const qwikSerializer = {
_deserializeData,
_serializeData,
Expand Down Expand Up @@ -131,3 +132,39 @@ export interface PlatformCloudflarePages {
env?: Record<string, any>;
ctx: { waitUntil: (promise: Promise<any>) => void };
}

const resolved = Promise.resolve();

class TextEncoderStream {
// 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,
};
},
};
}
}

0 comments on commit 1cf3a25

Please sign in to comment.