Compare commits
	
		
			226 Commits
		
	
	
		
			no_npm
			...
			9b59c4c89b
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9b59c4c89b | |||
|  | 00d84614c2 | ||
|  | 52e7cef7ef | ||
|  | fbff1bc201 | ||
|  | 7af15cc32d | ||
|  | 7f397ce753 | ||
|  | 8f8b039f65 | ||
|  | eeaf2d7b18 | ||
|  | db0b7d6444 | ||
|  | db6e7603f9 | ||
| 20fb7f9bc2 | |||
|  | ad5d7549d7 | ||
| 991f26e871 | |||
| e2293900de | |||
|  | 5d2965ffc5 | ||
|  | f255928af7 | ||
|  | a108dbadc5 | ||
|  | c409aca080 | ||
|  | 219424550b | ||
|  | f147acb51c | ||
|  | 9a692ed652 | ||
|  | 3a17c86a0f | ||
|  | 677a22987a | ||
|  | 89d912c6ff | ||
|  | 4cac6713ef | ||
|  | f3b0de745b | ||
|  | cc8a99752f | ||
|  | 6853d077e7 | ||
|  | 80a2b6f0dd | ||
|  | 4f68b3d7d6 | ||
|  | ef0ca40533 | ||
|  | f372ef18de | ||
|  | 181a3a2bfa | ||
|  | 61d08afb3b | ||
|  | 1ba025328d | ||
|  | a79fb39f54 | ||
|  | 3a72d74537 | ||
|  | e9ae74b7a9 | ||
|  | c305e9a83d | ||
|  | 16bce4c83d | ||
|  | 661997cd73 | ||
|  | 159f989d08 | ||
|  | 139df62ec4 | ||
|  | bae6387bb7 | ||
|  | bb7b9571a7 | ||
|  | a4dc29fb2b | ||
|  | 342f56ce1a | ||
|  | 05ecc90764 | ||
|  | 69cf505a90 | ||
|  | 9f41993566 | ||
|  | 5c9311fb85 | ||
|  | 5a8d52a5e3 | ||
|  | 0f145b4444 | ||
|  | aef4bb5edb | ||
|  | 36c854ef1b | ||
|  | edd428ff37 | ||
|  | 0612ba001e | ||
|  | 064680003d | ||
|  | 655f2af45a | ||
|  | ce03749c2f | ||
| 24ed412f50 | |||
| 7bd0fcc621 | |||
| e718672b58 | |||
|  | f6084b4339 | ||
|  | 9b0a5ff0a3 | ||
| a4ad0e1fa6 | |||
| af28e0c5d9 | |||
| 240a9f7fde | |||
| dca3237a71 | |||
| a541630848 | |||
| e067323714 | |||
|  | 1fff48568f | ||
|  | b4c666fbcf | ||
|  | b866c33c93 | ||
|  | 035cf0e91e | ||
|  | f3838ab4a8 | ||
|  | bf2b1c957a | ||
|  | 86bbc1899d | ||
|  | d41d7491d4 | ||
|  | 5fb43eb67c | ||
|  | 1eeef4ede4 | ||
|  | ebc749c5e0 | ||
|  | b0bbb72f35 | ||
|  | 2213c3874a | ||
|  | 6ebd72a86c | ||
|  | b6814a1445 | ||
|  | e3d18efdc6 | ||
|  | 869fb65738 | ||
|  | 56b939124e | ||
|  | ee1c1c0856 | ||
|  | b087ac8dd1 | ||
|  | d922667f56 | ||
|  | 5f6fefa7a6 | ||
|  | faa7e679ca | ||
|  | cd3bf26dbe | ||
|  | 830dc1bc43 | ||
|  | dc0f151a7f | ||
|  | 7f625e22f7 | ||
|  | 528b7b07a8 | ||
|  | 2b81e67ce7 | ||
|  | 827e7b51b5 | ||
|  | 16d529e935 | ||
|  | ad7702aaf4 | ||
|  | f5fbc8d19e | ||
|  | 0a8923bf12 | ||
|  | 4d572a2ec0 | ||
|  | d9a53d3e6e | ||
|  | 8da37ea5de | ||
|  | ff0fccd6c2 | ||
|  | 63c4576633 | ||
|  | b31d143bcd | ||
|  | 0d8aec8d61 | ||
|  | 1f9fdd205d | ||
|  | cdd0cf3739 | ||
|  | ba5c6b8d16 | ||
|  | cfef588283 | ||
|  | 318c5f7ba6 | ||
|  | ee03e7cd78 | ||
|  | 3b6934e348 | ||
|  | f161cc33b4 | ||
|  | 40f1f2588e | ||
|  | e4e025f67e | ||
|  | e12805a8aa | ||
|  | e76c845f16 | ||
|  | 072418695e | ||
|  | 584b66bc66 | ||
|  | f8db455f74 | ||
|  | f19c5d1049 | ||
|  | c5b859ec98 | ||
|  | 2ee93a7409 | ||
|  | bf1dbb68b8 | ||
|  | cf28e23d8e | ||
|  | 5939dec185 | ||
|  | 3ed1d775ac | ||
|  | 87b1c76aaf | ||
|  | 4599203bdf | ||
|  | d66bc9a6c4 | ||
|  | 80f0618736 | ||
|  | ac2bceefbb | ||
|  | dbf4f6b5dd | ||
|  | 8e9205cecc | ||
|  | e54a860172 | ||
|  | 5a8697cdd8 | ||
|  | 091ea973a8 | ||
|  | 939b7221ab | ||
|  | 934aaf7f51 | ||
|  | 930e21ccb7 | ||
|  | eb5c8eef6a | ||
|  | 03dd611a86 | ||
|  | f24376b192 | ||
|  | eea359d0ec | ||
|  | af9a71549b | ||
|  | 3178676fba | ||
|  | 3bdfab8219 | ||
|  | a3a24d9765 | ||
|  | 8afb53e77e | ||
|  | d6d9cf40f9 | ||
|  | 1010a142e2 | ||
|  | d3db5e2a5d | ||
|  | 0209375865 | ||
|  | 00a9d9c312 | ||
|  | 6835eef468 | ||
|  | 4626fd9c8d | ||
|  | fbb6e63c37 | ||
|  | 84c909a5db | ||
|  | 45e19bc7cc | ||
|  | 233bc6ff16 | ||
|  | 360b325ced | ||
|  | e93f98112b | ||
|  | 05cb051bc8 | ||
|  | 031cdd738a | ||
|  | c92ab077c0 | ||
|  | 6c31389327 | ||
|  | a8d4f3c300 | ||
|  | ab029eae2f | ||
|  | 447d0aae76 | ||
|  | 4870158430 | ||
|  | 0471b059a0 | ||
|  | 5bbe50b481 | ||
|  | bda2749879 | ||
|  | 028aa96b13 | ||
|  | 2deda5b68a | ||
|  | ee7098457e | ||
|  | 7a08960414 | ||
|  | 89909747f1 | ||
|  | 202e695e07 | ||
|  | 48e8e79659 | ||
|  | abb49f2cf3 | ||
|  | d1cd2a5213 | ||
|  | 27317844e0 | ||
|  | ee74e2fa90 | ||
|  | 5d8bd2e6f8 | ||
|  | cd4c7aeab8 | ||
|  | e37c3cf1b9 | ||
|  | 8858bab985 | ||
|  | afb0c332cc | ||
|  | 82c58c5c0c | ||
|  | 46bdd27431 | ||
|  | 1adfba1a37 | ||
|  | 54e55b1b0d | ||
|  | 08d37cc7f7 | ||
|  | aa781957e8 | ||
|  | c00477c93c | ||
|  | 035f09ac05 | ||
|  | 36e00bb29e | ||
|  | 10623873e8 | ||
|  | e536ba1019 | ||
|  | 85fc36d710 | ||
|  | 5d5ae164f3 | ||
|  | 79309c75df | ||
|  | 4b58c8d356 | ||
|  | 8f0d6260b0 | ||
|  | 93a83a35da | ||
|  | 4efc5d47d9 | ||
|  | ff8ef54e34 | ||
|  | 814a49812a | ||
|  | e0610bc1be | ||
|  | 962976c204 | ||
|  | 16080bdc16 | ||
|  | 20ce741341 | ||
|  | 13bb094fb3 | ||
|  | b43a55ffda | ||
|  | 45cbdcce70 | ||
|  | 1950cc8db0 | ||
|  | 90cfe0ec57 | ||
|  | 87e28548b9 | 
							
								
								
									
										8
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | Dockerfile | ||||||
|  | .git | ||||||
|  | npm-debug.log | ||||||
|  | node_modules | ||||||
|  | *.swp | ||||||
|  | *.swo | ||||||
|  | data | ||||||
|  | *.DS_Store | ||||||
							
								
								
									
										2
									
								
								.eslintignore
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.eslintignore
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | |||||||
|  | **/*.min.js | ||||||
|  | config.js | ||||||
							
								
								
									
										25
									
								
								.eslintrc.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								.eslintrc.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | |||||||
|  | { | ||||||
|  |     "env": { | ||||||
|  |         "es6": true, | ||||||
|  |         "node": true | ||||||
|  |     }, | ||||||
|  |     "extends": "eslint:recommended", | ||||||
|  |     "rules": { | ||||||
|  |         "indent": [ | ||||||
|  |             "error", | ||||||
|  |             2 | ||||||
|  |         ], | ||||||
|  |         "linebreak-style": [ | ||||||
|  |             "error", | ||||||
|  |             "unix" | ||||||
|  |         ], | ||||||
|  |         "quotes": [ | ||||||
|  |             "error", | ||||||
|  |             "single" | ||||||
|  |         ], | ||||||
|  |         "semi": [ | ||||||
|  |             "error", | ||||||
|  |             "always" | ||||||
|  |         ] | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -4,3 +4,4 @@ node_modules | |||||||
| *.swo | *.swo | ||||||
| data | data | ||||||
| *.DS_Store | *.DS_Store | ||||||
|  | config.json | ||||||
|   | |||||||
							
								
								
									
										68
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | |||||||
|  | FROM node:14.8.0-stretch | ||||||
|  |  | ||||||
|  | RUN mkdir -p /usr/src/app && \ | ||||||
|  |     chown node:node /usr/src/app | ||||||
|  |  | ||||||
|  | USER node:node | ||||||
|  |  | ||||||
|  | WORKDIR /usr/src/app | ||||||
|  |  | ||||||
|  | COPY --chown=node:node . . | ||||||
|  |  | ||||||
|  | RUN npm install && \ | ||||||
|  |     npm install redis@0.8.1 && \ | ||||||
|  |     npm install pg@4.1.1 && \ | ||||||
|  |     npm install memcached@2.2.2 && \ | ||||||
|  |     npm install aws-sdk@2.738.0 && \ | ||||||
|  |     npm install rethinkdbdash@2.3.31 | ||||||
|  |  | ||||||
|  | ENV STORAGE_TYPE=memcached \ | ||||||
|  |     STORAGE_HOST=127.0.0.1 \ | ||||||
|  |     STORAGE_PORT=11211\ | ||||||
|  |     STORAGE_EXPIRE_SECONDS=2592000\ | ||||||
|  |     STORAGE_DB=2 \ | ||||||
|  |     STORAGE_AWS_BUCKET= \ | ||||||
|  |     STORAGE_AWS_REGION= \ | ||||||
|  |     STORAGE_USENAME= \ | ||||||
|  |     STORAGE_PASSWORD= \ | ||||||
|  |     STORAGE_FILEPATH= | ||||||
|  |  | ||||||
|  | ENV LOGGING_LEVEL=verbose \ | ||||||
|  |     LOGGING_TYPE=Console \ | ||||||
|  |     LOGGING_COLORIZE=true | ||||||
|  |  | ||||||
|  | ENV HOST=0.0.0.0\ | ||||||
|  |     PORT=7777\ | ||||||
|  |     KEY_LENGTH=10\ | ||||||
|  |     MAX_LENGTH=400000\ | ||||||
|  |     STATIC_MAX_AGE=86400\ | ||||||
|  |     RECOMPRESS_STATIC_ASSETS=true | ||||||
|  |  | ||||||
|  | ENV KEYGENERATOR_TYPE=phonetic \ | ||||||
|  |     KEYGENERATOR_KEYSPACE= | ||||||
|  |  | ||||||
|  | ENV RATELIMITS_NORMAL_TOTAL_REQUESTS=500\ | ||||||
|  |     RATELIMITS_NORMAL_EVERY_MILLISECONDS=60000 \ | ||||||
|  |     RATELIMITS_WHITELIST_TOTAL_REQUESTS= \ | ||||||
|  |     RATELIMITS_WHITELIST_EVERY_MILLISECONDS=  \ | ||||||
|  |     # comma separated list for the whitelisted \ | ||||||
|  |     RATELIMITS_WHITELIST=example1.whitelist,example2.whitelist \ | ||||||
|  |     \ | ||||||
|  |     RATELIMITS_BLACKLIST_TOTAL_REQUESTS= \ | ||||||
|  |     RATELIMITS_BLACKLIST_EVERY_MILLISECONDS= \ | ||||||
|  |     # comma separated list for the blacklisted \ | ||||||
|  |     RATELIMITS_BLACKLIST=example1.blacklist,example2.blacklist | ||||||
|  | ENV DOCUMENTS=about=./about.md | ||||||
|  |  | ||||||
|  | EXPOSE ${PORT} | ||||||
|  | STOPSIGNAL SIGINT | ||||||
|  | ENTRYPOINT [ "bash", "docker-entrypoint.sh" ] | ||||||
|  |  | ||||||
|  | HEALTHCHECK --interval=30s --timeout=30s --start-period=5s \ | ||||||
|  |     --retries=3 CMD [ "sh", "-c", "echo -n 'curl localhost:7777... '; \ | ||||||
|  |     (\ | ||||||
|  |         curl -sf localhost:7777 > /dev/null\ | ||||||
|  |     ) && echo OK || (\ | ||||||
|  |         echo Fail && exit 2\ | ||||||
|  |     )"] | ||||||
|  | CMD ["npm", "start"] | ||||||
							
								
								
									
										235
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										235
									
								
								README.md
									
									
									
									
									
								
							| @@ -31,21 +31,31 @@ STDOUT.  Check the README there for more details and usages. | |||||||
| 1.  Download the package, and expand it | 1.  Download the package, and expand it | ||||||
| 2.  Explore the settings inside of config.js, but the defaults should be good | 2.  Explore the settings inside of config.js, but the defaults should be good | ||||||
| 3.  `npm install` | 3.  `npm install` | ||||||
| 4.  `npm start` | 4.  `npm start` (you may specify an optional `<config-path>` as well) | ||||||
|  |  | ||||||
| ## Settings | ## Settings | ||||||
|  |  | ||||||
| * `host` - the host the server runs on (default localhost) | * `host` - the host the server runs on (default localhost) | ||||||
| * `port` - the port the server runs on (default 7777) | * `port` - the port the server runs on (default 7777) | ||||||
| * `keyLength` - the length of the keys to user (default 10) | * `keyLength` - the length of the keys to user (default 10) | ||||||
| * `maxLength` - maximum length of a paste (default none) | * `maxLength` - maximum length of a paste (default 400000) | ||||||
| * `staticMaxAge` - max age for static assets (86400) | * `staticMaxAge` - max age for static assets (86400) | ||||||
| * `recompressStatisAssets` - whether or not to compile static js assets (true) | * `recompressStaticAssets` - whether or not to compile static js assets (true) | ||||||
| * `documents` - static documents to serve (ex: http://hastebin.com/about.com) | * `documents` - static documents to serve (ex: http://hastebin.com/about.com) | ||||||
|   in addition to static assets.  These will never expire. |   in addition to static assets.  These will never expire. | ||||||
| * `storage` - storage options (see below) | * `storage` - storage options (see below) | ||||||
| * `logging` - logging preferences | * `logging` - logging preferences | ||||||
| * `keyGenerator` - key generator options (see below) | * `keyGenerator` - key generator options (see below) | ||||||
|  | * `rateLimits` - settings for rate limiting (see below) | ||||||
|  |  | ||||||
|  | ## Rate Limiting | ||||||
|  |  | ||||||
|  | When present, the `rateLimits` option enables built-in rate limiting courtesy | ||||||
|  | of `connect-ratelimit`.  Any of the options supported by that library can be | ||||||
|  | used and set in `config.js`. | ||||||
|  |  | ||||||
|  | See the README for [connect-ratelimit](https://github.com/dharmafly/connect-ratelimit) | ||||||
|  | for more information! | ||||||
|  |  | ||||||
| ## Key Generation | ## Key Generation | ||||||
|  |  | ||||||
| @@ -87,11 +97,14 @@ something like: | |||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| Where `path` represents where you want the files stored | where `path` represents where you want the files stored. | ||||||
|  |  | ||||||
|  | File storage currently does not support paste expiration, you can follow [#191](https://github.com/seejohnrun/haste-server/issues/191) for status updates. | ||||||
|  |  | ||||||
| ### Redis | ### Redis | ||||||
|  |  | ||||||
| To use redis storage you must install the redis package in npm | To use redis storage you must install the `redis` package in npm, and have | ||||||
|  | `redis-server` running on the machine. | ||||||
|  |  | ||||||
| `npm install redis` | `npm install redis` | ||||||
|  |  | ||||||
| @@ -112,11 +125,62 @@ or post. | |||||||
|  |  | ||||||
| All of which are optional except `type` with very logical default values. | All of which are optional except `type` with very logical default values. | ||||||
|  |  | ||||||
|  | If your Redis server is configured for password authentification, use the `password` field. | ||||||
|  |  | ||||||
|  | ### Postgres | ||||||
|  |  | ||||||
|  | To use postgres storage you must install the `pg` package in npm | ||||||
|  |  | ||||||
|  | `npm install pg` | ||||||
|  |  | ||||||
|  | Once you've done that, your config section should look like: | ||||||
|  |  | ||||||
|  | ``` json | ||||||
|  | { | ||||||
|  |   "type": "postgres", | ||||||
|  |   "connectionUrl": "postgres://user:password@host:5432/database" | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | You can also just set the environment variable for `DATABASE_URL` to your database connection url. | ||||||
|  |  | ||||||
|  | You will have to manually add a table to your postgres database: | ||||||
|  |  | ||||||
|  | `create table entries (id serial primary key, key varchar(255) not null, value text not null, expiration int, unique(key));` | ||||||
|  |  | ||||||
|  | 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. | ||||||
|  |  | ||||||
|  | ### MongoDB | ||||||
|  |  | ||||||
|  | To use mongodb storage you must install the 'mongodb' package in npm | ||||||
|  |  | ||||||
|  | `npm install mongodb` | ||||||
|  |  | ||||||
|  | Once you've done that, your config section should look like: | ||||||
|  |  | ||||||
|  | ``` json | ||||||
|  | { | ||||||
|  |   "type": "mongo", | ||||||
|  |   "connectionUrl": "mongodb://localhost:27017/database" | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | You can also just set the environment variable for `DATABASE_URL` to your database connection url. | ||||||
|  |  | ||||||
|  | Unlike with postgres you do NOT have to create the table in your mongo database prior to running. | ||||||
|  |  | ||||||
|  | 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. | ||||||
|  |  | ||||||
| ### Memcached | ### Memcached | ||||||
|  |  | ||||||
| To use memcached storage you must install the `memcache` package via npm | To use memcache storage you must install the `memcached` package via npm | ||||||
|  |  | ||||||
| `npm install memcache` | `npm install memcached` | ||||||
|  |  | ||||||
| Once you've done that, your config section should look like: | Once you've done that, your config section should look like: | ||||||
|  |  | ||||||
| @@ -134,6 +198,161 @@ forward on GETs. | |||||||
|  |  | ||||||
| All of which are optional except `type` with very logical default values. | All of which are optional except `type` with very logical default values. | ||||||
|  |  | ||||||
|  | ### RethinkDB | ||||||
|  |  | ||||||
|  | To use the RethinkDB storage system, you must install the `rethinkdbdash` package via npm | ||||||
|  |  | ||||||
|  | `npm install rethinkdbdash` | ||||||
|  |  | ||||||
|  | Once you've done that, your config section should look like this: | ||||||
|  |  | ||||||
|  | ``` json | ||||||
|  | { | ||||||
|  |   "type": "rethinkdb", | ||||||
|  |   "host": "127.0.0.1", | ||||||
|  |   "port": 28015, | ||||||
|  |   "db": "haste" | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | In order for this to work, the database must be pre-created before the script is ran. | ||||||
|  | Also, you must create an `uploads` table, which will store all the data for uploads. | ||||||
|  |  | ||||||
|  | You can optionally add the `user` and `password` properties to use a user system. | ||||||
|  |  | ||||||
|  | ### Google Datastore | ||||||
|  |  | ||||||
|  | To use the Google Datastore storage system, you must install the `@google-cloud/datastore` package via npm | ||||||
|  |  | ||||||
|  | `npm install @google-cloud/datastore` | ||||||
|  |  | ||||||
|  | Once you've done that, your config section should look like this: | ||||||
|  |  | ||||||
|  | ``` json | ||||||
|  | { | ||||||
|  |   "type": "google-datastore" | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | Authentication is handled automatically by [Google Cloud service account credentials](https://cloud.google.com/docs/authentication/getting-started), by providing authentication details to the GOOGLE_APPLICATION_CREDENTIALS environmental variable. | ||||||
|  |  | ||||||
|  | ### Amazon S3 | ||||||
|  |  | ||||||
|  | To use [Amazon S3](https://aws.amazon.com/s3/) as a storage system, you must | ||||||
|  | install the `aws-sdk` package via npm: | ||||||
|  |  | ||||||
|  | `npm install aws-sdk` | ||||||
|  |  | ||||||
|  | Once you've done that, your config section should look like this: | ||||||
|  |  | ||||||
|  | ```json | ||||||
|  | { | ||||||
|  |   "type": "amazon-s3", | ||||||
|  |   "bucket": "your-bucket-name", | ||||||
|  |   "region": "us-east-1" | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | Authentication is handled automatically by the client. Check | ||||||
|  | [Amazon's documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html) | ||||||
|  | for more information. You will need to grant your role these permissions to | ||||||
|  | your bucket: | ||||||
|  |  | ||||||
|  | ```json | ||||||
|  | { | ||||||
|  |     "Version": "2012-10-17", | ||||||
|  |     "Statement": [ | ||||||
|  |         { | ||||||
|  |             "Action": [ | ||||||
|  |                 "s3:GetObject", | ||||||
|  |                 "s3:PutObject" | ||||||
|  |             ], | ||||||
|  |             "Effect": "Allow", | ||||||
|  |             "Resource": "arn:aws:s3:::your-bucket-name-goes-here/*" | ||||||
|  |         } | ||||||
|  |     ] | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | ## Docker | ||||||
|  |  | ||||||
|  | ### Build image | ||||||
|  |  | ||||||
|  | ```bash | ||||||
|  | docker build --tag haste-server . | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | ### Run container | ||||||
|  |  | ||||||
|  | For this example we will run haste-server, and connect it to a redis server | ||||||
|  |  | ||||||
|  | ```bash | ||||||
|  | docker run --name haste-server-container --env STORAGE_TYPE=redis --env STORAGE_HOST=redis-server --env STORAGE_PORT=6379 haste-server | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | ### Use docker-compose example | ||||||
|  |  | ||||||
|  | There is an example `docker-compose.yml` which runs haste-server together with memcached | ||||||
|  |  | ||||||
|  | ```bash | ||||||
|  | docker-compose up | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | ### Configuration | ||||||
|  |  | ||||||
|  | The docker image is configured using environmental variables as you can see in the example above. | ||||||
|  |  | ||||||
|  | Here is a list of all the environment variables | ||||||
|  |  | ||||||
|  | ### Storage | ||||||
|  |  | ||||||
|  | |          Name          | Default value |                                                  Description                                                  | | ||||||
|  | | :--------------------: | :-----------: | :-----------------------------------------------------------------------------------------------------------: | | ||||||
|  | |      STORAGE_TYPE      |   memcached   |    Type of storage . Accepted values: "memcached","redis","postgres","rethinkdb", "amazon-s3", and "file"     | | ||||||
|  | |      STORAGE_HOST      |   127.0.0.1   |                 Storage host. Applicable for types: memcached, redis, postgres, and rethinkdb                 | | ||||||
|  | |      STORAGE_PORT      |     11211     |           Port on the storage host. Applicable for types: memcached, redis, postgres, and rethinkdb           | | ||||||
|  | | STORAGE_EXPIRE_SECONDS |    2592000    | Number of seconds to expire keys in. Applicable for types. Redis, postgres, memcached. `expire` option to the | | ||||||
|  | |       STORAGE_DB       |       2       |                    The name of the database. Applicable for redis, postgres, and rethinkdb                    | | ||||||
|  | |    STORAGE_PASSWORD    |               |                       Password for database. Applicable for redis, postges, rethinkdb .                       | | ||||||
|  | |    STORAGE_USERNAME    |               |                           Database username. Applicable for postgres, and rethinkdb                           | | ||||||
|  | |   STORAGE_AWS_BUCKET   |               |                          Applicable for amazon-s3. This is the name of the S3 bucket                          | | ||||||
|  | |   STORAGE_AWS_REGION   |               |                      Applicable for amazon-s3. The region in which the bucket is located                      | | ||||||
|  | |    STORAGE_FILEPATH    |               |                            Path to file to save data to. Applicable for type file                             | | ||||||
|  |  | ||||||
|  | ### Logging | ||||||
|  |  | ||||||
|  | |       Name        | Default value | Description | | ||||||
|  | | :---------------: | :-----------: | :---------: | | ||||||
|  | |   LOGGING_LEVEL   |    verbose    |             | | ||||||
|  | |   LOGGING_TYPE=   |    Console    | | ||||||
|  | | LOGGING_COLORIZE= |     true      | | ||||||
|  |  | ||||||
|  | ### Basics | ||||||
|  |  | ||||||
|  | |           Name           |  Default value   |                                        Description                                        | | ||||||
|  | | :----------------------: | :--------------: | :---------------------------------------------------------------------------------------: | | ||||||
|  | |           HOST           |     0.0.0.0      |                         The hostname which the server answers on                          | | ||||||
|  | |           PORT           |       7777       |                          The port on which the server is running                          | | ||||||
|  | |        KEY_LENGTH        |        10        |                              the length of the keys to user                               | | ||||||
|  | |        MAX_LENGTH        |      400000      |                                 maximum length of a paste                                 | | ||||||
|  | |      STATIC_MAX_AGE      |      86400       |                                 max age for static assets                                 | | ||||||
|  | | RECOMPRESS_STATIC_ASSETS |       true       |                        whether or not to compile static js assets                         | | ||||||
|  | |    KEYGENERATOR_TYPE     |     phonetic     |             Type of key generator. Acceptable values: "phonetic", or "random"             | | ||||||
|  | |  KEYGENERATOR_KEYSPACE   |                  |                  keySpace argument is a string of acceptable characters                   | | ||||||
|  | |        DOCUMENTS         | about=./about.md | Comma separated list of static documents to serve. ex: \n about=./about.md,home=./home.md | | ||||||
|  |  | ||||||
|  | ### Rate limits | ||||||
|  |  | ||||||
|  | |                 Name                 |             Default value             |                                       Description                                        | | ||||||
|  | | :----------------------------------: | :-----------------------------------: | :--------------------------------------------------------------------------------------: | | ||||||
|  | |   RATELIMITS_NORMAL_TOTAL_REQUESTS   |                  500                  | By default anyone uncategorized will be subject to 500 requests in the defined timespan. | | ||||||
|  | | RATELIMITS_NORMAL_EVERY_MILLISECONDS |                 60000                 |             The timespan to allow the total requests for uncategorized users             | | ||||||
|  | | RATELIMITS_WHITELIST_TOTAL_REQUESTS  |                                       |      By default client names in the whitelist will not have their requests limited.      | | ||||||
|  | |  RATELIMITS_WHITELIST_EVERY_SECONDS  |                                       |      By default client names in the whitelist will not have their requests limited.      | | ||||||
|  | |         RATELIMITS_WHITELIST         | example1.whitelist,example2.whitelist |           Comma separated list of the clients which are in the whitelist pool            | | ||||||
|  | | RATELIMITS_BLACKLIST_TOTAL_REQUESTS  |                                       |    By default client names in the blacklist will be subject to 0 requests per hours.     | | ||||||
|  | |  RATELIMITS_BLACKLIST_EVERY_SECONDS  |                                       |     By default client names in the blacklist will be subject to 0 requests per hours     | | ||||||
|  | |         RATELIMITS_BLACKLIST         | example1.blacklist,example2.blacklist |           Comma separated list of the clients which are in the blacklistpool.            | | ||||||
|  |  | ||||||
| ## Author | ## Author | ||||||
|  |  | ||||||
| @@ -143,7 +362,7 @@ John Crepezzi <john.crepezzi@gmail.com> | |||||||
|  |  | ||||||
| (The MIT License) | (The MIT License) | ||||||
|  |  | ||||||
| Copyright © 2011 John Crepezzi | Copyright © 2011-2012 John Crepezzi | ||||||
|  |  | ||||||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | 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 | this software and associated documentation files (the ‘Software’), to deal in | ||||||
|   | |||||||
							
								
								
									
										41
									
								
								about.md
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								about.md
									
									
									
									
									
								
							| @@ -15,44 +15,19 @@ To make a new entry, click "New" (or type 'control + n') | |||||||
|  |  | ||||||
| ## From the Console | ## From the Console | ||||||
|  |  | ||||||
| Most of the time I want to show you some text, its coming from my current | [bin-client](git.webionite.com/ceda_ei/bin-client) | ||||||
| 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 | Add the following to your bashrc/zshrc | ||||||
|  | ``` | ||||||
| You can even take this a step further, and cut out the last step of copying the | export MKR_BIN='https://bin.webionite.com/' | ||||||
| URL with: | export HASTEBIN=1 | ||||||
|  | ``` | ||||||
| * osx: `cat something | haste | pbcopy` |  | ||||||
| * linux: `cat something | haste | xsel` |  | ||||||
| * windows: check out [WinHaste](https://github.com/ajryan/WinHaste) |  | ||||||
|  |  | ||||||
| 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. |  | ||||||
|   * osx: you will need to have an up to date version of Xcode |  | ||||||
|   * linux: you will need to have rubygems and ruby-devel installed |  | ||||||
|  |  | ||||||
| ## Duration |  | ||||||
|  |  | ||||||
| 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. |  | ||||||
|  |  | ||||||
| ## Open Source | ## Open Source | ||||||
|  |  | ||||||
| Haste can easily be installed behind your network, and its all open source! | Haste can easily be installed behind your network, and it's all open source! | ||||||
|  |  | ||||||
| * [haste-client](https://github.com/seejohnrun/haste-client) | * [haste-server](https://git.webionite.com/Webionite/haste-server) | ||||||
| * [haste-server](https://github.com/seejohnrun/haste-server) |  | ||||||
|  |  | ||||||
| ## Author | ## Author | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								docker-compose.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								docker-compose.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | version: '3.0' | ||||||
|  | services: | ||||||
|  |   haste-server: | ||||||
|  |     build: . | ||||||
|  |     networks: | ||||||
|  |       - db-network | ||||||
|  |     environment: | ||||||
|  |       - STORAGE_TYPE=memcached | ||||||
|  |       - STORAGE_HOST=memcached | ||||||
|  |       - STORAGE_PORT=11211 | ||||||
|  |     ports: | ||||||
|  |       - 7777:7777 | ||||||
|  |   memcached: | ||||||
|  |     image: memcached:latest | ||||||
|  |     networks: | ||||||
|  |       - db-network | ||||||
|  |  | ||||||
|  | networks: | ||||||
|  |   db-network: | ||||||
							
								
								
									
										108
									
								
								docker-entrypoint.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								docker-entrypoint.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,108 @@ | |||||||
|  | const { | ||||||
|  |   HOST, | ||||||
|  |   PORT, | ||||||
|  |   KEY_LENGTH, | ||||||
|  |   MAX_LENGTH, | ||||||
|  |   STATIC_MAX_AGE, | ||||||
|  |   RECOMPRESS_STATIC_ASSETS, | ||||||
|  |   STORAGE_TYPE, | ||||||
|  |   STORAGE_HOST, | ||||||
|  |   STORAGE_PORT, | ||||||
|  |   STORAGE_EXPIRE_SECONDS, | ||||||
|  |   STORAGE_DB, | ||||||
|  |   STORAGE_AWS_BUCKET, | ||||||
|  |   STORAGE_AWS_REGION, | ||||||
|  |   STORAGE_PASSWORD, | ||||||
|  |   STORAGE_USERNAME, | ||||||
|  |   STORAGE_FILEPATH, | ||||||
|  |   LOGGING_LEVEL, | ||||||
|  |   LOGGING_TYPE, | ||||||
|  |   LOGGING_COLORIZE, | ||||||
|  |   KEYGENERATOR_TYPE, | ||||||
|  |   KEY_GENERATOR_KEYSPACE, | ||||||
|  |   RATE_LIMITS_NORMAL_TOTAL_REQUESTS, | ||||||
|  |   RATE_LIMITS_NORMAL_EVERY_MILLISECONDS, | ||||||
|  |   RATE_LIMITS_WHITELIST_TOTAL_REQUESTS, | ||||||
|  |   RATE_LIMITS_WHITELIST_EVERY_MILLISECONDS, | ||||||
|  |   RATE_LIMITS_WHITELIST, | ||||||
|  |   RATE_LIMITS_BLACKLIST_TOTAL_REQUESTS, | ||||||
|  |   RATE_LIMITS_BLACKLIST_EVERY_MILLISECONDS, | ||||||
|  |   RATE_LIMITS_BLACKLIST, | ||||||
|  |   DOCUMENTS, | ||||||
|  | } = process.env; | ||||||
|  |  | ||||||
|  | const config = { | ||||||
|  |   host: HOST, | ||||||
|  |   port: PORT, | ||||||
|  |  | ||||||
|  |   keyLength: KEY_LENGTH, | ||||||
|  |  | ||||||
|  |   maxLength: MAX_LENGTH, | ||||||
|  |  | ||||||
|  |   staticMaxAge: STATIC_MAX_AGE, | ||||||
|  |  | ||||||
|  |   recompressStaticAssets: RECOMPRESS_STATIC_ASSETS, | ||||||
|  |  | ||||||
|  |   logging: [ | ||||||
|  |     { | ||||||
|  |       level: LOGGING_LEVEL, | ||||||
|  |       type: LOGGING_TYPE, | ||||||
|  |       colorize: LOGGING_COLORIZE, | ||||||
|  |     }, | ||||||
|  |   ], | ||||||
|  |  | ||||||
|  |   keyGenerator: { | ||||||
|  |     type: KEYGENERATOR_TYPE, | ||||||
|  |     keyspace: KEY_GENERATOR_KEYSPACE, | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   rateLimits: { | ||||||
|  |     whitelist: RATE_LIMITS_WHITELIST ? RATE_LIMITS_WHITELIST.split(",") : [], | ||||||
|  |     blacklist: RATE_LIMITS_BLACKLIST ? RATE_LIMITS_BLACKLIST.split(",") : [], | ||||||
|  |     categories: { | ||||||
|  |       normal: { | ||||||
|  |         totalRequests: RATE_LIMITS_NORMAL_TOTAL_REQUESTS, | ||||||
|  |         every: RATE_LIMITS_NORMAL_EVERY_MILLISECONDS, | ||||||
|  |       }, | ||||||
|  |       whitelist: | ||||||
|  |         RATE_LIMITS_WHITELIST_EVERY_MILLISECONDS || | ||||||
|  |         RATE_LIMITS_WHITELIST_TOTAL_REQUESTS | ||||||
|  |           ? { | ||||||
|  |               totalRequests: RATE_LIMITS_WHITELIST_TOTAL_REQUESTS, | ||||||
|  |               every: RATE_LIMITS_WHITELIST_EVERY_MILLISECONDS, | ||||||
|  |             } | ||||||
|  |           : null, | ||||||
|  |       blacklist: | ||||||
|  |         RATE_LIMITS_BLACKLIST_EVERY_MILLISECONDS || | ||||||
|  |         RATE_LIMITS_BLACKLIST_TOTAL_REQUESTS | ||||||
|  |           ? { | ||||||
|  |               totalRequests: RATE_LIMITS_WHITELIST_TOTAL_REQUESTS, | ||||||
|  |               every: RATE_LIMITS_BLACKLIST_EVERY_MILLISECONDS, | ||||||
|  |             } | ||||||
|  |           : null, | ||||||
|  |     }, | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   storage: { | ||||||
|  |     type: STORAGE_TYPE, | ||||||
|  |     host: STORAGE_HOST, | ||||||
|  |     port: STORAGE_PORT, | ||||||
|  |     expire: STORAGE_EXPIRE_SECONDS, | ||||||
|  |     bucket: STORAGE_AWS_BUCKET, | ||||||
|  |     region: STORAGE_AWS_REGION, | ||||||
|  |     connectionUrl: `postgres://${STORAGE_USERNAME}:${STORAGE_PASSWORD}@${STORAGE_HOST}:${STORAGE_PORT}/${STORAGE_DB}`, | ||||||
|  |     db: STORAGE_DB, | ||||||
|  |     user: STORAGE_USERNAME, | ||||||
|  |     password: STORAGE_PASSWORD, | ||||||
|  |     path: STORAGE_FILEPATH, | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   documents: DOCUMENTS | ||||||
|  |     ? DOCUMENTS.split(",").reduce((acc, item) => { | ||||||
|  |         const keyAndValueArray = item.replace(/\s/g, "").split("="); | ||||||
|  |         return { ...acc, [keyAndValueArray[0]]: keyAndValueArray[1] }; | ||||||
|  |       }, {}) | ||||||
|  |     : null, | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | console.log(JSON.stringify(config)); | ||||||
							
								
								
									
										9
									
								
								docker-entrypoint.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docker-entrypoint.sh
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | #!/bin/bash | ||||||
|  |  | ||||||
|  | # We use this file to translate environmental variables to .env files used by the application | ||||||
|  |  | ||||||
|  | set -e | ||||||
|  |  | ||||||
|  | node ./docker-entrypoint.js > ./config.js | ||||||
|  |  | ||||||
|  | exec "$@" | ||||||
| @@ -1,4 +1,5 @@ | |||||||
| var winston = require('winston'); | var winston = require('winston'); | ||||||
|  | var Busboy = require('busboy'); | ||||||
|  |  | ||||||
| // For handling serving stored documents | // For handling serving stored documents | ||||||
|  |  | ||||||
| @@ -15,34 +16,56 @@ var DocumentHandler = function(options) { | |||||||
| DocumentHandler.defaultKeyLength = 10; | DocumentHandler.defaultKeyLength = 10; | ||||||
|  |  | ||||||
| // Handle retrieving a document | // Handle retrieving a document | ||||||
| DocumentHandler.prototype.handleGet = function(key, response, skipExpire) { | DocumentHandler.prototype.handleGet = function(request, response, config) { | ||||||
|  |   const key = request.params.id.split('.')[0]; | ||||||
|  |   const skipExpire = !!config.documents[key]; | ||||||
|  |  | ||||||
|   this.store.get(key, function(ret) { |   this.store.get(key, function(ret) { | ||||||
|     if (ret) { |     if (ret) { | ||||||
|       winston.verbose('retrieved document', { key: key }); |       winston.verbose('retrieved document', { key: key }); | ||||||
|       response.writeHead(200, { 'content-type': 'application/json' }); |       response.writeHead(200, { 'content-type': 'application/json' }); | ||||||
|  |       if (request.method === 'HEAD') { | ||||||
|  |         response.end(); | ||||||
|  |       } else { | ||||||
|         response.end(JSON.stringify({ data: ret, key: key })); |         response.end(JSON.stringify({ data: ret, key: key })); | ||||||
|       } |       } | ||||||
|  |     } | ||||||
|     else { |     else { | ||||||
|       winston.warn('document not found', { key: key }); |       winston.warn('document not found', { key: key }); | ||||||
|       response.writeHead(404, { 'content-type': 'application/json' }); |       response.writeHead(404, { 'content-type': 'application/json' }); | ||||||
|  |       if (request.method === 'HEAD') { | ||||||
|  |         response.end(); | ||||||
|  |       } else { | ||||||
|         response.end(JSON.stringify({ message: 'Document not found.' })); |         response.end(JSON.stringify({ message: 'Document not found.' })); | ||||||
|       } |       } | ||||||
|  |     } | ||||||
|   }, skipExpire); |   }, skipExpire); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| // Handle retrieving the raw version of a document | // Handle retrieving the raw version of a document | ||||||
| DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) { | DocumentHandler.prototype.handleRawGet = function(request, response, config) { | ||||||
|  |   const key = request.params.id.split('.')[0]; | ||||||
|  |   const skipExpire = !!config.documents[key]; | ||||||
|  |  | ||||||
|   this.store.get(key, function(ret) { |   this.store.get(key, function(ret) { | ||||||
|     if (ret) { |     if (ret) { | ||||||
|       winston.verbose('retrieved raw document', { key: key }); |       winston.verbose('retrieved raw document', { key: key }); | ||||||
|       response.writeHead(200, { 'content-type': 'text/plain' }); |       response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' }); | ||||||
|  |       if (request.method === 'HEAD') { | ||||||
|  |         response.end(); | ||||||
|  |       } else { | ||||||
|         response.end(ret); |         response.end(ret); | ||||||
|       } |       } | ||||||
|  |     } | ||||||
|     else { |     else { | ||||||
|       winston.warn('raw document not found', { key: key }); |       winston.warn('raw document not found', { key: key }); | ||||||
|       response.writeHead(404, { 'content-type': 'application/json' }); |       response.writeHead(404, { 'content-type': 'application/json' }); | ||||||
|  |       if (request.method === 'HEAD') { | ||||||
|  |         response.end(); | ||||||
|  |       } else { | ||||||
|         response.end(JSON.stringify({ message: 'Document not found.' })); |         response.end(JSON.stringify({ message: 'Document not found.' })); | ||||||
|       } |       } | ||||||
|  |     } | ||||||
|   }, skipExpire); |   }, skipExpire); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -51,11 +74,10 @@ DocumentHandler.prototype.handlePost = function(request, response) { | |||||||
|   var _this = this; |   var _this = this; | ||||||
|   var buffer = ''; |   var buffer = ''; | ||||||
|   var cancelled = false; |   var cancelled = false; | ||||||
|   request.on('data', function(data) { |  | ||||||
|     if (!buffer) { |   // What to do when done | ||||||
|       response.writeHead(200, { 'content-type': 'application/json' }); |   var onSuccess = function () { | ||||||
|     } |     // Check length | ||||||
|     buffer += data.toString(); |  | ||||||
|     if (_this.maxLength && buffer.length > _this.maxLength) { |     if (_this.maxLength && buffer.length > _this.maxLength) { | ||||||
|       cancelled = true; |       cancelled = true; | ||||||
|       winston.warn('document >maxLength', { maxLength: _this.maxLength }); |       winston.warn('document >maxLength', { maxLength: _this.maxLength }); | ||||||
| @@ -63,14 +85,14 @@ DocumentHandler.prototype.handlePost = function(request, response) { | |||||||
|       response.end( |       response.end( | ||||||
|         JSON.stringify({ message: 'Document exceeds maximum length.' }) |         JSON.stringify({ message: 'Document exceeds maximum length.' }) | ||||||
|       ); |       ); | ||||||
|  |       return; | ||||||
|     } |     } | ||||||
|   }); |     // And then save if we should | ||||||
|   request.on('end', function(end) { |  | ||||||
|     if (cancelled) return; |  | ||||||
|     _this.chooseKey(function (key) { |     _this.chooseKey(function (key) { | ||||||
|       _this.store.set(key, buffer, function (res) { |       _this.store.set(key, buffer, function (res) { | ||||||
|         if (res) { |         if (res) { | ||||||
|           winston.verbose('added document', { key: key }); |           winston.verbose('added document', { key: key }); | ||||||
|  |           response.writeHead(200, { 'content-type': 'application/json' }); | ||||||
|           response.end(JSON.stringify({ key: key })); |           response.end(JSON.stringify({ key: key })); | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
| @@ -80,12 +102,37 @@ DocumentHandler.prototype.handlePost = function(request, response) { | |||||||
|         } |         } | ||||||
|       }); |       }); | ||||||
|     }); |     }); | ||||||
|  |   }; | ||||||
|  |  | ||||||
|  |   // If we should, parse a form to grab the data | ||||||
|  |   var ct = request.headers['content-type']; | ||||||
|  |   if (ct && ct.split(';')[0] === 'multipart/form-data') { | ||||||
|  |     var busboy = new Busboy({ headers: request.headers }); | ||||||
|  |     busboy.on('field', function (fieldname, val) { | ||||||
|  |       if (fieldname === 'data') { | ||||||
|  |         buffer = val; | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |     busboy.on('finish', function () { | ||||||
|  |       onSuccess(); | ||||||
|  |     }); | ||||||
|  |     request.pipe(busboy); | ||||||
|  |   // Otherwise, use our own and just grab flat data from POST body | ||||||
|  |   } else { | ||||||
|  |     request.on('data', function (data) { | ||||||
|  |       buffer += data.toString(); | ||||||
|  |     }); | ||||||
|  |     request.on('end', function () { | ||||||
|  |       if (cancelled) { return; } | ||||||
|  |       onSuccess(); | ||||||
|     }); |     }); | ||||||
|     request.on('error', function (error) { |     request.on('error', function (error) { | ||||||
|       winston.error('connection error: ' + error.message); |       winston.error('connection error: ' + error.message); | ||||||
|       response.writeHead(500, { 'content-type': 'application/json' }); |       response.writeHead(500, { 'content-type': 'application/json' }); | ||||||
|       response.end(JSON.stringify({ message: 'Connection error.' })); |       response.end(JSON.stringify({ message: 'Connection error.' })); | ||||||
|  |       cancelled = true; | ||||||
|     }); |     }); | ||||||
|  |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| // Keep choosing keys until one isn't taken | // Keep choosing keys until one isn't taken | ||||||
| @@ -98,7 +145,7 @@ DocumentHandler.prototype.chooseKey = function(callback) { | |||||||
|     } else { |     } else { | ||||||
|       callback(key); |       callback(key); | ||||||
|     } |     } | ||||||
|   }); |   }, true); // Don't bump expirations when key searching | ||||||
| }; | }; | ||||||
|  |  | ||||||
| DocumentHandler.prototype.acceptableKey = function() { | DocumentHandler.prototype.acceptableKey = function() { | ||||||
|   | |||||||
							
								
								
									
										56
									
								
								lib/document_stores/amazon-s3.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								lib/document_stores/amazon-s3.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | /*global require,module,process*/ | ||||||
