forked from stripe/sync-engine
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathndjson.ts
More file actions
76 lines (73 loc) · 2.25 KB
/
ndjson.ts
File metadata and controls
76 lines (73 loc) · 2.25 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
72
73
74
75
76
/** Parse an NDJSON string into an AsyncIterable of parsed objects. */
export async function* parseNdjson<T = unknown>(text: string): AsyncIterable<T> {
for (const line of text.split('\n')) {
const trimmed = line.trim()
if (trimmed.length > 0) {
yield JSON.parse(trimmed) as T
}
}
}
/** Serialize an AsyncIterable as a streaming NDJSON ReadableStream. */
export function toNdjsonStream(iter: AsyncIterable<unknown>): ReadableStream<Uint8Array> {
const enc = new TextEncoder()
const iterator = iter[Symbol.asyncIterator]()
return new ReadableStream({
async start(controller) {
try {
while (true) {
const result = await iterator.next()
if (result.done) break
controller.enqueue(enc.encode(JSON.stringify(result.value) + '\n'))
}
controller.close()
} catch (err) {
controller.error(err)
}
},
cancel() {
iterator.return?.()
},
})
}
/** Parse NDJSON from an AsyncIterable of chunks (e.g. Node process.stdin). */
export async function* parseNdjsonChunks<T = unknown>(
chunks: AsyncIterable<Uint8Array | string>
): AsyncIterable<T> {
const decoder = new TextDecoder()
let buffer = ''
for await (const chunk of chunks) {
buffer += typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true })
const lines = buffer.split('\n')
// Keep the last (possibly incomplete) line in the buffer
buffer = lines.pop() ?? ''
for (const line of lines) {
const trimmed = line.trim()
if (trimmed.length > 0) {
yield JSON.parse(trimmed) as T
}
}
}
// Handle any trailing content without final newline
const trimmed = buffer.trim()
if (trimmed.length > 0) {
yield JSON.parse(trimmed) as T
}
}
/** Parse an NDJSON ReadableStream (HTTP request body) into an AsyncIterable. */
export async function* parseNdjsonStream<T = unknown>(
stream: ReadableStream<Uint8Array>
): AsyncIterable<T> {
const reader = stream.getReader()
async function* toChunks(): AsyncIterable<Uint8Array> {
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
yield value
}
} finally {
reader.releaseLock()
}
}
yield* parseNdjsonChunks<T>(toChunks())
}