@prisma/param-graph 7.4.0-integration-parameterization.8 → 7.4.0-integration-parameterization.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,329 +22,264 @@ __export(serialization_exports, {
22
22
  serializeParamGraph: () => serializeParamGraph
23
23
  });
24
24
  module.exports = __toCommonJS(serialization_exports);
25
+ function serializeParamGraph(data) {
26
+ return new Serializer(data).serialize();
27
+ }
28
+ function deserializeParamGraph(serialized) {
29
+ return new Deserializer(serialized).deserialize();
30
+ }
25
31
  const FORMAT_COMPACT = 0;
26
32
  const FORMAT_WIDE = 1;
27
33
  const NONE_16 = 65535;
28
34
  const NONE_32 = 4294967295;
29
35
  const MAX_COMPACT_INDEX = 65534;
30
- const BASE64URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
31
36
  function encodeBase64url(bytes) {
32
- let result = "";
33
- const len = bytes.length;
34
- let i = 0;
35
- while (i < len) {
36
- const b0 = bytes[i++];
37
- const hasB1 = i < len;
38
- const b1 = hasB1 ? bytes[i++] : 0;
39
- const hasB2 = i < len;
40
- const b2 = hasB2 ? bytes[i++] : 0;
41
- result += BASE64URL_CHARS[b0 >> 2 & 63];
42
- result += BASE64URL_CHARS[(b0 << 4 | b1 >> 4) & 63];
43
- if (hasB1) {
44
- result += BASE64URL_CHARS[(b1 << 2 | b2 >> 6) & 63];
45
- }
46
- if (hasB2) {
47
- result += BASE64URL_CHARS[b2 & 63];
48
- }
49
- }
50
- return result;
37
+ return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64url");
51
38
  }
52
39
  function decodeBase64url(str) {
53
- const charToIndex = new Uint8Array(128);
54
- for (let i = 0; i < BASE64URL_CHARS.length; i++) {
55
- charToIndex[BASE64URL_CHARS.charCodeAt(i)] = i;
56
- }
57
- const len = str.length;
58
- const outputLen = Math.floor(len * 3 / 4);
59
- const bytes = new Uint8Array(outputLen);
60
- let bi = 0;
61
- for (let i = 0; i < len; ) {
62
- const c0 = charToIndex[str.charCodeAt(i++)];
63
- const c1 = i < len ? charToIndex[str.charCodeAt(i++)] : 0;
64
- const c2 = i < len ? charToIndex[str.charCodeAt(i++)] : 0;
65
- const c3 = i < len ? charToIndex[str.charCodeAt(i++)] : 0;
66
- bytes[bi++] = c0 << 2 | c1 >> 4;
67
- if (bi < outputLen) bytes[bi++] = (c1 << 4 | c2 >> 2) & 255;
68
- if (bi < outputLen) bytes[bi++] = (c2 << 6 | c3) & 255;
69
- }
70
- return bytes;
40
+ return Buffer.from(str, "base64url");
71
41
  }
