@calimero-network/mero-js 22.5.1 → 22.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,48 @@ export declare const CAPABILITIES: {
30
30
  };
31
31
  export type CapabilityName = keyof typeof CAPABILITIES;
32
32
  export type CapabilityBit = (typeof CAPABILITIES)[CapabilityName];
33
+ /**
34
+ * Ready-made masks for `setDefaultCapabilities` / `setMemberCapabilities` and
35
+ * `RelayClient.foundNamespace({ defaultCapabilities })`, so an app can name the
36
+ * default it gives members instead of passing a bare number.
37
+ *
38
+ * Why this matters: a namespace's default mask is copied into a non-admin
39
+ * member's row when they join, and a namespace starts without
40
+ * `CAN_CREATE_CONTEXT` in it. An app that never sets a default leaves every
41
+ * invited member unable to create a context until an admin grants it by hand.
42
+ *
43
+ * None of these include `CAN_AUTHOR_ON_BEHALF`; grant that deliberately (see
44
+ * `AdminApiClient.grantAuthorship` / `openToDelegatedExecution`).
45
+ */
46
+ export declare const CAPABILITY_PRESETS: {
47
+ /** No capabilities. */
48
+ readonly NONE: 0;
49
+ /** Join Open subgroups only: what a namespace starts with. */
50
+ readonly JOIN_ONLY: number;
51
+ /**
52
+ * Create contexts, invite members, join Open subgroups (= 7). A member who
53
+ * can work in the namespace but not reshape it.
54
+ */
55
+ readonly CONTRIBUTOR: number;
56
+ /**
57
+ * `CONTRIBUTOR` plus create and delete subgroups and manage their visibility
58
+ * (= 231). What mero-chat gives members, so they can start and close their
59
+ * own channels.
60
+ */
61
+ readonly COLLABORATOR: number;
62
+ };
63
+ export type CapabilityPreset = keyof typeof CAPABILITY_PRESETS;
64
+ /**
65
+ * Builds a u32 mask from capability names and/or bit values:
66
+ * `capMask('CAN_CREATE_CONTEXT', 'CAN_INVITE_MEMBERS') === 3`.
67
+ * Throws on a name core does not assign, so a typo can't silently yield 0.
68
+ */
69
+ export declare function capMask(...caps: Array<CapabilityName | number>): number;
70
+ /**
71
+ * The names of the assigned capability bits set in `mask`, in bit order.
72
+ * Unassigned bits (10 and above) are not reported.
73
+ */
74
+ export declare function capNames(mask: number): CapabilityName[];
33
75
  /**
34
76
  * Returns true if `mask` has every bit of `cap` set. Both operands are
35
77
  * coerced to unsigned 32-bit (`>>> 0`) before comparing so a high bit such
@@ -34,6 +34,70 @@ export const CAPABILITIES = {
34
34
  */
35
35
  CAN_AUTHOR_ON_BEHALF: 1 << 9,
36
36
  };
37
+ /**
38
+ * Ready-made masks for `setDefaultCapabilities` / `setMemberCapabilities` and
39
+ * `RelayClient.foundNamespace({ defaultCapabilities })`, so an app can name the
40
+ * default it gives members instead of passing a bare number.
41
+ *
42
+ * Why this matters: a namespace's default mask is copied into a non-admin
43
+ * member's row when they join, and a namespace starts without
44
+ * `CAN_CREATE_CONTEXT` in it. An app that never sets a default leaves every
45
+ * invited member unable to create a context until an admin grants it by hand.
46
+ *
47
+ * None of these include `CAN_AUTHOR_ON_BEHALF`; grant that deliberately (see
48
+ * `AdminApiClient.grantAuthorship` / `openToDelegatedExecution`).
49
+ */
50
+ export const CAPABILITY_PRESETS = {
51
+ /** No capabilities. */
52
+ NONE: 0,
53
+ /** Join Open subgroups only: what a namespace starts with. */
54
+ JOIN_ONLY: CAPABILITIES.CAN_JOIN_OPEN_SUBGROUPS,
55
+ /**
56
+ * Create contexts, invite members, join Open subgroups (= 7). A member who
57
+ * can work in the namespace but not reshape it.
58
+ */
59
+ CONTRIBUTOR: CAPABILITIES.CAN_CREATE_CONTEXT |
60
+ CAPABILITIES.CAN_INVITE_MEMBERS |
61
+ CAPABILITIES.CAN_JOIN_OPEN_SUBGROUPS,
62
+ /**
63
+ * `CONTRIBUTOR` plus create and delete subgroups and manage their visibility
64
+ * (= 231). What mero-chat gives members, so they can start and close their
65
+ * own channels.
66
+ */
67
+ COLLABORATOR: CAPABILITIES.CAN_CREATE_CONTEXT |
68
+ CAPABILITIES.CAN_INVITE_MEMBERS |
69
+ CAPABILITIES.CAN_JOIN_OPEN_SUBGROUPS |
70
+ CAPABILITIES.CAN_CREATE_SUBGROUP |
71
+ CAPABILITIES.CAN_DELETE_SUBGROUP |
72
+ CAPABILITIES.CAN_MANAGE_VISIBILITY,
73
+ };
74
+ /**
75
+ * Builds a u32 mask from capability names and/or bit values:
76
+ * `capMask('CAN_CREATE_CONTEXT', 'CAN_INVITE_MEMBERS') === 3`.
77
+ * Throws on a name core does not assign, so a typo can't silently yield 0.
78
+ */
79
+ export function capMask(...caps) {
80
+ let mask = 0;
81
+ for (const cap of caps) {
82
+ if (typeof cap === 'number') {
83
+ mask |= cap;
84
+ }
85
+ else if (Object.prototype.hasOwnProperty.call(CAPABILITIES, cap)) {
86
+ mask |= CAPABILITIES[cap];
87
+ }
88
+ else {
89
+ throw new Error(`unknown capability: ${String(cap)}`);
90
+ }
91
+ }
92
+ return mask >>> 0;
93
+ }
94
+ /**
95
+ * The names of the assigned capability bits set in `mask`, in bit order.
96
+ * Unassigned bits (10 and above) are not reported.
97
+ */
98
+ export function capNames(mask) {
99
+ return Object.keys(CAPABILITIES).filter((name) => hasCap(mask, CAPABILITIES[name]));
100
+ }
37
101
  /**
38
102
  * Returns true if `mask` has every bit of `cap` set. Both operands are
39
103
  * coerced to unsigned 32-bit (`>>> 0`) before comparing so a high bit such