Files
codeql-action/node_modules/@protobuf-ts/runtime-rpc/build/commonjs/rpc-options.js
Angela P Wen a196a714b8 Bump artifact dependencies if CODEQL_ACTION_ARTIFACT_V2_UPGRADE enabled (#2482)
Co-authored-by: Andrew Eisenberg <aeisenberg@github.com>
Co-authored-by: Henry Mercer <henrymercer@github.com>
2024-10-01 09:59:05 -07:00

67 lines
2.3 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeRpcOptions = void 0;
const runtime_1 = require("@protobuf-ts/runtime");
/**
* Merges custom RPC options with defaults. Returns a new instance and keeps
* the "defaults" and the "options" unmodified.
*
* Merges `RpcMetadata` "meta", overwriting values from "defaults" with
* values from "options". Does not append values to existing entries.
*
* Merges "jsonOptions", including "jsonOptions.typeRegistry", by creating
* a new array that contains types from "options.jsonOptions.typeRegistry"
* first, then types from "defaults.jsonOptions.typeRegistry".
*
* Merges "binaryOptions".
*
* Merges "interceptors" by creating a new array that contains interceptors
* from "defaults" first, then interceptors from "options".
*
* Works with objects that extend `RpcOptions`, but only if the added
* properties are of type Date, primitive like string, boolean, or Array
* of primitives. If you have other property types, you have to merge them
* yourself.
*/
function mergeRpcOptions(defaults, options) {
if (!options)
return defaults;
let o = {};
copy(defaults, o);
copy(options, o);
for (let key of Object.keys(options)) {
let val = options[key];
switch (key) {
case "jsonOptions":
o.jsonOptions = runtime_1.mergeJsonOptions(defaults.jsonOptions, o.jsonOptions);
break;
case "binaryOptions":
o.binaryOptions = runtime_1.mergeBinaryOptions(defaults.binaryOptions, o.binaryOptions);
break;
case "meta":
o.meta = {};
copy(defaults.meta, o.meta);
copy(options.meta, o.meta);
break;
case "interceptors":
o.interceptors = defaults.interceptors ? defaults.interceptors.concat(val) : val.concat();
break;
}
}
return o;
}
exports.mergeRpcOptions = mergeRpcOptions;
function copy(a, into) {
if (!a)
return;
let c = into;
for (let [k, v] of Object.entries(a)) {
if (v instanceof Date)
c[k] = new Date(v.getTime());
else if (Array.isArray(v))
c[k] = v.concat();
else
c[k] = v;
}
}