Skip to content

Commit

Permalink
[Refactor] Bundle duplicated replace function (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiyamaorange authored Mar 7, 2024
1 parent 04e15e0 commit 71b5e3a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
9 changes: 1 addition & 8 deletions src/browser/telemetry.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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) {
Expand Down
15 changes: 1 addition & 14 deletions src/server/telemetry.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/utility/replace.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 71b5e3a

Please sign in to comment.