From d87c3b7d61fb61cd5d0b1f2742257a7a1b749c33 Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Sun, 8 Jan 2012 23:44:04 -0600 Subject: [PATCH 1/5] Escape document when using the txt extension --- static/application.js | 11 ++++++++++- static/application.min.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/static/application.js b/static/application.js index e21f447..cff7443 100644 --- a/static/application.js +++ b/static/application.js @@ -4,6 +4,15 @@ var haste_document = function() { this.locked = false; }; +// Escapes HTML tag characters +haste_document.prototype.htmlEscape = function(s) { + return s + .replace(/&/g, '&') + .replace(/>/g, '>') + .replace(/'+a+"");$("#messages").prepend(c),setTimeout(function(){c.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c";$("#linenos").html(b)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){a?(c.$code.html(a.value),c.setTitle(a.key),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus(),c.addLineNumbers(a.lineCount)):c.newDocument()},this.lookupTypeByExtension(b[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var a=this.doc.data;this.newDocument(),this.$textarea.val(a)}},haste.prototype.lockDocument=function(){var a=this;this.doc.save(this.$textarea.val(),function(b,c){if(b)a.showMessage(b.message,"error");else if(c){a.$code.html(c.value),a.setTitle(c.key);var d="/"+c.key;c.language&&(d+="."+a.lookupExtensionByType(c.language)),window.history.pushState(null,a.appName+"-"+c.key,d),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus(),a.addLineNumbers(c.lineCount)}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&a.keyCode===83},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&a.keyCode===78},shortcutDescription:"control + n",action:function(){a.newDocument(!a.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(b){return a.doc.locked&&b.ctrlKey&&b.keyCode===68},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.ctrlKey&&b.keyCode==84},shortcutDescription:"control + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var b=0;b/g,">").replace(/'+a+"");$("#messages").prepend(c),setTimeout(function(){c.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c";$("#linenos").html(b)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){a?(c.$code.html(a.value),c.setTitle(a.key),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus(),c.addLineNumbers(a.lineCount)):c.newDocument()},this.lookupTypeByExtension(b[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var a=this.doc.data;this.newDocument(),this.$textarea.val(a)}},haste.prototype.lockDocument=function(){var a=this;this.doc.save(this.$textarea.val(),function(b,c){if(b)a.showMessage(b.message,"error");else if(c){a.$code.html(c.value),a.setTitle(c.key);var d="/"+c.key;c.language&&(d+="."+a.lookupExtensionByType(c.language)),window.history.pushState(null,a.appName+"-"+c.key,d),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus(),a.addLineNumbers(c.lineCount)}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&a.keyCode===83},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&a.keyCode===78},shortcutDescription:"control + n",action:function(){a.newDocument(!a.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(b){return a.doc.locked&&b.ctrlKey&&b.keyCode===68},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.ctrlKey&&b.keyCode==84},shortcutDescription:"control + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var b=0;b Date: Mon, 9 Jan 2012 22:12:00 -0500 Subject: [PATCH 2/5] Revert "updated icon set to include "raw/just text"" This reverts commit 31a701d491c49e39b688db62ba9f004d97d8bef8. --- static/function-icons.png | Bin 6178 -> 6824 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/static/function-icons.png b/static/function-icons.png index 575cf876d53fc56cca21c1bb74e461ffc9f3ad47..ba4cfb19f174c9fda40afc75a78215bcaf5506d1 100644 GIT binary patch delta 4148 zcmb7IdpOf?{~rs5T1mb`$Tt#_Rt{k^AvxqUryMee2$Mt0X_*h5G|f6VL?L8jDU^uF zA%}f)Xkv0clro00MpjP0&$@ok_4{4d^ZfJtvCppE_xp6e?$`Tu-}@9EDbZCXIoO@r zE-ohyfk3ufS(;^p9UtI ztEFnUb+!AN^uCeiQboMzrdRGtNrrz~7*}DSf36^h*tJaFDEk5v@1HP{ox3_pjQ&0Z z)UhwW|CNi6W}c@YL^C)+eO_-wG;|xRsqJob7GEPn4ok&Ml{$ySXK?=M>0FX2u~28g zej8j6S`;?PYmFv<=QraLUqM8(p!3iJKUDJOBi>_tu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t%X^||VW9Uk&{3cvj zx7{kY|B!pmA6bn_6W1(k!9)x&d6+vAM-@PTP-Zx71 z7azF!3ISx!x%^qh#E`xMU~>MI8y))F<@sZz;KF57yxY=_PIi&Y`iak%q>($Z%7nsI z=vT8d8q+u`(>G8HL!Z>rC^`;Rk0pAHG7{}Um|?{=D7^g%G;v+;JSbwyw9!Vxhu(G^ z1BJy9xu>HHUJXy;8mjdDR{Ytyel4i#>#HKtrHj$X5k?=7U=vGa3P4`goGPV{ecq>h zk@Y)^26)%Rw&LNQ7HFf$(r)zC@5F51h6%oc;^wx&A1Y&vzLsQjnv6+YDX1|9Yc+tO zhcpoWt-P(;3tn5SU{D)fmw(+%~z5XviR;+PY_q!s7PA>;LjEBv$q5X%Ypc~^8m5egF*l5v=b>J4u`sXt9 zyhOAu%?aaHh12=ogusv=B!y4$5o9#bMyAE|70X;x+7R^4@>YDs&)Uoda?-oDTU5{l zHzrK(g=cj|=?6EVLOB<+xLyy}#u0|qW?QbhA1~6H8*MZnLA~kUl?67v3a~du{SE{T z8e#&*QO|R=JSK;!%qw7das0Qug2&4JhH`TLU#h)oEWw<#D{Q2UrG_yt2 In0Vg(ADn^#Qvd(} From 6e4c08731907fe2468f36f6c4483b81fadff3c57 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 13 Jan 2012 11:16:42 -0500 Subject: [PATCH 3/5] Remove hashlib dependency and switch to mocha for testing --- lib/document_handler.js | 8 +++- lib/file_document_store.js | 13 +++++-- lib/key_generators/random.js | 3 ++ lib/redis_document_store.js | 1 - package.json | 6 +-- spec/document_handler_spec.js | 11 ++++-- spec/redis_document_store_spec.js | 65 +++++++------------------------ 7 files changed, 42 insertions(+), 65 deletions(-) diff --git a/lib/document_handler.js b/lib/document_handler.js index c8aec71..07291e7 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -86,9 +86,9 @@ DocumentHandler.prototype.handlePost = function(request, response) { }); }; -// Get a random key that hasn't been already used +// Keep choosing keys until one isn't taken DocumentHandler.prototype.chooseKey = function(callback) { - var key = this.keyGenerator.createKey(this.keyLength); + var key = this.acceptableKey(); var _this = this; this.store.get(key, function(ret) { if (ret) { @@ -99,4 +99,8 @@ DocumentHandler.prototype.chooseKey = function(callback) { }); }; +DocumentHandler.prototype.acceptableKey = function() { + return this.keyGenerator.createKey(this.keyLength); +}; + module.exports = DocumentHandler; diff --git a/lib/file_document_store.js b/lib/file_document_store.js index 76492f8..a0666fd 100644 --- a/lib/file_document_store.js +++ b/lib/file_document_store.js @@ -1,7 +1,7 @@ var fs = require('fs'); +var crypto = require('crypto'); var winston = require('winston'); -var hashlib = require('hashlib'); // For storing in files // options[type] = file @@ -12,12 +12,19 @@ var FileDocumentStore = function(options) { this.expire = options.expire; }; +// Generate md5 of a string +FileDocumentStore.md5 = function(str) { + var md5sum = crypto.createHash('md5'); + md5sum.update(str); + return md5sum.digest('hex'); +}; + // Save data in a file, key as md5 - since we don't know what we could be passed here FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) { try { var _this = this; fs.mkdir(this.basePath, '700', function() { - fs.writeFile(_this.basePath + '/' + hashlib.md5(key), data, 'utf8', function(err) { + fs.writeFile(_this.basePath + '/' + _this.md5(key), data, 'utf8', function(err) { if (err) { callback(false); } @@ -37,7 +44,7 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) { // Get data from a file from key FileDocumentStore.prototype.get = function(key, callback, skipExpire) { var _this = this; - fs.readFile(this.basePath + '/' + hashlib.md5(key), 'utf8', function(err, data) { + fs.readFile(this.basePath + '/' + _this..md5(key), 'utf8', function(err, data) { if (err) { callback(false); } diff --git a/lib/key_generators/random.js b/lib/key_generators/random.js index 4b48071..517fb7c 100644 --- a/lib/key_generators/random.js +++ b/lib/key_generators/random.js @@ -1,4 +1,7 @@ var RandomKeyGenerator = function(options) { + if (!options) { + options = {}; + } this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; }; diff --git a/lib/redis_document_store.js b/lib/redis_document_store.js index fc44f07..346436f 100644 --- a/lib/redis_document_store.js +++ b/lib/redis_document_store.js @@ -1,6 +1,5 @@ var redis = require('redis'); var winston = require('winston'); -var hashlib = require('hashlib'); // For storing in redis // options[type] = redis diff --git a/package.json b/package.json index 9c69eaa..7c2187e 100644 --- a/package.json +++ b/package.json @@ -19,13 +19,13 @@ "dependencies": { "winston": "*", - "hashlib": "*", "connect": "*", "uglify-js": "*" }, "devDependencies": { - "jasmine-node": "*" + "mocha": "*", + "should": "*" }, "bundledDependencies": [], @@ -46,7 +46,7 @@ "scripts": { "start": "node server.js", - "test": "jasmine-node spec" + "test": "mocha -r should spec/*" } } diff --git a/spec/document_handler_spec.js b/spec/document_handler_spec.js index 2e1fab6..e31eae8 100644 --- a/spec/document_handler_spec.js +++ b/spec/document_handler_spec.js @@ -1,17 +1,20 @@ var DocumentHandler = require('../lib/document_handler'); +var Generator = require('../lib/key_generators/random'); describe('document_handler', function() { describe('randomKey', function() { it('should choose a key of the proper length', function() { - var dh = new DocumentHandler({ keyLength: 6 }); - expect(dh.randomKey().length).toBe(6); + var gen = new Generator(); + var dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen }); + dh.acceptableKey().length.should.equal(6); }); it('should choose a default key length', function() { - var dh = new DocumentHandler(); - expect(dh.keyLength).toBe(DocumentHandler.defaultKeyLength); + var gen = new Generator(); + var dh = new DocumentHandler({ keyGenerator: gen }); + dh.keyLength.should.equal(DocumentHandler.defaultKeyLength); }); }); diff --git a/spec/redis_document_store_spec.js b/spec/redis_document_store_spec.js index da94712..1b06d6b 100644 --- a/spec/redis_document_store_spec.js +++ b/spec/redis_document_store_spec.js @@ -15,73 +15,34 @@ describe('redis_document_store', function() { describe('set', function() { - it('should be able to set a key and have an expiration set', function() { + it('should be able to set a key and have an expiration set', function(done) { var store = new RedisDocumentStore({ expire: 10 }); - runs(function() { - var _this = this; - store.set('hello1', 'world', function(worked) { - _this.result = worked; - }); - }); - waitsFor(function() { - return typeof(this.result) === 'boolean'; - }); - runs(function() { - var _this = this; + store.set('hello1', 'world', function() { RedisDocumentStore.client.ttl('hello1', function(err, res) { - expect(res).toBeGreaterThan(1); - _this.done = true; + res.should.be.above(1); + done(); }); }); - waitsFor(function() { - return this.done; - }); }); - it('should not set an expiration when told not to', function() { + it('should not set an expiration when told not to', function(done) { var store = new RedisDocumentStore({ expire: 10 }); - runs(function() { - var _this = this; - store.set('hello2', 'world', function(worked) { - _this.result = worked; - }, true); - }); - waitsFor(function() { - return typeof(this.result) === 'boolean'; - }); - runs(function() { - var _this = this; + store.set('hello2', 'world', function() { RedisDocumentStore.client.ttl('hello2', function(err, res) { - expect(res).toBe(-1); - _this.done = true; + res.should.equal(-1); + done(); }); - }); - waitsFor(function() { - return this.done; - }); + }, true); }); - it('should not set an expiration when expiration is off', function() { + it('should not set an expiration when expiration is off', function(done) { var store = new RedisDocumentStore({ expire: false }); - runs(function() { - var _this = this; - store.set('hello3', 'world', function(worked) { - _this.result = worked; - }); - }); - waitsFor(function() { - return typeof(this.result) === 'boolean'; - }); - runs(function() { - var _this = this; + store.set('hello3', 'world', function(worked) { RedisDocumentStore.client.ttl('hello3', function(err, res) { - expect(res).toBe(-1); - _this.done = true; + res.should.equal(-1); + done(); }); }); - waitsFor(function() { - return this.done; - }); }); }); From 483fce891d210462727f76e01de0f11c2824d600 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 17 Jan 2012 14:43:33 -0500 Subject: [PATCH 4/5] Added memcached support --- README.md | 25 +++++++++++++++++-- lib/memcached_document_store.js | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 lib/memcached_document_store.js diff --git a/README.md b/README.md index 6f604e6..3f10a26 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,9 @@ Where `path` represents where you want the files stored ### Redis -To use redis storage you must install the redis package in npm globall using +To use redis storage you must install the redis package in npm -`npm install redis --global` +`npm install redis` Once you've done that, your config section should look like: @@ -101,6 +101,27 @@ You can also set an `expire` option to the number of seconds to expire keys in. All of which are optional except `type` with very logical default values. +### Memcached + +To use memcached storage you must install the `memcache` package via npm + +`npm install memcache` + +Once you've done that, your config section should look like: + +``` json +{ + "type": "memcached", + "host": "127.0.0.1", + "port": 11211 +} +``` + +You can also set an `expire` option to the number of seconds to expire keys in. This behaves just like the redis expirations, but does not push expirations forward on GETs. + +All of which are optional except `type` with very logical default values. + + ## Author John Crepezzi diff --git a/lib/memcached_document_store.js b/lib/memcached_document_store.js new file mode 100644 index 0000000..e0ce405 --- /dev/null +++ b/lib/memcached_document_store.js @@ -0,0 +1,44 @@ +var memcached = require('memcache'); +var winston = require('winston'); + +// Create a new store with options +var MemcachedDocumentStore = function(options) { + this.expire = options.expire; + if (!MemcachedDocumentStore.client) { + MemcachedDocumentStore.connect(options); + } +}; + +// Create a connection +MemcachedDocumentStore.connect = function(options) { + var host = options.host || '127.0.0.1'; + var port = options.port || 11211; + this.client = new memcached.Client(port, host); + this.client.connect(); + this.client.on('connect', function() { + winston.info('connected to memcached on ' + host + ':' + port); + }); + this.client.on('error', function(e) { + winston.info('error connecting to memcached', { error: e }); + }); +}; + +// Save file in a key +MemcachedDocumentStore.prototype.set = function(key, data, callback, skipExpire) { + MemcachedDocumentStore.client.set(key, data, function(err, reply) { + err ? callback(false) : callback(true); + }, skipExpire ? 0 : this.expire); +}; + +// Get a file from a key +MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) { + var _this = this; + MemcachedDocumentStore.client.get(key, function(err, reply) { + callback(err ? false : reply); + if (_this.expire && !skipExpire) { + winston.warn('memcache store does not currently push forward expirations on GET'); + } + }); +}; + +module.exports = MemcachedDocumentStore; From f37d1ad401f7e8e0f12dbad274ff97e27e0670bb Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Sat, 21 Jan 2012 15:19:55 -0500 Subject: [PATCH 5/5] Clean up some code and move document stores into subfolder --- README.md | 46 ++++++++++++++----- TODO.md | 1 - about.md | 29 +++++++----- lib/document_handler.js | 8 ++-- .../file.js} | 13 ++++-- .../memcached.js} | 5 +- .../redis.js} | 7 ++- lib/key_generators/random.js | 4 +- server.js | 21 ++++++--- spec/redis_document_store_spec.js | 2 +- 10 files changed, 92 insertions(+), 44 deletions(-) delete mode 100644 TODO.md rename lib/{file_document_store.js => document_stores/file.js} (84%) rename lib/{memcached_document_store.js => document_stores/memcached.js} (87%) rename lib/{redis_document_store.js => document_stores/redis.js} (94%) diff --git a/README.md b/README.md index 3f10a26..1f3280d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # Haste -Haste is an open-source pastebin software written in node.js, which is easily installable in any network. It can be backed by either redis or filesystem, and has a very easy adapter interface for other stores. A publicly available version can be found at [hastebin.com](http://hastebin.com) +Haste is an open-source pastebin software written in node.js, which is easily +installable in any network. It can be backed by either redis or filesystem, +and has a very easy adapter interface for other stores. A publicly available +version can be found at [hastebin.com](http://hastebin.com) Major design objectives: @@ -8,11 +11,13 @@ Major design objectives: * Be really simple * Be easy to set up and use -Haste works really well with a little utility called haste-client, allowing you to do things like: +Haste works really well with a little utility called haste-client, allowing you +to do things like: `cat something | haste` -which will output a URL to share containing the contents of `cat something`'s STDOUT +which will output a URL to share containing the contents of `cat something`'s +STDOUT ## Tested Browsers @@ -35,7 +40,8 @@ which will output a URL to share containing the contents of `cat something`'s ST * `maxLength` - maximum length of a paste (default none) * `staticMaxAge` - max age for static assets (86400) * `recompressStatisAssets` - whether or not to compile static js assets (true) -* `documents` - static documents to serve (ex: http://hastebin.com/about.com) in addition to static assets. These will never expire. +* `documents` - static documents to serve (ex: http://hastebin.com/about.com) + in addition to static assets. These will never expire. * `storage` - storage options (see below) * `logging` - logging preferences * `keyGenerator` - key generator options (see below) @@ -63,13 +69,15 @@ Generates a random key } ``` -The _optional_ keySpace argument is a string of acceptable characters for the key. +The _optional_ keySpace argument is a string of acceptable characters +for the key. ## Storage ### File -To use file storage (the default) change the storage section in `config.js` to something like: +To use file storage (the default) change the storage section in `config.js` to +something like: ``` json { @@ -97,7 +105,9 @@ Once you've done that, your config section should look like: } ``` -You can also set an `expire` option to the number of seconds to expire keys in. This is off by default, but will constantly kick back expirations on each view or post. +You can also set an `expire` option to the number of seconds to expire keys in. +This is off by default, but will constantly kick back expirations on each view +or post. All of which are optional except `type` with very logical default values. @@ -117,7 +127,9 @@ Once you've done that, your config section should look like: } ``` -You can also set an `expire` option to the number of seconds to expire keys in. This behaves just like the redis expirations, but does not push expirations forward on GETs. +You can also set an `expire` option to the number of seconds to expire keys in. +This behaves just like the redis expirations, but does not push expirations +forward on GETs. All of which are optional except `type` with very logical default values. @@ -132,11 +144,23 @@ John Crepezzi Copyright © 2011 John Crepezzi -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the ‘Software’), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE +THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE ### Other components: diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 77f0551..0000000 --- a/TODO.md +++ /dev/null @@ -1 +0,0 @@ -* add feedback for errors to UI - esp. too long diff --git a/about.md b/about.md index 815dccd..731657a 100644 --- a/about.md +++ b/about.md @@ -1,33 +1,37 @@ # Haste Sharing code is a good thing, and it should be _really_ easy to do it. -A lot of times, I want to show you something I'm seeing - and that's where we use pastebins. +A lot of times, I want to show you something I'm seeing - and that's where we +use pastebins. Haste is the prettiest, easist to use pastebin ever made. ## Basic Usage -Type what you want me to see, click "Save", and then copy the URL. Send that URL -to someone and they'll see what you see. +Type what you want me to see, click "Save", and then copy the URL. Send that +URL to someone and they'll see what you see. To make a new entry, click "New" (or type 'control + n') ## From the Console -Most of the time I want to show you some text, its coming from my current console session. -We should make it really easy to take code from the console and send it to people. +Most of the time I want to show you some text, its coming from my current +console session. We should make it really easy to take code from the console +and send it to people. `cat something | haste` # http://hastebin.com/1238193 -You can even take this a step further, and cut out the last step of copying the URL with: +You can even take this a step further, and cut out the last step of copying the +URL with: * osx: `cat something | haste | pbcopy` * linux: `cat something | haste | xsel` -After running that, the STDOUT output of `cat something` will show up at a URL which has -been conveniently copied to your clipboard. +After running that, the STDOUT output of `cat something` will show up at a URL +which has been conveniently copied to your clipboard. -That's all there is to that, and you can install it with `gem install haste` right now. +That's all there is to that, and you can install it with `gem install haste` +right now. * osx: you will need to have an up to date version of Xcode * linux: you will need to have rubygems and ruby-devel installed @@ -37,9 +41,10 @@ Pastes will stay for 30 days from their last view. ## Privacy -While the contents of hastebin.com are not directly crawled by any search robot that -obeys "robots.txt", there should be no great expectation of privacy. Post things at your -own risk. Not responsible for any loss of data or removed pastes. +While the contents of hastebin.com are not directly crawled by any search robot +that obeys "robots.txt", there should be no great expectation of privacy. Post +things at your own risk. Not responsible for any loss of data or removed +pastes. ## Open Source diff --git a/lib/document_handler.js b/lib/document_handler.js index 07291e7..0ecc773 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -54,13 +54,15 @@ DocumentHandler.prototype.handlePost = function(request, response) { request.on('data', function(data) { if (!buffer) { response.writeHead(200, { 'content-type': 'application/json' }); - } + } buffer += data.toString(); if (_this.maxLength && buffer.length > _this.maxLength) { cancelled = true; winston.warn('document >maxLength', { maxLength: _this.maxLength }); response.writeHead(400, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'Document exceeds maximum length.' })); + response.end( + JSON.stringify({ message: 'Document exceeds maximum length.' }) + ); } }); request.on('end', function(end) { @@ -96,7 +98,7 @@ DocumentHandler.prototype.chooseKey = function(callback) { } else { callback(key); } - }); + }); }; DocumentHandler.prototype.acceptableKey = function() { diff --git a/lib/file_document_store.js b/lib/document_stores/file.js similarity index 84% rename from lib/file_document_store.js rename to lib/document_stores/file.js index a0666fd..d301db4 100644 --- a/lib/file_document_store.js +++ b/lib/document_stores/file.js @@ -19,19 +19,21 @@ FileDocumentStore.md5 = function(str) { return md5sum.digest('hex'); }; -// Save data in a file, key as md5 - since we don't know what we could be passed here +// Save data in a file, key as md5 - since we don't know what we could +// be passed here FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) { try { var _this = this; fs.mkdir(this.basePath, '700', function() { - fs.writeFile(_this.basePath + '/' + _this.md5(key), data, 'utf8', function(err) { + var fn = _this.basePath + '/' + _this.md5(key); + fs.writeFile(fn, data, 'utf8', function(err) { if (err) { callback(false); } else { callback(true); if (_this.expire && !skipExpire) { - winston.warn('file store cannot set expirations on keys'); + winston.warn('file store cannot set expirations on keys'); } } }); @@ -44,14 +46,15 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) { // Get data from a file from key FileDocumentStore.prototype.get = function(key, callback, skipExpire) { var _this = this; - fs.readFile(this.basePath + '/' + _this..md5(key), 'utf8', function(err, data) { + var fn = this.basePath + '/' + this.md5(key); + fs.readFile(fn, 'utf8', function(err, data) { if (err) { callback(false); } else { callback(data); if (_this.expire && !skipExpire) { - winston.warn('file store cannot set expirations on keys'); + winston.warn('file store cannot set expirations on keys'); } } }); diff --git a/lib/memcached_document_store.js b/lib/document_stores/memcached.js similarity index 87% rename from lib/memcached_document_store.js rename to lib/document_stores/memcached.js index e0ce405..5aac673 100644 --- a/lib/memcached_document_store.js +++ b/lib/document_stores/memcached.js @@ -24,7 +24,8 @@ MemcachedDocumentStore.connect = function(options) { }; // Save file in a key -MemcachedDocumentStore.prototype.set = function(key, data, callback, skipExpire) { +MemcachedDocumentStore.prototype.set = +function(key, data, callback, skipExpire) { MemcachedDocumentStore.client.set(key, data, function(err, reply) { err ? callback(false) : callback(true); }, skipExpire ? 0 : this.expire); @@ -36,7 +37,7 @@ MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) { MemcachedDocumentStore.client.get(key, function(err, reply) { callback(err ? false : reply); if (_this.expire && !skipExpire) { - winston.warn('memcache store does not currently push forward expirations on GET'); + winston.warn('store does not currently push forward expirations on GET'); } }); }; diff --git a/lib/redis_document_store.js b/lib/document_stores/redis.js similarity index 94% rename from lib/redis_document_store.js rename to lib/document_stores/redis.js index 346436f..22f0e61 100644 --- a/lib/redis_document_store.js +++ b/lib/document_stores/redis.js @@ -23,7 +23,10 @@ RedisDocumentStore.connect = function(options) { RedisDocumentStore.client = redis.createClient(port, host); RedisDocumentStore.client.select(index, function(err, reply) { if (err) { - winston.error('error connecting to redis index ' + index, { error: err.message }); + winston.error( + 'error connecting to redis index ' + index, + { error: err.message } + ); process.exit(1); } else { @@ -67,7 +70,7 @@ RedisDocumentStore.prototype.get = function(key, callback, skipExpire) { _this.setExpiration(key); } callback(err ? false : reply); - }); + }); }; module.exports = RedisDocumentStore; diff --git a/lib/key_generators/random.js b/lib/key_generators/random.js index 517fb7c..cd35f3d 100644 --- a/lib/key_generators/random.js +++ b/lib/key_generators/random.js @@ -8,8 +8,10 @@ var RandomKeyGenerator = function(options) { // Generate a random key RandomKeyGenerator.prototype.createKey = function(keyLength) { var text = ''; + var index; for (var i = 0; i < keyLength; i++) { - text += this.keyspace.charAt(Math.floor(Math.random() * this.keyspace.length)); + index = Math.floor(Math.random() * this.keyspace.length); + text += this.keyspace.charAt(index); } return text; }; diff --git a/server.js b/server.js index a46b89b..1023b7d 100644 --- a/server.js +++ b/server.js @@ -34,7 +34,7 @@ if (!config.storage) { if (!config.storage.type) { config.storage.type = 'file'; } -var Store = require('./lib/' + config.storage.type + '_document_store'); +var Store = require('./lib/document_stores/' + config.storage.type); var preferredStore = new Store(config.storage); // Compress the static javascript assets @@ -45,8 +45,10 @@ if (config.recompressStaticAssets) { for (var i = 0; i < list.length; i++) { var item = list[i]; var orig_code, ast; - if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) { - dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3); + if ((item.indexOf('.js') === item.length - 3) && + (item.indexOf('.min.js') === -1)) { + dest = item.substring(0, item.length - 3) + '.min' + + item.substring(item.length - 3); orig_code = fs.readFileSync('./static/' + item, 'utf8'); ast = jsp.parse(orig_code); ast = pro.ast_mangle(ast); @@ -67,7 +69,10 @@ for (var name in config.documents) { }, true); } else { - winston.warn('failed to load static document', { name: name, path: path }); + winston.warn( + 'failed to load static document', + { name: name, path: path } + ); } }); } @@ -96,14 +101,18 @@ connect.createServer( var key = request.params.id.split('.')[0]; return documentHandler.handleRawGet(key, response, skipExpire); }); - // add documents + // add documents app.post('/documents', function(request, response, next) { return documentHandler.handlePost(request, response); }); // get documents app.get('/documents/:id', function(request, response, next) { var skipExpire = !!config.documents[request.params.id]; - return documentHandler.handleGet(request.params.id, response, skipExpire); + return documentHandler.handleGet( + request.params.id, + response, + skipExpire + ); }); }), // Otherwise, static diff --git a/spec/redis_document_store_spec.js b/spec/redis_document_store_spec.js index 1b06d6b..8e2faf7 100644 --- a/spec/redis_document_store_spec.js +++ b/spec/redis_document_store_spec.js @@ -1,4 +1,4 @@ -var RedisDocumentStore = require('../lib/redis_document_store'); +var RedisDocumentStore = require('../lib/document_stores/redis'); var winston = require('winston'); winston.remove(winston.transports.Console);