MCPcopy Index your code
hub / github.com/simstudioai/sim / getJsonByteSize

Function getJsonByteSize

apps/sim/lib/logs/execution/logger.ts:80–152  ·  view source on GitHub ↗
(
  value: unknown,
  maxBytes = MAX_EXECUTION_DATA_BYTES + 1
)

Source from the content-addressed store, hash-verified

78type ExecutionData = WorkflowExecutionLog['executionData']
79
80function getJsonByteSize(
81 value: unknown,
82 maxBytes = MAX_EXECUTION_DATA_BYTES + 1
83): number | undefined {
84 const seen = new WeakSet<object>()
85 let bytes = 0
86
87 const add = (amount: number) => {
88 bytes += amount
89 if (bytes > maxBytes) {
90 throw new Error('json_size_limit_reached')
91 }
92 }
93
94 const visit = (item: unknown): void => {
95 if (item === undefined || typeof item === 'function' || typeof item === 'symbol') {
96 add(4)
97 return
98 }
99 if (item === null) {
100 add(4)
101 return
102 }
103 if (typeof item === 'string') {
104 add(Buffer.byteLength(JSON.stringify(item), 'utf8'))
105 return
106 }
107 if (typeof item === 'bigint') {
108 add(Buffer.byteLength(JSON.stringify(item.toString()), 'utf8'))
109 return
110 }
111 if (typeof item === 'number' || typeof item === 'boolean') {
112 add(Buffer.byteLength(JSON.stringify(item) ?? 'null', 'utf8'))
113 return
114 }
115 if (typeof item !== 'object') {
116 add(4)
117 return
118 }
119 if (seen.has(item)) {
120 return
121 }
122 seen.add(item)
123
124 if (Array.isArray(item)) {
125 add(2)
126 item.forEach((entry, index) => {
127 if (index > 0) add(1)
128 visit(entry)
129 })
130 return
131 }
132
133 const entries = Object.entries(item)
134 add(2)
135 entries.forEach(([key, entry], index) => {
136 if (entry === undefined || typeof entry === 'function' || typeof entry === 'symbol') return
137 if (index > 0) add(1)

Calls 2

getErrorMessageFunction · 0.90
visitFunction · 0.70

Tested by

no test coverage detected