Skip to content

Commit

Permalink
Merge pull request #467 from steelbrain/steelbrain/maintenance-rev2
Browse files Browse the repository at this point in the history
Maintenance
  • Loading branch information
steelbrain committed Aug 6, 2023
2 parents f3ba6ec + 59475bd commit d85310a
Show file tree
Hide file tree
Showing 7 changed files with 582 additions and 1,188 deletions.
35 changes: 0 additions & 35 deletions .circleci/config.yml

This file was deleted.

37 changes: 37 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: build_and_test

on:
push:
branches: [ main ]
pull_request:

jobs:
build_and_test:
runs-on: ubuntu-latest

container:
image: node:18

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Cache Yarn packages
uses: actions/cache@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Packager Version
run: yarn --version

- name: Install Dependencies
run: yarn

- name: Lint sources
run: yarn lint

- name: Test
run: yarn test
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,26 @@
],
"homepage": "https://github.com/steelbrain/node-ssh#readme",
"devDependencies": {
"@types/shell-escape": "^0.2.0",
"ava": "^3.15.0",
"@types/node": "18",
"@types/shell-escape": "^0.2.1",
"@types/ssh2": "^1.11.13",
"ava": "^5.3.1",
"eslint-config-steelbrain": "^11.0.0",
"node-pty": "^0.10.1",
"node-pty": "^1.0.0",
"ssh2": "^1.14.0",
"ssh2-streams": "^0.4.10",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
"dependencies": {
"@types/ssh2": "^1.11.10",
"is-stream": "^2.0.0",
"make-dir": "^3.1.0",
"sb-promise-queue": "^2.1.0",
"sb-scandir": "^3.1.0",
"shell-escape": "^0.2.0",
"ssh2": "^1.11.0"
"shell-escape": "^0.2.0"
},
"peerDependencies": {
"ssh2": "^1.14.0"
},
"ava": {
"files": [
Expand Down
44 changes: 24 additions & 20 deletions test/main-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,23 @@ sshit('connects to a server with an inline private key', async function (t, port
await connectWithInlinePrivateKey(port, client)
})
})
sshit('requests a shell that works', async function (t, port, client) {
await connectWithPassword(port, client)
const data = []
const shell = await client.requestShell()
shell.on('data', function (chunk) {
data.push(chunk)
})
shell.write('ls /\n')
await wait(50)
shell.end()
const joinedData = data.join('')
t.regex(joinedData, /ls \//)
})
sshit(
'requests a shell that works',
async function (t, port, client) {
await connectWithPassword(port, client)
const data: Buffer[] = []
const shell = await client.requestShell()
shell.on('data', function (chunk) {
data.push(chunk)
})
shell.write('ls /\n')
await wait(50)
shell.end()
const joinedData = data.join('')
t.regex(joinedData, /ls \//)
},
true,
)

sshit('creates directories with sftp properly', async function (t, port, client) {
await connectWithPassword(port, client)
Expand Down Expand Up @@ -218,7 +222,7 @@ sshit('puts entire directories at once', async function (t, port, client) {
getFixturePath('ignored/really/really/really/really/yes/deep files'),
getFixturePath('ignored/really/really/really/really/deep'),
]
const filesReceived = []
const filesReceived: string[] = []
const existsBefore = await Promise.all(remoteFiles.map((file) => exists(file)))
t.is(existsBefore.every(Boolean), false)
await client.putDirectory(getFixturePath('multiple'), getFixturePath('ignored'), {
Expand Down Expand Up @@ -251,7 +255,7 @@ sshit('gets entire directories at once', async function (t, port, client) {
getFixturePath('ignored-2/really/really/really/really/yes/deep files'),
getFixturePath('ignored-2/really/really/really/really/deep'),
]
const filesReceived = []
const filesReceived: string[] = []
const existsBefore = await Promise.all(localFiles.map((file) => exists(file)))
t.is(existsBefore.every(Boolean), false)
await client.getDirectory(getFixturePath('ignored-2'), getFixturePath('multiple'), {
Expand All @@ -269,7 +273,7 @@ sshit('gets entire directories at once', async function (t, port, client) {
})
sshit('allows stream callbacks on exec', async function (t, port, client) {
await connectWithPassword(port, client)
const outputFromCallbacks = { stdout: [], stderr: [] }
const outputFromCallbacks = { stdout: [] as Buffer[], stderr: [] as Buffer[] }
await client.exec('node', [getFixturePath('test-program')], {
stream: 'both',
onStderr(chunk) {
Expand All @@ -287,7 +291,7 @@ sshit('allows stream callbacks on exec', async function (t, port, client) {
})
sshit('allows stream callbacks on execCommand', async function (t, port, client) {
await connectWithPassword(port, client)
const outputFromCallbacks = { stdout: [], stderr: [] }
const outputFromCallbacks = { stdout: [] as Buffer[], stderr: [] as Buffer[] }
await client.execCommand(`node ${getFixturePath('test-program')}`, {
onStderr(chunk) {
outputFromCallbacks.stderr.push(chunk)
Expand Down Expand Up @@ -353,7 +357,7 @@ sshit('forwards an inbound TCP/IP connection to client', async function (t, port
resolve(undefined)
})

dispose()
setTimeout(() => dispose(), 100)
})

t.truthy(dispose)
Expand Down Expand Up @@ -414,7 +418,7 @@ sshit('forwards an inbound UNIX socket connection to client', async function (t,
resolve(undefined)
})

dispose()
setTimeout(() => dispose(), 100)
})

t.truthy(dispose)
Expand Down Expand Up @@ -465,7 +469,7 @@ sshit('forwards an inbound TCP/IP connection to client with automatically assign
resolve(undefined)
})

dispose()
setTimeout(() => dispose(), 100)
})

t.truthy(dispose)
Expand Down
68 changes: 34 additions & 34 deletions test/ssh-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,56 @@ function handleSFTP(accept) {
let dirHandle = 105185
const handles: Set<number> = new Set()
const dirHandles: Map<number, string[]> = new Map()
sftpStream.on('OPEN', function (reqid, filename, flags) {
sftpStream.on('OPEN', function (reqId, filename, flags) {
let handleId
try {
handleId = FS.openSync(filename, SFTPStream.flagsToString(flags))
} catch (error) {
console.error(error)
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}
handles.add(handleId)

const handle = Buffer.alloc(4)
handle.write(handleId.toString())
sftpStream.handle(reqid, handle)
sftpStream.handle(reqId, handle)
})
sftpStream.on('READ', function (reqid, givenHandle, offset, length) {
sftpStream.on('READ', function (reqId, givenHandle, offset, length) {
const handle = parseInt(givenHandle, 10)
if (!handles.has(handle)) {
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}

const contents = Buffer.alloc(length)
try {
FS.readSync(handle, contents, 0, length, offset)
} catch (error) {
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}
sftpStream.data(reqid, contents)
sftpStream.data(reqId, contents)
})
sftpStream.on('WRITE', function (reqid, givenHandle, offset, data) {
sftpStream.on('WRITE', function (reqId, givenHandle, offset, data) {
const handle = parseInt(givenHandle, 10)
if (!handles.has(handle)) {
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}

try {
FS.writeSync(handle, data, 0, data.length, offset)
sftpStream.status(reqid, STATUS_CODE.OK)
sftpStream.status(reqId, STATUS_CODE.OK)
} catch (error) {
console.error(error)
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
}
})
sftpStream.on('FSTAT', function (reqid, givenHandle) {
sftpStream.on('FSTAT', function (reqId, givenHandle) {
const handle = parseInt(givenHandle, 10)
if (!handles.has(handle)) {
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}

Expand All @@ -72,54 +72,54 @@ function handleSFTP(accept) {
stats = FS.fstatSync(handle)
} catch (error) {
console.error(error)
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}
sftpStream.attrs(reqid, stats)
sftpStream.attrs(reqId, stats)
})
sftpStream.on('CLOSE', function (reqid, givenHandle) {
sftpStream.on('CLOSE', function (reqId, givenHandle) {
const handle = parseInt(givenHandle, 10)
if (dirHandles.has(handle)) {
dirHandles.delete(handle)
sftpStream.status(reqid, STATUS_CODE.OK)
sftpStream.status(reqId, STATUS_CODE.OK)
return
}
if (handles.has(handle)) {
handles.delete(handle)
FS.close(handle, function () {
/* No Op */
})
sftpStream.status(reqid, STATUS_CODE.OK)
sftpStream.status(reqId, STATUS_CODE.OK)
} else {
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
}
})
sftpStream.on('MKDIR', function (reqid, path, attrs) {
sftpStream.on('MKDIR', function (reqId, path, attrs) {
try {
FS.mkdirSync(path, attrs.mode)
sftpStream.status(reqid, STATUS_CODE.OK)
sftpStream.status(reqId, STATUS_CODE.OK)
} catch (error) {
sftpStream.status(reqid, STATUS_CODE.FAILURE, error.message)
sftpStream.status(reqId, STATUS_CODE.FAILURE, error.message)
}
})
sftpStream.on('STAT', function (reqid, path) {
sftpStream.on('STAT', function (reqId, path) {
try {
const stats = FS.statSync(path)
sftpStream.attrs(reqid, stats)
sftpStream.attrs(reqId, stats)
} catch (error) {
sftpStream.status(reqid, STATUS_CODE.FAILURE, error.message)
sftpStream.status(reqId, STATUS_CODE.FAILURE, error.message)
}
})
sftpStream.on('OPENDIR', function (reqid, path) {
sftpStream.on('OPENDIR', function (reqId, path) {
let stat
try {
stat = FS.statSync(path)
} catch (error) {
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}
if (!stat.isDirectory()) {
sftpStream.status(reqid, STATUS_CODE.FAILURE)
sftpStream.status(reqId, STATUS_CODE.FAILURE)
return
}
const contents = FS.readdirSync(path)
Expand All @@ -129,19 +129,19 @@ function handleSFTP(accept) {
dirHandles.set(currentDirHandle, contents)
const handle = Buffer.alloc(8)
handle.write(currentDirHandle.toString())
sftpStream.handle(reqid, handle)
sftpStream.handle(reqId, handle)
})
sftpStream.on('READDIR', function (reqid, givenHandle) {
sftpStream.on('READDIR', function (reqId, givenHandle) {
const handle = parseInt(givenHandle, 10)
const contents = dirHandles.get(handle)
if (contents == null || !contents.length) {
sftpStream.status(reqid, STATUS_CODE.EOF)
sftpStream.status(reqId, STATUS_CODE.EOF)
return
}

const item = contents.pop()

sftpStream.name(reqid, [
sftpStream.name(reqId, [
{
filename: item,
longname: item,
Expand Down Expand Up @@ -180,8 +180,8 @@ function handleSession(acceptSession) {
const response = accept()
const spawnedProcess = ChildProcess.exec(info.command)
response.pipe(spawnedProcess.stdin)
spawnedProcess.stdout.pipe(response.stdout)
spawnedProcess.stderr.pipe(response.stderr)
spawnedProcess.stdout?.pipe(response.stdout)
spawnedProcess.stderr?.pipe(response.stderr)
})
session.on('sftp', handleSFTP)
}
Expand Down
2 changes: 1 addition & 1 deletion test/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test('throws if host is not valid', async function (t) {
await normalizeConfig({
username: 'asd',
host: null,
})
} as never)
},
'Either config.host or config.sock must be provided',
)
Expand Down
Loading

0 comments on commit d85310a

Please sign in to comment.