72
- function serializeParamGraph(data) {
73
- const rootKeys = Object.keys(data.roots);
74
- const maxIndex = Math.max(
75
- data.strings.length,
76
- data.inputNodes.length,
77
- data.outputNodes.length,
78
- rootKeys.length
79
- );
80
- const useWide = maxIndex > MAX_COMPACT_INDEX;
81
- let size = 1;
82
- size += useWide ? 12 : 6;
83
- for (const node of data.inputNodes) {
84
- size += useWide ? 4 : 2;
85
- const edgeCount = Object.keys(node.edges).length;
86
- size += edgeCount * (useWide ? 20 : 10);
42
+ class Serializer {
43
+ #data;
44
+ #useWide;
45
+ #buffer;
46
+ #view;
47
+ #offset = 0;
48
+ #rootKeys;
49
+ constructor(data) {
50
+ this.#data = data;
51
+ this.#rootKeys = Object.keys(data.roots);
52
+ const maxIndex = Math.max(
53
+ data.strings.length,
54
+ data.inputNodes.length,
55
+ data.outputNodes.length,
56
+ this.#rootKeys.length
57
+ );
58
+ this.#useWide = maxIndex > MAX_COMPACT_INDEX;
59
+ const size = this.#calculateBufferSize();
60
+ this.#buffer = new ArrayBuffer(size);
61
+ this.#view = new DataView(this.#buffer);
62
+ }
63
+ serialize() {
64
+ this.#writeHeader();
65
+ this.#writeInputNodes();
66
+ this.#writeOutputNodes();
67
+ this.#writeRoots();
68
+ return {
69
+ strings: this.#data.strings,
70
+ graph: encodeBase64url(new Uint8Array(this.#buffer))
71
+ };
87
72
  }
88
- for (const node of data.outputNodes) {
89
- size += useWide ? 4 : 2;
90
- const edgeCount = Object.keys(node.edges).length;
91
- size += edgeCount * (useWide ? 12 : 6);
73
+ get #wordSize() {
74
+ return this.#useWide ? 4 : 2;
92
75
  }
93
- size += rootKeys.length * (useWide ? 12 : 6);
94
- const buffer = new ArrayBuffer(size);
95
- const view = new DataView(buffer);
96
- let offset = 0;
97
- view.setUint8(offset++, useWide ? FORMAT_WIDE : FORMAT_COMPACT);
98
- if (useWide) {
99
- view.setUint32(offset, data.inputNodes.length, true);
100
- offset += 4;
101
- view.setUint32(offset, data.outputNodes.length, true);
102
- offset += 4;
103
- view.setUint32(offset, rootKeys.length, true);
104
- offset += 4;
105
- } else {
106
- view.setUint16(offset, data.inputNodes.length, true);
107
- offset += 2;
108
- view.setUint16(offset, data.outputNodes.length, true);
109
- offset += 2;
110
- view.setUint16(offset, rootKeys.length, true);
111
- offset += 2;
76
+ get #noneValue() {
77
+ return this.#useWide ? NONE_32 : NONE_16;
112
78
  }
113
- for (const node of data.inputNodes) {
114
- const fieldIndices = Object.keys(node.edges).map(Number);
115
- if (useWide) {
116
- view.setUint32(offset, fieldIndices.length, true);
117
- offset += 4;
79
+ #writeWord(value) {
80
+ if (this.#useWide) {
81
+ this.#view.setUint32(this.#offset, value, true);
118
82
  } else {
119
- view.setUint16(offset, fieldIndices.length, true);
120
- offset += 2;
83
+ this.#view.setUint16(this.#offset, value, true);
121
84
  }
122
- for (const fieldIndex of fieldIndices) {
123
- const edge = node.edges[fieldIndex];
124
- if (useWide) {
125
- view.setUint32(offset, fieldIndex, true);
126
- offset += 4;
127
- view.setUint16(offset, edge.scalarMask ?? 0, true);
128
- offset += 2;
129
- offset += 2;
130
- view.setUint32(offset, edge.childNodeId ?? NONE_32, true);
131
- offset += 4;
132
- view.setUint32(offset, edge.enumNameIndex ?? NONE_32, true);
133
- offset += 4;
134
- view.setUint8(offset, edge.flags);
135
- offset += 1;
136
- offset += 3;
137
- } else {
138
- view.setUint16(offset, fieldIndex, true);
139
- offset += 2;
140
- view.setUint16(offset, edge.scalarMask ?? 0, true);
141
- offset += 2;
142
- view.setUint16(offset, edge.childNodeId ?? NONE_16, true);
143
- offset += 2;
144
- view.setUint16(offset, edge.enumNameIndex ?? NONE_16, true);
145
- offset += 2;
146
- view.setUint8(offset, edge.flags);
147
- offset += 1;
148
- offset += 1;
149
- }
85
+ this.#offset += this.#wordSize;
86
+ }
87
+ #writeOptionalWord(value) {
88
+ this.#writeWord(value ?? this.#noneValue);
89
+ }
90
+ #writeByte(value) {
91
+ this.#view.setUint8(this.#offset, value);
92
+ this.#offset += 1;
93
+ }
94
+ #writeU16(value) {
95
+ this.#view.setUint16(this.#offset, value, true);
96
+ this.#offset += 2;
97
+ }
98
+ #skip(bytes) {
99
+ this.#offset += bytes;
100
+ }
101
+ #calculateBufferSize() {
102
+ let size = 1;
103
+ size += this.#useWide ? 12 : 6;
104
+ for (const node of this.#data.inputNodes) {
105
+ size += this.#wordSize;
106
+ const edgeCount = Object.keys(node.edges).length;
107
+ size += edgeCount * (this.#useWide ? 20 : 10);
108
+ }
109
+ for (const node of this.#data.outputNodes) {
110
+ size += this.#wordSize;
111
+ const edgeCount = Object.keys(node.edges).length;
112
+ size += edgeCount * (this.#useWide ? 12 : 6);
150
113
  }
114
+ size += this.#rootKeys.length * (this.#useWide ? 12 : 6);
115
+ return size;
151
116
  }
152
- for (const node of data.outputNodes) {
153
- const fieldIndices = Object.keys(node.edges).map(Number);
154
- if (useWide) {
155
- view.setUint32(offset, fieldIndices.length, true);
156
- offset += 4;
157
- } else {
158
- view.setUint16(offset, fieldIndices.length, true);
159
- offset += 2;
117
+ #writeHeader() {
118
+ this.#writeByte(this.#useWide ? FORMAT_WIDE : FORMAT_COMPACT);
119
+ this.#writeWord(this.#data.inputNodes.length);
120
+ this.#writeWord(this.#data.outputNodes.length);
121
+ this.#writeWord(this.#rootKeys.length);
122
+ }
123
+ #writeInputNodes() {
124
+ for (const node of this.#data.inputNodes) {
125
+ const fieldIndices = Object.keys(node.edges).map(Number);
126
+ this.#writeWord(fieldIndices.length);
127
+ for (const fieldIndex of fieldIndices) {
128
+ const edge = node.edges[fieldIndex];
129
+ this.#writeWord(fieldIndex);
130
+ this.#writeU16(edge.scalarMask ?? 0);
131
+ if (this.#useWide) {
132
+ this.#skip(2);
133
+ }
134
+ this.#writeOptionalWord(edge.childNodeId);
135
+ this.#writeOptionalWord(edge.enumNameIndex);
136
+ this.#writeByte(edge.flags);
137
+ this.#skip(this.#useWide ? 3 : 1);
138
+ }
160
139
  }
161
- for (const fieldIndex of fieldIndices) {
162
- const edge = node.edges[fieldIndex];
163
- if (useWide) {
164
- view.setUint32(offset, fieldIndex, true);
165
- offset += 4;
166
- view.setUint32(offset, edge.argsNodeId ?? NONE_32, true);
167
- offset += 4;
168
- view.setUint32(offset, edge.outputNodeId ?? NONE_32, true);
169
- offset += 4;
170
- } else {
171
- view.setUint16(offset, fieldIndex, true);
172
- offset += 2;
173
- view.setUint16(offset, edge.argsNodeId ?? NONE_16, true);
174
- offset += 2;
175
- view.setUint16(offset, edge.outputNodeId ?? NONE_16, true);
176
- offset += 2;
140
+ }
141
+ #writeOutputNodes() {
142
+ for (const node of this.#data.outputNodes) {
143
+ const fieldIndices = Object.keys(node.edges).map(Number);
144
+ this.#writeWord(fieldIndices.length);
145
+ for (const fieldIndex of fieldIndices) {
146
+ const edge = node.edges[fieldIndex];
147
+ this.#writeWord(fieldIndex);
148
+ this.#writeOptionalWord(edge.argsNodeId);
149
+ this.#writeOptionalWord(edge.outputNodeId);
177
150
  }
178
151
  }
179
152
  }
180
- for (const key of rootKeys) {
181
- const root = data.roots[key];
182
- const keyIndex = data.strings.indexOf(key);
183
- if (useWide) {
184
- view.setUint32(offset, keyIndex, true);
185
- offset += 4;
186
- view.setUint32(offset, root.argsNodeId ?? NONE_32, true);
187
- offset += 4;
188
- view.setUint32(offset, root.outputNodeId ?? NONE_32, true);
189
- offset += 4;
190
- } else {
191
- view.setUint16(offset, keyIndex, true);
192
- offset += 2;
193
- view.setUint16(offset, root.argsNodeId ?? NONE_16, true);
194
- offset += 2;
195
- view.setUint16(offset, root.outputNodeId ?? NONE_16, true);
196
- offset += 2;
153
+ #writeRoots() {
154
+ for (const key of this.#rootKeys) {
155
+ const root = this.#data.roots[key];
156
+ const keyIndex = this.#data.strings.indexOf(key);
157
+ this.#writeWord(keyIndex);
158
+ this.#writeOptionalWord(root.argsNodeId);
159
+ this.#writeOptionalWord(root.outputNodeId);
197
160
  }
198
161
  }
199
- return {
200
- strings: data.strings,
201
- graph: encodeBase64url(new Uint8Array(buffer))
202
- };
203
162
  }
204
- function deserializeParamGraph(serialized) {
205
- const bytes = decodeBase64url(serialized.graph);
206
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
207
- let offset = 0;
208
- const format = view.getUint8(offset++);
209
- const useWide = format === FORMAT_WIDE;
210
- let inputNodeCount;
211
- let outputNodeCount;
212
- let rootCount;
213
- if (useWide) {
214
- inputNodeCount = view.getUint32(offset, true);
215
- offset += 4;
216
- outputNodeCount = view.getUint32(offset, true);
217
- offset += 4;
218
- rootCount = view.getUint32(offset, true);
219
- offset += 4;
220
- } else {
221
- inputNodeCount = view.getUint16(offset, true);
222
- offset += 2;
223
- outputNodeCount = view.getUint16(offset, true);
224
- offset += 2;
225
- rootCount = view.getUint16(offset, true);
226
- offset += 2;
163
+ class Deserializer {
164
+ #serialized;
165
+ #view;
166
+ #offset = 0;
167
+ #useWide = false;
168
+ constructor(serialized) {
169
+ this.#serialized = serialized;
170
+ const bytes = decodeBase64url(serialized.graph);
171
+ this.#view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
172
+ }
173
+ deserialize() {
174
+ const { inputNodeCount, outputNodeCount, rootCount } = this.#readHeader();
175
+ const inputNodes = this.#readInputNodes(inputNodeCount);
176
+ const outputNodes = this.#readOutputNodes(outputNodeCount);
177
+ const roots = this.#readRoots(rootCount);
178
+ return {
179
+ strings: this.#serialized.strings,
180
+ inputNodes,
181
+ outputNodes,
182
+ roots
183
+ };
184
+ }
185
+ get #wordSize() {
186
+ return this.#useWide ? 4 : 2;
187
+ }
188
+ get #noneValue() {
189
+ return this.#useWide ? NONE_32 : NONE_16;
227
190
  }
228
- const inputNodes = [];
229
- for (let i = 0; i < inputNodeCount; i++) {
230
- let edgeCount;
231
- if (useWide) {
232
- edgeCount = view.getUint32(offset, true);
233
- offset += 4;
191
+ #readWord() {
192
+ let value;
193
+ if (this.#useWide) {
194
+ value = this.#view.getUint32(this.#offset, true);
234
195
  } else {
235
- edgeCount = view.getUint16(offset, true);
236
- offset += 2;
196
+ value = this.#view.getUint16(this.#offset, true);
237
197
  }
238
- const edges = {};
239
- for (let j = 0; j < edgeCount; j++) {
240
- let fieldIndex;
241
- let scalarMask;
242
- let childNodeId;
243
- let enumNameIndex;
244
- let flags;
245
- if (useWide) {
246
- fieldIndex = view.getUint32(offset, true);
247
- offset += 4;
248
- scalarMask = view.getUint16(offset, true);
249
- offset += 2;
250
- offset += 2;
251
- childNodeId = view.getUint32(offset, true);
252
- offset += 4;
253
- enumNameIndex = view.getUint32(offset, true);
254
- offset += 4;
255
- flags = view.getUint8(offset);
256
- offset += 1;
257
- offset += 3;
258
- } else {
259
- fieldIndex = view.getUint16(offset, true);
260
- offset += 2;
261
- scalarMask = view.getUint16(offset, true);
262
- offset += 2;
263
- childNodeId = view.getUint16(offset, true);
264
- offset += 2;
265
- enumNameIndex = view.getUint16(offset, true);
266
- offset += 2;
267
- flags = view.getUint8(offset);
268
- offset += 1;
269
- offset += 1;
198
+ this.#offset += this.#wordSize;
199
+ return value;
200
+ }
201
+ #readOptionalWord() {
202
+ const value = this.#readWord();
203
+ return value === this.#noneValue ? void 0 : value;
204
+ }
205
+ #readByte() {
206
+ const value = this.#view.getUint8(this.#offset);
207
+ this.#offset += 1;
208
+ return value;
209
+ }
210
+ #readU16() {
211
+ const value = this.#view.getUint16(this.#offset, true);
212
+ this.#offset += 2;
213
+ return value;
214
+ }
215
+ #skip(bytes) {
216
+ this.#offset += bytes;
217
+ }
218
+ #readHeader() {
219
+ const format = this.#readByte();
220
+ this.#useWide = format === FORMAT_WIDE;
221
+ const inputNodeCount = this.#readWord();
222
+ const outputNodeCount = this.#readWord();
223
+ const rootCount = this.#readWord();
224
+ return { inputNodeCount, outputNodeCount, rootCount };
225
+ }
226
+ #readInputNodes(count) {
227
+ const inputNodes = [];
228
+ for (let i = 0; i < count; i++) {
229
+ const edgeCount = this.#readWord();
230
+ const edges = {};
231
+ for (let j = 0; j < edgeCount; j++) {
232
+ const fieldIndex = this.#readWord();
233
+ const scalarMask = this.#readU16();
234
+ if (this.#useWide) {
235
+ this.#skip(2);
236
+ }
237
+ const childNodeId = this.#readOptionalWord();
238
+ const enumNameIndex = this.#readOptionalWord();
239
+ const flags = this.#readByte();
240
+ this.#skip(this.#useWide ? 3 : 1);
241
+ const edge = { flags };
242
+ if (scalarMask !== 0) edge.scalarMask = scalarMask;
243
+ if (childNodeId !== void 0) edge.childNodeId = childNodeId;
244
+ if (enumNameIndex !== void 0) edge.enumNameIndex = enumNameIndex;
245
+ edges[fieldIndex] = edge;
270
246
  }
271
- const edge = { flags };
272
- if (scalarMask !== 0) edge.scalarMask = scalarMask;
273
- if (childNodeId !== (useWide ? NONE_32 : NONE_16)) edge.childNodeId = childNodeId;
274
- if (enumNameIndex !== (useWide ? NONE_32 : NONE_16)) edge.enumNameIndex = enumNameIndex;
275
- edges[fieldIndex] = edge;
247
+ inputNodes.push({ edges });
276
248
  }
277
- inputNodes.push({ edges });
249
+ return inputNodes;
278
250
  }
279
- const outputNodes = [];
280
- for (let i = 0; i < outputNodeCount; i++) {
281
- let edgeCount;
282
- if (useWide) {
283
- edgeCount = view.getUint32(offset, true);
284
- offset += 4;
285
- } else {
286
- edgeCount = view.getUint16(offset, true);
287
- offset += 2;
288
- }
289
- const edges = {};
290
- for (let j = 0; j < edgeCount; j++) {
291
- let fieldIndex;
292
- let argsNodeId;
293
- let outputNodeId;
294
- if (useWide) {
295
- fieldIndex = view.getUint32(offset, true);
296
- offset += 4;
297
- argsNodeId = view.getUint32(offset, true);
298
- offset += 4;
299
- outputNodeId = view.getUint32(offset, true);
300
- offset += 4;
301
- } else {
302
- fieldIndex = view.getUint16(offset, true);
303
- offset += 2;
304
- argsNodeId = view.getUint16(offset, true);
305
- offset += 2;
306
- outputNodeId = view.getUint16(offset, true);
307
- offset += 2;
251
+ #readOutputNodes(count) {
252
+ const outputNodes = [];
253
+ for (let i = 0; i < count; i++) {
254
+ const edgeCount = this.#readWord();
255
+ const edges = {};
256
+ for (let j = 0; j < edgeCount; j++) {
257
+ const fieldIndex = this.#readWord();
258
+ const argsNodeId = this.#readOptionalWord();
259
+ const outputNodeId = this.#readOptionalWord();
260
+ const edge = {};
261
+ if (argsNodeId !== void 0) edge.argsNodeId = argsNodeId;
262
+ if (outputNodeId !== void 0) edge.outputNodeId = outputNodeId;
263
+ edges[fieldIndex] = edge;
308
264
  }
309
- const edge = {};
310
- if (argsNodeId !== (useWide ? NONE_32 : NONE_16)) edge.argsNodeId = argsNodeId;
311
- if (outputNodeId !== (useWide ? NONE_32 : NONE_16)) edge.outputNodeId = outputNodeId;
312
- edges[fieldIndex] = edge;
265
+ outputNodes.push({ edges });
313
266
  }
314
- outputNodes.push({ edges });
267
+ return outputNodes;
315
268
  }
316
- const roots = {};
317
- for (let i = 0; i < rootCount; i++) {
318
- let keyIndex;
319
- let argsNodeId;
320
- let outputNodeId;
321
- if (useWide) {
322
- keyIndex = view.getUint32(offset, true);
323
- offset += 4;
324
- argsNodeId = view.getUint32(offset, true);
325
- offset += 4;
326
- outputNodeId = view.getUint32(offset, true);
327
- offset += 4;
328
- } else {
329
- keyIndex = view.getUint16(offset, true);
330
- offset += 2;
331
- argsNodeId = view.getUint16(offset, true);
332
- offset += 2;
333
- outputNodeId = view.getUint16(offset, true);
334
- offset += 2;
269
+ #readRoots(count) {
270
+ const roots = {};
271
+ for (let i = 0; i < count; i++) {
272
+ const keyIndex = this.#readWord();
273
+ const argsNodeId = this.#readOptionalWord();
274
+ const outputNodeId = this.#readOptionalWord();
275
+ const key = this.#serialized.strings[keyIndex];
276
+ const root = {};
277
+ if (argsNodeId !== void 0) root.argsNodeId = argsNodeId;
278
+ if (outputNodeId !== void 0) root.outputNodeId = outputNodeId;
279
+ roots[key] = root;
335
280
  }
336
- const key = serialized.strings[keyIndex];
337
- const root = {};
338
- if (argsNodeId !== (useWide ? NONE_32 : NONE_16)) root.argsNodeId = argsNodeId;
339
- if (outputNodeId !== (useWide ? NONE_32 : NONE_16)) root.outputNodeId = outputNodeId;
340
- roots[key] = root;
281
+ return roots;
341
282
  }
342
- return {
343
- strings: serialized.strings,
344
- inputNodes,
345
- outputNodes,
346
- roots
347
- };
348
283
  }
349
284
  // Annotate the CommonJS export names for ESM import in node:
350
285
  0 && (module.exports = {
@@ -1,326 +1,261 @@
1
+ function serializeParamGraph(data) {
2
+ return new Serializer(data).serialize();
3
+ }
4
+ function deserializeParamGraph(serialized) {
5
+ return new Deserializer(serialized).deserialize();
6
+ }
1
7
  const FORMAT_COMPACT = 0;
2
8
  const FORMAT_WIDE = 1;
3
9
  const NONE_16 = 65535;
4
10
  const NONE_32 = 4294967295;
5
11
  const MAX_COMPACT_INDEX = 65534;
6
- const BASE64URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
7
12
  function encodeBase64url(bytes) {
8
- let result = "";
9
- const len = bytes.length;
10
- let i = 0;
11
- while (i < len) {
12
- const b0 = bytes[i++];
13
- const hasB1 = i < len;
14
- const b1 = hasB1 ? bytes[i++] : 0;
15
- const hasB2 = i < len;
16
- const b2 = hasB2 ? bytes[i++] : 0;
17
- result += BASE64URL_CHARS[b0 >> 2 & 63];
18
- result += BASE64URL_CHARS[(b0 << 4 | b1 >> 4) & 63];
19
- if (hasB1) {
20
- result += BASE64URL_CHARS[(b1 << 2 | b2 >> 6) & 63];
21
- }
22
- if (hasB2) {
23
- result += BASE64URL_CHARS[b2 & 63];
24
- }
25
- }
26
- return result;
13
+ return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64url");
27
14
  }
28
15
  function decodeBase64url(str) {
29
- const charToIndex = new Uint8Array(128);
30
- for (let i = 0; i < BASE64URL_CHARS.length; i++) {
31
- charToIndex[BASE64URL_CHARS.charCodeAt(i)] = i;
32
- }
33
- const len = str.length;
34
- const outputLen = Math.floor(len * 3 / 4);
35
- const bytes = new Uint8Array(outputLen);
36
- let bi = 0;
37
- for (let i = 0; i < len; ) {
38
- const c0 = charToIndex[str.charCodeAt(i++)];
39
- const c1 = i < len ? charToIndex[str.charCodeAt(i++)] : 0;
40
- const c2 = i < len ? charToIndex[str.charCodeAt(i++)] : 0;
41
- const c3 = i < len ? charToIndex[str.charCodeAt(i++)] : 0;
42
- bytes[bi++] = c0 << 2 | c1 >> 4;
43
- if (bi < outputLen) bytes[bi++] = (c1 << 4 | c2 >> 2) & 255;
44
- if (bi < outputLen) bytes[bi++] = (c2 << 6 | c3) & 255;
45
- }
46
- return bytes;
16
+ return Buffer.from(str, "base64url");
47
17
  }
48
- function serializeParamGraph(data) {
49
- const rootKeys = Object.keys(data.roots);
50
- const maxIndex = Math.max(
51
- data.strings.length,
52
- data.inputNodes.length,
53
- data.outputNodes.length,
54
- rootKeys.length
55
- );
56
- const useWide = maxIndex > MAX_COMPACT_INDEX;
57
- let size = 1;
58
- size += useWide ? 12 : 6;
59
- for (const node of data.inputNodes) {
60
- size += useWide ? 4 : 2;
61
- const edgeCount = Object.keys(node.edges).length;
62
- size += edgeCount * (useWide ? 20 : 10);
18
+ class Serializer {
19
+ #data;
20
+ #useWide;
21
+ #buffer;
22
+ #view;
23
+ #offset = 0;
24
+ #rootKeys;
25
+ constructor(data) {
26
+ this.#data = data;
27
+ this.#rootKeys = Object.keys(data.roots);
28
+ const maxIndex = Math.max(
29
+ data.strings.length,
30
+ data.inputNodes.length,
31
+ data.outputNodes.length,
32
+ this.#rootKeys.length
33
+ );
34
+ this.#useWide = maxIndex > MAX_COMPACT_INDEX;
35
+ const size = this.#calculateBufferSize();
36
+ this.#buffer = new ArrayBuffer(size);
37
+ this.#view = new DataView(this.#buffer);
38
+ }
39
+ serialize() {
40
+ this.#writeHeader();
41
+ this.#writeInputNodes();
42
+ this.#writeOutputNodes();
43
+ this.#writeRoots();
44
+ return {
45
+ strings: this.#data.strings,
46
+ graph: encodeBase64url(new Uint8Array(this.#buffer))
47
+ };
63
48
  }
64
- for (const node of data.outputNodes) {
65
- size += useWide ? 4 : 2;
66
- const edgeCount = Object.keys(node.edges).length;
67
- size += edgeCount * (useWide ? 12 : 6);
49
+ get #wordSize() {
50
+ return this.#useWide ? 4 : 2;
68
51
  }
69
- size += rootKeys.length * (useWide ? 12 : 6);
70
- const buffer = new ArrayBuffer(size);
71
- const view = new DataView(buffer);
72
- let offset = 0;
73
- view.setUint8(offset++, useWide ? FORMAT_WIDE : FORMAT_COMPACT);
74
- if (useWide) {
75
- view.setUint32(offset, data.inputNodes.length, true);
76
- offset += 4;
77
- view.setUint32(offset, data.outputNodes.length, true);
78
- offset += 4;
79
- view.setUint32(offset, rootKeys.length, true);
80
- offset += 4;
81
- } else {
82
- view.setUint16(offset, data.inputNodes.length, true);
83
- offset += 2;
84
- view.setUint16(offset, data.outputNodes.length, true);
85
- offset += 2;
86
- view.setUint16(offset, rootKeys.length, true);
87
- offset += 2;
52
+ get #noneValue() {
53
+ return this.#useWide ? NONE_32 : NONE_16;
88
54
  }
89
- for (const node of data.inputNodes) {
90
- const fieldIndices = Object.keys(node.edges).map(Number);
91
- if (useWide) {
92
- view.setUint32(offset, fieldIndices.length, true);
93
- offset += 4;
55
+ #writeWord(value) {
56
+ if (this.#useWide) {
57
+ this.#view.setUint32(this.#offset, value, true);
94
58
  } else {
95
- view.setUint16(offset, fieldIndices.length, true);
96
- offset += 2;
59
+ this.#view.setUint16(this.#offset, value, true);
97
60
  }
98
- for (const fieldIndex of fieldIndices) {
99
- const edge = node.edges[fieldIndex];
100
- if (useWide) {
101
- view.setUint32(offset, fieldIndex, true);
102
- offset += 4;
103
- view.setUint16(offset, edge.scalarMask ?? 0, true);
104
- offset += 2;
105
- offset += 2;
106
- view.setUint32(offset, edge.childNodeId ?? NONE_32, true);
107
- offset += 4;
108
- view.setUint32(offset, edge.enumNameIndex ?? NONE_32, true);
109
- offset += 4;
110
- view.setUint8(offset, edge.flags);
111
- offset += 1;
112
- offset += 3;
113
- } else {
114
- view.setUint16(offset, fieldIndex, true);
115
- offset += 2;
116
- view.setUint16(offset, edge.scalarMask ?? 0, true);
117
- offset += 2;
118
- view.setUint16(offset, edge.childNodeId ?? NONE_16, true);
119
- offset += 2;
120
- view.setUint16(offset, edge.enumNameIndex ?? NONE_16, true);
121
- offset += 2;
122
- view.setUint8(offset, edge.flags);
123
- offset += 1;
124
- offset += 1;
125
- }
61
+ this.#offset += this.#wordSize;
62
+ }
63
+ #writeOptionalWord(value) {
64
+ this.#writeWord(value ?? this.#noneValue);
65
+ }
66
+ #writeByte(value) {
67
+ this.#view.setUint8(this.#offset, value);
68
+ this.#offset += 1;
69
+ }
70
+ #writeU16(value) {
71
+ this.#view.setUint16(this.#offset, value, true);
72
+ this.#offset += 2;
73
+ }
74
+ #skip(bytes) {
75
+ this.#offset += bytes;
76
+ }
77
+ #calculateBufferSize() {
78
+ let size = 1;
79
+ size += this.#useWide ? 12 : 6;
80
+ for (const node of this.#data.inputNodes) {
81
+ size += this.#wordSize;
82
+ const edgeCount = Object.keys(node.edges).length;
83
+ size += edgeCount * (this.#useWide ? 20 : 10);
84
+ }
85
+ for (const node of this.#data.outputNodes) {
86
+ size += this.#wordSize;
87
+ const edgeCount = Object.keys(node.edges).length;
88
+ size += edgeCount * (this.#useWide ? 12 : 6);
126
89
  }
90
+ size += this.#rootKeys.length * (this.#useWide ? 12 : 6);
91
+ return size;
127
92
  }
128
- for (const node of data.outputNodes) {
129
- const fieldIndices = Object.keys(node.edges).map(Number);
130
- if (useWide) {
131
- view.setUint32(offset, fieldIndices.length, true);
132
- offset += 4;
133
- } else {
134
- view.setUint16(offset, fieldIndices.length, true);
135
- offset += 2;
93
+ #writeHeader() {
94
+ this.#writeByte(this.#useWide ? FORMAT_WIDE : FORMAT_COMPACT);
95
+ this.#writeWord(this.#data.inputNodes.length);
96
+ this.#writeWord(this.#data.outputNodes.length);
97
+ this.#writeWord(this.#rootKeys.length);
98
+ }
99
+ #writeInputNodes() {
100
+ for (const node of this.#data.inputNodes) {
101
+ const fieldIndices = Object.keys(node.edges).map(Number);
102
+ this.#writeWord(fieldIndices.length);
103
+ for (const fieldIndex of fieldIndices) {
104
+ const edge = node.edges[fieldIndex];
105
+ this.#writeWord(fieldIndex);
106
+ this.#writeU16(edge.scalarMask ?? 0);
107
+ if (this.#useWide) {
108
+ this.#skip(2);
109
+ }
110
+ this.#writeOptionalWord(edge.childNodeId);
111
+ this.#writeOptionalWord(edge.enumNameIndex);
112
+ this.#writeByte(edge.flags);
113
+ this.#skip(this.#useWide ? 3 : 1);
114
+ }
136
115
  }
137
- for (const fieldIndex of fieldIndices) {
138
- const edge = node.edges[fieldIndex];
139
- if (useWide) {
140
- view.setUint32(offset, fieldIndex, true);
141
- offset += 4;
142
- view.setUint32(offset, edge.argsNodeId ?? NONE_32, true);
143
- offset += 4;
144
- view.setUint32(offset, edge.outputNodeId ?? NONE_32, true);
145
- offset += 4;
146
- } else {
147
- view.setUint16(offset, fieldIndex, true);
148
- offset += 2;
149
- view.setUint16(offset, edge.argsNodeId ?? NONE_16, true);
150
- offset += 2;
151
- view.setUint16(offset, edge.outputNodeId ?? NONE_16, true);
152
- offset += 2;
116
+ }
117
+ #writeOutputNodes() {
118
+ for (const node of this.#data.outputNodes) {
119
+ const fieldIndices = Object.keys(node.edges).map(Number);
120
+ this.#writeWord(fieldIndices.length);
121
+ for (const fieldIndex of fieldIndices) {
122
+ const edge = node.edges[fieldIndex];
123
+ this.#writeWord(fieldIndex);
124
+ this.#writeOptionalWord(edge.argsNodeId);
125
+ this.#writeOptionalWord(edge.outputNodeId);
153
126
  }
154
127
  }
155
128
  }
156
- for (const key of rootKeys) {
157
- const root = data.roots[key];
158
- const keyIndex = data.strings.indexOf(key);
159
- if (useWide) {
160
- view.setUint32(offset, keyIndex, true);
161
- offset += 4;
162
- view.setUint32(offset, root.argsNodeId ?? NONE_32, true);
163
- offset += 4;
164
- view.setUint32(offset, root.outputNodeId ?? NONE_32, true);
165
- offset += 4;
166
- } else {
167
- view.setUint16(offset, keyIndex, true);
168
- offset += 2;
169
- view.setUint16(offset, root.argsNodeId ?? NONE_16, true);
170
- offset += 2;
171
- view.setUint16(offset, root.outputNodeId ?? NONE_16, true);
172
- offset += 2;
129
+ #writeRoots() {
130
+ for (const key of this.#rootKeys) {
131
+ const root = this.#data.roots[key];
132
+ const keyIndex = this.#data.strings.indexOf(key);
133
+ this.#writeWord(keyIndex);
134
+ this.#writeOptionalWord(root.argsNodeId);
135
+ this.#writeOptionalWord(root.outputNodeId);
173
136
  }
174
137
  }
175
- return {
176
- strings: data.strings,
177
- graph: encodeBase64url(new Uint8Array(buffer))
178
- };
179
138
  }
180
- function deserializeParamGraph(serialized) {
181
- const bytes = decodeBase64url(serialized.graph);
182
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
183
- let offset = 0;
184
- const format = view.getUint8(offset++);
185
- const useWide = format === FORMAT_WIDE;
186
- let inputNodeCount;
187
- let outputNodeCount;
188
- let rootCount;
189
- if (useWide) {
190
- inputNodeCount = view.getUint32(offset, true);
191
- offset += 4;
192
- outputNodeCount = view.getUint32(offset, true);
193
- offset += 4;
194
- rootCount = view.getUint32(offset, true);
195
- offset += 4;
196
- } else {
197
- inputNodeCount = view.getUint16(offset, true);
198
- offset += 2;
199
- outputNodeCount = view.getUint16(offset, true);
200
- offset += 2;
201
- rootCount = view.getUint16(offset, true);
202
- offset += 2;
139
+ class Deserializer {
140
+ #serialized;
141
+ #view;
142
+ #offset = 0;
143
+ #useWide = false;
144
+ constructor(serialized) {
145
+ this.#serialized = serialized;
146
+ const bytes = decodeBase64url(serialized.graph);
147
+ this.#view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
148
+ }
149
+ deserialize() {
150
+ const { inputNodeCount, outputNodeCount, rootCount } = this.#readHeader();
151
+ const inputNodes = this.#readInputNodes(inputNodeCount);
152
+ const outputNodes = this.#readOutputNodes(outputNodeCount);
153
+ const roots = this.#readRoots(rootCount);
154
+ return {
155
+ strings: this.#serialized.strings,
156
+ inputNodes,
157
+ outputNodes,
158
+ roots
159
+ };
160
+ }
161
+ get #wordSize() {
162
+ return this.#useWide ? 4 : 2;
163
+ }
164
+ get #noneValue() {
165
+ return this.#useWide ? NONE_32 : NONE_16;
203
166
  }
204
- const inputNodes = [];
205
- for (let i = 0; i < inputNodeCount; i++) {
206
- let edgeCount;
207
- if (useWide) {
208
- edgeCount = view.getUint32(offset, true);
209
- offset += 4;
167
+ #readWord() {
168
+ let value;
169
+ if (this.#useWide) {
170
+ value = this.#view.getUint32(this.#offset, true);
210
171
  } else {
211
- edgeCount = view.getUint16(offset, true);
212
- offset += 2;
172
+ value = this.#view.getUint16(this.#offset, true);
213
173
  }
214
- const edges = {};
215
- for (let j = 0; j < edgeCount; j++) {
216
- let fieldIndex;
217
- let scalarMask;
218
- let childNodeId;
219
- let enumNameIndex;
220
- let flags;
221
- if (useWide) {
222
- fieldIndex = view.getUint32(offset, true);
223
- offset += 4;
224
- scalarMask = view.getUint16(offset, true);
225
- offset += 2;
226
- offset += 2;
227
- childNodeId = view.getUint32(offset, true);
228
- offset += 4;
229
- enumNameIndex = view.getUint32(offset, true);
230
- offset += 4;
231
- flags = view.getUint8(offset);
232
- offset += 1;
233
- offset += 3;
234
- } else {
235
- fieldIndex = view.getUint16(offset, true);
236
- offset += 2;
237
- scalarMask = view.getUint16(offset, true);
238
- offset += 2;
239
- childNodeId = view.getUint16(offset, true);
240
- offset += 2;
241
- enumNameIndex = view.getUint16(offset, true);
242
- offset += 2;
243
- flags = view.getUint8(offset);
244
- offset += 1;
245
- offset += 1;
174
+ this.#offset += this.#wordSize;
175
+ return value;
176
+ }
177
+ #readOptionalWord() {
178
+ const value = this.#readWord();
179
+ return value === this.#noneValue ? void 0 : value;
180
+ }
181
+ #readByte() {
182
+ const value = this.#view.getUint8(this.#offset);
183
+ this.#offset += 1;
184
+ return value;
185
+ }
186
+ #readU16() {
187
+ const value = this.#view.getUint16(this.#offset, true);
188
+ this.#offset += 2;
189
+ return value;
190
+ }
191
+ #skip(bytes) {
192
+ this.#offset += bytes;
193
+ }
194
+ #readHeader() {
195
+ const format = this.#readByte();
196
+ this.#useWide = format === FORMAT_WIDE;
197
+ const inputNodeCount = this.#readWord();
198
+ const outputNodeCount = this.#readWord();
199
+ const rootCount = this.#readWord();
200
+ return { inputNodeCount, outputNodeCount, rootCount };
201
+ }
202
+ #readInputNodes(count) {
203
+ const inputNodes = [];
204
+ for (let i = 0; i < count; i++) {
205
+ const edgeCount = this.#readWord();
206
+ const edges = {};
207
+ for (let j = 0; j < edgeCount; j++) {
208
+ const fieldIndex = this.#readWord();
209
+ const scalarMask = this.#readU16();
210
+ if (this.#useWide) {
211
+ this.#skip(2);
212
+ }
213
+ const childNodeId = this.#readOptionalWord();
214
+ const enumNameIndex = this.#readOptionalWord();
215
+ const flags = this.#readByte();
216
+ this.#skip(this.#useWide ? 3 : 1);
217
+ const edge = { flags };
218
+ if (scalarMask !== 0) edge.scalarMask = scalarMask;
219
+ if (childNodeId !== void 0) edge.childNodeId = childNodeId;
220
+ if (enumNameIndex !== void 0) edge.enumNameIndex = enumNameIndex;
221
+ edges[fieldIndex] = edge;
246
222
  }
247
- const edge = { flags };
248
- if (scalarMask !== 0) edge.scalarMask = scalarMask;
249
- if (childNodeId !== (useWide ? NONE_32 : NONE_16)) edge.childNodeId = childNodeId;
250
- if (enumNameIndex !== (useWide ? NONE_32 : NONE_16)) edge.enumNameIndex = enumNameIndex;
251
- edges[fieldIndex] = edge;
223
+ inputNodes.push({ edges });
252
224
  }
253
- inputNodes.push({ edges });
225
+ return inputNodes;
254
226
  }
255
- const outputNodes = [];
256
- for (let i = 0; i < outputNodeCount; i++) {
257
- let edgeCount;
258
- if (useWide) {
259
- edgeCount = view.getUint32(offset, true);
260
- offset += 4;
261
- } else {
262
- edgeCount = view.getUint16(offset, true);
263
- offset += 2;
264
- }
265
- const edges = {};
266
- for (let j = 0; j < edgeCount; j++) {
267
- let fieldIndex;
268
- let argsNodeId;
269
- let outputNodeId;
270
- if (useWide) {
271
- fieldIndex = view.getUint32(offset, true);
272
- offset += 4;
273
- argsNodeId = view.getUint32(offset, true);
274
- offset += 4;
275
- outputNodeId = view.getUint32(offset, true);
276
- offset += 4;
277
- } else {
278
- fieldIndex = view.getUint16(offset, true);
279
- offset += 2;
280
- argsNodeId = view.getUint16(offset, true);
281
- offset += 2;
282
- outputNodeId = view.getUint16(offset, true);
283
- offset += 2;
227
+ #readOutputNodes(count) {
228
+ const outputNodes = [];
229
+ for (let i = 0; i < count; i++) {
230
+ const edgeCount = this.#readWord();
231
+ const edges = {};
232
+ for (let j = 0; j < edgeCount; j++) {
233
+ const fieldIndex = this.#readWord();
234
+ const argsNodeId = this.#readOptionalWord();
235
+ const outputNodeId = this.#readOptionalWord();
236
+ const edge = {};
237
+ if (argsNodeId !== void 0) edge.argsNodeId = argsNodeId;
238
+ if (outputNodeId !== void 0) edge.outputNodeId = outputNodeId;
239
+ edges[fieldIndex] = edge;
284
240
  }
285
- const edge = {};
286
- if (argsNodeId !== (useWide ? NONE_32 : NONE_16)) edge.argsNodeId = argsNodeId;
287
- if (outputNodeId !== (useWide ? NONE_32 : NONE_16)) edge.outputNodeId = outputNodeId;
288
- edges[fieldIndex] = edge;
241
+ outputNodes.push({ edges });
289
242
  }
290
- outputNodes.push({ edges });
243
+ return outputNodes;
291
244
  }
292
- const roots = {};
293
- for (let i = 0; i < rootCount; i++) {
294
- let keyIndex;
295
- let argsNodeId;
296
- let outputNodeId;
297
- if (useWide) {
298
- keyIndex = view.getUint32(offset, true);
299
- offset += 4;
300
- argsNodeId = view.getUint32(offset, true);
301
- offset += 4;
302
- outputNodeId = view.getUint32(offset, true);
303
- offset += 4;
304
- } else {
305
- keyIndex = view.getUint16(offset, true);
306
- offset += 2;
307
- argsNodeId = view.getUint16(offset, true);
308
- offset += 2;
309
- outputNodeId = view.getUint16(offset, true);
310
- offset += 2;
245
+ #readRoots(count) {
246
+ const roots = {};
247
+ for (let i = 0; i < count; i++) {
248
+ const keyIndex = this.#readWord();
249
+ const argsNodeId = this.#readOptionalWord();
250
+ const outputNodeId = this.#readOptionalWord();
251
+ const key = this.#serialized.strings[keyIndex];
252
+ const root = {};
253
+ if (argsNodeId !== void 0) root.argsNodeId = argsNodeId;
254
+ if (outputNodeId !== void 0) root.outputNodeId = outputNodeId;
255
+ roots[key] = root;
311
256
  }
312
- const key = serialized.strings[keyIndex];
313
- const root = {};
314
- if (argsNodeId !== (useWide ? NONE_32 : NONE_16)) root.argsNodeId = argsNodeId;
315
- if (outputNodeId !== (useWide ? NONE_32 : NONE_16)) root.outputNodeId = outputNodeId;
316
- roots[key] = root;
257
+ return roots;
317
258
  }
318
- return {
319
- strings: serialized.strings,
320
- inputNodes,
321
- outputNodes,
322
- roots
323
- };
324
259
  }
325
260
  export {
326
261
  deserializeParamGraph,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/param-graph",
3
- "version": "7.4.0-integration-parameterization.8",
3
+ "version": "7.4.0-integration-parameterization.10",
4
4
  "description": "This package is intended for Prisma's internal use",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",