Skip to content

Commit 9730a3d

Browse files
committed
switch tests to original node, update binding.js
Tests now are the same as node.js core, except for two imports changed. src/binding.js was also updated to current node core (v6.3.1).
1 parent 02c142b commit 9730a3d

34 files changed

+1229
-1017
lines changed

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: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,41 @@
1414
"pako": "~1.0.1"
1515
},
1616
"devDependencies": {
17+
"assert": "^1.4.1",
1718
"babel-cli": "^6.11.4",
18-
"babel-preset-es2015": "^6.13.2",
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",
1923
"brfs": "^1.0.1",
24+
"browserify": "^13.1.0",
25+
"exec-glob": "^1.2.1",
26+
"glob": "^7.0.5",
2027
"karma": "^1.1.0",
21-
"karma-browserify": "^5.0.3",
2228
"karma-chrome-launcher": "^1.0.1",
23-
"karma-firefox-launcher": "^0.1.7",
29+
"karma-firefox-launcher": "^1.0.0",
2430
"karma-mocha": "^1.0.1",
2531
"karma-mocha-own-reporter": "^1.1.2",
2632
"karma-phantomjs-launcher": "^1.0.0",
2733
"mocha": "^3.0.1",
2834
"phantomjs-prebuilt": "^2.1.7",
29-
"standard": "^6.0.8",
35+
"standard": "^7.1.2",
3036
"watchify": "^3.7.0"
3137
},
3238
"scripts": {
3339
"build": "babel src --out-dir lib",
34-
"lint": "standard \"src/binding.js\" \"test/**/*.js\"",
40+
"lint": "standard \"*.js\" \"!(node_modules|lib)/!(*test-zlib*|index).js\"",
3541
"pretest": "npm run build",
3642
"test": "npm run test:node && npm run test:browser",
37-
"test:browser": "karma start --single-run=true karma.conf.js",
38-
"test:node": "mocha test/test-*"
43+
"test:node": "exec-glob node test/test-*",
44+
"pretest:browser": "node test/build",
45+
"test:browser": "karma start --single-run=true karma.conf.js"
3946
},
4047
"babel": {
41-
"presets": [
42-
"es2015"
48+
"plugins": [
49+
"transform-es2015-arrow-functions",
50+
"transform-es2015-block-scoping",
51+
"transform-es2015-template-literals"
4352
]
4453
},
4554
"author": "Devon Govett <[email protected]>",

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

test/build.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
const browserify = require('browserify')
4+
const glob = require('glob')
5+
const path = require('path')
6+
const fs = require('fs')
7+
8+
function wrap (content, files) {
9+
return `
10+
(function () {
11+
var fs = {}
12+
var require
13+
var emitOnexit // set in common.js
14+
15+
var timeouts = {}
16+
var timeoutId = 0
17+
var setTimeout = function (fn, time) {
18+
if (time) throw new Error('time not supported in fake setTimeout')
19+
timeouts[++timeoutId] = fn
20+
return timeoutId
21+
}
22+
var clearTimeout = function (id) {
23+
delete timeouts[id]
24+
}
25+
function doTimeouts () {
26+
while (Object.keys(timeouts).length) {
27+
var id = Object.keys(timeouts)[0]
28+
timeouts[id]()
29+
delete timeouts[id]
30+
}
31+
emitOnexit()
32+
}
33+
34+
;${content}
35+
36+
describe('zlib-browserify', function () {
37+
${files.map(file => `
38+
it('${path.basename(file, '.js')}', function () {
39+
require(${JSON.stringify(path.normalize(file))})
40+
doTimeouts()
41+
})`
42+
).join('')}
43+
})
44+
})();
45+
`
46+
}
47+
48+
const browserified = path.join(__dirname, 'tmp/browserified.js')
49+
50+
glob(path.join(__dirname, 'test-*'), (err, files) => {
51+
if (err) throw err
52+
53+
// workaround for old assert version in browserify
54+
require('browserify/lib/builtins').assert = require.resolve('assert/')
55+
56+
const b = browserify({
57+
transform: ['babelify', 'brfs']
58+
})
59+
60+
b.require(files)
61+
b.bundle((err, buf) => {
62+
if (err) throw err
63+
64+
fs.writeFileSync(browserified, wrap(buf, files))
65+
console.log('bundled')
66+
})
67+
})

0 commit comments

Comments
 (0)