Skip to content

Commit c5f22e5

Browse files
tegefaulkesCMCDragonkai
authored andcommitted
NotificationId and PermissionId converted to using @matrixai/id
1 parent 5dbb397 commit c5f22e5

File tree

21 files changed

+503
-211
lines changed

21 files changed

+503
-211
lines changed

src/GenericIdTypes.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Type guards for generic RandomId types.
2+
3+
import { utils as idUtils } from "@matrixai/id";
4+
import { ErrorInvalidId } from "@/errors";
5+
6+
type RawRandomId = Buffer;
7+
type RandomId = string;
8+
const RawRandomIdLength = 16;
9+
10+
function isRawRandomId<T extends RawRandomId>(arg: any): arg is T {
11+
if (!( arg instanceof Buffer)) return false;
12+
return arg.length === RawRandomIdLength;
13+
}
14+
15+
/**
16+
* This will return arg as a valid VaultId or throw an error if it can't be converted.
17+
* This will take a multibase string of the ID or the raw Buffer of the ID.
18+
* @param arg - The variable we wish to convert
19+
* @throws vaultErrors.ErrorInvalidVaultId if the arg can't be converted into a VaultId
20+
* @returns VaultIdRaw
21+
*/
22+
function makeRawRandomId<T extends Buffer>(arg: any): T {
23+
let id = arg;
24+
// Checking and converting a string
25+
if (typeof arg === 'string'){
26+
// Covert the string to the Buffer form.
27+
id = idUtils.fromMultibase(arg);
28+
if (id == null) throw new ErrorInvalidId();
29+
id = Buffer.from(id);
30+
}
31+
32+
// checking if valid buffer.
33+
if (isRawRandomId<T>(id)) return id;
34+
throw new ErrorInvalidId();
35+
}
36+
37+
function isRandomId<T extends RandomId>(arg: any): arg is T {
38+
if (typeof arg !== 'string') return false;
39+
let id = idUtils.fromMultibase(arg);
40+
if (id == null) return false;
41+
return Buffer.from(id).length === RawRandomIdLength;
42+
}
43+
44+
function makeRandomId<T extends RandomId>(arg: any): T {
45+
let id = arg;
46+
if ((id instanceof Buffer)) {
47+
id = idUtils.toMultibase(arg, 'base58btc');
48+
}
49+
if(isRandomId<T>(id)) return id;
50+
throw new ErrorInvalidId();
51+
}
52+
53+
export {
54+
RawRandomId,
55+
RandomId,
56+
isRawRandomId,
57+
makeRawRandomId,
58+
isRandomId,
59+
makeRandomId,
60+
};

src/acl/ACL.ts

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Buffer } from 'buffer';
2-
import type { PermissionId, Permission, VaultActions } from './types';
1+
// import type { Buffer } from 'buffer';
2+
import type { PermissionId, Permission, VaultActions, PermissionIdString } from "./types";
33
import type { DB, DBLevel, DBOp } from '@matrixai/db';
44
import type { NodeId } from '../nodes/types';
55
import type { GestaltAction } from '../gestalts/types';
@@ -12,6 +12,7 @@ import * as aclUtils from './utils';
1212
import * as aclErrors from './errors';
1313
import { errors as dbErrors } from '@matrixai/db';
1414
import { CreateDestroy, ready } from '@matrixai/async-init/dist/CreateDestroy';
15+
import { makePermissionId, makePermissionIdString } from "./utils";
1516

