Skip to content
Draft
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
19 changes: 19 additions & 0 deletions packages/datadog-shimmer/src/shimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const skipMethodSize = skipMethods.size

const nonConfigurableModuleExports = new WeakMap()

const safetyMap = new WeakMap()

/**
* Copies properties from the original function to the wrapped function.
*
Expand Down Expand Up @@ -75,8 +77,17 @@ function copyObjectProperties (original, wrapped, skipKey) {
function wrapFunction (original, wrapper) {
if (typeof original !== 'function') return original

// Safe guard: If the wrapper is identical, we double wrap.
// This will not catch all types of double wrapping, just most.
const oldWrapper = safetyMap.get(original)
if (oldWrapper === wrapper) {
return original
}

const wrapped = wrapper(original)

safetyMap.set(wrapped, wrapper)

if (typeof original === 'function') {
assertNotClass(original)
copyProperties(original, wrapped)
Expand Down Expand Up @@ -138,8 +149,16 @@ function wrap (target, name, wrapper, options) {

assertMethod(target, name, original)

// Safe guard: If the wrapper is identical, we double wrap.
// This will not catch all types of double wrapping, just most.
const oldWrapper = safetyMap.get(original)
if (oldWrapper === wrapper) {
return
}
const wrapped = wrapper(original)

safetyMap.set(wrapped, wrapper)

copyProperties(original, wrapped)

if (descriptor.writable) {
Expand Down