-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
133 lines (116 loc) · 3.29 KB
/
index.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
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
// Import Node.js Dependencies
import path from "node:path";
import timers from "node:timers/promises";
import { on, EventEmitter } from "node:events";
// Import Third-party Dependencies
import { search } from "@nodesecure/npm-registry-sdk";
import { klona } from "klona/json";
import is from "@slimio/is";
import { Mutex } from "@openally/mutex";
// Import Internal Dependencies
import { fetchPackage } from "./src/utils.js";
// CONSTANTS
const kRegSearchLimit = 10;
const kDefaultCriteria = { popularity: 1 };
const kDefaultLimit = 500;
// eslint-disable-next-line func-style
const kDefaultFetcher = (raw: { package: { name: string; version: number } }) => `${raw.package.name}@${raw.package.version}`;
const kMaximumConcurrentDownload = 10;
export interface IRunOptions {
name: string;
location: string;
root: string;
}
export interface ISearchPackagesByCriteriaOptions {
limit?: string;
delay?: string;
dataFetcher?: any;
criteria?: any;
}
export async function* searchPackagesByCriteria(
options: ISearchPackagesByCriteriaOptions = {}
) {
const limit = Number(options.limit) || kDefaultLimit;
const delay = Number(options.delay) || 0;
const dataFetcher = is.func(options.dataFetcher)
? options.dataFetcher
: kDefaultFetcher;
const criteria = is.plainObject(options.criteria)
? klona(options.criteria)
: kDefaultCriteria;
let from = 0;
while (true) {
const searchOptions = Object.assign(criteria, {
text: "boost-exact:true",
size: kRegSearchLimit,
from
});
const { objects }: any = await search(searchOptions);
yield* objects.map(dataFetcher);
from += kRegSearchLimit;
if (from >= limit) {
break;
}
if (delay > 0) {
await timers.setTimeout(delay);
}
}
}
// eslint-disable-next-line max-params
export async function downloadFromSource(
source: AsyncGenerator<any, void, any>,
ee: EventEmitter,
lock: Mutex,
tmpLocation: string
) {
try {
for await (const packageExpr of source) {
const free = await lock.acquire();
setImmediate(() => {
const tmpPathLocation = path.join(tmpLocation, packageExpr);
fetchPackage(packageExpr, tmpPathLocation)
.then(() => {
ee.emit("row", {
done: false,
value: {
name: packageExpr,
location: tmpPathLocation,
root: tmpLocation
}
});
free();
})
.catch(console.error);
});
}
ee.emit("row", { done: true });
}
catch (error) {
ee.emit("row", { done: true, error });
}
}
export type IDownloadPackageOnRegistryOptions = {
tempLocation: string;
concurrency?: number;
};
export async function* downloadPackageOnRegistry(
source: AsyncGenerator<any, void, any>,
options: IDownloadPackageOnRegistryOptions
) {
const { tempLocation, concurrency = kMaximumConcurrentDownload } = options;
const lock = new Mutex({ concurrency });
const ee = new EventEmitter();
setImmediate(() => downloadFromSource(source, ee, lock, tempLocation));
for await (const [data] of on(ee, "row")) {
const { done, error = null, value = null } = data;
if (done) {
if (error !== null) {
throw error;
}
break;
}
else if (value !== null) {
yield value;
}
}
}