@hazae41/binary
Advanced tools
Comparing version 1.1.12 to 1.2.0
@@ -1,1 +0,6 @@ | ||
export { Binary } from './mods/binary/binary.js'; | ||
import * as index from './mods/index.js'; | ||
export { index as Binary }; | ||
export { Opaque, SafeOpaque, UnsafeOpaque } from './mods/binary/opaque.js'; | ||
export { Readable } from './mods/binary/readable.js'; | ||
export { Writable } from './mods/binary/writable.js'; | ||
export { Cursor } from './mods/cursor/cursor.js'; |
{ | ||
"type": "module", | ||
"name": "@hazae41/binary", | ||
"version": "1.1.12", | ||
"description": "Zero-copy bytes reader and writer", | ||
"version": "1.2.0", | ||
"description": "Zero-copy binary data types", | ||
"homepage": "https://github.com/hazae41/binary", | ||
@@ -24,9 +24,12 @@ "repository": "github:hazae41/binary", | ||
}, | ||
"dependencies": { | ||
"@hazae41/bytes": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@hazae41/phobos": "^1.0.10", | ||
"@rollup/plugin-typescript": "^11.0.0", | ||
"@types/node": "^18.11.19", | ||
"@types/node": "^18.13.0", | ||
"rimraf": "^4.1.2", | ||
"rollup": "^3.14.0", | ||
"rollup-plugin-dts": "^5.1.1", | ||
"rollup": "^3.15.0", | ||
"rollup-plugin-dts": "^5.2.0", | ||
"rollup-plugin-node-externals": "^5.1.2", | ||
@@ -67,6 +70,3 @@ "typescript": "^4.9.5" | ||
"unit-tested" | ||
], | ||
"dependencies": { | ||
"@hazae41/bytes": "^1.0.1" | ||
} | ||
] | ||
} |
@@ -5,3 +5,3 @@ <div align="center"> | ||
<h3 align="center"> | ||
Zero-copy bytes reader and writer 🏎️ | ||
Zero-copy binary data types 🏎️ | ||
</h3> | ||
@@ -15,26 +15,103 @@ | ||
### Current features | ||
## Current features | ||
- 100% TypeScript and ESM | ||
- No external dependencies | ||
- Unit-tested | ||
- Zero-copy reading and writing | ||
- No external dependency | ||
- Compatible with both Uint8Array and Buffer | ||
- Unit tested | ||
### Usage | ||
## Usage | ||
### Cursor | ||
#### Writing | ||
```typescript | ||
const binary = Binary.alloc(3) | ||
const cursor = Cursor.allocUnsafe(1024) | ||
cursor.writeUint8(123) | ||
cursor.writeUint16(1234) | ||
console.log(cursor.offset) // 3 | ||
``` | ||
#### Reading | ||
```typescript | ||
const binary = Binary.allocUnsafe(3) | ||
const bytes = new Uint8Array(/*...*/) | ||
const cursor = new Cursor(bytes) | ||
const uint8 = cursor.readUint8() | ||
const uint16 = cursor.readUint16() | ||
console.log(cursor.offset) // 3 | ||
``` | ||
### Binary data types | ||
#### Writable | ||
```typescript | ||
const binary = new Binary(Buffer.from([0x1, 0x2])) | ||
class MyObject implements Writable { | ||
constructor( | ||
readonly x: number, | ||
readonly y: number | ||
) {} | ||
size() { | ||
return 1 + 2 | ||
} | ||
write(cursor: Cursor) { | ||
cursor.writeUint8(this.x) | ||
cursor.writeUint16(this.y) | ||
} | ||
} | ||
``` | ||
```typescript | ||
binary.writeUint8(123) | ||
binary.writeUint16(1234) | ||
const myobject = new MyObject(1, 515) | ||
const bytes = Writable.toBytes(myobject) // Uint8Array([1, 2, 3]) | ||
``` | ||
#### Readable | ||
```typescript | ||
class MyObject { | ||
constructor( | ||
readonly x: number, | ||
readonly y: number | ||
) {} | ||
static read(cursor: Cursor) { | ||
const x = cursor.readUint8() | ||
const y = cursor.readUint16() | ||
return new this(x, y) | ||
} | ||
} | ||
``` | ||
```typescript | ||
const bytes = new Uint8Array([1, 2, 3]) | ||
const myobject = Readable.fromBytes(MyObject, bytes) // MyObject(1, 515) | ||
``` | ||
#### Opaque | ||
This is a binary data type that just holds bytes, it can be used when a binary data type is required | ||
```typescript | ||
const bytes = new Uint8Array([1, 2, 3]) | ||
const opaque = Readable.fromBytes(SafeOpaque, bytes) // Opaque(Uint8Array([1, 2, 3])) | ||
const myobject = opaque.into(MyObject) // MyObject(1, 515) | ||
``` | ||
```typescript | ||
const myobject = new MyObject(1, 515) | ||
const opaque = Opaque.from(myobject) // Opaque(Uint8Array([1, 2, 3])) | ||
const bytes = Writable.toBytes(opaque) // Uint8Array([1, 2, 3]) | ||
``` |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101511
40
1587
116
1
Updated@hazae41/bytes@^1.0.3