@xyo-network/image-thumbnail-plugin 2.75.5 → 2.75.7

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 (40) hide show
  1. package/dist/docs.json +4946 -4911
  2. package/dist/node/Diviner/Config.d.cts +23 -0
  3. package/dist/node/Diviner/Config.d.cts.map +1 -1
  4. package/dist/node/Diviner/Config.d.mts +23 -0
  5. package/dist/node/Diviner/Config.d.mts.map +1 -1
  6. package/dist/node/Diviner/Config.d.ts +23 -0
  7. package/dist/node/Diviner/Config.d.ts.map +1 -1
  8. package/dist/node/Diviner/Config.js.map +1 -1
  9. package/dist/node/Diviner/Config.mjs.map +1 -1
  10. package/dist/node/Diviner/Diviner.d.cts +38 -20
  11. package/dist/node/Diviner/Diviner.d.cts.map +1 -1
  12. package/dist/node/Diviner/Diviner.d.mts +38 -20
  13. package/dist/node/Diviner/Diviner.d.mts.map +1 -1
  14. package/dist/node/Diviner/Diviner.d.ts +38 -20
  15. package/dist/node/Diviner/Diviner.d.ts.map +1 -1
  16. package/dist/node/Diviner/Diviner.js +169 -139
  17. package/dist/node/Diviner/Diviner.js.map +1 -1
  18. package/dist/node/Diviner/Diviner.mjs +168 -138
  19. package/dist/node/Diviner/Diviner.mjs.map +1 -1
  20. package/dist/node/Diviner/index.js +169 -139
  21. package/dist/node/Diviner/index.js.map +1 -1
  22. package/dist/node/Diviner/index.mjs +168 -138
  23. package/dist/node/Diviner/index.mjs.map +1 -1
  24. package/dist/node/Plugin.d.cts +3 -0
  25. package/dist/node/Plugin.d.cts.map +1 -1
  26. package/dist/node/Plugin.d.mts +3 -0
  27. package/dist/node/Plugin.d.mts.map +1 -1
  28. package/dist/node/Plugin.d.ts +3 -0
  29. package/dist/node/Plugin.d.ts.map +1 -1
  30. package/dist/node/Plugin.js +175 -145
  31. package/dist/node/Plugin.js.map +1 -1
  32. package/dist/node/Plugin.mjs +172 -142
  33. package/dist/node/Plugin.mjs.map +1 -1
  34. package/dist/node/index.js +175 -145
  35. package/dist/node/index.js.map +1 -1
  36. package/dist/node/index.mjs +172 -142
  37. package/dist/node/index.mjs.map +1 -1
  38. package/package.json +22 -16
  39. package/src/Diviner/Config.ts +24 -0
  40. package/src/Diviner/Diviner.ts +233 -158
@@ -1,136 +1,188 @@
1
1
  // src/Diviner/Diviner.ts
2
2
  import { assertEx } from "@xylabs/assert";
3
- import { delay } from "@xylabs/delay";
4
- import { compact } from "@xylabs/lodash";
3
+ import { exists } from "@xylabs/exists";
5
4
  import { AbstractDiviner } from "@xyo-network/abstract-diviner";
6
- import { asArchivistInstance } from "@xyo-network/archivist-model";
5
+ import { asArchivistInstance, withArchivistModule } from "@xyo-network/archivist-model";
6
+ import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
7
+ import { isBoundWitness } from "@xyo-network/boundwitness-model";
7
8
  import { PayloadHasher } from "@xyo-network/core";
9
+ import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
8
10
  import { asDivinerInstance, DivinerConfigSchema } from "@xyo-network/diviner-model";
9
11
  import { PayloadDivinerQuerySchema } from "@xyo-network/diviner-payload-model";
10
- import { ImageThumbnailSchema as ImageThumbnailSchema2 } from "@xyo-network/image-thumbnail-payload-plugin";
12
+ import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
13
+ import { ImageThumbnailSchema as ImageThumbnailSchema2, isImageThumbnail } from "@xyo-network/image-thumbnail-payload-plugin";
14
+ import { PayloadBuilder } from "@xyo-network/payload-builder";
15
+ import { isPayloadOfSchemaType } from "@xyo-network/payload-model";
16
+ import { isUrlPayload } from "@xyo-network/url-payload-plugin";
17
+ import { isTimestamp, TimestampSchema } from "@xyo-network/witness-timestamp";
11
18
 
12
19
  // src/Diviner/Config.ts
13
20
  import { ImageThumbnailSchema } from "@xyo-network/image-thumbnail-payload-plugin";
14
21
  var ImageThumbnailDivinerConfigSchema = `${ImageThumbnailSchema}.diviner.config`;
15
22
 
16
23
  // src/Diviner/Diviner.ts
