forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
357 lines (301 loc) · 9.78 KB
/
Copy pathutils.ts
File metadata and controls
357 lines (301 loc) · 9.78 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import * as fs from 'fs';
import { extname } from 'path';
import * as minimatch from 'minimatch';
import { parseYaml } from './js-yaml';
import { env } from './env';
import { logger, colorize } from './logger';
import { HttpsProxyAgent } from 'https-proxy-agent';
import * as pluralizeOne from 'pluralize';
import type { HttpResolveConfig } from './config';
import type { UserContext } from './walk';
export { parseYaml, stringifyYaml } from './js-yaml';
export type StackFrame<T> = {
prev: StackFrame<T> | null;
value: T;
};
export type Stack<T> = StackFrame<T> | null;
export type StackNonEmpty<T> = StackFrame<T>;
export function pushStack<T, P extends Stack<T> = Stack<T>>(head: P, value: T) {
return { prev: head, value };
}
export function pluralize(sentence: string, count?: number, inclusive?: boolean) {
return sentence
.split(' ')
.map((word) => pluralizeOne(word, count, inclusive))
.join(' ');
}
export function popStack<T, P extends Stack<T>>(head: P) {
return head?.prev ?? null;
}
export type BundleOutputFormat = 'json' | 'yml' | 'yaml';
export async function loadYaml<T>(filename: string): Promise<T> {
const contents = await fs.promises.readFile(filename, 'utf-8');
return parseYaml(contents) as T;
}
export function isDefined<T>(x: T | undefined): x is T {
return x !== undefined;
}
export function isPlainObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
export function isEmptyObject(value: unknown): value is Record<string, unknown> {
return isPlainObject(value) && Object.keys(value).length === 0;
}
export function isNotEmptyObject(obj: unknown): boolean {
return isPlainObject(obj) && !isEmptyObject(obj);
}
export function isEmptyArray(value: unknown) {
return Array.isArray(value) && value.length === 0;
}
export function isNotEmptyArray<T>(args?: T[]): boolean {
return !!args && Array.isArray(args) && !!args.length;
}
export async function readFileFromUrl(url: string, config: HttpResolveConfig) {
const headers: Record<string, string> = {};
for (const header of config.headers) {
if (match(url, header.matches)) {
headers[header.name] =
header.envVariable !== undefined ? env[header.envVariable] || '' : header.value;
}
}
const req = await (config.customFetch || fetch)(url, {
headers: headers,
});
if (!req.ok) {
throw new Error(`Failed to load ${url}: ${req.status} ${req.statusText}`);
}
return { body: await req.text(), mimeType: req.headers.get('content-type') };
}
function match(url: string, pattern: string) {
if (!pattern.match(/^https?:\/\//)) {
// if pattern doesn't specify protocol directly, do not match against it
url = url.replace(/^https?:\/\//, '');
}
return minimatch(url, pattern);
}
export function pickObjectProps<T extends Record<string, unknown>>(
object: T,
keys: Array<string>
): T {
return Object.fromEntries(
keys.filter((key: string) => key in object).map((key: string) => [key, object[key]])
) as T;
}
export function omitObjectProps<T extends Record<string, unknown>>(
object: T,
keys: Array<string>
): T {
return Object.fromEntries(Object.entries(object).filter(([key]) => !keys.includes(key))) as T;
}
export function splitCamelCaseIntoWords(str: string) {
const camel = str
.split(/(?:[-._])|([A-Z][a-z]+)/)
.filter(isTruthy)
.map((item) => item.toLocaleLowerCase());
const caps = str
.split(/([A-Z]{2,})/)
.filter((e: string) => e && e === e.toUpperCase())
.map((item) => item.toLocaleLowerCase());
return new Set([...camel, ...caps]);
}
export function validateMimeType(
{ type, value }: any,
{ report, location }: UserContext,
allowedValues: string[]
) {
const ruleType = type === 'consumes' ? 'request' : 'response';
if (!allowedValues)
throw new Error(`Parameter "allowedValues" is not provided for "${ruleType}-mime-type" rule`);
if (!value[type]) return;
for (const mime of value[type]) {
if (!allowedValues.includes(mime)) {
report({
message: `Mime type "${mime}" is not allowed`,
location: location.child(value[type].indexOf(mime)).key(),
});
}
}
}
export function validateMimeTypeOAS3(
{ type, value }: any,
{ report, location }: UserContext,
allowedValues: string[]
) {
const ruleType = type === 'consumes' ? 'request' : 'response';
if (!allowedValues)
throw new Error(`Parameter "allowedValues" is not provided for "${ruleType}-mime-type" rule`);
if (!value.content) return;
for (const mime of Object.keys(value.content)) {
if (!allowedValues.includes(mime)) {
report({
message: `Mime type "${mime}" is not allowed`,
location: location.child('content').child(mime).key(),
});
}
}
}
export function readFileAsStringSync(filePath: string) {
return fs.readFileSync(filePath, 'utf-8');
}
export function yamlAndJsonSyncReader<T>(filePath: string): T {
const content = fs.readFileSync(filePath, 'utf-8');
return parseYaml(content) as T;
}
export function isPathParameter(pathSegment: string) {
return pathSegment.startsWith('{') && pathSegment.endsWith('}');
}
/**
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
*/
export function slash(path: string): string {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);
if (isExtendedLengthPath) {
return path;
}
return path.replace(/\\/g, '/');
}
// TODO: use it everywhere
export function isString(value: unknown): value is string {
return typeof value === 'string';
}
export function isNotString<T>(value: string | T): value is T {
return !isString(value);
}
export const assignConfig = <T extends string | { severity?: string }>(
target: Record<string, T>,
obj?: Record<string, T>
) => {
if (!obj) return;
for (const k of Object.keys(obj)) {
if (isPlainObject(target[k]) && typeof obj[k] === 'string') {
target[k].severity = obj[k];
} else {
target[k] = obj[k];
}
}
};
export function assignOnlyExistingConfig<T extends string | { severity?: string }>(
target: Record<string, T>,
obj?: Record<string, T>
) {
if (!obj) return;
for (const k of Object.keys(obj)) {
if (!target.hasOwnProperty(k)) continue;
if (isPlainObject(target[k]) && typeof obj[k] === 'string') {
target[k].severity = obj[k];
} else {
target[k] = obj[k];
}
}
}
export function getMatchingStatusCodeRange(code: number | string): string {
return `${code}`.replace(/^(\d)\d\d$/, (_, firstDigit) => `${firstDigit}XX`);
}
export function isCustomRuleId(id: string) {
return id.includes('/');
}
export function doesYamlFileExist(filePath: string): boolean {
return (
(extname(filePath) === '.yaml' || extname(filePath) === '.yml') &&
fs?.hasOwnProperty?.('existsSync') &&
fs.existsSync(filePath)
);
}
export function showWarningForDeprecatedField(
deprecatedField: string,
updatedField?: string,
updatedObject?: string,
link?: string
) {
const readMoreText = link ? `Read more about this change: ${link}` : '';
logger.warn(
`The '${colorize.red(deprecatedField)}' field is deprecated. ${
updatedField
? `Use ${colorize.green(getUpdatedFieldName(updatedField, updatedObject))} instead. `
: ''
}${readMoreText}\n`
);
}
export function showErrorForDeprecatedField(
deprecatedField: string,
updatedField?: string,
updatedObject?: string
) {
throw new Error(
`Do not use '${deprecatedField}' field. ${
updatedField ? `Use '${getUpdatedFieldName(updatedField, updatedObject)}' instead. ` : ''
}\n`
);
}
export type Falsy = undefined | null | false | '' | 0;
export function isTruthy<Truthy>(value: Truthy | Falsy): value is Truthy {
return !!value;
}
export function identity<T>(value: T): T {
return value;
}
export function keysOf<T>(obj: T) {
if (!obj) return [];
return Object.keys(obj) as (keyof T)[];
}
export function pickDefined<T extends Record<string, unknown>>(
obj?: T
): Record<string, unknown> | undefined {
if (!obj) return undefined;
const res: Record<string, unknown> = {};
for (const key in obj) {
if (obj[key] !== undefined) {
res[key] = obj[key];
}
}
return res;
}
export function nextTick() {
return new Promise((resolve) => {
setTimeout(resolve);
});
}
export async function pause(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function getUpdatedFieldName(updatedField: string, updatedObject?: string) {
return `${typeof updatedObject !== 'undefined' ? `${updatedObject}.` : ''}${updatedField}`;
}
export function getProxyAgent() {
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
return proxy ? new HttpsProxyAgent(proxy) : undefined;
}
/**
* Checks if two objects are deeply equal.
* Borrowed the source code from https://github.com/lukeed/dequal.
*/
export function dequal(foo: any, bar: any): boolean {
let ctor, len;
if (foo === bar) return true;
if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
if (ctor === Date) return foo.getTime() === bar.getTime();
if (ctor === RegExp) return foo.toString() === bar.toString();
if (ctor === Array) {
if ((len = foo.length) === bar.length) {
while (len-- && dequal(foo[len], bar[len]));
}
return len === -1;
}
if (!ctor || typeof foo === 'object') {
len = 0;
for (ctor in foo) {
if (
Object.prototype.hasOwnProperty.call(foo, ctor) &&
++len &&
!Object.prototype.hasOwnProperty.call(bar, ctor)
)
return false;
if (!(ctor in bar) || !dequal(foo[ctor], bar[ctor])) return false;
}
return Object.keys(bar).length === len;
}
}
return foo !== foo && bar !== bar;
}
export type CollectFn = (value: unknown) => void;
export type StrictObject<T extends object> = T & { [key: string]: undefined };