@xyo-network/image-thumbnail-plugin 2.74.5 → 2.75.2

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 (195) hide show
  1. package/dist/docs.json +1299 -1299
  2. package/dist/node/Diviner/Config.d.cts.map +1 -0
  3. package/dist/node/Diviner/Config.d.mts.map +1 -0
  4. package/dist/node/Diviner/Config.d.ts +11 -0
  5. package/dist/node/Diviner/Config.d.ts.map +1 -0
  6. package/dist/node/Diviner/Config.js +32 -0
  7. package/dist/node/Diviner/Config.js.map +1 -0
  8. package/dist/node/Diviner/Config.mjs +7 -0
  9. package/dist/node/Diviner/Config.mjs.map +1 -0
  10. package/dist/node/Diviner/Diviner.d.cts.map +1 -0
  11. package/dist/node/Diviner/Diviner.d.mts.map +1 -0
  12. package/dist/node/Diviner/Diviner.d.ts +30 -0
  13. package/dist/node/Diviner/Diviner.d.ts.map +1 -0
  14. package/dist/node/Diviner/Diviner.js +207 -0
  15. package/dist/node/Diviner/Diviner.js.map +1 -0
  16. package/dist/node/Diviner/Diviner.mjs +182 -0
  17. package/dist/node/Diviner/Diviner.mjs.map +1 -0
  18. package/dist/node/Diviner/Params.d.cts.map +1 -0
  19. package/dist/node/Diviner/Params.d.mts.map +1 -0
  20. package/dist/node/Diviner/Params.d.ts +5 -0
  21. package/dist/node/Diviner/Params.d.ts.map +1 -0
  22. package/dist/node/Diviner/Params.js +19 -0
  23. package/dist/node/Diviner/Params.js.map +1 -0
  24. package/dist/node/Diviner/Params.mjs +1 -0
  25. package/dist/node/Diviner/Params.mjs.map +1 -0
  26. package/dist/node/Diviner/index.d.cts.map +1 -0
  27. package/dist/node/Diviner/index.d.mts.map +1 -0
  28. package/dist/node/Diviner/index.d.ts +4 -0
  29. package/dist/node/Diviner/index.d.ts.map +1 -0
  30. package/dist/node/Diviner/index.js +209 -0
  31. package/dist/node/Diviner/index.js.map +1 -0
  32. package/dist/node/Diviner/index.mjs +181 -0
  33. package/dist/node/Diviner/index.mjs.map +1 -0
  34. package/dist/node/Plugin.d.cts.map +1 -0
  35. package/dist/node/Plugin.d.mts.map +1 -0
  36. package/dist/node/Plugin.d.ts +116 -0
  37. package/dist/node/Plugin.d.ts.map +1 -0
  38. package/dist/node/Plugin.js +556 -0
  39. package/dist/{index.mjs.map → node/Plugin.js.map} +1 -1
  40. package/dist/node/Plugin.mjs +521 -0
  41. package/dist/node/Plugin.mjs.map +1 -0
  42. package/dist/node/Witness/Config.d.cts.map +1 -0
  43. package/dist/node/Witness/Config.d.mts.map +1 -0
  44. package/dist/node/Witness/Config.d.ts +16 -0
  45. package/dist/node/Witness/Config.d.ts.map +1 -0
  46. package/dist/node/Witness/Config.js +32 -0
  47. package/dist/node/Witness/Config.js.map +1 -0
  48. package/dist/node/Witness/Config.mjs +7 -0
  49. package/dist/node/Witness/Config.mjs.map +1 -0
  50. package/dist/node/Witness/Params.d.cts.map +1 -0
  51. package/dist/node/Witness/Params.d.mts.map +1 -0
  52. package/dist/node/Witness/Params.d.ts +7 -0
  53. package/dist/node/Witness/Params.d.ts.map +1 -0
  54. package/dist/node/Witness/Params.js +19 -0
  55. package/dist/node/Witness/Params.js.map +1 -0
  56. package/dist/node/Witness/Params.mjs +1 -0
  57. package/dist/node/Witness/Params.mjs.map +1 -0
  58. package/dist/node/Witness/Witness.d.cts.map +1 -0
  59. package/dist/node/Witness/Witness.d.mts.map +1 -0
  60. package/dist/node/Witness/Witness.d.ts +66 -0
  61. package/dist/node/Witness/Witness.d.ts.map +1 -0
  62. package/dist/node/Witness/Witness.js +359 -0
  63. package/dist/node/Witness/Witness.js.map +1 -0
  64. package/dist/node/Witness/Witness.mjs +324 -0
  65. package/dist/node/Witness/Witness.mjs.map +1 -0
  66. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.cts.map +1 -0
  67. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.mts.map +1 -0
  68. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts +8 -0
  69. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts.map +1 -0
  70. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js +77 -0
  71. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js.map +1 -0
  72. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.mjs +42 -0
  73. package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.mjs.map +1 -0
  74. package/dist/node/Witness/ffmpeg/fluent/index.d.cts.map +1 -0
  75. package/dist/node/Witness/ffmpeg/fluent/index.d.mts.map +1 -0
  76. package/dist/node/Witness/ffmpeg/fluent/index.d.ts +2 -0
  77. package/dist/node/Witness/ffmpeg/fluent/index.d.ts.map +1 -0
  78. package/dist/node/Witness/ffmpeg/fluent/index.js +79 -0
  79. package/dist/node/Witness/ffmpeg/fluent/index.js.map +1 -0
  80. package/dist/node/Witness/ffmpeg/fluent/index.mjs +42 -0
  81. package/dist/node/Witness/ffmpeg/fluent/index.mjs.map +1 -0
  82. package/dist/node/Witness/ffmpeg/index.d.cts.map +1 -0
  83. package/dist/node/Witness/ffmpeg/index.d.mts.map +1 -0
  84. package/dist/node/Witness/ffmpeg/index.d.ts +3 -0
  85. package/dist/node/Witness/ffmpeg/index.d.ts.map +1 -0
  86. package/dist/node/Witness/ffmpeg/index.js +109 -0
  87. package/dist/node/Witness/ffmpeg/index.js.map +1 -0
  88. package/dist/node/Witness/ffmpeg/index.mjs +70 -0
  89. package/dist/node/Witness/ffmpeg/index.mjs.map +1 -0
  90. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.d.cts.map +1 -0
  91. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.d.mts.map +1 -0
  92. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.d.ts +9 -0
  93. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.d.ts.map +1 -0
  94. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.js +48 -0
  95. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.js.map +1 -0
  96. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.mjs +23 -0
  97. package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.mjs.map +1 -0
  98. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.cts.map +1 -0
  99. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.mts.map +1 -0
  100. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts +8 -0
  101. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts.map +1 -0
  102. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.js +56 -0
  103. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.js.map +1 -0
  104. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.mjs +29 -0
  105. package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.mjs.map +1 -0
  106. package/dist/node/Witness/ffmpeg/spawn/index.d.cts.map +1 -0
  107. package/dist/node/Witness/ffmpeg/spawn/index.d.mts.map +1 -0
  108. package/dist/node/Witness/ffmpeg/spawn/index.d.ts +3 -0
  109. package/dist/node/Witness/ffmpeg/spawn/index.d.ts.map +1 -0
  110. package/dist/node/Witness/ffmpeg/spawn/index.js +58 -0
  111. package/dist/node/Witness/ffmpeg/spawn/index.js.map +1 -0
  112. package/dist/node/Witness/ffmpeg/spawn/index.mjs +30 -0
  113. package/dist/node/Witness/ffmpeg/spawn/index.mjs.map +1 -0
  114. package/dist/node/Witness/index.d.cts.map +1 -0
  115. package/dist/node/Witness/index.d.mts.map +1 -0
  116. package/dist/node/Witness/index.d.ts +4 -0
  117. package/dist/node/Witness/index.d.ts.map +1 -0
  118. package/dist/node/Witness/index.js +363 -0
  119. package/dist/node/Witness/index.js.map +1 -0
  120. package/dist/node/Witness/index.mjs +325 -0
  121. package/dist/node/Witness/index.mjs.map +1 -0
  122. package/dist/node/index.d.cts.map +1 -0
  123. package/dist/node/index.d.mts.map +1 -0
  124. package/dist/node/index.d.ts +6 -0
  125. package/dist/node/index.d.ts.map +1 -0
  126. package/dist/{index.js → node/index.js} +15 -10
  127. package/dist/node/index.js.map +1 -0
  128. package/dist/{index.mjs → node/index.mjs} +15 -10
  129. package/dist/node/index.mjs.map +1 -0
  130. package/package.json +39 -38
  131. package/dist/Diviner/Config.d.mts.map +0 -1
  132. package/dist/Diviner/Config.d.ts.map +0 -1
  133. package/dist/Diviner/Diviner.d.mts.map +0 -1
  134. package/dist/Diviner/Diviner.d.ts.map +0 -1
  135. package/dist/Diviner/Params.d.mts.map +0 -1
  136. package/dist/Diviner/Params.d.ts.map +0 -1
  137. package/dist/Diviner/index.d.mts.map +0 -1
  138. package/dist/Diviner/index.d.ts.map +0 -1
  139. package/dist/Plugin.d.mts.map +0 -1
  140. package/dist/Plugin.d.ts.map +0 -1
  141. package/dist/Witness/Config.d.mts.map +0 -1
  142. package/dist/Witness/Config.d.ts.map +0 -1
  143. package/dist/Witness/Params.d.mts.map +0 -1
  144. package/dist/Witness/Params.d.ts.map +0 -1
  145. package/dist/Witness/Witness.d.mts.map +0 -1
  146. package/dist/Witness/Witness.d.ts.map +0 -1
  147. package/dist/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.mts.map +0 -1
  148. package/dist/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts.map +0 -1
  149. package/dist/Witness/ffmpeg/fluent/index.d.mts.map +0 -1
  150. package/dist/Witness/ffmpeg/fluent/index.d.ts.map +0 -1
  151. package/dist/Witness/ffmpeg/index.d.mts.map +0 -1
  152. package/dist/Witness/ffmpeg/index.d.ts.map +0 -1
  153. package/dist/Witness/ffmpeg/spawn/executeFfmpeg.d.mts.map +0 -1
  154. package/dist/Witness/ffmpeg/spawn/executeFfmpeg.d.ts.map +0 -1
  155. package/dist/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.mts.map +0 -1
  156. package/dist/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts.map +0 -1
  157. package/dist/Witness/ffmpeg/spawn/index.d.mts.map +0 -1
  158. package/dist/Witness/ffmpeg/spawn/index.d.ts.map +0 -1
  159. package/dist/Witness/index.d.mts.map +0 -1
  160. package/dist/Witness/index.d.ts.map +0 -1
  161. package/dist/index.d.mts.map +0 -1
  162. package/dist/index.d.ts.map +0 -1
  163. package/dist/index.js.map +0 -1
  164. /package/dist/{Diviner/Config.d.mts → node/Diviner/Config.d.cts} +0 -0
  165. /package/dist/{Diviner/Config.d.ts → node/Diviner/Config.d.mts} +0 -0
  166. /package/dist/{Diviner/Diviner.d.mts → node/Diviner/Diviner.d.cts} +0 -0
  167. /package/dist/{Diviner/Diviner.d.ts → node/Diviner/Diviner.d.mts} +0 -0
  168. /package/dist/{Diviner/Params.d.mts → node/Diviner/Params.d.cts} +0 -0
  169. /package/dist/{Diviner/Params.d.ts → node/Diviner/Params.d.mts} +0 -0
  170. /package/dist/{Diviner/index.d.mts → node/Diviner/index.d.cts} +0 -0
  171. /package/dist/{Diviner/index.d.ts → node/Diviner/index.d.mts} +0 -0
  172. /package/dist/{Plugin.d.mts → node/Plugin.d.cts} +0 -0
  173. /package/dist/{Plugin.d.ts → node/Plugin.d.mts} +0 -0
  174. /package/dist/{Witness/Config.d.mts → node/Witness/Config.d.cts} +0 -0
  175. /package/dist/{Witness/Config.d.ts → node/Witness/Config.d.mts} +0 -0
  176. /package/dist/{Witness/Params.d.mts → node/Witness/Params.d.cts} +0 -0
  177. /package/dist/{Witness/Params.d.ts → node/Witness/Params.d.mts} +0 -0
  178. /package/dist/{Witness/Witness.d.mts → node/Witness/Witness.d.cts} +0 -0
  179. /package/dist/{Witness/Witness.d.ts → node/Witness/Witness.d.mts} +0 -0
  180. /package/dist/{Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.mts → node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.cts} +0 -0
  181. /package/dist/{Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts → node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.mts} +0 -0
  182. /package/dist/{Witness/ffmpeg/fluent/index.d.mts → node/Witness/ffmpeg/fluent/index.d.cts} +0 -0
  183. /package/dist/{Witness/ffmpeg/fluent/index.d.ts → node/Witness/ffmpeg/fluent/index.d.mts} +0 -0
  184. /package/dist/{Witness/ffmpeg/index.d.mts → node/Witness/ffmpeg/index.d.cts} +0 -0
  185. /package/dist/{Witness/ffmpeg/index.d.ts → node/Witness/ffmpeg/index.d.mts} +0 -0
  186. /package/dist/{Witness/ffmpeg/spawn/executeFfmpeg.d.mts → node/Witness/ffmpeg/spawn/executeFfmpeg.d.cts} +0 -0
  187. /package/dist/{Witness/ffmpeg/spawn/executeFfmpeg.d.ts → node/Witness/ffmpeg/spawn/executeFfmpeg.d.mts} +0 -0
  188. /package/dist/{Witness/ffmpeg/spawn/getVideoFrameAsImage.d.mts → node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.cts} +0 -0
  189. /package/dist/{Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts → node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.mts} +0 -0
  190. /package/dist/{Witness/ffmpeg/spawn/index.d.mts → node/Witness/ffmpeg/spawn/index.d.cts} +0 -0
  191. /package/dist/{Witness/ffmpeg/spawn/index.d.ts → node/Witness/ffmpeg/spawn/index.d.mts} +0 -0
  192. /package/dist/{Witness/index.d.mts → node/Witness/index.d.cts} +0 -0
  193. /package/dist/{Witness/index.d.ts → node/Witness/index.d.mts} +0 -0
  194. /package/dist/{index.d.mts → node/index.d.cts} +0 -0
  195. /package/dist/{index.d.ts → node/index.d.mts} +0 -0
