https://project.mdnd-it.cc/work_packages/94
This commit is contained in:
2025-08-23 04:25:28 +02:00
parent 725516ad6c
commit 19cfa031d0
25823 changed files with 1095587 additions and 2801760 deletions
+18
View File
@@ -0,0 +1,18 @@
This software is released under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+69
View File
@@ -0,0 +1,69 @@
const { Transform } = require('readable-stream')
class Block extends Transform {
constructor (size, opts = {}) {
super(opts)
if (typeof size === 'object') {
opts = size
size = opts.size
}
this.size = size || 512
const { nopad, zeroPadding = true } = opts
if (nopad) this._zeroPadding = false
else this._zeroPadding = !!zeroPadding
this._buffered = []
this._bufferedBytes = 0
}
_transform (buf, enc, next) {
this._bufferedBytes += buf.length
this._buffered.push(buf)
while (this._bufferedBytes >= this.size) {
this._bufferedBytes -= this.size
// Assemble the buffers that will compose the final block
const blockBufs = []
let blockBufsBytes = 0
while (blockBufsBytes < this.size) {
const b = this._buffered.shift()
if (blockBufsBytes + b.length <= this.size) {
blockBufs.push(b)
blockBufsBytes += b.length
} else {
// If the last buffer is larger than needed for the block, just
// use the needed part
const neededSize = this.size - blockBufsBytes
blockBufs.push(b.slice(0, neededSize))
blockBufsBytes += neededSize
this._buffered.unshift(b.slice(neededSize))
}
}
// Then concat just those buffers, leaving the rest untouched in _buffered
this.push(Buffer.concat(blockBufs, this.size))
}
next()
}
_flush () {
if (this._bufferedBytes && this._zeroPadding) {
const zeroes = Buffer.alloc(this.size - this._bufferedBytes)
this._buffered.push(zeroes)
this.push(Buffer.concat(this._buffered))
this._buffered = null
} else if (this._bufferedBytes) {
this.push(Buffer.concat(this._buffered))
this._buffered = null
}
this.push(null)
}
}
module.exports = Block
+35
View File
@@ -0,0 +1,35 @@
{
"name": "block-stream2",
"version": "2.1.0",
"description": "transform input into equally-sized blocks of output",
"main": "index.js",
"dependencies": {
"readable-stream": "^3.4.0"
},
"devDependencies": {
"standard": "^16.0.3",
"tape": "^4.2.2"
},
"scripts": {
"test": "standard && tape test/*.js"
},
"repository": {
"type": "git",
"url": "git://github.com/substack/block-stream2.git"
},
"homepage": "https://github.com/substack/block-stream2",
"keywords": [
"stream",
"block",
"chunk",
"size",
"streams2",
"streams3"
],
"author": {
"name": "James Halliday",
"email": "mail@substack.net",
"url": "http://substack.net"
},
"license": "MIT"
}
+68
View File
@@ -0,0 +1,68 @@
# block-stream2
transform input into equally-sized chunks as output
streams3 version of
[block-stream](https://npmjs.org/package/block-stream)
[![build status](https://secure.travis-ci.org/substack/block-stream2.png)](http://travis-ci.org/substack/block-stream2)
# example
``` js
const BlockStream = require('block-stream2');
const through = require('through2');
process.stdin
.pipe(new BlockStream({ size: 16, zeroPadding: true }))
.pipe(through((buf, enc, next) => {
const str = buf.toString().replace(/[\x00-\x1f]/g, chr);
console.log(`buf[${buf.length}]=${str}`);
next();
}))
;
function chr (s) { return `\\x${pad(s.charCodeAt(0).toString(16),2)}` }
function pad (s, n) { return Array(n - s.length + 1).join('0') + s }
```
```
$ echo {c,d,f}{a,e,i,o,u}{t,g,r} | node example/stream.js
buf[16]=cat cag car cet
buf[16]=ceg cer cit cig
buf[16]=cir cot cog cor
buf[16]=cut cug cur dat
buf[16]=dag dar det deg
buf[16]=der dit dig dir
buf[16]=dot dog dor dut
buf[16]=dug dur fat fag
buf[16]=far fet feg fer
buf[16]=fit fig fir fot
buf[16]=fog for fut fug
buf[16]=fur\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
```
# methods
``` js
const BlockStream = require('block-stream2');
```
## const b = new BlockStream(opts)
## const b = new BlockStream(size, opts)
Create a new transform stream `b` that outputs chunks of length `size` or
`opts.size`.
When `opts.zeroPadding` is false, do not zero-pad the last chunk.
# install
With [npm](https://npmjs.org) do:
```
npm install block-stream2
```
# license
MIT