Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ssh-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import { Buffer } from 'node:buffer'
import glob from './glob.ts'
import { spawnSync } from 'node:child_process'
import os from 'node:os'
Expand Down Expand Up @@ -230,7 +231,7 @@ export default class SSHConfig extends Array<Line> {
/**
* Parse SSH config text into structured object.
*/
public static parse(text: string): SSHConfig {
public static parse(text: string | Buffer): SSHConfig {
return parse(text)
}

Expand Down Expand Up @@ -531,14 +532,17 @@ export default class SSHConfig extends Array<Line> {
/**
* Parse SSH config text into structured object.
*/
export function parse(text: string): SSHConfig {
export function parse(text: string | Buffer): SSHConfig {
// Handle Buffer input by converting to string
const input: string = typeof text === 'string' ? text : text.toString('utf-8')

let i = 0
let chr = next()
let config: SSHConfig = new SSHConfig()
let configWas = config

function next() {
return text[i++]
return input[i++]
}

function space(): Space {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,14 @@ describe('parse', function() {
assert.equal(config[0].type, LineType.DIRECTIVE)
assert.equal(config[0].value, 'name#with#hash')
})

// https://github.com/cyjake/ssh-config/issues/103
it('.parse Buffer input', async function() {
const text = await readFixture('config')
const buffer = Buffer.from(text, 'utf-8')
const configFromBuffer = parse(buffer)
const configFromString = parse(text)

assert.deepEqual(configFromBuffer, configFromString)
})
})