Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { ExecException, ExecFileException } from "child_process";
import type { IncomingMessage } from "http";
/**
* Narrows `val` to include the property `key` (if true is returned)
* @param val The object to be tested
* @param key The key to test if it is present on the object (must be a literal for tsc to do any meaningful typing)
*/
export function hasOwnProperty<S extends object, K extends PropertyKey>(val: S, key: K): val is (S & { [key in K]: unknown }) {
// this call syntax is for when `val` was created by `Object.create(null)`
return Object.prototype.hasOwnProperty.call(val, key);
}
/**
* Narrows `val` to a static type that includes fields of names in `keys`
* @param val the value that we are trying to type narrow
* @param keys the key names (must be literals for tsc to do any meaningful typing)
*/
export function hasOwnProperties<S extends object, K extends PropertyKey>(val: S, ...keys: K[]): val is (S & { [key in K]: unknown }) {
return keys.every(key => hasOwnProperty(val, key));
}
/**
* Narrows `val` to include the property `key` with type `V`
* @param val the value that we are trying to type narrow
* @param key The key to test if it is present on the object (must be a literal for tsc to do any meaningful typing)
* @param isValid a function to check if the field is valid
*/
export function hasTypedProperty<S extends object, K extends PropertyKey, V>(val: S, key: K, isValid: (value: unknown) => value is V): val is (S & { [key in K]: V }) {
return hasOwnProperty(val, key) && isValid(val[key]);
}
/**
* Narrows `val` to include the property `key` with type string
* @param val the value that we are trying to type narrow
* @param key The key to test if it is present on the object (must be a literal for tsc to do any meaningful typing)
*/
export function hasStringProperty<S extends object, K extends PropertyKey>(val: S, key: K): val is (S & { [key in K]: string }) {
return hasOwnProperty(val, key) && isString(val[key]);
}
/**
* Narrows `val` to include the property `key` with type `V | undefined` or doesn't contain it
* @param val the value that we are trying to type narrow
* @param key The key to test if it is present on the object (must be a literal for tsc to do any meaningful typing)
* @param isValid a function to check if the field (when present) is valid
*/
export function hasOptionalTypedProperty<S extends object, K extends PropertyKey, V>(val: S, key: K, isValid: (value: unknown) => value is V): val is (S & { [key in K]?: V }) {
if (hasOwnProperty(val, key)) {
return typeof val[key] === "undefined" || isValid(val[key]);
}
return true;
}
/**
* isRecord checks if `val` matches the signature `Record<T, V>` or `{ [label in T]: V }`
* @param val The value to be checked
* @param isKey a function for checking if the key is of the correct type
* @param isValue a function for checking if a value is of the correct type
*/
export function isRecord<T extends PropertyKey, V>(val: unknown, isKey: (key: unknown) => key is T, isValue: (value: unknown) => value is V): val is Record<T, V> {
return isObject(val) && Object.entries(val).every(([key, value]) => isKey(key) && isValue(value));
}
/**
* isTypedArray checks if `val` is an array and all of its entries are of type `T`
* @param val The value to be checked
* @param isEntry a function for checking if an entry is the correct type
*/
export function isTypedArray<T>(val: unknown, isEntry: (entry: unknown) => entry is T): val is T[] {
return Array.isArray(val) && val.every(isEntry);
}
/**
* checks if val is of type string
* @param val the value to be checked
*/
export function isString(val: unknown): val is string {
return typeof val === "string";
}
/**
* checks if val is of type Buffer
* @param val the value to be checked
*/export function isBuffer(val: unknown): val is Buffer {
return val instanceof Buffer;
}
/**
* checks if val is of type number
* @param val the value to be checked
*/
export function isNumber(val: unknown): val is number {
return typeof val === "number";
}
/**
* checks if val is of type boolean
* @param val the value to be checked
*/
export function isBoolean(val: unknown): val is boolean {
return typeof val === "boolean";
}
/**
* checks if val is of type object and isn't null
* @param val the value to be checked
*/
export function isObject(val: unknown): val is Record<string | symbol | number, unknown> {
return typeof val === "object" && val !== null;
}
/**
* checks if `val` is defined, useful for filtering out undefined values in a strict manner
*/
export function isDefined<T>(val: T | undefined | null): val is T {
return val != null;
}
export function isFunction(val: unknown): val is (...args: unknown[]) => unknown {
return typeof val === "function";
}
/**
* Checks if the value in the second position is non-nullable
*/
export function hasDefinedTupleValue<K, V>(pair: readonly [K, V | undefined | null]): pair is [K, V] {
return pair[1] != null;
}
/**
* Creates a new predicate function (with the same predicate) from `fn`. Such
* that it can be called with just the value to be tested.
*
* This is useful for when using `hasOptionalProperty` and `hasTypedProperty`
* @param fn A typescript user predicate function to be bound
* @param boundArgs the set of arguments to be passed to `fn` in the new function
*/
export function bindPredicate<FnArgs extends any[], T>(fn: (arg1: unknown, ...args: FnArgs) => arg1 is T, ...boundArgs: FnArgs): (arg1: unknown) => arg1 is T {
return (arg1: unknown): arg1 is T => fn(arg1, ...boundArgs);
}
export function hasDefiniteField<Field extends keyof T, T>(field: Field): (val: T) => val is T & { [f in Field]-?: NonNullable<T[Field]> } {
return (val): val is T & { [f in Field]-?: NonNullable<T[Field]> } => val[field] != null;
}
export function isPromiseLike(res: unknown): res is (Promise<unknown> | { then: (fn: (val: unknown) => any) => Promise<unknown> }) {
if (res instanceof Promise) {
return true;
}
return isObject(res)
&& hasTypedProperty(res, "then", isFunction);
}
export function isPromiseSettledRejected<T>(result: PromiseSettledResult<T>): result is PromiseRejectedResult {
return result.status === "rejected";
}
export function isPromiseSettledFulfilled<T>(result: PromiseSettledResult<T>): result is PromiseFulfilledResult<T> {
return result.status === "fulfilled";
}
export function isErrnoException(error: unknown): error is NodeJS.ErrnoException {
return isObject(error)
&& hasOptionalTypedProperty(error, "code", isString)
&& hasOptionalTypedProperty(error, "path", isString)
&& hasOptionalTypedProperty(error, "syscall", isString)
&& hasOptionalTypedProperty(error, "errno", isNumber)
&& error instanceof Error;
}
export function isExecException(error: unknown): error is ExecException {
return isObject(error)
&& hasOptionalTypedProperty(error, "cmd", isString)
&& hasOptionalTypedProperty(error, "killed", isBoolean)
&& hasOptionalTypedProperty(error, "signal", isString)
&& hasOptionalTypedProperty(error, "code", isNumber)
&& error instanceof Error;
}
export function isExecFileException(error: unknown): error is ExecFileException {
return isExecException(error) && isErrnoException(error);
}
export type OutputFormat = "string" | "buffer";
export type ComputeOutputFormat<Format> = Format extends "string"
? string
: Format extends "buffer"
? Buffer
: string | Buffer;
export interface ChildProcessExecpetion<Format> extends ExecFileException {
stderr: ComputeOutputFormat<Format>;
stdout: ComputeOutputFormat<Format>;
}
const isStringOrBuffer = (val: unknown): val is string | Buffer => isString(val) || isBuffer(val);
export function isChildProcessError(error: unknown, format?: OutputFormat): error is ChildProcessExecpetion<typeof format> {
if (!isExecFileException(error)) {
return false;
}
if (format === "string") {
return hasTypedProperty(error, "stderr", isString)
&& hasTypedProperty(error, "stdout", isString);
} else if (format === "buffer") {
return hasTypedProperty(error, "stderr", isBuffer)
&& hasTypedProperty(error, "stdout", isBuffer);
} else {
return hasTypedProperty(error, "stderr", isStringOrBuffer)
&& hasTypedProperty(error, "stdout", isStringOrBuffer);
}
}
export interface RequestLikeError extends Error {
statusCode?: number;
failed?: boolean;
timedOut?: boolean;
error?: string;
response?: IncomingMessage & { body?: any };
}
/**
* A type guard for checking if the error is similar in shape to a request package error
*/
export function isRequestError(error: unknown): error is RequestLikeError {
return isObject(error)
&& hasOptionalTypedProperty(error, "statusCode", isNumber)
&& hasOptionalTypedProperty(error, "failed", isBoolean)
&& hasOptionalTypedProperty(error, "timedOut", isBoolean)
&& hasOptionalTypedProperty(error, "error", isString)
&& hasOptionalTypedProperty(error, "response", isObject)
&& error instanceof Error;
}
|