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

Commit

Permalink
cli init
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jun 30, 2017
1 parent 5d11402 commit 56d1fa9
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,3 @@ tmp

# Files to ignore #
###################
cli.js
2 changes: 1 addition & 1 deletion bin/streamhut.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node

const path = require(`path`);
//require(path.join(`../cli`))
require(path.join(__dirname, `../cli`))
121 changes: 121 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const program = require('commander')
const {
arrayBufferWithMime,
arrayBufferMimeDecouple
} = require('arraybuffer-mime')
const WebSocket = require('ws')

const packageJson = require('./package.json')

program
.version(packageJson.version)
.option('-h, --host <host>', 'host URL', null, null)
.option('--not-secure', 'not using SSL.', null, false)
.option('-c, --channel <id>', 'channel ID', null, null)
.option('-t, --text <text>', 'text to send', null, null)
.option('-f, --file <filepath>', 'file to send', null, null)
.command('post', 'post to a channel')
.command('listen', 'listen on a channel')
.action((cmd) => {
const {
host,
notSecure,
channel,
text,
file
} = program

if (!host || !channel) {
program.outputHelp()
return false
}

if (cmd === 'listen') {
listen({
channel,
host,
notSecure
})
} else if (cmd === 'post') {
}
})

program.parse(process.argv)

function listen(props) {
const ws = new WebSocket(constructWebsocketUrl(props))
ws.binaryType = 'arraybuffer'

ws.on('open', () => {
console.log('connected.')
})

ws.on('error', (error) => {
console.error(error)
})

ws.on('message', (data) => {
if (isArrayBuffer(data)) {
const {mime, arrayBuffer} = arrayBufferMimeDecouple(data)
const buffer = Buffer.from(arrayBuffer)

// TODO
// check mime type
var str = buffer.toString('utf8')
if (str) {
console.log(`received ${new Date()}:`)
console.log(str)
}
}
})
}

function post(props) {
const {host, channel, notSecure} = props
const scheme = notSecure ? 'ws' : 'wss'


const ws = new WebSocket(constructWebsocketUrl(props))
ws.binaryType = 'arraybuffer'

ws.on('open', () => {
const mime = 'text/plain'
const arrayBuffer = str2ab('yabababb')
const abWithMime = arrayBufferWithMime(arrayBuffer, mime)
ws.send(abWithMime)
})

ws.on('error', (error) => {
if (/400|ECONNREFUSED/gi.test(error.message)) {
console.error('no on is listening on ', path)
}
})
}

function constructWebsocketUrl(props) {
const {host, channel, notSecure} = props
const scheme = notSecure ? 'ws' : 'wss'

if (!host || !channel) {
program.outputHelp()
return false
}

const path = (channel||'').replace(/^\/?/, '/')

return `${scheme}://${host}${path}`
}

//https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2) // 2 bytes for each char
var bufView = new Uint16Array(buf)
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i)
}
return buf
}

function isArrayBuffer(data) {
return /ArrayBuffer/gi.test(Object.prototype.toString.call(data))
}
19 changes: 19 additions & 0 deletions hut.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
( )
( )
( )
( )
) )
( ( /\
(_) / \ /\
________[_]________ /\/ \/ \
/\ /\ ______ \ / /\/\ /\/\
/ \ //_\ \ /\ \ /\/\/ \/ \
/\ / /\/\ //___\ \__/ \ \/
/ \ /\/ \//_____\ \ |[]| \
/\/\/\/ //_______\ \|__| \
/ \ /XXXXXXXXXX\ \
\ /_I_II I__I_\__________________\
I_I| I__I_____[]_|_[]_____I
I_II I__I_____[]_|_[]_____I
I II__I I XXXXXXX I
~~~~~" "~~~~~~~~~~~~~~~~~~~~~~~~
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"description": "Stream stuff",
"main": "server.js",
"bin": {
"btc-dash": "bin/index.js"
"streamhut": "bin/streamhut.js"
},
"dependencies": {
"arraybuffer-mime": "0.0.1",
"commander": "^2.10.0",
"ecstatic": "^0.5.5",
"hyperlinkify": "0.0.3",
"randomstring": "^1.0.3",
Expand Down
1 change: 1 addition & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>streamhut</title>
<meta charset="utf-8">
<meta name="description" content="stream stuff" />
<meta name="viewport" content="initial-scale=1, user-scalable=yes, width=device-width" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
Expand Down
5 changes: 3 additions & 2 deletions static/scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ form.addEventListener(`submit`, event => {
const files = [].slice.call(fileInput.files)

files.forEach(file => {
console.log(`file:`, file)
console.log(`file:`, file, file.type)
if (!file) return

const reader = new FileReader()
Expand Down Expand Up @@ -1016,7 +1016,6 @@ ws.addEventListener('message', event => {
const data = event.data

console.log('incoming...')
console.log(data)

try {
const json = JSON.parse(data)
Expand All @@ -1034,6 +1033,8 @@ ws.addEventListener('message', event => {

const {mime, arrayBuffer} = arrayBufferMimeDecouple(data)

console.log('received', mime)

const blob = new Blob([arrayBuffer], {type: mime})

let ext = mime.split(`/`).join(`_`).replace(/[^\w\d_]/gi, ``)
Expand Down
5 changes: 3 additions & 2 deletions static/scripts/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ form.addEventListener(`submit`, event => {
const files = [].slice.call(fileInput.files)

files.forEach(file => {
console.log(`file:`, file)
console.log(`file:`, file, file.type)
if (!file) return

const reader = new FileReader()
Expand Down Expand Up @@ -105,7 +105,6 @@ ws.addEventListener('message', event => {
const data = event.data

console.log('incoming...')
console.log(data)

try {
const json = JSON.parse(data)
Expand All @@ -123,6 +122,8 @@ ws.addEventListener('message', event => {

const {mime, arrayBuffer} = arrayBufferMimeDecouple(data)

console.log('received', mime)

const blob = new Blob([arrayBuffer], {type: mime})

let ext = mime.split(`/`).join(`_`).replace(/[^\w\d_]/gi, ``)
Expand Down

0 comments on commit 56d1fa9

Please sign in to comment.