Skip to content

Commit

Permalink
Merge branch 'main' into bcaudan/manual-view-tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Jun 4, 2021
2 parents 4fcff0d + 54b50b2 commit 2b79460
Show file tree
Hide file tree
Showing 18 changed files with 439 additions and 137 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
---

## v2.12.1

- 🔊 monitor potential invalid date ([#880](https://github.com/DataDog/browser-sdk/pull/880))

## v2.12.0

- ⚡️ start mutation observer only when needed ([#858](https://github.com/DataDog/browser-sdk/pull/858))
Expand Down
2 changes: 1 addition & 1 deletion developer-extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-sdk-developer-extension",
"version": "2.12.0",
"version": "2.12.1",
"private": true,
"scripts": {
"build": "rm -rf dist && webpack --mode production",
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"npmClient": "yarn",
"useWorkspaces": true,
"version": "2.12.0",
"version": "2.12.1",
"publishConfig": {
"access": "public"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"lint": "scripts/cli lint .",
"typecheck": "scripts/cli typecheck . && scripts/cli typecheck developer-extension",
"dev": "node scripts/dev-server.js",
"release": "lerna version --exact",
"release": "scripts/cli release",
"version": "scripts/cli version",
"publish:npm": "TARGET_DATACENTER=us BUILD_MODE=release yarn build && lerna publish from-package",
"test": "yarn test:unit:watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-core",
"version": "2.12.0",
"version": "2.12.1",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand Down
77 changes: 70 additions & 7 deletions packages/core/src/tools/limitModification.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Context } from './context'
import { limitModification } from './limitModification'

describe('limitModification', () => {
Expand Down Expand Up @@ -47,18 +48,66 @@ describe('limitModification', () => {
})
})

it('should not allow non string value on modifiable field', () => {
const object = { foo: { bar: 'bar' }, qux: 'qux' }
it('should not allow changing the type of the value on modifiable field', () => {
const object = {
string_to_undefined: 'bar',
string_to_number: 'qux',

null_to_object: null,
object_to_null: {},

undefined_to_object: undefined,
object_to_undefined: {},

array_to_object: [],
object_to_array: {},
}
const modifier = (candidate: any) => {
candidate.foo.bar = undefined
candidate.qux = 1234
candidate.string_to_undefined = undefined
candidate.string_to_number = 1234
candidate.null_to_object = {}
candidate.object_to_null = null
candidate.undefined_to_object = {}
candidate.object_to_undefined = undefined
candidate.array_to_object = {}
candidate.object_to_array = []
}

limitModification(object, ['foo.bar', 'qux'], modifier)
limitModification(object, Object.keys(object), modifier)

expect(object).toEqual({
foo: { bar: 'bar' },
qux: 'qux',
string_to_undefined: 'bar',
string_to_number: 'qux',

null_to_object: null,
object_to_null: {},

undefined_to_object: undefined,
object_to_undefined: {},

array_to_object: [],
object_to_array: {},
})
})

it('should allow emptying an object by setting it to null, undefined or deleting it', () => {
const object: any = {
a: { foo: 'a' },
b: { foo: 'b' },
c: { foo: 'c' },
}
const modifier = (candidate: any) => {
candidate.a = null
candidate.b = undefined
delete candidate.c
}

limitModification(object, Object.keys(object), modifier)

expect(object).toEqual({
a: {},
b: {},
c: {},
})
})

Expand All @@ -78,6 +127,20 @@ describe('limitModification', () => {
})
})

it('should allow modification on sub-fields for object fields', () => {
const object: Context = { foo: { bar: 'bar', baz: 'baz' } }
const modifier = (candidate: any) => {
candidate.foo.bar = { qux: 'qux' }
delete candidate.foo.baz
}

limitModification(object, ['foo'], modifier)

expect(object).toEqual({
foo: { bar: { qux: 'qux' } },
})
})

it('should return the result of the modifier', () => {
const object = { foo: { bar: 'bar' } }
const modifier = (candidate: any) => {
Expand Down
24 changes: 20 additions & 4 deletions packages/core/src/tools/limitModification.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Context, deepClone } from './context'

/**
* Current limitations:
* Current limitation:
* - field path do not support array, 'a.b.c' only
* - modifiable fields type must be string
*/
export function limitModification<T extends Context, Result>(
object: T,
Expand All @@ -15,8 +14,12 @@ export function limitModification<T extends Context, Result>(
modifiableFieldPaths.forEach((path) => {
const originalValue = get(object, path)
const newValue = get(clone, path)
if (typeof originalValue === 'string' && typeof newValue === 'string') {
const originalType = getType(originalValue)
const newType = getType(newValue)
if (newType === originalType) {
set(object, path, newValue)
} else if (originalType === 'object' && (newType === 'undefined' || newType === 'null')) {
set(object, path, {})
}
})
return result
Expand All @@ -33,7 +36,7 @@ function get(object: unknown, path: string) {
return current
}

function set(object: unknown, path: string, value: string) {
function set(object: unknown, path: string, value: unknown) {
let current = object
const fields = path.split('.')
for (let i = 0; i < fields.length; i += 1) {
Expand All @@ -52,3 +55,16 @@ function set(object: unknown, path: string, value: string) {
function isValidObjectContaining(object: unknown, field: string): object is { [key: string]: unknown } {
return typeof object === 'object' && object !== null && field in object
}

/**
* Similar to `typeof`, but distinguish plain objects from `null` and arrays
*/
function getType(value: unknown) {
if (value === null) {
return 'null'
}
if (Array.isArray(value)) {
return 'array'
}
return typeof value
}
4 changes: 2 additions & 2 deletions packages/logs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-logs",
"version": "2.12.0",
"version": "2.12.1",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand All @@ -13,7 +13,7 @@
"replace-build-env": "node ../../scripts/replace-build-env.js"
},
"dependencies": {
"@datadog/browser-core": "2.12.0",
"@datadog/browser-core": "2.12.1",
"tslib": "^1.10.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datadog/browser-rum-core",
"version": "2.12.0",
"version": "2.12.1",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand All @@ -12,7 +12,7 @@
"replace-build-env": "node ../../scripts/replace-build-env.js"
},
"dependencies": {
"@datadog/browser-core": "2.12.0",
"@datadog/browser-core": "2.12.1",
"tslib": "^1.10.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 2b79460

Please sign in to comment.