diff --git a/bower.json b/bower.json index 650212a..61e6c47 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "snappyjs", "main": "./dist/snappyjs.js", - "version": "0.4.1", + "version": "0.5.0", "homepage": "https://github.com/zhipeng-jia/snappyjs", "authors": [ "Zhipeng Jia " diff --git a/dist/snappyjs.js b/dist/snappyjs.js index fa74bd9..7bb237e 100644 --- a/dist/snappyjs.js +++ b/dist/snappyjs.js @@ -5,7 +5,7 @@ * snappyjs: * license: MIT * author: Zhipeng Jia - * version: 0.4.1 + * version: 0.5.0 * * This header is generated by licensify (https://github.com/twada/licensify) */ @@ -75,6 +75,10 @@ function isNode () { var is_node = isNode() +function isUint8Array (object) { + return object instanceof Uint8Array && (!is_node || !Buffer.isBuffer(object)) +} + function isArrayBuffer (object) { return object instanceof ArrayBuffer } @@ -89,12 +93,17 @@ function isBuffer (object) { var SnappyDecompressor = require('./snappy_decompressor').SnappyDecompressor var SnappyCompressor = require('./snappy_compressor').SnappyCompressor +var TYPE_ERROR_MSG = 'Argument compressed must be type of ArrayBuffer, Buffer, or Uint8Array' + function uncompress (compressed) { - if (!isArrayBuffer(compressed) && !isBuffer(compressed)) { - throw new TypeError('Argument compressed must be type of ArrayBuffer or Buffer') + if (!isUint8Array(compressed) && !isArrayBuffer(compressed) && !isBuffer(compressed)) { + throw new TypeError(TYPE_ERROR_MSG) } + var uint8_mode = false var array_buffer_mode = false - if (isArrayBuffer(compressed)) { + if (isUint8Array(compressed)) { + uint8_mode = true + } else if (isArrayBuffer(compressed)) { array_buffer_mode = true compressed = new Uint8Array(compressed) } @@ -104,7 +113,12 @@ function uncompress (compressed) { throw new Error('Invalid Snappy bitstream') } var uncompressed, uncompressed_view - if (array_buffer_mode) { + if (uint8_mode) { + uncompressed = new Uint8Array(length) + if (!decompressor.uncompressToBuffer(uncompressed)) { + throw new Error('Invalid Snappy bitstream') + } + } else if (array_buffer_mode) { uncompressed = new ArrayBuffer(length) uncompressed_view = new Uint8Array(uncompressed) if (!decompressor.uncompressToBuffer(uncompressed_view)) { @@ -120,11 +134,14 @@ function uncompress (compressed) { } function compress (uncompressed) { - if (!isArrayBuffer(uncompressed) && !isBuffer(uncompressed)) { - throw new TypeError('Argument uncompressed must be type of ArrayBuffer or Buffer') + if (!isUint8Array(uncompressed) && !isArrayBuffer(uncompressed) && !isBuffer(uncompressed)) { + throw new TypeError(TYPE_ERROR_MSG) } + var uint8_mode = false var array_buffer_mode = false - if (isArrayBuffer(uncompressed)) { + if (isUint8Array(compressed)) { + uint8_mode = true + } else if (isArrayBuffer(uncompressed)) { array_buffer_mode = true uncompressed = new Uint8Array(uncompressed) } @@ -132,7 +149,10 @@ function compress (uncompressed) { var max_length = compressor.maxCompressedLength() var compressed, compressed_view var length - if (array_buffer_mode) { + if (uint8_mode) { + compressed = new Uint8Array(max_length) + length = compressor.compressToBuffer(compressed) + } else if (array_buffer_mode) { compressed = new ArrayBuffer(max_length) compressed_view = new Uint8Array(compressed) length = compressor.compressToBuffer(compressed_view) diff --git a/dist/snappyjs.min.js b/dist/snappyjs.min.js index d026466..b166018 100644 --- a/dist/snappyjs.min.js +++ b/dist/snappyjs.min.js @@ -5,14 +5,14 @@ * snappyjs: * license: MIT * author: Zhipeng Jia - * version: 0.4.1 + * version: 0.5.0 * * This header is generated by licensify (https://github.com/twada/licensify) */ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o>>e}function load32(r,e){return r[e]+(r[e+1]<<8)+(r[e+2]<<16)+(r[e+3]<<24)}function equals32(r,e,a){return r[e]===r[a]&&r[e+1]===r[a+1]&&r[e+2]===r[a+2]&&r[e+3]===r[a+3]}function copyBytes(r,e,a,t,n){var o;for(o=0;n>o;o++)a[t+o]=r[e+o]}function emitLiteral(r,e,a,t,n){return 60>=a?(t[n]=a-1<<2,n+=1):256>a?(t[n]=240,t[n+1]=a-1,n+=2):(t[n]=244,t[n+1]=a-1&255,t[n+2]=a-1>>>8,n+=3),copyBytes(r,e,t,n,a),n+a}function emitCopyLessThan64(r,e,a,t){return 12>t&&2048>a?(r[e]=1+(t-4<<2)+(a>>>8<<5),r[e+1]=255&a,e+2):(r[e]=2+(t-1<<2),r[e+1]=255&a,r[e+2]=a>>>8,e+3)}function emitCopy(r,e,a,t){for(;t>=68;)e=emitCopyLessThan64(r,e,a,64),t-=64;return t>64&&(e=emitCopyLessThan64(r,e,a,60),t-=60),emitCopyLessThan64(r,e,a,t)}function compressFragment(r,e,a,t,n){for(var o=1;a>=1<=o;)o+=1;o-=1;var s=32-o;"undefined"==typeof global_hash_tables[o]&&(global_hash_tables[o]=new Uint16Array(1<=g)for(u=b-g,e+=1,l=hashFunc(load32(r,e),s);T;){y=32,f=e;do{if(e=f,p=l,m=y>>>5,y+=1,f=e+m,e>u){T=!1;break}l=hashFunc(load32(r,f),s),c=d+h[p],h[p]=e-d}while(!equals32(r,e,c));if(!T)break;n=emitLiteral(r,A,e-A,t,n);do{for(_=e,L=4;b>e+L&&r[e+L]===r[c+L];)L+=1;if(e+=L,C=_-c,n=emitCopy(t,n,C,L),A=e,e>=u){T=!1;break}B=hashFunc(load32(r,e-1),s),h[B]=e-1-d,S=hashFunc(load32(r,e),s),c=d+h[S],h[S]=e-d}while(equals32(r,e,c));if(!T)break;e+=1,l=hashFunc(load32(r,e),s)}return b>A&&(n=emitLiteral(r,A,b-A,t,n)),n}function putVarint(r,e,a){do e[a]=127&r,r>>>=7,r>0&&(e[a]+=128),a+=1;while(r>0);return a}function SnappyCompressor(r){this.array=r}var BLOCK_LOG=16,BLOCK_SIZE=1<n;)e=Math.min(t-n,BLOCK_SIZE),o=compressFragment(a,n,e,r,o),n+=e;return o},exports.SnappyCompressor=SnappyCompressor; diff --git a/package.json b/package.json index d8c6924..a4cdd12 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "snappyjs", - "version": "0.4.1", + "version": "0.5.0", "description": "JavaScript implementation of Google's Snappy compression library", "repository": "zhipeng-jia/snappyjs", "main": "index.js",