This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: init stream to buffer #1
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 放到前面了 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 额,这个还不能放前面,bufs 被清空了。问题不大,反正都是同步的。 |
||
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stream-to-buffer 被抢。。。 |
||
"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'); | ||
} | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个要 once?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
promise 只会 resolve 一次,这个多次调用也无所谓的