Files
orca/src/shared/remote-runtime-shared-control-ready.ts
T
NeilandOrca 879aad7dd6 oom(foundation): bound shared readers/limits + add BoundedMap primitive (#10299)
* oom(01): A1-shared-readers — reintroduce #10179 subset

Files: 18 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(02): A2-shared-image-media — reintroduce #10179 subset

Files: 7 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(03): A3-shared-fs-listing — reintroduce #10179 subset

Files: 21 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(04): A4-shared-remote-relay — reintroduce #10179 subset

Files: 8 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(05): A5-shared-misc — reintroduce #10179 subset

Files: 28 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(06): B-shared-wiring — reintroduce #10179 subset

Files: 81 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-24 21:36:57 -07:00

75 lines
2.1 KiB
TypeScript

import WebSocket from 'ws'
import { RemoteRuntimeClientError } from './remote-runtime-client-error'
import { REMOTE_RUNTIME_MAX_READY_WAITERS } from './remote-runtime-memory-limits'
import { remoteRuntimeUnavailableError } from './remote-runtime-request-frames'
import type {
SharedControlConnectionState,
SharedControlReadyWaiter
} from './remote-runtime-shared-control-types'
export function isSharedControlReady(args: {
state: SharedControlConnectionState
ws: WebSocket | null
sharedKey: Uint8Array | null
}): boolean {
return args.state === 'ready' && args.ws?.readyState === WebSocket.OPEN && !!args.sharedKey
}
export function waitForSharedControlReadyWithTimeout(args: {
readyWaiters: SharedControlReadyWaiter[]
timeoutMs: number
open: () => void
}): Promise<void> {
if (args.readyWaiters.length >= REMOTE_RUNTIME_MAX_READY_WAITERS) {
return Promise.reject(
new RemoteRuntimeClientError(
'remote_runtime_busy',
'Remote runtime connection wait limit reached; retry after pending work finishes.'
)
)
}
return new Promise<void>((resolve, reject) => {
let settled = false
let waiter!: SharedControlReadyWaiter
const timeout = setTimeout(() => {
if (settled) {
return
}
settled = true
const index = args.readyWaiters.indexOf(waiter)
if (index >= 0) {
args.readyWaiters.splice(index, 1)
}
reject(remoteRuntimeUnavailableError())
}, args.timeoutMs)
waiter = {
resolve: () => {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
resolve()
},
reject: (error) => {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
reject(error)
}
}
args.readyWaiters.push(waiter)
try {
args.open()
} catch (error) {
const index = args.readyWaiters.indexOf(waiter)
if (index >= 0) {
args.readyWaiters.splice(index, 1)
}
waiter.reject(error instanceof Error ? error : remoteRuntimeUnavailableError(String(error)))
}
})
}