MCPcopy Create free account
hub / github.com/Effect-TS/effect / ndJsonRpc

Function ndJsonRpc

packages/effect/src/unstable/rpc/RpcSerialization.ts:215–245  ·  view source on GitHub ↗
(options?: {
  readonly contentType?: string | undefined
  readonly maxBufferSize?: number | "unbounded" | undefined
})

Source from the content-addressed store, hash-verified

213 * @since 4.0.0
214 */
215export 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
247function decodeJsonRpcRaw(
248 decoded: JsonRpcMessage | Array<JsonRpcMessage>,

Callers 1

layerNdJsonRpcFunction · 0.85

Calls 5

makeNdjsonFunction · 0.85
decodeJsonRpcRawFunction · 0.85
encodeJsonRpcResponseFunction · 0.85
pushMethod · 0.80
ofMethod · 0.65

Tested by

no test coverage detected