-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathutils.ts
More file actions
842 lines (700 loc) · 25.3 KB
/
utils.ts
File metadata and controls
842 lines (700 loc) · 25.3 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
import { execSync } from 'node:child_process';
import { createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { mkdir, readFile } from 'node:fs/promises';
import type { IncomingMessage } from 'node:http';
import { get } from 'node:https';
import { homedir } from 'node:os';
import { dirname, join, relative } from 'node:path';
import process from 'node:process';
import { finished } from 'node:stream/promises';
import { DurationFormatter as SapphireDurationFormatter, TimeTypes } from '@sapphire/duration';
import { Timestamp } from '@sapphire/timestamp';
import AdmZip from 'adm-zip';
import _Ajv2019 from 'ajv/dist/2019.js';
import { type ActorRun, ApifyClient, type ApifyClientOptions, type Build } from 'apify-client';
import archiver from 'archiver';
import { AxiosHeaders } from 'axios';
import escapeStringRegexp from 'escape-string-regexp';
import ignoreModule, { type Ignore } from 'ignore';
import { getEncoding } from 'istextorbinary';
import { Mime } from 'mime';
import otherMimes from 'mime/types/other.js';
import standardMimes from 'mime/types/standard.js';
import { gte, minVersion, satisfies } from 'semver';
import { glob } from 'tinyglobby';
import {
ACTOR_ENV_VARS,
ACTOR_JOB_TERMINAL_STATUSES,
ACTOR_NAME,
APIFY_ENV_VARS,
KEY_VALUE_STORE_KEYS,
LOCAL_ACTOR_ENV_VARS,
LOCAL_STORAGE_SUBDIRS,
SOURCE_FILE_FORMATS,
} from '@apify/consts';
import {
APIFY_CLIENT_DEFAULT_HEADERS,
AUTH_FILE_PATH,
CommandExitCodes,
DEFAULT_LOCAL_STORAGE_DIR,
GLOBAL_CONFIGS_FOLDER,
LOCAL_CONFIG_PATH,
MINIMUM_SUPPORTED_PYTHON_VERSION,
SUPPORTED_NODEJS_VERSION,
} from './consts.js';
import { deleteFile, ensureFolderExistsSync, rimrafPromised } from './files.js';
import { inputFileRegExp, TEMP_INPUT_KEY_PREFIX } from './input-key.js';
import type { AuthJSON } from './types.js';
import { cliDebugPrint } from './utils/cliDebugPrint.js';
// `ignore` is a CJS package; TypeScript sees its default import as the module
// object rather than the callable factory, so we cast through unknown.
const makeIg = ignoreModule as unknown as () => Ignore;
// Export AJV properly: https://github.com/ajv-validator/ajv/issues/2132
// Welcome to the state of JavaScript/TypeScript and CJS/ESM interop.
export const Ajv2019 = _Ajv2019 as unknown as typeof import('ajv/dist/2019.js').default;
export const httpsGet = async (url: string) => {
return new Promise<IncomingMessage>((resolve, reject) => {
get(url, (response) => {
// Handle redirects
if (response.statusCode === 301 || response.statusCode === 302) {
resolve(httpsGet(response.headers.location!));
// Destroy the response to close the HTTP connection, otherwise this hangs for a long time with Node 19+ (due to HTTP keep-alive).
response.destroy();
} else {
resolve(response);
}
}).on('error', reject);
});
};
export const getLocalStorageDir = () => {
const envVar = APIFY_ENV_VARS.LOCAL_STORAGE_DIR;
return process.env[envVar] || process.env.CRAWLEE_STORAGE_DIR || DEFAULT_LOCAL_STORAGE_DIR;
};
export const getLocalKeyValueStorePath = (storeId?: string) => {
const envVar = ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID;
const storeDir = storeId || process.env[envVar] || LOCAL_ACTOR_ENV_VARS[envVar];
return join(getLocalStorageDir(), LOCAL_STORAGE_SUBDIRS.keyValueStores, storeDir);
};
export const getLocalDatasetPath = (storeId?: string) => {
const envVar = ACTOR_ENV_VARS.DEFAULT_DATASET_ID;
const storeDir = storeId || process.env[envVar] || LOCAL_ACTOR_ENV_VARS[envVar];
return join(getLocalStorageDir(), LOCAL_STORAGE_SUBDIRS.datasets, storeDir);
};
export const getLocalRequestQueuePath = (storeId?: string) => {
const envVar = ACTOR_ENV_VARS.DEFAULT_REQUEST_QUEUE_ID;
const storeDir = storeId || process.env[envVar] || LOCAL_ACTOR_ENV_VARS[envVar];
return join(getLocalStorageDir(), LOCAL_STORAGE_SUBDIRS.requestQueues, storeDir);
};
/**
* Returns object from auth file or empty object.
*/
export const getLocalUserInfo = async (): Promise<AuthJSON> => {
let result: AuthJSON = {};
try {
const raw = await readFile(AUTH_FILE_PATH(), 'utf-8');
result = JSON.parse(raw) as AuthJSON;
} catch {
return {};
}
if (!result.username && !result.id) {
throw new Error('Corrupted local user info was found. Please run "apify login" to fix it.');
}
return result;
};
/**
* Gets instance of ApifyClient for user otherwise throws error
*/
export async function getLoggedClientOrThrow() {
const loggedClient = await getLoggedClient();
if (!loggedClient) {
process.exitCode = CommandExitCodes.MissingAuth;
throw new Error('You are not logged in with your Apify account. Call "apify login" to fix that.');
}
return loggedClient;
}
const getTokenWithAuthFileFallback = (existingToken?: string) => {
if (!existingToken && existsSync(GLOBAL_CONFIGS_FOLDER()) && existsSync(AUTH_FILE_PATH())) {
const raw = readFileSync(AUTH_FILE_PATH(), 'utf-8');
return JSON.parse(raw).token;
}
return existingToken;
};
type CJSAxiosHeaders = import('axios', { with: { 'resolution-mode': 'require' }}).AxiosRequestConfig['headers'];
/**
* Returns options for ApifyClient
*/
export const getApifyClientOptions = (token?: string, apiBaseUrl?: string): ApifyClientOptions => {
token = getTokenWithAuthFileFallback(token);
return {
token,
baseUrl: apiBaseUrl || process.env.APIFY_CLIENT_BASE_URL,
requestInterceptors: [
(config) => {
config.headers ??= new AxiosHeaders() as CJSAxiosHeaders;
for (const [key, value] of Object.entries(APIFY_CLIENT_DEFAULT_HEADERS)) {
config.headers![key] = value;
}
return config;
},
],
};
};
/**
* Gets instance of ApifyClient for token or for params from global auth file.
* NOTE: It refreshes global auth file each run
* @param [token]
*/
export async function getLoggedClient(token?: string, apiBaseUrl?: string) {
token = getTokenWithAuthFileFallback(token);
const apifyClient = new ApifyClient(getApifyClientOptions(token, apiBaseUrl));
let userInfo;
try {
userInfo = await apifyClient.user('me').get();
} catch (err) {
cliDebugPrint('[getLoggedClient] error getting user info', { error: err, apiBaseUrl });
return null;
}
// Always refresh Auth file
ensureApifyDirectory(AUTH_FILE_PATH());
writeFileSync(AUTH_FILE_PATH(), JSON.stringify({ token: apifyClient.token, ...userInfo }, null, '\t'));
return apifyClient;
}
export const getLocalConfigPath = (cwd: string) => join(cwd, LOCAL_CONFIG_PATH);
export const getJsonFileContent = <T = Record<string, unknown>>(filePath: string) => {
if (!existsSync(filePath)) {
return;
}
return JSON.parse(readFileSync(filePath, { encoding: 'utf-8' })) as T;
};
export const getLocalConfig = (cwd: string) => getJsonFileContent(getLocalConfigPath(cwd));
export const setLocalConfig = async (localConfig: Record<string, unknown>, actDir?: string) => {
const fullPath = join(actDir || process.cwd(), LOCAL_CONFIG_PATH);
await mkdir(dirname(fullPath), { recursive: true });
writeFileSync(fullPath, JSON.stringify(localConfig, null, '\t'));
};
const GITIGNORE_REQUIRED_CONTENTS = [getLocalStorageDir(), 'node_modules', '.venv'];
export const setLocalEnv = async (actDir: string) => {
// Create folders for emulation Apify stores
const keyValueStorePath = getLocalKeyValueStorePath();
ensureFolderExistsSync(actDir, getLocalDatasetPath());
ensureFolderExistsSync(actDir, getLocalRequestQueuePath());
ensureFolderExistsSync(actDir, keyValueStorePath);
// Create or update gitignore
const gitignorePath = join(actDir, '.gitignore');
let gitignoreContents = '';
if (existsSync(gitignorePath)) {
gitignoreContents = readFileSync(gitignorePath, { encoding: 'utf-8' });
}
const gitignoreAdditions = [];
for (const gitignoreRequirement of GITIGNORE_REQUIRED_CONTENTS) {
if (!RegExp(`^${escapeStringRegexp(gitignoreRequirement)}$`, 'mg').test(gitignoreContents)) {
gitignoreAdditions.push(gitignoreRequirement);
}
}
if (gitignoreAdditions.length > 0) {
if (gitignoreContents.length > 0) {
gitignoreAdditions.unshift('# Added by Apify CLI');
writeFileSync(gitignorePath, `\n${gitignoreAdditions.join('\n')}\n`, {
flag: 'a',
});
} else {
writeFileSync(gitignorePath, `${gitignoreAdditions.join('\n')}\n`, {
flag: 'w',
});
}
}
};
const mime = new Mime(standardMimes, otherMimes).define(
{
// .tgz files don't have a MIME type defined, this fixes it
'application/gzip': ['tgz'],
// Default mime-type for .ts(x) files is video/mp2t. But in our usecases they're almost always TypeScript, which we want to treat as text
'text/typescript': ['ts', 'tsx', 'mts'],
},
true,
);
// Detect whether file is binary from its MIME type, or if not available, contents
const getSourceFileFormat = (filePath: string, fileContent: Buffer) => {
// Try to detect the MIME type from the file path
const contentType = mime.getType(filePath);
if (contentType) {
const format =
contentType.startsWith('text/') ||
contentType.includes('javascript') ||
contentType.includes('json') ||
contentType.includes('xml') ||
contentType.includes('application/node') || // .cjs files
contentType.includes('application/toml') || // for example pyproject.toml files
contentType.includes('application/x-sh') || // .sh files
contentType.includes('application/x-httpd-php') // .php files
? SOURCE_FILE_FORMATS.TEXT
: SOURCE_FILE_FORMATS.BASE64;
return format;
}
// If the MIME type detection failed, try to detect the file encoding from the file content
const encoding = getEncoding(fileContent);
return encoding === 'binary' ? SOURCE_FILE_FORMATS.BASE64 : SOURCE_FILE_FORMATS.TEXT;
};
export const createSourceFiles = async (paths: string[], cwd: string) => {
return paths.map((filePath) => {
const file = readFileSync(join(cwd, filePath));
const format = getSourceFileFormat(filePath, file);
return {
name: filePath,
format,
content: format === SOURCE_FILE_FORMATS.TEXT ? file.toString('utf8') : file.toString('base64'),
};
});
};
/**
* Fallback for when git is unavailable: find all .gitignore files and build a filter
* using the `ignore` package, scoped to each file's directory.
* Also walks ancestor directories to pick up parent .gitignore files (e.g. monorepo root),
* stopping at the first .git boundary found.
*/
const getGitignoreFallbackFilter = async (cwd: string): Promise<(paths: string[]) => string[]> => {
const gitignoreFiles = await glob('**/.gitignore', {
dot: true,
cwd,
ignore: ['.git/**'],
expandDirectories: false,
});
const filters: { dir: string; ig: Ignore; ancestorPrefix?: string }[] = [];
for (const gitignoreFile of gitignoreFiles) {
const gitignoreDir = dirname(gitignoreFile); // e.g. 'src' or '.'
const content = await readFile(join(cwd, gitignoreFile), 'utf-8');
filters.push({ dir: gitignoreDir === '.' ? '' : gitignoreDir, ig: makeIg().add(content) });
}
// Walk ancestor directories to pick up parent .gitignore files (e.g. monorepo root).
// Check for a .git boundary FIRST so we stop before processing the git root's own
// .gitignore — that file is handled by `git ls-files` when git is available, and
// avoids accidentally applying rules from an unrelated outer repository.
let parentDir = dirname(cwd);
while (parentDir !== dirname(parentDir)) {
if (existsSync(join(parentDir, '.git'))) {
break;
}
const parentGitignorePath = join(parentDir, '.gitignore');
if (existsSync(parentGitignorePath)) {
try {
const content = await readFile(parentGitignorePath, 'utf-8');
// Paths passed to this filter are relative to cwd. To test them against
// a .gitignore that lives above cwd we need to prepend the relative path
// from the ancestor dir to cwd so the ignore patterns see the right scope.
const ancestorPrefix = relative(parentDir, cwd);
filters.push({ dir: '', ig: makeIg().add(content), ancestorPrefix });
} catch {
// Ignore read errors
}
}
parentDir = dirname(parentDir);
}
if (filters.length === 0) {
return (paths) => paths;
}
return (paths) =>
paths.filter((filePath) => {
for (const { dir, ig, ancestorPrefix } of filters) {
let relativePath: string | null;
if (!dir) {
relativePath = ancestorPrefix ? `${ancestorPrefix}/${filePath}` : filePath;
} else if (filePath.startsWith(`${dir}/`)) {
relativePath = filePath.slice(dir.length + 1);
} else {
relativePath = null;
}
if (relativePath !== null && ig.ignores(relativePath)) {
return false;
}
}
return true;
});
};
interface ActorIgnoreResult {
/** Filter that removes paths matching non-negated .actorignore patterns */
excludeFilter: ((paths: string[]) => string[]) | null;
/** Patterns from negation lines (with `!` stripped) — files matching these should be force-included even if git-ignored */
forceIncludePatterns: string[];
}
const parseActorIgnore = async (cwd: string): Promise<ActorIgnoreResult> => {
const actorignorePath = join(cwd, '.actorignore');
if (!existsSync(actorignorePath)) {
return { excludeFilter: null, forceIncludePatterns: [] };
}
const content = await readFile(actorignorePath, 'utf-8');
const lines = content.split('\n');
const excludeLines: string[] = [];
const forceIncludePatterns: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
if (trimmed.startsWith('!')) {
forceIncludePatterns.push(trimmed.slice(1));
} else {
excludeLines.push(trimmed);
}
}
const excludeFilter =
excludeLines.length > 0
? (paths: string[]) => {
const ig = makeIg().add(excludeLines);
return paths.filter((filePath) => !ig.ignores(filePath));
}
: null;
return { excludeFilter, forceIncludePatterns };
};
/**
* Get Actor local files, omit files defined in .gitignore, .actorignore and .git folder
* All dot files(.file) and folders(.folder/) are included.
*/
export const getActorLocalFilePaths = async (cwd?: string) => {
const resolvedCwd = cwd ?? process.cwd();
const hardcodedIgnore = ['.git/**', 'apify_storage', 'node_modules', 'storage', 'crawlee_storage'];
// Parse .actorignore early to get both exclude filter and force-include patterns
const { excludeFilter: actorignoreFilter, forceIncludePatterns } = await parseActorIgnore(resolvedCwd);
let gitIgnoreFilter: ((paths: string[]) => string[]) | null = null;
// Use git ls-files to get gitignored paths — this correctly handles ancestor .gitignore files,
// nested .gitignore files, .git/info/exclude, and global gitignore config
try {
const gitIgnored = execSync('git ls-files --others --ignored --exclude-standard --directory', {
cwd: resolvedCwd,
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore'],
})
.split('\n')
.filter(Boolean);
if (gitIgnored.length > 0) {
const ig = makeIg().add(gitIgnored);
gitIgnoreFilter = (paths) => paths.filter((p) => !ig.ignores(p));
}
} catch {
// git is unavailable or directory is not a git repo — fall back to parsing .gitignore files
gitIgnoreFilter = await getGitignoreFallbackFilter(resolvedCwd);
}
const allFiles = await glob(['*', '**/**'], {
ignore: hardcodedIgnore,
dot: true,
expandDirectories: false,
cwd: resolvedCwd,
});
let paths = gitIgnoreFilter ? gitIgnoreFilter(allFiles) : allFiles;
if (actorignoreFilter) {
paths = actorignoreFilter(paths);
}
// Force-include: negation patterns in .actorignore (e.g. !dist/) override gitignore,
// allowing git-ignored files to be included in the push
if (forceIncludePatterns.length > 0) {
const forceIncludeIg = makeIg().add(forceIncludePatterns);
const forceIncluded = allFiles.filter((filePath) => forceIncludeIg.ignores(filePath));
const pathSet = new Set(paths);
for (const file of forceIncluded) {
pathSet.add(file);
}
paths = [...pathSet];
}
// .actor/ is the Actor specification folder — always include it regardless of gitignore/actorignore
const actorSpecFiles = allFiles.filter((p) => p === '.actor' || p.startsWith('.actor/'));
if (actorSpecFiles.length > 0) {
const pathSet = new Set(paths);
for (const file of actorSpecFiles) {
pathSet.add(file);
}
paths = [...pathSet];
}
return paths;
};
/**
* Create zip file with all Actor files specified with pathsToZip
*/
export const createActZip = async (zipName: string, pathsToZip: string[], cwd: string) => {
// NOTE: There can be a zip from a previous unfinished operation.
if (existsSync(zipName)) {
await deleteFile(zipName);
}
const writeStream = createWriteStream(zipName);
// Use compression level 6 for better balance between speed and compression ratio (default is 9)
const archive = archiver('zip', {
zlib: { level: 6 },
});
archive.pipe(writeStream);
pathsToZip.forEach((filePath) => archive.file(join(cwd, filePath), { name: filePath }));
await archive.finalize();
};
/**
* Get Actor input from local store
*/
export const getLocalInput = (cwd: string, inputKey?: string) => {
const defaultLocalStorePath = getLocalKeyValueStorePath();
const folderExists = existsSync(join(cwd, defaultLocalStorePath));
if (!folderExists) return;
const files = readdirSync(join(cwd, defaultLocalStorePath));
const inputName = files.find((file) => !!file.match(inputFileRegExp(inputKey ?? 'INPUT')));
// No input file
if (!inputName) return;
const input = readFileSync(join(cwd, defaultLocalStorePath, inputName));
const contentType = mime.getType(inputName);
return { body: input, contentType, fileName: inputName };
};
export const purgeDefaultQueue = async () => {
const defaultQueuesPath = getLocalRequestQueuePath();
if (existsSync(getLocalStorageDir()) && existsSync(defaultQueuesPath)) {
await rimrafPromised(defaultQueuesPath);
}
};
export const purgeDefaultDataset = async () => {
const defaultDatasetPath = getLocalDatasetPath();
if (existsSync(getLocalStorageDir()) && existsSync(defaultDatasetPath)) {
await rimrafPromised(defaultDatasetPath);
}
};
export const purgeDefaultKeyValueStore = async (...inputKeys: string[]) => {
const defaultKeyValueStorePath = getLocalKeyValueStorePath();
if (!existsSync(getLocalStorageDir()) || !existsSync(defaultKeyValueStorePath)) {
return;
}
const filesToDelete = readdirSync(defaultKeyValueStorePath);
const preserveRegExps = (inputKeys.length > 0 ? inputKeys : ['INPUT']).map(inputFileRegExp);
const deletePromises: Promise<void>[] = [];
filesToDelete.forEach((file) => {
if (!preserveRegExps.some((re) => re.test(file))) {
deletePromises.push(deleteFile(join(defaultKeyValueStorePath, file)));
}
});
await Promise.all(deletePromises);
};
export const outputJobLog = async ({
job,
timeoutMillis,
apifyClient,
}: {
job: ActorRun | Build;
timeoutMillis?: number;
apifyClient?: ApifyClient;
}) => {
const { id: logId, status } = job;
const client = apifyClient || new ApifyClient({ baseUrl: process.env.APIFY_CLIENT_BASE_URL });
// In case job was already done just output log
if (ACTOR_JOB_TERMINAL_STATUSES.includes(status as never)) {
if (process.env.APIFY_NO_LOGS_IN_TESTS) {
return;
}
const log = await client.log(logId).get();
process.stderr.write(log!);
return;
}
// In other case stream it to stderr
// eslint-disable-next-line no-async-promise-executor
return new Promise<'no-logs' | 'finished' | 'timeouts'>(async (resolve) => {
const stream = await client.log(logId).stream();
if (!stream) {
resolve('no-logs');
return;
}
let nodeTimeout: NodeJS.Timeout | null = null;
stream.on('data', (chunk) => {
// In tests, writing to process.stderr directly messes with vitest's output
// With that said, we still NEED to wait for this stream to end, as otherwise tests become flaky.
if (process.env.APIFY_NO_LOGS_IN_TESTS) {
return;
}
process.stderr.write(chunk.toString());
});
stream.once('end', () => {
resolve('finished');
if (nodeTimeout) {
clearTimeout(nodeTimeout);
}
});
if (timeoutMillis) {
nodeTimeout = setTimeout(() => {
stream.destroy();
resolve('timeouts');
}, timeoutMillis);
}
});
};
/**
* Returns npm command for current os
* NOTE: For window we have to returns npm.cmd instead of npm, otherwise it doesn't work
*/
export const getNpmCmd = (): string => {
return /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
};
/**
* Returns true if apify storage is empty (expect INPUT.*)
*/
export const checkIfStorageIsEmpty = async (inputKey?: string) => {
const key = inputKey || KEY_VALUE_STORE_KEYS.INPUT;
const filesWithoutInput = await glob([
`${getLocalStorageDir()}/**`,
`!${getLocalKeyValueStorePath()}/${key}.*`,
`!${getLocalKeyValueStorePath()}/${TEMP_INPUT_KEY_PREFIX}${key}.*`,
]);
return filesWithoutInput.length === 0;
};
/**
* Validates Actor name, if finds issue throws error.
* @param actorName
*/
export const validateActorName = (actorName: string) => {
if (!ACTOR_NAME.REGEX.test(actorName)) {
throw new Error('The Actor name must be a DNS hostname-friendly string (e.g. my-newest-actor).');
}
if (actorName.length < ACTOR_NAME.MIN_LENGTH) {
throw new Error('The Actor name must be at least 3 characters long.');
}
if (actorName.length > ACTOR_NAME.MAX_LENGTH) {
throw new Error('The Actor name must be a maximum of 30 characters long.');
}
};
export const sanitizeActorName = (actorName: string) => {
let sanitizedName = actorName.replaceAll(/[^a-zA-Z0-9-]/g, '-');
if (sanitizedName.length < ACTOR_NAME.MIN_LENGTH) {
sanitizedName = `${sanitizedName}-apify-actor`;
}
sanitizedName = sanitizedName.replaceAll(/^-+/g, '').replaceAll(/-+$/g, '');
return sanitizedName.slice(0, ACTOR_NAME.MAX_LENGTH);
};
export const isPythonVersionSupported = (installedPythonVersion: string) => {
return satisfies(installedPythonVersion, `^${MINIMUM_SUPPORTED_PYTHON_VERSION}`);
};
export const isNodeVersionSupported = (installedNodeVersion: string) => {
// SUPPORTED_NODEJS_VERSION can be a version range,
// we need to get the minimum supported version from that range to be able to compare them
const minimumSupportedNodeVersion = minVersion(SUPPORTED_NODEJS_VERSION)!;
return gte(installedNodeVersion, minimumSupportedNodeVersion);
};
export const downloadAndUnzip = async ({ url, pathTo }: { url: string; pathTo: string }) => {
const zipStream = await httpsGet(url);
const chunks: Buffer[] = [];
zipStream.on('data', (chunk) => chunks.push(chunk));
await finished(zipStream);
const zip = new AdmZip(Buffer.concat(chunks));
zip.extractAllTo(pathTo, true);
};
/**
* Ensures the Apify directory exists, as well as nested folders (for tests)
*/
export function ensureApifyDirectory(file: string) {
const path = dirname(file);
mkdirSync(path, { recursive: true });
}
export const TimestampFormatter = new Timestamp('YYYY-MM-DD [at] HH:mm:ss');
export const MultilineTimestampFormatter = new Timestamp(`YYYY-MM-DD[\n]HH:mm:ss`);
export const DateOnlyTimestampFormatter = new Timestamp('YYYY-MM-DD');
export const DurationFormatter = new SapphireDurationFormatter();
export const ShortDurationFormatter = new SapphireDurationFormatter({
[TimeTypes.Day]: {
DEFAULT: 'd',
},
[TimeTypes.Hour]: {
DEFAULT: 'h',
},
[TimeTypes.Minute]: {
DEFAULT: 'm',
},
[TimeTypes.Month]: {
DEFAULT: 'M',
},
[TimeTypes.Second]: {
DEFAULT: 's',
},
[TimeTypes.Week]: {
DEFAULT: 'w',
},
[TimeTypes.Year]: {
DEFAULT: 'y',
},
});
/**
* A "polyfill" for Object.groupBy
*/
export function objectGroupBy<K extends PropertyKey, T>(
items: Iterable<T>,
keySelector: (item: T, index: number) => K,
): Partial<Record<K, T[]>> {
if ('groupBy' in Object) {
return (Object.groupBy as typeof objectGroupBy)(items, keySelector);
}
const result: Partial<Record<K, T[]>> = {};
let i = 0;
for (const item of items) {
const key = keySelector(item, i++);
if (!result[key]) {
result[key] = [];
}
result[key].push(item);
}
return result;
}
/**
* A "polyfill" for Map.groupBy
*/
export function mapGroupBy<K, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K): Map<K, T[]> {
const map = new Map<K, T[]>();
let index = 0;
for (const value of items) {
const key = keySelector(value, index++);
const list = map.get(key);
if (list) {
list.push(value);
} else {
map.set(key, [value]);
}
}
return map;
}
export function printJsonToStdout(object: unknown) {
console.log(JSON.stringify(object, null, 2));
}
export const tildify = (path: string) => {
if (path.startsWith(homedir())) {
return path.replace(homedir(), '~');
}
return path;
};
export function detectShell() {
const shell = process.env.SHELL ?? '';
if (shell.includes('zsh')) {
return 'zsh';
}
if (shell.includes('bash')) {
return 'bash';
}
if (shell.includes('fish')) {
return 'fish';
}
return 'unknown';
}
export function shellConfigFile(userHomeDirectory: string, shell: ReturnType<typeof detectShell>): string | null {
// eslint-disable-next-line default-case -- We do not want to add a shell and it fall through to default case
switch (shell) {
case 'bash': {
const configFiles = [join(userHomeDirectory, '.bashrc'), join(userHomeDirectory, '.bash_profile')];
if (process.env.XDG_CONFIG_HOME) {
configFiles.push(
join(process.env.XDG_CONFIG_HOME, '.bashrc'),
join(process.env.XDG_CONFIG_HOME, '.bash_profile'),
join(process.env.XDG_CONFIG_HOME, 'bashrc'),
join(process.env.XDG_CONFIG_HOME, 'bash_profile'),
);
}
for (const maybeConfigFile of configFiles) {
if (existsSync(maybeConfigFile)) {
return maybeConfigFile;
}
}
return null;
}
case 'zsh': {
const zshBaseDir = process.env.ZDOTDIR || homedir();
return join(zshBaseDir, '.zshrc');
}
case 'fish': {
return join(userHomeDirectory, '.config', 'fish', 'config.fish');
}
case 'unknown': {
return null;
}
}
}