Compare commits
	
		
			1 Commits
		
	
	
		
			20fb7f9bc2
			...
			codemirror
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 2781201d3a | 
| @@ -1,6 +0,0 @@ | ||||
| npm-debug.log | ||||
| node_modules | ||||
| *.swp | ||||
| *.swo | ||||
| data | ||||
| *.DS_Store | ||||
| @@ -1,2 +0,0 @@ | ||||
| **/*.min.js | ||||
| config.js | ||||
| @@ -1,25 +0,0 @@ | ||||
| { | ||||
|     "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,4 +4,3 @@ node_modules | ||||
| *.swo | ||||
| data | ||||
| *.DS_Store | ||||
| config.json | ||||
|   | ||||
							
								
								
									
										63
									
								
								Dockerfile
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								Dockerfile
									
									
									
									
									
								
							| @@ -1,63 +0,0 @@ | ||||
| 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_USENAMER= \ | ||||
|     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 [ "curl" , "-f" "localhost:${PORT}", "||", "exit", "1"] | ||||
| CMD ["npm", "start"] | ||||
							
								
								
									
										257
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										257
									
								
								README.md
									
									
									
									
									
								
							| @@ -31,31 +31,21 @@ STDOUT.  Check the README there for more details and usages. | ||||
| 1.  Download the package, and expand it | ||||
| 2.  Explore the settings inside of config.js, but the defaults should be good | ||||
| 3.  `npm install` | ||||
| 4.  `npm start` (you may specify an optional `<config-path>` as well) | ||||
| 4.  `npm start` | ||||
|  | ||||
| ## Settings | ||||
|  | ||||
| * `host` - the host the server runs on (default localhost) | ||||
| * `port` - the port the server runs on (default 7777) | ||||
| * `keyLength` - the length of the keys to user (default 10) | ||||
| * `maxLength` - maximum length of a paste (default 400000) | ||||
| * `maxLength` - maximum length of a paste (default none) | ||||
| * `staticMaxAge` - max age for static assets (86400) | ||||
| * `recompressStaticAssets` - whether or not to compile static js assets (true) | ||||
| * `recompressStatisAssets` - whether or not to compile static js assets (true) | ||||
| * `documents` - static documents to serve (ex: http://hastebin.com/about.com) | ||||
|   in addition to static assets.  These will never expire. | ||||
| * `storage` - storage options (see below) | ||||
| * `logging` - logging preferences | ||||
| * `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 | ||||
|  | ||||
| @@ -65,7 +55,7 @@ Attempts to generate phonetic keys, similar to `pwgen` | ||||
|  | ||||
| ``` json | ||||
| { | ||||
|   "type": "phonetic" | ||||
| 	"type": "phonetic" | ||||
| } | ||||
| ``` | ||||
|  | ||||
| @@ -75,7 +65,7 @@ Generates a random key | ||||
|  | ||||
| ``` json | ||||
| { | ||||
|   "type": "random", | ||||
| 	"type": "random", | ||||
|   "keyspace": "abcdef" | ||||
| } | ||||
| ``` | ||||
| @@ -92,19 +82,16 @@ something like: | ||||
|  | ||||
| ``` json | ||||
| { | ||||
|   "path": "./data", | ||||
|   "type": "file" | ||||
| 	"path": "./data", | ||||
| 	"type": "file" | ||||
| } | ||||
| ``` | ||||
|  | ||||
| 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. | ||||
| Where `path` represents where you want the files stored | ||||
|  | ||||
| ### Redis | ||||
|  | ||||
| To use redis storage you must install the `redis` package in npm, and have | ||||
| `redis-server` running on the machine. | ||||
| To use redis storage you must install the redis package in npm | ||||
|  | ||||
| `npm install redis` | ||||
|  | ||||
| @@ -112,10 +99,10 @@ Once you've done that, your config section should look like: | ||||
|  | ||||
| ``` json | ||||
| { | ||||
|   "type": "redis", | ||||
|   "host": "localhost", | ||||
|   "port": 6379, | ||||
|   "db": 2 | ||||
| 	"type": "redis", | ||||
| 	"host": "localhost", | ||||
| 	"port": 6379, | ||||
| 	"db": 2 | ||||
| } | ||||
| ``` | ||||
|  | ||||
| @@ -125,70 +112,19 @@ or post. | ||||
|  | ||||
| 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' pachage in npm | ||||
|  | ||||
| `npm install mongodb` | ||||
|  | ||||
| Once you've done that, your config section should look like: | ||||
|  | ||||
| ``` json | ||||
| { | ||||
|   "type": "mongodb", | ||||
|   "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 | ||||
|  | ||||
| To use memcache storage you must install the `memcached` package via npm | ||||
| To use memcached storage you must install the `memcache` package via npm | ||||
|  | ||||
| `npm install memcached` | ||||
| `npm install memcache` | ||||
|  | ||||
| Once you've done that, your config section should look like: | ||||
|  | ||||
| ``` json | ||||
| { | ||||
|   "type": "memcached", | ||||
|   "host": "127.0.0.1", | ||||
|   "port": 11211 | ||||
| 	"type": "memcached", | ||||
| 	"host": "127.0.0.1", | ||||
| 	"port": 11211 | ||||
| } | ||||
| ``` | ||||
|  | ||||
| @@ -198,161 +134,6 @@ forward on GETs. | ||||
|  | ||||
| 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 | ||||
|  | ||||
| @@ -362,7 +143,7 @@ John Crepezzi <john.crepezzi@gmail.com> | ||||
|  | ||||
| (The MIT License) | ||||
|  | ||||
| Copyright © 2011-2012 John Crepezzi | ||||
| Copyright © 2011 John Crepezzi | ||||
|  | ||||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | ||||
| this software and associated documentation files (the ‘Software’), to deal in | ||||
|   | ||||
							
								
								
									
										41
									
								
								about.md
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								about.md
									
									
									
									
									
								
							| @@ -15,19 +15,44 @@ To make a new entry, click "New" (or type 'control + n') | ||||
|  | ||||
| ## From the Console | ||||
|  | ||||
| [bin-client](git.webionite.com/ceda_ei/bin-client) | ||||
| Most of the time I want to show you some text, its coming from my current | ||||
| console session.  We should make it really easy to take code from the console | ||||
| and send it to people. | ||||
|  | ||||
| Add the following to your bashrc/zshrc | ||||
| ``` | ||||
| export MKR_BIN='https://bin.webionite.com/' | ||||
| export HASTEBIN=1 | ||||
| ``` | ||||
| `cat something | haste` # http://hastebin.com/1238193 | ||||
|  | ||||
| You can even take this a step further, and cut out the last step of copying the | ||||
| URL with: | ||||
|  | ||||
| * 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 | ||||
|  | ||||
| Haste can easily be installed behind your network, and it's all open source! | ||||
| Haste can easily be installed behind your network, and its all open source! | ||||
|  | ||||
| * [haste-server](https://git.webionite.com/Webionite/haste-server) | ||||
| * [haste-client](https://github.com/seejohnrun/haste-client) | ||||
| * [haste-server](https://github.com/seejohnrun/haste-server) | ||||
|  | ||||
| ## Author | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| { | ||||
| 
 | ||||
|   "host": "0.0.0.0", | ||||
|   "host": "localhost", | ||||
|   "port": 7777, | ||||
| 
 | ||||
|   "keyLength": 10, | ||||
| @@ -23,17 +23,12 @@ | ||||
|     "type": "phonetic" | ||||
|   }, | ||||
| 
 | ||||
|   "rateLimits": { | ||||
|     "categories": { | ||||
|       "normal": { | ||||
|         "totalRequests": 500, | ||||
|         "every": 60000 | ||||
|       } | ||||
|     } | ||||
|   }, | ||||
| 
 | ||||
|   "storage": { | ||||
|     "type": "file" | ||||
|     "type": "redis", | ||||
|     "host": "localhost", | ||||
|     "port": 6379, | ||||
|     "db": 2, | ||||
|     "expire": 2592000 | ||||
|   }, | ||||
| 
 | ||||
|   "documents": { | ||||
| @@ -1,19 +0,0 @@ | ||||
| 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: | ||||
| @@ -1,108 +0,0 @@ | ||||
| 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)); | ||||
| @@ -1,9 +0,0 @@ | ||||
| #!/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,5 +1,4 @@ | ||||
| var winston = require('winston'); | ||||
| var Busboy = require('busboy'); | ||||
|  | ||||
| // For handling serving stored documents | ||||
|  | ||||
| @@ -16,68 +15,47 @@ var DocumentHandler = function(options) { | ||||
| DocumentHandler.defaultKeyLength = 10; | ||||
|  | ||||
| // Handle retrieving a document | ||||
| DocumentHandler.prototype.handleGet = function(request, response, config) { | ||||
|   const key = request.params.id.split('.')[0]; | ||||
|   const skipExpire = !!config.documents[key]; | ||||
|  | ||||
| DocumentHandler.prototype.handleGet = function(key, response, skipExpire) { | ||||
|   this.store.get(key, function(ret) { | ||||
|     if (ret) { | ||||
|       winston.verbose('retrieved document', { key: key }); | ||||
|       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 { | ||||
|       winston.warn('document not found', { key: key }); | ||||
|       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); | ||||
| }; | ||||
|  | ||||
| // Handle retrieving the raw version of a document | ||||
| DocumentHandler.prototype.handleRawGet = function(request, response, config) { | ||||
|   const key = request.params.id.split('.')[0]; | ||||
|   const skipExpire = !!config.documents[key]; | ||||
|  | ||||
| DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) { | ||||
|   this.store.get(key, function(ret) { | ||||
|     if (ret) { | ||||
|       winston.verbose('retrieved raw document', { key: key }); | ||||
|       response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' }); | ||||
|       if (request.method === 'HEAD') { | ||||
|         response.end(); | ||||
|       } else { | ||||
|         response.end(ret); | ||||
|       } | ||||
|       response.writeHead(200, { 'content-type': 'text/plain' }); | ||||
|       response.end(ret); | ||||
|     } | ||||
|     else { | ||||
|       winston.warn('raw document not found', { key: key }); | ||||
|       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); | ||||
| }; | ||||
|  | ||||
| // Handle adding a new Document | ||||
| DocumentHandler.prototype.handlePost = function (request, response) { | ||||
| DocumentHandler.prototype.handlePost = function(request, response) { | ||||
|   var _this = this; | ||||
|   var buffer = ''; | ||||
|   var cancelled = false; | ||||
|  | ||||
|   // What to do when done | ||||
|   var onSuccess = function () { | ||||
|     // Check length | ||||
|   request.on('data', function(data) { | ||||
|     if (!buffer) { | ||||
|       response.writeHead(200, { 'content-type': 'application/json' }); | ||||
|     } | ||||
|     buffer += JSON.parse(data.toString()).data; | ||||
|     if (_this.maxLength && buffer.length > _this.maxLength) { | ||||
|       cancelled = true; | ||||
|       winston.warn('document >maxLength', { maxLength: _this.maxLength }); | ||||
| @@ -85,14 +63,14 @@ DocumentHandler.prototype.handlePost = function (request, response) { | ||||
|       response.end( | ||||
|         JSON.stringify({ message: 'Document exceeds maximum length.' }) | ||||
|       ); | ||||
|       return; | ||||
|     } | ||||
|     // And then save if we should | ||||
|     _this.chooseKey(function (key) { | ||||
|       _this.store.set(key, buffer, function (res) { | ||||
|   }); | ||||
|   request.on('end', function(end) { | ||||
|     if (cancelled) return; | ||||
|     _this.chooseKey(function(key) { | ||||
|       _this.store.set(key, buffer, function(res) { | ||||
|         if (res) { | ||||
|           winston.verbose('added document', { key: key }); | ||||
|           response.writeHead(200, { 'content-type': 'application/json' }); | ||||
|           response.end(JSON.stringify({ key: key })); | ||||
|         } | ||||
|         else { | ||||
| @@ -102,37 +80,12 @@ 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) { | ||||
|       winston.error('connection error: ' + error.message); | ||||
|       response.writeHead(500, { 'content-type': 'application/json' }); | ||||
|       response.end(JSON.stringify({ message: 'Connection error.' })); | ||||
|       cancelled = true; | ||||
|     }); | ||||
|   } | ||||
|   }); | ||||
|   request.on('error', function(error) { | ||||
|     winston.error('connection error: ' + error.message); | ||||
|     response.writeHead(500, { 'content-type': 'application/json' }); | ||||
|     response.end(JSON.stringify({ message: 'Connection error.' })); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| // Keep choosing keys until one isn't taken | ||||
| @@ -145,7 +98,7 @@ DocumentHandler.prototype.chooseKey = function(callback) { | ||||
|     } else { | ||||
|       callback(key); | ||||
|     } | ||||
|   }, true); // Don't bump expirations when key searching | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| DocumentHandler.prototype.acceptableKey = function() { | ||||
|   | ||||
| @@ -1,56 +0,0 @@ | ||||
| /*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; | ||||
| @@ -1,89 +0,0 @@ | ||||
| /*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,54 +1,45 @@ | ||||
| const memcached = require('memcached'); | ||||
| const winston = require('winston'); | ||||
| var memcached = require('memcache'); | ||||
| var winston = require('winston'); | ||||
|  | ||||
| class MemcachedDocumentStore { | ||||
|  | ||||
|   // Create a new store with options | ||||
|   constructor(options) { | ||||
|     this.expire = options.expire; | ||||
|  | ||||
|     const host = options.host || '127.0.0.1'; | ||||
|     const port = options.port || 11211; | ||||
|     const url = `${host}:${port}`; | ||||
|     this.connect(url); | ||||
| // Create a new store with options | ||||
| var MemcachedDocumentStore = function(options) { | ||||
|   this.expire = options.expire; | ||||
|   if (!MemcachedDocumentStore.client) { | ||||
|     MemcachedDocumentStore.connect(options); | ||||
|   } | ||||
| }; | ||||
|  | ||||
|   // Create a connection | ||||
|   connect(url) { | ||||
|     this.client = new memcached(url); | ||||
| // Create a connection | ||||
| MemcachedDocumentStore.connect = function(options) { | ||||
|   var host = options.host || '127.0.0.1'; | ||||
|   var port = options.port || 11211; | ||||
|   this.client = new memcached.Client(port, host); | ||||
|   this.client.connect(); | ||||
|   this.client.on('connect', function() { | ||||
|     winston.info('connected to memcached on ' + host + ':' + port); | ||||
|   }); | ||||
|   this.client.on('error', function(e) { | ||||
|     winston.info('error connecting to memcached', { error: e }); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
|     winston.info(`connecting to memcached on ${url}`); | ||||
| // Save file in a key | ||||
| MemcachedDocumentStore.prototype.set = | ||||
| function(key, data, callback, skipExpire) { | ||||
|   MemcachedDocumentStore.client.set(key, data, function(err, reply) { | ||||
|     err ? callback(false) : callback(true); | ||||
|   }, skipExpire ? 0 : this.expire); | ||||
| }; | ||||
|  | ||||
|     this.client.on('failure', function(error) { | ||||
|       winston.info('error connecting to memcached', {error}); | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   // Save file in a key | ||||
|   set(key, data, callback, skipExpire) { | ||||
|     this.client.set(key, data, skipExpire ? 0 : this.expire || 0, (error) => { | ||||
|       callback(!error); | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   // Get a file from a key | ||||
|   get(key, callback, skipExpire) { | ||||
|     this.client.get(key, (error, data) => { | ||||
|       const value = error ? false : data; | ||||
|  | ||||
|       callback(value); | ||||
|  | ||||
|       // 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); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
| } | ||||
| // Get a file from a key | ||||
| MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) { | ||||
|   var _this = this; | ||||
|   MemcachedDocumentStore.client.get(key, function(err, reply) { | ||||
|     callback(err ? false : reply); | ||||
|     if (_this.expire && !skipExpire) { | ||||
|       winston.warn('store does not currently push forward expirations on GET'); | ||||
|     } | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| module.exports = MemcachedDocumentStore; | ||||
|   | ||||
| @@ -1,88 +0,0 @@ | ||||
|  | ||||
|  | ||||
| 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; | ||||
| @@ -1,80 +0,0 @@ | ||||
| /*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,13 +8,9 @@ var winston = require('winston'); | ||||
| // options[db] - The db to use (default 0) | ||||
| // options[expire] - The time to live for each key set (default never) | ||||
|  | ||||
| var RedisDocumentStore = function(options, client) { | ||||
| var RedisDocumentStore = function(options) { | ||||
|   this.expire = options.expire; | ||||
|   if (client) { | ||||
|     winston.info('using predefined redis client'); | ||||
|     RedisDocumentStore.client = client; | ||||
|   } else if (!RedisDocumentStore.client) { | ||||
|     winston.info('configuring redis'); | ||||
|   if (!RedisDocumentStore.client) { | ||||
|     RedisDocumentStore.connect(options); | ||||
|   } | ||||
| }; | ||||
| @@ -25,20 +21,11 @@ RedisDocumentStore.connect = function(options) { | ||||
|   var port = options.port || 6379; | ||||
|   var index = options.db || 0; | ||||
|   RedisDocumentStore.client = redis.createClient(port, host); | ||||
|   // 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) { | ||||
|   RedisDocumentStore.client.select(index, function(err, reply) { | ||||
|     if (err) { | ||||
|       winston.error( | ||||
|         'error connecting to redis index ' + index, | ||||
|         { error: err } | ||||
|         { error: err.message } | ||||
|       ); | ||||
|       process.exit(1); | ||||
|     } | ||||
| @@ -51,7 +38,7 @@ RedisDocumentStore.connect = function(options) { | ||||
| // Save file in a key | ||||
| RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) { | ||||
|   var _this = this; | ||||
|   RedisDocumentStore.client.set(key, data, function(err) { | ||||
|   RedisDocumentStore.client.set(key, data, function(err, reply) { | ||||
|     if (err) { | ||||
|       callback(false); | ||||
|     } | ||||
| @@ -67,7 +54,7 @@ RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) { | ||||
| // Expire a key in expire time if set | ||||
| RedisDocumentStore.prototype.setExpiration = function(key) { | ||||
|   if (this.expire) { | ||||
|     RedisDocumentStore.client.expire(key, this.expire, function(err) { | ||||
|     RedisDocumentStore.client.expire(key, this.expire, function(err, reply) { | ||||
|       if (err) { | ||||
|         winston.error('failed to set expiry on key: ' + key); | ||||
|       } | ||||
|   | ||||
| @@ -1,46 +0,0 @@ | ||||
| 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; | ||||
| @@ -1,32 +0,0 @@ | ||||
| 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,27 +1,32 @@ | ||||
| // Draws inspiration from pwgen and http://tools.arantius.com/password | ||||
|  | ||||
| const randOf = (collection) => { | ||||
|   return () => { | ||||
|     return collection[Math.floor(Math.random() * collection.length)]; | ||||
|   }; | ||||
| var PhoneticKeyGenerator = function(options) { | ||||
|   // No options | ||||
| }; | ||||
|  | ||||
| // Helper methods to get an random vowel or consonant | ||||
| const randVowel = randOf('aeiou'); | ||||
| const randConsonant = randOf('bcdfghjklmnpqrstvwxyz'); | ||||
|  | ||||
| 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; | ||||
| // Generate a phonetic key | ||||
| PhoneticKeyGenerator.prototype.createKey = function(keyLength) { | ||||
|   var text = ''; | ||||
|   for (var i = 0; i < keyLength; i++) { | ||||
|     text += (i % 2 == 0) ? this.randConsonant() : this.randVowel(); | ||||
|   } | ||||
|  | ||||
|   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,20 +1,19 @@ | ||||
| module.exports = class RandomKeyGenerator { | ||||
|  | ||||
|   // Initialize a new generator with the given keySpace | ||||
|   constructor(options = {}) { | ||||
|     this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||||
| var RandomKeyGenerator = function(options) { | ||||
|   if (!options) { | ||||
|     options = {}; | ||||
|   } | ||||
|  | ||||
|   // Generate a key of the given length | ||||
|   createKey(keyLength) { | ||||
|     var text = ''; | ||||
|  | ||||
|     for (var i = 0; i < keyLength; i++) { | ||||
|       const index = Math.floor(Math.random() * this.keyspace.length); | ||||
|       text += this.keyspace.charAt(index); | ||||
|     } | ||||
|  | ||||
|     return text; | ||||
|   } | ||||
|  | ||||
|   this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||||
| }; | ||||
|  | ||||
| // Generate a random key | ||||
| RandomKeyGenerator.prototype.createKey = function(keyLength) { | ||||
|   var text = ''; | ||||
|   var index; | ||||
|   for (var i = 0; i < keyLength; i++) { | ||||
|     index = Math.floor(Math.random() * this.keyspace.length); | ||||
|     text += this.keyspace.charAt(index); | ||||
|   } | ||||
|   return text; | ||||
| }; | ||||
|  | ||||
| module.exports = RandomKeyGenerator; | ||||
|   | ||||
							
								
								
									
										1652
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1652
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										93
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										93
									
								
								package.json
									
									
									
									
									
								
							| @@ -1,47 +1,52 @@ | ||||
| { | ||||
|   "name": "haste", | ||||
|   "version": "0.1.0", | ||||
|   "private": true, | ||||
|   "description": "Private Pastebin Server", | ||||
|   "keywords": [ | ||||
|     "paste", | ||||
|     "pastebin" | ||||
|   ], | ||||
|   "author": { | ||||
|     "name": "John Crepezzi", | ||||
|     "email": "john.crepezzi@gmail.com", | ||||
|     "url": "http://seejohncode.com/" | ||||
|  | ||||
| 	"name": "haste", | ||||
| 	"version": "0.0.1", | ||||
|  | ||||
| 	"private": true, | ||||
|  | ||||
| 	"description": "Private Paste", | ||||
|  | ||||
| 	"keywords": [ "paste", "pastebin" ], | ||||
|  | ||||
| 	"author": { | ||||
| 		"name": "John Crepezzi", | ||||
| 		"email": "john.crepezzi@gmail.com", | ||||
| 		"url": "http://seejohncode.com/" | ||||
| 	}, | ||||
|  | ||||
| 	"main": "haste", | ||||
|  | ||||
| 	"dependencies": { | ||||
| 		"winston": "*", | ||||
|     "connect": "< 2", | ||||
|     "uglify-js": "*" | ||||
| 	}, | ||||
|  | ||||
| 	"devDependencies": { | ||||
|     "mocha": "*", | ||||
|     "should": "*" | ||||
|   }, | ||||
|   "main": "haste", | ||||
|   "dependencies": { | ||||
|     "busboy": "0.2.4", | ||||
|     "connect": "^3.7.0", | ||||
|     "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": { | ||||
|     "mocha": "^8.1.3" | ||||
|   }, | ||||
|   "bundledDependencies": [], | ||||
|   "bin": { | ||||
|     "haste-server": "./server.js" | ||||
|   }, | ||||
|   "files": [ | ||||
|     "server.js", | ||||
|     "lib", | ||||
|     "static" | ||||
|   ], | ||||
|   "directories": { | ||||
|     "lib": "./lib" | ||||
|   }, | ||||
|   "scripts": { | ||||
|     "start": "node server.js", | ||||
|     "test": "mocha --recursive" | ||||
|   } | ||||
|  | ||||
| 	"bundledDependencies": [], | ||||
|  | ||||
| 	"engines": { | ||||
| 		"node": "*" | ||||
| 	}, | ||||
|  | ||||
| 	"bin": { | ||||
| 		"haste-server": "./server.js" | ||||
| 	}, | ||||
|  | ||||
| 	"files": [ "server.js", "lib", "static" ], | ||||
|  | ||||
| 	"directories": { | ||||
| 		"lib": "./lib" | ||||
| 	}, | ||||
|  | ||||
| 	"scripts": { | ||||
| 		"start": "node server.js", | ||||
|     "test": "mocha -r should spec/*" | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
							
								
								
									
										177
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										177
									
								
								server.js
									
									
									
									
									
								
							| @@ -1,29 +1,22 @@ | ||||
| // vim: set ts=2 sw=2 sts=2 et: | ||||
| var http = require('http'); | ||||
| var url = require('url'); | ||||
| var fs = require('fs'); | ||||
|  | ||||
| var uglify = require('uglify-js'); | ||||
| var winston = require('winston'); | ||||
| 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'); | ||||
|  | ||||
| // Load the configuration and set some defaults | ||||
| var config = require('./config.json'); | ||||
| config.port = process.env.PORT || config.port || 7777; | ||||
| config.host = process.env.HOST || config.host || 'localhost'; | ||||
| var config = JSON.parse(fs.readFileSync('config.js', 'utf8')); | ||||
| config.port = config.port || 7777; | ||||
| config.host = config.host || 'localhost'; | ||||
|  | ||||
| // Set up the logger | ||||
| if (config.logging) { | ||||
|   try { | ||||
|     winston.remove(winston.transports.Console); | ||||
|   } catch(e) { | ||||
|     /* was not present */ | ||||
|   } | ||||
|  | ||||
|   } catch(er) { } | ||||
|   var detail, type; | ||||
|   for (var i = 0; i < config.logging.length; i++) { | ||||
|     detail = config.logging[i]; | ||||
| @@ -41,48 +34,47 @@ if (!config.storage) { | ||||
| if (!config.storage.type) { | ||||
|   config.storage.type = 'file'; | ||||
| } | ||||
|  | ||||
| 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); | ||||
| } | ||||
| var Store = require('./lib/document_stores/' + config.storage.type); | ||||
| var preferredStore = new Store(config.storage); | ||||
|  | ||||
| // Compress the static javascript assets | ||||
| if (config.recompressStaticAssets) { | ||||
|   var jsp = require("uglify-js").parser; | ||||
|   var pro = require("uglify-js").uglify; | ||||
|   var list = fs.readdirSync('./static'); | ||||
|   for (var j = 0; j < list.length; j++) { | ||||
|     var item = list[j]; | ||||
|     if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) { | ||||
|       var dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3); | ||||
|       var orig_code = fs.readFileSync('./static/' + item, 'utf8'); | ||||
|  | ||||
|       fs.writeFileSync('./static/' + dest, uglify.minify(orig_code).code, 'utf8'); | ||||
|   for (var i = 0; i < list.length; i++) { | ||||
|     var item = list[i]; | ||||
|     var orig_code, ast; | ||||
|     if ((item.indexOf('.js') === item.length - 3) && | ||||
|         (item.indexOf('.min.js') === -1)) { | ||||
|       dest = item.substring(0, item.length - 3) + '.min' + | ||||
|         item.substring(item.length - 3); | ||||
|       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); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| // Send the static documents into the preferred store, skipping expirations | ||||
| var path, data; | ||||
| for (var name in config.documents) { | ||||
|   path = config.documents[name]; | ||||
|   data = fs.readFileSync(path, 'utf8'); | ||||
|   winston.info('loading static document', { name: name, path: path }); | ||||
|   if (data) { | ||||
|     preferredStore.set(name, data, function(cb) { | ||||
|       winston.debug('loaded static document', { success: cb }); | ||||
|     }, true); | ||||
|   } | ||||
|   else { | ||||
|     winston.warn('failed to load static document', { name: name, path: path }); | ||||
|   } | ||||
|   var path = config.documents[name]; | ||||
|   fs.readFile(path, 'utf8', function(err, data) { | ||||
|     if (data && !err) { | ||||
|       preferredStore.set(name, data, function(cb) { | ||||
|         winston.info('loaded static document', { name: name, path: path }); | ||||
|       }, true); | ||||
|     } | ||||
|     else { | ||||
|       winston.warn( | ||||
|         'failed to load static document', | ||||
|         { name: name, path: path } | ||||
|       ); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  | ||||
| // Pick up a key generator | ||||
| @@ -99,69 +91,42 @@ var documentHandler = new DocumentHandler({ | ||||
|   keyGenerator: keyGenerator | ||||
| }); | ||||
|  | ||||
| var app = connect(); | ||||
|  | ||||
| // Rate limit all requests | ||||
| 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 | ||||
|   router.get('/raw/:id', raw); | ||||
|   router.get('/:id/raw', raw); | ||||
|   router.head('/raw/:id', raw); | ||||
|   router.head('/:id/raw', raw); | ||||
|   // add documents | ||||
|  | ||||
|   router.post('/documents', function(request, response) { | ||||
|     return documentHandler.handlePost(request, response); | ||||
|   }); | ||||
|  | ||||
|   // get documents | ||||
|   router.get('/documents/:id', function(request, response) { | ||||
|     return documentHandler.handleGet(request, response, config); | ||||
|   }); | ||||
|  | ||||
|   router.head('/documents/:id', function(request, response) { | ||||
|     return documentHandler.handleGet(request, response, config); | ||||
|   }); | ||||
| })); | ||||
|  | ||||
| // 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, | ||||
| // so route it back to / | ||||
| app.use(route(function(router) { | ||||
|   router.get('/:id', function(request, response, next) { | ||||
|     if (request.headers.accept && request.headers.accept.includes('html')) { | ||||
|       request.sturl = '/'; | ||||
| // Set the server up with a static cache | ||||
| connect.createServer( | ||||
|   // First look for api calls | ||||
|   connect.router(function(app) { | ||||
|     // get raw documents - support getting with extension | ||||
|     app.get('/raw/:id', function(request, response, next) { | ||||
|       var skipExpire = !!config.documents[request.params.id]; | ||||
|       var key = request.params.id.split('.')[0]; | ||||
|       return documentHandler.handleRawGet(key, response, skipExpire); | ||||
|     }); | ||||
|     // add documents | ||||
|     app.post('/documents', function(request, response, next) { | ||||
|       return documentHandler.handlePost(request, response); | ||||
|     }); | ||||
|     // get documents | ||||
|     app.get('/documents/:id', function(request, response, next) { | ||||
|       var skipExpire = !!config.documents[request.params.id]; | ||||
|       return documentHandler.handleGet( | ||||
|         request.params.id, | ||||
|         response, | ||||
|         skipExpire | ||||
|       ); | ||||
|     }); | ||||
|   }), | ||||
|   // Otherwise, static | ||||
|   connect.staticCache(), | ||||
|   connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }), | ||||
|   // Then we can loop back - and everything else should be a token, | ||||
|   // so route it back to /index.html | ||||
|   connect.router(function(app) { | ||||
|     app.get('/:id', function(request, response, next) { | ||||
|       request.url = request.originalUrl = '/index.html'; | ||||
|       next(); | ||||
|     } else { | ||||
|       return raw(request, response); | ||||
|     } | ||||
|   }); | ||||
| })); | ||||
|  | ||||
| // 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); | ||||
|     }); | ||||
|   }), | ||||
|   connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }) | ||||
| ).listen(config.port, config.host); | ||||
|  | ||||
| winston.info('listening on ' + config.host + ':' + config.port); | ||||
|   | ||||
| @@ -1,7 +1,3 @@ | ||||
| /* global describe, it */ | ||||
| 
 | ||||
