@quatrain/worker 1.1.7 → 1.1.11

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/lib/FileSystem.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -17,6 +8,7 @@ const fs_1 = __importDefault(require("fs"));
17
8
  const path_1 = __importDefault(require("path"));
18
9
  const axios_1 = __importDefault(require("axios"));
19
10
  const Worker_1 = require("./Worker");
11
+ const node_fetch_native_1 = __importDefault(require("node-fetch-native"));
20
12
  class FileSystem {
21
13
  static prepare(folder) {
22
14
  Worker_1.Worker.log(`Setting up process folder ${folder}`);
@@ -45,44 +37,88 @@ class FileSystem {
45
37
  fs_1.default.rmdirSync(folder);
46
38
  }
47
39
  }
48
- static downloadFile(url, filepath) {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- try {
51
- const writer = fs_1.default.createWriteStream(filepath);
52
- const response = yield axios_1.default.get(url, {
53
- responseType: 'stream',
54
- });
55
- response.data.pipe(writer);
56
- return new Promise((resolve, reject) => {
57
- writer.on('finish', resolve);
58
- writer.on('error', reject);
59
- });
60
- }
61
- catch (error) {
62
- console.error(`Download error: ${error.message}`);
63
- throw error;
64
- }
65
- });
40
+ static async downloadFile(url, filepath) {
41
+ try {
42
+ const writer = fs_1.default.createWriteStream(filepath);
43
+ const response = await axios_1.default.get(url, {
44
+ responseType: 'stream',
45
+ });
46
+ response.data.pipe(writer);
47
+ return new Promise((resolve, reject) => {
48
+ writer.on('finish', resolve);
49
+ writer.on('error', reject);
50
+ });
51
+ }
52
+ catch (error) {
53
+ console.error(`Download error: ${error.message}`);
54
+ throw error;
55
+ }
66
56
  }
67
57
  static safeString(name) {
68
58
  return name.replace(/\s+/g, '_');
69
59
  }
