From 37ffdd59462a5201a346bdf23852fa4532c5e15d Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Wed, 17 Jul 2019 17:59:02 +0200 Subject: [PATCH] perf: remove manual enumerability modifications Instead of changing the enumerability of certain fields, just generate the appropriate output of the `tree()` call. This is possible in dag-pb as there are no user-defined dynamic fields that we might be able to resolve into. Closes #152. --- src/dag-link/dagLink.js | 5 ---- src/dag-node/dagNode.js | 5 ---- src/resolver.js | 23 +++++++----------- src/visibility.js | 53 ----------------------------------------- 4 files changed, 9 insertions(+), 77 deletions(-) delete mode 100644 src/visibility.js diff --git a/src/dag-link/dagLink.js b/src/dag-link/dagLink.js index f10d82c..82f4ef0 100644 --- a/src/dag-link/dagLink.js +++ b/src/dag-link/dagLink.js @@ -2,7 +2,6 @@ const CID = require('cids') const assert = require('assert') -const visibility = require('../visibility') const withIs = require('class-is') // Link represents an IPFS Merkle DAG Link between Nodes. @@ -17,10 +16,6 @@ class DAGLink { this._nameBuf = null this._size = size this._cid = new CID(cid) - - // Make sure we have a nice public API that can be used by an IPLD resolver - visibility.hidePrivateFields(this) - visibility.addEnumerableGetters(this, ['Hash', 'Name', 'Tsize']) } toString () { diff --git a/src/dag-node/dagNode.js b/src/dag-node/dagNode.js index 36d59cd..b8e7092 100644 --- a/src/dag-node/dagNode.js +++ b/src/dag-node/dagNode.js @@ -2,7 +2,6 @@ const withIs = require('class-is') const sortLinks = require('./sortLinks') -const visibility = require('../visibility') const DAGLink = require('../dag-link/dagLink') const { serializeDAGNode } = require('../serialize.js') const toDAGLink = require('./toDagLink') @@ -38,10 +37,6 @@ class DAGNode { this._data = data this._links = links this._serializedSize = serializedSize - - // Make sure we have a nice public API that can be used by an IPLD resolver - visibility.hidePrivateFields(this) - visibility.addEnumerableGetters(this, ['Data', 'Links']) } toJSON () { diff --git a/src/resolver.js b/src/resolver.js index a1888fb..d5ebed3 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -54,19 +54,6 @@ exports.resolve = (binaryBlob, path) => { } } -const traverse = function * (node, path) { - // Traverse only objects and arrays - if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' || - node === null) { - return - } - for (const item of Object.keys(node)) { - const nextpath = path === undefined ? item : path + '/' + item - yield nextpath - yield * traverse(node[item], nextpath) - } -} - /** * Return all available paths of a block. * @@ -77,5 +64,13 @@ const traverse = function * (node, path) { exports.tree = function * (binaryBlob) { const node = util.deserialize(binaryBlob) - yield * traverse(node) + // There is always a `Data` and `Links` property + yield 'Data' + yield 'Links' + for (let ii = 0; ii < node.Links.length; ii++) { + yield `Links/${ii}` + yield `Links/${ii}/Name` + yield `Links/${ii}/Tsize` + yield `Links/${ii}/Hash` + } } diff --git a/src/visibility.js b/src/visibility.js deleted file mode 100644 index 02b1280..0000000 --- a/src/visibility.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict' - -/** - * Make certain getters enumnerable - * - * This can be used to add additional getters that are enumerable and hence - * show up on an `Object.keys()` call. - * - * @param {Object} object - The object it should be applied to - * @param {Array.} fields - The fields that should be made enumnerable - */ -const addEnumerableGetters = (object, fields) => { - for (const field of fields) { - let prop - let proto = object - // Walk up the proottype chain until a property with the given name is - // found - while (prop === undefined) { - proto = Object.getPrototypeOf(proto) - if (proto === null) { - throw new Error(`no getter named '${field}' found`) - } - prop = Object.getOwnPropertyDescriptor(proto, field) - } - - // There is a property with the correct name, but it's not a getter - if (prop.get === undefined) { - throw new Error(`no getter named '${field}' found`) - } - Object.defineProperty(object, field, { - enumerable: true, - get: prop.get - }) - } -} - -/** - * Makes all properties with a leading underscore non-enumerable. - * - * @param {Object} object - The object it should be applied to - */ -const hidePrivateFields = (object) => { - for (const key in object) { - if (key[0] === '_') { - Object.defineProperty(object, key, { enumerable: false }) - } - } -} - -module.exports = { - addEnumerableGetters, - hidePrivateFields -}