Skip to content

Commit f37d1ad

Browse files
committed
Clean up some code and move document stores into subfolder
1 parent 483fce8 commit f37d1ad

File tree

10 files changed

+94
-46
lines changed

10 files changed

+94
-46
lines changed

README.md

+37-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
# Haste
22

3-
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)
3+
Haste is an open-source pastebin software written in node.js, which is easily
4+
installable in any network. It can be backed by either redis or filesystem,
5+
and has a very easy adapter interface for other stores. A publicly available
6+
version can be found at [hastebin.com](http://hastebin.com)
47

58
Major design objectives:
69

710
* Be really pretty
811
* Be really simple
912
* Be easy to set up and use
1013

11-
Haste works really well with a little utility called haste-client, allowing you to do things like:
14+
Haste works really well with a little utility called haste-client, allowing you
15+
to do things like:
1216

1317
`cat something | haste`
1418

15-
which will output a URL to share containing the contents of `cat something`'s STDOUT
19+
which will output a URL to share containing the contents of `cat something`'s
20+
STDOUT
1621

1722
## Tested Browsers
1823

@@ -35,7 +40,8 @@ which will output a URL to share containing the contents of `cat something`'s ST
3540
* `maxLength` - maximum length of a paste (default none)
3641
* `staticMaxAge` - max age for static assets (86400)
3742
* `recompressStatisAssets` - whether or not to compile static js assets (true)
38-
* `documents` - static documents to serve (ex: http://hastebin.com/about.com) in addition to static assets. These will never expire.
43+
* `documents` - static documents to serve (ex: http://hastebin.com/about.com)
44+
in addition to static assets. These will never expire.
3945
* `storage` - storage options (see below)
4046
* `logging` - logging preferences
4147
* `keyGenerator` - key generator options (see below)
@@ -63,13 +69,15 @@ Generates a random key
6369
}
6470
```
6571

66-
The _optional_ keySpace argument is a string of acceptable characters for the key.
72+
The _optional_ keySpace argument is a string of acceptable characters
73+
for the key.
6774

6875
## Storage
6976

7077
### File
7178

72-
To use file storage (the default) change the storage section in `config.js` to something like:
79+
To use file storage (the default) change the storage section in `config.js` to
80+
something like:
7381

7482
``` json
7583
{
@@ -97,7 +105,9 @@ Once you've done that, your config section should look like:
97105
}
98106
```
99107

100-
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.
108+
You can also set an `expire` option to the number of seconds to expire keys in.
109+
This is off by default, but will constantly kick back expirations on each view
110+
or post.
101111

102112
All of which are optional except `type` with very logical default values.
103113

@@ -117,7 +127,9 @@ Once you've done that, your config section should look like:
117127
}
118128
```
119129

120-
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.
130+
You can also set an `expire` option to the number of seconds to expire keys in.
131+
This behaves just like the redis expirations, but does not push expirations
132+
forward on GETs.
121133

122134
All of which are optional except `type` with very logical default values.
123135

@@ -132,11 +144,23 @@ John Crepezzi <[email protected]>
132144

133145
Copyright © 2011 John Crepezzi
134146

135-
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:
136-
137-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
138-
139-
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
147+
Permission is hereby granted, free of charge, to any person obtaining a copy of
148+
this software and associated documentation files (the ‘Software’), to deal in
149+
the Software without restriction, including without limitation the rights to
150+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
151+
of the Software, and to permit persons to whom the Software is furnished to do
152+
so, subject to the following conditions:
153+
154+
The above copyright notice and this permission notice shall be included in all
155+
copies or substantial portions of the Software.
156+
157+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
159+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
161+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
162+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
163+
SOFTWARE
140164

141165
### Other components:
142166

TODO.md

-1
This file was deleted.

about.md

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
# Haste
22

33
Sharing code is a good thing, and it should be _really_ easy to do it.
4-
A lot of times, I want to show you something I'm seeing - and that's where we use pastebins.
4+
A lot of times, I want to show you something I'm seeing - and that's where we
5+
use pastebins.
56

67
Haste is the prettiest, easist to use pastebin ever made.
78

89
## Basic Usage
910

10-
Type what you want me to see, click "Save", and then copy the URL. Send that URL
11-
to someone and they'll see what you see.
11+
Type what you want me to see, click "Save", and then copy the URL. Send that
12+
URL to someone and they'll see what you see.
1213

1314
To make a new entry, click "New" (or type 'control + n')
1415

1516
## From the Console
1617

17-
Most of the time I want to show you some text, its coming from my current console session.
18-
We should make it really easy to take code from the console and send it to people.
18+
Most of the time I want to show you some text, its coming from my current
19+
console session. We should make it really easy to take code from the console
20+
and send it to people.
1921

2022
`cat something | haste` # http://hastebin.com/1238193
2123

22-
You can even take this a step further, and cut out the last step of copying the URL with:
24+
You can even take this a step further, and cut out the last step of copying the
25+
URL with:
2326

2427
* osx: `cat something | haste | pbcopy`
2528
* linux: `cat something | haste | xsel`
2629

27-
After running that, the STDOUT output of `cat something` will show up at a URL which has
28-
been conveniently copied to your clipboard.
30+
After running that, the STDOUT output of `cat something` will show up at a URL
31+
which has been conveniently copied to your clipboard.
2932

30-
That's all there is to that, and you can install it with `gem install haste` right now.
33+
That's all there is to that, and you can install it with `gem install haste`
34+
right now.
3135
* osx: you will need to have an up to date version of Xcode
3236
* linux: you will need to have rubygems and ruby-devel installed
3337

@@ -37,9 +41,10 @@ Pastes will stay for 30 days from their last view.
3741

3842
## Privacy
3943

40-
While the contents of hastebin.com are not directly crawled by any search robot that
41-
obeys "robots.txt", there should be no great expectation of privacy. Post things at your
42-
own risk. Not responsible for any loss of data or removed pastes.
44+
While the contents of hastebin.com are not directly crawled by any search robot
45+
that obeys "robots.txt", there should be no great expectation of privacy. Post
46+
things at your own risk. Not responsible for any loss of data or removed
47+
pastes.
4348

4449
## Open Source
4550

lib/document_handler.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ DocumentHandler.prototype.handlePost = function(request, response) {
5454
request.on('data', function(data) {
5555
if (!buffer) {
5656
response.writeHead(200, { 'content-type': 'application/json' });
57-
}
57+
}
5858
buffer += data.toString();
5959
if (_this.maxLength && buffer.length > _this.maxLength) {
6060
cancelled = true;
6161
winston.warn('document >maxLength', { maxLength: _this.maxLength });
6262
response.writeHead(400, { 'content-type': 'application/json' });
63-
response.end(JSON.stringify({ message: 'Document exceeds maximum length.' }));
63+
response.end(
64+
JSON.stringify({ message: 'Document exceeds maximum length.' })
65+
);
6466
}
6567
});
6668
request.on('end', function(end) {
@@ -96,7 +98,7 @@ DocumentHandler.prototype.chooseKey = function(callback) {
9698
} else {
9799
callback(key);
98100
}
99-
});
101+
});
100102
};
101103

