@marekp995/pv-node-smb2
v2.0.8
Published
SMB2 Client
Readme
SMB2 Client for Node.js
Introduction
This library calls the C libraries for libsmbclient to read and search through directories.
The original fork was unable to connect to newer servers, and rather than hacking about with the smb2 protocol, it is far easier to leverage the great work of the samba team, and use their libsmbclient library via NAPI bindings. This is much quicker than other wrappers that fork/exec smbclient, and the only OS pre-req is to install the libsmbclient library.
Prerequisites
macOS
Install samba via Homebrew:
brew install sambaLinux
Install the libsmbclient development package:
sudo apt-get install libsmbclient-dev # Debian/Ubuntu
# or
sudo yum install libsmbclient-devel # RHEL/CentOSInstallation
npm install -S pv-node-smb2API
Asynchronicity
No async calls supported yet; there's a skeleton for a read function, but much more work is still needed.
Sync calls
Reading Files
import {PvDirent,PvNodeSmb2} from 'pv-node-smb2'
const smb2Client = new PvNodeSmb2('workgroup', 'user', 'password')
// Read a file from SMB share
const content = smb2Client.readFileSync('smb://<host or IP>/Windows Path/goes/here/foo.txt')
console.log(content.toString('base64'))
// or
console.log(content.toString('utf8'))Getting File Information
// Get file metadata
const fileInfo: PvDirent = smb2Client.fileInfo('smb://<host or IP>/Windows Path/goes/here/foo.txt')
console.log(`File: ${fileInfo.name}`)
console.log(`Size: ${fileInfo.size} bytes`)
console.log(`Is File: ${fileInfo.isFile}`)
console.log(`Is Directory: ${fileInfo.isDir}`)
console.log(`Modified: ${fileInfo.mtime}`)
console.log(`Created: ${fileInfo.btime}`)Reading Directories
// Read directory contents
const entries: PvDirent[] = smb2Client.readDir('smb://<host or IP>/Windows Path/goes/here/')
entries.forEach(entry => {
console.log(`${entry.isDir ? '[DIR]' : '[FILE]'} ${entry.name} (${entry.size} bytes)`)
})Writing Files
// Write a buffer to SMB share (overwrites if file exists)
const dataBuffer = Buffer.from("Hello, World!", "utf8")
const bytesWritten = smb2Client.writeFileSync('smb://<host or IP>/Windows Path/goes/here/foo.txt', dataBuffer)
console.log(`Written ${bytesWritten} bytes`)Renaming/Moving Files
// Rename or move a file on SMB share
const result = smb2Client.renameFileSync(
'smb://<host or IP>/Windows Path/goes/here/foo.txt',
'smb://<host or IP>/Windows Path/goes/here/bar.txt'
)
// Returns 0 if successfulDeleting Files
// Delete a file from SMB share
const result = smb2Client.unlinkFileSync('smb://<host or IP>/Windows Path/goes/here/foo.txt')
// or use the alias
const result2 = smb2Client.deleteFileSync('smb://<host or IP>/Windows Path/goes/here/bar.txt')
// Returns 0 if successfulComplete Example
import {PvDirent, PvNodeSmb2} from 'pv-node-smb2'
const smb2Client = new PvNodeSmb2('workgroup', 'user', 'password')
try {
// Read a file
const content = smb2Client.readFileSync('smb://server/share/file.txt')
console.log(content.toString('utf8'))
// Get file info
const info = smb2Client.fileInfo('smb://server/share/file.txt')
console.log(`File size: ${info.size} bytes`)
// List directory
const entries = smb2Client.readDir('smb://server/share/')
entries.forEach(entry => {
console.log(entry.name)
})
// Write a file
const data = Buffer.from('Hello from Node.js!', 'utf8')
smb2Client.writeFileSync('smb://server/share/newfile.txt', data)
// Rename a file
smb2Client.renameFileSync(
'smb://server/share/newfile.txt',
'smb://server/share/renamed.txt'
)
// Delete a file
smb2Client.unlinkFileSync('smb://server/share/renamed.txt')
} catch (error) {
console.error('SMB operation failed:', error)
} finally {
// Clean up connection
smb2Client.deleteContext()
}Connection management
Each instance of the PvNodeSmb2 class will have its own connection.
Contributors
References
The[MS-SMB2]: Server Message Block (SMB) Protocol Versions 2 and 3
Copyright (C) 2014 Microsoft
http://msdn.microsoft.com/en-us/library/cc246482.aspxLicense
(The MIT License)
Copyright (c) 2013-2014 Benjamin Chelli <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
