|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Buffer = require('buffer').Buffer; |
| 4 | +const { serialize, deserialize } = require('v8'); |
| 5 | +const { SafeSet } = require('internal/safe_globals'); |
| 6 | + |
| 7 | +const kSerializedError = 0; |
| 8 | +const kSerializedObject = 1; |
| 9 | +const kInspectedError = 2; |
| 10 | + |
| 11 | +const GetPrototypeOf = Object.getPrototypeOf; |
| 12 | +const GetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; |
| 13 | +const GetOwnPropertyNames = Object.getOwnPropertyNames; |
| 14 | +const DefineProperty = Object.defineProperty; |
| 15 | +const Assign = Object.assign; |
| 16 | +const ObjectPrototypeToString = |
| 17 | + Function.prototype.call.bind(Object.prototype.toString); |
| 18 | +const ForEach = Function.prototype.call.bind(Array.prototype.forEach); |
| 19 | +const Call = Function.prototype.call.bind(Function.prototype.call); |
| 20 | + |
| 21 | +const errors = { |
| 22 | + Error, TypeError, RangeError, URIError, SyntaxError, ReferenceError, EvalError |
| 23 | +}; |
| 24 | +const errorConstructorNames = new SafeSet(Object.keys(errors)); |
| 25 | + |
| 26 | +function TryGetAllProperties(object, target = object) { |
| 27 | + const all = Object.create(null); |
| 28 | + if (object === null) |
| 29 | + return all; |
| 30 | + Assign(all, TryGetAllProperties(GetPrototypeOf(object), target)); |
| 31 | + const keys = GetOwnPropertyNames(object); |
| 32 | + ForEach(keys, (key) => { |
| 33 | + const descriptor = GetOwnPropertyDescriptor(object, key); |
| 34 | + const getter = descriptor.get; |
| 35 | + if (getter && key !== '__proto__') { |
| 36 | + try { |
| 37 | + descriptor.value = Call(getter, target); |
| 38 | + } catch {} |
| 39 | + } |
| 40 | + if ('value' in descriptor && typeof descriptor.value !== 'function') { |
| 41 | + delete descriptor.get; |
| 42 | + delete descriptor.set; |
| 43 | + all[key] = descriptor; |
| 44 | + } |
| 45 | + }); |
| 46 | + return all; |
| 47 | +} |
| 48 | + |
| 49 | +function GetConstructors(object) { |
| 50 | + const constructors = []; |
| 51 | + |
| 52 | + for (var current = object; |
| 53 | + current !== null; |
| 54 | + current = GetPrototypeOf(current)) { |
| 55 | + const desc = GetOwnPropertyDescriptor(current, 'constructor'); |
| 56 | + if (desc && desc.value) { |
| 57 | + DefineProperty(constructors, constructors.length, { |
| 58 | + value: desc.value, enumerable: true |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return constructors; |
| 64 | +} |
| 65 | + |
| 66 | +function GetName(object) { |
| 67 | + const desc = GetOwnPropertyDescriptor(object, 'name'); |
| 68 | + return desc && desc.value; |
| 69 | +} |
| 70 | + |
| 71 | +let util; |
| 72 | +function lazyUtil() { |
| 73 | + if (!util) |
| 74 | + util = require('util'); |
| 75 | + return util; |
| 76 | +} |
| 77 | + |
| 78 | +function serializeError(error) { |
| 79 | + try { |
| 80 | + if (typeof error === 'object' && |
| 81 | + ObjectPrototypeToString(error) === '[object Error]') { |
| 82 | + const constructors = GetConstructors(error); |
| 83 | + for (var i = constructors.length - 1; i >= 0; i--) { |
| 84 | + const name = GetName(constructors[i]); |
| 85 | + if (errorConstructorNames.has(name)) { |
| 86 | + try { error.stack; } catch {} |
| 87 | + const serialized = serialize({ |
| 88 | + constructor: name, |
| 89 | + properties: TryGetAllProperties(error) |
| 90 | + }); |
| 91 | + return Buffer.concat([Buffer.from([kSerializedError]), serialized]); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } catch {} |
| 96 | + try { |
| 97 | + const serialized = serialize(error); |
| 98 | + return Buffer.concat([Buffer.from([kSerializedObject]), serialized]); |
| 99 | + } catch {} |
| 100 | + return Buffer.concat([Buffer.from([kInspectedError]), |
| 101 | + Buffer.from(lazyUtil().inspect(error), 'utf8')]); |
| 102 | +} |
| 103 | + |
| 104 | +function deserializeError(error) { |
| 105 | + switch (error[0]) { |
| 106 | + case kSerializedError: |
| 107 | + const { constructor, properties } = deserialize(error.subarray(1)); |
| 108 | + const ctor = errors[constructor]; |
| 109 | + return Object.create(ctor.prototype, properties); |
| 110 | + case kSerializedObject: |
| 111 | + return deserialize(error.subarray(1)); |
| 112 | + case kInspectedError: |
| 113 | + const buf = Buffer.from(error.buffer, |
| 114 | + error.byteOffset + 1, |
| 115 | + error.byteLength - 1); |
| 116 | + return buf.toString('utf8'); |
| 117 | + } |
| 118 | + require('assert').fail('This should not happen'); |
| 119 | +} |
| 120 | + |
| 121 | +module.exports = { serializeError, deserializeError }; |
0 commit comments