https://github.com/system233/node-binary-streams
https://github.com/system233/node-binary-streams
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/system233/node-binary-streams
- Owner: System233
- License: mit
- Created: 2023-05-09T15:12:25.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-05-15T06:53:52.000Z (about 2 years ago)
- Last Synced: 2025-02-18T23:46:46.140Z (3 months ago)
- Language: TypeScript
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Binary Streams
[![npm][npm-image]][npm-url]
[npm-image]: https://img.shields.io/npm/v/binary-streams.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/binary-streamsA `BufferLike` Binary Stream Wrapper for Node `Readable`/`Writable`/`Duplex` Stream.
## Installation
```sh
npm install binary-streams
```## Usages
For `Readable` stream
```ts
import { BinaryReadStream } from "binary-streams";
import { Readable } from "node:stream";const buffer = Buffer.from([
0x78, 0x56, 0x34, 0x12,
0xff, 0xff
]);
const rstream=new Readable({
read(){
stream.push(buffer)
stream.push(null)
}
});
const stream = BinaryReadStream.from(rstream);
console.log('readInt32LE','0x'+(await stream.readInt32LE()).toString(16));
console.log('readInt8',await stream.readInt8());
console.log('readUint8',await stream.readUint8());// OUTPUT:
// readInt32LE 0x12345678
// readInt8 -1
// readUint8 255
```For `Writable` stream
```ts
import { BinaryWriteStream } from "binary-streams";
import { Writable } from "node:stream";const wstream=new Writable();
wstream._write=chunk=>console.log('write',chunk);const stream=BinaryWriteStream.from(wstream);
stream.writeInt32(0x12345678)// OPTPUT:
// write
```For `Duplex` stream
```ts
import { BinaryDuplexStream } from "binary-streams"
import net from 'node:net'const server = net.createServer((socket) =>socket.pipe(socket));
server.listen()
const {address:host,port}=server.address();
const stream=BinaryDuplexStream.from(net.connect({host,port}));stream.writeInt32LE(0x12345678);
const result=await stream.readInt32LE();console.log('result','0x'+result.toString(16));
stream.end();
server.close();// OUTPUT:
// result 0x12345678
```## License
[MIT License](LICENSE) Copyright (c) 2023 System233