(options?: {
readonly contentType?: string | undefined
readonly maxBufferSize?: number | "unbounded" | undefined
})
| 213 | * @since 4.0.0 |
| 214 | */ |
| 215 | export const ndJsonRpc = (options?: { |
| 216 | readonly contentType?: string | undefined |
| 217 | readonly maxBufferSize?: number | "unbounded" | undefined |
| 218 | }): RpcSerialization["Service"] => |
| 219 | RpcSerialization.of({ |
| 220 | contentType: options?.contentType ?? "application/json-rpc", |
| 221 | includesFraming: true, |
| 222 | makeUnsafe: () => { |
| 223 | const parser = makeNdjson({ maxBufferSize: options?.maxBufferSize }).makeUnsafe() |
| 224 | const batches = new Map<string, { |
| 225 | readonly size: number |
| 226 | readonly responses: Map<string, RpcMessage.FromServerEncoded> |
| 227 | }>() |
| 228 | return ({ |
| 229 | decode: (bytes) => { |
| 230 | const frames = parser.decode(bytes) |
| 231 | if (frames.length === 0) return [] |
| 232 | const messages: Array<RpcMessage.FromClientEncoded | RpcMessage.FromServerEncoded> = [] |
| 233 | for (let i = 0; i < frames.length; i++) { |
| 234 | const frame = frames[i] |
| 235 | messages.push(...decodeJsonRpcRaw(frame as any, batches) as any) |
| 236 | } |
| 237 | return messages |
| 238 | }, |
| 239 | encode: (response) => { |
| 240 | const encoded = encodeJsonRpcResponse(response as any, batches) |
| 241 | return encoded && parser.encode(encoded) |
| 242 | } |
| 243 | }) |
| 244 | } |
| 245 | }) |
| 246 | |
| 247 | function decodeJsonRpcRaw( |
| 248 | decoded: JsonRpcMessage | Array<JsonRpcMessage>, |
no test coverage detected