(options: StandardHandlerOptions<T>)
| 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: {}, |
nothing calls this directly
no test coverage detected