|  |  | ||||||
|  | var AWS = require('aws-sdk'); | ||||||
|  | var winston = require('winston'); | ||||||
|  |  | ||||||
|  | var AmazonS3DocumentStore = function(options) { | ||||||
|  |   this.expire = options.expire; | ||||||
|  |   this.bucket = options.bucket; | ||||||
|  |   this.client = new AWS.S3({region: options.region}); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | AmazonS3DocumentStore.prototype.get = function(key, callback, skipExpire) { | ||||||
|  |   var _this = this; | ||||||
|  |  | ||||||
|  |   var req = { | ||||||
|  |     Bucket: _this.bucket, | ||||||
|  |     Key: key | ||||||
|  |   }; | ||||||
|  |  | ||||||
|  |   _this.client.getObject(req, function(err, data) { | ||||||
|  |     if(err) { | ||||||
|  |       callback(false); | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |       callback(data.Body.toString('utf-8')); | ||||||
|  |       if (_this.expire && !skipExpire) { | ||||||
|  |         winston.warn('amazon s3 store cannot set expirations on keys'); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | AmazonS3DocumentStore.prototype.set = function(key, data, callback, skipExpire) { | ||||||
|  |   var _this = this; | ||||||
|  |  | ||||||
|  |   var req = { | ||||||
|  |     Bucket: _this.bucket, | ||||||
|  |     Key: key, | ||||||
|  |     Body: data, | ||||||
|  |     ContentType: 'text/plain' | ||||||
|  |   }; | ||||||
|  |  | ||||||
|  |   _this.client.putObject(req, function(err, data) { | ||||||
|  |     if (err) { | ||||||
|  |       callback(false); | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |       callback(true); | ||||||
|  |       if (_this.expire && !skipExpire) { | ||||||
|  |         winston.warn('amazon s3 store cannot set expirations on keys'); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | module.exports = AmazonS3DocumentStore; | ||||||
							
								
								
									
										89
									
								
								lib/document_stores/google-datastore.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								lib/document_stores/google-datastore.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,89 @@ | |||||||
|  | /*global require,module,process*/ | ||||||
|  |  | ||||||
|  | const Datastore = require('@google-cloud/datastore'); | ||||||
|  | const winston = require('winston'); | ||||||
|  |  | ||||||
|  | class GoogleDatastoreDocumentStore { | ||||||
|  |  | ||||||
|  |   // Create a new store with options | ||||||
|  |   constructor(options) { | ||||||
|  |     this.kind = "Haste"; | ||||||
|  |     this.expire = options.expire; | ||||||
|  |     this.datastore = new Datastore(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   // Save file in a key | ||||||
|  |   set(key, data, callback, skipExpire) { | ||||||
|  |     var expireTime = (skipExpire || this.expire === undefined) ? null : new Date(Date.now() + this.expire * 1000); | ||||||
|  |  | ||||||
|  |     var taskKey = this.datastore.key([this.kind, key]) | ||||||
|  |     var task = { | ||||||
|  |       key: taskKey, | ||||||
|  |       data: [ | ||||||
|  |         { | ||||||
|  |           name: 'value', | ||||||
|  |           value: data, | ||||||
|  |           excludeFromIndexes: true | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |           name: 'expiration', | ||||||
|  |           value: expireTime | ||||||
|  |         } | ||||||
|  |       ] | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     this.datastore.insert(task).then(() => { | ||||||
|  |       callback(true); | ||||||
|  |     }) | ||||||
|  |     .catch(err => { | ||||||
|  |       callback(false); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   // Get a file from a key | ||||||
|  |   get(key, callback, skipExpire) { | ||||||
|  |     var taskKey = this.datastore.key([this.kind, key]) | ||||||
|  |  | ||||||
|  |     this.datastore.get(taskKey).then((entity) => { | ||||||
|  |       if (skipExpire || entity[0]["expiration"] == null) { | ||||||
|  |         callback(entity[0]["value"]); | ||||||
|  |       } | ||||||
|  |       else { | ||||||
|  |         // check for expiry | ||||||
|  |         if (entity[0]["expiration"] < new Date()) { | ||||||
|  |           winston.info("document expired", {key: key, expiration: entity[0]["expiration"], check: new Date(null)}); | ||||||
|  |           callback(false); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |           // update expiry | ||||||
|  |           var task = { | ||||||
|  |             key: taskKey, | ||||||
|  |             data: [ | ||||||
|  |               { | ||||||
|  |                 name: 'value', | ||||||
|  |                 value: entity[0]["value"], | ||||||
|  |                 excludeFromIndexes: true | ||||||
|  |               }, | ||||||
|  |               { | ||||||
|  |                 name: 'expiration', | ||||||
|  |                 value: new Date(Date.now() + this.expire * 1000) | ||||||
|  |               } | ||||||
|  |             ] | ||||||
|  |           }; | ||||||
|  |           this.datastore.update(task).then(() => { | ||||||
|  |           }) | ||||||
|  |           .catch(err => { | ||||||
|  |             winston.error("failed to update expiration", {error: err}); | ||||||
|  |           }); | ||||||
|  |           callback(entity[0]["value"]); | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     }) | ||||||
|  |     .catch(err => { | ||||||
|  |         winston.error("Error retrieving value from Google Datastore", {error: err}); | ||||||
|  |         callback(false); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | module.exports = GoogleDatastoreDocumentStore; | ||||||
| @@ -1,45 +1,54 @@ | |||||||
| var memcached = require('memcache'); | const memcached = require('memcached'); | ||||||
| var winston = require('winston'); | const winston = require('winston'); | ||||||
|  |  | ||||||
|  | class MemcachedDocumentStore { | ||||||
|  |  | ||||||
|   // Create a new store with options |   // Create a new store with options | ||||||
| var MemcachedDocumentStore = function(options) { |   constructor(options) { | ||||||
|     this.expire = options.expire; |     this.expire = options.expire; | ||||||
|   if (!MemcachedDocumentStore.client) { |  | ||||||
|     MemcachedDocumentStore.connect(options); |     const host = options.host || '127.0.0.1'; | ||||||
|  |     const port = options.port || 11211; | ||||||
|  |     const url = `${host}:${port}`; | ||||||
|  |     this.connect(url); | ||||||
|   } |   } | ||||||
| }; |  | ||||||
|  |  | ||||||
|   // Create a connection |   // Create a connection | ||||||
| MemcachedDocumentStore.connect = function(options) { |   connect(url) { | ||||||
|   var host = options.host || '127.0.0.1'; |     this.client = new memcached(url); | ||||||
|   var port = options.port || 11211; |  | ||||||
|   this.client = new memcached.Client(port, host); |     winston.info(`connecting to memcached on ${url}`); | ||||||
|   this.client.connect(); |  | ||||||
|   this.client.on('connect', function() { |     this.client.on('failure', function(error) { | ||||||
|     winston.info('connected to memcached on ' + host + ':' + port); |       winston.info('error connecting to memcached', {error}); | ||||||
|     }); |     }); | ||||||
|   this.client.on('error', function(e) { |   } | ||||||
|     winston.info('error connecting to memcached', { error: e }); |  | ||||||
|   }); |  | ||||||
| }; |  | ||||||
|  |  | ||||||
|   // Save file in a key |   // Save file in a key | ||||||
| MemcachedDocumentStore.prototype.set = |   set(key, data, callback, skipExpire) { | ||||||
| function(key, data, callback, skipExpire) { |     this.client.set(key, data, skipExpire ? 0 : this.expire || 0, (error) => { | ||||||
|   MemcachedDocumentStore.client.set(key, data, function(err, reply) { |       callback(!error); | ||||||
|     err ? callback(false) : callback(true); |     }); | ||||||
|   }, skipExpire ? 0 : this.expire); |   } | ||||||
| }; |  | ||||||
|  |  | ||||||
|   // Get a file from a key |   // Get a file from a key | ||||||
| MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) { |   get(key, callback, skipExpire) { | ||||||
|   var _this = this; |     this.client.get(key, (error, data) => { | ||||||
|   MemcachedDocumentStore.client.get(key, function(err, reply) { |       const value = error ? false : data; | ||||||
|     callback(err ? false : reply); |  | ||||||
|     if (_this.expire && !skipExpire) { |       callback(value); | ||||||
|       winston.warn('store does not currently push forward expirations on GET'); |  | ||||||
|  |       // Update the key so that the expiration is pushed forward | ||||||
|  |       if (value && !skipExpire) { | ||||||
|  |         this.set(key, data, (updateSucceeded) => { | ||||||
|  |           if (!updateSucceeded) { | ||||||
|  |             winston.error('failed to update expiration on GET', {key}); | ||||||
|  |           } | ||||||
|  |         }, skipExpire); | ||||||
|       } |       } | ||||||
|     }); |     }); | ||||||
| }; |   } | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = MemcachedDocumentStore; | module.exports = MemcachedDocumentStore; | ||||||
|   | |||||||
							
								
								
									
										88
									
								
								lib/document_stores/mongo.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								lib/document_stores/mongo.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,88 @@ | |||||||
|  |  | ||||||
|  |  | ||||||
|  | var MongoClient = require('mongodb').MongoClient, | ||||||
|  |     winston = require('winston'); | ||||||
|  |  | ||||||
|  | var MongoDocumentStore = function (options) { | ||||||
|  |     this.expire = options.expire; | ||||||
|  |     this.connectionUrl = process.env.DATABASE_URl || options.connectionUrl; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { | ||||||
|  |     var now = Math.floor(new Date().getTime() / 1000), | ||||||
|  |         that = this; | ||||||
|  |  | ||||||
|  |     this.safeConnect(function (err, db) { | ||||||
|  |         if (err) | ||||||
|  |             return callback(false); | ||||||
|  |          | ||||||
|  |         db.collection('entries').update({ | ||||||
|  |             'entry_id': key, | ||||||
|  |             $or: [ | ||||||
|  |                 { expiration: -1 }, | ||||||
|  |                 { expiration: { $gt: now } } | ||||||
|  |             ] | ||||||
|  |         }, { | ||||||
|  |             'entry_id': key, | ||||||
|  |             'value': data, | ||||||
|  |             'expiration': that.expire && !skipExpire ? that.expire + now : -1 | ||||||
|  |         }, { | ||||||
|  |             upsert: true | ||||||
|  |         }, function (err, existing) { | ||||||
|  |             if (err) { | ||||||
|  |                 winston.error('error persisting value to mongodb', { error: err }); | ||||||
|  |                 return callback(false); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             callback(true); | ||||||
|  |         }); | ||||||
|  |     }); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { | ||||||
|  |     var now = Math.floor(new Date().getTime() / 1000), | ||||||
|  |         that = this; | ||||||
|  |  | ||||||
|  |     this.safeConnect(function (err, db) { | ||||||
|  |         if (err) | ||||||
|  |             return callback(false); | ||||||
|  |          | ||||||
|  |         db.collection('entries').findOne({ | ||||||
|  |             'entry_id': key, | ||||||
|  |             $or: [ | ||||||
|  |                 { expiration: -1 }, | ||||||
|  |                 { expiration: { $gt: now } } | ||||||
|  |             ] | ||||||
|  |         }, function (err, entry) { | ||||||
|  |             if (err) { | ||||||
|  |                 winston.error('error persisting value to mongodb', { error: err }); | ||||||
|  |                 return callback(false); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             callback(entry === null ? false : entry.value); | ||||||
|  |  | ||||||
|  |             if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) { | ||||||
|  |                 db.collection('entries').update({ | ||||||
|  |                     'entry_id': key | ||||||
|  |                 }, { | ||||||
|  |                     $set: { | ||||||
|  |                         'expiration': that.expire + now | ||||||
|  |                     } | ||||||
|  |                 }, function (err, result) { }); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |     }); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | MongoDocumentStore.prototype.safeConnect = function (callback) { | ||||||
|  |     MongoClient.connect(this.connectionUrl, function (err, db) { | ||||||
|  |         if (err) { | ||||||
|  |             winston.error('error connecting to mongodb', { error: err }); | ||||||
|  |             callback(err); | ||||||
|  |         } else { | ||||||
|  |             callback(undefined, db); | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | module.exports = MongoDocumentStore; | ||||||
							
								
								
									
										80
									
								
								lib/document_stores/postgres.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								lib/document_stores/postgres.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,80 @@ | |||||||
|  | /*global require,module,process*/ | ||||||
|  |  | ||||||
|  | var winston = require('winston'); | ||||||
|  | const {Pool} = require('pg'); | ||||||
|  |  | ||||||
|  | // create table entries (id serial primary key, key varchar(255) not null, value text not null, expiration int, unique(key)); | ||||||
|  |  | ||||||
|  | // A postgres document store | ||||||
|  | var PostgresDocumentStore = function (options) { | ||||||
|  |   this.expireJS = options.expire; | ||||||
|  |  | ||||||
|  |   const connectionString = process.env.DATABASE_URL || options.connectionUrl; | ||||||
|  |   this.pool = new Pool({connectionString}); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | PostgresDocumentStore.prototype = { | ||||||
|  |  | ||||||
|  |   // Set a given key | ||||||
|  |   set: function (key, data, callback, skipExpire) { | ||||||
|  |     var now = Math.floor(new Date().getTime() / 1000); | ||||||
|  |     var that = this; | ||||||
|  |     this.safeConnect(function (err, client, done) { | ||||||
|  |       if (err) { return callback(false); } | ||||||
|  |       client.query('INSERT INTO entries (key, value, expiration) VALUES ($1, $2, $3)', [ | ||||||
|  |         key, | ||||||
|  |         data, | ||||||
|  |         that.expireJS && !skipExpire ? that.expireJS + now : null | ||||||
|  |       ], function (err) { | ||||||
|  |         if (err) { | ||||||
|  |           winston.error('error persisting value to postgres', { error: err }); | ||||||
|  |           return callback(false); | ||||||
|  |         } | ||||||
|  |         callback(true); | ||||||
|  |         done(); | ||||||
|  |       }); | ||||||
|  |     }); | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   // Get a given key's data | ||||||
|  |   get: function (key, callback, skipExpire) { | ||||||
|  |     var now = Math.floor(new Date().getTime() / 1000); | ||||||
|  |     var that = this; | ||||||
|  |     this.safeConnect(function (err, client, done) { | ||||||
|  |       if (err) { return callback(false); } | ||||||
|  |       client.query('SELECT id,value,expiration from entries where KEY = $1 and (expiration IS NULL or expiration > $2)', [key, now], function (err, result) { | ||||||
|  |         if (err) { | ||||||
|  |           winston.error('error retrieving value from postgres', { error: err }); | ||||||
|  |           return callback(false); | ||||||
|  |         } | ||||||
|  |         callback(result.rows.length ? result.rows[0].value : false); | ||||||
|  |         if (result.rows.length && that.expireJS && !skipExpire) { | ||||||
|  |           client.query('UPDATE entries SET expiration = $1 WHERE ID = $2', [ | ||||||
|  |             that.expireJS + now, | ||||||
|  |             result.rows[0].id | ||||||
|  |           ], function (err) { | ||||||
|  |             if (!err) { | ||||||
|  |               done(); | ||||||
|  |             } | ||||||
|  |           }); | ||||||
|  |         } else { | ||||||
|  |           done(); | ||||||
|  |         } | ||||||
|  |       }); | ||||||
|  |     }); | ||||||
|  |   }, | ||||||
|  |  | ||||||
|  |   // A connection wrapper | ||||||
|  |   safeConnect: function (callback) { | ||||||
|  |     this.pool.connect((error, client, done) => { | ||||||
|  |       if (error) { | ||||||
|  |         winston.error('error connecting to postgres', {error}); | ||||||
|  |         callback(error); | ||||||
|  |       } else { | ||||||
|  |         callback(undefined, client, done); | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | module.exports = PostgresDocumentStore; | ||||||
| @@ -8,9 +8,13 @@ var winston = require('winston'); | |||||||
| // options[db] - The db to use (default 0) | // options[db] - The db to use (default 0) | ||||||
| // options[expire] - The time to live for each key set (default never) | // options[expire] - The time to live for each key set (default never) | ||||||
|  |  | ||||||
| var RedisDocumentStore = function(options) { | var RedisDocumentStore = function(options, client) { | ||||||
|   this.expire = options.expire; |   this.expire = options.expire; | ||||||
|   if (!RedisDocumentStore.client) { |   if (client) { | ||||||
|  |     winston.info('using predefined redis client'); | ||||||
|  |     RedisDocumentStore.client = client; | ||||||
|  |   } else if (!RedisDocumentStore.client) { | ||||||
|  |     winston.info('configuring redis'); | ||||||
|     RedisDocumentStore.connect(options); |     RedisDocumentStore.connect(options); | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
| @@ -21,11 +25,20 @@ RedisDocumentStore.connect = function(options) { | |||||||
|   var port = options.port || 6379; |   var port = options.port || 6379; | ||||||
|   var index = options.db || 0; |   var index = options.db || 0; | ||||||
|   RedisDocumentStore.client = redis.createClient(port, host); |   RedisDocumentStore.client = redis.createClient(port, host); | ||||||
|   RedisDocumentStore.client.select(index, function(err, reply) { |   // authenticate if password is provided | ||||||
|  |   if (options.password) { | ||||||
|  |     RedisDocumentStore.client.auth(options.password); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   RedisDocumentStore.client.on('error', function(err) { | ||||||
|  |     winston.error('redis disconnected', err); | ||||||
|  |   }); | ||||||
|  |  | ||||||
|  |   RedisDocumentStore.client.select(index, function(err) { | ||||||
|     if (err) { |     if (err) { | ||||||
|       winston.error( |       winston.error( | ||||||
|         'error connecting to redis index ' + index, |         'error connecting to redis index ' + index, | ||||||
|         { error: err.message } |         { error: err } | ||||||
|       ); |       ); | ||||||
|       process.exit(1); |       process.exit(1); | ||||||
|     } |     } | ||||||
| @@ -38,7 +51,7 @@ RedisDocumentStore.connect = function(options) { | |||||||
| // Save file in a key | // Save file in a key | ||||||
| RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) { | RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) { | ||||||
|   var _this = this; |   var _this = this; | ||||||
|   RedisDocumentStore.client.set(key, data, function(err, reply) { |   RedisDocumentStore.client.set(key, data, function(err) { | ||||||
|     if (err) { |     if (err) { | ||||||
|       callback(false); |       callback(false); | ||||||
|     } |     } | ||||||
| @@ -54,7 +67,7 @@ RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) { | |||||||
| // Expire a key in expire time if set | // Expire a key in expire time if set | ||||||
| RedisDocumentStore.prototype.setExpiration = function(key) { | RedisDocumentStore.prototype.setExpiration = function(key) { | ||||||
|   if (this.expire) { |   if (this.expire) { | ||||||
|     RedisDocumentStore.client.expire(key, this.expire, function(err, reply) { |     RedisDocumentStore.client.expire(key, this.expire, function(err) { | ||||||
|       if (err) { |       if (err) { | ||||||
|         winston.error('failed to set expiry on key: ' + key); |         winston.error('failed to set expiry on key: ' + key); | ||||||
|       } |       } | ||||||
|   | |||||||
							
								
								
									
										46
									
								
								lib/document_stores/rethinkdb.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								lib/document_stores/rethinkdb.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | |||||||
|  | const crypto = require('crypto'); | ||||||
|  | const rethink = require('rethinkdbdash'); | ||||||
|  | const winston = require('winston'); | ||||||
|  |  | ||||||
|  | const md5 = (str) => { | ||||||
|  |   const md5sum = crypto.createHash('md5'); | ||||||
|  |   md5sum.update(str); | ||||||
|  |   return md5sum.digest('hex'); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | class RethinkDBStore { | ||||||
|  |   constructor(options) { | ||||||
|  |     this.client = rethink({ | ||||||
|  |       silent: true, | ||||||
|  |       host: options.host || '127.0.0.1', | ||||||
|  |       port: options.port || 28015, | ||||||
|  |       db: options.db || 'haste', | ||||||
|  |       user: options.user || 'admin', | ||||||
|  |       password: options.password || '' | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   set(key, data, callback) { | ||||||
|  |     this.client.table('uploads').insert({ id: md5(key), data: data }).run((error) => { | ||||||
|  |       if (error) { | ||||||
|  |         callback(false); | ||||||
|  |         winston.error('failed to insert to table', error); | ||||||
|  |         return; | ||||||
|  |       } | ||||||
|  |       callback(true); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   get(key, callback) { | ||||||
|  |     this.client.table('uploads').get(md5(key)).run((error, result) => { | ||||||
|  |       if (error || !result) { | ||||||
|  |         callback(false); | ||||||
|  |         if (error) winston.error('failed to insert to table', error); | ||||||
|  |         return; | ||||||
|  |       } | ||||||
|  |       callback(result.data); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | module.exports = RethinkDBStore; | ||||||
							
								
								
									
										32
									
								
								lib/key_generators/dictionary.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								lib/key_generators/dictionary.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | const fs = require('fs'); | ||||||
|  |  | ||||||
|  | module.exports = class DictionaryGenerator { | ||||||
|  |  | ||||||
|  |   constructor(options, readyCallback) { | ||||||
|  |     // Check options format | ||||||
|  |     if (!options)      throw Error('No options passed to generator'); | ||||||
|  |     if (!options.path) throw Error('No dictionary path specified in options'); | ||||||
|  |  | ||||||
|  |     // Load dictionary | ||||||
|  |     fs.readFile(options.path, 'utf8', (err, data) => { | ||||||
|  |       if (err) throw err; | ||||||
|  |  | ||||||
|  |       this.dictionary = data.split(/[\n\r]+/); | ||||||
|  |  | ||||||
|  |       if (readyCallback) readyCallback(); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   // Generates a dictionary-based key, of keyLength words | ||||||
|  |   createKey(keyLength) { | ||||||
|  |     let text = ''; | ||||||
|  |  | ||||||
|  |     for (let i = 0; i < keyLength; i++) { | ||||||
|  |       const index = Math.floor(Math.random() * this.dictionary.length); | ||||||
|  |       text += this.dictionary[index]; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return text; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | }; | ||||||
| @@ -1,32 +1,27 @@ | |||||||
| // Draws inspiration from pwgen and http://tools.arantius.com/password | // Draws inspiration from pwgen and http://tools.arantius.com/password | ||||||
| var PhoneticKeyGenerator = function(options) { |  | ||||||
|   // No options | const randOf = (collection) => { | ||||||
|  |   return () => { | ||||||
|  |     return collection[Math.floor(Math.random() * collection.length)]; | ||||||
|  |   }; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| // Generate a phonetic key | // Helper methods to get an random vowel or consonant | ||||||
| PhoneticKeyGenerator.prototype.createKey = function(keyLength) { | const randVowel = randOf('aeiou'); | ||||||
|   var text = ''; | const randConsonant = randOf('bcdfghjklmnpqrstvwxyz'); | ||||||
|   for (var i = 0; i < keyLength; i++) { |  | ||||||
|     text += (i % 2 == 0) ? this.randConsonant() : this.randVowel(); | module.exports = class PhoneticKeyGenerator { | ||||||
|  |  | ||||||
|  |   // Generate a phonetic key of alternating consonant & vowel | ||||||
|  |   createKey(keyLength) { | ||||||
|  |     let text = ''; | ||||||
|  |     const start = Math.round(Math.random()); | ||||||
|  |  | ||||||
|  |     for (let i = 0; i < keyLength; i++) { | ||||||
|  |       text += (i % 2 == start) ? randConsonant() : randVowel(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return text; |     return text; | ||||||
|  |   } | ||||||
|  |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| PhoneticKeyGenerator.consonants = 'bcdfghjklmnpqrstvwxy'; |  | ||||||
| PhoneticKeyGenerator.vowels = 'aeiou'; |  | ||||||
|  |  | ||||||
| // Get an random vowel |  | ||||||
| PhoneticKeyGenerator.prototype.randVowel = function() { |  | ||||||
|   return PhoneticKeyGenerator.vowels[ |  | ||||||
|     Math.floor(Math.random() * PhoneticKeyGenerator.vowels.length) |  | ||||||
|   ]; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| // Get an random consonant |  | ||||||
| PhoneticKeyGenerator.prototype.randConsonant = function() { |  | ||||||
|   return PhoneticKeyGenerator.consonants[ |  | ||||||
|     Math.floor(Math.random() * PhoneticKeyGenerator.consonants.length) |  | ||||||
|   ]; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| module.exports = PhoneticKeyGenerator; |  | ||||||
|   | |||||||
| @@ -1,19 +1,20 @@ | |||||||
| var RandomKeyGenerator = function(options) { | module.exports = class RandomKeyGenerator { | ||||||
|   if (!options) { |  | ||||||
|     options = {}; |  | ||||||
|   } |  | ||||||
|   this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| // Generate a random key |   // Initialize a new generator with the given keySpace | ||||||
| RandomKeyGenerator.prototype.createKey = function(keyLength) { |   constructor(options = {}) { | ||||||
|  |     this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   // Generate a key of the given length | ||||||
|  |   createKey(keyLength) { | ||||||
|     var text = ''; |     var text = ''; | ||||||
|   var index; |  | ||||||
|     for (var i = 0; i < keyLength; i++) { |     for (var i = 0; i < keyLength; i++) { | ||||||
|     index = Math.floor(Math.random() * this.keyspace.length); |       const index = Math.floor(Math.random() * this.keyspace.length); | ||||||
|       text += this.keyspace.charAt(index); |       text += this.keyspace.charAt(index); | ||||||
|     } |     } | ||||||
|   return text; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| module.exports = RandomKeyGenerator; |     return text; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | }; | ||||||
|   | |||||||
							
								
								
									
										1652
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										1652
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										51
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								package.json
									
									
									
									
									
								
							| @@ -1,52 +1,47 @@ | |||||||
| { | { | ||||||
|  |  | ||||||
|   "name": "haste", |   "name": "haste", | ||||||
| 	"version": "0.0.1", |   "version": "0.1.0", | ||||||
|  |  | ||||||
|   "private": true, |   "private": true, | ||||||
|  |   "description": "Private Pastebin Server", | ||||||
| 	"description": "Private Paste", |   "keywords": [ | ||||||
|  |     "paste", | ||||||
| 	"keywords": [ "paste", "pastebin" ], |     "pastebin" | ||||||
|  |   ], | ||||||
|   "author": { |   "author": { | ||||||
|     "name": "John Crepezzi", |     "name": "John Crepezzi", | ||||||
|     "email": "john.crepezzi@gmail.com", |     "email": "john.crepezzi@gmail.com", | ||||||
|     "url": "http://seejohncode.com/" |     "url": "http://seejohncode.com/" | ||||||
|   }, |   }, | ||||||
|  |  | ||||||
|   "main": "haste", |   "main": "haste", | ||||||
|  |  | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
| 		"winston": "*", |     "busboy": "0.2.4", | ||||||
|     "connect": "*", |     "connect": "^3.7.0", | ||||||
|     "uglify-js": "*" |     "connect-ratelimit": "0.0.7", | ||||||
|  |     "connect-route": "0.1.5", | ||||||
|  |     "pg": "^8.0.0", | ||||||
|  |     "redis": "0.8.1", | ||||||
|  |     "redis-url": "0.1.0", | ||||||
|  |     "st": "^2.0.0", | ||||||
|  |     "uglify-js": "3.1.6", | ||||||
|  |     "winston": "^2.0.0" | ||||||
|   }, |   }, | ||||||
|  |  | ||||||
|   "devDependencies": { |   "devDependencies": { | ||||||
|     "mocha": "*", |     "mocha": "^8.1.3" | ||||||
|     "should": "*" |  | ||||||
|   }, |   }, | ||||||
|  |  | ||||||
|   "bundledDependencies": [], |   "bundledDependencies": [], | ||||||
|  |  | ||||||
| 	"engines": { |  | ||||||
| 		"node": "*" |  | ||||||
| 	}, |  | ||||||
|  |  | ||||||
|   "bin": { |   "bin": { | ||||||
|     "haste-server": "./server.js" |     "haste-server": "./server.js" | ||||||
|   }, |   }, | ||||||
|  |   "files": [ | ||||||
| 	"files": [ "server.js", "lib", "static" ], |     "server.js", | ||||||
|  |     "lib", | ||||||
|  |     "static" | ||||||
|  |   ], | ||||||
|   "directories": { |   "directories": { | ||||||
|     "lib": "./lib" |     "lib": "./lib" | ||||||
|   }, |   }, | ||||||
|  |  | ||||||
|   "scripts": { |   "scripts": { | ||||||
|     "start": "node server.js", |     "start": "node server.js", | ||||||
|     "test": "mocha -r should spec/*" |     "test": "mocha --recursive" | ||||||
|   } |   } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
| 
 | 
 | ||||||
|   "host": "localhost", |   "host": "0.0.0.0", | ||||||
|   "port": 7777, |   "port": 7777, | ||||||
| 
 | 
 | ||||||
|   "keyLength": 10, |   "keyLength": 10, | ||||||
| @@ -23,12 +23,17 @@ | |||||||
|     "type": "phonetic" |     "type": "phonetic" | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|  |   "rateLimits": { | ||||||
|  |     "categories": { | ||||||
|  |       "normal": { | ||||||
|  |         "totalRequests": 500, | ||||||
|  |         "every": 60000 | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  | 
 | ||||||
|   "storage": { |   "storage": { | ||||||
|     "type": "redis", |     "type": "file" | ||||||
|     "host": "localhost", |  | ||||||
|     "port": 6379, |  | ||||||
|     "db": 2, |  | ||||||
|     "expire": 2592000 |  | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   "documents": { |   "documents": { | ||||||
							
								
								
									
										151
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										151
									
								
								server.js
									
									
									
									
									
								
							| @@ -1,22 +1,29 @@ | |||||||
|  | // vim: set ts=2 sw=2 sts=2 et: | ||||||
| var http = require('http'); | var http = require('http'); | ||||||
| var url = require('url'); |  | ||||||
| var fs = require('fs'); | var fs = require('fs'); | ||||||
|  |  | ||||||
|  | var uglify = require('uglify-js'); | ||||||
| var winston = require('winston'); | var winston = require('winston'); | ||||||
| var connect = require('connect'); | var connect = require('connect'); | ||||||
|  | var route = require('connect-route'); | ||||||
|  | var connect_st = require('st'); | ||||||
|  | var connect_rate_limit = require('connect-ratelimit'); | ||||||
|  |  | ||||||
| var DocumentHandler = require('./lib/document_handler'); | var DocumentHandler = require('./lib/document_handler'); | ||||||
|  |  | ||||||
| // Load the configuration and set some defaults | // Load the configuration and set some defaults | ||||||
| var config = JSON.parse(fs.readFileSync('config.js', 'utf8')); | var config = require('./config.json'); | ||||||
| config.port = config.port || 7777; | config.port = process.env.PORT || config.port || 7777; | ||||||
| config.host = config.host || 'localhost'; | config.host = process.env.HOST || config.host || 'localhost'; | ||||||
|  |  | ||||||
| // Set up the logger | // Set up the logger | ||||||
| if (config.logging) { | if (config.logging) { | ||||||
|   try { |   try { | ||||||
|     winston.remove(winston.transports.Console); |     winston.remove(winston.transports.Console); | ||||||
|   } catch(er) { } |   } catch(e) { | ||||||
|  |     /* was not present */ | ||||||
|  |   } | ||||||
|  |  | ||||||
|   var detail, type; |   var detail, type; | ||||||
|   for (var i = 0; i < config.logging.length; i++) { |   for (var i = 0; i < config.logging.length; i++) { | ||||||
|     detail = config.logging[i]; |     detail = config.logging[i]; | ||||||
| @@ -34,47 +41,48 @@ if (!config.storage) { | |||||||
| if (!config.storage.type) { | if (!config.storage.type) { | ||||||
|   config.storage.type = 'file'; |   config.storage.type = 'file'; | ||||||
| } | } | ||||||
| var Store = require('./lib/document_stores/' + config.storage.type); |  | ||||||
| var preferredStore = new Store(config.storage); | var Store, preferredStore; | ||||||
|  |  | ||||||
|  | if (process.env.REDISTOGO_URL && config.storage.type === 'redis') { | ||||||
|  |   var redisClient = require('redis-url').connect(process.env.REDISTOGO_URL); | ||||||
|  |   Store = require('./lib/document_stores/redis'); | ||||||
|  |   preferredStore = new Store(config.storage, redisClient); | ||||||
|  | } | ||||||
|  | else { | ||||||
|  |   Store = require('./lib/document_stores/' + config.storage.type); | ||||||
|  |   preferredStore = new Store(config.storage); | ||||||
|  | } | ||||||
|  |  | ||||||
| // Compress the static javascript assets | // Compress the static javascript assets | ||||||
| if (config.recompressStaticAssets) { | if (config.recompressStaticAssets) { | ||||||
|   var jsp = require("uglify-js").parser; |  | ||||||
|   var pro = require("uglify-js").uglify; |  | ||||||
|   var list = fs.readdirSync('./static'); |   var list = fs.readdirSync('./static'); | ||||||
|   for (var i = 0; i < list.length; i++) { |   for (var j = 0; j < list.length; j++) { | ||||||
|     var item = list[i]; |     var item = list[j]; | ||||||
|     var orig_code, ast; |     if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) { | ||||||
|     if ((item.indexOf('.js') === item.length - 3) && |       var dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3); | ||||||
|         (item.indexOf('.min.js') === -1)) { |       var orig_code = fs.readFileSync('./static/' + item, 'utf8'); | ||||||
|       dest = item.substring(0, item.length - 3) + '.min' + |  | ||||||
|         item.substring(item.length - 3); |       fs.writeFileSync('./static/' + dest, uglify.minify(orig_code).code, 'utf8'); | ||||||
|       orig_code = fs.readFileSync('./static/' + item, 'utf8'); |  | ||||||
|       ast = jsp.parse(orig_code); |  | ||||||
|       ast = pro.ast_mangle(ast); |  | ||||||
|       ast = pro.ast_squeeze(ast); |  | ||||||
|       fs.writeFileSync('./static/' + dest, pro.gen_code(ast), 'utf8'); |  | ||||||
|       winston.info('compressed ' + item + ' into ' + dest); |       winston.info('compressed ' + item + ' into ' + dest); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| // Send the static documents into the preferred store, skipping expirations | // Send the static documents into the preferred store, skipping expirations | ||||||
|  | var path, data; | ||||||
| for (var name in config.documents) { | for (var name in config.documents) { | ||||||
|   var path = config.documents[name]; |   path = config.documents[name]; | ||||||
|   fs.readFile(path, 'utf8', function(err, data) { |   data = fs.readFileSync(path, 'utf8'); | ||||||
|     if (data && !err) { |   winston.info('loading static document', { name: name, path: path }); | ||||||
|  |   if (data) { | ||||||
|     preferredStore.set(name, data, function(cb) { |     preferredStore.set(name, data, function(cb) { | ||||||
|         winston.info('loaded static document', { name: name, path: path }); |       winston.debug('loaded static document', { success: cb }); | ||||||
|     }, true); |     }, true); | ||||||
|   } |   } | ||||||
|   else { |   else { | ||||||
|       winston.warn( |     winston.warn('failed to load static document', { name: name, path: path }); | ||||||
|         'failed to load static document', |  | ||||||
|         { name: name, path: path } |  | ||||||
|       ); |  | ||||||
|   } |   } | ||||||
|   }); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // Pick up a key generator | // Pick up a key generator | ||||||
| @@ -91,42 +99,69 @@ var documentHandler = new DocumentHandler({ | |||||||
|   keyGenerator: keyGenerator |   keyGenerator: keyGenerator | ||||||
| }); | }); | ||||||
|  |  | ||||||
| // Set the server up with a static cache | var app = connect(); | ||||||
| connect.createServer( |  | ||||||
|   // First look for api calls | // Rate limit all requests | ||||||
|   connect.router(function(app) { | if (config.rateLimits) { | ||||||
|  |   config.rateLimits.end = true; | ||||||
|  |   app.use(connect_rate_limit(config.rateLimits)); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function raw(request, response) { | ||||||
|  |   return documentHandler.handleRawGet(request, response, config); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // first look at API calls | ||||||
|  | app.use(route(function(router) { | ||||||
|   // get raw documents - support getting with extension |   // get raw documents - support getting with extension | ||||||
|     app.get('/raw/:id', function(request, response, next) { |   router.get('/raw/:id', raw); | ||||||
|       var skipExpire = !!config.documents[request.params.id]; |   router.get('/:id/raw', raw); | ||||||
|       var key = request.params.id.split('.')[0]; |   router.head('/raw/:id', raw); | ||||||
|       return documentHandler.handleRawGet(key, response, skipExpire); |   router.head('/:id/raw', raw); | ||||||
|     }); |  | ||||||
|   // add documents |   // add documents | ||||||
|     app.post('/documents', function(request, response, next) { |  | ||||||
|  |   router.post('/documents', function(request, response) { | ||||||
|     return documentHandler.handlePost(request, response); |     return documentHandler.handlePost(request, response); | ||||||
|   }); |   }); | ||||||
|  |  | ||||||
|   // get documents |   // get documents | ||||||
|     app.get('/documents/:id', function(request, response, next) { |   router.get('/documents/:id', function(request, response) { | ||||||
|       var skipExpire = !!config.documents[request.params.id]; |     return documentHandler.handleGet(request, response, config); | ||||||
|       return documentHandler.handleGet( |  | ||||||
|         request.params.id, |  | ||||||
|         response, |  | ||||||
|         skipExpire |  | ||||||
|       ); |  | ||||||
|   }); |   }); | ||||||
|   }), |  | ||||||
|   // Otherwise, static |   router.head('/documents/:id', function(request, response) { | ||||||
|   connect.staticCache(), |     return documentHandler.handleGet(request, response, config); | ||||||
|   connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }), |   }); | ||||||
|  | })); | ||||||
|  |  | ||||||
|  | // Otherwise, try to match static files | ||||||
|  | app.use(connect_st({ | ||||||
|  |   path: __dirname + '/static', | ||||||
|  |   content: { maxAge: config.staticMaxAge }, | ||||||
|  |   passthrough: true, | ||||||
|  |   index: false | ||||||
|  | })); | ||||||
|  |  | ||||||
| // Then we can loop back - and everything else should be a token, | // Then we can loop back - and everything else should be a token, | ||||||
|   // so route it back to /index.html | // so route it back to / | ||||||
|   connect.router(function(app) { | app.use(route(function(router) { | ||||||
|     app.get('/:id', function(request, response, next) { |   router.get('/:id', function(request, response, next) { | ||||||
|       request.url = request.originalUrl = '/index.html'; |     if (request.headers.accept && request.headers.accept.includes('html')) { | ||||||
|  |       request.sturl = '/'; | ||||||
|       next(); |       next(); | ||||||
|  |     } else { | ||||||
|  |       return raw(request, response); | ||||||
|  |     } | ||||||
|   }); |   }); | ||||||
|   }), | })); | ||||||
|   connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }) |  | ||||||
| ).listen(config.port, config.host); | // And match index | ||||||
|  | app.use(connect_st({ | ||||||
|  |   path: __dirname + '/static', | ||||||
|  |   content: { maxAge: config.staticMaxAge }, | ||||||
|  |   index: 'index.html' | ||||||
|  | })); | ||||||
|  |  | ||||||
|  | http.createServer(app).listen(config.port, config.host); | ||||||
|  |  | ||||||
| winston.info('listening on ' + config.host + ':' + config.port); | winston.info('listening on ' + config.host + ':' + config.port); | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| body { | body { | ||||||
| 	background: #002B36; | 	background: #fcfcfc; | ||||||
| 	padding: 20px 50px; | 	padding: 20px 50px; | ||||||
| 	margin: 0px; | 	margin: 0px; | ||||||
| } | } | ||||||
| @@ -9,7 +9,7 @@ body { | |||||||
| textarea { | textarea { | ||||||
| 	background: transparent; | 	background: transparent; | ||||||
| 	border: 0px; | 	border: 0px; | ||||||
| 	color: #fff; | 	color: #000; | ||||||
| 	padding: 0px; | 	padding: 0px; | ||||||
| 	width: 100%; | 	width: 100%; | ||||||
| 	height: 100%; | 	height: 100%; | ||||||
| @@ -17,6 +17,8 @@ textarea { | |||||||
| 	outline: none; | 	outline: none; | ||||||
| 	resize: none; | 	resize: none; | ||||||
| 	font-size: 13px; | 	font-size: 13px; | ||||||
|  | 	margin-top: 0; | ||||||
|  | 	margin-bottom: 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| /* the line numbers */ | /* the line numbers */ | ||||||
| @@ -25,12 +27,13 @@ textarea { | |||||||
| 	color: #7d7d7d; | 	color: #7d7d7d; | ||||||
| 	z-index: -1000; | 	z-index: -1000; | ||||||
| 	position: absolute; | 	position: absolute; | ||||||
| 	top: 20px; | 	/* top: 20px; */ | ||||||
| 	left: 0px; | 	left: 0px; | ||||||
| 	width: 30px; /* 30 to get 20 away from box */ | 	width: 30px; /* 30 to get 20 away from box */ | ||||||
| 	font-size: 13px; | 	font-size: 13px; | ||||||
| 	font-family: monospace; | 	font-family: monospace; | ||||||
| 	text-align: right; | 	text-align: right; | ||||||
|  | 	user-select: none; | ||||||
| } | } | ||||||
|  |  | ||||||
| /* code box when locked */ | /* code box when locked */ | ||||||
| @@ -42,6 +45,8 @@ textarea { | |||||||
| 	border: 0px; | 	border: 0px; | ||||||
| 	outline: none; | 	outline: none; | ||||||
| 	font-size: 13px; | 	font-size: 13px; | ||||||
|  | 	overflow: inherit; | ||||||
|  | 	background: #fcfcfc; | ||||||
| } | } | ||||||
|  |  | ||||||
| #box code { | #box code { | ||||||
| @@ -52,32 +57,35 @@ textarea { | |||||||
| /* key */ | /* key */ | ||||||
|  |  | ||||||
| #key { | #key { | ||||||
| 	position: fixed; | 	display: flex; | ||||||
| 	top: 0px; | 	justify-content: space-between; | ||||||
| 	right: 0px; | 	width: 100%; | ||||||
| 	z-index: +1000; /* watch out */ | 	position: sticky; | ||||||
|  | 	flex-wrap: row; | ||||||
| } | } | ||||||
|  |  | ||||||
| #box1 { | #box1 { | ||||||
| 	padding: 5px; | 	padding: 5px; | ||||||
| 	text-align: center; | 	text-align: center; | ||||||
| 	background: #00222b; | 	background: #fcfcfc; | ||||||
| } | } | ||||||
|  |  | ||||||
| #box2 { | #box2 { | ||||||
| 	background: #08323c; | 	display: flex; | ||||||
|  | 	justify-content: right; | ||||||
|  | 	background: #fcfcfc; | ||||||
| 	font-size: 0px; | 	font-size: 0px; | ||||||
| 	padding: 0px 5px; | 	padding: 0px 5px; | ||||||
| } | } | ||||||
|  |  | ||||||
| #box1 a.logo, #box1 a.logo:visited { | a.logo, a.logo:visited { | ||||||
| 	display: inline-block; | 	display: inline-block; | ||||||
| 	background: url(logo.png); | 	background: url(logo.png); | ||||||
| 	width: 126px; | 	width: 126px; | ||||||
|  | 	min-width: 126px; | ||||||
| 	height: 42px; | 	height: 42px; | ||||||
| } | } | ||||||
|  |  | ||||||
| #box1 a.logo:hover { | a.logo:hover { | ||||||
| 	background-position: 0 bottom; | 	background-position: 0 bottom; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -111,18 +119,26 @@ textarea { | |||||||
| } | } | ||||||
|  |  | ||||||
| #box3, #messages li { | #box3, #messages li { | ||||||
| 	background: #173e48; | 	position: absolute; | ||||||
|  | 	right: 100px; | ||||||
|  | 	background: #fcfcfc; | ||||||
| 	font-family: Helvetica, sans-serif; | 	font-family: Helvetica, sans-serif; | ||||||
| 	font-size: 12px; | 	font-size: 12px; | ||||||
| 	line-height: 14px; | 	line-height: 14px; | ||||||
| 	padding: 10px 15px; | 	padding: 10px 15px; | ||||||
|  | 	user-select: none; | ||||||
| } | } | ||||||
|  |  | ||||||
| #box3 .label, #messages li { | #box3 .label { | ||||||
| 	color: #fff; | 	color: #000; | ||||||
| 	font-weight: bold; | 	font-weight: bold; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #messages li { | ||||||
|  | 	color: #FFF; | ||||||
|  | 	right: 50px; | ||||||
|  | } | ||||||
|  |  | ||||||
| #box3 .shortcut { | #box3 .shortcut { | ||||||
| 	color: #c4dce3; | 	color: #c4dce3; | ||||||
| 	font-weight: normal; | 	font-weight: normal; | ||||||
| @@ -147,18 +163,15 @@ textarea { | |||||||
| #box2 .function.twitter { background-position: -153px top; } | #box2 .function.twitter { background-position: -153px top; } | ||||||
| #box2 .function.enabled.twitter { background-position: -153px center; } | #box2 .function.enabled.twitter { background-position: -153px center; } | ||||||
| #box2 .function.enabled.twitter:hover { background-position: -153px bottom; } | #box2 .function.enabled.twitter:hover { background-position: -153px bottom; } | ||||||
|  | #box2 .button-picture{ border-width: 0; font-size: inherit; } | ||||||
|  |  | ||||||
| #messages { | #messages { | ||||||
| 	position:fixed; |  | ||||||
| 	top:0px; |  | ||||||
| 	right:138px; |  | ||||||
| 	margin:0; | 	margin:0; | ||||||
| 	padding:0; | 	padding:0; | ||||||
| 	width:400px; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| #messages li { | #messages li { | ||||||
| 	background:rgba(23,62,72,0.8); | 	background:rgba(252,252,252,0.8); | ||||||
| 	margin:0 auto; | 	margin:0 auto; | ||||||
| 	list-style:none; | 	list-style:none; | ||||||
| } | } | ||||||
| @@ -166,3 +179,4 @@ textarea { | |||||||
| #messages li.error { | #messages li.error { | ||||||
| 	background:rgba(102,8,0,0.8); | 	background:rgba(102,8,0,0.8); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,3 +1,5 @@ | |||||||
|  | /* global $, hljs, window, document */ | ||||||
|  |  | ||||||
| ///// represents a single document | ///// represents a single document | ||||||
|  |  | ||||||
| var haste_document = function() { | var haste_document = function() { | ||||||
| @@ -42,10 +44,10 @@ haste_document.prototype.load = function(key, callback, lang) { | |||||||
|         value: high.value, |         value: high.value, | ||||||
|         key: key, |         key: key, | ||||||
|         language: high.language || lang, |         language: high.language || lang, | ||||||
|         lineCount: res.data.split("\n").length |         lineCount: res.data.split('\n').length | ||||||
|       }); |       }); | ||||||
|     }, |     }, | ||||||
|     error: function(err) { |     error: function() { | ||||||
|       callback(false); |       callback(false); | ||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
| @@ -62,6 +64,7 @@ haste_document.prototype.save = function(data, callback) { | |||||||
|     type: 'post', |     type: 'post', | ||||||
|     data: data, |     data: data, | ||||||
|     dataType: 'json', |     dataType: 'json', | ||||||
|  |     contentType: 'text/plain; charset=utf-8', | ||||||
|     success: function(res) { |     success: function(res) { | ||||||
|       _this.locked = true; |       _this.locked = true; | ||||||
|       _this.key = res.key; |       _this.key = res.key; | ||||||
| @@ -70,7 +73,7 @@ haste_document.prototype.save = function(data, callback) { | |||||||
|         value: high.value, |         value: high.value, | ||||||
|         key: res.key, |         key: res.key, | ||||||
|         language: high.language, |         language: high.language, | ||||||
|         lineCount: data.split("\n").length |         lineCount: data.split('\n').length | ||||||
|       }); |       }); | ||||||
|     }, |     }, | ||||||
|     error: function(res) { |     error: function(res) { | ||||||
| @@ -165,9 +168,9 @@ haste.extensionMap = { | |||||||
|   rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go', |   rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go', | ||||||
|   xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript', |   xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript', | ||||||
|   lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec', |   lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec', | ||||||
|   vala: 'vala', cs: 'cs', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini', |   vala: 'vala', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini', | ||||||
|   diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell', |   diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell', | ||||||
|   md: 'markdown', txt: '', coffee: 'coffee' |   md: 'markdown', txt: '', coffee: 'coffee', swift: 'swift' | ||||||
| }; | }; | ||||||
|  |  | ||||||
| // Look up the extension preferred for a type | // Look up the extension preferred for a type | ||||||
| @@ -274,7 +277,7 @@ haste.prototype.configureButtons = function() { | |||||||
|       $where: $('#box2 .new'), |       $where: $('#box2 .new'), | ||||||
|       label: 'New', |       label: 'New', | ||||||
|       shortcut: function(evt) { |       shortcut: function(evt) { | ||||||
|         return evt.ctrlKey && evt.keyCode === 78   |         return evt.ctrlKey && evt.keyCode === 78; | ||||||
|       }, |       }, | ||||||
|       shortcutDescription: 'control + n', |       shortcutDescription: 'control + n', | ||||||
|       action: function() { |       action: function() { | ||||||
| @@ -307,9 +310,9 @@ haste.prototype.configureButtons = function() { | |||||||
|       $where: $('#box2 .twitter'), |       $where: $('#box2 .twitter'), | ||||||
|       label: 'Twitter', |       label: 'Twitter', | ||||||
|       shortcut: function(evt) { |       shortcut: function(evt) { | ||||||
|         return _this.options.twitter && _this.doc.locked && evt.ctrlKey && evt.keyCode == 84; |         return _this.options.twitter && _this.doc.locked && evt.shiftKey && evt.ctrlKey && evt.keyCode == 84; | ||||||
|       }, |       }, | ||||||
|       shortcutDescription: 'control + t', |       shortcutDescription: 'control + shift + t', | ||||||
|       action: function() { |       action: function() { | ||||||
|         window.open('https://twitter.com/share?url=' + encodeURI(window.location.href)); |         window.open('https://twitter.com/share?url=' + encodeURI(window.location.href)); | ||||||
|       } |       } | ||||||
| @@ -329,14 +332,14 @@ haste.prototype.configureButton = function(options) { | |||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
|   // Show the label |   // Show the label | ||||||
|   options.$where.mouseenter(function(evt) { |   options.$where.mouseenter(function() { | ||||||
|     $('#box3 .label').text(options.label); |     $('#box3 .label').text(options.label); | ||||||
|     $('#box3 .shortcut').text(options.shortcutDescription || ''); |     $('#box3 .shortcut').text(options.shortcutDescription || ''); | ||||||
|     $('#box3').show(); |     $('#box3').show(); | ||||||
|     $(this).append($('#pointer').remove().show()); |     $(this).append($('#pointer').remove().show()); | ||||||
|   }); |   }); | ||||||
|   // Hide the label |   // Hide the label | ||||||
|   options.$where.mouseleave(function(evt) { |   options.$where.mouseleave(function() { | ||||||
|     $('#box3').hide(); |     $('#box3').hide(); | ||||||
|     $('#pointer').hide(); |     $('#pointer').hide(); | ||||||
|   }); |   }); | ||||||
| @@ -369,7 +372,7 @@ $(function() { | |||||||
|       // For browsers like Internet Explorer |       // For browsers like Internet Explorer | ||||||
|       if (document.selection) { |       if (document.selection) { | ||||||
|         this.focus(); |         this.focus(); | ||||||
|         sel = document.selection.createRange(); |         var sel = document.selection.createRange(); | ||||||
|         sel.text = myValue; |         sel.text = myValue; | ||||||
|         this.focus(); |         this.focus(); | ||||||
|       } |       } | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								static/application.min.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								static/application.min.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 14 KiB | 
							
								
								
									
										7
									
								
								static/highlight.min.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								static/highlight.min.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -1,13 +1,12 @@ | |||||||
| <html> | <html> | ||||||
|  |  | ||||||
| 	<head> | 	<head> | ||||||
|  | 		<title>Webionite hastebin</title> | ||||||
| 		<title>hastebin</title> | 		<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||||
|  | 		<meta charset="utf-8" /> | ||||||
| 		<link rel="stylesheet" type="text/css" href="solarized_dark.css"/> | 		<link rel="stylesheet" type="text/css" href="tomorrow.css"/> | ||||||
| 		<link rel="stylesheet" type="text/css" href="application.css"/> | 		<link rel="stylesheet" type="text/css" href="application.css"/> | ||||||
|  |  | ||||||
| 		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> | 		<script type="text/javascript" src="jquery.min.js"></script> | ||||||
| 		<script type="text/javascript" src="highlight.min.js"></script> | 		<script type="text/javascript" src="highlight.min.js"></script> | ||||||
| 		<script type="text/javascript" src="application.min.js"></script> | 		<script type="text/javascript" src="application.min.js"></script> | ||||||
|  |  | ||||||
| @@ -39,30 +38,27 @@ | |||||||
| 	</head> | 	</head> | ||||||
|  |  | ||||||
| 	<body> | 	<body> | ||||||
| 		<ul id="messages"></ul> |  | ||||||
|  |  | ||||||
| 		<div id="key"> | 		<div id="key"> | ||||||
| 		  <div id="pointer" style="display:none;"></div> |  | ||||||
| 			<div id="box1"> |  | ||||||
| 			<a href="/about.md" class="logo"></a> | 			<a href="/about.md" class="logo"></a> | ||||||
| 			</div> | 			<div> | ||||||
|  | 				<div id="pointer" style="display:none;"></div> | ||||||
| 				<div id="box2"> | 				<div id="box2"> | ||||||
| 				<div class="save function"></div> | 					<button class="save function button-picture">Save</button> | ||||||
| 				<div class="new function"></div> | 					<button class="new function button-picture">New</button> | ||||||
| 				<div class="duplicate function"></div> | 					<button class="duplicate function button-picture">Duplicate & Edit</button> | ||||||
| 				<div class="raw function"></div> | 					<button class="raw function button-picture">Just Text</button> | ||||||
| 				<div class="twitter function"></div> |  | ||||||
| 				</div> | 				</div> | ||||||
| 				<div id="box3" style="display:none;"> | 				<div id="box3" style="display:none;"> | ||||||
| 					<div class="label"></div> | 					<div class="label"></div> | ||||||
| 					<div class="shortcut"></div> | 					<div class="shortcut"></div> | ||||||
| 				</div> | 				</div> | ||||||
|  | 				<ul id="messages"></ul> | ||||||
|  | 			</div> | ||||||
| 		</div> | 		</div> | ||||||
|  |  | ||||||
| 		<div id="linenos"></div> | 		<div id="linenos"></div> | ||||||
| 		<pre id="box" style="display:none;" tabindex="0"><code></code></pre> | 		<pre id="box" style="display:none;" class="hljs" tabindex="0"><code></code></pre> | ||||||
| 		<textarea spellcheck="false" style="display:none;"></textarea> | 		<textarea spellcheck="false" style="display:none;"></textarea> | ||||||
|  |  | ||||||
| 	</body> | 	</body> | ||||||
|  |  | ||||||
| </html> | </html> | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								static/jquery.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								static/jquery.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								static/logo.png
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								static/logo.png
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 5.0 KiB | 
| @@ -1,98 +0,0 @@ | |||||||
| /* |  | ||||||
|  |  | ||||||
| Orginal Style from ethanschoonover.com/solarized (c) Jeremy Hull <sourdrums@gmail.com> |  | ||||||
|  |  | ||||||
| */ |  | ||||||
|  |  | ||||||
| pre code { |  | ||||||
|   display: block; padding: 0.5em; |  | ||||||
|   background: #002b36; color: #92a0a0; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .comment, |  | ||||||
| pre .template_comment, |  | ||||||
| pre .diff .header, |  | ||||||
| pre .doctype, |  | ||||||
| pre .lisp .string, |  | ||||||
| pre .javadoc { |  | ||||||
|   color: #586e75; |  | ||||||
|   font-style: italic; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .keyword, |  | ||||||
| pre .css .rule .keyword, |  | ||||||
| pre .winutils, |  | ||||||
| pre .javascript .title, |  | ||||||
| pre .method, |  | ||||||
| pre .addition, |  | ||||||
| pre .css .tag, |  | ||||||
| pre .lisp .title { |  | ||||||
|   color: #859900; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .number, |  | ||||||
| pre .command, |  | ||||||
| pre .string, |  | ||||||
| pre .tag .value, |  | ||||||
| pre .phpdoc, |  | ||||||
| pre .tex .formula, |  | ||||||
| pre .regexp, |  | ||||||
| pre .hexcolor { |  | ||||||
|   color: #2aa198; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .title, |  | ||||||
| pre .localvars, |  | ||||||
| pre .function .title, |  | ||||||
| pre .chunk, |  | ||||||
| pre .decorator, |  | ||||||
| pre .builtin, |  | ||||||
| pre .built_in, |  | ||||||
| pre .lisp .title, |  | ||||||
| pre .identifier, |  | ||||||
| pre .title .keymethods, |  | ||||||
| pre .id, |  | ||||||
| pre .header { |  | ||||||
|   color: #268bd2; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .tag .title, |  | ||||||
| pre .rules .property, |  | ||||||
| pre .django .tag .keyword { |  | ||||||
|   font-weight: bold; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .attribute, |  | ||||||
| pre .variable, |  | ||||||
| pre .instancevar, |  | ||||||
| pre .lisp .body, |  | ||||||
| pre .smalltalk .number, |  | ||||||
| pre .constant, |  | ||||||
| pre .class .title, |  | ||||||
| pre .parent, |  | ||||||
| pre .haskell .label { |  | ||||||
|   color: #b58900; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .preprocessor, |  | ||||||
| pre .pi, |  | ||||||
| pre .shebang, |  | ||||||
| pre .symbol, |  | ||||||
| pre .diff .change, |  | ||||||
| pre .special, |  | ||||||
| pre .keymethods, |  | ||||||
| pre .attr_selector, |  | ||||||
| pre .important, |  | ||||||
| pre .subst, |  | ||||||
| pre .cdata { |  | ||||||
|   color: #cb4b16; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .deletion { |  | ||||||
|   color: #dc322f; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| pre .tex .formula, |  | ||||||
| pre .code { |  | ||||||
|   background: #073642; |  | ||||||
| } |  | ||||||
							
								
								
									
										72
									
								
								static/tomorrow.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								static/tomorrow.css
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,72 @@ | |||||||
|  | /* http://jmblog.github.com/color-themes-for-google-code-highlightjs */ | ||||||
|  |  | ||||||
|  | /* Tomorrow Comment */ | ||||||
|  | .hljs-comment, | ||||||
|  | .hljs-quote { | ||||||
|  |   color: #8e908c; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /* Tomorrow Red */ | ||||||
|  | .hljs-variable, | ||||||
|  | .hljs-template-variable, | ||||||
|  | .hljs-tag, | ||||||
|  | .hljs-name, | ||||||
|  | .hljs-selector-id, | ||||||
|  | .hljs-selector-class, | ||||||
|  | .hljs-regexp, | ||||||
|  | .hljs-deletion { | ||||||
|  |   color: #c82829; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /* Tomorrow Orange */ | ||||||
|  | .hljs-number, | ||||||
|  | .hljs-built_in, | ||||||
|  | .hljs-builtin-name, | ||||||
|  | .hljs-literal, | ||||||
|  | .hljs-type, | ||||||
|  | .hljs-params, | ||||||
|  | .hljs-meta, | ||||||
|  | .hljs-link { | ||||||
|  |   color: #f5871f; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /* Tomorrow Yellow */ | ||||||
|  | .hljs-attribute { | ||||||
|  |   color: #eab700; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /* Tomorrow Green */ | ||||||
|  | .hljs-string, | ||||||
|  | .hljs-symbol, | ||||||
|  | .hljs-bullet, | ||||||
|  | .hljs-addition { | ||||||
|  |   color: #718c00; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /* Tomorrow Blue */ | ||||||
|  | .hljs-title, | ||||||
|  | .hljs-section { | ||||||
|  |   color: #4271ae; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /* Tomorrow Purple */ | ||||||
|  | .hljs-keyword, | ||||||
|  | .hljs-selector-tag { | ||||||
|  |   color: #8959a8; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | .hljs { | ||||||
|  |   display: block; | ||||||
|  |   overflow-x: auto; | ||||||
|  |   background: white; | ||||||
|  |   color: #4d4d4c; | ||||||
|  |   padding: 0.5em; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | .hljs-emphasis { | ||||||
|  |   font-style: italic; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | .hljs-strong { | ||||||
|  |   font-weight: bold; | ||||||
|  | } | ||||||
| @@ -1,3 +1,7 @@ | |||||||
|  | /* global describe, it */ | ||||||
|  | 
 | ||||||
|  | var assert = require('assert'); | ||||||
|  | 
 | ||||||
| var DocumentHandler = require('../lib/document_handler'); | var DocumentHandler = require('../lib/document_handler'); | ||||||
| var Generator = require('../lib/key_generators/random'); | var Generator = require('../lib/key_generators/random'); | ||||||
| 
 | 
 | ||||||
| @@ -8,13 +12,13 @@ describe('document_handler', function() { | |||||||
|     it('should choose a key of the proper length', function() { |     it('should choose a key of the proper length', function() { | ||||||
|       var gen = new Generator(); |       var gen = new Generator(); | ||||||
|       var dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen }); |       var dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen }); | ||||||
|       dh.acceptableKey().length.should.equal(6); |       assert.equal(6, dh.acceptableKey().length); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     it('should choose a default key length', function() { |     it('should choose a default key length', function() { | ||||||
|       var gen = new Generator(); |       var gen = new Generator(); | ||||||
|       var dh = new DocumentHandler({ keyGenerator: gen }); |       var dh = new DocumentHandler({ keyGenerator: gen }); | ||||||
|       dh.keyLength.should.equal(DocumentHandler.defaultKeyLength); |       assert.equal(dh.keyLength, DocumentHandler.defaultKeyLength); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|   }); |   }); | ||||||
							
								
								
									
										33
									
								
								test/key_generators/dictionary_spec.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								test/key_generators/dictionary_spec.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | |||||||
|  | /* global describe, it */ | ||||||
|  |  | ||||||
|  | const assert = require('assert'); | ||||||
|  |  | ||||||
|  | const fs = require('fs'); | ||||||
|  |  | ||||||
|  | const Generator = require('../../lib/key_generators/dictionary'); | ||||||
|  |  | ||||||
|  | describe('RandomKeyGenerator', function() { | ||||||
|  |   describe('randomKey', function() { | ||||||
|  |     it('should throw an error if given no options', () => { | ||||||
|  |       assert.throws(() => { | ||||||
|  |         new Generator(); | ||||||
|  |       }, Error); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     it('should throw an error if given no path', () => { | ||||||
|  |       assert.throws(() => { | ||||||
|  |         new Generator({}); | ||||||
|  |       }, Error); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     it('should return a key of the proper number of words from the given dictionary', () => { | ||||||
|  |       const path = '/tmp/haste-server-test-dictionary'; | ||||||
|  |       const words = ['cat']; | ||||||
|  |       fs.writeFileSync(path, words.join('\n')); | ||||||
|  |  | ||||||
|  |       const gen = new Generator({path}, () => { | ||||||
|  |         assert.equal('catcatcat', gen.createKey(3)); | ||||||
|  |       }); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
							
								
								
									
										27
									
								
								test/key_generators/phonetic_spec.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								test/key_generators/phonetic_spec.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | /* global describe, it */ | ||||||
|  |  | ||||||
|  | const assert = require('assert'); | ||||||
|  |  | ||||||
|  | const Generator = require('../../lib/key_generators/phonetic'); | ||||||
|  |  | ||||||
|  | const vowels = 'aeiou'; | ||||||
|  | const consonants = 'bcdfghjklmnpqrstvwxyz'; | ||||||
|  |  | ||||||
|  | describe('RandomKeyGenerator', () => { | ||||||
|  |   describe('randomKey', () => { | ||||||
|  |     it('should return a key of the proper length', () => { | ||||||
|  |       const gen = new Generator(); | ||||||
|  |       assert.equal(6, gen.createKey(6).length); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     it('should alternate consonants and vowels', () => { | ||||||
|  |       const gen = new Generator(); | ||||||
|  |  | ||||||
|  |       const key = gen.createKey(3); | ||||||
|  |  | ||||||
|  |       assert.ok(consonants.includes(key[0])); | ||||||
|  |       assert.ok(consonants.includes(key[2])); | ||||||
|  |       assert.ok(vowels.includes(key[1])); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
							
								
								
									
										19
									
								
								test/key_generators/random_spec.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								test/key_generators/random_spec.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | /* global describe, it */ | ||||||
|  |  | ||||||
|  | const assert = require('assert'); | ||||||
|  |  | ||||||
|  | const Generator = require('../../lib/key_generators/random'); | ||||||
|  |  | ||||||
|  | describe('RandomKeyGenerator', () => { | ||||||
|  |   describe('randomKey', () => { | ||||||
|  |     it('should return a key of the proper length', () => { | ||||||
|  |       const gen = new Generator(); | ||||||
|  |       assert.equal(6, gen.createKey(6).length); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     it('should use a key from the given keyset if given', () => { | ||||||
|  |       const gen = new Generator({keyspace: 'A'}); | ||||||
|  |       assert.equal('AAAAAA', gen.createKey(6)); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
| @@ -1,8 +1,12 @@ | |||||||
| var RedisDocumentStore = require('../lib/document_stores/redis'); | /* global it, describe, afterEach */ | ||||||
|  | 
 | ||||||
|  | var assert = require('assert'); | ||||||
| 
 | 
 | ||||||
| var winston = require('winston'); | var winston = require('winston'); | ||||||
| winston.remove(winston.transports.Console); | winston.remove(winston.transports.Console); | ||||||
| 
 | 
 | ||||||
|  | var RedisDocumentStore = require('../lib/document_stores/redis'); | ||||||
|  | 
 | ||||||
| describe('redis_document_store', function() { | describe('redis_document_store', function() { | ||||||
| 
 | 
 | ||||||
|   /* reconnect to redis on each test */ |   /* reconnect to redis on each test */ | ||||||
| @@ -19,7 +23,7 @@ describe('redis_document_store', function() { | |||||||
|       var store = new RedisDocumentStore({ expire: 10 }); |       var store = new RedisDocumentStore({ expire: 10 }); | ||||||
|       store.set('hello1', 'world', function() { |       store.set('hello1', 'world', function() { | ||||||
|         RedisDocumentStore.client.ttl('hello1', function(err, res) { |         RedisDocumentStore.client.ttl('hello1', function(err, res) { | ||||||
|           res.should.be.above(1); |           assert.ok(res > 1); | ||||||
|           done(); |           done(); | ||||||
|         }); |         }); | ||||||
|       }); |       }); | ||||||
| @@ -29,7 +33,7 @@ describe('redis_document_store', function() { | |||||||
|       var store = new RedisDocumentStore({ expire: 10 }); |       var store = new RedisDocumentStore({ expire: 10 }); | ||||||
|       store.set('hello2', 'world', function() { |       store.set('hello2', 'world', function() { | ||||||
|         RedisDocumentStore.client.ttl('hello2', function(err, res) { |         RedisDocumentStore.client.ttl('hello2', function(err, res) { | ||||||
|           res.should.equal(-1); |           assert.equal(-1, res); | ||||||
|           done(); |           done(); | ||||||
|         }); |         }); | ||||||
|       }, true); |       }, true); | ||||||
| @@ -37,9 +41,9 @@ describe('redis_document_store', function() { | |||||||
| 
 | 
 | ||||||
|     it('should not set an expiration when expiration is off', function(done) { |     it('should not set an expiration when expiration is off', function(done) { | ||||||
|       var store = new RedisDocumentStore({ expire: false }); |       var store = new RedisDocumentStore({ expire: false }); | ||||||
|       store.set('hello3', 'world', function(worked) { |       store.set('hello3', 'world', function() { | ||||||
|         RedisDocumentStore.client.ttl('hello3', function(err, res) { |         RedisDocumentStore.client.ttl('hello3', function(err, res) { | ||||||
|           res.should.equal(-1); |           assert.equal(-1, res); | ||||||
|           done(); |           done(); | ||||||
|         }); |         }); | ||||||
|       }); |       }); | ||||||
		Reference in New Issue
	
	Block a user