24
+ var ModuleStateSchema = "network.xyo.module.state";
25
+ var isModuleState = isPayloadOfSchemaType(ModuleStateSchema);
26
+ var ImageThumbnailResultIndexSchema = `${ImageThumbnailSchema2}.index`;
27
+ var isImageThumbnailResult = isPayloadOfSchemaType(ImageThumbnailResultIndexSchema);
28
+ var moduleName = "ImageThumbnailDiviner";
17
29
  var ImageThumbnailDiviner = class extends AbstractDiviner {
18
30
  static configSchemas = [ImageThumbnailDivinerConfigSchema, DivinerConfigSchema];
19
- _archivistInstance;
20
- _initializeArchivistConnectionIfNeededPromise;
21
- _map;
22
- _payloadDivinerInstance;
23
31
  _pollId;
24
- // static override get configSchema() {
25
- // return ImageThumbnailDivinerConfigSchema
26
- // }
27
- get archivist() {
28
- return this.config.archivist;
29
- }
30
- get payloadDiviner() {
31
- return this.config.payloadDiviner;
32
- }
33
32
  get payloadDivinerLimit() {
34
33
  return this.config.payloadDivinerLimit ?? 1e4;
35
34
  }
36
35
  get pollFrequency() {
37
- return this.config.pollFrequency;
38
- }
39
- //using promise as mutex
40
- async getArchivistInstance() {
41
- if (this._archivistInstance && !await this._archivistInstance) {
42
- this._archivistInstance = void 0;
43
- }
44
- this._archivistInstance = this._archivistInstance ?? (async () => {
45
- const module = this.archivist ? await this.resolve(this.archivist) : void 0;
46
- return asArchivistInstance(module, "Provided archivist address did not resolve to an Archivist");
47
- })();
48
- return this._archivistInstance;
36
+ return this.config.pollFrequency ?? 1e4;
49
37
  }
50
- //using promise as mutex
51
- async getPayloadDivinerInstance() {
52
- const payloadDivinerAddress = this.payloadDiviner;
53
- if (payloadDivinerAddress) {
54
- if (this._payloadDivinerInstance && !await this._payloadDivinerInstance) {
55
- this._payloadDivinerInstance = void 0;
56
- }
57
- this._payloadDivinerInstance = this._payloadDivinerInstance ?? (async () => {
58
- const module = await this.resolve(payloadDivinerAddress);
59
- return asDivinerInstance(module, "Provided payload diviner address did not resolve to a Diviner");
60
- })();
61
- return this._payloadDivinerInstance;
62
- }
38
+ backgroundDivine = async () => {
39
+ const lastState = await this.retrieveState() ?? { offset: 0 };
40
+ const { offset } = lastState;
41
+ const boundWitnessDiviner = await this.getBoundWitnessDivinerForStore("thumbnailStore");
42
+ const query = new PayloadBuilder({ schema: BoundWitnessDivinerQuerySchema }).fields({
43
+ limit: this.payloadDivinerLimit,
44
+ offset,
45
+ order: "asc",
46
+ payload_schemas: [ImageThumbnailSchema2, TimestampSchema]
47
+ });
48
+ const batch = await boundWitnessDiviner.divine([query]);
49
+ if (batch.length === 0)
50
+ return;
51
+ const imageThumbnailTimestampTuples = batch.filter(isBoundWitness).map((bw) => {
52
+ var _a, _b, _c;
53
+ const imageThumbnailIndexes = (_a = bw.payload_schemas) == null ? void 0 : _a.map((schema, index) => schema === ImageThumbnailSchema2 ? index : void 0).filter(exists);
54
+ const timestampIndex = (_b = bw.payload_schemas) == null ? void 0 : _b.findIndex((schema) => schema === TimestampSchema);
55
+ if (!imageThumbnailIndexes.length || timestampIndex === -1)
56
+ return void 0;
57
+ const imageThumbnails = bw.payload_hashes.map((hash, index) => imageThumbnailIndexes.includes(index) ? hash : void 0).filter(exists);
58
+ const timestamp = (_c = bw.payload_hashes) == null ? void 0 : _c[timestampIndex];
59
+ return imageThumbnails.map((imageThumbnail) => [imageThumbnail, timestamp]);
60
+ }).flat().filter(exists);
61
+ const archivist = await this.getArchivistForStore("thumbnailStore");
62
+ const payloadTuples = (await Promise.all(
63
+ imageThumbnailTimestampTuples.map(async ([imageThumbnailHash, timestampHash]) => {
64
+ const results = await archivist.get([imageThumbnailHash, timestampHash]);
65
+ const imageThumbnailPayload = results.find(isImageThumbnail);
66
+ const timestampPayload = results.find(isTimestamp);
67
+ if (!imageThumbnailPayload || !timestampPayload)
68
+ return void 0;
69
+ const calculatedImageThumbnailHash = await PayloadHasher.hashAsync(imageThumbnailPayload);
70
+ const calculatedTimestampHash = await PayloadHasher.hashAsync(timestampPayload);
71
+ if (imageThumbnailHash !== calculatedImageThumbnailHash || timestampHash !== calculatedTimestampHash)
72
+ return void 0;
73
+ return [imageThumbnailHash, imageThumbnailPayload, timestampHash, timestampPayload];
74
+ })
75
+ )).filter(exists);
76
+ const indexedResults = payloadTuples.map(([thumbnailHash, thumbnailPayload, timestampHash, timestampPayload]) => {
77
+ var _a;
78
+ const { sourceUrl: url } = thumbnailPayload;
79
+ const { timestamp } = timestampPayload;
80
+ const status = ((_a = thumbnailPayload.http) == null ? void 0 : _a.status) ?? -1;
81
+ const sources = [thumbnailHash, timestampHash];
82
+ const result = new PayloadBuilder({ schema: ImageThumbnailResultIndexSchema }).fields({ sources, status, timestamp, url }).build();
83
+ return result;
84
+ });
85
+ const indexArchivist = await this.getArchivistForStore("indexStore");
86
+ await indexArchivist.insert(indexedResults);
87
+ const nextOffset = offset + batch.length + 1;
88
+ const currentState = { ...lastState, offset: nextOffset };
89
+ await this.commitState(currentState);
90
+ };
91
+ /**
92
+ * Commit the internal state of the Diviner process. This is similar
93
+ * to a transaction completion in a database and should only be called
94
+ * when results have been successfully persisted to the appropriate
95
+ * external stores.
96
+ */
97
+ async commitState(state) {
98
+ var _a;
99
+ const stateStore = assertEx((_a = this.config.stateStore) == null ? void 0 : _a.archivist, `${moduleName}: No stateStore configured`);
100
+ const module = assertEx(await this.resolve(stateStore), `${moduleName}: Failed to resolve stateStore`);
101
+ await withArchivistModule(module, async (archivist) => {
102
+ const mod = ArchivistWrapper.wrap(archivist, this.account);
103
+ const payload = new PayloadBuilder({ schema: ModuleStateSchema }).fields({ state }).build();
104
+ await mod.insert([payload]);
105
+ });
63
106
  }
64
107
  async divineHandler(payloads = []) {
65
- await this.initializeArchivistConnectionIfNeeded();
66
- const urls = payloads.map((urlPayload) => urlPayload.url);
67
- const map = await this.getSafeMap();
68
- const archivist = await this.getArchivistInstance();
69
- const hashes = compact(urls.map((url) => map == null ? void 0 : map[url]));
70
- return (await archivist.get(hashes)).filter((payload) => payload.schema === ImageThumbnailSchema2);
108
+ const urls = payloads.filter(isUrlPayload).map((urlPayload) => urlPayload.url);
109
+ const diviner = await this.getPayloadDivinerForStore("indexStore");
110
+ const results = (await Promise.all(
111
+ urls.map(async (url) => {
112
+ const query = new PayloadBuilder({ schema: PayloadDivinerQuerySchema }).fields({ limit: 1, offset: 0, order: "desc", url }).build();
113
+ return await diviner.divine([query]);
114
+ })
115
+ )).flat().filter(isImageThumbnailResult);
116
+ return results;
71
117
  }
72
- //using promise as mutex
73
- initializeArchivistConnectionIfNeeded() {
74
- this._initializeArchivistConnectionIfNeededPromise = this._initializeArchivistConnectionIfNeededPromise ?? (async () => {
75
- if (!this._map) {
76
- await this.attachArchivistEvents();
77
- console.log("initializeArchivistConnectionIfNeeded: attachArchivistEvents done");
78
- await this.poll();
79
- console.log("initializeArchivistConnectionIfNeeded: poll done");
80
- }
81
- })();
82
- return this._initializeArchivistConnectionIfNeededPromise;
118
+ async getArchivistForStore(store, wrap) {
119
+ var _a, _b;
120
+ const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`);
121
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist`);
122
+ return wrap ? ArchivistWrapper.wrap(mod, this.account) : asArchivistInstance(mod, () => `${moduleName}: ${store}.archivist is not an Archivist`);
83
123
  }
84
- async loadMap() {
85
- if (this.payloadDiviner) {
86
- return await this.loadMapWithPayloadDiviner();
87
- } else {
88
- return await this.loadMapWithAll();
89
- }
124
+ async getBoundWitnessDivinerForStore(store, wrap) {
125
+ var _a, _b;
126
+ const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`);
127
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner`);
128
+ return wrap ? DivinerWrapper.wrap(mod, this.account) : asDivinerInstance(mod, () => `${moduleName}: ${store}.boundWitnessDiviner is not a Diviner`);
129
+ }
130
+ async getPayloadDivinerForStore(store, wrap) {
131
+ var _a, _b;
132
+ const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a[store]) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`);
133
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner`);
134
+ return wrap ? DivinerWrapper.wrap(mod, this.account) : asDivinerInstance(mod, () => `${moduleName}: ${store}.payloadDiviner is not a Diviner`);
90
135
  }
