Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Running a node.js REPL over `curl`
/**
* Requires node v0.7.7 or greater.
*
* To connect: $ curl -sSNT. localhost:8000
*/
var http = require('http')
, repl = require('repl')
, buf0 = new Buffer([0])
var server = http.createServer(function (req, res) {
res.setHeader('content-type', 'multipart/octet-stream')
res.write('Welcome to the Fun House\r\n')
repl.start({
prompt: 'curl repl> '
, input: req
, output: res
, terminal: false
, useColors: true
, useGlobal: false
})
// log
console.log(req.headers['user-agent'])
// hack to thread stdin and stdout
// simultaneously in curl's single thread
var iv = setInterval(function () {
res.write(buf0)
}, 100)
res.connection.on('end', function () {
clearInterval(iv)
})
})
server.listen(8000)
☮ ~ (master) ⚡ curl -sSNT. localhost:8000
Welcome to the Fun House
curl repl> process.platform
'darwin'
curl repl> process.arch
'x64'
curl repl> process.cwd()
'/Users/nrajlich'
curl repl> path
{ resolve: [Function],
normalize: [Function],
join: [Function],
relative: [Function],
dirname: [Function],
basename: [Function],
extname: [Function],
_makeLong: [Function] }
curl repl> ^C
☮ ~ (master) ⚡
@chemdemo

This comment has been minimized.

Copy link

@chemdemo chemdemo commented Dec 3, 2012

jnjnj

@grantgeorge

This comment has been minimized.

Copy link

@grantgeorge grantgeorge commented May 10, 2016

nice

@crazyohpooh

This comment has been minimized.

Copy link

@crazyohpooh crazyohpooh commented May 26, 2017

Hey

@mk-pmb

This comment has been minimized.

Copy link

@mk-pmb mk-pmb commented Sep 18, 2017

The -sSNT curl options decode to: --silent --show-error --no-buffer --upload-file .. The dot as file name means to read file content from stdin in non-blocking mode, aka "show downloaded data while I'm still typing". I'd prefix the command with rlwrap to add line editing.

@SamB

This comment has been minimized.

Copy link

@SamB SamB commented Aug 3, 2019

@mk-pmb Now you tell me, after I already looked that all those up!

@mk-pmb

This comment has been minimized.

Copy link

@mk-pmb mk-pmb commented Aug 3, 2019

@SamB You're welcome! :-)

@coderofsalvation

This comment has been minimized.

Copy link

@coderofsalvation coderofsalvation commented Jan 28, 2021

interesting, it works fine when running with the http-module.
However, when running as express middleware, the null-byte(s) in setInterval are printed as spaces in curl output.
Any idea what express might be adding here?

@mk-pmb

This comment has been minimized.

Copy link

@mk-pmb mk-pmb commented Jan 28, 2021

Sounds like express, indeed, sends null bytes. Or maybe you're using a different terminal, or different settings so that now they're rendered as space, rather than ignored. Anyway, you can use stdbuf -i0 -o0 -e0 tr -d '\000' to filter them out.

@coderofsalvation

This comment has been minimized.

Copy link

@coderofsalvation coderofsalvation commented Jan 28, 2021

ah thanks, that makes sense.
In the meantime I've also discovered polka, an express alternative which doesn't abstracts the native http req & res away.
I've quickly put this together for re-use:

https://github.com/coderofsalvation/middleware-remoteshell

Hopefully this allows me and others to route all stderr/stdout into a tmux mega-dashboard :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment