diff --git a/CHANGELOG.md b/CHANGELOG.md index 612439f..552e0ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Make `Develry.HttpAgent` classes return sockets from `createConnection` (backport) * Don't throw errors when an *unused/free* socket has an error in `Develry.HttpAgent` (backport) +* Add some devtools functions for custom inspect formatting (backport) ## 0.8.18 (2024-01-19) diff --git a/lib/devtools.js b/lib/devtools.js new file mode 100644 index 0000000..3ea0f29 --- /dev/null +++ b/lib/devtools.js @@ -0,0 +1,91 @@ +const defStat = Blast.createStaticDefiner(Blast); + +/** + * Identifier symbols + * + * @author Jelle De Loecker + * @since 0.9.3 + * @version 0.9.3 + */ +const JANEWAY_LEFT = defStat('JANEWAY_LEFT', Symbol.for('janeway_arg_left')), + JANEWAY_RIGHT = defStat('JANEWAY_RIGHT', Symbol.for('janeway_arg_right')); + +/** + * Get all the object getters + * + * @author Jelle De Loecker + * @since 0.9.3 + * @version 0.9.3 + */ +defStat(function getObjectGetters(obj) { + + const result = new Map(); + + let descriptors, + descriptor, + symbols, + current = obj, + entry, + key; + + while (current && typeof current == 'object') { + descriptors = Object.getOwnPropertyDescriptors(current); + + // Get the symbols + symbols = this.getObjectSymbols(current); + + if (symbols && symbols.length) { + let i; + + for (i = 0; i < symbols.length; i++) { + key = symbols[i]; + descriptor = Object.getOwnPropertyDescriptor(current, key); + + if (descriptor.get) { + descriptor.symbol = key; + descriptors[String(key)] = descriptor; + } + } + } + + for (key in descriptors) { + + if (key == '__proto__') { + continue; + } + + entry = descriptors[key]; + + if (entry.get) { + result.set(key, entry); + } + } + + if (current.__proto__) { + current = current.__proto__; + } else { + break; + } + } + + return result; +}); + +/** + * Get all the symbol properties of the given object + * + * @author Jelle De Loecker + * @since 0.9.3 + * @version 0.9.3 + * + * @param {Object} arg + * + * @return {Array} + */ +defStat(function getObjectSymbols(arg) { + if (!Object.getOwnPropertySymbols) { + return []; + } + + return Object.getOwnPropertySymbols(arg); +}); \ No newline at end of file diff --git a/lib/init.js b/lib/init.js index b1adbc6..a92afea 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1602,6 +1602,7 @@ function BlastInit(modifyPrototype) { Blast.require('stream_delayed', {client: false}); Blast.require('stream_combined', {client: false}); Blast.require('form_data', {client: false}); + Blast.require('devtools', {client: false}); } Blast.require('request_events', {add_wrapper: true, client: true});