Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/wkt/any.js

raw
1 "use strict";
2 // Copyright 2021-2026 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.anyPack = anyPack;
17 exports.anyIs = anyIs;
18 exports.anyUnpack = anyUnpack;
19 exports.anyUnpackTo = anyUnpackTo;
20 const any_pb_js_1 = require("./gen/google/protobuf/any_pb.js");
21 const create_js_1 = require("../create.js");
22 const to_binary_js_1 = require("../to-binary.js");
23 const from_binary_js_1 = require("../from-binary.js");
24 function anyPack(schema, message, into) {
25 let ret = false;
26 if (!into) {
27 into = (0, create_js_1.create)(any_pb_js_1.AnySchema);
28 ret = true;
29 }
30 into.value = (0, to_binary_js_1.toBinary)(schema, message);
31 into.typeUrl = typeNameToUrl(message.$typeName);
32 return ret ? into : undefined;
33 }
34 function anyIs(any, descOrTypeName) {
35 if (any.typeUrl === "") {
36 return false;
37 }
38 const want = typeof descOrTypeName == "string"
39 ? descOrTypeName
40 : descOrTypeName.typeName;
41 const got = typeUrlToName(any.typeUrl);
42 return want === got;
43 }
44 function anyUnpack(any, registryOrMessageDesc) {
45 if (any.typeUrl === "") {
46 return undefined;
47 }
48 const desc = registryOrMessageDesc.kind == "message"
49 ? registryOrMessageDesc
50 : registryOrMessageDesc.getMessage(typeUrlToName(any.typeUrl));
51 if (!desc || !anyIs(any, desc)) {
52 return undefined;
53 }
54 return (0, from_binary_js_1.fromBinary)(desc, any.value);
55 }
56 /**
57 * Same as anyUnpack but unpacks into the target message.
58 */
59 function anyUnpackTo(any, schema, message) {
60 if (!anyIs(any, schema)) {
61 return undefined;
62 }
63 return (0, from_binary_js_1.mergeFromBinary)(schema, message, any.value);
64 }
65 function typeNameToUrl(name) {
66 return `type.googleapis.com/${name}`;
67 }
68 function typeUrlToName(url) {
69 const slash = url.lastIndexOf("/");
70 const name = slash >= 0 ? url.substring(slash + 1) : url;
71 if (!name.length) {
72 throw new Error(`invalid type url: ${url}`);
73 }
74 return name;
75 }