Commit 08003d55 authored by Fabrice Marsaud's avatar Fabrice Marsaud

Read and Write streams

parent 69414849
{
"comments": false,
"compact": true,
"optional": [
// Experimental features.
// "minification.constantFolding",
// "minification.deadCodeElimination",
"es7.asyncFunctions",
"es7.decorators",
"es7.exportExtensions",
"es7.functionBind"
]
}
import Bigint from '../tools/bigint'
import Bluebird from 'bluebird'
import {Readable} from 'stream'
import {request} from '../tools/smb2-forge'
const requestAsync = Bluebird.promisify(request)
const maxPacketSize = 0x00010000
class SmbReadableStream extends Readable {
constructor (connection, file, options) {
super(options)
this.connection = connection
this.encoding = options && options.encoding
this.file = file
this.offset = new Bigint(8)
let fileLength = 0
for (let i = 0; i < file.EndofFile.length; i++) {
fileLength |= file.EndofFile[i] << (i * 8)
}
this.fileLength = fileLength
this.wait = false
}
async _read (size) {
while (this.offset.lt(this.fileLength)/* && size > 0*/) {
if (this.wait) {
return
}
const rest = this.offset.sub(this.fileLength).neg()
const packetSize = Math.min(maxPacketSize, rest.toNumber/*, size*/)
const offset = new Bigint(this.offset)
this.wait = true
let content = await requestAsync('read', {
FileId: this.file.FileId,
Length: packetSize,
Offset: offset.toBuffer()
}, this.connection)
this.wait = false
if (this.encoding) {
content = content.toString(this.encoding)
}
this.offset = this.offset.add(packetSize)
// size -= packetSize
if (!this.push(content)) {
return
}
}
if (this.offset.ge(this.fileLength)) {
await requestAsync('close', this.file, this.connection)
this.push(null)
}
}
}
export default function (path, options, cb) {
if(typeof options == 'function'){
cb = options;
options = {};
}
request('open', {path}, this, (err, file) => {
if (err) {
cb(err)
} else {
cb(null, new SmbReadableStream(this, file, options))
}
})
}
import Bigint from '../tools/bigint'
import Bluebird from 'bluebird'
import {request} from '../tools/smb2-forge'
import {Writable} from 'stream'
const requestAsync = Bluebird.promisify(request)
const maxPacketSize = new Bigint(8, 0x00010000 - 0x71)
class SmbWritableStream extends Writable {
constructor (connection, file, options) {
super(options)
this.connection = connection
this.encoding = options.encoding || 'utf8'
this.file = file
this.offset = new Bigint(8)
}
async _write (chunk, encoding, next) {
const count = this.count++
encoding = this.encoding || encoding
chunk = Buffer.isBuffer(chunk) ? chunk : new Buffer(chunk, encoding)
while(chunk.length > 0) {
const packetSize = Math.min(maxPacketSize.toNumber(), chunk.length)
const packet = chunk.slice(0, packetSize)
chunk = chunk.slice(packetSize)
const offset = new Bigint(this.offset)
this.offset = this.offset.add(packetSize)
await requestAsync('write', {
FileId: this.file.FileId,
Offset: offset.toBuffer(),
Buffer: packet
}, this.connection)
}
next()
}
async end (...args) {
try {
super.end(...args)
} finally {
await requestAsync('close', this.file, this.connection)
}
}
}
export default function (path, options, cb) {
if(typeof options == 'function'){
cb = options;
options = {};
}
request('create', {path}, this, (err, file) => {
if (err) {
cb(err)
} else {
cb(null, new SmbWritableStream(this, file, options))
}
})
}
......@@ -88,6 +88,8 @@ proto.exists = SMB2Connection.requireConnect(require('./api/exists'));
proto.rename = SMB2Connection.requireConnect(require('./api/rename'));
proto.readFile = SMB2Connection.requireConnect(require('./api/readfile'));
proto.createReadStream = SMB2Connection.requireConnect(require('./api/createReadStream'));
proto.createWriteStream = SMB2Connection.requireConnect(require('./api/createWriteStream'));
proto.writeFile = SMB2Connection.requireConnect(require('./api/writefile'));
proto.unlink = SMB2Connection.requireConnect(require('./api/unlink'));
......
{
"name":"smb2"
, "description":"SMB2 Client"
, "homepage": "https://github.com/bchelli/node-smb2"
, "version":"0.2.6"
, "engines": [
"name": "smb2",
"description": "SMB2 Client",
"homepage": "https://github.com/bchelli/node-smb2",
"version": "0.2.6",
"engines": [
"node"
]
, "author":{
"name": "Benjamin Chelli"
, "email": "benjamin@chelli.net"
, "url": "https://github.com/bchelli"
}
, "main":"lib/smb2.js"
, "repository": {
"type": "git"
, "url": "https://github.com/bchelli/node-smb2"
}
, "dependencies": {
],
"author": {
"name": "Benjamin Chelli",
"email": "benjamin@chelli.net",
"url": "https://github.com/bchelli"
},
"main": "lib/smb2.js",
"repository": {
"type": "git",
"url": "https://github.com/bchelli/node-smb2"
},
"dependencies": {
"ntlm": "~0.1.1"
},
"keywords": [
"SMB",
"SMB2",
"SMB3",
"NTLM",
"CIFS",
"Samba"
],
"scripts": {
"build": "./node_modules/.bin/babel --source-maps --out-dir=lib/api/ lib/api/src"
},
"devDependencies": {
"babel": "^5.8.34",
"babel-eslint": "^4.1.6",
"source-map-support": "^0.4.0"
},
"standard": {
"parser": "babel-eslint"
}
, "keywords": [
"SMB"
, "SMB2"
, "SMB3"
, "NTLM"
, "CIFS"
, "Samba"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment