(options?: {
readonly contentType?: string | undefined
readonly maxBufferSize?: number | "unbounded" | undefined
})
| 178 | * @category serialization |
| 179 | */ |
| 180 | export const ndJsonRpc = (options?: { |
| 181 | readonly contentType?: string | undefined |
| 182 | readonly maxBufferSize?: number | "unbounded" | undefined |
| 183 | }): RpcSerialization["Type"] => |
| 184 | RpcSerialization.of({ |
| 185 | contentType: options?.contentType ?? "application/json-rpc", |
| 186 | includesFraming: true, |
| 187 | unsafeMake: () => { |
| 188 | const parser = makeNdjson(options).unsafeMake() |
| 189 | const batches = new Map<string, { |
| 190 | readonly size: number |
| 191 | readonly responses: Map<string, RpcMessage.FromServerEncoded> |
| 192 | }>() |
| 193 | return ({ |
| 194 | decode: (bytes) => { |
| 195 | const frames = parser.decode(bytes) |
| 196 | if (frames.length === 0) return [] |
| 197 | const messages: Array<RpcMessage.FromClientEncoded | RpcMessage.FromServerEncoded> = [] |
| 198 | for (let i = 0; i < frames.length; i++) { |
| 199 | const frame = frames[i] |
| 200 | // eslint-disable-next-line no-restricted-syntax |
| 201 | messages.push(...decodeJsonRpcRaw(frame as any, batches) as any) |
| 202 | } |
| 203 | return messages |
| 204 | }, |
| 205 | encode: (response) => { |
| 206 | const encoded = encodeJsonRpcResponse(response as any, batches) |
| 207 | return encoded && parser.encode(encoded) |
| 208 | } |
| 209 | }) |
| 210 | } |
| 211 | }) |
| 212 | |
| 213 | function decodeJsonRpcRaw( |
| 214 | decoded: JsonRpcMessage | Array<JsonRpcMessage>, |
no test coverage detected