1617
interface ACL extends CreateDestroy {}
1718
@CreateDestroy()
@@ -121,13 +122,15 @@ class ACL {
121122
nodeId2: NodeId,
122123
): Promise<boolean> {
123124
return await this._transaction(async () => {
124-
const permId1 = await this.db.get<PermissionId>(
125+
const permId1 = await this.db.get(
125126
this.aclNodesDbDomain,
126127
nodeId1,
128+
true,
127129
);
128-
const permId2 = await this.db.get<PermissionId>(
130+
const permId2 = await this.db.get(
129131
this.aclNodesDbDomain,
130132
nodeId2,
133+
true,
131134
);
132135
if (permId1 != null && permId2 != null && permId1 === permId2) {
133136
return true;
@@ -139,13 +142,15 @@ class ACL {
139142
@ready(new aclErrors.ErrorACLDestroyed())
140143
public async getNodePerms(): Promise<Array<Record<NodeId, Permission>>> {
141144
return await this._transaction(async () => {
142-
const permIds: Record<PermissionId, Record<NodeId, Permission>> = {};
145+
const permIds: Record<PermissionIdString, Record<NodeId, Permission>> = {};
143146
for await (const o of this.aclNodesDb.createReadStream()) {
144147
const nodeId = (o as any).key as NodeId;
145148
const data = (o as any).value as Buffer;
146-
const permId = await this.db.deserializeDecrypt<PermissionId>(
147-
data,
148-
false,
149+
const permId = makePermissionIdString(
150+
await this.db.deserializeDecrypt(
151+
data,
152+
true,
153+
)
149154
);
150155
let nodePerm: Record<NodeId, Permission>;
151156
if (permId in permIds) {
@@ -161,7 +166,7 @@ class ACL {
161166
} else {
162167
const permRef = (await this.db.get(
163168
this.aclPermsDbDomain,
164-
permId,
169+
makePermissionId(permId),
165170
)) as Ref<Permission>;
166171
nodePerm = { [nodeId]: permRef.object };
167172
permIds[permId] = nodePerm;
@@ -192,9 +197,10 @@ class ACL {
192197
const nodePerm: Record<NodeId, Permission> = {};
193198
const nodeIdsGc: Set<NodeId> = new Set();
194199
for (const nodeId in nodeIds) {
195-
const permId = await this.db.get<PermissionId>(
200+
const permId = await this.db.get(
196201
this.aclNodesDbDomain,
197202
nodeId as NodeId,
203+
true,
198204
);
199205
if (permId == null) {
200206
// Invalid node id
@@ -238,9 +244,10 @@ class ACL {
238244
@ready(new aclErrors.ErrorACLDestroyed())
239245
public async getNodePerm(nodeId: NodeId): Promise<Permission | undefined> {
240246
return await this._transaction(async () => {
241-
const permId = await this.db.get<PermissionId>(
247+
const permId = await this.db.get(
242248
this.aclNodesDbDomain,
243249
nodeId,
250+
true,
244251
);
245252
if (permId == null) {
246253
return;
@@ -273,9 +280,10 @@ class ACL {
273280
const perms: Record<NodeId, Permission> = {};
274281
const nodeIdsGc: Set<NodeId> = new Set();
275282
for (const nodeId in nodeIds) {
276-
const permId = await this.db.get<PermissionId>(
283+
const permId = await this.db.get(
277284
this.aclNodesDbDomain,
278285
nodeId as NodeId,
286+
true,
279287
);
280288
if (permId == null) {
281289
// Invalid node id
@@ -310,9 +318,10 @@ class ACL {
310318
action: GestaltAction,
311319
): Promise<void> {
312320
return await this._transaction(async () => {
313-
const permId = await this.db.get<PermissionId>(
321+
const permId = await this.db.get(
314322
this.aclNodesDbDomain,
315323
nodeId,
324+
true,
316325
);
317326
const ops: Array<DBOp> = [];
318327
if (permId == null) {
@@ -338,6 +347,7 @@ class ACL {
338347
domain: this.aclNodesDbDomain,
339348
key: nodeId,
340349
value: permId,
350+
raw: true,
341351
},
342352
);
343353
} else {
@@ -363,9 +373,10 @@ class ACL {
363373
action: GestaltAction,
364374
): Promise<void> {
365375
return await this._transaction(async () => {
366-
const permId = await this.db.get<PermissionId>(
376+
const permId = await this.db.get(
367377
this.aclNodesDbDomain,
368378
nodeId,
379+
true,
369380
);
370381
if (permId == null) {
371382
return;
@@ -391,9 +402,10 @@ class ACL {
391402
this.aclVaultsDbDomain,
392403
vaultId,
393404
)) ?? {};
394-
const permId = await this.db.get<PermissionId>(
405+
const permId = await this.db.get(
395406
this.aclNodesDbDomain,
396407
nodeId,
408+
true,
397409
);
398410
if (permId == null) {
399411
throw new aclErrors.ErrorACLNodeIdMissing();
@@ -421,6 +433,7 @@ class ACL {
421433
domain: this.aclNodesDbDomain,
422434
key: nodeId,
423435
value: permId,
436+
raw: true,
424437
},
425438
{
426439
type: 'put',
@@ -447,9 +460,10 @@ class ACL {
447460
if (nodeIds == null || !(nodeId in nodeIds)) {
448461
return;
449462
}
450-
const permId = await this.db.get<PermissionId>(
463+
const permId = await this.db.get(
451464
this.aclNodesDbDomain,
452465
nodeId,
466+
true,
453467
);
454468
if (permId == null) {
455469
return;
@@ -489,21 +503,23 @@ class ACL {
489503
perm: Permission,
490504
): Promise<Array<DBOp>> {
491505
const ops: Array<DBOp> = [];
492-
const permIdCounts: Record<PermissionId, number> = {};
506+
const permIdCounts: Record<PermissionIdString, number> = {};
493507
for (const nodeId of nodeIds) {
494-
const permId = await this.db.get<PermissionId>(
508+
const permId = await this.db.get(
495509
this.aclNodesDbDomain,
496510
nodeId,
511+
true,
497512
);
498513
if (permId == null) {
499514
continue;
500515
}
501-
permIdCounts[permId] = (permIdCounts[permId] ?? 0) + 1;
516+
const permIdString = makePermissionIdString(permId);
517+
permIdCounts[permIdString] = (permIdCounts[permIdString] ?? 0) + 1;
502518
}
503519
for (const permId in permIdCounts) {
504520
const permRef = (await this.db.get(
505521
this.aclPermsDbDomain,
506-
permId as PermissionId,
522+
permId as PermissionIdString,
507523
)) as Ref<Permission>;
508524
permRef.count = permRef.count - permIdCounts[permId];
509525
if (permRef.count === 0) {
@@ -538,6 +554,7 @@ class ACL {
538554
type: 'put',
539555
key: nodeId,
540556
value: permId,
557+
raw: true,
541558
});
542559
}
543560
return ops;
@@ -556,9 +573,10 @@ class ACL {
556573
nodeId: NodeId,
557574
perm: Permission,
558575
): Promise<Array<DBOp>> {
559-
const permId = await this.db.get<PermissionId>(
576+
const permId = await this.db.get(
560577
this.aclNodesDbDomain,
561578
nodeId,
579+
true,
562580
);
563581
const ops: Array<DBOp> = [];
564582
if (permId == null) {
@@ -579,6 +597,7 @@ class ACL {
579597
domain: this.aclNodesDbDomain,
580598
key: nodeId,
581599
value: permId,
600+
raw: true,
582601
},
583602
);
584603
} else {
@@ -608,9 +627,10 @@ class ACL {
608627

609628
@ready(new aclErrors.ErrorACLDestroyed())
610629
public async unsetNodePermOps(nodeId: NodeId): Promise<Array<DBOp>> {
611-
const permId = await this.db.get<PermissionId>(
630+
const permId = await this.db.get(
612631
this.aclNodesDbDomain,
613632
nodeId,
633+
true,
614634
);
615635
if (permId == null) {
616636
return [];
@@ -657,9 +677,10 @@ class ACL {
657677
}
658678
const ops: Array<DBOp> = [];
659679
for (const nodeId in nodeIds) {
660-
const permId = await this.db.get<PermissionId>(
680+
const permId = await this.db.get(
661681
this.aclNodesDbDomain,
662682
nodeId as NodeId,
683+
true,
663684
);
664685
// Skip if the nodeId doesn't exist
665686
// this means that it previously been removed
@@ -705,9 +726,10 @@ class ACL {
705726
nodeIdsJoin: Array<NodeId>,
706727
perm?: Permission,
707728
): Promise<Array<DBOp>> {
708-
const permId = await this.db.get<PermissionId>(
729+
const permId = await this.db.get(
709730
this.aclNodesDbDomain,
710731
nodeId,
732+
true,
711733
);
712734
if (permId == null) {
713735
throw new aclErrors.ErrorACLNodeIdMissing();
@@ -722,9 +744,10 @@ class ACL {
722744
permRef.object = perm;
723745
}
724746
for (const nodeIdJoin of nodeIdsJoin) {
725-
const permIdJoin = await this.db.get<PermissionId>(
747+
const permIdJoin = await this.db.get(
726748
this.aclNodesDbDomain,
727749
nodeIdJoin,
750+
true,
728751
);
729752
if (permIdJoin === permId) {
730753
continue;
@@ -756,6 +779,7 @@ class ACL {
756779
domain: this.aclNodesDbDomain,
757780
key: nodeIdJoin,
758781
value: permId,
782+
raw: true,
759783
});
760784
}
761785
ops.push({
@@ -793,9 +817,10 @@ class ACL {
793817
const ops: Array<DBOp> = [];
794818
const nodeIdsGc: Set<NodeId> = new Set();
795819
for (const nodeId in nodeIds) {
796-
const permId = await this.db.get<PermissionId>(
820+
const permId = await this.db.get(
797821
this.aclNodesDbDomain,
798822
nodeId as NodeId,
823+
true,
799824
);
800825
if (permId == null) {
801826
// Invalid node id

src/acl/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import type { Opaque } from '../types';
22
import type { GestaltAction } from '../gestalts/types';
33
import type { VaultActions, VaultId } from "../vaults/types";
4+
import { RandomId, RawRandomId } from "../GenericIdTypes";
45

5-
type PermissionId = Opaque<'PermissionId', string>;
6+
type PermissionId = Buffer;//Opaque<'PermissionId', RawRandomId>;
7+
8+
type PermissionIdString = Opaque<'PermissionIdString', RandomId>;
69

710
type Permission = {
811
gestalt: GestaltActions;
9-
vaults: Record<VaultId, VaultActions>;
12+
vaults: Record<VaultId | string, VaultActions>; // FIXME: the string union on VaultId is to prevent some false errors.
1013
};
1114

1215
type GestaltActions = Partial<Record<GestaltAction, null>>;
1316

14-
export type { PermissionId, Permission, GestaltActions, VaultActions };
17+
export type { PermissionId, PermissionIdString, Permission, GestaltActions, VaultActions };

0 commit comments

Comments
 (0)