zyket 1.2.15 → 1.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zyket",
3
- "version": "1.2.15",
3
+ "version": "1.2.17",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,8 +1,25 @@
1
- module.exports = class Worker {
2
- name;
3
- queueName = null;
4
-
5
- constructor(name) {
6
- this.name = name;
7
- }
8
- }
1
+ module.exports = class Worker {
2
+ name;
3
+ queueName = null;
4
+
5
+ // Per-instance configuration. Controls how many BullMQ workers are spawned
6
+ // for this class and what extra info each one receives:
7
+ // - number -> spawns N identical instances (instance = {} for each)
8
+ // - array -> spawns one instance per entry, each entry passed to handle()
9
+ // as `instance` (e.g. [{ proxy: '...' }, { proxy: '...' }])
10
+ // - function({ container }) -> resolved at boot, must return a number/array
11
+ // (e.g. load the proxy list from env/DB dynamically)
12
+ // - empty -> a single default instance
13
+ instances = [];
14
+
15
+ // BullMQ Worker options (e.g. { concurrency: 5 }). Can be:
16
+ // - object -> applied to every instance
17
+ // - function({ container, instance, index }) -> resolved at boot per
18
+ // instance (sync or async), so options can be computed
19
+ // dynamically (per-proxy concurrency, env-driven values, ...)
20
+ options = {};
21
+
22
+ constructor(name) {
23
+ this.name = name;
24
+ }
25
+ }
@@ -9,6 +9,7 @@ module.exports = class BullMQ extends Service {
9
9
  #container
10
10
  queues = {};
11
11
  queuesEvents = {};
12
+ workers = [];
12
13
 
13
14
  constructor(container) {
14
15
  super("queues");
@@ -35,13 +36,40 @@ module.exports = class BullMQ extends Service {
35
36
  this.#container.get('logger').warn(`Queue ${wkr.queueName} not found for worker ${wkr.name}, skipping...`);
36
37
  continue;
37
38
  }
38
- new BullWorker(
39
- wkr.queueName,
40
- async (job) => wkr.handle({ container: this.#container, job }),
41
- this.#connection()
42
- );
43
- this.#container.get('logger').info(`Worker ${wkr.name} for queue ${wkr.queueName} initialized`);
39
+
40
+ const instances = await this.#resolveInstances(wkr);
41
+ for (const [index, instance] of instances.entries()) {
42
+ const options = await this.#resolveOptions(wkr, instance, index);
43
+ const bullWorker = new BullWorker(
44
+ wkr.queueName,
45
+ async (job) => wkr.handle({ container: this.#container, job, instance, index }),
46
+ { ...this.#connection(), ...options }
47
+ );
48
+ this.workers.push(bullWorker);
49
+ }
50
+ this.#container.get('logger').info(`Worker ${wkr.name} for queue ${wkr.queueName} initialized with ${instances.length} instance(s)`);
51
+ }
52
+ }
53
+
54
+ async #resolveInstances(wkr) {
55
+ let instances = wkr.instances;
56
+ if (typeof instances === 'function') {
57
+ instances = await instances({ container: this.#container });
44
58
  }
59
+ if (typeof instances === 'number' && instances > 0) {
60
+ return Array.from({ length: instances }, () => ({}));
61
+ }
62
+ if (Array.isArray(instances) && instances.length) {
63
+ return instances;
64
+ }
65
+ return [{}];
66
+ }
67
+
68
+ async #resolveOptions(wkr, instance, index) {
69
+ const options = typeof wkr.options === 'function'
70
+ ? await wkr.options({ container: this.#container, instance, index })
71
+ : wkr.options;
72
+ return options || {};
45
73
  }
46
74
 
47
75
  async addJob(queueName, jobName, data, opts = {}, waitForCompletion = false) {
@@ -22,7 +22,7 @@ module.exports = [
22
22
  eventsActivated ? ["events", EventService, ["@service_container"]] : null,
23
23
  databaseActivated ? ["database", Database, ["@service_container", process.env.DATABASE_URL]] : null,
24
24
  ["cache", Cache, ["@service_container", process.env.CACHE_URL || '']],
25
- s3Activated ? ["s3", S3, ["@service_container", process.env.S3_ENDPOINT, process.env.S3_PORT, process.env.S3_USE_SSL === "true", process.env.S3_ACCESS_KEY, process.env.S3_SECRET_KEY]] : null,
25
+ s3Activated ? ["s3", S3, ["@service_container", process.env.S3_ENDPOINT, process.env.S3_PORT, process.env.S3_USE_SSL === "true", process.env.S3_ACCESS_KEY, process.env.S3_SECRET_KEY, process.env.S3_PUBLIC_BUCKETS, process.env.S3_PRIVATE_BUCKETS]] : null,
26
26
  schedulerActivated ? ["scheduler", Scheduler, ["@service_container"]] : null,
27
27
  bullmqActivated ? ["bullmq", require("./bullmq"), ["@service_container"]] : null,
28
28
  socketActivated ? ["socketio", SocketIO, ["@service_container"]] : null,
@@ -8,8 +8,10 @@ module.exports = class S3 extends Service {
8
8
  #useSSL
9
9
  #accessKey
10
10
  #secretKey
11
+ #publicBuckets
12
+ #privateBuckets
11
13
 
12
- constructor(container, endPoint, port, useSSL, accessKey, secretKey) {
14
+ constructor(container, endPoint, port, useSSL, accessKey, secretKey, publicBuckets, privateBuckets) {
13
15
  super('s3')
14
16
  this.#container = container
15
17
  this.#endPoint = endPoint
@@ -17,6 +19,8 @@ module.exports = class S3 extends Service {
17
19
  this.#useSSL = useSSL
18
20
  this.#accessKey = accessKey
19
21
  this.#secretKey = secretKey
22
+ this.#publicBuckets = this.#parseBuckets(publicBuckets)
23
+ this.#privateBuckets = this.#parseBuckets(privateBuckets)
20
24
  }
21
25
 
22
26
  async boot() {
@@ -27,6 +31,53 @@ module.exports = class S3 extends Service {
27
31
  accessKey: this.#accessKey,
28
32
  secretKey: this.#secretKey
29
33
  })
34
+
35
+ await this.initBuckets()
36
+ }
37
+
38
+ #parseBuckets(value) {
39
+ if (!value) return []
40
+ return value
41
+ .split(',')
42
+ .map((name) => name.trim())
43
+ .filter(Boolean)
44
+ }
45
+
46
+ async initBuckets() {
47
+ for (const bucketName of this.#privateBuckets) {
48
+ await this.ensureBucket(bucketName, false)
49
+ }
50
+ for (const bucketName of this.#publicBuckets) {
51
+ await this.ensureBucket(bucketName, true)
52
+ }
53
+ }
54
+
55
+ async ensureBucket(bucketName, isPublic = false) {
56
+ const exists = await this.client.bucketExists(bucketName)
57
+ if (!exists) {
58
+ this.#container.get('logger').info(`Creating ${isPublic ? 'public' : 'private'} bucket ${bucketName}`)
59
+ await this.client.makeBucket(bucketName, 'us-east-1')
60
+ }
61
+
62
+ if (isPublic) {
63
+ await this.setBucketPublic(bucketName)
64
+ }
65
+ }
66
+
67
+ async setBucketPublic(bucketName) {
68
+ const policy = {
69
+ Version: '2012-10-17',
70
+ Statement: [
71
+ {
72
+ Effect: 'Allow',
73
+ Principal: { AWS: ['*'] },
74
+ Action: ['s3:GetObject'],
75
+ Resource: [`arn:aws:s3:::${bucketName}/*`],
76
+ },
77
+ ],
78
+ }
79
+ this.#container.get('logger').debug(`Setting public read policy on bucket ${bucketName}`)
80
+ return this.client.setBucketPolicy(bucketName, JSON.stringify(policy))
30
81
  }
31
82
 
32
83
  async saveFile(bucketName, fileName, file, contentType = 'binary/octet-stream') {
@@ -31,6 +31,8 @@ module.exports = class EnvManager {
31
31
  S3_USE_SSL: true,
32
32
  S3_ACCESS_KEY: '',
33
33
  S3_SECRET_KEY: '',
34
+ S3_PUBLIC_BUCKETS: '',
35
+ S3_PRIVATE_BUCKETS: '',
34
36
  DISABLE_LOGGER: false,
35
37
  LOG_DIRECTORY: "./logs",
36
38
  QUEUES: '',