Scoop API

scoop:scoop.http.asyncio:0.1.0

src/sse/source.scoop

Selected production source · src/sse/source.scoop

1package scoop.http.asyncio
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.platform.memory.NativeByteBuffer
7/**
8 * A single-pass event-stream reader over a borrowed source by default. It tracks last-event ID and
9 * retry fields but does not open or reconnect network transports.
10 */
11public class SseSource private constructor(private val reader: SseReader) : AsyncCloseable {
12 public constructor(source: AsyncByteSource, limits: SseLimits = SseLimits(), initialLastEventId: String = "", bufferSize: Long = 8192L)
13 : this(createSseReader(source, None, limits, initialLastEventId, bufferSize)) {}
15 /**
16 * Factories that transfer underlying endpoint cleanup responsibility.
17 */
18 public companion object {
19 /**
20 * Creates the adapter and assumes cleanup responsibility for the supplied asynchronous
21 * endpoint.
22 */
23 public fun <T> takeOwnership(source: T, limits: SseLimits = SseLimits(), initialLastEventId: String = "", bufferSize: Long = 8192L): SseSource
24 where T : AsyncByteSource, T : AsyncCloseable =
25 SseSource(createSseReader(source, Some<AsyncCloseable>(source), limits, initialLastEventId, bufferSize))
26 }
28 /**
29 * Whether the complete protocol boundary or input end has been reached.
30 */
31 public val isFinished: Boolean get() = reader.decoder.isFinished
32 /**
33 * Whether adapter cleanup has completed.
34 */
35 public val isClosed: Boolean get() = reader.io.isClosed
36 /**
37 * The current event-stream last-event ID, including updates with no dispatched event.
38 */
39 public val lastEventId: String get() = reader.decoder.lastEventId
40 /**
41 * The most recent valid server retry interval in milliseconds, if supplied.
42 */
43 public val retryMilliseconds: Long? get() = reader.decoder.retryMilliseconds
44 /**
45 * The retained terminal exception, or None before failure.
46 */
47 public val failure: Throwable? get() = reader.io.failure
48 /**
49 * Reads until an event is available or input ends, updating stream ID and retry state.
50 */
51 public suspend fun next(): SseEvent? = reader.next()
52 /**
53 * Closes parser/writer state and an endpoint whose ownership was transferred; borrowed
54 * endpoints retain their own cleanup responsibility.
55 */
56 public override suspend fun close() { reader.io.close() }
59private fun createSseReader(source: AsyncByteSource, owner: AsyncCloseable?, limits: SseLimits, initialLastEventId: String, bufferSize: Long): SseReader {
60 val loop = currentEventLoop()
61 val job = httpJob(loop, true)
62 if (bufferSize <= 0L) throw IllegalArgumentException(Some("SSE buffer size must be positive"))
63 val decoder = SseDecoder(limits, initialLastEventId)
64 val input = try { NativeByteBuffer.allocate(bufferSize) }
65 catch (error: Throwable) {
66 val failure = messageCleanup(Some(error)) { decoder.close() }
67 throw failure!!
68 }
69 try {
70 job.ensureActive()
71 return SseReader(source, owner, loop, decoder, input)
72 } catch (error: Throwable) {
73 var failure = messageCleanup(Some(error)) { input.close() }
74 failure = messageCleanup(failure) { decoder.close() }
75 throw failure!!
76 }