Socket
Socket
Sign inDemoInstall

@hazae41/binary

Package Overview
Dependencies
3
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.3.1 to 1.3.2

2

dist/types/index.d.ts
import * as index from './mods/index.js';
export { index as Binary };
export { Empty } from './mods/binary/empty.js';
export { BinaryError, BinaryReadError, BinaryWriteError, CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from './mods/binary/errors.js';
export { ReadError, ReadUnderflowError, ReadUnknownError, ReadWriteError, SizeUnknownError, WriteError, WriteUnderflowError, WriteUnknownError } from './mods/binary/errors.js';
export { Opaque, SafeOpaque, UnsafeOpaque } from './mods/binary/opaque.js';
export { Readable } from './mods/binary/readable.js';
export { Writable } from './mods/binary/writable.js';
import { Cursor } from '@hazae41/cursor';
import { Result } from '@hazae41/result';
declare class Empty {
/**
* An empty BDT
*/
constructor();
[Symbol.dispose](): void;
trySize(): Result<number, never>;
tryWrite(cursor: Cursor): Result<void, never>;
static tryRead(cursor: Cursor): Result<Empty, never>;
sizeOrThrow(): number;
writeOrThrow(cursor: Cursor): void;
static readOrThrow(cursor: Cursor): Empty;
}
export { Empty };

@@ -1,8 +0,13 @@

import { BytesError } from '@hazae41/bytes';
import { CursorReadError, Cursor, CursorWriteError } from '@hazae41/cursor';
import { Cursor } from '@hazae41/cursor';
type BinaryError = BinaryReadError | BinaryWriteError;
type BinaryReadError = BytesError | CursorReadLengthUnderflowError | CursorReadError;
declare class CursorReadLengthUnderflowError extends Error {
type ReadWriteError = ReadError | WriteError;
type ReadError = ReadUnderflowError | ReadUnknownError;
declare class ReadUnknownError extends Error {
#private;
readonly name: string;
constructor(options: ErrorOptions);
static from(cause: unknown): ReadUnknownError;
}
declare class ReadUnderflowError extends Error {
#private;
readonly cursorOffset: number;

@@ -12,7 +17,19 @@ readonly cursorLength: number;

constructor(cursorOffset: number, cursorLength: number);
static from(cursor: Cursor): CursorReadLengthUnderflowError;
static from(cursor: Cursor): ReadUnderflowError;
}
type BinaryWriteError = BytesError | CursorWriteLenghtUnderflowError | CursorWriteError;
declare class CursorWriteLenghtUnderflowError extends Error {
type WriteError = SizeUnknownError | WriteUnderflowError | WriteUnknownError;
declare class SizeUnknownError extends Error {
#private;
readonly name: string;
constructor(options: ErrorOptions);
static from(cause: unknown): SizeUnknownError;
}
declare class WriteUnknownError extends Error {
#private;
readonly name: string;
constructor(options: ErrorOptions);
static from(cause: unknown): WriteUnknownError;
}
declare class WriteUnderflowError extends Error {
#private;
readonly cursorOffset: number;

@@ -22,5 +39,5 @@ readonly cursorLength: number;

constructor(cursorOffset: number, cursorLength: number);
static from(cursor: Cursor): CursorWriteLenghtUnderflowError;
static from(cursor: Cursor): WriteUnderflowError;
}
export { type BinaryError, type BinaryReadError, type BinaryWriteError, CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError };
export { type ReadError, ReadUnderflowError, ReadUnknownError, type ReadWriteError, SizeUnknownError, type WriteError, WriteUnderflowError, WriteUnknownError };
import { Cursor } from '@hazae41/cursor';
import { Result } from '@hazae41/result';
import { Readable } from './readable.js';
import { BinaryWriteError, BinaryReadError } from './errors.js';
import { ReadError, WriteError } from './errors.js';
import { Writable } from './writable.js';

@@ -14,15 +14,22 @@

constructor(bytes: T);
[Symbol.dispose](): void;
/**
* View bytes into a new Opaque
* @param bytes
* @returns
*/
static new<T extends Uint8Array>(bytes: T): Opaque<T>;
/**
* Size this
* Copy bytes into a new Opaque
* @param bytes
* @returns
*/
trySize(): Result<number, never>;
static from(bytes: Uint8Array): Opaque<Uint8Array>;
sizeOrThrow(): number;
writeOrThrow(cursor: Cursor): void;
/**
* Write this
* @param cursor
* Transform this opaque into a binary data type
* @param readable
* @returns
*/
tryWrite(cursor: Cursor): Result<void, BinaryWriteError>;
readIntoOrThrow<T extends Readable.Infer<T>>(readable: T): Readable.Output<T>;
/**

@@ -33,3 +40,3 @@ * Transform this opaque into a binary data type

*/
tryReadInto<T extends Readable.Infer<T>>(readable: T): Result<Readable.ReadOutput<T>, Readable.ReadError<T> | BinaryReadError>;
tryReadInto<T extends Readable.Infer<T>>(readable: T): Result<Readable.Output<T>, ReadError>;
/**

@@ -40,3 +47,9 @@ * Create an opaque from a binary data type

*/
static tryWriteFrom<T extends Writable.Infer<T>>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | BinaryWriteError>;
static writeFromOrThrow(writable: Writable): Opaque;
/**
* Create an opaque from a binary data type
* @param writable
* @returns
*/
static tryWriteFrom(writable: Writable): Result<Opaque, WriteError>;
}

@@ -52,9 +65,16 @@ /**

*/
function tryRead(cursor: Cursor): Result<Opaque, BinaryReadError>;
function read(cursor: Cursor): Opaque<Uint8Array>;
function readOrThrow(cursor: Cursor): Opaque<Uint8Array>;
/**
* Perform unsafe zero-copy conversion to `Opaque` if `T instanceof Opaque`, else use `Opaque.tryWriteFrom`
* Perform unsafe zero-copy conversion to Opaque if already Opaque
* @param writable
* @returns
*/
function tryWriteFrom<T extends Writable.Infer<T>>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | BinaryWriteError>;
function writeFromOrThrow(writable: Writable): Opaque<any>;
/**
* Perform unsafe zero-copy conversion to Opaque if already Opaque
* @param writable
* @returns
*/
function tryWriteFrom(writable: Writable): Result<Opaque, WriteError>;
}

@@ -70,11 +90,18 @@ /**

*/
function tryRead(cursor: Cursor): Result<Opaque, BinaryReadError>;
function read(cursor: Cursor): Opaque<Uint8Array>;
function readOrThrow(cursor: Cursor): Opaque<Uint8Array>;
/**
* Perform safe copy conversion to `Opaque` if `T instanceof Opaque`, else use `Opaque.tryWriteFrom`
* Perform safe copy conversion to Opaque if already Opaque
* @param writable
* @returns
*/
function tryWriteFrom<T extends Writable.Infer<T>>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | BinaryWriteError>;
function writeFromOrThrow(writable: Writable): Opaque<Uint8Array>;
/**
* Perform safe copy conversion to Opaque if already Opaque
* @param writable
* @returns
*/
function tryWriteFrom(writable: Writable): Result<Opaque, WriteError>;
}
export { Opaque, SafeOpaque, UnsafeOpaque };
import { Cursor } from '@hazae41/cursor';
import { Result } from '@hazae41/result';
import { BinaryReadError } from './errors.js';
import { ReadUnknownError, ReadUnderflowError } from './errors.js';

