Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Bundle duplicated replace function #1128

Merged
merged 3 commits into from
Mar 7, 2024
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
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;