plugin.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Plugin = void 0;
  4. const tslib_1 = require("tslib");
  5. const tronweb_js_1 = require("../tronweb.js");
  6. const index_js_1 = tslib_1.__importDefault(require("../utils/index.js"));
  7. const semver_1 = tslib_1.__importDefault(require("semver"));
  8. class Plugin {
  9. tronWeb;
  10. pluginNoOverride;
  11. disablePlugins;
  12. constructor(tronWeb, options = {}) {
  13. if (!tronWeb || !(tronWeb instanceof tronweb_js_1.TronWeb))
  14. throw new Error('Expected instance of TronWeb');
  15. this.tronWeb = tronWeb;
  16. this.pluginNoOverride = ['register'];
  17. this.disablePlugins = !!options.disablePlugins;
  18. }
  19. register(Plugin, options) {
  20. let pluginInterface = {
  21. requires: '0.0.0',
  22. components: {},
  23. };
  24. const result = {
  25. libs: [],
  26. plugged: [],
  27. skipped: [],
  28. error: undefined,
  29. };
  30. if (this.disablePlugins) {
  31. result.error = 'This instance of TronWeb has plugins disabled.';
  32. return result;
  33. }
  34. const plugin = new Plugin(this.tronWeb);
  35. if (index_js_1.default.isFunction(plugin.pluginInterface)) {
  36. pluginInterface = plugin.pluginInterface(options);
  37. }
  38. if (semver_1.default.satisfies(tronweb_js_1.TronWeb.version, pluginInterface.requires)) {
  39. if (pluginInterface.fullClass) {
  40. // plug the entire class at the same level of tronWeb.trx
  41. const className = plugin.constructor.name;
  42. const classInstanceName = className.substring(0, 1).toLowerCase() + className.substring(1);
  43. if (className !== classInstanceName) {
  44. Object.assign(tronweb_js_1.TronWeb, {
  45. [className]: Plugin,
  46. });
  47. Object.assign(this.tronWeb, {
  48. [classInstanceName]: plugin,
  49. });
  50. result.libs.push(className);
  51. }
  52. }
  53. else {
  54. // plug methods into a class, like trx
  55. for (const component in pluginInterface.components) {
  56. // eslint-disable-next-line no-prototype-builtins
  57. if (!this.tronWeb.hasOwnProperty(component)) {
  58. continue;
  59. }
  60. const methods = pluginInterface.components[component];
  61. const pluginNoOverride = this.tronWeb[component].pluginNoOverride || [];
  62. for (const method in methods) {
  63. if (method === 'constructor' ||
  64. (this.tronWeb[component][method] &&
  65. (pluginNoOverride.includes(method) || // blacklisted methods
  66. /^_/.test(method))) // private methods
  67. ) {
  68. result.skipped.push(method);
  69. continue;
  70. }
  71. this.tronWeb[component][method] = methods[method].bind(this.tronWeb[component]);
  72. result.plugged.push(method);
  73. }
  74. }
  75. }
  76. }
  77. else {
  78. throw new Error('The plugin is not compatible with this version of TronWeb');
  79. }
  80. return result;
  81. }
  82. }
  83. exports.Plugin = Plugin;
  84. //# sourceMappingURL=plugin.js.map