Skip to content

Commit fe0eb16

Browse files
Merge pull request browserify#19 from jscissr/ipfs-update
Use original node code
2 parents 57d3204 + f9268e1 commit fe0eb16

40 files changed

+1695
-1425
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ node_modules
22
.DS_Store
33

44
*.log
5+
6+
test/tmp
7+
lib

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
.DS_Store
3+
4+
*.log
5+
6+
test/tmp

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
language: node_js
22
node_js:
3-
- 4
4-
- 5
53
- stable
64

75
before_install:

LICENSE

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,52 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+
23+
This project contains parts of Node.js.
24+
Node.js is licensed for use as follows:
25+
26+
"""
27+
Copyright Node.js contributors. All rights reserved.
28+
29+
Permission is hereby granted, free of charge, to any person obtaining a copy
30+
of this software and associated documentation files (the "Software"), to
31+
deal in the Software without restriction, including without limitation the
32+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
33+
sell copies of the Software, and to permit persons to whom the Software is
34+
furnished to do so, subject to the following conditions:
35+
36+
The above copyright notice and this permission notice shall be included in
37+
all copies or substantial portions of the Software.
38+
39+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
44+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
45+
IN THE SOFTWARE.
46+
"""
47+
48+
This license applies to parts of Node.js originating from the
49+
https://github.com/joyent/node repository:
50+
51+
"""
52+
Copyright Joyent, Inc. and other Node contributors. All rights reserved.
53+
Permission is hereby granted, free of charge, to any person obtaining a copy
54+
of this software and associated documentation files (the "Software"), to
55+
deal in the Software without restriction, including without limitation the
56+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
57+
sell copies of the Software, and to permit persons to whom the Software is
58+
furnished to do so, subject to the following conditions:
59+
60+
The above copyright notice and this permission notice shall be included in
61+
all copies or substantial portions of the Software.
62+
63+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
64+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
65+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
66+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
67+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
68+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
69+
IN THE SOFTWARE.
70+
"""

