-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
139 lines (128 loc) · 4.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// The MIT License (MIT)
//
// Copyright (c) 2016 Zhipeng Jia
//
// 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.
'use strict'
function isNode () {
if (typeof process === 'object') {
if (typeof process.versions === 'object') {
if (typeof process.versions.node !== 'undefined') {
return true
}
}
}
return false
}
function isUint8Array (object) {
return object instanceof Uint8Array && (!isNode() || !Buffer.isBuffer(object))
}
function isArrayBuffer (object) {
return object instanceof ArrayBuffer
}
function isBuffer (object) {
if (!isNode()) {
return false
}
return Buffer.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, maxLength) {
if (!isUint8Array(compressed) && !isArrayBuffer(compressed) && !isBuffer(compressed)) {
throw new TypeError(TYPE_ERROR_MSG)
}
var uint8Mode = false
var arrayBufferMode = false
if (isUint8Array(compressed)) {
uint8Mode = true
} else if (isArrayBuffer(compressed)) {
arrayBufferMode = true
compressed = new Uint8Array(compressed)
}
var decompressor = new SnappyDecompressor(compressed)
var length = decompressor.readUncompressedLength()
if (length === -1) {
throw new Error('Invalid Snappy bitstream')
}
if (length > maxLength) {
throw new Error(`The uncompressed length of ${length} is too big, expect at most ${maxLength}`)
}
var uncompressed, uncompressedView
if (uint8Mode) {
uncompressed = new Uint8Array(length)
if (!decompressor.uncompressToBuffer(uncompressed)) {
throw new Error('Invalid Snappy bitstream')
}
} else if (arrayBufferMode) {
uncompressed = new ArrayBuffer(length)
uncompressedView = new Uint8Array(uncompressed)
if (!decompressor.uncompressToBuffer(uncompressedView)) {
throw new Error('Invalid Snappy bitstream')
}
} else {
uncompressed = Buffer.alloc(length)
if (!decompressor.uncompressToBuffer(uncompressed)) {
throw new Error('Invalid Snappy bitstream')
}
}
return uncompressed
}
function compress (uncompressed) {
if (!isUint8Array(uncompressed) && !isArrayBuffer(uncompressed) && !isBuffer(uncompressed)) {
throw new TypeError(TYPE_ERROR_MSG)
}
var uint8Mode = false
var arrayBufferMode = false
if (isUint8Array(uncompressed)) {
uint8Mode = true
} else if (isArrayBuffer(uncompressed)) {
arrayBufferMode = true
uncompressed = new Uint8Array(uncompressed)
}
var compressor = new SnappyCompressor(uncompressed)
var maxLength = compressor.maxCompressedLength()
var compressed, compressedView
var length
if (uint8Mode) {
compressed = new Uint8Array(maxLength)
length = compressor.compressToBuffer(compressed)
} else if (arrayBufferMode) {
compressed = new ArrayBuffer(maxLength)
compressedView = new Uint8Array(compressed)
length = compressor.compressToBuffer(compressedView)
} else {
compressed = Buffer.alloc(maxLength)
length = compressor.compressToBuffer(compressed)
}
if (!compressed.slice) { // ie11
var compressedArray = new Uint8Array(Array.prototype.slice.call(compressed, 0, length))
if (uint8Mode) {
return compressedArray
} else if (arrayBufferMode) {
return compressedArray.buffer
} else {
throw new Error('Not implemented')
}
}
return compressed.slice(0, length)
}
exports.uncompress = uncompress
exports.compress = compress