@derec-alliance/nodejs 0.0.1 → 0.0.3

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/index.js CHANGED
@@ -10,7 +10,7 @@ const SenderKind = Object.freeze({ Owner: 0, Helper: 1, ReplicaSource: 3, Replic
10
10
 
11
11
  const ContactMode = Object.freeze({ InlineKeys: 0, HashedKeys: 1, NoKeys: 2 });
12
12
 
13
- const FlowKind = Object.freeze({ Pairing: 0, Discovery: 1, ProtectSecret: 2, VerifyShares: 3, RecoverSecret: 4, Unpair: 5, UpdateChannelInfo: 6, SyncCheck: 7, RemoveReplica: 8 });
13
+ const FlowKind = Object.freeze({ Pairing: 0, Discovery: 1, ProtectSecret: 2, VerifyShares: 3, RecoverSecret: 4, Unpair: 5, UpdateChannelInfo: 6, ReplicaDiscovery: 7, UnpairReplica: 8 });
14
14
 
15
15
  const primitives = {
16
16
  discovery: {
@@ -95,8 +95,74 @@ const envelope = {
95
95
  read_trace_id: wasm.envelope_read_trace_id,
96
96
  };
97
97
 
98
+ /**
99
+ * Whether a channel or member with these attributes survives `filter`.
100
+ *
101
+ * Every empty field means "do not restrict", `exclude` is applied after `ids`,
102
+ * and the restrictions combine with AND — the same contract the core states on
103
+ * `ChannelFilter`. A store whose backing cannot express the filter as a query
104
+ * can list and call this; that is correct but transfers the rows the filter
105
+ * exists to leave behind.
106
+ *
107
+ * `id` is a decimal string, as ids are everywhere on this bridge. `role` is the
108
+ * peer's `SenderKind` name for `listHelpers` and the member's `ReplicaRole`
109
+ * name for `listReplicas`.
110
+ */
111
+ function channelFilterMatches(filter, id, status, role) {
112
+ if (!filter) return true;
113
+ const ids = filter.ids ?? [];
114
+ const statuses = filter.status ?? [];
115
+ const exclude = filter.exclude ?? [];
116
+ if (ids.length > 0 && !ids.includes(id)) return false;
117
+ if (statuses.length > 0 && !statuses.includes(status)) return false;
118
+ if (filter.role != null && filter.role !== role) return false;
119
+ return !exclude.includes(id);
120
+ }
121
+
122
+ function advertisedEndpoints(message) {
123
+ if (!message) return [];
124
+ const offers = message.supported_transports ?? [];
125
+ if (offers.length > 0) return offers;
126
+ return message.transport_protocol ? [message.transport_protocol] : [];
127
+ }
128
+
129
+ function sequentialFailover(dialer) {
130
+ return {
131
+ async send(endpoints, message) {
132
+ let last;
133
+ for (const endpoint of endpoints) {
134
+ try {
135
+ await dialer(endpoint, message);
136
+ return;
137
+ } catch (e) {
138
+ last = e;
139
+ }
140
+ }
141
+ // `endpoints` is never empty — the library refuses to record a peer
142
+ // whose endpoints were all filtered away — so reaching here means at
143
+ // least one attempt was made and `last` is populated.
144
+ throw last ?? new Error("send was called with no endpoints");
145
+ },
146
+ };
147
+ }
148
+
149
+ function singleEndpointTransport(dialer) {
150
+ return {
151
+ async send(endpoints, message) {
152
+ if (endpoints.length === 0) {
153
+ throw new Error("send was called with no endpoints");
154
+ }
155
+ await dialer(endpoints[0], message);
156
+ },
157
+ };
158
+ }
159
+
98
160
  module.exports = {
99
161
  primitives,
162
+ channelFilterMatches,
163
+ advertisedEndpoints,
164
+ sequentialFailover,
165
+ singleEndpointTransport,
100
166
  envelope,
101
167
  DeRecProtocol,
102
168
  DeRecProtocolBuilder,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@derec-alliance/nodejs",
3
3
  "description": "Node.js WebAssembly bindings for derec-library, the Rust SDK for the DeRec protocol.",
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "type": "git",