-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathbel-serializer.js
More file actions
89 lines (75 loc) · 2.29 KB
/
Copy pathbel-serializer.js
File metadata and controls
89 lines (75 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Copyright 2020-2026 New Relic, Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import { stringify } from '../util/stringify'
var hasOwnProp = Object.prototype.hasOwnProperty
var MAX_ATTRIBUTES = 64
export function nullable (val, fn, comma) {
return val || val === 0 || val === ''
? fn(val) + (comma ? ',' : '')
: '!'
}
export function numeric (n, noDefault) {
if (noDefault) {
return Math.floor(n).toString(36)
}
return (n === undefined || n === 0) ? '' : Math.floor(n).toString(36)
}
export function getAddStringContext (obfuscator, truncator) {
let stringTableIdx = 0
const stringTable = Object.prototype.hasOwnProperty.call(Object, 'create') ? Object.create(null) : {}
return addString
function addString (str) {
if (typeof str === 'undefined' || str === '') return ''
str = obfuscator?.obfuscateString(String(str)) ?? String(str)
str = truncator?.(str) ?? str
if (hasOwnProp.call(stringTable, str)) {
return numeric(stringTable[str], true)
} else {
stringTable[str] = stringTableIdx++
return quoteString(str)
}
}
}
export function addCustomAttributes (attrs, addString) {
var attrParts = []
Object.entries(attrs || {}).forEach(([key, val]) => {
if (attrParts.length >= MAX_ATTRIBUTES) return
var type = 5
var serializedValue
// add key to string table first
key = addString(key)
switch (typeof val) {
case 'object':
if (val) {
// serialize objects to strings
serializedValue = addString(stringify(val))
} else {
// null attribute type
type = 9
}
break
case 'number':
type = 6
// make sure numbers contain a `.` so they are parsed as doubles
serializedValue = val % 1 ? val : val + '.'
break
case 'boolean':
type = val ? 7 : 8
break
case 'undefined':
// we treat undefined as a null attribute (since dirac does not have a concept of undefined)
type = 9
break
default:
serializedValue = addString(val)
}
attrParts.push([type, key + (serializedValue ? ',' + serializedValue : '')])
})
return attrParts
}
var escapable = /([,\\;])/g
function quoteString (str) {
return "'" + str.replace(escapable, '\\$1')
}