zyro-gateway 1.0.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 ADDED
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2026-05-17
9
+
10
+ ### Added
11
+
12
+ - Modular server under `src/` (config, routes, Socket.IO, terminal logging).
13
+ - CLI: `zyro-gateway`, `zyro-gateway config`, `zyro-gateway help`.
14
+ - Programmatic API: `require('zyro-gateway/server')` → `createGateway()`, `start()`.
15
+ - HTTP fallback: `POST /api/register`, `POST /api/income`, `POST /api/notification`.
16
+ - REST: dashboard, devices, transactions, notifications.
17
+ - Browser client (`Zyro.connect`) bundled to `dist/zyro.js`.
18
+ - Documentation: README setup, API reference, website integration.
19
+
20
+ ### Changed
21
+
22
+ - npm package name `zyro-gateway` (the `zyro` name is already taken on npm).
23
+
24
+ [1.0.0]: https://github.com/orod-codes/zyro-getway/releases/tag/v1.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 orod-codes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,226 @@
1
+ # Zyro Gateway
2
+
3
+ [![npm version](https://img.shields.io/npm/v/zyro-gateway)](https://www.npmjs.com/package/zyro-gateway)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
5
+
6
+ Real-time sync between a **phone monitor app** (Flutter) and your **desktop terminal / website**. Income transactions and notifications flow over Socket.IO, with HTTP fallback when sockets are unavailable.
7
+
8
+ ## Features
9
+
10
+ - Terminal server with live **connected devices** and **income logs** (name, amount, sender, ref)
11
+ - Browser client (`Zyro.connect`) for dashboards and stores
12
+ - Pairing rooms — isolate traffic per store with a short pairing code
13
+ - REST API for polling clients and HTTP-only phones
14
+ - Config via `zyro.config.js` in your project (not committed)
15
+
16
+ ## Requirements
17
+
18
+ - Node.js **18+**
19
+ - Phone app and website on the same LAN as the gateway (or USB `adb reverse` for local dev)
20
+
21
+ ## Quick start
22
+
23
+ ```bash
24
+ npm install zyro-gateway
25
+ npx zyro-gateway config # creates ./zyro.config.js
26
+ # Edit ip, port, pairingCode in zyro.config.js
27
+ npx zyro-gateway # start server
28
+ ```
29
+
30
+ From a git clone:
31
+
32
+ ```bash
33
+ cd zyrogetway
34
+ npm install
35
+ npm run config
36
+ npm start
37
+ ```
38
+
39
+ The terminal prints **config path**, **pairing code**, **IP**, and **port**. Use those in your phone app under **Setup → Zyro Gateway**.
40
+
41
+ ### USB debugging (Android)
42
+
43
+ ```bash
44
+ adb reverse tcp:3001 tcp:3001
45
+ ```
46
+
47
+ On the phone, set gateway IP to `127.0.0.1` and port `3001` (match `zyro.config.js`).
48
+
49
+ ## Configuration
50
+
51
+ | Field | Description |
52
+ |--------|-------------|
53
+ | `ip` | PC LAN address. Leave `''` to auto-detect. |
54
+ | `port` | Listen port (default `3000` if unset; example uses `3001`). |
55
+ | `pairingCode` | 4–12 letters/numbers; must match the phone app. |
56
+ | `deviceName` | Optional label for web clients. |
57
+ | `autoConnect` | Browser client: connect on load. |
58
+ | `pollIntervalMs` | HTTP polling interval when sockets fail. |
59
+
60
+ **Load order**
61
+
62
+ 1. `ZYRO_CONFIG` environment variable (absolute path)
63
+ 2. `./zyro.config.js` in the current working directory
64
+ 3. `zyro.config.js` inside the installed package
65
+
66
+ **Environment**
67
+
68
+ | Variable | Purpose |
69
+ |----------|---------|
70
+ | `PORT` | Override listen port |
71
+ | `ZYRO_CONFIG` | Path to config file |
72
+
73
+ ## Phone app
74
+
75
+ Point **Setup → Zyro Gateway** at the same **IP**, **port**, and **pairing code** shown in the terminal.
76
+
77
+ When Socket.IO is blocked, the app can register and post income over HTTP (`/api/register`, `/api/income`). The terminal still lists the device and logs income.
78
+
79
+ ## Website integration
80
+
81
+ Serve the bundled client from the gateway (after `npm run build` / install):
82
+
83
+ ```html
84
+ <script src="http://YOUR-IP:3001/socket.io/socket.io.js"></script>
85
+ <script src="http://YOUR-IP:3001/zyro/zyro.js"></script>
86
+ <script>
87
+ const sync = Zyro.connect({
88
+ ip: '192.168.1.10',
89
+ port: 3001,
90
+ pairingCode: 'MYSTORE',
91
+ role: 'desktop',
92
+ deviceName: 'My Store',
93
+ });
94
+
95
+ sync.on('transaction', (tx) => {
96
+ console.log('Income', tx.amount, tx.name);
97
+ });
98
+
99
+ sync.on('notification', (note) => {
100
+ console.log('Notification', note.title);
101
+ });
102
+
103
+ sync.on('dashboard', (data) => {
104
+ console.log('Today total', data.stats.todayTotal);
105
+ });
106
+ </script>
107
+ ```
108
+
109
+ ### ESM / bundlers
110
+
111
+ ```javascript
112
+ import { connect } from 'zyro-gateway';
113
+ const sync = connect({ ip: '192.168.1.10', port: 3001, pairingCode: 'MYSTORE', role: 'desktop' });
114
+ ```
115
+
116
+ ## Programmatic server
117
+
118
+ Embed the gateway in your own Node process:
119
+
120
+ ```javascript
121
+ const { createGateway } = require('zyro-gateway/server');
122
+
123
+ async function main() {
124
+ const gateway = createGateway({ packageRoot: __dirname });
125
+ const { url, pairingCode } = await gateway.start();
126
+ console.log('Listening at', url, 'pairing', pairingCode);
127
+ }
128
+
129
+ main();
130
+ ```
131
+
132
+ `createGateway()` returns `{ app, server, io, start(), stop() }` without listening until you call `start()`.
133
+
134
+ ## HTTP API
135
+
136
+ All pairing-scoped routes accept `?pairing=CODE` or header `X-Pairing: CODE`. If `pairingCode` is set in `zyro.config.js`, it becomes the default.
137
+
138
+ | Method | Path | Description |
139
+ |--------|------|-------------|
140
+ | GET | `/` | Service index + endpoint list |
141
+ | GET | `/api/info` | Server info, URLs, features |
142
+ | GET | `/api/config` | IP, port, pairing from config |
143
+ | GET | `/api/dashboard` | Today stats + recent activity |
144
+ | GET | `/api/devices` | Connected devices for pairing room |
145
+ | GET | `/api/transactions` | Transactions (`?after=ISO` for incremental) |
146
+ | GET | `/api/notifications` | Notifications (`?after=ISO`) |
147
+ | POST | `/api/register` | Register phone via HTTP |
148
+ | POST | `/api/income` | Post income JSON body |
149
+ | POST | `/api/notification` | Post notification JSON body |
150
+
151
+ Static client: `GET /zyro/zyro.js`
152
+
153
+ ## Socket.IO events
154
+
155
+ **Client → server**
156
+
157
+ | Event | Payload |
158
+ |--------|---------|
159
+ | `register` | `{ deviceName, platform, ... }` |
160
+ | `income_transaction` | Transaction object |
161
+ | `notification_event` | Notification object |
162
+
163
+ **Server → client**
164
+
165
+ | Event | Description |
166
+ |--------|-------------|
167
+ | `sync_ready` | Handshake OK |
168
+ | `sync_error` | Invalid pairing |
169
+ | `history` | Recent transactions |
170
+ | `notification_history` | Recent notifications |
171
+ | `income_transaction` | New transaction |
172
+ | `notification_event` | New notification |
173
+ | `dashboard_update` | Stats snapshot |
174
+ | `presence` | Device counts |
175
+ | `device_joined` / `device_left` / `device_updated` | Device lifecycle |
176
+
177
+ Connect query: `?pairing=MYSTORE&role=phone|desktop&deviceName=Label`
178
+
179
+ ## CLI
180
+
181
+ ```bash
182
+ zyro-gateway # Start server
183
+ zyro-gateway config # Create zyro.config.js in cwd
184
+ zyro-gateway help # Usage
185
+ ```
186
+
187
+ ## Project layout
188
+
189
+ ```
190
+ bin/zyro-gateway.js CLI entry
191
+ src/
192
+ config/ load-config, pairing helpers
193
+ server/ routes, socket, broadcast, terminal
194
+ utils/ network, formatting
195
+ index.js start() + exports
196
+ zyro/zyro.js ESM browser client (source)
197
+ dist/zyro.js IIFE bundle (published)
198
+ scripts/ build, init-config
199
+ server.js npm start shim
200
+ ```
201
+
202
+ ## npm scripts (development)
203
+
204
+ | Script | Action |
205
+ |--------|--------|
206
+ | `npm run config` | Create `zyro.config.js` in cwd |
207
+ | `npm run build` | Bundle browser client to `dist/zyro.js` |
208
+ | `npm start` | Build + start gateway |
209
+ | `npm run dev` | Start with `--watch` |
210
+
211
+ ## Publish to npm
212
+
213
+ The package name on npm is **`zyro-gateway`** (`zyro` is already registered).
214
+
215
+ ```bash
216
+ npm run build
217
+ npm publish --access public
218
+ ```
219
+
220
+ ## License
221
+
222
+ MIT — see [LICENSE](LICENSE).
223
+
224
+ ## Repository
225
+
226
+ https://github.com/orod-codes/zyro-getway
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const cmd = process.argv[2];
5
+
6
+ if (cmd === 'config' || cmd === 'init') {
7
+ require('../scripts/init-config.js');
8
+ } else if (cmd === 'help' || cmd === '--help' || cmd === '-h') {
9
+ console.log(`
10
+ Zyro Gateway — real-time phone ↔ desktop sync
11
+
12
+ Usage:
13
+ zyro-gateway Start the gateway server
14
+ zyro-gateway config Create zyro.config.js in the current directory
15
+ zyro-gateway help Show this message
16
+
17
+ Environment:
18
+ PORT Override listen port
19
+ ZYRO_CONFIG Path to zyro.config.js
20
+
21
+ Docs: https://github.com/orod-codes/zyro-getway#readme
22
+ `);
23
+ } else {
24
+ require('../src/index').start();
25
+ }
package/dist/zyro.js ADDED
@@ -0,0 +1,395 @@
1
+ var Zyro = (() => {
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // zyro/browser-entry.js
21
+ var browser_entry_exports = {};
22
+ __export(browser_entry_exports, {
23
+ DEFAULTS: () => DEFAULTS,
24
+ EVENTS: () => EVENTS,
25
+ ZyroConnection: () => ZyroConnection,
26
+ connect: () => connect
27
+ });
28
+
29
+ // zyro/zyro.js
30
+ var DEFAULTS = {
31
+ ip: "",
32
+ port: 3e3,
33
+ serverUrl: "",
34
+ pairingCode: "",
35
+ role: "desktop",
36
+ deviceName: "Web Client",
37
+ autoConnect: true,
38
+ pollIntervalMs: 1500,
39
+ maxItems: 200
40
+ };
41
+ function resolveBaseUrl(config) {
42
+ if (config.serverUrl) return normalizeUrl(config.serverUrl);
43
+ const ip = String(config.ip || "").trim();
44
+ const port = Number(config.port) || 3e3;
45
+ if (!ip) {
46
+ if (typeof location !== "undefined") {
47
+ return normalizeUrl(`http://${location.hostname}:${port}`);
48
+ }
49
+ throw new Error("Zyro: set ip and port in zyro.config.js");
50
+ }
51
+ return normalizeUrl(`http://${ip}:${port}`);
52
+ }
53
+ var EVENTS = {
54
+ READY: "ready",
55
+ STATUS: "status",
56
+ ERROR: "error",
57
+ TRANSACTION: "transaction",
58
+ NOTIFICATION: "notification",
59
+ DASHBOARD: "dashboard",
60
+ PRESENCE: "presence",
61
+ DEVICES: "devices"
62
+ };
63
+ function normalizeUrl(url) {
64
+ let u = String(url || "").trim().replace(/\/+$/, "");
65
+ if (!u && typeof location !== "undefined") return location.origin;
66
+ if (u.startsWith("ws://")) u = `http://${u.slice(5)}`;
67
+ if (u.startsWith("wss://")) u = `https://${u.slice(6)}`;
68
+ return u;
69
+ }
70
+ function normalizePairing(raw) {
71
+ const p = String(raw || "").trim().toUpperCase().replace(/[^A-Z0-9]/g, "");
72
+ if (p.length < 4 || p.length > 12) return "";
73
+ return p;
74
+ }
75
+ function txKey(tx) {
76
+ if (tx.id) return String(tx.id);
77
+ return `${tx.transactionNumber || tx.referenceNumber || ""}|${tx.timestamp || tx.receivedAt}|${tx.amount}`;
78
+ }
79
+ function noteKey(n) {
80
+ if (n.id) return String(n.id);
81
+ return `${n.timestamp || n.receivedAt}|${n.preview}`;
82
+ }
83
+ var ZyroConnection = class {
84
+ constructor(options = {}) {
85
+ this.config = { ...DEFAULTS, ...options };
86
+ this._handlers = /* @__PURE__ */ new Map();
87
+ this._socket = null;
88
+ this._baseUrl = "";
89
+ this._pairingCode = "";
90
+ this._status = "idle";
91
+ this._pollTimers = [];
92
+ this.transactions = [];
93
+ this.notifications = [];
94
+ this.devices = /* @__PURE__ */ new Map();
95
+ this.dashboard = null;
96
+ this.presence = { phones: 0, desktops: 0 };
97
+ this._seenTx = /* @__PURE__ */ new Set();
98
+ this._seenNotes = /* @__PURE__ */ new Set();
99
+ this._lastTxTime = "";
100
+ this._lastNoteTime = "";
101
+ if (this.config.autoConnect) {
102
+ this.connect().catch((err) => this._emit(EVENTS.ERROR, err));
103
+ }
104
+ }
105
+ on(event, fn) {
106
+ if (!this._handlers.has(event)) this._handlers.set(event, /* @__PURE__ */ new Set());
107
+ this._handlers.get(event).add(fn);
108
+ return () => this.off(event, fn);
109
+ }
110
+ off(event, fn) {
111
+ this._handlers.get(event)?.delete(fn);
112
+ }
113
+ _emit(event, ...args) {
114
+ const set = this._handlers.get(event);
115
+ if (!set) return;
116
+ for (const fn of [...set]) fn(...args);
117
+ }
118
+ _setStatus(status, detail = {}) {
119
+ this._status = status;
120
+ this._emit(EVENTS.STATUS, { status, ...detail });
121
+ }
122
+ async connect() {
123
+ this._baseUrl = resolveBaseUrl(this.config);
124
+ this._setStatus("connecting");
125
+ const infoRes = await fetch(`${this._baseUrl}/api/info`, { cache: "no-store" });
126
+ if (!infoRes.ok) throw new Error(`Zyro: cannot reach server (${infoRes.status})`);
127
+ const configured = normalizePairing(this.config.pairingCode);
128
+ if (!configured) {
129
+ throw new Error(
130
+ "Zyro: pairingCode is required in zyro.config.js (4\u201312 letters/numbers)"
131
+ );
132
+ }
133
+ this._pairingCode = configured;
134
+ const info = await infoRes.json();
135
+ const cfgPort = Number(this.config.port) || 3e3;
136
+ if (info.port && info.port !== cfgPort) {
137
+ console.warn(
138
+ `Zyro: server port is ${info.port} but zyro.config.js port is ${cfgPort} \u2014 fix port and restart npm start`
139
+ );
140
+ }
141
+ if (info.ip && !this.config.ip) {
142
+ this.config.ip = info.ip;
143
+ }
144
+ this._connectSocket();
145
+ this._startPollers();
146
+ if (typeof document !== "undefined") {
147
+ document.addEventListener("visibilitychange", () => {
148
+ if (document.visibilityState === "visible") {
149
+ this._pollTransactions();
150
+ this._pollNotifications();
151
+ this._refreshDashboard();
152
+ if (this._socket && !this._socket.connected) this._socket.connect();
153
+ }
154
+ });
155
+ }
156
+ return { pairingCode: this._pairingCode, serverUrl: this._baseUrl };
157
+ }
158
+ async setPairingCode(code) {
159
+ const next = normalizePairing(code);
160
+ if (!next) throw new Error("Zyro: invalid pairing code (4\u201312 letters/numbers)");
161
+ this.config.pairingCode = next;
162
+ this.disconnect();
163
+ this._seenTx.clear();
164
+ this._seenNotes.clear();
165
+ this._lastTxTime = "";
166
+ this._lastNoteTime = "";
167
+ this.transactions.length = 0;
168
+ this.notifications.length = 0;
169
+ this.devices.clear();
170
+ return this.connect();
171
+ }
172
+ _pairingQuery(extra = {}) {
173
+ const params = new URLSearchParams({ pairing: this._pairingCode, ...extra });
174
+ return `?${params.toString()}`;
175
+ }
176
+ _fetchOpts() {
177
+ return {
178
+ cache: "no-store",
179
+ headers: { "X-Pairing": this._pairingCode }
180
+ };
181
+ }
182
+ disconnect() {
183
+ this._stopPollers();
184
+ if (this._socket) {
185
+ this._socket.removeAllListeners();
186
+ this._socket.disconnect();
187
+ this._socket = null;
188
+ }
189
+ this._setStatus("disconnected");
190
+ }
191
+ _connectSocket() {
192
+ const io = typeof globalThis !== "undefined" ? globalThis.io : null;
193
+ if (!io) {
194
+ this._setStatus("polling", { reason: "socket.io not loaded \u2014 using HTTP poll only" });
195
+ return;
196
+ }
197
+ if (this._socket) {
198
+ this._socket.removeAllListeners();
199
+ this._socket.disconnect();
200
+ }
201
+ this._socket = io(this._baseUrl, {
202
+ query: {
203
+ pairing: this._pairingCode,
204
+ role: this.config.role,
205
+ deviceName: this.config.deviceName
206
+ },
207
+ transports: ["websocket", "polling"],
208
+ reconnection: true,
209
+ reconnectionAttempts: Infinity,
210
+ reconnectionDelay: 1e3,
211
+ reconnectionDelayMax: 5e3,
212
+ timeout: 2e4
213
+ });
214
+ this._socket.on("connect", () => {
215
+ this._setStatus("connected");
216
+ this._pollTransactions();
217
+ this._pollNotifications();
218
+ this._refreshDashboard();
219
+ });
220
+ this._socket.on("disconnect", () => this._setStatus("reconnecting"));
221
+ this._socket.on("connect_error", () => this._setStatus("reconnecting"));
222
+ this._socket.io.on("reconnect", () => {
223
+ this._pollTransactions();
224
+ this._pollNotifications();
225
+ this._refreshDashboard();
226
+ });
227
+ this._socket.on("sync_ready", (data) => {
228
+ this._emit(EVENTS.READY, data);
229
+ this._pollTransactions();
230
+ this._pollNotifications();
231
+ this._refreshDashboard();
232
+ });
233
+ this._socket.on("presence", (data) => {
234
+ this.presence = data;
235
+ if (Array.isArray(data.devices)) this._mergeDevices(data.devices);
236
+ this._emit(EVENTS.PRESENCE, data);
237
+ this._emit(EVENTS.DEVICES, this.getDevices());
238
+ });
239
+ this._socket.on("device_joined", (d) => {
240
+ this._upsertDevice(d);
241
+ this._emit(EVENTS.DEVICES, this.getDevices());
242
+ });
243
+ this._socket.on("device_updated", (d) => {
244
+ this._upsertDevice(d);
245
+ this._emit(EVENTS.DEVICES, this.getDevices());
246
+ });
247
+ this._socket.on("device_left", ({ id }) => {
248
+ this.devices.delete(id);
249
+ this._emit(EVENTS.DEVICES, this.getDevices());
250
+ });
251
+ this._socket.on("history", (list) => this._mergeTxList(list));
252
+ this._socket.on("notification_history", (list) => this._mergeNoteList(list));
253
+ this._socket.on("income_transaction", (tx) => {
254
+ if (this._addTransaction(tx)) this._emit(EVENTS.TRANSACTION, tx);
255
+ });
256
+ this._socket.on("notification_event", (note) => {
257
+ if (this._addNotification(note)) this._emit(EVENTS.NOTIFICATION, note);
258
+ });
259
+ this._socket.on("dashboard_update", (payload) => {
260
+ this.dashboard = payload;
261
+ this._emit(EVENTS.DASHBOARD, payload);
262
+ });
263
+ }
264
+ _startPollers() {
265
+ this._stopPollers();
266
+ const ms = this.config.pollIntervalMs;
267
+ this._pollTransactions();
268
+ this._pollNotifications();
269
+ this._refreshDashboard();
270
+ this._pollTimers.push(setInterval(() => this._pollTransactions(), ms));
271
+ this._pollTimers.push(setInterval(() => this._pollNotifications(), ms));
272
+ this._pollTimers.push(setInterval(() => this._refreshDashboard(), ms * 2));
273
+ }
274
+ _stopPollers() {
275
+ for (const t of this._pollTimers) clearInterval(t);
276
+ this._pollTimers = [];
277
+ }
278
+ async _pollTransactions() {
279
+ try {
280
+ const extra = this._lastTxTime ? { after: this._lastTxTime } : {};
281
+ const data = await fetch(
282
+ `${this._baseUrl}/api/transactions${this._pairingQuery(extra)}`,
283
+ this._fetchOpts()
284
+ ).then((r) => r.json());
285
+ for (const tx of data.transactions || []) {
286
+ if (this._addTransaction(tx)) this._emit(EVENTS.TRANSACTION, tx);
287
+ }
288
+ } catch (_) {
289
+ }
290
+ }
291
+ async _pollNotifications() {
292
+ try {
293
+ const extra = this._lastNoteTime ? { after: this._lastNoteTime } : {};
294
+ const data = await fetch(
295
+ `${this._baseUrl}/api/notifications${this._pairingQuery(extra)}`,
296
+ this._fetchOpts()
297
+ ).then((r) => r.json());
298
+ for (const note of data.notifications || []) {
299
+ if (this._addNotification(note)) this._emit(EVENTS.NOTIFICATION, note);
300
+ }
301
+ } catch (_) {
302
+ }
303
+ }
304
+ async _refreshDashboard() {
305
+ try {
306
+ const data = await fetch(
307
+ `${this._baseUrl}/api/dashboard${this._pairingQuery()}`,
308
+ this._fetchOpts()
309
+ ).then((r) => r.json());
310
+ this.dashboard = data;
311
+ if (data.stats) {
312
+ this.presence = {
313
+ phones: data.stats.phones,
314
+ desktops: data.stats.desktops
315
+ };
316
+ }
317
+ this._emit(EVENTS.DASHBOARD, data);
318
+ } catch (_) {
319
+ }
320
+ try {
321
+ const data = await fetch(
322
+ `${this._baseUrl}/api/devices${this._pairingQuery()}`,
323
+ this._fetchOpts()
324
+ ).then((r) => r.json());
325
+ if (Array.isArray(data.devices)) {
326
+ this.devices.clear();
327
+ this._mergeDevices(data.devices);
328
+ this._emit(EVENTS.DEVICES, this.getDevices());
329
+ }
330
+ } catch (_) {
331
+ }
332
+ }
333
+ _addTransaction(tx) {
334
+ const key = txKey(tx);
335
+ if (this._seenTx.has(key)) return false;
336
+ this._seenTx.add(key);
337
+ const t = String(tx.receivedAt || tx.timestamp || "");
338
+ if (t && t > this._lastTxTime) this._lastTxTime = t;
339
+ this.transactions.unshift(tx);
340
+ while (this.transactions.length > this.config.maxItems) {
341
+ const removed = this.transactions.pop();
342
+ this._seenTx.delete(txKey(removed));
343
+ }
344
+ return true;
345
+ }
346
+ _mergeTxList(list) {
347
+ if (!Array.isArray(list)) return;
348
+ for (let i = list.length - 1; i >= 0; i -= 1) {
349
+ if (this._addTransaction(list[i])) this._emit(EVENTS.TRANSACTION, list[i]);
350
+ }
351
+ }
352
+ _addNotification(note) {
353
+ const key = noteKey(note);
354
+ if (this._seenNotes.has(key)) return false;
355
+ this._seenNotes.add(key);
356
+ const t = String(note.receivedAt || note.timestamp || "");
357
+ if (t && t > this._lastNoteTime) this._lastNoteTime = t;
358
+ this.notifications.unshift(note);
359
+ while (this.notifications.length > this.config.maxItems) {
360
+ const removed = this.notifications.pop();
361
+ this._seenNotes.delete(noteKey(removed));
362
+ }
363
+ return true;
364
+ }
365
+ _mergeNoteList(list) {
366
+ if (!Array.isArray(list)) return;
367
+ for (let i = list.length - 1; i >= 0; i -= 1) {
368
+ if (this._addNotification(list[i])) this._emit(EVENTS.NOTIFICATION, list[i]);
369
+ }
370
+ }
371
+ _upsertDevice(d) {
372
+ if (!d?.id) return;
373
+ this.devices.set(d.id, { ...this.devices.get(d.id), ...d, online: true });
374
+ }
375
+ _mergeDevices(list) {
376
+ for (const d of list) this._upsertDevice(d);
377
+ }
378
+ getDevices() {
379
+ return Array.from(this.devices.values());
380
+ }
381
+ get isConnected() {
382
+ return this._status === "connected";
383
+ }
384
+ get pairingCode() {
385
+ return this._pairingCode;
386
+ }
387
+ get serverUrl() {
388
+ return this._baseUrl;
389
+ }
390
+ };
391
+ function connect(options) {
392
+ return new ZyroConnection(options);
393
+ }
394
+ return __toCommonJS(browser_entry_exports);
395
+ })();