@hazae41/binary
Advanced tools
| 'use strict'; | ||
| class CursorReadLengthUnderflowError extends Error { | ||
| cursor; | ||
| #class = CursorReadLengthUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after read`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| class CursorWriteLenghtUnderflowError extends Error { | ||
| cursor; | ||
| #class = CursorWriteLenghtUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after write`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| exports.CursorReadLengthUnderflowError = CursorReadLengthUnderflowError; | ||
| exports.CursorWriteLenghtUnderflowError = CursorWriteLenghtUnderflowError; | ||
| //# sourceMappingURL=errors.cjs.map |
| {"version":3,"file":"errors.cjs","sources":["../../../../src/mods/binary/errors.ts"],"sourcesContent":["import { Cursor, CursorReadError, CursorWriteError } from \"@hazae41/cursor\"\n\nexport type BinaryError =\n | BinaryReadError\n | BinaryWriteError\n\nexport type BinaryReadError =\n | CursorReadLengthUnderflowError\n | CursorReadError\n\nexport class CursorReadLengthUnderflowError extends Error {\n readonly #class = CursorReadLengthUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after read`)\n }\n}\n\nexport type BinaryWriteError =\n | CursorWriteLenghtUnderflowError\n | CursorWriteError\n\nexport class CursorWriteLenghtUnderflowError extends Error {\n readonly #class = CursorWriteLenghtUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after write`)\n }\n}"],"names":[],"mappings":";;AAUM,MAAO,8BAA+B,SAAQ,KAAK,CAAA;AAI5C,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,8BAA8B,CAAA;AAEhD,IAAA,WAAA,CACW,MAAc,EAAA;AAEvB,QAAA,KAAK,CAAC,CAAc,WAAA,EAAA,MAAM,CAAC,SAAS,CAAA,2BAAA,CAA6B,CAAC,CAAA;QAFzD,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;KAGxB;AACF,CAAA;AAMK,MAAO,+BAAgC,SAAQ,KAAK,CAAA;AAI7C,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,+BAA+B,CAAA;AAEjD,IAAA,WAAA,CACW,MAAc,EAAA;AAEvB,QAAA,KAAK,CAAC,CAAc,WAAA,EAAA,MAAM,CAAC,SAAS,CAAA,4BAAA,CAA8B,CAAC,CAAA;QAF1D,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;KAGxB;AACF;;;;;"} |
| class CursorReadLengthUnderflowError extends Error { | ||
| cursor; | ||
| #class = CursorReadLengthUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after read`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| class CursorWriteLenghtUnderflowError extends Error { | ||
| cursor; | ||
| #class = CursorWriteLenghtUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after write`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| export { CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError }; | ||
| //# sourceMappingURL=errors.mjs.map |
| {"version":3,"file":"errors.mjs","sources":["../../../../src/mods/binary/errors.ts"],"sourcesContent":["import { Cursor, CursorReadError, CursorWriteError } from \"@hazae41/cursor\"\n\nexport type BinaryError =\n | BinaryReadError\n | BinaryWriteError\n\nexport type BinaryReadError =\n | CursorReadLengthUnderflowError\n | CursorReadError\n\nexport class CursorReadLengthUnderflowError extends Error {\n readonly #class = CursorReadLengthUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after read`)\n }\n}\n\nexport type BinaryWriteError =\n | CursorWriteLenghtUnderflowError\n | CursorWriteError\n\nexport class CursorWriteLenghtUnderflowError extends Error {\n readonly #class = CursorWriteLenghtUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after write`)\n }\n}"],"names":[],"mappings":"AAUM,MAAO,8BAA+B,SAAQ,KAAK,CAAA;AAI5C,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,8BAA8B,CAAA;AAEhD,IAAA,WAAA,CACW,MAAc,EAAA;AAEvB,QAAA,KAAK,CAAC,CAAc,WAAA,EAAA,MAAM,CAAC,SAAS,CAAA,2BAAA,CAA6B,CAAC,CAAA;QAFzD,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;KAGxB;AACF,CAAA;AAMK,MAAO,+BAAgC,SAAQ,KAAK,CAAA;AAI7C,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,+BAA+B,CAAA;AAEjD,IAAA,WAAA,CACW,MAAc,EAAA;AAEvB,QAAA,KAAK,CAAC,CAAc,WAAA,EAAA,MAAM,CAAC,SAAS,CAAA,4BAAA,CAA8B,CAAC,CAAA;QAF1D,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;KAGxB;AACF;;;;"} |
| import { CursorReadError, Cursor, CursorWriteError } from '@hazae41/cursor'; | ||
| type BinaryError = BinaryReadError | BinaryWriteError; | ||
| type BinaryReadError = CursorReadLengthUnderflowError | CursorReadError; | ||
| declare class CursorReadLengthUnderflowError extends Error { | ||
| #private; | ||
| readonly cursor: Cursor; | ||
| constructor(cursor: Cursor); | ||
| } | ||
| type BinaryWriteError = CursorWriteLenghtUnderflowError | CursorWriteError; | ||
| declare class CursorWriteLenghtUnderflowError extends Error { | ||
| #private; | ||
| readonly cursor: Cursor; | ||
| constructor(cursor: Cursor); | ||
| } | ||
| export { BinaryError, BinaryReadError, BinaryWriteError, CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError }; |
@@ -5,2 +5,3 @@ 'use strict'; | ||
| var empty = require('./mods/binary/empty.cjs'); | ||
| var errors = require('./mods/binary/errors.cjs'); | ||
| var opaque = require('./mods/binary/opaque.cjs'); | ||
@@ -14,2 +15,4 @@ var readable = require('./mods/binary/readable.cjs'); | ||
| exports.Empty = empty.Empty; | ||
| exports.CursorReadLengthUnderflowError = errors.CursorReadLengthUnderflowError; | ||
| exports.CursorWriteLenghtUnderflowError = errors.CursorWriteLenghtUnderflowError; | ||
| exports.Opaque = opaque.Opaque; | ||
@@ -24,3 +27,2 @@ Object.defineProperty(exports, 'SafeOpaque', { | ||
| }); | ||
| exports.BinaryReadUnderflowError = readable.BinaryReadUnderflowError; | ||
| Object.defineProperty(exports, 'Readable', { | ||
@@ -30,3 +32,2 @@ enumerable: true, | ||
| }); | ||
| exports.BinaryWriteUnderflowError = writable.BinaryWriteUnderflowError; | ||
| Object.defineProperty(exports, 'Writable', { | ||
@@ -33,0 +34,0 @@ enumerable: true, |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} | ||
| {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} |
@@ -66,2 +66,7 @@ 'use strict'; | ||
| (function (UnsafeOpaque) { | ||
| /** | ||
| * Unsafe zero-copy read remaining bytes from cursor | ||
| * @param cursor | ||
| * @returns | ||
| */ | ||
| function tryRead(cursor) { | ||
@@ -71,2 +76,13 @@ return cursor.tryRead(cursor.remaining).mapSync(Opaque.new); | ||
| UnsafeOpaque.tryRead = tryRead; | ||
| /** | ||
| * Perform unsafe zero-copy conversion to `Opaque` if `T instanceof Opaque`, else use `Opaque.tryFrom` | ||
| * @param writable | ||
| * @returns | ||
| */ | ||
| function tryFrom(writable) { | ||
| if (writable instanceof Opaque) | ||
| return new result.Ok(Opaque.new(writable.bytes)); | ||
| return Opaque.tryFrom(writable); | ||
| } | ||
| UnsafeOpaque.tryFrom = tryFrom; | ||
| })(exports.UnsafeOpaque = exports.UnsafeOpaque || (exports.UnsafeOpaque = {})); | ||
@@ -78,2 +94,7 @@ /** | ||
| (function (SafeOpaque) { | ||
| /** | ||
| * Safe copy read remaining bytes from cursor | ||
| * @param cursor | ||
| * @returns | ||
| */ | ||
| function tryRead(cursor) { | ||
@@ -83,2 +104,11 @@ return cursor.tryRead(cursor.remaining).mapSync(Opaque.from); | ||
| SafeOpaque.tryRead = tryRead; | ||
| /** | ||
| * Perform safe, copy conversion to `Opaque` using `Opaque.tryFrom` | ||
| * @param writable | ||
| * @returns | ||
| */ | ||
| function tryFrom(writable) { | ||
| return Opaque.tryFrom(writable); | ||
| } | ||
| SafeOpaque.tryFrom = tryFrom; | ||
| })(exports.SafeOpaque = exports.SafeOpaque || (exports.SafeOpaque = {})); | ||
@@ -85,0 +115,0 @@ |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"opaque.cjs","sources":["../../../../src/mods/binary/opaque.ts"],"sourcesContent":["import { Bytes, Sized } from \"@hazae41/bytes\";\nimport { Cursor, CursorReadLengthOverflowError, CursorWriteLengthOverflowError } from \"@hazae41/cursor\";\nimport { Ok, Result } from \"@hazae41/result\";\nimport { BinaryReadUnderflowError, Readable } from \"mods/binary/readable.js\";\nimport { BinaryWriteUnderflowError, Writable } from \"./writable.js\";\n\nexport class Opaque<T extends Bytes = Bytes> {\n\n /**\n * A binary data type that just holds bytes\n * @param bytes \n */\n constructor(\n readonly bytes: T\n ) { }\n\n static new<T extends Bytes>(bytes: T) {\n return new Opaque(bytes)\n }\n\n static empty() {\n return new Opaque(Bytes.alloc(0))\n }\n\n static alloc<N extends number>(length: N) {\n return new Opaque(Bytes.alloc(length))\n }\n\n static allocUnsafe<N extends number>(length: N) {\n return new Opaque(Bytes.allocUnsafe(length))\n }\n\n static from<N extends number>(sized: Sized<number, N>) {\n return new Opaque(Bytes.from(sized))\n }\n\n static random<N extends number>(length: N) {\n return new Opaque(Bytes.random(length))\n }\n\n tryPrepare(): Result<this, never> {\n return new Ok(this)\n }\n\n trySize(): Result<number, never> {\n return new Ok(this.bytes.length)\n }\n\n tryWrite(cursor: Cursor): Result<void, CursorWriteLengthOverflowError> {\n return cursor.tryWrite(this.bytes)\n }\n\n /**\n * Transform this opaque into a binary data type\n * @param readable \n * @returns \n */\n tryInto<T extends Readable>(readable: T): Result<Readable.ReadOutput<T>, Readable.ReadError<T> | BinaryReadUnderflowError> {\n return Readable.tryReadFromBytes(readable, this.bytes)\n }\n\n /**\n * Create an opaque from a binary data type\n * @param writable \n * @returns \n */\n static tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | BinaryWriteUnderflowError> {\n return Writable.tryWriteToBytes(writable).mapSync(Opaque.new)\n }\n\n}\n\n/**\n * Read an opaque by viewing bytes\n */\nexport namespace UnsafeOpaque {\n\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(Opaque.new)\n }\n\n}\n\n/**\n * Read an opaque by copying bytes\n */\nexport namespace SafeOpaque {\n\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(Opaque.from)\n }\n\n}"],"names":["Bytes","Ok","readable","Readable","writable","Writable","UnsafeOpaque","SafeOpaque"],"mappings":";;;;;;;MAMa,MAAM,CAAA;AAON,IAAA,KAAA,CAAA;AALX;;;AAGG;AACH,IAAA,WAAA,CACW,KAAQ,EAAA;QAAR,IAAK,CAAA,KAAA,GAAL,KAAK,CAAG;KACd;IAEL,OAAO,GAAG,CAAkB,KAAQ,EAAA;AAClC,QAAA,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,CAAA;KACzB;AAED,IAAA,OAAO,KAAK,GAAA;QACV,OAAO,IAAI,MAAM,CAACA,WAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;KAClC;IAED,OAAO,KAAK,CAAmB,MAAS,EAAA;QACtC,OAAO,IAAI,MAAM,CAACA,WAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;KACvC;IAED,OAAO,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gBA,oBAAY,GAAZA,oBAAY,KAAZA,oBAAY,GAM5B,EAAA,CAAA,CAAA,CAAA;AAED;;AAEG;AACcC,4BAMhB;AAND,CAAA,UAAiB,UAAU,EAAA;IAEzB,SAAgB,OAAO,CAAC,MAAc,EAAA;AACpC,QAAA,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;KAC7D;AAFe,IAAA,UAAA,CAAA,OAAO,UAEtB,CAAA;AAEH,CAAC,EANgBA,kBAAU,GAAVA,kBAAU,KAAVA,kBAAU,GAM1B,EAAA,CAAA,CAAA;;;;"} | ||
| {"version":3,"file":"opaque.cjs","sources":["../../../../src/mods/binary/opaque.ts"],"sourcesContent":["import { Bytes, Sized } from \"@hazae41/bytes\";\nimport { Cursor, CursorReadLengthOverflowError, CursorWriteLengthOverflowError } from \"@hazae41/cursor\";\nimport { Ok, Result } from \"@hazae41/result\";\nimport { Readable } from \"mods/binary/readable.js\";\nimport { CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from \"./errors.js\";\nimport { Writable } from \"./writable.js\";\n\nexport class Opaque<T extends Bytes = Bytes> {\n\n /**\n * A binary data type that just holds bytes\n * @param bytes \n */\n constructor(\n readonly bytes: T\n ) { }\n\n static new<T extends Bytes>(bytes: T) {\n return new Opaque(bytes)\n }\n\n static empty() {\n return new Opaque(Bytes.alloc(0))\n }\n\n static alloc<N extends number>(length: N) {\n return new Opaque(Bytes.alloc(length))\n }\n\n static allocUnsafe<N extends number>(length: N) {\n return new Opaque(Bytes.allocUnsafe(length))\n }\n\n static from<N extends number>(sized: Sized<number, N>) {\n return new Opaque(Bytes.from(sized))\n }\n\n static random<N extends number>(length: N) {\n return new Opaque(Bytes.random(length))\n }\n\n tryPrepare(): Result<this, never> {\n return new Ok(this)\n }\n\n trySize(): Result<number, never> {\n return new Ok(this.bytes.length)\n }\n\n tryWrite(cursor: Cursor): Result<void, CursorWriteLengthOverflowError> {\n return cursor.tryWrite(this.bytes)\n }\n\n /**\n * Transform this opaque into a binary data type\n * @param readable \n * @returns \n */\n tryInto<T extends Readable>(readable: T): Result<Readable.ReadOutput<T>, Readable.ReadError<T> | CursorReadLengthUnderflowError> {\n return Readable.tryReadFromBytes(readable, this.bytes)\n }\n\n /**\n * Create an opaque from a binary data type\n * @param writable \n * @returns \n */\n static tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError> {\n return Writable.tryWriteToBytes(writable).mapSync(Opaque.new)\n }\n\n}\n\n/**\n * Read an opaque by viewing bytes\n */\nexport namespace UnsafeOpaque {\n\n /**\n * Unsafe zero-copy read remaining bytes from cursor\n * @param cursor \n * @returns \n */\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(Opaque.new)\n }\n\n /**\n * Perform unsafe zero-copy conversion to `Opaque` if `T instanceof Opaque`, else use `Opaque.tryFrom`\n * @param writable \n * @returns \n */\n export function tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError> {\n if (writable instanceof Opaque)\n return new Ok(Opaque.new(writable.bytes))\n return Opaque.tryFrom(writable)\n }\n\n}\n\n/**\n * Read an opaque by copying bytes\n */\nexport namespace SafeOpaque {\n\n /**\n * Safe copy read remaining bytes from cursor\n * @param cursor \n * @returns \n */\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(Opaque.from)\n }\n\n /**\n * Perform safe, copy conversion to `Opaque` using `Opaque.tryFrom`\n * @param writable \n * @returns \n */\n export function tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError> {\n return Opaque.tryFrom(writable)\n }\n\n\n}"],"names":["Bytes","Ok","readable","Readable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W,EAAA;AACrD,QAAA,OAAO,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;KAChC;AAFe,IAAA,UAAA,CAAA,OAAO,UAEtB,CAAA;AAGH,CAAC,EArBgBA,kBAAU,GAAVA,kBAAU,KAAVA,kBAAU,GAqB1B,EAAA,CAAA,CAAA;;;;"} |
@@ -5,11 +5,4 @@ 'use strict'; | ||
| var result = require('@hazae41/result'); | ||
| var errors = require('./errors.cjs'); | ||
| class BinaryReadUnderflowError extends Error { | ||
| cursor; | ||
| #class = BinaryReadUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after read`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| exports.Readable = void 0; | ||
@@ -47,3 +40,3 @@ (function (Readable) { | ||
| if (cursor$1.remaining) | ||
| return new result.Err(new BinaryReadUnderflowError(cursor$1)); | ||
| return new result.Err(new errors.CursorReadLengthUnderflowError(cursor$1)); | ||
| return result$1; | ||
@@ -53,4 +46,2 @@ } | ||
| })(exports.Readable = exports.Readable || (exports.Readable = {})); | ||
| exports.BinaryReadUnderflowError = BinaryReadUnderflowError; | ||
| //# sourceMappingURL=readable.cjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"readable.cjs","sources":["../../../../src/mods/binary/readable.ts"],"sourcesContent":["import { Bytes } from \"@hazae41/bytes\";\nimport { Cursor, CursorReadError } from \"@hazae41/cursor\";\nimport { Err, Result } from \"@hazae41/result\";\n\nexport type BinaryReadError =\n | BinaryReadUnderflowError\n | CursorReadError\n\nexport class BinaryReadUnderflowError extends Error {\n readonly #class = BinaryReadUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after read`)\n }\n}\n\n/**\n * A readable binary data type\n */\nexport interface Readable<ReadOutput = unknown, ReadError = unknown> {\n\n /**\n * Read from a cursor\n * @param cursor \n */\n tryRead(cursor: Cursor): Result<Readable.ReadOutput<this>, Readable.ReadError<this>>\n\n}\n\nexport namespace Readable {\n\n export type ReadOutput<T extends Readable> = T extends Readable<infer ReadOutput, unknown> ? ReadOutput : never\n\n export type ReadError<T extends Readable> = T extends Readable<unknown, infer ReadError> ? ReadError : never\n\n /**\n * Try to read a binary data type from a cursor\n * - on Ok: returns the Ok containing the BDT\n * - on Err: rollback the offset, and returns the Err\n * @param readable \n * @param cursor \n * @returns \n */\n export function tryReadOrRollback<T extends Readable>(readable: T, cursor: Cursor): Result<ReadOutput<T>, ReadError<T>> {\n const offset = cursor.offset\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n cursor.offset = offset\n\n return result\n }\n\n /**\n * Read from bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we read less bytes than the expected length\n * @param readable \n * @param bytes \n * @returns \n */\n export function tryReadFromBytes<T extends Readable>(readable: T, bytes: Bytes): Result<ReadOutput<T>, ReadError<T> | BinaryReadUnderflowError> {\n const cursor = new Cursor(bytes)\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new BinaryReadUnderflowError(cursor))\n\n return result\n }\n\n}"],"names":["Readable","cursor","Cursor","result","Err"],"mappings":";;;;;AAQM,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AAItC,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,wBAAwB,CAAA;AAE1C,IAAA,WAAA,CACW,MAAc,EAAA;AAEvB,QAAA,KAAK,CAAC,CAAc,WAAA,EAAA,MAAM,CAAC,SAAS,CAAA,2BAAA,CAA6B,CAAC,CAAA;QAFzD,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;KAGxB;AACF,CAAA;AAegBA,0BA6ChB;AA7CD,CAAA,UAAiB,QAAQ,EAAA;AAMvB;;;;;;;AAOG;AACH,IAAA,SAAgB,iBAAiB,CAAqB,QAAW,EAAE,MAAc,EAAA;AAC/E,QAAA,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;QAC5B,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QAEvC,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,YAAA,MAAM,CAAC,MAAM,GAAG,MAAM,CAAA;AAExB,QAAA,OAAO,MAAM,CAAA;KACd;AARe,IAAA,QAAA,CAAA,iBAAiB,oBAQhC,CAAA;AAED;;;;;;;AAOG;AACH,IAAA,SAAgB,gBAAgB,CAAqB,QAAW,EAAE,KAAY,EAAA;AAC5E,QAAA,MAAMC,QAAM,GAAG,IAAIC,aAAM,CAAC,KAAK,CAAC,CAAA;QAChC,MAAMC,QAAM,GAAG,QAAQ,CAAC,OAAO,CAACF,QAAM,CAAC,CAAA;QAEvC,IAAIE,QAAM,CAAC,KAAK,EAAE;AAChB,YAAA,OAAOA,QAAM,CAAA;QAEf,IAAIF,QAAM,CAAC,SAAS;YAClB,OAAO,IAAIG,UAAG,CAAC,IAAI,wBAAwB,CAACH,QAAM,CAAC,CAAC,CAAA;AAEtD,QAAA,OAAOE,QAAM,CAAA;KACd;AAXe,IAAA,QAAA,CAAA,gBAAgB,mBAW/B,CAAA;AAEH,CAAC,EA7CgBH,gBAAQ,GAARA,gBAAQ,KAARA,gBAAQ,GA6CxB,EAAA,CAAA,CAAA;;;;"} | ||
| {"version":3,"file":"readable.cjs","sources":["../../../../src/mods/binary/readable.ts"],"sourcesContent":["import { Bytes } from \"@hazae41/bytes\";\nimport { Cursor } from \"@hazae41/cursor\";\nimport { Err, Result } from \"@hazae41/result\";\nimport { CursorReadLengthUnderflowError } from \"./errors.js\";\n\n/**\n * A readable binary data type\n */\nexport interface Readable<ReadOutput = unknown, ReadError = unknown> {\n\n /**\n * Read from a cursor\n * @param cursor \n */\n tryRead(cursor: Cursor): Result<Readable.ReadOutput<this>, Readable.ReadError<this>>\n\n}\n\nexport namespace Readable {\n\n export type ReadOutput<T extends Readable> = T extends Readable<infer ReadOutput, unknown> ? ReadOutput : never\n\n export type ReadError<T extends Readable> = T extends Readable<unknown, infer ReadError> ? ReadError : never\n\n /**\n * Try to read a binary data type from a cursor\n * - on Ok: returns the Ok containing the BDT\n * - on Err: rollback the offset, and returns the Err\n * @param readable \n * @param cursor \n * @returns \n */\n export function tryReadOrRollback<T extends Readable>(readable: T, cursor: Cursor): Result<ReadOutput<T>, ReadError<T>> {\n const offset = cursor.offset\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n cursor.offset = offset\n\n return result\n }\n\n /**\n * Read from bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we read less bytes than the expected length\n * @param readable \n * @param bytes \n * @returns \n */\n export function tryReadFromBytes<T extends Readable>(readable: T, bytes: Bytes): Result<ReadOutput<T>, ReadError<T> | CursorReadLengthUnderflowError> {\n const cursor = new Cursor(bytes)\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new CursorReadLengthUnderflowError(cursor))\n\n return result\n }\n\n}"],"names":["Readable","cursor","Cursor","result","Err","CursorReadLengthUnderflowError"],"mappings":";;;;;;AAkBiBA,0BA6ChB;AA7CD,CAAA,UAAiB,QAAQ,EAAA;AAMvB;;;;;;;AAOG;AACH,IAAA,SAAgB,iBAAiB,CAAqB,QAAW,EAAE,MAAc,EAAA;AAC/E,QAAA,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;QAC5B,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QAEvC,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,YAAA,MAAM,CAAC,MAAM,GAAG,MAAM,CAAA;AAExB,QAAA,OAAO,MAAM,CAAA;KACd;AARe,IAAA,QAAA,CAAA,iBAAiB,oBAQhC,CAAA;AAED;;;;;;;AAOG;AACH,IAAA,SAAgB,gBAAgB,CAAqB,QAAW,EAAE,KAAY,EAAA;AAC5E,QAAA,MAAMC,QAAM,GAAG,IAAIC,aAAM,CAAC,KAAK,CAAC,CAAA;QAChC,MAAMC,QAAM,GAAG,QAAQ,CAAC,OAAO,CAACF,QAAM,CAAC,CAAA;QAEvC,IAAIE,QAAM,CAAC,KAAK,EAAE;AAChB,YAAA,OAAOA,QAAM,CAAA;QAEf,IAAIF,QAAM,CAAC,SAAS;YAClB,OAAO,IAAIG,UAAG,CAAC,IAAIC,qCAA8B,CAACJ,QAAM,CAAC,CAAC,CAAA;AAE5D,QAAA,OAAOE,QAAM,CAAA;KACd;AAXe,IAAA,QAAA,CAAA,gBAAgB,mBAW/B,CAAA;AAEH,CAAC,EA7CgBH,gBAAQ,GAARA,gBAAQ,KAARA,gBAAQ,GA6CxB,EAAA,CAAA,CAAA;;"} |
@@ -5,11 +5,4 @@ 'use strict'; | ||
| var result = require('@hazae41/result'); | ||
| var errors = require('./errors.cjs'); | ||
| class BinaryWriteUnderflowError extends Error { | ||
| cursor; | ||
| #class = BinaryWriteUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after write`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| exports.Writable = void 0; | ||
@@ -33,3 +26,3 @@ (function (Writable) { | ||
| if (cursor$1.remaining) | ||
| return new result.Err(new BinaryWriteUnderflowError(cursor$1)); | ||
| return new result.Err(new errors.CursorWriteLenghtUnderflowError(cursor$1)); | ||
| return new result.Ok(cursor$1.bytes); | ||
@@ -39,4 +32,2 @@ } | ||
| })(exports.Writable = exports.Writable || (exports.Writable = {})); | ||
| exports.BinaryWriteUnderflowError = BinaryWriteUnderflowError; | ||
| //# sourceMappingURL=writable.cjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"writable.cjs","sources":["../../../../src/mods/binary/writable.ts"],"sourcesContent":["import { Bytes } from \"@hazae41/bytes\"\nimport { Cursor, CursorWriteError } from \"@hazae41/cursor\"\nimport { Err, Ok, Result } from \"@hazae41/result\"\n\nexport type BinaryWriteError =\n | BinaryWriteUnderflowError\n | CursorWriteError\n\nexport class BinaryWriteUnderflowError extends Error {\n readonly #class = BinaryWriteUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after write`)\n }\n}\n\n/**\n * A writable binary data type\n */\nexport interface Writable<SizeError = unknown, WriteError = unknown> {\n\n /**\n * Compute the amount of bytes to allocate\n */\n trySize(): Result<number, Writable.SizeError<this>>\n\n /**\n * Write to a cursor\n * @param cursor \n */\n tryWrite(cursor: Cursor): Result<void, Writable.WriteError<this>>\n\n}\n\nexport namespace Writable {\n\n export type SizeError<T extends Writable> = T extends Writable<infer SizeError, unknown> ? SizeError : never\n\n export type WriteError<T extends Writable> = T extends Writable<unknown, infer WriteError> ? WriteError : never\n\n /**\n * Write to bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we have written less bytes than allocated\n * @param writable \n * @returns \n */\n export function tryWriteToBytes<T extends Writable>(writable: T): Result<Bytes, SizeError<T> | WriteError<T> | BinaryWriteUnderflowError> {\n const size = writable.trySize()\n\n if (size.isErr())\n return size\n\n const cursor = Cursor.allocUnsafe(size.inner)\n const result = writable.tryWrite(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new BinaryWriteUnderflowError(cursor))\n\n return new Ok(cursor.bytes)\n }\n\n}"],"names":["Writable","cursor","Cursor","result","Err","Ok"],"mappings":";;;;;AAQM,MAAO,yBAA0B,SAAQ,KAAK,CAAA;AAIvC,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,yBAAyB,CAAA;AAE3C,IAAA,WAAA,CACW,MAAc,EAAA;AAEvB,QAAA,KAAK,CAAC,CAAc,WAAA,EAAA,MAAM,CAAC,SAAS,CAAA,4BAAA,CAA8B,CAAC,CAAA;QAF1D,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;KAGxB;AACF,CAAA;AAoBgBA,0BA+BhB;AA/BD,CAAA,UAAiB,QAAQ,EAAA;AAMvB;;;;;;AAMG;IACH,SAAgB,eAAe,CAAqB,QAAW,EAAA;AAC7D,QAAA,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAA;QAE/B,IAAI,IAAI,CAAC,KAAK,EAAE;AACd,YAAA,OAAO,IAAI,CAAA;QAEb,MAAMC,QAAM,GAAGC,aAAM,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC7C,MAAMC,QAAM,GAAG,QAAQ,CAAC,QAAQ,CAACF,QAAM,CAAC,CAAA;QAExC,IAAIE,QAAM,CAAC,KAAK,EAAE;AAChB,YAAA,OAAOA,QAAM,CAAA;QAEf,IAAIF,QAAM,CAAC,SAAS;YAClB,OAAO,IAAIG,UAAG,CAAC,IAAI,yBAAyB,CAACH,QAAM,CAAC,CAAC,CAAA;AAEvD,QAAA,OAAO,IAAII,SAAE,CAACJ,QAAM,CAAC,KAAK,CAAC,CAAA;KAC5B;AAhBe,IAAA,QAAA,CAAA,eAAe,kBAgB9B,CAAA;AAEH,CAAC,EA/BgBD,gBAAQ,GAARA,gBAAQ,KAARA,gBAAQ,GA+BxB,EAAA,CAAA,CAAA;;;;"} | ||
| {"version":3,"file":"writable.cjs","sources":["../../../../src/mods/binary/writable.ts"],"sourcesContent":["import { Bytes } from \"@hazae41/bytes\"\nimport { Cursor } from \"@hazae41/cursor\"\nimport { Err, Ok, Result } from \"@hazae41/result\"\nimport { CursorWriteLenghtUnderflowError } from \"./errors.js\"\n\n/**\n * A writable binary data type\n */\nexport interface Writable<SizeError = unknown, WriteError = unknown> {\n\n /**\n * Compute the amount of bytes to allocate\n */\n trySize(): Result<number, Writable.SizeError<this>>\n\n /**\n * Write to a cursor\n * @param cursor \n */\n tryWrite(cursor: Cursor): Result<void, Writable.WriteError<this>>\n\n}\n\nexport namespace Writable {\n\n export type SizeError<T extends Writable> = T extends Writable<infer SizeError, unknown> ? SizeError : never\n\n export type WriteError<T extends Writable> = T extends Writable<unknown, infer WriteError> ? WriteError : never\n\n /**\n * Write to bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we have written less bytes than allocated\n * @param writable \n * @returns \n */\n export function tryWriteToBytes<T extends Writable>(writable: T): Result<Bytes, SizeError<T> | WriteError<T> | CursorWriteLenghtUnderflowError> {\n const size = writable.trySize()\n\n if (size.isErr())\n return size\n\n const cursor = Cursor.allocUnsafe(size.inner)\n const result = writable.tryWrite(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new CursorWriteLenghtUnderflowError(cursor))\n\n return new Ok(cursor.bytes)\n }\n\n}"],"names":["Writable","cursor","Cursor","result","Err","CursorWriteLenghtUnderflowError","Ok"],"mappings":";;;;;;AAuBiBA,0BA+BhB;AA/BD,CAAA,UAAiB,QAAQ,EAAA;AAMvB;;;;;;AAMG;IACH,SAAgB,eAAe,CAAqB,QAAW,EAAA;AAC7D,QAAA,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAA;QAE/B,IAAI,IAAI,CAAC,KAAK,EAAE;AACd,YAAA,OAAO,IAAI,CAAA;QAEb,MAAMC,QAAM,GAAGC,aAAM,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC7C,MAAMC,QAAM,GAAG,QAAQ,CAAC,QAAQ,CAACF,QAAM,CAAC,CAAA;QAExC,IAAIE,QAAM,CAAC,KAAK,EAAE;AAChB,YAAA,OAAOA,QAAM,CAAA;QAEf,IAAIF,QAAM,CAAC,SAAS;YAClB,OAAO,IAAIG,UAAG,CAAC,IAAIC,sCAA+B,CAACJ,QAAM,CAAC,CAAC,CAAA;AAE7D,QAAA,OAAO,IAAIK,SAAE,CAACL,QAAM,CAAC,KAAK,CAAC,CAAA;KAC5B;AAhBe,IAAA,QAAA,CAAA,eAAe,kBAgB9B,CAAA;AAEH,CAAC,EA/BgBD,gBAAQ,GAARA,gBAAQ,KAARA,gBAAQ,GA+BxB,EAAA,CAAA,CAAA;;"} |
| 'use strict'; | ||
| var empty = require('./binary/empty.cjs'); | ||
| var errors = require('./binary/errors.cjs'); | ||
| var opaque = require('./binary/opaque.cjs'); | ||
@@ -11,2 +12,4 @@ var readable = require('./binary/readable.cjs'); | ||
| exports.Empty = empty.Empty; | ||
| exports.CursorReadLengthUnderflowError = errors.CursorReadLengthUnderflowError; | ||
| exports.CursorWriteLenghtUnderflowError = errors.CursorWriteLenghtUnderflowError; | ||
| exports.Opaque = opaque.Opaque; | ||
@@ -21,3 +24,2 @@ Object.defineProperty(exports, 'SafeOpaque', { | ||
| }); | ||
| exports.BinaryReadUnderflowError = readable.BinaryReadUnderflowError; | ||
| Object.defineProperty(exports, 'Readable', { | ||
@@ -27,3 +29,2 @@ enumerable: true, | ||
| }); | ||
| exports.BinaryWriteUnderflowError = writable.BinaryWriteUnderflowError; | ||
| Object.defineProperty(exports, 'Writable', { | ||
@@ -30,0 +31,0 @@ enumerable: true, |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;"} | ||
| {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;"} |
| import * as index from './mods/index.mjs'; | ||
| export { index as Binary }; | ||
| export { Empty } from './mods/binary/empty.mjs'; | ||
| export { CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from './mods/binary/errors.mjs'; | ||
| export { Opaque, SafeOpaque, UnsafeOpaque } from './mods/binary/opaque.mjs'; | ||
| export { BinaryReadUnderflowError, Readable } from './mods/binary/readable.mjs'; | ||
| export { BinaryWriteUnderflowError, Writable } from './mods/binary/writable.mjs'; | ||
| export { Readable } from './mods/binary/readable.mjs'; | ||
| export { Writable } from './mods/binary/writable.mjs'; | ||
| //# sourceMappingURL=index.mjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"} | ||
| {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"} |
@@ -64,2 +64,7 @@ import { Bytes } from '@hazae41/bytes'; | ||
| (function (UnsafeOpaque) { | ||
| /** | ||
| * Unsafe zero-copy read remaining bytes from cursor | ||
| * @param cursor | ||
| * @returns | ||
| */ | ||
| function tryRead(cursor) { | ||
@@ -69,2 +74,13 @@ return cursor.tryRead(cursor.remaining).mapSync(Opaque.new); | ||
| UnsafeOpaque.tryRead = tryRead; | ||
| /** | ||
| * Perform unsafe zero-copy conversion to `Opaque` if `T instanceof Opaque`, else use `Opaque.tryFrom` | ||
| * @param writable | ||
| * @returns | ||
| */ | ||
| function tryFrom(writable) { | ||
| if (writable instanceof Opaque) | ||
| return new Ok(Opaque.new(writable.bytes)); | ||
| return Opaque.tryFrom(writable); | ||
| } | ||
| UnsafeOpaque.tryFrom = tryFrom; | ||
| })(UnsafeOpaque = UnsafeOpaque || (UnsafeOpaque = {})); | ||
@@ -76,2 +92,7 @@ /** | ||
| (function (SafeOpaque) { | ||
| /** | ||
| * Safe copy read remaining bytes from cursor | ||
| * @param cursor | ||
| * @returns | ||
| */ | ||
| function tryRead(cursor) { | ||
@@ -81,2 +102,11 @@ return cursor.tryRead(cursor.remaining).mapSync(Opaque.from); | ||
| SafeOpaque.tryRead = tryRead; | ||
| /** | ||
| * Perform safe, copy conversion to `Opaque` using `Opaque.tryFrom` | ||
| * @param writable | ||
| * @returns | ||
| */ | ||
| function tryFrom(writable) { | ||
| return Opaque.tryFrom(writable); | ||
| } | ||
| SafeOpaque.tryFrom = tryFrom; | ||
| })(SafeOpaque = SafeOpaque || (SafeOpaque = {})); | ||
@@ -83,0 +113,0 @@ |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"opaque.mjs","sources":["../../../../src/mods/binary/opaque.ts"],"sourcesContent":["import { Bytes, Sized } from \"@hazae41/bytes\";\nimport { Cursor, CursorReadLengthOverflowError, CursorWriteLengthOverflowError } from \"@hazae41/cursor\";\nimport { Ok, Result } from \"@hazae41/result\";\nimport { BinaryReadUnderflowError, Readable } from \"mods/binary/readable.js\";\nimport { BinaryWriteUnderflowError, Writable } from \"./writable.js\";\n\nexport class Opaque<T extends Bytes = Bytes> {\n\n /**\n * A binary data type that just holds bytes\n * @param bytes \n */\n constructor(\n readonly bytes: T\n ) { }\n\n static new<T extends Bytes>(bytes: T) {\n return new Opaque(bytes)\n }\n\n static empty() {\n return new Opaque(Bytes.alloc(0))\n }\n\n static alloc<N extends number>(length: N) {\n return new Opaque(Bytes.alloc(length))\n }\n\n static allocUnsafe<N extends number>(length: N) {\n return new Opaque(Bytes.allocUnsafe(length))\n }\n\n static from<N extends number>(sized: Sized<number, N>) {\n return new Opaque(Bytes.from(sized))\n }\n\n static random<N extends number>(length: N) {\n return new Opaque(Bytes.random(length))\n }\n\n tryPrepare(): Result<this, never> {\n return new Ok(this)\n }\n\n trySize(): Result<number, never> {\n return new Ok(this.bytes.length)\n }\n\n tryWrite(cursor: Cursor): Result<void, CursorWriteLengthOverflowError> {\n return cursor.tryWrite(this.bytes)\n }\n\n /**\n * Transform this opaque into a binary data type\n * @param readable \n * @returns \n */\n tryInto<T extends Readable>(readable: T): Result<Readable.ReadOutput<T>, Readable.ReadError<T> | BinaryReadUnderflowError> {\n return Readable.tryReadFromBytes(readable, this.bytes)\n }\n\n /**\n * Create an opaque from a binary data type\n * @param writable \n * @returns \n */\n static tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | BinaryWriteUnderflowError> {\n return Writable.tryWriteToBytes(writable).mapSync(Opaque.new)\n }\n\n}\n\n/**\n * Read an opaque by viewing bytes\n */\nexport namespace UnsafeOpaque {\n\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(Opaque.new)\n }\n\n}\n\n/**\n * Read an opaque by copying bytes\n */\nexport namespace SafeOpaque {\n\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(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CAAC,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;KACvD;AAED;;;;AAIG;IACH,OAAO,OAAO,CAAqB,QAAW,EAAA;AAC5C,QAAA,OAAO,QAAQ,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;KAC9D;AAEF,CAAA;AAED;;AAEG;AACG,IAAW,aAMhB;AAND,CAAA,UAAiB,YAAY,EAAA;IAE3B,SAAgB,OAAO,CAAC,MAAc,EAAA;AACpC,QAAA,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;KAC5D;AAFe,IAAA,YAAA,CAAA,OAAO,UAEtB,CAAA;AAEH,CAAC,EANgB,YAAY,GAAZ,YAAY,KAAZ,YAAY,GAM5B,EAAA,CAAA,CAAA,CAAA;AAED;;AAEG;AACG,IAAW,WAMhB;AAND,CAAA,UAAiB,UAAU,EAAA;IAEzB,SAAgB,OAAO,CAAC,MAAc,EAAA;AACpC,QAAA,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;KAC7D;AAFe,IAAA,UAAA,CAAA,OAAO,UAEtB,CAAA;AAEH,CAAC,EANgB,UAAU,GAAV,UAAU,KAAV,UAAU,GAM1B,EAAA,CAAA,CAAA;;;;"} | ||
| {"version":3,"file":"opaque.mjs","sources":["../../../../src/mods/binary/opaque.ts"],"sourcesContent":["import { Bytes, Sized } from \"@hazae41/bytes\";\nimport { Cursor, CursorReadLengthOverflowError, CursorWriteLengthOverflowError } from \"@hazae41/cursor\";\nimport { Ok, Result } from \"@hazae41/result\";\nimport { Readable } from \"mods/binary/readable.js\";\nimport { CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from \"./errors.js\";\nimport { Writable } from \"./writable.js\";\n\nexport class Opaque<T extends Bytes = Bytes> {\n\n /**\n * A binary data type that just holds bytes\n * @param bytes \n */\n constructor(\n readonly bytes: T\n ) { }\n\n static new<T extends Bytes>(bytes: T) {\n return new Opaque(bytes)\n }\n\n static empty() {\n return new Opaque(Bytes.alloc(0))\n }\n\n static alloc<N extends number>(length: N) {\n return new Opaque(Bytes.alloc(length))\n }\n\n static allocUnsafe<N extends number>(length: N) {\n return new Opaque(Bytes.allocUnsafe(length))\n }\n\n static from<N extends number>(sized: Sized<number, N>) {\n return new Opaque(Bytes.from(sized))\n }\n\n static random<N extends number>(length: N) {\n return new Opaque(Bytes.random(length))\n }\n\n tryPrepare(): Result<this, never> {\n return new Ok(this)\n }\n\n trySize(): Result<number, never> {\n return new Ok(this.bytes.length)\n }\n\n tryWrite(cursor: Cursor): Result<void, CursorWriteLengthOverflowError> {\n return cursor.tryWrite(this.bytes)\n }\n\n /**\n * Transform this opaque into a binary data type\n * @param readable \n * @returns \n */\n tryInto<T extends Readable>(readable: T): Result<Readable.ReadOutput<T>, Readable.ReadError<T> | CursorReadLengthUnderflowError> {\n return Readable.tryReadFromBytes(readable, this.bytes)\n }\n\n /**\n * Create an opaque from a binary data type\n * @param writable \n * @returns \n */\n static tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError> {\n return Writable.tryWriteToBytes(writable).mapSync(Opaque.new)\n }\n\n}\n\n/**\n * Read an opaque by viewing bytes\n */\nexport namespace UnsafeOpaque {\n\n /**\n * Unsafe zero-copy read remaining bytes from cursor\n * @param cursor \n * @returns \n */\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(Opaque.new)\n }\n\n /**\n * Perform unsafe zero-copy conversion to `Opaque` if `T instanceof Opaque`, else use `Opaque.tryFrom`\n * @param writable \n * @returns \n */\n export function tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError> {\n if (writable instanceof Opaque)\n return new Ok(Opaque.new(writable.bytes))\n return Opaque.tryFrom(writable)\n }\n\n}\n\n/**\n * Read an opaque by copying bytes\n */\nexport namespace SafeOpaque {\n\n /**\n * Safe copy read remaining bytes from cursor\n * @param cursor \n * @returns \n */\n export function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError> {\n return cursor.tryRead(cursor.remaining).mapSync(Opaque.from)\n }\n\n /**\n * Perform safe, copy conversion to `Opaque` using `Opaque.tryFrom`\n * @param writable \n * @returns \n */\n export function tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError> {\n return Opaque.tryFrom(writable)\n }\n\n\n}"],"names":[],"mappings":";;;;;MAOa,MAAM,CAAA;AAON,IAAA,KAAA,CAAA;AALX;;;AAGG;AACH,IAAA,WAAA,CACW,KAAQ,EAAA;QAAR,IAAK,CAAA,KAAA,GAAL,KAAK,CAAG;KACd;IAEL,OAAO,GAAG,CAAkB,KAAQ,EAAA;AAClC,QAAA,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,CAAA;KACzB;AAED,IAAA,OAAO,KAAK,GAAA;QACV,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;KAClC;IAED,OAAO,KAAK,CAAmB,MAAS,EAAA;QACtC,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;KACvC;IAED,OAAO,WAAW,CAAmB,MAAS,EAAA;QAC5C,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;KAC7C;IAED,OAAO,IAAI,CAAmB,KAAuB,EAAA;QACnD,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;KACrC;IAED,OAAO,MAAM,CAAmB,M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,YAAA,CAAA,OAAO,UAItB,CAAA;AAEH,CAAC,EAtBgB,YAAY,GAAZ,YAAY,KAAZ,YAAY,GAsB5B,EAAA,CAAA,CAAA,CAAA;AAED;;AAEG;AACG,IAAW,WAqBhB;AArBD,CAAA,UAAiB,UAAU,EAAA;AAEzB;;;;AAIG;IACH,SAAgB,OAAO,CAAC,MAAc,EAAA;AACpC,QAAA,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;KAC7D;AAFe,IAAA,UAAA,CAAA,OAAO,UAEtB,CAAA;AAED;;;;AAIG;IACH,SAAgB,OAAO,CAAqB,QAAW,EAAA;AACrD,QAAA,OAAO,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;KAChC;AAFe,IAAA,UAAA,CAAA,OAAO,UAEtB,CAAA;AAGH,CAAC,EArBgB,UAAU,GAAV,UAAU,KAAV,UAAU,GAqB1B,EAAA,CAAA,CAAA;;;;"} |
| import { Cursor } from '@hazae41/cursor'; | ||
| import { Err } from '@hazae41/result'; | ||
| import { CursorReadLengthUnderflowError } from './errors.mjs'; | ||
| class BinaryReadUnderflowError extends Error { | ||
| cursor; | ||
| #class = BinaryReadUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after read`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| var Readable; | ||
@@ -44,3 +37,3 @@ (function (Readable) { | ||
| if (cursor.remaining) | ||
| return new Err(new BinaryReadUnderflowError(cursor)); | ||
| return new Err(new CursorReadLengthUnderflowError(cursor)); | ||
| return result; | ||
@@ -51,3 +44,3 @@ } | ||
| export { BinaryReadUnderflowError, Readable }; | ||
| export { Readable }; | ||
| //# sourceMappingURL=readable.mjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"readable.mjs","sources":["../../../../src/mods/binary/readable.ts"],"sourcesContent":["import { Bytes } from \"@hazae41/bytes\";\nimport { Cursor, CursorReadError } from \"@hazae41/cursor\";\nimport { Err, Result } from \"@hazae41/result\";\n\nexport type BinaryReadError =\n | BinaryReadUnderflowError\n | CursorReadError\n\nexport class BinaryReadUnderflowError extends Error {\n readonly #class = BinaryReadUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after read`)\n }\n}\n\n/**\n * A readable binary data type\n */\nexport interface Readable<ReadOutput = unknown, ReadError = unknown> {\n\n /**\n * Read from a cursor\n * @param cursor \n */\n tryRead(cursor: Cursor): Result<Readable.ReadOutput<this>, Readable.ReadError<this>>\n\n}\n\nexport namespace Readable {\n\n export type ReadOutput<T extends Readable> = T extends Readable<infer ReadOutput, unknown> ? ReadOutput : never\n\n export type ReadError<T extends Readable> = T extends Readable<unknown, infer ReadError> ? ReadError : never\n\n /**\n * Try to read a binary data type from a cursor\n * - on Ok: returns the Ok containing the BDT\n * - on Err: rollback the offset, and returns the Err\n * @param readable \n * @param cursor \n * @returns \n */\n export function tryReadOrRollback<T extends Readable>(readable: T, cursor: Cursor): Result<ReadOutput<T>, ReadError<T>> {\n const offset = cursor.offset\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n cursor.offset = offset\n\n return result\n }\n\n /**\n * Read from bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we read less bytes than the expected length\n * @param readable \n * @param bytes \n * @returns \n */\n export function tryReadFromBytes<T extends Readable>(readable: T, bytes: Bytes): Result<ReadOutput<T>, ReadError<T> | BinaryReadUnderflowError> {\n const cursor = new Cursor(bytes)\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new BinaryReadUnderflowError(cursor))\n\n return result\n }\n\n}"],"names":[],"mappings":";;;AAQM,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AAItC,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,wBAAwB,CAAA;AAE1C,IAAA,WAAA,CACW,M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\nimport { Cursor } from \"@hazae41/cursor\";\nimport { Err, Result } from \"@hazae41/result\";\nimport { CursorReadLengthUnderflowError } from \"./errors.js\";\n\n/**\n * A readable binary data type\n */\nexport interface Readable<ReadOutput = unknown, ReadError = unknown> {\n\n /**\n * Read from a cursor\n * @param cursor \n */\n tryRead(cursor: Cursor): Result<Readable.ReadOutput<this>, Readable.ReadError<this>>\n\n}\n\nexport namespace Readable {\n\n export type ReadOutput<T extends Readable> = T extends Readable<infer ReadOutput, unknown> ? ReadOutput : never\n\n export type ReadError<T extends Readable> = T extends Readable<unknown, infer ReadError> ? ReadError : never\n\n /**\n * Try to read a binary data type from a cursor\n * - on Ok: returns the Ok containing the BDT\n * - on Err: rollback the offset, and returns the Err\n * @param readable \n * @param cursor \n * @returns \n */\n export function tryReadOrRollback<T extends Readable>(readable: T, cursor: Cursor): Result<ReadOutput<T>, ReadError<T>> {\n const offset = cursor.offset\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n cursor.offset = offset\n\n return result\n }\n\n /**\n * Read from bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we read less bytes than the expected length\n * @param readable \n * @param bytes \n * @returns \n */\n export function tryReadFromBytes<T extends Readable>(readable: T, bytes: Bytes): Result<ReadOutput<T>, ReadError<T> | CursorReadLengthUnderflowError> {\n const cursor = new Cursor(bytes)\n const result = readable.tryRead(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new CursorReadLengthUnderflowError(cursor))\n\n return result\n }\n\n}"],"names":[],"mappings":";;;;AAkBM,IAAW,SA6ChB;AA7CD,CAAA,UAAiB,QAAQ,EAAA;AAMvB;;;;;;;AAOG;AACH,IAAA,SAAgB,iBAAiB,CAAqB,QAAW,EAAE,MAAc,EAAA;AAC/E,QAAA,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;QAC5B,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QAEvC,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,YAAA,MAAM,CAAC,MAAM,GAAG,MAAM,CAAA;AAExB,QAAA,OAAO,MAAM,CAAA;KACd;AARe,IAAA,QAAA,CAAA,iBAAiB,oBAQhC,CAAA;AAED;;;;;;;AAOG;AACH,IAAA,SAAgB,gBAAgB,CAAqB,QAAW,EAAE,KAAY,EAAA;AAC5E,QAAA,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CAAA;QAChC,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QAEvC,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,YAAA,OAAO,MAAM,CAAA;QAEf,IAAI,MAAM,CAAC,SAAS;YAClB,OAAO,IAAI,GAAG,CAAC,IAAI,8BAA8B,CAAC,MAAM,CAAC,CAAC,CAAA;AAE5D,QAAA,OAAO,MAAM,CAAA;KACd;AAXe,IAAA,QAAA,CAAA,gBAAgB,mBAW/B,CAAA;AAEH,CAAC,EA7CgB,QAAQ,GAAR,QAAQ,KAAR,QAAQ,GA6CxB,EAAA,CAAA,CAAA;;;;"} |
| import { Cursor } from '@hazae41/cursor'; | ||
| import { Err, Ok } from '@hazae41/result'; | ||
| import { CursorWriteLenghtUnderflowError } from './errors.mjs'; | ||
| class BinaryWriteUnderflowError extends Error { | ||
| cursor; | ||
| #class = BinaryWriteUnderflowError; | ||
| constructor(cursor) { | ||
| super(`Cursor has ${cursor.remaining} remaining bytes after write`); | ||
| this.cursor = cursor; | ||
| } | ||
| } | ||
| var Writable; | ||
@@ -30,3 +23,3 @@ (function (Writable) { | ||
| if (cursor.remaining) | ||
| return new Err(new BinaryWriteUnderflowError(cursor)); | ||
| return new Err(new CursorWriteLenghtUnderflowError(cursor)); | ||
| return new Ok(cursor.bytes); | ||
@@ -37,3 +30,3 @@ } | ||
| export { BinaryWriteUnderflowError, Writable }; | ||
| export { Writable }; | ||
| //# sourceMappingURL=writable.mjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"writable.mjs","sources":["../../../../src/mods/binary/writable.ts"],"sourcesContent":["import { Bytes } from \"@hazae41/bytes\"\nimport { Cursor, CursorWriteError } from \"@hazae41/cursor\"\nimport { Err, Ok, Result } from \"@hazae41/result\"\n\nexport type BinaryWriteError =\n | BinaryWriteUnderflowError\n | CursorWriteError\n\nexport class BinaryWriteUnderflowError extends Error {\n readonly #class = BinaryWriteUnderflowError\n\n constructor(\n readonly cursor: Cursor\n ) {\n super(`Cursor has ${cursor.remaining} remaining bytes after write`)\n }\n}\n\n/**\n * A writable binary data type\n */\nexport interface Writable<SizeError = unknown, WriteError = unknown> {\n\n /**\n * Compute the amount of bytes to allocate\n */\n trySize(): Result<number, Writable.SizeError<this>>\n\n /**\n * Write to a cursor\n * @param cursor \n */\n tryWrite(cursor: Cursor): Result<void, Writable.WriteError<this>>\n\n}\n\nexport namespace Writable {\n\n export type SizeError<T extends Writable> = T extends Writable<infer SizeError, unknown> ? SizeError : never\n\n export type WriteError<T extends Writable> = T extends Writable<unknown, infer WriteError> ? WriteError : never\n\n /**\n * Write to bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we have written less bytes than allocated\n * @param writable \n * @returns \n */\n export function tryWriteToBytes<T extends Writable>(writable: T): Result<Bytes, SizeError<T> | WriteError<T> | BinaryWriteUnderflowError> {\n const size = writable.trySize()\n\n if (size.isErr())\n return size\n\n const cursor = Cursor.allocUnsafe(size.inner)\n const result = writable.tryWrite(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new BinaryWriteUnderflowError(cursor))\n\n return new Ok(cursor.bytes)\n }\n\n}"],"names":[],"mappings":";;;AAQM,MAAO,yBAA0B,SAAQ,KAAK,CAAA;AAIvC,IAAA,MAAA,CAAA;IAHF,MAAM,GAAG,yBAAyB,CAAA;AAE3C,IAAA,WAAA,CACW,MAAc,EAAA;AAEvB,QAAA,KAAK,CAAC,CAAc,WAAA,EAAA,MAAM,CAAC,SAAS,CAAA,4BAAA,CAA8B,CAAC,CAAA;QAF1D,IAAM,CAAA,MAAA,GAAN,MAAM,CAAQ;KAGxB;AACF,CAAA;AAoBK,IAAW,SA+BhB;AA/BD,CAAA,UAAiB,QAAQ,EAAA;AAMvB;;;;;;AAMG;IACH,SAAgB,eAAe,CAAqB,QAAW,EAAA;AAC7D,QAAA,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAA;QAE/B,IAAI,IAAI,CAAC,KAAK,EAAE;AACd,YAAA,OAAO,IAAI,CAAA;QAEb,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC7C,MAAM,MAAM,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;QAExC,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,YAAA,OAAO,MAAM,CAAA;QAEf,IAAI,MAAM,CAAC,SAAS;YAClB,OAAO,IAAI,GAAG,CAAC,IAAI,yBAAyB,CAAC,MAAM,CAAC,CAAC,CAAA;AAEvD,QAAA,OAAO,IAAI,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;KAC5B;AAhBe,IAAA,QAAA,CAAA,eAAe,kBAgB9B,CAAA;AAEH,CAAC,EA/BgB,QAAQ,GAAR,QAAQ,KAAR,QAAQ,GA+BxB,EAAA,CAAA,CAAA;;;;"} | ||
| {"version":3,"file":"writable.mjs","sources":["../../../../src/mods/binary/writable.ts"],"sourcesContent":["import { Bytes } from \"@hazae41/bytes\"\nimport { Cursor } from \"@hazae41/cursor\"\nimport { Err, Ok, Result } from \"@hazae41/result\"\nimport { CursorWriteLenghtUnderflowError } from \"./errors.js\"\n\n/**\n * A writable binary data type\n */\nexport interface Writable<SizeError = unknown, WriteError = unknown> {\n\n /**\n * Compute the amount of bytes to allocate\n */\n trySize(): Result<number, Writable.SizeError<this>>\n\n /**\n * Write to a cursor\n * @param cursor \n */\n tryWrite(cursor: Cursor): Result<void, Writable.WriteError<this>>\n\n}\n\nexport namespace Writable {\n\n export type SizeError<T extends Writable> = T extends Writable<infer SizeError, unknown> ? SizeError : never\n\n export type WriteError<T extends Writable> = T extends Writable<unknown, infer WriteError> ? WriteError : never\n\n /**\n * Write to bytes and check for underflow\n * \n * Underflow is when the cursor has remaining bytes; meaning we have written less bytes than allocated\n * @param writable \n * @returns \n */\n export function tryWriteToBytes<T extends Writable>(writable: T): Result<Bytes, SizeError<T> | WriteError<T> | CursorWriteLenghtUnderflowError> {\n const size = writable.trySize()\n\n if (size.isErr())\n return size\n\n const cursor = Cursor.allocUnsafe(size.inner)\n const result = writable.tryWrite(cursor)\n\n if (result.isErr())\n return result\n\n if (cursor.remaining)\n return new Err(new CursorWriteLenghtUnderflowError(cursor))\n\n return new Ok(cursor.bytes)\n }\n\n}"],"names":[],"mappings":";;;;AAuBM,IAAW,SA+BhB;AA/BD,CAAA,UAAiB,QAAQ,EAAA;AAMvB;;;;;;AAMG;IACH,SAAgB,eAAe,CAAqB,QAAW,EAAA;AAC7D,QAAA,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAA;QAE/B,IAAI,IAAI,CAAC,KAAK,EAAE;AACd,YAAA,OAAO,IAAI,CAAA;QAEb,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC7C,MAAM,MAAM,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;QAExC,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,YAAA,OAAO,MAAM,CAAA;QAEf,IAAI,MAAM,CAAC,SAAS;YAClB,OAAO,IAAI,GAAG,CAAC,IAAI,+BAA+B,CAAC,MAAM,CAAC,CAAC,CAAA;AAE7D,QAAA,OAAO,IAAI,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;KAC5B;AAhBe,IAAA,QAAA,CAAA,eAAe,kBAgB9B,CAAA;AAEH,CAAC,EA/BgB,QAAQ,GAAR,QAAQ,KAAR,QAAQ,GA+BxB,EAAA,CAAA,CAAA;;;;"} |
| export { Empty } from './binary/empty.mjs'; | ||
| export { CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from './binary/errors.mjs'; | ||
| export { Opaque, SafeOpaque, UnsafeOpaque } from './binary/opaque.mjs'; | ||
| export { BinaryReadUnderflowError, Readable } from './binary/readable.mjs'; | ||
| export { BinaryWriteUnderflowError, Writable } from './binary/writable.mjs'; | ||
| export { Readable } from './binary/readable.mjs'; | ||
| export { Writable } from './binary/writable.mjs'; | ||
| //# sourceMappingURL=index.mjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"} | ||
| {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"} |
| 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 { Opaque, SafeOpaque, UnsafeOpaque } from './mods/binary/opaque.js'; | ||
| export { BinaryReadError, BinaryReadUnderflowError, Readable } from './mods/binary/readable.js'; | ||
| export { BinaryWriteError, BinaryWriteUnderflowError, Writable } from './mods/binary/writable.js'; | ||
| export { Readable } from './mods/binary/readable.js'; | ||
| export { Writable } from './mods/binary/writable.js'; |
| import { Bytes, Sized } from '@hazae41/bytes'; | ||
| import { Cursor, CursorWriteLengthOverflowError, CursorReadLengthOverflowError } from '@hazae41/cursor'; | ||
| import { Result } from '@hazae41/result'; | ||
| import { Readable, BinaryReadUnderflowError } from './readable.js'; | ||
| import { Writable, BinaryWriteUnderflowError } from './writable.js'; | ||
| import { Readable } from './readable.js'; | ||
| import { CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from './errors.js'; | ||
| import { Writable } from './writable.js'; | ||
@@ -28,3 +29,3 @@ declare class Opaque<T extends Bytes = Bytes> { | ||
| */ | ||
| tryInto<T extends Readable>(readable: T): Result<Readable.ReadOutput<T>, Readable.ReadError<T> | BinaryReadUnderflowError>; | ||
| tryInto<T extends Readable>(readable: T): Result<Readable.ReadOutput<T>, Readable.ReadError<T> | CursorReadLengthUnderflowError>; | ||
| /** | ||
@@ -35,3 +36,3 @@ * Create an opaque from a binary data type | ||
| */ | ||
| static tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | BinaryWriteUnderflowError>; | ||
| static tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError>; | ||
| } | ||
@@ -42,3 +43,14 @@ /** | ||
| declare namespace UnsafeOpaque { | ||
| /** | ||
| * Unsafe zero-copy read remaining bytes from cursor | ||
| * @param cursor | ||
| * @returns | ||
| */ | ||
| function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError>; | ||
| /** | ||
| * Perform unsafe zero-copy conversion to `Opaque` if `T instanceof Opaque`, else use `Opaque.tryFrom` | ||
| * @param writable | ||
| * @returns | ||
| */ | ||
| function tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError>; | ||
| } | ||
@@ -49,5 +61,16 @@ /** | ||
| declare namespace SafeOpaque { | ||
| /** | ||
| * Safe copy read remaining bytes from cursor | ||
| * @param cursor | ||
| * @returns | ||
| */ | ||
| function tryRead(cursor: Cursor): Result<Opaque, CursorReadLengthOverflowError>; | ||
| /** | ||
| * Perform safe, copy conversion to `Opaque` using `Opaque.tryFrom` | ||
| * @param writable | ||
| * @returns | ||
| */ | ||
| function tryFrom<T extends Writable>(writable: T): Result<Opaque, Writable.SizeError<T> | Writable.WriteError<T> | CursorWriteLenghtUnderflowError>; | ||
| } | ||
| export { Opaque, SafeOpaque, UnsafeOpaque }; |
| import { Bytes } from '@hazae41/bytes'; | ||
| import { CursorReadError, Cursor } from '@hazae41/cursor'; | ||
| import { Cursor } from '@hazae41/cursor'; | ||
| import { Result } from '@hazae41/result'; | ||
| import { CursorReadLengthUnderflowError } from './errors.js'; | ||
| type BinaryReadError = BinaryReadUnderflowError | CursorReadError; | ||
| declare class BinaryReadUnderflowError extends Error { | ||
| #private; | ||
| readonly cursor: Cursor; | ||
| constructor(cursor: Cursor); | ||
| } | ||
| /** | ||
@@ -41,5 +36,5 @@ * A readable binary data type | ||
| */ | ||
| function tryReadFromBytes<T extends Readable>(readable: T, bytes: Bytes): Result<ReadOutput<T>, ReadError<T> | BinaryReadUnderflowError>; | ||
| function tryReadFromBytes<T extends Readable>(readable: T, bytes: Bytes): Result<ReadOutput<T>, ReadError<T> | CursorReadLengthUnderflowError>; | ||
| } | ||
| export { BinaryReadError, BinaryReadUnderflowError, Readable }; | ||
| export { Readable }; |
| import { Bytes } from '@hazae41/bytes'; | ||
| import { CursorWriteError, Cursor } from '@hazae41/cursor'; | ||
| import { Cursor } from '@hazae41/cursor'; | ||
| import { Result } from '@hazae41/result'; | ||
| import { CursorWriteLenghtUnderflowError } from './errors.js'; | ||
| type BinaryWriteError = BinaryWriteUnderflowError | CursorWriteError; | ||
| declare class BinaryWriteUnderflowError extends Error { | ||
| #private; | ||
| readonly cursor: Cursor; | ||
| constructor(cursor: Cursor); | ||
| } | ||
| /** | ||
@@ -35,5 +30,5 @@ * A writable binary data type | ||
| */ | ||
| function tryWriteToBytes<T extends Writable>(writable: T): Result<Bytes, SizeError<T> | WriteError<T> | BinaryWriteUnderflowError>; | ||
| function tryWriteToBytes<T extends Writable>(writable: T): Result<Bytes, SizeError<T> | WriteError<T> | CursorWriteLenghtUnderflowError>; | ||
| } | ||
| export { BinaryWriteError, BinaryWriteUnderflowError, Writable }; | ||
| export { Writable }; |
| export { Empty } from './binary/empty.js'; | ||
| export { BinaryError, BinaryReadError, BinaryWriteError, CursorReadLengthUnderflowError, CursorWriteLenghtUnderflowError } from './binary/errors.js'; | ||
| export { Opaque, SafeOpaque, UnsafeOpaque } from './binary/opaque.js'; | ||
| export { BinaryReadError, BinaryReadUnderflowError, Readable } from './binary/readable.js'; | ||
| export { BinaryWriteError, BinaryWriteUnderflowError, Writable } from './binary/writable.js'; | ||
| export { Readable } from './binary/readable.js'; | ||
| export { Writable } from './binary/writable.js'; |
+1
-1
| { | ||
| "type": "module", | ||
| "name": "@hazae41/binary", | ||
| "version": "1.2.27", | ||
| "version": "1.2.28", | ||
| "description": "Zero-copy binary data types", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/hazae41/binary", |
53996
16.08%37
15.63%681
17.62%