-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathutils.ts
More file actions
71 lines (58 loc) · 1.79 KB
/
utils.ts
File metadata and controls
71 lines (58 loc) · 1.79 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
import { TimeoutError } from 'e2b'
export function formatRequestTimeoutError(error: unknown) {
if (error instanceof Error && error.name === 'AbortError') {
return new TimeoutError(
"Request timed out — the 'requestTimeoutMs' option can be used to increase this timeout"
)
}
return error
}
export function formatExecutionTimeoutError(error: unknown) {
if (error instanceof Error && error.name === 'AbortError') {
return new TimeoutError(
"Execution timed out — the 'timeoutMs' option can be used to increase this timeout"
)
}
return error
}
export async function* readLines(stream: ReadableStream<Uint8Array>) {
const reader = stream.getReader()
const decoder = new TextDecoder()
const pending: string[] = []
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
if (pending.length > 0) {
yield pending.join('')
}
break
}
if (value !== undefined) {
const chunk = decoder.decode(value, { stream: true })
if (chunk.indexOf('\n') === -1) {
// No newline — accumulate in O(1)
pending.push(chunk)
continue
}
// Chunk contains newline(s) — split and yield complete lines
const parts = chunk.split('\n')
// First part completes the pending line
pending.push(parts[0])
yield pending.join('')
pending.length = 0
// Middle parts are already complete lines
for (let i = 1; i < parts.length - 1; i++) {
yield parts[i]
}
// Last part starts a new pending line (may be empty)
const last = parts[parts.length - 1]
if (last.length > 0) {
pending.push(last)
}
}
}
} finally {
reader.releaseLock()
}
}