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
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Oct 26, 2023
1 parent fc356de commit d56277e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 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,7 @@ export interface TsonAsyncOptions {
* @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random`
*/
nonce?: () => number | string;
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;
}

Check warning on line 6 in src/internals/esque.ts

View check run for this annotation

Codecov / codecov/patch

src/internals/esque.ts#L1-L6

Added lines #L1 - L6 were not covered by tests

0 comments on commit d56277e

Please sign in to comment.