From 7291685bee48fb1903174f1ba91ef897a05c8707 Mon Sep 17 00:00:00 2001 From: "Christopher Jeffrey (JJ)" Date: Mon, 5 Feb 2024 01:12:57 -0500 Subject: [PATCH] Remove class syntax for better babelification (#345) --- index.js | 56 +++++++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index 58f331e..cbe4c51 100644 --- a/index.js +++ b/index.js @@ -1836,42 +1836,36 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { // Simplified versions from Node, changed for Buffer-only usage const errors = {} function E (sym, getMessage, Base) { - errors[sym] = class NodeError extends Base { - constructor () { - super() - - Object.defineProperty(this, 'message', { - value: getMessage.apply(this, arguments), - writable: true, - configurable: true - }) - - // Add the error code to the name to include it in the stack trace. - this.name = `${this.name} [${sym}]` - // Access the stack to generate the error message including the error code - // from the name. - this.stack // eslint-disable-line no-unused-expressions - // Reset the name to the actual name. - delete this.name + function NodeError () { + const err = new Base(getMessage.apply(null, arguments)) + + Object.setPrototypeOf(err, NodeError.prototype) + + // Node.js `err.code` properties are own/enumerable properties. + err.code = sym + // Add the error code to the name to include it in the stack trace. + err.name = `${err.name} [${sym}]` + // Remove NodeError from the stack trace. + if (Error.captureStackTrace) { + Error.captureStackTrace(err, NodeError) } + // Access the stack to generate the error message including the error code + // from the name. + err.stack // eslint-disable-line no-unused-expressions + // Reset the name to the actual name. + delete err.name - get code () { - return sym - } + return err + } - set code (value) { - Object.defineProperty(this, 'code', { - configurable: true, - enumerable: true, - value, - writable: true - }) - } + Object.setPrototypeOf(NodeError.prototype, Base.prototype) + Object.setPrototypeOf(NodeError, Base) - toString () { - return `${this.name} [${sym}]: ${this.message}` - } + NodeError.prototype.toString = function toString () { + return `${this.name} [${sym}]: ${this.message}` } + + errors[sym] = NodeError } E('ERR_BUFFER_OUT_OF_BOUNDS', -- 2.34.1