This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdd5499
commit 3a99f5f
Showing
11 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
write: true, | ||
prefix: '^', | ||
test: [ | ||
'test', | ||
'benchmark', | ||
], | ||
devdep: [ | ||
'egg-bin', | ||
'egg-ci', | ||
'autod', | ||
'eslint', | ||
'eslint-config-egg', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "eslint-config-egg" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
logs/ | ||
npm-debug.log | ||
node_modules/ | ||
coverage/ | ||
.idea/ | ||
.DS_Store | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- '8' | ||
- '9' | ||
install: | ||
- npm i npminstall && npminstall | ||
script: | ||
- npm run ci | ||
after_script: | ||
- npminstall codecov && codecov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
environment: | ||
matrix: | ||
- nodejs_version: '8' | ||
- nodejs_version: '9' | ||
|
||
install: | ||
- ps: Install-Product node $env:nodejs_version | ||
- npm i npminstall && node_modules\.bin\npminstall | ||
|
||
test_script: | ||
- node --version | ||
- npm --version | ||
- npm run test | ||
|
||
build: off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const bytes = require('humanize-bytes'); | ||
|
||
module.exports = (stream, options = {}) => { | ||
return new Promise((resolve, reject) => { | ||
if (!stream.readable) return resolve(); | ||
const maxSize = options.maxSize && bytes(options.maxSize); | ||
|
||
const bufs = []; | ||
let size = 0; | ||
let error; | ||
|
||
stream.on('data', onData); | ||
stream.on('end', onEnd); | ||
stream.on('error', onEnd); | ||
stream.on('close', onClose); | ||
|
||
function onData(buf) { | ||
if (error) return; | ||
|
||
size += buf.length; | ||
if (maxSize && size > maxSize) { | ||
error = new Error(`entity size exceed ${options.maxSize}`); | ||
error.code = 'ENTITY_TOO_LARGE'; | ||
// usualy use in parse http request so we set status = 413 | ||
error.status = 413; | ||
return; | ||
} | ||
bufs.push(buf); | ||
} | ||
|
||
function onEnd(err) { | ||
if (err instanceof Error) error = err; | ||
done(); | ||
} | ||
|
||
function onClose() { | ||
done(); | ||
} | ||
|
||
function done() { | ||
cleanup(); | ||
if (error) reject(error); | ||
else resolve(Buffer.concat(bufs)); | ||
} | ||
|
||
function cleanup() { | ||
stream.removeListener('data', onData); | ||
stream.removeListener('end', onEnd); | ||
stream.removeListener('error', onEnd); | ||
stream.removeListener('close', onClose); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "stream-to-buf", | ||
"description": "convert stream to buffer", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/node-modules/stream-to-buf", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/node-modules/stream-to-buf.git" | ||
}, | ||
"dependencies": { | ||
"humanize-bytes": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"autod": "^3.0.1", | ||
"egg-bin": "^4.4.0", | ||
"egg-ci": "^1.8.0", | ||
"eslint": "^4.18.1", | ||
"eslint-config-egg": "^7.0.0" | ||
}, | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"lint": "eslint test *.js", | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"test-local": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"ci": "npm run lint && npm run cov", | ||
"autod": "autod" | ||
}, | ||
"author": "dead_horse", | ||
"engines": { | ||
"node": ">= 8.0.0" | ||
}, | ||
"ci": { | ||
"version": "8, 9" | ||
}, | ||
"license": "MIT" | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const streamToBuffer = require('../'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
describe('stream-to-buf', () => { | ||
it('should collect buffer success', async () => { | ||
const small = fs.createReadStream(path.join(__dirname, 'fixtures/smallfile')); | ||
let buffer = await streamToBuffer(small); | ||
assert(buffer.length === 1024); | ||
|
||
const big = fs.createReadStream(path.join(__dirname, 'fixtures/bigfile')); | ||
buffer = await streamToBuffer(big); | ||
assert(buffer.length === 102400); | ||
}); | ||
|
||
it('should collect buffer when end and close both emitted', async () => { | ||
const small = fs.createReadStream(path.join(__dirname, 'fixtures/smallfile')); | ||
small.once('end', () => small.emit('close')); | ||
const buffer = await streamToBuffer(small); | ||
assert(buffer.length === 1024); | ||
}); | ||
|
||
it('should return undefined if not readable', async () => { | ||
const buffer = await streamToBuffer('foo'); | ||
assert(!buffer); | ||
}); | ||
|
||
it('should collect buffer with maxSize', async () => { | ||
const small = fs.createReadStream(path.join(__dirname, 'fixtures/smallfile')); | ||
const big = fs.createReadStream(path.join(__dirname, 'fixtures/bigfile')); | ||
const buffer = await streamToBuffer(small, { maxSize: '2kb' }); | ||
assert(buffer.length === 1024); | ||
try { | ||
await streamToBuffer(big, { maxSize: '2kb' }); | ||
throw new Error('should not execute'); | ||
} catch (err) { | ||
assert(err.message === 'entity size exceed 2kb'); | ||
assert(err.code === 'ENTITY_TOO_LARGE'); | ||
assert(err.status === 413); | ||
} | ||
}); | ||
|
||
it('should throw end by error', async () => { | ||
const stream = fs.createReadStream(path.join(__dirname, 'fixtures/bigfile')); | ||
setImmediate(() => { | ||
stream.emit('error', new Error('mock error')); | ||
}); | ||
try { | ||
await streamToBuffer(stream); | ||
throw new Error('should not execute'); | ||
} catch (err) { | ||
assert(err.message === 'mock error'); | ||
} | ||
}); | ||
}); |