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

Function makeNdjson

packages/rpc/src/RpcSerialization.ts:82–137  ·  view source on GitHub ↗
(options?: StreamOptions)

Source from the content-addressed store, hash-verified

80 * @category serialization
81 */
82export const makeNdjson = (options?: StreamOptions): RpcSerialization["Type"] => {
83 const maxBufferSize = options?.maxBufferSize ?? defaultMaxBufferSize
84 return RpcSerialization.of({
85 contentType: "application/ndjson",
86 includesFraming: true,
87 unsafeMake: () => {
88 const decoder = new TextDecoder()
89 let buffer = ""
90 return ({
91 decode: (bytes) => {
92 buffer += typeof bytes === "string" ? bytes : decoder.decode(bytes)
93 let position = 0
94 let nlIndex = buffer.indexOf("\n", position)
95 const items: Array<unknown> = []
96 while (nlIndex !== -1) {
97 const bufferSize = nlIndex - position
98 if (isBufferSizeExceeded(bufferSize, maxBufferSize)) {
99 buffer = ""
100 throw new RpcSerializationError({
101 reason: "BufferSizeExceeded",
102 maxBufferSize,
103 bufferSize
104 })
105 }
106 const item = JSON.parse(buffer.slice(position, nlIndex))
107 items.push(item)
108 position = nlIndex + 1
109 nlIndex = buffer.indexOf("\n", position)
110 }
111 buffer = buffer.slice(position)
112 const bufferSize = buffer.length
113 if (isBufferSizeExceeded(bufferSize, maxBufferSize)) {
114 buffer = ""
115 throw new RpcSerializationError({
116 reason: "BufferSizeExceeded",
117 maxBufferSize,
118 bufferSize
119 })
120 }
121 return items
122 },
123 encode: (response) => {
124 if (Array.isArray(response)) {
125 if (response.length === 0) return undefined
126 let data = ""
127 for (let i = 0; i < response.length; i++) {
128 data += JSON.stringify(response[i]) + "\n"
129 }
130 return data
131 }
132 return JSON.stringify(response) + "\n"
133 }
134 })
135 }
136 })
137}
138
139/**

Callers 3

ndJsonRpcFunction · 0.85
layerNdjsonWithFunction · 0.85

Calls 4

isBufferSizeExceededFunction · 0.85
decodeMethod · 0.80
parseMethod · 0.80
ofMethod · 0.65

Tested by

no test coverage detected