Browse files

Initial commit

  • Loading branch information...
0 parents commit a55b1d557686f7a37aca730073a459c78f3b6eb4 @tcr committed Jan 4, 2013
Showing with 108 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +23 −0 README.md
  3. +65 −0 index.js
  4. +11 −0 package.json
  5. +7 −0 test.js
2 .gitignore
@@ -0,0 +1,2 @@
+node_modules
+.smeeconf
23 README.md
@@ -0,0 +1,23 @@
+# smee, persistent local webhooks
+
+Create a webhook locally which persists between script runs, with no configuration required:
+
+```
+var smee = require('smee');
+smee.persistentHook(function (err, hook) {
+ console.log('Your webhook URL:', hook.url);
+ hook.on('callback', function (json) {
+ console.log('Received webhook:', json);
+ });
+});
+```
+
+## Requirements
+
+Install [localtunnel](https://github.com/progrium/localtunnel):
+
+ pip install localtunnel
+
+## License
+
+MIT
65 index.js
@@ -0,0 +1,65 @@
+var spawn = require('child_process').spawn
+ , util = require('util')
+ , EventEmitter = require('events').EventEmitter;
+
+var express = require('express')
+ , nconf = require('nconf');
+
+nconf.file('.smeeconf');
+
+var PORT = 9009;
+
+util.inherits(Hook, EventEmitter);
+
+function Hook () {
+}
+
+exports.persistentHook = function (callback) {
+ var hook = new Hook();
+ var name = 'smee' + String(Math.random()).substr(2);
+
+ if (!nconf.get('tunnel')) {
+ nconf.set('tunnel', name);
+ nconf.save(startServer);
+ } else {
+ name = nconf.get('tunnel');
+ startServer();
+ }
+
+ function startServer () {
+ var app = express();
+
+ app.use(express.bodyParser());
+
+ app.all('/', function (req, res) {
+ if (res.json) {
+ hook.emit('callback', res.json);
+ }
+ res.send('cool');
+ })
+
+ app.listen(PORT, function () {
+ hook.local = 'http://localhost:' + PORT;
+ startTunnel();
+ });
+ }
+
+ function startTunnel () {
+ var lt = spawn('localtunnel-beta', ['-n', name, PORT]);
+ lt.stdout.on('data', function (data) {
+ data = String(data);
+ var look = 'is now accessible from ';
+ if (data.indexOf(look) > -1) {
+ hook.url = data.substr(data.indexOf(look) + look.length);
+ callback(null, hook);
+ } else {
+ callback(data, null);
+ }
+ })
+ lt.on('exit', function (code) {
+ if (code) {
+ callback(code, null);
+ }
+ })
+ }
+};
11 package.json
@@ -0,0 +1,11 @@
+{
+ "name": "smee",
+ "version": "0.0.1",
+ "main": "index.js",
+ "repository": "https://github.com/tcr/smee",
+ "author": "Tim Cameron Ryan",
+ "license": "MIT",
+ "dependencies": {
+ "nconf": "~0.6.7"
+ }
+}
7 test.js
@@ -0,0 +1,7 @@
+var smee = require('smee');
+smee.persistentHook(function (err, hook) {
+ console.log('Your webhook URL:', hook.url);
+ hook.on('callback', function (json) {
+ console.log('Received webhook:', json);
+ });
+});

0 comments on commit a55b1d5

Please sign in to comment.