// With promise, ideal with ES2017 async functions
constcontent=awaitsmb2Client.readFile('foo.txt');
console.log(content);
```
### Construction
> `var smb2Client = new SMB2 ( options )`
The SMB2 class is the constructor of your SMB2 client.
the parameter ```options``` accepts this list of attributes:
the parameter `options` accepts this list of attributes:
-```share``` (mandatory): the share you want to access
-```domain``` (mandatory): the domain of which the user is registred
-```username``` (mandatory): the username of the user that access the share
-```password``` (mandatory): the password
-```port``` (optional): default ```445```, the port of the SMB server
-```packetConcurrency``` (optional): default ```20```, the number of simulatanous packet when writting / reading data from the share
-```autoCloseTimeout``` (optional): default ```10000```, the timeout in milliseconds before to close the SMB2 session and the socket, if setted to ```0``` the connection will never be closed unless you do it
-`share`: the share you want to access
-`domain`: the domain of which the user is registered
-`username`: the username of the user that access the share
-`password`: the password
-`port` (optional): default `445`, the port of the SMB server
-`packetConcurrency` (optional): default `20`, the number of simultaneous packet when writing / reading data from the share
-`autoCloseTimeout` (optional): default `0`, the timeout in milliseconds before to close the SMB2 session and the socket, if set to `0` the connection will never be closed unless you do it
Example:
```javascript
// load the library
varSMB2=require('smb2');
// create an SMB2 instance
varsmb2Client=newSMB2({
share:'\\\\000.000.000.000\\c$'
,domain:'DOMAIN'
,username:'username'
,password:'password!'
share:'\\\\000.000.000.000\\c$',
domain:'DOMAIN',
username:'username',
password:'password!',
});
```
### smb2Client.readdir ( path, callback )
Asynchronous readdir(3). Reads the contents of a directory. The callback gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.
### Connection management
The connection to the SMB server will be automatically open when necessary.
Unless you have set `autoCloseTimeout` to `0` during client construction, the connection will be closed automatically.
If you have set `autoCloseTimeout` to `0`, the connection MUST be closed manually:
```js
smb2Client.disconnect();
```
### High level methods
> `smb2Client.ensureDir ( path, callback )`
Ensures that the directory exists. If the directory structure does not exist, it is created.
Returns a read stream on the file. Unlike fs.createReadStream, this function is asynchronous, as we need use asynchronous smb requests to get the stream.
-`filename` String
-`data` String | Buffer
-`options` Object
-`encoding` String | Null default = `'utf8'`
-`callback` Function
Asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer.
The encoding option is ignored if data is a buffer.
Returns a write stream on the file. Unlike fs.createWriteStream, this function is asynchronous, as we need use asynchronous smb requests to get the stream.
Buffer.alloc(10),// buffer where to store the data
0,// offset in the buffer
10,// number of bytes to read
0,// offset in the file
function(err,bytesRead,buffer){
smb2Client.close(fd,function(){});
if(err)throwcb(err);
console.log(bytesRead,buffer);
}
);
});
```
> This API is modeled after Node's `fs` module.
...
...
@@ -194,6 +274,7 @@ smb2Client.open('path\\to\\the\\file', function (err, fd) {
> Note: be careful of `autoCloseTimeout` with this process as it is not intended to cover multiple method calls, you should set it to `0` and manually `disconnect()`.