ts-proto 1.136.0 → 1.137.0
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/build/main.js +73 -30
- package/build/options.js +1 -0
- package/build/types.js +12 -0
- package/package.json +1 -1
package/build/main.js
CHANGED
|
@@ -225,7 +225,7 @@ function makeUtils(options) {
|
|
|
225
225
|
...longs,
|
|
226
226
|
...makeComparisonUtils(),
|
|
227
227
|
...makeNiceGrpcServerStreamingMethodResult(),
|
|
228
|
-
...makeGrpcWebErrorClass(),
|
|
228
|
+
...makeGrpcWebErrorClass(bytes),
|
|
229
229
|
};
|
|
230
230
|
}
|
|
231
231
|
exports.makeUtils = makeUtils;
|
|
@@ -286,6 +286,11 @@ function makeLongUtils(options, bytes) {
|
|
|
286
286
|
return long.toString();
|
|
287
287
|
}
|
|
288
288
|
`);
|
|
289
|
+
const longToBigint = (0, ts_poet_1.conditionalOutput)("longToBigint", (0, ts_poet_1.code) `
|
|
290
|
+
function longToBigint(long: ${Long}) {
|
|
291
|
+
return BigInt(long.toString());
|
|
292
|
+
}
|
|
293
|
+
`);
|
|
289
294
|
const longToNumber = (0, ts_poet_1.conditionalOutput)("longToNumber", (0, ts_poet_1.code) `
|
|
290
295
|
function longToNumber(long: ${Long}): number {
|
|
291
296
|
if (long.gt(Number.MAX_SAFE_INTEGER)) {
|
|
@@ -294,14 +299,14 @@ function makeLongUtils(options, bytes) {
|
|
|
294
299
|
return long.toNumber();
|
|
295
300
|
}
|
|
296
301
|
`);
|
|
297
|
-
return { numberToLong, longToNumber, longToString, Long };
|
|
302
|
+
return { numberToLong, longToNumber, longToString, longToBigint, Long };
|
|
298
303
|
}
|
|
299
304
|
function makeByteUtils() {
|
|
300
|
-
const globalThis = (0, ts_poet_1.conditionalOutput)("
|
|
305
|
+
const globalThis = (0, ts_poet_1.conditionalOutput)("tsProtoGlobalThis", (0, ts_poet_1.code) `
|
|
301
306
|
declare var self: any | undefined;
|
|
302
307
|
declare var window: any | undefined;
|
|
303
308
|
declare var global: any | undefined;
|
|
304
|
-
var
|
|
309
|
+
var tsProtoGlobalThis: any = (() => {
|
|
305
310
|
if (typeof globalThis !== "undefined") return globalThis;
|
|
306
311
|
if (typeof self !== "undefined") return self;
|
|
307
312
|
if (typeof window !== "undefined") return window;
|
|
@@ -409,6 +414,10 @@ function makeTimestampMethods(options, longs) {
|
|
|
409
414
|
toNumberCode = "t.seconds.toNumber()";
|
|
410
415
|
seconds = (0, ts_poet_1.code) `${longs.numberToLong}(date.getTime() / 1_000)`;
|
|
411
416
|
}
|
|
417
|
+
else if (options.forceLong === options_1.LongOption.BIGINT) {
|
|
418
|
+
toNumberCode = "Number(t.seconds.toString())";
|
|
419
|
+
seconds = (0, ts_poet_1.code) `BigInt(Math.trunc(date.getTime() / 1_000))`;
|
|
420
|
+
}
|
|
412
421
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
413
422
|
toNumberCode = "Number(t.seconds)";
|
|
414
423
|
// Must discard the fractional piece here
|
|
@@ -492,9 +501,9 @@ function makeNiceGrpcServerStreamingMethodResult() {
|
|
|
492
501
|
`);
|
|
493
502
|
return { NiceGrpcServerStreamingMethodResult };
|
|
494
503
|
}
|
|
495
|
-
function makeGrpcWebErrorClass() {
|
|
504
|
+
function makeGrpcWebErrorClass(bytes) {
|
|
496
505
|
const GrpcWebError = (0, ts_poet_1.conditionalOutput)("GrpcWebError", (0, ts_poet_1.code) `
|
|
497
|
-
export class GrpcWebError extends globalThis.Error {
|
|
506
|
+
export class GrpcWebError extends ${bytes.globalThis}.Error {
|
|
498
507
|
constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) {
|
|
499
508
|
super(message);
|
|
500
509
|
}
|
|
@@ -655,6 +664,9 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
655
664
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
656
665
|
readSnippet = (0, ts_poet_1.code) `${utils.longToString}(${readSnippet} as Long)`;
|
|
657
666
|
}
|
|
667
|
+
else if (options.forceLong === options_1.LongOption.BIGINT) {
|
|
668
|
+
readSnippet = (0, ts_poet_1.code) `${utils.longToBigint}(${readSnippet} as Long)`;
|
|
669
|
+
}
|
|
658
670
|
else {
|
|
659
671
|
readSnippet = (0, ts_poet_1.code) `${utils.longToNumber}(${readSnippet} as Long)`;
|
|
660
672
|
}
|
|
@@ -781,6 +793,10 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
781
793
|
const toNumber = (0, types_1.getEnumMethod)(ctx, field.typeName, "ToNumber");
|
|
782
794
|
writeSnippet = (place) => (0, ts_poet_1.code) `writer.uint32(${tag}).${(0, types_1.toReaderCall)(field)}(${toNumber}(${place}))`;
|
|
783
795
|
}
|
|
796
|
+
else if ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.BIGINT) {
|
|
797
|
+
const tag = ((field.number << 3) | (0, types_1.basicWireType)(field.type)) >>> 0;
|
|
798
|
+
writeSnippet = (place) => (0, ts_poet_1.code) `writer.uint32(${tag}).${(0, types_1.toReaderCall)(field)}(${place}.toString())`;
|
|
799
|
+
}
|
|
784
800
|
else if ((0, types_1.isScalar)(field) || (0, types_1.isEnum)(field)) {
|
|
785
801
|
const tag = ((field.number << 3) | (0, types_1.basicWireType)(field.type)) >>> 0;
|
|
786
802
|
writeSnippet = (place) => (0, ts_poet_1.code) `writer.uint32(${tag}).${(0, types_1.toReaderCall)(field)}(${place})`;
|
|
@@ -1015,6 +1031,9 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1015
1031
|
const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, field, { keepValueType: true }).toCodeString());
|
|
1016
1032
|
return (0, ts_poet_1.code) `${cstr}.fromValue(${from})`;
|
|
1017
1033
|
}
|
|
1034
|
+
else if ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.BIGINT) {
|
|
1035
|
+
return (0, ts_poet_1.code) `BigInt(${from})`;
|
|
1036
|
+
}
|
|
1018
1037
|
else {
|
|
1019
1038
|
const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, field, { keepValueType: true }).toCodeString());
|
|
1020
1039
|
return (0, ts_poet_1.code) `${cstr}(${from})`;
|
|
@@ -1045,6 +1064,9 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1045
1064
|
if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.LONG) {
|
|
1046
1065
|
return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString())}.fromValue(${from})`;
|
|
1047
1066
|
}
|
|
1067
|
+
else if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.BIGINT) {
|
|
1068
|
+
return (0, ts_poet_1.code) `BigInt(${from})`;
|
|
1069
|
+
}
|
|
1048
1070
|
else if ((0, types_1.isBytesValueType)(field)) {
|
|
1049
1071
|
return (0, ts_poet_1.code) `new ${(0, case_1.capitalize)(valueType.toCodeString())}(${from})`;
|
|
1050
1072
|
}
|
|
@@ -1267,6 +1289,9 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1267
1289
|
else if ((0, types_1.isLong)(valueType) && options.forceLong === options_1.LongOption.LONG) {
|
|
1268
1290
|
return (0, ts_poet_1.code) `${from}.toString()`;
|
|
1269
1291
|
}
|
|
1292
|
+
else if ((0, types_1.isLong)(valueType) && options.forceLong === options_1.LongOption.BIGINT) {
|
|
1293
|
+
return (0, ts_poet_1.code) `${from}.toString()`;
|
|
1294
|
+
}
|
|
1270
1295
|
else if ((0, types_1.isWholeNumber)(valueType) && !((0, types_1.isLong)(valueType) && options.forceLong === options_1.LongOption.STRING)) {
|
|
1271
1296
|
return (0, ts_poet_1.code) `Math.round(${from})`;
|
|
1272
1297
|
}
|
|
@@ -1304,6 +1329,9 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1304
1329
|
const v = (0, types_1.isWithinOneOf)(field) ? "undefined" : (0, types_1.defaultValue)(ctx, field);
|
|
1305
1330
|
return (0, ts_poet_1.code) `(${from} || ${v}).toString()`;
|
|
1306
1331
|
}
|
|
1332
|
+
else if ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.BIGINT) {
|
|
1333
|
+
return (0, ts_poet_1.code) `${from}.toString()`;
|
|
1334
|
+
}
|
|
1307
1335
|
else if ((0, types_1.isWholeNumber)(field) && !((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.STRING)) {
|
|
1308
1336
|
return (0, ts_poet_1.code) `Math.round(${from})`;
|
|
1309
1337
|
}
|
|
@@ -1512,11 +1540,15 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1512
1540
|
function generateWrap(ctx, fullProtoTypeName, fieldNames) {
|
|
1513
1541
|
const chunks = [];
|
|
1514
1542
|
if ((0, types_1.isStructTypeName)(fullProtoTypeName)) {
|
|
1543
|
+
let setStatement = "struct.fields[key] = object[key];";
|
|
1544
|
+
if (ctx.options.useMapType) {
|
|
1545
|
+
setStatement = "struct.fields.set(key, object[key]);";
|
|
1546
|
+
}
|
|
1515
1547
|
chunks.push((0, ts_poet_1.code) `wrap(object: {[key: string]: any} | undefined): Struct {
|
|
1516
1548
|
const struct = createBaseStruct();
|
|
1517
1549
|
if (object !== undefined) {
|
|
1518
1550
|
Object.keys(object).forEach(key => {
|
|
1519
|
-
|
|
1551
|
+
${setStatement}
|
|
1520
1552
|
});
|
|
1521
1553
|
}
|
|
1522
1554
|
return struct;
|
|
@@ -1593,13 +1625,24 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1593
1625
|
function generateUnwrap(ctx, fullProtoTypeName, fieldNames) {
|
|
1594
1626
|
const chunks = [];
|
|
1595
1627
|
if ((0, types_1.isStructTypeName)(fullProtoTypeName)) {
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1628
|
+
if (ctx.options.useMapType) {
|
|
1629
|
+
chunks.push((0, ts_poet_1.code) `unwrap(message: Struct): {[key: string]: any} {
|
|
1630
|
+
const object: { [key: string]: any } = {};
|
|
1631
|
+
[...message.fields.keys()].forEach((key) => {
|
|
1632
|
+
object[key] = message.fields.get(key);
|
|
1633
|
+
});
|
|
1634
|
+
return object;
|
|
1635
|
+
}`);
|
|
1636
|
+
}
|
|
1637
|
+
else {
|
|
1638
|
+
chunks.push((0, ts_poet_1.code) `unwrap(message: Struct): {[key: string]: any} {
|
|
1639
|
+
const object: { [key: string]: any } = {};
|
|
1640
|
+
Object.keys(message.fields).forEach(key => {
|
|
1641
|
+
object[key] = message.fields[key];
|
|
1642
|
+
});
|
|
1643
|
+
return object;
|
|
1644
|
+
}`);
|
|
1645
|
+
}
|
|
1603
1646
|
}
|
|
1604
1647
|
if ((0, types_1.isAnyValueTypeName)(fullProtoTypeName)) {
|
|
1605
1648
|
if (ctx.options.oneof === options_1.OneofOption.UNIONS) {
|
|
@@ -1619,25 +1662,25 @@ function generateUnwrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1619
1662
|
} else {
|
|
1620
1663
|
return undefined;
|
|
1621
1664
|
}
|
|
1622
|
-
|
|
1665
|
+
}`);
|
|
1623
1666
|
}
|
|
1624
1667
|
else {
|
|
1625
1668
|
chunks.push((0, ts_poet_1.code) `unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1669
|
+
if (message?.${fieldNames.stringValue} !== undefined) {
|
|
1670
|
+
return message.${fieldNames.stringValue};
|
|
1671
|
+
} else if (message?.${fieldNames.numberValue} !== undefined) {
|
|
1672
|
+
return message.${fieldNames.numberValue};
|
|
1673
|
+
} else if (message?.${fieldNames.boolValue} !== undefined) {
|
|
1674
|
+
return message.${fieldNames.boolValue};
|
|
1675
|
+
} else if (message?.${fieldNames.structValue} !== undefined) {
|
|
1676
|
+
return message.${fieldNames.structValue};
|
|
1677
|
+
} else if (message?.${fieldNames.listValue} !== undefined) {
|
|
1678
|
+
return message.${fieldNames.listValue};
|
|
1679
|
+
} else if (message?.${fieldNames.nullValue} !== undefined) {
|
|
1680
|
+
return null;
|
|
1681
|
+
}
|
|
1682
|
+
return undefined;
|
|
1683
|
+
}`);
|
|
1641
1684
|
}
|
|
1642
1685
|
}
|
|
1643
1686
|
if ((0, types_1.isListValueTypeName)(fullProtoTypeName)) {
|
package/build/options.js
CHANGED
package/build/types.js
CHANGED
|
@@ -192,6 +192,9 @@ function defaultValue(ctx, field) {
|
|
|
192
192
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
193
193
|
return '"0"';
|
|
194
194
|
}
|
|
195
|
+
else if (options.forceLong === options_1.LongOption.BIGINT) {
|
|
196
|
+
return 'BigInt("0")';
|
|
197
|
+
}
|
|
195
198
|
else {
|
|
196
199
|
return 0;
|
|
197
200
|
}
|
|
@@ -204,6 +207,9 @@ function defaultValue(ctx, field) {
|
|
|
204
207
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
205
208
|
return '"0"';
|
|
206
209
|
}
|
|
210
|
+
else if (options.forceLong === options_1.LongOption.BIGINT) {
|
|
211
|
+
return 'BigInt("0")';
|
|
212
|
+
}
|
|
207
213
|
else {
|
|
208
214
|
return 0;
|
|
209
215
|
}
|
|
@@ -263,6 +269,9 @@ function notDefaultCheck(ctx, field, messageOptions, place) {
|
|
|
263
269
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
264
270
|
return (0, ts_poet_1.code) `${maybeNotUndefinedAnd} ${place} !== "0"`;
|
|
265
271
|
}
|
|
272
|
+
else if (options.forceLong === options_1.LongOption.BIGINT) {
|
|
273
|
+
return (0, ts_poet_1.code) `${maybeNotUndefinedAnd} ${place} !== BigInt("0")`;
|
|
274
|
+
}
|
|
266
275
|
else {
|
|
267
276
|
return (0, ts_poet_1.code) `${maybeNotUndefinedAnd} ${place} !== 0`;
|
|
268
277
|
}
|
|
@@ -508,6 +517,9 @@ function longTypeName(ctx) {
|
|
|
508
517
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
509
518
|
return (0, ts_poet_1.code) `string`;
|
|
510
519
|
}
|
|
520
|
+
else if (options.forceLong === options_1.LongOption.BIGINT) {
|
|
521
|
+
return (0, ts_poet_1.code) `bigint`;
|
|
522
|
+
}
|
|
511
523
|
else {
|
|
512
524
|
return (0, ts_poet_1.code) `number`;
|
|
513
525
|
}
|