Skip to content

Commit 06ad51b

Browse files
authored
Merge pull request #187 from DataDog/congyao/add-sourcemaps-and-telemetry
[rum-privacy] add sourcemaps and telemetry
2 parents 7541cca + 5933f3e commit 06ad51b

37 files changed

Lines changed: 525 additions & 170 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
80.5 KB
Binary file not shown.

LICENSES-3rdparty.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Component,Origin,Licence,Copyright
187187
@rollup/plugin-json,virtual,MIT,rollup (https://github.com/rollup/plugins/tree/master/packages/json#readme)
188188
@rollup/plugin-node-resolve,virtual,MIT,Rich Harris (https://github.com/rollup/plugins/tree/master/packages/node-resolve/#readme)
189189
@rollup/plugin-terser,virtual,MIT,Peter Placzek (https://github.com/rollup/plugins/tree/master/packages/terser#readme)
190+
@rollup/plugin-typescript,virtual,MIT,Oskar Segersvärd (https://github.com/rollup/plugins/tree/master/packages/typescript/#readme)
190191
@rollup/pluginutils,virtual,MIT,Rich Harris (https://github.com/rollup/plugins/tree/master/packages/pluginutils#readme)
191192
@rollup/rollup-darwin-arm64,npm,MIT,Lukas Taegert-Atkinson (https://rollupjs.org/)
192193
@rollup/rollup-darwin-x64,npm,MIT,Lukas Taegert-Atkinson (https://rollupjs.org/)
@@ -903,6 +904,7 @@ to-regex-range,npm,MIT,Jon Schlinkert (https://github.com/micromatch/to-regex-ra
903904
tough-cookie,npm,BSD-3-Clause,Jeremy Stashewsky (https://github.com/salesforce/tough-cookie)
904905
ts-api-utils,virtual,MIT,JoshuaKGoldberg (https://www.npmjs.com/package/ts-api-utils)
905906
ts-jest,virtual,MIT,Kulshekhar Kabra (https://kulshekhar.github.io/ts-jest)
907+
ts-loader,virtual,MIT,John Reilly (https://github.com/TypeStrong/ts-loader)
906908
ts-node,virtual,MIT,Blake Embrey (https://typestrong.org/ts-node)
907909
tsconfig-paths,npm,MIT,Jonas Kello (https://www.npmjs.com/package/tsconfig-paths)
908910
tslib,npm,0BSD,Microsoft Corp. (https://www.typescriptlang.org/)

packages/plugins/injection/src/esbuild.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import os from 'os';
1414
import path from 'path';
1515

1616
import { PLUGIN_NAME } from './constants';
17-
import { getContentToInject } from './helpers';
17+
import { getContentToInject, isNodeSystemError } from './helpers';
1818
import type { ContentsToInject } from './types';
1919

2020
const fsp = fs.promises;
@@ -118,15 +118,25 @@ export const getEsbuildPlugin = (
118118

119119
// Write the content.
120120
const proms = outputs.map(async (output) => {
121-
const source = await fsp.readFile(output, 'utf-8');
122-
const data = await esbuild.transform(source, {
123-
loader: 'default',
124-
banner,
125-
footer,
126-
});
127-
128-
// FIXME: Handle sourcemaps.
129-
await fsp.writeFile(output, data.code);
121+
try {
122+
const source = await fsp.readFile(output, 'utf-8');
123+
const data = await esbuild.transform(source, {
124+
loader: 'default',
125+
banner,
126+
footer,
127+
});
128+
129+
// FIXME: Handle sourcemaps.
130+
await fsp.writeFile(output, data.code);
131+
} catch (e) {
132+
if (isNodeSystemError(e) && e.code === 'ENOENT') {
133+
// When we are using sub-builds, the entry file of sub-builds may not exist
134+
// Hence we should skip the file injection in this case.
135+
log.warn(`Could not inject content in ${output}: ${e}`);
136+
} else {
137+
throw e;
138+
}
139+
}
130140
});
131141

132142
await Promise.all(proms);

packages/plugins/injection/src/helpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,11 @@ export const addInjections = async (
133133
contentsToInject[value.position].set(id, value.value);
134134
}
135135
};
136+
137+
export interface NodeSystemError extends Error {
138+
code: string;
139+
}
140+
141+
export const isNodeSystemError = (e: unknown): e is NodeSystemError => {
142+
return e instanceof Error && 'code' in e;
143+
};

packages/plugins/rum/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"typecheck": "tsc --noEmit"
3232
},
3333
"dependencies": {
34-
"@datadog/js-instrumentation-wasm": "0.9.4",
34+
"@datadog/js-instrumentation-wasm": "1.0.3",
3535
"@dd/core": "workspace:*",
3636
"@rollup/pluginutils": "5.1.4",
3737
"chalk": "2.3.1"

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

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

10-
export function $(newValues: string[] | TemplateStringsArray) {
10+
/* __PURE__ */ const $DD_ADD_TO_DICTIONARY = (newValues: string[] | TemplateStringsArray) => {
1111
const initialSize = globalAny.$DD_ALLOW.size;
12-
newValues.forEach((value) => globalAny.$DD_ALLOW.add(value));
12+
if ((newValues as unknown as TemplateStringsArray).raw) {
13+
// We're being used as a template tag function. The invocation will look like this:
14+
// const D = $('foo', $`bar${0}`, 'baz');
15+
// In this context, our only role is to extract the TemplateStringsArray array so that
16+
// the top-level call to $ can make use of it. So, we just need to return our first
17+
// argument.
18+
return newValues;
19+
}
20+
21+
newValues.flat().forEach((value) => {
22+
globalAny.$DD_ALLOW.add(value.toLocaleLowerCase());
23+
});
24+
1325
if (globalAny.$DD_ALLOW.size !== initialSize) {
1426
if (globalAny.$DD_ALLOW_OBSERVERS) {
1527
globalAny.$DD_ALLOW_OBSERVERS.forEach((cb: () => void) => cb());
1628
}
1729
}
1830

1931
return newValues;
20-
}
32+
};
33+
34+
// Process any queued items and set up the queue mechanism
35+
(() => {
36+
const queueName = '$DD_A_Q';
37+
const addToDictionary = $DD_ADD_TO_DICTIONARY;
38+
39+
// Initialize queue if it doesn't exist
40+
globalAny[queueName] = globalAny[queueName] || [];
41+
42+
// Process all existing items in the queue
43+
globalAny[queueName].forEach(addToDictionary);
44+
45+
// Clear the queue
46+
globalAny[queueName].length = 0;
47+
48+
// Replace push method with our add function
49+
globalAny[queueName].push = addToDictionary;
50+
})();

0 commit comments

Comments
 (0)