@vbarbarosh/node-helpers 1.1.4 → 2.0.1

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.
Files changed (102) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/array_chunk.js +1 -1
  4. package/src/array_chunk.test.js +2 -2
  5. package/src/array_gcd.js +1 -1
  6. package/src/array_gcd.test.js +2 -2
  7. package/src/array_group.js +2 -2
  8. package/src/array_group_map.js +1 -1
  9. package/src/array_index.js +1 -1
  10. package/src/array_index.test.js +2 -2
  11. package/src/array_max.js +1 -1
  12. package/src/array_max.test.js +2 -2
  13. package/src/array_min.js +1 -1
  14. package/src/array_min.test.js +2 -2
  15. package/src/array_shuffle.js +1 -1
  16. package/src/array_sort.js +2 -2
  17. package/src/array_sort.test.js +2 -2
  18. package/src/array_sort_other.js +2 -2
  19. package/src/array_sort_other.test.js +2 -2
  20. package/src/array_sum.js +1 -1
  21. package/src/array_sum.test.js +2 -2
  22. package/src/array_unique_last.js +1 -1
  23. package/src/cli.js +8 -25
  24. package/src/crypto_decrypt_aes256.js +2 -2
  25. package/src/crypto_encrypt_aes256.js +2 -2
  26. package/src/crypto_encrypt_aes256.test.js +3 -3
  27. package/src/crypto_hash_md5.js +2 -2
  28. package/src/crypto_hash_sha256.js +2 -2
  29. package/src/crypto_hash_sha256.test.js +2 -2
  30. package/src/crypto_hmac_sha256.js +2 -2
  31. package/src/fcmp_dates.js +1 -1
  32. package/src/fcmp_numbers.js +1 -1
  33. package/src/fcmp_strings.js +1 -1
  34. package/src/format_thousands.js +1 -1
  35. package/src/fs_append.js +3 -3
  36. package/src/fs_append_json.js +3 -3
  37. package/src/fs_assert_file_exists.js +2 -2
  38. package/src/fs_assert_file_readable.js +2 -2
  39. package/src/fs_copy.js +3 -3
  40. package/src/fs_exists.js +2 -2
  41. package/src/fs_fclose.js +3 -3
  42. package/src/fs_fi.js +6 -6
  43. package/src/fs_find.js +5 -5
  44. package/src/fs_fopen.js +3 -3
  45. package/src/fs_fopen_update.js +2 -2
  46. package/src/fs_fread.js +3 -3
  47. package/src/fs_fsize.js +2 -2
  48. package/src/fs_fstat.js +3 -3
  49. package/src/fs_fwrite.js +3 -3
  50. package/src/fs_ls.js +5 -5
  51. package/src/fs_lsr.js +5 -5
  52. package/src/fs_lstat.js +3 -3
  53. package/src/fs_mkdir.js +3 -3
  54. package/src/fs_mkdirp.js +17 -11
  55. package/src/fs_mkdirp.test.js +15 -0
  56. package/src/fs_path_basename.js +2 -2
  57. package/src/fs_path_dirname.js +2 -2
  58. package/src/fs_path_join.js +2 -2
  59. package/src/fs_path_resolve.js +2 -2
  60. package/src/fs_path_sanitize.js +2 -2
  61. package/src/fs_path_tempdir.js +2 -2
  62. package/src/fs_read.js +3 -3
  63. package/src/fs_read_json.js +2 -2
  64. package/src/fs_read_stream.js +3 -1
  65. package/src/fs_read_utf8.js +2 -2
  66. package/src/fs_readdir.js +3 -3
  67. package/src/fs_readlink.js +3 -3
  68. package/src/fs_rename.js +3 -3
  69. package/src/fs_rm.js +3 -3
  70. package/src/fs_rmdir.js +3 -3
  71. package/src/fs_rmrf.js +7 -7
  72. package/src/fs_size.js +2 -2
  73. package/src/fs_tempdir.js +6 -6
  74. package/src/fs_write.js +3 -3
  75. package/src/fs_write_stream.js +3 -1
  76. package/src/http_delete.js +2 -2
  77. package/src/http_get_blob.js +2 -2
  78. package/src/http_get_buffer.js +2 -2
  79. package/src/http_get_file.js +6 -6
  80. package/src/http_get_json.js +2 -2
  81. package/src/http_patch_json.js +2 -2
  82. package/src/http_post_json.js +2 -2
  83. package/src/http_post_multipart.js +3 -3
  84. package/src/http_post_urlencoded.js +3 -3
  85. package/src/http_put_file.js +6 -6
  86. package/src/http_put_json.js +2 -2
  87. package/src/identity.js +1 -1
  88. package/src/ignore.js +1 -1
  89. package/src/json_stringify_stable.js +2 -2
  90. package/src/now_atom.js +1 -1
  91. package/src/random_int.js +6 -0
  92. package/src/redis_connect.js +21 -0
  93. package/src/redis_lshift.js +8 -0
  94. package/src/redis_poll.js +106 -0
  95. package/src/redis_poll_zshift_callback_rpush.js +141 -0
  96. package/src/redis_poll_zshift_spawn_rpush.js +159 -0
  97. package/src/redis_zshift.js +9 -0
  98. package/src/stream_data_ln.js +1 -1
  99. package/src/stream_data_ln.test.js +4 -4
  100. package/src/stream_promise.js +2 -2
  101. package/src/stream_promise.test.js +3 -3
  102. package/src/throttle.js +1 -1
