ts-proto 2.12.0 → 2.12.1

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/src/main.js CHANGED
@@ -1089,7 +1089,13 @@ function generateDecode(ctx, fullName, messageDesc) {
1089
1089
  length?: number,
1090
1090
  ): ${fullName} {
1091
1091
  const reader = input instanceof ${BinaryReader} ? input : new ${BinaryReader}(input);
1092
- const end = length === undefined ? reader.len : reader.pos + length;
1092
+ const previousRecursionDepth = (reader as any).__tsProtoDecodeDepth ?? 0;
1093
+ if (previousRecursionDepth >= 100) {
1094
+ throw new ${ctx.utils.globalThis}.Error("protobuf decode recursion limit exceeded");
1095
+ }
1096
+ (reader as any).__tsProtoDecodeDepth = previousRecursionDepth + 1;
1097
+ try {
1098
+ const end = length === undefined ? reader.len : reader.pos + length;
1093
1099
  `);
1094
1100
  chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
1095
1101
  // start the tag loop
@@ -1255,7 +1261,12 @@ function generateDecode(ctx, fullName, messageDesc) {
1255
1261
  // and then wrap up the while/return
1256
1262
  chunks.push((0, ts_poet_1.code) `}`);
1257
1263
  chunks.push((0, ts_poet_1.code) `return message;`);
1258
- chunks.push((0, ts_poet_1.code) `}`);
1264
+ chunks.push((0, ts_poet_1.code) `
1265
+ } finally {
1266
+ (reader as any).__tsProtoDecodeDepth = previousRecursionDepth;
1267
+ }
1268
+ }
1269
+ `);
1259
1270
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
1260
1271
  }
1261
1272
  /** Returns a generic writer.doSomething based on the basic type */
@@ -2039,7 +2050,10 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
2039
2050
  protoJsonComparison = (0, ts_poet_1.code) `
2040
2051
  : ${ctx.utils.isObject}(${protoJsonProperty})
2041
2052
  ? (${ctx.utils.globalThis}.Object.entries(${protoJsonProperty}) as [string, any][]).reduce((acc: ${fieldType}, [key, value]: [string, any]) => {
2042
- acc[${i}] = ${readSnippet("value")};
2053
+ ${ctx.utils.globalThis}.Object.defineProperty(acc, ${i}, {
2054
+ value: ${readSnippet("value")},
2055
+ enumerable: true, configurable: true, writable: true,
2056
+ });
2043
2057
  return acc;
2044
2058
  }, {})
2045
2059
  `;
@@ -2047,7 +2061,10 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
2047
2061
  chunks.push((0, ts_poet_1.code) `
2048
2062
  ${fieldKey}: ${ctx.utils.isObject}(${jsonProperty})
2049
2063
  ? (${ctx.utils.globalThis}.Object.entries(${jsonProperty}) as [string, any][]).reduce((acc: ${fieldType}, [key, value]: [string, any]) => {
2050
- acc[${i}] = ${readSnippet("value")};
2064
+ ${ctx.utils.globalThis}.Object.defineProperty(acc, ${i}, {
2065
+ value: ${readSnippet("value")},
2066
+ enumerable: true, configurable: true, writable: true,
2067
+ });
2051
2068
  return acc;
2052
2069
  }, {})
2053
2070
  ${protoJsonComparison}
@@ -2221,6 +2238,9 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2221
2238
  const jsonName = (0, utils_1.getFieldJsonName)(field, options);
2222
2239
  const jsonProperty = (0, utils_1.getPropertyAccessor)("obj", jsonName);
2223
2240
  const messageProperty = (0, utils_1.getPropertyAccessor)("message", fieldName);
2241
+ const setJsonProperty = (value) => jsonName === "__proto__"
2242
+ ? (0, ts_poet_1.code) `${utils.globalThis}.Object.defineProperty(obj, "__proto__", { value: ${value}, enumerable: true });`
2243
+ : (0, ts_poet_1.code) `${jsonProperty} = ${value};`;
2224
2244
  const readSnippet = (from) => {
2225
2245
  if ((0, types_1.isEnum)(field)) {
2226
2246
  const toJson = (0, types_1.getEnumMethod)(ctx, field.typeName, "ToJSON");
@@ -2332,7 +2352,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2332
2352
  if ((0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)) {
2333
2353
  chunks.push((0, ts_poet_1.code) `
2334
2354
  if (${messageProperty}?.size) {
2335
- ${jsonProperty} = {};
2355
+ ${setJsonProperty("{}")}
2336
2356
  ${messageProperty}.forEach((v, k) => {
2337
2357
  ${jsonProperty}[${i}] = ${readSnippet("v")};
2338
2358
  });
@@ -2345,7 +2365,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2345
2365
  if (${messageProperty}) {
2346
2366
  const entries = ${utils.globalThis}.Object.entries(${messageProperty}) as [string, ${mapInfo.valueType}][];
2347
2367
  if (entries.length > 0) {
2348
- ${jsonProperty} = {};
2368
+ ${setJsonProperty("{}")}
2349
2369
  entries.forEach(([k, v]) => {
2350
2370
  ${jsonProperty}[${i}] = ${readSnippet("v")};
2351
2371
  });
@@ -2360,7 +2380,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2360
2380
  const maybeMap = needMap ? (0, ts_poet_1.code) `.map(e => ${readSnippet("e")})` : "";
2361
2381
  chunks.push((0, ts_poet_1.code) `
2362
2382
  if (${messageProperty}?.length) {
2363
- ${jsonProperty} = ${messageProperty}${maybeMap};
2383
+ ${setJsonProperty((0, ts_poet_1.code) `${messageProperty}${maybeMap}`)}
2364
2384
  }
2365
2385
  `);
2366
2386
  }
@@ -2372,7 +2392,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2372
2392
  const valueName = (0, utils_1.oneofValueName)(fieldName, options);
2373
2393
  chunks.push((0, ts_poet_1.code) `
2374
2394
  ${currentIfTarget === oneofNameWithMessage ? "else " : ""}if (${oneofNameWithMessage}?.$case === '${fieldName}') {
2375
- ${jsonProperty} = ${readSnippet(`${oneofNameWithMessage}.${valueName}`)};
2395
+ ${setJsonProperty(readSnippet(`${oneofNameWithMessage}.${valueName}`))}
2376
2396
  }
2377
2397
  `);
2378
2398
  currentIfTarget = oneofNameWithMessage;
@@ -2384,7 +2404,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2384
2404
  : `${messageProperty} !== undefined ${(0, utils_1.withAndMaybeCheckIsNotNull)(options, messageProperty)}`;
2385
2405
  chunks.push((0, ts_poet_1.code) `
2386
2406
  if (${check}) {
2387
- ${jsonProperty} = ${readSnippet(`${messageProperty}`)};
2407
+ ${setJsonProperty(readSnippet(`${messageProperty}`))}
2388
2408
  }
2389
2409
  `);
2390
2410
  }
@@ -296,7 +296,7 @@ function defaultValue(ctx, field) {
296
296
  case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL:
297
297
  return useDefaultValue ? field.defaultValue : false;
298
298
  case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_STRING:
299
- return useDefaultValue ? `"${field.defaultValue}"` : '""';
299
+ return useDefaultValue ? JSON.stringify(field.defaultValue) : '""';
300
300
  case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BYTES:
301
301
  // todo(proto2): need to look into all the possible default values for the bytes type, and handle each one
302
302
  if (options.env === options_1.EnvOption.NODE) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.12.0",
3
+ "version": "2.12.1",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",