70
- static uploadFile(filename, destination, mime = 'application/octet-stream') {
60
+ // static uploadFile18(
61
+ // filename: string,
62
+ // destination: string,
63
+ // mime: string = 'application/octet-stream'
64
+ // ) {
65
+ // return new Promise((resolve, reject) => {
66
+ // try {
67
+ // const { size } = fs.statSync(filename)
68
+ // const bufferContent = fs.readFileSync(filename)
69
+ // fetch(destination, {
70
+ // method: 'POST',
71
+ // mode: 'cors',
72
+ // headers: {
73
+ // 'Content-Type': mime,
74
+ // 'Content-length': String(size),
75
+ // },
76
+ // body: bufferContent,
77
+ // })
78
+ // .then(() => resolve(undefined))
79
+ // .catch((err) => reject(err))
80
+ // } catch (err: any) {
81
+ // reject(err as Error)
82
+ // }
83
+ // })
84
+ // }
85
+ static uploadFile(filename, destination, mime = 'video/mp4') {
71
86
  return new Promise((resolve, reject) => {
72
87
  try {
88
+ let done = 0; // total bytes uploaded
89
+ let prev = 0; // latest value of done stored when % done was displayed
73
90
  const { size } = fs_1.default.statSync(filename);
74
- const bufferContent = fs_1.default.readFileSync(filename);
75
- fetch(destination, {
76
- method: 'POST',
91
+ const readStream = fs_1.default.createReadStream(filename);
92
+ const sizeMB = (size / (1024 * 1024)).toFixed(2);
93
+ readStream.on('data', (data) => {
94
+ done += data.length;
95
+ if (done - prev >= size / 20) {
96
+ // only display message when 5% more has been uploaded
97
+ Worker_1.Worker.info(`${((done / size) * 100).toFixed(2)}% - ${(done /
98
+ (1024 * 1024)).toFixed(2)}MB / ${sizeMB}MB`);
99
+ prev = done;
100
+ }
101
+ });
102
+ Worker_1.Worker.info(`Uploading file ${filename} with size ${size} bytes`);
103
+ (0, node_fetch_native_1.default)(destination, {
104
+ //res.headers.get('location'), {
105
+ method: 'PUT',
77
106
  mode: 'cors',
107
+ duplex: 'half',
108
+ body: readStream,
78
109
  headers: {
79
110
  'Content-Type': mime,
80
111
  'Content-length': String(size),
81
112
  },
82
- body: bufferContent,
83
113
  })
84
- .then(() => resolve(undefined))
114
+ .then((res) => resolve(res))
85
115
  .catch((err) => reject(err));
116
+ // } else {
117
+ // Worker.error(res)
118
+ // reject(new Error('Unable to init upload'))
119
+ // }
120
+ //})
121
+ // .catch((err: any) => reject(err))
86
122
  }
87
123
  catch (err) {
88
124
  reject(err);
package/lib/Worker.js CHANGED
@@ -9,13 +9,51 @@ const os_1 = __importDefault(require("os"));
9
9
  const axios_1 = __importDefault(require("axios"));
10
10
  const child_process_1 = require("child_process");
11
11
  class Worker extends core_1.Core {
12
+ static endpoint = '';
13
+ /**
14
+ * Execute an external command in a promise
15
+ * @see https://stackoverflow.com/questions/46289682/how-to-wait-for-child-process-spawn-execution-with-async-await
16
+ * @see https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node
17
+ * @param string command
18
+ * @param array args
19
+ * @return Promise
20
+ */
21
+ static execPromise = (command, args = [], cwd = process.cwd()) => {
22
+ Worker.log(`Executing command ${command} in ${cwd} with arguments:`);
23
+ args.forEach((arg) => console.log(`\t${arg}`));
24
+ return new Promise((resolve, reject) => {
25
+ const child = (0, child_process_1.spawn)(command, args, { cwd });
26
+ child.stdout.on('data', (data) => {
27
+ console.info(`stdout: ${data}`);
28
+ });
29
+ child.stderr.on('data', (data) => {
30
+ console.info(`stderr: ${data}`);
31
+ });
32
+ child.on('close', (code) => {
33
+ if (code !== 0) {
34
+ console.error(`Command execution failed with code: ${code}`);
35
+ reject(code);
36
+ }
37
+ else {
38
+ console.info(`Command execution completed with code: ${code}`);
39
+ resolve(undefined);
40
+ }
41
+ });
42
+ });
43
+ };
12
44
  static pushEvent(event, data = {}, ts = 0) {
13
45
  if (!this.endpoint) {
14
46
  Worker.log(`Events endpoint is not set, can't send update!`);
15
47
  return false;
16
48
  }
17
49
  ts = ts === Date.now() ? Date.now() + 1 : Date.now();
18
- const payload = Object.assign(Object.assign({ event, worker: `Container ${os_1.default.hostname}`, os: `${os_1.default.type} ${os_1.default.release} (${os_1.default.platform} ${os_1.default.arch})` }, data), { ts });
50
+ const payload = {
51
+ event,
52
+ worker: `Container ${os_1.default.hostname}`,
53
+ os: `${os_1.default.type} ${os_1.default.release} (${os_1.default.platform} ${os_1.default.arch})`,
54
+ ...data,
55
+ ts,
56
+ };
19
57
  axios_1.default
20
58
  .put(Worker.endpoint, payload)
21
59
  .then((res) => Worker.log(`Event pushed to backend: ${res.statusText}`))
@@ -23,35 +61,3 @@ class Worker extends core_1.Core {
23
61
  }
24
62
  }
25
63
  exports.Worker = Worker;
26
- Worker.endpoint = '';
27
- /**
28
- * Execute an external command in a promise
29
- * @see https://stackoverflow.com/questions/46289682/how-to-wait-for-child-process-spawn-execution-with-async-await
30
- * @see https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node
31
- * @param string command
32
- * @param array args
33
- * @return Promise
34
- */
35
- Worker.execPromise = (command, args = [], cwd = process.cwd()) => {
36
- Worker.log(`Executing command ${command} in ${cwd} with arguments:`);
37
- args.forEach((arg) => console.log(`\t${arg}`));
38
- return new Promise((resolve, reject) => {
39
- const child = (0, child_process_1.spawn)(command, args, { cwd });
40
- child.stdout.on('data', (data) => {
41
- console.info(`stdout: ${data}`);
42
- });
43
- child.stderr.on('data', (data) => {
44
- console.info(`stderr: ${data}`);
45
- });
46
- child.on('close', (code) => {
47
- if (code !== 0) {
48
- console.error(`Command execution failed with code: ${code}`);
49
- reject(code);
50
- }
51
- else {
52
- console.info(`Command execution completed with code: ${code}`);
53
- resolve(undefined);
54
- }
55
- });
56
- });
57
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/worker",
3
- "version": "1.1.7",
3
+ "version": "1.1.11",
4
4
  "description": "Container Worker helpers",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -24,16 +24,20 @@
24
24
  "typescript": "^5.1.5"
25
25
  },
26
26
  "dependencies": {
27
- "@quatrain/core": "^1.1.7",
27
+ "@quatrain/core": "^1.1.10",
28
28
  "axios": "^1.7.7",
29
- "fs-extra": "^11.2.0"
29
+ "fs-extra": "^11.2.0",
30
+ "node-fetch-native": "^1.6.4"
30
31
  },
31
32
  "scripts": {
32
- "test": "tsc && jest --verbose",
33
+ "test-ci": "jest --runInBand",
33
34
  "build": "tsc",
34
35
  "wbuild": "tsc --watch",
35
36
  "bump-to": "yarn version",
36
- "patch": "yarn version --patch --no-git-tag-version",
37
- "publish": "yarn build && yarn npm publish --access public"
37
+ "hash": "node ../../bin/hashFolder.js",
38
+ "hash_persist": "yarn hash > .hash_latest.txt",
39
+ "hash_compare": "yarn hash > .hash_newest.txt && cmp -s .hash_latest.txt .hash_newest.txt",
40
+ "publish": "yarn hash_compare || yarn publish_process",
41
+ "publish_process": "yarn version patch && yarn build && yarn npm publish --access public && yarn hash_persist"
38
42
  }
39
43
  }