@graphql-mesh/transport-protobuf 0.0.0-stage → 0.7.2
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.
- package/CHANGELOG.md +1225 -0
- package/dist/cjs/index.js +394 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/patchLongJs.js +14 -0
- package/dist/cjs/protobuf/codec.js +393 -0
- package/dist/cjs/protobuf/dial.js +40 -0
- package/dist/cjs/protobuf/mapping.js +226 -0
- package/dist/cjs/utils.js +129 -0
- package/dist/esm/index.js +390 -0
- package/dist/esm/patchLongJs.js +11 -0
- package/dist/esm/protobuf/codec.js +389 -0
- package/dist/esm/protobuf/dial.js +33 -0
- package/dist/esm/protobuf/mapping.js +222 -0
- package/dist/esm/utils.js +122 -0
- package/dist/package.json +61 -0
- package/dist/typings/index.d.cts +41 -0
- package/dist/typings/index.d.ts +41 -0
- package/dist/typings/patchLongJs.d.cts +1 -0
- package/dist/typings/patchLongJs.d.ts +1 -0
- package/dist/typings/protobuf/codec.d.cts +3 -0
- package/dist/typings/protobuf/codec.d.ts +3 -0
- package/dist/typings/protobuf/dial.d.cts +16 -0
- package/dist/typings/protobuf/dial.d.ts +16 -0
- package/dist/typings/protobuf/mapping.d.cts +31 -0
- package/dist/typings/protobuf/mapping.d.ts +31 -0
- package/dist/typings/utils.d.cts +12 -0
- package/dist/typings/utils.d.ts +12 -0
- package/package.json +66 -4
- package/src/index.ts +562 -0
- package/src/patchLongJs.ts +12 -0
- package/src/protobuf/codec.ts +429 -0
- package/src/protobuf/dial.ts +56 -0
- package/src/protobuf/mapping.ts +259 -0
- package/src/utils.ts +158 -0
- package/test/mapping-codec.spec.ts +52 -0
- package/test/utils.spec.ts +182 -0
- package/README.md +0 -3
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeMessage = encodeMessage;
|
|
4
|
+
exports.decodeMessage = decodeMessage;
|
|
5
|
+
const mapping_js_1 = require("./mapping.js");
|
|
6
|
+
const WIRE_VARINT = 0;
|
|
7
|
+
const WIRE_64 = 1;
|
|
8
|
+
const WIRE_LEN = 2;
|
|
9
|
+
const WIRE_32 = 5;
|
|
10
|
+
function encodeMessage(fields, args, localRoot = args, thisRoot) {
|
|
11
|
+
const chunks = [];
|
|
12
|
+
for (const field of fields) {
|
|
13
|
+
writeField(chunks, field, args, localRoot, thisRoot);
|
|
14
|
+
}
|
|
15
|
+
return concat(chunks);
|
|
16
|
+
}
|
|
17
|
+
function resolveFieldValue(bind, args, localRoot, thisRoot) {
|
|
18
|
+
if (bind == null || bind === '')
|
|
19
|
+
return undefined;
|
|
20
|
+
if (bind.startsWith('$args') || bind.startsWith('$this')) {
|
|
21
|
+
return (0, mapping_js_1.resolveBind)(bind, args, thisRoot);
|
|
22
|
+
}
|
|
23
|
+
return (0, mapping_js_1.resolveBind)(bind, localRoot, thisRoot);
|
|
24
|
+
}
|
|
25
|
+
function writeField(chunks, field, args, localRoot, thisRoot) {
|
|
26
|
+
const type = field.type;
|
|
27
|
+
if (type.kind === 'message') {
|
|
28
|
+
let childRoot = localRoot;
|
|
29
|
+
if (field.bind != null && !field.bind.startsWith('$args') && !field.bind.startsWith('$this')) {
|
|
30
|
+
const fromLocal = (0, mapping_js_1.resolveBind)(field.bind, localRoot, thisRoot);
|
|
31
|
+
if (fromLocal != null && typeof fromLocal === 'object') {
|
|
32
|
+
childRoot = fromLocal;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const nestedFields = field.fields ?? type.fields;
|
|
36
|
+
const nested = encodeMessage(nestedFields, args, childRoot, thisRoot);
|
|
37
|
+
if (nested.length === 0)
|
|
38
|
+
return;
|
|
39
|
+
writeTag(chunks, field.id, WIRE_LEN);
|
|
40
|
+
writeVarint(chunks, nested.length);
|
|
41
|
+
chunks.push(nested);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (type.kind === 'repeated') {
|
|
45
|
+
const list = resolveFieldValue(field.bind, args, localRoot, thisRoot);
|
|
46
|
+
if (!Array.isArray(list))
|
|
47
|
+
return;
|
|
48
|
+
const elementType = type.element.kind === 'message' && field.fields?.length
|
|
49
|
+
? { kind: 'message', fields: field.fields }
|
|
50
|
+
: type.element;
|
|
51
|
+
for (const item of list) {
|
|
52
|
+
writeValue(chunks, field.id, elementType, item);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const value = resolveFieldValue(field.bind, args, localRoot, thisRoot);
|
|
57
|
+
if (value === undefined || value === null)
|
|
58
|
+
return;
|
|
59
|
+
writeValue(chunks, field.id, type, value);
|
|
60
|
+
}
|
|
61
|
+
function writeValue(chunks, id, type, value) {
|
|
62
|
+
if (type.kind === 'scalar') {
|
|
63
|
+
switch (type.name) {
|
|
64
|
+
case 'bool':
|
|
65
|
+
writeTag(chunks, id, WIRE_VARINT);
|
|
66
|
+
writeVarint(chunks, value ? 1 : 0);
|
|
67
|
+
return;
|
|
68
|
+
case 'int32':
|
|
69
|
+
case 'uint32':
|
|
70
|
+
case 'int64':
|
|
71
|
+
case 'uint64':
|
|
72
|
+
writeTag(chunks, id, WIRE_VARINT);
|
|
73
|
+
writeVarint(chunks, toLongish(value));
|
|
74
|
+
return;
|
|
75
|
+
case 'sint32':
|
|
76
|
+
writeTag(chunks, id, WIRE_VARINT);
|
|
77
|
+
writeVarint(chunks, zigzag32(Number(value)));
|
|
78
|
+
return;
|
|
79
|
+
case 'sint64':
|
|
80
|
+
writeTag(chunks, id, WIRE_VARINT);
|
|
81
|
+
writeVarint(chunks, zigzag64(toLongish(value)));
|
|
82
|
+
return;
|
|
83
|
+
case 'fixed32':
|
|
84
|
+
case 'sfixed32':
|
|
85
|
+
writeTag(chunks, id, WIRE_32);
|
|
86
|
+
writeFixed32(chunks, Number(value) >>> 0);
|
|
87
|
+
return;
|
|
88
|
+
case 'fixed64':
|
|
89
|
+
case 'sfixed64':
|
|
90
|
+
writeTag(chunks, id, WIRE_64);
|
|
91
|
+
writeFixed64(chunks, toLongish(value));
|
|
92
|
+
return;
|
|
93
|
+
case 'float':
|
|
94
|
+
writeTag(chunks, id, WIRE_32);
|
|
95
|
+
writeFloat32(chunks, Number(value));
|
|
96
|
+
return;
|
|
97
|
+
case 'double':
|
|
98
|
+
writeTag(chunks, id, WIRE_64);
|
|
99
|
+
writeFloat64(chunks, Number(value));
|
|
100
|
+
return;
|
|
101
|
+
case 'string': {
|
|
102
|
+
const bytes = new TextEncoder().encode(String(value));
|
|
103
|
+
writeTag(chunks, id, WIRE_LEN);
|
|
104
|
+
writeVarint(chunks, bytes.length);
|
|
105
|
+
chunks.push(bytes);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
case 'bytes': {
|
|
109
|
+
const bytes = value instanceof Uint8Array
|
|
110
|
+
? value
|
|
111
|
+
: typeof value === 'string'
|
|
112
|
+
? new TextEncoder().encode(value)
|
|
113
|
+
: new Uint8Array(value);
|
|
114
|
+
writeTag(chunks, id, WIRE_LEN);
|
|
115
|
+
writeVarint(chunks, bytes.length);
|
|
116
|
+
chunks.push(bytes);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
default:
|
|
120
|
+
throw new Error(`Unsupported scalar ${type.name}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (type.kind === 'enum') {
|
|
124
|
+
const num = typeof value === 'number'
|
|
125
|
+
? value
|
|
126
|
+
: type.values[String(value)] ?? type.values[String(value).toUpperCase()];
|
|
127
|
+
if (num == null) {
|
|
128
|
+
throw new Error(`Unknown enum value ${value}`);
|
|
129
|
+
}
|
|
130
|
+
writeTag(chunks, id, WIRE_VARINT);
|
|
131
|
+
writeVarint(chunks, num);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (type.kind === 'message') {
|
|
135
|
+
const nested = encodeMessage(type.fields, value ?? {});
|
|
136
|
+
writeTag(chunks, id, WIRE_LEN);
|
|
137
|
+
writeVarint(chunks, nested.length);
|
|
138
|
+
chunks.push(nested);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (type.kind === 'map') {
|
|
142
|
+
// value should be object; each entry is a synthetic message key=1 value=2
|
|
143
|
+
const obj = value ?? {};
|
|
144
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
145
|
+
const entry = encodeMessage([
|
|
146
|
+
{ id: 1, type: type.key, bind: 'key' },
|
|
147
|
+
{ id: 2, type: type.value, bind: 'value' },
|
|
148
|
+
], { key: k, value: v });
|
|
149
|
+
writeTag(chunks, id, WIRE_LEN);
|
|
150
|
+
writeVarint(chunks, entry.length);
|
|
151
|
+
chunks.push(entry);
|
|
152
|
+
}
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function decodeMessage(fields, data) {
|
|
157
|
+
const byId = new Map(fields.map(f => [f.id, f]));
|
|
158
|
+
const out = {};
|
|
159
|
+
// Absent repeated/map fields are empty collections in protobuf wire format.
|
|
160
|
+
for (const field of fields) {
|
|
161
|
+
const key = field.bind && !field.bind.startsWith('$') ? field.bind : `_${field.id}`;
|
|
162
|
+
if (field.type.kind === 'repeated')
|
|
163
|
+
out[key] = [];
|
|
164
|
+
else if (field.type.kind === 'map')
|
|
165
|
+
out[key] = {};
|
|
166
|
+
}
|
|
167
|
+
let i = 0;
|
|
168
|
+
while (i < data.length) {
|
|
169
|
+
const tag = readVarint(data, i);
|
|
170
|
+
i = tag.next;
|
|
171
|
+
const tagNum = Number(tag.value);
|
|
172
|
+
const id = tagNum >>> 3;
|
|
173
|
+
const wire = tagNum & 7;
|
|
174
|
+
const field = byId.get(id);
|
|
175
|
+
if (!field) {
|
|
176
|
+
i = skipWire(data, i, wire);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
let typeForRead = field.type;
|
|
180
|
+
if (field.type.kind === 'message' && field.fields?.length) {
|
|
181
|
+
typeForRead = { kind: 'message', fields: field.fields };
|
|
182
|
+
}
|
|
183
|
+
else if (field.type.kind === 'repeated' && field.type.element.kind === 'message' && field.fields?.length) {
|
|
184
|
+
typeForRead = {
|
|
185
|
+
kind: 'repeated',
|
|
186
|
+
element: { kind: 'message', fields: field.fields },
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const decoded = readValue(data, i, wire, typeForRead);
|
|
190
|
+
i = decoded.next;
|
|
191
|
+
const key = field.bind && !field.bind.startsWith('$') ? field.bind : `_${id}`;
|
|
192
|
+
if (field.type.kind === 'map') {
|
|
193
|
+
const entry = decoded.value;
|
|
194
|
+
out[key] = out[key] ?? {};
|
|
195
|
+
out[key][entry.key] = entry.value;
|
|
196
|
+
}
|
|
197
|
+
else if (field.type.kind === 'repeated') {
|
|
198
|
+
out[key] = out[key] ?? [];
|
|
199
|
+
out[key].push(decoded.value);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
out[key] = decoded.value;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Map enum numbers back to names when bind keys used
|
|
206
|
+
for (const field of fields) {
|
|
207
|
+
const key = field.bind && !field.bind.startsWith('$') ? field.bind : `_${field.id}`;
|
|
208
|
+
if (field.type.kind === 'enum' && out[key] != null && typeof out[key] === 'number') {
|
|
209
|
+
const name = Object.entries(field.type.values).find(([, n]) => n === out[key])?.[0];
|
|
210
|
+
if (name)
|
|
211
|
+
out[key] = name;
|
|
212
|
+
}
|
|
213
|
+
if (field.type.kind === 'repeated' && field.type.element.kind === 'enum' && Array.isArray(out[key])) {
|
|
214
|
+
const enumValues = field.type.element.values;
|
|
215
|
+
out[key] = out[key].map((n) => {
|
|
216
|
+
if (typeof n !== 'number')
|
|
217
|
+
return n;
|
|
218
|
+
return Object.entries(enumValues).find(([, v]) => v === n)?.[0] ?? n;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
function readValue(data, i, wire, type) {
|
|
225
|
+
if (type.kind === 'repeated') {
|
|
226
|
+
// element fields carried on parent MappingField are applied by caller via typeForRead
|
|
227
|
+
return readValue(data, i, wire, type.element);
|
|
228
|
+
}
|
|
229
|
+
if (type.kind === 'map') {
|
|
230
|
+
const len = readVarint(data, i);
|
|
231
|
+
const end = len.next + Number(len.value);
|
|
232
|
+
const raw = data.subarray(len.next, end);
|
|
233
|
+
const entry = decodeMessage([
|
|
234
|
+
{ id: 1, type: type.key, bind: 'key' },
|
|
235
|
+
{ id: 2, type: type.value, bind: 'value' },
|
|
236
|
+
], raw);
|
|
237
|
+
return { value: entry, next: end, raw };
|
|
238
|
+
}
|
|
239
|
+
if (type.kind === 'message' || (type.kind === 'scalar' && (type.name === 'string' || type.name === 'bytes'))) {
|
|
240
|
+
const len = readVarint(data, i);
|
|
241
|
+
const end = len.next + Number(len.value);
|
|
242
|
+
const raw = data.subarray(len.next, end);
|
|
243
|
+
if (type.kind === 'scalar' && type.name === 'string') {
|
|
244
|
+
return { value: new TextDecoder().decode(raw), next: end, raw };
|
|
245
|
+
}
|
|
246
|
+
if (type.kind === 'scalar' && type.name === 'bytes') {
|
|
247
|
+
// graphql-scalars `Byte` validates `Buffer` instances (not bare Uint8Array).
|
|
248
|
+
return { value: Buffer.from(raw), next: end, raw };
|
|
249
|
+
}
|
|
250
|
+
if (type.kind !== 'message') {
|
|
251
|
+
throw new Error('Expected message type after length-delimited scalar branch');
|
|
252
|
+
}
|
|
253
|
+
return { value: decodeMessage(type.fields, raw), next: end, raw };
|
|
254
|
+
}
|
|
255
|
+
if (wire === WIRE_VARINT) {
|
|
256
|
+
const v = readVarint(data, i);
|
|
257
|
+
let value = v.value;
|
|
258
|
+
if (type.kind === 'scalar' && type.name === 'sint32')
|
|
259
|
+
value = unzigzag32(Number(v.value));
|
|
260
|
+
else if (type.kind === 'scalar' && type.name === 'sint64')
|
|
261
|
+
value = unzigzag64(v.value);
|
|
262
|
+
else if (type.kind === 'scalar' && type.name === 'bool')
|
|
263
|
+
value = Number(v.value) !== 0;
|
|
264
|
+
else if (type.kind === 'enum')
|
|
265
|
+
value = Number(v.value);
|
|
266
|
+
else if (type.kind === 'scalar' &&
|
|
267
|
+
(type.name === 'int32' ||
|
|
268
|
+
type.name === 'uint32' ||
|
|
269
|
+
type.name === 'int64' ||
|
|
270
|
+
type.name === 'uint64')) {
|
|
271
|
+
// Prefer Number when safe for GraphQL Int / Float paths
|
|
272
|
+
const asNum = Number(v.value);
|
|
273
|
+
value = BigInt(asNum) === v.value && Number.isSafeInteger(asNum) ? asNum : v.value;
|
|
274
|
+
}
|
|
275
|
+
return { value, next: v.next };
|
|
276
|
+
}
|
|
277
|
+
if (wire === WIRE_32) {
|
|
278
|
+
const view = new DataView(data.buffer, data.byteOffset + i, 4);
|
|
279
|
+
let value;
|
|
280
|
+
if (type.kind === 'scalar' && type.name === 'float')
|
|
281
|
+
value = view.getFloat32(0, true);
|
|
282
|
+
else
|
|
283
|
+
value = view.getUint32(0, true);
|
|
284
|
+
return { value, next: i + 4 };
|
|
285
|
+
}
|
|
286
|
+
if (wire === WIRE_64) {
|
|
287
|
+
const view = new DataView(data.buffer, data.byteOffset + i, 8);
|
|
288
|
+
let value;
|
|
289
|
+
if (type.kind === 'scalar' && type.name === 'double')
|
|
290
|
+
value = view.getFloat64(0, true);
|
|
291
|
+
else
|
|
292
|
+
value = view.getBigUint64(0, true);
|
|
293
|
+
return { value, next: i + 8 };
|
|
294
|
+
}
|
|
295
|
+
if (wire === WIRE_LEN) {
|
|
296
|
+
const len = readVarint(data, i);
|
|
297
|
+
const n = Number(len.value);
|
|
298
|
+
return { value: data.subarray(len.next, len.next + n), next: len.next + n };
|
|
299
|
+
}
|
|
300
|
+
throw new Error(`Unsupported wire type ${wire}`);
|
|
301
|
+
}
|
|
302
|
+
function skipWire(data, i, wire) {
|
|
303
|
+
if (wire === WIRE_VARINT)
|
|
304
|
+
return readVarint(data, i).next;
|
|
305
|
+
if (wire === WIRE_64)
|
|
306
|
+
return i + 8;
|
|
307
|
+
if (wire === WIRE_32)
|
|
308
|
+
return i + 4;
|
|
309
|
+
if (wire === WIRE_LEN) {
|
|
310
|
+
const len = readVarint(data, i);
|
|
311
|
+
return len.next + Number(len.value);
|
|
312
|
+
}
|
|
313
|
+
throw new Error(`Cannot skip wire type ${wire}`);
|
|
314
|
+
}
|
|
315
|
+
function writeTag(chunks, id, wire) {
|
|
316
|
+
writeVarint(chunks, (id << 3) | wire);
|
|
317
|
+
}
|
|
318
|
+
function writeVarint(chunks, value) {
|
|
319
|
+
let n = typeof value === 'bigint' ? value : BigInt(Math.trunc(Number(value)));
|
|
320
|
+
if (n < 0n) {
|
|
321
|
+
n = (1n << 64n) + n;
|
|
322
|
+
}
|
|
323
|
+
const out = [];
|
|
324
|
+
while (n > 0x7fn) {
|
|
325
|
+
out.push(Number((n & 0x7fn) | 0x80n));
|
|
326
|
+
n >>= 7n;
|
|
327
|
+
}
|
|
328
|
+
out.push(Number(n));
|
|
329
|
+
chunks.push(Uint8Array.from(out));
|
|
330
|
+
}
|
|
331
|
+
function readVarint(data, i) {
|
|
332
|
+
let result = 0n;
|
|
333
|
+
let shift = 0n;
|
|
334
|
+
let pos = i;
|
|
335
|
+
while (pos < data.length) {
|
|
336
|
+
const b = data[pos++];
|
|
337
|
+
result |= BigInt(b & 0x7f) << shift;
|
|
338
|
+
if ((b & 0x80) === 0)
|
|
339
|
+
break;
|
|
340
|
+
shift += 7n;
|
|
341
|
+
}
|
|
342
|
+
return { value: result, next: pos };
|
|
343
|
+
}
|
|
344
|
+
function writeFixed32(chunks, value) {
|
|
345
|
+
const buf = new Uint8Array(4);
|
|
346
|
+
new DataView(buf.buffer).setUint32(0, value, true);
|
|
347
|
+
chunks.push(buf);
|
|
348
|
+
}
|
|
349
|
+
function writeFixed64(chunks, value) {
|
|
350
|
+
const buf = new Uint8Array(8);
|
|
351
|
+
new DataView(buf.buffer).setBigUint64(0, BigInt(value), true);
|
|
352
|
+
chunks.push(buf);
|
|
353
|
+
}
|
|
354
|
+
function writeFloat32(chunks, value) {
|
|
355
|
+
const buf = new Uint8Array(4);
|
|
356
|
+
new DataView(buf.buffer).setFloat32(0, value, true);
|
|
357
|
+
chunks.push(buf);
|
|
358
|
+
}
|
|
359
|
+
function writeFloat64(chunks, value) {
|
|
360
|
+
const buf = new Uint8Array(8);
|
|
361
|
+
new DataView(buf.buffer).setFloat64(0, value, true);
|
|
362
|
+
chunks.push(buf);
|
|
363
|
+
}
|
|
364
|
+
function zigzag32(n) {
|
|
365
|
+
return (n << 1) ^ (n >> 31);
|
|
366
|
+
}
|
|
367
|
+
function zigzag64(n) {
|
|
368
|
+
const v = BigInt(n);
|
|
369
|
+
return (v << 1n) ^ (v >> 63n);
|
|
370
|
+
}
|
|
371
|
+
function unzigzag32(n) {
|
|
372
|
+
return (n >>> 1) ^ -(n & 1);
|
|
373
|
+
}
|
|
374
|
+
function unzigzag64(n) {
|
|
375
|
+
return (n >> 1n) ^ -(n & 1n);
|
|
376
|
+
}
|
|
377
|
+
function toLongish(value) {
|
|
378
|
+
if (typeof value === 'bigint')
|
|
379
|
+
return value;
|
|
380
|
+
if (typeof value === 'string')
|
|
381
|
+
return BigInt(value);
|
|
382
|
+
return Math.trunc(Number(value));
|
|
383
|
+
}
|
|
384
|
+
function concat(chunks) {
|
|
385
|
+
const len = chunks.reduce((a, c) => a + c.length, 0);
|
|
386
|
+
const out = new Uint8Array(len);
|
|
387
|
+
let o = 0;
|
|
388
|
+
for (const c of chunks) {
|
|
389
|
+
out.set(c, o);
|
|
390
|
+
o += c.length;
|
|
391
|
+
}
|
|
392
|
+
return out;
|
|
393
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_PROTOBUF_HTTP_CONTENT_TYPE = void 0;
|
|
4
|
+
exports.isHttpLocation = isHttpLocation;
|
|
5
|
+
exports.buildRpcPath = buildRpcPath;
|
|
6
|
+
exports.resolveHttpUrl = resolveHttpUrl;
|
|
7
|
+
exports.httpProtobufUnary = httpProtobufUnary;
|
|
8
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
9
|
+
function isHttpLocation(location) {
|
|
10
|
+
return /^https?:\/\//i.test(location);
|
|
11
|
+
}
|
|
12
|
+
function buildRpcPath(service, method) {
|
|
13
|
+
const svc = service?.replace(/^\//, '') || '';
|
|
14
|
+
if (!svc)
|
|
15
|
+
return `/${method}`;
|
|
16
|
+
return `/${svc}/${method}`;
|
|
17
|
+
}
|
|
18
|
+
function resolveHttpUrl(location, service, method) {
|
|
19
|
+
const base = location.replace(/\/$/, '');
|
|
20
|
+
return `${base}${buildRpcPath(service, method)}`;
|
|
21
|
+
}
|
|
22
|
+
/** Default Connect unary binary content type; override via `@transport(headers:)` (e.g. Twirp `application/protobuf`). */
|
|
23
|
+
exports.DEFAULT_PROTOBUF_HTTP_CONTENT_TYPE = 'application/proto';
|
|
24
|
+
async function httpProtobufUnary(opts) {
|
|
25
|
+
const fetchFn = opts.fetchFn || fetch_1.fetch;
|
|
26
|
+
const res = await fetchFn(opts.url, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: {
|
|
29
|
+
'content-type': exports.DEFAULT_PROTOBUF_HTTP_CONTENT_TYPE,
|
|
30
|
+
...opts.headers,
|
|
31
|
+
},
|
|
32
|
+
body: opts.body,
|
|
33
|
+
signal: opts.signal,
|
|
34
|
+
}, opts.context, opts.info);
|
|
35
|
+
if (!res.ok) {
|
|
36
|
+
const text = await res.text().catch(() => '');
|
|
37
|
+
throw new Error(`Protobuf HTTP ${res.status}: ${text || res.statusText}`);
|
|
38
|
+
}
|
|
39
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
40
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Compact protobuf mapping dialect (see Mesh protobuf RPC spec).
|
|
4
|
+
* body/selection: fieldId(type): bind with nested { }
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.parseMapping = parseMapping;
|
|
8
|
+
exports.resolveBind = resolveBind;
|
|
9
|
+
function parseMapping(source) {
|
|
10
|
+
const s = source.trim();
|
|
11
|
+
if (!s)
|
|
12
|
+
return [];
|
|
13
|
+
return parseFieldList(s, 0).fields;
|
|
14
|
+
}
|
|
15
|
+
function parseFieldList(source, start) {
|
|
16
|
+
const fields = [];
|
|
17
|
+
let i = skipWs(source, start);
|
|
18
|
+
while (i < source.length && source[i] !== '}') {
|
|
19
|
+
const parsed = parseField(source, i);
|
|
20
|
+
fields.push(parsed.field);
|
|
21
|
+
i = skipWs(source, parsed.end);
|
|
22
|
+
}
|
|
23
|
+
return { fields, end: i };
|
|
24
|
+
}
|
|
25
|
+
function parseField(source, start) {
|
|
26
|
+
let i = skipWs(source, start);
|
|
27
|
+
const idStart = i;
|
|
28
|
+
while (i < source.length && /[0-9]/.test(source[i]))
|
|
29
|
+
i++;
|
|
30
|
+
if (i === idStart) {
|
|
31
|
+
throw new Error(`Expected field id at ${snippet(source, start)}`);
|
|
32
|
+
}
|
|
33
|
+
const id = Number(source.slice(idStart, i));
|
|
34
|
+
i = skipWs(source, i);
|
|
35
|
+
if (source[i] !== '(') {
|
|
36
|
+
throw new Error(`Expected '(' after field id ${id} at ${snippet(source, i)}`);
|
|
37
|
+
}
|
|
38
|
+
i++;
|
|
39
|
+
const typeParsed = parseType(source, i);
|
|
40
|
+
i = skipWs(source, typeParsed.end);
|
|
41
|
+
if (source[i] !== ')') {
|
|
42
|
+
throw new Error(`Expected ')' after type at ${snippet(source, i)}`);
|
|
43
|
+
}
|
|
44
|
+
i++;
|
|
45
|
+
i = skipWs(source, i);
|
|
46
|
+
if (source[i] !== ':') {
|
|
47
|
+
throw new Error(`Expected ':' after type at ${snippet(source, i)}`);
|
|
48
|
+
}
|
|
49
|
+
i++;
|
|
50
|
+
i = skipWs(source, i);
|
|
51
|
+
if (source[i] === '{') {
|
|
52
|
+
i++;
|
|
53
|
+
const nested = parseFieldList(source, i);
|
|
54
|
+
i = skipWs(source, nested.end);
|
|
55
|
+
if (source[i] !== '}') {
|
|
56
|
+
throw new Error(`Expected '}' at ${snippet(source, i)}`);
|
|
57
|
+
}
|
|
58
|
+
i++;
|
|
59
|
+
// optional bind after nested block: `1(message): movie { ... }` already consumed;
|
|
60
|
+
// support `1(message): { ... }` or `1(message): name { ... }` — if we had a bind before `{`
|
|
61
|
+
return {
|
|
62
|
+
field: {
|
|
63
|
+
id,
|
|
64
|
+
type: { kind: 'message', fields: nested.fields },
|
|
65
|
+
fields: nested.fields,
|
|
66
|
+
},
|
|
67
|
+
end: i,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const bindStart = i;
|
|
71
|
+
while (i < source.length && !/[\s}]/.test(source[i])) {
|
|
72
|
+
// allow $args.foo.bar
|
|
73
|
+
if (source[i] === '{')
|
|
74
|
+
break;
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
// If next non-ws is `{`, bind was a name before nested message
|
|
78
|
+
const afterBind = skipWs(source, i);
|
|
79
|
+
if (source[afterBind] === '{') {
|
|
80
|
+
const bind = source.slice(bindStart, i).trim();
|
|
81
|
+
i = afterBind + 1;
|
|
82
|
+
const nested = parseFieldList(source, i);
|
|
83
|
+
i = skipWs(source, nested.end);
|
|
84
|
+
if (source[i] !== '}') {
|
|
85
|
+
throw new Error(`Expected '}' at ${snippet(source, i)}`);
|
|
86
|
+
}
|
|
87
|
+
i++;
|
|
88
|
+
const baseType = typeParsed.type;
|
|
89
|
+
const messageType = { kind: 'message', fields: nested.fields };
|
|
90
|
+
const type = baseType.kind === 'repeated'
|
|
91
|
+
? { kind: 'repeated', element: messageType }
|
|
92
|
+
: messageType;
|
|
93
|
+
return {
|
|
94
|
+
field: {
|
|
95
|
+
id,
|
|
96
|
+
type,
|
|
97
|
+
bind: bind || undefined,
|
|
98
|
+
fields: nested.fields,
|
|
99
|
+
},
|
|
100
|
+
end: i,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const bind = source.slice(bindStart, i).trim();
|
|
104
|
+
return {
|
|
105
|
+
field: {
|
|
106
|
+
id,
|
|
107
|
+
type: typeParsed.type,
|
|
108
|
+
bind: bind || undefined,
|
|
109
|
+
},
|
|
110
|
+
end: i,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function parseType(source, start) {
|
|
114
|
+
let i = skipWs(source, start);
|
|
115
|
+
if (source.startsWith('repeated', i)) {
|
|
116
|
+
i = skipWs(source, i + 'repeated'.length);
|
|
117
|
+
const el = parseType(source, i);
|
|
118
|
+
return { type: { kind: 'repeated', element: el.type }, end: el.end };
|
|
119
|
+
}
|
|
120
|
+
if (source.startsWith('map<', i)) {
|
|
121
|
+
i = skipWs(source, i + 4);
|
|
122
|
+
const key = parseType(source, i);
|
|
123
|
+
i = skipWs(source, key.end);
|
|
124
|
+
if (source[i] !== ',') {
|
|
125
|
+
throw new Error(`Expected ',' in map<> at ${snippet(source, i)}`);
|
|
126
|
+
}
|
|
127
|
+
i++;
|
|
128
|
+
const value = parseType(source, i);
|
|
129
|
+
i = skipWs(source, value.end);
|
|
130
|
+
if (source[i] !== '>') {
|
|
131
|
+
throw new Error(`Expected '>' in map<> at ${snippet(source, i)}`);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
type: { kind: 'map', key: key.type, value: value.type },
|
|
135
|
+
end: i + 1,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (source.startsWith('enum{', i) || source.startsWith('enum {', i)) {
|
|
139
|
+
const brace = source.indexOf('{', i);
|
|
140
|
+
i = brace + 1;
|
|
141
|
+
const values = {};
|
|
142
|
+
i = skipWs(source, i);
|
|
143
|
+
while (i < source.length && source[i] !== '}') {
|
|
144
|
+
const nameStart = i;
|
|
145
|
+
while (i < source.length && /[A-Za-z0-9_]/.test(source[i]))
|
|
146
|
+
i++;
|
|
147
|
+
const name = source.slice(nameStart, i);
|
|
148
|
+
i = skipWs(source, i);
|
|
149
|
+
if (source[i] !== '=') {
|
|
150
|
+
throw new Error(`Expected '=' in enum at ${snippet(source, i)}`);
|
|
151
|
+
}
|
|
152
|
+
i++;
|
|
153
|
+
i = skipWs(source, i);
|
|
154
|
+
const numStart = i;
|
|
155
|
+
while (i < source.length && /[0-9-]/.test(source[i]))
|
|
156
|
+
i++;
|
|
157
|
+
values[name] = Number(source.slice(numStart, i));
|
|
158
|
+
i = skipWs(source, i);
|
|
159
|
+
if (source[i] === ',') {
|
|
160
|
+
i++;
|
|
161
|
+
i = skipWs(source, i);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (source[i] !== '}') {
|
|
165
|
+
throw new Error(`Expected '}' in enum at ${snippet(source, i)}`);
|
|
166
|
+
}
|
|
167
|
+
return { type: { kind: 'enum', values }, end: i + 1 };
|
|
168
|
+
}
|
|
169
|
+
if (source.startsWith('message', i)) {
|
|
170
|
+
return { type: { kind: 'message', fields: [] }, end: i + 'message'.length };
|
|
171
|
+
}
|
|
172
|
+
const nameStart = i;
|
|
173
|
+
while (i < source.length && /[a-z0-9]/.test(source[i]))
|
|
174
|
+
i++;
|
|
175
|
+
const name = source.slice(nameStart, i);
|
|
176
|
+
return { type: { kind: 'scalar', name }, end: i };
|
|
177
|
+
}
|
|
178
|
+
function skipWs(source, i) {
|
|
179
|
+
while (i < source.length) {
|
|
180
|
+
if (/\s/.test(source[i])) {
|
|
181
|
+
i++;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
// `#` comments are documentation-only (loader emit / hand SDL); skip to EOL
|
|
185
|
+
if (source[i] === '#') {
|
|
186
|
+
while (i < source.length && source[i] !== '\n')
|
|
187
|
+
i++;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
return i;
|
|
193
|
+
}
|
|
194
|
+
function snippet(source, i) {
|
|
195
|
+
return JSON.stringify(source.slice(Math.max(0, i - 10), i + 20));
|
|
196
|
+
}
|
|
197
|
+
function resolveBind(bind, args, thisRoot) {
|
|
198
|
+
if (bind == null || bind === '') {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
if (bind.startsWith('$this.')) {
|
|
202
|
+
return getPath(thisRoot ?? args, bind.slice('$this.'.length));
|
|
203
|
+
}
|
|
204
|
+
if (bind === '$this') {
|
|
205
|
+
return thisRoot ?? args;
|
|
206
|
+
}
|
|
207
|
+
if (bind.startsWith('$args.')) {
|
|
208
|
+
return getPath(args, bind.slice('$args.'.length));
|
|
209
|
+
}
|
|
210
|
+
if (bind === '$args') {
|
|
211
|
+
return args;
|
|
212
|
+
}
|
|
213
|
+
return getPath(args, bind);
|
|
214
|
+
}
|
|
215
|
+
function getPath(obj, pathStr) {
|
|
216
|
+
if (obj == null)
|
|
217
|
+
return undefined;
|
|
218
|
+
const parts = pathStr.split('.');
|
|
219
|
+
let cur = obj;
|
|
220
|
+
for (const p of parts) {
|
|
221
|
+
if (cur == null)
|
|
222
|
+
return undefined;
|
|
223
|
+
cur = cur[p];
|
|
224
|
+
}
|
|
225
|
+
return cur;
|
|
226
|
+
}
|