MCPcopy
hub / github.com/prisma/prisma / getDMMFBuffered

Function getDMMFBuffered

packages/get-dmmf/src/index.ts:110–161  ·  view source on GitHub ↗

* Read DMMF from the handle-based buffered WASM API in chunks and parse via * chunked string decoding. This bypasses V8's string length limit by reading * the DMMF as Uint8Array chunks from a caller-owned DmmfBuffer handle. * * Requires `get_dmmf_buffered()` (returns a `DmmfBuffer` handle) from

(params: string)

Source from the content-addressed store, hash-verified

108 * See: https://github.com/prisma/prisma/issues/29111
109 */
110function getDMMFBuffered(params: string): DMMF.Document | GetDMMFError {
111 const CHUNK_SIZE = 16 * 1024 * 1024 // 16MB chunks — well under V8 string limit
112
113 if (typeof prismaSchemaWasm.get_dmmf_buffered !== 'function') {
114 return {
115 type: 'wasm-error' as const,
116 reason: '(get-dmmf-buffered wasm)',
117 error: new Error(
118 "Buffered DMMF API not available. It's required for schemas that do not fit within the default V8 memory limit. Ensure you are using latest @prisma/prisma-schema-wasm.",
119 ),
120 }
121 }
122
123 const buffer = prismaSchemaWasm.get_dmmf_buffered(params)
124
125 try {
126 const totalBytes = buffer.len()
127 debug(`DMMF buffered: ${totalBytes} bytes (${(totalBytes / 1024 / 1024).toFixed(1)}MB)`)
128
129 // Use a streaming JSON parser that processes Uint8Array chunks directly,
130 // never creating a single large string.
131 const parser = new JSONParser()
132 let result: DMMF.Document | undefined
133
134 parser.onValue = ({ value, stack }) => {
135 if (stack.length === 0 && value !== undefined) {
136 result = value as unknown as DMMF.Document
137 }
138 }
139
140 let offset = 0
141 while (offset < totalBytes) {
142 const len = Math.min(CHUNK_SIZE, totalBytes - offset)
143 const chunk = buffer.read_chunk(offset, len)
144 parser.write(chunk)
145 offset += len
146 }
147
148 if (result === undefined) {
149 return {
150 type: 'parse-json' as const,
151 reason: '(get-dmmf-buffered parse)',
152 error: new Error('Streaming JSON parse produced no result'),
153 }
154 }
155
156 debug(`DMMF parsed via streaming parser (${(totalBytes / 1024 / 1024).toFixed(1)}MB)`)
157 return result
158 } finally {
159 buffer.free()
160 }
161}
162
163/**
164 * Check if an error is the V8 string length limit.

Callers 1

getDMMFFunction · 0.85

Calls 3

debugFunction · 0.85
toFixedMethod · 0.80
writeMethod · 0.65

Tested by

no test coverage detected