Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sail-sail/block_stream2
A stream of blocks. Write data into it, and it'll output data in buffer blocks the size you specify, padding with zeroes if necessary.
https://github.com/sail-sail/block_stream2
Last synced: 21 days ago
JSON representation
A stream of blocks. Write data into it, and it'll output data in buffer blocks the size you specify, padding with zeroes if necessary.
- Host: GitHub
- URL: https://github.com/sail-sail/block_stream2
- Owner: sail-sail
- Created: 2022-05-23T01:22:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-23T02:31:30.000Z (over 2 years ago)
- Last Synced: 2024-10-04T13:11:40.882Z (about 1 month ago)
- Language: TypeScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# block_stream2
A stream of blocks.
Write data into it, and it'll output data in buffer blocks the size you specify, padding with zeroes if necessary.
fock by https://github.com/substack/block-stream2
## usage
```ts
import { assert, assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { stat } from "https://deno.land/[email protected]/node/fs/promises.ts"
import fs from "https://deno.land/[email protected]/node/fs.ts";
import { Buffer } from "https://deno.land/[email protected]/node/buffer.ts";
import { BlockStream2 } from "https://deno.land/x/block_stream2/mod.ts";Deno.test("BlockStream2", async function() {
const b = new BlockStream2(16);
const fstr = fs.createReadStream("./mod_test.ts", { encoding: 'utf8' })
fstr.pipe(b)
b.resume();
let totalBytes = 0;
const stats = await stat("./mod_test.ts");
await new Promise((resolve, reject) => {
b.on("data", (data) => {
assertEquals(data.byteLength, 16, 'chunks should be 16 bytes long');
assert(Buffer.isBuffer(data));
totalBytes += data.length;
});
b.on("error", reject);
b.on('end', () => {
const expectedBytes = stats.size + (16 - stats.size % 16)
assertEquals(totalBytes, expectedBytes);
resolve(undefined);
});
});
});
```When .end() or .flush() is called, it'll pad the block with zeroes.