@quatrain/worker 1.2.2 → 1.2.4

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.
@@ -1,9 +1,45 @@
1
1
  import { FileType } from '@quatrain/storage';
2
+ /**
3
+ * Utility class providing synchronous and asynchronous file system operations
4
+ * specifically tailored for the worker environment (e.g., managing temp folders, downloading remote files).
5
+ */
2
6
  export declare class FileSystem {
7
+ /**
8
+ * Prepares a clean processing directory, creating it along with required subdirectories (`images`, `vecto`).
9
+ * Any existing folder with the same name will be deleted first.
10
+ *
11
+ * @param folder - The base directory path to set up.
12
+ */
3
13
  static prepare(folder: string): void;
14
+ /**
15
+ * Synchronously creates a single directory.
16
+ *
17
+ * @param folder - The directory path.
18
+ */
4
19
  static makeFolder(folder: string): void;
20
+ /**
21
+ * Recursively and synchronously removes a folder and its entire contents.
22
+ *
23
+ * @param folder - The target directory to destroy.
24
+ * @param recursively - Whether to traverse and delete nested folders. Defaults to true.
25
+ * @throws {Error} If `recursively` is false but nested folders are encountered.
26
+ */
5
27
  static removeFolder(folder: string, recursively?: boolean): void;
28
+ /**
29
+ * Downloads a file from an external HTTP/HTTPS URL into the local filesystem.
30
+ *
31
+ * @param url - The remote resource URL.
32
+ * @param filepath - The local destination path.
33
+ * @returns A promise resolving when the download finishes.
34
+ * @throws {Error} If the HTTP request or stream fails.
35
+ */
6
36
  static downloadFile(url: string, filepath: string): Promise<unknown>;
37
+ /**
38
+ * Sanitizes a string to be used safely as a filename by replacing spaces with underscores.
39
+ *
40
+ * @param name - The original string.
41
+ * @returns The sanitized string.
42
+ */
7
43
  static safeString(name: string): string;
8
44
  /**
9
45
  * Upload file to public URL and return meta data
@@ -43,7 +43,17 @@ const axios_1 = __importDefault(require("axios"));
43
43
  const Worker_1 = require("./Worker");
44
44
  const node_fetch_native_1 = __importDefault(require("node-fetch-native"));
45
45
  const ffmpeg = __importStar(require("fluent-ffmpeg"));
46
+ /**
47
+ * Utility class providing synchronous and asynchronous file system operations
48
+ * specifically tailored for the worker environment (e.g., managing temp folders, downloading remote files).
49
+ */
46
50
  class FileSystem {
51
+ /**
52
+ * Prepares a clean processing directory, creating it along with required subdirectories (`images`, `vecto`).
53
+ * Any existing folder with the same name will be deleted first.
54
+ *
55
+ * @param folder - The base directory path to set up.
56
+ */
47
57
  static prepare(folder) {
48
58
  Worker_1.Worker.debug(`Setting up process folder ${folder}`);
49
59
  this.removeFolder(folder);
@@ -51,9 +61,21 @@ class FileSystem {
51
61
  node_fs_1.default.mkdirSync(folder + node_path_1.default.sep + 'images');
52
62
  node_fs_1.default.mkdirSync(folder + node_path_1.default.sep + 'vecto');
53
63
  }
64
+ /**
65
+ * Synchronously creates a single directory.
66
+ *
67
+ * @param folder - The directory path.
68
+ */
54
69
  static makeFolder(folder) {
55
70
  node_fs_1.default.mkdirSync(folder);
56
71
  }
72
+ /**
73
+ * Recursively and synchronously removes a folder and its entire contents.
74
+ *
75
+ * @param folder - The target directory to destroy.
76
+ * @param recursively - Whether to traverse and delete nested folders. Defaults to true.
77
+ * @throws {Error} If `recursively` is false but nested folders are encountered.
78
+ */
57
79
  static removeFolder(folder, recursively = true) {
58
80
  if (node_fs_1.default.existsSync(folder)) {
59
81
  node_fs_1.default.readdirSync(folder).forEach((element) => {
@@ -71,6 +93,14 @@ class FileSystem {
71
93
  node_fs_1.default.rmdirSync(folder);
72
94
  }
73
95
  }
96
+ /**
97
+ * Downloads a file from an external HTTP/HTTPS URL into the local filesystem.
98
+ *
99
+ * @param url - The remote resource URL.
100
+ * @param filepath - The local destination path.
101
+ * @returns A promise resolving when the download finishes.
102
+ * @throws {Error} If the HTTP request or stream fails.
103
+ */
74
104
  static async downloadFile(url, filepath) {
75
105
  try {
76
106
  Worker_1.Worker.debug(`Downloading file at ${url} to ${filepath}`);
@@ -89,8 +119,14 @@ class FileSystem {
89
119
  throw err;
90
120
  }
91
121
  }
122
+ /**
123
+ * Sanitizes a string to be used safely as a filename by replacing spaces with underscores.
124
+ *
125
+ * @param name - The original string.
126
+ * @returns The sanitized string.
127
+ */
92
128
  static safeString(name) {
93
- return name.replace(/\s+/g, '_');
129
+ return name.replaceAll(/\s+/g, '_');
94
130
  }
95
131
  /**
96
132
  * Upload file to public URL and return meta data
@@ -174,13 +210,13 @@ class FileSystem {
174
210
  }
175
211
  // Worker.debug(`ffprobe getInfo: ${JSON.stringify(metadata)}`)
176
212
  const { width, height, duration, bit_rate: bitrate, nb_frames: nbFramees, } = metadata.streams[0];
177
- const nb_frames = parseFloat(nbFramees);
178
- const framerate = nb_frames / parseFloat(duration);
213
+ const nb_frames = Number.parseFloat(nbFramees);
214
+ const framerate = nb_frames / Number.parseFloat(duration);
179
215
  resolve({
180
216
  width,
181
217
  height,
182
- framerate: parseInt(framerate.toFixed(0)),
183
- duration: parseInt(duration),
218
+ framerate: Number.parseInt(framerate.toFixed(0)),
219
+ duration: Number.parseInt(duration),
184
220
  bitrate,
185
221
  });
186
222
  }));
package/dist/Helpers.d.ts CHANGED
@@ -1,5 +1,9 @@
1
+ /**
2
+ * General utility class providing common static helpers for workers.
3
+ */
1
4
  export declare class Helpers {
2
- static FFMPEG: string;
5
+ /** Default absolute path to the system's FFmpeg binary. */
6
+ static FFMPEG: Promise<string>;
3
7
  /**
4
8
  * Generate a thubnail from a video file at given frame position
5
9
  * @param videoPath path to video file
package/dist/Helpers.js CHANGED
@@ -2,8 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Helpers = void 0;
4
4
  const Worker_1 = require("./Worker");
5
+ /**
6
+ * General utility class providing common static helpers for workers.
7
+ */
5
8
  class Helpers {
6
- static FFMPEG = '/usr/bin/ffmpeg';
9
+ /** Default absolute path to the system's FFmpeg binary. */
10
+ static FFMPEG = Worker_1.Worker.getSystemCommandPath('ffmpeg');
7
11
  /**
8
12
  * Generate a thubnail from a video file at given frame position
9
13
  * @param videoPath path to video file
@@ -28,7 +32,7 @@ class Helpers {
28
32
  '-y',
29
33
  ];
30
34
  Worker_1.Worker.info(`Generating Thumbnail : ${outputPath}`);
31
- return await Worker_1.Worker.execPromise(Helpers.FFMPEG, ffmpegParams);
35
+ return await Worker_1.Worker.execPromise(await Helpers.FFMPEG, ffmpegParams);
32
36
  };
33
37
  }
34
38
  exports.Helpers = Helpers;
package/dist/Worker.d.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  import { Core } from '@quatrain/core';
2
2
  import { HandlerParameters } from './types/HandlerParameters';
3
+ /**
4
+ * The core orchestration class for background task workers.
5
+ * Manages event reporting, child process execution, and queue listening.
6
+ */
3
7
  export declare class Worker extends Core {
8
+ /** The HTTP endpoint used to push worker status events. */
4
9
  static endpoint: string;
10
+ /** Dedicated logger instance for the worker subsystem. */
5
11
  static readonly logger: any;
6
12
  /**
7
13
  * Execute an external command in a promise
package/dist/Worker.js CHANGED
@@ -8,8 +8,14 @@ const core_1 = require("@quatrain/core");
8
8
  const node_os_1 = __importDefault(require("node:os"));
9
9
  const axios_1 = __importDefault(require("axios"));
10
10
  const node_child_process_1 = require("node:child_process");
11
+ /**
12
+ * The core orchestration class for background task workers.
13
+ * Manages event reporting, child process execution, and queue listening.
14
+ */
11
15
  class Worker extends core_1.Core {
16
+ /** The HTTP endpoint used to push worker status events. */
12
17
  static endpoint = '';
18
+ /** Dedicated logger instance for the worker subsystem. */
13
19
  static logger = this.addLogger('Worker');
14
20
  /**
15
21
  * Execute an external command in a promise
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/worker",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Container Worker helpers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -34,9 +34,9 @@
34
34
  "typescript": "^5.1.5"
35
35
  },
36
36
  "dependencies": {
37
- "@quatrain/core": "^1.2.5",
38
- "@quatrain/queue": "^1.2.1",
39
- "@quatrain/storage": "^1.2.2",
37
+ "@quatrain/core": "^1.2.6",
38
+ "@quatrain/queue": "^1.2.3",
39
+ "@quatrain/storage": "^1.2.4",
40
40
  "axios": "^1.7.7",
41
41
  "fluent-ffmpeg": "^2.1.2",
42
42
  "fs-extra": "^11.2.0",
package/src/FileSystem.ts CHANGED
@@ -6,7 +6,17 @@ import fetch from 'node-fetch-native'
6
6
  import * as ffmpeg from 'fluent-ffmpeg'
7
7
  import { FileType } from '@quatrain/storage'
8
8
 
9
+ /**
10
+ * Utility class providing synchronous and asynchronous file system operations
11
+ * specifically tailored for the worker environment (e.g., managing temp folders, downloading remote files).
12
+ */
9
13
  export class FileSystem {
14
+ /**
15
+ * Prepares a clean processing directory, creating it along with required subdirectories (`images`, `vecto`).
16
+ * Any existing folder with the same name will be deleted first.
17
+ *
18
+ * @param folder - The base directory path to set up.
19
+ */
10
20
  static prepare(folder: string) {
11
21
  Worker.debug(`Setting up process folder ${folder}`)
12
22
  this.removeFolder(folder)
@@ -16,10 +26,22 @@ export class FileSystem {
16
26
  fs.mkdirSync(folder + path.sep + 'vecto')
17
27
  }
18
28
 
29
+ /**
30
+ * Synchronously creates a single directory.
31
+ *
32
+ * @param folder - The directory path.
33
+ */
19
34
  static makeFolder(folder: string) {
20
35
  fs.mkdirSync(folder)
21
36
  }
22
37
 
38
+ /**
39
+ * Recursively and synchronously removes a folder and its entire contents.
40
+ *
41
+ * @param folder - The target directory to destroy.
42
+ * @param recursively - Whether to traverse and delete nested folders. Defaults to true.
43
+ * @throws {Error} If `recursively` is false but nested folders are encountered.
44
+ */
23
45
  static removeFolder(folder: string, recursively = true) {
24
46
  if (fs.existsSync(folder)) {
25
47
  fs.readdirSync(folder).forEach((element) => {
@@ -38,6 +60,14 @@ export class FileSystem {
38
60
  }
39
61
  }
40
62
 
63
+ /**
64
+ * Downloads a file from an external HTTP/HTTPS URL into the local filesystem.
65
+ *
66
+ * @param url - The remote resource URL.
67
+ * @param filepath - The local destination path.
68
+ * @returns A promise resolving when the download finishes.
69
+ * @throws {Error} If the HTTP request or stream fails.
70
+ */
41
71
  static async downloadFile(url: string, filepath: string) {
42
72
  try {
43
73
  Worker.debug(`Downloading file at ${url} to ${filepath}`)
@@ -58,8 +88,14 @@ export class FileSystem {
58
88
  }
59
89
  }
60
90
 
91
+ /**
92
+ * Sanitizes a string to be used safely as a filename by replacing spaces with underscores.
93
+ *
94
+ * @param name - The original string.
95
+ * @returns The sanitized string.
96
+ */
61
97
  static safeString(name: string) {
62
- return name.replace(/\s+/g, '_')
98
+ return name.replaceAll(/\s+/g, '_')
63
99
  }
64
100
 
65
101
  /**
@@ -90,7 +126,7 @@ export class FileSystem {
90
126
  'Content-Type': meta.contentType || mime,
91
127
  'Content-length': String(size),
92
128
  },
93
- })
129
+ } as any)
94
130
  .then(() => resolve({ ...meta, size, uploadUrl: undefined }))
95
131
  .catch((err) => {
96
132
  Worker.error(
@@ -134,7 +170,7 @@ export class FileSystem {
134
170
  'Content-Type': meta.contentType || mime,
135
171
  'Content-length': String(size),
136
172
  },
137
- })
173
+ } as any)
138
174
  .then(() => resolve({ ...meta, size, uploadUrl: undefined }))
139
175
  .catch((err) => {
140
176
  Worker.error(
@@ -168,13 +204,13 @@ export class FileSystem {
168
204
  bit_rate: bitrate,
169
205
  nb_frames: nbFramees,
170
206
  } = metadata.streams[0]
171
- const nb_frames: number = parseFloat(nbFramees as string)
172
- const framerate = nb_frames / parseFloat(duration as string)
207
+ const nb_frames: number = Number.parseFloat(nbFramees as string)
208
+ const framerate = nb_frames / Number.parseFloat(duration as string)
173
209
  resolve({
174
210
  width,
175
211
  height,
176
- framerate: parseInt(framerate.toFixed(0)),
177
- duration: parseInt(duration as string),
212
+ framerate: Number.parseInt(framerate.toFixed(0)),
213
+ duration: Number.parseInt(duration as string),
178
214
  bitrate,
179
215
  })
180
216
  })
package/src/Helpers.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import { Worker } from './Worker'
2
2
  import path from 'node:path'
3
3
 
4
+ /**
5
+ * General utility class providing common static helpers for workers.
6
+ */
4
7
  export class Helpers {
5
- static FFMPEG = '/usr/bin/ffmpeg'
8
+ /** Default absolute path to the system's FFmpeg binary. */
9
+ static FFMPEG = Worker.getSystemCommandPath('ffmpeg')
6
10
 
7
11
  /**
8
12
  * Generate a thubnail from a video file at given frame position
@@ -35,6 +39,6 @@ export class Helpers {
35
39
 
36
40
  Worker.info(`Generating Thumbnail : ${outputPath}`)
37
41
 
38
- return await Worker.execPromise(Helpers.FFMPEG, ffmpegParams)
42
+ return await Worker.execPromise(await Helpers.FFMPEG, ffmpegParams)
39
43
  }
40
44
  }
package/src/Worker.ts CHANGED
@@ -4,8 +4,14 @@ import axios from 'axios'
4
4
  import { spawn } from 'node:child_process'
5
5
  import { HandlerParameters } from './types/HandlerParameters'
6
6
 
7
+ /**
8
+ * The core orchestration class for background task workers.
9
+ * Manages event reporting, child process execution, and queue listening.
10
+ */
7
11
  export class Worker extends Core {
12
+ /** The HTTP endpoint used to push worker status events. */
8
13
  static endpoint: string = ''
14
+ /** Dedicated logger instance for the worker subsystem. */
9
15
  static readonly logger = this.addLogger('Worker')
10
16
 
11
17
  /**