zlib: make constants keep readonly
In zlib module, a dozen constants were exported to user land, If user change the constant, maybe lead unexcepted error. Make them readonly and freezon. PR-URL: https://github.com/iojs/io.js/pull/1361 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
This commit is contained in:
parent
d726a177ed
commit
372bf83818
16
lib/zlib.js
16
lib/zlib.js
@ -30,11 +30,15 @@ binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
|
|||||||
const bkeys = Object.keys(binding);
|
const bkeys = Object.keys(binding);
|
||||||
for (var bk = 0; bk < bkeys.length; bk++) {
|
for (var bk = 0; bk < bkeys.length; bk++) {
|
||||||
var bkey = bkeys[bk];
|
var bkey = bkeys[bk];
|
||||||
if (bkey.match(/^Z/)) exports[bkey] = binding[bkey];
|
if (bkey.match(/^Z/)) {
|
||||||
|
Object.defineProperty(exports, bkey, {
|
||||||
|
enumerable: true, value: binding[bkey], writable: false
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// translation table for return codes.
|
// translation table for return codes.
|
||||||
exports.codes = {
|
const codes = {
|
||||||
Z_OK: binding.Z_OK,
|
Z_OK: binding.Z_OK,
|
||||||
Z_STREAM_END: binding.Z_STREAM_END,
|
Z_STREAM_END: binding.Z_STREAM_END,
|
||||||
Z_NEED_DICT: binding.Z_NEED_DICT,
|
Z_NEED_DICT: binding.Z_NEED_DICT,
|
||||||
@ -46,12 +50,16 @@ exports.codes = {
|
|||||||
Z_VERSION_ERROR: binding.Z_VERSION_ERROR
|
Z_VERSION_ERROR: binding.Z_VERSION_ERROR
|
||||||
};
|
};
|
||||||
|
|
||||||
const ckeys = Object.keys(exports.codes);
|
const ckeys = Object.keys(codes);
|
||||||
for (var ck = 0; ck < ckeys.length; ck++) {
|
for (var ck = 0; ck < ckeys.length; ck++) {
|
||||||
var ckey = ckeys[ck];
|
var ckey = ckeys[ck];
|
||||||
exports.codes[exports.codes[ckey]] = ckey;
|
codes[codes[ckey]] = ckey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(exports, 'codes', {
|
||||||
|
enumerable: true, value: Object.freeze(codes), writable: false
|
||||||
|
});
|
||||||
|
|
||||||
exports.Deflate = Deflate;
|
exports.Deflate = Deflate;
|
||||||
exports.Inflate = Inflate;
|
exports.Inflate = Inflate;
|
||||||
exports.Gzip = Gzip;
|
exports.Gzip = Gzip;
|
||||||
|
16
test/parallel/test-zlib-const.js
Normal file
16
test/parallel/test-zlib-const.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
var common = require('../common');
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
|
var zlib = require('zlib');
|
||||||
|
|
||||||
|
assert.equal(zlib.Z_OK, 0, 'Z_OK should be 0');
|
||||||
|
zlib.Z_OK = 1;
|
||||||
|
assert.equal(zlib.Z_OK, 0, 'Z_OK should be 0');
|
||||||
|
|
||||||
|
assert.equal(zlib.codes.Z_OK, 0, 'Z_OK should be 0');
|
||||||
|
zlib.codes.Z_OK = 1;
|
||||||
|
assert.equal(zlib.codes.Z_OK, 0, 'zlib.codes.Z_OK should be 0');
|
||||||
|
zlib.codes = {Z_OK: 1};
|
||||||
|
assert.equal(zlib.codes.Z_OK, 0, 'zlib.codes.Z_OK should be 0');
|
||||||
|
|
||||||
|
assert.ok(Object.isFrozen(zlib.codes), 'zlib.codes should be frozen');
|
Loading…
x
Reference in New Issue
Block a user