-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathutils.ts
More file actions
144 lines (131 loc) · 3.91 KB
/
utils.ts
File metadata and controls
144 lines (131 loc) · 3.91 KB
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
export function ensureEndpointIdFormat(id: string): string {
const parts = id.split("/");
if (parts.length > 1) {
return id;
}
const [, appOwner, appId] = /^([0-9]+)-([a-zA-Z0-9-]+)$/.exec(id) || [];
if (appOwner && appId) {
return `${appOwner}/${appId}`;
}
throw new Error(
`Invalid app id: ${id}. Must be in the format <appOwner>/<appId>`,
);
}
const ENDPOINT_NAMESPACES = ["workflows", "comfy"] as const;
type EndpointNamespace = (typeof ENDPOINT_NAMESPACES)[number];
export type EndpointId = {
readonly owner: string;
readonly alias: string;
readonly path?: string;
readonly namespace?: EndpointNamespace;
};
export function parseEndpointId(id: string): EndpointId {
const normalizedId = ensureEndpointIdFormat(id);
const parts = normalizedId.split("/");
if (ENDPOINT_NAMESPACES.includes(parts[0] as any)) {
return {
owner: parts[1],
alias: parts[2],
path: parts.slice(3).join("/") || undefined,
namespace: parts[0] as EndpointNamespace,
};
}
return {
owner: parts[0],
alias: parts[1],
path: parts.slice(2).join("/") || undefined,
};
}
/**
* Resolves the endpoint path, normalizing it and applying a default.
* If no explicit path is provided and the app already ends with the
* default path, returns undefined to avoid duplication.
*
* @param app - The app/endpoint identifier
* @param path - An explicitly provided path (always used if present)
* @param defaultPath - The default path to use (e.g. "/realtime")
* @returns The resolved path, or undefined if the app already includes it
*/
export function resolveEndpointPath(
app: string,
path: string | undefined,
defaultPath: string,
): string | undefined {
if (path) {
return `/${path.replace(/^\/+/, "")}`;
}
if (app.endsWith(defaultPath)) {
return undefined;
}
return defaultPath;
}
export function isValidUrl(url: string) {
try {
const { host } = new URL(url);
return /(fal\.(ai|run))$/.test(host);
} catch (_) {
return false;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function throttle<T extends (...args: any[]) => any>(
func: T,
limit: number,
leading = false,
): (...funcArgs: Parameters<T>) => ReturnType<T> | void {
let lastFunc: NodeJS.Timeout | null;
let lastRan: number;
return (...args: Parameters<T>): ReturnType<T> | void => {
if (!lastRan && leading) {
func(...args);
lastRan = Date.now();
} else {
if (lastFunc) {
clearTimeout(lastFunc);
}
lastFunc = setTimeout(
() => {
if (Date.now() - lastRan >= limit) {
func(...args);
lastRan = Date.now();
}
},
limit - (Date.now() - lastRan),
);
}
};
}
let isRunningInReact: boolean | undefined;
/**
* Not really the most optimal way to detect if we're running in React,
* but the idea here is that we can support multiple rendering engines
* (starting with React), with all their peculiarities, without having
* to add a dependency or creating custom integrations (e.g. custom hooks).
*
* Yes, a bit of magic to make things works out-of-the-box.
* @returns `true` if running in React, `false` otherwise.
*/
export function isReact() {
if (isRunningInReact === undefined) {
const stack = new Error().stack;
isRunningInReact =
!!stack &&
(stack.includes("node_modules/react-dom/") ||
stack.includes("node_modules/next/"));
}
return isRunningInReact;
}
/**
* Check if a value is a plain object.
* @param value - The value to check.
* @returns `true` if the value is a plain object, `false` otherwise.
*/
export function isPlainObject(value: any): boolean {
return !!value && Object.getPrototypeOf(value) === Object.prototype;
}
/**
* Utility function to sleep for a given number of milliseconds
*/
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}