package/src/fs_mkdirp.js CHANGED
@@ -1,26 +1,32 @@
1
- import fs_lstat from './fs_lstat';
2
- import fs_mkdir from './fs_mkdir';
3
- import fs_path_dirname from './fs_path_dirname';
1
+ const fs_exists = require('./fs_exists');
2
+ const fs_mkdir = require('./fs_mkdir');
3
+ const fs_path_dirname = require('./fs_path_dirname');
4
4
 
5
5
  async function fs_mkdirp(pathname)
6
6
  {
7
7
  const parents = [];
8
8
 
9
9
  for (let p = pathname; p && p != '/'; p = fs_path_dirname(p)) {
10
- try {
11
- await fs_lstat(p);
10
+ if (await fs_exists(p)) {
12
11
  break;
13
12
  }
14
- catch (error) {
15
- parents.unshift(p);
16
- }
13
+ parents.push(p);
17
14
  }
18
15
 
19
- for (let i = 0, end = parents.length; i < end; ++i) {
20
- await fs_mkdir(parents[i]);
16
+ while (parents.length) {
17
+ try {
18
+ await fs_mkdir(parents.pop());
19
+ }
20
+ catch (error) {
21
+ // Error: EEXIST: file already exists, mkdir [...]
22
+ if (error.code == 'EEXIST') {
23
+ break;
24
+ }
25
+ throw error;
26
+ }
21
27
  }
22
28
 
23
29
  return pathname;
24
30
  }
25
31
 
26
- export default fs_mkdirp;
32
+ module.exports = fs_mkdirp;
@@ -0,0 +1,15 @@
1
+ const assert = require('assert');
2
+ const fs_fi = require('./fs_fi');
3
+ const fs_mkdirp = require('./fs_mkdirp');
4
+ const fs_path_resolve = require('./fs_path_resolve');
5
+ const fs_tempdir = require('./fs_tempdir');
6
+
7
+ describe('fs_mkdirp', function () {
8
+ it('should accept empty array', async function () {
9
+ await fs_tempdir(async function (d) {
10
+ const tmp = fs_path_resolve(d, 'a/b/c/d/e');
11
+ await fs_mkdirp(tmp);
12
+ assert(await fs_fi(tmp).then(v => v.isDirectory()));
13
+ });
14
+ });
15
+ });
@@ -1,8 +1,8 @@
1
- import path from 'path';
1
+ const path = require('path');
2
2
 
3
3
  function fs_path_basename(p)
4
4
  {
5
5
  return path.basename(p);
6
6
  }
7
7
 
8
- export default fs_path_basename;
8
+ module.exports = fs_path_basename;
@@ -1,8 +1,8 @@
1
- import path from 'path';
1
+ const path = require('path');
2
2
 
3
3
  function fs_path_dirname(p)
4
4
  {
5
5
  return path.dirname(p);
6
6
  }
7
7
 
8
- export default fs_path_dirname;
8
+ module.exports = fs_path_dirname;
@@ -1,8 +1,8 @@
1
- import path from 'path';
1
+ const path = require('path');
2
2
 
3
3
  function fs_path_join(...parts)
4
4
  {
5
5
  return path.join(...parts);
6
6
  }
7
7
 
8
- export default fs_path_join;
8
+ module.exports = fs_path_join;
@@ -1,8 +1,8 @@
1
- import path from 'path';
1
+ const path = require('path');
2
2
 
3
3
  function fs_path_resolve(...parts)
4
4
  {
5
5
  return path.resolve(...parts);
6
6
  }
7
7
 
8
- export default fs_path_resolve;
8
+ module.exports = fs_path_resolve;
@@ -1,8 +1,8 @@
1
- import sanitize_filename from 'sanitize-filename';
1
+ const sanitize_filename = require('sanitize-filename');
2
2
 
3
3
  function fs_path_sanitize(filename)
4
4
  {
5
5
  return sanitize_filename(filename);
6
6
  }
7
7
 
8
- export default fs_path_sanitize;
8
+ module.exports = fs_path_sanitize;
@@ -1,8 +1,8 @@
1
- import os from 'os';
1
+ const os = require('os');
2
2
 
3
3
  function fs_path_tempdir()
4
4
  {
5
5
  return os.tmpdir();
6
6
  }
7
7
 
8
- export default fs_path_tempdir;
8
+ module.exports = fs_path_tempdir;
package/src/fs_read.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import util from 'util';
1
+ const fs = require('fs');
2
+ const util = require('util');
3
3
 
4
4
  // https://nodejs.org/api/util.html#util_util_promisify_original
5
- export default util.promisify(fs.readFile);
5
+ module.exports = util.promisify(fs.readFile);
@@ -1,4 +1,4 @@
1
- import fs_read from './fs_read';
1
+ const fs_read = require('./fs_read');
2
2
 
3
3
  // XXX Should support catchReturn, e.g.
4
4
  // fs_read_json(file).catchReturn(default_value)
@@ -7,4 +7,4 @@ function fs_read_json(file)
7
7
  return fs_read(file, {encoding: 'utf8'}).then(JSON.parse);
8
8
  }
9
9
 
10
- export default fs_read_json;
10
+ module.exports = fs_read_json;
@@ -1 +1,3 @@
1
- export {createReadStream as default} from 'fs';
1
+ const fs = require('fs');
2
+
3
+ module.exports = fs.createReadStream;
@@ -1,8 +1,8 @@
1
- import fs_read from './fs_read';
1
+ const fs_read = require('./fs_read');
2
2
 
3
3
  async function fs_read_utf8(file)
4
4
  {
5
5
  return await fs_read(file, {encoding: 'utf8'});
6
6
  }
7
7
 
8
- export default fs_read_utf8;
8
+ module.exports = fs_read_utf8;
package/src/fs_readdir.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import util from 'util';
1
+ const fs = require('fs');
2
+ const util = require('util');
3
3
 
4
4
  // https://nodejs.org/api/util.html#util_util_promisify_original
5
- export default util.promisify(fs.readdir);
5
+ module.exports = util.promisify(fs.readdir);
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import util from 'util';
1
+ const fs = require('fs');
2
+ const util = require('util');
3
3
 
4
4
  // https://nodejs.org/api/util.html#util_util_promisify_original
5
- export default util.promisify(fs.readlink);
5
+ module.exports = util.promisify(fs.readlink);
package/src/fs_rename.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import util from 'util';
1
+ const fs = require('fs');
2
+ const util = require('util');
3
3
 
4
4
  // https://nodejs.org/api/util.html#util_util_promisify_original
5
- export default util.promisify(fs.rename);
5
+ module.exports = util.promisify(fs.rename);
package/src/fs_rm.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import util from 'util';
1
+ const fs = require('fs');
2
+ const util = require('util');
3
3
 
4
4
  // https://nodejs.org/api/util.html#util_util_promisify_original
5
- export default util.promisify(fs.unlink);
5
+ module.exports = util.promisify(fs.unlink);
package/src/fs_rmdir.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import util from 'util';
1
+ const fs = require('fs');
2
+ const util = require('util');
3
3
 
4
4
  // https://nodejs.org/api/util.html#util_util_promisify_original
5
- export default util.promisify(fs.rmdir);
5
+ module.exports = util.promisify(fs.rmdir);
package/src/fs_rmrf.js CHANGED
@@ -1,9 +1,9 @@
1
- import Promise from 'bluebird';
2
- import fs_fi from './fs_fi';
3
- import fs_rm from './fs_rm';
4
- import fs_rmdir from './fs_rmdir';
5
- import fs_path_resolve from './fs_path_resolve';
6
- import fs_readdir from './fs_readdir';
1
+ const Promise = require('bluebird');
2
+ const fs_fi = require('./fs_fi');
3
+ const fs_path_resolve = require('./fs_path_resolve');
4
+ const fs_readdir = require('./fs_readdir');
5
+ const fs_rm = require('./fs_rm');
6
+ const fs_rmdir = require('./fs_rmdir');
7
7
 
8
8
  async function fs_rmrf(pathname)
9
9
  {
@@ -22,4 +22,4 @@ async function fs_rmrf(pathname)
22
22
  }
23
23
  }
