Files
orca/src/shared/remote-runtime-memory-limits.ts
T
Neilandsotashimozono 37c00630d4 fix(remote-files): open large HTML, PDFs and images on a paired runtime (#29426)
* fix(remote-files): open HTML, PDFs and images too large for one reply on a paired runtime

files.read stops at 512 KiB and files.readPreview at about 3 MB (one 4 MiB
reply, after base64), so larger files on a paired runtime failed to open.
Read them in chunks with the files.stat and files.readChunk methods hosts
already have, and refuse a save the host's 1 MiB frame cap would answer by
closing the connection.

Fixes #29306
Fixes #26062

Co-authored-by: sotashimozono <shimozono-sota631@g.ecc.u-tokyo.ac.jp>

* fix(remote-files): keep a UTF-8 BOM when paging a large remote text file

* fix(remote-files): pin a paged read to one pairing and compare ctime

* fix(remote-files): pin the pairing before a remote read's first request

---------

Co-authored-by: sotashimozono <shimozono-sota631@g.ecc.u-tokyo.ac.jp>
2026-10-15 23:21:07 -07:00

93 lines
3.9 KiB
TypeScript

import {
JsonStringifyByteLimitError,
stringifyJsonWithinByteLimit
} from './node-bounded-json-stringify'
import { RemoteRuntimeClientError } from './remote-runtime-client-error'
import type { RuntimeOrchestrationEnvelope } from './runtime-rpc-envelope'
export const REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES = 4 * 1024 * 1024
// Why: a host refuses a larger client frame by closing the socket (1009), failing every call on it.
export const REMOTE_RUNTIME_MAX_INBOUND_FRAME_BYTES = 1024 * 1024
// Why: sealing inflates a request by up to 4/3, so this much JSON-escaped content still fits a frame.
export const REMOTE_RUNTIME_MAX_FILE_WRITE_CONTENT_BYTES = 720 * 1024
export const REMOTE_RUNTIME_MAX_WEBSOCKET_FRAME_BYTES = 8 * 1024 * 1024 + 64
export const REMOTE_RUNTIME_MAX_SUBSCRIPTIONS = 256
export const REMOTE_RUNTIME_MAX_SUBSCRIPTION_PARAM_BYTES = 1024 * 1024
export const REMOTE_RUNTIME_MAX_RETAINED_SUBSCRIPTION_BYTES = 16 * 1024 * 1024
export const REMOTE_RUNTIME_MAX_PENDING_REQUESTS = 256
export const REMOTE_RUNTIME_MAX_PENDING_RPC_BYTES = 32 * 1024 * 1024
export const REMOTE_RUNTIME_MAX_PREPARED_RPC_BYTES = REMOTE_RUNTIME_MAX_PENDING_RPC_BYTES
export const REMOTE_RUNTIME_MAX_PROCESS_PENDING_REQUESTS = REMOTE_RUNTIME_MAX_PENDING_REQUESTS * 2
export const REMOTE_RUNTIME_MAX_PROCESS_PENDING_RPC_BYTES = REMOTE_RUNTIME_MAX_PENDING_RPC_BYTES * 2
export const REMOTE_RUNTIME_MAX_READY_WAITERS =
REMOTE_RUNTIME_MAX_PENDING_REQUESTS + REMOTE_RUNTIME_MAX_SUBSCRIPTIONS
export const REMOTE_RUNTIME_MAX_OUTBOUND_BINARY_FRAME_BYTES = 8 * 1024 * 1024
export function serializeRemoteRuntimePayload(value: unknown): string {
try {
return stringifyJsonWithinByteLimit(value, REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES).serialized
} catch (error) {
if (error instanceof JsonStringifyByteLimitError) {
throw new RemoteRuntimeClientError(
'invalid_argument',
`Remote runtime JSON payload exceeds ${REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES} bytes.`
)
}
const message = error instanceof Error ? error.message : String(error)
throw new RemoteRuntimeClientError(
'invalid_argument',
`Remote runtime JSON payload could not be serialized: ${message}`
)
}
}
export function measureRemoteRuntimeSubscriptionParams(params: unknown): number {
if (params === undefined) {
return 0
}
try {
return stringifyJsonWithinByteLimit(params, REMOTE_RUNTIME_MAX_SUBSCRIPTION_PARAM_BYTES)
.byteLength
} catch (error) {
if (error instanceof JsonStringifyByteLimitError) {
throw new RemoteRuntimeClientError(
'invalid_argument',
`Remote runtime subscription parameters exceed ${REMOTE_RUNTIME_MAX_SUBSCRIPTION_PARAM_BYTES} bytes.`
)
}
const message = error instanceof Error ? error.message : String(error)
throw new RemoteRuntimeClientError(
'invalid_argument',
`Remote runtime subscription parameters could not be serialized: ${message}`
)
}
}
export function serializeRemoteRuntimeRpcRequest(args: {
requestId: string
deviceToken: string
method: string
params: unknown
envelope?: RuntimeOrchestrationEnvelope
}): string {
return serializeRemoteRuntimePayload({
id: args.requestId,
deviceToken: args.deviceToken,
method: args.method,
params: args.params,
orchestrationCapability: args.envelope?.orchestrationCapability,
orchestrationContractVersion: args.envelope?.orchestrationContractVersion,
orchestrationRequestId: args.envelope?.orchestrationRequestId,
compatibilityInvocationId: args.envelope?.compatibilityInvocationId,
orchestrationCompatibilityEvidence: args.envelope?.orchestrationCompatibilityEvidence
})
}
export function retainedRemoteRuntimeJsonStringBytes(value: string): number {
return value.length * 3
}
export function isRemoteRuntimeBinaryFrameWithinLimit(bytes: Uint8Array<ArrayBufferLike>): boolean {
return bytes.byteLength <= REMOTE_RUNTIME_MAX_OUTBOUND_BINARY_FRAME_BYTES
}