-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.ts
41 lines (33 loc) · 1.41 KB
/
utils.ts
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
import type {
ExpressListenerLike,
LegacyServerLike,
ListenerLike,
NativeServerLike,
ServerLike,
} from "./types.ts";
export const isString = (thing: unknown): thing is string =>
typeof thing === "string";
export const isListener = (thing: unknown): thing is ListenerLike =>
thing instanceof Object && thing !== null && "listen" in thing;
export const isExpressListener = (
thing: unknown,
): thing is ExpressListenerLike =>
thing instanceof Object && thing !== null && "locals" in thing &&
"mountpath" in thing && "all" in thing && "engine" in thing &&
"listen" in thing && "param" in thing && "path" in thing &&
"render" in thing && "route" in thing && "set" in thing && "use" in thing;
const isCommonServer = (thing: unknown): thing is ServerLike =>
thing instanceof Object && thing !== null && "close" in thing;
export const isStdLegacyServer = (thing: unknown): thing is LegacyServerLike =>
isCommonServer(thing) &&
"listener" in thing;
export const isStdNativeServer = (thing: unknown): thing is NativeServerLike =>
isCommonServer(thing) &&
"addrs" in thing;
export const isExpressServer = (thing: unknown): thing is NativeServerLike =>
isCommonServer(thing) &&
"listening" in thing &&
"address" in thing && typeof thing.address === "function";
export const isServer = (thing: unknown): thing is ServerLike =>
isStdLegacyServer(thing) || isStdNativeServer(thing) ||
isExpressServer(thing);