From 29087e0eced8cbe0a00ea5856bd99f43b824bd3a Mon Sep 17 00:00:00 2001 From: ericz Date: Thu, 19 Dec 2013 10:21:49 -0800 Subject: [PATCH 1/5] Merge --- dist/binarypack.js | 7 ++++++- dist/binarypack.min.js | 2 +- lib/binarypack.js | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dist/binarypack.js b/dist/binarypack.js index 3ca3f7d..18462a0 100644 --- a/dist/binarypack.js +++ b/dist/binarypack.js @@ -568,7 +568,12 @@ function _utf8Replace(m){ } function utf8Length(str){ - return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length; + if (str.length > 600) { + // Blob method faster for large strings + return (new Blob([str])).size; + } else { + return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length; + } } })(this); diff --git a/dist/binarypack.min.js b/dist/binarypack.min.js index 8b98713..ece8465 100644 --- a/dist/binarypack.min.js +++ b/dist/binarypack.min.js @@ -1 +1 @@ -/*! binarypack.min.js build:0.0.7, production. Copyright(c) 2012 Eric Zhang MIT Licensed */(function(e){function n(){this._pieces=[],this._parts=[]}function r(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function i(){this.bufferBuilder=new n}function s(e){var t=e.charCodeAt(0);return t<=2047?"00":t<=65535?"000":t<=2097151?"0000":t<=67108863?"00000":"000000"}function o(e){return e.replace(/[^\u0000-\u007F]/g,s).length}var t={};t.useBlobBuilder=function(){try{return new Blob([]),!1}catch(e){return!0}}(),t.useArrayBufferView=!t.useBlobBuilder&&function(){try{return(new Blob([new Uint8Array([])])).size===0}catch(e){return!0}}(),e.binaryFeatures=t,e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,n.prototype.append=function(e){typeof e=="number"?this._pieces.push(e):(this.flush(),this._parts.push(e))},n.prototype.flush=function(){if(this._pieces.length>0){var e=new Uint8Array(this._pieces);t.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},n.prototype.getBuffer=function(){this.flush();if(t.useBlobBuilder){var e=new BlobBuilder;for(var n=0,r=this._parts.length;n>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},r.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},r.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},i.prototype.getBuffer=function(){return this.bufferBuilder.getBuffer()},i.prototype.pack=function(e){var n=typeof e;if(n=="string")this.pack_string(e);else if(n=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(n=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(n=="undefined")this.bufferBuilder.append(192);else{if(n!="object")throw new Error('Type "'+n+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var r=e.constructor;if(r==Array)this.pack_array(e);else if(r==Blob||r==File)this.pack_bin(e);else if(r==ArrayBuffer)t.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)t.useArrayBufferView?this.pack_bin(new Uint8Array(e.buffer)):this.pack_bin(e.buffer);else if(r==Object)this.pack_object(e);else if(r==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+r.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}this.bufferBuilder.flush()},i.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_string=function(e){var t=o(e);if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n>8),this.bufferBuilder.append(e&255)},i.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},i.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},i.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},i.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)}})(this) \ No newline at end of file +/*! binarypack.min.js build:0.0.7, production. Copyright(c) 2012 Eric Zhang MIT Licensed */(function(e){function n(){this._pieces=[],this._parts=[]}function r(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function i(){this.bufferBuilder=new n}function s(e){var t=e.charCodeAt(0);return t<=2047?"00":t<=65535?"000":t<=2097151?"0000":t<=67108863?"00000":"000000"}function o(e){return e.length>600?(new Blob([e])).size:e.replace(/[^\u0000-\u007F]/g,s).length}var t={};t.useBlobBuilder=function(){try{return new Blob([]),!1}catch(e){return!0}}(),t.useArrayBufferView=!t.useBlobBuilder&&function(){try{return(new Blob([new Uint8Array([])])).size===0}catch(e){return!0}}(),e.binaryFeatures=t,e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,n.prototype.append=function(e){typeof e=="number"?this._pieces.push(e):(this.flush(),this._parts.push(e))},n.prototype.flush=function(){if(this._pieces.length>0){var e=new Uint8Array(this._pieces);t.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},n.prototype.getBuffer=function(){this.flush();if(t.useBlobBuilder){var e=new BlobBuilder;for(var n=0,r=this._parts.length;n>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},r.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},r.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},i.prototype.getBuffer=function(){return this.bufferBuilder.getBuffer()},i.prototype.pack=function(e){var n=typeof e;if(n=="string")this.pack_string(e);else if(n=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(n=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(n=="undefined")this.bufferBuilder.append(192);else{if(n!="object")throw new Error('Type "'+n+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var r=e.constructor;if(r==Array)this.pack_array(e);else if(r==Blob||r==File)this.pack_bin(e);else if(r==ArrayBuffer)t.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)t.useArrayBufferView?this.pack_bin(new Uint8Array(e.buffer)):this.pack_bin(e.buffer);else if(r==Object)this.pack_object(e);else if(r==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+r.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}this.bufferBuilder.flush()},i.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_string=function(e){var t=o(e);if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n>8),this.bufferBuilder.append(e&255)},i.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},i.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},i.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},i.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)}})(this) \ No newline at end of file diff --git a/lib/binarypack.js b/lib/binarypack.js index e9cd516..3eab23c 100644 --- a/lib/binarypack.js +++ b/lib/binarypack.js @@ -508,5 +508,10 @@ function _utf8Replace(m){ } function utf8Length(str){ - return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length; + if (str.length > 600) { + // Blob method faster for large strings + return (new Blob([str])).size; + } else { + return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length; + } } From 22e161e3ccb4e20f7f6c14bbaff65c18c21d86d1 Mon Sep 17 00:00:00 2001 From: ericz Date: Thu, 19 Dec 2013 10:31:45 -0800 Subject: [PATCH 2/5] Sync package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fef7f36..7a797ee 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.7", "description": "BinaryPack serialization for the web browser", "homepage": "https://github.com/binaryjs/js-binarypack", + "main": "./dist/binarypack.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -11,7 +12,6 @@ "url": "git://github.com/binaryjs/js-binarypack.git" }, "author": "Eric Zhang", - "main": "index.js", "bin": { "js-binarypack": "build.js" }, From 24e75d5ec9ee9e7ac3a032046451aec38cc69526 Mon Sep 17 00:00:00 2001 From: lmb Date: Sat, 9 Aug 2014 10:52:55 +0200 Subject: [PATCH 3/5] Properly export BinaryPack --- lib/binarypack.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/binarypack.js b/lib/binarypack.js index 3eab23c..01b0cc5 100644 --- a/lib/binarypack.js +++ b/lib/binarypack.js @@ -1,4 +1,4 @@ -exports.BinaryPack = { +BinaryPack = { unpack: function(data){ var unpacker = new Unpacker(data); return unpacker.unpack(); @@ -11,6 +11,8 @@ exports.BinaryPack = { } }; +exports.BinaryPack = BinaryPack; + function Unpacker (data){ // Data is ArrayBuffer this.index = 0; From 99689d19b815bfde973120bc7f48a303ae886ac8 Mon Sep 17 00:00:00 2001 From: lmb Date: Fri, 15 Aug 2014 14:53:56 +0100 Subject: [PATCH 4/5] Make exports browserify compliant --- dist/binarypack.js | 11 ++++++----- lib/bufferbuilder.js | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dist/binarypack.js b/dist/binarypack.js index 18462a0..145aaa0 100644 --- a/dist/binarypack.js +++ b/dist/binarypack.js @@ -1,5 +1,5 @@ -/*! binarypack.js build:0.0.7, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ -(function(exports){ +/*! binarypack.js build:0.0.7, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ +(function(exports){ var binaryFeatures = {}; binaryFeatures.useBlobBuilder = (function(){ try { @@ -19,7 +19,8 @@ binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function( })(); exports.binaryFeatures = binaryFeatures; -exports.BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; +BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; +exports.BlobBuilder = BlobBuilder; function BufferBuilder(){ this._pieces = []; @@ -575,5 +576,5 @@ function utf8Length(str){ return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length; } } - -})(this); + +})(this); diff --git a/lib/bufferbuilder.js b/lib/bufferbuilder.js index a37e207..c6aeae5 100644 --- a/lib/bufferbuilder.js +++ b/lib/bufferbuilder.js @@ -17,7 +17,8 @@ binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function( })(); exports.binaryFeatures = binaryFeatures; -exports.BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; +BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; +exports.BlobBuilder = BlobBuilder; function BufferBuilder(){ this._pieces = []; From 32feccf99954a7c5c288a9d531620d639412109c Mon Sep 17 00:00:00 2001 From: lmb Date: Tue, 9 Sep 2014 22:06:42 +0100 Subject: [PATCH 5/5] Finalize browserify integration and update build script --- .gitignore | 2 + .npmignore | 0 Gruntfile.js | 51 ++++ Makefile | 2 +- bin/build.js | 211 --------------- dist/binarypack.js | 580 ----------------------------------------- dist/binarypack.min.js | 1 - lib/binarypack.js | 10 +- lib/bufferbuilder.js | 8 +- lib/exports.js | 6 + package.json | 16 +- 11 files changed, 79 insertions(+), 808 deletions(-) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 Gruntfile.js delete mode 100644 bin/build.js delete mode 100644 dist/binarypack.js delete mode 100644 dist/binarypack.min.js create mode 100644 lib/exports.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1eae0cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dist/ +node_modules/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..e69de29 diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..96140c7 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,51 @@ +module.exports = function(grunt) { + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + + browserify: { + dev: { + options: { + prepend: ['/*! binarypack.js build:<%= pkg.version %>, development. '+ + 'Copyright(c) 2012 Eric Zhang MIT Licensed */'] + }, + src: ['lib/exports.js'], + dest: 'dist/binarypack.js' + } + }, + + uglify: { + prod: { + options: { + mangle: true, compress: true, + }, + src: 'dist/binarypack.js', + dest: 'dist/binarypack.min.js' + } + }, + + concat: { + dev: { + options: { + banner: '/*! binarypack.js build:<%= pkg.version %>, production. '+ + 'Copyright(c) 2012 Eric Zhang MIT Licensed */' + }, + src: 'dist/binarypack.js', + dest: 'dist/binarypack.js', + }, + prod: { + options: { + banner: '/*! binarypack.js build:<%= pkg.version %>, production. '+ + 'Copyright(c) 2012 Eric Zhang MIT Licensed */' + }, + src: 'dist/binarypack.min.js', + dest: 'dist/binarypack.min.js', + } + } + }); + + grunt.loadNpmTasks('grunt-browserify'); + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-contrib-concat'); + + grunt.registerTask('default', ['browserify', 'uglify', 'concat']); +} \ No newline at end of file diff --git a/Makefile b/Makefile index fe43143..456ac8d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ default: compress compress: - @node bin/build.js; \ No newline at end of file + @./node_modules/.bin/grunt \ No newline at end of file diff --git a/bin/build.js b/bin/build.js deleted file mode 100644 index bfb7d7e..0000000 --- a/bin/build.js +++ /dev/null @@ -1,211 +0,0 @@ -/** - * Module dependencies. - */ - -var fs = require('fs') - , package = JSON.parse(fs.readFileSync(__dirname+ '/../package.json')) - , uglify = require('uglify-js'); - -/** - * License headers. - * - * @api private - */ - -var template = '/*! binarypack.%ext% build:' + package.version + ', %type%. Copyright(c) 2012 Eric Zhang 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 = [ - 'bufferbuilder.js' - , 'binarypack.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/binarypack.js', 'w') - , content - , 0 - , 'utf8' - ); - console.log('Successfully generated the development build: binarypack.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/binarypack.min.js', 'w') - , content - , 0 - , 'utf8' - ); - console.log('Successfully generated the production build: binarypack.min.js'); - }); -} \ No newline at end of file diff --git a/dist/binarypack.js b/dist/binarypack.js deleted file mode 100644 index 145aaa0..0000000 --- a/dist/binarypack.js +++ /dev/null @@ -1,580 +0,0 @@ -/*! binarypack.js build:0.0.7, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ -(function(exports){ -var binaryFeatures = {}; -binaryFeatures.useBlobBuilder = (function(){ - try { - new Blob([]); - return false; - } catch (e) { - return true; - } -})(); - -binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function(){ - try { - return (new Blob([new Uint8Array([])])).size === 0; - } catch (e) { - return true; - } -})(); - -exports.binaryFeatures = binaryFeatures; -BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; -exports.BlobBuilder = BlobBuilder; - -function BufferBuilder(){ - this._pieces = []; - this._parts = []; -} - -BufferBuilder.prototype.append = function(data) { - if(typeof data === 'number') { - this._pieces.push(data); - } else { - this.flush(); - this._parts.push(data); - } -}; - -BufferBuilder.prototype.flush = function() { - if (this._pieces.length > 0) { - var buf = new Uint8Array(this._pieces); - if(!binaryFeatures.useArrayBufferView) { - buf = buf.buffer; - } - this._parts.push(buf); - this._pieces = []; - } -}; - -BufferBuilder.prototype.getBuffer = function() { - this.flush(); - if(binaryFeatures.useBlobBuilder) { - var builder = new BlobBuilder(); - for(var i = 0, ii = this._parts.length; i < ii; i++) { - builder.append(this._parts[i]); - } - return builder.getBlob(); - } else { - return new Blob(this._parts); - } -}; -exports.BinaryPack = { - unpack: function(data){ - var unpacker = new Unpacker(data); - return unpacker.unpack(); - }, - pack: function(data){ - var packer = new Packer(); - packer.pack(data); - var buffer = packer.getBuffer(); - return buffer; - } -}; - -function Unpacker (data){ - // Data is ArrayBuffer - this.index = 0; - this.dataBuffer = data; - this.dataView = new Uint8Array(this.dataBuffer); - this.length = this.dataBuffer.byteLength; -} - - -Unpacker.prototype.unpack = function(){ - var type = this.unpack_uint8(); - if (type < 0x80){ - var positive_fixnum = type; - return positive_fixnum; - } else if ((type ^ 0xe0) < 0x20){ - var negative_fixnum = (type ^ 0xe0) - 0x20; - return negative_fixnum; - } - var size; - if ((size = type ^ 0xa0) <= 0x0f){ - return this.unpack_raw(size); - } else if ((size = type ^ 0xb0) <= 0x0f){ - return this.unpack_string(size); - } else if ((size = type ^ 0x90) <= 0x0f){ - return this.unpack_array(size); - } else if ((size = type ^ 0x80) <= 0x0f){ - return this.unpack_map(size); - } - switch(type){ - case 0xc0: - return null; - case 0xc1: - return undefined; - case 0xc2: - return false; - case 0xc3: - return true; - case 0xca: - return this.unpack_float(); - case 0xcb: - return this.unpack_double(); - case 0xcc: - return this.unpack_uint8(); - case 0xcd: - return this.unpack_uint16(); - case 0xce: - return this.unpack_uint32(); - case 0xcf: - return this.unpack_uint64(); - case 0xd0: - return this.unpack_int8(); - case 0xd1: - return this.unpack_int16(); - case 0xd2: - return this.unpack_int32(); - case 0xd3: - return this.unpack_int64(); - case 0xd4: - return undefined; - case 0xd5: - return undefined; - case 0xd6: - return undefined; - case 0xd7: - return undefined; - case 0xd8: - size = this.unpack_uint16(); - return this.unpack_string(size); - case 0xd9: - size = this.unpack_uint32(); - return this.unpack_string(size); - case 0xda: - size = this.unpack_uint16(); - return this.unpack_raw(size); - case 0xdb: - size = this.unpack_uint32(); - return this.unpack_raw(size); - case 0xdc: - size = this.unpack_uint16(); - return this.unpack_array(size); - case 0xdd: - size = this.unpack_uint32(); - return this.unpack_array(size); - case 0xde: - size = this.unpack_uint16(); - return this.unpack_map(size); - case 0xdf: - size = this.unpack_uint32(); - return this.unpack_map(size); - } -} - -Unpacker.prototype.unpack_uint8 = function(){ - var byte = this.dataView[this.index] & 0xff; - this.index++; - return byte; -}; - -Unpacker.prototype.unpack_uint16 = function(){ - var bytes = this.read(2); - var uint16 = - ((bytes[0] & 0xff) * 256) + (bytes[1] & 0xff); - this.index += 2; - return uint16; -} - -Unpacker.prototype.unpack_uint32 = function(){ - var bytes = this.read(4); - var uint32 = - ((bytes[0] * 256 + - bytes[1]) * 256 + - bytes[2]) * 256 + - bytes[3]; - this.index += 4; - return uint32; -} - -Unpacker.prototype.unpack_uint64 = function(){ - var bytes = this.read(8); - var uint64 = - ((((((bytes[0] * 256 + - bytes[1]) * 256 + - bytes[2]) * 256 + - bytes[3]) * 256 + - bytes[4]) * 256 + - bytes[5]) * 256 + - bytes[6]) * 256 + - bytes[7]; - this.index += 8; - return uint64; -} - - -Unpacker.prototype.unpack_int8 = function(){ - var uint8 = this.unpack_uint8(); - return (uint8 < 0x80 ) ? uint8 : uint8 - (1 << 8); -}; - -Unpacker.prototype.unpack_int16 = function(){ - var uint16 = this.unpack_uint16(); - return (uint16 < 0x8000 ) ? uint16 : uint16 - (1 << 16); -} - -Unpacker.prototype.unpack_int32 = function(){ - var uint32 = this.unpack_uint32(); - return (uint32 < Math.pow(2, 31) ) ? uint32 : - uint32 - Math.pow(2, 32); -} - -Unpacker.prototype.unpack_int64 = function(){ - var uint64 = this.unpack_uint64(); - return (uint64 < Math.pow(2, 63) ) ? uint64 : - uint64 - Math.pow(2, 64); -} - -Unpacker.prototype.unpack_raw = function(size){ - if ( this.length < this.index + size){ - throw new Error('BinaryPackFailure: index is out of range' - + ' ' + this.index + ' ' + size + ' ' + this.length); - } - var buf = this.dataBuffer.slice(this.index, this.index + size); - this.index += size; - - //buf = util.bufferToString(buf); - - return buf; -} - -Unpacker.prototype.unpack_string = function(size){ - var bytes = this.read(size); - var i = 0, str = '', c, code; - while(i < size){ - c = bytes[i]; - if ( c < 128){ - str += String.fromCharCode(c); - i++; - } else if ((c ^ 0xc0) < 32){ - code = ((c ^ 0xc0) << 6) | (bytes[i+1] & 63); - str += String.fromCharCode(code); - i += 2; - } else { - code = ((c & 15) << 12) | ((bytes[i+1] & 63) << 6) | - (bytes[i+2] & 63); - str += String.fromCharCode(code); - i += 3; - } - } - this.index += size; - return str; -} - -Unpacker.prototype.unpack_array = function(size){ - var objects = new Array(size); - for(var i = 0; i < size ; i++){ - objects[i] = this.unpack(); - } - return objects; -} - -Unpacker.prototype.unpack_map = function(size){ - var map = {}; - for(var i = 0; i < size ; i++){ - var key = this.unpack(); - var value = this.unpack(); - map[key] = value; - } - return map; -} - -Unpacker.prototype.unpack_float = function(){ - var uint32 = this.unpack_uint32(); - var sign = uint32 >> 31; - var exp = ((uint32 >> 23) & 0xff) - 127; - var fraction = ( uint32 & 0x7fffff ) | 0x800000; - return (sign == 0 ? 1 : -1) * - fraction * Math.pow(2, exp - 23); -} - -Unpacker.prototype.unpack_double = function(){ - var h32 = this.unpack_uint32(); - var l32 = this.unpack_uint32(); - var sign = h32 >> 31; - var exp = ((h32 >> 20) & 0x7ff) - 1023; - var hfrac = ( h32 & 0xfffff ) | 0x100000; - var frac = hfrac * Math.pow(2, exp - 20) + - l32 * Math.pow(2, exp - 52); - return (sign == 0 ? 1 : -1) * frac; -} - -Unpacker.prototype.read = function(length){ - var j = this.index; - if (j + length <= this.length) { - return this.dataView.subarray(j, j + length); - } else { - throw new Error('BinaryPackFailure: read index out of range'); - } -} - -function Packer(){ - this.bufferBuilder = new BufferBuilder(); -} - -Packer.prototype.getBuffer = function(){ - return this.bufferBuilder.getBuffer(); -} - -Packer.prototype.pack = function(value){ - var type = typeof(value); - if (type == 'string'){ - this.pack_string(value); - } else if (type == 'number'){ - if (Math.floor(value) === value){ - this.pack_integer(value); - } else{ - this.pack_double(value); - } - } else if (type == 'boolean'){ - if (value === true){ - this.bufferBuilder.append(0xc3); - } else if (value === false){ - this.bufferBuilder.append(0xc2); - } - } else if (type == 'undefined'){ - this.bufferBuilder.append(0xc0); - } else if (type == 'object'){ - if (value === null){ - this.bufferBuilder.append(0xc0); - } else { - var constructor = value.constructor; - if (constructor == Array){ - this.pack_array(value); - } else if (constructor == Blob || constructor == File) { - this.pack_bin(value); - } else if (constructor == ArrayBuffer) { - if(binaryFeatures.useArrayBufferView) { - this.pack_bin(new Uint8Array(value)); - } else { - this.pack_bin(value); - } - } else if ('BYTES_PER_ELEMENT' in value){ - if(binaryFeatures.useArrayBufferView) { - this.pack_bin(new Uint8Array(value.buffer)); - } else { - this.pack_bin(value.buffer); - } - } else if (constructor == Object){ - this.pack_object(value); - } else if (constructor == Date){ - this.pack_string(value.toString()); - } else if (typeof value.toBinaryPack == 'function'){ - this.bufferBuilder.append(value.toBinaryPack()); - } else { - throw new Error('Type "' + constructor.toString() + '" not yet supported'); - } - } - } else { - throw new Error('Type "' + type + '" not yet supported'); - } - this.bufferBuilder.flush(); -} - - -Packer.prototype.pack_bin = function(blob){ - var length = blob.length || blob.byteLength || blob.size; - if (length <= 0x0f){ - this.pack_uint8(0xa0 + length); - } else if (length <= 0xffff){ - this.bufferBuilder.append(0xda) ; - this.pack_uint16(length); - } else if (length <= 0xffffffff){ - this.bufferBuilder.append(0xdb); - this.pack_uint32(length); - } else{ - throw new Error('Invalid length'); - return; - } - this.bufferBuilder.append(blob); -} - -Packer.prototype.pack_string = function(str){ - var length = utf8Length(str); - - if (length <= 0x0f){ - this.pack_uint8(0xb0 + length); - } else if (length <= 0xffff){ - this.bufferBuilder.append(0xd8) ; - this.pack_uint16(length); - } else if (length <= 0xffffffff){ - this.bufferBuilder.append(0xd9); - this.pack_uint32(length); - } else{ - throw new Error('Invalid length'); - return; - } - this.bufferBuilder.append(str); -} - -Packer.prototype.pack_array = function(ary){ - var length = ary.length; - if (length <= 0x0f){ - this.pack_uint8(0x90 + length); - } else if (length <= 0xffff){ - this.bufferBuilder.append(0xdc) - this.pack_uint16(length); - } else if (length <= 0xffffffff){ - this.bufferBuilder.append(0xdd); - this.pack_uint32(length); - } else{ - throw new Error('Invalid length'); - } - for(var i = 0; i < length ; i++){ - this.pack(ary[i]); - } -} - -Packer.prototype.pack_integer = function(num){ - if ( -0x20 <= num && num <= 0x7f){ - this.bufferBuilder.append(num & 0xff); - } else if (0x00 <= num && num <= 0xff){ - this.bufferBuilder.append(0xcc); - this.pack_uint8(num); - } else if (-0x80 <= num && num <= 0x7f){ - this.bufferBuilder.append(0xd0); - this.pack_int8(num); - } else if ( 0x0000 <= num && num <= 0xffff){ - this.bufferBuilder.append(0xcd); - this.pack_uint16(num); - } else if (-0x8000 <= num && num <= 0x7fff){ - this.bufferBuilder.append(0xd1); - this.pack_int16(num); - } else if ( 0x00000000 <= num && num <= 0xffffffff){ - this.bufferBuilder.append(0xce); - this.pack_uint32(num); - } else if (-0x80000000 <= num && num <= 0x7fffffff){ - this.bufferBuilder.append(0xd2); - this.pack_int32(num); - } else if (-0x8000000000000000 <= num && num <= 0x7FFFFFFFFFFFFFFF){ - this.bufferBuilder.append(0xd3); - this.pack_int64(num); - } else if (0x0000000000000000 <= num && num <= 0xFFFFFFFFFFFFFFFF){ - this.bufferBuilder.append(0xcf); - this.pack_uint64(num); - } else{ - throw new Error('Invalid integer'); - } -} - -Packer.prototype.pack_double = function(num){ - var sign = 0; - if (num < 0){ - sign = 1; - num = -num; - } - var exp = Math.floor(Math.log(num) / Math.LN2); - var frac0 = num / Math.pow(2, exp) - 1; - var frac1 = Math.floor(frac0 * Math.pow(2, 52)); - var b32 = Math.pow(2, 32); - var h32 = (sign << 31) | ((exp+1023) << 20) | - (frac1 / b32) & 0x0fffff; - var l32 = frac1 % b32; - this.bufferBuilder.append(0xcb); - this.pack_int32(h32); - this.pack_int32(l32); -} - -Packer.prototype.pack_object = function(obj){ - var keys = Object.keys(obj); - var length = keys.length; - if (length <= 0x0f){ - this.pack_uint8(0x80 + length); - } else if (length <= 0xffff){ - this.bufferBuilder.append(0xde); - this.pack_uint16(length); - } else if (length <= 0xffffffff){ - this.bufferBuilder.append(0xdf); - this.pack_uint32(length); - } else{ - throw new Error('Invalid length'); - } - for(var prop in obj){ - if (obj.hasOwnProperty(prop)){ - this.pack(prop); - this.pack(obj[prop]); - } - } -} - -Packer.prototype.pack_uint8 = function(num){ - this.bufferBuilder.append(num); -} - -Packer.prototype.pack_uint16 = function(num){ - this.bufferBuilder.append(num >> 8); - this.bufferBuilder.append(num & 0xff); -} - -Packer.prototype.pack_uint32 = function(num){ - var n = num & 0xffffffff; - this.bufferBuilder.append((n & 0xff000000) >>> 24); - this.bufferBuilder.append((n & 0x00ff0000) >>> 16); - this.bufferBuilder.append((n & 0x0000ff00) >>> 8); - this.bufferBuilder.append((n & 0x000000ff)); -} - -Packer.prototype.pack_uint64 = function(num){ - var high = num / Math.pow(2, 32); - var low = num % Math.pow(2, 32); - this.bufferBuilder.append((high & 0xff000000) >>> 24); - this.bufferBuilder.append((high & 0x00ff0000) >>> 16); - this.bufferBuilder.append((high & 0x0000ff00) >>> 8); - this.bufferBuilder.append((high & 0x000000ff)); - this.bufferBuilder.append((low & 0xff000000) >>> 24); - this.bufferBuilder.append((low & 0x00ff0000) >>> 16); - this.bufferBuilder.append((low & 0x0000ff00) >>> 8); - this.bufferBuilder.append((low & 0x000000ff)); -} - -Packer.prototype.pack_int8 = function(num){ - this.bufferBuilder.append(num & 0xff); -} - -Packer.prototype.pack_int16 = function(num){ - this.bufferBuilder.append((num & 0xff00) >> 8); - this.bufferBuilder.append(num & 0xff); -} - -Packer.prototype.pack_int32 = function(num){ - this.bufferBuilder.append((num >>> 24) & 0xff); - this.bufferBuilder.append((num & 0x00ff0000) >>> 16); - this.bufferBuilder.append((num & 0x0000ff00) >>> 8); - this.bufferBuilder.append((num & 0x000000ff)); -} - -Packer.prototype.pack_int64 = function(num){ - var high = Math.floor(num / Math.pow(2, 32)); - var low = num % Math.pow(2, 32); - this.bufferBuilder.append((high & 0xff000000) >>> 24); - this.bufferBuilder.append((high & 0x00ff0000) >>> 16); - this.bufferBuilder.append((high & 0x0000ff00) >>> 8); - this.bufferBuilder.append((high & 0x000000ff)); - this.bufferBuilder.append((low & 0xff000000) >>> 24); - this.bufferBuilder.append((low & 0x00ff0000) >>> 16); - this.bufferBuilder.append((low & 0x0000ff00) >>> 8); - this.bufferBuilder.append((low & 0x000000ff)); -} - -function _utf8Replace(m){ - var code = m.charCodeAt(0); - - if(code <= 0x7ff) return '00'; - if(code <= 0xffff) return '000'; - if(code <= 0x1fffff) return '0000'; - if(code <= 0x3ffffff) return '00000'; - return '000000'; -} - -function utf8Length(str){ - if (str.length > 600) { - // Blob method faster for large strings - return (new Blob([str])).size; - } else { - return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length; - } -} - -})(this); diff --git a/dist/binarypack.min.js b/dist/binarypack.min.js deleted file mode 100644 index ece8465..0000000 --- a/dist/binarypack.min.js +++ /dev/null @@ -1 +0,0 @@ -/*! binarypack.min.js build:0.0.7, production. Copyright(c) 2012 Eric Zhang MIT Licensed */(function(e){function n(){this._pieces=[],this._parts=[]}function r(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function i(){this.bufferBuilder=new n}function s(e){var t=e.charCodeAt(0);return t<=2047?"00":t<=65535?"000":t<=2097151?"0000":t<=67108863?"00000":"000000"}function o(e){return e.length>600?(new Blob([e])).size:e.replace(/[^\u0000-\u007F]/g,s).length}var t={};t.useBlobBuilder=function(){try{return new Blob([]),!1}catch(e){return!0}}(),t.useArrayBufferView=!t.useBlobBuilder&&function(){try{return(new Blob([new Uint8Array([])])).size===0}catch(e){return!0}}(),e.binaryFeatures=t,e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,n.prototype.append=function(e){typeof e=="number"?this._pieces.push(e):(this.flush(),this._parts.push(e))},n.prototype.flush=function(){if(this._pieces.length>0){var e=new Uint8Array(this._pieces);t.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},n.prototype.getBuffer=function(){this.flush();if(t.useBlobBuilder){var e=new BlobBuilder;for(var n=0,r=this._parts.length;n>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},r.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},r.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},i.prototype.getBuffer=function(){return this.bufferBuilder.getBuffer()},i.prototype.pack=function(e){var n=typeof e;if(n=="string")this.pack_string(e);else if(n=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(n=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(n=="undefined")this.bufferBuilder.append(192);else{if(n!="object")throw new Error('Type "'+n+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var r=e.constructor;if(r==Array)this.pack_array(e);else if(r==Blob||r==File)this.pack_bin(e);else if(r==ArrayBuffer)t.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)t.useArrayBufferView?this.pack_bin(new Uint8Array(e.buffer)):this.pack_bin(e.buffer);else if(r==Object)this.pack_object(e);else if(r==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+r.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}this.bufferBuilder.flush()},i.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_string=function(e){var t=o(e);if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n>8),this.bufferBuilder.append(e&255)},i.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},i.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},i.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},i.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)}})(this) \ No newline at end of file diff --git a/lib/binarypack.js b/lib/binarypack.js index 01b0cc5..19629c6 100644 --- a/lib/binarypack.js +++ b/lib/binarypack.js @@ -1,4 +1,7 @@ -BinaryPack = { +var BufferBuilder = require('./bufferbuilder').BufferBuilder; +var binaryFeatures = require('./bufferbuilder').binaryFeatures; + +var BinaryPack = { unpack: function(data){ var unpacker = new Unpacker(data); return unpacker.unpack(); @@ -11,7 +14,7 @@ BinaryPack = { } }; -exports.BinaryPack = BinaryPack; +module.exports = BinaryPack; function Unpacker (data){ // Data is ArrayBuffer @@ -21,7 +24,6 @@ function Unpacker (data){ this.length = this.dataBuffer.byteLength; } - Unpacker.prototype.unpack = function(){ var type = this.unpack_uint8(); if (type < 0x80){ @@ -327,7 +329,6 @@ Packer.prototype.pack_bin = function(blob){ this.pack_uint32(length); } else{ throw new Error('Invalid length'); - return; } this.bufferBuilder.append(blob); } @@ -345,7 +346,6 @@ Packer.prototype.pack_string = function(str){ this.pack_uint32(length); } else{ throw new Error('Invalid length'); - return; } this.bufferBuilder.append(str); } diff --git a/lib/bufferbuilder.js b/lib/bufferbuilder.js index c6aeae5..334f92a 100644 --- a/lib/bufferbuilder.js +++ b/lib/bufferbuilder.js @@ -16,9 +16,9 @@ binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function( } })(); -exports.binaryFeatures = binaryFeatures; -BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; -exports.BlobBuilder = BlobBuilder; +module.exports.binaryFeatures = binaryFeatures; +var BlobBuilder = module.exports.BlobBuilder = window.WebKitBlobBuilder || + window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; function BufferBuilder(){ this._pieces = []; @@ -57,3 +57,5 @@ BufferBuilder.prototype.getBuffer = function() { return new Blob(this._parts); } }; + +module.exports.BufferBuilder = BufferBuilder; diff --git a/lib/exports.js b/lib/exports.js new file mode 100644 index 0000000..93b9d27 --- /dev/null +++ b/lib/exports.js @@ -0,0 +1,6 @@ +var BufferBuilderExports = require('./bufferbuilder'); + +window.BufferBuilder = BufferBuilderExports.BufferBuilder; +window.binaryFeatures = BufferBuilderExports.binaryFeatures; +window.BlobBuilder = BufferBuilderExports.BlobBuilder; +window.BinaryPack = require('./binarypack'); diff --git a/package.json b/package.json index 7a797ee..739508e 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,9 @@ "version": "0.0.7", "description": "BinaryPack serialization for the web browser", "homepage": "https://github.com/binaryjs/js-binarypack", - "main": "./dist/binarypack.js", + "main": "./lib/binarypack.js", "scripts": { + "prepublish": "./node_modules/.bin/grunt", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -12,12 +13,13 @@ "url": "git://github.com/binaryjs/js-binarypack.git" }, "author": "Eric Zhang", - "bin": { - "js-binarypack": "build.js" + "devDependencies": { + "grunt": "^0.4.5", + "grunt-browserify": "^3.0.1", + "grunt-cli": "^0.1.13", + "grunt-contrib-concat": "^0.5.0", + "grunt-contrib-uglify": "^0.5.1", + "uglifyjs": "^2.3.6" }, - "dependencies": { - "uglify-js": "~1.3.4" - }, - "devDependencies": {}, "license": "BSD" }