24
24
 
25
- export default fs_rmrf;
25
+ module.exports = fs_rmrf;
package/src/fs_size.js CHANGED
@@ -1,8 +1,8 @@
1
- import fs_fi from './fs_fi';
1
+ const fs_fi = require('./fs_fi');
2
2
 
3
3
  async function fs_size(file)
4
4
  {
5
5
  return fs_fi(file).then(v => v.size);
6
6
  }
7
7
 
8
- export default fs_size;
8
+ module.exports = fs_size;
package/src/fs_tempdir.js CHANGED
@@ -1,8 +1,8 @@
1
- import fs from 'fs';
2
- import fs_path_resolve from './fs_path_resolve';
3
- import fs_path_tempdir from './fs_path_tempdir';
4
- import fs_rmrf from './fs_rmrf';
5
- import util from 'util';
1
+ const fs = require('fs');
2
+ const fs_path_resolve = require('./fs_path_resolve');
3
+ const fs_path_tempdir = require('./fs_path_tempdir');
4
+ const fs_rmrf = require('./fs_rmrf');
5
+ const util = require('util');
6
6
 
7
7
  // https://nodejs.org/api/util.html#util_util_promisify_original
8
8
  const mkdtemp = util.promisify(fs.mkdtemp);
@@ -39,4 +39,4 @@ async function fs_tempdir(fn)
39
39
  }
