Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: make constants keep readonly #1361

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
const bkeys = Object.keys(binding);
for (var bk = 0; bk < bkeys.length; 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.
exports.codes = {
const codes = {
Z_OK: binding.Z_OK,
Z_STREAM_END: binding.Z_STREAM_END,
Z_NEED_DICT: binding.Z_NEED_DICT,
Expand All @@ -46,12 +50,16 @@ exports.codes = {
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++) {
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.Inflate = Inflate;
exports.Gzip = Gzip;
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-zlib-const.js
Original file line number Diff line number Diff line change
@@ -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');