Skip to content

Commit

Permalink
zlib: use common owner symbol to access JS wrapper
Browse files Browse the repository at this point in the history
Use the same symbol that other `AsyncWrap` instances also use
for accessing the JS wrapper.

PR-URL: #23189
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
  • Loading branch information
addaleax authored and jasnell committed Oct 17, 2018
1 parent 9fa7c2a commit 72f95e2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
Buffer,
kMaxLength
} = require('buffer');
const { owner_symbol } = require('internal/async_hooks').symbols;

const constants = process.binding('constants').zlib;
const {
Expand Down Expand Up @@ -143,7 +144,7 @@ function zlibBufferSync(engine, buffer) {
}

function zlibOnError(message, errno) {
var self = this.jsref;
var self = this[owner_symbol];
// there is no way to cleanly recover.
// continuing only obscures problems.
_close(self);
Expand Down Expand Up @@ -289,7 +290,8 @@ function Zlib(opts, mode) {
Transform.call(this, opts);
this.bytesWritten = 0;
this._handle = new binding.Zlib(mode);
this._handle.jsref = this; // Used by processCallback() and zlibOnError()
// Used by processCallback() and zlibOnError()
this._handle[owner_symbol] = this;
this._handle.onerror = zlibOnError;
this._hadError = false;
this._writeState = new Uint32Array(2);
Expand Down Expand Up @@ -717,6 +719,13 @@ function createProperty(ctor) {
};
}

// Legacy alias on the C++ wrapper object. This is not public API, so we may
// want to runtime-deprecate it at some point. There's no hurry, though.
Object.defineProperty(binding.Zlib.prototype, 'jsref', {
get() { return this[owner_symbol]; },
set(v) { return this[owner_symbol] = v; }
});

module.exports = {
Deflate,
Inflate,
Expand Down

0 comments on commit 72f95e2

Please sign in to comment.