Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions src/metrics/collections/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,37 @@ export async function startTrackingCollections (page) {

// Via https://github.com/jonschlinkert/is-plain-object/blob/0a47f0f/is-plain-object.js
function isPlainObject (o) {
function isObject (o) {
return toString.call(o) === '[object Object]'
}
if (isObject(o) === false) {
return false
}
try {
function isObject (o) {
return toString.call(o) === '[object Object]'
}
if (isObject(o) === false) {
return false
}

// If has modified constructor
const ctor = o.constructor
if (ctor === undefined) {
return true
}
// If has modified constructor
const ctor = o.constructor
if (ctor === undefined) {
return true
}

// If has modified prototype
const prot = ctor.prototype
if (isObject(prot) === false) {
return false
}
// If has modified prototype
const prot = ctor.prototype
if (isObject(prot) === false) {
return false
}

// If constructor does not have an Object-specific method
if (hasOwnProperty.call(prot, 'isPrototypeOf') === false) {
return false
}

// If constructor does not have an Object-specific method
if (hasOwnProperty.call(prot, 'isPrototypeOf') === false) {
// Most likely a plain Object
return true
} catch (err) {
// if anything throws, bail out and decide it's not a plain object
return false
}

// Most likely a plain Object
return true
}

function getSize (obj) {
Expand Down
14 changes: 14 additions & 0 deletions test/www/invalidCollection/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@
const router = makeRouter(['', 'info'])

const notAMap = Object.create(new Map())
const nullConstructor = { constructor: null }
const constructorThatThrows = {
get constructor() {
throw new Error('haha!')
}
}
const toStringThatThrows = {
get [Symbol.toStringTag]() {
throw new Error('yolo!')
}
}

router.addAfterHook('/info', () => {
console.log(notAMap)
console.log(nullConstructor)
console.log(constructorThatThrows)
console.log(toStringThatThrows)
})
</script>
</body>
Expand Down
Loading