Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
file post
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jun 30, 2017
1 parent d773cbe commit 3da89b0
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ program
host,
notSecure,
text,
file
filepath: file
})
} else {
showHelp()
Expand Down Expand Up @@ -103,19 +103,36 @@ function listen(props) {
}

function post(props) {
const {text, file} = props
const {text, filepath} = props

const url = constructWebsocketUrl(props)

const ws = new WebSocket(url)
ws.binaryType = 'arraybuffer'

const send = (arrayBuffer) => {
ws.send(arrayBuffer)
}

ws.on('open', () => {
console.log(`posting data to ${url}:\n\n${text}\n`)
const mime = 'text/plain'
const arrayBuffer = str2ab(text)
const abWithMime = arrayBufferWithMime(arrayBuffer, mime)
ws.send(abWithMime)
if (text) {
console.log(`posting data to ${url}:\n\n${text}\n`)
const mime = 'text/plain'
const arrayBuffer = str2ab(text)
const abWithMime = arrayBufferWithMime(arrayBuffer, mime)
send(abWithMime)
}

if (filepath) {
console.log(`posting file data to ${url}:\n\n${filepath}\n`)
const data = fs.readFileSync(filepath, 'utf8')

const mime = 'text/plain'
const arrayBuffer = str2ab(data)
const abWithMime = arrayBufferWithMime(arrayBuffer, mime)
send(abWithMime)
}

ws.close()
})

Expand Down

0 comments on commit 3da89b0

Please sign in to comment.