40
40
  }
41
41
 
42
- export default fs_tempdir;
42
+ module.exports = fs_tempdir;
package/src/fs_write.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from 'fs';
2
- import util from 'util';
1
+ const fs = require('fs');
2
+ const util = require('util');
3
3
 
4
4
  // https://nodejs.org/api/util.html#util_util_promisify_original
5
- export default util.promisify(fs.writeFile);
5
+ module.exports = util.promisify(fs.writeFile);
@@ -1 +1,3 @@
1
- export {createWriteStream as default} from 'fs';
1
+ const fs = require('fs');
2
+
3
+ module.exports = fs.createWriteStream;
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
1
+ const axios = require('axios');
2
2
 
3
3
  function http_delete(url, options)
4
4
  {
5
5
  return axios.delete(url, {responseType: 'json', ...options}).then(v => v.data);
6
6
  }
7
7
 
8
- export default http_delete;
8
+ module.exports = http_delete;
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
1
+ const axios = require('axios');
2
2
 
3
3
  function http_get_blob(url, options)
4
4
  {
5
5
  return axios.get(url, {responseType: 'blob', ...options}).then(v => v.data);
6
6
  }
7
7
 
8
- export default http_get_blob;
8
+ module.exports = http_get_blob;
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
1
+ const axios = require('axios');
2
2
 
