@zapiack/react-native 0.1.0 → 0.2.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.
- package/CHANGELOG.md +5 -0
- package/README.md +90 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/zapiack/reactnative/ZapiackPushModule.kt +3 -0
- package/dist/calls.d.mts +198 -0
- package/dist/calls.d.ts +198 -0
- package/dist/calls.js +793 -0
- package/dist/calls.mjs +406 -0
- package/dist/chunk-CCLICX3L.mjs +371 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +389 -6
- package/dist/index.mjs +21 -2
- package/dist/realtime-BAcafCSr.d.mts +173 -0
- package/dist/realtime-BAcafCSr.d.ts +173 -0
- package/ios/Zapiack/Api.swift +6 -2
- package/ios/Zapiack/LayoutModel.swift +128 -0
- package/ios/Zapiack/LayoutView.swift +206 -0
- package/ios/Zapiack/Models.swift +1 -1
- package/ios/Zapiack/PushClient.swift +9 -1
- package/ios/Zapiack/ZapiackLiveActivityWidget.swift +170 -0
- package/ios/Zapiack/ZapiackLiveAttributes.swift +78 -0
- package/ios/Zapiack/ZapiackPush.swift +27 -0
- package/ios/ZapiackPushModule.m +1 -0
- package/ios/ZapiackPushModule.swift +5 -0
- package/package.json +32 -5
package/dist/index.js
CHANGED
|
@@ -18,14 +18,385 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
DEFAULT_REALTIME_URL: () => DEFAULT_REALTIME_URL,
|
|
23
24
|
ZapiackError: () => ZapiackError,
|
|
24
25
|
ZapiackPush: () => ZapiackPush,
|
|
25
|
-
|
|
26
|
+
ZapiackRealtime: () => ZapiackRealtime,
|
|
27
|
+
ZapiackRealtimeError: () => ZapiackRealtimeError,
|
|
28
|
+
browserRealtimeEnv: () => browserRealtimeEnv,
|
|
29
|
+
default: () => src_default
|
|
26
30
|
});
|
|
27
|
-
module.exports = __toCommonJS(
|
|
31
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
32
|
var import_react_native = require("react-native");
|
|
33
|
+
|
|
34
|
+
// src/realtime.ts
|
|
35
|
+
var ZapiackRealtimeError = class extends Error {
|
|
36
|
+
constructor(message, code = 0, reason) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.code = code;
|
|
39
|
+
this.reason = reason;
|
|
40
|
+
this.name = "ZapiackRealtimeError";
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var DEFAULT_REALTIME_URL = "wss://rt.zapiack.com";
|
|
44
|
+
var OPEN = 1;
|
|
45
|
+
var MAX_OUTBOX = 100;
|
|
46
|
+
var browserRealtimeEnv = () => ({
|
|
47
|
+
socket: (url) => new globalThis.WebSocket(url),
|
|
48
|
+
setTimeout: (fn, ms) => setTimeout(fn, ms),
|
|
49
|
+
clearTimeout: (id) => clearTimeout(id),
|
|
50
|
+
setInterval: (fn, ms) => setInterval(fn, ms),
|
|
51
|
+
clearInterval: (id) => clearInterval(id),
|
|
52
|
+
random: () => Math.random()
|
|
53
|
+
});
|
|
54
|
+
var after = (a, b) => {
|
|
55
|
+
if (!b) return true;
|
|
56
|
+
const [am = 0, as = 0] = a.split("-").map(Number);
|
|
57
|
+
const [bm = 0, bs = 0] = b.split("-").map(Number);
|
|
58
|
+
return am > bm || am === bm && as > bs;
|
|
59
|
+
};
|
|
60
|
+
var ZapiackRealtime = class {
|
|
61
|
+
constructor(options, env = browserRealtimeEnv(), now = Date.now) {
|
|
62
|
+
this.env = env;
|
|
63
|
+
this.now = now;
|
|
64
|
+
this.ws = null;
|
|
65
|
+
this._state = "idle";
|
|
66
|
+
this.subs = /* @__PURE__ */ new Map();
|
|
67
|
+
this.presenceHandlers = /* @__PURE__ */ new Map();
|
|
68
|
+
this.members = /* @__PURE__ */ new Map();
|
|
69
|
+
this.waiting = /* @__PURE__ */ new Map();
|
|
70
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
71
|
+
this.rpcSeq = 0;
|
|
72
|
+
this.stateHandlers = /* @__PURE__ */ new Set();
|
|
73
|
+
this.errorHandlers = /* @__PURE__ */ new Set();
|
|
74
|
+
this.outbox = [];
|
|
75
|
+
this.attempt = 0;
|
|
76
|
+
this.wanted = false;
|
|
77
|
+
this.heard = 0;
|
|
78
|
+
this.ackSeq = 0;
|
|
79
|
+
this.first = null;
|
|
80
|
+
/** Set by an error the server marks fatal, for runtimes that cannot see a custom close code. */
|
|
81
|
+
this.refused = false;
|
|
82
|
+
this._connId = null;
|
|
83
|
+
if (typeof options?.getToken !== "function") throw new ZapiackRealtimeError("getToken is required: a function that asks your server for a token.");
|
|
84
|
+
this.o = { url: DEFAULT_REALTIME_URL, heartbeatMs: 25e3, backoffMs: { min: 1e3, max: 3e4 }, ackTimeoutMs: 1e4, ...options };
|
|
85
|
+
}
|
|
86
|
+
get state() {
|
|
87
|
+
return this._state;
|
|
88
|
+
}
|
|
89
|
+
/** This connection's id, once connected. Other members use it to signal you. */
|
|
90
|
+
get connectionId() {
|
|
91
|
+
return this._connId;
|
|
92
|
+
}
|
|
93
|
+
onState(fn) {
|
|
94
|
+
this.stateHandlers.add(fn);
|
|
95
|
+
return () => this.stateHandlers.delete(fn);
|
|
96
|
+
}
|
|
97
|
+
onError(fn) {
|
|
98
|
+
this.errorHandlers.add(fn);
|
|
99
|
+
return () => this.errorHandlers.delete(fn);
|
|
100
|
+
}
|
|
101
|
+
/** Connects and resolves once the server has said hello. It keeps itself connected afterwards until `disconnect()`. */
|
|
102
|
+
connect() {
|
|
103
|
+
if (this._state === "connected") return Promise.resolve();
|
|
104
|
+
if (this.first) return new Promise((resolve, reject) => this.chain(resolve, reject));
|
|
105
|
+
this.wanted = true;
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
this.first = { resolve, reject };
|
|
108
|
+
void this.open();
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
chain(resolve, reject) {
|
|
112
|
+
const f = this.first;
|
|
113
|
+
this.first = {
|
|
114
|
+
resolve: () => {
|
|
115
|
+
f.resolve();
|
|
116
|
+
resolve();
|
|
117
|
+
},
|
|
118
|
+
reject: (e) => {
|
|
119
|
+
f.reject(e);
|
|
120
|
+
reject(e);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/** Leaves for good: closes the socket, forgets the pending sends. Subscriptions stay, for a later `connect()`. */
|
|
125
|
+
disconnect() {
|
|
126
|
+
this.wanted = false;
|
|
127
|
+
this.env.clearTimeout(this.retry);
|
|
128
|
+
this.stopBeat();
|
|
129
|
+
const ws = this.ws;
|
|
130
|
+
this.ws = null;
|
|
131
|
+
ws?.close(1e3, "bye");
|
|
132
|
+
this.failAll(new ZapiackRealtimeError("Disconnected."));
|
|
133
|
+
this.outbox = [];
|
|
134
|
+
this.set("closed");
|
|
135
|
+
}
|
|
136
|
+
/** Calls `handler` for every message on the channel, now and after reconnects. Returns how to stop. */
|
|
137
|
+
subscribe(channel, handler, options = {}) {
|
|
138
|
+
let sub = this.subs.get(channel);
|
|
139
|
+
const fresh = !sub;
|
|
140
|
+
if (!sub) this.subs.set(channel, sub = { handlers: /* @__PURE__ */ new Set(), cursor: options.resumeFrom });
|
|
141
|
+
sub.handlers.add(handler);
|
|
142
|
+
if (fresh) this.send({ op: "subscribe", channel, resumeFrom: sub.cursor });
|
|
143
|
+
return () => {
|
|
144
|
+
const s = this.subs.get(channel);
|
|
145
|
+
if (!s) return;
|
|
146
|
+
s.handlers.delete(handler);
|
|
147
|
+
if (s.handlers.size === 0) {
|
|
148
|
+
this.subs.delete(channel);
|
|
149
|
+
this.presenceHandlers.delete(channel);
|
|
150
|
+
this.members.delete(channel);
|
|
151
|
+
this.send({ op: "unsubscribe", channel });
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/** Sends a message. Resolves when the server confirms, unless `ack: false`. */
|
|
156
|
+
publish(channel, data, options = {}) {
|
|
157
|
+
const frame = { op: "publish", channel, event: options.event ?? "message", data, persist: options.persist === true };
|
|
158
|
+
if (options.ack === false) {
|
|
159
|
+
this.send(frame);
|
|
160
|
+
return Promise.resolve();
|
|
161
|
+
}
|
|
162
|
+
const ackId = `a${++this.ackSeq}`;
|
|
163
|
+
frame["ackId"] = ackId;
|
|
164
|
+
return new Promise((resolve, reject) => {
|
|
165
|
+
const timer = this.env.setTimeout(() => {
|
|
166
|
+
this.waiting.delete(ackId);
|
|
167
|
+
reject(new ZapiackRealtimeError("The server did not confirm in time.", 0));
|
|
168
|
+
}, this.o.ackTimeoutMs);
|
|
169
|
+
this.waiting.set(ackId, { resolve, reject, timer });
|
|
170
|
+
if (!this.send(frame)) {
|
|
171
|
+
this.settle(ackId)?.reject(new ZapiackRealtimeError("Could not send: too many messages waiting for a connection.", 429));
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Asks a Zapiack service something over this connection and waits for the answer: the way a call client talks to the media server.
|
|
177
|
+
* Needs a token minted for a call. It is never queued for later: if the line is down it fails at once, because an answer to an old
|
|
178
|
+
* question is worse than none.
|
|
179
|
+
*/
|
|
180
|
+
request(method, data = {}) {
|
|
181
|
+
if (!this.ws || this.ws.readyState !== OPEN || this._state !== "connected") return Promise.reject(new ZapiackRealtimeError("Not connected.", 0, "no_route"));
|
|
182
|
+
const correlationId = `r${++this.rpcSeq}`;
|
|
183
|
+
return new Promise((resolve, reject) => {
|
|
184
|
+
const timer = this.env.setTimeout(() => {
|
|
185
|
+
this.pending.delete(correlationId);
|
|
186
|
+
reject(new ZapiackRealtimeError("The server did not answer in time.", 0, "timeout"));
|
|
187
|
+
}, this.o.ackTimeoutMs + 2e3);
|
|
188
|
+
this.pending.set(correlationId, { resolve, reject, timer });
|
|
189
|
+
this.ws.send(JSON.stringify({ op: "rpc", method, correlationId, data }));
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/** Sends to one connection in a channel you are subscribed to. Used for peer-to-peer setup such as WebRTC. */
|
|
193
|
+
signal(channel, to, event, data) {
|
|
194
|
+
this.send({ op: "signal", channel, to, event, data });
|
|
195
|
+
}
|
|
196
|
+
/** Presence in a channel you are subscribed to. */
|
|
197
|
+
presence(channel) {
|
|
198
|
+
return {
|
|
199
|
+
enter: (data = null) => this.send({ op: "presence.enter", channel, data }),
|
|
200
|
+
update: (data) => this.send({ op: "presence.update", channel, data }),
|
|
201
|
+
leave: () => this.send({ op: "presence.leave", channel }),
|
|
202
|
+
/** Who is here now, as last told. */
|
|
203
|
+
members: () => [...this.members.get(channel)?.values() ?? []],
|
|
204
|
+
onChange: (fn) => {
|
|
205
|
+
let set = this.presenceHandlers.get(channel);
|
|
206
|
+
if (!set) this.presenceHandlers.set(channel, set = /* @__PURE__ */ new Set());
|
|
207
|
+
set.add(fn);
|
|
208
|
+
return () => set.delete(fn);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
// ───────────────────────────── internals
|
|
213
|
+
set(s) {
|
|
214
|
+
if (this._state === s) return;
|
|
215
|
+
this._state = s;
|
|
216
|
+
for (const fn of this.stateHandlers) fn(s);
|
|
217
|
+
}
|
|
218
|
+
fail(e) {
|
|
219
|
+
for (const fn of this.errorHandlers) fn(e);
|
|
220
|
+
}
|
|
221
|
+
settle(ackId) {
|
|
222
|
+
const w = this.waiting.get(ackId);
|
|
223
|
+
if (!w) return void 0;
|
|
224
|
+
this.waiting.delete(ackId);
|
|
225
|
+
this.env.clearTimeout(w.timer);
|
|
226
|
+
return w;
|
|
227
|
+
}
|
|
228
|
+
failAll(e) {
|
|
229
|
+
for (const id of [...this.waiting.keys()]) this.settle(id)?.reject(e);
|
|
230
|
+
this.failRequests(e);
|
|
231
|
+
}
|
|
232
|
+
failRequests(e) {
|
|
233
|
+
for (const [id, p] of [...this.pending]) {
|
|
234
|
+
this.pending.delete(id);
|
|
235
|
+
this.env.clearTimeout(p.timer);
|
|
236
|
+
p.reject(e);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/** Writes now if the line is open, otherwise keeps it (a short while) for when it is. False when there is no room. */
|
|
240
|
+
send(frame) {
|
|
241
|
+
const text = JSON.stringify(frame);
|
|
242
|
+
if (this.ws && this.ws.readyState === OPEN && this._state === "connected") {
|
|
243
|
+
this.ws.send(text);
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
if (frame["op"] === "subscribe" || frame["op"] === "unsubscribe") return true;
|
|
247
|
+
if (this.outbox.length >= MAX_OUTBOX) return false;
|
|
248
|
+
this.outbox.push(text);
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
async open() {
|
|
252
|
+
if (!this.wanted) return;
|
|
253
|
+
this.set(this.attempt === 0 && this._state !== "reconnecting" ? "connecting" : "reconnecting");
|
|
254
|
+
let token;
|
|
255
|
+
try {
|
|
256
|
+
token = await this.o.getToken();
|
|
257
|
+
if (!token || typeof token !== "string") throw new Error("getToken returned no token.");
|
|
258
|
+
} catch (e) {
|
|
259
|
+
this.fail(new ZapiackRealtimeError(`Could not get a token: ${e instanceof Error ? e.message : String(e)}`, 0));
|
|
260
|
+
return this.later();
|
|
261
|
+
}
|
|
262
|
+
if (!this.wanted) return;
|
|
263
|
+
let ws;
|
|
264
|
+
try {
|
|
265
|
+
ws = this.env.socket(`${this.o.url.replace(/\/+$/, "")}/?token=${encodeURIComponent(token)}`);
|
|
266
|
+
} catch (e) {
|
|
267
|
+
this.fail(new ZapiackRealtimeError(`Could not open the connection: ${e instanceof Error ? e.message : String(e)}`, 0));
|
|
268
|
+
return this.later();
|
|
269
|
+
}
|
|
270
|
+
this.ws = ws;
|
|
271
|
+
ws.onmessage = (ev) => this.onFrame(ws, ev.data);
|
|
272
|
+
ws.onerror = () => void 0;
|
|
273
|
+
ws.onclose = (ev) => this.onClose(ws, ev?.code ?? 1006);
|
|
274
|
+
}
|
|
275
|
+
onFrame(ws, raw) {
|
|
276
|
+
if (ws !== this.ws) return;
|
|
277
|
+
this.heard = this.now();
|
|
278
|
+
let f;
|
|
279
|
+
try {
|
|
280
|
+
f = JSON.parse(String(raw));
|
|
281
|
+
} catch {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
switch (f["op"]) {
|
|
285
|
+
case "hello":
|
|
286
|
+
this._connId = f["connId"];
|
|
287
|
+
this.attempt = 0;
|
|
288
|
+
this.set("connected");
|
|
289
|
+
this.rejoin();
|
|
290
|
+
for (const text of this.outbox.splice(0)) ws.send(text);
|
|
291
|
+
this.startBeat(ws);
|
|
292
|
+
this.first?.resolve();
|
|
293
|
+
this.first = null;
|
|
294
|
+
return;
|
|
295
|
+
case "message":
|
|
296
|
+
return this.onMessage(f);
|
|
297
|
+
case "ack": {
|
|
298
|
+
this.settle(f["ackId"])?.resolve({ msgId: f["msgId"], delivered: f["delivered"] ?? 0 });
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
case "rpc.result":
|
|
302
|
+
case "rpc.error": {
|
|
303
|
+
const id = String(f["correlationId"]);
|
|
304
|
+
const p = this.pending.get(id);
|
|
305
|
+
if (!p) return;
|
|
306
|
+
this.pending.delete(id);
|
|
307
|
+
this.env.clearTimeout(p.timer);
|
|
308
|
+
if (f["op"] === "rpc.result") p.resolve(f["data"]);
|
|
309
|
+
else p.reject(new ZapiackRealtimeError(String(f["message"] ?? "The request failed."), 0, String(f["code"] ?? "internal")));
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
case "error": {
|
|
313
|
+
if (f["fatal"] === true) this.refused = true;
|
|
314
|
+
const err = new ZapiackRealtimeError(String(f["message"] ?? "Error"), Number(f["code"] ?? 0));
|
|
315
|
+
const ref = typeof f["ref"] === "string" ? f["ref"] : void 0;
|
|
316
|
+
if (ref && this.settle(ref)?.reject(err)) return;
|
|
317
|
+
return this.fail(err);
|
|
318
|
+
}
|
|
319
|
+
case "presence.members": {
|
|
320
|
+
const map = /* @__PURE__ */ new Map();
|
|
321
|
+
for (const m of f["members"] ?? []) map.set(m.connId, m);
|
|
322
|
+
this.members.set(f["channel"], map);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
case "resume.failed":
|
|
326
|
+
return this.fail(new ZapiackRealtimeError(`Missed messages on ${f["channel"]} could not be replayed. Reload its history.`, 410));
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
onMessage(m) {
|
|
330
|
+
if (m.event.startsWith("presence.")) return this.onPresence(m);
|
|
331
|
+
const sub = this.subs.get(m.channel);
|
|
332
|
+
if (!sub) return;
|
|
333
|
+
if (m.persisted) {
|
|
334
|
+
if (!after(m.msgId, sub.cursor)) return;
|
|
335
|
+
sub.cursor = m.msgId;
|
|
336
|
+
}
|
|
337
|
+
for (const h of [...sub.handlers]) {
|
|
338
|
+
try {
|
|
339
|
+
h(m);
|
|
340
|
+
} catch {
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
onPresence(m) {
|
|
345
|
+
const d = m.data;
|
|
346
|
+
const action = m.event.slice("presence.".length);
|
|
347
|
+
const map = this.members.get(m.channel) ?? /* @__PURE__ */ new Map();
|
|
348
|
+
this.members.set(m.channel, map);
|
|
349
|
+
if (action === "leave") map.delete(d.connId);
|
|
350
|
+
else map.set(d.connId, { connId: d.connId, user: d.user, data: d.data, at: m.ts });
|
|
351
|
+
for (const fn of this.presenceHandlers.get(m.channel) ?? []) fn({ action, member: { connId: d.connId, user: d.user, data: d.data } });
|
|
352
|
+
}
|
|
353
|
+
rejoin() {
|
|
354
|
+
for (const [channel, sub] of this.subs) this.ws?.send(JSON.stringify({ op: "subscribe", channel, resumeFrom: sub.cursor }));
|
|
355
|
+
}
|
|
356
|
+
startBeat(ws) {
|
|
357
|
+
this.stopBeat();
|
|
358
|
+
this.heard = this.now();
|
|
359
|
+
this.beat = this.env.setInterval(() => {
|
|
360
|
+
if (ws !== this.ws) return;
|
|
361
|
+
if (this.now() - this.heard > this.o.heartbeatMs * 2) return void ws.close(4e3, "no heartbeat");
|
|
362
|
+
if (ws.readyState === OPEN) ws.send('{"op":"ping"}');
|
|
363
|
+
}, this.o.heartbeatMs);
|
|
364
|
+
}
|
|
365
|
+
stopBeat() {
|
|
366
|
+
this.env.clearInterval(this.beat);
|
|
367
|
+
this.beat = void 0;
|
|
368
|
+
}
|
|
369
|
+
onClose(ws, code) {
|
|
370
|
+
if (ws !== this.ws) return;
|
|
371
|
+
this.ws = null;
|
|
372
|
+
this.stopBeat();
|
|
373
|
+
this._connId = null;
|
|
374
|
+
this.failRequests(new ZapiackRealtimeError("The connection dropped.", 0, "no_route"));
|
|
375
|
+
if (!this.wanted) return;
|
|
376
|
+
if (code === 4403 || this.refused) {
|
|
377
|
+
this.refused = false;
|
|
378
|
+
this.wanted = false;
|
|
379
|
+
this.failAll(new ZapiackRealtimeError("The project does not allow this connection.", 403));
|
|
380
|
+
this.first?.reject(new ZapiackRealtimeError("The project does not allow this connection.", 403));
|
|
381
|
+
this.first = null;
|
|
382
|
+
this.set("closed");
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
this.set("reconnecting");
|
|
386
|
+
this.later(code === 4401 || code === 1012 ? 0 : void 0);
|
|
387
|
+
}
|
|
388
|
+
later(force) {
|
|
389
|
+
this.env.clearTimeout(this.retry);
|
|
390
|
+
this.attempt += 1;
|
|
391
|
+
const { min, max } = this.o.backoffMs;
|
|
392
|
+
const ceiling = Math.min(max, min * 2 ** (this.attempt - 1));
|
|
393
|
+
const wait = force ?? Math.round(min + this.env.random() * (ceiling - min));
|
|
394
|
+
this.set("reconnecting");
|
|
395
|
+
this.retry = this.env.setTimeout(() => void this.open(), wait);
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// src/index.ts
|
|
29
400
|
var ZapiackError = class extends Error {
|
|
30
401
|
constructor(message) {
|
|
31
402
|
super(message);
|
|
@@ -87,6 +458,14 @@ var ZapiackPush = {
|
|
|
87
458
|
flush() {
|
|
88
459
|
return native().flush();
|
|
89
460
|
},
|
|
461
|
+
/**
|
|
462
|
+
* Lets Zapiack start Live Activities on iOS 17.2 and later: a template push with a timer or progress then counts down on the lock
|
|
463
|
+
* screen and in the Dynamic Island. Also add ZapiackLiveActivityWidget to a widget extension (see the iOS README). Does nothing on
|
|
464
|
+
* Android, where those pushes are drawn in the notification itself.
|
|
465
|
+
*/
|
|
466
|
+
enableLiveActivities() {
|
|
467
|
+
return native().enableLiveActivities();
|
|
468
|
+
},
|
|
90
469
|
/** Notifications tapped while the app is running. Returns a function that stops listening. */
|
|
91
470
|
onTap(listener) {
|
|
92
471
|
const module2 = native();
|
|
@@ -99,9 +478,13 @@ var ZapiackPush = {
|
|
|
99
478
|
return native().initialTap();
|
|
100
479
|
}
|
|
101
480
|
};
|
|
102
|
-
var
|
|
481
|
+
var src_default = ZapiackPush;
|
|
103
482
|
// Annotate the CommonJS export names for ESM import in node:
|
|
104
483
|
0 && (module.exports = {
|
|
484
|
+
DEFAULT_REALTIME_URL,
|
|
105
485
|
ZapiackError,
|
|
106
|
-
ZapiackPush
|
|
486
|
+
ZapiackPush,
|
|
487
|
+
ZapiackRealtime,
|
|
488
|
+
ZapiackRealtimeError,
|
|
489
|
+
browserRealtimeEnv
|
|
107
490
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_REALTIME_URL,
|
|
3
|
+
ZapiackRealtime,
|
|
4
|
+
ZapiackRealtimeError,
|
|
5
|
+
browserRealtimeEnv
|
|
6
|
+
} from "./chunk-CCLICX3L.mjs";
|
|
7
|
+
|
|
1
8
|
// src/index.ts
|
|
2
9
|
import { NativeEventEmitter, NativeModules, Platform } from "react-native";
|
|
3
10
|
var ZapiackError = class extends Error {
|
|
@@ -61,6 +68,14 @@ var ZapiackPush = {
|
|
|
61
68
|
flush() {
|
|
62
69
|
return native().flush();
|
|
63
70
|
},
|
|
71
|
+
/**
|
|
72
|
+
* Lets Zapiack start Live Activities on iOS 17.2 and later: a template push with a timer or progress then counts down on the lock
|
|
73
|
+
* screen and in the Dynamic Island. Also add ZapiackLiveActivityWidget to a widget extension (see the iOS README). Does nothing on
|
|
74
|
+
* Android, where those pushes are drawn in the notification itself.
|
|
75
|
+
*/
|
|
76
|
+
enableLiveActivities() {
|
|
77
|
+
return native().enableLiveActivities();
|
|
78
|
+
},
|
|
64
79
|
/** Notifications tapped while the app is running. Returns a function that stops listening. */
|
|
65
80
|
onTap(listener) {
|
|
66
81
|
const module = native();
|
|
@@ -73,9 +88,13 @@ var ZapiackPush = {
|
|
|
73
88
|
return native().initialTap();
|
|
74
89
|
}
|
|
75
90
|
};
|
|
76
|
-
var
|
|
91
|
+
var src_default = ZapiackPush;
|
|
77
92
|
export {
|
|
93
|
+
DEFAULT_REALTIME_URL,
|
|
78
94
|
ZapiackError,
|
|
79
95
|
ZapiackPush,
|
|
80
|
-
|
|
96
|
+
ZapiackRealtime,
|
|
97
|
+
ZapiackRealtimeError,
|
|
98
|
+
browserRealtimeEnv,
|
|
99
|
+
src_default as default
|
|
81
100
|
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zapiack Realtime: live messages over one WebSocket.
|
|
3
|
+
*
|
|
4
|
+
* Your server mints a short-lived token with its API key (`POST /v1/realtime/tokens`) and hands it to the client, so the API key never
|
|
5
|
+
* reaches a browser or an app. The client asks for a token every time it connects, so a token that expires is simply replaced.
|
|
6
|
+
*
|
|
7
|
+
* It reconnects by itself with a growing, jittered pause, re-joins every channel and asks for what it missed since the last persisted
|
|
8
|
+
* message it saw, so a lost signal costs no messages. Nothing here touches push, so it works in any JavaScript runtime that has a
|
|
9
|
+
* WebSocket: browsers, React Native, Node 22+.
|
|
10
|
+
*/
|
|
11
|
+
type RealtimeState = 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'closed';
|
|
12
|
+
interface RealtimeMessage<T = unknown> {
|
|
13
|
+
msgId: string;
|
|
14
|
+
ts: number;
|
|
15
|
+
channel: string;
|
|
16
|
+
event: string;
|
|
17
|
+
data: T;
|
|
18
|
+
/** The user id (or connection id) of the sender. */
|
|
19
|
+
from: string | null;
|
|
20
|
+
/** True when it was sent while this client was away and is being replayed. */
|
|
21
|
+
replayed?: boolean;
|
|
22
|
+
}
|
|
23
|
+
interface PresenceMember {
|
|
24
|
+
connId: string;
|
|
25
|
+
user: string | null;
|
|
26
|
+
data: unknown;
|
|
27
|
+
at: number;
|
|
28
|
+
}
|
|
29
|
+
interface PublishOptions {
|
|
30
|
+
/** The name of the event. Default `message`. */
|
|
31
|
+
event?: string;
|
|
32
|
+
/** Keep it in the channel's history, so clients that were away can replay it. Default false. */
|
|
33
|
+
persist?: boolean;
|
|
34
|
+
/** Wait for the server to confirm. Default true. Pass false to fire and forget. */
|
|
35
|
+
ack?: boolean;
|
|
36
|
+
}
|
|
37
|
+
interface PublishResult {
|
|
38
|
+
msgId: string;
|
|
39
|
+
/** How many connections were subscribed when it was sent. */
|
|
40
|
+
delivered: number;
|
|
41
|
+
}
|
|
42
|
+
declare class ZapiackRealtimeError extends Error {
|
|
43
|
+
/** The server's code: 401 token, 403 not allowed, 413 too large, 429 slow down, 503 try again. 0 for a problem on this side. */
|
|
44
|
+
readonly code: number;
|
|
45
|
+
/** For a failed request (`request()`): the service's own word for it: `forbidden`, `invalid`, `no_route`, `timeout`, `rate_limited` or `internal`. */
|
|
46
|
+
readonly reason?: string | undefined;
|
|
47
|
+
constructor(message: string,
|
|
48
|
+
/** The server's code: 401 token, 403 not allowed, 413 too large, 429 slow down, 503 try again. 0 for a problem on this side. */
|
|
49
|
+
code?: number,
|
|
50
|
+
/** For a failed request (`request()`): the service's own word for it: `forbidden`, `invalid`, `no_route`, `timeout`, `rate_limited` or `internal`. */
|
|
51
|
+
reason?: string | undefined);
|
|
52
|
+
}
|
|
53
|
+
interface SocketLike {
|
|
54
|
+
readyState: number;
|
|
55
|
+
send(data: string): void;
|
|
56
|
+
close(code?: number, reason?: string): void;
|
|
57
|
+
onopen: ((ev?: unknown) => void) | null;
|
|
58
|
+
onmessage: ((ev: {
|
|
59
|
+
data: unknown;
|
|
60
|
+
}) => void) | null;
|
|
61
|
+
onclose: ((ev: {
|
|
62
|
+
code: number;
|
|
63
|
+
}) => void) | null;
|
|
64
|
+
onerror: ((ev?: unknown) => void) | null;
|
|
65
|
+
}
|
|
66
|
+
interface RealtimeEnv {
|
|
67
|
+
socket(url: string): SocketLike;
|
|
68
|
+
setTimeout(fn: () => void, ms: number): unknown;
|
|
69
|
+
clearTimeout(id: unknown): void;
|
|
70
|
+
setInterval(fn: () => void, ms: number): unknown;
|
|
71
|
+
clearInterval(id: unknown): void;
|
|
72
|
+
random(): number;
|
|
73
|
+
}
|
|
74
|
+
interface RealtimeOptions {
|
|
75
|
+
/** Returns a fresh token from your server. Called on every connect, so an expired token is replaced. */
|
|
76
|
+
getToken: () => Promise<string>;
|
|
77
|
+
/** Default `wss://rt.zapiack.com`. */
|
|
78
|
+
url?: string;
|
|
79
|
+
/** How often to check the line is alive. Default 25 seconds. */
|
|
80
|
+
heartbeatMs?: number;
|
|
81
|
+
/** The shortest and longest pause before reconnecting. Default 1s and 30s. */
|
|
82
|
+
backoffMs?: {
|
|
83
|
+
min: number;
|
|
84
|
+
max: number;
|
|
85
|
+
};
|
|
86
|
+
/** How long to wait for a confirmation. Default 10 seconds. */
|
|
87
|
+
ackTimeoutMs?: number;
|
|
88
|
+
}
|
|
89
|
+
declare const DEFAULT_REALTIME_URL = "wss://rt.zapiack.com";
|
|
90
|
+
declare const browserRealtimeEnv: () => RealtimeEnv;
|
|
91
|
+
type Handler = (m: RealtimeMessage) => void;
|
|
92
|
+
declare class ZapiackRealtime {
|
|
93
|
+
private readonly env;
|
|
94
|
+
private readonly now;
|
|
95
|
+
private readonly o;
|
|
96
|
+
private ws;
|
|
97
|
+
private _state;
|
|
98
|
+
private readonly subs;
|
|
99
|
+
private readonly presenceHandlers;
|
|
100
|
+
private readonly members;
|
|
101
|
+
private readonly waiting;
|
|
102
|
+
private readonly pending;
|
|
103
|
+
private rpcSeq;
|
|
104
|
+
private readonly stateHandlers;
|
|
105
|
+
private readonly errorHandlers;
|
|
106
|
+
private outbox;
|
|
107
|
+
private attempt;
|
|
108
|
+
private wanted;
|
|
109
|
+
private retry;
|
|
110
|
+
private beat;
|
|
111
|
+
private heard;
|
|
112
|
+
private ackSeq;
|
|
113
|
+
private first;
|
|
114
|
+
/** Set by an error the server marks fatal, for runtimes that cannot see a custom close code. */
|
|
115
|
+
private refused;
|
|
116
|
+
private _connId;
|
|
117
|
+
constructor(options: RealtimeOptions, env?: RealtimeEnv, now?: () => number);
|
|
118
|
+
get state(): RealtimeState;
|
|
119
|
+
/** This connection's id, once connected. Other members use it to signal you. */
|
|
120
|
+
get connectionId(): string | null;
|
|
121
|
+
onState(fn: (s: RealtimeState) => void): () => void;
|
|
122
|
+
onError(fn: (e: ZapiackRealtimeError) => void): () => void;
|
|
123
|
+
/** Connects and resolves once the server has said hello. It keeps itself connected afterwards until `disconnect()`. */
|
|
124
|
+
connect(): Promise<void>;
|
|
125
|
+
private chain;
|
|
126
|
+
/** Leaves for good: closes the socket, forgets the pending sends. Subscriptions stay, for a later `connect()`. */
|
|
127
|
+
disconnect(): void;
|
|
128
|
+
/** Calls `handler` for every message on the channel, now and after reconnects. Returns how to stop. */
|
|
129
|
+
subscribe(channel: string, handler: Handler, options?: {
|
|
130
|
+
resumeFrom?: string;
|
|
131
|
+
}): () => void;
|
|
132
|
+
/** Sends a message. Resolves when the server confirms, unless `ack: false`. */
|
|
133
|
+
publish(channel: string, data: unknown, options?: PublishOptions): Promise<PublishResult | void>;
|
|
134
|
+
/**
|
|
135
|
+
* Asks a Zapiack service something over this connection and waits for the answer: the way a call client talks to the media server.
|
|
136
|
+
* Needs a token minted for a call. It is never queued for later: if the line is down it fails at once, because an answer to an old
|
|
137
|
+
* question is worse than none.
|
|
138
|
+
*/
|
|
139
|
+
request<T = unknown>(method: string, data?: unknown): Promise<T>;
|
|
140
|
+
/** Sends to one connection in a channel you are subscribed to. Used for peer-to-peer setup such as WebRTC. */
|
|
141
|
+
signal(channel: string, to: string, event: string, data: unknown): void;
|
|
142
|
+
/** Presence in a channel you are subscribed to. */
|
|
143
|
+
presence(channel: string): {
|
|
144
|
+
enter: (data?: unknown) => boolean;
|
|
145
|
+
update: (data: unknown) => boolean;
|
|
146
|
+
leave: () => boolean;
|
|
147
|
+
/** Who is here now, as last told. */
|
|
148
|
+
members: () => PresenceMember[];
|
|
149
|
+
onChange: (fn: (e: {
|
|
150
|
+
action: "enter" | "update" | "leave";
|
|
151
|
+
member: Partial<PresenceMember>;
|
|
152
|
+
}) => void) => () => boolean;
|
|
153
|
+
};
|
|
154
|
+
private set;
|
|
155
|
+
private fail;
|
|
156
|
+
private settle;
|
|
157
|
+
private failAll;
|
|
158
|
+
private failRequests;
|
|
159
|
+
/** Writes now if the line is open, otherwise keeps it (a short while) for when it is. False when there is no room. */
|
|
160
|
+
private send;
|
|
161
|
+
private open;
|
|
162
|
+
private onFrame;
|
|
163
|
+
private onMessage;
|
|
164
|
+
private onPresence;
|
|
165
|
+
private rejoin;
|
|
166
|
+
private startBeat;
|
|
167
|
+
private stopBeat;
|
|
168
|
+
private onClose;
|
|
169
|
+
private later;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export { DEFAULT_REALTIME_URL as D, ZapiackRealtime as Z, ZapiackRealtimeError as a, browserRealtimeEnv as g };
|
|
173
|
+
export type { PresenceMember as P, RealtimeEnv as R, SocketLike as S, PublishOptions as b, PublishResult as c, RealtimeMessage as d, RealtimeOptions as e, RealtimeState as f };
|