49 lines
2.1 KiB
JavaScript
Vendored
49 lines
2.1 KiB
JavaScript
Vendored
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
import { Message } from "./message.js";
|
|
/**
|
|
* Check whether the given object is any subtype of Message or is a specific
|
|
* Message by passing the type.
|
|
*
|
|
* Just like `instanceof`, `isMessage` narrows the type. The advantage of
|
|
* `isMessage` is that it compares identity by the message type name, not by
|
|
* class identity. This makes it robust against the dual package hazard and
|
|
* similar situations, where the same message is duplicated.
|
|
*
|
|
* This function is _mostly_ equivalent to the `instanceof` operator. For
|
|
* example, `isMessage(foo, MyMessage)` is the same as `foo instanceof MyMessage`,
|
|
* and `isMessage(foo)` is the same as `foo instanceof Message`. In most cases,
|
|
* `isMessage` should be preferred over `instanceof`.
|
|
*
|
|
* However, due to the fact that `isMessage` does not use class identity, there
|
|
* are subtle differences between this function and `instanceof`. Notably,
|
|
* calling `isMessage` on an explicit type of Message will return false.
|
|
*/
|
|
export function isMessage(arg, type) {
|
|
if (arg === null || typeof arg != "object") {
|
|
return false;
|
|
}
|
|
if (!Object.getOwnPropertyNames(Message.prototype).every((m) => m in arg && typeof arg[m] == "function")) {
|
|
return false;
|
|
}
|
|
const actualType = arg.getType();
|
|
if (actualType === null ||
|
|
typeof actualType != "function" ||
|
|
!("typeName" in actualType) ||
|
|
typeof actualType.typeName != "string") {
|
|
return false;
|
|
}
|
|
return type === undefined ? true : actualType.typeName == type.typeName;
|
|
}
|