3
3
  function http_get_buffer(url, options)
4
4
  {
5
5
  return axios.get(url, {responseType: 'arraybuffer', ...options}).then(v => v.data);
6
6
  }
7
7
 
8
- export default http_get_buffer;
8
+ module.exports = http_get_buffer;
@@ -1,9 +1,9 @@
1
- import * as fs from 'fs';
2
- import * as stream from 'stream';
3
- import axios from 'axios';
4
- import {promisify} from 'util';
1
+ const axios = require('axios');
2
+ const fs = require('fs');
3
+ const stream = require('stream');
4
+ const util = require('util');
5
5
 
6
- const stream_finished = promisify(stream.finished);
6
+ const stream_finished = util.promisify(stream.finished);
7
7
 
8
8
  // https://stackoverflow.com/a/61269447
9
9
  function http_get_file(url, out_file, options)
@@ -14,4 +14,4 @@ function http_get_file(url, out_file, options)
14
14
  });
15
15
  }
16
16
 
17
- export default http_get_file;
17
+ module.exports = http_get_file;
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
1
+ const axios = require('axios');
2
2
 
3
3
  function http_get_json(url, options)
4
4
  {
5
5
  return axios.get(url, {responseType: 'json', ...options}).then(v => v.data);
6
6
  }
7
7
 
8
- export default http_get_json;
8
+ module.exports = http_get_json;
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
1
+ const axios = require('axios');
2
2
 
3
3
  function http_patch_json(url, body, options)
4
4
  {
5
5
  return axios.patch(url, body, options).then(v => v.data);
6
6
  }
7
7
 
8
- export default http_patch_json;
8
+ module.exports = http_patch_json;
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
1
+ const axios = require('axios');
2
2
 
3
3
  function http_post_json(url, body, options)
4
4
  {
5
5
  return axios.post(url, body, options).then(v => v.data);
6
6
  }
7
7
 
8
- export default http_post_json;
8
+ module.exports = http_post_json;
@@ -1,5 +1,5 @@
1
- import FormData from 'form-data';
2
- import axios from 'axios';
1
+ const FormData = require('form-data');
2
+ const axios = require('axios');
3
3
 
4
4
  // Provide examples for the following common tasks:
5
5
  //
@@ -60,4 +60,4 @@ function http_post_multipart(url, items, options)
60
60
  return Promise.resolve(axios.post(url, form, options)).then(v => v.data);
61
61
  }
62
62
 
63
- export default http_post_multipart;
63
+ module.exports = http_post_multipart;
@@ -1,9 +1,9 @@
1
- import axios from 'axios';
2
- import qs from 'querystring';
1
+ const axios = require('axios');
2
+ const qs = require('querystring');
3
3
 
4
4
  function http_post_urlencoded(url, body, options)
5
5
  {
6
6
  return axios.post(url, qs.stringify(body), options).then(v => v.data);
7
7
  }
8
8
 
9
- export default http_post_urlencoded;
9
+ module.exports = http_post_urlencoded;
@@ -1,7 +1,7 @@
1
- import fs from 'fs';
2
- import fs_size from './fs_size';
3
- import http from 'http';
4
- import https from 'https';
1
+ const fs = require('fs');
2
+ const fs_size = require('./fs_size');
3
+ const http = require('http');
4
+ const https = require('https');
5
5
 
6
6
  async function http_put_file(url, file, options = {})
7
7
  {
@@ -17,7 +17,7 @@ async function http_put_file(url, file, options = {})
17
17
  const total = await fs_size(file);
18
18
  return new Promise(function (resolve, reject) {
19
19
  const chunks = [];
20
- const request = (url.startsWith('http:') ? http : https).request(url, {method: 'PUT', headers: {'Content-Length': total}}, function (response) {
20
+ const request = (url.startsWith('http:') ? http : https).request(url, {method: 'PUT', headers: {...options.headers, 'Content-Length': total}}, function (response) {
21
21
  response.on('error', reject);
22
22
  if (options.progress_download) {
23
23
  const total_download = response.headers['content-length'] ? parseInt(response.headers['content-length']) : null;
@@ -57,4 +57,4 @@ async function http_put_file(url, file, options = {})
57
57
  });
58
58
  }
59
59
 
60
- export default http_put_file;
60
+ module.exports = http_put_file;
@@ -1,8 +1,8 @@
1
- import axios from 'axios';
1
+ const axios = require('axios');
2
2
 
3
3
  function http_put_json(url, body, options)
4
4
  {
5
5
  return axios.put(url, body, options).then(v => v.data);
6
6
  }
7
7
 
8
- export default http_put_json;
8
+ module.exports = http_put_json;
package/src/identity.js CHANGED
@@ -3,4 +3,4 @@ function identity(value)
3
3
  return value;
4
4
  }
5
5
 
6
- export default identity;
6
+ module.exports = identity;
package/src/ignore.js CHANGED
@@ -2,4 +2,4 @@ function ignore()
2
2
  {
3
3
  }
4
4
 
5
- export default ignore;
5
+ module.exports = ignore;
@@ -1,3 +1,3 @@
1
- import json_stringify_stable from 'json-stable-stringify';
1
+ const json_stringify_stable = require('json-stable-stringify');
2
2
 
3
- export default json_stringify_stable;
3
+ module.exports = json_stringify_stable;
package/src/now_atom.js CHANGED
@@ -9,4 +9,4 @@ function now_atom()
9
9
  return (new Date()).toJSON();
10
10
  }
11
11
 
12
- export default now_atom;
12
+ module.exports = now_atom;
@@ -0,0 +1,6 @@
1
+ function random_int(min, max)
2
+ {
3
+ return Math.floor(Math.random()*(max - min + 1) + min);
4
+ }
5
+
6
+ module.exports = random_int;
@@ -0,0 +1,21 @@
1
+ const Redis = require('redis');
2
+
3
+ /**
4
+ * tls://11.11.11.11:6378?ip-google=11.11.11.11&ip-external=22.22.22.22&password=xx-xx-xx&ssl[verify_peer]=0&ssl[verify_peer_name]=0
5
+ *
6
+ * @param url
7
+ * @returns {*}
8
+ */
9
+ function redis_connect(url)
10
+ {
11
+ if (url.match(/^tls:/)) {
12
+ return Redis.createClient(url.replace(/^tls:/, 'rediss:'), {
13
+ tls: {
14
+ rejectUnauthorized: false,
15
+ },
16
+ });
17
+ }
18
+ return Redis.createClient(url.replace(/^tcp:/, 'redis:'));
19
+ }
20
+
21
+ module.exports = redis_connect;
@@ -0,0 +1,8 @@
1
+ async function redis_lshift(redis, queue, limit = 1)
2
+ {
3
+ const last = limit - 1;
4
+ const [items] = await redis.multi().lrange(queue, 0, last).ltrim(queue, limit, -1).exec_p();
5
+ return items;
6
+ }
7
+
8
+ module.exports = redis_lshift;
@@ -0,0 +1,106 @@
1
+ const Promise = require('bluebird');
2
+ const random_int = require('./random_int');
3
+ const redis_zshift = require('./redis_zshift');
4
+
5
+ async function redis_poll(options)
6
+ {
7
+ const {log, version} = options;
8
+ const {
9
+ poll_message,
10
+ push_response,
11
+ process_message,
12
+ log_waiter_begin,
13
+ log_waiter_end,
14
+ log_waiter_sleep,
15
+ log_waiter_rpush,
16
+ log_waiter_error,
17
+ log_waiter_error_parse,
18
+ log_waiter_error_no_uid,
19
+ } = options;
20
+
21
+ log(`[${log_waiter_begin}] v${version}`);
22
+ try {
23
+ for (let iter = 1; iter <= 24*60*60; ++iter) {
24
+
25
+ // const message_string = await redis.lpop_p(redis_input_queue);
26
+ const message = await poll_message();
27
+ if (!message) {
28
+ const ms = random_int(500, 1500);
29
+ log(`[${log_waiter_sleep}] ${ms}ms`);
30
+ await Promise.delay(ms);
31
+ continue;
32
+ }
33
+
34
+ try {
35
+ const uid = message.uid;
36
+
37
+ if (!uid) {
38
+ log(`[${log_waiter_error_no_uid}] uid is missed; skipped`);
39
+ continue;
40
+ }
41
+ if (!message.expires_at) {
42
+ throw new Error('expires_at is missed');
43
+ }
44
+
45
+ // This is necessary because we use signed urls which has limited lifetime.
46
+ // Basically, `expires_at` tell when signed url will be expired.
47
+ if (new Date(message.expires_at).getTime() <= new Date().getTime()) {
48
+ throw new Error('Expired');
49
+ }
50
+
51
+ const value = await worker(log.spawn(), message, options);
52
+ await push_response({uid: message.uid, version, type: 'resolve', value});
53
+
54
+ log(`[${log_waiter_rpush}]`);
55
+ }
56
+ catch (error) {
57
+ const response = {uid: message.uid, version, type: 'reject', value: `${(error && (error.stack || error.message)) || 'Error N/A'}`};
58
+ log(`[${log_waiter_error}] ${JSON.stringify({response})}`);
59
+ await push_response(response);
60
+ }
61
+ }
62
+ }
63
+ finally {
64
+ log(`[${log_waiter_end}]`);
65
+ }
66
+ }
67
+
68
+ async function worker(log, message, options)
69
+ {
70
+ const {redis, redis_output_queue, spawn_command, version} = options;
71
+ const {
72
+ callback,
73
+ log_worker_begin,
74
+ log_worker_end_ok,
75
+ log_worker_end_error,
76
+ log_worker_stdout,
77
+ log_worker_stderr,
78
+ log_worker_user_friendly_status,
79
+ } = options;
80
+
81
+ log(`[${log_worker_begin}] ${message.uid}`);
82
+
83
+ const uid = message.uid;
84
+
85
+ try {
86
+ // GOTCHA
87
+ // Promise won't update its status until at least one message would
88
+ // be taken from output queue. Pushing generic "Started...." message
89
+ // will tell the promise to refresh its status.
90
+ await user_friendly_status('Started...');
91
+ const out = await callback({message, log: log.spawn(), user_friendly_status});
92
+ log(`[${log_worker_end_ok}]`);
93
+ return out;
94
+ }
95
+ catch (error) {
96
+ log(`[${log_worker_end_error}] ${error.message}`);
97
+ throw error;
98
+ }
99
+
100
+ async function user_friendly_status(value) {
101
+ log(`[${log_worker_user_friendly_status}] ${value}`);
102
+ await redis.rpush_p(redis_output_queue, JSON.stringify({uid, version, type: 'user_friendly_status', value}));
103
+ }
104
+ }
105
+
106
+ module.exports = redis_poll;