102104
DocumentHandler.prototype.acceptableKey = function() {

lib/file_document_store.js renamed to lib/document_stores/file.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ FileDocumentStore.md5 = function(str) {
1919
return md5sum.digest('hex');
2020
};
2121

22-
// Save data in a file, key as md5 - since we don't know what we could be passed here
22+
// Save data in a file, key as md5 - since we don't know what we could
23+
// be passed here
2324
FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
2425
try {
2526
var _this = this;
2627
fs.mkdir(this.basePath, '700', function() {
27-
fs.writeFile(_this.basePath + '/' + _this.md5(key), data, 'utf8', function(err) {
28+
var fn = _this.basePath + '/' + _this.md5(key);
29+
fs.writeFile(fn, data, 'utf8', function(err) {
2830
if (err) {
2931
callback(false);
3032
}
3133
else {
3234
callback(true);
3335
if (_this.expire && !skipExpire) {
34-
winston.warn('file store cannot set expirations on keys');
36+
winston.warn('file store cannot set expirations on keys');
3537
}
3638
}
3739
});
@@ -44,14 +46,15 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
4446
// Get data from a file from key
4547
FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
4648
var _this = this;
47-
fs.readFile(this.basePath + '/' + _this..md5(key), 'utf8', function(err, data) {
49+
var fn = this.basePath + '/' + this.md5(key);
50+
fs.readFile(fn, 'utf8', function(err, data) {
4851
if (err) {
4952
callback(false);
5053
}
5154
else {
5255
callback(data);
5356
if (_this.expire && !skipExpire) {
54-
winston.warn('file store cannot set expirations on keys');
57+
winston.warn('file store cannot set expirations on keys');
5558
}
5659
}
5760
});

lib/memcached_document_store.js renamed to lib/document_stores/memcached.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ MemcachedDocumentStore.connect = function(options) {
2424
};
2525

2626
// Save file in a key
27-
MemcachedDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
27+
MemcachedDocumentStore.prototype.set =
28+
function(key, data, callback, skipExpire) {
2829
MemcachedDocumentStore.client.set(key, data, function(err, reply) {
2930
err ? callback(false) : callback(true);
3031
}, skipExpire ? 0 : this.expire);
@@ -36,7 +37,7 @@ MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) {
3637
MemcachedDocumentStore.client.get(key, function(err, reply) {
3738
callback(err ? false : reply);
3839
if (_this.expire && !skipExpire) {
39-
winston.warn('memcache store does not currently push forward expirations on GET');
40+
winston.warn('store does not currently push forward expirations on GET');
4041
}
4142
});
4243
};

lib/redis_document_store.js renamed to lib/document_stores/redis.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ RedisDocumentStore.connect = function(options) {
2323
RedisDocumentStore.client = redis.createClient(port, host);
2424
RedisDocumentStore.client.select(index, function(err, reply) {
2525
if (err) {
26-
winston.error('error connecting to redis index ' + index, { error: err.message });
26+
winston.error(
27+
'error connecting to redis index ' + index,
28+
{ error: err.message }
29+
);
2730
process.exit(1);
2831
}
2932
else {
@@ -67,7 +70,7 @@ RedisDocumentStore.prototype.get = function(key, callback, skipExpire) {
6770
_this.setExpiration(key);
6871
}
6972
callback(err ? false : reply);
70-
});
73+
});
7174
};
7275

7376
module.exports = RedisDocumentStore;

lib/key_generators/random.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ var RandomKeyGenerator = function(options) {
88
// Generate a random key
99
RandomKeyGenerator.prototype.createKey = function(keyLength) {
1010
var text = '';
11+
var index;
1112
for (var i = 0; i < keyLength; i++) {
12-
text += this.keyspace.charAt(Math.floor(Math.random() * this.keyspace.length));
13+
index = Math.floor(Math.random() * this.keyspace.length);
14+
text += this.keyspace.charAt(index);
1315
}
1416
return text;
1517
};

server.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (!config.storage) {
3434
if (!config.storage.type) {
3535
config.storage.type = 'file';
3636
}
37-
var Store = require('./lib/' + config.storage.type + '_document_store');
37+
var Store = require('./lib/document_stores/' + config.storage.type);
3838
var preferredStore = new Store(config.storage);
3939

4040
// Compress the static javascript assets
@@ -45,8 +45,10 @@ if (config.recompressStaticAssets) {
4545
for (var i = 0; i < list.length; i++) {
4646
var item = list[i];
4747
var orig_code, ast;
48-
if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) {
49-
dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3);
48+
if ((item.indexOf('.js') === item.length - 3) &&
49+
(item.indexOf('.min.js') === -1)) {
50+
dest = item.substring(0, item.length - 3) + '.min' +
51+
item.substring(item.length - 3);
5052
orig_code = fs.readFileSync('./static/' + item, 'utf8');
5153
ast = jsp.parse(orig_code);
5254
ast = pro.ast_mangle(ast);
@@ -67,7 +69,10 @@ for (var name in config.documents) {
6769
}, true);
6870
}
6971
else {
70-
winston.warn('failed to load static document', { name: name, path: path });
72+
winston.warn(
73+
'failed to load static document',
74+
{ name: name, path: path }
75+
);
7176
}
7277
});
7378
}
@@ -96,14 +101,18 @@ connect.createServer(
96101
var key = request.params.id.split('.')[0];
97102
return documentHandler.handleRawGet(key, response, skipExpire);
98103
});
99-
// add documents
104+
// add documents
100105
app.post('/documents', function(request, response, next) {
101106
return documentHandler.handlePost(request, response);
102107
});
103108
// get documents
104109
app.get('/documents/:id', function(request, response, next) {
105110
var skipExpire = !!config.documents[request.params.id];
106-
return documentHandler.handleGet(request.params.id, response, skipExpire);
111+
return documentHandler.handleGet(
112+
request.params.id,
113+
response,
114+
skipExpire
115+
);
107116
});
108117
}),
109118
// Otherwise, static

spec/redis_document_store_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var RedisDocumentStore = require('../lib/redis_document_store');
1+
var RedisDocumentStore = require('../lib/document_stores/redis');
22

33
var winston = require('winston');
44
winston.remove(winston.transports.Console);

0 commit comments

Comments
 (0)