-
Notifications
You must be signed in to change notification settings - Fork 665
Expand file tree
/
Copy pathutil.ts
More file actions
339 lines (312 loc) · 8.41 KB
/
util.ts
File metadata and controls
339 lines (312 loc) · 8.41 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
// Copyright 2023 Google LLC
//
// 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 * as fs from 'fs';
import * as os from 'os';
import path = require('path');
const WELL_KNOWN_CERTIFICATE_CONFIG_FILE = 'certificate_config.json';
const CLOUDSDK_CONFIG_DIRECTORY = 'gcloud';
/**
* A utility for converting snake_case to camelCase.
*
* For, for example `my_snake_string` becomes `mySnakeString`.
*/
export type SnakeToCamel<S> = S extends `${infer FirstWord}_${infer Remainder}`
? `${FirstWord}${Capitalize<SnakeToCamel<Remainder>>}`
: S;
/**
* A utility for converting an type's keys from snake_case
* to camelCase, if the keys are strings.
*
* For example:
*
* ```ts
* {
* my_snake_string: boolean;
* myCamelString: string;
* my_snake_obj: {
* my_snake_obj_string: string;
* };
* }
* ```
*
* becomes:
*
* ```ts
* {
* mySnakeString: boolean;
* myCamelString: string;
* mySnakeObj: {
* mySnakeObjString: string;
* }
* }
* ```
*
* @remarks
*
* The generated documentation for the camelCase'd properties won't be available
* until {@link https://github.com/microsoft/TypeScript/issues/50715} has been
* resolved.
*/
export type SnakeToCamelObject<T> = {
[K in keyof T as SnakeToCamel<K>]: T[K] extends {}
? SnakeToCamelObject<T[K]>
: T[K];
};
/**
* A utility for adding camelCase versions of a type's snake_case keys, if the
* keys are strings, preserving any existing keys.
*
* For example:
*
* ```ts
* {
* my_snake_boolean: boolean;
* myCamelString: string;
* my_snake_obj: {
* my_snake_obj_string: string;
* };
* }
* ```
*
* becomes:
*
* ```ts
* {
* my_snake_boolean: boolean;
* mySnakeBoolean: boolean;
* myCamelString: string;
* my_snake_obj: {
* my_snake_obj_string: string;
* };
* mySnakeObj: {
* mySnakeObjString: string;
* }
* }
* ```
* @remarks
*
* The generated documentation for the camelCase'd properties won't be available
* until {@link https://github.com/microsoft/TypeScript/issues/50715} has been
* resolved.
*
* Tracking: {@link https://github.com/googleapis/google-auth-library-nodejs/issues/1686}
*/
export type OriginalAndCamel<T> = {
[K in keyof T as K | SnakeToCamel<K>]: T[K] extends {}
? OriginalAndCamel<T[K]>
: T[K];
};
/**
* Returns the camel case of a provided string.
*
* @remarks
*
* Match any `_` and not `_` pair, then return the uppercase of the not `_`
* character.
*
* @param str the string to convert
* @returns the camelCase'd string
*/
export function snakeToCamel<T extends string>(str: T): SnakeToCamel<T> {
return str.replace(/([_][^_])/g, match =>
match.slice(1).toUpperCase(),
) as SnakeToCamel<T>;
}
/**
* Get the value of `obj[key]` or `obj[camelCaseKey]`, with a preference
* for original, non-camelCase key.
*
* @param obj object to lookup a value in
* @returns a `get` function for getting `obj[key || snakeKey]`, if available
*/
export function originalOrCamelOptions<T extends {}>(obj?: T) {
/**
*
* @param key an index of object, preferably snake_case
* @returns the value `obj[key || snakeKey]`, if available
*/
function get<K extends keyof OriginalAndCamel<T> & string>(key: K) {
const o = (obj || {}) as OriginalAndCamel<T>;
return o[key] ?? o[snakeToCamel(key) as K];
}
return {get};
}
export interface LRUCacheOptions {
/**
* The maximum number of items to cache.
*/
capacity: number;
/**
* An optional max age for items in milliseconds.
*/
maxAge?: number;
}
/**
* A simple LRU cache utility.
* Not meant for external usage.
*
* @experimental
*/
export class LRUCache<T> {
readonly capacity: number;
/**
* Maps are in order. Thus, the older item is the first item.
*
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map}
*/
#cache = new Map<string, {lastAccessed: number; value: T}>();
maxAge?: number;
constructor(options: LRUCacheOptions) {
this.capacity = options.capacity;
this.maxAge = options.maxAge;
}
/**
* Moves the key to the end of the cache.
*
* @param key the key to move
* @param value the value of the key
*/
#moveToEnd(key: string, value: T) {
this.#cache.delete(key);
this.#cache.set(key, {
value,
lastAccessed: Date.now(),
});
}
/**
* Add an item to the cache.
*
* @param key the key to upsert
* @param value the value of the key
*/
set(key: string, value: T) {
this.#moveToEnd(key, value);
this.#evict();
}
/**
* Get an item from the cache.
*
* @param key the key to retrieve
*/
get(key: string): T | undefined {
const item = this.#cache.get(key);
if (!item) return;
this.#moveToEnd(key, item.value);
this.#evict();
return item.value;
}
/**
* Maintain the cache based on capacity and TTL.
*/
#evict() {
const cutoffDate = this.maxAge ? Date.now() - this.maxAge : 0;
/**
* Because we know Maps are in order, this item is both the
* last item in the list (capacity) and oldest (maxAge).
*/
let oldestItem = this.#cache.entries().next();
while (
!oldestItem.done &&
(this.#cache.size > this.capacity || // too many
oldestItem.value[1].lastAccessed < cutoffDate) // too old
) {
this.#cache.delete(oldestItem.value[0]);
oldestItem = this.#cache.entries().next();
}
}
}
// Given and object remove fields where value is undefined.
export function removeUndefinedValuesInObject(object: {
[key: string]: unknown;
}): {
[key: string]: unknown;
} {
Object.entries(object).forEach(([key, value]) => {
if (value === undefined || value === 'undefined') {
delete object[key];
}
});
return object;
}
/**
* Helper to check if a path points to a valid file.
*/
export async function isValidFile(filePath: string): Promise<boolean> {
try {
const stats = await fs.promises.lstat(filePath);
return stats.isFile();
} catch (e) {
return false;
}
}
/**
* Determines the well-known gcloud location for the certificate config file.
* @returns The platform-specific path to the configuration file.
* @internal
*/
export function getWellKnownCertificateConfigFileLocation(): string {
const configDir =
process.env.CLOUDSDK_CONFIG ||
(_isWindows()
? path.join(process.env.APPDATA || '', CLOUDSDK_CONFIG_DIRECTORY)
: path.join(
process.env.HOME || '',
'.config',
CLOUDSDK_CONFIG_DIRECTORY,
));
return path.join(configDir, WELL_KNOWN_CERTIFICATE_CONFIG_FILE);
}
/**
* Checks if the current operating system is Windows.
* @returns True if the OS is Windows, false otherwise.
* @internal
*/
function _isWindows(): boolean {
return os.platform().startsWith('win');
}
/**
* Returns the workforce identity pool ID if it is determinable
* from the audience resource name.
* @param audience The audience used to determine the pool ID.
* @return The pool ID associated with the workforce identity pool, if
* this can be determined from the audience field. Otherwise, null is
* returned.
*/
export function getWorkforcePoolIdFromAudience(
audience: string,
): string | null {
// STS audience pattern:
// .../workforcePools/$WORKFORCE_POOL_ID/providers/...
return (
audience.match(/\/workforcePools\/(?<poolId>[^/]+)\/providers\//)?.groups
?.poolId ?? null
);
}
/**
* Returns the workload identity pool ID if it is determinable
* from the audience resource name.
* @param audience The audience used to determine the pool ID.
* @return The pool ID associated with the workload identity pool, if
* this can be determined from the audience field. Otherwise, null is
* returned.
*/
export function getWorkloadPoolIdFromAudience(audience: string): string | null {
// STS audience pattern:
// .../workloadIdentityPools/POOL_ID/providers/...
return (
audience.match(
/\/workloadIdentityPools\/(?<workloadPool>[^/]+)\/providers\//,
)?.groups?.workloadPool ?? null
);
}