@bufbuild/protobuf 2.5.2 → 2.6.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.
@@ -13,5 +13,6 @@ export { fromBinary, mergeFromBinary } from "./from-binary.js";
13
13
  export type { BinaryReadOptions } from "./from-binary.js";
14
14
  export * from "./to-json.js";
15
15
  export * from "./from-json.js";
16
+ export * from "./merge.js";
16
17
  export { hasExtension, getExtension, setExtension, clearExtension, hasOption, getOption, } from "./extensions.js";
17
18
  export * from "./proto-int64.js";
package/dist/cjs/index.js CHANGED
@@ -43,6 +43,7 @@ Object.defineProperty(exports, "fromBinary", { enumerable: true, get: function (
43
43
  Object.defineProperty(exports, "mergeFromBinary", { enumerable: true, get: function () { return from_binary_js_1.mergeFromBinary; } });
44
44
  __exportStar(require("./to-json.js"), exports);
45
45
  __exportStar(require("./from-json.js"), exports);
46
+ __exportStar(require("./merge.js"), exports);
46
47
  var extensions_js_1 = require("./extensions.js");
47
48
  Object.defineProperty(exports, "hasExtension", { enumerable: true, get: function () { return extensions_js_1.hasExtension; } });
48
49
  Object.defineProperty(exports, "getExtension", { enumerable: true, get: function () { return extensions_js_1.getExtension; } });
@@ -0,0 +1,13 @@
1
+ import type { MessageShape } from "./types.js";
2
+ import type { DescMessage } from "./descriptors.js";
3
+ /**
4
+ * Merge message `source` into message `target`, following Protobuf semantics.
5
+ *
6
+ * This is the same as serializing the source message, then deserializing it
7
+ * into the target message via `mergeFromBinary()`, with one difference:
8
+ * While serialization will create a copy of all values, `merge()` will copy
9
+ * the reference for `bytes` and messages.
10
+ *
11
+ * Also see https://protobuf.com/docs/language-spec#merging-protobuf-messages
12
+ */
13
+ export declare function merge<Desc extends DescMessage>(schema: Desc, target: MessageShape<Desc>, source: MessageShape<Desc>): void;
@@ -0,0 +1,70 @@
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.merge = merge;
17
+ const reflect_js_1 = require("./reflect/reflect.js");
18
+ /**
19
+ * Merge message `source` into message `target`, following Protobuf semantics.
20
+ *
21
+ * This is the same as serializing the source message, then deserializing it
22
+ * into the target message via `mergeFromBinary()`, with one difference:
23
+ * While serialization will create a copy of all values, `merge()` will copy
24
+ * the reference for `bytes` and messages.
25
+ *
26
+ * Also see https://protobuf.com/docs/language-spec#merging-protobuf-messages
27
+ */
28
+ function merge(schema, target, source) {
29
+ reflectMerge((0, reflect_js_1.reflect)(schema, target), (0, reflect_js_1.reflect)(schema, source));
30
+ }
31
+ function reflectMerge(target, source) {
32
+ var _a;
33
+ var _b;
34
+ const sourceUnknown = source.message.$unknown;
35
+ if (sourceUnknown !== undefined && sourceUnknown.length > 0) {
36
+ (_a = (_b = target.message).$unknown) !== null && _a !== void 0 ? _a : (_b.$unknown = []);
37
+ target.message.$unknown.push(...sourceUnknown);
38
+ }
39
+ for (const f of target.fields) {
40
+ if (!source.isSet(f)) {
41
+ continue;
42
+ }
43
+ switch (f.fieldKind) {
44
+ case "scalar":
45
+ case "enum":
46
+ target.set(f, source.get(f));
47
+ break;
48
+ case "message":
49
+ if (target.isSet(f)) {
50
+ reflectMerge(target.get(f), source.get(f));
51
+ }
52
+ else {
53
+ target.set(f, source.get(f));
54
+ }
55
+ break;
56
+ case "list":
57
+ const list = target.get(f);
58
+ for (const e of source.get(f)) {
59
+ list.add(e);
60
+ }
61
+ break;
62
+ case "map":
63
+ const map = target.get(f);
64
+ for (const [k, v] of source.get(f)) {
65
+ map.set(k, v);
66
+ }
67
+ break;
68
+ }
69
+ }
70
+ }
@@ -13,5 +13,6 @@ export { fromBinary, mergeFromBinary } from "./from-binary.js";
13
13
  export type { BinaryReadOptions } from "./from-binary.js";
14
14
  export * from "./to-json.js";
15
15
  export * from "./from-json.js";
16
+ export * from "./merge.js";
16
17
  export { hasExtension, getExtension, setExtension, clearExtension, hasOption, getOption, } from "./extensions.js";
17
18
  export * from "./proto-int64.js";
package/dist/esm/index.js CHANGED
@@ -23,5 +23,6 @@ export { toBinary } from "./to-binary.js";
23
23
  export { fromBinary, mergeFromBinary } from "./from-binary.js";
24
24
  export * from "./to-json.js";
25
25
  export * from "./from-json.js";
26
+ export * from "./merge.js";
26
27
  export { hasExtension, getExtension, setExtension, clearExtension, hasOption, getOption, } from "./extensions.js";
27
28
  export * from "./proto-int64.js";
@@ -0,0 +1,13 @@
1
+ import type { MessageShape } from "./types.js";
2
+ import type { DescMessage } from "./descriptors.js";
3
+ /**
4
+ * Merge message `source` into message `target`, following Protobuf semantics.
5
+ *
6
+ * This is the same as serializing the source message, then deserializing it
7
+ * into the target message via `mergeFromBinary()`, with one difference:
8
+ * While serialization will create a copy of all values, `merge()` will copy
9
+ * the reference for `bytes` and messages.
10
+ *
11
+ * Also see https://protobuf.com/docs/language-spec#merging-protobuf-messages
12
+ */
13
+ export declare function merge<Desc extends DescMessage>(schema: Desc, target: MessageShape<Desc>, source: MessageShape<Desc>): void;
@@ -0,0 +1,67 @@
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 { reflect } from "./reflect/reflect.js";
15
+ /**
16
+ * Merge message `source` into message `target`, following Protobuf semantics.
17
+ *
18
+ * This is the same as serializing the source message, then deserializing it
19
+ * into the target message via `mergeFromBinary()`, with one difference:
20
+ * While serialization will create a copy of all values, `merge()` will copy
21
+ * the reference for `bytes` and messages.
22
+ *
23
+ * Also see https://protobuf.com/docs/language-spec#merging-protobuf-messages
24
+ */
25
+ export function merge(schema, target, source) {
26
+ reflectMerge(reflect(schema, target), reflect(schema, source));
27
+ }
28
+ function reflectMerge(target, source) {
29
+ var _a;
30
+ var _b;
31
+ const sourceUnknown = source.message.$unknown;
32
+ if (sourceUnknown !== undefined && sourceUnknown.length > 0) {
33
+ (_a = (_b = target.message).$unknown) !== null && _a !== void 0 ? _a : (_b.$unknown = []);
34
+ target.message.$unknown.push(...sourceUnknown);
35
+ }
36
+ for (const f of target.fields) {
37
+ if (!source.isSet(f)) {
38
+ continue;
39
+ }
40
+ switch (f.fieldKind) {
41
+ case "scalar":
42
+ case "enum":
43
+ target.set(f, source.get(f));
44
+ break;
45
+ case "message":
46
+ if (target.isSet(f)) {
47
+ reflectMerge(target.get(f), source.get(f));
48
+ }
49
+ else {
50
+ target.set(f, source.get(f));
51
+ }
52
+ break;
53
+ case "list":
54
+ const list = target.get(f);
55
+ for (const e of source.get(f)) {
56
+ list.add(e);
57
+ }
58
+ break;
59
+ case "map":
60
+ const map = target.get(f);
61
+ for (const [k, v] of source.get(f)) {
62
+ map.set(k, v);
63
+ }
64
+ break;
65
+ }
66
+ }
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protobuf",
3
- "version": "2.5.2",
3
+ "version": "2.6.0",
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"],