Skip to content

Commit

Permalink
New TextDecoderStream and TextEncoderStream implementations (#1782)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Nov 8, 2024
1 parent 4c30dd3 commit 6c006e1
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/flat-dragons-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@whatwg-node/node-fetch': minor
'@whatwg-node/fetch': minor
---

\`TextDecoderStream\` and \`TextEncoderStream\`
4 changes: 4 additions & 0 deletions packages/fetch/dist/create-node-ponyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = function createNodePonyfill(opts = {}) {
TransformStream: globalThis.TransformStream,
CompressionStream: globalThis.CompressionStream,
DecompressionStream: globalThis.DecompressionStream,
TextDecoderStream: globalThis.TextDecoderStream,
TextEncoderStream: globalThis.TextEncoderStream,
Blob: globalThis.Blob,
File: globalThis.File,
crypto: globalThis.crypto,
Expand Down Expand Up @@ -51,6 +53,8 @@ module.exports = function createNodePonyfill(opts = {}) {
ponyfills.TransformStream = newNodeFetch.TransformStream;
ponyfills.CompressionStream = newNodeFetch.CompressionStream;
ponyfills.DecompressionStream = newNodeFetch.DecompressionStream;
ponyfills.TextDecoderStream = newNodeFetch.TextDecoderStream;
ponyfills.TextEncoderStream = newNodeFetch.TextEncoderStream;

ponyfills.Blob = newNodeFetch.Blob;
ponyfills.File = newNodeFetch.File;
Expand Down
4 changes: 4 additions & 0 deletions packages/fetch/dist/esm-ponyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const WritableStream = globalThis.WritableStream;
const TransformStream = globalThis.TransformStream;
const CompressionStream = globalThis.CompressionStream;
const DecompressionStream = globalThis.DecompressionStream;
const TextDecoderStream = globalThis.TextDecoderStream;
const TextEncoderStream = globalThis.TextEncoderStream;
const Blob = globalThis.Blob;
const File = globalThis.File;
const crypto = globalThis.crypto;
Expand All @@ -29,6 +31,8 @@ export {
TransformStream,
CompressionStream,
DecompressionStream,
TextDecoderStream,
TextEncoderStream,
Blob,
File,
crypto,
Expand Down
4 changes: 3 additions & 1 deletion packages/fetch/dist/global-ponyfill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports.fetch = globalThis.fetch; // To enable: import {fetch} from 'cross-fetch'
module.exports.fetch = globalThis.fetch;
module.exports.Headers = globalThis.Headers;
module.exports.Request = globalThis.Request;
module.exports.Response = globalThis.Response;
Expand All @@ -8,6 +8,8 @@ module.exports.WritableStream = globalThis.WritableStream;
module.exports.TransformStream = globalThis.TransformStream;
module.exports.CompressionStream = globalThis.CompressionStream;
module.exports.DecompressionStream = globalThis.DecompressionStream;
module.exports.TextDecoderStream = globalThis.TextDecoderStream;
module.exports.TextEncoderStream = globalThis.TextEncoderStream;
module.exports.Blob = globalThis.Blob;
module.exports.File = globalThis.File;
module.exports.crypto = globalThis.crypto;
Expand Down
4 changes: 4 additions & 0 deletions packages/fetch/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ declare module '@whatwg-node/fetch' {
export const TransformStream: typeof globalThis.TransformStream;
export const CompressionStream: typeof globalThis.CompressionStream;
export const DecompressionStream: typeof globalThis.DecompressionStream;
export const TextDecoderStream: typeof globalThis.TextDecoderStream;
export const TextEncoderStream: typeof globalThis.TextEncoderStream;
export const Blob: typeof globalThis.Blob;
export const File: typeof globalThis.File;
export const crypto: typeof globalThis.crypto;
Expand Down Expand Up @@ -57,6 +59,8 @@ declare module '@whatwg-node/fetch' {
TransformStream: typeof TransformStream;
CompressionStream: typeof CompressionStream;
DecompressionStream: typeof DecompressionStream;
TextDecoderStream: typeof TextDecoderStream;
TextEncoderStream: typeof TextEncoderStream;
Blob: typeof Blob;
File: typeof File;
crypto: typeof crypto;
Expand Down
2 changes: 2 additions & 0 deletions packages/fetch/dist/node-ponyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports.WritableStream = ponyfills.WritableStream;
module.exports.TransformStream = ponyfills.TransformStream;
module.exports.CompressionStream = ponyfills.CompressionStream;
module.exports.DecompressionStream = ponyfills.DecompressionStream;
module.exports.TextDecoderStream = ponyfills.TextDecoderStream;
module.exports.TextEncoderStream = ponyfills.TextEncoderStream;
module.exports.Blob = ponyfills.Blob;
module.exports.File = ponyfills.File;
module.exports.crypto = ponyfills.crypto;
Expand Down
3 changes: 1 addition & 2 deletions packages/fetch/dist/shouldSkipPonyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ function isNextJs() {
}

module.exports = function shouldSkipPonyfill() {
// Bun and Deno already have a Fetch API
if (globalThis.Deno) {
return true
}
if (process.versions.bun) {
if (globalThis.Bun) {
return true
}
if (isNextJs()) {
Expand Down
21 changes: 21 additions & 0 deletions packages/node-fetch/src/Blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ export class PonyfillBlob implements Blob {
});
}

_json: any = null;

json() {
if (this._json) {
return fakePromise(this._json);
}
return this.text().then(text => {
this._json = JSON.parse(text);
return this._json;
});
}

_formData: FormData | null = null;

formData() {
if (this._formData) {
return fakePromise(this._formData);
}
throw new Error('Not implemented');
}

get size() {
if (this._size == null) {
this._size = 0;
Expand Down
52 changes: 52 additions & 0 deletions packages/node-fetch/src/TextEncoderDecoderStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { PonyfillTextDecoder, PonyfillTextEncoder } from './TextEncoderDecoder.js';
import { PonyfillTransformStream } from './TransformStream.js';

export class PonyfillTextDecoderStream
extends PonyfillTransformStream
implements TextDecoderStream
{
private textDecoder: TextDecoder;
constructor(encoding?: BufferEncoding, options?: TextDecoderOptions) {
super({
transform: (chunk, controller) => {
controller.enqueue(this.textDecoder.decode(chunk, { stream: true }));
},
});
this.textDecoder = new PonyfillTextDecoder(encoding, options);
}

get encoding(): string {
return this.textDecoder.encoding;
}

get fatal(): boolean {
return this.textDecoder.fatal;
}

get ignoreBOM(): boolean {
return this.textDecoder.ignoreBOM;
}
}

export class PonyfillTextEncoderStream
extends PonyfillTransformStream
implements TextEncoderStream
{
private textEncoder: TextEncoder;
constructor(encoding?: BufferEncoding) {
super({
transform: (chunk, controller) => {
controller.enqueue(this.textEncoder.encode(chunk));
},
});
this.textEncoder = new PonyfillTextEncoder(encoding);
}

get encoding(): string {
return this.textEncoder.encoding;
}

encode(input: string): Uint8Array {
return this.textEncoder.encode(input);
}
}
4 changes: 4 additions & 0 deletions packages/node-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export { PonyfillTransformStream as TransformStream } from './TransformStream.js
export { PonyfillCompressionStream as CompressionStream } from './CompressionStream.js';
export { PonyfillDecompressionStream as DecompressionStream } from './DecompressionStream.js';
export { PonyfillIteratorObject as IteratorObject } from './IteratorObject.js';
export {
PonyfillTextDecoderStream as TextDecoderStream,
PonyfillTextEncoderStream as TextEncoderStream,
} from './TextEncoderDecoderStream.js';
53 changes: 53 additions & 0 deletions packages/node-fetch/tests/TextEncoderDecoderStream.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { PonyfillReadableStream } from '../src/ReadableStream';
import { PonyfillTextEncoder } from '../src/TextEncoderDecoder';
import {
PonyfillTextDecoderStream,
PonyfillTextEncoderStream,
} from '../src/TextEncoderDecoderStream';

describe('TextEncoderDecoderStream', () => {
it('TextEncoderStream', async () => {
const readableStream = new PonyfillReadableStream({
start(controller) {
controller.enqueue(Buffer.from('Hello, '));
controller.enqueue(Buffer.from('world!'));
controller.close();
},
});
const pipedStream = readableStream.pipeThrough(new PonyfillTextEncoderStream());
const reader = pipedStream.getReader();
const chunks: Uint8Array[] = [];
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
chunks.push(value);
}
const encoded = Buffer.concat(chunks);
expect(encoded.toString('utf-8')).toBe('Hello, world!');
});
it('TextDecoderStream', async () => {
const textEncoder = new PonyfillTextEncoder();
const decodedHello = textEncoder.encode('Hello, ');
const decodedWorld = textEncoder.encode('world!');
const readableStream = new PonyfillReadableStream({
start(controller) {
controller.enqueue(decodedHello);
controller.enqueue(decodedWorld);
controller.close();
},
});
const chunks: string[] = [];
const pipedStream = readableStream.pipeThrough(new PonyfillTextDecoderStream());
const reader = pipedStream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
chunks.push(value);
}
expect(chunks.join('')).toBe('Hello, world!');
});
});
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9499,7 +9499,6 @@ [email protected]:

uWebSockets.js@uNetworking/uWebSockets.js#v20.49.0:
version "20.49.0"
uid "442087c0a01bf146acb7386910739ec81df06700"
resolved "https://codeload.github.com/uNetworking/uWebSockets.js/tar.gz/442087c0a01bf146acb7386910739ec81df06700"

ufo@^1.5.4:
Expand Down

0 comments on commit 6c006e1

Please sign in to comment.