Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
feat: configurable TextDecoder instance (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Oct 26, 2023
1 parent fc356de commit f4b3630
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"contributorsrc",
"conventionalcommits",
"deferreds",
"esque",
"gzipped",
"Iterarable",
"KATT",
Expand Down
6 changes: 6 additions & 0 deletions src/async/asyncTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TextDecoderEsque } from "../internals/esque.js";
import {
TsonBranded,
TsonType,
Expand Down Expand Up @@ -66,6 +67,11 @@ export interface TsonAsyncOptions {
* @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random`
*/
nonce?: () => number | string;
/**
* Customize a text decoder if your runtime doesn't support the `TextDecoder` API
* @default TextDecoder
*/
textDecoder?: TextDecoderEsque;

/**
* The list of types to use
Expand Down
3 changes: 2 additions & 1 deletion src/async/deserializeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,11 @@ export function createTsonParseEventSource(opts: TsonAsyncOptions) {
export function createTsonParseJsonStreamResponse(opts: TsonAsyncOptions) {
const instance = createTsonParseAsync(opts);

const textDecoder = opts.textDecoder ?? new TextDecoder();

return async <TValue = unknown>(response: Response) => {
assert(response.body, "Response body is empty");

const textDecoder = new TextDecoder();
const stringIterator = mapIterable(
readableStreamToAsyncIterable(response.body),
(v) => textDecoder.decode(v),
Expand Down
6 changes: 6 additions & 0 deletions src/internals/esque.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @internal
*/
export interface TextDecoderEsque {
decode(chunk: Uint8Array): string;
}

0 comments on commit f4b3630

Please sign in to comment.