| var assert = require('assert'); | ||||
| 
 | ||||
| var DocumentHandler = require('../lib/document_handler'); | ||||
| var Generator = require('../lib/key_generators/random'); | ||||
| 
 | ||||
| @@ -12,13 +8,13 @@ describe('document_handler', function() { | ||||
|     it('should choose a key of the proper length', function() { | ||||
|       var gen = new Generator(); | ||||
|       var dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen }); | ||||
|       assert.equal(6, dh.acceptableKey().length); | ||||
|       dh.acceptableKey().length.should.equal(6); | ||||
|     }); | ||||
| 
 | ||||
|     it('should choose a default key length', function() { | ||||
|       var gen = new Generator(); | ||||
|       var dh = new DocumentHandler({ keyGenerator: gen }); | ||||
|       assert.equal(dh.keyLength, DocumentHandler.defaultKeyLength); | ||||
|       dh.keyLength.should.equal(DocumentHandler.defaultKeyLength); | ||||
|     }); | ||||
| 
 | ||||
|   }); | ||||
| @@ -1,12 +1,8 @@ | ||||
| /* global it, describe, afterEach */ | ||||
| 
 | ||||
| var assert = require('assert'); | ||||
| var RedisDocumentStore = require('../lib/document_stores/redis'); | ||||
| 
 | ||||