@@ -0,0 +1,556 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/Plugin.ts
31
+ var Plugin_exports = {};
32
+ __export(Plugin_exports, {
33
+ ImageThumbnailPlugin: () => ImageThumbnailPlugin
34
+ });
35
+ module.exports = __toCommonJS(Plugin_exports);
36
+ var import_image_thumbnail_payload_plugin5 = require("@xyo-network/image-thumbnail-payload-plugin");
37
+ var import_payload_model = require("@xyo-network/payload-model");
38
+ var import_payloadset_plugin = require("@xyo-network/payloadset-plugin");
39
+
40
+ // src/Diviner/Config.ts
41
+ var import_image_thumbnail_payload_plugin = require("@xyo-network/image-thumbnail-payload-plugin");
42
+ var ImageThumbnailDivinerConfigSchema = `${import_image_thumbnail_payload_plugin.ImageThumbnailSchema}.diviner.config`;
43
+
44
+ // src/Diviner/Diviner.ts
45
+ var import_assert = require("@xylabs/assert");
46
+ var import_delay = require("@xylabs/delay");
47
+ var import_lodash = require("@xylabs/lodash");
48
+ var import_abstract_diviner = require("@xyo-network/abstract-diviner");
49
+ var import_archivist_model = require("@xyo-network/archivist-model");
50
+ var import_core = require("@xyo-network/core");
51
+ var import_diviner_model = require("@xyo-network/diviner-model");
52
+ var import_diviner_payload_model = require("@xyo-network/diviner-payload-model");
53
+ var import_image_thumbnail_payload_plugin2 = require("@xyo-network/image-thumbnail-payload-plugin");
54
+ var ImageThumbnailDiviner = class extends import_abstract_diviner.AbstractDiviner {
55
+ static configSchemas = [ImageThumbnailDivinerConfigSchema, import_diviner_model.DivinerConfigSchema];
56
+ _archivistInstance;
57
+ _initializeArchivistConnectionIfNeededPromise;
58
+ _map;
59
+ _payloadDivinerInstance;
60
+ _pollId;
61
+ // static override get configSchema() {
62
+ // return ImageThumbnailDivinerConfigSchema
63
+ // }
64
+ get archivist() {
65
+ return this.config.archivist;
66
+ }
67
+ get payloadDiviner() {
68
+ return this.config.payloadDiviner;
69
+ }
70
+ get payloadDivinerLimit() {
71
+ return this.config.payloadDivinerLimit ?? 1e4;
72
+ }
73
+ get pollFrequency() {
74
+ return this.config.pollFrequency;
75
+ }
76
+ //using promise as mutex
77
+ async getArchivistInstance() {
78
+ if (this._archivistInstance && !await this._archivistInstance) {
79
+ this._archivistInstance = void 0;
80
+ }
81
+ this._archivistInstance = this._archivistInstance ?? (async () => {
82
+ const module2 = this.archivist ? await this.resolve(this.archivist) : void 0;
83
+ return (0, import_archivist_model.asArchivistInstance)(module2, "Provided archivist address did not resolve to an Archivist");
84
+ })();
85
+ return this._archivistInstance;
86
+ }
87
+ //using promise as mutex
88
+ async getPayloadDivinerInstance() {
89
+ const payloadDivinerAddress = this.payloadDiviner;
90
+ if (payloadDivinerAddress) {
91
+ if (this._payloadDivinerInstance && !await this._payloadDivinerInstance) {
92
+ this._payloadDivinerInstance = void 0;
93
+ }
94
+ this._payloadDivinerInstance = this._payloadDivinerInstance ?? (async () => {
95
+ const module2 = await this.resolve(payloadDivinerAddress);
96
+ return (0, import_diviner_model.asDivinerInstance)(module2, "Provided payload diviner address did not resolve to a Diviner");
97
+ })();
98
+ return this._payloadDivinerInstance;
99
+ }
100
+ }
101
+ async divineHandler(payloads = []) {
102
+ await this.initializeArchivistConnectionIfNeeded();
103
+ const urls = payloads.map((urlPayload) => urlPayload.url);
104
+ const map = await this.getSafeMap();
105
+ const archivist = await this.getArchivistInstance();
106
+ const hashes = (0, import_lodash.compact)(urls.map((url) => map == null ? void 0 : map[url]));
107
+ return (await archivist.get(hashes)).filter((payload) => payload.schema === import_image_thumbnail_payload_plugin2.ImageThumbnailSchema);
108
+ }
109
+ //using promise as mutex
110
+ initializeArchivistConnectionIfNeeded() {
111
+ this._initializeArchivistConnectionIfNeededPromise = this._initializeArchivistConnectionIfNeededPromise ?? (async () => {
112
+ if (!this._map) {
113
+ await this.attachArchivistEvents();
114
+ console.log("initializeArchivistConnectionIfNeeded: attachArchivistEvents done");
115
+ await this.poll();
116
+ console.log("initializeArchivistConnectionIfNeeded: poll done");
117
+ }
118
+ })();
119
+ return this._initializeArchivistConnectionIfNeededPromise;
120
+ }
121
+ async loadMap() {
122
+ if (this.payloadDiviner) {
123
+ return await this.loadMapWithPayloadDiviner();
124
+ } else {
125
+ return await this.loadMapWithAll();
126
+ }
127
+ }
128
+ async loadMapWithAll() {
129
+ var _a;
130
+ if (await this.started()) {
131
+ const archivist = await this.getArchivistInstance();
132
+ (0, import_assert.assertEx)(archivist.all, "Archivist does not support 'all'");
133
+ const allPayloads = await ((_a = archivist.all) == null ? void 0 : _a.call(archivist)) ?? [];
134
+ const imagePayloadPairs = await Promise.all(
135
+ allPayloads.filter((payload) => payload.schema === import_image_thumbnail_payload_plugin2.ImageThumbnailSchema).map(async (payload) => [await import_core.PayloadHasher.hashAsync(payload), payload])
136
+ );
137
+ this._map = imagePayloadPairs.reduce((prev, [hash, payload]) => {
138
+ prev[payload.sourceUrl] = hash;
139
+ return prev;
140
+ }, {});
141
+ }
142
+ }
143
+ async loadMapWithPayloadDiviner() {
144
+ console.log("loadMapWithPayloadDiviner: started");
145
+ if (await this.started()) {
146
+ const diviner = await this.getPayloadDivinerInstance();
147
+ let offset = void 0;
148
+ let moreAvailable = true;
149
+ if (diviner) {
150
+ const newMap = {};
151
+ while (moreAvailable) {
152
+ const payloadDivinerQuery = {
153
+ limit: this.payloadDivinerLimit,
154
+ offset,
155
+ schema: import_diviner_payload_model.PayloadDivinerQuerySchema,
156
+ schemas: [import_image_thumbnail_payload_plugin2.ImageThumbnailSchema]
157
+ };
158
+ const payloads = await diviner.divine([payloadDivinerQuery]);
159
+ offset = (offset ?? 0) + payloads.length;
160
+ moreAvailable = payloads.length > 0;
161
+ console.log(`loadMapWithPayloadDiviner.offset: ${offset}`);
162
+ console.log(`loadMapWithPayloadDiviner.moreAvailable: ${moreAvailable}`);
163
+ const imagePayloadPairs = await Promise.all(
164
+ payloads.filter((payload) => payload.schema === import_image_thumbnail_payload_plugin2.ImageThumbnailSchema).map(async (payload) => [await import_core.PayloadHasher.hashAsync(payload), payload])
165
+ );
166
+ imagePayloadPairs.forEach(([hash, payload]) => newMap[payload.sourceUrl] = hash);
167
+ }
168
+ this._map = newMap;
169
+ }
170
+ }
171
+ }
172
+ async stopHandler(_timeout) {
173
+ if (this._pollId) {
174
+ clearTimeout(this._pollId);
175
+ this._pollId = void 0;
176
+ }
177
+ return await super.stopHandler();
178
+ }
179
+ async attachArchivistEvents() {
180
+ const archivist = await this.getArchivistInstance();
181
+ const mapPromise = this.getSafeMap();
182
+ archivist.on("inserted", async ({ payloads }) => {
183
+ const map = await mapPromise;
184
+ const thumbnails = (0, import_lodash.compact)(payloads.filter((payload) => payload.schema === import_image_thumbnail_payload_plugin2.ImageThumbnailSchema));
185
+ await Promise.all(thumbnails.map(async (payload) => map[payload.sourceUrl] = await import_core.PayloadHasher.hashAsync(payload)));
186
+ });
187
+ }
188
+ async getSafeMap() {
189
+ let mapRetry = 100;
190
+ let map = this._map;
191
+ while (!map) {
192
+ await (0, import_delay.delay)(100);
193
+ mapRetry = mapRetry - 1;
194
+ if (mapRetry === 0) {
195
+ throw Error("Map Not Loaded");
196
+ }
197
+ map = this._map;
198
+ }
199
+ return map;
200
+ }
201
+ async poll() {
202
+ if (await this.started()) {
203
+ const pollFrequency = this.pollFrequency;
204
+ if (pollFrequency) {
205
+ this._pollId = setTimeout(async () => {
206
+ this._pollId = void 0;
207
+ await this.loadMap();
208
+ await this.poll();
209
+ }, pollFrequency);
210
+ } else {
211
+ await this.loadMap();
212
+ }
213
+ }
214
+ }
215
+ };
216
+
217
+ // src/Witness/Config.ts
218
+ var import_image_thumbnail_payload_plugin3 = require("@xyo-network/image-thumbnail-payload-plugin");
219
+ var ImageThumbnailWitnessConfigSchema = `${import_image_thumbnail_payload_plugin3.ImageThumbnailSchema}.witness.config`;
220
+
221
+ // src/Witness/Witness.ts
222
+ var import_node_dns = require("dns");
223
+ var import_lodash2 = require("@xylabs/lodash");
224
+ var import_url = require("@xylabs/url");
225
+ var import_axios = require("@xyo-network/axios");
226
+ var import_core3 = require("@xyo-network/core");
227
+ var import_image_thumbnail_payload_plugin4 = require("@xyo-network/image-thumbnail-payload-plugin");
228
+ var import_url_payload_plugin = require("@xyo-network/url-payload-plugin");
229
+ var import_witness = require("@xyo-network/witness");
230
+ var import_async_mutex = require("async-mutex");
231
+ var import_file_type = __toESM(require("file-type"));
232
+ var import_gm = __toESM(require("gm"));
233
+ var import_hasbin = __toESM(require("hasbin"));
234
+ var import_hash_wasm = require("hash-wasm");
235
+ var import_lru_cache = require("lru-cache");
236
+ var import_sha = __toESM(require("sha.js"));
237
+ var import_url_parse = __toESM(require("url-parse"));
238
+
239
+ // src/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.ts
240
+ var import_core2 = require("@xyo-network/core");
241
+ var import_fluent_ffmpeg = __toESM(require("fluent-ffmpeg"));
242
+ var import_promises = require("fs/promises");
243
+ var import_os = require("os");
244
+ var import_stream = require("stream");
245
+ var FfmpegOutputStream = class extends import_stream.Writable {
246
+ chunks = [];
247
+ constructor(options) {
248
+ super(options);
249
+ }
250
+ _write(chunk, _encoding, callback) {
251
+ this.chunks.push(chunk);
252
+ callback();
253
+ }
254
+ /**
255
+ * Collects the output from ffmpeg into a buffer.
256
+ * @returns A buffer containing the concatenated
257
+ * output from ffmpeg.
258
+ */
259
+ toBuffer = () => Buffer.concat(this.chunks);
260
+ };
261
+ var getVideoFrameAsImageFluent = async (videoBuffer) => {
262
+ const tmpFile = `/${(0, import_os.tmpdir)()}/${(0, import_core2.uuid)()}`;
263
+ try {
264
+ await (0, import_promises.writeFile)(tmpFile, videoBuffer, { encoding: "binary" });
265
+ const imageBuffer = await new Promise((resolve, reject) => {
266
+ const ffmpegOutput = new FfmpegOutputStream();
267
+ (0, import_fluent_ffmpeg.default)().on("error", (err) => reject(err.message)).on("end", () => resolve(ffmpegOutput.toBuffer())).input(tmpFile).takeFrames(1).withNoAudio().outputOptions("-f image2pipe").videoCodec("png").pipe(ffmpegOutput);
268
+ });
269
+ return imageBuffer;
270
+ } finally {
271
+ try {
272
+ await (0, import_promises.unlink)(tmpFile);
273
+ } catch {
274
+ }
275
+ }
276
+ };
277
+
278
+ // src/Witness/Witness.ts
279
+ var gm = import_gm.default.subClass({ imageMagick: "7+" });
280
+ var ImageThumbnailWitness = class _ImageThumbnailWitness extends import_witness.AbstractWitness {
281
+ static configSchemas = [ImageThumbnailWitnessConfigSchema];
282
+ _cache;
283
+ _semaphore = new import_async_mutex.Semaphore(this.maxAsyncProcesses);
284
+ get cache() {
285
+ this._cache = this._cache ?? new import_lru_cache.LRUCache({
286
+ max: this.maxCacheEntries,
287
+ maxSize: this.maxCacheBytes,
288
+ //just returning the size of the data
289
+ sizeCalculation: (value) => {
290
+ var _a;
291
+ return ((_a = value.url) == null ? void 0 : _a.length) ?? 1;
292
+ }
293
+ });
294
+ return this._cache;
295
+ }
296
+ get encoding() {
297
+ return this.config.encoding ?? "PNG";
298
+ }
299
+ get height() {
300
+ return this.config.height ?? 128;
301
+ }
302
+ get ipfGateway() {
303
+ return this.config.ipfsGateway ?? "5d7b6582.beta.decentralnetworkservices.com";
304
+ }
305
+ get maxAsyncProcesses() {
306
+ return this.config.maxAsyncProcesses ?? 2;
307
+ }
308
+ get maxCacheBytes() {
309
+ return this.config.maxCacheBytes ?? 1024 * 1024 * 16;
310
+ }
311
+ get maxCacheEntries() {
312
+ return this.config.maxCacheEntries ?? 500;
313
+ }
314
+ get quality() {
315
+ return this.config.quality ?? 50;
316
+ }
317
+ get width() {
318
+ return this.config.width ?? 128;
319
+ }
320
+ static async binaryToSha256(data) {
321
+ await import_core3.PayloadHasher.wasmInitialized;
322
+ if (import_core3.PayloadHasher.wasmSupport.canUseWasm) {
323
+ try {
324
+ return await (0, import_hash_wasm.sha256)(data);
325
+ } catch (ex) {
326
+ import_core3.PayloadHasher.wasmSupport.allowWasm = false;
327
+ }
328
+ }
329
+ return (0, import_sha.default)("sha256").update(data).digest().toString();
330
+ }
331
+ static bufferFromDataUrl(url) {
332
+ if (url.startsWith("data:image")) {
333
+ const data = url.split(",")[1];
334
+ if (data) {
335
+ return Buffer.from(Uint8Array.from(atob(data), (c) => c.charCodeAt(0)));
336
+ } else {
337
+ const error = {
338
+ message: "Invalid data Url",
339
+ name: "ImageThumbnailWitnessError",
340
+ url
341
+ };
342
+ throw error;
343
+ }
344
+ }
345
+ }
346
+ /**
347
+ * Returns the equivalent IPFS gateway URL for the supplied URL.
348
+ * @param urlToCheck The URL to check
349
+ * @returns If the supplied URL is an IPFS URL, it converts the URL to the
350
+ * equivalent IPFS gateway URL. Otherwise, returns the original URL.
351
+ */
352
+ checkIpfsUrl(urlToCheck) {
353
+ const url = new import_url.URL(urlToCheck);
354
+ let protocol = url.protocol;
355
+ let host = url.host;
356
+ let path = url.pathname;
357
+ const query = url.search;
358
+ if (protocol === "ipfs:") {
359
+ protocol = "https:";
360
+ host = this.ipfGateway;
361
+ path = url.host === "ipfs" ? `ipfs${path}` : `ipfs/${url.host}${path}`;
362
+ const root = `${protocol}//${host}/${path}`;
363
+ return (query == null ? void 0 : query.length) > 0 ? `${root}?${query}` : root;
364
+ } else {
365
+ return urlToCheck;
366
+ }
367
+ }
368
+ async observeHandler(payloads = []) {
369
+ if (!import_hasbin.default.sync("magick")) {
370
+ throw Error("ImageMagick is required for this witness");
371
+ }
372
+ const urlPayloads = payloads.filter((payload) => payload.schema === import_url_payload_plugin.UrlSchema);
373
+ return await this._semaphore.runExclusive(
374
+ async () => (0, import_lodash2.compact)(
375
+ await Promise.all(
376
+ urlPayloads.map(async ({ url }) => {
377
+ const cachedResult = this.cache.get(url);
378
+ if (cachedResult) {
379
+ return cachedResult;
380
+ }
381
+ let result;
382
+ const dataBuffer = _ImageThumbnailWitness.bufferFromDataUrl(url);
383
+ if (dataBuffer) {
384
+ result = {
385
+ schema: import_image_thumbnail_payload_plugin4.ImageThumbnailSchema,
386
+ sourceHash: await _ImageThumbnailWitness.binaryToSha256(dataBuffer),
387
+ sourceUrl: url,
388
+ url
389
+ };
390
+ } else {
391
+ const mutatedUrl = this.checkIpfsUrl(url);
392
+ result = await this.fromHttp(mutatedUrl, url);
393
+ }
394
+ this.cache.set(url, result);
395
+ return result;
396
+ })
397
+ )
398
+ )
399
+ );
400
+ }
401
+ async createThumbnailDataUrl(sourceBuffer, encoding) {
402
+ const thumb = await new Promise((resolve, reject) => {
403
+ gm(sourceBuffer).quality(this.quality).resize(this.width, this.height).flatten().toBuffer(encoding ?? this.encoding, (error, buffer) => {
404
+ if (error) {
405
+ reject(error);
406
+ } else {
407
+ resolve(buffer);
408
+ }
409
+ });
410
+ });
411
+ return `data:image/png;base64,${thumb.toString("base64")}`;
412
+ }
413
+ /**
414
+ * Creates an image thumbnail from a video.
415
+ * @param videoBuffer The input video buffer.
416
+ * @returns An buffer containing an image thumbnail for the video.
417
+ */
418
+ async createThumbnailFromVideo(videoBuffer) {
419
+ const imageBuffer = await getVideoFrameAsImageFluent(videoBuffer);
420
+ return this.createThumbnailDataUrl(imageBuffer);
421
+ }
422
+ async fromHttp(url, sourceUrl) {
423
+ var _a, _b, _c, _d, _e, _f;
424
+ let response;
425
+ let dnsResult;
426
+ try {
427
+ const urlObj = new import_url_parse.default(url);
428
+ dnsResult = await import_node_dns.promises.resolve(urlObj.host);
429
+ } catch (ex) {
430
+ const error = ex;
431
+ const result2 = {
432
+ http: {
433
+ dnsError: error.code
434
+ },
435
+ schema: import_image_thumbnail_payload_plugin4.ImageThumbnailSchema,
436
+ sourceUrl: sourceUrl ?? url
437
+ };
438
+ return result2;
439
+ }
440
+ try {
441
+ response = await import_axios.axios.get(url, {
442
+ responseType: "arraybuffer"
443
+ });
444
+ } catch (ex) {
445
+ const axiosError = ex;
446
+ if (axiosError.isAxiosError) {
447
+ const result2 = {
448
+ http: {
449
+ ipAddress: dnsResult[0],
450
+ status: (_a = axiosError == null ? void 0 : axiosError.response) == null ? void 0 : _a.status
451
+ },
452
+ schema: import_image_thumbnail_payload_plugin4.ImageThumbnailSchema,
453
+ sourceUrl: sourceUrl ?? url
454
+ };
455
+ return result2;
456
+ } else {
457
+ throw ex;
458
+ }
459
+ }
460
+ const result = {
461
+ http: {
462
+ status: response.status
463
+ },
464
+ schema: import_image_thumbnail_payload_plugin4.ImageThumbnailSchema,
465
+ sourceUrl: sourceUrl ?? url
466
+ };
467
+ if (response.status >= 200 && response.status < 300) {
468
+ const contentType = (_b = response.headers["content-type"]) == null ? void 0 : _b.toString();
469
+ const [mediaType, fileType] = contentType.split("/");
470
+ result.mime = result.mime ?? {};
471
+ result.mime.returned = mediaType;
472
+ const sourceBuffer = Buffer.from(response.data, "binary");
473
+ try {
474
+ result.mime.detected = await import_file_type.default.fromBuffer(sourceBuffer);
475
+ } catch (ex) {
476
+ const error = ex;
477
+ (_c = this.logger) == null ? void 0 : _c.error(`FileType error: ${error.message}`);
478
+ }
479
+ const processImage = async (encoding2) => {
480
+ result.sourceHash = await _ImageThumbnailWitness.binaryToSha256(sourceBuffer);
481
+ result.url = await this.createThumbnailDataUrl(sourceBuffer, encoding2);
482
+ };
483
+ const processVideo = async () => {
484
+ if (import_hasbin.default.sync("ffmpeg")) {
485
+ result.sourceHash = await _ImageThumbnailWitness.binaryToSha256(sourceBuffer);
486
+ result.url = await this.createThumbnailFromVideo(sourceBuffer);
487
+ } else {
488
+ result.mime = result.mime ?? {};
489
+ result.mime.invalid = true;
490
+ }
491
+ };
492
+ let encoding = "PNG";
493
+ switch (fileType.toUpperCase()) {
494
+ case "GIF":
495
+ encoding = "GIF";
496
+ break;
497
+ case "JPG":
498
+ case "JPEG":
499
+ encoding = "JPG";
500
+ break;
501
+ }
502
+ switch (mediaType) {
503
+ case "image": {
504
+ await processImage(encoding);
505
+ result.mime.type = mediaType;
506
+ break;
507
+ }
508
+ case "video": {
509
+ await processVideo();
510
+ result.mime.type = mediaType;
511
+ break;
512
+ }
513
+ default: {
514
+ switch ((_d = result.mime.detected) == null ? void 0 : _d.mime) {
515
+ case "image": {
516
+ await processImage();
517
+ result.mime.type = (_e = result.mime.detected) == null ? void 0 : _e.mime;
518
+ break;
519
+ }
520
+ case "video": {
521
+ await processVideo();
522
+ result.mime.type = (_f = result.mime.detected) == null ? void 0 : _f.mime;
523
+ break;
524
+ }
525
+ default: {
526
+ result.mime.invalid = true;
527
+ break;
528
+ }
529
+ }
530
+ break;
531
+ }
532
+ }
533
+ }
534
+ return result;
535
+ }
536
+ };
537
+
538
+ // src/Plugin.ts
539
+ var ImageThumbnailPlugin = () => (0, import_payloadset_plugin.createPayloadSetDualPlugin)(
540
+ { required: { [import_image_thumbnail_payload_plugin5.ImageThumbnailSchema]: 1 }, schema: import_payload_model.PayloadSetSchema },
541
+ {
542
+ diviner: async (params) => {
543
+ const result = await ImageThumbnailDiviner.create(params);
544
+ return result;
545
+ },
546
+ witness: async (params) => {
547
+ const result = await ImageThumbnailWitness.create(params);
548
+ return result;
549
+ }
550
+ }
551
+ );
552
+ // Annotate the CommonJS export names for ESM import in node:
553
+ 0 && (module.exports = {
554
+ ImageThumbnailPlugin
555
+ });
556
+ //# sourceMappingURL=Plugin.js.map