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

feat: configurable TextDecoder instance #80

Merged
merged 3 commits into from
Oct 26, 2023
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
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;
}
Loading