@johnhenry/browsermesh-apps 0.0.0
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/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +36 -0
- package/src/apps.mjs +1351 -0
- package/src/audit.mjs +622 -0
- package/src/chat.mjs +595 -0
- package/src/compat.mjs +30 -0
- package/src/consensus.mjs +784 -0
- package/src/devtools.mjs +255 -0
- package/src/gpu.mjs +732 -0
- package/src/index.mjs +44 -0
- package/src/marketplace-ui.mjs +245 -0
- package/src/marketplace.mjs +754 -0
- package/src/orchestrator.mjs +1575 -0
- package/src/payments.mjs +909 -0
- package/src/peer-agent-swarm.mjs +569 -0
- package/src/peer-agent.mjs +547 -0
- package/src/peer-chat.mjs +381 -0
- package/src/peer-compute.mjs +687 -0
- package/src/peer-encrypted-store.mjs +514 -0
- package/src/peer-escrow.mjs +573 -0
- package/src/peer-files.mjs +470 -0
- package/src/peer-health.mjs +727 -0
- package/src/peer-ipfs.mjs +374 -0
- package/src/peer-node.mjs +793 -0
- package/src/peer-payments.mjs +613 -0
- package/src/peer-registry.mjs +595 -0
- package/src/peer-routing.mjs +555 -0
- package/src/peer-services.mjs +478 -0
- package/src/peer-session.mjs +917 -0
- package/src/peer-terminal.mjs +500 -0
- package/src/peer-timestamp.mjs +403 -0
- package/src/peer-torrent.mjs +628 -0
- package/src/peer-verification.mjs +522 -0
- package/src/quotas.mjs +621 -0
- package/src/resources.mjs +679 -0
- package/src/scheduler.mjs +846 -0
- package/src/silent-catch.mjs +55 -0
- package/src/tools.mjs +668 -0
- package/src/visualizations.mjs +902 -0
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* clawser-mesh-marketplace.js -- Service marketplace for BrowserMesh.
|
|
3
|
+
*
|
|
4
|
+
* Publish, discover, and review services offered by mesh peers. Includes
|
|
5
|
+
* text search, category/tag filtering, rating aggregation, and an efficient
|
|
6
|
+
* inverted index for O(1) lookups.
|
|
7
|
+
*
|
|
8
|
+
* No browser-only imports at module level.
|
|
9
|
+
*
|
|
10
|
+
* Run tests:
|
|
11
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-marketplace.test.mjs
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { MESH_TYPE } from '@johnhenry/browsermesh-primitives';
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Wire constants (re-exported from canonical registry)
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
export const LISTING_PUBLISH = MESH_TYPE.LISTING_PUBLISH; // 0xdf
|
|
21
|
+
export const LISTING_QUERY = MESH_TYPE.LISTING_QUERY; // 0xe0
|
|
22
|
+
export const LISTING_RESPONSE = MESH_TYPE.LISTING_RESPONSE; // 0xe1
|
|
23
|
+
export const LISTING_PURCHASE = MESH_TYPE.LISTING_PURCHASE; // 0xe2
|
|
24
|
+
export const REVIEW_SUBMIT = MESH_TYPE.REVIEW_SUBMIT; // 0xe3
|
|
25
|
+
export const REVIEW_QUERY = MESH_TYPE.REVIEW_QUERY; // 0xe4
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Valid enumerations
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
const VALID_STATUSES = Object.freeze([
|
|
32
|
+
'active', 'paused', 'expired', 'removed',
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
const VALID_PRICING_MODELS = Object.freeze([
|
|
36
|
+
'free', 'per-call', 'subscription', 'credits',
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// ServiceListing
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A service offered on the marketplace.
|
|
45
|
+
*/
|
|
46
|
+
export class ServiceListing {
|
|
47
|
+
/**
|
|
48
|
+
* @param {object} opts
|
|
49
|
+
* @param {string} opts.id
|
|
50
|
+
* @param {string} opts.name
|
|
51
|
+
* @param {string} opts.description
|
|
52
|
+
* @param {string} opts.providerPodId
|
|
53
|
+
* @param {string} opts.category
|
|
54
|
+
* @param {object} [opts.pricing]
|
|
55
|
+
* @param {string} [opts.pricing.model='free']
|
|
56
|
+
* @param {number} [opts.pricing.amount=0]
|
|
57
|
+
* @param {string} [opts.pricing.currency='credits']
|
|
58
|
+
* @param {string[]} [opts.tags=[]]
|
|
59
|
+
* @param {string} [opts.version='1.0.0']
|
|
60
|
+
* @param {string|null} [opts.endpoint=null]
|
|
61
|
+
* @param {object} [opts.metadata={}]
|
|
62
|
+
* @param {number} [opts.publishedAt]
|
|
63
|
+
* @param {number|null} [opts.expiresAt=null]
|
|
64
|
+
* @param {string} [opts.status='active']
|
|
65
|
+
*/
|
|
66
|
+
constructor({
|
|
67
|
+
id,
|
|
68
|
+
name,
|
|
69
|
+
description,
|
|
70
|
+
providerPodId,
|
|
71
|
+
category,
|
|
72
|
+
pricing = {},
|
|
73
|
+
tags = [],
|
|
74
|
+
version = '1.0.0',
|
|
75
|
+
endpoint,
|
|
76
|
+
metadata = {},
|
|
77
|
+
publishedAt,
|
|
78
|
+
expiresAt,
|
|
79
|
+
status = 'active',
|
|
80
|
+
}) {
|
|
81
|
+
if (!id || typeof id !== 'string') {
|
|
82
|
+
throw new Error('id is required and must be a non-empty string');
|
|
83
|
+
}
|
|
84
|
+
if (!name || typeof name !== 'string') {
|
|
85
|
+
throw new Error('name is required and must be a non-empty string');
|
|
86
|
+
}
|
|
87
|
+
if (!providerPodId || typeof providerPodId !== 'string') {
|
|
88
|
+
throw new Error('providerPodId is required and must be a non-empty string');
|
|
89
|
+
}
|
|
90
|
+
if (!category || typeof category !== 'string') {
|
|
91
|
+
throw new Error('category is required and must be a non-empty string');
|
|
92
|
+
}
|
|
93
|
+
if (!VALID_STATUSES.includes(status)) {
|
|
94
|
+
throw new Error(`Invalid status: "${status}". Must be one of: ${VALID_STATUSES.join(', ')}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Validate pricing model
|
|
98
|
+
const pricingModel = pricing.model ?? 'free';
|
|
99
|
+
if (!VALID_PRICING_MODELS.includes(pricingModel)) {
|
|
100
|
+
throw new Error(`Invalid pricing model: "${pricingModel}". Must be one of: ${VALID_PRICING_MODELS.join(', ')}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
this.id = id;
|
|
104
|
+
this.name = name;
|
|
105
|
+
this.description = description ?? '';
|
|
106
|
+
this.providerPodId = providerPodId;
|
|
107
|
+
this.category = category;
|
|
108
|
+
this.pricing = {
|
|
109
|
+
model: pricingModel,
|
|
110
|
+
amount: pricing.amount ?? 0,
|
|
111
|
+
currency: pricing.currency ?? 'credits',
|
|
112
|
+
};
|
|
113
|
+
this.tags = [...tags];
|
|
114
|
+
this.version = version;
|
|
115
|
+
this.endpoint = endpoint ?? null;
|
|
116
|
+
this.metadata = metadata ? { ...metadata } : {};
|
|
117
|
+
this.publishedAt = publishedAt ?? Date.now();
|
|
118
|
+
this.expiresAt = expiresAt ?? null;
|
|
119
|
+
this.status = status;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Whether the listing has passed its expiration date.
|
|
124
|
+
* @param {number} [now]
|
|
125
|
+
* @returns {boolean}
|
|
126
|
+
*/
|
|
127
|
+
isExpired(now = Date.now()) {
|
|
128
|
+
if (this.expiresAt === null || this.expiresAt === undefined) return false;
|
|
129
|
+
return now > this.expiresAt;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Check if the listing matches a text query (searches name, description, tags).
|
|
134
|
+
* @param {string} query
|
|
135
|
+
* @returns {boolean}
|
|
136
|
+
*/
|
|
137
|
+
matchesQuery(query) {
|
|
138
|
+
if (!query || query.length === 0) return true;
|
|
139
|
+
const q = query.toLowerCase();
|
|
140
|
+
if (this.name.toLowerCase().includes(q)) return true;
|
|
141
|
+
if (this.description.toLowerCase().includes(q)) return true;
|
|
142
|
+
for (const tag of this.tags) {
|
|
143
|
+
if (tag.toLowerCase().includes(q)) return true;
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
toJSON() {
|
|
149
|
+
return {
|
|
150
|
+
id: this.id,
|
|
151
|
+
name: this.name,
|
|
152
|
+
description: this.description,
|
|
153
|
+
providerPodId: this.providerPodId,
|
|
154
|
+
category: this.category,
|
|
155
|
+
pricing: { ...this.pricing },
|
|
156
|
+
tags: [...this.tags],
|
|
157
|
+
version: this.version,
|
|
158
|
+
endpoint: this.endpoint,
|
|
159
|
+
metadata: { ...this.metadata },
|
|
160
|
+
publishedAt: this.publishedAt,
|
|
161
|
+
expiresAt: this.expiresAt,
|
|
162
|
+
status: this.status,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @param {object} data
|
|
168
|
+
* @returns {ServiceListing}
|
|
169
|
+
*/
|
|
170
|
+
static fromJSON(data) {
|
|
171
|
+
return new ServiceListing({
|
|
172
|
+
id: data.id,
|
|
173
|
+
name: data.name,
|
|
174
|
+
description: data.description,
|
|
175
|
+
providerPodId: data.providerPodId,
|
|
176
|
+
category: data.category,
|
|
177
|
+
pricing: data.pricing,
|
|
178
|
+
tags: data.tags,
|
|
179
|
+
version: data.version,
|
|
180
|
+
endpoint: data.endpoint,
|
|
181
|
+
metadata: data.metadata,
|
|
182
|
+
publishedAt: data.publishedAt,
|
|
183
|
+
expiresAt: data.expiresAt,
|
|
184
|
+
status: data.status,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ---------------------------------------------------------------------------
|
|
190
|
+
// ServiceReview
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* A review/rating for a marketplace service.
|
|
195
|
+
*/
|
|
196
|
+
export class ServiceReview {
|
|
197
|
+
/**
|
|
198
|
+
* @param {object} opts
|
|
199
|
+
* @param {string} opts.id
|
|
200
|
+
* @param {string} opts.listingId
|
|
201
|
+
* @param {string} opts.reviewerPodId
|
|
202
|
+
* @param {number} opts.rating - 1-5 integer
|
|
203
|
+
* @param {string|null} [opts.comment=null]
|
|
204
|
+
* @param {number} [opts.createdAt]
|
|
205
|
+
*/
|
|
206
|
+
constructor({ id, listingId, reviewerPodId, rating, comment, createdAt }) {
|
|
207
|
+
if (!id || typeof id !== 'string') {
|
|
208
|
+
throw new Error('id is required and must be a non-empty string');
|
|
209
|
+
}
|
|
210
|
+
if (!listingId || typeof listingId !== 'string') {
|
|
211
|
+
throw new Error('listingId is required and must be a non-empty string');
|
|
212
|
+
}
|
|
213
|
+
if (!reviewerPodId || typeof reviewerPodId !== 'string') {
|
|
214
|
+
throw new Error('reviewerPodId is required and must be a non-empty string');
|
|
215
|
+
}
|
|
216
|
+
if (rating === undefined || rating === null) {
|
|
217
|
+
throw new Error('rating is required');
|
|
218
|
+
}
|
|
219
|
+
if (!Number.isInteger(rating)) {
|
|
220
|
+
throw new Error('rating must be an integer between 1 and 5');
|
|
221
|
+
}
|
|
222
|
+
if (rating < 1 || rating > 5) {
|
|
223
|
+
throw new Error('rating must be between 1 and 5');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
this.id = id;
|
|
227
|
+
this.listingId = listingId;
|
|
228
|
+
this.reviewerPodId = reviewerPodId;
|
|
229
|
+
this.rating = rating;
|
|
230
|
+
this.comment = comment ?? null;
|
|
231
|
+
this.createdAt = createdAt ?? Date.now();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
toJSON() {
|
|
235
|
+
return {
|
|
236
|
+
id: this.id,
|
|
237
|
+
listingId: this.listingId,
|
|
238
|
+
reviewerPodId: this.reviewerPodId,
|
|
239
|
+
rating: this.rating,
|
|
240
|
+
comment: this.comment,
|
|
241
|
+
createdAt: this.createdAt,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @param {object} data
|
|
247
|
+
* @returns {ServiceReview}
|
|
248
|
+
*/
|
|
249
|
+
static fromJSON(data) {
|
|
250
|
+
return new ServiceReview({
|
|
251
|
+
id: data.id,
|
|
252
|
+
listingId: data.listingId,
|
|
253
|
+
reviewerPodId: data.reviewerPodId,
|
|
254
|
+
rating: data.rating,
|
|
255
|
+
comment: data.comment,
|
|
256
|
+
createdAt: data.createdAt,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ---------------------------------------------------------------------------
|
|
262
|
+
// Marketplace
|
|
263
|
+
// ---------------------------------------------------------------------------
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Service marketplace manager.
|
|
267
|
+
*/
|
|
268
|
+
export class Marketplace {
|
|
269
|
+
/** @type {string} */
|
|
270
|
+
#localPodId;
|
|
271
|
+
/** @type {Map<string, ServiceListing>} */
|
|
272
|
+
#listings = new Map();
|
|
273
|
+
/** @type {Map<string, ServiceReview[]>} listingId -> reviews */
|
|
274
|
+
#reviews = new Map();
|
|
275
|
+
/** @type {Set<string>} review IDs for duplicate detection */
|
|
276
|
+
#reviewIds = new Set();
|
|
277
|
+
/** @type {MarketplaceIndex} */
|
|
278
|
+
#index = new MarketplaceIndex();
|
|
279
|
+
|
|
280
|
+
// Callbacks
|
|
281
|
+
#onPublish = [];
|
|
282
|
+
#onUnpublish = [];
|
|
283
|
+
#onReview = [];
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @param {object} opts
|
|
287
|
+
* @param {string} opts.localPodId
|
|
288
|
+
*/
|
|
289
|
+
constructor({ localPodId } = {}) {
|
|
290
|
+
if (!localPodId || typeof localPodId !== 'string') {
|
|
291
|
+
throw new Error('localPodId is required and must be a non-empty string');
|
|
292
|
+
}
|
|
293
|
+
this.#localPodId = localPodId;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @returns {string} */
|
|
297
|
+
get localPodId() {
|
|
298
|
+
return this.#localPodId;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// -- Listing lifecycle ----------------------------------------------------
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Publish a service listing.
|
|
305
|
+
* @param {ServiceListing} listing
|
|
306
|
+
* @returns {string} listing ID
|
|
307
|
+
*/
|
|
308
|
+
publish(listing) {
|
|
309
|
+
if (this.#listings.has(listing.id)) {
|
|
310
|
+
throw new Error(`Listing "${listing.id}" already exists`);
|
|
311
|
+
}
|
|
312
|
+
this.#listings.set(listing.id, listing);
|
|
313
|
+
this.#index.addListing(listing);
|
|
314
|
+
for (const cb of this.#onPublish) cb(listing);
|
|
315
|
+
return listing.id;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Remove a listing (only if owner).
|
|
320
|
+
* @param {string} listingId
|
|
321
|
+
* @returns {boolean}
|
|
322
|
+
*/
|
|
323
|
+
unpublish(listingId) {
|
|
324
|
+
const listing = this.#listings.get(listingId);
|
|
325
|
+
if (!listing) return false;
|
|
326
|
+
if (listing.providerPodId !== this.#localPodId) {
|
|
327
|
+
throw new Error('Not the owner of this listing');
|
|
328
|
+
}
|
|
329
|
+
this.#listings.delete(listingId);
|
|
330
|
+
this.#index.removeListing(listingId);
|
|
331
|
+
for (const cb of this.#onUnpublish) cb(listingId);
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Update a listing (only if owner).
|
|
337
|
+
* @param {string} listingId
|
|
338
|
+
* @param {object} updates
|
|
339
|
+
*/
|
|
340
|
+
update(listingId, updates) {
|
|
341
|
+
const listing = this.#listings.get(listingId);
|
|
342
|
+
if (!listing) throw new Error(`Listing "${listingId}" not found`);
|
|
343
|
+
if (listing.providerPodId !== this.#localPodId) {
|
|
344
|
+
throw new Error('Not the owner of this listing');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Remove from index before update
|
|
348
|
+
this.#index.removeListing(listingId);
|
|
349
|
+
|
|
350
|
+
// Apply updates
|
|
351
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
352
|
+
if (key === 'id') continue; // Cannot change ID
|
|
353
|
+
listing[key] = value;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Re-index
|
|
357
|
+
this.#index.addListing(listing);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// -- Query ----------------------------------------------------------------
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Search listings with multiple criteria.
|
|
364
|
+
* @param {object} query
|
|
365
|
+
* @param {string} [query.text]
|
|
366
|
+
* @param {string} [query.category]
|
|
367
|
+
* @param {string[]} [query.tags]
|
|
368
|
+
* @param {number} [query.minPrice]
|
|
369
|
+
* @param {number} [query.maxPrice]
|
|
370
|
+
* @param {number} [query.minRating]
|
|
371
|
+
* @returns {ServiceListing[]}
|
|
372
|
+
*/
|
|
373
|
+
search(query = {}) {
|
|
374
|
+
let results = [];
|
|
375
|
+
|
|
376
|
+
for (const listing of this.#listings.values()) {
|
|
377
|
+
// Only active listings
|
|
378
|
+
if (listing.status !== 'active') continue;
|
|
379
|
+
|
|
380
|
+
// Text search
|
|
381
|
+
if (query.text && !listing.matchesQuery(query.text)) continue;
|
|
382
|
+
|
|
383
|
+
// Category
|
|
384
|
+
if (query.category && listing.category !== query.category) continue;
|
|
385
|
+
|
|
386
|
+
// Tags
|
|
387
|
+
if (query.tags && query.tags.length > 0) {
|
|
388
|
+
const hasAllTags = query.tags.every(t => listing.tags.includes(t));
|
|
389
|
+
if (!hasAllTags) continue;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Price range
|
|
393
|
+
if (query.minPrice !== undefined && listing.pricing.amount < query.minPrice) continue;
|
|
394
|
+
if (query.maxPrice !== undefined && listing.pricing.amount > query.maxPrice) continue;
|
|
395
|
+
|
|
396
|
+
// Minimum rating
|
|
397
|
+
if (query.minRating !== undefined) {
|
|
398
|
+
const avg = this.getAverageRating(listing.id);
|
|
399
|
+
if (avg < query.minRating) continue;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
results.push(listing);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return results;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Get a listing by ID.
|
|
410
|
+
* @param {string} id
|
|
411
|
+
* @returns {ServiceListing|null}
|
|
412
|
+
*/
|
|
413
|
+
getListingById(id) {
|
|
414
|
+
return this.#listings.get(id) ?? null;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Get all listings by a specific provider.
|
|
419
|
+
* @param {string} podId
|
|
420
|
+
* @returns {ServiceListing[]}
|
|
421
|
+
*/
|
|
422
|
+
getListingsByProvider(podId) {
|
|
423
|
+
const results = [];
|
|
424
|
+
for (const listing of this.#listings.values()) {
|
|
425
|
+
if (listing.providerPodId === podId) results.push(listing);
|
|
426
|
+
}
|
|
427
|
+
return results;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// -- Reviews --------------------------------------------------------------
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Add a review for a listing.
|
|
434
|
+
* @param {ServiceReview} review
|
|
435
|
+
*/
|
|
436
|
+
addReview(review) {
|
|
437
|
+
// Check listing exists
|
|
438
|
+
const listing = this.#listings.get(review.listingId);
|
|
439
|
+
if (!listing) throw new Error('Listing not found');
|
|
440
|
+
|
|
441
|
+
// Prevent self-review
|
|
442
|
+
if (review.reviewerPodId === listing.providerPodId) {
|
|
443
|
+
throw new Error('Self-review is not allowed');
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Check for duplicate review ID
|
|
447
|
+
if (this.#reviewIds.has(review.id)) {
|
|
448
|
+
throw new Error(`Review "${review.id}" already exists`);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (!this.#reviews.has(review.listingId)) {
|
|
452
|
+
this.#reviews.set(review.listingId, []);
|
|
453
|
+
}
|
|
454
|
+
this.#reviews.get(review.listingId).push(review);
|
|
455
|
+
this.#reviewIds.add(review.id);
|
|
456
|
+
|
|
457
|
+
for (const cb of this.#onReview) cb(review);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Get all reviews for a listing.
|
|
462
|
+
* @param {string} listingId
|
|
463
|
+
* @returns {ServiceReview[]}
|
|
464
|
+
*/
|
|
465
|
+
getReviews(listingId) {
|
|
466
|
+
return this.#reviews.get(listingId) ?? [];
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Calculate the average rating for a listing.
|
|
471
|
+
* @param {string} listingId
|
|
472
|
+
* @returns {number} 0 if no reviews
|
|
473
|
+
*/
|
|
474
|
+
getAverageRating(listingId) {
|
|
475
|
+
const reviews = this.#reviews.get(listingId);
|
|
476
|
+
if (!reviews || reviews.length === 0) return 0;
|
|
477
|
+
const sum = reviews.reduce((acc, r) => acc + r.rating, 0);
|
|
478
|
+
return sum / reviews.length;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// -- Categories -----------------------------------------------------------
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Get all distinct categories.
|
|
485
|
+
* @returns {string[]}
|
|
486
|
+
*/
|
|
487
|
+
getCategories() {
|
|
488
|
+
const cats = new Set();
|
|
489
|
+
for (const listing of this.#listings.values()) {
|
|
490
|
+
cats.add(listing.category);
|
|
491
|
+
}
|
|
492
|
+
return [...cats];
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// -- Featured -------------------------------------------------------------
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Get top-rated active listings.
|
|
499
|
+
* @param {number} [limit=10]
|
|
500
|
+
* @returns {ServiceListing[]}
|
|
501
|
+
*/
|
|
502
|
+
getFeatured(limit = 10) {
|
|
503
|
+
const active = [];
|
|
504
|
+
for (const listing of this.#listings.values()) {
|
|
505
|
+
if (listing.status !== 'active') continue;
|
|
506
|
+
const avg = this.getAverageRating(listing.id);
|
|
507
|
+
active.push({ listing, avg });
|
|
508
|
+
}
|
|
509
|
+
active.sort((a, b) => b.avg - a.avg);
|
|
510
|
+
return active.slice(0, limit).map(e => e.listing);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// -- Callbacks ------------------------------------------------------------
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* @param {function(ServiceListing)} cb
|
|
517
|
+
*/
|
|
518
|
+
onPublish(cb) {
|
|
519
|
+
this.#onPublish.push(cb);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* @param {function(string)} cb - receives listingId
|
|
524
|
+
*/
|
|
525
|
+
onUnpublish(cb) {
|
|
526
|
+
this.#onUnpublish.push(cb);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* @param {function(ServiceReview)} cb
|
|
531
|
+
*/
|
|
532
|
+
onReview(cb) {
|
|
533
|
+
this.#onReview.push(cb);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// -- Stats ----------------------------------------------------------------
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* @returns {{ totalListings: number, activeListings: number, totalReviews: number, avgRating: number }}
|
|
540
|
+
*/
|
|
541
|
+
getStats() {
|
|
542
|
+
let activeCount = 0;
|
|
543
|
+
let totalReviewCount = 0;
|
|
544
|
+
let ratingSum = 0;
|
|
545
|
+
|
|
546
|
+
for (const listing of this.#listings.values()) {
|
|
547
|
+
if (listing.status === 'active') activeCount++;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
for (const reviews of this.#reviews.values()) {
|
|
551
|
+
totalReviewCount += reviews.length;
|
|
552
|
+
for (const r of reviews) {
|
|
553
|
+
ratingSum += r.rating;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return {
|
|
558
|
+
totalListings: this.#listings.size,
|
|
559
|
+
activeListings: activeCount,
|
|
560
|
+
totalReviews: totalReviewCount,
|
|
561
|
+
avgRating: totalReviewCount > 0 ? ratingSum / totalReviewCount : 0,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// -- Serialization --------------------------------------------------------
|
|
566
|
+
|
|
567
|
+
toJSON() {
|
|
568
|
+
const listings = [];
|
|
569
|
+
for (const l of this.#listings.values()) {
|
|
570
|
+
listings.push(l.toJSON());
|
|
571
|
+
}
|
|
572
|
+
const reviews = {};
|
|
573
|
+
for (const [listingId, revs] of this.#reviews) {
|
|
574
|
+
reviews[listingId] = revs.map(r => r.toJSON());
|
|
575
|
+
}
|
|
576
|
+
return {
|
|
577
|
+
localPodId: this.#localPodId,
|
|
578
|
+
listings,
|
|
579
|
+
reviews,
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* @param {object} data
|
|
585
|
+
* @returns {Marketplace}
|
|
586
|
+
*/
|
|
587
|
+
static fromJSON(data) {
|
|
588
|
+
const mp = new Marketplace({ localPodId: data.localPodId });
|
|
589
|
+
|
|
590
|
+
// Restore listings
|
|
591
|
+
if (data.listings) {
|
|
592
|
+
for (const ld of data.listings) {
|
|
593
|
+
const listing = ServiceListing.fromJSON(ld);
|
|
594
|
+
mp.#listings.set(listing.id, listing);
|
|
595
|
+
mp.#index.addListing(listing);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// Restore reviews
|
|
600
|
+
if (data.reviews) {
|
|
601
|
+
for (const [listingId, revs] of Object.entries(data.reviews)) {
|
|
602
|
+
const reviewList = revs.map(rd => ServiceReview.fromJSON(rd));
|
|
603
|
+
mp.#reviews.set(listingId, reviewList);
|
|
604
|
+
for (const r of reviewList) {
|
|
605
|
+
mp.#reviewIds.add(r.id);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
return mp;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// ---------------------------------------------------------------------------
|
|
615
|
+
// MarketplaceIndex
|
|
616
|
+
// ---------------------------------------------------------------------------
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Efficient inverted index for marketplace queries.
|
|
620
|
+
*/
|
|
621
|
+
export class MarketplaceIndex {
|
|
622
|
+
/** @type {Map<string, Set<string>>} category -> listing IDs */
|
|
623
|
+
#byCategory = new Map();
|
|
624
|
+
/** @type {Map<string, Set<string>>} tag -> listing IDs */
|
|
625
|
+
#byTag = new Map();
|
|
626
|
+
/** @type {Map<string, Set<string>>} providerPodId -> listing IDs */
|
|
627
|
+
#byProvider = new Map();
|
|
628
|
+
/** @type {Map<string, ServiceListing>} id -> listing (for full-text search) */
|
|
629
|
+
#listingCache = new Map();
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Index a listing.
|
|
633
|
+
* @param {ServiceListing} listing
|
|
634
|
+
*/
|
|
635
|
+
addListing(listing) {
|
|
636
|
+
// Cache for full-text search
|
|
637
|
+
this.#listingCache.set(listing.id, listing);
|
|
638
|
+
|
|
639
|
+
// Category index
|
|
640
|
+
if (!this.#byCategory.has(listing.category)) {
|
|
641
|
+
this.#byCategory.set(listing.category, new Set());
|
|
642
|
+
}
|
|
643
|
+
this.#byCategory.get(listing.category).add(listing.id);
|
|
644
|
+
|
|
645
|
+
// Tag indexes
|
|
646
|
+
for (const tag of listing.tags) {
|
|
647
|
+
if (!this.#byTag.has(tag)) {
|
|
648
|
+
this.#byTag.set(tag, new Set());
|
|
649
|
+
}
|
|
650
|
+
this.#byTag.get(tag).add(listing.id);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Provider index
|
|
654
|
+
if (!this.#byProvider.has(listing.providerPodId)) {
|
|
655
|
+
this.#byProvider.set(listing.providerPodId, new Set());
|
|
656
|
+
}
|
|
657
|
+
this.#byProvider.get(listing.providerPodId).add(listing.id);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Remove a listing from all indexes.
|
|
662
|
+
* @param {string} listingId
|
|
663
|
+
*/
|
|
664
|
+
removeListing(listingId) {
|
|
665
|
+
const listing = this.#listingCache.get(listingId);
|
|
666
|
+
if (!listing) return;
|
|
667
|
+
|
|
668
|
+
// Category
|
|
669
|
+
const catSet = this.#byCategory.get(listing.category);
|
|
670
|
+
if (catSet) {
|
|
671
|
+
catSet.delete(listingId);
|
|
672
|
+
if (catSet.size === 0) this.#byCategory.delete(listing.category);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// Tags
|
|
676
|
+
for (const tag of listing.tags) {
|
|
677
|
+
const tagSet = this.#byTag.get(tag);
|
|
678
|
+
if (tagSet) {
|
|
679
|
+
tagSet.delete(listingId);
|
|
680
|
+
if (tagSet.size === 0) this.#byTag.delete(tag);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// Provider
|
|
685
|
+
const provSet = this.#byProvider.get(listing.providerPodId);
|
|
686
|
+
if (provSet) {
|
|
687
|
+
provSet.delete(listingId);
|
|
688
|
+
if (provSet.size === 0) this.#byProvider.delete(listing.providerPodId);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
this.#listingCache.delete(listingId);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* O(1) lookup by category.
|
|
696
|
+
* @param {string} category
|
|
697
|
+
* @returns {string[]} listing IDs
|
|
698
|
+
*/
|
|
699
|
+
queryByCategory(category) {
|
|
700
|
+
const set = this.#byCategory.get(category);
|
|
701
|
+
return set ? [...set] : [];
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* O(1) lookup by tag.
|
|
706
|
+
* @param {string} tag
|
|
707
|
+
* @returns {string[]} listing IDs
|
|
708
|
+
*/
|
|
709
|
+
queryByTag(tag) {
|
|
710
|
+
const set = this.#byTag.get(tag);
|
|
711
|
+
return set ? [...set] : [];
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* O(1) lookup by provider.
|
|
716
|
+
* @param {string} podId
|
|
717
|
+
* @returns {string[]} listing IDs
|
|
718
|
+
*/
|
|
719
|
+
queryByProvider(podId) {
|
|
720
|
+
const set = this.#byProvider.get(podId);
|
|
721
|
+
return set ? [...set] : [];
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Full-text search across name, description, and tags.
|
|
726
|
+
* @param {string} text
|
|
727
|
+
* @returns {string[]} matching listing IDs (deduplicated)
|
|
728
|
+
*/
|
|
729
|
+
fullTextSearch(text) {
|
|
730
|
+
if (!text || text.length === 0) return [];
|
|
731
|
+
|
|
732
|
+
const q = text.toLowerCase();
|
|
733
|
+
const matched = new Set();
|
|
734
|
+
|
|
735
|
+
for (const [id, listing] of this.#listingCache) {
|
|
736
|
+
if (listing.name.toLowerCase().includes(q)) {
|
|
737
|
+
matched.add(id);
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
if (listing.description.toLowerCase().includes(q)) {
|
|
741
|
+
matched.add(id);
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
for (const tag of listing.tags) {
|
|
745
|
+
if (tag.toLowerCase().includes(q)) {
|
|
746
|
+
matched.add(id);
|
|
747
|
+
break;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
return [...matched];
|
|
753
|
+
}
|
|
754
|
+
}
|