From 71b5e3a8d29a07283432b2bce17349afdb4a10ee Mon Sep 17 00:00:00 2001 From: Fujimura Kaito <63333564+fujiyamaorange@users.noreply.github.com> Date: Fri, 8 Mar 2024 00:05:18 +0900 Subject: [PATCH] [Refactor] Bundle duplicated `replace` function (#1128) --- src/browser/telemetry.js | 9 +-------- src/server/telemetry.js | 15 +-------------- src/utility/replace.js | 9 +++++++++ 3 files changed, 11 insertions(+), 22 deletions(-) create mode 100644 src/utility/replace.js diff --git a/src/browser/telemetry.js b/src/browser/telemetry.js index e4e965781..e4678694e 100644 --- a/src/browser/telemetry.js +++ b/src/browser/telemetry.js @@ -1,5 +1,6 @@ var _ = require('../utility'); var headers = require('../utility/headers'); +var replace = require('../utility/replace'); var scrub = require('../scrub'); var urlparser = require('./url'); var domUtil = require('./domUtility'); @@ -21,14 +22,6 @@ var defaults = { errorOnContentSecurityPolicy: false }; -function replace(obj, name, replacement, replacements, type) { - var orig = obj[name]; - obj[name] = replacement(orig); - if (replacements) { - replacements[type].push([obj, name, orig]); - } -} - function restore(replacements, type) { var b; while (replacements[type].length) { diff --git a/src/server/telemetry.js b/src/server/telemetry.js index 51d396d5d..99264553a 100644 --- a/src/server/telemetry.js +++ b/src/server/telemetry.js @@ -1,6 +1,7 @@ var http = require('http'); var https = require('https'); var _ = require('../utility'); +var replace = require('../utility/replace'); var urlHelpers = require('./telemetry/urlHelpers'); var defaults = { @@ -149,20 +150,6 @@ Instrumenter.prototype.instrumentConsole = function() { }, this.replacements, 'log'); }; -// TODO: These helpers are duplicated in src/browser/telemetry.js, -// and may be candidates for extraction into a shared module. -// It is recommended that before doing so, the author should allow -// for more telemetry types to be implemented for the Node target -// to ensure that the implementations of these helpers don't diverge. -// If they do diverge, there's little point in the shared module. -function replace(obj, name, replacement, replacements, type) { - var orig = obj[name]; - obj[name] = replacement(orig); - if (replacements) { - replacements[type].push([obj, name, orig]); - } -} - function restore(replacements, type) { var b; while (replacements[type].length) { diff --git a/src/utility/replace.js b/src/utility/replace.js new file mode 100644 index 000000000..892bcc25b --- /dev/null +++ b/src/utility/replace.js @@ -0,0 +1,9 @@ +function replace(obj, name, replacement, replacements, type) { + var orig = obj[name]; + obj[name] = replacement(orig); + if (replacements) { + replacements[type].push([obj, name, orig]); + } + } + +module.exports = replace;