Skip to content

Commit b900a91

Browse files
committed
feat: use global variable to be more reliable
1 parent 1bff567 commit b900a91

4 files changed

Lines changed: 45 additions & 37 deletions

File tree

packages/plugins/rum/src/built/privacy-helpers.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const globalAny: any = globalThis;
88
globalAny.$DD_ALLOW = new Set();
99

10-
export function $(newValues: string[] | TemplateStringsArray) {
10+
const $DD_ADD_TO_DICTIONARY = (newValues: string[] | TemplateStringsArray) => {
1111
const initialSize = globalAny.$DD_ALLOW.size;
1212
if ((newValues as unknown as TemplateStringsArray).raw) {
1313
// We're being used as a template tag function. The invocation will look like this:
@@ -36,4 +36,23 @@ export function $(newValues: string[] | TemplateStringsArray) {
3636
}
3737

3838
return newValues;
39-
}
39+
};
40+
41+
// Process any queued items and set up the queue mechanism
42+
(() => {
43+
const queueName = '$DD_A_Q';
44+
const global = globalThis as any;
45+
const addToDictionary = $DD_ADD_TO_DICTIONARY;
46+
47+
// Initialize queue if it doesn't exist
48+
global[queueName] = global[queueName] || [];
49+
50+
// Process all existing items in the queue
51+
global[queueName].forEach(addToDictionary);
52+
53+
// Clear the queue
54+
global[queueName].length = 0;
55+
56+
// Replace push method with our add function
57+
global[queueName].push = addToDictionary;
58+
})();

packages/plugins/rum/src/privacy/index.ts

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// Copyright 2019-Present Datadog, Inc.
44

55
import { instrument } from '@datadog/js-instrumentation-wasm';
6-
import { readFileSync } from '@dd/core/helpers/fs';
76
import type { GlobalContext, PluginOptions } from '@dd/core/types';
7+
import { InjectPosition } from '@dd/core/types';
88
import { createFilter } from '@rollup/pluginutils';
99
import path from 'node:path';
1010

11-
import { PLUGIN_NAME, PRIVACY_HELPERS_FILE_NAME } from './constants';
11+
import { PLUGIN_NAME } from './constants';
1212
import { buildTransformOptions } from './transform';
1313
import type { PrivacyOptionsWithDefaults } from './types';
1414

@@ -22,40 +22,20 @@ export const getPrivacyPlugin = (
2222
return;
2323
}
2424

25+
context.inject({
26+
type: 'file',
27+
position: InjectPosition.BEFORE,
28+
value: path.join(__dirname, './privacy-helpers.js'),
29+
});
30+
2531
const transformOptions = buildTransformOptions(pluginOptions);
2632
const transformFilter = createFilter(pluginOptions.include, pluginOptions.exclude);
27-
const { helpersModule } = pluginOptions;
2833
return {
2934
name: PLUGIN_NAME,
3035
// Enforce when the plugin will be executed.
3136
// Not supported by Rollup and ESBuild.
3237
// https://vitejs.dev/guide/api-plugin.html#plugin-ordering
3338
enforce: 'post',
34-
// webpack's id filter is outside of loader logic,
35-
// an additional hook is needed for better perf on webpack
36-
async resolveId(source) {
37-
if (source.includes(helpersModule)) {
38-
return { id: source };
39-
}
40-
return null;
41-
},
42-
43-
loadInclude(id) {
44-
if (id.includes(helpersModule)) {
45-
return true;
46-
}
47-
return false;
48-
},
49-
50-
async load(id) {
51-
if (id.includes(helpersModule)) {
52-
const filename = `${path.join(__dirname, PRIVACY_HELPERS_FILE_NAME)}.${id.endsWith('.cjs') ? 'js' : 'mjs'}`;
53-
return { code: readFileSync(filename), map: null };
54-
}
55-
return null;
56-
},
57-
// webpack's id filter is outside of loader logic,
58-
// an additional hook is needed for better perf on webpack
5939
transformInclude(id) {
6040
return transformFilter(id);
6141
},
@@ -68,8 +48,7 @@ export const getPrivacyPlugin = (
6848
embedCodeInSourceMap: true,
6949
};
7050
}
71-
const result = instrument({ id, code }, transformOptions);
72-
return result;
51+
return instrument({ id, code }, transformOptions);
7352
} catch (e) {
7453
log.error(`Instrumentation Error: ${e}`);
7554
return {

packages/plugins/rum/src/privacy/transform.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ export function buildTransformOptions(
1717
return {
1818
privacy: {
1919
addToDictionaryHelper: {
20-
import: {
21-
cjsModule: `${pluginOptions.helpersModule}.cjs`,
22-
esmModule: `${pluginOptions.helpersModule}.mjs`,
23-
func: pluginOptions.addToDictionaryFunctionName ?? '$',
20+
expression: {
21+
code: `((q='$DD_A_Q',g=globalThis)=>(g[q]=g[q]||[],(v=>(g[q].push(v),v))))()`,
2422
},
23+
// import: {
24+
// cjsModule: `${pluginOptions.helpersModule}.cjs`,
25+
// esmModule: `${pluginOptions.helpersModule}.mjs`,
26+
// func: pluginOptions.addToDictionaryFunctionName ?? '$',
27+
// },
2528
},
2629
},
2730
};

packages/plugins/rum/src/validate.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ export const validatePrivacyOptions = (
130130
};
131131
}
132132

133-
log.debug(`datadog-rum-privacy plugin options: ${JSON.stringify(toReturn.config)}`);
133+
log.debug(
134+
`datadog-rum-privacy plugin options: ${JSON.stringify(toReturn.config, (_, value) => {
135+
if (value instanceof RegExp) {
136+
return value.toString();
137+
}
138+
return value;
139+
})}`,
140+
);
134141

135142
return toReturn;
136143
};

0 commit comments

Comments
 (0)