91
- async loadMapWithAll() {
136
+ /**
137
+ * Retrieves the last state of the Diviner process. Used to recover state after
138
+ * preemptions, reboots, etc.
139
+ */
140
+ async retrieveState() {
92
141
  var _a;
93
- if (await this.started()) {
94
- const archivist = await this.getArchivistInstance();
95
- assertEx(archivist.all, "Archivist does not support 'all'");
96
- const allPayloads = await ((_a = archivist.all) == null ? void 0 : _a.call(archivist)) ?? [];
97
- const imagePayloadPairs = await Promise.all(
98
- allPayloads.filter((payload) => payload.schema === ImageThumbnailSchema2).map(async (payload) => [await PayloadHasher.hashAsync(payload), payload])
99
- );
100
- this._map = imagePayloadPairs.reduce((prev, [hash, payload]) => {
101
- prev[payload.sourceUrl] = hash;
102
- return prev;
103
- }, {});
142
+ let hash = "";
143
+ const diviner = await this.getBoundWitnessDivinerForStore("stateStore");
144
+ const query = new PayloadBuilder({ schema: BoundWitnessDivinerQuerySchema }).fields({
145
+ address: this.account.address,
146
+ limit: 1,
147
+ offset: 0,
148
+ order: "desc",
149
+ payload_schemas: [ModuleStateSchema]
150
+ });
151
+ const boundWitnesses = await diviner.divine([query]);
152
+ if (boundWitnesses.length > 0) {
153
+ const boundWitness = boundWitnesses[0];
154
+ if (isBoundWitness(boundWitness)) {
155
+ hash = boundWitness.addresses.map((address, index) => ({ address, index })).filter(({ address }) => address === this.account.address).reduce(
156
+ (prev, curr) => {
157
+ var _a2;
158
+ return ((_a2 = boundWitness.payload_schemas) == null ? void 0 : _a2[curr == null ? void 0 : curr.index]) === ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
159
+ },
160
+ ""
161
+ );
162
+ }
104
163
  }
105
- }
106
- async loadMapWithPayloadDiviner() {
107
- console.log("loadMapWithPayloadDiviner: started");
108
- if (await this.started()) {
109
- const diviner = await this.getPayloadDivinerInstance();
110
- let offset = void 0;
111
- let moreAvailable = true;
112
- if (diviner) {
113
- const newMap = {};
114
- while (moreAvailable) {
115
- const payloadDivinerQuery = {
116
- limit: this.payloadDivinerLimit,
117
- offset,
118
- schema: PayloadDivinerQuerySchema,
119
- schemas: [ImageThumbnailSchema2]
120
- };
121
- const payloads = await diviner.divine([payloadDivinerQuery]);
122
- offset = (offset ?? 0) + payloads.length;
123
- moreAvailable = payloads.length > 0;
124
- console.log(`loadMapWithPayloadDiviner.offset: ${offset}`);
125
- console.log(`loadMapWithPayloadDiviner.moreAvailable: ${moreAvailable}`);
126
- const imagePayloadPairs = await Promise.all(
127
- payloads.filter((payload) => payload.schema === ImageThumbnailSchema2).map(async (payload) => [await PayloadHasher.hashAsync(payload), payload])
128
- );
129
- imagePayloadPairs.forEach(([hash, payload]) => newMap[payload.sourceUrl] = hash);
164
+ if (hash) {
165
+ const stateStoreArchivist = assertEx((_a = this.config.stateStore) == null ? void 0 : _a.archivist, `${moduleName}: No stateStore archivist configured`);
166
+ await withArchivistModule(
167
+ assertEx(await this.resolve(stateStoreArchivist), `${moduleName}: Failed to resolve stateStore archivist`),
168
+ async (mod) => {
169
+ const archivist = ArchivistWrapper.wrap(mod, this.account);
170
+ const payloads = await archivist.get([hash]);
171
+ if (payloads.length > 0) {
172
+ const payload = payloads[0];
173
+ if (isModuleState(payload)) {
174
+ return payload.state;
175
+ }
176
+ }
130
177
  }
131
- this._map = newMap;
132
- }
178
+ );
133
179
  }
180
+ return void 0;
181
+ }
182
+ async startHandler() {
183
+ await super.startHandler();
184
+ this.poll();
185
+ return true;
134
186
  }
135
187
  async stopHandler(_timeout) {
136
188
  if (this._pollId) {
@@ -139,41 +191,19 @@ var ImageThumbnailDiviner = class extends AbstractDiviner {
139
191
  }
140
192
  return await super.stopHandler();
141
193
  }
142
- async attachArchivistEvents() {
143
- const archivist = await this.getArchivistInstance();
144
- const mapPromise = this.getSafeMap();
145
- archivist.on("inserted", async ({ payloads }) => {
146
- const map = await mapPromise;
147
- const thumbnails = compact(payloads.filter((payload) => payload.schema === ImageThumbnailSchema2));
148
- await Promise.all(thumbnails.map(async (payload) => map[payload.sourceUrl] = await PayloadHasher.hashAsync(payload)));
149
- });
150
- }
151
- async getSafeMap() {
152
- let mapRetry = 100;
153
- let map = this._map;
154
- while (!map) {
155
- await delay(100);
156
- mapRetry = mapRetry - 1;
157
- if (mapRetry === 0) {
158
- throw Error("Map Not Loaded");
159
- }
160
- map = this._map;
161
- }
162
- return map;
163
- }
164
- async poll() {
165
- if (await this.started()) {
166
- const pollFrequency = this.pollFrequency;
167
- if (pollFrequency) {
168
- this._pollId = setTimeout(async () => {
169
- this._pollId = void 0;
170
- await this.loadMap();
171
- await this.poll();
172
- }, pollFrequency);
173
- } else {
174
- await this.loadMap();
194
+ poll() {
195
+ this._pollId = setTimeout(async () => {
196
+ try {
197
+ await this.backgroundDivine();
198
+ } catch (e) {
199
+ console.log(e);
200
+ } finally {
201
+ if (this._pollId)
202
+ clearTimeout(this._pollId);
203
+ this._pollId = void 0;
204
+ this.poll();
175
205
  }
176
- }
206
+ }, this.pollFrequency);
177
207
  }
178
208
  };
179
209
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/Diviner/Diviner.ts","../../../src/Diviner/Config.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { delay } from '@xylabs/delay'\nimport { compact } from '@xylabs/lodash'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist-model'\nimport { PayloadHasher } from '@xyo-network/core'\nimport { asDivinerInstance, DivinerConfigSchema, DivinerInstance } from '@xyo-network/diviner-model'\nimport { PayloadDivinerQueryPayload, PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'\nimport { ImageThumbnail, ImageThumbnailSchema } from '@xyo-network/image-thumbnail-payload-plugin'\nimport { UrlPayload } from '@xyo-network/url-payload-plugin'\n\nimport { ImageThumbnailDivinerConfigSchema } from './Config'\nimport { ImageThumbnailDivinerParams } from './Params'\n\nexport class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams = ImageThumbnailDivinerParams> extends AbstractDiviner<TParams> {\n static override configSchemas = [ImageThumbnailDivinerConfigSchema, DivinerConfigSchema]\n\n private _archivistInstance: Promise<ArchivistInstance> | undefined\n private _initializeArchivistConnectionIfNeededPromise: Promise<void> | undefined\n private _map: Record<string, string> | undefined\n private _payloadDivinerInstance: Promise<DivinerInstance> | undefined\n private _pollId?: string | number | NodeJS.Timeout\n\n // static override get configSchema() {\n // return ImageThumbnailDivinerConfigSchema\n // }\n\n get archivist() {\n return this.config.archivist\n }\n\n get payloadDiviner() {\n return this.config.payloadDiviner\n }\n\n get payloadDivinerLimit() {\n return this.config.payloadDivinerLimit ?? 10000\n }\n\n get pollFrequency() {\n return this.config.pollFrequency\n }\n\n //using promise as mutex\n async getArchivistInstance(): Promise<ArchivistInstance> {\n //if previously checked, but not found, clear promise\n if (this._archivistInstance && !(await this._archivistInstance)) {\n this._archivistInstance = undefined\n }\n this._archivistInstance =\n this._archivistInstance ??\n (async () => {\n const module = this.archivist ? await this.resolve(this.archivist) : undefined\n return asArchivistInstance(module, 'Provided archivist address did not resolve to an Archivist')\n })()\n return this._archivistInstance\n }\n\n //using promise as mutex\n async getPayloadDivinerInstance(): Promise<DivinerInstance | undefined> {\n const payloadDivinerAddress = this.payloadDiviner\n if (payloadDivinerAddress) {\n //if previously checked, but not found, clear promise\n if (this._payloadDivinerInstance && !(await this._payloadDivinerInstance)) {\n this._payloadDivinerInstance = undefined\n }\n this._payloadDivinerInstance =\n this._payloadDivinerInstance ??\n (async () => {\n const module = await this.resolve(payloadDivinerAddress)\n return asDivinerInstance(module, 'Provided payload diviner address did not resolve to a Diviner')\n })()\n\n return this._payloadDivinerInstance\n }\n }\n\n protected override async divineHandler(payloads: UrlPayload[] = []): Promise<ImageThumbnail[]> {\n await this.initializeArchivistConnectionIfNeeded()\n const urls = payloads.map((urlPayload) => urlPayload.url)\n const map = await this.getSafeMap()\n const archivist = await this.getArchivistInstance()\n const hashes = compact(urls.map((url) => map?.[url]))\n return (await archivist.get(hashes)).filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema)\n }\n\n //using promise as mutex\n protected initializeArchivistConnectionIfNeeded() {\n this._initializeArchivistConnectionIfNeededPromise =\n this._initializeArchivistConnectionIfNeededPromise ??\n (async () => {\n if (!this._map) {\n await this.attachArchivistEvents()\n console.log('initializeArchivistConnectionIfNeeded: attachArchivistEvents done')\n await this.poll()\n console.log('initializeArchivistConnectionIfNeeded: poll done')\n }\n })()\n return this._initializeArchivistConnectionIfNeededPromise\n }\n\n protected async loadMap() {\n if (this.payloadDiviner) {\n return await this.loadMapWithPayloadDiviner()\n } else {\n return await this.loadMapWithAll()\n }\n }\n\n protected async loadMapWithAll() {\n if (await this.started()) {\n const archivist = await this.getArchivistInstance()\n assertEx(archivist.all, \"Archivist does not support 'all'\")\n const allPayloads = (await archivist.all?.()) ?? []\n const imagePayloadPairs = await Promise.all(\n allPayloads\n .filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema)\n .map<Promise<[string, ImageThumbnail]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),\n )\n this._map = imagePayloadPairs.reduce<Record<string, string>>((prev, [hash, payload]) => {\n prev[payload.sourceUrl] = hash\n return prev\n }, {})\n }\n }\n\n protected async loadMapWithPayloadDiviner() {\n console.log('loadMapWithPayloadDiviner: started')\n if (await this.started()) {\n const diviner = await this.getPayloadDivinerInstance()\n let offset: number | undefined = undefined\n let moreAvailable = true\n if (diviner) {\n const newMap: Record<string, string> = {}\n while (moreAvailable) {\n const payloadDivinerQuery: PayloadDivinerQueryPayload = {\n limit: this.payloadDivinerLimit,\n offset,\n schema: PayloadDivinerQuerySchema,\n schemas: [ImageThumbnailSchema],\n }\n const payloads = await diviner.divine([payloadDivinerQuery])\n offset = (offset ?? 0) + payloads.length\n moreAvailable = payloads.length > 0\n console.log(`loadMapWithPayloadDiviner.offset: ${offset}`)\n console.log(`loadMapWithPayloadDiviner.moreAvailable: ${moreAvailable}`)\n const imagePayloadPairs = await Promise.all(\n payloads\n .filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema)\n .map<Promise<[string, ImageThumbnail]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),\n )\n imagePayloadPairs.forEach(([hash, payload]) => (newMap[payload.sourceUrl] = hash))\n }\n this._map = newMap\n }\n }\n }\n\n protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {\n if (this._pollId) {\n clearTimeout(this._pollId)\n this._pollId = undefined\n }\n return await super.stopHandler()\n }\n\n private async attachArchivistEvents() {\n const archivist = await this.getArchivistInstance()\n const mapPromise = this.getSafeMap()\n archivist.on('inserted', async ({ payloads }) => {\n const map = await mapPromise\n const thumbnails = compact(payloads.filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema))\n await Promise.all(thumbnails.map(async (payload) => (map[payload.sourceUrl] = await PayloadHasher.hashAsync(payload))))\n })\n }\n\n private async getSafeMap() {\n let mapRetry = 100 //10 seconds max\n let map = this._map\n while (!map) {\n await delay(100)\n mapRetry = mapRetry - 1\n if (mapRetry === 0) {\n throw Error('Map Not Loaded')\n }\n map = this._map\n }\n return map\n }\n\n private async poll() {\n if (await this.started()) {\n const pollFrequency = this.pollFrequency\n if (pollFrequency) {\n this._pollId = setTimeout(async () => {\n this._pollId = undefined\n await this.loadMap()\n await this.poll()\n }, pollFrequency)\n } else {\n await this.loadMap()\n }\n }\n }\n}\n","import { DivinerConfig } from '@xyo-network/diviner-model'\nimport { ImageThumbnailSchema } from '@xyo-network/image-thumbnail-payload-plugin'\n\nexport const ImageThumbnailDivinerConfigSchema = `${ImageThumbnailSchema}.diviner.config` as const\nexport type ImageThumbnailDivinerConfigSchema = typeof ImageThumbnailDivinerConfigSchema\n\nexport type ImageThumbnailDivinerConfig = DivinerConfig<{\n archivist?: string\n payloadDiviner?: string\n payloadDivinerLimit?: number\n pollFrequency?: number\n schema: ImageThumbnailDivinerConfigSchema\n}>\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,aAAa;AACtB,SAAS,eAAe;AACxB,SAAS,uBAAuB;AAChC,SAA4B,2BAA2B;AACvD,SAAS,qBAAqB;AAC9B,SAAS,mBAAmB,2BAA4C;AACxE,SAAqC,iCAAiC;AACtE,SAAyB,wBAAAA,6BAA4B;;;ACPrD,SAAS,4BAA4B;AAE9B,IAAM,oCAAoC,GAAG,oBAAoB;;;ADWjE,IAAM,wBAAN,cAA+G,gBAAyB;AAAA,EAC7I,OAAgB,gBAAgB,CAAC,mCAAmC,mBAAmB;AAAA,EAE/E;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA,EAMR,IAAI,YAAY;AACd,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA,EAEA,IAAI,iBAAiB;AACnB,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA,EAEA,IAAI,sBAAsB;AACxB,WAAO,KAAK,OAAO,uBAAuB;AAAA,EAC5C;AAAA,EAEA,IAAI,gBAAgB;AAClB,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA;AAAA,EAGA,MAAM,uBAAmD;AAEvD,QAAI,KAAK,sBAAsB,CAAE,MAAM,KAAK,oBAAqB;AAC/D,WAAK,qBAAqB;AAAA,IAC5B;AACA,SAAK,qBACH,KAAK,uBACJ,YAAY;AACX,YAAM,SAAS,KAAK,YAAY,MAAM,KAAK,QAAQ,KAAK,SAAS,IAAI;AACrE,aAAO,oBAAoB,QAAQ,4DAA4D;AAAA,IACjG,GAAG;AACL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"version":3,"sources":["../../../src/Diviner/Diviner.ts","../../../src/Diviner/Config.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { asArchivistInstance, withArchivistModule } from '@xyo-network/archivist-model'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { PayloadHasher } from '@xyo-network/core'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { asDivinerInstance, DivinerConfigSchema } from '@xyo-network/diviner-model'\nimport { PayloadDivinerQueryPayload, PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport { ImageThumbnailSchema, isImageThumbnail } from '@xyo-network/image-thumbnail-payload-plugin'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { isPayloadOfSchemaType, Payload } from '@xyo-network/payload-model'\nimport { isUrlPayload } from '@xyo-network/url-payload-plugin'\nimport { isTimestamp, TimestampSchema } from '@xyo-network/witness-timestamp'\n\nimport { ImageThumbnailDivinerConfig, ImageThumbnailDivinerConfigSchema } from './Config'\nimport { ImageThumbnailDivinerParams } from './Params'\n\n/**\n * TODO: Once the shape settles, make a generic payload so that it\n * can be used for other modules\n */\ninterface State<T> {\n state: T\n}\n\ninterface ImageThumbnailDivinerState {\n offset: number\n}\n\nconst ModuleStateSchema = 'network.xyo.module.state' as const\ntype ModuleStateSchema = typeof ModuleStateSchema\n\ntype ModuleState = Payload<State<ImageThumbnailDivinerState>, ModuleStateSchema>\n\nconst isModuleState = isPayloadOfSchemaType<ModuleState>(ModuleStateSchema)\n\ntype ConfigStoreKey = 'indexStore' | 'stateStore' | 'thumbnailStore'\n\ntype ConfigStore = Extract<keyof ImageThumbnailDivinerConfig, ConfigStoreKey>\n\nconst ImageThumbnailResultIndexSchema = `${ImageThumbnailSchema}.index` as const\ntype ImageThumbnailResultIndexSchema = typeof ImageThumbnailResultIndexSchema\n\ninterface ImageThumbnailResultInfo {\n sources: string[]\n // TODO: Something richer than HTTP status code that allows for info about failure modes\n status: number\n timestamp: number\n url: string\n}\n\ntype ImageThumbnailResult = Payload<ImageThumbnailResultInfo, ImageThumbnailResultIndexSchema>\n\nconst isImageThumbnailResult = isPayloadOfSchemaType<ImageThumbnailResult>(ImageThumbnailResultIndexSchema)\n\n/**\n * The fields that will need to be indexed on in the underlying store\n */\ntype QueryableImageThumbnailResultProperties = Extract<keyof ImageThumbnailResult, 'url' | 'timestamp' | 'status'>\n\n/**\n * The query that will be used to retrieve the results from the underlying store\n */\ntype ImageThumbnailResultQuery = PayloadDivinerQueryPayload & { schemas: [ImageThumbnailSchema] } & Pick<\n ImageThumbnailResult,\n QueryableImageThumbnailResultProperties\n >\n\nconst moduleName = 'ImageThumbnailDiviner'\n\nexport class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams = ImageThumbnailDivinerParams> extends AbstractDiviner<TParams> {\n static override configSchemas = [ImageThumbnailDivinerConfigSchema, DivinerConfigSchema]\n\n private _pollId?: string | number | NodeJS.Timeout\n\n get payloadDivinerLimit() {\n return this.config.payloadDivinerLimit ?? 1_0000\n }\n\n get pollFrequency() {\n return this.config.pollFrequency ?? 10_000\n }\n\n protected backgroundDivine = async (): Promise<void> => {\n // Load last state\n const lastState = (await this.retrieveState()) ?? { offset: 0 }\n const { offset } = lastState\n // Get next batch of results\n const boundWitnessDiviner = await this.getBoundWitnessDivinerForStore('thumbnailStore')\n const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema }).fields({\n limit: this.payloadDivinerLimit,\n offset,\n order: 'asc',\n payload_schemas: [ImageThumbnailSchema, TimestampSchema],\n })\n const batch = await boundWitnessDiviner.divine([query])\n if (batch.length === 0) return\n const imageThumbnailTimestampTuples = batch\n .filter(isBoundWitness)\n .map((bw) => {\n const imageThumbnailIndexes = bw.payload_schemas?.map((schema, index) => (schema === ImageThumbnailSchema ? index : undefined)).filter(exists)\n const timestampIndex = bw.payload_schemas?.findIndex((schema) => schema === TimestampSchema)\n if (!imageThumbnailIndexes.length || timestampIndex === -1) return undefined\n const imageThumbnails = bw.payload_hashes.map((hash, index) => (imageThumbnailIndexes.includes(index) ? hash : undefined)).filter(exists)\n const timestamp = bw.payload_hashes?.[timestampIndex]\n return imageThumbnails.map((imageThumbnail) => [imageThumbnail, timestamp] as const)\n })\n .flat()\n .filter(exists)\n const archivist = await this.getArchivistForStore('thumbnailStore')\n const payloadTuples = (\n await Promise.all(\n imageThumbnailTimestampTuples.map(async ([imageThumbnailHash, timestampHash]) => {\n const results = await archivist.get([imageThumbnailHash, timestampHash])\n const imageThumbnailPayload = results.find(isImageThumbnail)\n const timestampPayload = results.find(isTimestamp)\n if (!imageThumbnailPayload || !timestampPayload) return undefined\n const calculatedImageThumbnailHash = await PayloadHasher.hashAsync(imageThumbnailPayload)\n const calculatedTimestampHash = await PayloadHasher.hashAsync(timestampPayload)\n if (imageThumbnailHash !== calculatedImageThumbnailHash || timestampHash !== calculatedTimestampHash) return undefined\n return [imageThumbnailHash, imageThumbnailPayload, timestampHash, timestampPayload] as const\n }),\n )\n ).filter(exists)\n // Build index results\n const indexedResults = payloadTuples.map(([thumbnailHash, thumbnailPayload, timestampHash, timestampPayload]) => {\n const { sourceUrl: url } = thumbnailPayload\n const { timestamp } = timestampPayload\n const status = thumbnailPayload.http?.status ?? -1\n const sources = [thumbnailHash, timestampHash]\n const result = new PayloadBuilder<ImageThumbnailResult>({ schema: ImageThumbnailResultIndexSchema })\n .fields({ sources, status, timestamp, url })\n .build()\n return result\n })\n // Insert index results\n const indexArchivist = await this.getArchivistForStore('indexStore')\n await indexArchivist.insert(indexedResults)\n // Update state\n const nextOffset = offset + batch.length + 1\n const currentState = { ...lastState, offset: nextOffset }\n await this.commitState(currentState)\n }\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n */\n protected async commitState(state: ImageThumbnailDivinerState) {\n const stateStore = assertEx(this.config.stateStore?.archivist, `${moduleName}: No stateStore configured`)\n const module = assertEx(await this.resolve(stateStore), `${moduleName}: Failed to resolve stateStore`)\n await withArchivistModule(module, async (archivist) => {\n const mod = ArchivistWrapper.wrap(archivist, this.account)\n const payload = new PayloadBuilder<ModuleState>({ schema: ModuleStateSchema }).fields({ state }).build()\n await mod.insert([payload])\n })\n }\n\n protected override async divineHandler(payloads: Payload[] = []): Promise<ImageThumbnailResult[]> {\n const urls = payloads.filter(isUrlPayload).map((urlPayload) => urlPayload.url)\n const diviner = await this.getPayloadDivinerForStore('indexStore')\n const results = (\n await Promise.all(\n urls.map(async (url) => {\n const query = new PayloadBuilder<ImageThumbnailResultQuery>({ schema: PayloadDivinerQuerySchema })\n // TODO: Expose status, limit (and possibly offset) to caller. Currently only exposing URL\n .fields({ limit: 1, offset: 0, order: 'desc', url })\n .build()\n return await diviner.divine([query])\n }),\n )\n )\n .flat()\n .filter(isImageThumbnailResult)\n return results\n }\n\n protected async getArchivistForStore(store: ConfigStore, wrap?: boolean) {\n const name = assertEx(this.config?.[store]?.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist`)\n return wrap ? ArchivistWrapper.wrap(mod, this.account) : asArchivistInstance(mod, () => `${moduleName}: ${store}.archivist is not an Archivist`)\n }\n\n protected async getBoundWitnessDivinerForStore(store: ConfigStore, wrap?: boolean) {\n const name = assertEx(this.config?.[store]?.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner`)\n return wrap\n ? DivinerWrapper.wrap(mod, this.account)\n : asDivinerInstance(mod, () => `${moduleName}: ${store}.boundWitnessDiviner is not a Diviner`)\n }\n\n protected async getPayloadDivinerForStore(store: ConfigStore, wrap?: boolean) {\n const name = assertEx(this.config?.[store]?.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner`)\n return wrap ? DivinerWrapper.wrap(mod, this.account) : asDivinerInstance(mod, () => `${moduleName}: ${store}.payloadDiviner is not a Diviner`)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n protected async retrieveState(): Promise<ImageThumbnailDivinerState | undefined> {\n let hash: string = ''\n const diviner = await this.getBoundWitnessDivinerForStore('stateStore')\n const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema }).fields({\n address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const stateStoreArchivist = assertEx(this.config.stateStore?.archivist, `${moduleName}: No stateStore archivist configured`)\n await withArchivistModule(\n assertEx(await this.resolve(stateStoreArchivist), `${moduleName}: Failed to resolve stateStore archivist`),\n async (mod) => {\n const archivist = ArchivistWrapper.wrap(mod, this.account)\n const payloads = await archivist.get([hash])\n if (payloads.length > 0) {\n const payload = payloads[0]\n if (isModuleState(payload)) {\n return payload.state\n }\n }\n },\n )\n }\n return undefined\n }\n\n protected override async startHandler(): Promise<boolean> {\n await super.startHandler()\n this.poll()\n return true\n }\n\n protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {\n if (this._pollId) {\n clearTimeout(this._pollId)\n this._pollId = undefined\n }\n return await super.stopHandler()\n }\n\n private poll() {\n this._pollId = setTimeout(async () => {\n try {\n await this.backgroundDivine()\n } catch (e) {\n console.log(e)\n } finally {\n if (this._pollId) clearTimeout(this._pollId)\n this._pollId = undefined\n this.poll()\n }\n }, this.pollFrequency)\n }\n}\n","import { DivinerConfig } from '@xyo-network/diviner-model'\nimport { ImageThumbnailSchema } from '@xyo-network/image-thumbnail-payload-plugin'\n\nexport const ImageThumbnailDivinerConfigSchema = `${ImageThumbnailSchema}.diviner.config` as const\nexport type ImageThumbnailDivinerConfigSchema = typeof ImageThumbnailDivinerConfigSchema\n\n/**\n * Describes an Archivist/Diviner combination\n * that enables searching signed payloads\n */\nexport interface SearchableStorage {\n archivist: string\n boundWitnessDiviner: string\n payloadDiviner: string\n}\n\nexport type ImageThumbnailDivinerConfig = DivinerConfig<{\n /** @deprecated Use appropriate Storage */\n archivist?: string\n /**\n * Where the diviner should store it's index\n */\n indexStore?: SearchableStorage\n /** @deprecated Use appropriate Storage */\n payloadDiviner?: string\n payloadDivinerLimit?: number\n pollFrequency?: number\n schema: ImageThumbnailDivinerConfigSchema\n /**\n * Where the diviner should persist its internal state\n */\n stateStore?: SearchableStorage\n /**\n * Where the diviner should look for stored thumbnails\n */\n thumbnailStore?: SearchableStorage\n}>\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,cAAc;AACvB,SAAS,uBAAuB;AAChC,SAAS,qBAAqB,2BAA2B;AACzD,SAAS,wBAAwB;AACjC,SAAS,sBAAsB;AAC/B,SAAS,qBAAqB;AAC9B,SAA0C,sCAAsC;AAChF,SAAS,mBAAmB,2BAA2B;AACvD,SAAqC,iCAAiC;AACtE,SAAS,sBAAsB;AAC/B,SAAS,wBAAAA,uBAAsB,wBAAwB;AACvD,SAAS,sBAAsB;AAC/B,SAA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