Skip to content

Commit

Permalink
a2f3739 chore(TextEncoderStream): better polyfill tests (#6310)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickJS committed May 17, 2024
1 parent 271c362 commit 6b4df09
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 64 deletions.
37 changes: 4 additions & 33 deletions middleware/bun/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// packages/qwik-city/middleware/bun/index.ts
import {
mergeHeadersCookies,
requestHandler
requestHandler,
_TextEncoderStream_polyfill
} from "../request-handler/index.mjs";
import { getNotFound } from "@qwik-city-not-found-paths";
import { isStaticPath } from "@qwik-city-static-paths";
Expand Down Expand Up @@ -62,40 +63,10 @@ var MIME_TYPES = {

// packages/qwik-city/middleware/bun/index.ts
import { join, extname } from "node:path";
var TextEncoderStream_polyfill = class {
constructor() {
this._encoder = new TextEncoder();
this._reader = null;
this.ready = Promise.resolve();
this.closed = false;
this.readable = new ReadableStream({
start: (controller) => {
this._reader = controller;
}
});
this.writable = new WritableStream({
write: async (chunk) => {
if (chunk != null && this._reader) {
const encoded = this._encoder.encode(chunk);
this._reader.enqueue(encoded);
}
},
close: () => {
var _a;
(_a = this._reader) == null ? void 0 : _a.close();
this.closed = true;
},
abort: (reason) => {
var _a;
(_a = this._reader) == null ? void 0 : _a.error(reason);
this.closed = true;
}
});
}
};
function createQwikCity(opts) {
var _a;
globalThis.TextEncoderStream = TextEncoderStream || TextEncoderStream_polyfill;
globalThis.TextEncoderStream ||= class TextEncoderStream extends _TextEncoderStream_polyfill {
};
const qwikSerializer = {
_deserializeData,
_serializeData,
Expand Down
34 changes: 4 additions & 30 deletions middleware/cloudflare-pages/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// packages/qwik-city/middleware/cloudflare-pages/index.ts
import {
mergeHeadersCookies,
requestHandler
requestHandler,
_TextEncoderStream_polyfill
} from "../request-handler/index.mjs";
import { getNotFound } from "@qwik-city-not-found-paths";
import { isStaticPath } from "@qwik-city-static-paths";
Expand All @@ -11,7 +12,8 @@ function createQwikCity(opts) {
try {
new globalThis.TextEncoderStream();
} catch (e) {
globalThis.TextEncoderStream = TextEncoderStream;
globalThis.TextEncoderStream = class TextEncoderStream extends _TextEncoderStream_polyfill {
};
}
const qwikSerializer = {
_deserializeData,
Expand Down Expand Up @@ -97,34 +99,6 @@ function createQwikCity(opts) {
}
return onCloudflarePagesFetch;
}
var resolved = Promise.resolve();
var TextEncoderStream = class {
constructor() {
this._writer = null;
this.readable = {
pipeTo: (writableStream) => {
this._writer = writableStream.getWriter();
}
};
this.writable = {
getWriter: () => {
if (!this._writer) {
throw new Error("No writable stream");
}
const encoder = new TextEncoder();
return {
write: async (chunk) => {
if (chunk != null) {
await this._writer.write(encoder.encode(chunk));
}
},
close: () => this._writer.close(),
ready: resolved
};
}
};
}
};
export {
createQwikCity
};
55 changes: 55 additions & 0 deletions middleware/request-handler/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ __export(request_handler_exports, {
AbortMessage: () => AbortMessage,
RedirectMessage: () => RedirectMessage,
ServerError: () => ServerError,
_TextEncoderStream_polyfill: () => _TextEncoderStream_polyfill,
getErrorHtml: () => getErrorHtml,
mergeHeadersCookies: () => mergeHeadersCookies,
requestHandler: () => requestHandler
Expand Down Expand Up @@ -1442,11 +1443,65 @@ async function loadRequestHandlers(qwikCityPlan, pathname, method, checkOrigin,
}
return null;
}

// packages/qwik-city/middleware/request-handler/polyfill.ts
var _TextEncoderStream_polyfill = class {
#pendingHighSurrogate = null;
#handle = new TextEncoder();
#transform = new TransformStream({
transform: (chunk, controller) => {
chunk = String(chunk);
let finalChunk = "";
for (const item of chunk) {
const codeUnit = item.charCodeAt(0);
if (this.#pendingHighSurrogate !== null) {
const highSurrogate = this.#pendingHighSurrogate;
this.#pendingHighSurrogate = null;
if (codeUnit >= 56320 && codeUnit <= 57343) {
finalChunk += highSurrogate + item;
continue;
}
finalChunk += "\uFFFD";
}
if (codeUnit >= 55296 && codeUnit <= 56319) {
this.#pendingHighSurrogate = item;
continue;
}
if (codeUnit >= 56320 && codeUnit <= 57343) {
finalChunk += "\uFFFD";
continue;
}
finalChunk += item;
}
if (finalChunk) {
controller.enqueue(this.#handle.encode(finalChunk));
}
},
flush: (controller) => {
if (this.#pendingHighSurrogate !== null) {
controller.enqueue(new Uint8Array([239, 191, 189]));
}
}
});
get encoding() {
return this.#handle.encoding;
}
get readable() {
return this.#transform.readable;
}
get writable() {
return this.#transform.writable;
}
get [Symbol.toStringTag]() {
return "TextEncoderStream";
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
AbortMessage,
RedirectMessage,
ServerError,
_TextEncoderStream_polyfill,
getErrorHtml,
mergeHeadersCookies,
requestHandler
Expand Down
14 changes: 14 additions & 0 deletions middleware/request-handler/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,4 +653,18 @@ declare type StatusCodes = InformationalCode | SuccessCode | ClientErrorCode | S
*/
declare type SuccessCode = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226;

/**
* TextEncoderStream polyfill based on Node.js' implementation
* https://github.com/nodejs/node/blob/3f3226c8e363a5f06c1e6a37abd59b6b8c1923f1/lib/internal/webstreams/encoding.js#L38-L119
* (MIT License)
*/
/** @internal */
export declare class _TextEncoderStream_polyfill {
#private;
get encoding(): string;
get readable(): ReadableStream<Uint8Array>;
get writable(): WritableStream<string>;
get [Symbol.toStringTag](): string;
}

export { }
54 changes: 54 additions & 0 deletions middleware/request-handler/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,10 +1401,64 @@ async function loadRequestHandlers(qwikCityPlan, pathname, method, checkOrigin,
}
return null;
}

// packages/qwik-city/middleware/request-handler/polyfill.ts
var _TextEncoderStream_polyfill = class {
#pendingHighSurrogate = null;
#handle = new TextEncoder();
#transform = new TransformStream({
transform: (chunk, controller) => {
chunk = String(chunk);
let finalChunk = "";
for (const item of chunk) {
const codeUnit = item.charCodeAt(0);
if (this.#pendingHighSurrogate !== null) {
const highSurrogate = this.#pendingHighSurrogate;
this.#pendingHighSurrogate = null;
if (codeUnit >= 56320 && codeUnit <= 57343) {
finalChunk += highSurrogate + item;
continue;
}
finalChunk += "\uFFFD";
}
if (codeUnit >= 55296 && codeUnit <= 56319) {
this.#pendingHighSurrogate = item;
continue;
}
if (codeUnit >= 56320 && codeUnit <= 57343) {
finalChunk += "\uFFFD";
continue;
}
finalChunk += item;
}
if (finalChunk) {
controller.enqueue(this.#handle.encode(finalChunk));
}
},
flush: (controller) => {
if (this.#pendingHighSurrogate !== null) {
controller.enqueue(new Uint8Array([239, 191, 189]));
}
}
});
get encoding() {
return this.#handle.encoding;
}
get readable() {
return this.#transform.readable;
}
get writable() {
return this.#transform.writable;
}
get [Symbol.toStringTag]() {
return "TextEncoderStream";
}
};
export {
AbortMessage,
RedirectMessage,
ServerError,
_TextEncoderStream_polyfill,
getErrorHtml,
mergeHeadersCookies,
requestHandler
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@builder.io/qwik-city",
"description": "The meta-framework for Qwik.",
"version": "1.5.4-dev20240516053525",
"version": "1.5.4-dev20240517014936",
"bugs": "https://github.com/QwikDev/qwik/issues",
"dependencies": {
"@mdx-js/mdx": "^3.0.1",
Expand Down

0 comments on commit 6b4df09

Please sign in to comment.