@@ -8,3 +8,3 @@ /**

*/
interface Readable<ReadOutput = unknown, ReadError = unknown> {
interface Readable<Output = unknown> {
/**

@@ -14,12 +14,9 @@ * Read from a cursor

*/
tryRead(cursor: Cursor): Result<ReadOutput, ReadError>;
readOrThrow(cursor: Cursor): Output;
}
declare namespace Readable {
type Infer<T extends Readable> = Readable<Readable.ReadOutput<T>, Readable.ReadError<T>>;
type ReadOutput<T extends Readable> = T extends Readable<infer ReadOutput, unknown> ? ReadOutput : never;
type ReadError<T extends Readable> = T extends Readable<unknown, infer ReadError> ? ReadError : never;
type Infer<T extends Readable> = Readable<Readable.Output<T>>;
type Output<T extends Readable> = T extends Readable<infer O> ? O : never;
/**
* Try to read a binary data type from a cursor
* - on Ok: returns the Ok containing the BDT
* - on Err: rollback the offset, and returns the Err
* Call readOrThrow()
* @param readable

@@ -29,14 +26,36 @@ * @param cursor

*/
function tryReadOrRollback<T extends Infer<T>>(readable: T, cursor: Cursor): Result<ReadOutput<T>, ReadError<T>>;
function tryRead<T extends Infer<T>>(readable: T, cursor: Cursor): Result<Output<T>, ReadUnknownError>;
/**
* Read from bytes and check for underflow
*
* Underflow is when the cursor has remaining bytes; meaning we read less bytes than the expected length
* Call readOrThrow() but rollback the cursor on error
* @throws whatever readOrThrow() throws
* @param readable
* @param cursor
* @returns
*/
function readOrRollbackAndThrow<T extends Infer<T>>(readable: T, cursor: Cursor): Output<T>;
/**
* Call readOrThrow() but rollback the cursor on error
* @param readable
* @param cursor
* @returns
*/
function tryReadOrRollback<T extends Infer<T>>(readable: T, cursor: Cursor): Result<Output<T>, ReadUnknownError>;
/**
* Call readOrThrow() on the given bytes and check for underflow
* @throws whatever readOrThrow() throws
* @throws ReadUnderflowError on underflow
* @param readable
* @param bytes
* @returns
*/
function tryReadFromBytes<T extends Infer<T>>(readable: T, bytes: Uint8Array): Result<ReadOutput<T>, ReadError<T> | BinaryReadError>;
function readFromBytesOrThrow<T extends Infer<T>>(readable: T, bytes: Uint8Array): Output<T>;
/**
* Call readOrThrow() on the given bytes and check for underflow
* @param readable
* @param bytes
* @returns
*/
function tryReadFromBytes<T extends Infer<T>>(readable: T, bytes: Uint8Array): Result<Output<T>, ReadUnknownError | ReadUnderflowError>;
}
export { Readable };
import { Cursor } from '@hazae41/cursor';
import { Result } from '@hazae41/result';
import { BinaryWriteError } from './errors.js';
import { Ok, Err, Result } from '@hazae41/result';
import { SizeUnknownError, WriteUnknownError, WriteUnderflowError } from './errors.js';

@@ -8,7 +8,7 @@ /**

*/
interface Writable<SizeError = unknown, WriteError = unknown> {
interface Writable {
/**
* Compute the amount of bytes to allocate
*/
trySize(): Result<number, SizeError>;
sizeOrThrow(): number;
/**

@@ -18,18 +18,33 @@ * Write to a cursor

*/
tryWrite(cursor: Cursor): Result<void, WriteError>;
writeOrThrow(cursor: Cursor): void;
}
declare namespace Writable {
type Infer<T extends Writable> = Writable<SizeError<T>, WriteError<T>>;
type SizeError<T extends Writable> = T extends Writable<infer SizeError, unknown> ? SizeError : never;
type WriteError<T extends Writable> = T extends Writable<unknown, infer WriteError> ? WriteError : never;
/**
* Write to bytes and check for underflow
*
* Underflow is when the cursor has remaining bytes; meaning we have written less bytes than allocated
* Call sizeOrThrow()
* @param writable
* @returns
*/
function tryWriteToBytes<T extends Writable.Infer<T>>(writable: T): Result<Uint8Array, SizeError<T> | WriteError<T> | BinaryWriteError>;
function trySize(writable: Writable): Ok<number> | Err<SizeUnknownError>;
/**
* Call writeOrThrow()
* @param writable
* @param cursor
* @returns
*/
function tryWrite(writable: Writable, cursor: Cursor): Ok<void> | Err<WriteUnknownError>;
/**
* Call writeOrThrow() on sizeOrThrow()-sized bytes and check for underflow
* @throws whatever sizeOrThrow() or writeOrThrow() throws
* @param writable
* @returns
*/
function writeToBytesOrThrow(writable: Writable): Uint8Array;
/**
* Call writeOrThrow() on sizeOrThrow()-sized bytes and check for underflow
* @param writable
* @returns
*/
function tryWriteToBytes(writable: Writable): Result<Uint8Array, SizeUnknownError | WriteUnknownError | WriteUnderflowError>;
}
export { Writable };
export { Empty } from './binary/empty.js';
export { BinaryError, BinaryReadError, BinaryWriteError, CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from './binary/errors.js';
export { ReadError, ReadUnderflowError, ReadUnknownError, ReadWriteError, SizeUnknownError, WriteError, WriteUnderflowError, WriteUnknownError } from './binary/errors.js';
export { Opaque, SafeOpaque, UnsafeOpaque } from './binary/opaque.js';
export { Readable } from './binary/readable.js';
export { Writable } from './binary/writable.js';
{
"type": "module",
"name": "@hazae41/binary",
"version": "1.3.1",
"version": "1.3.2",
"description": "Zero-copy binary data types",

@@ -25,14 +25,14 @@ "homepage": "https://github.com/hazae41/binary",

"dependencies": {
"@hazae41/bytes": "^1.2.1",
"@hazae41/cursor": "^1.1.26",
"@hazae41/result": "^1.1.2"
"@hazae41/bytes": "^1.2.6",
"@hazae41/cursor": "^1.2.1",
"@hazae41/result": "^1.1.8"
},
"devDependencies": {
"@hazae41/phobos": "^1.0.10",
"@rollup/plugin-typescript": "^11.1.3",
"@types/node": "^20.5.9",
"rimraf": "^5.0.1",
"rollup": "^3.29.0",
"rollup-plugin-dts": "^6.0.1",
"rollup-plugin-node-externals": "^6.1.1",
"@hazae41/rimraf": "^1.0.1",
"@rollup/plugin-typescript": "^11.1.5",
"@types/node": "^20.8.10",
"rollup": "^4.3.0",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-node-externals": "^6.1.2",
"typescript": "^5.2.2"

@@ -39,0 +39,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc