MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / parseEnvelope

Function parseEnvelope

packages/core/src/utils/envelope.ts:145–177  ·  view source on GitHub ↗
(env: string | Uint8Array)

Source from the content-addressed store, hash-verified

143 * Parses an envelope
144 */
145export function parseEnvelope(env: string | Uint8Array): Envelope {
146 let buffer = typeof env === 'string' ? encodeUTF8(env) : env;
147
148 function readBinary(length: number): Uint8Array {
149 const bin = buffer.subarray(0, length);
150 // Replace the buffer with the remaining data excluding trailing newline
151 buffer = buffer.subarray(length + 1);
152 return bin;
153 }
154
155 function readJson<T>(): T {
156 let i = buffer.indexOf(0xa);
157 // If we couldn't find a newline, we must have found the end of the buffer
158 if (i < 0) {
159 i = buffer.length;
160 }
161
162 return JSON.parse(decodeUTF8(readBinary(i))) as T;
163 }
164
165 const envelopeHeader = readJson<BaseEnvelopeHeaders>();
166 // eslint-disable-next-line @typescript-eslint/no-explicit-any
167 const items: [any, any][] = [];
168
169 while (buffer.length) {
170 const itemHeader = readJson<BaseEnvelopeItemHeaders>();
171 const binaryLength = typeof itemHeader.length === 'number' ? itemHeader.length : undefined;
172
173 items.push([itemHeader, binaryLength ? readBinary(binaryLength) : readJson()]);
174 }
175
176 return [envelopeHeader, items];
177}
178
179/**
180 * Creates envelope item for a single span

Callers 8

envelope.test.tsFile · 0.90
metadata.test.tsFile · 0.90
createTestTransportFunction · 0.90
handleTunnelRequestFunction · 0.90
doCallbackFunction · 0.90
createIndexedDbStoreFunction · 0.50

Calls 4

encodeUTF8Function · 0.85
readBinaryFunction · 0.85
pushMethod · 0.80
readJsonFunction · 0.70

Tested by 1

createTestTransportFunction · 0.72