hts/packages/isdk/shared/utils.ts

55 lines
1.6 KiB
TypeScript

import {
StreamPartType,
StreamStringPrefixes,
parseStreamPart,
} from './stream-parts';
export * from './generate-id';
// TODO remove (breaking change)
export { generateId as nanoid } from './generate-id';
// simple decoder signatures:
function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
function createChunkDecoder(
complex: false,
): (chunk: Uint8Array | undefined) => string;
// complex decoder signature:
function createChunkDecoder(
complex: true,
): (chunk: Uint8Array | undefined) => StreamPartType[];
// combined signature for when the client calls this function with a boolean:
function createChunkDecoder(
complex?: boolean,
): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
function createChunkDecoder(complex?: boolean) {
const decoder = new TextDecoder();
if (!complex) {
return function (chunk: Uint8Array | undefined): string {
if (!chunk) return '';
return decoder.decode(chunk, { stream: true });
};
}
return function (chunk: Uint8Array | undefined) {
const decoded = decoder
.decode(chunk, { stream: true })
.split('\n')
.filter(line => line !== ''); // splitting leaves an empty string at the end
return decoded.map(parseStreamPart).filter(Boolean);
};
}
export { createChunkDecoder };
export const isStreamStringEqualToType = (
type: keyof typeof StreamStringPrefixes,
value: string,
): value is StreamString =>
value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith('\n');
export type StreamString =
`${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;