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

Function ndJsonRpc

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

Source from the content-addressed store, hash-verified

178 * @category serialization
179 */
180export 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
213function decodeJsonRpcRaw(
214 decoded: JsonRpcMessage | Array<JsonRpcMessage>,

Callers 1

layerNdJsonRpcFunction · 0.85

Calls 6

makeNdjsonFunction · 0.85
decodeJsonRpcRawFunction · 0.85
encodeJsonRpcResponseFunction · 0.85
decodeMethod · 0.80
encodeMethod · 0.80
ofMethod · 0.65

Tested by

no test coverage detected