karma.conf.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
module.exports = function (karma) {
22
karma.set({
3-
frameworks: ['browserify', 'mocha'],
4-
files: ['test/**/test-*.js'],
5-
preprocessors: {
6-
'test/**/test-*.js': ['browserify']
7-
},
8-
browserify: {
9-
debug: true,
10-
transform: ['brfs']
11-
},
3+
frameworks: ['mocha'],
4+
files: ['test/tmp/browserified.js'],
125
reporters: ['mocha-own'],
136
mochaOwnReporter: {
147
reporter: 'spec'

package.json

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,50 @@
66
"zlib",
77
"browserify"
88
],
9-
"main": "src/index.js",
9+
"main": "lib/index.js",
1010
"directories": {
1111
"test": "test"
1212
},
1313
"dependencies": {
1414
"pako": "~1.0.1"
1515
},
1616
"devDependencies": {
17+
"assert": "^1.4.1",
18+
"babel-cli": "^6.11.4",
19+
"babel-plugin-transform-es2015-arrow-functions": "^6.8.0",
20+
"babel-plugin-transform-es2015-block-scoping": "^6.10.1",
21+
"babel-plugin-transform-es2015-template-literals": "^6.8.0",
22+
"babelify": "^7.3.0",
1723
"brfs": "^1.0.1",
24+
"browserify": "^13.1.0",
25+
"exec-glob": "^1.2.1",
26+
"glob": "^7.0.5",
1827
"karma": "^1.1.0",
19-
"karma-browserify": "^5.0.3",
2028
"karma-chrome-launcher": "^1.0.1",
21-
"karma-firefox-launcher": "^0.1.7",
29+
"karma-firefox-launcher": "^1.0.0",
2230
"karma-mocha": "^1.0.1",
2331
"karma-mocha-own-reporter": "^1.1.2",
2432
"karma-phantomjs-launcher": "^1.0.0",
2533
"mocha": "^3.0.1",
2634
"phantomjs-prebuilt": "^2.1.7",
27-
"standard": "^6.0.8",
35+
"standard": "^7.1.2",
2836
"watchify": "^3.7.0"
2937
},
30-
"testling": {
31-
"files": "test/*.js",
32-
"browsers": [
33-
"ie/6..latest",
34-
"chrome/22..latest",
35-
"firefox/16..latest",
36-
"safari/latest",
37-
"opera/11.0..latest",
38-
"iphone/6",
39-
"ipad/6",
40-
"android-browser/latest"
41-
]
42-
},
4338
"scripts": {
44-
"lint": "standard",
39+
"build": "babel src --out-dir lib",
40+
"lint": "standard \"*.js\" \"!(node_modules|lib)/!(*test-zlib*|index).js\"",
41+
"pretest": "npm run build",
4542
"test": "npm run test:node && npm run test:browser",
46-
"test:browser": "karma start --single-run=true karma.conf.js",
47-
"test:node": "mocha test/test-*"
43+
"test:node": "node node_modules/exec-glob node \"test/test-*\"",
44+
"pretest:browser": "node test/build",
45+
"test:browser": "karma start --single-run=true karma.conf.js"
46+
},
47+
"babel": {
48+
"plugins": [
49+
"transform-es2015-arrow-functions",
50+
"transform-es2015-block-scoping",
51+
"transform-es2015-template-literals"
52+
]
4853
},
4954
"author": "Devon Govett <[email protected]>",
5055
"license": "MIT",

src/binding.js

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
/* eslint camelcase: "off" */
23

34
var assert = require('assert')
45

@@ -23,17 +24,15 @@ exports.UNZIP = 7
2324

2425
var GZIP_HEADER_ID1 = 0x1f
2526
var GZIP_HEADER_ID2 = 0x8b
26-
var GZIP_MIN_HEADER_SIZE = 10
2727

2828
/**
2929
* Emulate Node's zlib C++ layer for use by the JS layer in index.js
3030
*/
3131
function Zlib (mode) {
32-
if (mode == null || mode < exports.DEFLATE || mode > exports.UNZIP) {
32+
if (typeof mode !== 'number' || mode < exports.DEFLATE || mode > exports.UNZIP) {
3333
throw new TypeError('Bad argument')
3434
}
3535

36-
this.chunk_size = 0
3736
this.dictionary = null
3837
this.err = 0
3938
this.flush = 0
@@ -45,6 +44,7 @@ function Zlib (mode) {
4544
this.windowBits = 0
4645
this.write_in_progress = false
4746
this.pending_close = false
47+
this.gzip_id_bytes_read = 0
4848
}
4949

5050
Zlib.prototype.close = function () {
@@ -60,15 +60,14 @@ Zlib.prototype.close = function () {
6060

6161
if (this.mode === exports.DEFLATE || this.mode === exports.GZIP || this.mode === exports.DEFLATERAW) {
6262
zlib_deflate.deflateEnd(this.strm)
63-
} else {
63+
} else if (this.mode === exports.INFLATE || this.mode === exports.GUNZIP ||
64+
this.mode === exports.INFLATERAW || this.mode === exports.UNZIP) {
6465
zlib_inflate.inflateEnd(this.strm)
6566
}
6667

6768
this.mode = exports.NONE
6869

69-
if (this.dictionary != null) {
70-
this.dictionary = null
71-
}
70+
this.dictionary = null
7271
}
7372

7473
Zlib.prototype.write = function (flush, input, in_off, in_len, out, out_off, out_len) {
@@ -108,12 +107,6 @@ Zlib.prototype._write = function (async, flush, input, in_off, in_len, out, out_
108107
in_off = 0
109108
}
110109

111-
if (out._set) {
112-
out.set = out._set
113-
} else {
114-
out.set = bufferSet
115-
}
116-
117110
this.strm.avail_in = in_len
118111
this.strm.input = input
119112
this.strm.next_in = in_off
@@ -122,8 +115,6 @@ Zlib.prototype._write = function (async, flush, input, in_off, in_len, out, out_
122115
this.strm.next_out = out_off
123116
this.flush = flush
124117

125-
this.chunk_size = out_len
126-
127118
if (!async) {
128119
// sync version
129120
this._process()
@@ -154,6 +145,8 @@ Zlib.prototype._afterSync = function () {
154145
}
155146

156147
Zlib.prototype._process = function () {
148+
var next_expected_header_byte = null
149+
157150
// If the avail_out is left at 0, then it means that it ran out
158151
// of room. If there was avail_out left over, then it means
159152
// that all of the input was consumed.
@@ -164,6 +157,50 @@ Zlib.prototype._process = function () {
164157
this.err = zlib_deflate.deflate(this.strm, this.flush)
165158
break
166159
case exports.UNZIP:
160+
if (this.strm.avail_in > 0) {
161+
next_expected_header_byte = this.strm.next_in
162+
}
163+
164+
switch (this.gzip_id_bytes_read) {
165+
case 0:
166+
if (next_expected_header_byte === null) {
167+
break
168+
}
169+
170+
if (this.strm.input[next_expected_header_byte] === GZIP_HEADER_ID1) {
171+
this.gzip_id_bytes_read = 1
172+
next_expected_header_byte++
173+
174+
if (this.strm.avail_in === 1) {
175+
// The only available byte was already read.
176+
break
177+
}
178+
} else {
179+
this.mode = exports.INFLATE
180+
break
181+
}
182+
183+
// fallthrough
184+
case 1:
185+
if (next_expected_header_byte === null) {
186+
break
187+
}
188+
189+
if (this.strm.input[next_expected_header_byte] === GZIP_HEADER_ID2) {
190+
this.gzip_id_bytes_read = 2
191+
this.mode = exports.GUNZIP
192+
} else {
193+
// There is no actual difference between INFLATE and INFLATERAW
194+
// (after initialization).
195+
this.mode = exports.INFLATE
196+
}
197+
198+
break
199+
default:
200+
throw new Error('invalid number of gzip magic number bytes read')
201+
}
202+
203+
// fallthrough
167204
case exports.INFLATE:
168205
case exports.GUNZIP:
169206
case exports.INFLATERAW:
@@ -183,16 +220,15 @@ Zlib.prototype._process = function () {
183220
this.err = exports.Z_NEED_DICT
184221
}
185222
}
186-
while (this.strm.avail_in >= GZIP_MIN_HEADER_SIZE &&
187-
this.mode === exports.GUNZIP) {
223+
while (this.strm.avail_in > 0 &&
224+
this.mode === exports.GUNZIP &&
225+
this.err === exports.Z_STREAM_END &&
226+
this.strm.next_in[0] !== 0x00) {
188227
// Bytes remain in input buffer. Perhaps this is another compressed
189228
// member in the same archive, or just trailing garbage.
190-
// Check the header to find out.
191-
if (this.strm.next_in[0] !== GZIP_HEADER_ID1 ||
192-
this.strm.next_in[1] !== GZIP_HEADER_ID2) {
193-
// Not a valid gzip member
194-
break
195-
}
229+
// Trailing zero bytes are okay, though, since they are frequently
230+
// used for padding.
231+
196232
this.reset()
197233
this.err = zlib_inflate.inflate(this.strm, this.flush)
198234
}
@@ -250,6 +286,9 @@ Zlib.prototype._after = function () {
250286
}
251287

252288
Zlib.prototype._error = function (message) {
289+
if (this.strm.msg) {
290+
message = this.strm.msg
291+
}
253292
this.onerror(message, this.err)
254293

255294
// no hope of rescue.
@@ -262,7 +301,7 @@ Zlib.prototype._error = function (message) {
262301
Zlib.prototype.init = function (windowBits, level, memLevel, strategy, dictionary) {
263302
assert(arguments.length === 4 || arguments.length === 5, 'init(windowBits, level, memLevel, strategy, [dictionary])')
264303

265-
assert(windowBits >= 6 && windowBits <= 15, 'invalid windowBits')
304+
assert(windowBits >= 8 && windowBits <= 15, 'invalid windowBits')
266305
assert(level >= -1 && level <= 9, 'invalid compression level')
267306

268307
assert(memLevel >= 1 && memLevel <= 9, 'invalid memlevel')
@@ -390,11 +429,4 @@ Zlib.prototype._reset = function () {
390429
}
391430
}
392431

393-
// set method for Node buffers, used by pako
394-
function bufferSet (data, offset) {
395-
for (var i = 0; i < data.length; i++) {
396-
this[offset + i] = data[i]
397-
}
398-
}
399-
400432
exports.Zlib = Zlib

0 commit comments

Comments
 (0)