From 249bb8da2fc8efd1394d7977a571d9d7f28cf296 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 PR-URL: https://github.com/nodejs/node/pull/8105 Reviewed-By: Anna Henningsen Reviewed-By: Evan Lucas Reviewed-By: James M Snell --- 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 073879d9e9f..055f8779b18 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 867bbbf2aee..075426fd6bf 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);