first commit
This commit is contained in:
		
							
								
								
									
										6
									
								
								node_modules/binaryjs/.gitmodules
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								node_modules/binaryjs/.gitmodules
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | ||||
| [submodule "deps/js-binarypack"] | ||||
| 	path = deps/js-binarypack | ||||
| 	url = git://github.com/binaryjs/js-binarypack.git | ||||
| [submodule "deps/EventEmitter"] | ||||
| 	path = deps/EventEmitter | ||||
| 	url = git://github.com/ericz/EventEmitter.git | ||||
							
								
								
									
										15
									
								
								node_modules/binaryjs/.npmignore
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								node_modules/binaryjs/.npmignore
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| lib-cov | ||||
| *.seed | ||||
| *.log | ||||
| *.csv | ||||
| *.dat | ||||
| *.out | ||||
| *.pid | ||||
| *.gz | ||||
|  | ||||
| pids | ||||
| logs | ||||
| results | ||||
|  | ||||
| node_modules | ||||
| npm-debug.log | ||||
							
								
								
									
										4
									
								
								node_modules/binaryjs/.travis.yml
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								node_modules/binaryjs/.travis.yml
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | ||||
| language: node_js | ||||
| node_js: | ||||
|   - 0.6 | ||||
|   - 0.8 | ||||
							
								
								
									
										22
									
								
								node_modules/binaryjs/LICENSE
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								node_modules/binaryjs/LICENSE
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| Copyright (c) 2012 Eric Zhang, http://binaryjs.com | ||||
|  | ||||
| (The MIT License) | ||||
|  | ||||
| Permission is hereby granted, free of charge, to any person obtaining | ||||
| a copy of this software and associated documentation files (the | ||||
| "Software"), to deal in the Software without restriction, including | ||||
| without limitation the rights to use, copy, modify, merge, publish, | ||||
| distribute, sublicense, and/or sell copies of the Software, and to | ||||
| permit persons to whom the Software is furnished to do so, subject to | ||||
| the following conditions: | ||||
|  | ||||
| The above copyright notice and this permission notice shall be | ||||
| included in all copies or substantial portions of the Software. | ||||
|  | ||||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||||
| LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||||
| OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||||
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||
							
								
								
									
										12
									
								
								node_modules/binaryjs/Makefile
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								node_modules/binaryjs/Makefile
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| default: compress | ||||
|  | ||||
| compress: | ||||
| 	@node bin/build.js; | ||||
|    | ||||
| test: | ||||
| 	@mocha \ | ||||
| 		-t 2000 \ | ||||
| 		$(TESTFLAGS) \ | ||||
| 		$(TESTS) | ||||
|      | ||||
| .PHONY: test | ||||
							
								
								
									
										64
									
								
								node_modules/binaryjs/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								node_modules/binaryjs/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,64 @@ | ||||
