MCPcopy Create free account
hub / github.com/middleapi/orpc / init

Method init

packages/server/src/plugins/batch.ts:69–193  ·  view source on GitHub ↗
(options: StandardHandlerOptions<T>)

Source from the content-addressed store, hash-verified

67 }
68
69 init(options: StandardHandlerOptions<T>): void {
70 options.rootInterceptors ??= []
71
72 options.rootInterceptors.unshift(async (options) => {
73 const xHeader = flattenHeader(options.request.headers['x-orpc-batch'])
74
75 if (xHeader === undefined) {
76 return options.next()
77 }
78
79 let isParsing = false
80
81 try {
82 return await runWithSpan({ name: 'handle_batch_request' }, async (span) => {
83 const mode = xHeader === 'buffered' ? 'buffered' : 'streaming'
84
85 isParsing = true
86 const parsed = parseBatchRequest({ ...options.request, body: await options.request.body() })
87 isParsing = false
88
89 span?.setAttribute('batch.mode', mode)
90 span?.setAttribute('batch.size', parsed.length)
91
92 const maxSize = await value(this.maxSize, options)
93
94 if (parsed.length > maxSize) {
95 const message = 'Batch request size exceeds the maximum allowed size'
96 setSpanError(span, message)
97
98 return {
99 matched: true,
100 response: {
101 status: 413,
102 headers: {},
103 body: message,
104 },
105 }
106 }
107
108 const responses: Promise<BatchResponseBodyItem>[] = parsed
109 .map((request, index) => {
110 const mapped = this.mapRequestItem(request, options)
111
112 return options
113 .next({ ...options, request: { ...mapped, body: () => Promise.resolve(mapped.body) } })
114 .then(({ response, matched }) => {
115 span?.addEvent(`response.${index}.${matched ? 'success' : 'not_matched'}`)
116
117 if (matched) {
118 if (
119 response.body instanceof Blob
120 || response.body instanceof FormData
121 || isAsyncIteratorObject(response.body)
122 ) {
123 return {
124 index,
125 status: 500,
126 headers: {},

Callers

nothing calls this directly

Calls 12

flattenHeaderFunction · 0.90
runWithSpanFunction · 0.90
parseBatchRequestFunction · 0.90
valueFunction · 0.90
setSpanErrorFunction · 0.90
isAsyncIteratorObjectFunction · 0.90
toBatchResponseFunction · 0.90
bodyMethod · 0.80
setAttributeMethod · 0.80
mapRequestItemMethod · 0.80
thenMethod · 0.80
nextMethod · 0.45

Tested by

no test coverage detected