@fkn/lib 0.9.54 → 0.9.55

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/rooms.js CHANGED
@@ -4,10 +4,10 @@ import { t as i } from "./busy-DsMWrSzc.js";
4
4
  import { ROOM_TEMPORARY_MS as a } from "./contract.js";
5
5
  //#region src/rooms.ts
6
6
  var o = /* @__PURE__ */ e({
7
- available: () => y,
8
- create: () => S,
9
- join: () => C,
10
- open: () => x
7
+ available: () => w,
8
+ create: () => D,
9
+ join: () => O,
10
+ open: () => E
11
11
  }), s = {
12
12
  wrongKey: "rooms: wrong room key",
13
13
  inviteNoKey: "rooms: the invite carries no key",
@@ -43,6 +43,14 @@ var o = /* @__PURE__ */ e({
43
43
  temporaryUnavailable: "rooms: temporary claims are not available",
44
44
  mailboxUnavailable: "rooms: the mailbox switch is not available",
45
45
  guestUnavailable: "rooms: guest joins are not available",
46
+ schemasUnavailable: "rooms: room schemas are not available",
47
+ badSchema: "rooms: the schema is not one a room can take",
48
+ newerSchema: "rooms: the room holds a newer schema",
49
+ noSchema: "rooms: the room has no schema",
50
+ misfit: "rooms: the message does not fit the room's schema",
51
+ ownerSends: "rooms: only the owner sends this type",
52
+ accountSends: "rooms: sending this needs an account",
53
+ perAccount: "rooms: you have as many of these here as the room allows",
46
54
  tooFast: "rooms: sending too fast",
47
55
  tooLarge: "rooms: the message is too large",
48
56
  unavailable: "rooms: rooms are unavailable",
@@ -55,95 +63,131 @@ var o = /* @__PURE__ */ e({
55
63
  send: !0,
56
64
  receive: !0,
57
65
  maxMessageBytes: 262144
58
- }, d = () => ({
66
+ }, d = 2048, f = () => ({
59
67
  messages: 0,
60
68
  bytes: 0,
61
69
  cap: 5e8,
62
70
  archived: !1
63
- }), f = async () => {
71
+ }), p = async () => {
64
72
  if (!t) return;
65
73
  let e = (await n("a room").catch(() => void 0))?.rooms;
66
74
  return typeof e?.open == "function" ? e : void 0;
67
- }, p = () => {
75
+ }, m = () => {
68
76
  throw c("unavailable", s.unavailable);
69
- }, m = (e) => e === "blocked" ? "blocked" : e === "not-found" ? "ended" : "unavailable", h = (e) => {
77
+ }, h = (e) => e === "blocked" ? "blocked" : e === "not-found" ? "ended" : "unavailable", g = (e) => {
70
78
  if (e !== void 0 && e !== !1) return e === !0 ? a.default : typeof e != "number" || !Number.isInteger(e) ? null : e >= a.min && e <= a.max ? e : null;
71
- }, g = (e, t) => {
79
+ }, _ = (e, t) => {
72
80
  let n = t ? e?.guest : e;
73
81
  return typeof n?.join == "function" ? n : void 0;
74
- }, _ = (e) => {
82
+ }, v = (e) => {
83
+ let t;
84
+ try {
85
+ t = JSON.stringify(e);
86
+ } catch {}
87
+ let n = t === void 0 ? void 0 : JSON.parse(t), r = typeof n == "object" && !!n && !Array.isArray(n) && typeof n.type == "string";
88
+ if (t === void 0 || !r || t.length > d) throw c("invalid", s.misfit);
89
+ return t;
90
+ }, y = (e) => {
91
+ let t;
92
+ try {
93
+ t = JSON.parse(JSON.stringify(e));
94
+ } catch {}
95
+ if (typeof t != "object" || !t || Array.isArray(t)) throw c("invalid", s.badSchema);
96
+ return t;
97
+ }, b = (e) => e?.schema === void 0 ? e : {
98
+ ...e,
99
+ schema: y(e.schema)
100
+ }, x = (e) => {
101
+ if (e.schemas !== !0) throw c("unavailable", s.schemasUnavailable);
102
+ return e;
103
+ }, S = (e) => {
75
104
  let t = e?.guest;
76
105
  if (t !== void 0 && typeof t != "boolean") throw c("invalid", s.malformed);
77
106
  return t === !0;
78
- }, v = async (e, t) => {
79
- let n = e.invite, a = e, o = 0, f = e.self, p = e.owner, _ = e.claimed, v = e.mailbox, y = 0, b = await e.defaults().catch(() => u), x = !1, S = /* @__PURE__ */ new Set(), C = /* @__PURE__ */ new Set(), w = i("room"), T = () => {}, E = () => {}, D = new Promise((e) => {
80
- E = e;
81
- }), O = (e) => {
82
- let t = [...C];
83
- C.clear();
107
+ }, C = async (e, t) => {
108
+ let n = e.invite, a = e, o = 0, d = e.self, p = e.owner, m = e.claimed, b = e.mailbox, S = 0, C = await e.defaults().catch(() => u), w = e.schemas === !0 ? await Promise.resolve(e.schema()).catch(() => null) : null, T = 0, E = !1, D = /* @__PURE__ */ new Set(), O = /* @__PURE__ */ new Set(), k = i("room"), A = () => {}, j = () => {}, M = new Promise((e) => {
109
+ j = e;
110
+ }), N = (e) => {
111
+ let t = [...O];
112
+ O.clear();
84
113
  for (let n of t) try {
85
114
  n(e);
86
115
  } catch {}
87
- }, k = (e) => {
88
- x || (x = !0, T(), S.clear(), w(), O(c("closed", s.ended)), E({ reason: e }));
89
- }, A = (e) => {
90
- if (x) return;
116
+ }, P = (e) => {
117
+ E || (E = !0, A(), D.clear(), k(), N(c("closed", s.ended)), j({ reason: e }));
118
+ }, F = (e) => {
119
+ if (E) return;
91
120
  let t = e;
92
- if (t.type === "defaults" && (b = t.defaults), t.type === "permissions" && t.id === f.id && (f = {
93
- id: f.id,
121
+ if (t.type === "defaults" && (C = t.defaults), t.type === "schema" && (T += 1, w = t.schema), t.type === "message" || t.type === "edited") {
122
+ let e = {
123
+ ...t.message,
124
+ envelope: t.message.envelope ?? null
125
+ };
126
+ t = t.type === "message" ? {
127
+ ...t,
128
+ message: e
129
+ } : {
130
+ type: "edited",
131
+ message: e
132
+ };
133
+ }
134
+ if (t.type === "permissions" && t.id === d.id && (d = {
135
+ id: d.id,
94
136
  permissions: t.permissions,
95
137
  maxMessageBytes: t.maxMessageBytes
96
138
  }), t.type === "claim") {
97
- y += 1;
139
+ S += 1;
98
140
  let e = t.claimed && t.mailbox !== !1;
99
141
  t = {
100
142
  type: "claim",
101
143
  claimed: t.claimed,
102
144
  owner: t.owner,
103
145
  mailbox: e
104
- }, _ = t.claimed, p = t.owner, v = e ? d() : null;
146
+ }, m = t.claimed, p = t.owner, b = e ? f() : null;
105
147
  }
106
- for (let e of [...S]) try {
148
+ for (let e of [...D]) try {
107
149
  e(t);
108
150
  } catch {}
109
- }, j = (e, t) => {
110
- Promise.resolve(e.on(A)).catch(() => {}), Promise.resolve(e.closed).then((e) => {
111
- t === o && k(e.reason);
151
+ }, I = (e, t) => {
152
+ Promise.resolve(e.on(F)).catch(() => {}), Promise.resolve(e.closed).then((e) => {
153
+ t === o && P(e.reason);
112
154
  }, () => {});
113
- }, M = async (e, r) => {
114
- let i = g(e, t);
115
- if (!i) return k("unavailable");
155
+ }, L = async (e, r) => {
156
+ let i = _(e, t);
157
+ if (!i) return P("unavailable");
116
158
  let s = await i.join(n).catch(() => void 0);
117
- if (x || r !== o) return;
118
- if (!s) return k("unavailable");
119
- if ("error" in s) return k(m(s.error));
120
- if (s.self.id !== f.id) return Promise.resolve(s.leave()).catch(() => {}), k("unavailable");
121
- a = s, f = s.self, p = s.owner, _ = s.claimed, v = s.mailbox, j(s, r);
159
+ if (E || r !== o) return;
160
+ if (!s) return P("unavailable");
161
+ if ("error" in s) return P(h(s.error));
162
+ if (s.self.id !== d.id) return Promise.resolve(s.leave()).catch(() => {}), P("unavailable");
163
+ a = s, d = s.self, p = s.owner, m = s.claimed, b = s.mailbox, I(s, r);
122
164
  let c = await s.defaults().catch(() => void 0);
123
- c && !x && r === o && (b = c);
165
+ if (c && !E && r === o && (C = c), s.schemas !== !0) return;
166
+ let l = await Promise.resolve(s.schema()).catch(() => void 0);
167
+ l !== void 0 && !E && r === o && (w = l);
124
168
  };
125
- T = r((e, t) => {
169
+ A = r((e, t) => {
126
170
  let n = o;
127
- o = t, !(x || n === 0 || n === t) && (O(c("unavailable", s.unavailable)), M(e.rooms, t));
128
- }), j(e, o);
129
- let N = async (e) => {
130
- if (x) throw c("closed", s.ended);
171
+ o = t, !(E || n === 0 || n === t) && (N(c("unavailable", s.unavailable)), L(e.rooms, t));
172
+ }), I(e, o);
173
+ let R = async (e) => {
174
+ if (E) throw c("closed", s.ended);
131
175
  let t = () => {}, n = new Promise((e, n) => {
132
176
  t = n;
133
177
  });
134
- C.add(t);
178
+ O.add(t);
135
179
  try {
136
180
  return await Promise.race([Promise.resolve(e(a)), n]);
137
181
  } catch (e) {
138
182
  throw l(e) ? e : c("unavailable", s.unavailable);
139
183
  } finally {
140
- C.delete(t);
184
+ O.delete(t);
141
185
  }
142
- }, P = async (e) => {
143
- let t = await N(e);
186
+ }, z = async (e) => {
187
+ let t = await R(e);
144
188
  if ("error" in t) throw c(t.error, t.message);
145
- }, F = async (e) => {
146
- let t = await N(e);
189
+ }, B = async (e) => {
190
+ let t = await R(e);
147
191
  if (typeof t == "object" && t && "error" in t) throw c(t.error, t.message);
148
192
  return t;
149
193
  };
@@ -153,34 +197,47 @@ var o = /* @__PURE__ */ e({
153
197
  key: e.key,
154
198
  invite: n,
155
199
  get self() {
156
- return f;
200
+ return d;
157
201
  },
158
202
  get owner() {
159
203
  return p;
160
204
  },
161
205
  get claimed() {
162
- return _;
206
+ return m;
163
207
  },
164
208
  get mailbox() {
165
- return v;
209
+ return b;
210
+ },
211
+ defaults: () => C,
212
+ members: () => R((e) => e.members()),
213
+ schema: () => w,
214
+ send: async (e, t) => {
215
+ if (t === void 0) return z((t) => t.send(e));
216
+ let n = v(t);
217
+ return z((t) => x(t).send(e, n));
218
+ },
219
+ edit: async (e, t, n) => {
220
+ if (n === void 0) return z((n) => n.edit(e, t));
221
+ let r = v(n);
222
+ return z((n) => x(n).edit(e, t, r));
223
+ },
224
+ setSchema: async (e) => {
225
+ let t = y(e), n = T;
226
+ await z((e) => x(e).setSchema(t)), T === n && (w = t);
166
227
  },
167
- defaults: () => b,
168
- members: () => N((e) => e.members()),
169
- send: (e) => P((t) => t.send(e)),
170
- edit: (e, t) => P((n) => n.edit(e, t)),
171
- delete: (e, t) => P((n) => n.delete(e, t)),
172
- backlog: (e, t) => F((n) => n.backlog(e, t)),
173
- setDefault: (e, t) => P((n) => n.setDefault(e, t)),
174
- limit: (e, t) => P((n) => n.limit(e, t)),
175
- grant: (e, t) => P((n) => n.grant(e, t)),
176
- revoke: (e, t) => P((n) => n.revoke(e, t)),
177
- remove: (e) => P((t) => t.remove(e)),
178
- block: (e) => P((t) => t.block(e)),
179
- unblock: (e) => P((t) => t.unblock(e)),
228
+ delete: (e, t) => z((n) => n.delete(e, t)),
229
+ backlog: (e, t) => B((n) => n.backlog(e, t)),
230
+ setDefault: (e, t) => z((n) => n.setDefault(e, t)),
231
+ limit: (e, t) => z((n) => n.limit(e, t)),
232
+ grant: (e, t) => z((n) => n.grant(e, t)),
233
+ revoke: (e, t) => z((n) => n.revoke(e, t)),
234
+ remove: (e) => z((t) => t.remove(e)),
235
+ block: (e) => z((t) => t.block(e)),
236
+ unblock: (e) => z((t) => t.unblock(e)),
180
237
  claim: async (e) => {
181
238
  let t = e?.description;
182
239
  if (t !== void 0 && typeof t != "string") throw c("invalid", s.badDescription);
183
- let n = h(e?.temporary);
240
+ let n = g(e?.temporary);
184
241
  if (n === null) throw c("invalid", s.badTemporary);
185
242
  let r = e?.mailbox;
186
243
  if (r !== void 0 && typeof r != "boolean") throw c("invalid", s.malformed);
@@ -189,7 +246,7 @@ var o = /* @__PURE__ */ e({
189
246
  ...n === void 0 ? {} : { temporary: n },
190
247
  ...r === void 0 ? {} : { mailbox: r }
191
248
  };
192
- return Object.keys(i).length === 0 ? P((e) => e.claim()) : P((e) => {
249
+ return Object.keys(i).length === 0 ? z((e) => e.claim()) : z((e) => {
193
250
  if (n !== void 0 && e.temporaryClaims !== !0) throw c("unavailable", s.temporaryUnavailable);
194
251
  if (r === !1 && e.mailboxSwitch !== !0) throw c("unavailable", s.mailboxUnavailable);
195
252
  return e.claim(i);
@@ -197,43 +254,48 @@ var o = /* @__PURE__ */ e({
197
254
  },
198
255
  setMailbox: async (e) => {
199
256
  if (typeof e != "boolean") throw c("invalid", s.malformed);
200
- let t = y;
201
- await P((t) => {
257
+ let t = S;
258
+ await z((t) => {
202
259
  if (t.mailboxSwitch !== !0) throw c("unavailable", s.mailboxUnavailable);
203
260
  return t.setMailbox(e);
204
- }), y === t && (e ? v === null && (v = d()) : v = null);
261
+ }), S === t && (e ? b === null && (b = f()) : b = null);
205
262
  },
206
- release: () => P((e) => e.release()),
263
+ release: () => z((e) => e.release()),
207
264
  usage: async () => {
208
- let e = await F((e) => e.usage());
209
- return v = e, e;
265
+ let e = await B((e) => e.usage());
266
+ return b = e, e;
210
267
  },
211
- on: async (e) => x ? () => {} : (S.add(e), () => {
212
- S.delete(e);
268
+ on: async (e) => E ? () => {} : (D.add(e), () => {
269
+ D.delete(e);
213
270
  }),
214
271
  leave: async () => {
215
- await Promise.resolve(a.leave()).catch(() => {}), k("left");
272
+ await Promise.resolve(a.leave()).catch(() => {}), P("left");
216
273
  },
217
- closed: D
274
+ closed: M
218
275
  };
219
- }, y = async () => {
220
- let e = await f();
276
+ }, w = async () => {
277
+ let e = await p();
221
278
  return e ? e.available().catch(() => !1) : !1;
222
- }, b = async (e, t) => {
223
- let n = await f();
224
- if (!n) throw c("unavailable", s.unavailable);
225
- let r = g(n, e);
226
- if (!r) throw c("unavailable", s.guestUnavailable);
227
- let i = await t(r).catch(p);
228
- if ("error" in i) throw c(i.error, i.message);
229
- return v(i, e);
230
- }, x = async (e, t) => {
279
+ }, T = async (e, t, n) => {
280
+ let r = await p();
281
+ if (!r) throw c("unavailable", s.unavailable);
282
+ let i = _(r, e);
283
+ if (!i) throw c("unavailable", s.guestUnavailable);
284
+ let a = await n(i).catch(m);
285
+ if ("error" in a) throw c(a.error, a.message);
286
+ if (t?.schema !== void 0 && a.schemas !== !0) throw await Promise.resolve(a.leave()).catch(() => {}), c("unavailable", s.schemasUnavailable);
287
+ return C(a, e);
288
+ }, E = async (e, t) => {
231
289
  if (typeof e != "string" || e.length === 0 || e.length > 72) throw c("invalid", s.badName);
232
- return b(_(t), (n) => n.open(e, t));
233
- }, S = async (e) => b(_(e), (t) => t.create(e)), C = async (e, t) => {
290
+ let n = S(t), r = b(t);
291
+ return T(n, r, (t) => t.open(e, r));
292
+ }, D = async (e) => {
293
+ let t = S(e), n = b(e);
294
+ return T(t, n, (e) => e.create(n));
295
+ }, O = async (e, t) => {
234
296
  let n = typeof e == "string" ? e.indexOf(".") : -1;
235
297
  if (n <= 0 || n === e.length - 1) throw c("invalid", s.inviteNoKey);
236
- return b(_(t), (n) => n.join(e, t));
298
+ return T(S(t), void 0, (n) => n.join(e, t));
237
299
  };
238
300
  //#endregion
239
- export { y as available, S as create, C as join, x as open, o as t };
301
+ export { w as available, D as create, O as join, E as open, o as t };
package/storage.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Ct as StorageProgress, Ot as StoredObject, kt as StoredPage, wt as StoragePutOptions, xt as StorageErrorCode } from "./contract-CMQOic3K.js";
1
+ import { Dt as StorageProgress, Mt as StoredObject, Nt as StoredPage, Ot as StoragePutOptions, Tt as StorageErrorCode } from "./contract-DFycgFJK.js";
2
2
  declare namespace storage_d_exports {
3
3
  export { StorageError, StorageErrorCode, StorageProgress, StoragePutOptions, StoredBlob, StoredObject, StoredPage, available, remove as delete, get, list, put };
4
4
  }