@daytonaio/sdk 0.170.0 → 0.171.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/cjs/CodeInterpreter.d.ts +95 -0
- package/cjs/CodeInterpreter.d.ts.map +1 -0
- package/cjs/CodeInterpreter.js +297 -0
- package/cjs/CodeInterpreter.js.map +1 -0
- package/cjs/ComputerUse.d.ts +539 -0
- package/cjs/ComputerUse.d.ts.map +1 -0
- package/cjs/ComputerUse.js +808 -0
- package/cjs/ComputerUse.js.map +1 -0
- package/cjs/Daytona.d.ts +355 -0
- package/cjs/Daytona.d.ts.map +1 -0
- package/cjs/Daytona.js +527 -0
- package/cjs/Daytona.js.map +1 -0
- package/cjs/FileSystem.d.ts +371 -0
- package/cjs/FileSystem.d.ts.map +1 -0
- package/cjs/FileSystem.js +489 -0
- package/cjs/FileSystem.js.map +1 -0
- package/cjs/Git.d.ts +203 -0
- package/cjs/Git.d.ts.map +1 -0
- package/cjs/Git.js +326 -0
- package/cjs/Git.js.map +1 -0
- package/cjs/Image.d.ts +265 -0
- package/cjs/Image.d.ts.map +1 -0
- package/cjs/Image.js +586 -0
- package/cjs/Image.js.map +1 -0
- package/cjs/LspServer.d.ts +172 -0
- package/cjs/LspServer.d.ts.map +1 -0
- package/cjs/LspServer.js +259 -0
- package/cjs/LspServer.js.map +1 -0
- package/cjs/ObjectStorage.d.ts +79 -0
- package/cjs/ObjectStorage.d.ts.map +1 -0
- package/cjs/ObjectStorage.js +199 -0
- package/cjs/ObjectStorage.js.map +1 -0
- package/cjs/Process.d.ts +490 -0
- package/cjs/Process.d.ts.map +1 -0
- package/cjs/Process.js +647 -0
- package/cjs/Process.js.map +1 -0
- package/cjs/PtyHandle.d.ts +155 -0
- package/cjs/PtyHandle.d.ts.map +1 -0
- package/cjs/PtyHandle.js +416 -0
- package/cjs/PtyHandle.js.map +1 -0
- package/cjs/README.md +180 -0
- package/cjs/Sandbox.d.ts +476 -0
- package/cjs/Sandbox.d.ts.map +1 -0
- package/cjs/Sandbox.js +905 -0
- package/cjs/Sandbox.js.map +1 -0
- package/cjs/Snapshot.d.ts +140 -0
- package/cjs/Snapshot.d.ts.map +1 -0
- package/cjs/Snapshot.js +248 -0
- package/cjs/Snapshot.js.map +1 -0
- package/cjs/Volume.d.ts +84 -0
- package/cjs/Volume.d.ts.map +1 -0
- package/cjs/Volume.js +119 -0
- package/cjs/Volume.js.map +1 -0
- package/cjs/__tests__/helpers.d.ts +6 -0
- package/cjs/__tests__/helpers.d.ts.map +1 -0
- package/cjs/__tests__/helpers.js +26 -0
- package/cjs/__tests__/helpers.js.map +1 -0
- package/cjs/errors/DaytonaError.d.ts +171 -0
- package/cjs/errors/DaytonaError.d.ts.map +1 -0
- package/cjs/errors/DaytonaError.js +258 -0
- package/cjs/errors/DaytonaError.js.map +1 -0
- package/cjs/index.d.ts +22 -0
- package/cjs/index.d.ts.map +1 -0
- package/cjs/index.js +49 -0
- package/cjs/index.js.map +1 -0
- package/cjs/package.json +3 -0
- package/cjs/types/Charts.d.ts +38 -0
- package/cjs/types/Charts.d.ts.map +1 -0
- package/cjs/types/Charts.js +37 -0
- package/cjs/types/Charts.js.map +1 -0
- package/cjs/types/CodeInterpreter.d.ts +77 -0
- package/cjs/types/CodeInterpreter.d.ts.map +1 -0
- package/cjs/types/CodeInterpreter.js +7 -0
- package/cjs/types/CodeInterpreter.js.map +1 -0
- package/cjs/types/ExecuteResponse.d.ts +26 -0
- package/cjs/types/ExecuteResponse.d.ts.map +1 -0
- package/cjs/types/ExecuteResponse.js +7 -0
- package/cjs/types/ExecuteResponse.js.map +1 -0
- package/cjs/types/Pty.d.ts +48 -0
- package/cjs/types/Pty.d.ts.map +1 -0
- package/cjs/types/Pty.js +7 -0
- package/cjs/types/Pty.js.map +1 -0
- package/cjs/utils/Binary.d.ts +39 -0
- package/cjs/utils/Binary.d.ts.map +1 -0
- package/cjs/utils/Binary.js +168 -0
- package/cjs/utils/Binary.js.map +1 -0
- package/cjs/utils/FileTransfer.d.ts +15 -0
- package/cjs/utils/FileTransfer.d.ts.map +1 -0
- package/cjs/utils/FileTransfer.js +261 -0
- package/cjs/utils/FileTransfer.js.map +1 -0
- package/cjs/utils/Import.d.ts +125 -0
- package/cjs/utils/Import.d.ts.map +1 -0
- package/cjs/utils/Import.js +112 -0
- package/cjs/utils/Import.js.map +1 -0
- package/cjs/utils/Multipart.d.ts +29 -0
- package/cjs/utils/Multipart.d.ts.map +1 -0
- package/cjs/utils/Multipart.js +115 -0
- package/cjs/utils/Multipart.js.map +1 -0
- package/cjs/utils/Runtime.d.ts +44 -0
- package/cjs/utils/Runtime.d.ts.map +1 -0
- package/cjs/utils/Runtime.js +95 -0
- package/cjs/utils/Runtime.js.map +1 -0
- package/cjs/utils/Stream.d.ts +20 -0
- package/cjs/utils/Stream.d.ts.map +1 -0
- package/cjs/utils/Stream.js +364 -0
- package/cjs/utils/Stream.js.map +1 -0
- package/cjs/utils/WebSocket.d.ts +10 -0
- package/cjs/utils/WebSocket.d.ts.map +1 -0
- package/cjs/utils/WebSocket.js +26 -0
- package/cjs/utils/WebSocket.js.map +1 -0
- package/cjs/utils/otel.decorator.d.ts +83 -0
- package/cjs/utils/otel.decorator.d.ts.map +1 -0
- package/cjs/utils/otel.decorator.js +141 -0
- package/cjs/utils/otel.decorator.js.map +1 -0
- package/esm/CodeInterpreter.d.ts +95 -0
- package/esm/CodeInterpreter.d.ts.map +1 -0
- package/esm/CodeInterpreter.js +292 -0
- package/esm/CodeInterpreter.js.map +1 -0
- package/esm/ComputerUse.d.ts +539 -0
- package/esm/ComputerUse.d.ts.map +1 -0
- package/esm/ComputerUse.js +799 -0
- package/esm/ComputerUse.js.map +1 -0
- package/esm/Daytona.d.ts +355 -0
- package/esm/Daytona.d.ts.map +1 -0
- package/esm/Daytona.js +523 -0
- package/esm/Daytona.js.map +1 -0
- package/esm/FileSystem.d.ts +371 -0
- package/esm/FileSystem.d.ts.map +1 -0
- package/esm/FileSystem.js +485 -0
- package/esm/FileSystem.js.map +1 -0
- package/esm/Git.d.ts +203 -0
- package/esm/Git.d.ts.map +1 -0
- package/esm/Git.js +322 -0
- package/esm/Git.js.map +1 -0
- package/esm/Image.d.ts +265 -0
- package/esm/Image.d.ts.map +1 -0
- package/esm/Image.js +581 -0
- package/esm/Image.js.map +1 -0
- package/esm/LspServer.d.ts +172 -0
- package/esm/LspServer.d.ts.map +1 -0
- package/esm/LspServer.js +255 -0
- package/esm/LspServer.js.map +1 -0
- package/esm/ObjectStorage.d.ts +79 -0
- package/esm/ObjectStorage.d.ts.map +1 -0
- package/esm/ObjectStorage.js +195 -0
- package/esm/ObjectStorage.js.map +1 -0
- package/esm/Process.d.ts +490 -0
- package/esm/Process.d.ts.map +1 -0
- package/esm/Process.js +642 -0
- package/esm/Process.js.map +1 -0
- package/esm/PtyHandle.d.ts +155 -0
- package/esm/PtyHandle.d.ts.map +1 -0
- package/esm/PtyHandle.js +412 -0
- package/esm/PtyHandle.js.map +1 -0
- package/esm/README.md +180 -0
- package/esm/Sandbox.d.ts +476 -0
- package/esm/Sandbox.d.ts.map +1 -0
- package/esm/Sandbox.js +901 -0
- package/esm/Sandbox.js.map +1 -0
- package/esm/Snapshot.d.ts +140 -0
- package/esm/Snapshot.d.ts.map +1 -0
- package/esm/Snapshot.js +244 -0
- package/esm/Snapshot.js.map +1 -0
- package/esm/Volume.d.ts +84 -0
- package/esm/Volume.d.ts.map +1 -0
- package/esm/Volume.js +115 -0
- package/esm/Volume.js.map +1 -0
- package/esm/__tests__/helpers.d.ts +6 -0
- package/esm/__tests__/helpers.d.ts.map +1 -0
- package/esm/__tests__/helpers.js +20 -0
- package/esm/__tests__/helpers.js.map +1 -0
- package/esm/errors/DaytonaError.d.ts +171 -0
- package/esm/errors/DaytonaError.d.ts.map +1 -0
- package/esm/errors/DaytonaError.js +243 -0
- package/esm/errors/DaytonaError.js.map +1 -0
- package/esm/index.d.ts +22 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +21 -0
- package/esm/index.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/types/Charts.d.ts +38 -0
- package/esm/types/Charts.d.ts.map +1 -0
- package/esm/types/Charts.js +33 -0
- package/esm/types/Charts.js.map +1 -0
- package/esm/types/CodeInterpreter.d.ts +77 -0
- package/esm/types/CodeInterpreter.d.ts.map +1 -0
- package/esm/types/CodeInterpreter.js +6 -0
- package/esm/types/CodeInterpreter.js.map +1 -0
- package/esm/types/ExecuteResponse.d.ts +26 -0
- package/esm/types/ExecuteResponse.d.ts.map +1 -0
- package/esm/types/ExecuteResponse.js +6 -0
- package/esm/types/ExecuteResponse.js.map +1 -0
- package/esm/types/Pty.d.ts +48 -0
- package/esm/types/Pty.d.ts.map +1 -0
- package/esm/types/Pty.js +6 -0
- package/esm/types/Pty.js.map +1 -0
- package/esm/utils/Binary.d.ts +39 -0
- package/esm/utils/Binary.d.ts.map +1 -0
- package/esm/utils/Binary.js +157 -0
- package/esm/utils/Binary.js.map +1 -0
- package/esm/utils/FileTransfer.d.ts +15 -0
- package/esm/utils/FileTransfer.d.ts.map +1 -0
- package/esm/utils/FileTransfer.js +254 -0
- package/esm/utils/FileTransfer.js.map +1 -0
- package/esm/utils/Import.d.ts +95 -0
- package/esm/utils/Import.d.ts.map +1 -0
- package/esm/utils/Import.js +77 -0
- package/esm/utils/Import.js.map +1 -0
- package/esm/utils/Multipart.d.ts +29 -0
- package/esm/utils/Multipart.d.ts.map +1 -0
- package/esm/utils/Multipart.js +109 -0
- package/esm/utils/Multipart.js.map +1 -0
- package/esm/utils/Runtime.d.ts +44 -0
- package/esm/utils/Runtime.d.ts.map +1 -0
- package/esm/utils/Runtime.js +89 -0
- package/esm/utils/Runtime.js.map +1 -0
- package/esm/utils/Stream.d.ts +20 -0
- package/esm/utils/Stream.d.ts.map +1 -0
- package/esm/utils/Stream.js +360 -0
- package/esm/utils/Stream.js.map +1 -0
- package/esm/utils/WebSocket.d.ts +10 -0
- package/esm/utils/WebSocket.d.ts.map +1 -0
- package/esm/utils/WebSocket.js +22 -0
- package/esm/utils/WebSocket.js.map +1 -0
- package/esm/utils/otel.decorator.d.ts +83 -0
- package/esm/utils/otel.decorator.d.ts.map +1 -0
- package/esm/utils/otel.decorator.js +136 -0
- package/esm/utils/otel.decorator.js.map +1 -0
- package/package.json +20 -8
- package/src/ComputerUse.d.ts +4 -4
- package/src/ComputerUse.js +4 -4
- package/src/Sandbox.d.ts +1 -18
- package/src/Sandbox.js +0 -29
- package/src/Sandbox.js.map +1 -1
- package/src/__tests__/helpers.js +0 -2
- package/src/__tests__/helpers.js.map +1 -1
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { STDOUT_PREFIX_BYTES, STDERR_PREFIX_BYTES, MAX_PREFIX_LEN } from '../Process.js';
|
|
2
|
+
function isStreamTerminationError(err) {
|
|
3
|
+
if (!err || typeof err !== 'object')
|
|
4
|
+
return false;
|
|
5
|
+
const e = err;
|
|
6
|
+
// Typical Undici / fetch termination cases
|
|
7
|
+
if (e.name === 'AbortError')
|
|
8
|
+
return true;
|
|
9
|
+
if (typeof e.message === 'string' && /terminated/i.test(e.message))
|
|
10
|
+
return true;
|
|
11
|
+
if (e.code === 'UND_ERR_SOCKET')
|
|
12
|
+
return true;
|
|
13
|
+
// Look into nested causes, Undici often nests errors
|
|
14
|
+
if (e.cause)
|
|
15
|
+
return isStreamTerminationError(e.cause);
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Process a streaming response from fetch(), where getStream() returns a Fetch Response.
|
|
20
|
+
*
|
|
21
|
+
* @param getStream – zero-arg function that does `await fetch(...)` and returns the Response
|
|
22
|
+
* @param onChunk – called with each decoded UTF-8 chunk
|
|
23
|
+
* @param shouldTerminate – pollable; if true for two consecutive timeouts (or once if requireConsecutiveTermination=false), the loop breaks
|
|
24
|
+
* @param chunkTimeout – milliseconds to wait for a new chunk before calling shouldTerminate()
|
|
25
|
+
* @param requireConsecutiveTermination – whether you need two time-outs in a row to break
|
|
26
|
+
*/
|
|
27
|
+
export async function processStreamingResponse(getStream, onChunk, shouldTerminate, chunkTimeout = 2000, requireConsecutiveTermination = true) {
|
|
28
|
+
const res = await getStream();
|
|
29
|
+
if (!res.body)
|
|
30
|
+
throw new Error('No streaming support');
|
|
31
|
+
const reader = res.body.getReader();
|
|
32
|
+
const decoder = new TextDecoder('utf-8');
|
|
33
|
+
const TIMEOUT = Symbol();
|
|
34
|
+
let exitCheckStreak = 0;
|
|
35
|
+
// Only one pending read promise at a time:
|
|
36
|
+
let readPromise = null;
|
|
37
|
+
try {
|
|
38
|
+
while (true) {
|
|
39
|
+
// Start a read if none in flight
|
|
40
|
+
if (!readPromise) {
|
|
41
|
+
readPromise = reader.read().then((r) => (r.done ? null : (r.value ?? new Uint8Array(0))));
|
|
42
|
+
}
|
|
43
|
+
// Race that single read against your timeout
|
|
44
|
+
const timeoutPromise = new Promise((r) => setTimeout(() => r(TIMEOUT), chunkTimeout));
|
|
45
|
+
const result = await Promise.race([readPromise, timeoutPromise]);
|
|
46
|
+
if (result === TIMEOUT) {
|
|
47
|
+
// no data yet, but the readPromise is still pending
|
|
48
|
+
const stop = await shouldTerminate();
|
|
49
|
+
if (stop) {
|
|
50
|
+
exitCheckStreak++;
|
|
51
|
+
if (!requireConsecutiveTermination || exitCheckStreak > 1) {
|
|
52
|
+
try {
|
|
53
|
+
await reader.cancel();
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
/* ignore */
|
|
57
|
+
}
|
|
58
|
+
readPromise = null;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
exitCheckStreak = 0;
|
|
64
|
+
}
|
|
65
|
+
// loop again—but do NOT overwrite readPromise!
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// readPromise has resolved
|
|
69
|
+
readPromise = null;
|
|
70
|
+
if (result === null) {
|
|
71
|
+
// stream closed
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
// valid chunk - use {stream: true} to buffer incomplete UTF-8 sequences
|
|
75
|
+
onChunk(decoder.decode(result, { stream: true }));
|
|
76
|
+
exitCheckStreak = 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
if (!isStreamTerminationError(err)) {
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
// Flush any remaining buffered bytes from the decoder
|
|
87
|
+
const remaining = decoder.decode();
|
|
88
|
+
if (remaining) {
|
|
89
|
+
onChunk(remaining);
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await reader.cancel();
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
/* ignore */
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Demultiplexes a WebSocket stream into separate stdout and stderr streams.
|
|
101
|
+
*
|
|
102
|
+
* @param socket - The WebSocket instance to demultiplex.
|
|
103
|
+
* @param onStdout - Callback function for stdout messages.
|
|
104
|
+
* @param onStderr - Callback function for stderr messages.
|
|
105
|
+
*/
|
|
106
|
+
export function stdDemuxStream(ws, onStdout, onStderr) {
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
// If running in a browser or any WebSocket supporting binaryType, use ArrayBuffer for binary data
|
|
109
|
+
if ('binaryType' in ws) {
|
|
110
|
+
ws.binaryType = 'arraybuffer'; // ensure binary frames yield ArrayBuffer, not Blob
|
|
111
|
+
}
|
|
112
|
+
// Separate decoders for stdout and stderr to maintain independent UTF-8 decoding state
|
|
113
|
+
const stdoutDecoder = new TextDecoder('utf-8');
|
|
114
|
+
const stderrDecoder = new TextDecoder('utf-8');
|
|
115
|
+
const buf = []; // Buffer to accumulate incoming chunks
|
|
116
|
+
let currentDataType = null; // Track current stream type
|
|
117
|
+
// Helper function to emit payload data
|
|
118
|
+
const emit = (payload) => {
|
|
119
|
+
if (payload.length === 0)
|
|
120
|
+
return;
|
|
121
|
+
// Use {stream: true} to buffer incomplete UTF-8 sequences for the next chunk
|
|
122
|
+
if (currentDataType === 'stdout') {
|
|
123
|
+
const text = stdoutDecoder.decode(payload, { stream: true });
|
|
124
|
+
onStdout(text);
|
|
125
|
+
}
|
|
126
|
+
else if (currentDataType === 'stderr') {
|
|
127
|
+
const text = stderrDecoder.decode(payload, { stream: true });
|
|
128
|
+
onStderr(text);
|
|
129
|
+
}
|
|
130
|
+
// If currentDataType is null, drop unlabeled bytes (shouldn't happen with proper labeling)
|
|
131
|
+
};
|
|
132
|
+
// Helper function to find a subarray within a larger array
|
|
133
|
+
const findSubarray = (haystack, needle) => {
|
|
134
|
+
if (needle.length === 0)
|
|
135
|
+
return 0;
|
|
136
|
+
if (haystack.length < needle.length)
|
|
137
|
+
return -1;
|
|
138
|
+
for (let i = 0; i <= haystack.length - needle.length; i++) {
|
|
139
|
+
let found = true;
|
|
140
|
+
for (let j = 0; j < needle.length; j++) {
|
|
141
|
+
if (haystack[i + j] !== needle[j]) {
|
|
142
|
+
found = false;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (found)
|
|
147
|
+
return i;
|
|
148
|
+
}
|
|
149
|
+
return -1;
|
|
150
|
+
};
|
|
151
|
+
// Event handler for incoming messages (Node: Buffer/ArrayBuffer/String; Browser: event.data etc.)
|
|
152
|
+
const handleMessage = (event) => {
|
|
153
|
+
// Normalize event/data between Node (ws) and browser WebSocket
|
|
154
|
+
const data = event && event instanceof Object && 'data' in event ? event.data : event;
|
|
155
|
+
try {
|
|
156
|
+
// Prepare a Uint8Array for the message data
|
|
157
|
+
let bytes;
|
|
158
|
+
if (typeof data === 'string') {
|
|
159
|
+
// Convert string to bytes for consistent byte-based demuxing
|
|
160
|
+
bytes = new TextEncoder().encode(data);
|
|
161
|
+
}
|
|
162
|
+
else if (data instanceof ArrayBuffer) {
|
|
163
|
+
bytes = new Uint8Array(data);
|
|
164
|
+
}
|
|
165
|
+
else if (ArrayBuffer.isView(data)) {
|
|
166
|
+
// Covers Node.js Buffer (Uint8Array subclass) and other TypedArrays
|
|
167
|
+
bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
168
|
+
}
|
|
169
|
+
else if (data instanceof Blob) {
|
|
170
|
+
// Browser binary frames might be Blob if binaryType wasn't set in time. Convert to ArrayBuffer asynchronously.
|
|
171
|
+
data.arrayBuffer().then((buf) => {
|
|
172
|
+
try {
|
|
173
|
+
processChunk(new Uint8Array(buf));
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
handleError(err);
|
|
177
|
+
}
|
|
178
|
+
}, (err) => {
|
|
179
|
+
handleError(err);
|
|
180
|
+
});
|
|
181
|
+
return; // will continue asynchronously once blob is read
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
throw new Error(`Unsupported message data type: ${Object.prototype.toString.call(data)}`);
|
|
185
|
+
}
|
|
186
|
+
// Process the chunk
|
|
187
|
+
processChunk(bytes);
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
// On any synchronous error in processing, clean up and reject.
|
|
191
|
+
cleanup();
|
|
192
|
+
try {
|
|
193
|
+
ws.close();
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
/* ignore if already closed */
|
|
197
|
+
}
|
|
198
|
+
reject(err);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
// Process a chunk of data with buffering and safe region handling
|
|
202
|
+
const processChunk = (chunk) => {
|
|
203
|
+
if (chunk.length === 0)
|
|
204
|
+
return;
|
|
205
|
+
// Add chunk to buffer
|
|
206
|
+
buf.push(...chunk);
|
|
207
|
+
// Process as much as we can, preserving only bytes that could be part of a prefix
|
|
208
|
+
while (true) {
|
|
209
|
+
const bufArray = new Uint8Array(buf);
|
|
210
|
+
// Calculate how many bytes we can safely process
|
|
211
|
+
// We need to keep bytes that could potentially be the start of a prefix marker
|
|
212
|
+
let safeLen = buf.length;
|
|
213
|
+
// Check if the last few bytes could be part of a prefix marker
|
|
214
|
+
if (buf.length >= MAX_PREFIX_LEN) {
|
|
215
|
+
// Check if the last byte could be part of a prefix (must be 0x01 or 0x02)
|
|
216
|
+
const lastByte = buf[buf.length - 1];
|
|
217
|
+
if (lastByte !== 0x01 && lastByte !== 0x02) {
|
|
218
|
+
// Last byte can't be part of any prefix, safe to process everything
|
|
219
|
+
safeLen = buf.length;
|
|
220
|
+
}
|
|
221
|
+
else if (buf.length >= MAX_PREFIX_LEN + 1) {
|
|
222
|
+
// Check second-to-last byte if buffer is long enough
|
|
223
|
+
const secondLastByte = buf[buf.length - 2];
|
|
224
|
+
if (secondLastByte !== 0x01 && secondLastByte !== 0x02) {
|
|
225
|
+
// Second-to-last byte can't be part of any prefix, safe to process all but last byte
|
|
226
|
+
safeLen = buf.length - 1;
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
// Both last bytes could be part of prefix, keep MAX_PREFIX_LEN - 1 bytes
|
|
230
|
+
safeLen = buf.length - (MAX_PREFIX_LEN - 1);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
// Buffer is exactly MAX_PREFIX_LEN, keep MAX_PREFIX_LEN - 1 bytes
|
|
235
|
+
safeLen = buf.length - (MAX_PREFIX_LEN - 1);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
// Buffer shorter than MAX_PREFIX_LEN, keep MAX_PREFIX_LEN - 1 bytes
|
|
240
|
+
safeLen = buf.length - (MAX_PREFIX_LEN - 1);
|
|
241
|
+
}
|
|
242
|
+
if (safeLen <= 0) {
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
// Find earliest next marker within the safe region
|
|
246
|
+
const safeRegion = bufArray.subarray(0, safeLen);
|
|
247
|
+
const stdoutIndex = findSubarray(safeRegion, STDOUT_PREFIX_BYTES);
|
|
248
|
+
const stderrIndex = findSubarray(safeRegion, STDERR_PREFIX_BYTES);
|
|
249
|
+
let nextIdx = -1;
|
|
250
|
+
let nextKind = null;
|
|
251
|
+
let nextLen = 0;
|
|
252
|
+
if (stdoutIndex !== -1 && (stderrIndex === -1 || stdoutIndex < stderrIndex)) {
|
|
253
|
+
nextIdx = stdoutIndex;
|
|
254
|
+
nextKind = 'stdout';
|
|
255
|
+
nextLen = STDOUT_PREFIX_BYTES.length;
|
|
256
|
+
}
|
|
257
|
+
else if (stderrIndex !== -1) {
|
|
258
|
+
nextIdx = stderrIndex;
|
|
259
|
+
nextKind = 'stderr';
|
|
260
|
+
nextLen = STDERR_PREFIX_BYTES.length;
|
|
261
|
+
}
|
|
262
|
+
if (nextIdx === -1) {
|
|
263
|
+
// No full marker in safe region: emit everything we safely can as payload
|
|
264
|
+
const toEmit = bufArray.subarray(0, safeLen);
|
|
265
|
+
emit(toEmit);
|
|
266
|
+
buf.splice(0, safeLen);
|
|
267
|
+
break; // wait for more data to resolve any partial marker at the end
|
|
268
|
+
}
|
|
269
|
+
// We found a marker. Emit preceding bytes (if any) under the current stream.
|
|
270
|
+
if (nextIdx > 0) {
|
|
271
|
+
const toEmit = bufArray.subarray(0, nextIdx);
|
|
272
|
+
emit(toEmit);
|
|
273
|
+
}
|
|
274
|
+
// Advance past the marker and switch current stream
|
|
275
|
+
buf.splice(0, nextIdx + nextLen);
|
|
276
|
+
currentDataType = nextKind;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
// Event handler for errors
|
|
280
|
+
const handleError = (error) => {
|
|
281
|
+
// Convert Event or plain error to Error instance for consistency
|
|
282
|
+
const err = error && error instanceof Event ? new Error('WebSocket error') : error;
|
|
283
|
+
cleanup();
|
|
284
|
+
try {
|
|
285
|
+
ws.close();
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
/* ignore if already closed */
|
|
289
|
+
}
|
|
290
|
+
reject(err);
|
|
291
|
+
};
|
|
292
|
+
// Event handler for socket closure
|
|
293
|
+
const handleClose = () => {
|
|
294
|
+
// Flush any remaining buffered payload on clean close
|
|
295
|
+
if (buf.length > 0 && currentDataType) {
|
|
296
|
+
const remainingBytes = new Uint8Array(buf);
|
|
297
|
+
// Use {stream: false} or omit to flush any buffered incomplete UTF-8 sequences
|
|
298
|
+
if (currentDataType === 'stdout') {
|
|
299
|
+
const text = stdoutDecoder.decode(remainingBytes, { stream: false });
|
|
300
|
+
onStdout(text);
|
|
301
|
+
}
|
|
302
|
+
else if (currentDataType === 'stderr') {
|
|
303
|
+
const text = stderrDecoder.decode(remainingBytes, { stream: false });
|
|
304
|
+
onStderr(text);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
// Flush any remaining bytes in the decoders even if buf is empty
|
|
309
|
+
const stdoutFlushed = stdoutDecoder.decode();
|
|
310
|
+
const stderrFlushed = stderrDecoder.decode();
|
|
311
|
+
if (stdoutFlushed)
|
|
312
|
+
onStdout(stdoutFlushed);
|
|
313
|
+
if (stderrFlushed)
|
|
314
|
+
onStderr(stderrFlushed);
|
|
315
|
+
}
|
|
316
|
+
cleanup();
|
|
317
|
+
resolve();
|
|
318
|
+
};
|
|
319
|
+
// Cleanup function to remove all listeners to avoid memory leaks
|
|
320
|
+
const cleanup = () => {
|
|
321
|
+
if (ws.removeEventListener) {
|
|
322
|
+
// Browser (EventTarget) style cleanup
|
|
323
|
+
ws.removeEventListener('message', handleMessage);
|
|
324
|
+
ws.removeEventListener('error', handleError);
|
|
325
|
+
ws.removeEventListener('close', handleClose);
|
|
326
|
+
}
|
|
327
|
+
if (ws.off) {
|
|
328
|
+
// Node.js ws (EventEmitter) style cleanup (supported in Node 14+)
|
|
329
|
+
ws.off('message', handleMessage);
|
|
330
|
+
ws.off('error', handleError);
|
|
331
|
+
ws.off('close', handleClose);
|
|
332
|
+
}
|
|
333
|
+
else if (ws.removeListener) {
|
|
334
|
+
// Node.js ws fallback for older Node versions
|
|
335
|
+
;
|
|
336
|
+
ws.removeListener('message', handleMessage);
|
|
337
|
+
ws.removeListener('error', handleError);
|
|
338
|
+
ws.removeListener('close', handleClose);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
// Attach event listeners in a way compatible with both Node (EventEmitter) and browser (EventTarget):
|
|
342
|
+
if (ws.addEventListener) {
|
|
343
|
+
// Browser or WebSocket implementation with EventTarget interface
|
|
344
|
+
ws.addEventListener('message', handleMessage);
|
|
345
|
+
ws.addEventListener('error', handleError);
|
|
346
|
+
ws.addEventListener('close', handleClose);
|
|
347
|
+
}
|
|
348
|
+
else if (ws.on) {
|
|
349
|
+
// Node.js ws library (EventEmitter) interface
|
|
350
|
+
ws.on('message', handleMessage); // ws@8+ yields Buffer for text frames, which we handle via TextDecoder
|
|
351
|
+
ws.on('error', handleError);
|
|
352
|
+
ws.on('close', handleClose);
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
// Unknown WebSocket interface - should not happen with isomorphic-ws
|
|
356
|
+
throw new Error('Unsupported WebSocket implementation');
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
//# sourceMappingURL=Stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Stream.js","sourceRoot":"","sources":["../../../../../../libs/sdk-typescript/src/utils/Stream.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAErF,SAAS,wBAAwB,CAAC,GAAY;IAC5C,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAEjD,MAAM,CAAC,GAAG,GAAU,CAAA;IAEpB,2CAA2C;IAC3C,IAAI,CAAC,CAAC,IAAI,KAAK,YAAY;QAAE,OAAO,IAAI,CAAA;IACxC,IAAI,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ,IAAI,aAAa,CAAC,IA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,CAAC,CACpD;gBAAC,EAAU,CAAC,cAAc,CAAC,OAAO,EAAE,WAAW,CAAC,CAChD;gBAAC,EAAU,CAAC,cAAc,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;YACnD,CAAC;QACH,CAAC,CAAA;QAED,sGAAsG;QACtG,IAAI,EAAE,CAAC,gBAAgB,EAAE,CAAC;YACxB,iEAAiE;YACjE,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,aAAoB,CAAC,CAAA;YACpD,EAAE,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAkB,CAAC,CAAA;YAChD,EAAE,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAkB,CAAC,CAAA;QAClD,CAAC;aAAM,IAAK,EAAU,CAAC,EAAE,EAAE,CAAC;YAC1B,8CAA8C;YAC9C,EAAE,CAAC,EAAE,CAAC,SAAS,EAAE,aAAa,CAAC,CAAA,CAAC,uEAAuE;YACvG,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;YAC3B,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;QAC7B,CAAC;aAAM,CAAC;YACN,qEAAqE;YACrE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;QACzD,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import WebSocket from 'isomorphic-ws';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an authenticated WebSocket connection to the sandbox toolbox.
|
|
4
|
+
*
|
|
5
|
+
* @param url - The websocket URL (ws[s]://...)
|
|
6
|
+
* @param headers - Headers to forward when running in Node environments
|
|
7
|
+
* @param getPreviewToken - Lazy getter for preview tokens (required for browser/serverless runtimes)
|
|
8
|
+
*/
|
|
9
|
+
export declare function createSandboxWebSocket(url: string, headers: Record<string, any>, getPreviewToken: () => Promise<string>): Promise<WebSocket>;
|
|
10
|
+
//# sourceMappingURL=WebSocket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocket.d.ts","sourceRoot":"","sources":["../../../../../../libs/sdk-typescript/src/utils/WebSocket.ts"],"names":[],"mappings":"AAKA,OAAO,SAAS,MAAM,eAAe,CAAA;AAGrC;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5B,eAAe,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,GACrC,OAAO,CAAC,SAAS,CAAC,CAWpB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Daytona Platforms Inc.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import WebSocket from 'isomorphic-ws';
|
|
6
|
+
import { RUNTIME, Runtime } from './Runtime.js';
|
|
7
|
+
/**
|
|
8
|
+
* Creates an authenticated WebSocket connection to the sandbox toolbox.
|
|
9
|
+
*
|
|
10
|
+
* @param url - The websocket URL (ws[s]://...)
|
|
11
|
+
* @param headers - Headers to forward when running in Node environments
|
|
12
|
+
* @param getPreviewToken - Lazy getter for preview tokens (required for browser/serverless runtimes)
|
|
13
|
+
*/
|
|
14
|
+
export async function createSandboxWebSocket(url, headers, getPreviewToken) {
|
|
15
|
+
if (RUNTIME === Runtime.BROWSER || RUNTIME === Runtime.DENO || RUNTIME === Runtime.SERVERLESS) {
|
|
16
|
+
const previewToken = await getPreviewToken();
|
|
17
|
+
const separator = url.includes('?') ? '&' : '?';
|
|
18
|
+
return new WebSocket(`${url}${separator}DAYTONA_SANDBOX_AUTH_KEY=${previewToken}`, `X-Daytona-SDK-Version~${String(headers['X-Daytona-SDK-Version'] ?? '')}`);
|
|
19
|
+
}
|
|
20
|
+
return new WebSocket(url, { headers });
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=WebSocket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocket.js","sourceRoot":"","sources":["../../../../../../libs/sdk-typescript/src/utils/WebSocket.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,SAAS,MAAM,eAAe,CAAA;AACrC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAE5C;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,GAAW,EACX,OAA4B,EAC5B,eAAsC;IAEtC,IAAI,OAAO,KAAK,OAAO,CAAC,OAAO,IAAI,OAAO,KAAK,OAAO,CAAC,IAAI,IAAI,OAAO,KAAK,OAAO,CAAC,UAAU,EAAE,CAAC;QAC9F,MAAM,YAAY,GAAG,MAAM,eAAe,EAAE,CAAA;QAC5C,MAAM,SAAS,GAAG,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAA;QAC/C,OAAO,IAAI,SAAS,CAClB,GAAG,GAAG,GAAG,SAAS,4BAA4B,YAAY,EAAE,EAC5D,yBAAyB,MAAM,CAAC,OAAO,CAAC,uBAAuB,CAAC,IAAI,EAAE,CAAC,EAAE,CAC1E,CAAA;IACH,CAAC;IAED,OAAO,IAAI,SAAS,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,CAAC,CAAA;AACxC,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for span instrumentation
|
|
3
|
+
*/
|
|
4
|
+
export interface SpanConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Custom name for the span. If not provided, uses `ClassName.methodName` format
|
|
7
|
+
*/
|
|
8
|
+
name?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Additional attributes to attach to the span
|
|
11
|
+
*/
|
|
12
|
+
attributes?: Record<string, string>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Configuration options for metric instrumentation
|
|
16
|
+
*/
|
|
17
|
+
export interface MetricConfig {
|
|
18
|
+
/**
|
|
19
|
+
* Custom name for the metric. If not provided, uses `ClassName.methodName` format
|
|
20
|
+
*/
|
|
21
|
+
name?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Description for the metrics being collected
|
|
24
|
+
*/
|
|
25
|
+
description?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Additional labels to attach to the metrics
|
|
28
|
+
*/
|
|
29
|
+
labels?: Record<string, string>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Configuration options for the combined instrumentation decorator
|
|
33
|
+
*/
|
|
34
|
+
export interface InstrumentationConfig {
|
|
35
|
+
/**
|
|
36
|
+
* Custom name for the span and metric. If not provided, uses `ClassName.methodName` format
|
|
37
|
+
*/
|
|
38
|
+
name?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Description for the metrics being collected
|
|
41
|
+
*/
|
|
42
|
+
description?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Additional labels/attributes to attach to spans and metrics
|
|
45
|
+
*/
|
|
46
|
+
labels?: Record<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* Enable trace collection (default: true)
|
|
49
|
+
*/
|
|
50
|
+
enableTraces?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Enable metrics collection (default: true)
|
|
53
|
+
*/
|
|
54
|
+
enableMetrics?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Decorator for instrumenting methods with OpenTelemetry spans (traces only)
|
|
58
|
+
*
|
|
59
|
+
* @param config - Configuration object or string name for the span
|
|
60
|
+
*
|
|
61
|
+
*/
|
|
62
|
+
export declare function WithSpan(config?: string | SpanConfig): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
63
|
+
/**
|
|
64
|
+
* Decorator for instrumenting methods with OpenTelemetry metrics (metrics only)
|
|
65
|
+
*
|
|
66
|
+
* Collects two metrics:
|
|
67
|
+
* - Counter: `{name}_executions` - tracks number of executions with status (success/error)
|
|
68
|
+
* - Histogram: `{name}_duration` - tracks execution duration in milliseconds
|
|
69
|
+
*
|
|
70
|
+
* @param config - Configuration object or string name for the metric
|
|
71
|
+
*
|
|
72
|
+
*/
|
|
73
|
+
export declare function WithMetric(config?: string | MetricConfig): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
74
|
+
/**
|
|
75
|
+
* Decorator for instrumenting methods with both OpenTelemetry traces and metrics
|
|
76
|
+
*
|
|
77
|
+
* This decorator composes @WithSpan and @WithMetric to provide both trace and metric collection.
|
|
78
|
+
* You can selectively enable/disable traces or metrics using the config options.
|
|
79
|
+
*
|
|
80
|
+
* @param config - Configuration object or string name for the instrumentation
|
|
81
|
+
*/
|
|
82
|
+
export declare function WithInstrumentation(config?: string | InstrumentationConfig): MethodDecorator;
|
|
83
|
+
//# sourceMappingURL=otel.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otel.decorator.d.ts","sourceRoot":"","sources":["../../../../../../libs/sdk-typescript/src/utils/otel.decorator.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B;;OAEG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB;;OAEG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAaD;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,UAAU,IAC3C,QAAQ,MAAM,EAAE,aAAa,MAAM,GAAG,MAAM,EAAE,YAAY,kBAAkB,UA0CrF;AAED;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,IAC/C,QAAQ,MAAM,EAAE,aAAa,MAAM,GAAG,MAAM,EAAE,YAAY,kBAAkB,UA6CrF;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,qBAAqB,GAAG,eAAe,CAiB5F"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Daytona Platforms Inc.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { trace, context, metrics, SpanStatusCode } from '@opentelemetry/api';
|
|
6
|
+
// Lazy initialization to ensure SDK is started before getting tracer/meter
|
|
7
|
+
const getTracer = () => trace.getTracer('');
|
|
8
|
+
const getMeter = () => metrics.getMeter('');
|
|
9
|
+
const executionHistograms = new Map();
|
|
10
|
+
/**
|
|
11
|
+
* Converts a string to snake_case for Prometheus-friendly metric names
|
|
12
|
+
*/
|
|
13
|
+
function toSnakeCase(str) {
|
|
14
|
+
return str
|
|
15
|
+
.replace(/([A-Z])/g, '_$1')
|
|
16
|
+
.toLowerCase()
|
|
17
|
+
.replace(/^_/, '')
|
|
18
|
+
.replace(/\./g, '_');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Decorator for instrumenting methods with OpenTelemetry spans (traces only)
|
|
22
|
+
*
|
|
23
|
+
* @param config - Configuration object or string name for the span
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
export function WithSpan(config) {
|
|
27
|
+
return (target, propertyKey, descriptor) => {
|
|
28
|
+
const originalMethod = descriptor.value;
|
|
29
|
+
const methodName = String(propertyKey);
|
|
30
|
+
descriptor.value = async function (...args) {
|
|
31
|
+
const cfg = typeof config === 'string' ? { name: config } : config || {};
|
|
32
|
+
const { name, attributes = {} } = cfg;
|
|
33
|
+
const spanName = name || `${target.constructor.name}.${methodName}`;
|
|
34
|
+
const allAttributes = {
|
|
35
|
+
component: target.constructor.name,
|
|
36
|
+
method: methodName,
|
|
37
|
+
...attributes,
|
|
38
|
+
};
|
|
39
|
+
const span = getTracer().startSpan(spanName, {
|
|
40
|
+
attributes: allAttributes,
|
|
41
|
+
}, context.active());
|
|
42
|
+
return context.with(trace.setSpan(context.active(), span), async () => {
|
|
43
|
+
try {
|
|
44
|
+
const result = await originalMethod.apply(this, args);
|
|
45
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
span.setStatus({
|
|
50
|
+
code: SpanStatusCode.ERROR,
|
|
51
|
+
message: error instanceof Error ? error.message : String(error),
|
|
52
|
+
});
|
|
53
|
+
span.recordException(error instanceof Error ? error : new Error(String(error)));
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
span.end();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Decorator for instrumenting methods with OpenTelemetry metrics (metrics only)
|
|
65
|
+
*
|
|
66
|
+
* Collects two metrics:
|
|
67
|
+
* - Counter: `{name}_executions` - tracks number of executions with status (success/error)
|
|
68
|
+
* - Histogram: `{name}_duration` - tracks execution duration in milliseconds
|
|
69
|
+
*
|
|
70
|
+
* @param config - Configuration object or string name for the metric
|
|
71
|
+
*
|
|
72
|
+
*/
|
|
73
|
+
export function WithMetric(config) {
|
|
74
|
+
return (target, propertyKey, descriptor) => {
|
|
75
|
+
const originalMethod = descriptor.value;
|
|
76
|
+
const methodName = String(propertyKey);
|
|
77
|
+
descriptor.value = async function (...args) {
|
|
78
|
+
const cfg = typeof config === 'string' ? { name: config } : config || {};
|
|
79
|
+
const { name, description, labels = {} } = cfg;
|
|
80
|
+
const metricName = toSnakeCase(name || `${target.constructor.name}.${methodName}`);
|
|
81
|
+
const allLabels = {
|
|
82
|
+
component: target.constructor.name,
|
|
83
|
+
method: methodName,
|
|
84
|
+
...labels,
|
|
85
|
+
};
|
|
86
|
+
// Get or create histogram for this method
|
|
87
|
+
if (!executionHistograms.has(metricName)) {
|
|
88
|
+
executionHistograms.set(metricName, getMeter().createHistogram(`${metricName}_duration`, {
|
|
89
|
+
description: description || `Duration of executions for ${metricName}`,
|
|
90
|
+
unit: 'ms',
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
const histogram = executionHistograms.get(metricName);
|
|
94
|
+
if (!histogram) {
|
|
95
|
+
throw new Error(`Histogram not found for metric: ${metricName}`);
|
|
96
|
+
}
|
|
97
|
+
const startTime = Date.now();
|
|
98
|
+
let status = 'success';
|
|
99
|
+
try {
|
|
100
|
+
const result = await originalMethod.apply(this, args);
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
status = 'error';
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
const duration = Date.now() - startTime;
|
|
109
|
+
histogram.record(duration, { ...allLabels, status });
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Decorator for instrumenting methods with both OpenTelemetry traces and metrics
|
|
116
|
+
*
|
|
117
|
+
* This decorator composes @WithSpan and @WithMetric to provide both trace and metric collection.
|
|
118
|
+
* You can selectively enable/disable traces or metrics using the config options.
|
|
119
|
+
*
|
|
120
|
+
* @param config - Configuration object or string name for the instrumentation
|
|
121
|
+
*/
|
|
122
|
+
export function WithInstrumentation(config) {
|
|
123
|
+
const cfg = typeof config === 'string' ? { name: config } : config || {};
|
|
124
|
+
const { enableTraces = true, enableMetrics = true, name, description, labels } = cfg;
|
|
125
|
+
const decorators = [];
|
|
126
|
+
if (enableTraces) {
|
|
127
|
+
decorators.push(WithSpan({ name, attributes: labels }));
|
|
128
|
+
}
|
|
129
|
+
if (enableMetrics) {
|
|
130
|
+
decorators.push(WithMetric({ name, description, labels }));
|
|
131
|
+
}
|
|
132
|
+
return (target, propertyKey, descriptor) => {
|
|
133
|
+
decorators.forEach((decorator) => decorator(target, propertyKey, descriptor));
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=otel.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otel.decorator.js","sourceRoot":"","sources":["../../../../../../libs/sdk-typescript/src/utils/otel.decorator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAa,MAAM,oBAAoB,CAAA;AAEvF,2EAA2E;AAC3E,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;AAC3C,MAAM,QAAQ,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;AAE3C,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAqB,CAAA;AA4DxD;;GAEG;AACH,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,GAAG;SACP,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC;SAC1B,WAAW,EAAE;SACb,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;SACjB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,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