| [](http://travis-ci.org/binaryjs/binaryjs) | ||||
|  | ||||
| BinaryJS | ||||
| ======== | ||||
|  | ||||
| We're under development! | ||||
|  | ||||
| For docs and more info | ||||
| http://binaryjs.com | ||||
|  | ||||
| Node binary websocket streaming made easy | ||||
|  | ||||
|  | ||||
| ## Download | ||||
|  | ||||
| Server | ||||
|  | ||||
| ```console | ||||
| $ npm install binaryjs | ||||
| ``` | ||||
| or from Git | ||||
| ```console | ||||
| $ git clone git://github.com/binaryjs/binaryjs.git | ||||
| $ cd binaryjs | ||||
| $ npm install -g | ||||
| ``` | ||||
|  | ||||
| Client | ||||
|  | ||||
| ```html | ||||
| <script src="http://cdn.binaryjs.com/0/binary.js"></script> | ||||
| ``` | ||||
|  | ||||
| ### http://binaryjs.com for docs and demos | ||||
|  | ||||
|  | ||||
| ## Changelog | ||||
| 0.2.1 | ||||
|  | ||||
| - Update js-binarypack to 0.0.7, fast utf8 support now on by default. | ||||
|  | ||||
| 0.2.0 | ||||
|  | ||||
| - Fix critical BinaryPack issue prevent TypedArrays of 16 bit or greater from being serialized correctly | ||||
|  | ||||
| 0.1.9 | ||||
|  | ||||
| - Fix close internal socket (removing not supported code/message parameters) | ||||
|  | ||||
| 0.1.8 | ||||
|  | ||||
| - Does not throw exceptions when writing on a stream with closed underlying socket | ||||
| - StreamID no longer has to be a number | ||||
| - Use newer version of node-binarypack | ||||
|  | ||||
| 0.1.7 | ||||
|  | ||||
| - Fix critical bug involving drain event not firing. Bump `streamws` to 0.1.1 | ||||
|  | ||||
| 0.1.5 | ||||
|  | ||||
| - `streamws` version `>=0.1.0` is now required | ||||
| - Streams no longer add their own listeners to error/close/drain events (fixes leaks) | ||||
| - Calls to `socket.send` no longer include `{binary: true}` or callback parameters (fixes type error in some browsers) | ||||
							
								
								
									
										217
									
								
								node_modules/binaryjs/bin/build.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										217
									
								
								node_modules/binaryjs/bin/build.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,217 @@ | ||||
| /** | ||||
|  * Module dependencies. | ||||
|  */ | ||||
|  | ||||
| var fs = require('fs') | ||||
|   , package = JSON.parse(fs.readFileSync(__dirname+ '/../package.json')) | ||||
|   , uglify = require('uglify-js'); | ||||
|  | ||||
| /** | ||||
|  * License headers. | ||||
|  * | ||||
|  * @api private | ||||
|  */ | ||||
|  | ||||
| var template = '/*! binary.%ext% build:' + package.version + ', %type%. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */' | ||||
|   , prefix = '\n(function(exports){\n' | ||||
|   , development = template.replace('%type%', 'development').replace('%ext%', 'js') | ||||
|   , production = template.replace('%type%', 'production').replace('%ext%', 'min.js') | ||||
|   , suffix = '\n})(this);\n'; | ||||
|  | ||||
| /** | ||||
|  * If statements, these allows you to create serveride & client side compatible | ||||
|  * code using specially designed `if` statements that remove serverside | ||||
|  * designed code from the source files | ||||
|  * | ||||
|  * @api private | ||||
|  */ | ||||
|  | ||||
| var starttagIF = '// if node' | ||||
|   , endtagIF = '// end node'; | ||||
|  | ||||
| /** | ||||
|  * The modules that are required to create a base build of BinaryJS client. | ||||
|  * | ||||
|  * @const | ||||
|  * @type {Array} | ||||
|  * @api private | ||||
|  */ | ||||
|  | ||||
| var base = [ | ||||
|     '../deps/js-binarypack/lib/bufferbuilder.js' | ||||
|   , '../deps/js-binarypack/lib/binarypack.js' | ||||
|   , '../deps/EventEmitter/EventEmitter.js' | ||||
|   , 'util.js' | ||||
|   , 'client/stream.js' | ||||
|   , 'client/blob_stream.js' | ||||
|   , 'stream.js' | ||||
|   , 'client.js' | ||||
| ]; | ||||
|  | ||||
|  | ||||
| /** | ||||
|  * Builds a custom BinaryJS client distribution based on the transports that you | ||||
|  * need. You can configure the build to create development build or production | ||||
|  * build (minified). | ||||
|  * | ||||
|  * @param {Array} transports The transports that needs to be bundled. | ||||
|  * @param {Object} [options] Options to configure the building process. | ||||
|  * @param {Function} callback Last argument should always be the callback | ||||
|  * @callback {String|Boolean} err An optional argument, if it exists than an error | ||||
|  *    occurred during the build process. | ||||
|  * @callback {String} result The result of the build process. | ||||
|  * @api public | ||||
|  */ | ||||
|  | ||||
| var builder = module.exports = function () { | ||||
|     var options, callback, error = null | ||||
|     , args = Array.prototype.slice.call(arguments, 0) | ||||
|     , settings = { | ||||
|         minify: true | ||||
|       , node: false | ||||
|       , custom: [] | ||||
|       }; | ||||
|  | ||||
|   // Fancy pancy argument support this makes any pattern possible mainly | ||||
|   // because we require only one of each type | ||||
|   args.forEach(function (arg) { | ||||
|     var type = Object.prototype.toString.call(arg) | ||||
|         .replace(/\[object\s(\w+)\]/gi , '$1' ).toLowerCase(); | ||||
|  | ||||
|     switch (type) { | ||||
|       case 'object': | ||||
|         return options = arg; | ||||
|       case 'function': | ||||
|         return callback = arg; | ||||
|     } | ||||
|   }); | ||||
|  | ||||
|   // Add defaults | ||||
|   options = options || {}; | ||||
|  | ||||
|   // Merge the data | ||||
|   for(var option in options) { | ||||
|     settings[option] = options[option]; | ||||
|   } | ||||
|  | ||||
|   // Start creating a dependencies chain with all the required files for the | ||||
|   // custom BinaryJS client bundle. | ||||
|   var files = []; | ||||
|   base.forEach(function (file) { | ||||
|     files.push(__dirname + '/../lib/' + file); | ||||
|   }); | ||||
|  | ||||
|   var results = {}; | ||||
|   files.forEach(function (file) { | ||||
|     fs.readFile(file, function (err, content) { | ||||
|       if (err) error = err; | ||||
|       results[file] = content; | ||||
|  | ||||
|       // check if we are done yet, or not.. Just by checking the size of the result | ||||
|       // object. | ||||
|       if (Object.keys(results).length !== files.length) return; | ||||
|  | ||||
|  | ||||
|       // concatinate the file contents in order | ||||
|       var code = development | ||||
|         , ignore = 0; | ||||
|  | ||||
|       code += prefix; | ||||
|        | ||||
|       files.forEach(function (file) { | ||||
|         code += results[file]; | ||||
|       }); | ||||
|  | ||||
|       // check if we need to add custom code | ||||
|       if (settings.custom.length) { | ||||
|         settings.custom.forEach(function (content) { | ||||
|           code += content; | ||||
|         }); | ||||
|       } | ||||
|  | ||||
|       // Search for conditional code blocks that need to be removed as they | ||||
|       // where designed for a server side env. but only if we don't want to | ||||
|       // make this build node compatible. | ||||
|       if (!settings.node) { | ||||
|         code = code.split('\n').filter(function (line) { | ||||
|           // check if there are tags in here | ||||
|           var start = line.indexOf(starttagIF) >= 0 | ||||
|             , end = line.indexOf(endtagIF) >= 0 | ||||
|             , ret = ignore; | ||||
|  | ||||
|           // ignore the current line | ||||
|           if (start) { | ||||
|             ignore++; | ||||
|             ret = ignore; | ||||
|           } | ||||
|  | ||||
|           // stop ignoring the next line | ||||
|           if (end) { | ||||
|             ignore--; | ||||
|           } | ||||
|  | ||||
|           return ret == 0; | ||||
|         }).join('\n'); | ||||
|       } | ||||
|        | ||||
|       code += suffix; | ||||
|  | ||||
|       // check if we need to process it any further | ||||
|       if (settings.minify) { | ||||
|         var ast = uglify.parser.parse(code); | ||||
|         ast = uglify.uglify.ast_mangle(ast); | ||||
|         ast = uglify.uglify.ast_squeeze(ast); | ||||
|  | ||||
|         code = production + uglify.uglify.gen_code(ast, { ascii_only: true }); | ||||
|       } | ||||
|  | ||||
|       callback(error, code); | ||||
|     }) | ||||
|   }) | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  * Builder version is also the current client version | ||||
|  * this way we don't have to do another include for the | ||||
|  * clients version number and we can just include the builder. | ||||
|  * | ||||
|  * @type {String} | ||||
|  * @api public | ||||
|  */ | ||||
|  | ||||
| builder.version = package.version; | ||||
|  | ||||
| /** | ||||
|  * Command line support, this allows us to generate builds without having | ||||
|  * to load it as module. | ||||
|  */ | ||||
|  | ||||
| if (!module.parent){ | ||||
|   // the first 2 are `node` and the path to this file, we don't need them | ||||
|   var args = process.argv.slice(2); | ||||
|   // build a development build | ||||
|   builder(args.length ? args : false, { minify:false }, function (err, content) { | ||||
|     if (err) return console.error(err); | ||||
| 		console.log(__dirname); | ||||
|     fs.write( | ||||
|         fs.openSync(__dirname + '/../dist/binary.js', 'w') | ||||
|       , content | ||||
|       , 0 | ||||
|       , 'utf8' | ||||
|     ); | ||||
|     console.log('Successfully generated the development build: binary.js'); | ||||
|   }); | ||||
|  | ||||
|   // and build a production build | ||||
|   builder(args.length ? args : false, function (err, content) { | ||||
|     if (err) return console.error(err); | ||||
|  | ||||
|     fs.write( | ||||
|         fs.openSync(__dirname + '/../dist/binary.min.js', 'w') | ||||
|       , content | ||||
|       , 0 | ||||
|       , 'utf8' | ||||
|     ); | ||||
|     console.log('Successfully generated the production build: binary.min.js'); | ||||
|   }); | ||||
| } | ||||
							
								
								
									
										7
									
								
								node_modules/binaryjs/dist/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								node_modules/binaryjs/dist/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| ## BinaryJS Clients | ||||
|  | ||||
| These files are available via cdn | ||||
|  | ||||
| http://cdn.binaryjs.com/0/binary.js | ||||
|  | ||||
| http://cdn.binaryjs.com/0/binary.min.js | ||||
							
								
								
									
										1559
									
								
								node_modules/binaryjs/dist/binary.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1559
									
								
								node_modules/binaryjs/dist/binary.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										1
									
								
								node_modules/binaryjs/dist/binary.min.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								node_modules/binaryjs/dist/binary.min.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										4
									
								
								node_modules/binaryjs/doc/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								node_modules/binaryjs/doc/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | ||||
| ## BinaryJS Documentations | ||||
|  | ||||
| ### [Getting started guide](https://github.com/binaryjs/binaryjs/blob/master/doc/start.md) | ||||
| ### [API reference](https://github.com/binaryjs/binaryjs/blob/master/doc/api.md) | ||||
							
								
								
									
										216
									
								
								node_modules/binaryjs/doc/api.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										216
									
								
								node_modules/binaryjs/doc/api.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,216 @@ | ||||
| # BinaryJS API Reference | ||||
|  | ||||
| ## Class: binaryjs.BinaryServer `Node.js only` | ||||
|  | ||||
| This class is a the BinaryJS websocket server. It is an `EventEmitter`. | ||||
|  | ||||
| ### new binaryjs.BinaryServer([options], [callback]) | ||||
|  | ||||
| * `options` Object | ||||
|   * `host` String. Default `0.0.0.0` | ||||
|   * `port` Number | ||||
|   * `server` Object. Use an existing `http.Server` or `ws.Server` instead of creating a new one. | ||||
|   * `path` String. The path on which to accept WebSocket connections | ||||
|   * `chunkSize` Number. Passed into constructor of connecting `BinaryClient`. Default `40960` | ||||
|  | ||||
| Construct a new server object. | ||||
|  | ||||
| Either `port` must be provided or `server` to use an existing `http.Server`. | ||||
|  | ||||
| ### server.clients | ||||
|  | ||||
| A hash of all connected clients. Keys are client ids and values are instances of `binaryjs.BinaryClient`. | ||||
|  | ||||
| ### server.close() | ||||
|  | ||||
| Close the server and terminate all clients. | ||||
|  | ||||
| ### Event: 'error' | ||||
|  | ||||
| `function (error) { }` | ||||
|  | ||||
| If the underlying server emits an error, it will be forwarded here. | ||||
|  | ||||
| ### Event: 'connection' | ||||
|  | ||||
| `function (client) { }` | ||||
|  | ||||
| When a new BinaryJS connection is established. `client` is an object of type `binaryjs.BinaryClient`. | ||||
|  | ||||
|  | ||||
|  | ||||
| ## Class: binaryjs.BinaryClient `Node.js and browsers` | ||||
|  | ||||
| This class represents a BinaryJS websocket client connection. It is an `EventEmitter`. | ||||
|  | ||||
| ### new binaryjs.BinaryClient(address, [options]) | ||||
|  | ||||
| Connects to the given `address` (in the form `ws://url`).  | ||||
|  | ||||
| * `options` Object | ||||
|   * `chunkSize` Number. Size of chunks when using `client.send`. Default `40960` | ||||
|  | ||||
| `BinaryClient` can also be obtained in the `connection` event of a `BinaryServer` | ||||
|    | ||||
| ### client.streams | ||||
|  | ||||
| A hash of all current streams. Keys are stream ids and values are instances of `binaryjs.BinaryStream`. | ||||
|  | ||||
| ### client.createStream([meta]) | ||||
|  | ||||
| Returns `binaryjs.BinaryStream` | ||||
|  | ||||
| Creates a new readable and writable `binaryjs.BinaryStream` object, triggering a `stream` event on the remote client with the given `meta` data parameter. | ||||
|  | ||||
| ### client.send(data, [meta]) | ||||
|  | ||||
| Returns `binaryjs.BinaryStream` | ||||
|  | ||||
| Creates a new readable and writable `binaryjs.BinaryStream` object with the given `meta` data and sends `data` through the connection. | ||||
|  | ||||
| If `data` is a Node.js `Buffer` or browser `ArrayBuffer`, `ArrayBufferView`, `Blob`, or `File`, the data will be chunked according to `options.chunkSize` and streamed through the new `BinaryStream`. | ||||
|  | ||||
| If `data` is a Node.js stream (including `BinaryStream` objects), it will be piped into a new `BinaryStream` | ||||
|  | ||||
| If `data` is any other type, it will be sent through the stream in one chunk. | ||||
|  | ||||
| ### client.close() | ||||
|  | ||||
| Gracefully closes the connection. | ||||
|  | ||||
| ### Event: 'stream' | ||||
|  | ||||
| `function (stream, meta) { }` | ||||
|  | ||||
| Emitted when a new stream is initialized by the remote client.  | ||||
|  | ||||
| `stream` is a `binaryjs.BinaryStream` object that is both readable and writable. | ||||
|  | ||||
| `meta` is the data given when the stream was created via `createStream(meta)` or `send(data, meta)`, sent verbatim. | ||||
|  | ||||
| ### Event: 'error' | ||||
|  | ||||
| `function (error) { }` | ||||
|  | ||||
| If the client emits an error, this event is emitted (errors from the underlying `net.Socket` are forwarded here). | ||||
|  | ||||
| ### Event: 'close' | ||||
|  | ||||
| `function () { }` | ||||
|  | ||||
| Is emitted when the connection is closed. | ||||
|  | ||||
| The `close` event is also emitted when then underlying `net.Socket` closes the connection (`end` or `close`). | ||||
|  | ||||
| ### Event: 'open' | ||||
|  | ||||
| `function () { }` | ||||
|  | ||||
| Emitted when the connection is established. | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| ## Class: binaryjs.BinaryStream `Node.js and browsers` | ||||
|  | ||||
| This class represents a BinaryJS client stream. It is a Node.js `Stream` and `EventEmitter`. | ||||
|  | ||||
| BinaryStreams are created through the `BinaryClient` `createStream` and `send` methods and received through the `stream` event. | ||||
|  | ||||
| Because `BinaryStream` extends the Node.js `stream`, all other streams can be piped into or from a `BinaryStream`. The browser implements a browser version of the Node.js `stream` API. | ||||
|    | ||||
| ### stream.id | ||||
|  | ||||
| A id number identifying the stream. Unique to the given client, but not globally. | ||||
|  | ||||
| ### stream.readable | ||||
|  | ||||
| Whether stream is readable. | ||||
|  | ||||
| ### stream.writable | ||||
|  | ||||
| Whether stream is writable. | ||||
|  | ||||
| ### stream.paused | ||||
|  | ||||
| Whether stream is paused. | ||||
|  | ||||
| ### stream.pause() | ||||
|  | ||||
| Pause the stream. | ||||
|  | ||||
| ### stream.resume() | ||||
|  | ||||
| Resume the stream. | ||||
|  | ||||
| ### stream.end() | ||||
|  | ||||
| Sends an end message, triggering the `end` event and marks `stream.readable` false but does not close the socket. | ||||
|  | ||||
|  | ||||
| ### stream.write(data) | ||||
|  | ||||
| Returns `true` if data is written immediately or `false` if data is buffered in socket. | ||||
|  | ||||
| Writes `data` through the connection. `data` can be any JSON compatible type or binary data. Note data will not be chunked. `client.send` should be used for chunking. | ||||
|  | ||||
| ### stream.destroy() | ||||
|  | ||||
| Immediately closed the socket. | ||||
|  | ||||
| ### stream.pipe(destination, [options]) | ||||
|  | ||||
| This is a Stream.prototype method available on all Streams. | ||||
|  | ||||
| See: | ||||
| http://nodejs.org/api/stream.html#stream_stream_pipe_destination_options | ||||
|  | ||||
| ### Event: 'data' | ||||
|  | ||||
| `function (data) { }` | ||||
|  | ||||
| Is emitted when data is received through the socket. | ||||
|  | ||||
| For non-binary types, data is received verbatim as sent. | ||||
|  | ||||
| On Node.js, binary data is received as `Buffer`. | ||||
|  | ||||
| On browsers, binary data is received as `ArrayBuffer`. | ||||
|  | ||||
| ### Event: 'pause' | ||||
|  | ||||
| `function () { }` | ||||
|  | ||||
| Is emitted when stream is paused. | ||||
|  | ||||
| ### Event: 'resume' | ||||
|  | ||||
| `function () { }` | ||||
|  | ||||
| Is emitted when stream is resumed. | ||||
|  | ||||
| ### Event: 'end' | ||||
|  | ||||
| `function () { }` | ||||
|  | ||||
| Is emitted when `stream.end` has been called. `stream.readable` is set to `false`. | ||||
|  | ||||
| ### Event: 'close' | ||||
|  | ||||
| `function () { }` | ||||
|  | ||||
| Emitted when the connection is destroyed or its underlying socket is closed. | ||||
|  | ||||
|  | ||||
| ### Event: 'drain' | ||||
|  | ||||
| `function () { }` | ||||
|  | ||||
| Emitted when the underlying socket buffer has drained. Used for stream pipe internals. | ||||
|  | ||||
| ### Event: 'error' | ||||
|  | ||||
| `function (error) { }` | ||||
|  | ||||
| If the client emits an error, this event is emitted (errors from the underlying net.Socket are forwarded here). `stream.readable` and `stream.writable` are set to `false`. | ||||
|  | ||||
							
								
								
									
										168
									
								
								node_modules/binaryjs/doc/start.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										168
									
								
								node_modules/binaryjs/doc/start.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,168 @@ | ||||
| # Getting Started with BinaryJS | ||||
|  | ||||
| In this guide we'll download, install, and then build the [helloworld](https://github.com/binaryjs/binaryjs/tree/master/examples/helloworld) example. In this example we send clients a picture of a flower by piping the file into a binary websocket; | ||||
|  | ||||
| ## Download | ||||
|  | ||||
| 1. Install the BinaryJS server | ||||
|  | ||||
| ```console | ||||
| $ npm install binaryjs | ||||
| ``` | ||||
| or from Git | ||||
| ```console | ||||
| $ git://github.com/binaryjs/binaryjs.git | ||||
| $ cd binaryjs  | ||||
| $ npm install -g | ||||
| ``` | ||||
|  | ||||
| 2. Get the latest client library for your webpage | ||||
|  | ||||
| ```html | ||||
| <script src="http://cdn.binaryjs.com/0/binary.js"></script> | ||||
| ``` | ||||
|  | ||||
| ## Create the server | ||||
| [View complete server.js source](https://github.com/binaryjs/binaryjs/blob/master/examples/helloworld/server.js) | ||||
|  | ||||
| First we include BinaryJS and the fs module | ||||
|  | ||||
| ```js | ||||
| var BinaryServer = require('binaryjs').BinaryServer; | ||||
| var fs = require('fs'); | ||||
| ``` | ||||
|  | ||||
| Then we create a new server, specifying that we want to run it on port 9000: | ||||
|  | ||||
| ```js | ||||
| var server = BinaryServer({port: 9000}); | ||||
| ``` | ||||
|  | ||||
| Now we'll add a callback for new client connections | ||||
|  | ||||
| ```js | ||||
| server.on('connection', function(client){ | ||||
|   // | ||||
| }); | ||||
| ``` | ||||
|  | ||||
| Let's add to that callback so when a client connects, we'll send them a picture as a hello world | ||||
|  | ||||
| ```js | ||||
| server.on('connection', function(client){	 | ||||
|   var file = fs.createReadStream(__dirname + '/flower.png'); | ||||
|   client.send(file);  | ||||
| }); | ||||
| ``` | ||||
|  | ||||
| Note that instead of `client.send(file)` we could've done this: | ||||
|  | ||||
| ```js | ||||
| var stream = client.createStream(); | ||||
| file.pipe(stream); | ||||
| ``` | ||||
|  | ||||
| `client.send` is a helper function for doing just that when it takes a stream parameter, note that behavior is different for other parameter types. [See API reference](https://github.com/binaryjs/binaryjs/blob/master/doc/api.md#clientsenddata-meta). | ||||
|  | ||||
| That's it for the server! | ||||
|  | ||||
| ## Create the client | ||||
| [View complete index.html source](https://github.com/binaryjs/binaryjs/blob/master/examples/helloworld/index.html) | ||||
|  | ||||
| Make sure to include the client library, served by the cdn | ||||
|  | ||||
| ```html | ||||
| <script src="http://cdn.binaryjs.com/0/binary.js"></script> | ||||
| ``` | ||||
|  | ||||
| Now here's our client side Javascript so we can receive that file | ||||
|  | ||||
| First we connection the server we just created | ||||
|  | ||||
| ```js | ||||
| var client = new BinaryClient('ws://localhost:9000'); | ||||
| ``` | ||||
|  | ||||
| Then we'll add a callback for the `stream` event, which is emitted when the server sends us the flower file stream. | ||||
|  | ||||
| ```js | ||||
| client.on('stream', function(stream, meta){     | ||||
|   // | ||||
| }); | ||||
| ``` | ||||
|  | ||||
| Now to finish up, inside that callback we need to do several things: | ||||
| * Create an array to hold the parts of the image | ||||
| * Add to array on the stream `data` event | ||||
| * Create an `<img>` element and set the src to the data | ||||
|  | ||||
| ```js | ||||
| client.on('stream', function(stream, meta){     | ||||
|   	var parts = []; | ||||
| 	stream.on('data', function(data){ | ||||
| 	  parts.push(data); | ||||
| 	}); | ||||
| 	stream.on('end', function(){ | ||||
| 	  var img = document.createElement("img"); | ||||
| 	  img.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts)); | ||||
| 	  document.body.appendChild(img); | ||||
| 	}); | ||||
| }); | ||||
| ``` | ||||
|  | ||||
| ## Integrating with an existing node app | ||||
| BinaryServer can listen on an existing [http.Server](http://nodejs.org/api/http.html#http_class_http_server) by specifying the `server` parameter in the options. See [fileupload](https://github.com/binaryjs/binaryjs/tree/master/examples/fileupload) example of this. | ||||
|  | ||||
| ```js | ||||
| var http = require('http'); | ||||
| var server = http.createServer(function (req, res) { | ||||
|   res.writeHead(200, {'Content-Type': 'text/plain'}); | ||||
|   res.end('Hello World\n'); | ||||
| }).listen(9000); | ||||
|  | ||||
| var BinaryServer = require('binaryjs').BinaryServer, | ||||
|     fs = require('fs'); | ||||
|  | ||||
| // Create a BinaryServer attached to our existing server | ||||
| var binaryserver = new BinaryServer({server: server, path: '/binary-endpoint'}); | ||||
|  | ||||
| binaryserver.on('connection', function(client){ | ||||
|   var file = fs.createReadStream(__dirname + '/flower.png'); | ||||
|   client.send(file); | ||||
| }); | ||||
| ``` | ||||
|  | ||||
| Note that we've also supplied `path: 'binary-endpoint'` to set an endpoint that doesn't clash with the original application.  When creating the client you can connect to this endpoint with: | ||||
|  | ||||
| ```js | ||||
| var client = new BinaryClient('ws://localhost:9000/binary-endpoint'); | ||||
| ``` | ||||
|  | ||||
| ### Express.js | ||||
|  | ||||
| If your app runs on express js - the normal `app.listen(9000)` won't give you access to the base http server, instead you should create the a server and pass the express app as a request listener: | ||||
|  | ||||
| ```js | ||||
| var http = require('http'); | ||||
| var app = require('express')(); | ||||
|  | ||||
| // create a server with the express app as a listener | ||||
| var server = http.createServer(app).listen(9000); | ||||
|  | ||||
| // attach BinaryServer to the base http server | ||||
| ``` | ||||
|  | ||||
|  | ||||
| ## Running the example | ||||
| Run the server, and open index.html in your favorite binary socket compatible browser, and you'll see a lovely flower! | ||||
|  | ||||
| ## Additonal details | ||||
| This example shows you how to include the server and client BinaryJS libraries and stream your first piece of data.  | ||||
|  | ||||
| BinaryJS is extremely flexible. You aren't limited to sending only binary data. In fact you can send any JSON compatible type and take advantage of the compact BinaryPack serialization format. | ||||
|  | ||||
| You aren't limited to piping streams around either. You can create as many streams as you want for each client, and send data bidirectionally, whether its a large buffer at once, chunked, or just strings and numbers and arrays and hashes. | ||||
|  | ||||
| [Check out the API reference](https://github.com/binaryjs/binaryjs/blob/master/doc/api.md) | ||||
|  | ||||
| [Ask questions on the mailing list](http://groups.google.com/group/binaryjs-group) | ||||
							
								
								
									
										21
									
								
								node_modules/binaryjs/examples/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								node_modules/binaryjs/examples/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| ## BinaryJS Examples | ||||
|  | ||||
| ### [Hello World](https://github.com/binaryjs/binaryjs/tree/master/examples/helloworld) | ||||
|  | ||||
| [View live demo](http://examples.binaryjs.com/hw.html) | ||||
|  | ||||
| Simple example that streams a flower picture to the client | ||||
|  | ||||
| ### [Streaming File Upload](https://github.com/binaryjs/binaryjs/tree/master/examples/fileupload) | ||||
|  | ||||
| HTML5 drag and drop file upload with percentage progress | ||||
|  | ||||
| ### [Image Share](https://github.com/binaryjs/binaryjs/tree/master/examples/imageshare) | ||||
|  | ||||
| Upload and broadcast images to other browsers (via file input) | ||||
|  | ||||
| ### [ASCII Webcam](https://github.com/ericz/ascam) | ||||
|  | ||||
| [View live demo](http://ascam.ericzhang.com/) | ||||
|  | ||||
| [Requires Chrome 21+] Stream webcam video in ASCII (or not) without flash! | ||||
							
								
								
									
										19
									
								
								node_modules/binaryjs/examples/fileupload/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								node_modules/binaryjs/examples/fileupload/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | ||||
| ## Drag and Drop File Upload Example w/ Percentage completion | ||||
|  | ||||
|  | ||||
| 1. Run the server: | ||||
| ```   | ||||
| node server.js | ||||
| ```  | ||||
|  | ||||
| 2. Go to | ||||
| ``` | ||||
| http://localhost:9000/ | ||||
| ```  | ||||
|  | ||||
| 3. Drag a file into the box. Percent transferred will be shown and the file will be saved in the example's directory. | ||||
|  | ||||
|  | ||||
|  | ||||
| Server code is contained in `server.js` | ||||
| Client side code is contained in `public/index.html` | ||||
							
								
								
									
										46
									
								
								node_modules/binaryjs/examples/fileupload/public/index.html
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								node_modules/binaryjs/examples/fileupload/public/index.html
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,46 @@ | ||||
| <html> | ||||
| <head> | ||||
|   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>  | ||||
|   <script src="http://cdn.binaryjs.com/0/binary.js"></script> | ||||
|   <script> | ||||
|     var client = new BinaryClient('ws://localhost:9000'); | ||||
|      | ||||
|     // Wait for connection to BinaryJS server | ||||
|     client.on('open', function(){ | ||||
|       var box = $('#box'); | ||||
|       box.on('dragenter', doNothing); | ||||
|       box.on('dragover', doNothing); | ||||
|       box.text('Drag files here'); | ||||
|       box.on('drop', function(e){ | ||||
|         e.originalEvent.preventDefault(); | ||||
|         var file = e.originalEvent.dataTransfer.files[0]; | ||||
|          | ||||
|         // Add to list of uploaded files | ||||
|         $('<div align="center"></div>').append($('<a></a>').text(file.name).prop('href', '/'+file.name)).appendTo('body'); | ||||
|          | ||||
|         // `client.send` is a helper function that creates a stream with the  | ||||
|         // given metadata, and then chunks up and streams the data. | ||||
|         var stream = client.send(file, {name: file.name, size: file.size}); | ||||
|          | ||||
|         // Print progress | ||||
|         var tx = 0; | ||||
|         stream.on('data', function(data){ | ||||
|           $('#progress').text(Math.round(tx+=data.rx*100) + '% complete'); | ||||
|         }); | ||||
|       });  | ||||
|     }); | ||||
|      | ||||
|     // Deal with DOM quirks | ||||
|     function doNothing (e){ | ||||
|       e.preventDefault(); | ||||
|       e.stopPropagation(); | ||||
|     } | ||||
|  </script> | ||||
| </head> | ||||
| <body> | ||||
| <div id="progress" align="center">0% complete</div> | ||||
| <div id="box" style="background: #eee; font-size: 26px; width: 400px; height: 300px;line-height: 300px; margin: 0 auto; text-align: center;"> | ||||
|   Connecting... | ||||
| </div> | ||||
| </body> | ||||
| </html> | ||||
							
								
								
									
										34
									
								
								node_modules/binaryjs/examples/fileupload/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								node_modules/binaryjs/examples/fileupload/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,34 @@ | ||||
| var fs = require('fs'); | ||||
| var http = require('http'); | ||||
|  | ||||
| // Serve client side statically | ||||
| var express = require('express'); | ||||
| var app = express(); | ||||
| app.use(express.static(__dirname + '/public')); | ||||
|  | ||||
| var server = http.createServer(app); | ||||
|  | ||||
| // Start Binary.js server | ||||
| var BinaryServer = require('../../').BinaryServer; | ||||
| var bs = BinaryServer({server: server}); | ||||
|  | ||||
| // Wait for new user connections | ||||
| bs.on('connection', function(client){ | ||||
|   // Incoming stream from browsers | ||||
|   client.on('stream', function(stream, meta){ | ||||
|     // | ||||
|     var file = fs.createWriteStream(__dirname+ '/public/' + meta.name); | ||||
|     stream.pipe(file); | ||||
|     // | ||||
|     // Send progress back | ||||
|     stream.on('data', function(data){ | ||||
|       stream.write({rx: data.length / meta.size}); | ||||
|     }); | ||||
|     // | ||||
|   }); | ||||
| }); | ||||
| // | ||||
| // | ||||
|  | ||||
| server.listen(9000); | ||||
| console.log('HTTP and BinaryJS server started on port 9000'); | ||||
							
								
								
									
										14
									
								
								node_modules/binaryjs/examples/helloworld/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								node_modules/binaryjs/examples/helloworld/README.md
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| ## Hello World example | ||||
|  | ||||
|  | ||||
| 1. Run the server: | ||||
| ```   | ||||
| node server.js | ||||
| ```  | ||||
|  | ||||
| 2. Open `index.html` in your browser | ||||
|  | ||||
| You should see a flower. If not, your browser probably doesn't support binary websockets. | ||||
|  | ||||
| Server code is contained in `server.js` | ||||
| Client side code is contained in `index.html` | ||||
							
								
								
									
										
											BIN
										
									
								
								node_modules/binaryjs/examples/helloworld/flower.png
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								node_modules/binaryjs/examples/helloworld/flower.png
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 102 KiB | 
							
								
								
									
										26
									
								
								node_modules/binaryjs/examples/helloworld/index.html
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								node_modules/binaryjs/examples/helloworld/index.html
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | ||||
| <html> | ||||
| <head> | ||||
|   <script src="http://cdn.binaryjs.com/0/binary.js"></script> | ||||
|   <script> | ||||
|     // Connect to Binary.js server | ||||
|     var client = new BinaryClient('ws://localhost:9000'); | ||||
|     // Received new stream from server! | ||||
|     client.on('stream', function(stream, meta){     | ||||
|       // Buffer for parts | ||||
|       var parts = []; | ||||
|       // Got new data | ||||
|       stream.on('data', function(data){ | ||||
|         parts.push(data); | ||||
|       }); | ||||
|       stream.on('end', function(){ | ||||
|         // Display new data in browser! | ||||
|         var img = document.createElement("img"); | ||||
|         img.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts)); | ||||
|         document.body.appendChild(img); | ||||
|       }); | ||||
|     }); | ||||
|   </script> | ||||
| </head> | ||||
| <body> | ||||
| </body> | ||||
| </html> | ||||
							
								
								
									
										12
									
								
								node_modules/binaryjs/examples/helloworld/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								node_modules/binaryjs/examples/helloworld/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| var BinaryServer = require('../../').BinaryServer; | ||||
| var fs = require('fs'); | ||||
|  | ||||
| // Start Binary.js server | ||||
| var server = BinaryServer({port: 9000}); | ||||
| // Wait for new user connections | ||||
| server.on('connection', function(client){ | ||||
|   // Stream a flower as a hello! | ||||
|   var file = fs.createReadStream(__dirname + '/flower.png'); | ||||
|   client.send(file);  | ||||
| }); | ||||
|  | ||||
							
								
								
									
										38
									
								
								node_modules/binaryjs/examples/imageshare/public/index.html
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								node_modules/binaryjs/examples/imageshare/public/index.html
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | ||||
| <!DOCTYPE html> | ||||
| <html> | ||||
| <head> | ||||
|   <script src="http://cdn.binaryjs.com/0/binary.js"></script> | ||||
| </head> | ||||
| <body style="background-size:cover"> | ||||
|  | ||||
|   <input type="file" id="fileinput" accept="image/*" capture="camera" /> | ||||
|  | ||||
|   <script type="text/javascript"> | ||||
|  | ||||
|     // connect to the same host this was served from | ||||
|     var client = new BinaryClient('ws://' + document.location.host); | ||||
|  | ||||
|     client.on('stream', function(stream, meta){ | ||||
|  | ||||
|       // collect stream data | ||||
|       var parts = []; | ||||
|       stream.on('data', function(data){ | ||||
|         parts.push(data); | ||||
|       }); | ||||
|  | ||||
|       // when finished, set it as the background image | ||||
|       stream.on('end', function(){ | ||||
|         var url = (window.URL || window.webkitURL).createObjectURL(new Blob(parts)); | ||||
|         document.body.style.backgroundImage = 'url(' + url + ')'; | ||||
|       }); | ||||
|     }); | ||||
|  | ||||
|     // listen for a file being chosen | ||||
|     fileinput.addEventListener('change', function(event){ | ||||
|         var file = event.target.files[0]; | ||||
|         client.send(file); | ||||
|     }, false); | ||||
|  | ||||
|   </script> | ||||
| </body> | ||||
| </html> | ||||
							
								
								
									
										37
									
								
								node_modules/binaryjs/examples/imageshare/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								node_modules/binaryjs/examples/imageshare/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,37 @@ | ||||
| var fs = require('fs'); | ||||
| var http = require('http'); | ||||
|  | ||||
| // Serve client side statically | ||||
| var express = require('express'); | ||||
| var app = express(); | ||||
| app.use(express.static(__dirname + '/public')); | ||||
|  | ||||
| var server = http.createServer(app); | ||||
|  | ||||
| // Start Binary.js server | ||||
| var BinaryServer = require('../../').BinaryServer; | ||||
|  | ||||
| // link it to express | ||||
| var bs = BinaryServer({server: server}); | ||||
|  | ||||
| // Wait for new user connections | ||||
| bs.on('connection', function(client){ | ||||
|  | ||||
|   // Incoming stream from browsers | ||||
|   client.on('stream', function(stream, meta){ | ||||
|  | ||||
|     // broadcast to all other clients | ||||
|     for(var id in bs.clients){ | ||||
|       if(bs.clients.hasOwnProperty(id)){ | ||||
|         var otherClient = bs.clients[id]; | ||||
|         if(otherClient != client){ | ||||
|           var send = otherClient.createStream(meta); | ||||
|           stream.pipe(send); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| }); | ||||
|  | ||||
| server.listen(9000); | ||||
| console.log('HTTP and BinaryJS server started on port 9000'); | ||||
							
								
								
									
										251
									
								
								node_modules/binaryjs/lib/client.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										251
									
								
								node_modules/binaryjs/lib/client.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,251 @@ | ||||
| // if node | ||||
| var Stream = require('stream').Stream; | ||||
| var EventEmitter = require('events').EventEmitter; | ||||
| var BufferReadStream = require('streamers').BufferReadStream; | ||||
| var WebSocket = require('streamws'); | ||||
|  | ||||
| var util = require('./util'); | ||||
| var BinaryStream = require('./stream').BinaryStream; | ||||
|  | ||||
| // end node | ||||
|  | ||||
| function BinaryClient(socket, options) { | ||||
|   if (!(this instanceof BinaryClient)) return new BinaryClient(socket, options); | ||||
|    | ||||
|   EventEmitter.call(this); | ||||
|    | ||||
|   var self = this; | ||||
|    | ||||
|   this._options = util.extend({ | ||||
|     chunkSize: 40960 | ||||
|   }, options); | ||||
|    | ||||
|   this.streams = {}; | ||||
|    | ||||
|   if(typeof socket === 'string') { | ||||
|     this._nextId = 0; | ||||
|     this._socket = new WebSocket(socket); | ||||
|   } else { | ||||
|     // Use odd numbered ids for server originated streams | ||||
|     this._nextId = 1; | ||||
|     this._socket = socket; | ||||
|   } | ||||
|    | ||||
|   this._socket.binaryType = 'arraybuffer'; | ||||
|    | ||||
|   this._socket.addEventListener('open', function(){ | ||||
|     self.emit('open'); | ||||
|   }); | ||||
|   // if node | ||||
|   this._socket.on('drain', function(){ | ||||
|     var ids = Object.keys(self.streams); | ||||
|     for (var i = 0, ii = ids.length; i < ii; i++) { | ||||
|       self.streams[ids[i]]._onDrain(); | ||||
|     } | ||||
|   }); | ||||
|   // end node | ||||
|   this._socket.addEventListener('error', function(error){ | ||||
|     var ids = Object.keys(self.streams); | ||||
|     for (var i = 0, ii = ids.length; i < ii; i++) { | ||||
|       self.streams[ids[i]]._onError(error); | ||||
|     } | ||||
|     self.emit('error', error); | ||||
|   }); | ||||
|   this._socket.addEventListener('close', function(code, message){ | ||||
|     var ids = Object.keys(self.streams); | ||||
|     for (var i = 0, ii = ids.length; i < ii; i++) { | ||||
|       self.streams[ids[i]]._onClose(); | ||||
|     } | ||||
|     self.emit('close', code, message); | ||||
|   }); | ||||
|   this._socket.addEventListener('message', function(data, flags){ | ||||
|     util.setZeroTimeout(function(){ | ||||
|    | ||||
|       // Message format | ||||
|       // [type, payload, bonus ] | ||||
|       // | ||||
|       // Reserved | ||||
|       // [ 0  , X , X ] | ||||
|       //  | ||||
|       // | ||||
|       // New stream | ||||
|       // [ 1  , Meta , new streamId ] | ||||
|       //  | ||||
|       // | ||||
|       // Data | ||||
|       // [ 2  , Data , streamId ] | ||||
|       //  | ||||
|       // | ||||
|       // Pause | ||||
|       // [ 3  , null , streamId ] | ||||
|       //  | ||||
|       // | ||||
|       // Resume | ||||
|       // [ 4  , null , streamId ] | ||||
|       //  | ||||
|       // | ||||
|       // End | ||||
|       // [ 5  , null , streamId ] | ||||
|       //  | ||||
|       // | ||||
|       // Close | ||||
|       // [ 6  , null , streamId ] | ||||
|       //  | ||||
|        | ||||
|       data = data.data; | ||||
|        | ||||
|       try { | ||||
|           data = util.unpack(data); | ||||
|       } catch (ex) { | ||||
|           return self.emit('error', new Error('Received unparsable message: ' + ex)); | ||||
|       } | ||||
|       if (!(data instanceof Array)) | ||||
|           return self.emit('error', new Error('Received non-array message')); | ||||
|       if (data.length != 3) | ||||
|           return self.emit('error', new Error('Received message with wrong part count: ' + data.length)); | ||||
|       if ('number' != typeof data[0]) | ||||
|           return self.emit('error', new Error('Received message with non-number type: ' + data[0])); | ||||
|        | ||||
|       switch(data[0]) { | ||||
|         case 0: | ||||
|           // Reserved | ||||
|           break; | ||||
|         case 1: | ||||
|           var meta = data[1]; | ||||
|           var streamId = data[2]; | ||||
|           var binaryStream = self._receiveStream(streamId); | ||||
|           self.emit('stream', binaryStream, meta); | ||||
|           break; | ||||
|         case 2: | ||||
|           var payload = data[1]; | ||||
|           var streamId = data[2]; | ||||
|           var binaryStream = self.streams[streamId]; | ||||
|           if(binaryStream) { | ||||
|             binaryStream._onData(payload); | ||||
|           } else { | ||||
|             self.emit('error', new Error('Received `data` message for unknown stream: ' + streamId)); | ||||
|           } | ||||
|           break; | ||||
|         case 3: | ||||
|           var streamId = data[2]; | ||||
|           var binaryStream = self.streams[streamId]; | ||||
|           if(binaryStream) { | ||||
|             binaryStream._onPause(); | ||||
|           } else { | ||||
|             self.emit('error', new Error('Received `pause` message for unknown stream: ' + streamId)); | ||||
|           } | ||||
|           break; | ||||
|         case 4: | ||||
|           var streamId = data[2]; | ||||
|           var binaryStream = self.streams[streamId]; | ||||
|           if(binaryStream) { | ||||
|             binaryStream._onResume(); | ||||
|           } else { | ||||
|             self.emit('error', new Error('Received `resume` message for unknown stream: ' + streamId)); | ||||
|           } | ||||
|           break; | ||||
|         case 5: | ||||
|           var streamId = data[2]; | ||||
|           var binaryStream = self.streams[streamId]; | ||||
|           if(binaryStream) { | ||||
|             binaryStream._onEnd(); | ||||
|           } else { | ||||
|             self.emit('error', new Error('Received `end` message for unknown stream: ' + streamId)); | ||||
|           } | ||||
|           break; | ||||
|         case 6: | ||||
|           var streamId = data[2]; | ||||
|           var binaryStream = self.streams[streamId]; | ||||
|           if(binaryStream) { | ||||
|             binaryStream._onClose(); | ||||
|           } else { | ||||
|             self.emit('error', new Error('Received `close` message for unknown stream: ' + streamId)); | ||||
|           } | ||||
|           break; | ||||
|         default: | ||||
|           self.emit('error', new Error('Unrecognized message type received: ' + data[0])); | ||||
|       } | ||||
|     }); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| util.inherits(BinaryClient, EventEmitter); | ||||
|  | ||||
| BinaryClient.prototype.send = function(data, meta){ | ||||
|   var stream = this.createStream(meta); | ||||
|   if(data instanceof Stream) { | ||||
|     data.pipe(stream); | ||||
|   } else if (util.isNode === true) { | ||||
|     if(Buffer.isBuffer(data)) { | ||||
|       (new BufferReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream); | ||||
|     } else { | ||||
|       stream.write(data); | ||||
|     }  | ||||
|   } else if (util.isNode !== true) { | ||||
|     if(data.constructor == Blob || data.constructor == File) { | ||||
|       (new BlobReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream); | ||||
|     } else if (data.constructor == ArrayBuffer) { | ||||
|       var blob; | ||||
|       if(binaryFeatures.useArrayBufferView) { | ||||
|         data = new Uint8Array(data); | ||||
|       } | ||||
|       if(binaryFeatures.useBlobBuilder) { | ||||
|         var builder = new BlobBuilder(); | ||||
|         builder.append(data); | ||||
|         blob = builder.getBlob() | ||||
|       } else { | ||||
|         blob = new Blob([data]); | ||||
|       } | ||||
|       (new BlobReadStream(blob, {chunkSize: this._options.chunkSize})).pipe(stream); | ||||
|     } else if (typeof data === 'object' && 'BYTES_PER_ELEMENT' in data) { | ||||
|       var blob; | ||||
|       if(!binaryFeatures.useArrayBufferView) { | ||||
|         // Warn | ||||
|         data = data.buffer; | ||||
|       } | ||||
|       if(binaryFeatures.useBlobBuilder) { | ||||
|         var builder = new BlobBuilder(); | ||||
|         builder.append(data); | ||||
|         blob = builder.getBlob() | ||||
|       } else { | ||||
|         blob = new Blob([data]); | ||||
|       } | ||||
|       (new BlobReadStream(blob, {chunkSize: this._options.chunkSize})).pipe(stream); | ||||
|     } else { | ||||
|       stream.write(data); | ||||
|     } | ||||
|   } | ||||
|   return stream; | ||||
| }; | ||||
|  | ||||
| BinaryClient.prototype._receiveStream = function(streamId){ | ||||
|   var self = this; | ||||
|   var binaryStream = new BinaryStream(this._socket, streamId, false); | ||||
|   binaryStream.on('close', function(){ | ||||
|     delete self.streams[streamId]; | ||||
|   }); | ||||
|   this.streams[streamId] = binaryStream; | ||||
|   return binaryStream; | ||||
| }; | ||||
|  | ||||
| BinaryClient.prototype.createStream = function(meta){ | ||||
|   if(this._socket.readyState !== WebSocket.OPEN) { | ||||
|     throw new Error('Client is not yet connected or has closed'); | ||||
|     return; | ||||
|   } | ||||
|   var self = this; | ||||
|   var streamId = this._nextId; | ||||
|   this._nextId += 2; | ||||
|   var binaryStream = new BinaryStream(this._socket, streamId, true, meta); | ||||
|   binaryStream.on('close', function(){ | ||||
|     delete self.streams[streamId]; | ||||
|   }); | ||||
|   this.streams[streamId] = binaryStream; | ||||
|   return binaryStream; | ||||
| }; | ||||
|  | ||||
| BinaryClient.prototype.close = BinaryClient.prototype.destroy = function() { | ||||
|   this._socket.close(); | ||||
| }; | ||||
|  | ||||
| exports.BinaryClient = BinaryClient; | ||||
							
								
								
									
										305
									
								
								node_modules/binaryjs/lib/client/blob_stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										305
									
								
								node_modules/binaryjs/lib/client/blob_stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,305 @@ | ||||
| function BlobReadStream(source, options){ | ||||
|   Stream.call(this); | ||||
|    | ||||
|   options = util.extend({ | ||||
|       readDelay: 0, | ||||
|       paused: false | ||||
|   }, options); | ||||
|    | ||||
|   this._source = source; | ||||
|   this._start = 0; | ||||
|   this._readChunkSize = options.chunkSize || source.size; | ||||
|   this._readDelay = options.readDelay; | ||||
|    | ||||
|   this.readable = true; | ||||
|   this.paused = options.paused; | ||||
|    | ||||
|   this._read(); | ||||
| } | ||||
|  | ||||
| util.inherits(BlobReadStream, Stream); | ||||
|  | ||||
|  | ||||
| BlobReadStream.prototype.pause = function(){ | ||||
|   this.paused = true; | ||||
| }; | ||||
|  | ||||
| BlobReadStream.prototype.resume = function(){ | ||||
|   this.paused = false; | ||||
|   this._read(); | ||||
| }; | ||||
|  | ||||
| BlobReadStream.prototype.destroy = function(){ | ||||
|   this.readable = false; | ||||
|   clearTimeout(this._timeoutId); | ||||
| }; | ||||
|  | ||||
| BlobReadStream.prototype._read = function(){ | ||||
|      | ||||
|   var self = this; | ||||
|    | ||||
|   function emitReadChunk(){ | ||||
|     self._emitReadChunk(); | ||||
|   } | ||||
|    | ||||
|   var readDelay = this._readDelay; | ||||
|   if (readDelay !== 0){ | ||||
|     this._timeoutId = setTimeout(emitReadChunk, readDelay); | ||||
|   } else { | ||||
|     util.setZeroTimeout(emitReadChunk); | ||||
|   } | ||||
|      | ||||
| }; | ||||
|  | ||||
| BlobReadStream.prototype._emitReadChunk = function(){ | ||||
|      | ||||
|   if(this.paused || !this.readable) return; | ||||
|    | ||||
|   var chunkSize = Math.min(this._source.size - this._start, this._readChunkSize); | ||||
|    | ||||
|   if(chunkSize === 0){ | ||||
|       this.readable = false; | ||||
|       this.emit("end"); | ||||
|       return; | ||||
|   } | ||||
|    | ||||
|   var sourceEnd = this._start + chunkSize; | ||||
|   var chunk = (this._source.slice || this._source.webkitSlice || this._source.mozSlice).call(this._source, this._start, sourceEnd); | ||||
|    | ||||
|   this._start = sourceEnd; | ||||
|   this._read(); | ||||
|    | ||||
|   this.emit("data", chunk); | ||||
|    | ||||
| }; | ||||
|  | ||||
| /* | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| function BlobWriteStream(options){ | ||||
|      | ||||
|     stream.Stream.call(this); | ||||
|      | ||||
|     options = _.extend({ | ||||
|         onFull: onFull, | ||||
|         onEnd: function(){}, | ||||
|         minBlockAllocSize: 0, | ||||
|         drainDelay:0 | ||||
|     }, options); | ||||
|      | ||||
|     this._onFull = options.onFull; | ||||
|     this._onEnd = options.onEnd; | ||||
|     this._onWrite = options.onWrite; | ||||
|      | ||||
|     this._minBlockAllocSize = options.minBlockAllocSize; | ||||
|     this._maxBlockAllocSize = options.maxBlockAllocSize; | ||||
|     this._drainDelay = options.drainDelay; | ||||
|      | ||||
|     this._buffer = new Buffer(options.minBlockAllocSize); | ||||
|     this._destination = this._buffer; | ||||
|     this._destinationPos = 0; | ||||
|      | ||||
|     this._writeQueue = []; | ||||
|     this._pendingOnFull = false; | ||||
|     this._pendingQueueDrain = false; | ||||
|      | ||||
|     this.writable = true; | ||||
|     this.bytesWritten = 0; | ||||
| } | ||||
|  | ||||
| util.inherits(BlobWriteStream, stream.Stream); | ||||
|  | ||||
| BlobWriteStream.prototype.getBuffer = function(){ | ||||
|     return this._buffer; | ||||
| }; | ||||
|  | ||||
| BlobWriteStream.prototype.write = function(data, encoding){ | ||||
|      | ||||
|     if(!this.writable){ | ||||
|         throw new Error("stream is not writable"); | ||||
|     } | ||||
|      | ||||
|     if(!Buffer.isBuffer(data)){ | ||||
|         data = new Buffer(data, encoding); | ||||
|     } | ||||
|      | ||||
|     if(data.length){ | ||||
|         this._writeQueue.push(data); | ||||
|     } | ||||
|      | ||||
|     this._commit(); | ||||
|      | ||||
|     return this._writeQueue.length === 0; | ||||
| }; | ||||
|  | ||||
| BlobWriteStream.prototype._commit = function(){ | ||||
|      | ||||
|     var self = this; | ||||
|      | ||||
|     var destination = this._destination; | ||||
|     var writeQueue = this._writeQueue; | ||||
|      | ||||
|     var startDestinationPos = this._destinationPos; | ||||
|      | ||||
|     while(writeQueue.length && destination.length){ | ||||
|          | ||||
|         var head = writeQueue[0]; | ||||
|          | ||||
|         var copySize = Math.min(destination.length, head.length); | ||||
|          | ||||
|         head.copy(destination, 0, 0, copySize); | ||||
|          | ||||
|         head = head.slice(copySize); | ||||
|         destination = destination.slice(copySize); | ||||
|          | ||||
|         this.bytesWritten += copySize; | ||||
|         this._destinationPos += copySize; | ||||
|          | ||||
|         if(head.length === 0){ | ||||
|             writeQueue.shift(); | ||||
|         } | ||||
|         else{ | ||||
|             writeQueue[0] = head; | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     this._destination = destination; | ||||
|      | ||||
|     bytesCommitted = this._destinationPos - startDestinationPos; | ||||
|     if(bytesCommitted){ | ||||
|         if(this._onWrite){ | ||||
|              | ||||
|             if(writeQueue.length){ | ||||
|                 this._pendingQueueDrain = true; | ||||
|             } | ||||
|              | ||||
|             // By locking destination the buffer is frozen and the onWrite | ||||
|             // callback cannot miss any write commits | ||||
|             this._destination = emptyBuffer; | ||||
|              | ||||
|             var consumer = this._onWrite; | ||||
|             this._onWrite = null; | ||||
|              | ||||
|             consumer.call(this, function(nextCallback){ | ||||
|                 util.setZeroTimeout(function(){ | ||||
|                     self._destination = destination; | ||||
|                     self._onWrite = nextCallback; | ||||
|                     self._commit(); | ||||
|                 }); | ||||
|             }, consumer); | ||||
|              | ||||
|             return; | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     if(writeQueue.length){ | ||||
|          | ||||
|         this._pendingQueueDrain = true; | ||||
|         this._growBuffer(); | ||||
|     } | ||||
|     else if(this._pendingQueueDrain){ | ||||
|          | ||||
|         this._pendingQueueDrain = false; | ||||
|          | ||||
|         if(this._drainDelay !== 0){ | ||||
|             setTimeout(function(){ | ||||
|                 self.emit("drain"); | ||||
|             }, this._drainDelay); | ||||
|         } | ||||
|         else{ | ||||
|             util.setZeroTimeout(function(){ | ||||
|                 self.emit("drain"); | ||||
|             }); | ||||
|         } | ||||
|     } | ||||
| }; | ||||
|  | ||||
| BlobWriteStream.prototype._growBuffer = function(){ | ||||
|      | ||||
|     var self = this; | ||||
|     var writeQueue = this._writeQueue; | ||||
|      | ||||
|     var requestSize = this._minBlockAllocSize; | ||||
|      | ||||
|     var maxBlockAllocSize = this._maxBlockAllocSize; | ||||
|     var add = (maxBlockAllocSize === undefined ? function(a, b){return a + b;} : function(a, b){return Math.min(a + b, maxBlockAllocSize);}); | ||||
|      | ||||
|     for(var i = 0, queueLength = writeQueue.length; i < queueLength; i++){ | ||||
|         requestSize = add(requestSize, writeQueue[i].length); | ||||
|     } | ||||
|      | ||||
|     // Prevent concurrent onFull callbacks | ||||
|     if(this._pendingOnFull){ | ||||
|         return; | ||||
|     } | ||||
|     this._pendingOnFull = true; | ||||
|      | ||||
|     this._onFull(this._buffer, requestSize, function(buffer, destination){ | ||||
|         util.setZeroTimeout(function(){ | ||||
|              | ||||
|             self._pendingOnFull = false; | ||||
|              | ||||
|             if(!destination){ | ||||
|                 if(self.writable){ | ||||
|                     self.emit("error", new Error("buffer is full")); | ||||
|                 } | ||||
|                 self.destroy(); | ||||
|                 return; | ||||
|             } | ||||
|              | ||||
|             self._buffer = buffer; | ||||
|             self._destination = destination; | ||||
|              | ||||
|             self._commit(); | ||||
|         }); | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| BlobWriteStream.prototype.end = function(data, encoding){ | ||||
|      | ||||
|     var self = this; | ||||
|      | ||||
|     function _end(){ | ||||
|         self.writable = false; | ||||
|         self._onEnd(); | ||||
|     } | ||||
|      | ||||
|     if(data){ | ||||
|         if(this.write(data, encoding)){ | ||||
|             _end(); | ||||
|         }else{ | ||||
|             self.writable = false; | ||||
|             this.once("drain", _end); | ||||
|         } | ||||
|     } | ||||
|     else{ | ||||
|         _end(); | ||||
|     } | ||||
| }; | ||||
|  | ||||
| BlobWriteStream.prototype.destroy = function(){ | ||||
|     this.writable = false; | ||||
|     this._pendingQueueDrain = false; | ||||
|     this._writeQueue = []; | ||||
| }; | ||||
|  | ||||
| BlobWriteStream.prototype.consume = function(consume){ | ||||
|      | ||||
|     this._buffer = this._buffer.slice(consume); | ||||
|     this._destinationPos -= consume; | ||||
| }; | ||||
|  | ||||
| BlobWriteStream.prototype.getCommittedSlice = function(){ | ||||
|     return this._buffer.slice(0, this._destinationPos); | ||||
| }; | ||||
|  | ||||
| function onFull(buffer, extraSize, callback){ | ||||
|     var newBuffer = new Buffer(buffer.length + extraSize); | ||||
|     buffer.copy(newBuffer); | ||||
|     callback(newBuffer, newBuffer.slice(buffer.length)); | ||||
| } | ||||
| */ | ||||
| exports.BlobReadStream = BlobReadStream; | ||||
| exports.BlobWriteStream = BlobWriteStream; | ||||
							
								
								
									
										92
									
								
								node_modules/binaryjs/lib/client/stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								node_modules/binaryjs/lib/client/stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,92 @@ | ||||
|  | ||||
|  | ||||
| function Stream() { | ||||
|   EventEmitter.call(this); | ||||
| } | ||||
|  | ||||
| util.inherits(Stream, EventEmitter); | ||||
|  | ||||
| Stream.prototype.pipe = function(dest, options) { | ||||
|   var source = this; | ||||
|  | ||||
|   function ondata(chunk) { | ||||
|     if (dest.writable) { | ||||
|       if (false === dest.write(chunk) && source.pause) { | ||||
|         source.pause(); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   source.on('data', ondata); | ||||
|  | ||||
|   function ondrain() { | ||||
|     if (source.readable && source.resume) { | ||||
|       source.resume(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   dest.on('drain', ondrain); | ||||
|  | ||||
|   // If the 'end' option is not supplied, dest.end() will be called when | ||||
|   // source gets the 'end' or 'close' events.  Only dest.end() once. | ||||
|   if (!dest._isStdio && (!options || options.end !== false)) { | ||||
|     source.on('end', onend); | ||||
|     source.on('close', onclose); | ||||
|   } | ||||
|  | ||||
|   var didOnEnd = false; | ||||
|   function onend() { | ||||
|     if (didOnEnd) return; | ||||
|     didOnEnd = true; | ||||
|  | ||||
|     dest.end(); | ||||
|   } | ||||
|  | ||||
|  | ||||
|   function onclose() { | ||||
|     if (didOnEnd) return; | ||||
|     didOnEnd = true; | ||||
|  | ||||
|     dest.destroy(); | ||||
|   } | ||||
|  | ||||
|   // don't leave dangling pipes when there are errors. | ||||
|   function onerror(er) { | ||||
|     cleanup(); | ||||
|     if (this.listeners('error').length === 0) { | ||||
|       throw er; // Unhandled stream error in pipe. | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   source.on('error', onerror); | ||||
|   dest.on('error', onerror); | ||||
|  | ||||
|   // remove all the event listeners that were added. | ||||
|   function cleanup() { | ||||
|     source.removeListener('data', ondata); | ||||
|     dest.removeListener('drain', ondrain); | ||||
|  | ||||
|     source.removeListener('end', onend); | ||||
|     source.removeListener('close', onclose); | ||||
|  | ||||
|     source.removeListener('error', onerror); | ||||
|     dest.removeListener('error', onerror); | ||||
|  | ||||
|     source.removeListener('end', cleanup); | ||||
|     source.removeListener('close', cleanup); | ||||
|  | ||||
|     dest.removeListener('end', cleanup); | ||||
|     dest.removeListener('close', cleanup); | ||||
|   } | ||||
|  | ||||
|   source.on('end', cleanup); | ||||
|   source.on('close', cleanup); | ||||
|  | ||||
|   dest.on('end', cleanup); | ||||
|   dest.on('close', cleanup); | ||||
|  | ||||
|   dest.emit('pipe', source); | ||||
|  | ||||
|   // Allow for unix-like usage: A.pipe(B).pipe(C) | ||||
|   return dest; | ||||
| }; | ||||
							
								
								
									
										76
									
								
								node_modules/binaryjs/lib/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								node_modules/binaryjs/lib/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,76 @@ | ||||
| var ws = require('streamws'); | ||||
| var EventEmitter = require('events').EventEmitter; | ||||
| var util = require('./util'); | ||||
|  | ||||
| var BinaryClient = require('./client').BinaryClient; | ||||
|  | ||||
| function BinaryServer(options) { | ||||
|   if (!(this instanceof BinaryServer)) return new BinaryServer(options); | ||||
|  | ||||
|   var self = this; | ||||
|  | ||||
|   options = util.extend({ | ||||
|     host: '0.0.0.0', | ||||
|     chunkSize: 40960 | ||||
|   }, options); | ||||
|  | ||||
|   this.clients = {}; | ||||
|   this._clientCounter = 0; | ||||
|  | ||||
|    | ||||
|   if (options.server && (options.server instanceof ws.Server)) { | ||||
|     this._server  = options.server; | ||||
|   } else { | ||||
|     this._server  = new ws.Server(options); | ||||
|   } | ||||
|    | ||||
|   this._server.on('connection', function(socket){ | ||||
|     var clientId = self._clientCounter; | ||||
|     var binaryClient = new BinaryClient(socket, options); | ||||
|     binaryClient.id = clientId; | ||||
|     self.clients[clientId] = binaryClient; | ||||
|     self._clientCounter++; | ||||
|     binaryClient.on('close', function(){ | ||||
|       delete self.clients[clientId]; | ||||
|     }); | ||||
|     self.emit('connection', binaryClient); | ||||
|   }); | ||||
|   this._server.on('error', function(error){ | ||||
|     self.emit('error', error); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| util.inherits(BinaryServer, EventEmitter); | ||||
|  | ||||
| BinaryServer.prototype.close = function(code, message){ | ||||
|   this._server.close(code, message); | ||||
| } | ||||
|  | ||||
| exports.BinaryClient = BinaryClient; | ||||
| exports.BinaryServer = BinaryServer; | ||||
|  | ||||
| // Expose a method similar to http.createServer | ||||
| // | ||||
| // usage: | ||||
| //  var fs = require('fs'); | ||||
| //  require('binaryjs').createServer(function(c) { | ||||
| //    fs.createReadStream('some.bin').pipe(c); | ||||
| //  }); | ||||
| // | ||||
| exports.createServer = function(fn) { | ||||
|   var server; | ||||
|  | ||||
|   return { | ||||
|     listen : function(port, opts) { | ||||
|       opts = opts || {}; | ||||
|       opts.port = port; | ||||
|  | ||||
|       var server = new BinaryServer(opts); | ||||
|       server.on('connection', function(conn) { | ||||
|         fn && fn(conn.createStream()); | ||||
|       }); | ||||
|  | ||||
|       return server; | ||||
|     } | ||||
|   } | ||||
| }; | ||||
							
								
								
									
										128
									
								
								node_modules/binaryjs/lib/stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								node_modules/binaryjs/lib/stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,128 @@ | ||||
| // if node | ||||
| var Stream = require('stream').Stream; | ||||
| var util = require('./util'); | ||||
| // end node | ||||
|  | ||||
| function BinaryStream(socket, id, create, meta) { | ||||
|   if (!(this instanceof BinaryStream)) return new BinaryStream(options); | ||||
|    | ||||
|   var self = this; | ||||
|    | ||||
|   Stream.call(this); | ||||
|  | ||||
|    | ||||
|   this.id = id; | ||||
|   this._socket = socket; | ||||
|      | ||||
|   this.writable = true; | ||||
|   this.readable = true; | ||||
|   this.paused = false; | ||||
|    | ||||
|   this._closed = false; | ||||
|   this._ended = false; | ||||
|    | ||||
|   if(create) { | ||||
|     // This is a stream we are creating | ||||
|     this._write(1, meta, this.id); | ||||
|   } | ||||
| } | ||||
|  | ||||
| util.inherits(BinaryStream, Stream); | ||||
|  | ||||
|  | ||||
| BinaryStream.prototype._onDrain = function() { | ||||
|   if(!this.paused) { | ||||
|     this.emit('drain'); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype._onClose = function() { | ||||
|   // Emit close event | ||||
|   if (this._closed) { | ||||
|     return; | ||||
|   } | ||||
|   this.readable = false; | ||||
|   this.writable = false; | ||||
|   this._closed = true; | ||||
|   this.emit('close'); | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype._onError = function(error){ | ||||
|   this.readable = false; | ||||
|   this.writable = false; | ||||
|   this.emit('error', error); | ||||
| }; | ||||
|  | ||||
| // Write stream | ||||
|  | ||||
| BinaryStream.prototype._onPause = function() { | ||||
|   // Emit pause event | ||||
|   this.paused = true; | ||||
|   this.emit('pause'); | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype._onResume = function() { | ||||
|   // Emit resume event | ||||
|   this.paused = false; | ||||
|   this.emit('resume'); | ||||
|   this.emit('drain'); | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype._write = function(code, data, bonus) { | ||||
|   if (this._socket.readyState !== this._socket.constructor.OPEN) { | ||||
|     return false; | ||||
|   } | ||||
|   var message = util.pack([code, data, bonus]); | ||||
|   return this._socket.send(message) !== false; | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype.write = function(data) { | ||||
|   if(this.writable) { | ||||
|     var out = this._write(2, data, this.id); | ||||
|     return !this.paused && out; | ||||
|   } else { | ||||
|     throw new Error('Stream is not writable'); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype.end = function() { | ||||
|   this._ended = true; | ||||
|   this.readable = false; | ||||
|   this._write(5, null, this.id); | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype.destroy = BinaryStream.prototype.destroySoon = function() { | ||||
|   this._onClose(); | ||||
|   this._write(6, null, this.id); | ||||
| }; | ||||
|  | ||||
|  | ||||
| // Read stream | ||||
|  | ||||
| BinaryStream.prototype._onEnd = function() { | ||||
|   if(this._ended) {  | ||||
|     return; | ||||
|   } | ||||
|   this._ended = true; | ||||
|   this.readable = false; | ||||
|   this.emit('end'); | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype._onData = function(data) { | ||||
|   // Dispatch  | ||||
|   this.emit('data', data); | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype.pause = function() { | ||||
|   this._onPause(); | ||||
|   this._write(3, null, this.id); | ||||
| }; | ||||
|  | ||||
| BinaryStream.prototype.resume = function() { | ||||
|   this._onResume(); | ||||
|   this._write(4, null, this.id); | ||||
| }; | ||||
|  | ||||
| // if node | ||||
| exports.BinaryStream = BinaryStream; | ||||
| // end node | ||||
							
								
								
									
										68
									
								
								node_modules/binaryjs/lib/util.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								node_modules/binaryjs/lib/util.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,68 @@ | ||||
| // if node | ||||
| var BinaryPack = require('binarypack'); | ||||
| // end node | ||||
|  | ||||
| var util = { | ||||
|   inherits: function(ctor, superCtor) { | ||||
|     ctor.super_ = superCtor; | ||||
|     ctor.prototype = Object.create(superCtor.prototype, { | ||||
|       constructor: { | ||||
|         value: ctor, | ||||
|         enumerable: false, | ||||
|         writable: true, | ||||
|         configurable: true | ||||
|       } | ||||
|     }); | ||||
|   }, | ||||
|   extend: function(dest, source) { | ||||
|     for(var key in source) { | ||||
|       if(source.hasOwnProperty(key)) { | ||||
|         dest[key] = source[key]; | ||||
|       } | ||||
|     } | ||||
|     return dest; | ||||
|   }, | ||||
|   pack: BinaryPack.pack, | ||||
|   unpack: BinaryPack.unpack, | ||||
|   // if node | ||||
|   isNode: true, | ||||
|   // end node | ||||
|   setZeroTimeout: (function(global) { | ||||
|     // if node | ||||
|      | ||||
|     return process.nextTick; | ||||
|      | ||||
|     // end node | ||||
|     var timeouts = []; | ||||
|     var messageName = 'zero-timeout-message'; | ||||
|  | ||||
|     // Like setTimeout, but only takes a function argument.	 There's | ||||
|     // no time argument (always zero) and no arguments (you have to | ||||
|     // use a closure). | ||||
|     function setZeroTimeoutPostMessage(fn) { | ||||
|       timeouts.push(fn); | ||||
|       global.postMessage(messageName, '*'); | ||||
|     }		 | ||||
|  | ||||
|     function handleMessage(event) { | ||||
|       if (event.source == global && event.data == messageName) { | ||||
|         if (event.stopPropagation) { | ||||
|           event.stopPropagation(); | ||||
|         } | ||||
|         if (timeouts.length) { | ||||
|           timeouts.shift()(); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|     if (global.addEventListener) { | ||||
|       global.addEventListener('message', handleMessage, true); | ||||
|     } else if (global.attachEvent) { | ||||
|       global.attachEvent('onmessage', handleMessage); | ||||
|     } | ||||
|     return setZeroTimeoutPostMessage; | ||||
|   }(this)) | ||||
| }; | ||||
|  | ||||
| // if node | ||||
| module.exports = util; | ||||
| // end node | ||||
							
								
								
									
										27
									
								
								node_modules/binaryjs/package.json
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								node_modules/binaryjs/package.json
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| { | ||||
|   "author": "Eric Zhang (http://ericzhang.com)", | ||||
|   "name": "binaryjs", | ||||
|   "description": "Binary realtime streaming made easy", | ||||
|   "version": "0.2.1", | ||||
|   "homepage": "http://binaryjs.com", | ||||
|   "repository": { | ||||
|     "type": "git", | ||||
|     "url": "git://github.com/binaryjs/binaryjs.git" | ||||
|   }, | ||||
|   "main": "lib/server.js", | ||||
|   "scripts": { | ||||
|     "test": "make test" | ||||
|   }, | ||||
|   "engines": { | ||||
|     "node": ">=0.6.0" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "streamws": ">=0.1.1", | ||||
|     "binarypack": ">=0.0.4", | ||||
|     "streamers": ">=0.1.0" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "mocha": "~1.3.0", | ||||
|     "uglify-js": "~1.3.5" | ||||
|   } | ||||
| } | ||||
							
								
								
									
										108
									
								
								node_modules/binaryjs/test/client.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								node_modules/binaryjs/test/client.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,108 @@ | ||||
| var assert = require('assert'); | ||||
| var binaryjs = require('../'); | ||||
| var BinaryServer = binaryjs.BinaryServer; | ||||
| var BinaryClient = binaryjs.BinaryClient; | ||||
| var http = require('http'); | ||||
|  | ||||
| var server, client, serverUrl = 'ws://localhost:9101'; | ||||
|  | ||||
| describe('BinaryClient', function(){ | ||||
|   beforeEach(function(){ | ||||
|     server = new BinaryServer({port: 9101}); | ||||
|   }); | ||||
|   afterEach(function(){ | ||||
|     server.close(); | ||||
|   }); | ||||
|   describe('events for clients', function(){ | ||||
|     it('should be opennable and closeable', function(done){ | ||||
|       server.on('connection', function(client){ | ||||
|         client.on('close', function(){ | ||||
|           done(); | ||||
|         }); | ||||
|       }); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('open', function(){ | ||||
|         client.close(); | ||||
|       }); | ||||
|     }); | ||||
|     it('should receive streams', function(done){ | ||||
|       server.on('connection', function(client){ | ||||
|         client.on('stream', function(){ | ||||
|           done(); | ||||
|         }); | ||||
|       }); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('open', function(){ | ||||
|        | ||||
|         client.createStream(); | ||||
|       }); | ||||
|     }); | ||||
|   }); | ||||
|    | ||||
|   describe('sending data', function(){ | ||||
|     it('should be able to send buffers', function(done){ | ||||
|       var string = 'test'; | ||||
|       server.on('connection', function(client){ | ||||
|         client.on('stream', function(stream){ | ||||
|           stream.on('data', function(data){ | ||||
|             assert.equal(data.toString(), string); | ||||
|             done(); | ||||
|           }); | ||||
|         }); | ||||
|       }); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('open', function(){ | ||||
|         client.send(new Buffer(string)); | ||||
|       });   | ||||
|     }); | ||||
|   }); | ||||
|   describe('.streams', function(){ | ||||
|     it('should contain a list of streams', function(done){ | ||||
|       server.on('connection', function(client){ | ||||
|         var i = 0; | ||||
|         var startLength = Object.keys(client.streams).length; | ||||
|         client.on('stream', function(stream){ | ||||
|           assert.equal(client.streams[stream.id], stream); | ||||
|           if(++i == 4) { | ||||
|             var endLength = Object.keys(client.streams).length; | ||||
|             assert.equal(endLength - startLength, i); | ||||
|             done(); | ||||
|           } | ||||
|         }); | ||||
|         var stream = client.createStream(); | ||||
|         assert.equal(client.streams[stream.id], stream); | ||||
|         i++; | ||||
|         stream = client.createStream(); | ||||
|         assert.equal(client.streams[stream.id], stream); | ||||
|         i++; | ||||
|       }); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('open', function(){ | ||||
|         client.createStream(); | ||||
|         client.createStream(); | ||||
|       }); | ||||
|     }); | ||||
|     it('should delete streams upon close event', function(done){ | ||||
|       var closed = 0; | ||||
|       server.on('connection', function(client){ | ||||
|         client.on('stream', function(stream){ | ||||
|           stream.on('close', function(){ | ||||
|             assert(!(stream.id in client.streams)); | ||||
|             done(); | ||||
|           }); | ||||
|         }); | ||||
|         var stream = client.createStream(); | ||||
|         stream.on('close', function(){ | ||||
|           assert(!(stream.id in client.streams)); | ||||
|         }); | ||||
|         stream.destroy(); | ||||
|       }); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('open', function(){ | ||||
|         var stream = client.createStream(); | ||||
|         stream.destroy(); | ||||
|       }); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
|  | ||||
							
								
								
									
										44
									
								
								node_modules/binaryjs/test/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								node_modules/binaryjs/test/server.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,44 @@ | ||||
| var assert = require('assert'); | ||||
| var binaryjs = require('../'); | ||||
| var BinaryServer = binaryjs.BinaryServer; | ||||
| var BinaryClient = binaryjs.BinaryClient; | ||||
| var http = require('http'); | ||||
|  | ||||
| var server, client, serverUrl = 'ws://localhost:9101'; | ||||
|  | ||||
| describe('BinaryServer', function(){ | ||||
|   describe('creating servers', function(){ | ||||
|     it('should allow creating servers with a port', function(){ | ||||
|       server = new BinaryServer({port: 9101}); | ||||
|     }); | ||||
|     it('should allow creating servers with an http server', function(){ | ||||
|       new BinaryServer({port: 9102}); | ||||
|     }); | ||||
|   }); | ||||
|   describe('.clients', function(){ | ||||
|     it('should contain a list of clients', function(done){ | ||||
|       var i = 0; | ||||
|       var startLength = Object.keys(server.clients).length; | ||||
|       server.on('connection', function(client){ | ||||
|         assert.equal(server.clients[client.id], client); | ||||
|         if(++i == 3) { | ||||
|           var endLength = Object.keys(server.clients).length; | ||||
|           assert.equal(endLength - startLength, i); | ||||
|           done(); | ||||
|         } | ||||
|       }); | ||||
|       new BinaryClient(serverUrl); | ||||
|       new BinaryClient(serverUrl); | ||||
|       new BinaryClient(serverUrl); | ||||
|     }); | ||||
|   }); | ||||
|   describe('.close()', function(){ | ||||
|     it('should prevent future clients connecting', function(done){ | ||||
|       server.close(); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('error', function(){ | ||||
|         done(); | ||||
|       }); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
							
								
								
									
										67
									
								
								node_modules/binaryjs/test/stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								node_modules/binaryjs/test/stream.js
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,67 @@ | ||||
| var assert = require('assert'); | ||||
| var binaryjs = require('../'); | ||||
| var BinaryServer = binaryjs.BinaryServer; | ||||
| var BinaryClient = binaryjs.BinaryClient; | ||||
| var http = require('http'); | ||||
|  | ||||
| var server, client, serverUrl = 'ws://localhost:9101'; | ||||
|  | ||||
| describe('BinaryStream', function(){ | ||||
|   beforeEach(function(){ | ||||
|     server = new BinaryServer({port: 9101}); | ||||
|   }); | ||||
|   afterEach(function(){ | ||||
|     server.close(); | ||||
|   }); | ||||
|   describe('Messaging', function(){ | ||||
|     it('should send and receive messages', function(done){ | ||||
|       server.on('connection', function(client){ | ||||
|         client.on('stream', function(stream){ | ||||
|           stream.on('data', function(){ | ||||
|             done(); | ||||
|           }); | ||||
|           stream.write('hi'); | ||||
|         }); | ||||
|       }); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('open', function(){ | ||||
|         var stream = client.createStream(); | ||||
|         stream.on('data', function(){ | ||||
|           stream.write('bye'); | ||||
|         }); | ||||
|       });  | ||||
|     }); | ||||
|     it('should send and receive pause, resume, and end', function(done){ | ||||
|       server.on('connection', function(client){ | ||||
|         client.on('stream', function(stream){ | ||||
|           stream.on('pause', function(){ | ||||
|             assert(stream.paused); | ||||
|           }); | ||||
|           stream.resume(); | ||||
|           stream.on('resume', function(){ | ||||
|             assert(!stream.paused); | ||||
|           }); | ||||
|           stream.on('end', function(){ | ||||
|             assert(!stream.readable); | ||||
|             done(); | ||||
|           }); | ||||
|         }); | ||||
|       }); | ||||
|       var client = new BinaryClient(serverUrl); | ||||
|       client.on('open', function(){ | ||||
|         var stream = client.createStream(); | ||||
|         stream.pause(); | ||||
|         stream.on('pause', function(){ | ||||
|           assert(stream.paused); | ||||
|         }); | ||||
|         stream.on('resume', function(){ | ||||
|           assert(!stream.paused); | ||||
|           stream.end(); | ||||
|         }); | ||||
|         stream.on('end', function(){ | ||||
|           assert(!stream.readable); | ||||
|         }); | ||||
|       });  | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user