| var winston = require('winston'); | ||||
| winston.remove(winston.transports.Console); | ||||
| 
 | ||||
| var RedisDocumentStore = require('../lib/document_stores/redis'); | ||||
| 
 | ||||
| describe('redis_document_store', function() { | ||||
| 
 | ||||
|   /* reconnect to redis on each test */ | ||||
| @@ -23,7 +19,7 @@ describe('redis_document_store', function() { | ||||
|       var store = new RedisDocumentStore({ expire: 10 }); | ||||
|       store.set('hello1', 'world', function() { | ||||
|         RedisDocumentStore.client.ttl('hello1', function(err, res) { | ||||
|           assert.ok(res > 1); | ||||
|           res.should.be.above(1); | ||||
|           done(); | ||||
|         }); | ||||
|       }); | ||||
| @@ -33,7 +29,7 @@ describe('redis_document_store', function() { | ||||
|       var store = new RedisDocumentStore({ expire: 10 }); | ||||
|       store.set('hello2', 'world', function() { | ||||
|         RedisDocumentStore.client.ttl('hello2', function(err, res) { | ||||
|           assert.equal(-1, res); | ||||
|           res.should.equal(-1); | ||||
|           done(); | ||||
|         }); | ||||
|       }, true); | ||||
| @@ -41,9 +37,9 @@ describe('redis_document_store', function() { | ||||
| 
 | ||||
|     it('should not set an expiration when expiration is off', function(done) { | ||||
|       var store = new RedisDocumentStore({ expire: false }); | ||||
|       store.set('hello3', 'world', function() { | ||||
|       store.set('hello3', 'world', function(worked) { | ||||
|         RedisDocumentStore.client.ttl('hello3', function(err, res) { | ||||
|           assert.equal(-1, res); | ||||
|           res.should.equal(-1); | ||||
|           done(); | ||||
|         }); | ||||
|       }); | ||||
| @@ -1,182 +1,201 @@ | ||||
| html, body, div, pre, textarea, header, h1, a, nav, ul, li { | ||||
|   margin: 0; | ||||
|   padding: 0; | ||||
| } | ||||
|  | ||||
| body { | ||||
| 	background: #fcfcfc; | ||||
| 	padding: 20px 50px; | ||||
| 	margin: 0px; | ||||
|   font: 13px monospace; | ||||
| } | ||||
|  | ||||
| /* textarea */ | ||||
|  | ||||
| textarea { | ||||
| 	background: transparent; | ||||
| 	border: 0px; | ||||
| 	color: #000; | ||||
| 	padding: 0px; | ||||
| 	width: 100%; | ||||
| 	height: 100%; | ||||
| 	font-family: monospace; | ||||
| 	outline: none; | ||||
| 	resize: none; | ||||
| 	font-size: 13px; | ||||
| 	margin-top: 0; | ||||
| 	margin-bottom: 0; | ||||
| header { | ||||
|   position: fixed; | ||||
|   top: 0; | ||||
|   right: 0; | ||||
|   z-index: 1000; | ||||
| } | ||||
|  | ||||
| /* the line numbers */ | ||||
|  | ||||
| #linenos { | ||||
| 	color: #7d7d7d; | ||||
| 	z-index: -1000; | ||||
| 	position: absolute; | ||||
| 	/* top: 20px; */ | ||||
| 	left: 0px; | ||||
| 	width: 30px; /* 30 to get 20 away from box */ | ||||
| 	font-size: 13px; | ||||
| 	font-family: monospace; | ||||
| 	text-align: right; | ||||
| 	user-select: none; | ||||
| header h1 { | ||||
|   background: #00222b; | ||||
|   padding: 5px 22px; | ||||
| } | ||||
|  | ||||
| /* code box when locked */ | ||||
|  | ||||
| #box { | ||||
| 	padding: 0px; | ||||
| 	margin: 0px; | ||||
| 	width: 100%; | ||||
| 	border: 0px; | ||||
| 	outline: none; | ||||
| 	font-size: 13px; | ||||
| 	overflow: inherit; | ||||
| 	background: #fcfcfc; | ||||
| header h1 a { | ||||
|   background: transparent url('logo.png') no-repeat top center; | ||||
|   display: block; | ||||
|   overflow: hidden; | ||||
|   text-indent: -9999px; | ||||
|   width: 126px; | ||||
|   height: 42px; | ||||
| } | ||||
|  | ||||
| #box code { | ||||
| 	padding: 0px; | ||||
| 	background: transparent !important; /* don't hide hastebox */ | ||||
| header h1 a:hover { | ||||
|   background-position: bottom center; | ||||
| } | ||||
|  | ||||
| /* key */ | ||||
|  | ||||
| #key { | ||||
| 	display: flex; | ||||
| 	justify-content: space-between; | ||||
| 	width: 100%; | ||||
| 	position: sticky; | ||||
| 	flex-wrap: row; | ||||
| } | ||||
| #box1 { | ||||
| 	padding: 5px; | ||||
| 	text-align: center; | ||||
| 	background: #fcfcfc; | ||||
| header ul { | ||||
|   background: #08323c; | ||||
|   font-size: 0; | ||||
|   list-style: none; | ||||
|   /*overflow: hidden;*/ | ||||
|   text-align: center; | ||||
| } | ||||
|  | ||||
| #box2 { | ||||
| 	display: flex; | ||||
| 	justify-content: right; | ||||
| 	background: #fcfcfc; | ||||
| 	font-size: 0px; | ||||
| 	padding: 0px 5px; | ||||
| header ul li { | ||||
|   display: inline-block; | ||||
|   position: relative; | ||||
| } | ||||
|  | ||||
| a.logo, a.logo:visited { | ||||
| 	display: inline-block; | ||||
| 	background: url(logo.png); | ||||
| 	width: 126px; | ||||
| 	min-width: 126px; | ||||
| 	height: 42px; | ||||
| header ul li .pointer { | ||||
|   background: transparent url('hover-dropdown-tip.png') no-repeat; | ||||
|   display: inline-block; | ||||
|   text-align: center; | ||||
|   width: 10px; | ||||
|   height: 5px; | ||||
| } | ||||
|  | ||||
| a.logo:hover { | ||||
| 	background-position: 0 bottom; | ||||
| header ul li a { | ||||
|   background: transparent url('function-icons.png'); | ||||
|   display: block; | ||||
|   overflow: hidden; | ||||
|   text-indent: -9999px; | ||||
|   width: 32px; | ||||
|   height: 37px; | ||||
| } | ||||
|  | ||||
| #box2 .function { | ||||
| 	background: url(function-icons.png); | ||||
| 	width: 32px; | ||||
| 	height: 37px; | ||||
| 	display: inline-block; | ||||
| 	position: relative; | ||||
| header ul li a.disabled { | ||||
|   cursor: default; | ||||
| } | ||||
|  | ||||
| #box2 .link embed { | ||||
| 	vertical-align: bottom; /* fix for zeroClipboard style */ | ||||
| header li a.save { background-position: -5px center; } | ||||
| header li a.save:hover { background-position: -5px bottom; } | ||||
| header li a.save.disabled { background-position: -5px top; } | ||||
|  | ||||
| header li a.new { background-position: -42px center; } | ||||
| header li a.new:hover { background-position: -42px bottom; } | ||||
| header li a.new.disabled { background-position: -42px top; } | ||||
|  | ||||
| header li a.edit { background-position: -79px center; } | ||||
| header li a.edit:hover { background-position: -79px bottom; } | ||||
| header li a.edit.disabled { background-position: -79px top; } | ||||
|  | ||||
| header li a.raw { background-position: -116px center; } | ||||
| header li a.raw:hover { background-position: -116px bottom; } | ||||
| header li a.raw.disabled { background-position: -116px top; } | ||||
|  | ||||
| header li a.twitter { background-position: -153px center; } | ||||
| header li a.twitter:hover { background-position: -153px bottom; } | ||||
| header li a.twitter.disabled { background-position: -153px top; } | ||||
|  | ||||
| #editor { | ||||
|   position: absolute; | ||||
|   top: 0; | ||||
|   left: 0; | ||||
|   right: 0; | ||||
|   bottom: 0; | ||||
| } | ||||
|  | ||||
| #box2 .function.enabled:hover { | ||||
| 	cursor: hand; | ||||
| 	cursor: pointer; | ||||
| .CodeMirror { | ||||
|   line-height: 1em; | ||||
|   height: 100%; | ||||
| } | ||||
|  | ||||
| #pointer { | ||||
| 	display: block; | ||||
| 	height: 5px; | ||||
| 	width: 10px; | ||||
| 	background: url(hover-dropdown-tip.png); | ||||
| 	bottom: 0px; | ||||
| 	position: absolute; | ||||
| 	margin: auto; | ||||
| 	left: 0px; | ||||
| 	right: 0px; | ||||
| .CodeMirror-scroll { | ||||
|   height: 100%; | ||||
|   overflow: auto; | ||||
|   position: relative; | ||||
| } | ||||
|  | ||||
| #box3, #messages li { | ||||
| 	position: absolute; | ||||
| 	right: 100px; | ||||
| 	background: #fcfcfc; | ||||
| 	font-family: Helvetica, sans-serif; | ||||
| 	font-size: 12px; | ||||
| 	line-height: 14px; | ||||
| 	padding: 10px 15px; | ||||
| 	user-select: none; | ||||
| .CodeMirror-gutter { | ||||
|   height: 100%; | ||||
|   min-width: 2em; | ||||
|   position: absolute; | ||||
|   top: 0; | ||||
|   left: 0; | ||||
| } | ||||
|  | ||||
| #box3 .label { | ||||
| 	color: #000; | ||||
| 	font-weight: bold; | ||||
| .CodeMirror-gutter-text { | ||||
|   text-align: right; | ||||
|   padding: 0.4em 0.2em 0.4em 0.4em; | ||||
|   white-space: pre; | ||||
| } | ||||
|  | ||||
| #messages li { | ||||
| 	color: #FFF; | ||||
| 	right: 50px; | ||||
| .CodeMirror-lines { | ||||
|   padding: 0.4em; | ||||
| } | ||||
|  | ||||
| #box3 .shortcut { | ||||
| 	color: #c4dce3; | ||||
| 	font-weight: normal; | ||||
| .CodeMirror textarea { | ||||
|   outline: 0; | ||||
| } | ||||
|  | ||||
| #box2 .function.save { background-position: -5px top; } | ||||
| #box2 .function.enabled.save { background-position: -5px center; } | ||||
| #box2 .function.enabled.save:hover { background-position: -5px bottom; } | ||||
|  | ||||
| #box2 .function.new { background-position: -42px top; } | ||||
| #box2 .function.enabled.new { background-position: -42px center; } | ||||
| #box2 .function.enabled.new:hover { background-position: -42px bottom; } | ||||
|  | ||||
| #box2 .function.duplicate { background-position: -79px top; } | ||||
| #box2 .function.enabled.duplicate { background-position: -79px center; } | ||||
| #box2 .function.enabled.duplicate:hover { background-position: -79px bottom; } | ||||
|  | ||||
| #box2 .function.raw { background-position: -116px top; } | ||||
| #box2 .function.enabled.raw { background-position: -116px center; } | ||||
| #box2 .function.enabled.raw:hover { background-position: -116px bottom; } | ||||
|  | ||||
| #box2 .function.twitter { background-position: -153px top; } | ||||
| #box2 .function.enabled.twitter { background-position: -153px center; } | ||||
| #box2 .function.enabled.twitter:hover { background-position: -153px bottom; } | ||||
| #box2 .button-picture{ border-width: 0; font-size: inherit; } | ||||
|  | ||||
| #messages { | ||||
| 	margin:0; | ||||
| 	padding:0; | ||||
| .CodeMirror pre.CodeMirror-cursor { | ||||
|   position: absolute; | ||||
|   visibility: hidden; | ||||
|   z-index: 10; | ||||
| } | ||||
|  | ||||
| #messages li { | ||||
| 	background:rgba(252,252,252,0.8); | ||||
| 	margin:0 auto; | ||||
| 	list-style:none; | ||||
| .CodeMirror-focused pre.CodeMirror-cursor { | ||||
|   visibility: visible; | ||||
| } | ||||
|  | ||||
| #messages li.error { | ||||
| 	background:rgba(102,8,0,0.8); | ||||
| span.cm-header, span.cm-strong { | ||||
|   font-weight: bold; | ||||
| } | ||||
|  | ||||
| span.cm-em { | ||||
|   font-style: italic; | ||||
| } | ||||
|  | ||||
| span.cm-emstrong { | ||||
|   font-style: italic; font-weight: bold; | ||||
| } | ||||
|  | ||||
| span.cm-link { | ||||
|   text-decoration: underline; | ||||
| } | ||||
|  | ||||
| /* Solarized (dark) theme */ | ||||
|  | ||||
| .cm-s-solarized-dark { | ||||
|   background: #002b36; | ||||
|   color: #839496; | ||||
| } | ||||
|  | ||||
| .cm-s-solarized-dark div.CodeMirror-selected { | ||||
|   background: #586e75; | ||||
| } | ||||
|  | ||||
| .cm-s-solarized-dark .CodeMirror-gutter { | ||||
|   background: #073642; | ||||
| } | ||||
|  | ||||
| .cm-s-solarized-dark .CodeMirror-gutter-text { | ||||
|   color: #586e75; | ||||
| } | ||||
|  | ||||
| .cm-s-solarized-dark .CodeMirror-cursor { | ||||
|   border-left: 1px solid #839496; | ||||
| } | ||||
|  | ||||
| .cm-s-solarized-dark span.cm-keyword    { color: #268bd2; } | ||||
| .cm-s-solarized-dark span.cm-atom       { color: #b58900; } | ||||
| .cm-s-solarized-dark span.cm-number     { color: #2aa198; } | ||||
| .cm-s-solarized-dark span.cm-def        { color: #839496; } | ||||
| .cm-s-solarized-dark span.cm-variable   { color: #839496; } | ||||
| .cm-s-solarized-dark span.cm-variable-2 { color: #b58900; } | ||||
| .cm-s-solarized-dark span.cm-variable-3 { color: #268bd2; } | ||||
| .cm-s-solarized-dark span.cm-property   { color: #859900; } | ||||
| .cm-s-solarized-dark span.cm-operator   { color: #2aa198; } | ||||
| .cm-s-solarized-dark span.cm-comment    { color: #586e75; } | ||||
| .cm-s-solarized-dark span.cm-string     { color: #2aa198; } | ||||
| .cm-s-solarized-dark span.cm-string-2   { color: #2aa198; } | ||||
| .cm-s-solarized-dark span.cm-meta       { color: #586e75; } | ||||
| .cm-s-solarized-dark span.cm-error      { color: #dc322f; } | ||||
| .cm-s-solarized-dark span.cm-qualifier  { color: #268bd2; } | ||||
| .cm-s-solarized-dark span.cm-builtin    { color: #b58900; } | ||||
| .cm-s-solarized-dark span.cm-bracket    { color: #dc322f; } | ||||
| .cm-s-solarized-dark span.cm-tag        { color: #268bd2; } | ||||
| .cm-s-solarized-dark span.cm-attribute  { color: #839496; } | ||||
| .cm-s-solarized-dark span.cm-header     { color: #cb4b16; } | ||||
| .cm-s-solarized-dark span.cm-quote      { color: #586e75; } | ||||
| .cm-s-solarized-dark span.cm-hr         { color: #cb4b16; } | ||||
| .cm-s-solarized-dark span.cm-link       { color: #6c71c4; } | ||||
|   | ||||
| @@ -1,398 +1,171 @@ | ||||
| /* global $, hljs, window, document */ | ||||
| window.Haste = { | ||||
|   Models: {}, | ||||
|   Views: {}, | ||||
|   Routers: {}, | ||||
|  | ||||
| ///// represents a single document | ||||
|   extensionMap: { | ||||
|     clj: 'clojure', coffee: 'coffeescript', css: 'css', diff: 'diff', go: 'go', | ||||
|     hs: 'haskell', html: 'htmlmixed', js: 'javascript', lua: 'lua', | ||||
|     md: 'markdown', markdown: 'markdown', sql: 'mysql', pl: 'perl', php: 'php', | ||||
|     py: 'python', r: 'r', rb: 'ruby', scm: 'scheme', xml: 'xml', yml: 'yaml' | ||||
|   }, | ||||
|  | ||||
| var haste_document = function() { | ||||
|   this.locked = false; | ||||
| }; | ||||
|  | ||||
| // Escapes HTML tag characters | ||||
| haste_document.prototype.htmlEscape = function(s) { | ||||
|   return s | ||||
|     .replace(/&/g, '&') | ||||
|     .replace(/>/g, '>') | ||||
|     .replace(/</g, '<') | ||||
|     .replace(/"/g, '"'); | ||||
| }; | ||||
|  | ||||
| // Get this document from the server and lock it here | ||||
| haste_document.prototype.load = function(key, callback, lang) { | ||||
|   var _this = this; | ||||
|   $.ajax('/documents/' + key, { | ||||
|     type: 'get', | ||||
|     dataType: 'json', | ||||
|     success: function(res) { | ||||
|       _this.locked = true; | ||||
|       _this.key = key; | ||||
|       _this.data = res.data; | ||||
|       try { | ||||
|         var high; | ||||
|         if (lang === 'txt') { | ||||
|           high = { value: _this.htmlEscape(res.data) }; | ||||
|         } | ||||
|         else if (lang) { | ||||
|           high = hljs.highlight(lang, res.data); | ||||
|         } | ||||
|         else { | ||||
|           high = hljs.highlightAuto(res.data); | ||||
|         } | ||||
|       } catch(err) { | ||||
|         // failed highlight, fall back on auto | ||||
|         high = hljs.highlightAuto(res.data); | ||||
|       } | ||||
|       callback({ | ||||
|         value: high.value, | ||||
|         key: key, | ||||
|         language: high.language || lang, | ||||
|         lineCount: res.data.split('\n').length | ||||
|       }); | ||||
|     }, | ||||
|     error: function() { | ||||
|       callback(false); | ||||
|     } | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| // Save this document to the server and lock it here | ||||
| haste_document.prototype.save = function(data, callback) { | ||||
|   if (this.locked) { | ||||
|     return false; | ||||
|   } | ||||
|   this.data = data; | ||||
|   var _this = this; | ||||
|   $.ajax('/documents', { | ||||
|     type: 'post', | ||||
|     data: data, | ||||
|     dataType: 'json', | ||||
|     contentType: 'text/plain; charset=utf-8', | ||||
|     success: function(res) { | ||||
|       _this.locked = true; | ||||
|       _this.key = res.key; | ||||
|       var high = hljs.highlightAuto(data); | ||||
|       callback(null, { | ||||
|         value: high.value, | ||||
|         key: res.key, | ||||
|         language: high.language, | ||||
|         lineCount: data.split('\n').length | ||||
|       }); | ||||
|     }, | ||||
|     error: function(res) { | ||||
|       try { | ||||
|         callback($.parseJSON(res.responseText)); | ||||
|       } | ||||
|       catch (e) { | ||||
|         callback({message: 'Something went wrong!'}); | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| ///// represents the paste application | ||||
|  | ||||
| var haste = function(appName, options) { | ||||
|   this.appName = appName; | ||||
|   this.$textarea = $('textarea'); | ||||
|   this.$box = $('#box'); | ||||
|   this.$code = $('#box code'); | ||||
|   this.$linenos = $('#linenos'); | ||||
|   this.options = options; | ||||
|   this.configureShortcuts(); | ||||
|   this.configureButtons(); | ||||
|   // If twitter is disabled, hide the button | ||||
|   if (!options.twitter) { | ||||
|     $('#box2 .twitter').hide(); | ||||
|   init: function() { | ||||
|     new Haste.Routers.Document(); | ||||
|     Backbone.history.start({ pushState: true }); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| // Set the page title - include the appName | ||||
| haste.prototype.setTitle = function(ext) { | ||||
|   var title = ext ? this.appName + ' - ' + ext : this.appName; | ||||
|   document.title = title; | ||||
| }; | ||||
|  | ||||
| // Show a message box | ||||
| haste.prototype.showMessage = function(msg, cls) { | ||||
|   var msgBox = $('<li class="'+(cls || 'info')+'">'+msg+'</li>'); | ||||
|   $('#messages').prepend(msgBox); | ||||
|   setTimeout(function() { | ||||
|     msgBox.slideUp('fast', function() { $(this).remove(); }); | ||||
|   }, 3000); | ||||
| }; | ||||
|  | ||||
| // Show the light key | ||||
| haste.prototype.lightKey = function() { | ||||
|   this.configureKey(['new', 'save']); | ||||
| }; | ||||
|  | ||||
| // Show the full key | ||||
| haste.prototype.fullKey = function() { | ||||
|   this.configureKey(['new', 'duplicate', 'twitter', 'raw']); | ||||
| }; | ||||
|  | ||||
| // Set the key up for certain things to be enabled | ||||
| haste.prototype.configureKey = function(enable) { | ||||
|   var $this, i = 0; | ||||
|   $('#box2 .function').each(function() { | ||||
|     $this = $(this); | ||||
|     for (i = 0; i < enable.length; i++) { | ||||
|       if ($this.hasClass(enable[i])) { | ||||
|         $this.addClass('enabled'); | ||||
|         return true; | ||||
|       } | ||||
|     } | ||||
|     $this.removeClass('enabled'); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| // Remove the current document (if there is one) | ||||
| // and set up for a new one | ||||
| haste.prototype.newDocument = function(hideHistory) { | ||||
|   this.$box.hide(); | ||||
|   this.doc = new haste_document(); | ||||
|   if (!hideHistory) { | ||||
|     window.history.pushState(null, this.appName, '/'); | ||||
|   } | ||||
|   this.setTitle(); | ||||
|   this.lightKey(); | ||||
|   this.$textarea.val('').show('fast', function() { | ||||
|     this.focus(); | ||||
|   }); | ||||
|   this.removeLineNumbers(); | ||||
| }; | ||||
|  | ||||
| // Map of common extensions | ||||
| // Note: this list does not need to include anything that IS its extension, | ||||
| // due to the behavior of lookupTypeByExtension and lookupExtensionByType | ||||
| // Note: optimized for lookupTypeByExtension | ||||
| haste.extensionMap = { | ||||
|   rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go', | ||||
|   xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript', | ||||
|   lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec', | ||||
|   vala: 'vala', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini', | ||||
|   diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell', | ||||
|   md: 'markdown', txt: '', coffee: 'coffee', swift: 'swift' | ||||
| }; | ||||
|  | ||||
| // Look up the extension preferred for a type | ||||
| // If not found, return the type itself - which we'll place as the extension | ||||
| haste.prototype.lookupExtensionByType = function(type) { | ||||
|   for (var key in haste.extensionMap) { | ||||
|     if (haste.extensionMap[key] === type) return key; | ||||
|   } | ||||
|   return type; | ||||
| }; | ||||
|  | ||||
| // Look up the type for a given extension | ||||
| // If not found, return the extension - which we'll attempt to use as the type | ||||
| haste.prototype.lookupTypeByExtension = function(ext) { | ||||
|   return haste.extensionMap[ext] || ext; | ||||
| }; | ||||
|  | ||||
| // Add line numbers to the document | ||||
| // For the specified number of lines | ||||
| haste.prototype.addLineNumbers = function(lineCount) { | ||||
|   var h = ''; | ||||
|   for (var i = 0; i < lineCount; i++) { | ||||
|     h += (i + 1).toString() + '<br/>'; | ||||
|   } | ||||
|   $('#linenos').html(h); | ||||
| }; | ||||
|  | ||||
| // Remove the line numbers | ||||
| haste.prototype.removeLineNumbers = function() { | ||||
|   $('#linenos').html('>'); | ||||
| }; | ||||
|  | ||||
| // Load a document and show it | ||||
| haste.prototype.loadDocument = function(key) { | ||||
|   // Split the key up | ||||
|   var parts = key.split('.', 2); | ||||
|   // Ask for what we want | ||||
|   var _this = this; | ||||
|   _this.doc = new haste_document(); | ||||
|   _this.doc.load(parts[0], function(ret) { | ||||
|     if (ret) { | ||||
|       _this.$code.html(ret.value); | ||||
|       _this.setTitle(ret.key); | ||||
|       _this.fullKey(); | ||||
|       _this.$textarea.val('').hide(); | ||||
|       _this.$box.show().focus(); | ||||
|       _this.addLineNumbers(ret.lineCount); | ||||
|     } | ||||
|     else { | ||||
|       _this.newDocument(); | ||||
|     } | ||||
|   }, this.lookupTypeByExtension(parts[1])); | ||||
| }; | ||||
|  | ||||
| // Duplicate the current document - only if locked | ||||
| haste.prototype.duplicateDocument = function() { | ||||
|   if (this.doc.locked) { | ||||
|     var currentData = this.doc.data; | ||||
|     this.newDocument(); | ||||
|     this.$textarea.val(currentData); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| // Lock the current document | ||||
| haste.prototype.lockDocument = function() { | ||||
|   var _this = this; | ||||
|   this.doc.save(this.$textarea.val(), function(err, ret) { | ||||
|     if (err) { | ||||
|       _this.showMessage(err.message, 'error'); | ||||
|     } | ||||
|     else if (ret) { | ||||
|       _this.$code.html(ret.value); | ||||
|       _this.setTitle(ret.key); | ||||
|       var file = '/' + ret.key; | ||||
|       if (ret.language) { | ||||
|         file += '.' + _this.lookupExtensionByType(ret.language); | ||||
|       } | ||||
|       window.history.pushState(null, _this.appName + '-' + ret.key, file); | ||||
|       _this.fullKey(); | ||||
|       _this.$textarea.val('').hide(); | ||||
|       _this.$box.show().focus(); | ||||
|       _this.addLineNumbers(ret.lineCount); | ||||
|     } | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| haste.prototype.configureButtons = function() { | ||||
|   var _this = this; | ||||
|   this.buttons = [ | ||||
|     { | ||||
|       $where: $('#box2 .save'), | ||||
|       label: 'Save', | ||||
|       shortcutDescription: 'control + s', | ||||
|       shortcut: function(evt) { | ||||
|         return evt.ctrlKey && (evt.keyCode === 83); | ||||
|       }, | ||||
|       action: function() { | ||||
|         if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') { | ||||
|           _this.lockDocument(); | ||||
|         } | ||||
|       } | ||||
|     }, | ||||
|     { | ||||
|       $where: $('#box2 .new'), | ||||
|       label: 'New', | ||||
|       shortcut: function(evt) { | ||||
|         return evt.ctrlKey && evt.keyCode === 78; | ||||
|       }, | ||||
|       shortcutDescription: 'control + n', | ||||
|       action: function() { | ||||
|         _this.newDocument(!_this.doc.key); | ||||
|       } | ||||
|     }, | ||||
|     { | ||||
|       $where: $('#box2 .duplicate'), | ||||
|       label: 'Duplicate & Edit', | ||||
|       shortcut: function(evt) { | ||||
|         return _this.doc.locked && evt.ctrlKey && evt.keyCode === 68; | ||||
|       }, | ||||
|       shortcutDescription: 'control + d', | ||||
|       action: function() { | ||||
|         _this.duplicateDocument(); | ||||
|       } | ||||
|     }, | ||||
|     { | ||||
|       $where: $('#box2 .raw'), | ||||
|       label: 'Just Text', | ||||
|       shortcut: function(evt) { | ||||
|         return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82; | ||||
|       }, | ||||
|       shortcutDescription: 'control + shift + r', | ||||
|       action: function() { | ||||
|         window.location.href = '/raw/' + _this.doc.key; | ||||
|       } | ||||
|     }, | ||||
|     { | ||||
|       $where: $('#box2 .twitter'), | ||||
|       label: 'Twitter', | ||||
|       shortcut: function(evt) { | ||||
|         return _this.options.twitter && _this.doc.locked && evt.shiftKey && evt.ctrlKey && evt.keyCode == 84; | ||||
|       }, | ||||
|       shortcutDescription: 'control + shift + t', | ||||
|       action: function() { | ||||
|         window.open('https://twitter.com/share?url=' + encodeURI(window.location.href)); | ||||
|       } | ||||
|     } | ||||
|   ]; | ||||
|   for (var i = 0; i < this.buttons.length; i++) { | ||||
|     this.configureButton(this.buttons[i]); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| haste.prototype.configureButton = function(options) { | ||||
|   // Handle the click action | ||||
|   options.$where.click(function(evt) { | ||||
|     evt.preventDefault(); | ||||
|     if (!options.clickDisabled && $(this).hasClass('enabled')) { | ||||
|       options.action(); | ||||
|     } | ||||
|   }); | ||||
|   // Show the label | ||||
|   options.$where.mouseenter(function() { | ||||
|     $('#box3 .label').text(options.label); | ||||
|     $('#box3 .shortcut').text(options.shortcutDescription || ''); | ||||
|     $('#box3').show(); | ||||
|     $(this).append($('#pointer').remove().show()); | ||||
|   }); | ||||
|   // Hide the label | ||||
|   options.$where.mouseleave(function() { | ||||
|     $('#box3').hide(); | ||||
|     $('#pointer').hide(); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| // Configure keyboard shortcuts for the textarea | ||||
| haste.prototype.configureShortcuts = function() { | ||||
|   var _this = this; | ||||
|   $(document.body).keydown(function(evt) { | ||||
|     var button; | ||||
|     for (var i = 0 ; i < _this.buttons.length; i++) { | ||||
|       button = _this.buttons[i]; | ||||
|       if (button.shortcut && button.shortcut(evt)) { | ||||
|         evt.preventDefault(); | ||||
|         button.action(); | ||||
|         return; | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| ///// Tab behavior in the textarea - 2 spaces per tab | ||||
| $(function() { | ||||
|  | ||||
|   $('textarea').keydown(function(evt) { | ||||
|     if (evt.keyCode === 9) { | ||||
|       evt.preventDefault(); | ||||
|       var myValue = '  '; | ||||
|       // http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery | ||||
|       // For browsers like Internet Explorer | ||||
|       if (document.selection) { | ||||
|         this.focus(); | ||||
|         var sel = document.selection.createRange(); | ||||
|         sel.text = myValue; | ||||
|         this.focus(); | ||||
|       } | ||||
|       // Mozilla and Webkit | ||||
|       else if (this.selectionStart || this.selectionStart == '0') { | ||||
|         var startPos = this.selectionStart; | ||||
|         var endPos = this.selectionEnd; | ||||
|         var scrollTop = this.scrollTop; | ||||
|         this.value = this.value.substring(0, startPos) + myValue + | ||||
|           this.value.substring(endPos,this.value.length); | ||||
|         this.focus(); | ||||
|         this.selectionStart = startPos + myValue.length; | ||||
|         this.selectionEnd = startPos + myValue.length; | ||||
|         this.scrollTop = scrollTop; | ||||
|       } | ||||
|       else { | ||||
|         this.value += myValue; | ||||
|         this.focus(); | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
|  | ||||
| Haste.Models.Document = Backbone.Model.extend({ | ||||
|   idAttribute: 'key', | ||||
|   urlRoot: '/documents' | ||||
| }); | ||||
|  | ||||
| Haste.Routers.Document = Backbone.Router.extend({ | ||||
|   routes: { | ||||
|     ':id.:extension': 'show', | ||||
|     ':id': 'show', | ||||
|     '': 'new' | ||||
|   }, | ||||
|  | ||||
|   initialize: function() { | ||||
|     this.editor = new Haste.Views.EditorView(); | ||||
|   }, | ||||
|  | ||||
|   show: function(id, extension) { | ||||
|     this.editor.load(id, extension); | ||||
|   }, | ||||
|  | ||||
|   new: function() { | ||||
|     this.editor.new(); | ||||
|   } | ||||
| }); | ||||
|  | ||||
| Haste.Views.ActionsView = Backbone.View.extend({ | ||||
|   el: 'header', | ||||
|  | ||||
|   events: { | ||||
|     'click .new': 'new', | ||||
|     'click .save': 'save', | ||||
|     'click .edit': 'edit', | ||||
|     'click .raw': 'raw', | ||||
|     'click .twitter': 'raw' | ||||
|   }, | ||||
|  | ||||
|   initialize: function() { | ||||
|     this.parent = this.options.parent; | ||||
|   }, | ||||
|  | ||||
|   toggleActions: function() { | ||||
|     var klass = 'disabled'; | ||||
|  | ||||
|     if (this.parent.model.isNew()) { | ||||
|       $('.save', this.el).removeClass(klass); | ||||
|       $('.edit, .raw, .twitter', this.el).addClass(klass); | ||||
|     } else { | ||||
|       $('.save', this.el).addClass(klass); | ||||
|       $('.edit, .raw, .twitter', this.el).removeClass(klass); | ||||
|     } | ||||
|  | ||||
|     this.setLink('.raw', 'raw/' + this.parent.model.id); | ||||
|     this.setLink('.twitter', 'https://twitter.com/share?url=' + encodeURI(window.location.href)); | ||||
|   }, | ||||
|  | ||||
|   setLink: function(el, href) { | ||||
|     if (this.parent.model.isNew()) { | ||||
|       href = '#'; | ||||
|     } | ||||
|  | ||||
|     $(el, this.el).attr('href', href); | ||||
|   }, | ||||
|  | ||||
|   new: function(event) { | ||||
|     event.preventDefault(); | ||||
|     this.parent.new(); | ||||
|     Backbone.history.navigate(''); | ||||
|   }, | ||||
|  | ||||
|   save: function(event) { | ||||
|     event.preventDefault(); | ||||
|  | ||||
|     if (!this.parent.model.isNew()) { return; } | ||||
|  | ||||
|     this.parent.save(); | ||||
|   }, | ||||
|  | ||||
|   edit: function(event) { | ||||
|     event.preventDefault(); | ||||
|  | ||||
|     if (this.parent.model.isNew()) { return; } | ||||
|  | ||||
|     this.parent.model.set('key', null); | ||||
|     Backbone.history.navigate('/'); | ||||
|   }, | ||||
|  | ||||
|   raw: function(event) { | ||||
|     if (this.model.isNew()) { | ||||
|       event.preventDefault(); | ||||
|     } | ||||
|   }, | ||||
| }); | ||||
|  | ||||
| Haste.Views.EditorView = Backbone.View.extend({ | ||||
|   el: 'textarea', | ||||
|  | ||||
|   initialize: function() { | ||||
|     this.codeMirror = CodeMirror.fromTextArea(this.el, { | ||||
|       mode: 'null', | ||||
|       lineNumbers: true, | ||||
|       theme: 'solarized-dark' | ||||
|     }); | ||||
|  | ||||
|     this.actionsView = new Haste.Views.ActionsView({ parent: this }); | ||||
|   }, | ||||
|  | ||||
|   render: function() { | ||||
|     this.codeMirror.setOption('mode', this.model.get('mode') || 'null'); | ||||
|     this.codeMirror.setValue(this.model.get('data') || ''); | ||||
|  | ||||
|     return this; | ||||
|   }, | ||||
|  | ||||
|   new: function() { | ||||
|     this.model = new Haste.Models.Document(); | ||||
|  | ||||
|     this.model.on('change', this.render, this); | ||||
|     this.model.on('change', this.toggleLock, this); | ||||
|     this.model.on('change', this.actionsView.toggleActions, this.actionsView); | ||||
|  | ||||
|     this.model.trigger('change'); | ||||
|   }, | ||||
|  | ||||
|   load: function(key, extension) { | ||||
|     this.new(); | ||||
|  | ||||
|     var mode = Haste.extensionMap[extension]; | ||||
|     this.model.set({ key: key, mode: mode }, { silent: true }); | ||||
|  | ||||
|     this.model.fetch(); | ||||
|   }, | ||||
|  | ||||
|   save: function() { | ||||
|     var data = this.codeMirror.getValue(); | ||||
|  | ||||
|     if (!data) { return; } | ||||
|  | ||||
|     this.model.save('data', data, { | ||||
|       success: function(model, response) { | ||||
|         Backbone.history.navigate(model.id); | ||||
|       } | ||||
|     }); | ||||
|   }, | ||||
|  | ||||
|   toggleLock: function() { | ||||
|     this.codeMirror.setOption('readOnly', !this.model.isNew()); | ||||
|     this.actionsView.toggleActions(); | ||||
|   } | ||||
| }); | ||||
|  | ||||
| $(function() { | ||||
|   Haste.init(); | ||||
| }); | ||||
|   | ||||
							
								
								
									
										2
									
								
								static/application.min.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								static/application.min.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										37
									
								
								static/backbone.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								static/backbone.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| // Backbone.js 0.9.1 | ||||
|  | ||||
| // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. | ||||
| // Backbone may be freely distributed under the MIT license. | ||||
| // For all details and documentation: | ||||
| // http://backbonejs.org | ||||
| (function(){var i=this,r=i.Backbone,s=Array.prototype.slice,t=Array.prototype.splice,g;g="undefined"!==typeof exports?exports:i.Backbone={};g.VERSION="0.9.1";var f=i._;!f&&"undefined"!==typeof require&&(f=require("underscore"));var h=i.jQuery||i.Zepto||i.ender;g.setDomLibrary=function(a){h=a};g.noConflict=function(){i.Backbone=r;return this};g.emulateHTTP=!1;g.emulateJSON=!1;g.Events={on:function(a,b,c){for(var d,a=a.split(/\s+/),e=this._callbacks||(this._callbacks={});d=a.shift();){d=e[d]||(e[d]= | ||||
| {});var f=d.tail||(d.tail=d.next={});f.callback=b;f.context=c;d.tail=f.next={}}return this},off:function(a,b,c){var d,e,f;if(a){if(e=this._callbacks)for(a=a.split(/\s+/);d=a.shift();)if(f=e[d],delete e[d],b&&f)for(;(f=f.next)&&f.next;)if(!(f.callback===b&&(!c||f.context===c)))this.on(d,f.callback,f.context)}else delete this._callbacks;return this},trigger:function(a){var b,c,d,e;if(!(d=this._callbacks))return this;e=d.all;for((a=a.split(/\s+/)).push(null);b=a.shift();)e&&a.push({next:e.next,tail:e.tail, | ||||
| event:b}),(c=d[b])&&a.push({next:c.next,tail:c.tail});for(e=s.call(arguments,1);c=a.pop();){b=c.tail;for(d=c.event?[c.event].concat(e):e;(c=c.next)!==b;)c.callback.apply(c.context||this,d)}return this}};g.Events.bind=g.Events.on;g.Events.unbind=g.Events.off;g.Model=function(a,b){var c;a||(a={});b&&b.parse&&(a=this.parse(a));if(c=j(this,"defaults"))a=f.extend({},c,a);b&&b.collection&&(this.collection=b.collection);this.attributes={};this._escapedAttributes={};this.cid=f.uniqueId("c");if(!this.set(a, | ||||
| {silent:!0}))throw Error("Can't create an invalid model");delete this._changed;this._previousAttributes=f.clone(this.attributes);this.initialize.apply(this,arguments)};f.extend(g.Model.prototype,g.Events,{idAttribute:"id",initialize:function(){},toJSON:function(){return f.clone(this.attributes)},get:function(a){return this.attributes[a]},escape:function(a){var b;if(b=this._escapedAttributes[a])return b;b=this.attributes[a];return this._escapedAttributes[a]=f.escape(null==b?"":""+b)},has:function(a){return null!= | ||||
| this.attributes[a]},set:function(a,b,c){var d,e;f.isObject(a)||null==a?(d=a,c=b):(d={},d[a]=b);c||(c={});if(!d)return this;d instanceof g.Model&&(d=d.attributes);if(c.unset)for(e in d)d[e]=void 0;if(!this._validate(d,c))return!1;this.idAttribute in d&&(this.id=d[this.idAttribute]);var b=this.attributes,k=this._escapedAttributes,n=this._previousAttributes||{},h=this._setting;this._changed||(this._changed={});this._setting=!0;for(e in d)if(a=d[e],f.isEqual(b[e],a)||delete k[e],c.unset?delete b[e]:b[e]= | ||||
| a,this._changing&&!f.isEqual(this._changed[e],a)&&(this.trigger("change:"+e,this,a,c),this._moreChanges=!0),delete this._changed[e],!f.isEqual(n[e],a)||f.has(b,e)!=f.has(n,e))this._changed[e]=a;h||(!c.silent&&this.hasChanged()&&this.change(c),this._setting=!1);return this},unset:function(a,b){(b||(b={})).unset=!0;return this.set(a,null,b)},clear:function(a){(a||(a={})).unset=!0;return this.set(f.clone(this.attributes),a)},fetch:function(a){var a=a?f.clone(a):{},b=this,c=a.success;a.success=function(d, | ||||
| e,f){if(!b.set(b.parse(d,f),a))return!1;c&&c(b,d)};a.error=g.wrapError(a.error,b,a);return(this.sync||g.sync).call(this,"read",this,a)},save:function(a,b,c){var d,e;f.isObject(a)||null==a?(d=a,c=b):(d={},d[a]=b);c=c?f.clone(c):{};c.wait&&(e=f.clone(this.attributes));a=f.extend({},c,{silent:!0});if(d&&!this.set(d,c.wait?a:c))return!1;var k=this,h=c.success;c.success=function(a,b,e){b=k.parse(a,e);c.wait&&(b=f.extend(d||{},b));if(!k.set(b,c))return!1;h?h(k,a):k.trigger("sync",k,a,c)};c.error=g.wrapError(c.error, | ||||
| k,c);b=this.isNew()?"create":"update";b=(this.sync||g.sync).call(this,b,this,c);c.wait&&this.set(e,a);return b},destroy:function(a){var a=a?f.clone(a):{},b=this,c=a.success,d=function(){b.trigger("destroy",b,b.collection,a)};if(this.isNew())return d();a.success=function(e){a.wait&&d();c?c(b,e):b.trigger("sync",b,e,a)};a.error=g.wrapError(a.error,b,a);var e=(this.sync||g.sync).call(this,"delete",this,a);a.wait||d();return e},url:function(){var a=j(this.collection,"url")||j(this,"urlRoot")||o();return this.isNew()? | ||||
| a:a+("/"==a.charAt(a.length-1)?"":"/")+encodeURIComponent(this.id)},parse:function(a){return a},clone:function(){return new this.constructor(this.attributes)},isNew:function(){return null==this.id},change:function(a){if(this._changing||!this.hasChanged())return this;this._moreChanges=this._changing=!0;for(var b in this._changed)this.trigger("change:"+b,this,this._changed[b],a);for(;this._moreChanges;)this._moreChanges=!1,this.trigger("change",this,a);this._previousAttributes=f.clone(this.attributes); | ||||
| delete this._changed;this._changing=!1;return this},hasChanged:function(a){return!arguments.length?!f.isEmpty(this._changed):this._changed&&f.has(this._changed,a)},changedAttributes:function(a){if(!a)return this.hasChanged()?f.clone(this._changed):!1;var b,c=!1,d=this._previousAttributes,e;for(e in a)if(!f.isEqual(d[e],b=a[e]))(c||(c={}))[e]=b;return c},previous:function(a){return!arguments.length||!this._previousAttributes?null:this._previousAttributes[a]},previousAttributes:function(){return f.clone(this._previousAttributes)}, | ||||
| isValid:function(){return!this.validate(this.attributes)},_validate:function(a,b){if(b.silent||!this.validate)return!0;var a=f.extend({},this.attributes,a),c=this.validate(a,b);if(!c)return!0;b&&b.error?b.error(this,c,b):this.trigger("error",this,c,b);return!1}});g.Collection=function(a,b){b||(b={});b.comparator&&(this.comparator=b.comparator);this._reset();this.initialize.apply(this,arguments);a&&this.reset(a,{silent:!0,parse:b.parse})};f.extend(g.Collection.prototype,g.Events,{model:g.Model,initialize:function(){}, | ||||
| toJSON:function(){return this.map(function(a){return a.toJSON()})},add:function(a,b){var c,d,e,g,h,i={},j={};b||(b={});a=f.isArray(a)?a.slice():[a];for(c=0,d=a.length;c<d;c++){if(!(e=a[c]=this._prepareModel(a[c],b)))throw Error("Can't add an invalid model to a collection");if(i[g=e.cid]||this._byCid[g]||null!=(h=e.id)&&(j[h]||this._byId[h]))throw Error("Can't add the same model to a collection twice");i[g]=j[h]=e}for(c=0;c<d;c++)(e=a[c]).on("all",this._onModelEvent,this),this._byCid[e.cid]=e,null!= | ||||
| e.id&&(this._byId[e.id]=e);this.length+=d;t.apply(this.models,[null!=b.at?b.at:this.models.length,0].concat(a));this.comparator&&this.sort({silent:!0});if(b.silent)return this;for(c=0,d=this.models.length;c<d;c++)if(i[(e=this.models[c]).cid])b.index=c,e.trigger("add",e,this,b);return this},remove:function(a,b){var c,d,e,g;b||(b={});a=f.isArray(a)?a.slice():[a];for(c=0,d=a.length;c<d;c++)if(g=this.getByCid(a[c])||this.get(a[c]))delete this._byId[g.id],delete this._byCid[g.cid],e=this.indexOf(g),this.models.splice(e, | ||||
| 1),this.length--,b.silent||(b.index=e,g.trigger("remove",g,this,b)),this._removeReference(g);return this},get:function(a){return null==a?null:this._byId[null!=a.id?a.id:a]},getByCid:function(a){return a&&this._byCid[a.cid||a]},at:function(a){return this.models[a]},sort:function(a){a||(a={});if(!this.comparator)throw Error("Cannot sort a set without a comparator");var b=f.bind(this.comparator,this);1==this.comparator.length?this.models=this.sortBy(b):this.models.sort(b);a.silent||this.trigger("reset", | ||||
| this,a);return this},pluck:function(a){return f.map(this.models,function(b){return b.get(a)})},reset:function(a,b){a||(a=[]);b||(b={});for(var c=0,d=this.models.length;c<d;c++)this._removeReference(this.models[c]);this._reset();this.add(a,{silent:!0,parse:b.parse});b.silent||this.trigger("reset",this,b);return this},fetch:function(a){a=a?f.clone(a):{};void 0===a.parse&&(a.parse=!0);var b=this,c=a.success;a.success=function(d,e,f){b[a.add?"add":"reset"](b.parse(d,f),a);c&&c(b,d)};a.error=g.wrapError(a.error, | ||||
| b,a);return(this.sync||g.sync).call(this,"read",this,a)},create:function(a,b){var c=this,b=b?f.clone(b):{},a=this._prepareModel(a,b);if(!a)return!1;b.wait||c.add(a,b);var d=b.success;b.success=function(e,f){b.wait&&c.add(e,b);d?d(e,f):e.trigger("sync",a,f,b)};a.save(null,b);return a},parse:function(a){return a},chain:function(){return f(this.models).chain()},_reset:function(){this.length=0;this.models=[];this._byId={};this._byCid={}},_prepareModel:function(a,b){a instanceof g.Model?a.collection|| | ||||
| (a.collection=this):(b.collection=this,a=new this.model(a,b),a._validate(a.attributes,b)||(a=!1));return a},_removeReference:function(a){this==a.collection&&delete a.collection;a.off("all",this._onModelEvent,this)},_onModelEvent:function(a,b,c,d){("add"==a||"remove"==a)&&c!=this||("destroy"==a&&this.remove(b,d),b&&a==="change:"+b.idAttribute&&(delete this._byId[b.previous(b.idAttribute)],this._byId[b.id]=b),this.trigger.apply(this,arguments))}});f.each("forEach,each,map,reduce,reduceRight,find,detect,filter,select,reject,every,all,some,any,include,contains,invoke,max,min,sortBy,sortedIndex,toArray,size,first,initial,rest,last,without,indexOf,shuffle,lastIndexOf,isEmpty,groupBy".split(","), | ||||
| function(a){g.Collection.prototype[a]=function(){return f[a].apply(f,[this.models].concat(f.toArray(arguments)))}});g.Router=function(a){a||(a={});a.routes&&(this.routes=a.routes);this._bindRoutes();this.initialize.apply(this,arguments)};var u=/:\w+/g,v=/\*\w+/g,w=/[-[\]{}()+?.,\\^$|#\s]/g;f.extend(g.Router.prototype,g.Events,{initialize:function(){},route:function(a,b,c){g.history||(g.history=new g.History);f.isRegExp(a)||(a=this._routeToRegExp(a));c||(c=this[b]);g.history.route(a,f.bind(function(d){d= | ||||
| this._extractParameters(a,d);c&&c.apply(this,d);this.trigger.apply(this,["route:"+b].concat(d));g.history.trigger("route",this,b,d)},this));return this},navigate:function(a,b){g.history.navigate(a,b)},_bindRoutes:function(){if(this.routes){var a=[],b;for(b in this.routes)a.unshift([b,this.routes[b]]);b=0;for(var c=a.length;b<c;b++)this.route(a[b][0],a[b][1],this[a[b][1]])}},_routeToRegExp:function(a){a=a.replace(w,"\\$&").replace(u,"([^/]+)").replace(v,"(.*?)");return RegExp("^"+a+"$")},_extractParameters:function(a, | ||||
| b){return a.exec(b).slice(1)}});g.History=function(){this.handlers=[];f.bindAll(this,"checkUrl")};var m=/^[#\/]/,x=/msie [\w.]+/,l=!1;f.extend(g.History.prototype,g.Events,{interval:50,getFragment:function(a,b){if(null==a)if(this._hasPushState||b){var a=window.location.pathname,c=window.location.search;c&&(a+=c)}else a=window.location.hash;a=decodeURIComponent(a);a.indexOf(this.options.root)||(a=a.substr(this.options.root.length));return a.replace(m,"")},start:function(a){if(l)throw Error("Backbone.history has already been started"); | ||||
| this.options=f.extend({},{root:"/"},this.options,a);this._wantsHashChange=!1!==this.options.hashChange;this._wantsPushState=!!this.options.pushState;this._hasPushState=!(!this.options.pushState||!window.history||!window.history.pushState);var a=this.getFragment(),b=document.documentMode;if(b=x.exec(navigator.userAgent.toLowerCase())&&(!b||7>=b))this.iframe=h('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo("body")[0].contentWindow,this.navigate(a);this._hasPushState?h(window).bind("popstate", | ||||
| this.checkUrl):this._wantsHashChange&&"onhashchange"in window&&!b?h(window).bind("hashchange",this.checkUrl):this._wantsHashChange&&(this._checkUrlInterval=setInterval(this.checkUrl,this.interval));this.fragment=a;l=!0;a=window.location;b=a.pathname==this.options.root;if(this._wantsHashChange&&this._wantsPushState&&!this._hasPushState&&!b)return this.fragment=this.getFragment(null,!0),window.location.replace(this.options.root+"#"+this.fragment),!0;this._wantsPushState&&this._hasPushState&&b&&a.hash&& | ||||
| (this.fragment=a.hash.replace(m,""),window.history.replaceState({},document.title,a.protocol+"//"+a.host+this.options.root+this.fragment));if(!this.options.silent)return this.loadUrl()},stop:function(){h(window).unbind("popstate",this.checkUrl).unbind("hashchange",this.checkUrl);clearInterval(this._checkUrlInterval);l=!1},route:function(a,b){this.handlers.unshift({route:a,callback:b})},checkUrl:function(){var a=this.getFragment();a==this.fragment&&this.iframe&&(a=this.getFragment(this.iframe.location.hash)); | ||||
| if(a==this.fragment||a==decodeURIComponent(this.fragment))return!1;this.iframe&&this.navigate(a);this.loadUrl()||this.loadUrl(window.location.hash)},loadUrl:function(a){var b=this.fragment=this.getFragment(a);return f.any(this.handlers,function(a){if(a.route.test(b))return a.callback(b),!0})},navigate:function(a,b){if(!l)return!1;if(!b||!0===b)b={trigger:b};var c=(a||"").replace(m,"");this.fragment==c||this.fragment==decodeURIComponent(c)||(this._hasPushState?(0!=c.indexOf(this.options.root)&&(c= | ||||
| this.options.root+c),this.fragment=c,window.history[b.replace?"replaceState":"pushState"]({},document.title,c)):this._wantsHashChange?(this.fragment=c,this._updateHash(window.location,c,b.replace),this.iframe&&c!=this.getFragment(this.iframe.location.hash)&&(b.replace||this.iframe.document.open().close(),this._updateHash(this.iframe.location,c,b.replace))):window.location.assign(this.options.root+a),b.trigger&&this.loadUrl(a))},_updateHash:function(a,b,c){c?a.replace(a.toString().replace(/(javascript:|#).*$/, | ||||
| "")+"#"+b):a.hash=b}});g.View=function(a){this.cid=f.uniqueId("view");this._configure(a||{});this._ensureElement();this.initialize.apply(this,arguments);this.delegateEvents()};var y=/^(\S+)\s*(.*)$/,p="model,collection,el,id,attributes,className,tagName".split(",");f.extend(g.View.prototype,g.Events,{tagName:"div",$:function(a){return this.$el.find(a)},initialize:function(){},render:function(){return this},remove:function(){this.$el.remove();return this},make:function(a,b,c){a=document.createElement(a); | ||||
| b&&h(a).attr(b);c&&h(a).html(c);return a},setElement:function(a,b){this.$el=h(a);this.el=this.$el[0];!1!==b&&this.delegateEvents();return this},delegateEvents:function(a){if(a||(a=j(this,"events"))){this.undelegateEvents();for(var b in a){var c=a[b];f.isFunction(c)||(c=this[a[b]]);if(!c)throw Error('Event "'+a[b]+'" does not exist');var d=b.match(y),e=d[1],d=d[2],c=f.bind(c,this),e=e+(".delegateEvents"+this.cid);""===d?this.$el.bind(e,c):this.$el.delegate(d,e,c)}}},undelegateEvents:function(){this.$el.unbind(".delegateEvents"+ | ||||
| this.cid)},_configure:function(a){this.options&&(a=f.extend({},this.options,a));for(var b=0,c=p.length;b<c;b++){var d=p[b];a[d]&&(this[d]=a[d])}this.options=a},_ensureElement:function(){if(this.el)this.setElement(this.el,!1);else{var a=j(this,"attributes")||{};this.id&&(a.id=this.id);this.className&&(a["class"]=this.className);this.setElement(this.make(this.tagName,a),!1)}}});g.Model.extend=g.Collection.extend=g.Router.extend=g.View.extend=function(a,b){var c=z(this,a,b);c.extend=this.extend;return c}; | ||||
| var A={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};g.sync=function(a,b,c){var d=A[a],e={type:d,dataType:"json"};c.url||(e.url=j(b,"url")||o());if(!c.data&&b&&("create"==a||"update"==a))e.contentType="application/json",e.data=JSON.stringify(b.toJSON());g.emulateJSON&&(e.contentType="application/x-www-form-urlencoded",e.data=e.data?{model:e.data}:{});if(g.emulateHTTP&&("PUT"===d||"DELETE"===d))g.emulateJSON&&(e.data._method=d),e.type="POST",e.beforeSend=function(a){a.setRequestHeader("X-HTTP-Method-Override", | ||||
| d)};"GET"!==e.type&&!g.emulateJSON&&(e.processData=!1);return h.ajax(f.extend(e,c))};g.wrapError=function(a,b,c){return function(d,e){e=d===b?e:d;a?a(b,e,c):b.trigger("error",b,e,c)}};var q=function(){},z=function(a,b,c){var d;d=b&&b.hasOwnProperty("constructor")?b.constructor:function(){a.apply(this,arguments)};f.extend(d,a);q.prototype=a.prototype;d.prototype=new q;b&&f.extend(d.prototype,b);c&&f.extend(d,c);d.prototype.constructor=d;d.__super__=a.prototype;return d},j=function(a,b){return!a||!a[b]? | ||||
| null:f.isFunction(a[b])?a[b]():a[b]},o=function(){throw Error('A "url" property or function must be specified');}}).call(this); | ||||
							
								
								
									
										1
									
								
								static/codemirror.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								static/codemirror.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 6.1 KiB | 
							
								
								
									
										6
									
								
								static/highlight.min.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								static/highlight.min.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -1,64 +1,35 @@ | ||||
| <html> | ||||
| 	<head> | ||||
| 		<title>Webionite hastebin</title> | ||||
| 		<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||
| 		<meta charset="utf-8" /> | ||||
| 		<link rel="stylesheet" type="text/css" href="tomorrow.css"/> | ||||
| 		<link rel="stylesheet" type="text/css" href="application.css"/> | ||||
| <!DOCTYPE html> | ||||
| <html lang="en"> | ||||
|   <head> | ||||
|     <meta charset="utf-8" /> | ||||
|  | ||||
| 		<script type="text/javascript" src="jquery.min.js"></script> | ||||
| 		<script type="text/javascript" src="highlight.min.js"></script> | ||||
| 		<script type="text/javascript" src="application.min.js"></script> | ||||
|     <title>Hastebin</title> | ||||
|  | ||||
| 		<meta name="robots" content="noindex,nofollow"/> | ||||
|     <link rel="stylesheet" href="application.css" /> | ||||
|  | ||||
| 		<script type="text/javascript"> | ||||
| 			var app = null; | ||||
| 			// Handle pops | ||||
| 			var handlePop = function(evt) { | ||||
| 				var path = evt.target.location.pathname; | ||||
| 				if (path === '/') { app.newDocument(true); } | ||||
| 				else { app.loadDocument(path.substring(1, path.length)); } | ||||
| 			}; | ||||
| 			// Set up the pop state to handle loads, skipping the first load | ||||
| 			// to make chrome behave like others: | ||||
| 			// http://code.google.com/p/chromium/issues/detail?id=63040 | ||||
| 			setTimeout(function() { | ||||
| 				window.onpopstate = function(evt) { | ||||
| 					try { handlePop(evt); } catch(err) { /* not loaded yet */ } | ||||
| 				}; | ||||
| 			}, 1000); | ||||
| 			// Construct app and load initial path | ||||
| 			$(function() { | ||||
| 				app = new haste('hastebin', { twitter: true }); | ||||
| 				handlePop({ target: window }); | ||||
| 			}); | ||||
| 		</script> | ||||
|     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | ||||
|     <script src="codemirror.min.js"></script> | ||||
|     <script src="underscore.min.js"></script> | ||||
|     <script src="backbone.min.js"></script> | ||||
|     <script src="application.min.js"></script> | ||||
|   </head> | ||||
|   <body> | ||||
|     <header> | ||||
|       <h1><a href="about.md">Hastebin</a></h1> | ||||
|  | ||||
| 	</head> | ||||
|       <nav> | ||||
|         <ul> | ||||
|           <li><a href="#" class="save">Save</a></li> | ||||
|           <li><a href="#" class="new">New</a></li> | ||||
|           <li><a href="#" class="edit">Edit</a></li> | ||||
|           <li><a href="#" class="raw">Raw</a></li> | ||||
|           <li><a href="#" class="twitter">Twitter</a></li> | ||||
|         </ul> | ||||
|       </nav> | ||||
|     </header> | ||||
|  | ||||
| 	<body> | ||||
|  | ||||
| 		<div id="key"> | ||||
| 			<a href="/about.md" class="logo"></a> | ||||
| 			<div> | ||||
| 				<div id="pointer" style="display:none;"></div> | ||||
| 				<div id="box2"> | ||||
| 					<button class="save function button-picture">Save</button> | ||||
| 					<button class="new function button-picture">New</button> | ||||
| 					<button class="duplicate function button-picture">Duplicate & Edit</button> | ||||
| 					<button class="raw function button-picture">Just Text</button> | ||||
| 				</div> | ||||
| 				<div id="box3" style="display:none;"> | ||||
| 					<div class="label"></div> | ||||
| 					<div class="shortcut"></div> | ||||
| 				</div> | ||||
| 				<ul id="messages"></ul> | ||||
| 			</div> | ||||
| 		</div> | ||||
|  | ||||
| 		<div id="linenos"></div> | ||||
| 		<pre id="box" style="display:none;" class="hljs" tabindex="0"><code></code></pre> | ||||
| 		<textarea spellcheck="false" style="display:none;"></textarea> | ||||
| 	</body> | ||||
|     <div id="editor"> | ||||
|       <textarea></textarea> | ||||
|     </div> | ||||
|   </body> | ||||
| </html> | ||||
|   | ||||
							
								
								
									
										4
									
								
								static/jquery.min.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								static/jquery.min.js
									
									
									
									
										vendored
									
									
								
							
										
											
												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: 5.0 KiB After Width: | Height: | Size: 4.6 KiB | 
| @@ -1,72 +0,0 @@ | ||||
| /* 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; | ||||
| } | ||||
							
								
								
									
										31
									
								
								static/underscore.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								static/underscore.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| // Underscore.js 1.3.1 | ||||
| // (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc. | ||||
| // Underscore is freely distributable under the MIT license. | ||||
| // Portions of Underscore are inspired or borrowed from Prototype, | ||||
| // Oliver Steele's Functional, and John Resig's Micro-Templating. | ||||
| // For all details and documentation: | ||||
| // http://documentcloud.github.com/underscore | ||||
| (function(){function q(a,c,d){if(a===c)return a!==0||1/a==1/c;if(a==null||c==null)return a===c;if(a._chain)a=a._wrapped;if(c._chain)c=c._wrapped;if(a.isEqual&&b.isFunction(a.isEqual))return a.isEqual(c);if(c.isEqual&&b.isFunction(c.isEqual))return c.isEqual(a);var e=l.call(a);if(e!=l.call(c))return false;switch(e){case "[object String]":return a==String(c);case "[object Number]":return a!=+a?c!=+c:a==0?1/a==1/c:a==+c;case "[object Date]":case "[object Boolean]":return+a==+c;case "[object RegExp]":return a.source== | ||||
| c.source&&a.global==c.global&&a.multiline==c.multiline&&a.ignoreCase==c.ignoreCase}if(typeof a!="object"||typeof c!="object")return false;for(var f=d.length;f--;)if(d[f]==a)return true;d.push(a);var f=0,g=true;if(e=="[object Array]"){if(f=a.length,g=f==c.length)for(;f--;)if(!(g=f in a==f in c&&q(a[f],c[f],d)))break}else{if("constructor"in a!="constructor"in c||a.constructor!=c.constructor)return false;for(var h in a)if(b.has(a,h)&&(f++,!(g=b.has(c,h)&&q(a[h],c[h],d))))break;if(g){for(h in c)if(b.has(c, | ||||
| h)&&!f--)break;g=!f}}d.pop();return g}var r=this,G=r._,n={},k=Array.prototype,o=Object.prototype,i=k.slice,H=k.unshift,l=o.toString,I=o.hasOwnProperty,w=k.forEach,x=k.map,y=k.reduce,z=k.reduceRight,A=k.filter,B=k.every,C=k.some,p=k.indexOf,D=k.lastIndexOf,o=Array.isArray,J=Object.keys,s=Function.prototype.bind,b=function(a){return new m(a)};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports)exports=module.exports=b;exports._=b}else r._=b;b.VERSION="1.3.1";var j=b.each= | ||||
| b.forEach=function(a,c,d){if(a!=null)if(w&&a.forEach===w)a.forEach(c,d);else if(a.length===+a.length)for(var e=0,f=a.length;e<f;e++){if(e in a&&c.call(d,a[e],e,a)===n)break}else for(e in a)if(b.has(a,e)&&c.call(d,a[e],e,a)===n)break};b.map=b.collect=function(a,c,b){var e=[];if(a==null)return e;if(x&&a.map===x)return a.map(c,b);j(a,function(a,g,h){e[e.length]=c.call(b,a,g,h)});if(a.length===+a.length)e.length=a.length;return e};b.reduce=b.foldl=b.inject=function(a,c,d,e){var f=arguments.length>2;a== | ||||
| null&&(a=[]);if(y&&a.reduce===y)return e&&(c=b.bind(c,e)),f?a.reduce(c,d):a.reduce(c);j(a,function(a,b,i){f?d=c.call(e,d,a,b,i):(d=a,f=true)});if(!f)throw new TypeError("Reduce of empty array with no initial value");return d};b.reduceRight=b.foldr=function(a,c,d,e){var f=arguments.length>2;a==null&&(a=[]);if(z&&a.reduceRight===z)return e&&(c=b.bind(c,e)),f?a.reduceRight(c,d):a.reduceRight(c);var g=b.toArray(a).reverse();e&&!f&&(c=b.bind(c,e));return f?b.reduce(g,c,d,e):b.reduce(g,c)};b.find=b.detect= | ||||
| function(a,c,b){var e;E(a,function(a,g,h){if(c.call(b,a,g,h))return e=a,true});return e};b.filter=b.select=function(a,c,b){var e=[];if(a==null)return e;if(A&&a.filter===A)return a.filter(c,b);j(a,function(a,g,h){c.call(b,a,g,h)&&(e[e.length]=a)});return e};b.reject=function(a,c,b){var e=[];if(a==null)return e;j(a,function(a,g,h){c.call(b,a,g,h)||(e[e.length]=a)});return e};b.every=b.all=function(a,c,b){var e=true;if(a==null)return e;if(B&&a.every===B)return a.every(c,b);j(a,function(a,g,h){if(!(e= | ||||
| e&&c.call(b,a,g,h)))return n});return e};var E=b.some=b.any=function(a,c,d){c||(c=b.identity);var e=false;if(a==null)return e;if(C&&a.some===C)return a.some(c,d);j(a,function(a,b,h){if(e||(e=c.call(d,a,b,h)))return n});return!!e};b.include=b.contains=function(a,c){var b=false;if(a==null)return b;return p&&a.indexOf===p?a.indexOf(c)!=-1:b=E(a,function(a){return a===c})};b.invoke=function(a,c){var d=i.call(arguments,2);return b.map(a,function(a){return(b.isFunction(c)?c||a:a[c]).apply(a,d)})};b.pluck= | ||||
| function(a,c){return b.map(a,function(a){return a[c]})};b.max=function(a,c,d){if(!c&&b.isArray(a))return Math.max.apply(Math,a);if(!c&&b.isEmpty(a))return-Infinity;var e={computed:-Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b>=e.computed&&(e={value:a,computed:b})});return e.value};b.min=function(a,c,d){if(!c&&b.isArray(a))return Math.min.apply(Math,a);if(!c&&b.isEmpty(a))return Infinity;var e={computed:Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b<e.computed&&(e={value:a,computed:b})}); | ||||
| return e.value};b.shuffle=function(a){var b=[],d;j(a,function(a,f){f==0?b[0]=a:(d=Math.floor(Math.random()*(f+1)),b[f]=b[d],b[d]=a)});return b};b.sortBy=function(a,c,d){return b.pluck(b.map(a,function(a,b,g){return{value:a,criteria:c.call(d,a,b,g)}}).sort(function(a,b){var c=a.criteria,d=b.criteria;return c<d?-1:c>d?1:0}),"value")};b.groupBy=function(a,c){var d={},e=b.isFunction(c)?c:function(a){return a[c]};j(a,function(a,b){var c=e(a,b);(d[c]||(d[c]=[])).push(a)});return d};b.sortedIndex=function(a, | ||||
| c,d){d||(d=b.identity);for(var e=0,f=a.length;e<f;){var g=e+f>>1;d(a[g])<d(c)?e=g+1:f=g}return e};b.toArray=function(a){return!a?[]:a.toArray?a.toArray():b.isArray(a)?i.call(a):b.isArguments(a)?i.call(a):b.values(a)};b.size=function(a){return b.toArray(a).length};b.first=b.head=function(a,b,d){return b!=null&&!d?i.call(a,0,b):a[0]};b.initial=function(a,b,d){return i.call(a,0,a.length-(b==null||d?1:b))};b.last=function(a,b,d){return b!=null&&!d?i.call(a,Math.max(a.length-b,0)):a[a.length-1]};b.rest= | ||||
| b.tail=function(a,b,d){return i.call(a,b==null||d?1:b)};b.compact=function(a){return b.filter(a,function(a){return!!a})};b.flatten=function(a,c){return b.reduce(a,function(a,e){if(b.isArray(e))return a.concat(c?e:b.flatten(e));a[a.length]=e;return a},[])};b.without=function(a){return b.difference(a,i.call(arguments,1))};b.uniq=b.unique=function(a,c,d){var d=d?b.map(a,d):a,e=[];b.reduce(d,function(d,g,h){if(0==h||(c===true?b.last(d)!=g:!b.include(d,g)))d[d.length]=g,e[e.length]=a[h];return d},[]); | ||||
| return e};b.union=function(){return b.uniq(b.flatten(arguments,true))};b.intersection=b.intersect=function(a){var c=i.call(arguments,1);return b.filter(b.uniq(a),function(a){return b.every(c,function(c){return b.indexOf(c,a)>=0})})};b.difference=function(a){var c=b.flatten(i.call(arguments,1));return b.filter(a,function(a){return!b.include(c,a)})};b.zip=function(){for(var a=i.call(arguments),c=b.max(b.pluck(a,"length")),d=Array(c),e=0;e<c;e++)d[e]=b.pluck(a,""+e);return d};b.indexOf=function(a,c, | ||||
| d){if(a==null)return-1;var e;if(d)return d=b.sortedIndex(a,c),a[d]===c?d:-1;if(p&&a.indexOf===p)return a.indexOf(c);for(d=0,e=a.length;d<e;d++)if(d in a&&a[d]===c)return d;return-1};b.lastIndexOf=function(a,b){if(a==null)return-1;if(D&&a.lastIndexOf===D)return a.lastIndexOf(b);for(var d=a.length;d--;)if(d in a&&a[d]===b)return d;return-1};b.range=function(a,b,d){arguments.length<=1&&(b=a||0,a=0);for(var d=arguments[2]||1,e=Math.max(Math.ceil((b-a)/d),0),f=0,g=Array(e);f<e;)g[f++]=a,a+=d;return g}; | ||||
| var F=function(){};b.bind=function(a,c){var d,e;if(a.bind===s&&s)return s.apply(a,i.call(arguments,1));if(!b.isFunction(a))throw new TypeError;e=i.call(arguments,2);return d=function(){if(!(this instanceof d))return a.apply(c,e.concat(i.call(arguments)));F.prototype=a.prototype;var b=new F,g=a.apply(b,e.concat(i.call(arguments)));return Object(g)===g?g:b}};b.bindAll=function(a){var c=i.call(arguments,1);c.length==0&&(c=b.functions(a));j(c,function(c){a[c]=b.bind(a[c],a)});return a};b.memoize=function(a, | ||||
| c){var d={};c||(c=b.identity);return function(){var e=c.apply(this,arguments);return b.has(d,e)?d[e]:d[e]=a.apply(this,arguments)}};b.delay=function(a,b){var d=i.call(arguments,2);return setTimeout(function(){return a.apply(a,d)},b)};b.defer=function(a){return b.delay.apply(b,[a,1].concat(i.call(arguments,1)))};b.throttle=function(a,c){var d,e,f,g,h,i=b.debounce(function(){h=g=false},c);return function(){d=this;e=arguments;var b;f||(f=setTimeout(function(){f=null;h&&a.apply(d,e);i()},c));g?h=true: | ||||
| a.apply(d,e);i();g=true}};b.debounce=function(a,b){var d;return function(){var e=this,f=arguments;clearTimeout(d);d=setTimeout(function(){d=null;a.apply(e,f)},b)}};b.once=function(a){var b=false,d;return function(){if(b)return d;b=true;return d=a.apply(this,arguments)}};b.wrap=function(a,b){return function(){var d=[a].concat(i.call(arguments,0));return b.apply(this,d)}};b.compose=function(){var a=arguments;return function(){for(var b=arguments,d=a.length-1;d>=0;d--)b=[a[d].apply(this,b)];return b[0]}}; | ||||
| b.after=function(a,b){return a<=0?b():function(){if(--a<1)return b.apply(this,arguments)}};b.keys=J||function(a){if(a!==Object(a))throw new TypeError("Invalid object");var c=[],d;for(d in a)b.has(a,d)&&(c[c.length]=d);return c};b.values=function(a){return b.map(a,b.identity)};b.functions=b.methods=function(a){var c=[],d;for(d in a)b.isFunction(a[d])&&c.push(d);return c.sort()};b.extend=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]=b[d]});return a};b.defaults=function(a){j(i.call(arguments, | ||||
| 1),function(b){for(var d in b)a[d]==null&&(a[d]=b[d])});return a};b.clone=function(a){return!b.isObject(a)?a:b.isArray(a)?a.slice():b.extend({},a)};b.tap=function(a,b){b(a);return a};b.isEqual=function(a,b){return q(a,b,[])};b.isEmpty=function(a){if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(b.has(a,c))return false;return true};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=o||function(a){return l.call(a)=="[object Array]"};b.isObject=function(a){return a===Object(a)}; | ||||
| b.isArguments=function(a){return l.call(a)=="[object Arguments]"};if(!b.isArguments(arguments))b.isArguments=function(a){return!(!a||!b.has(a,"callee"))};b.isFunction=function(a){return l.call(a)=="[object Function]"};b.isString=function(a){return l.call(a)=="[object String]"};b.isNumber=function(a){return l.call(a)=="[object Number]"};b.isNaN=function(a){return a!==a};b.isBoolean=function(a){return a===true||a===false||l.call(a)=="[object Boolean]"};b.isDate=function(a){return l.call(a)=="[object Date]"}; | ||||
| b.isRegExp=function(a){return l.call(a)=="[object RegExp]"};b.isNull=function(a){return a===null};b.isUndefined=function(a){return a===void 0};b.has=function(a,b){return I.call(a,b)};b.noConflict=function(){r._=G;return this};b.identity=function(a){return a};b.times=function(a,b,d){for(var e=0;e<a;e++)b.call(d,e)};b.escape=function(a){return(""+a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/\//g,"/")};b.mixin=function(a){j(b.functions(a), | ||||
| function(c){K(c,b[c]=a[c])})};var L=0;b.uniqueId=function(a){var b=L++;return a?a+b:b};b.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var t=/.^/,u=function(a){return a.replace(/\\\\/g,"\\").replace(/\\'/g,"'")};b.template=function(a,c){var d=b.templateSettings,d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+a.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(d.escape||t,function(a,b){return"',_.escape("+ | ||||
| u(b)+"),'"}).replace(d.interpolate||t,function(a,b){return"',"+u(b)+",'"}).replace(d.evaluate||t,function(a,b){return"');"+u(b).replace(/[\r\n\t]/g," ")+";__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');",e=new Function("obj","_",d);return c?e(c,b):function(a){return e.call(this,a,b)}};b.chain=function(a){return b(a).chain()};var m=function(a){this._wrapped=a};b.prototype=m.prototype;var v=function(a,c){return c?b(a).chain():a},K=function(a,c){m.prototype[a]= | ||||
| function(){var a=i.call(arguments);H.call(a,this._wrapped);return v(c.apply(b,a),this._chain)}};b.mixin(b);j("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var b=k[a];m.prototype[a]=function(){var d=this._wrapped;b.apply(d,arguments);var e=d.length;(a=="shift"||a=="splice")&&e===0&&delete d[0];return v(d,this._chain)}});j(["concat","join","slice"],function(a){var b=k[a];m.prototype[a]=function(){return v(b.apply(this._wrapped,arguments),this._chain)}});m.prototype.chain=function(){this._chain= | ||||
| true;return this};m.prototype.value=function(){return this._wrapped}}).call(this); | ||||
| @@ -1,33 +0,0 @@ | ||||
| /* 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)); | ||||
|       }); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
| @@ -1,27 +0,0 @@ | ||||
| /* 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])); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
| @@ -1,19 +0,0 @@ | ||||
| /* 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)); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user