-
Notifications
You must be signed in to change notification settings - Fork 0
/
ionic-space-v1.ts
1247 lines (1134 loc) · 50.6 KB
/
ionic-space-v1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Filesystem, Directory, Encoding, } from '@capacitor/filesystem';
import * as h from 'ts-gib/dist/helper';
import { IbGib_V1, IbGibRel8ns_V1, GIB, ROOT, isPrimitive, } from 'ts-gib/dist/V1';
import { getIbGibAddr, IbGibAddr } from 'ts-gib';
import * as c from '../../constants';
import { SpaceBase_V1 } from './space-base-v1';
import {
IbGibSpaceData, IbGibSpaceOptionsData, IbGibSpaceOptionsRel8ns,
IbGibSpaceOptionsIbGib, IbGibSpaceResultData, IbGibSpaceResultRel8ns,
IbGibSpaceResultIbGib
} from '../../types/space';
import { isBinary, getBinHashAndExt } from '../../helper/ibgib';
import { tryRead } from '../../helper/ionic';
import { getSpaceIb, getTjpIbGib, getSpecialIbGib, getInfoFromSpaceIb } from '../../helper/space';
import { argy_ } from '../witness-helper';
import { validateIbGibIntrinsically } from '../../helper/validate';
const logalot = c.GLOBAL_LOG_A_LOT || false;
// #region Space related interfaces/constants
/**
* This is the shape of data about this space itself (not the contained ibgibs' spaces).
*/
export interface IonicSpaceData_V1 extends IbGibSpaceData {
/**
* Redeclared here to make this required (not optional)
*/
uuid: string;
baseDir: Directory;
encoding: Encoding;
baseSubPath: string;
spaceSubPath: string;
ibgibsSubPath: string;
metaSubPath: string;
binSubPath: string;
dnaSubPath: string;
n?: number;
timestamp?: string;
}
/**
* Used in bootstrapping.
*
* If you change this, please bumpt the version
*
* (but of course won't be the end of the world when this doesn't happen).
*/
const DEFAULT_IONIC_SPACE_DATA_V1: IonicSpaceData_V1 = {
version: '5',
uuid: c.ZERO_SPACE_ID,
name: c.IBGIB_SPACE_NAME_DEFAULT,
classname: 'IonicSpace_V1',
baseDir: c.IBGIB_BASE_DIR,
encoding: c.IBGIB_ENCODING,
baseSubPath: c.IBGIB_BASE_SUBPATH,
spaceSubPath: c.IBGIB_SPACE_SUBPATH_DEFAULT,
ibgibsSubPath: c.IBGIB_IBGIBS_SUBPATH,
metaSubPath: c.IBGIB_META_SUBPATH,
binSubPath: c.IBGIB_BIN_SUBPATH,
dnaSubPath: c.IBGIB_DNA_SUBPATH,
persistOptsAndResultIbGibs: false,
validateIbGibAddrsMatchIbGibs: false,
longPollingIntervalMs: c.DEFAULT_LOCAL_SPACE_POLLING_INTERVAL_MS,
allowPrimitiveArgs: false,
catchAllErrors: true,
description: c.DEFAULT_LOCAL_SPACE_DESCRIPTION,
trace: false,
}
/** Marker interface atm */
export interface IonicSpaceRel8ns_V1 extends IbGibRel8ns_V1 { }
/**
* Space options involve whether we're getting/putting ibgibs categorized as
* meta, bin, dna.
*
* We'll leverage the fact that we don't need to get dna very often, and that
* meta ibgibs act differently and are recorded differently.
*
* For example, we don't necessarily want to keep the past of certain meta
* objects, because it may change (and thus grow) too quickly.
*/
export interface IonicSpaceOptionsData extends IbGibSpaceOptionsData {
/**
* If truthy, will look in the meta subpath first, then the regular if not found.
*/
isMeta?: boolean;
/**
* Are we looking for a DNA ibgib?
*/
isDna?: boolean;
}
export interface IonicSpaceOptionsRel8ns extends IbGibSpaceOptionsRel8ns {
}
/** Marker interface atm */
export interface IonicSpaceOptionsIbGib
extends IbGibSpaceOptionsIbGib<IbGib_V1, IonicSpaceOptionsData, IonicSpaceOptionsRel8ns> {
}
/** Marker interface atm */
export interface IonicSpaceResultData extends IbGibSpaceResultData {
}
export interface IonicSpaceResultRel8ns extends IbGibSpaceResultRel8ns {
}
export interface IonicSpaceResultIbGib
extends IbGibSpaceResultIbGib<IbGib_V1, IonicSpaceResultData, IonicSpaceResultRel8ns> {
}
// #endregion
// #region get/put files related (re-using from files service)
interface FileResult {
success?: boolean;
/**
* If errored, this will contain the errorMsg.
*/
errorMsg?: string;
}
/**
* Options for retrieving data from the file system.
*/
interface GetIbGibFileOpts {
/**
* If getting ibGib object, this is its address.
*/
addr?: IbGibAddr;
/**
* If truthy, will look in the meta subpath first, then the regular if not found.
*/
isMeta?: boolean;
/**
* Are we looking for a DNA ibgib?
*/
isDna?: boolean;
}
/**
* Result for retrieving an ibGib from the file system.
*/
interface GetIbGibFileResult extends FileResult {
/**
* ibGib if retrieving a "regular" ibGib.
*
* This is used when you're not getting a pic, e.g.
*/
ibGib?: IbGib_V1;
}
interface PutIbGibFileOpts {
ibGib?: IbGib_V1;
/**
* If true, will store with metas.
*/
isMeta?: boolean;
/**
* If true, will store in a different folder.
*/
isDna?: boolean;
}
interface PutIbGibFileResult extends FileResult { }
interface DeleteIbGibFileOpts extends GetIbGibFileOpts { }
interface DeleteIbGibFilesResult extends FileResult { }
// #endregion
/**
* basic validation for an ionic space
*/
export async function validateIonicSpace_V1Intrinsically({ space }: { space: IonicSpace_V1 }): Promise<string[] | null> {
const lc = `[${validateIonicSpace_V1Intrinsically.name}]`;
try {
if (logalot) { console.log(`${lc} starting... (I: fcfe57145426ab4a5a0e961e87001922)`); }
const errors: string[] = (await validateIbGibIntrinsically({ ibGib: space })) ?? [];
const { ib, gib, data, rel8ns } = space;
if (!ib) { errors.push('ib required.'); }
if (!gib) { errors.push('gib required.'); }
if (gib === GIB) { errors.push('gib cannot be primitive.'); }
if (!data) { errors.push('data required.'); }
if (!data.name) { errors.push('space name required.') }
// if (!data.classname) { errors.push('classname required.') }
if (data.classname && data.classname !== IonicSpace_V1.name) {
errors.push(`unknown classname (${data.classname}). data.classname !== IonicSpace_V1.name`);
}
if (!data.baseDir) { errors.push(`data.baseDir required.`) }
if (!data.baseSubPath) { errors.push(`data.baseSubPath required.`) }
if (!data.binSubPath) { errors.push(`data.binSubPath required.`) }
if (!data.dnaSubPath) { errors.push(`data.dnaSubPath required.`) }
if (!data.ibgibsSubPath) { errors.push(`data.ibgibsSubPath required.`) }
if (!data.metaSubPath) { errors.push(`data.metaSubPath required.`) }
if (data.n && typeof data.n !== 'number') { errors.push(`data.n must be a number.`) }
if (data.n === undefined) {
// the very first space record (tjp) has an undefined n and no rel8ns
if (rel8ns) { errors.push(`rel8ns not expected when data.n is falsy (custom temporal junction point indicator I suppose...)`); }
}
if (!data.spaceSubPath) { errors.push(`data.spaceSubPath required.`) }
if (!data.uuid) { errors.push(`data.uuid required.`) }
if (!data.encoding) { errors.push(`data.encoding required.`) }
/** should probably get this from Capacitor... */
const validEncodings = ["utf8", "ascii", "utf16"];
if (!validEncodings.includes(data.encoding)) {
errors.push(`invalid encoding: ${data.encoding}. validEncodings: ${validEncodings.join(', ')}`);
}
// ensure ib matches up with internal data
const { spaceClassname, spaceId, spaceName } = getInfoFromSpaceIb({ spaceIb: ib });
if (data.classname && (spaceClassname !== data.classname)) {
errors.push(`ib's spaceClassname (${spaceClassname}) must match data.classname (${data.classname})`);
}
if (spaceId !== data.uuid) {
errors.push(`ib's spaceId (${spaceId}) must match data.uuid (${data.uuid})`);
}
if (spaceName !== data.name) {
errors.push(`ib's spaceName (${spaceName}) must match data.name (${data.name})`);
}
// ensure rel8ns make sense
if (data.n === undefined && (rel8ns?.past ?? []).length > 0) {
errors.push(`"past" rel8n not expected when data.n is falsy`);
}
if (data.n && (rel8ns?.past ?? []).length === 0) {
errors.push(`"past" rel8n required when data.n is truthy`);
}
if (data.n === 0 && (rel8ns?.past ?? []).length !== 1) {
errors.push(`"past" rel8n expected to have a single record when data.n === 0`);
}
if (rel8ns?.past?.length > 0) {
const pastAddrs = <IbGibAddr[]>rel8ns.past;
pastAddrs.forEach(x => {
const { ib: pastIb } = h.getIbAndGib({ ibGibAddr: x });
const pastIbInfo = getInfoFromSpaceIb({ spaceIb: pastIb });
if (pastIbInfo.spaceClassname && (pastIbInfo.spaceClassname !== spaceClassname)) {
errors.push(`rel8ns.past address classname (${pastIbInfo.spaceClassname}) must match current spaceClassname (${spaceClassname})`);
}
if (pastIbInfo.spaceId !== spaceId) {
errors.push(`rel8ns.past address spaceId (${pastIbInfo.spaceId}) must match current spaceId (${spaceId})`);
}
// i want to allow this, but for now we're going to require not changing the name...
if (pastIbInfo.spaceName !== spaceName) {
errors.push(`rel8ns.past address spaceName (${pastIbInfo.spaceName}) must match current spaceName (${spaceName})`);
}
});
}
return errors;
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
/**
* Base class convenience for a local space with V1 ibgibs.
*
* This naively caches ibGibs in memory. When not found there,
* will looks in files using Ionic `FileSystem`.
*/
export class IonicSpace_V1<
TData extends IonicSpaceData_V1 = IonicSpaceData_V1,
TRel8ns extends IbGibRel8ns_V1 = IbGibRel8ns_V1
> extends SpaceBase_V1<
IbGib_V1,
IonicSpaceOptionsData,
IonicSpaceOptionsRel8ns,
IonicSpaceOptionsIbGib,
IonicSpaceResultData,
IonicSpaceResultRel8ns,
IonicSpaceResultIbGib,
TData,
TRel8ns
> {
/**
* Log context for convenience with logging. (Ignore if you don't want to use this.)
*/
protected lc: string = `[${IonicSpace_V1.name}]`;
/**
* Naive caching in-memory. Memory leak as it stands right now!
*/
protected ibGibs: { [key: string]: IbGib_V1 } = {};
/**
* Check every time app starts if paths exist.
* But don't check every time do anything whatsoever.
*
* ## notes
*
* for use in `ensureDirs` function.
*/
private pathExistsMap = {};
constructor(
// /**
// * Default predicate value when putting an unknown ibGib.
// *
// * ## notes
// *
// * So when a repo witnesses another ibGib, it either defaults to
// * storing that ibGib or not storing that ibGib. This is what that
// * is referring to. If it's optimistic, then it stores any ibGib by
// * default and it passes its put predicate.
// */
// public optimisticPut: boolean = true,
initialData: TData,
initialRel8ns: TRel8ns,
) {
super(initialData ?? h.clone(DEFAULT_IONIC_SPACE_DATA_V1), initialRel8ns);
const lc = `${this.lc}[ctor]`;
try {
if (logalot) { console.log(`${lc} starting...`); }
this.initialize();
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
/**
* Factory static method to create the space with the given
* `dto` param's ibGib properties.
*
* We do this because when we persist this space (and its settings
* located in `data`), we do not save the actual class instantiation
* but just the ibgib properties. Use this factory method to
* create a new space instance and rehydrate from that saved dto.
*
* ## notes
*
* * DTO stands for data transfer object.
*
* @param dto space ibGib dto that we're going to load from
* @returns newly created space built upon `dto`
*/
static async createFromDto<
TData extends IonicSpaceData_V1 = IonicSpaceData_V1,
TRel8ns extends IbGibRel8ns_V1 = IbGibRel8ns_V1
>(dto: IbGib_V1<TData, TRel8ns>): Promise<IonicSpace_V1<TData, TRel8ns>> {
const lc = `[${IonicSpace_V1.name}][${this.createFromDto.name}]`;
if (logalot) { console.log(`${lc}`); }
const space = new IonicSpace_V1<TData, TRel8ns>(null, null);
await space.loadIbGibDto(dto);
return space;
}
protected async validateWitnessArg(arg: IonicSpaceOptionsIbGib): Promise<string[]> {
const lc = `${this.lc}[${this.validateWitnessArg.name}]`;
let errors: string[] = [];
try {
errors = (await super.validateWitnessArg(arg)) || [];
if (arg.data?.cmd === 'put' && (arg.ibGibs || []).length === 0) {
errors.push(`when "put" cmd is called, ibGibs required.`);
}
if (arg.data?.cmd === 'get' && (arg.data?.ibGibAddrs || []).length === 0) {
errors.push(`when "get" cmd is called, ibGibAddrs required.`);
}
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
} finally {
if (errors?.length > 0) { console.error(`${lc} errors: ${errors}`); }
}
return errors;
}
/**
* Initializes to default space values.
*/
protected initialize(): void {
const lc = `${this.lc}[${this.initialize.name}]`;
try {
if (logalot) { console.log(`${lc} starting...`); }
if (!this.data) { this.data = h.clone(DEFAULT_IONIC_SPACE_DATA_V1); }
if (!this.data.baseDir) { this.data.baseDir = c.IBGIB_BASE_DIR; }
if (!this.data.encoding) { this.data.encoding = c.IBGIB_ENCODING; }
if (!this.data.baseSubPath) { this.data.baseSubPath = c.IBGIB_BASE_SUBPATH; }
if (!this.data.spaceSubPath) { this.data.spaceSubPath = c.IBGIB_SPACE_SUBPATH_DEFAULT; }
if (!this.data.ibgibsSubPath) { this.data.ibgibsSubPath = c.IBGIB_IBGIBS_SUBPATH; }
if (!this.data.metaSubPath) { this.data.metaSubPath = c.IBGIB_META_SUBPATH; }
if (!this.data.binSubPath) { this.data.binSubPath = c.IBGIB_BIN_SUBPATH; }
if (!this.data.dnaSubPath) { this.data.dnaSubPath = c.IBGIB_DNA_SUBPATH; }
this.ib = getSpaceIb({ space: this, classname: IonicSpace_V1.name });
} catch (error) {
console.error(`${lc} ${error.message}`);
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
private hasInCache({ addr }: { addr: IbGibAddr }): Promise<boolean> {
if (isPrimitive({ gib: h.getIbAndGib({ ibGibAddr: addr }).gib })) {
// primitives never cached
return Promise.resolve(false);
} else if (Object.keys(this.ibGibs).includes(addr)) {
// has in local instance cache
return Promise.resolve(true);
} else if (this.cacheSvc) {
// not local so delegate to cache svc
return this.cacheSvc.has({ addr });
} else {
// no external cache svc and not in local instance cache
return Promise.resolve(false);
}
}
private async putInCache({ addr, ibGib }: { addr: IbGibAddr, ibGib: IbGib_V1 }): Promise<void> {
const lc = `${this.lc}[${this.putInCache.name}][${addr}]`;
try {
if (logalot) { console.log(`${lc} starting...`); }
if (!ibGib) { throw new Error(`ibGib required (E: 993e26fe40894bab9feccac3938f37df)`); }
if (isPrimitive({ ibGib })) {
if (logalot) { console.log(`${lc} skipping caching primitive (I: a04dfb691582a4db8a0bfecfefe5e622)`); }
return;
} else {
if (logalot) { console.log(`${lc} caching addr (I: b996a306f256ee5f39db1e2f5d515c22)`); }
}
// instance cache
this.ibGibs[addr] = ibGib;
// cache svc
if (this.cacheSvc) { await this.cacheSvc.put({ addr, ibGib }); }
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
private async getFromCache({ addr }: { addr: IbGibAddr }): Promise<IbGib_V1 | undefined> {
const lc = `${this.lc}[${this.getFromCache.name}]`;
try {
if (logalot) { console.log(`${lc} starting...`); }
let cached = this.ibGibs[addr];
if (!cached && this.cacheSvc) {
const info = await this.cacheSvc.get({ addr });
cached = info.ibGib;
}
return cached;
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
private deleteFromCache({ addr }: { addr: IbGibAddr }): Promise<void> {
const lc = `${this.lc}[${this.getFromCache.name}]`;
try {
if (logalot) { console.log(`${lc} starting...`); }
delete this.ibGibs[addr];
return Promise.resolve();
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
protected async getImpl(arg: IonicSpaceOptionsIbGib):
Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.getImpl.name}]`;
const resultIbGibs: IbGib_V1[] = [];
const resultData: IonicSpaceResultData = { optsAddr: getIbGibAddr({ ibGib: arg }), }
let notFoundIbGibAddrs: IbGibAddr[] = undefined;
try {
if (!arg.data) { throw new Error('arg.data is falsy'); }
const { ibGibAddrs, isMeta, isDna, } = arg.data!;
const binAddrs = (ibGibAddrs || []).filter(addr => isBinary({ addr }));
let ibGibAddrsNonBin = ibGibAddrs.filter(addr => !binAddrs.includes(addr));
if (logalot) { console.log(`${lc} getting non-bin ibgibs. ibGibAddrsNonBin: ${ibGibAddrsNonBin}`); }
for (let i = 0; i < ibGibAddrsNonBin.length; i++) {
const addr = ibGibAddrsNonBin[i];
if (!arg.data.force && await this.hasInCache({ addr })) {
if (logalot) { console.log(`${lc} YES found in naive cache. (I: 0b23f394fd944c2a96df3543dd0a59c5)`); }
// console.log(`${lc} YES found in naive cache. ${addr} TESTING REMOVE THIS YO (I: 55344e9fa3984bc9bf3f7a8aa3b4cbf5)`);
// console.log(`${lc} YES found in naive cache. TESTING REMOVE THIS YO (I: 55344e9fa3984bc9bf3f7a8aa3b4cbf5)`);
const cached = await this.getFromCache({ addr });
if (!cached) { throw new Error(`(UNEXPECTED) we had in cache but failed to retrieve? (E: 4af334f7a90cb22ffa3b549d0db19a22)`); }
resultIbGibs.push(cached);
} else {
// not found in memory, so look in files
if (!isPrimitive({ gib: h.getIbAndGib({ ibGibAddr: addr }).gib }) && this.cacheSvc) {
if (logalot) { console.log(`${lc} NOT found in naive cache. Expensive(r) loading of file... TESTING REMOVE THIS YO (I: 7983e6afb9bf440a83b90de64c487be3)`); }
}
const getResult = await this.getFile({ addr, isMeta, isDna, });
if (getResult?.success && getResult.ibGib) {
if (logalot) { console.log(`${lc} ${addr} PUTTING IN in naive cache. Expensive(r) loading of file... TESTING REMOVE THIS YO (I: fe0612406c334aa88bcc51bb35150de3)`); }
await this.putInCache({ addr, ibGib: getResult.ibGib })
resultIbGibs.push(getResult.ibGib!);
} else {
// not found in memory or in files
if (!notFoundIbGibAddrs) { notFoundIbGibAddrs = []; }
notFoundIbGibAddrs.push(addr);
}
}
}
for (let i = 0; i < binAddrs.length; i++) {
const addr = binAddrs[i];
const { binHash, binExt } = getBinHashAndExt({ addr });
// getting binary, not a regular ibGib via addr
if (logalot) { console.log(`${lc} getting binHash.binExt: ${binHash}.${binExt}`); }
const getResult = await this.getFile({ addr });
if (getResult?.success && getResult.ibGib?.data) {
if (logalot) { console.log(`${lc} getResult.success. ibGib.data.length: ${getResult.ibGib!.data!.length}`); }
resultIbGibs.push(getResult.ibGib);
} else {
if (logalot) { console.log(`${lc} not found in files. (binData is not cached atm)`) }
// not found in files. (binData is not cached atm)
notFoundIbGibAddrs.push(addr);
}
}
if (notFoundIbGibAddrs && notFoundIbGibAddrs.length > 0) {
resultData.addrsNotFound = notFoundIbGibAddrs;
resultData.success = false;
} else {
resultData.success = true;
}
} catch (error) {
console.error(`${lc} error: ${error.message}`);
resultData.errors = [error.message];
}
try {
const result = await this.resulty({ resultData });
if (resultIbGibs.length > 0) {
result.ibGibs = resultIbGibs;
}
return result;
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
}
}
protected async putImpl(arg: IonicSpaceOptionsIbGib): Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.putImpl.name}]`;
const resultData: IonicSpaceResultData = { optsAddr: getIbGibAddr({ ibGib: arg }), }
const errors: string[] = [];
try {
if (logalot) { console.log(`validating arg and internal state...`); }
if (!arg.data) { throw new Error('arg.data required. (E: d64a46e5efab4b09b57850ecf0854386)'); }
if (arg.ibGibs?.length === 0) { throw new Error(`arg.ibGibs required. (E: b4930d564b284fb9b26b542f14143a28)`); }
if (logalot) { console.log(`arg and internal state validated...calling impl func`); }
return await this.putIbGibsImpl(arg); // <<<< returns early
} catch (error) {
console.error(`${lc} error: ${error.message}`);
resultData.errors = errors.concat([error.message]);
resultData.success = false;
}
// only executes if there is an error.
const result = await this.resulty({ resultData });
return result;
}
protected async putIbGibsImpl(arg: IonicSpaceOptionsIbGib): Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.putIbGibsImpl.name}]`;
const resultData: IonicSpaceResultData = { optsAddr: getIbGibAddr({ ibGib: arg }), }
const errors: string[] = [];
const warnings: string[] = [];
const addrsErrored: IbGibAddr[] = [];
try {
if (!arg.data) { throw new Error('arg.data is falsy (E: aae5757c158d4a799deb7fca9d6245f0)'); }
const { isMeta, isDna, force } = arg.data!;
const ibGibs = arg.ibGibs || [];
const addrsAlreadyHave: IbGibAddr[] = [];
for (let i = 0; i < ibGibs.length; i++) {
const ibGib = ibGibs[i];
const addr = getIbGibAddr({ ibGib });
if (logalot) { console.log(`${lc} checking to see if already exists...`); }
const getResult = await this.getFile({ addr, isMeta, isDna });
// const getResult: any = null; // testing performance
if (getResult?.success && getResult.ibGib) {
// already exists...
if (logalot) { console.log(`${lc} already exists...`); }
if (force) {
// ...but save anyway.
if (logalot) { console.log(`${lc} Forcing save of already put addr: ${addr} (I: 325f363e5d438b43c24b810c150e4e22)`); }
const putResult = await this.putFile({ ibGib, isMeta, isDna });
if (putResult.success) {
if (!isDna) {
// naive cache will cause "memory leak" eventually
await this.putInCache({ addr, ibGib });
}
} else {
errors.push(putResult.errorMsg || `${lc} error putting ${addr}`);
addrsErrored.push(addr);
}
} else {
// ...so annotate
// deb ugger;
warnings.push(`${lc} skipping (non-force) of already exists addr: ${addr} (W: b7fbe22473014dd090db88aee631fecb)`);
addrsAlreadyHave.push(addr);
}
} else {
// does not already exist.
if (logalot) { console.log(`${lc} does NOT already exist...`); }
const putResult = await this.putFile({ ibGib, isMeta, isDna, });
if (putResult.success) {
if (!isDna) { await this.putInCache({ addr, ibGib }); }
} else {
errors.push(putResult.errorMsg || `${lc} error putting ${addr}`);
addrsErrored.push(addr);
}
}
}
if (addrsAlreadyHave.length > 0) { resultData.addrsAlreadyHave = addrsAlreadyHave; }
if (warnings.length > 0) { resultData.warnings = warnings; }
if (errors.length === 0) {
resultData.success = true;
} else {
resultData.errors = errors;
resultData.addrsErrored = addrsErrored;
}
} catch (error) {
console.error(`${lc} error: ${error.message}`);
resultData.errors = errors.concat([error.message]);
resultData.addrsErrored = addrsErrored;
resultData.success = false;
}
const result = await this.resulty({ resultData });
return result;
}
protected async deleteImpl(arg: IonicSpaceOptionsIbGib):
Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.deleteImpl.name}]`;
const resultData: IonicSpaceResultData = { optsAddr: getIbGibAddr({ ibGib: arg }), }
const errors: string[] = [];
const warnings: string[] = [];
const addrsDeleted: IbGibAddr[] = [];
const addrsErrored: IbGibAddr[] = [];
try {
if (!arg.data) { throw new Error('arg.data is falsy'); }
const { isMeta, isDna, } = arg.data!;
const ibGibAddrs = arg.data!.ibGibAddrs || [];
// delete cached entries first
// const cachedAddrs = Object.entries(this.ibGibs)
// .filter(([x,y]) => ibGibAddrs.includes(x))
// .map(([x,y]) => x);
// cachedAddrs.forEach(addr => { delete this.ibGibs[addr]; });
// iterate through ibGibs, but this may be an empty array if we're doing binData.
for (let i = 0; i < ibGibAddrs.length; i++) {
const addr = ibGibAddrs[i];
await this.deleteFromCache({ addr });
const deleteResult = await this.deleteFile({ addr, isMeta, isDna, });
if (deleteResult?.success) {
addrsDeleted.push(addr);
} else {
errors.push(deleteResult.errorMsg || `delete failed: addr (${addr})`);
addrsErrored.push(addr);
}
}
if (warnings.length > 0) { resultData.warnings = warnings; }
if (errors.length === 0) {
resultData.success = true;
resultData.addrs = addrsDeleted;
} else {
resultData.errors = errors;
resultData.addrsErrored = addrsErrored;
if (addrsDeleted.length > 0) {
const warningMsg =
`some addrs (${addrsDeleted.length}) were indeed deleted, but not all. See result addrs and addrsErrored.`;
resultData.warnings = (resultData.warnings || []).concat([warningMsg]);
}
}
} catch (error) {
console.error(`${lc} error: ${error.message}`);
resultData.errors = errors.concat([error.message]);
resultData.addrsErrored = addrsErrored;
resultData.success = false;
}
const result = await this.resulty({ resultData });
return result;
}
protected async getAddrsImpl(arg: IonicSpaceOptionsIbGib):
Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.getAddrsImpl.name}]`;
throw new Error(`${lc} not implemented. (E: 74dd50ce4b564559bc44d6c08d446cb3)`);
}
/**
* Performs a naive `exists: boolean` or `includes: boolean` analog.
*
* If all of the addresses are found, will result in `success` and `can` being `true`.
*
* Else, `can` will be falsy, and `addrsNotFound` will be populated with all/some of
* the queried addresses.
*
* ## notes
*
* This does not take authorization into account in any way. it's a simple, naive in-memory
* storage ibGib witness.
*
* @returns result ibGib whose primary value is `can`
*/
protected async canGetImpl(arg: IonicSpaceOptionsIbGib):
Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.canGetImpl.name}]`;
const resultData: IonicSpaceResultData = { optsAddr: getIbGibAddr({ ibGib: arg }), }
try {
throw new Error(`not implemented (E: 2a45977c584c6bbbc4f164c16106bd22)`);
} catch (error) {
console.error(`${lc} error: ${error.message}`);
resultData.errors = [error.message];
}
const result = await this.resulty({ resultData });
return result;
}
protected async canPutImpl(arg: IonicSpaceOptionsIbGib):
Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.canPutImpl.name}]`;
const resultData: IonicSpaceResultData = { optsAddr: getIbGibAddr({ ibGib: arg }), }
try {
throw new Error(`not implemented (E: 25a005d496efc69faefdec155ffb4a22)`);
} catch (error) {
console.error(`${lc} error: ${error.message}`);
resultData.errors = [error.message];
}
const result = await this.resulty({ resultData });
return result;
}
protected async canDeleteImpl(arg: IonicSpaceOptionsIbGib):
Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.canDeleteImpl.name}]`;
throw new Error(`${lc} not implemented (E: a3306138b4ac429ba5f8f293ad7dd07b)`);
}
protected async getLatestAddrsImpl(arg: IonicSpaceOptionsIbGib):
Promise<IonicSpaceResultIbGib> {
const lc = `${this.lc}[${this.getLatestAddrsImpl.name}]`;
const resultData: IonicSpaceResultData = { optsAddr: getIbGibAddr({ ibGib: arg }), }
try {
if (logalot) { console.log(`${lc} starting...`); }
const latestAddrs = new Set<IbGibAddr>();
const addrsNotFound = new Set<IbGibAddr>();
const addrsErrored = new Set<IbGibAddr>();
resultData.latestAddrsMap = {};
// iterate through incoming ibGibAddrs, get their corresponding
// ibgib from ionic ("file") storage, and use our existing
// `getLatestAddr` function
for (let i = 0; i < arg.data.ibGibAddrs.length; i++) {
const addr = arg.data.ibGibAddrs[i];
const getResult = await this.getFile({ addr });
if (getResult?.success && getResult.ibGib) {
const ibGib = getResult.ibGib!;
const latestAddr = await this.getLatestAddr_Yo({ ibGib });
if (latestAddr) {
latestAddrs.add(latestAddr);
resultData.latestAddrsMap[addr] = latestAddr;
} else {
console.warn(`expecting latestAddr to either be assigned or throw. Adding addr (${addr}) to addrsErrored. (W: 35c3712b1a1e579ccf28c389eb2ecc22)`);
addrsErrored.add(addr);
}
} else if (getResult?.success) {
addrsNotFound.add(addr);
resultData.latestAddrsMap[addr] = null;
} else if (getResult.errorMsg) {
addrsErrored.add(addr);
} else {
throw new Error(`unknown (invalid) getResult: ${h.pretty(getResult)} (E: 2674ad5d30294be29a3d3fdcf54ef6d9)`);
}
}
resultData.addrs = latestAddrs.size > 0 ? [...latestAddrs] : undefined;
resultData.addrsErrored = addrsErrored.size > 0 ? [...addrsErrored] : undefined;
resultData.addrsNotFound = addrsNotFound.size > 0 ? [...addrsNotFound] : undefined;
// we didn't throw, so that's a success
resultData.success = true;
} catch (error) {
const emsg = `${lc} ${error.message}`;
console.error(emsg);
resultData.errors = [emsg];
}
try {
let result = await this.resulty({ resultData });
return result;
} catch (error) {
console.error(`${lc}[resulty] ${error.message}`);
throw error;
}
}
/**
* Ionic space uses a special ibgib "latest" to track the latest ibgibs in
* timelines locally.
*
* @returns latest addr in this space according to the "latest" special ibgib index
*/
private async getLatestAddr_Yo({
ibGib,
}: {
ibGib: IbGib_V1,
}): Promise<IbGibAddr> {
let lc = `${this.lc}[${this.getLatestAddr_Yo.name}]`;
if (logalot) { console.log(`${lc} starting...`); }
try {
if (!ibGib) { throw new Error(`ibGib required (E: 1f8f0cb61d1b4c708b06762048735c22)`); }
// latest addr is indexed by tjpAddr, so we need to get this first...
let ibGibAddr = h.getIbGibAddr({ ibGib });
if (ibGib.gib === GIB) { return ibGibAddr; }
// get the tjp for the rel8nName mapping, and also for some checking logic
let tjp = await getTjpIbGib({ ibGib, space: this });
if (!tjp) {
console.warn(`${lc} tjp not found for ${ibGibAddr}? Should at least just be the ibGib's address itself. (W: 860bdcaaf80548feb6b61b4cde21a722)`);
tjp = ibGib;
}
const tjpAddr = h.getIbGibAddr({ ibGib: tjp });
if (logalot) { console.log(`${lc} tjp (${tjpAddr}) (I: 5245a2ec85a943f98479e93a32d67f22)`); }
const specialLatest = await getSpecialIbGib({ type: "latest", space: this });
if (!specialLatest) { throw new Error(`(UNEXPECTED) specialLatest falsy. Not initialized? (E: 3f475efb6b1b4447a0b002461304bbce)`); }
if (!specialLatest.rel8ns) { specialLatest.rel8ns = {}; }
if (logalot) { console.log(`${lc} specialLatest addr: ${h.getIbGibAddr({ ibGib: specialLatest })}`); }
const latestAddr = specialLatest.rel8ns[tjpAddr]?.length > 0 ?
specialLatest.rel8ns[tjpAddr][0] :
ibGibAddr;
return latestAddr;
} catch (error) {
console.error(`${lc} ${error.message}`);
throw error;
} finally {
if (logalot) { console.log(`${lc} complete.`); }
}
}
/**
* Extremely crude implementation that just
* saves the ibgibs alongside existing data.
*
* ## future
*
* * At the very least, this could be changed similar to dna to have
* its own folder.
*/
protected async persistOptsAndResultIbGibs({
arg,
result,
}: {
arg: IonicSpaceOptionsIbGib,
result: IonicSpaceResultIbGib,
}): Promise<void> {
const lc = `${this.lc}[${this.persistOptsAndResultIbGibs.name}]`;
if (logalot || this.data?.trace) {
console.log(`${lc} doing arg?.data?.cmd: ${arg?.data?.cmd}, result?.data?.success: ${result?.data?.success}`);
}
const ibGibs = [arg, result ?? ROOT];
let argPersist = await argy_<IonicSpaceOptionsData, IonicSpaceOptionsRel8ns, IonicSpaceOptionsIbGib>({
argData: {
cmd: 'put',
isMeta: true,
catchAllErrors: true,
ibGibAddrs: ibGibs.map(x => h.getIbGibAddr({ ibGib: x })),
},
});
argPersist.ibGibs = ibGibs;
// this is a best effort storage, so we aren't using the result
// in the future, we should incorporate what to do if this persistence
// fails into the larger success requirements of spaces.
const resPut = await this.putIbGibsImpl(argPersist);
if (!resPut.data.success || resPut.data.errors) {
console.error(`${lc} Errors persisting arg & result: ${resPut.data.errors.join('\n')}. (E: 65ef314a4f8e445d851dab5b290e9a03)`);
}
}
// #region files related
protected async putFile({
ibGib,
isMeta,
isDna,
}: PutIbGibFileOpts): Promise<PutIbGibFileResult> {
const lc = `${this.lc}[${this.putFile.name}]`;
let result: PutIbGibFileResult = {};
try {
if (!ibGib) { throw new Error(`ibGib required.`) };
const thisData = this.data!;
await this.ensureDirs();
let path: string = "";
let filename: string = "";
let data: any = "";
if (ibGib) {
const addr = getIbGibAddr({ ibGib });
filename = this.getFilename({ addr });
const isBin = isBinary({ addr });
path = this.buildPath({ filename, isMeta, isDna, isBin });
if (!isBin) {
// we only want to persist the ibGib protocol
const bareIbGib: IbGib_V1 = { ib: ibGib.ib, gib: ibGib.gib };
if (ibGib.data) { bareIbGib.data = ibGib.data!; }
if (ibGib.rel8ns) { bareIbGib.rel8ns = ibGib.rel8ns!; }
data = JSON.stringify(bareIbGib);
} else {
data = ibGib.data!;
}
}
if (logalot) { console.log(`${lc} path: ${path}, directory: ${thisData.baseDir}, encoding: ${thisData.encoding} (I: d4440ececbe6c859f8fcf6d7ece12522)`); }
const resWrite = await Filesystem.writeFile({
path,
data,
directory: thisData.baseDir,
encoding: thisData.encoding,
});
if (logalot) { console.log(`${lc} resWrite.uri: ${resWrite.uri}`); }
result.success = true;
} catch (error) {
const errorMsg = `${lc} ${error.message}`;
console.error(errorMsg);
result.errorMsg = errorMsg;
}
return result;
}
protected async deleteFile({
addr,
isMeta,
isDna,
}: DeleteIbGibFileOpts): Promise<DeleteIbGibFilesResult> {
const lc = `${this.lc}[${this.deleteFile.name}]`;
const result: DeleteIbGibFilesResult = {};
try {
if (!addr) { throw new Error(`addr required.`) };
const data = this.data;
let path: string = "";
let filename: string = "";
if (!isBinary({ addr })) {
filename = this.getFilename({ addr });
path = this.buildPath({ filename, isMeta, isDna });
} else {
filename = this.getFilename({ addr });
path = this.buildPath({ filename, isMeta: false, isDna: false, isBin: true });
}
if (logalot) { console.log(`${lc} path: ${path}, directory: ${data.baseDir}`); }
const _ = await Filesystem.deleteFile({
path,
directory: data.baseDir,
});
if (logalot) { console.log(`${lc} deleted. path: ${path}`); }
result.success = true;
} catch (error) {
const errorMsg = `${lc} ${error.message}`;
if (!errorMsg.includes('File does not exist')) {
console.error(errorMsg);
} else {
if (logalot) { console.log(`${lc} attempted to delete non-existent file. ${errorMsg} (I: 8953b51a5f14960e5ea2e86f6c6a7622)`); }
}
result.errorMsg = errorMsg;
}