MCPcopy Create free account
hub / github.com/Snapchat/Valdi / drainBuffer

Method drainBuffer

npm_modules/cli/src/utils/daemonClient.ts:147–182  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

145 }
146}
147
148// ─── Packet encoding ─────────────────────────────────────────────────────────
149
150const MAGIC = Buffer.from([0x33, 0xc6, 0x00, 0x01]);
151const HEADER_SIZE = 8; // 4 magic + 4 uint32LE length
152// Heap dumps and element snapshots legitimately exceed trace-sized payloads. Keep generic
153// framing bounded while applying the tighter trace limit once the inner request is identified.
154export const MAX_DAEMON_INNER_PAYLOAD_BYTES = 64 * 1024 * 1024;
155export const MAX_DAEMON_PACKET_PAYLOAD_BYTES = 128 * 1024 * 1024;
156export const MAX_DAEMON_TRACE_PAYLOAD_BYTES = 4 * 1024 * 1024;
157const MAX_DAEMON_BUFFERED_BYTES = HEADER_SIZE + MAX_DAEMON_PACKET_PAYLOAD_BYTES;
158
159function encodePacket(json: object): Buffer {
160 const serialized = JSON.stringify(json);
161 const payloadLength = Buffer.byteLength(serialized, 'utf8');
162 if (payloadLength > MAX_DAEMON_PACKET_PAYLOAD_BYTES) {
163 throw new Error(`ValdiPacket payload exceeds the ${MAX_DAEMON_PACKET_PAYLOAD_BYTES}-byte limit.`);
164 }
165 const payload = Buffer.from(serialized, 'utf8');
166 const header = Buffer.alloc(8);
167 MAGIC.copy(header, 0);
168 header.writeUInt32LE(payload.length, 4);
169 return Buffer.concat([header, payload]);
170}
171
172// ─── DaemonConnection ────────────────────────────────────────────────────────
173
174// In the direct-to-device protocol we are "client 1" (non-zero required by device JS check).
175const DIRECT_CLIENT_ID = 1;
176const ADB_FORWARD_REFRESH_INTERVAL_MS = 5000;
177const adbForwardedAtByEndpoint = new Map<string, number>();
178const adbForwardRefreshByEndpoint = new Map<string, Promise<void>>();
179
180interface PendingRequest {
181 resolve: (value: Record<string, unknown>) => void;
182 reject: (err: Error) => void;
183}
184
185interface PendingPayloadRequest extends PendingRequest {

Callers 1

onDataMethod · 0.95

Calls 5

dispatchMessageMethod · 0.95
destroyMethod · 0.65
toStringMethod · 0.65
concatMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected