agora-foundation 3.8.1 → 3.9.0-alpha

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 (91) hide show
  1. package/lib/decorator/log/handler.d.ts +1 -1
  2. package/lib/decorator/log/handler.js +57 -64
  3. package/lib/decorator/log/index.d.ts +26 -1
  4. package/lib/decorator/log/index.js +50 -5
  5. package/lib/decorator/log/log-serializer.d.ts +1 -1
  6. package/lib/decorator/log/log-serializer.js +15 -2
  7. package/lib/decorator/proxy.d.ts +1 -1
  8. package/lib/decorator/proxy.js +16 -16
  9. package/lib/decorator/type.d.ts +3 -2
  10. package/lib/decorator/type.js +2 -2
  11. package/lib/utilities/desc-type.d.ts +1 -0
  12. package/lib/utilities/desc-type.js +1 -1
  13. package/lib/utilities/env.d.ts +2 -1
  14. package/lib/utilities/env.js +1 -0
  15. package/lib/utilities/{agora-error.d.ts → error/agora-error.d.ts} +5 -11
  16. package/lib/utilities/error/agora-error.js +148 -0
  17. package/lib/utilities/error/error-code.d.ts +69 -0
  18. package/lib/utilities/error/error-code.js +81 -0
  19. package/lib/utilities/logger.d.ts +13 -1
  20. package/lib/utilities/logger.js +103 -13
  21. package/lib/utilities/observable.js +1 -0
  22. package/lib/utilities/package-info.d.ts +8 -0
  23. package/lib/utilities/package-info.js +85 -0
  24. package/lib/utilities/regex.d.ts +1 -0
  25. package/lib/utilities/regex.js +14 -0
  26. package/lib/utilities/tools.d.ts +28 -0
  27. package/lib/utilities/tools.js +154 -0
  28. package/lib/worker/worker-factory.js +22 -1
  29. package/lib-es/decorator/bound/index.js +12 -0
  30. package/lib-es/decorator/index.js +4 -0
  31. package/lib-es/decorator/lodash/index.js +34 -0
  32. package/lib-es/decorator/log/handler.js +71 -0
  33. package/lib-es/decorator/log/index.js +73 -0
  34. package/lib-es/decorator/log/log-serializer.js +28 -0
  35. package/lib-es/decorator/proxy.js +149 -0
  36. package/lib-es/decorator/type.js +7 -0
  37. package/lib-es/decorator/validate/index.js +32 -0
  38. package/lib-es/logger/common.js +45 -0
  39. package/lib-es/logger/constants.js +3 -0
  40. package/lib-es/logger/hijack.js +61 -0
  41. package/lib-es/logger/index.js +52 -0
  42. package/lib-es/logger/logger-impl.js +122 -0
  43. package/lib-es/logger/manager-impl.js +63 -0
  44. package/lib-es/logger/type.js +7 -0
  45. package/lib-es/restful-scheduler/scheduler.js +224 -0
  46. package/lib-es/schedule/index.js +3 -0
  47. package/lib-es/schedule/scheduler.js +102 -0
  48. package/lib-es/schedule/task.js +159 -0
  49. package/lib-es/upload/index.js +1 -0
  50. package/lib-es/upload/scheduler.js +145 -0
  51. package/lib-es/upload/type.js +1 -0
  52. package/lib-es/utilities/async-retry.js +177 -0
  53. package/lib-es/utilities/browser.js +20 -0
  54. package/lib-es/utilities/desc-type.js +3 -0
  55. package/lib-es/utilities/env.js +55 -0
  56. package/lib-es/utilities/error/agora-error.js +140 -0
  57. package/lib-es/utilities/error/error-code.js +74 -0
  58. package/lib-es/utilities/events.js +125 -0
  59. package/lib-es/utilities/file-reader.js +15 -0
  60. package/lib-es/utilities/interceptor.js +23 -0
  61. package/lib-es/utilities/logger.js +109 -0
  62. package/lib-es/utilities/misc.js +74 -0
  63. package/lib-es/utilities/observable.js +97 -0
  64. package/lib-es/utilities/package-info.js +78 -0
  65. package/lib-es/utilities/race-condition.js +17 -0
  66. package/lib-es/utilities/regex.js +7 -0
  67. package/lib-es/utilities/safely-remove-child.js +29 -0
  68. package/lib-es/utilities/schema.js +36 -0
  69. package/lib-es/utilities/tools.js +144 -0
  70. package/lib-es/utilities/value-check.js +3 -0
  71. package/lib-es/utilities/zip.js +200 -0
  72. package/lib-es/worker/constants.js +13 -0
  73. package/lib-es/worker/handler/binary.js +157 -0
  74. package/lib-es/worker/handler/db.js +34 -0
  75. package/lib-es/worker/handler/fs-log.js +149 -0
  76. package/lib-es/worker/handler/log-rotate-new.js +230 -0
  77. package/lib-es/worker/handler/log-rotate.js +80 -0
  78. package/lib-es/worker/handler/log.js +291 -0
  79. package/lib-es/worker/handler/reply.js +15 -0
  80. package/lib-es/worker/index.js +8 -0
  81. package/lib-es/worker/interactor.js +435 -0
  82. package/lib-es/worker/mutex.js +57 -0
  83. package/lib-es/worker/type.js +1 -0
  84. package/lib-es/worker/worker-entry.js +3 -0
  85. package/lib-es/worker/worker-factory.js +15 -0
  86. package/lib-es/worker/worker-installer.js +71 -0
  87. package/package.json +8 -4
  88. package/lib/utilities/agora-error.js +0 -128
  89. package/lib/worker/545.worker-factory.js +0 -1
  90. package/lib/worker/847.worker-factory.js +0 -2
  91. package/lib/worker/847.worker-factory.js.LICENSE.txt +0 -24
@@ -0,0 +1,61 @@
1
+ import "core-js/modules/es.symbol.js";
2
+ import "core-js/modules/es.array.push.js";
3
+ import "core-js/modules/es.object.define-properties.js";
4
+ import "core-js/modules/es.object.define-property.js";
5
+ import "core-js/modules/es.object.get-own-property-descriptor.js";
6
+ import "core-js/modules/es.object.get-own-property-descriptors.js";
7
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
8
+ import "core-js/modules/es.array.filter.js";
9
+ import "core-js/modules/es.array.for-each.js";
10
+ import "core-js/modules/es.array.index-of.js";
11
+ import "core-js/modules/es.object.keys.js";
12
+ import "core-js/modules/es.object.to-string.js";
13
+ import "core-js/modules/esnext.iterator.constructor.js";
14
+ import "core-js/modules/esnext.iterator.filter.js";
15
+ import "core-js/modules/esnext.iterator.for-each.js";
16
+ import "core-js/modules/web.dom-collections.for-each.js";
17
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
18
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
+ var methods = ['info', 'error', 'warn', 'debug'];
20
+ export var originConsole = window.console;
21
+ export var setupConsoleHijack = function setupConsoleHijack(logger) {
22
+ var console = _objectSpread({}, originConsole);
23
+ //@ts-ignore
24
+ if (console.__hijackSetup) {
25
+ console.log('[logger] hijack already setup!');
26
+ return;
27
+ }
28
+ console.log("[logger] setup hijack..");
29
+ function proxy(methodName) {
30
+ return function () {
31
+ switch (methodName) {
32
+ case 'debug':
33
+ logger.debug.apply(logger, arguments);
34
+ break;
35
+ case 'info':
36
+ logger.info.apply(logger, arguments);
37
+ break;
38
+ case 'warn':
39
+ logger.warn.apply(logger, arguments);
40
+ break;
41
+ case 'error':
42
+ logger.error.apply(logger, arguments);
43
+ break;
44
+ }
45
+ };
46
+ }
47
+ Object.keys(console).filter(function (e) {
48
+ return methods.indexOf(e) >= 0;
49
+ }).forEach(function (methodName) {
50
+ console[methodName] = proxy(methodName);
51
+ });
52
+ //@ts-ignore
53
+ console.__hijackSetup = true;
54
+ window.console = console;
55
+ };
56
+ export var restoreConsoleHijack = function restoreConsoleHijack() {
57
+ console.log("[logger] restore hijack..");
58
+ window.console = originConsole;
59
+ //@ts-ignore
60
+ console.__hijackSetup = false;
61
+ };
@@ -0,0 +1,52 @@
1
+ import "core-js/modules/es.array.for-each.js";
2
+ import "core-js/modules/es.object.to-string.js";
3
+ import "core-js/modules/esnext.iterator.constructor.js";
4
+ import "core-js/modules/esnext.iterator.for-each.js";
5
+ import "core-js/modules/web.dom-collections.for-each.js";
6
+ import isBoolean from 'lodash/isBoolean';
7
+ import { getWorkerSingleton } from '../worker';
8
+ import { loggerLabels, loggerRegistrar } from './common';
9
+ import { LoggerImpl } from './logger-impl';
10
+ import { LogManagerImpl } from './manager-impl';
11
+ import { LogLevel } from './type';
12
+ var logLevel = LogLevel.DEBUG;
13
+ export var getLogger = function getLogger() {
14
+ var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
15
+ var label = opts.label || 'agora-default';
16
+ if (!loggerRegistrar.has(label)) {
17
+ loggerLabels.add(label);
18
+ loggerRegistrar.set(label, new LoggerImpl(label, {}, {
19
+ console: true,
20
+ database: true
21
+ }, logLevel, getWorkerSingleton()));
22
+ }
23
+ return loggerRegistrar.get(label);
24
+ };
25
+ export var createLogger = function createLogger() {
26
+ var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
27
+ var label = opts.label || 'agora-default';
28
+ var databaseValue = isBoolean(opts.database) ? opts.database : true;
29
+ var consoleValue = isBoolean(opts.console) ? opts.console : true;
30
+ var logger = new LoggerImpl(label, opts, {
31
+ console: consoleValue,
32
+ database: databaseValue
33
+ }, logLevel, getWorkerSingleton());
34
+ loggerLabels.add(label);
35
+ return logger;
36
+ };
37
+ var logManager;
38
+ export var getLogManager = function getLogManager() {
39
+ if (!logManager) {
40
+ logManager = new LogManagerImpl(getWorkerSingleton());
41
+ }
42
+ return logManager;
43
+ };
44
+ export var setLogLevel = function setLogLevel(_logLevel) {
45
+ logLevel = _logLevel;
46
+ loggerRegistrar.forEach(function (logger) {
47
+ logger.setLogLevel(logLevel);
48
+ });
49
+ };
50
+ export { LogLevel } from './type';
51
+ export { restoreConsoleHijack, setupConsoleHijack } from './hijack';
52
+ export { logLevelMap } from './constants';
@@ -0,0 +1,122 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import "core-js/modules/es.array.concat.js";
5
+ import "core-js/modules/es.array.join.js";
6
+ import "core-js/modules/es.array.unshift.js";
7
+ import upperCase from 'lodash/upperCase';
8
+ import dayjs from 'dayjs';
9
+ import { logLevelMap } from './constants';
10
+ import { LogLevel } from './type';
11
+ import { originConsole } from './hijack';
12
+ import padEnd from 'lodash/padEnd';
13
+ import { isElectron } from '../utilities/env';
14
+ export var LoggerImpl = /*#__PURE__*/function () {
15
+ function LoggerImpl(_label, _opts, _dest, _logLevel, _logWorkerInteractor) {
16
+ _classCallCheck(this, LoggerImpl);
17
+ this._label = _label;
18
+ this._opts = _opts;
19
+ this._dest = _dest;
20
+ this._logLevel = _logLevel;
21
+ this._logWorkerInteractor = _logWorkerInteractor;
22
+ if (isElectron() && _dest.database) {
23
+ if (window.require) {
24
+ var path = window.require('path');
25
+ var _window$require = window.require('@electron/remote'),
26
+ app = _window$require.app;
27
+ var logFolderPath = path.resolve(app.getPath('logs'), 'logs');
28
+ var maxFiles = _opts.maxFiles || 5;
29
+ _logWorkerInteractor.addFsLogger(_label, logFolderPath, maxFiles);
30
+ } else {
31
+ this.warn('window.require is not present, logWorkerInteractor.addFsLogger cannot proceed');
32
+ }
33
+ }
34
+ }
35
+ return _createClass(LoggerImpl, [{
36
+ key: "setLogLevel",
37
+ value: function setLogLevel(logLevel) {
38
+ this._logLevel = logLevel;
39
+ }
40
+ }, {
41
+ key: "debug",
42
+ value: function debug() {
43
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
44
+ args[_key] = arguments[_key];
45
+ }
46
+ this._log.apply(this, [LogLevel.DEBUG].concat(args));
47
+ }
48
+ }, {
49
+ key: "info",
50
+ value: function info() {
51
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
52
+ args[_key2] = arguments[_key2];
53
+ }
54
+ this._log.apply(this, [LogLevel.INFO].concat(args));
55
+ }
56
+ }, {
57
+ key: "warn",
58
+ value: function warn() {
59
+ for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
60
+ args[_key3] = arguments[_key3];
61
+ }
62
+ this._log.apply(this, [LogLevel.WARN].concat(args));
63
+ }
64
+ }, {
65
+ key: "error",
66
+ value: function error() {
67
+ for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
68
+ args[_key4] = arguments[_key4];
69
+ }
70
+ this._log.apply(this, [LogLevel.ERROR].concat(args));
71
+ }
72
+ }, {
73
+ key: "_log",
74
+ value: function _log(logLevel) {
75
+ if (logLevel > this._logLevel) {
76
+ return;
77
+ }
78
+
79
+ // prepend prefix to the log message if it exists
80
+ for (var _len5 = arguments.length, args = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
81
+ args[_key5 - 1] = arguments[_key5];
82
+ }
83
+ if (this._opts.prefix) {
84
+ args.unshift("".concat(this._opts.prefix));
85
+ }
86
+ // prepend log level to second position of the log message
87
+ args.unshift(padEnd(upperCase(logLevelMap[logLevel] || 'unknown'), 5, ' '));
88
+ // prepend log level to first position of the log message
89
+ args.unshift(dayjs().format('YYYY-MM-DD HH:mm:ss.SSS'));
90
+ if (this._dest.database) {
91
+ this._pipeToDatabase(args.join(' '));
92
+ }
93
+ if (this._dest.console) {
94
+ this._pipeToConsole(logLevel, args);
95
+ }
96
+ }
97
+ }, {
98
+ key: "_pipeToDatabase",
99
+ value: function _pipeToDatabase(message) {
100
+ this._logWorkerInteractor.sendLog(message, this._label);
101
+ }
102
+ }, {
103
+ key: "_pipeToConsole",
104
+ value: function _pipeToConsole(logLevel, args) {
105
+ var console = originConsole;
106
+ switch (logLevel) {
107
+ case LogLevel.DEBUG:
108
+ console.debug.apply(console, _toConsumableArray(args));
109
+ break;
110
+ case LogLevel.INFO:
111
+ console.info.apply(console, _toConsumableArray(args));
112
+ break;
113
+ case LogLevel.WARN:
114
+ console.warn.apply(console, _toConsumableArray(args));
115
+ break;
116
+ case LogLevel.ERROR:
117
+ console.error.apply(console, _toConsumableArray(args));
118
+ break;
119
+ }
120
+ }
121
+ }]);
122
+ }();
@@ -0,0 +1,63 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
5
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
6
+ import "core-js/modules/es.array.from.js";
7
+ import "core-js/modules/es.string.iterator.js";
8
+ import { loggerLabels } from './common';
9
+ export var LogManagerImpl = /*#__PURE__*/function () {
10
+ function LogManagerImpl(_logWorkerInteractor) {
11
+ _classCallCheck(this, LogManagerImpl);
12
+ _defineProperty(this, "_lastId", -1);
13
+ this._logWorkerInteractor = _logWorkerInteractor;
14
+ }
15
+ return _createClass(LogManagerImpl, [{
16
+ key: "flush",
17
+ value: function () {
18
+ var _flush = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
19
+ return _regeneratorRuntime.wrap(function (_context) {
20
+ while (1) switch (_context.prev = _context.next) {
21
+ case 0:
22
+ _context.next = 1;
23
+ return this._logWorkerInteractor.deleteLogs(Array.from(loggerLabels), this._lastId);
24
+ case 1:
25
+ case "end":
26
+ return _context.stop();
27
+ }
28
+ }, _callee, this);
29
+ }));
30
+ function flush() {
31
+ return _flush.apply(this, arguments);
32
+ }
33
+ return flush;
34
+ }()
35
+ }, {
36
+ key: "collectLogs",
37
+ value: function () {
38
+ var _collectLogs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
39
+ var _yield$this$_logWorke, file, lastId;
40
+ return _regeneratorRuntime.wrap(function (_context2) {
41
+ while (1) switch (_context2.prev = _context2.next) {
42
+ case 0:
43
+ _context2.next = 1;
44
+ return this._logWorkerInteractor.collectLogs(Array.from(loggerLabels));
45
+ case 1:
46
+ _yield$this$_logWorke = _context2.sent;
47
+ file = _yield$this$_logWorke.file;
48
+ lastId = _yield$this$_logWorke.lastId;
49
+ this._lastId = lastId;
50
+ return _context2.abrupt("return", file);
51
+ case 2:
52
+ case "end":
53
+ return _context2.stop();
54
+ }
55
+ }, _callee2, this);
56
+ }));
57
+ function collectLogs() {
58
+ return _collectLogs.apply(this, arguments);
59
+ }
60
+ return collectLogs;
61
+ }()
62
+ }]);
63
+ }();
@@ -0,0 +1,7 @@
1
+ export var LogLevel = /*#__PURE__*/function (LogLevel) {
2
+ LogLevel[LogLevel["ERROR"] = 1] = "ERROR";
3
+ LogLevel[LogLevel["WARN"] = 2] = "WARN";
4
+ LogLevel[LogLevel["INFO"] = 3] = "INFO";
5
+ LogLevel[LogLevel["DEBUG"] = 4] = "DEBUG";
6
+ return LogLevel;
7
+ }({});
@@ -0,0 +1,224 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
3
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
4
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
5
+ import _createClass from "@babel/runtime/helpers/createClass";
6
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
7
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
8
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
9
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
10
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
11
+ import "core-js/modules/es.array.find-index.js";
12
+ import "core-js/modules/es.array.for-each.js";
13
+ import "core-js/modules/es.symbol.js";
14
+ import "core-js/modules/es.symbol.description.js";
15
+ import "core-js/modules/es.symbol.iterator.js";
16
+ import "core-js/modules/es.error.cause.js";
17
+ import "core-js/modules/es.error.to-string.js";
18
+ import "core-js/modules/es.array.from.js";
19
+ import "core-js/modules/es.array.is-array.js";
20
+ import "core-js/modules/es.array.iterator.js";
21
+ import "core-js/modules/es.array.map.js";
22
+ import "core-js/modules/es.array.push.js";
23
+ import "core-js/modules/es.array.splice.js";
24
+ import "core-js/modules/es.date.now.js";
25
+ import "core-js/modules/es.array.slice.js";
26
+ import "core-js/modules/es.date.to-string.js";
27
+ import "core-js/modules/es.function.name.js";
28
+ import "core-js/modules/es.map.js";
29
+ import "core-js/modules/es.object.to-string.js";
30
+ import "core-js/modules/es.regexp.constructor.js";
31
+ import "core-js/modules/es.regexp.dot-all.js";
32
+ import "core-js/modules/es.regexp.exec.js";
33
+ import "core-js/modules/es.regexp.sticky.js";
34
+ import "core-js/modules/es.regexp.test.js";
35
+ import "core-js/modules/es.regexp.to-string.js";
36
+ import "core-js/modules/es.string.iterator.js";
37
+ import "core-js/modules/esnext.iterator.constructor.js";
38
+ import "core-js/modules/esnext.iterator.for-each.js";
39
+ import "core-js/modules/esnext.iterator.map.js";
40
+ import "core-js/modules/esnext.map.delete-all.js";
41
+ import "core-js/modules/esnext.map.emplace.js";
42
+ import "core-js/modules/esnext.map.every.js";
43
+ import "core-js/modules/esnext.map.filter.js";
44
+ import "core-js/modules/esnext.map.find.js";
45
+ import "core-js/modules/esnext.map.find-key.js";
46
+ import "core-js/modules/esnext.map.includes.js";
47
+ import "core-js/modules/esnext.map.key-of.js";
48
+ import "core-js/modules/esnext.map.map-keys.js";
49
+ import "core-js/modules/esnext.map.map-values.js";
50
+ import "core-js/modules/esnext.map.merge.js";
51
+ import "core-js/modules/esnext.map.reduce.js";
52
+ import "core-js/modules/esnext.map.some.js";
53
+ import "core-js/modules/esnext.map.update.js";
54
+ import "core-js/modules/web.dom-collections.for-each.js";
55
+ import "core-js/modules/web.dom-collections.iterator.js";
56
+ export var DEFAULT_MAX_CONCURRENT = 10;
57
+ var AgoraRequestWeightManager = /*#__PURE__*/function () {
58
+ function AgoraRequestWeightManager() {
59
+ _classCallCheck(this, AgoraRequestWeightManager);
60
+ _defineProperty(this, "_rules", new Map());
61
+ }
62
+ return _createClass(AgoraRequestWeightManager, [{
63
+ key: "getWeight",
64
+ value: function getWeight(url) {
65
+ var _iterator = _createForOfIteratorHelper(this._rules),
66
+ _step;
67
+ try {
68
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
69
+ var _step$value = _slicedToArray(_step.value, 2),
70
+ pattern = _step$value[0],
71
+ weight = _step$value[1];
72
+ if (pattern.test(url)) {
73
+ return weight;
74
+ }
75
+ }
76
+ } catch (err) {
77
+ _iterator.e(err);
78
+ } finally {
79
+ _iterator.f();
80
+ }
81
+ return 0;
82
+ }
83
+ }, {
84
+ key: "addRule",
85
+ value: function addRule(pattern, weight) {
86
+ if (typeof pattern === 'string') {
87
+ this._rules.set(new RegExp(pattern), weight);
88
+ } else {
89
+ this._rules.set(pattern, weight);
90
+ }
91
+ }
92
+ }]);
93
+ }();
94
+ var AgoraPriorityQueue = /*#__PURE__*/function () {
95
+ function AgoraPriorityQueue() {
96
+ _classCallCheck(this, AgoraPriorityQueue);
97
+ _defineProperty(this, "_items", []);
98
+ }
99
+ return _createClass(AgoraPriorityQueue, [{
100
+ key: "enqueue",
101
+ value: function enqueue(item) {
102
+ this._items.push(item);
103
+ }
104
+ }, {
105
+ key: "dequeue",
106
+ value: function dequeue(timeoutThreshold) {
107
+ var _this = this;
108
+ var now = Date.now();
109
+ var taskIndex = this._items.findIndex(function (item) {
110
+ return now - (item === null || item === void 0 ? void 0 : item.addedAt) > timeoutThreshold;
111
+ });
112
+ if (taskIndex !== -1) {
113
+ return this._items.splice(taskIndex, 1)[0];
114
+ }
115
+ taskIndex = this._items.findIndex(function (item) {
116
+ return (item === null || item === void 0 ? void 0 : item.weight) === Math.max.apply(Math, _toConsumableArray(_this._items.map(function (item) {
117
+ return item === null || item === void 0 ? void 0 : item.weight;
118
+ })));
119
+ });
120
+ if (taskIndex !== -1) {
121
+ return this._items.splice(taskIndex, 1)[0];
122
+ }
123
+ return undefined;
124
+ }
125
+ }]);
126
+ }();
127
+ export var AgoraRequestScheduler = /*#__PURE__*/function () {
128
+ // in ms
129
+
130
+ function AgoraRequestScheduler(configs) {
131
+ var _rules$size,
132
+ _this2 = this;
133
+ _classCallCheck(this, AgoraRequestScheduler);
134
+ _defineProperty(this, "_maxConcurrent", DEFAULT_MAX_CONCURRENT);
135
+ _defineProperty(this, "_runningCount", 0);
136
+ _defineProperty(this, "_queueControl", new AgoraPriorityQueue());
137
+ _defineProperty(this, "_weightManager", new AgoraRequestWeightManager());
138
+ _defineProperty(this, "_timeoutThreshold", 5000);
139
+ var _ref = configs !== null && configs !== void 0 ? configs : {},
140
+ rules = _ref.rules,
141
+ maxConcurrent = _ref.maxConcurrent;
142
+ if (maxConcurrent && typeof maxConcurrent === 'number') {
143
+ this._maxConcurrent = maxConcurrent;
144
+ }
145
+ if (((_rules$size = rules === null || rules === void 0 ? void 0 : rules.size) !== null && _rules$size !== void 0 ? _rules$size : 0) > 0) {
146
+ rules === null || rules === void 0 || rules.forEach(function (v, k) {
147
+ if ((k instanceof RegExp || typeof k === 'string') && typeof v === 'number') {
148
+ _this2._weightManager.addRule(k, v);
149
+ }
150
+ });
151
+ }
152
+ }
153
+ return _createClass(AgoraRequestScheduler, [{
154
+ key: "addTask",
155
+ value: function addTask(url, executor) {
156
+ var task = {
157
+ url: url,
158
+ weight: this._weightManager.getWeight(url),
159
+ addedAt: Date.now(),
160
+ executor: executor
161
+ };
162
+ this._queueControl.enqueue(task);
163
+ this._executeTask();
164
+ }
165
+ }, {
166
+ key: "_isIdle",
167
+ value: function _isIdle() {
168
+ if (this._runningCount < this._maxConcurrent) {
169
+ return true;
170
+ }
171
+ return false;
172
+ }
173
+ }, {
174
+ key: "_executeTask",
175
+ value: function () {
176
+ var _executeTask2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
177
+ var task, _t;
178
+ return _regeneratorRuntime.wrap(function (_context) {
179
+ while (1) switch (_context.prev = _context.next) {
180
+ case 0:
181
+ if (this._isIdle()) {
182
+ _context.next = 1;
183
+ break;
184
+ }
185
+ return _context.abrupt("return");
186
+ case 1:
187
+ task = this._queueControl.dequeue(this._timeoutThreshold);
188
+ if (!task) {
189
+ _context.next = 6;
190
+ break;
191
+ }
192
+ this._runningCount++;
193
+ _context.prev = 2;
194
+ _context.next = 3;
195
+ return task.executor();
196
+ case 3:
197
+ _context.next = 5;
198
+ break;
199
+ case 4:
200
+ _context.prev = 4;
201
+ _t = _context["catch"](2);
202
+ case 5:
203
+ _context.prev = 5;
204
+ this._onTaskComplete();
205
+ return _context.finish(5);
206
+ case 6:
207
+ case "end":
208
+ return _context.stop();
209
+ }
210
+ }, _callee, this, [[2, 4, 5, 6]]);
211
+ }));
212
+ function _executeTask() {
213
+ return _executeTask2.apply(this, arguments);
214
+ }
215
+ return _executeTask;
216
+ }()
217
+ }, {
218
+ key: "_onTaskComplete",
219
+ value: function _onTaskComplete() {
220
+ this._runningCount--;
221
+ this._executeTask();
222
+ }
223
+ }]);
224
+ }();
@@ -0,0 +1,3 @@
1
+ import * as _AgoraScheduler from "./scheduler";
2
+ export { _AgoraScheduler as AgoraScheduler };
3
+ export { ActionWhenTaskFail } from "./task";
@@ -0,0 +1,102 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ import "core-js/modules/es.array.iterator.js";
5
+ import "core-js/modules/es.object.to-string.js";
6
+ import "core-js/modules/es.string.iterator.js";
7
+ import "core-js/modules/es.weak-set.js";
8
+ import "core-js/modules/esnext.weak-set.add-all.js";
9
+ import "core-js/modules/esnext.weak-set.delete-all.js";
10
+ import "core-js/modules/web.dom-collections.iterator.js";
11
+ import { ActionWhenTaskFail } from './task';
12
+ import { IntervalTask, PollingTask } from './task';
13
+ export var Duration = /*#__PURE__*/function () {
14
+ function Duration() {
15
+ _classCallCheck(this, Duration);
16
+ }
17
+ return _createClass(Duration, null, [{
18
+ key: "second",
19
+ value: function second(amount) {
20
+ return this._factors.second * amount;
21
+ }
22
+ }, {
23
+ key: "minute",
24
+ value: function minute(amount) {
25
+ return this._factors.minute * amount;
26
+ }
27
+ }, {
28
+ key: "hour",
29
+ value: function hour(amount) {
30
+ return this._factors.hour * amount;
31
+ }
32
+ }]);
33
+ }();
34
+ _defineProperty(Duration, "_factors", {
35
+ hour: 1000 * 60 * 60,
36
+ minute: 1000 * 60,
37
+ second: 1000
38
+ });
39
+ var Scheduler = /*#__PURE__*/function () {
40
+ function Scheduler() {
41
+ _classCallCheck(this, Scheduler);
42
+ _defineProperty(this, "_tasks", new WeakSet());
43
+ }
44
+ return _createClass(Scheduler, [{
45
+ key: "addPollingTask",
46
+ value: function addPollingTask(runnable, intervalInMs) {
47
+ var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
48
+ var onFail = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
49
+ return ActionWhenTaskFail.CONTINUE;
50
+ };
51
+ var task = new PollingTask({
52
+ runnable: runnable,
53
+ interval: intervalInMs,
54
+ immediate: immediate,
55
+ onFail: onFail
56
+ });
57
+ task.start();
58
+ this._tasks.add(task);
59
+ return task;
60
+ }
61
+ }, {
62
+ key: "addDelayTask",
63
+ value: function addDelayTask(runnable, delayInMs) {
64
+ var _this = this;
65
+ var task = new PollingTask({
66
+ runnable: runnable,
67
+ interval: delayInMs,
68
+ once: true,
69
+ onFail: function onFail() {
70
+ return ActionWhenTaskFail.EXIT;
71
+ },
72
+ onComplete: function onComplete() {
73
+ _this._tasks["delete"](task);
74
+ }
75
+ });
76
+ task.start();
77
+ this._tasks.add(task);
78
+ return task;
79
+ }
80
+ }, {
81
+ key: "addIntervalTask",
82
+ value: function addIntervalTask(runnable, intervalInMs) {
83
+ var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
84
+ var onFail = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
85
+ return ActionWhenTaskFail.CONTINUE;
86
+ };
87
+ var task = new IntervalTask({
88
+ runnable: runnable,
89
+ interval: intervalInMs,
90
+ immediate: immediate,
91
+ onFail: onFail
92
+ });
93
+ task.start();
94
+ this._tasks.add(task);
95
+ return task;
96
+ }
97
+ }]);
98
+ }();
99
+ export var createScheduler = function createScheduler() {
100
+ return new Scheduler();
101
+ };
102
+ export var shared = createScheduler();