@xyo-network/image-thumbnail-plugin 2.71.10 → 2.71.12
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.
- package/dist/cjs/Diviner/Config.js +6 -0
- package/dist/cjs/Diviner/Config.js.map +1 -0
- package/dist/cjs/Diviner/Diviner.js +200 -0
- package/dist/cjs/Diviner/Diviner.js.map +1 -0
- package/dist/cjs/Diviner/Params.js +3 -0
- package/dist/cjs/Diviner/Params.js.map +1 -0
- package/dist/cjs/Diviner/index.js +7 -0
- package/dist/cjs/Diviner/index.js.map +1 -0
- package/dist/cjs/Plugin.js +6 -1
- package/dist/cjs/Plugin.js.map +1 -1
- package/dist/cjs/Witness/Witness.js +3 -3
- package/dist/cjs/Witness/Witness.js.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/docs.json +28852 -6911
- package/dist/esm/Diviner/Config.js +3 -0
- package/dist/esm/Diviner/Config.js.map +1 -0
- package/dist/esm/Diviner/Diviner.js +176 -0
- package/dist/esm/Diviner/Diviner.js.map +1 -0
- package/dist/esm/Diviner/Params.js +2 -0
- package/dist/esm/Diviner/Params.js.map +1 -0
- package/dist/esm/Diviner/index.js +4 -0
- package/dist/esm/Diviner/index.js.map +1 -0
- package/dist/esm/Plugin.js +7 -2
- package/dist/esm/Plugin.js.map +1 -1
- package/dist/esm/Witness/Witness.js +3 -3
- package/dist/esm/Witness/Witness.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/types/Diviner/Config.d.ts +10 -0
- package/dist/types/Diviner/Config.d.ts.map +1 -0
- package/dist/types/Diviner/Diviner.d.ts +29 -0
- package/dist/types/Diviner/Diviner.d.ts.map +1 -0
- package/dist/types/Diviner/Params.d.ts +5 -0
- package/dist/types/Diviner/Params.d.ts.map +1 -0
- package/dist/types/Diviner/index.d.ts +4 -0
- package/dist/types/Diviner/index.d.ts.map +1 -0
- package/dist/types/Plugin.d.ts +49 -1
- package/dist/types/Plugin.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +16 -9
- package/src/Diviner/Config.ts +12 -0
- package/src/Diviner/Diviner.ts +197 -0
- package/src/Diviner/Params.ts +6 -0
- package/src/Diviner/index.ts +3 -0
- package/src/Plugin.ts +7 -2
- package/src/Witness/Witness.ts +3 -3
- package/src/index.ts +1 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { assertEx } from '@xylabs/assert'
|
|
2
|
+
import { delay } from '@xylabs/delay'
|
|
3
|
+
import { AbstractDiviner } from '@xyo-network/abstract-diviner'
|
|
4
|
+
import { ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist-model'
|
|
5
|
+
import { PayloadHasher } from '@xyo-network/core'
|
|
6
|
+
import { asDivinerInstance, DivinerConfigSchema, DivinerInstance } from '@xyo-network/diviner-model'
|
|
7
|
+
import { PayloadDivinerQueryPayload, PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
|
|
8
|
+
import { ImageThumbnail, ImageThumbnailSchema } from '@xyo-network/image-thumbnail-payload-plugin'
|
|
9
|
+
import { UrlPayload } from '@xyo-network/url-payload-plugin'
|
|
10
|
+
import compact from 'lodash/compact'
|
|
11
|
+
|
|
12
|
+
import { ImageThumbnailDivinerConfigSchema } from './Config'
|
|
13
|
+
import { ImageThumbnailDivinerParams } from './Params'
|
|
14
|
+
|
|
15
|
+
export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams = ImageThumbnailDivinerParams> extends AbstractDiviner<TParams> {
|
|
16
|
+
static override configSchemas = [ImageThumbnailDivinerConfigSchema, DivinerConfigSchema]
|
|
17
|
+
|
|
18
|
+
private _archivistInstance: Promise<ArchivistInstance> | undefined
|
|
19
|
+
private _initializeArchivistConnectionIfNeededPromise: Promise<void> | undefined
|
|
20
|
+
private _map: Record<string, string> | undefined
|
|
21
|
+
private _payloadDivinerInstance: Promise<DivinerInstance> | undefined
|
|
22
|
+
private _pollId?: string | number | NodeJS.Timeout
|
|
23
|
+
|
|
24
|
+
get archivist() {
|
|
25
|
+
return this.config.archivist
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get payloadDiviner() {
|
|
29
|
+
return this.config.payloadDiviner
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get pollFrequency() {
|
|
33
|
+
return this.config.pollFrequency
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//using promise as mutex
|
|
37
|
+
async getArchivistInstance(): Promise<ArchivistInstance> {
|
|
38
|
+
//if previously checked, but not found, clear promise
|
|
39
|
+
if (this._archivistInstance && !(await this._archivistInstance)) {
|
|
40
|
+
this._archivistInstance = undefined
|
|
41
|
+
}
|
|
42
|
+
this._archivistInstance =
|
|
43
|
+
this._archivistInstance ??
|
|
44
|
+
(async () => {
|
|
45
|
+
const module = await this.resolve(this.archivist)
|
|
46
|
+
return asArchivistInstance(module, 'Provided archivist address did not resolve to an Archivist')
|
|
47
|
+
})()
|
|
48
|
+
return this._archivistInstance
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//using promise as mutex
|
|
52
|
+
async getPayloadDivinerInstance(): Promise<DivinerInstance | undefined> {
|
|
53
|
+
const payloadDivinerAddress = this.payloadDiviner
|
|
54
|
+
if (payloadDivinerAddress) {
|
|
55
|
+
//if previously checked, but not found, clear promise
|
|
56
|
+
if (this._payloadDivinerInstance && !(await this._payloadDivinerInstance)) {
|
|
57
|
+
this._payloadDivinerInstance = undefined
|
|
58
|
+
}
|
|
59
|
+
this._payloadDivinerInstance =
|
|
60
|
+
this._payloadDivinerInstance ??
|
|
61
|
+
(async () => {
|
|
62
|
+
const module = await this.resolve(payloadDivinerAddress)
|
|
63
|
+
return asDivinerInstance(module, 'Provided payload diviner address did not resolve to a Diviner')
|
|
64
|
+
})()
|
|
65
|
+
|
|
66
|
+
return this._payloadDivinerInstance
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected override async divineHandler(payloads: UrlPayload[] = []): Promise<ImageThumbnail[]> {
|
|
71
|
+
await this.initializeArchivistConnectionIfNeeded()
|
|
72
|
+
const urls = payloads.map((urlPayload) => urlPayload.url)
|
|
73
|
+
const map = await this.getSafeMap()
|
|
74
|
+
const archivist = await this.getArchivistInstance()
|
|
75
|
+
const hashes = compact(urls.map((url) => map?.[url]))
|
|
76
|
+
return (await archivist.get(hashes)).filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//using promise as mutex
|
|
80
|
+
protected initializeArchivistConnectionIfNeeded() {
|
|
81
|
+
this._initializeArchivistConnectionIfNeededPromise =
|
|
82
|
+
this._initializeArchivistConnectionIfNeededPromise ??
|
|
83
|
+
(async () => {
|
|
84
|
+
if (!this._map) {
|
|
85
|
+
await this.attachArchivistEvents()
|
|
86
|
+
console.log('initializeArchivistConnectionIfNeeded: attachArchivistEvents done')
|
|
87
|
+
await this.poll()
|
|
88
|
+
console.log('initializeArchivistConnectionIfNeeded: poll done')
|
|
89
|
+
}
|
|
90
|
+
})()
|
|
91
|
+
return this._initializeArchivistConnectionIfNeededPromise
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
protected async loadMap() {
|
|
95
|
+
if (this.payloadDiviner) {
|
|
96
|
+
return await this.loadMapWithPayloadDiviner()
|
|
97
|
+
} else {
|
|
98
|
+
return await this.loadMapWithAll()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected async loadMapWithAll() {
|
|
103
|
+
if (await this.started()) {
|
|
104
|
+
const archivist = await this.getArchivistInstance()
|
|
105
|
+
assertEx(archivist.all, "Archivist does not support 'all'")
|
|
106
|
+
const allPayloads = (await archivist.all?.()) ?? []
|
|
107
|
+
const imagePayloadPairs = await Promise.all(
|
|
108
|
+
allPayloads
|
|
109
|
+
.filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema)
|
|
110
|
+
.map<Promise<[string, ImageThumbnail]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),
|
|
111
|
+
)
|
|
112
|
+
this._map = imagePayloadPairs.reduce<Record<string, string>>((prev, [hash, payload]) => {
|
|
113
|
+
prev[payload.sourceUrl] = hash
|
|
114
|
+
return prev
|
|
115
|
+
}, {})
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
protected async loadMapWithPayloadDiviner() {
|
|
120
|
+
console.log('loadMapWithPayloadDiviner: started')
|
|
121
|
+
if (await this.started()) {
|
|
122
|
+
const diviner = await this.getPayloadDivinerInstance()
|
|
123
|
+
let offsetHash: string | undefined = undefined
|
|
124
|
+
let moreAvailable = true
|
|
125
|
+
if (diviner) {
|
|
126
|
+
const newMap: Record<string, string> = {}
|
|
127
|
+
while (moreAvailable) {
|
|
128
|
+
const payloadDivinerQuery: PayloadDivinerQueryPayload = {
|
|
129
|
+
hash: offsetHash,
|
|
130
|
+
schema: PayloadDivinerQuerySchema,
|
|
131
|
+
schemas: [ImageThumbnailSchema],
|
|
132
|
+
}
|
|
133
|
+
const payloads = await diviner.divine([payloadDivinerQuery])
|
|
134
|
+
const lastPayload = payloads[payloads.length - 1]
|
|
135
|
+
offsetHash = lastPayload ? await PayloadHasher.hashAsync(lastPayload) : undefined
|
|
136
|
+
moreAvailable = payloads.length > 0
|
|
137
|
+
console.log(`loadMapWithPayloadDiviner.offsetHash: ${offsetHash}`)
|
|
138
|
+
console.log(`loadMapWithPayloadDiviner.moreAvailable: ${moreAvailable}`)
|
|
139
|
+
const imagePayloadPairs = await Promise.all(
|
|
140
|
+
payloads
|
|
141
|
+
.filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema)
|
|
142
|
+
.map<Promise<[string, ImageThumbnail]>>(async (payload) => [await PayloadHasher.hashAsync(payload), payload]),
|
|
143
|
+
)
|
|
144
|
+
imagePayloadPairs.forEach(([hash, payload]) => (newMap[payload.sourceUrl] = hash))
|
|
145
|
+
}
|
|
146
|
+
this._map = newMap
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {
|
|
152
|
+
if (this._pollId) {
|
|
153
|
+
clearTimeout(this._pollId)
|
|
154
|
+
this._pollId = undefined
|
|
155
|
+
}
|
|
156
|
+
return await super.stopHandler()
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private async attachArchivistEvents() {
|
|
160
|
+
const archivist = await this.getArchivistInstance()
|
|
161
|
+
const mapPromise = this.getSafeMap()
|
|
162
|
+
archivist.on('inserted', async ({ payloads }) => {
|
|
163
|
+
const map = await mapPromise
|
|
164
|
+
const thumbnails = compact(payloads.filter((payload): payload is ImageThumbnail => payload.schema === ImageThumbnailSchema))
|
|
165
|
+
await Promise.all(thumbnails.map(async (payload) => (map[payload.sourceUrl] = await PayloadHasher.hashAsync(payload))))
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private async getSafeMap() {
|
|
170
|
+
let mapRetry = 100 //10 seconds max
|
|
171
|
+
let map = this._map
|
|
172
|
+
while (!map) {
|
|
173
|
+
await delay(100)
|
|
174
|
+
mapRetry = mapRetry - 1
|
|
175
|
+
if (mapRetry === 0) {
|
|
176
|
+
throw Error('Map Not Loaded')
|
|
177
|
+
}
|
|
178
|
+
map = this._map
|
|
179
|
+
}
|
|
180
|
+
return map
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private async poll() {
|
|
184
|
+
if (await this.started()) {
|
|
185
|
+
const pollFrequency = this.pollFrequency
|
|
186
|
+
if (pollFrequency) {
|
|
187
|
+
this._pollId = setTimeout(async () => {
|
|
188
|
+
this._pollId = undefined
|
|
189
|
+
await this.loadMap()
|
|
190
|
+
await this.poll()
|
|
191
|
+
}, pollFrequency)
|
|
192
|
+
} else {
|
|
193
|
+
await this.loadMap()
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DivinerParams } from '@xyo-network/diviner-model'
|
|
2
|
+
import { AnyConfigSchema } from '@xyo-network/module'
|
|
3
|
+
|
|
4
|
+
import { ImageThumbnailDivinerConfig } from './Config'
|
|
5
|
+
|
|
6
|
+
export type ImageThumbnailDivinerParams = DivinerParams<AnyConfigSchema<ImageThumbnailDivinerConfig>>
|
package/src/Plugin.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { ImageThumbnailSchema } from '@xyo-network/image-thumbnail-payload-plugin'
|
|
2
2
|
import { PayloadSetSchema } from '@xyo-network/payload-model'
|
|
3
|
-
import {
|
|
3
|
+
import { createPayloadSetDualPlugin } from '@xyo-network/payloadset-plugin'
|
|
4
4
|
|
|
5
|
+
import { ImageThumbnailDiviner } from './Diviner'
|
|
5
6
|
import { ImageThumbnailWitness } from './Witness'
|
|
6
7
|
|
|
7
8
|
export const ImageThumbnailPlugin = () =>
|
|
8
|
-
|
|
9
|
+
createPayloadSetDualPlugin<ImageThumbnailWitness, ImageThumbnailDiviner>(
|
|
9
10
|
{ required: { [ImageThumbnailSchema]: 1 }, schema: PayloadSetSchema },
|
|
10
11
|
{
|
|
12
|
+
diviner: async (params) => {
|
|
13
|
+
const result = await ImageThumbnailDiviner.create(params)
|
|
14
|
+
return result
|
|
15
|
+
},
|
|
11
16
|
witness: async (params) => {
|
|
12
17
|
const result = await ImageThumbnailWitness.create(params)
|
|
13
18
|
return result
|
package/src/Witness/Witness.ts
CHANGED
|
@@ -58,15 +58,15 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
get maxAsyncProcesses() {
|
|
61
|
-
return this.config.maxAsyncProcesses ??
|
|
61
|
+
return this.config.maxAsyncProcesses ?? 2
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
get maxCacheBytes() {
|
|
65
|
-
return this.config.maxCacheBytes ?? 1024 * 1024 *
|
|
65
|
+
return this.config.maxCacheBytes ?? 1024 * 1024 * 16 //64MB max size
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
get maxCacheEntries() {
|
|
69
|
-
return this.config.maxCacheEntries ??
|
|
69
|
+
return this.config.maxCacheEntries ?? 500
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
get quality() {
|