@mochabug/adapt-web 1.0.0-rc37 → 1.0.0-rc38

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.
@@ -107,6 +107,7 @@ var MbAdapt = (() => {
107
107
  let e2 = this.getAttribute("data-cap-api-endpoint");
108
108
  if (!e2 && window?.CAP_CUSTOM_FETCH) e2 = "/";
109
109
  else if (!e2) throw new Error("Missing API endpoint. Either custom fetch or an API endpoint must be provided.");
110
+ e2.endsWith("/") || (e2 += "/");
110
111
  const { challenge: s2, token: i2 } = await (await t(`${e2}challenge`, { method: "POST" })).json();
111
112
  let a = s2;
112
113
  if (!Array.isArray(a)) {
@@ -2188,6 +2189,9 @@ var MbAdapt = (() => {
2188
2189
  }
2189
2190
  return b.join("");
2190
2191
  }
2192
+ function protoSnakeCase(lowerCamelCase) {
2193
+ return lowerCamelCase.replace(/[A-Z]/g, (letter) => "_" + letter.toLowerCase());
2194
+ }
2191
2195
  var reservedObjectProperties = /* @__PURE__ */ new Set([
2192
2196
  // names reserved by JavaScript
2193
2197
  "constructor",
@@ -4153,8 +4157,8 @@ var MbAdapt = (() => {
4153
4157
  }
4154
4158
  function fieldMaskToJson(val) {
4155
4159
  return val.paths.map((p) => {
4156
- if (p.match(/_[0-9]?_/g) || p.match(/[A-Z]/g)) {
4157
- throw new Error(`cannot encode message ${val.$typeName} to JSON: lowerCamelCase of path name "` + p + '" is irreversible');
4160
+ if (protoSnakeCase(protoCamelCase(p)) !== p) {
4161
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: lowerCamelCase of path name "${p}" is irreversible`);
4158
4162
  }
4159
4163
  return protoCamelCase(p);
4160
4164
  }).join(",");
@@ -4239,6 +4243,18 @@ var MbAdapt = (() => {
4239
4243
  }
4240
4244
  return msg.message;
4241
4245
  }
4246
+ var messageJsonFields = /* @__PURE__ */ new WeakMap();
4247
+ function getJsonField(desc, jsonKey) {
4248
+ var _a;
4249
+ if (!messageJsonFields.has(desc)) {
4250
+ const jsonNames = /* @__PURE__ */ new Map();
4251
+ for (const field of desc.fields) {
4252
+ jsonNames.set(field.name, field).set(field.jsonName, field);
4253
+ }
4254
+ messageJsonFields.set(desc, jsonNames);
4255
+ }
4256
+ return (_a = messageJsonFields.get(desc)) === null || _a === void 0 ? void 0 : _a.get(jsonKey);
4257
+ }
4242
4258
  function readMessage2(msg, json, opts) {
4243
4259
  var _a;
4244
4260
  if (tryWktFromJson(msg, json, opts)) {
@@ -4248,12 +4264,8 @@ var MbAdapt = (() => {
4248
4264
  throw new Error(`cannot decode ${msg.desc} from JSON: ${formatVal(json)}`);
4249
4265
  }
4250
4266
  const oneofSeen = /* @__PURE__ */ new Map();
4251
- const jsonNames = /* @__PURE__ */ new Map();
4252
- for (const field of msg.desc.fields) {
4253
- jsonNames.set(field.name, field).set(field.jsonName, field);
4254
- }
4255
4267
  for (const [jsonKey, jsonValue] of Object.entries(json)) {
4256
- const field = jsonNames.get(jsonKey);
4268
+ const field = getJsonField(msg.desc, jsonKey);
4257
4269
  if (field) {
4258
4270
  if (field.oneof) {
4259
4271
  if (jsonValue === null && field.fieldKind == "scalar") {
@@ -4299,6 +4311,20 @@ var MbAdapt = (() => {
4299
4311
  break;
4300
4312
  }
4301
4313
  }
4314
+ function readListOrMapItem(field, json, opts) {
4315
+ if (field.scalar && json !== null) {
4316
+ return scalarFromJson(field, json);
4317
+ }
4318
+ if (field.message && !isResetSentinelNullValue(field, json)) {
4319
+ const msgValue = reflect(field.message);
4320
+ readMessage2(msgValue, json, opts);
4321
+ return msgValue;
4322
+ }
4323
+ if (field.enum && !isResetSentinelNullValue(field, json)) {
4324
+ return readEnum(field.enum, json, opts.ignoreUnknownFields);
4325
+ }
4326
+ throw new FieldError(field, `${field.fieldKind === "list" ? "list item" : "map value"} must not be null`);
4327
+ }
4302
4328
  function readMapField(map, json, opts) {
4303
4329
  if (json === null) {
4304
4330
  return;
@@ -4308,28 +4334,11 @@ var MbAdapt = (() => {
4308
4334
  throw new FieldError(field, "expected object, got " + formatVal(json));
4309
4335
  }
4310
4336
  for (const [jsonMapKey, jsonMapValue] of Object.entries(json)) {
4311
- if (jsonMapValue === null && !isSafeNullValueInListOrMap(field)) {
4312
- throw new FieldError(field, "map value must not be null");
4313
- }
4314
- let value;
4315
- switch (field.mapKind) {
4316
- case "message":
4317
- const msgValue = reflect(field.message);
4318
- readMessage2(msgValue, jsonMapValue, opts);
4319
- value = msgValue;
4320
- break;
4321
- case "enum":
4322
- value = readEnum(field.enum, jsonMapValue, opts.ignoreUnknownFields, true);
4323
- if (value === tokenIgnoredUnknownEnum) {
4324
- return;
4325
- }
4326
- break;
4327
- case "scalar":
4328
- value = scalarFromJson(field, jsonMapValue, true);
4329
- break;
4330
- }
4331
4337
  const key = mapKeyFromJson(field.mapKey, jsonMapKey);
4332
- map.set(key, value);
4338
+ const value = readListOrMapItem(field, jsonMapValue, opts);
4339
+ if (value !== tokenIgnoredUnknownEnum) {
4340
+ map.set(key, value);
4341
+ }
4333
4342
  }
4334
4343
  }
4335
4344
  function readListField2(list, json, opts) {
@@ -4341,33 +4350,14 @@ var MbAdapt = (() => {
4341
4350
  throw new FieldError(field, "expected Array, got " + formatVal(json));
4342
4351
  }
4343
4352
  for (const jsonItem of json) {
4344
- if (jsonItem === null && !isSafeNullValueInListOrMap(field)) {
4345
- throw new FieldError(field, "list item must not be null");
4346
- }
4347
- switch (field.listKind) {
4348
- case "message":
4349
- const msgValue = reflect(field.message);
4350
- readMessage2(msgValue, jsonItem, opts);
4351
- list.add(msgValue);
4352
- break;
4353
- case "enum":
4354
- const enumValue = readEnum(field.enum, jsonItem, opts.ignoreUnknownFields, true);
4355
- if (enumValue !== tokenIgnoredUnknownEnum) {
4356
- list.add(enumValue);
4357
- }
4358
- break;
4359
- case "scalar":
4360
- list.add(scalarFromJson(field, jsonItem, true));
4361
- break;
4353
+ const value = readListOrMapItem(field, jsonItem, opts);
4354
+ if (value !== tokenIgnoredUnknownEnum) {
4355
+ list.add(value);
4362
4356
  }
4363
4357
  }
4364
4358
  }
4365
- function isSafeNullValueInListOrMap(field) {
4366
- var _a, _b;
4367
- return ((_a = field.message) === null || _a === void 0 ? void 0 : _a.typeName) == "google.protobuf.Value" || ((_b = field.enum) === null || _b === void 0 ? void 0 : _b.typeName) == "google.protobuf.NullValue";
4368
- }
4369
4359
  function readMessageField2(msg, field, json, opts) {
4370
- if (json === null && field.message.typeName != "google.protobuf.Value") {
4360
+ if (isResetSentinelNullValue(field, json)) {
4371
4361
  msg.clear(field);
4372
4362
  return;
4373
4363
  }
@@ -4376,28 +4366,30 @@ var MbAdapt = (() => {
4376
4366
  msg.set(field, msgValue);
4377
4367
  }
4378
4368
  function readEnumField(msg, field, json, opts) {
4379
- const enumValue = readEnum(field.enum, json, opts.ignoreUnknownFields, false);
4380
- if (enumValue === tokenNull) {
4369
+ if (isResetSentinelNullValue(field, json)) {
4381
4370
  msg.clear(field);
4382
- } else if (enumValue !== tokenIgnoredUnknownEnum) {
4371
+ return;
4372
+ }
4373
+ const enumValue = readEnum(field.enum, json, opts.ignoreUnknownFields);
4374
+ if (enumValue !== tokenIgnoredUnknownEnum) {
4383
4375
  msg.set(field, enumValue);
4384
4376
  }
4385
4377
  }
4386
4378
  function readScalarField(msg, field, json) {
4387
- const scalarValue = scalarFromJson(field, json, false);
4388
- if (scalarValue === tokenNull) {
4379
+ if (json === null) {
4389
4380
  msg.clear(field);
4390
4381
  } else {
4391
- msg.set(field, scalarValue);
4382
+ msg.set(field, scalarFromJson(field, json));
4392
4383
  }
4393
4384
  }
4385
+ function isResetSentinelNullValue(field, json) {
4386
+ var _a, _b;
4387
+ return json === null && ((_a = field.message) === null || _a === void 0 ? void 0 : _a.typeName) != "google.protobuf.Value" && ((_b = field.enum) === null || _b === void 0 ? void 0 : _b.typeName) != "google.protobuf.NullValue";
4388
+ }
4394
4389
  var tokenIgnoredUnknownEnum = /* @__PURE__ */ Symbol();
4395
- function readEnum(desc, json, ignoreUnknownFields, nullAsZeroValue) {
4390
+ function readEnum(desc, json, ignoreUnknownFields) {
4396
4391
  if (json === null) {
4397
- if (desc.typeName == "google.protobuf.NullValue") {
4398
- return 0;
4399
- }
4400
- return nullAsZeroValue ? desc.values[0].number : tokenNull;
4392
+ return desc.values[0].number;
4401
4393
  }
4402
4394
  switch (typeof json) {
4403
4395
  case "number":
@@ -4417,14 +4409,7 @@ var MbAdapt = (() => {
4417
4409
  }
4418
4410
  throw new Error(`cannot decode ${desc} from JSON: ${formatVal(json)}`);
4419
4411
  }
4420
- var tokenNull = /* @__PURE__ */ Symbol();
4421
- function scalarFromJson(field, json, nullAsZeroValue) {
4422
- if (json === null) {
4423
- if (nullAsZeroValue) {
4424
- return scalarZeroValue(field.scalar, false);
4425
- }
4426
- return tokenNull;
4427
- }
4412
+ function scalarFromJson(field, json) {
4428
4413
  switch (field.scalar) {
4429
4414
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
4430
4415
  // Either numbers or strings are accepted. Exponent notation is also accepted.
@@ -4484,24 +4469,24 @@ var MbAdapt = (() => {
4484
4469
  }
4485
4470
  return json;
4486
4471
  }
4487
- function mapKeyFromJson(type, json) {
4472
+ function mapKeyFromJson(type, jsonString) {
4488
4473
  switch (type) {
4489
4474
  case ScalarType.BOOL:
4490
- switch (json) {
4475
+ switch (jsonString) {
4491
4476
  case "true":
4492
4477
  return true;
4493
4478
  case "false":
4494
4479
  return false;
4495
4480
  }
4496
- return json;
4481
+ return jsonString;
4497
4482
  case ScalarType.INT32:
4498
4483
  case ScalarType.FIXED32:
4499
4484
  case ScalarType.UINT32:
4500
4485
  case ScalarType.SFIXED32:
4501
4486
  case ScalarType.SINT32:
4502
- return int32FromJson(json);
4487
+ return int32FromJson(jsonString);
4503
4488
  default:
4504
- return json;
4489
+ return jsonString;
4505
4490
  }
4506
4491
  }
4507
4492
  function int32FromJson(json) {
@@ -4564,7 +4549,7 @@ var MbAdapt = (() => {
4564
4549
  if (jsonValue === null) {
4565
4550
  msg.clear(valueField);
4566
4551
  } else {
4567
- msg.set(valueField, scalarFromJson(valueField, jsonValue, true));
4552
+ msg.set(valueField, scalarFromJson(valueField, jsonValue));
4568
4553
  }
4569
4554
  return true;
4570
4555
  }
@@ -4655,14 +4640,12 @@ var MbAdapt = (() => {
4655
4640
  if (json === "") {
4656
4641
  return;
4657
4642
  }
4658
- function camelToSnake(str) {
4659
- if (str.includes("_")) {
4643
+ fieldMask.paths = json.split(",").map((path) => {
4644
+ if (path.includes("_")) {
4660
4645
  throw new Error(`cannot decode message ${fieldMask.$typeName} from JSON: path names must be lowerCamelCase`);
4661
4646
  }
4662
- const sc = str.replace(/[A-Z]/g, (letter) => "_" + letter.toLowerCase());
4663
- return sc[0] === "_" ? sc.substring(1) : sc;
4664
- }
4665
- fieldMask.paths = json.split(",").map(camelToSnake);
4647
+ return protoSnakeCase(path);
4648
+ });
4666
4649
  }
4667
4650
  function structFromJson(struct, json) {
4668
4651
  if (typeof json != "object" || json == null || Array.isArray(json)) {
@@ -6899,7 +6882,7 @@ var MbAdapt = (() => {
6899
6882
  // src/index.ts
6900
6883
  if (typeof window !== "undefined" && true && !window.CAP_CUSTOM_WASM_URL) {
6901
6884
  window.CAP_CUSTOM_WASM_URL = new URL(
6902
- "https://cdn.mochabug.com/adapt/web/1.0.0-rc37/cap_wasm.js",
6885
+ "https://cdn.mochabug.com/adapt/web/1.0.0-rc38/cap_wasm.js",
6903
6886
  window.location.href
6904
6887
  ).href;
6905
6888
  }