From 651c92cad15c60e053ad71db49b3105f4372097c Mon Sep 17 00:00:00 2001 From: Bryan English Date: Sun, 14 Aug 2016 15:27:40 -0700 Subject: [PATCH] util: fix deprecated class prototype Ensure the wrapped class prototype is exactly the unwrapped class prototype, rather than an object whose prototype is the unwrapped class prototype. This ensures that instances of the unwrapped class are instances of the wrapped class. This is useful when both a wrapped class and a factory for the unwrapped class are both exposed. Ref: https://github.com/nodejs/node/pull/8103 --- lib/internal/util.js | 5 ++++- test/fixtures/deprecated-userland-class.js | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/internal/util.js b/lib/internal/util.js index 073879d9e9fb3f..055f8779b18b7a 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -69,7 +69,10 @@ exports._deprecate = function(fn, msg) { // The wrapper will keep the same prototype as fn to maintain prototype chain Object.setPrototypeOf(deprecated, fn); if (fn.prototype) { - Object.setPrototypeOf(deprecated.prototype, fn.prototype); + // Setting this (rather than using Object.setPrototype, as above) ensures + // that calling the unwrapped constructor gives an instanceof the wrapped + // constructor. + deprecated.prototype = fn.prototype; } return deprecated; diff --git a/test/fixtures/deprecated-userland-class.js b/test/fixtures/deprecated-userland-class.js index 867bbbf2aeea4f..075426fd6bf16c 100644 --- a/test/fixtures/deprecated-userland-class.js +++ b/test/fixtures/deprecated-userland-class.js @@ -7,6 +7,9 @@ class deprecatedClass { const deprecated = util.deprecate(deprecatedClass, 'deprecatedClass is deprecated.'); const instance = new deprecated(); +const deprecatedInstance = new deprecatedClass(); assert(instance instanceof deprecated); assert(instance instanceof deprecatedClass); +assert(deprecatedInstance instanceof deprecated); +assert(deprecatedInstance instanceof deprecatedClass);