UNPKG

univ-conv

Version:

The universal binary and text converter

158 lines 5.87 kB
import { AbstractConverter, } from "./converters/AbstractConverter"; import { ArrayBufferConverter } from "./converters/ArrayBufferConverter"; import { Base64Converter } from "./converters/Base64Converter"; import { BinaryConverter } from "./converters/BinaryConverter"; import { getType, isEmpty, } from "./converters/core"; import { closeStream, hasBlob, hasReadableStream, isWritable, isWritableStream, pipeNodeStream, pipeWebStream, } from "./converters/Environment"; import { FalseConverter } from "./converters/FalseConverter"; import { HexConverter } from "./converters/HexConverter"; import { TextConverter } from "./converters/TextConverter"; import { Uint8ArrayConverter } from "./converters/Uint8ArrayConverter"; import { URLConverter } from "./converters/URLConverter"; class DefaultUnivConv { constructor() { this.converters = new Map(); } _empty(input) { if (typeof input === "string") { return ""; } const converter = this._find(input); return converter.empty(); } _emptyOf(type) { const converter = this._of(type); return converter.empty(); } _find(input, options) { for (const converter of this.converters.values()) { if (converter.is(input, options)) { return converter; } } throw new Error(`No converter: input=${getType(input)}, srcStringType=${options?.inputStringType}`); } _of(type) { const converter = this.converters.get(type); if (converter) { return converter; } throw new Error(`No converter: type=${type}`); } addConverter(name, converter) { this.converters.set(name, converter); } async convert(returnType, input, options) { if (options?.length === 0) { return this._emptyOf(returnType); } const converter = this._of(returnType); return await converter.from(input, options); } is(type, input, options) { const converter = this._of(type); return converter.is(input, options); } async merge(to, chunks, options) { const results = await this._convertAll(to, chunks, options); const converter = this._of(to); return await converter.merge(results, options); } async pipe(input, output, options) { if (isWritable(output)) { const readable = await this._of("readable").from(input, options); try { await pipeNodeStream(readable, output); } catch (e) { closeStream(output, e); } } else if (isWritableStream(output)) { let stream; try { stream = await this._of("readablestream").from(input, options); await pipeWebStream(stream, output); closeStream(output); } catch (e) { closeStream(output, e); } } else if (typeof output === "string") { await this._of("url").from(input, { ...options, outputURL: output, }); } else { throw new Error("Illegal output type: " + getType(output)); } } removeConverter(name) { this.converters.delete(name); } async size(input, options) { const converter = this._find(input, options); return await converter.size(input, options); } async slice(input, options) { if (typeof options.start !== "number" && typeof options.length !== "number") { throw new Error("Illegal argument: options.start and options.length are undefined."); } if (isEmpty(input, options)) { return Promise.resolve(this._empty(input)); } const converter = this._find(input, options); if (converter) { return await converter.from(input, options); } throw new Error("Illegal output type: " + getType(input)); } async _convertAll(returnType, chunks, options) { const results = []; for (const chunk of chunks) { const converted = await this.convert(returnType, chunk, options); results.push(converted); } return results; } } export const getUnivConv = async () => { if (AbstractConverter._UNIV_CONV) { return AbstractConverter._UNIV_CONV; } const univConv = new DefaultUnivConv(); univConv.addConverter("base64", new Base64Converter()); univConv.addConverter("binary", new BinaryConverter()); univConv.addConverter("hex", new HexConverter()); univConv.addConverter("url", new URLConverter()); univConv.addConverter("text", new TextConverter()); univConv.addConverter("arraybuffer", new ArrayBufferConverter()); univConv.addConverter("uint8array", new Uint8ArrayConverter()); if (hasBlob) { const bc = new (await import("./converters/BlobConverter")).BlobConverter(); univConv.addConverter("blob", bc); } else { univConv.addConverter("blob", new FalseConverter("Blob")); } try { const rc = new (await import("./converters/ReadableConverter")).ReadableConverter(); univConv.addConverter("readable", rc); } catch { univConv.addConverter("readable", new FalseConverter("Readable")); } if (hasReadableStream) { const rsc = new (await import("./converters/ReadableStreamConverter")).ReadableStreamConverter(); univConv.addConverter("readablestream", rsc); } else { univConv.addConverter("readablestream", new FalseConverter("ReadableStream")); } AbstractConverter._UNIV_CONV = univConv; return AbstractConverter._UNIV_CONV; }; //# sourceMappingURL=UnivConv.js.map