-
Notifications
You must be signed in to change notification settings - Fork 111
Read/Write streams #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bchelli
wants to merge
9
commits into
master
Choose a base branch
from
rw-streams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2f070f2
feat: create streams
fridjon 370dfe9
fix: type
fridjon 0827900
fix: typo
fridjon 123f1bf
added .vscode to gitignore, code to TODO.md, package-lock
bwiercinski 6dde2fa
fix README.md
bwiercinski 26f8ea5
simplify computing of fileLength
bwiercinski 51d609b
added options to createReadStream with start, end properties
bwiercinski aea3b64
update documentation with new functionality
bwiercinski 02045a8
Close cleanup + reorder chuncks based actual order of chuncks
bchelli 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .DS_Store | ||
| node_modules | ||
| test.js | ||
| .vscode |
This file contains hidden or 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 hidden or 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 hidden or 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,148 @@ | ||
| var RS = require('readable-stream') | ||
| , SMB2Forge = require('../tools/smb2-forge') | ||
| , SMB2Request = SMB2Forge.request | ||
| , bigint = require('../tools/bigint') | ||
| ; | ||
|
|
||
| /* | ||
| * createReadStream | ||
| * ================ | ||
| * | ||
| * Return a read stream for a file on the share | ||
| */ | ||
| module.exports = function(filename, options){ | ||
| options = processOptions(options); | ||
|
|
||
| var connection = this | ||
| , file | ||
| , bufferedChunks = [] | ||
| , chunckPushed = 0 | ||
| , chunckRequested = 0 | ||
| , opened = false | ||
| , fileLength = 0 | ||
| , offset = new bigint(8).add(options.start) | ||
| , stop = false | ||
| , nbRemainingPackets = 0 | ||
| , maxPacketSize = 0x00010000 | ||
| , readable = new RS.Readable({ | ||
| read: function(size) { | ||
| if (opened) { | ||
| readNext(); | ||
| } | ||
| } | ||
| }) | ||
| ; | ||
|
|
||
| var close = function () { | ||
| stop = true; | ||
| if (opened) { | ||
| opened = false; | ||
| SMB2Request('close', file, connection, function(err){ | ||
| if(err){ | ||
| readable.emit('error', err); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| SMB2Request('open', {path:filename}, connection, function(err, openedFile) { | ||
| file = openedFile; | ||
| opened = true; | ||
| if(err) { | ||
| readable.emit('error', err); | ||
| return close(); | ||
| } else { | ||
| fileLength = file.EndofFile.readUInt16LE(); | ||
| if(options.start >= fileLength) { | ||
| readable.emit('error', new Error('start (' + options.start + ') is greater equal than file length (' + fileLength + ')!')); | ||
| return close(); | ||
| } | ||
| fileLength = Math.min(fileLength, options.end + 1); | ||
| readNext(); | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| function callback(position, offset) { | ||
| return function(err, content){ | ||
| if(stop) { | ||
| return close(); | ||
| } | ||
| if(err) { | ||
| readable.emit('error', err) | ||
| return close(); | ||
| } else { | ||
| bufferedChunks[position] = { loaded: true, content: content }; | ||
| if (position === chunckPushed) { | ||
| while (bufferedChunks[chunckPushed] && bufferedChunks[chunckPushed].loaded) { | ||
| readable.push(bufferedChunks[chunckPushed].content); | ||
| delete bufferedChunks[chunckPushed]; | ||
| chunckPushed++; | ||
| } | ||
| } | ||
| nbRemainingPackets--; | ||
| readNext(); | ||
| if (chunckPushed == chunckRequested && nbRemainingPackets == 0) { | ||
| return close(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function readNext() { | ||
| while (nbRemainingPackets < connection.packetConcurrency && offset.lt(fileLength)){ | ||
| // process packet size | ||
| var rest = offset.sub(fileLength).neg(); | ||
| var packetSize = rest.gt(maxPacketSize) ? maxPacketSize : rest.toNumber(); | ||
| // generate buffer | ||
| SMB2Request('read', { | ||
| 'FileId':file.FileId | ||
| , 'Length':packetSize | ||
| , 'Offset':offset.toBuffer() | ||
| }, connection, callback(chunckRequested, offset)); | ||
| offset = offset.add(packetSize); | ||
| chunckRequested++; | ||
| nbRemainingPackets++; | ||
| } | ||
| } | ||
|
|
||
| return readable; | ||
| } | ||
|
|
||
| function processOptions(options) { | ||
|
|
||
| // create the options object | ||
| if (options === undefined) { | ||
| options = {}; | ||
| } else if (typeof options === 'string') { | ||
| options = { encoding: options }; | ||
| } else if (typeof options === 'object') { | ||
| // this is a valid options object | ||
| } else { | ||
| throw new TypeError('"options" argument must be a string or an object'); | ||
| } | ||
|
|
||
| // check the start option | ||
| if (options.start === undefined) { | ||
| options.start = 0; | ||
| } else if (typeof options.start !== 'number') { | ||
| throw new TypeError('start (' + options.start + ') must be a Number'); | ||
| } | ||
|
|
||
| // check the end option | ||
| if (options.end === undefined) { | ||
| options.end = Infinity; | ||
| } else if (typeof options.end !== 'number') { | ||
| throw new TypeError('end (' + options.end + ') must be a Number'); | ||
| } | ||
|
|
||
| // check logical values for start and end | ||
| if (options.start > options.end) { | ||
| throw new Error('start (' + options.start + ') must be <= end (' + options.end + ')'); | ||
| } else if (options.start < 0) { | ||
| throw new Error('start (' + options.start + ') must be >= zero'); | ||
| } | ||
|
|
||
| return options; | ||
|
|
||
| } | ||
Oops, something went wrong.
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.
whoops I think it should be
readUInt32LE()sorry