@seedprotocol/sdk 0.6.6 → 0.6.8
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/{BasePathResolver-BGxb4Qod.js → BasePathResolver-B6CKqoKw.js} +137 -4
- package/dist/BasePathResolver-B6CKqoKw.js.map +1 -0
- package/dist/{index-CJNKYI37.js → ClientManager-ChcmUL47.js} +1928 -5543
- package/dist/ClientManager-ChcmUL47.js.map +1 -0
- package/dist/{ModelProperty-Bvg9cJcd.js → ModelProperty-D79uV42p.js} +42 -36
- package/dist/ModelProperty-D79uV42p.js.map +1 -0
- package/dist/{Schema-BNJBY80k.js → Schema-a8uO-hIZ.js} +48 -47
- package/dist/Schema-a8uO-hIZ.js.map +1 -0
- package/dist/{SchemaValidationService-aqXkwXTr.js → SchemaValidationService-Y49vqast.js} +16 -12
- package/dist/SchemaValidationService-Y49vqast.js.map +1 -0
- package/dist/{backfillMetadataPropertyIds-DYkvoOMz.js → backfillMetadataPropertyIds-CBsf7LnU.js} +6 -3
- package/dist/{backfillMetadataPropertyIds-DYkvoOMz.js.map → backfillMetadataPropertyIds-CBsf7LnU.js.map} +1 -1
- package/dist/cjs/{ModelProperty-alntkSnH.js → ModelProperty-D9FGaGuE.js} +25 -20
- package/dist/cjs/ModelProperty-D9FGaGuE.js.map +1 -0
- package/dist/cjs/{Schema-ByFv6fcb.js → Schema-BiJIPLEL.js} +29 -27
- package/dist/cjs/Schema-BiJIPLEL.js.map +1 -0
- package/dist/cjs/{SchemaValidationService-D55MWsxv.js → SchemaValidationService-DtYs_CNY.js} +9 -6
- package/dist/cjs/SchemaValidationService-DtYs_CNY.js.map +1 -0
- package/dist/cjs/{backfillMetadataPropertyIds-DrrL5RqD.js → backfillMetadataPropertyIds-2XaN3b9S.js} +7 -5
- package/dist/cjs/{backfillMetadataPropertyIds-DrrL5RqD.js.map → backfillMetadataPropertyIds-2XaN3b9S.js.map} +1 -1
- package/dist/cjs/{eas-D2E3_i_c.js → eas-CMNppmGE.js} +32 -6
- package/dist/cjs/eas-CMNppmGE.js.map +1 -0
- package/dist/cjs/{getItem-B2zLVB_l.js → getItem-B6jEINjW.js} +6 -4
- package/dist/cjs/{getItem-B2zLVB_l.js.map → getItem-B6jEINjW.js.map} +1 -1
- package/dist/cjs/{getPublishPayload-Bv94k4ak.js → getPublishPayload-D5DhLGvN.js} +66 -60
- package/dist/cjs/getPublishPayload-D5DhLGvN.js.map +1 -0
- package/dist/cjs/{getPublishUploads-CSbHUxYk.js → getPublishUploads-DMhCCKYF.js} +21 -13
- package/dist/cjs/getPublishUploads-DMhCCKYF.js.map +1 -0
- package/dist/cjs/{getSegmentedItemProperties-CMhXKQHz.js → getSegmentedItemProperties-CU8rtBAo.js} +3 -51
- package/dist/cjs/getSegmentedItemProperties-CU8rtBAo.js.map +1 -0
- package/dist/cjs/{index-D-9WglxG.js → index-CSNtEnbF.js} +5986 -5389
- package/dist/cjs/index-CSNtEnbF.js.map +1 -0
- package/dist/cjs/{index-pSLjO-ch.js → index-DPp9rdRL.js} +10 -6
- package/dist/cjs/index-DPp9rdRL.js.map +1 -0
- package/dist/cjs/{property-gUK2PijH.js → property-DlKiAWt4.js} +11 -7
- package/dist/cjs/{property-gUK2PijH.js.map → property-DlKiAWt4.js.map} +1 -1
- package/dist/cjs/{syncDbWithEas-CYtl7VTD.js → syncDbWithEas-C6n34o_Q.js} +14 -8
- package/dist/cjs/syncDbWithEas-C6n34o_Q.js.map +1 -0
- package/dist/{configurePlatform-bGvKBBQy.js → configurePlatform-CE0Vew17.js} +6 -3
- package/dist/{configurePlatform-bGvKBBQy.js.map → configurePlatform-CE0Vew17.js.map} +1 -1
- package/dist/db/drizzle/0014_add_error_code_to_publish_processes.sql +1 -0
- package/dist/db/drizzle/meta/0014_snapshot.json +1269 -0
- package/dist/db/drizzle/meta/_journal.json +7 -0
- package/dist/{db-YiH_kIpE.js → db-WbrzEf7c.js} +266 -53
- package/dist/db-WbrzEf7c.js.map +1 -0
- package/dist/{eas-1nUCcmeh.js → eas-CfYCu4IX.js} +9 -6
- package/dist/eas-CfYCu4IX.js.map +1 -0
- package/dist/generateId-BopUwD8F.js +9 -0
- package/dist/generateId-BopUwD8F.js.map +1 -0
- package/dist/{getItem-Bb8bTYzY.js → getItem-Br-bGNFO.js} +12 -10
- package/dist/{getItem-Bb8bTYzY.js.map → getItem-Br-bGNFO.js.map} +1 -1
- package/dist/{getPublishPayload-BHw8mPSb.js → getPublishPayload-BIrTVUxb.js} +72 -65
- package/dist/getPublishPayload-BIrTVUxb.js.map +1 -0
- package/dist/{getPublishUploads-BYhckxFl.js → getPublishUploads-C6cDeZdz.js} +22 -13
- package/dist/getPublishUploads-C6cDeZdz.js.map +1 -0
- package/dist/{getSegmentedItemProperties-B73nMPBv.js → getSegmentedItemProperties-BmKE71vU.js} +5 -5
- package/dist/{getSegmentedItemProperties-B73nMPBv.js.map → getSegmentedItemProperties-BmKE71vU.js.map} +1 -1
- package/dist/index-280bL-n4.js +1122 -0
- package/dist/index-280bL-n4.js.map +1 -0
- package/dist/{index-DzgMt8ba.js → index-ClSv5pTA.js} +17 -14
- package/dist/index-ClSv5pTA.js.map +1 -0
- package/dist/{index-C_5xwOPQ.js → index-Djd3667v.js} +2 -2
- package/dist/index-Djd3667v.js.map +1 -0
- package/dist/index-HRHkbVQd.js +3492 -0
- package/dist/index-HRHkbVQd.js.map +1 -0
- package/dist/{index.node-ZLiBj9Cu.js → index.node-DBtQkEfz.js} +8 -7
- package/dist/index.node-DBtQkEfz.js.map +1 -0
- package/dist/main.cjs +10 -4
- package/dist/main.cjs.map +1 -1
- package/dist/main.js +43 -43
- package/dist/main.js.map +1 -1
- package/dist/model-DGMe-TTm.js +25 -0
- package/dist/model-DGMe-TTm.js.map +1 -0
- package/dist/node.js +21 -17
- package/dist/node.js.map +1 -1
- package/dist/platform.browser.js +162 -63
- package/dist/platform.browser.js.map +1 -1
- package/dist/platform.node.js +5 -5
- package/dist/{property-Dcb50seV.js → property-CQjuZ4JW.js} +20 -17
- package/dist/property-CQjuZ4JW.js.map +1 -0
- package/dist/seedSchema/PublishProcessSchema.ts +1 -0
- package/dist/src/Item/Item.d.ts +6 -0
- package/dist/src/Item/Item.d.ts.map +1 -1
- package/dist/src/Item/service/actors/loadOrCreateItem.d.ts.map +1 -1
- package/dist/src/ItemProperty/ItemProperty.d.ts +10 -1
- package/dist/src/ItemProperty/ItemProperty.d.ts.map +1 -1
- package/dist/src/ItemProperty/service/actors/hydrateFromDb.d.ts.map +1 -1
- package/dist/src/ItemProperty/service/actors/loadOrCreateProperty.d.ts.map +1 -1
- package/dist/src/ItemProperty/service/actors/resolveRelatedValue.d.ts.map +1 -1
- package/dist/src/ItemProperty/service/actors/saveValueToDb/saveRelation.d.ts.map +1 -1
- package/dist/src/ItemProperty/service/propertyMachine.d.ts.map +1 -1
- package/dist/src/Model/service/actors/loadOrCreateModel.d.ts.map +1 -1
- package/dist/src/ModelProperty/ModelProperty.d.ts.map +1 -1
- package/dist/src/Schema/service/actors/loadOrCreateSchema.d.ts.map +1 -1
- package/dist/src/Schema/service/actors/writeModelsToDb.d.ts.map +1 -1
- package/dist/src/Schema/service/addModelsMachine.d.ts.map +1 -1
- package/dist/src/Schema/service/validation/SchemaValidationService.d.ts.map +1 -1
- package/dist/src/browser/db/Db.d.ts.map +1 -1
- package/dist/src/browser/db/drizzleFiles.d.ts +3 -2
- package/dist/src/browser/db/drizzleFiles.d.ts.map +1 -1
- package/dist/src/browser/db/embeddedMigrations.d.ts +17 -0
- package/dist/src/browser/db/embeddedMigrations.d.ts.map +1 -0
- package/dist/src/browser/helpers/ArweaveClient.d.ts.map +1 -1
- package/dist/src/browser/helpers/EasClient.d.ts +2 -1
- package/dist/src/browser/helpers/EasClient.d.ts.map +1 -1
- package/dist/src/browser/helpers/FileManager.d.ts +1 -1
- package/dist/src/browser/helpers/FileManager.d.ts.map +1 -1
- package/dist/src/browser/workers/FileDownloader.d.ts +1 -1
- package/dist/src/browser/workers/FileDownloader.d.ts.map +1 -1
- package/dist/src/browser/workers/filesDownload.d.ts.map +1 -1
- package/dist/src/client/ClientManager.d.ts +18 -3
- package/dist/src/client/ClientManager.d.ts.map +1 -1
- package/dist/src/client/actors/platformClassesInit.d.ts.map +1 -1
- package/dist/src/client/actors/saveConfig.d.ts.map +1 -1
- package/dist/src/client/clientManagerMachine.d.ts +6 -1
- package/dist/src/client/clientManagerMachine.d.ts.map +1 -1
- package/dist/src/client/constants.d.ts +4 -0
- package/dist/src/client/constants.d.ts.map +1 -1
- package/dist/src/client/nodeEndpointDefaults.d.ts +8 -0
- package/dist/src/client/nodeEndpointDefaults.d.ts.map +1 -0
- package/dist/src/db/read/getPublishPayload.d.ts.map +1 -1
- package/dist/src/db/read/getPublishUploads.d.ts.map +1 -1
- package/dist/src/db/read/getRelatedItemsForPublish.d.ts.map +1 -1
- package/dist/src/db/write/createMetadata.d.ts.map +1 -1
- package/dist/src/eas.d.ts +2 -2
- package/dist/src/eas.d.ts.map +1 -1
- package/dist/src/events/files/download.d.ts.map +1 -1
- package/dist/src/events/item/syncDbWithEas.d.ts.map +1 -1
- package/dist/src/helpers/ArweaveClient/index.d.ts.map +1 -1
- package/dist/src/helpers/ArweaveClient/selectReadGateway.d.ts +1 -1
- package/dist/src/helpers/ArweaveClient/selectReadGateway.d.ts.map +1 -1
- package/dist/src/helpers/FileManager/BaseFileManager.d.ts +2 -2
- package/dist/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
- package/dist/src/helpers/FileManager/IFileManager.d.ts +4 -0
- package/dist/src/helpers/FileManager/IFileManager.d.ts.map +1 -1
- package/dist/src/helpers/constants.d.ts +9 -2
- package/dist/src/helpers/constants.d.ts.map +1 -1
- package/dist/src/helpers/crypto.d.ts +3 -2
- package/dist/src/helpers/crypto.d.ts.map +1 -1
- package/dist/src/helpers/db.d.ts +6 -0
- package/dist/src/helpers/db.d.ts.map +1 -1
- package/dist/src/helpers/ensureImageLocal.d.ts +24 -0
- package/dist/src/helpers/ensureImageLocal.d.ts.map +1 -0
- package/dist/src/helpers/ethereumUtils.d.ts.map +1 -1
- package/dist/src/helpers/gateway/gatewayState.d.ts +1 -1
- package/dist/src/helpers/gateway/gatewayState.d.ts.map +1 -1
- package/dist/src/helpers/index.d.ts +10 -2
- package/dist/src/helpers/index.d.ts.map +1 -1
- package/dist/src/helpers/localDbChain.d.ts +22 -0
- package/dist/src/helpers/localDbChain.d.ts.map +1 -0
- package/dist/src/helpers/metadataPropertyNames.d.ts +9 -0
- package/dist/src/helpers/metadataPropertyNames.d.ts.map +1 -1
- package/dist/src/helpers/property/index.d.ts +12 -0
- package/dist/src/helpers/property/index.d.ts.map +1 -1
- package/dist/src/helpers/readHtmlBodyForStorageSeed.d.ts.map +1 -1
- package/dist/src/helpers/relationSeedRef.d.ts +8 -0
- package/dist/src/helpers/relationSeedRef.d.ts.map +1 -1
- package/dist/src/imports/json.d.ts.map +1 -1
- package/dist/src/index.d.ts +11 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/node/easLodashHook.d.ts +2 -0
- package/dist/src/node/easLodashHook.d.ts.map +1 -0
- package/dist/src/node/easLodashRegister.d.ts +2 -0
- package/dist/src/node/easLodashRegister.d.ts.map +1 -0
- package/dist/src/node/helpers/FileManager.d.ts +2 -1
- package/dist/src/node/helpers/FileManager.d.ts.map +1 -1
- package/dist/src/node/index.d.ts +2 -1
- package/dist/src/node/index.d.ts.map +1 -1
- package/dist/src/seedSchema/PublishProcessSchema.d.ts +19 -0
- package/dist/src/seedSchema/PublishProcessSchema.d.ts.map +1 -1
- package/dist/src/types/fileManager.d.ts +4 -0
- package/dist/src/types/fileManager.d.ts.map +1 -1
- package/dist/src/types/index.d.ts +15 -1
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/{syncDbWithEas-B0RUeDuj.js → syncDbWithEas-CuARXfyN.js} +24 -18
- package/dist/syncDbWithEas-CuARXfyN.js.map +1 -0
- package/dist/{updateSeedRevokedAt-D5ZbBehZ.js → updateSeedRevokedAt-CBcLfHWs.js} +2 -2
- package/dist/{updateSeedRevokedAt-D5ZbBehZ.js.map → updateSeedRevokedAt-CBcLfHWs.js.map} +1 -1
- package/package.json +12 -8
- package/src/node/easLodashHook.js +22 -0
- package/src/node/easLodashRegister.js +13 -0
- package/dist/BasePathResolver-BGxb4Qod.js.map +0 -1
- package/dist/ModelProperty-Bvg9cJcd.js.map +0 -1
- package/dist/Schema-BNJBY80k.js.map +0 -1
- package/dist/SchemaValidationService-aqXkwXTr.js.map +0 -1
- package/dist/cjs/ModelProperty-alntkSnH.js.map +0 -1
- package/dist/cjs/Schema-ByFv6fcb.js.map +0 -1
- package/dist/cjs/SchemaValidationService-D55MWsxv.js.map +0 -1
- package/dist/cjs/eas-D2E3_i_c.js.map +0 -1
- package/dist/cjs/getPublishPayload-Bv94k4ak.js.map +0 -1
- package/dist/cjs/getPublishUploads-CSbHUxYk.js.map +0 -1
- package/dist/cjs/getSegmentedItemProperties-CMhXKQHz.js.map +0 -1
- package/dist/cjs/index-D-9WglxG.js.map +0 -1
- package/dist/cjs/index-pSLjO-ch.js.map +0 -1
- package/dist/cjs/syncDbWithEas-CYtl7VTD.js.map +0 -1
- package/dist/constants-Bre6dTJR.js +0 -122
- package/dist/constants-Bre6dTJR.js.map +0 -1
- package/dist/constants-CTs2RUrM.js +0 -657
- package/dist/constants-CTs2RUrM.js.map +0 -1
- package/dist/db-YiH_kIpE.js.map +0 -1
- package/dist/eas-1nUCcmeh.js.map +0 -1
- package/dist/getPublishPayload-BHw8mPSb.js.map +0 -1
- package/dist/getPublishUploads-BYhckxFl.js.map +0 -1
- package/dist/index-CJNKYI37.js.map +0 -1
- package/dist/index-C_5xwOPQ.js.map +0 -1
- package/dist/index-DzgMt8ba.js.map +0 -1
- package/dist/index.node-ZLiBj9Cu.js.map +0 -1
- package/dist/property-Dcb50seV.js.map +0 -1
- package/dist/seedSchema/AppStateSchema.d.ts +0 -82
- package/dist/seedSchema/AppStateSchema.d.ts.map +0 -1
- package/dist/seedSchema/ConfigSchema.d.ts +0 -101
- package/dist/seedSchema/ConfigSchema.d.ts.map +0 -1
- package/dist/seedSchema/MetadataSchema.d.ts +0 -422
- package/dist/seedSchema/MetadataSchema.d.ts.map +0 -1
- package/dist/seedSchema/ModelSchema.d.ts +0 -243
- package/dist/seedSchema/ModelSchema.d.ts.map +0 -1
- package/dist/seedSchema/ModelSchemaSchema.d.ts +0 -61
- package/dist/seedSchema/ModelSchemaSchema.d.ts.map +0 -1
- package/dist/seedSchema/ModelUidSchema.d.ts +0 -64
- package/dist/seedSchema/ModelUidSchema.d.ts.map +0 -1
- package/dist/seedSchema/PropertyUidSchema.d.ts +0 -64
- package/dist/seedSchema/PropertyUidSchema.d.ts.map +0 -1
- package/dist/seedSchema/SchemaSchema.d.ts +0 -169
- package/dist/seedSchema/SchemaSchema.d.ts.map +0 -1
- package/dist/seedSchema/SeedSchema.d.ts +0 -192
- package/dist/seedSchema/SeedSchema.d.ts.map +0 -1
- package/dist/seedSchema/VersionSchema.d.ts +0 -194
- package/dist/seedSchema/VersionSchema.d.ts.map +0 -1
- package/dist/seedSchema/index.d.ts +0 -14
- package/dist/seedSchema/index.d.ts.map +0 -1
- package/dist/syncDbWithEas-B0RUeDuj.js.map +0 -1
|
@@ -0,0 +1,1122 @@
|
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
import { a as BaseDb, b as metadata, B as BaseFileManager, c as appState, C as ImageSize } from './BasePathResolver-B6CKqoKw.js';
|
|
3
|
+
import { isEasAttestationExplorerUrl, BaseArweaveClient, ensureReadGatewaySelected, getArweaveReadBaseUrls } from '@seedprotocol/arweave';
|
|
4
|
+
import { BaseQueryClient, BaseEasClient, GET_FILES_METADATA } from '@seedprotocol/eas';
|
|
5
|
+
import { waitFor } from 'xstate';
|
|
6
|
+
import 'pluralize';
|
|
7
|
+
import { p as getAllAddressesFromDb } from './db-WbrzEf7c.js';
|
|
8
|
+
import { and, eq, or, isNotNull } from 'drizzle-orm';
|
|
9
|
+
import 'nanoid';
|
|
10
|
+
import 'nanoid-dictionary';
|
|
11
|
+
import '@seedprotocol/query';
|
|
12
|
+
import { throttle } from 'lodash-es';
|
|
13
|
+
|
|
14
|
+
/** Same rules as getCorrectId in helpers/index (avoid barrel circular import). */
|
|
15
|
+
function parseLocalIdOrUid(localIdOrUid) {
|
|
16
|
+
const id = { localId: undefined, uid: undefined };
|
|
17
|
+
if (!localIdOrUid)
|
|
18
|
+
return id;
|
|
19
|
+
if (localIdOrUid.length === 10) {
|
|
20
|
+
id.localId = localIdOrUid;
|
|
21
|
+
}
|
|
22
|
+
if (localIdOrUid.startsWith('0x') && localIdOrUid.length === 66) {
|
|
23
|
+
id.uid = localIdOrUid;
|
|
24
|
+
}
|
|
25
|
+
return id;
|
|
26
|
+
}
|
|
27
|
+
/** Relation/image refs may be string ids or `{ seedLocalId?, seedUid? }`. */
|
|
28
|
+
function normalizeRelationPropertyValue(value) {
|
|
29
|
+
if (value == null)
|
|
30
|
+
return undefined;
|
|
31
|
+
if (typeof value === 'string') {
|
|
32
|
+
const t = value.trim();
|
|
33
|
+
return t !== '' ? t : undefined;
|
|
34
|
+
}
|
|
35
|
+
if (typeof value === 'object' && value !== null) {
|
|
36
|
+
const o = value;
|
|
37
|
+
if (typeof o.seedLocalId === 'string' && o.seedLocalId.trim() !== '') {
|
|
38
|
+
return o.seedLocalId.trim();
|
|
39
|
+
}
|
|
40
|
+
if (typeof o.seedUid === 'string' && o.seedUid.trim() !== '') {
|
|
41
|
+
return o.seedUid.trim();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
/** getCorrectId only accepts 10-char local ids and 0x66 uids; local seed refs can be up to 21 chars. */
|
|
47
|
+
const LOCAL_SEED_REF = /^[a-zA-Z0-9_-]{10,21}$/;
|
|
48
|
+
/** Resolve seed ids from a single string (context propertyValue or DB row). */
|
|
49
|
+
function resolveSeedIdsFromRefString(s) {
|
|
50
|
+
const fromCorrect = parseLocalIdOrUid(s);
|
|
51
|
+
if (fromCorrect.localId || fromCorrect.uid) {
|
|
52
|
+
return { seedLocalId: fromCorrect.localId, seedUid: fromCorrect.uid };
|
|
53
|
+
}
|
|
54
|
+
if (LOCAL_SEED_REF.test(s) && !s.startsWith('0x')) {
|
|
55
|
+
return { seedLocalId: s };
|
|
56
|
+
}
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* True when a relation ref is an attested seed uid (0x…) rather than a local id. A uid only exists once
|
|
61
|
+
* the seed is on EAS, so publish can attest the uid as-is when no local copy exists (e.g. a server
|
|
62
|
+
* publishing on someone's behalf that only holds the uids it was given). Callers must not walk into it.
|
|
63
|
+
*/
|
|
64
|
+
function isPublishedSeedRef(value) {
|
|
65
|
+
const ref = normalizeRelationPropertyValue(value);
|
|
66
|
+
if (!ref)
|
|
67
|
+
return false;
|
|
68
|
+
const { seedLocalId, seedUid } = resolveSeedIdsFromRefString(ref);
|
|
69
|
+
return !!seedUid && !seedLocalId;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Storage seed local id (nanoid) or 0x uid. Stricter than resolveSeedIdsFromRefString, which accepts
|
|
73
|
+
* any 10-char string (so a 10-char html snippet like "<p>raw</p>" would pass as a local id).
|
|
74
|
+
*/
|
|
75
|
+
const STORAGE_SEED_REF = /^(?:[A-Za-z0-9_-]{10,21}|0x[0-9a-fA-F]{64})$/;
|
|
76
|
+
/** True when a File/Image/Html value is already a storage seed reference rather than raw content. */
|
|
77
|
+
function isStorageSeedRef(value) {
|
|
78
|
+
return typeof value === 'string' && STORAGE_SEED_REF.test(value.trim());
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const seedUidToStorageTransactionId = new Map();
|
|
82
|
+
const getStorageTransactionIdForSeedUid = async (seedUid) => {
|
|
83
|
+
if (seedUidToStorageTransactionId.has(seedUid)) {
|
|
84
|
+
return seedUidToStorageTransactionId.get(seedUid);
|
|
85
|
+
}
|
|
86
|
+
const appDb = BaseDb.getAppDb();
|
|
87
|
+
const results = (await appDb
|
|
88
|
+
.select({
|
|
89
|
+
storageTransactionId: metadata.propertyValue,
|
|
90
|
+
})
|
|
91
|
+
.from(metadata)
|
|
92
|
+
.where(and(eq(metadata.seedUid, seedUid), eq(metadata.propertyName, 'storageTransactionId'))));
|
|
93
|
+
if (!results || results.length === 0) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
seedUidToStorageTransactionId.set(seedUid, results[0].storageTransactionId);
|
|
97
|
+
return results[0].storageTransactionId;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const logger$3 = debug('seedSdk:helpers:mediaRef');
|
|
101
|
+
/** Typical Arweave transaction id: 43 URL-safe base64 characters. */
|
|
102
|
+
const ARWEAVE_TX_ID = /^[a-zA-Z0-9_-]{43}$/;
|
|
103
|
+
function trimOrEmpty(raw) {
|
|
104
|
+
if (raw == null)
|
|
105
|
+
return '';
|
|
106
|
+
return typeof raw === 'string' ? raw.trim() : String(raw).trim();
|
|
107
|
+
}
|
|
108
|
+
function coalesceStringField(item, key) {
|
|
109
|
+
const v = item[key];
|
|
110
|
+
if (v == null)
|
|
111
|
+
return '';
|
|
112
|
+
if (typeof v === 'string')
|
|
113
|
+
return v.trim();
|
|
114
|
+
if (typeof v === 'number' || typeof v === 'boolean')
|
|
115
|
+
return String(v);
|
|
116
|
+
return '';
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Try JSON relation payloads and plain relation id strings.
|
|
120
|
+
*/
|
|
121
|
+
function unwrapRelationString(raw) {
|
|
122
|
+
const t = raw.trim();
|
|
123
|
+
if (!t)
|
|
124
|
+
return t;
|
|
125
|
+
try {
|
|
126
|
+
const parsed = JSON.parse(t);
|
|
127
|
+
const fromObj = normalizeRelationPropertyValue(parsed);
|
|
128
|
+
if (fromObj)
|
|
129
|
+
return fromObj;
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// not JSON
|
|
133
|
+
}
|
|
134
|
+
return t;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Pure, sync classification of a media-related string from feeds or XML.
|
|
138
|
+
*/
|
|
139
|
+
function classifyMediaRef(raw, options) {
|
|
140
|
+
const s = trimOrEmpty(raw);
|
|
141
|
+
if (!s) {
|
|
142
|
+
return { kind: 'empty' };
|
|
143
|
+
}
|
|
144
|
+
const treatAs = options?.treatAs;
|
|
145
|
+
if (treatAs === 'url') {
|
|
146
|
+
return { kind: 'url', href: s };
|
|
147
|
+
}
|
|
148
|
+
if (treatAs === 'arweaveTx') {
|
|
149
|
+
return { kind: 'arweaveTxId', txId: s };
|
|
150
|
+
}
|
|
151
|
+
if (treatAs === 'seedUid') {
|
|
152
|
+
if (s.startsWith('0x') && s.length === 66) {
|
|
153
|
+
return { kind: 'seedUid', uid: s };
|
|
154
|
+
}
|
|
155
|
+
return { kind: 'unknown', raw: s };
|
|
156
|
+
}
|
|
157
|
+
if (s.startsWith('http://') || s.startsWith('https://') || s.startsWith('blob:') || s.startsWith('data:')) {
|
|
158
|
+
if (isEasAttestationExplorerUrl(s)) {
|
|
159
|
+
return { kind: 'nonMediaUrl', href: s };
|
|
160
|
+
}
|
|
161
|
+
return { kind: 'url', href: s };
|
|
162
|
+
}
|
|
163
|
+
const unwrapped = unwrapRelationString(s);
|
|
164
|
+
const ids = resolveSeedIdsFromRefString(unwrapped);
|
|
165
|
+
if (ids.seedUid) {
|
|
166
|
+
return { kind: 'seedUid', uid: ids.seedUid };
|
|
167
|
+
}
|
|
168
|
+
if (ids.seedLocalId) {
|
|
169
|
+
return { kind: 'seedLocalId', localId: ids.seedLocalId };
|
|
170
|
+
}
|
|
171
|
+
if (unwrapped.startsWith('0x') && unwrapped.length === 66) {
|
|
172
|
+
return { kind: 'seedUid', uid: unwrapped };
|
|
173
|
+
}
|
|
174
|
+
if (ARWEAVE_TX_ID.test(unwrapped) && !unwrapped.startsWith('0x')) {
|
|
175
|
+
return { kind: 'arweaveTxId', txId: unwrapped };
|
|
176
|
+
}
|
|
177
|
+
return { kind: 'unknown', raw: s };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Same as convertTxIdToImage but local to this module to avoid circular imports with helpers/index.
|
|
181
|
+
*/
|
|
182
|
+
async function tryLocalBlobUrlFromTxId(txId) {
|
|
183
|
+
const imageFilePath = BaseFileManager.getFilesPath('images', txId);
|
|
184
|
+
const fileExists = await BaseFileManager.pathExists(imageFilePath);
|
|
185
|
+
if (!fileExists) {
|
|
186
|
+
logger$3(`[tryLocalBlobUrlFromTxId] ${imageFilePath} does not exist`);
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
const buffer = await BaseFileManager.readFileAsBuffer(imageFilePath);
|
|
190
|
+
let arrayBuffer;
|
|
191
|
+
if (buffer instanceof Blob) {
|
|
192
|
+
arrayBuffer = await buffer.arrayBuffer();
|
|
193
|
+
}
|
|
194
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
195
|
+
arrayBuffer = buffer;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
const sliced = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
199
|
+
if (sliced instanceof SharedArrayBuffer) {
|
|
200
|
+
const uint8Array = new Uint8Array(sliced);
|
|
201
|
+
arrayBuffer = uint8Array.buffer.slice(0);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
arrayBuffer = sliced;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const uint = new Uint8Array(arrayBuffer);
|
|
208
|
+
const imageBlob = new Blob([uint]);
|
|
209
|
+
return URL.createObjectURL(imageBlob);
|
|
210
|
+
}
|
|
211
|
+
async function resolveTxId(txId) {
|
|
212
|
+
const local = await tryLocalBlobUrlFromTxId(txId);
|
|
213
|
+
if (local) {
|
|
214
|
+
return { status: 'ready', href: local, source: 'localBlob' };
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
status: 'ready',
|
|
218
|
+
href: BaseArweaveClient.getRawUrl(txId),
|
|
219
|
+
source: 'gateway',
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Resolve a classified media reference to a display URL (https/blob/data), using local files when present.
|
|
224
|
+
*/
|
|
225
|
+
async function resolveMediaRef(raw, options) {
|
|
226
|
+
const classification = classifyMediaRef(raw, options);
|
|
227
|
+
switch (classification.kind) {
|
|
228
|
+
case 'empty':
|
|
229
|
+
return { status: 'empty' };
|
|
230
|
+
case 'url':
|
|
231
|
+
return {
|
|
232
|
+
status: 'ready',
|
|
233
|
+
href: classification.href,
|
|
234
|
+
source: 'direct',
|
|
235
|
+
};
|
|
236
|
+
case 'nonMediaUrl':
|
|
237
|
+
return {
|
|
238
|
+
status: 'unresolved',
|
|
239
|
+
reason: 'non_media_url',
|
|
240
|
+
classification,
|
|
241
|
+
};
|
|
242
|
+
case 'seedLocalId':
|
|
243
|
+
return {
|
|
244
|
+
status: 'unresolved',
|
|
245
|
+
reason: 'seed_local_id_not_portable',
|
|
246
|
+
classification,
|
|
247
|
+
};
|
|
248
|
+
case 'unknown':
|
|
249
|
+
return {
|
|
250
|
+
status: 'unresolved',
|
|
251
|
+
reason: 'unknown_ref',
|
|
252
|
+
classification,
|
|
253
|
+
};
|
|
254
|
+
case 'arweaveTxId':
|
|
255
|
+
return resolveTxId(classification.txId);
|
|
256
|
+
case 'seedUid': {
|
|
257
|
+
const appDb = BaseDb.getAppDb();
|
|
258
|
+
if (!appDb) {
|
|
259
|
+
return {
|
|
260
|
+
status: 'unresolved',
|
|
261
|
+
reason: 'seed_uid_requires_app_db',
|
|
262
|
+
classification,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const storageTransactionId = await getStorageTransactionIdForSeedUid(classification.uid);
|
|
266
|
+
if (!storageTransactionId) {
|
|
267
|
+
return {
|
|
268
|
+
status: 'unresolved',
|
|
269
|
+
reason: 'seed_uid_no_storage_transaction',
|
|
270
|
+
classification,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
return resolveTxId(storageTransactionId);
|
|
274
|
+
}
|
|
275
|
+
default:
|
|
276
|
+
return {
|
|
277
|
+
status: 'unresolved',
|
|
278
|
+
reason: 'unknown_ref',
|
|
279
|
+
classification,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Read a string field from a parsed item, trying camelCase and snake_case keys.
|
|
285
|
+
*/
|
|
286
|
+
function getFeedItemStringField(item, key) {
|
|
287
|
+
const direct = coalesceStringField(item, key);
|
|
288
|
+
if (direct)
|
|
289
|
+
return direct;
|
|
290
|
+
const snake = key.replace(/[A-Z]/g, (l) => `_${l.toLowerCase()}`);
|
|
291
|
+
if (snake !== key) {
|
|
292
|
+
const alt = coalesceStringField(item, snake);
|
|
293
|
+
if (alt)
|
|
294
|
+
return alt;
|
|
295
|
+
}
|
|
296
|
+
const parts = key.split('_');
|
|
297
|
+
if (parts.length > 1) {
|
|
298
|
+
const camel = parts[0] + parts.slice(1).map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join('');
|
|
299
|
+
const alt2 = coalesceStringField(item, camel);
|
|
300
|
+
if (alt2)
|
|
301
|
+
return alt2;
|
|
302
|
+
}
|
|
303
|
+
return '';
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Apply a field manifest to a plain feed item: classify media/file fields; pass through html/text.
|
|
307
|
+
*/
|
|
308
|
+
function normalizeFeedItemFields(item, manifest) {
|
|
309
|
+
const out = {};
|
|
310
|
+
for (const [fieldKey, descriptor] of Object.entries(manifest)) {
|
|
311
|
+
const raw = getFeedItemStringField(item, fieldKey);
|
|
312
|
+
if (!raw) {
|
|
313
|
+
out[fieldKey] = undefined;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (descriptor.role === 'html') {
|
|
317
|
+
out[fieldKey] = { role: 'html', raw };
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if (descriptor.role === 'text') {
|
|
321
|
+
out[fieldKey] = { role: 'text', raw };
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
const classification = classifyMediaRef(raw, { treatAs: descriptor.treatAs });
|
|
325
|
+
out[fieldKey] = {
|
|
326
|
+
role: descriptor.role,
|
|
327
|
+
raw,
|
|
328
|
+
classification,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return out;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
class SeedEventEmitter {
|
|
335
|
+
constructor() {
|
|
336
|
+
this.listeners = new Map();
|
|
337
|
+
}
|
|
338
|
+
on(event, listener) {
|
|
339
|
+
const set = this.listeners.get(event) ?? new Set();
|
|
340
|
+
set.add(listener);
|
|
341
|
+
this.listeners.set(event, set);
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
addListener(event, listener) {
|
|
345
|
+
return this.on(event, listener);
|
|
346
|
+
}
|
|
347
|
+
once(event, listener) {
|
|
348
|
+
const wrapped = (...args) => {
|
|
349
|
+
this.off(event, wrapped);
|
|
350
|
+
listener(...args);
|
|
351
|
+
};
|
|
352
|
+
return this.on(event, wrapped);
|
|
353
|
+
}
|
|
354
|
+
off(event, listener) {
|
|
355
|
+
const set = this.listeners.get(event);
|
|
356
|
+
if (!set)
|
|
357
|
+
return this;
|
|
358
|
+
set.delete(listener);
|
|
359
|
+
if (set.size === 0)
|
|
360
|
+
this.listeners.delete(event);
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
removeListener(event, listener) {
|
|
364
|
+
return this.off(event, listener);
|
|
365
|
+
}
|
|
366
|
+
removeAllListeners(event) {
|
|
367
|
+
if (typeof event === 'undefined') {
|
|
368
|
+
this.listeners.clear();
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
this.listeners.delete(event);
|
|
372
|
+
return this;
|
|
373
|
+
}
|
|
374
|
+
emit(event, ...args) {
|
|
375
|
+
const set = this.listeners.get(event);
|
|
376
|
+
if (!set || set.size === 0)
|
|
377
|
+
return false;
|
|
378
|
+
for (const listener of [...set]) {
|
|
379
|
+
listener(...args);
|
|
380
|
+
}
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const eventEmitter = new SeedEventEmitter();
|
|
385
|
+
|
|
386
|
+
const getMetadata = async (props) => {
|
|
387
|
+
const appDb = BaseDb.getAppDb();
|
|
388
|
+
let storageTransactionId;
|
|
389
|
+
if (props) {
|
|
390
|
+
storageTransactionId = props.storageTransactionId;
|
|
391
|
+
}
|
|
392
|
+
const whereClauses = [];
|
|
393
|
+
if (storageTransactionId) {
|
|
394
|
+
whereClauses.push(eq(metadata.propertyName, 'storageTransactionId'));
|
|
395
|
+
whereClauses.push(eq(metadata.propertyValue, storageTransactionId));
|
|
396
|
+
}
|
|
397
|
+
const metadataRows = await appDb
|
|
398
|
+
.select()
|
|
399
|
+
.from(metadata)
|
|
400
|
+
.where(and(...whereClauses));
|
|
401
|
+
if (!metadataRows || metadataRows.length === 0) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
return metadataRows[0];
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const saveMetadata = async (metadataRecord, metadataValues) => {
|
|
408
|
+
if (!metadataRecord.localId) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const appDb = BaseDb.getAppDb();
|
|
412
|
+
await appDb
|
|
413
|
+
.update(metadata)
|
|
414
|
+
.set({
|
|
415
|
+
...metadataValues,
|
|
416
|
+
updatedAt: Date.now(),
|
|
417
|
+
})
|
|
418
|
+
.where(eq(metadata.localId, metadataRecord.localId));
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
const isNode = () => {
|
|
422
|
+
return (typeof process !== 'undefined' &&
|
|
423
|
+
process.versions != null &&
|
|
424
|
+
process.versions.node != null);
|
|
425
|
+
};
|
|
426
|
+
const isBrowser = () => {
|
|
427
|
+
return !isElectron() && typeof document !== 'undefined' && typeof window !== 'undefined';
|
|
428
|
+
};
|
|
429
|
+
const isElectron = () => {
|
|
430
|
+
return typeof process !== 'undefined' && process.versions != null && process.versions.electron != null;
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* Detects if code is running in an Electron renderer process.
|
|
434
|
+
*
|
|
435
|
+
* Uses multiple detection methods for robustness:
|
|
436
|
+
* 1. Checks process.type === 'renderer' (most reliable)
|
|
437
|
+
* 2. Falls back to checking if Electron is present and window/document exist
|
|
438
|
+
*
|
|
439
|
+
* @returns true if running in Electron renderer process, false otherwise
|
|
440
|
+
*/
|
|
441
|
+
const isElectronRenderer = () => {
|
|
442
|
+
// Method 1: Check process.type (most reliable - official Electron API)
|
|
443
|
+
if (typeof process !== 'undefined' &&
|
|
444
|
+
process.type === 'renderer') {
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
// Method 2: Fallback - Electron + browser-like environment
|
|
448
|
+
// Renderer processes have window/document, main process doesn't (in modern Electron)
|
|
449
|
+
if (isElectron() &&
|
|
450
|
+
typeof window !== 'undefined' &&
|
|
451
|
+
typeof document !== 'undefined') {
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
return false;
|
|
455
|
+
};
|
|
456
|
+
/**
|
|
457
|
+
* True where OPFS-backed Arweave file downloads should run: normal browsers and
|
|
458
|
+
* Electron renderer. {@link isBrowser} is false for every Electron build (including renderer).
|
|
459
|
+
*/
|
|
460
|
+
const supportsOpfsFileDownloads = () => {
|
|
461
|
+
return isBrowser() || isElectronRenderer();
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
const logger$2 = debug('seedSdk:files:download');
|
|
465
|
+
const AUTO_BULK_DOWNLOAD_THROTTLE_MS = 10000;
|
|
466
|
+
/** Avoid refetch-on-every-call: transient empty indexer responses were overwriting good cache (see trailing throttle ~10s after success). */
|
|
467
|
+
const GET_FILES_METADATA_STALE_MS = 120000;
|
|
468
|
+
let bulkDownloadInFlight = null;
|
|
469
|
+
const lazyDownloadInFlightByTransactionId = new Map();
|
|
470
|
+
// syncDbFiles helper - internal service removed, functionality moved here
|
|
471
|
+
const syncDbFiles = async (endpoints) => {
|
|
472
|
+
// TODO: Implement syncDbFiles functionality if needed
|
|
473
|
+
// This was previously in @/services/internal/helpers
|
|
474
|
+
logger$2('[download] syncDbFiles called but not yet implemented');
|
|
475
|
+
return Promise.resolve();
|
|
476
|
+
};
|
|
477
|
+
const getAppDbReady = async () => {
|
|
478
|
+
if (!BaseDb.isAppDbReady()) {
|
|
479
|
+
// Wait for ClientManager to be ready (DB_INIT state or later)
|
|
480
|
+
const _mod_0 = await import('./ClientManager-ChcmUL47.js');
|
|
481
|
+
const _ns_0 = _mod_0.ap;
|
|
482
|
+
const clientManagerMod = _ns_0;
|
|
483
|
+
const { getClient } = clientManagerMod;
|
|
484
|
+
const clientManager = getClient();
|
|
485
|
+
const clientService = clientManager.getService();
|
|
486
|
+
await waitFor(clientService, (snapshot) => {
|
|
487
|
+
const state = snapshot.value;
|
|
488
|
+
return state === 'dbInit' ||
|
|
489
|
+
state === 'saveConfig' ||
|
|
490
|
+
state === 'processSchemaFiles' ||
|
|
491
|
+
state === 'addModelsToStore' ||
|
|
492
|
+
state === 'addModelsToDb' ||
|
|
493
|
+
state === 'idle';
|
|
494
|
+
}, { timeout: 30000 });
|
|
495
|
+
}
|
|
496
|
+
return BaseDb.getAppDb();
|
|
497
|
+
};
|
|
498
|
+
const getExcludedTransactions = async () => {
|
|
499
|
+
const appDb = await getAppDbReady();
|
|
500
|
+
if (!appDb) {
|
|
501
|
+
return new Set();
|
|
502
|
+
}
|
|
503
|
+
const excludedTransactionsQuery = await appDb
|
|
504
|
+
.select()
|
|
505
|
+
.from(appState)
|
|
506
|
+
.where(eq(appState.key, 'excludedTransactions'));
|
|
507
|
+
const excludedTransactions = new Set();
|
|
508
|
+
if (excludedTransactionsQuery && excludedTransactionsQuery.length === 1) {
|
|
509
|
+
const valueString = excludedTransactionsQuery[0].value;
|
|
510
|
+
if (valueString) {
|
|
511
|
+
const excludedTransactionsArray = JSON.parse(valueString);
|
|
512
|
+
for (const txId of excludedTransactionsArray) {
|
|
513
|
+
excludedTransactions.add(txId);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return excludedTransactions;
|
|
518
|
+
};
|
|
519
|
+
/** Arweave transaction ids are 43 chars in the URL-safe base64 alphabet. */
|
|
520
|
+
const ARWEAVE_TX_ID_RE$1 = /^[a-z0-9_-]{43}$/i;
|
|
521
|
+
const normalizeArweaveTxId = (raw) => {
|
|
522
|
+
if (raw == null || raw === '')
|
|
523
|
+
return undefined;
|
|
524
|
+
const t = String(raw).trim();
|
|
525
|
+
if (ARWEAVE_TX_ID_RE$1.test(t))
|
|
526
|
+
return t;
|
|
527
|
+
const m = t.match(/^([a-z0-9_-]{43})(?:\.[^/]+)?$/i);
|
|
528
|
+
if (m)
|
|
529
|
+
return m[1];
|
|
530
|
+
// Values may be full gateway URLs or paths; scan for the embedded tx id.
|
|
531
|
+
const embedded = t.match(/[a-z0-9_-]{43}/gi);
|
|
532
|
+
if (embedded) {
|
|
533
|
+
for (const candidate of embedded) {
|
|
534
|
+
if (ARWEAVE_TX_ID_RE$1.test(candidate))
|
|
535
|
+
return candidate;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return undefined;
|
|
539
|
+
};
|
|
540
|
+
const mergeAddressLists = (a, b) => {
|
|
541
|
+
const seen = new Set();
|
|
542
|
+
const out = [];
|
|
543
|
+
for (const addr of [...(a ?? []), ...(b ?? [])]) {
|
|
544
|
+
if (!addr || !addr.trim())
|
|
545
|
+
continue;
|
|
546
|
+
const k = addr.toLowerCase();
|
|
547
|
+
if (seen.has(k))
|
|
548
|
+
continue;
|
|
549
|
+
seen.add(k);
|
|
550
|
+
out.push(addr);
|
|
551
|
+
}
|
|
552
|
+
return out;
|
|
553
|
+
};
|
|
554
|
+
const collectTransactionIdsFromLocalMetadata = async () => {
|
|
555
|
+
const appDb = await getAppDbReady();
|
|
556
|
+
if (!appDb) {
|
|
557
|
+
return [];
|
|
558
|
+
}
|
|
559
|
+
const nameOr = or(eq(metadata.propertyName, 'storageTransactionId'), eq(metadata.propertyName, 'storage_transaction_id'), eq(metadata.propertyName, 'transactionId'));
|
|
560
|
+
const propRows = await appDb
|
|
561
|
+
.select({ propertyValue: metadata.propertyValue })
|
|
562
|
+
.from(metadata)
|
|
563
|
+
.where(and(nameOr, isNotNull(metadata.propertyValue)));
|
|
564
|
+
const refRows = await appDb
|
|
565
|
+
.select({ refResolvedValue: metadata.refResolvedValue })
|
|
566
|
+
.from(metadata)
|
|
567
|
+
.where(isNotNull(metadata.refResolvedValue));
|
|
568
|
+
const ids = new Set();
|
|
569
|
+
for (const row of propRows) {
|
|
570
|
+
const id = normalizeArweaveTxId(row.propertyValue ?? undefined);
|
|
571
|
+
if (id)
|
|
572
|
+
ids.add(id);
|
|
573
|
+
}
|
|
574
|
+
for (const row of refRows) {
|
|
575
|
+
const id = normalizeArweaveTxId(row.refResolvedValue ?? undefined);
|
|
576
|
+
if (id)
|
|
577
|
+
ids.add(id);
|
|
578
|
+
}
|
|
579
|
+
return Array.from(ids);
|
|
580
|
+
};
|
|
581
|
+
const parseTransactionIdsFromMetadata = (filesMetadata, excludedTransactions) => {
|
|
582
|
+
const transactionIds = [];
|
|
583
|
+
for (const fileMetadata of filesMetadata) {
|
|
584
|
+
// Validate and parse decodedDataJson
|
|
585
|
+
if (!fileMetadata.decodedDataJson || fileMetadata.decodedDataJson.trim() === '') {
|
|
586
|
+
console.warn('[events/files] [download] empty decodedDataJson for fileMetadata: ', fileMetadata.id);
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
let json;
|
|
590
|
+
try {
|
|
591
|
+
json = JSON.parse(fileMetadata.decodedDataJson);
|
|
592
|
+
}
|
|
593
|
+
catch (error) {
|
|
594
|
+
console.warn('[events/files] [download] failed to parse decodedDataJson for fileMetadata: ', fileMetadata.id, error);
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
if (!Array.isArray(json) || json.length === 0 || !json[0]?.value?.value) {
|
|
598
|
+
console.warn('[events/files] [download] invalid decodedDataJson structure for fileMetadata: ', fileMetadata.id);
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
const rawTx = json[0].value.value;
|
|
602
|
+
if (typeof rawTx !== 'string' || rawTx.trim().length === 0) {
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
const transactionId = normalizeArweaveTxId(rawTx.trim());
|
|
606
|
+
if (!transactionId) {
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (excludedTransactions.has(transactionId) || excludedTransactions.has(rawTx.trim())) {
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
transactionIds.push(transactionId);
|
|
613
|
+
}
|
|
614
|
+
return transactionIds;
|
|
615
|
+
};
|
|
616
|
+
const downloadTransactionIds = async (rawTransactionIds, options) => {
|
|
617
|
+
if (!supportsOpfsFileDownloads()) {
|
|
618
|
+
return false;
|
|
619
|
+
}
|
|
620
|
+
const transactionIds = Array.from(new Set(rawTransactionIds.filter((tx) => !!tx && tx.trim().length > 0)));
|
|
621
|
+
if (transactionIds.length === 0) {
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
const excludedTransactions = await getExcludedTransactions();
|
|
625
|
+
const queryClient = BaseQueryClient.getQueryClient();
|
|
626
|
+
const transactionIdsToDownload = [];
|
|
627
|
+
await ensureReadGatewaySelected();
|
|
628
|
+
for (const transactionId of transactionIds) {
|
|
629
|
+
if (excludedTransactions.has(transactionId)) {
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
try {
|
|
633
|
+
// Browser: do not call getTransactionStatus() — many gateways (e.g. g8way.io) redirect or omit
|
|
634
|
+
// CORS headers, so fetch() fails and we would wrongly exclude txs. The OPFS worker fetch decides success.
|
|
635
|
+
let tags = [];
|
|
636
|
+
try {
|
|
637
|
+
tags = await queryClient.fetchQuery({
|
|
638
|
+
queryKey: ['getTransactionTags', transactionId],
|
|
639
|
+
queryFn: async () => BaseArweaveClient.getTransactionTags(transactionId),
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
catch {
|
|
643
|
+
tags = [];
|
|
644
|
+
}
|
|
645
|
+
if (tags && tags.length > 0) {
|
|
646
|
+
for (const { name, value } of tags) {
|
|
647
|
+
if (name !== 'Content-SHA-256')
|
|
648
|
+
continue;
|
|
649
|
+
const metadataRecord = await getMetadata({
|
|
650
|
+
storageTransactionId: transactionId,
|
|
651
|
+
});
|
|
652
|
+
if (metadataRecord) {
|
|
653
|
+
await saveMetadata(metadataRecord, {
|
|
654
|
+
contentHash: value,
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
transactionIdsToDownload.push(transactionId);
|
|
660
|
+
}
|
|
661
|
+
catch (error) {
|
|
662
|
+
logger$2(error);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (transactionIdsToDownload.length === 0) {
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
// Preferred gateways first: a just-published item is only on the gateway it was uploaded through.
|
|
669
|
+
const arweaveBaseUrls = getArweaveReadBaseUrls();
|
|
670
|
+
if (transactionIdsToDownload.length === 1) {
|
|
671
|
+
await BaseFileManager.downloadFileByTransactionId({
|
|
672
|
+
transactionId: transactionIdsToDownload[0],
|
|
673
|
+
arweaveHost: BaseArweaveClient.getBaseUrl(),
|
|
674
|
+
arweaveBaseUrls,
|
|
675
|
+
excludedTransactions,
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
else {
|
|
679
|
+
await BaseFileManager.downloadAllFiles({
|
|
680
|
+
transactionIds: transactionIdsToDownload,
|
|
681
|
+
arweaveHost: BaseArweaveClient.getBaseUrl(),
|
|
682
|
+
arweaveBaseUrls,
|
|
683
|
+
excludedTransactions,
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
if (options?.resizeAllImages !== false) {
|
|
687
|
+
await BaseFileManager.resizeAllImages({
|
|
688
|
+
width: 480,
|
|
689
|
+
height: 480,
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
return true;
|
|
693
|
+
};
|
|
694
|
+
const runBulkDownloadWithDedupe = async (transactionIds) => {
|
|
695
|
+
if (bulkDownloadInFlight) {
|
|
696
|
+
return bulkDownloadInFlight;
|
|
697
|
+
}
|
|
698
|
+
bulkDownloadInFlight = (async () => {
|
|
699
|
+
try {
|
|
700
|
+
await downloadTransactionIds(transactionIds, { resizeAllImages: true });
|
|
701
|
+
}
|
|
702
|
+
finally {
|
|
703
|
+
bulkDownloadInFlight = null;
|
|
704
|
+
}
|
|
705
|
+
})();
|
|
706
|
+
return bulkDownloadInFlight;
|
|
707
|
+
};
|
|
708
|
+
const downloadAllFilesRequestHandler = async ({ endpoints, eventId, }) => {
|
|
709
|
+
if (!supportsOpfsFileDownloads()) {
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
await syncDbFiles();
|
|
713
|
+
eventEmitter.emit('fs.downloadAll.success', { eventId });
|
|
714
|
+
eventEmitter.emit('fs.downloadAllBinary.request', { endpoints });
|
|
715
|
+
};
|
|
716
|
+
const downloadAllFilesBinaryRequestHandler = async (addressesHintOrPayload) => {
|
|
717
|
+
if (!supportsOpfsFileDownloads()) {
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
const addressesHint = Array.isArray(addressesHintOrPayload)
|
|
721
|
+
? addressesHintOrPayload
|
|
722
|
+
: undefined;
|
|
723
|
+
let fromDb = [];
|
|
724
|
+
if (BaseDb.isAppDbReady()) {
|
|
725
|
+
fromDb = await getAllAddressesFromDb();
|
|
726
|
+
}
|
|
727
|
+
if (!BaseDb.isAppDbReady()) {
|
|
728
|
+
await getAppDbReady();
|
|
729
|
+
fromDb = await getAllAddressesFromDb();
|
|
730
|
+
}
|
|
731
|
+
const addresses = mergeAddressLists(addressesHint, fromDb);
|
|
732
|
+
if (!addresses || addresses.length === 0) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
const queryClient = BaseQueryClient.getQueryClient();
|
|
736
|
+
const easClient = BaseEasClient.getEasClient();
|
|
737
|
+
const filesMetadataQueryKey = ['getFilesMetadata', ...addresses];
|
|
738
|
+
const fetchFilesMetadata = () => easClient.request(GET_FILES_METADATA, {
|
|
739
|
+
where: {
|
|
740
|
+
attester: {
|
|
741
|
+
in: addresses,
|
|
742
|
+
},
|
|
743
|
+
schema: {
|
|
744
|
+
is: {
|
|
745
|
+
id: {
|
|
746
|
+
equals: '0x55fdefb36fcbbaebeb7d6b41dc3a1a9666e4e42154267c889de064faa7ede517',
|
|
747
|
+
},
|
|
748
|
+
},
|
|
749
|
+
},
|
|
750
|
+
},
|
|
751
|
+
});
|
|
752
|
+
let { filesMetadata } = await queryClient.fetchQuery({
|
|
753
|
+
queryKey: filesMetadataQueryKey,
|
|
754
|
+
queryFn: fetchFilesMetadata,
|
|
755
|
+
staleTime: GET_FILES_METADATA_STALE_MS,
|
|
756
|
+
});
|
|
757
|
+
if ((!Array.isArray(filesMetadata) || filesMetadata.length === 0) && addresses.length > 0) {
|
|
758
|
+
await queryClient.removeQueries({ queryKey: filesMetadataQueryKey });
|
|
759
|
+
({ filesMetadata } = await queryClient.fetchQuery({
|
|
760
|
+
queryKey: filesMetadataQueryKey,
|
|
761
|
+
queryFn: fetchFilesMetadata,
|
|
762
|
+
staleTime: GET_FILES_METADATA_STALE_MS,
|
|
763
|
+
}));
|
|
764
|
+
}
|
|
765
|
+
const filesRoot = BaseFileManager.getWorkingDir();
|
|
766
|
+
await BaseFileManager.createDirIfNotExists(filesRoot);
|
|
767
|
+
await BaseFileManager.createDirIfNotExists(BaseFileManager.getFilesPath('html'));
|
|
768
|
+
await BaseFileManager.createDirIfNotExists(BaseFileManager.getFilesPath('json'));
|
|
769
|
+
await BaseFileManager.createDirIfNotExists(BaseFileManager.getFilesPath('images'));
|
|
770
|
+
const excludedTransactions = await getExcludedTransactions();
|
|
771
|
+
const fromEas = parseTransactionIdsFromMetadata(filesMetadata ?? [], excludedTransactions);
|
|
772
|
+
const fromLocal = await collectTransactionIdsFromLocalMetadata();
|
|
773
|
+
const transactionIds = Array.from(new Set([...fromEas, ...fromLocal]));
|
|
774
|
+
await runBulkDownloadWithDedupe(transactionIds);
|
|
775
|
+
};
|
|
776
|
+
const downloadTransactionIdWithDedupe = async (transactionId) => {
|
|
777
|
+
if (!supportsOpfsFileDownloads() || !transactionId || transactionId.trim().length === 0) {
|
|
778
|
+
return false;
|
|
779
|
+
}
|
|
780
|
+
const normalizedTxId = normalizeArweaveTxId(transactionId) ?? transactionId.trim();
|
|
781
|
+
const existing = lazyDownloadInFlightByTransactionId.get(normalizedTxId);
|
|
782
|
+
if (existing) {
|
|
783
|
+
return existing;
|
|
784
|
+
}
|
|
785
|
+
const inFlight = (async () => {
|
|
786
|
+
try {
|
|
787
|
+
const ok = await downloadTransactionIds([normalizedTxId], { resizeAllImages: false });
|
|
788
|
+
return ok;
|
|
789
|
+
}
|
|
790
|
+
finally {
|
|
791
|
+
lazyDownloadInFlightByTransactionId.delete(normalizedTxId);
|
|
792
|
+
}
|
|
793
|
+
})();
|
|
794
|
+
lazyDownloadInFlightByTransactionId.set(normalizedTxId, inFlight);
|
|
795
|
+
return inFlight;
|
|
796
|
+
};
|
|
797
|
+
const scheduleBulkFilesDownloadFromEasSyncThrottled = throttle((addressesHint) => {
|
|
798
|
+
void downloadAllFilesBinaryRequestHandler(addressesHint).catch((error) => {
|
|
799
|
+
logger$2('[scheduleBulkFilesDownloadFromEasSync] failed', error);
|
|
800
|
+
});
|
|
801
|
+
}, AUTO_BULK_DOWNLOAD_THROTTLE_MS, { leading: true, trailing: false });
|
|
802
|
+
const scheduleBulkFilesDownloadFromEasSync = (addressesHint) => {
|
|
803
|
+
if (!supportsOpfsFileDownloads()) {
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
scheduleBulkFilesDownloadFromEasSyncThrottled(addressesHint);
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
var download = /*#__PURE__*/Object.freeze({
|
|
810
|
+
__proto__: null,
|
|
811
|
+
downloadAllFilesBinaryRequestHandler: downloadAllFilesBinaryRequestHandler,
|
|
812
|
+
downloadAllFilesRequestHandler: downloadAllFilesRequestHandler,
|
|
813
|
+
downloadTransactionIdWithDedupe: downloadTransactionIdWithDedupe,
|
|
814
|
+
scheduleBulkFilesDownloadFromEasSync: scheduleBulkFilesDownloadFromEasSync
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
const logger$1 = debug('seedSdk:helpers:ensureImageLocal');
|
|
818
|
+
/** Arweave transaction ids are 43 chars in the URL-safe base64 alphabet. */
|
|
819
|
+
const ARWEAVE_TX_ID_RE = /^[a-z0-9_-]{43}$/i;
|
|
820
|
+
const IMAGE_SIZES = [
|
|
821
|
+
ImageSize.EXTRA_SMALL,
|
|
822
|
+
ImageSize.SMALL,
|
|
823
|
+
ImageSize.MEDIUM,
|
|
824
|
+
ImageSize.LARGE,
|
|
825
|
+
ImageSize.EXTRA_LARGE,
|
|
826
|
+
];
|
|
827
|
+
const inFlightByKey = new Map();
|
|
828
|
+
const normalizeArweaveTxIdForEnsure = (raw) => {
|
|
829
|
+
if (raw == null || raw === '')
|
|
830
|
+
return undefined;
|
|
831
|
+
const t = String(raw).trim();
|
|
832
|
+
if (ARWEAVE_TX_ID_RE.test(t))
|
|
833
|
+
return t;
|
|
834
|
+
const m = t.match(/^([a-z0-9_-]{43})(?:\.[^/]+)?$/i);
|
|
835
|
+
if (m)
|
|
836
|
+
return m[1];
|
|
837
|
+
const embedded = t.match(/[a-z0-9_-]{43}/gi);
|
|
838
|
+
if (embedded) {
|
|
839
|
+
for (const candidate of embedded) {
|
|
840
|
+
if (ARWEAVE_TX_ID_RE.test(candidate))
|
|
841
|
+
return candidate;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
return undefined;
|
|
845
|
+
};
|
|
846
|
+
const closestImageSize = (width) => {
|
|
847
|
+
let best = ImageSize.EXTRA_SMALL;
|
|
848
|
+
let bestDist = Math.abs(best - width);
|
|
849
|
+
for (const size of IMAGE_SIZES) {
|
|
850
|
+
const dist = Math.abs(size - width);
|
|
851
|
+
if (dist < bestDist) {
|
|
852
|
+
best = size;
|
|
853
|
+
bestDist = dist;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return best;
|
|
857
|
+
};
|
|
858
|
+
const mapRequestedWidthsToImageSizes = (widths) => {
|
|
859
|
+
const requested = widths && widths.length > 0 ? widths : [ImageSize.EXTRA_SMALL];
|
|
860
|
+
const unique = new Set();
|
|
861
|
+
for (const w of requested) {
|
|
862
|
+
if (typeof w !== 'number' || !Number.isFinite(w) || w <= 0)
|
|
863
|
+
continue;
|
|
864
|
+
unique.add(closestImageSize(w));
|
|
865
|
+
}
|
|
866
|
+
if (unique.size === 0) {
|
|
867
|
+
unique.add(ImageSize.EXTRA_SMALL);
|
|
868
|
+
}
|
|
869
|
+
return Array.from(unique).sort((a, b) => a - b);
|
|
870
|
+
};
|
|
871
|
+
const getBasename = (filePath) => {
|
|
872
|
+
const path = BaseFileManager.getPathModule();
|
|
873
|
+
return path.basename(filePath);
|
|
874
|
+
};
|
|
875
|
+
const getBasenameWithoutExtension = (fileName) => {
|
|
876
|
+
const match = fileName.match(/^(.*[\/\\])?([^\/\\]+?)(\.[^.\/\\]*)?$/);
|
|
877
|
+
return match?.[2] ?? fileName;
|
|
878
|
+
};
|
|
879
|
+
const resolveMatchingImagePath = async (needle) => {
|
|
880
|
+
const dirPath = BaseFileManager.getFilesPath('images');
|
|
881
|
+
const dirExists = await BaseFileManager.pathExists(dirPath);
|
|
882
|
+
if (!dirExists)
|
|
883
|
+
return undefined;
|
|
884
|
+
const fs = await BaseFileManager.getFs();
|
|
885
|
+
const path = BaseFileManager.getPathModule();
|
|
886
|
+
const files = await fs.promises.readdir(dirPath);
|
|
887
|
+
// Prefer exact basename match (files only); skip size dirs.
|
|
888
|
+
const exact = files.find((file) => path.basename(file) === needle);
|
|
889
|
+
if (exact) {
|
|
890
|
+
const candidate = BaseFileManager.getFilesPath('images', exact);
|
|
891
|
+
try {
|
|
892
|
+
const zenfs = await BaseFileManager.getFs();
|
|
893
|
+
const stat = await zenfs.promises.stat(candidate);
|
|
894
|
+
if (stat.isFile())
|
|
895
|
+
return candidate;
|
|
896
|
+
}
|
|
897
|
+
catch {
|
|
898
|
+
// fall through
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
const matchingFiles = files.filter((file) => {
|
|
902
|
+
const base = path.basename(file);
|
|
903
|
+
return base.includes(needle);
|
|
904
|
+
});
|
|
905
|
+
for (const file of matchingFiles) {
|
|
906
|
+
const candidate = BaseFileManager.getFilesPath('images', file);
|
|
907
|
+
try {
|
|
908
|
+
const zenfs = await BaseFileManager.getFs();
|
|
909
|
+
const stat = await zenfs.promises.stat(candidate);
|
|
910
|
+
if (stat.isFile())
|
|
911
|
+
return candidate;
|
|
912
|
+
}
|
|
913
|
+
catch {
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
return undefined;
|
|
918
|
+
};
|
|
919
|
+
const sizedVariantPath = (originalFilePath, size) => {
|
|
920
|
+
const path = BaseFileManager.getPathModule();
|
|
921
|
+
const dir = path.dirname(originalFilePath);
|
|
922
|
+
const base = getBasenameWithoutExtension(getBasename(originalFilePath));
|
|
923
|
+
return path.join(dir, String(size), `${base}.webp`);
|
|
924
|
+
};
|
|
925
|
+
const ensureKey = (txId, fileName, sizes) => {
|
|
926
|
+
const idPart = txId ?? fileName ?? '';
|
|
927
|
+
return `${idPart}|${sizes.join(',')}`;
|
|
928
|
+
};
|
|
929
|
+
const runEnsureImageLocal = async (params) => {
|
|
930
|
+
if (!supportsOpfsFileDownloads()) {
|
|
931
|
+
return { status: 'unsupported' };
|
|
932
|
+
}
|
|
933
|
+
const fileName = params.fileName?.trim() || undefined;
|
|
934
|
+
const txId = normalizeArweaveTxIdForEnsure(params.transactionId) ??
|
|
935
|
+
normalizeArweaveTxIdForEnsure(fileName);
|
|
936
|
+
if (!txId && !fileName) {
|
|
937
|
+
return { status: 'missing-id' };
|
|
938
|
+
}
|
|
939
|
+
const targetSizes = mapRequestedWidthsToImageSizes(params.widths);
|
|
940
|
+
const needles = [fileName, txId].filter((n) => typeof n === 'string' && n.length > 0);
|
|
941
|
+
try {
|
|
942
|
+
let filePath;
|
|
943
|
+
for (const needle of needles) {
|
|
944
|
+
filePath = await resolveMatchingImagePath(needle);
|
|
945
|
+
if (filePath)
|
|
946
|
+
break;
|
|
947
|
+
}
|
|
948
|
+
if (!filePath && txId) {
|
|
949
|
+
await downloadTransactionIdWithDedupe(txId);
|
|
950
|
+
for (const needle of needles) {
|
|
951
|
+
filePath = await resolveMatchingImagePath(needle);
|
|
952
|
+
if (filePath)
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
// Downloads store as bare tx id
|
|
956
|
+
if (!filePath) {
|
|
957
|
+
const direct = BaseFileManager.getFilesPath('images', txId);
|
|
958
|
+
if (await BaseFileManager.pathExists(direct)) {
|
|
959
|
+
filePath = direct;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
if (!filePath) {
|
|
964
|
+
return { status: 'failed' };
|
|
965
|
+
}
|
|
966
|
+
const createdWidths = [];
|
|
967
|
+
for (const size of targetSizes) {
|
|
968
|
+
const variantPath = sizedVariantPath(filePath, size);
|
|
969
|
+
const exists = await BaseFileManager.pathExists(variantPath);
|
|
970
|
+
if (exists)
|
|
971
|
+
continue;
|
|
972
|
+
await BaseFileManager.resizeImage({
|
|
973
|
+
filePath,
|
|
974
|
+
width: size,
|
|
975
|
+
height: size,
|
|
976
|
+
});
|
|
977
|
+
createdWidths.push(size);
|
|
978
|
+
}
|
|
979
|
+
return {
|
|
980
|
+
status: 'ready',
|
|
981
|
+
filePath,
|
|
982
|
+
createdWidths,
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
catch (error) {
|
|
986
|
+
logger$1('ensureImageLocal failed', error);
|
|
987
|
+
return { status: 'failed' };
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
/**
|
|
991
|
+
* Ensure an image original exists in OPFS (downloading from Arweave if needed)
|
|
992
|
+
* and create only the requested ImageSize variant(s) for that one file.
|
|
993
|
+
*/
|
|
994
|
+
const ensureImageLocal = async (params) => {
|
|
995
|
+
if (!supportsOpfsFileDownloads()) {
|
|
996
|
+
return { status: 'unsupported' };
|
|
997
|
+
}
|
|
998
|
+
const fileName = params.fileName?.trim() || undefined;
|
|
999
|
+
const txId = normalizeArweaveTxIdForEnsure(params.transactionId) ??
|
|
1000
|
+
normalizeArweaveTxIdForEnsure(fileName);
|
|
1001
|
+
if (!txId && !fileName) {
|
|
1002
|
+
return { status: 'missing-id' };
|
|
1003
|
+
}
|
|
1004
|
+
const targetSizes = mapRequestedWidthsToImageSizes(params.widths);
|
|
1005
|
+
const key = ensureKey(txId, fileName, targetSizes);
|
|
1006
|
+
const existing = inFlightByKey.get(key);
|
|
1007
|
+
if (existing) {
|
|
1008
|
+
return existing;
|
|
1009
|
+
}
|
|
1010
|
+
const inFlight = (async () => {
|
|
1011
|
+
try {
|
|
1012
|
+
return await runEnsureImageLocal({
|
|
1013
|
+
transactionId: txId,
|
|
1014
|
+
fileName,
|
|
1015
|
+
widths: targetSizes,
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
finally {
|
|
1019
|
+
inFlightByKey.delete(key);
|
|
1020
|
+
}
|
|
1021
|
+
})();
|
|
1022
|
+
inFlightByKey.set(key, inFlight);
|
|
1023
|
+
return inFlight;
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
const logger = debug('seedSdk:shared:helpers');
|
|
1027
|
+
const toSnakeCase = (str) => {
|
|
1028
|
+
return str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
1029
|
+
};
|
|
1030
|
+
const getMimeType = (base64) => {
|
|
1031
|
+
if (!base64) {
|
|
1032
|
+
return null;
|
|
1033
|
+
}
|
|
1034
|
+
const result = base64.match(/^data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,/);
|
|
1035
|
+
if (result && result.length > 1) {
|
|
1036
|
+
return result[1];
|
|
1037
|
+
}
|
|
1038
|
+
else {
|
|
1039
|
+
return null; // MIME type could not be determined
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
const getCorrectId = (localIdOrUid) => {
|
|
1043
|
+
const id = {
|
|
1044
|
+
localId: undefined,
|
|
1045
|
+
uid: undefined,
|
|
1046
|
+
};
|
|
1047
|
+
if (!localIdOrUid) {
|
|
1048
|
+
return id;
|
|
1049
|
+
}
|
|
1050
|
+
if (localIdOrUid.length === 10) {
|
|
1051
|
+
id.localId = localIdOrUid;
|
|
1052
|
+
}
|
|
1053
|
+
if (localIdOrUid.startsWith('0x') && localIdOrUid.length === 66) {
|
|
1054
|
+
id.uid = localIdOrUid;
|
|
1055
|
+
}
|
|
1056
|
+
return id;
|
|
1057
|
+
};
|
|
1058
|
+
const getDataTypeFromString = (data) => {
|
|
1059
|
+
const nonImageBase64Regex = /^(?!data:image\/(?:jpeg|png|gif|bmp|webp);base64,)[A-Za-z0-9+/=]+$/;
|
|
1060
|
+
if (nonImageBase64Regex.test(data)) {
|
|
1061
|
+
return 'base64';
|
|
1062
|
+
}
|
|
1063
|
+
// Regular expression for base64 (simple version, checking for base64 format)
|
|
1064
|
+
const imageBase64Regex = /^data:image\/[a-zA-Z]+;base64,[A-Za-z0-9+/]+={0,2}$/;
|
|
1065
|
+
if (imageBase64Regex.test(data)) {
|
|
1066
|
+
return 'imageBase64';
|
|
1067
|
+
}
|
|
1068
|
+
// Blob URLs are fetchable but session-scoped; recognize them as url type
|
|
1069
|
+
if (data.startsWith('blob:')) {
|
|
1070
|
+
return 'url';
|
|
1071
|
+
}
|
|
1072
|
+
// Regular expression for URL (simple version, checking for common URL format)
|
|
1073
|
+
const urlRegex = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;
|
|
1074
|
+
if (urlRegex.test(data)) {
|
|
1075
|
+
return 'url';
|
|
1076
|
+
}
|
|
1077
|
+
return null;
|
|
1078
|
+
};
|
|
1079
|
+
const convertTxIdToImage = async (txId) => {
|
|
1080
|
+
const imageFilePath = BaseFileManager.getFilesPath('images', txId);
|
|
1081
|
+
const fileExists = await BaseFileManager.pathExists(imageFilePath);
|
|
1082
|
+
if (!fileExists) {
|
|
1083
|
+
logger(`[ItemView] [updateImage] ${imageFilePath} does not exist`);
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
const buffer = await BaseFileManager.readFileAsBuffer(imageFilePath);
|
|
1087
|
+
// Convert Blob to ArrayBuffer if needed, or use Buffer directly
|
|
1088
|
+
let arrayBuffer;
|
|
1089
|
+
if (buffer instanceof Blob) {
|
|
1090
|
+
arrayBuffer = await buffer.arrayBuffer();
|
|
1091
|
+
}
|
|
1092
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
1093
|
+
arrayBuffer = buffer;
|
|
1094
|
+
}
|
|
1095
|
+
else {
|
|
1096
|
+
// Node.js Buffer
|
|
1097
|
+
const sliced = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
1098
|
+
// Handle SharedArrayBuffer case by creating a new ArrayBuffer
|
|
1099
|
+
if (sliced instanceof SharedArrayBuffer) {
|
|
1100
|
+
const uint8Array = new Uint8Array(sliced);
|
|
1101
|
+
arrayBuffer = uint8Array.buffer.slice(0);
|
|
1102
|
+
}
|
|
1103
|
+
else {
|
|
1104
|
+
arrayBuffer = sliced;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
const uint = new Uint8Array(arrayBuffer);
|
|
1108
|
+
const imageBlob = new Blob([uint]);
|
|
1109
|
+
return URL.createObjectURL(imageBlob);
|
|
1110
|
+
};
|
|
1111
|
+
/**
|
|
1112
|
+
* Constructs an Arweave URL for a storage transaction ID
|
|
1113
|
+
* @param storageTransactionId - The Arweave transaction ID
|
|
1114
|
+
* @returns The full URL to access the transaction data on Arweave (e.g., https://arweave.net/raw/{transactionId})
|
|
1115
|
+
* @deprecated Use BaseArweaveClient.getRawUrl() instead for better consistency and testability
|
|
1116
|
+
*/
|
|
1117
|
+
const getArweaveUrlForTransaction = (storageTransactionId) => {
|
|
1118
|
+
return BaseArweaveClient.getRawUrl(storageTransactionId);
|
|
1119
|
+
};
|
|
1120
|
+
|
|
1121
|
+
export { isStorageSeedRef as a, getArweaveUrlForTransaction as b, classifyMediaRef as c, closestImageSize as d, eventEmitter as e, ensureImageLocal as f, getCorrectId as g, getFeedItemStringField as h, isPublishedSeedRef as i, normalizeArweaveTxIdForEnsure as j, normalizeFeedItemFields as k, resolveMediaRef as l, getStorageTransactionIdForSeedUid as m, normalizeRelationPropertyValue as n, convertTxIdToImage as o, downloadTransactionIdWithDedupe as p, getDataTypeFromString as q, resolveSeedIdsFromRefString as r, scheduleBulkFilesDownloadFromEasSync as s, toSnakeCase as t, getMimeType as u, isNode as v, isBrowser as w, downloadAllFilesRequestHandler as x, downloadAllFilesBinaryRequestHandler as y, download as z };
|
|
1122
|
+
//# sourceMappingURL=index-280bL-n4.js.map
|