initial commit

This commit is contained in:
dude
2026-09-01 18:33:56 +00:00
commit c24449ee0a
2761 changed files with 302737 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Environment variable files
.envrc
.env
# Temporary / data folders
.tmp
data/
# Source directory
src/
# Test directory
test/
.eslintrc
.babelrc
+6
View File
@@ -0,0 +1,6 @@
sudo: false
language: node_js
notifications:
email: false
node_js:
- 6
+20
View File
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2016 Olivier Lalonde <olalonde@gmail.com>
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.
+77
View File
@@ -0,0 +1,77 @@
# common-streams
[![Build Status](https://travis-ci.org/blockai/common-streams.svg?branch=master)](https://travis-ci.org/blockai/common-streams)
A collection of useful streams.
## Install
```bash
npm install --save common-streams
```
Requires Node v6+
## Usage
See [./test](./test) directory for more usage examples.
Example:
```javascript
import { HashStream, SizeStream } from 'common-streams'
import concat from 'concat-stream'
const rs = getReadStream() // e.g. fs.createReadStream(...)
const ws = getWriteStream() // e.g. fs.createWriteStream(...)
const sha1Stream = new HashStream('sha1')
const sizeStream = new SizeStream()
rs.pipe(sha1Stream).pipe(concat(([{ digest }]) => {
console.log(digest, '=', '4129def2ea7cb7945ddfbb785969898fca2e34c3')
}))
rs.pipe(sizeStream).pipe(concat(([{ size }]) => {
console.log(size, '=', 2447774)
}))
rs.pipe(ws)
```
### HashStream
Transform stream that operates in object mode and returns a single `{ digest: 'hash digest' }` object.
`new HashStream(algorithm, [, opts])`
**algorithm**: any algorithm supported by [crypto.createHash](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm)
**opts.encoding**: any encoding supported by [hash.digest](https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding). Defaults to `hex`.
### SizeStream
Transform stream that operates in object mode and returns a single `{
size: 1000 }` object where the size is an integer representing the total
size in bytes of the stream.
`new SizeStream()`
If passed a callback `new SizeStream(cb)` it will just act as a
PassThrough stream and call the callback when it's done.
### RandomStream
`new RandomStream(max)`
Readable stream that emits `max` random bytes before ending.
### DiscardStream
`new DiscardStream(start)`
Swallows all bytes until `start` is reached.
### MeterStream
`new MeterStream(max)`
See [meterstream](https://github.com/blockai/meterstream)
+34
View File
@@ -0,0 +1,34 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _stream = require('stream');
// Discards "bytesToDiscard" bytes
class DiscardStream extends _stream.Transform {
constructor() {
let bytesToDiscard = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];
super();
this.bytesToDiscard = bytesToDiscard;
this.byteCount = 0;
this.done = false;
}
_transform(chunk, encoding, cb) {
if (this.done) return cb(null, chunk);
this.byteCount += chunk.length;
if (this.byteCount > this.bytesToDiscard) {
this.done = true;
const bytesToKeep = this.byteCount - this.bytesToDiscard;
const firstChunk = chunk.slice(-bytesToKeep);
return cb(null, firstChunk);
}
cb(null);
}
}
exports.default = DiscardStream;
+38
View File
@@ -0,0 +1,38 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _stream = require('stream');
var _crypto = require('crypto');
class HashStream extends _stream.Transform {
constructor(algo) {
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var _ref$encoding = _ref.encoding;
let encoding = _ref$encoding === undefined ? 'hex' : _ref$encoding;
super({ objectMode: true });
this.algo = algo;
this.digestEncoding = encoding;
this.hashSum = (0, _crypto.createHash)(algo);
}
_transform(chunk, encoding, cb) {
// Update hash sum objects
this.hashSum.update(chunk);
cb();
}
_flush(cb) {
// Compute hash digest
const digest = this.hashSum.digest(this.digestEncoding);
// this.emit('hash', hash)
this.push({ digest });
cb(null);
}
}
exports.default = HashStream;
+28
View File
@@ -0,0 +1,28 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _stream = require('stream');
var _crypto = require('crypto');
class RandomStream extends _stream.Readable {
constructor(max) {
super();
this.remainingBytes = max;
}
_read(requestedSize) {
if (this.remainingBytes === 0) return this.push(null);
const size = Math.min(this.remainingBytes, requestedSize);
this.remainingBytes -= size;
(0, _crypto.randomBytes)(size, (err, buf) => {
if (err) return this.emit('error', err);
this.push(buf);
});
}
}
exports.default = RandomStream;
+40
View File
@@ -0,0 +1,40 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _stream = require('stream');
class SizeStream extends _stream.Transform {
constructor(cb) {
super({ objectMode: true });
this.streamSize = 0;
this.cb = cb;
}
_transform(chunk, encoding, cb) {
if (this.cb) return this._transformCb(chunk, encoding, cb);
// Update hash sum objects
this.streamSize = this.streamSize + chunk.length;
cb();
}
_flush(cb) {
if (this.cb) return this._flushCb(cb);
const size = this.streamSize;
this.push({ size });
cb(null);
}
_transformCb(chunk, encoding, cb) {
this.streamSize = this.streamSize + chunk.length;
cb(null, chunk);
}
_flushCb(cb) {
const size = this.streamSize;
this.cb(size);
cb(null);
}
}
exports.default = SizeStream;
+52
View File
@@ -0,0 +1,52 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _HashStream = require('./HashStream');
Object.defineProperty(exports, 'HashStream', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_HashStream).default;
}
});
var _SizeStream = require('./SizeStream');
Object.defineProperty(exports, 'SizeStream', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_SizeStream).default;
}
});
var _RandomStream = require('./RandomStream');
Object.defineProperty(exports, 'RandomStream', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_RandomStream).default;
}
});
var _DiscardStream = require('./DiscardStream');
Object.defineProperty(exports, 'DiscardStream', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_DiscardStream).default;
}
});
var _meterstream = require('meterstream');
Object.defineProperty(exports, 'MeterStream', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_meterstream).default;
}
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
+62
View File
@@ -0,0 +1,62 @@
{
"name": "common-streams",
"version": "1.4.0",
"description": "",
"main": "./lib/index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "npm run test:fast",
"build": "npm run clean && babel ./src --out-dir ./lib --copy-files",
"clean": "rimraf ./lib",
"lint": "eslint src/ test/",
"pretest": "npm run lint",
"test:fast": "babel-tape-runner test/*.test.js",
"test:watch": "nodemon --exec npm -- run --silent test:fast || true",
"semantic-release": "git push && npm test && semantic-release pre && npm run build && npm publish && semantic-release post"
},
"repository": {
"type": "git",
"url": "git+https://github.com/blockai/common-streams.git"
},
"keywords": [],
"author": "Oli Lalonde <olalonde@gmail.com> (https://syskall.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/blockai/common-streams/issues"
},
"homepage": "https://github.com/blockai/common-streams#readme",
"engines": {
"node": ">=6"
},
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015-node6": "^0.3.0",
"babel-tape-runner": "^2.0.1",
"blue-tape": "^0.2.0",
"concat-stream": "^1.5.2",
"cz-conventional-changelog": "^1.2.0",
"eslint-config-blockai": "^1.0.1",
"mkdirp": "^0.5.1",
"nodemon": "^1.10.2",
"rimraf": "^2.5.4",
"semantic-release": "^4.3.5"
},
"release": {
"debug": false,
"verifyConditions": {
"path": "./node_modules/semantic-release/dist/lib/plugin-noop.js"
}
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"dependencies": {
"meterstream": "^1.0.1"
}
}