Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@fuman/io

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fuman/io - npm Package Compare versions

Comparing version
0.0.4
to
0.0.8
+2
_utils.d.cts
export declare function nextPowerOfTwo(n: number): number;
export declare function getDv(buffer: Uint8Array): DataView;
export * from './reader.js';
export * from './utils.js';
import { ISyncReadable } from '../types.js';
/** a bit reader that reads bits from a byte-aligned stream */
export declare class BitReader implements ISyncReadable {
#private;
/** @param readable fuman readable stream */
constructor(readable: ISyncReadable);
/** Whether the reader is currently aligned on a byte boundary */
get isAligned(): boolean;
/** Skip any remaining bits in the current byte. No-op if already aligned */
align(): void;
/** The current bit position within the last consumed byte */
get bitPosition(): number;
readSync(bytes: number): Uint8Array;
/** read a number of bits from the stream, and return them as a number */
readBits(size: number): number;
/** read a number of bits from the stream, and return them as a bigint */
readBitsBig(size: number): bigint;
/** skip a number of bits from the stream */
skipBits(size: number): void;
}
/** Reverse bit ordering of a single byte */
export declare function reverse8Bits(byte: number): number;
/** Reverse bits of a numeric value, treating it as a `size`-bit number */
export declare function reverseBits(value: number, size: number): number;
/** Reverse bits of a bigint, treating it as a `size`-bit number */
export declare function reverseBitsBig(value: bigint, size: number | bigint): bigint;
/**
* Reverse the bit ordering of each byte in the byte array, **in place**
*
* @example `reverseBitsAll(new Uint8Array([0b10101010, 0b01010101])) // becomes [0b01010101, 0b10101010]
*/
export declare function reverseBitsAll(buf: Uint8Array): void;
import { IReadable } from './types.js';
export declare class BufReader implements IReadable {
#private;
constructor(readable: IReadable, size?: number);
/** the size of the buffer */
get bufferSize(): number;
/** the number of bytes that are currently buffered */
get buffered(): number;
read(into: Uint8Array): Promise<number>;
}
import { IReadable, ISyncReadable, ISyncWritable, IWritable } from './types.js';
/** a byte buffer implementing fuman readable/writable streams */
export declare class Bytes implements IReadable, IWritable, ISyncReadable, ISyncWritable {
#private;
constructor(buf: Uint8Array);
static alloc(capacity?: number): Bytes;
static from(data: Uint8Array): Bytes;
/** Total number of bytes in the underlying buffer */
get capacity(): number;
/** Number of bytes available to be read */
get available(): number;
/** Number of bytes written */
get written(): number;
readSync(bytes: number): Uint8Array;
read(into: Uint8Array): Promise<number>;
writeSync(size: number): Uint8Array;
disposeWriteSync(written?: number): void;
write(bytes: Uint8Array): Promise<void>;
/**
* get the "result" of the buffer, i.e. everything that has been written so far,
* but not yet read
*
* **Note**: this method returns a view into the underlying buffer, and does advance the read cursor
*/
result(): Uint8Array;
/** Reclaim memory by only keeping the yet-unread data */
reclaim(): void;
/** Mark last n bytes as unread */
rewind(n: number): void;
/** reset the read/write cursors */
reset(): void;
}
import { Bytes } from '../bytes.js';
import { ISyncWritable } from '../types.js';
import { IFrameDecoder, IFrameEncoder } from './types.js';
/** options for {@link DelimiterCodec} */
export interface DelimiterCodecOptions {
/**
* Strategy for handling delimiter.
* - `keep` - delimiter is kept at the end of each frame
* - `discard` - delimiter is discarded
*
* Ignored for encoding (delimiter is always appended after the frame)
*
* @default 'discard'
*/
strategy?: 'keep' | 'discard';
}
/** a simple frame codec that uses a delimiter to separate frames */
export declare class DelimiterCodec implements IFrameDecoder, IFrameEncoder {
#private;
readonly delimiter: Uint8Array;
readonly options?: DelimiterCodecOptions | undefined;
/**
* @param delimiter delimiter to use
* @param options options
*/
constructor(delimiter: Uint8Array, options?: DelimiterCodecOptions | undefined);
decode(buf: Bytes, eof: boolean): Uint8Array | null;
encode(data: Uint8Array, into: ISyncWritable): void;
reset(): void;
}
export * from './delimiter.js';
export * from './length-delimited.js';
export * from './reader.js';
export * from './text-delimiter.js';
export * from './types.js';
export * from './writer.js';
import { Bytes } from '../bytes.js';
import { ISyncWritable } from '../types.js';
import { IFrameDecoder, IFrameEncoder } from './types.js';
/** options for {@link LengthDelimitedCodec} */
export interface LengthDelimitedCodecOptions {
/**
* function that will be called to read the length of the frame
*
* @example `read.uint32le`
*/
read?: (r: Bytes) => number | null;
/**
* function that will be called to write the length of the frame
*
* @example `write.uint32le`
*/
write?: (w: ISyncWritable, n: number) => void;
}
/** a simple frame codec that uses a length prefix to separate frames */
export declare class LengthDelimitedCodec implements IFrameDecoder, IFrameEncoder {
#private;
constructor(options: LengthDelimitedCodecOptions);
decode(buf: Bytes): Uint8Array | null;
encode(frame: Uint8Array, into: ISyncWritable): void;
reset(): void;
}
import { IReadable } from '../types.js';
import { IFrameDecoder } from './types.js';
/** options for {@link FramedReader} */
export interface FramedReaderOptions {
initialBufferSize?: number;
readChunkSize?: number;
}
/** a reader that decodes frames one by one from a readable stream */
export declare class FramedReader<Frame> {
#private;
/**
* @param readable fuman readable stream to read from
* @param decoder frame decoder
* @param options extra options
*/
constructor(readable: IReadable, decoder: IFrameDecoder<Frame>, options?: FramedReaderOptions);
/** read a next frame from the stream, or `null` if the stream ended */
read(): Promise<Frame | null>;
/** create an async iterator that yields frames */
[Symbol.asyncIterator](): AsyncIterator<Frame>;
}
import { Bytes } from '../bytes.js';
import { ISyncWritable } from '../types.js';
import { DelimiterCodecOptions } from './delimiter.js';
import { IFrameDecoder, IFrameEncoder } from './types.js';
/** wrapper over {@link DelimiterCodec} that handles text frames */
export declare class TextDelimiterCodec implements IFrameDecoder<string>, IFrameEncoder<string> {
#private;
constructor(delimiter: Uint8Array | string, options?: DelimiterCodecOptions);
decode(buf: Bytes, eof: boolean): string | null;
encode(data: string, into: ISyncWritable): void;
reset(): void;
}
import { MaybePromise } from '@fuman/utils';
import { Bytes } from '../bytes.js';
import { ISyncWritable } from '../types.js';
export interface IFrameDecoder<Frame = Uint8Array> {
/**
* Decode a frame from a buffer
*
* > **Important implementation notice**: When returning byte arrays, make sure that the returned array is **not**
* > a view into the original buffer, as the underlying buffer may get invalidated
*/
decode: (buf: Bytes, eof: boolean) => MaybePromise<Frame | null>;
}
export interface IFrameEncoder<Frame = Uint8Array> {
/** Encode a frame into a writable stream */
encode: (frame: Frame, into: ISyncWritable) => MaybePromise<void>;
/** Reset the encoder, should it have any internal state */
reset: () => void;
}
import { IWritable } from '../types.js';
import { IFrameEncoder } from './types.js';
/** options for {@link FramedWriter} */
export interface FramedWriterOptions {
initialBufferSize?: number;
}
/** a writer that encodes frames one by one into a writable stream */
export declare class FramedWriter<Frame = Uint8Array> {
#private;
/**
* @param writable fuman writable stream to write to
* @param encoder frame encoder
* @param options extra options
*/
constructor(writable: IWritable, encoder: IFrameEncoder<Frame>, options?: FramedWriterOptions);
/** write a frame to the stream */
write(frame: Frame): Promise<void>;
}
/**
* Error thrown when trying to read more bytes than available.
*
* The part that was read is available in the `part` property.
*/
export declare class PartialReadError extends RangeError {
/** the part that was read */
readonly part: Uint8Array;
constructor(
/** the part that was read */
part: Uint8Array, expectedLength: number);
}
import * as read from './read/index.js';
import * as write from './write/index.js';
export * from './bits/index.js';
export * from './buf-reader.js';
export * from './bytes.js';
export * from './codec/index.js';
export * from './errors.js';
export * from './read/adapters.js';
export * from './reader-with-final.js';
export * from './types.js';
export * from './write/adapters.js';
export { read, write };
import { IClosable, IReadable, ISyncReadable } from '../types.js';
/** create an async readable stream from a sync readable stream */
export declare function fumanSyncReadableToAsync(readable: ISyncReadable): IReadable;
/** convert a web ReadableStream to a fuman readable stream */
export declare function webReadableToFuman(readable: ReadableStream<Uint8Array>): IReadable & IClosable;
/** convert a fuman readable stream to a web ReadableStream */
export declare function fumanReadableToWeb(readable: IReadable): ReadableStream<Uint8Array>;
export * from './strings.js';
import { IReadable } from '../../types.js';
/**
* read exactly N bytes from the source
*
* @param readable fuman readable stream
* @param length length of the buffer to read, or a buffer to read into (when a number is passed, a new buffer will be allocated)
* @param onEof what to do when the end of the stream is reached
* - `error` - throw an {@link PartialReadError}
* - `truncate` - truncate the buffer to the number of bytes that were read. note that this might return 0 bytes
*/
export declare function exactly(readable: IReadable, length: number | Uint8Array, onEof?: 'error' | 'truncate'): Promise<Uint8Array>;
/**
* read the source until it ends, and return the buffer
*
* @param readable fuman readable stream
* @param chunkSize size of the chunks to read
*/
export declare function untilEnd(readable: IReadable, chunkSize?: number): Promise<Uint8Array>;
import * as async from './async/index.js';
export * from './numbers.js';
export * from './strings.js';
export { async };
import { ISyncReadable } from '../types.js';
/** read a uint8 from the source */
export declare function uint8(readable: ISyncReadable): number;
/** read an int8 from the source (fuman readable stream or a buffer) */
export declare function int8(readable: ISyncReadable | Uint8Array): number;
/** read a uint16 (little endian) from the source (fuman readable stream or a buffer) */
export declare function uint16le(readable: ISyncReadable | Uint8Array): number;
/** read a uint16 (big endian) from the source (fuman readable stream or a buffer) */
export declare function uint16be(readable: ISyncReadable | Uint8Array): number;
/** read a uint24 (little endian) from the source (fuman readable stream or a buffer) */
export declare function uint24le(readable: ISyncReadable | Uint8Array): number;
/** read a uint24 (big endian) from the source (fuman readable stream or a buffer) */
export declare function uint24be(readable: ISyncReadable | Uint8Array): number;
/** read a uint32 (little endian) from the source (fuman readable stream or a buffer) */
export declare function uint32le(readable: ISyncReadable | Uint8Array): number;
/** read a uint32 (big endian) from the source (fuman readable stream or a buffer) */
export declare function uint32be(readable: ISyncReadable | Uint8Array): number;
/** read a uint64 (little endian) from the source (fuman readable stream or a buffer) */
export declare function uint64le(readable: ISyncReadable | Uint8Array): bigint;
/** read a uint64 (big endian) from the source (fuman readable stream or a buffer) */
export declare function uint64be(readable: ISyncReadable | Uint8Array): bigint;
/** read an int16 (little endian) from the source (fuman readable stream or a buffer) */
export declare function int16le(readable: ISyncReadable | Uint8Array): number;
/** read an int16 (big endian) from the source (fuman readable stream or a buffer) */
export declare function int16be(readable: ISyncReadable | Uint8Array): number;
/** read an int24 (little endian) from the source (fuman readable stream or a buffer) */
export declare function int24le(readable: ISyncReadable | Uint8Array): number;
/** read an int24 (big endian) from the source (fuman readable stream or a buffer) */
export declare function int24be(readable: ISyncReadable | Uint8Array): number;
/** read an int32 (little endian) from the source (fuman readable stream or a buffer) */
export declare function int32le(readable: ISyncReadable | Uint8Array): number;
/** read an int32 (big endian) from the source (fuman readable stream or a buffer) */
export declare function int32be(readable: ISyncReadable | Uint8Array): number;
/** read an int64 (little endian) from the source (fuman readable stream or a buffer) */
export declare function int64le(readable: ISyncReadable | Uint8Array): bigint;
/** read an int64 (big endian) from the source (fuman readable stream or a buffer) */
export declare function int64be(readable: ISyncReadable | Uint8Array): bigint;
/** read a variable-size uint (little endian) from the source (fuman readable stream or a buffer) */
export declare function uintle(readable: ISyncReadable | Uint8Array, size: number): bigint;
/** read a variable-size uint (big endian) from the source (fuman readable stream or a buffer) */
export declare function uintbe(readable: ISyncReadable | Uint8Array, size: number): bigint;
/** read a variable-size int (big endian) from the source (fuman readable stream or a buffer) */
export declare function intbe(readable: ISyncReadable | Uint8Array, size: number): bigint;
/** read a variable-size int (little endian) from the source (fuman readable stream or a buffer) */
export declare function intle(readable: ISyncReadable | Uint8Array, size: number): bigint;
/** read a float32 (little endian) from the source (fuman readable stream or a buffer) */
export declare function float32le(readable: ISyncReadable | Uint8Array): number;
/** read a float32 (big endian) from the source (fuman readable stream or a buffer) */
export declare function float32be(readable: ISyncReadable | Uint8Array): number;
/** read a float64 (little endian) from the source (fuman readable stream or a buffer) */
export declare function float64le(readable: ISyncReadable | Uint8Array): number;
/** read a float64 (big endian) from the source (fuman readable stream or a buffer) */
export declare function float64be(readable: ISyncReadable | Uint8Array): number;
import { ISyncReadable } from '../types.js';
export declare function exactly(readable: ISyncReadable, length: number): Uint8Array;
export declare function rawString(readable: ISyncReadable, length: number): string;
export declare function utf8String(readable: ISyncReadable, length: number): string;
export declare function untilCondition(readable: ISyncReadable, condition: (byte: number) => boolean): Uint8Array;
export declare function untilEnd(readable: ISyncReadable, chunkSize?: number): Uint8Array;
export declare function untilZero(readable: ISyncReadable): Uint8Array;
export declare function cUtf8String(readable: ISyncReadable): string;
export declare function lengthPrefixed(readLength: (readable: ISyncReadable) => number, readable: ISyncReadable): Uint8Array;
import { IReadable } from './types.js';
/** result of {@link ReaderWithFinal#readWithFinal} */
export interface ReaderWithFinalResult {
/** number of bytes read */
readonly nread: number;
/** whether this was the last chunk */
readonly final: boolean;
}
/**
* a reader that reads one read ahead, allowing the caller to know
* whether the chunk being read is the last one
*/
export declare class ReaderWithFinal implements IReadable {
#private;
constructor(readable: IReadable, params?: {
internalBufferSize?: number;
});
/** read a chunk of data, and whether it is the last one */
readWithFinal(into: Uint8Array): Promise<ReaderWithFinalResult>;
read(into: Uint8Array): Promise<number>;
}
export declare function isByobCapableStream(stream: ReadableStream<Uint8Array>): boolean;
/** A synchronous readable stream */
export interface ISyncReadable {
/**
* Read the specified number of bytes from the source
* and return them as a Uint8Array.
*
* **The returned Uint8Array**:
* - *may* be a view into a larger buffer
* - is only valid until the next call to `readSync`
* - may be smaller than the requested number of bytes if the end of the source is reached.
* > these constraints allow for more efficient zero-copy implementations in many cases
*
* @param bytes The number of bytes to read
* @returns Uint8Array containing the bytes that were read.
*/
readSync: (bytes: number) => Uint8Array;
}
/** A readable stream */
export interface IReadable {
/**
* Read data from the underlying source into the provided Uint8Array,
* up to the length of the array, and return the number of bytes read.
*
* If there are no bytes available currently, the implementation is supposed to wait
* until at least one byte is available, and only then resolve the promise.
* Resolving the promise with a zero-length Uint8Array is marking the end of the source.
*
* @param bytes The number of bytes to read
* @returns Uint8Array containing the bytes that were read.
*/
read: (into: Uint8Array) => Promise<number>;
}
/** Something that can be closed */
export interface IClosable {
/**
* Close the underlying source.
*/
close: () => void;
}
/** A synchronous writable stream */
export interface ISyncWritable {
/**
* Write the specified number of bytes to the underlying source.
*
* The implementation is supposed to make sure there are at least `bytes` bytes
* available in the underlying source and return a Uint8Array that can be written to.
* The returned Uint8Array must be valid at least until the next call to `writeSync`
* or `disposeWriteSync`.
*
* If the caller writes less than `bytes` bytes to the returned Uint8Array,
* `disposeWriteSync` must be called with the number of bytes that were actually written.
*
* @param bytes The number of bytes to write
* @returns Uint8Array of length `bytes` that can be written to
*/
writeSync: (bytes: number) => Uint8Array;
/**
* Explicitly dispose of the buffer that was returned by the last call to `writeSync`.
*
* If less than `bytes` bytes were written to the buffer, the number of bytes that were
* written must be passed as the `written` argument.
*/
disposeWriteSync: (written?: number) => void;
}
/** A writable stream */
export interface IWritable {
/** Write bytes to the underlying stream, resolving once the write is complete */
write: (bytes: Uint8Array) => Promise<void>;
}
import { IClosable, ISyncWritable, IWritable } from '../types.js';
export declare function fumanSyncWritableToAsync(sync: ISyncWritable): IWritable;
export declare function webWritableToFuman(writable: WritableStream<Uint8Array>): IWritable & IClosable;
export declare function fumanWritableToWeb(writable: IWritable): WritableStream<Uint8Array>;
export * from './numbers.js';
export * from './pipe.js';
export * from './strings.js';
import { ISyncWritable } from '../types.js';
/** write a uint8 to the target (fuman writable stream or a buffer) */
export declare function uint8(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write an int8 to the target (fuman writable stream or a buffer) */
export declare function int8(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write a uint16 (little endian) to the target (fuman writable stream or a buffer) */
export declare function uint16le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write a uint16 (big endian) to the target (fuman writable stream or a buffer) */
export declare function uint16be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write an int16 (little endian) to the target (fuman writable stream or a buffer) */
export declare function int16le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write an int16 (big endian) to the target (fuman writable stream or a buffer) */
export declare function int16be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write a uint24 (little endian) to the target (fuman writable stream or a buffer) */
export declare function uint24le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write a uint24 (big endian) to the target (fuman writable stream or a buffer) */
export declare function uint24be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write an int24 (little endian) to the target (fuman writable stream or a buffer) */
export declare function int24le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write an int24 (big endian) to the target (fuman writable stream or a buffer) */
export declare function int24be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write a uint32 (little endian) to the target (fuman writable stream or a buffer) */
export declare function uint32le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write a uint32 (big endian) to the target (fuman writable stream or a buffer) */
export declare function uint32be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write an int32 (little endian) to the target (fuman writable stream or a buffer) */
export declare function int32le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write an int32 (big endian) to the target (fuman writable stream or a buffer) */
export declare function int32be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
/** write a uint64 (little endian) to the target (fuman writable stream or a buffer) */
export declare function uint64le(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
/** write a uint64 (big endian) to the target (fuman writable stream or a buffer) */
export declare function uint64be(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
/** write an int64 (little endian) to the target (fuman writable stream or a buffer) */
export declare function int64le(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
/** write an int64 (big endian) to the target (fuman writable stream or a buffer) */
export declare function int64be(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
/** write a variable-size uint (little endian) to the target (fuman writable stream or a buffer) */
export declare function uintle(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
/** write a variable-size uint (big endian) to the target (fuman writable stream or a buffer) */
export declare function uintbe(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
/** write a variable-size int (little endian) to the target (fuman writable stream or a buffer) */
export declare function intle(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
/** write a variable-size int (big endian) to the target (fuman writable stream or a buffer) */
export declare function intbe(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
/** write a float32 (little endian) to the target (fuman writable stream or a buffer) */
export declare function float32le(writable: ISyncWritable | Uint8Array, value: number): void;
/** write a float32 (big endian) to the target (fuman writable stream or a buffer) */
export declare function float32be(writable: ISyncWritable | Uint8Array, value: number): void;
/** write a float64 (little endian) to the target (fuman writable stream or a buffer) */
export declare function float64le(writable: ISyncWritable | Uint8Array, value: number): void;
/** write a float64 (big endian) to the target (fuman writable stream or a buffer) */
export declare function float64be(writable: ISyncWritable | Uint8Array, value: number): void;
import { IReadable, IWritable } from '../types.js';
/** pipe the contents of a readable stream (until it ends) into a writable stream */
export declare function pipe(into: IWritable, readable: IReadable): Promise<void>;
import { ISyncWritable } from '../types.js';
/** write a buffer to the stream */
export declare function bytes(writable: ISyncWritable, bytes: Uint8Array): void;
/** write a buffer to the stream, but in reverse order */
export declare function bytesReversed(writable: ISyncWritable, bytes: Uint8Array): void;
/** write a raw string to the stream (`.charCodeAt` is used to get the codepoints) */
export declare function rawString(writable: ISyncWritable, str: string): void;
/** write a utf8-encoded string to the stream */
export declare function utf8String(writable: ISyncWritable, str: string): void;
/** write a utf8-encoded string to the stream, with a null terminator */
export declare function cUtf8String(writable: ISyncWritable, str: string): void;
+3
-1
"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const utils = require("@fuman/utils");
const bytes = require("../bytes.cjs");

@@ -25,4 +26,5 @@ class FramedWriter {

if (buffer.length > 0) {
const copy = utils.u8.allocWith(buffer);
this.#buffer.reset();
await this.#writable.write(buffer);
await this.#writable.write(copy);
}

@@ -29,0 +31,0 @@ }

@@ -0,1 +1,2 @@

import { u8 } from "@fuman/utils";
import { Bytes } from "../bytes.js";

@@ -23,4 +24,5 @@ class FramedWriter {

if (buffer.length > 0) {
const copy = u8.allocWith(buffer);
this.#buffer.reset();
await this.#writable.write(buffer);
await this.#writable.write(copy);
}

@@ -27,0 +29,0 @@ }

{
"name": "@fuman/io",
"type": "module",
"version": "0.0.4",
"version": "0.0.8",
"description": "experimental i/o abstractions",

@@ -6,0 +6,0 @@ "license": "MIT",