@bufbuild/protobuf 2.9.0 → 2.10.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/README.md CHANGED
@@ -6,7 +6,7 @@ code generator plugin.
6
6
  ## Protocol Buffers for ECMAScript
7
7
 
8
8
  A complete implementation of [Protocol Buffers](https://protobuf.dev/) in TypeScript,
9
- suitable for web browsers and Node.js, created by [Buf](https://buf.build).
9
+ suitable for web browsers, Node.js, and Deno, created by [Buf](https://buf.build).
10
10
 
11
11
  **Protobuf-ES** is a solid, modern alternative to existing Protobuf implementations for the JavaScript ecosystem. It's
12
12
  the first project in this space to provide a comprehensive plugin framework and decouple the base types from RPC
@@ -39,14 +39,18 @@ function reflect(messageDesc, message,
39
39
  check = true) {
40
40
  return new ReflectMessageImpl(messageDesc, message, check);
41
41
  }
42
+ const messageSortedFields = new WeakMap();
42
43
  class ReflectMessageImpl {
43
44
  get sortedFields() {
44
- var _a;
45
- return ((_a = this._sortedFields) !== null && _a !== void 0 ? _a :
46
- // biome-ignore lint/suspicious/noAssignInExpressions: no
47
- (this._sortedFields = this.desc.fields
45
+ const cached = messageSortedFields.get(this.desc);
46
+ if (cached) {
47
+ return cached;
48
+ }
49
+ const sortedFields = this.desc.fields
48
50
  .concat()
49
- .sort((a, b) => a.number - b.number)));
51
+ .sort((a, b) => a.number - b.number);
52
+ messageSortedFields.set(this.desc, sortedFields);
53
+ return sortedFields;
50
54
  }
51
55
  constructor(messageDesc, message, check = true) {
52
56
  this.lists = new Map();
@@ -229,7 +229,7 @@ const DELIMITED = 2;
229
229
  const OPEN = 1;
230
230
  // biome-ignore format: want this to read well
231
231
  // bootstrap-inject defaults: EDITION_PROTO2 to EDITION_2024: export const minimumEdition: SupportedEdition = $minimumEdition, maximumEdition: SupportedEdition = $maximumEdition;
232
- // generated from protoc v32.0
232
+ // generated from protoc v33.0
233
233
  exports.minimumEdition = 998, exports.maximumEdition = 1001;
234
234
  const featureDefaults = {
235
235
  // EDITION_PROTO2
@@ -297,13 +297,17 @@ function anyToJson(val, opts) {
297
297
  return json;
298
298
  }
299
299
  function durationToJson(val) {
300
- if (Number(val.seconds) > 315576000000 ||
301
- Number(val.seconds) < -315576000000) {
300
+ const seconds = Number(val.seconds);
301
+ const nanos = val.nanos;
302
+ if (seconds > 315576000000 || seconds < -315576000000) {
302
303
  throw new Error(`cannot encode message ${val.$typeName} to JSON: value out of range`);
303
304
  }
305
+ if ((seconds > 0 && nanos < 0) || (seconds < 0 && nanos > 0)) {
306
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos sign must match seconds sign`);
307
+ }
304
308
  let text = val.seconds.toString();
305
- if (val.nanos !== 0) {
306
- let nanosStr = Math.abs(val.nanos).toString();
309
+ if (nanos !== 0) {
310
+ let nanosStr = Math.abs(nanos).toString();
307
311
  nanosStr = "0".repeat(9 - nanosStr.length) + nanosStr;
308
312
  if (nanosStr.substring(3) === "000000") {
309
313
  nanosStr = nanosStr.substring(0, 3);
@@ -312,7 +316,7 @@ function durationToJson(val) {
312
316
  nanosStr = nanosStr.substring(0, 6);
313
317
  }
314
318
  text += "." + nanosStr;
315
- if (val.nanos < 0 && Number(val.seconds) == 0) {
319
+ if (nanos < 0 && seconds == 0) {
316
320
  text = "-" + text;
317
321
  }
318
322
  }
@@ -370,6 +374,9 @@ function timestampToJson(val) {
370
374
  if (val.nanos < 0) {
371
375
  throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be negative`);
372
376
  }
377
+ if (val.nanos > 999999999) {
378
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be greater than 99999999`);
379
+ }
373
380
  let z = "Z";
374
381
  if (val.nanos > 0) {
375
382
  const nanosStr = (val.nanos + 1000000000).toString().substring(1);
@@ -0,0 +1,9 @@
1
+ import type { Duration } from "./gen/google/protobuf/duration_pb.js";
2
+ /**
3
+ * Create a google.protobuf.Duration message from a Unix timestamp in milliseconds.
4
+ */
5
+ export declare function durationFromMs(durationMs: number): Duration;
6
+ /**
7
+ * Convert a google.protobuf.Duration to a Unix timestamp in milliseconds.
8
+ */
9
+ export declare function durationMs(duration: Duration): number;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ // Copyright 2021-2025 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.durationFromMs = durationFromMs;
17
+ exports.durationMs = durationMs;
18
+ const duration_pb_js_1 = require("./gen/google/protobuf/duration_pb.js");
19
+ const create_js_1 = require("../create.js");
20
+ const proto_int64_js_1 = require("../proto-int64.js");
21
+ /**
22
+ * Create a google.protobuf.Duration message from a Unix timestamp in milliseconds.
23
+ */
24
+ function durationFromMs(durationMs) {
25
+ const sign = durationMs < 0 ? -1 : 1;
26
+ const absDurationMs = Math.abs(durationMs);
27
+ const absSeconds = Math.floor(absDurationMs / 1000);
28
+ const absNanos = (absDurationMs - absSeconds * 1000) * 1000000;
29
+ return (0, create_js_1.create)(duration_pb_js_1.DurationSchema, {
30
+ seconds: proto_int64_js_1.protoInt64.parse(absSeconds * sign),
31
+ nanos: absNanos === 0 ? 0 : absNanos * sign, // deliberately avoid signed 0 - it does not serialize
32
+ });
33
+ }
34
+ /**
35
+ * Convert a google.protobuf.Duration to a Unix timestamp in milliseconds.
36
+ */
37
+ function durationMs(duration) {
38
+ return Number(duration.seconds) * 1000 + Math.round(duration.nanos / 1000000);
39
+ }
@@ -100,17 +100,18 @@ export declare const file_google_protobuf_timestamp: GenFile;
100
100
  */
101
101
  export type Timestamp = Message<"google.protobuf.Timestamp"> & {
102
102
  /**
103
- * Represents seconds of UTC time since Unix epoch
104
- * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
105
- * 9999-12-31T23:59:59Z inclusive.
103
+ * Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must
104
+ * be between -315576000000 and 315576000000 inclusive (which corresponds to
105
+ * 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z).
106
106
  *
107
107
  * @generated from field: int64 seconds = 1;
108
108
  */
109
109
  seconds: bigint;
110
110
  /**
111
- * Non-negative fractions of a second at nanosecond resolution. Negative
112
- * second values with fractions must still have non-negative nanos values
113
- * that count forward in time. Must be from 0 to 999,999,999
111
+ * Non-negative fractions of a second at nanosecond resolution. This field is
112
+ * the nanosecond portion of the duration, not an alternative to seconds.
113
+ * Negative second values with fractions must still have non-negative nanos
114
+ * values that count forward in time. Must be between 0 and 999,999,999
114
115
  * inclusive.
115
116
  *
116
117
  * @generated from field: int32 nanos = 2;
@@ -1,4 +1,5 @@
1
1
  export * from "./timestamp.js";
2
+ export * from "./duration.js";
2
3
  export * from "./any.js";
3
4
  export * from "./wrappers.js";
4
5
  export * from "./gen/google/protobuf/any_pb.js";
@@ -28,6 +28,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  __exportStar(require("./timestamp.js"), exports);
31
+ __exportStar(require("./duration.js"), exports);
31
32
  __exportStar(require("./any.js"), exports);
32
33
  __exportStar(require("./wrappers.js"), exports);
33
34
  __exportStar(require("./gen/google/protobuf/any_pb.js"), exports);
@@ -34,14 +34,18 @@ export function reflect(messageDesc, message,
34
34
  check = true) {
35
35
  return new ReflectMessageImpl(messageDesc, message, check);
36
36
  }
37
+ const messageSortedFields = new WeakMap();
37
38
  class ReflectMessageImpl {
38
39
  get sortedFields() {
39
- var _a;
40
- return ((_a = this._sortedFields) !== null && _a !== void 0 ? _a :
41
- // biome-ignore lint/suspicious/noAssignInExpressions: no
42
- (this._sortedFields = this.desc.fields
40
+ const cached = messageSortedFields.get(this.desc);
41
+ if (cached) {
42
+ return cached;
43
+ }
44
+ const sortedFields = this.desc.fields
43
45
  .concat()
44
- .sort((a, b) => a.number - b.number)));
46
+ .sort((a, b) => a.number - b.number);
47
+ messageSortedFields.set(this.desc, sortedFields);
48
+ return sortedFields;
45
49
  }
46
50
  constructor(messageDesc, message, check = true) {
47
51
  this.lists = new Map();
@@ -223,7 +223,7 @@ const DELIMITED = 2;
223
223
  const OPEN = 1;
224
224
  // biome-ignore format: want this to read well
225
225
  // bootstrap-inject defaults: EDITION_PROTO2 to EDITION_2024: export const minimumEdition: SupportedEdition = $minimumEdition, maximumEdition: SupportedEdition = $maximumEdition;
226
- // generated from protoc v32.0
226
+ // generated from protoc v33.0
227
227
  export const minimumEdition = 998, maximumEdition = 1001;
228
228
  const featureDefaults = {
229
229
  // EDITION_PROTO2
@@ -292,13 +292,17 @@ function anyToJson(val, opts) {
292
292
  return json;
293
293
  }
294
294
  function durationToJson(val) {
295
- if (Number(val.seconds) > 315576000000 ||
296
- Number(val.seconds) < -315576000000) {
295
+ const seconds = Number(val.seconds);
296
+ const nanos = val.nanos;
297
+ if (seconds > 315576000000 || seconds < -315576000000) {
297
298
  throw new Error(`cannot encode message ${val.$typeName} to JSON: value out of range`);
298
299
  }
300
+ if ((seconds > 0 && nanos < 0) || (seconds < 0 && nanos > 0)) {
301
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos sign must match seconds sign`);
302
+ }
299
303
  let text = val.seconds.toString();
300
- if (val.nanos !== 0) {
301
- let nanosStr = Math.abs(val.nanos).toString();
304
+ if (nanos !== 0) {
305
+ let nanosStr = Math.abs(nanos).toString();
302
306
  nanosStr = "0".repeat(9 - nanosStr.length) + nanosStr;
303
307
  if (nanosStr.substring(3) === "000000") {
304
308
  nanosStr = nanosStr.substring(0, 3);
@@ -307,7 +311,7 @@ function durationToJson(val) {
307
311
  nanosStr = nanosStr.substring(0, 6);
308
312
  }
309
313
  text += "." + nanosStr;
310
- if (val.nanos < 0 && Number(val.seconds) == 0) {
314
+ if (nanos < 0 && seconds == 0) {
311
315
  text = "-" + text;
312
316
  }
313
317
  }
@@ -365,6 +369,9 @@ function timestampToJson(val) {
365
369
  if (val.nanos < 0) {
366
370
  throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be negative`);
367
371
  }
372
+ if (val.nanos > 999999999) {
373
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be greater than 99999999`);
374
+ }
368
375
  let z = "Z";
369
376
  if (val.nanos > 0) {
370
377
  const nanosStr = (val.nanos + 1000000000).toString().substring(1);
@@ -0,0 +1,9 @@
1
+ import type { Duration } from "./gen/google/protobuf/duration_pb.js";
2
+ /**
3
+ * Create a google.protobuf.Duration message from a Unix timestamp in milliseconds.
4
+ */
5
+ export declare function durationFromMs(durationMs: number): Duration;
6
+ /**
7
+ * Convert a google.protobuf.Duration to a Unix timestamp in milliseconds.
8
+ */
9
+ export declare function durationMs(duration: Duration): number;
@@ -0,0 +1,35 @@
1
+ // Copyright 2021-2025 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { DurationSchema } from "./gen/google/protobuf/duration_pb.js";
15
+ import { create } from "../create.js";
16
+ import { protoInt64 } from "../proto-int64.js";
17
+ /**
18
+ * Create a google.protobuf.Duration message from a Unix timestamp in milliseconds.
19
+ */
20
+ export function durationFromMs(durationMs) {
21
+ const sign = durationMs < 0 ? -1 : 1;
22
+ const absDurationMs = Math.abs(durationMs);
23
+ const absSeconds = Math.floor(absDurationMs / 1000);
24
+ const absNanos = (absDurationMs - absSeconds * 1000) * 1000000;
25
+ return create(DurationSchema, {
26
+ seconds: protoInt64.parse(absSeconds * sign),
27
+ nanos: absNanos === 0 ? 0 : absNanos * sign, // deliberately avoid signed 0 - it does not serialize
28
+ });
29
+ }
30
+ /**
31
+ * Convert a google.protobuf.Duration to a Unix timestamp in milliseconds.
32
+ */
33
+ export function durationMs(duration) {
34
+ return Number(duration.seconds) * 1000 + Math.round(duration.nanos / 1000000);
35
+ }
@@ -100,17 +100,18 @@ export declare const file_google_protobuf_timestamp: GenFile;
100
100
  */
101
101
  export type Timestamp = Message<"google.protobuf.Timestamp"> & {
102
102
  /**
103
- * Represents seconds of UTC time since Unix epoch
104
- * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
105
- * 9999-12-31T23:59:59Z inclusive.
103
+ * Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must
104
+ * be between -315576000000 and 315576000000 inclusive (which corresponds to
105
+ * 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z).
106
106
  *
107
107
  * @generated from field: int64 seconds = 1;
108
108
  */
109
109
  seconds: bigint;
110
110
  /**
111
- * Non-negative fractions of a second at nanosecond resolution. Negative
112
- * second values with fractions must still have non-negative nanos values
113
- * that count forward in time. Must be from 0 to 999,999,999
111
+ * Non-negative fractions of a second at nanosecond resolution. This field is
112
+ * the nanosecond portion of the duration, not an alternative to seconds.
113
+ * Negative second values with fractions must still have non-negative nanos
114
+ * values that count forward in time. Must be between 0 and 999,999,999
114
115
  * inclusive.
115
116
  *
116
117
  * @generated from field: int32 nanos = 2;
@@ -1,4 +1,5 @@
1
1
  export * from "./timestamp.js";
2
+ export * from "./duration.js";
2
3
  export * from "./any.js";
3
4
  export * from "./wrappers.js";
4
5
  export * from "./gen/google/protobuf/any_pb.js";
@@ -12,6 +12,7 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  export * from "./timestamp.js";
15
+ export * from "./duration.js";
15
16
  export * from "./any.js";
16
17
  export * from "./wrappers.js";
17
18
  export * from "./gen/google/protobuf/any_pb.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protobuf",
3
- "version": "2.9.0",
3
+ "version": "2.10.1",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "A complete implementation of Protocol Buffers in TypeScript, suitable for web browsers and Node.js.",
6
6
  "keywords": ["protobuf", "schema", "typescript", "ecmascript"],