mobs-ws 0.16.1 → 0.18.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/README.md +56 -9
- package/package.json +1 -1
- package/src/index.js +341 -9
package/README.md
CHANGED
|
@@ -2,13 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
Official JavaScript client for the MOBS `/v1/ws/metar` WebSocket (protocol 4).
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
`
|
|
11
|
-
|
|
5
|
+
Requires Node 22+ (or a browser). `mobs-ws` 0.17.0+ for region and
|
|
6
|
+
platform subscriptions, 0.18.0 for D-ATIS and feed notices (server v0.44.0+). There are no runtime dependencies. The client
|
|
7
|
+
uses the global `WebSocket` in the browser and in Node 22+. Snapshot
|
|
8
|
+
and resume are opt-in. The client applies `awc_confirmed` frames
|
|
9
|
+
last-wins per (`icao`, `versions`), handles `station_late`,
|
|
10
|
+
`station_recovered`, and `usage` (use `requestUsage()`), and ignores
|
|
11
|
+
unknown frame types. Late alerts are kept as the latest frame per
|
|
12
|
+
`outage_id` and cleared on `station_recovered`. More than one
|
|
13
|
+
`station_late` can arrive in the same outage. Snapshot frames are
|
|
14
|
+
marked `cached`; they are merged into `latest` and do not advance
|
|
15
|
+
`seq`.
|
|
16
|
+
|
|
17
|
+
A change-only `filter`, parsed `delta`, a US `state`, a `bbox`, and a
|
|
18
|
+
`platform` (`polymarket_com` or `polymarket_us`) are opt-in on a
|
|
19
|
+
mid-session `subscribe`, not on the handshake query. A region or
|
|
20
|
+
platform grant counts as one subscription; a socket may hold at most
|
|
21
|
+
10 grants. `invalid_region`, `unknown_platform`, and `too_many_grants`
|
|
22
|
+
are stored on `lastError`. On reconnect, the client passes `since_seq`
|
|
23
|
+
and `boot_id` as before, and re-subscribes held grants. The first
|
|
24
|
+
state, first bounding box, and first platform can be replayed; any
|
|
25
|
+
extra grants of the same kind are restored by snapshot, not replay.
|
|
12
26
|
|
|
13
27
|
The API key is never embedded. Pass it to the constructor, or set
|
|
14
28
|
`MOBS_TOKEN` (Node). In the browser, pass the token in and the client
|
|
@@ -16,7 +30,7 @@ uses `?token=` because a page cannot set socket headers. A browser
|
|
|
16
30
|
`WebSocket` cannot see a handshake 401 or 403, so the client retries.
|
|
17
31
|
|
|
18
32
|
```bash
|
|
19
|
-
npm install mobs-ws
|
|
33
|
+
npm install mobs-ws@0.18.0
|
|
20
34
|
export MOBS_TOKEN=$MOBS_TOKEN
|
|
21
35
|
```
|
|
22
36
|
|
|
@@ -28,10 +42,43 @@ const client = new MetarClient({
|
|
|
28
42
|
token: process.env.MOBS_TOKEN, // or "YOUR_API_KEY"
|
|
29
43
|
snapshot: true,
|
|
30
44
|
resume: true,
|
|
45
|
+
platform: "polymarket_com",
|
|
31
46
|
onRecord: (rec) => console.log(rec.icao, rec.raw_metar, rec.awc_confirmed),
|
|
32
47
|
});
|
|
33
48
|
|
|
49
|
+
// Mid-session: client.subscribe([], {state: "NY"})
|
|
50
|
+
// or client.subscribe([], {bbox: {south:40.0,west:-75.0,north:41.5,east:-73.0}})
|
|
34
51
|
await client.runForever();
|
|
35
52
|
```
|
|
36
53
|
|
|
37
|
-
|
|
54
|
+
## D-ATIS and feed notices (0.18.0, server v0.44.0+)
|
|
55
|
+
|
|
56
|
+
Pass `datis: ["KLGA"]` (handshake) or call `client.subscribeDatis(["KJFK"], {types: ["combined"]})`
|
|
57
|
+
mid-session; `client.unsubscribeDatis([...])` drops them without touching METAR. The key needs
|
|
58
|
+
the `datis` scope. Each `new_datis_arrived` record (`DatisRecord` JSDoc type) is kept in
|
|
59
|
+
`datisLatest["ICAO/type"]`: a later `issued_at` wins, then a higher `revision`. `onDatis(record)`
|
|
60
|
+
fires when it changes. Snapshot frames (`cached: true`) do not advance `seq`. `backlog: true` marks a
|
|
61
|
+
newer copy that arrived late. `datisTypes` (`combined`, `arr`, `dep`) are sent with a subscribe
|
|
62
|
+
once the socket opens. On a resume fallback with `datis_fallback`, the client refetches
|
|
63
|
+
`GET /v1/datis/latest`.
|
|
64
|
+
|
|
65
|
+
A server without D-ATIS answers with no `datis_icaos`. The client then sets `datisSupported = false`
|
|
66
|
+
and `lastError` to `{code: "unsupported", message: "server does not support D-ATIS"}`, and
|
|
67
|
+
`subscribeDatis()` throws `Error("server does not support D-ATIS")`. METAR keeps working.
|
|
68
|
+
A key without the scope gets `forbidden`, and a server with the product off gets `unavailable`;
|
|
69
|
+
both land on `lastError`.
|
|
70
|
+
|
|
71
|
+
`feed_notice` frames replace `client.notices` (whole state, `FeedNotice` JSDoc type) and call
|
|
72
|
+
`onFeedNotice(notices)`. Opt out with `notices: false`, or later with `client.setNotices(false)`.
|
|
73
|
+
|
|
74
|
+
D-ATIS from mobs-ws is not for operational or flight-safety use; it is best effort with no guarantee.
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
const client = new MetarClient({
|
|
78
|
+
airports: ["KLGA"], datis: ["KLGA"], datisTypes: ["combined"],
|
|
79
|
+
onDatis: (rec) => console.log(rec.icao, rec.type, rec.letter, rec.text.slice(0, 60)),
|
|
80
|
+
onFeedNotice: (notices) => console.log(notices.map((n) => n.message)),
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
MIT licensed. See [LICENSE](https://github.com/Mobs-ws/mobs-sdks/blob/main/LICENSE).
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,8 +3,28 @@
|
|
|
3
3
|
// No runtime dependencies: uses the global WebSocket (browser and Node 22+).
|
|
4
4
|
// The token comes only from the caller or the MOBS_TOKEN environment
|
|
5
5
|
// variable. It is never embedded.
|
|
6
|
+
// 0.18.0 (server v0.44.0+): per-station D-ATIS (`datis` option or
|
|
7
|
+
// subscribeDatis(); the key needs the `datis` scope), new_datis_arrived kept
|
|
8
|
+
// per (icao, type) newest issued_at then revision, onDatis, datis_fallback
|
|
9
|
+
// resync from GET /v1/datis/latest, and feed_notice (`notices`,
|
|
10
|
+
// onFeedNotice; opt out with `notices: false`).
|
|
6
11
|
|
|
7
12
|
export const DEFAULT_URL = "wss://mobs.ws/v1/ws/metar";
|
|
13
|
+
export const MAX_GRANTS = 10;
|
|
14
|
+
export const KNOWN_PLATFORMS = Object.freeze(["polymarket_com", "polymarket_us"]);
|
|
15
|
+
export const ERROR_CODES = Object.freeze([
|
|
16
|
+
"invalid_airports",
|
|
17
|
+
"bad_request",
|
|
18
|
+
"unknown_action",
|
|
19
|
+
"too_many_airports",
|
|
20
|
+
"invalid_region",
|
|
21
|
+
"unknown_platform",
|
|
22
|
+
"too_many_grants",
|
|
23
|
+
"forbidden",
|
|
24
|
+
"unavailable",
|
|
25
|
+
]);
|
|
26
|
+
export const DATIS_TYPES = Object.freeze(["combined", "arr", "dep"]);
|
|
27
|
+
export const DATIS_UNSUPPORTED = "server does not support D-ATIS";
|
|
8
28
|
export const PING_EVERY_MS = 20000;
|
|
9
29
|
export const PONG_TIMEOUT_MS = 15000;
|
|
10
30
|
export const BACKOFF_START_MS = 1000;
|
|
@@ -64,9 +84,62 @@ export function applyAwcConfirmed(latest, message) {
|
|
|
64
84
|
return latest;
|
|
65
85
|
}
|
|
66
86
|
|
|
67
|
-
|
|
87
|
+
/**
|
|
88
|
+
* One D-ATIS bulletin (`data[]` of new_datis_arrived, and GET /v1/datis rows).
|
|
89
|
+
* @typedef {Object} DatisRecord
|
|
90
|
+
* @property {string} icao
|
|
91
|
+
* @property {"combined"|"arr"|"dep"} type
|
|
92
|
+
* @property {string} letter
|
|
93
|
+
* @property {string} issued_at ISO 8601, milliseconds, Z
|
|
94
|
+
* @property {string|null} datis_time HHMM as broadcast
|
|
95
|
+
* @property {string} text
|
|
96
|
+
* @property {string} arrived_at
|
|
97
|
+
* @property {"swim"|"atis.info"} source
|
|
98
|
+
* @property {string|null} source_time
|
|
99
|
+
* @property {number} revision
|
|
100
|
+
* @property {string} versions e.g. "KLGA/combined/U/2026-11-19T11:51Z/0"
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One feed notice (`feed_notice.notices[]` and /v1/status `notices[]`).
|
|
105
|
+
* @typedef {Object} FeedNotice
|
|
106
|
+
* @property {string} id
|
|
107
|
+
* @property {"metar"|"datis"} feed
|
|
108
|
+
* @property {string|null} source
|
|
109
|
+
* @property {string[]|null} icaos
|
|
110
|
+
* @property {string} kind
|
|
111
|
+
* @property {string} message
|
|
112
|
+
* @property {string} starts_at
|
|
113
|
+
* @property {string|null} ends_at
|
|
114
|
+
* @property {string} posted_at
|
|
115
|
+
* @property {string} posted_by
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
export function datisKey(record) {
|
|
119
|
+
return String((record && record.icao) || "").toUpperCase() + "/" + String((record && record.type) || "");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Keep the newest bulletin per icao/type: later issued_at, then higher revision. True when it changed. */
|
|
123
|
+
export function mergeDatis(latest, record) {
|
|
124
|
+
if (!record || !record.icao || !record.type) return false;
|
|
125
|
+
const key = datisKey(record);
|
|
126
|
+
const held = latest[key];
|
|
127
|
+
if (held) {
|
|
128
|
+
if (record.versions === held.versions) return false;
|
|
129
|
+
const mine = String(record.issued_at || "");
|
|
130
|
+
const theirs = String(held.issued_at || "");
|
|
131
|
+
if (mine < theirs) return false;
|
|
132
|
+
if (mine === theirs && Number(record.revision || 0) < Number(held.revision || 0)) return false;
|
|
133
|
+
}
|
|
134
|
+
latest[key] = Object.assign({}, record);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function handshakeUrl(base, airports, {snapshot = true, sinceSeq = null, bootId = null, datis = null, notices = true} = {}) {
|
|
68
139
|
const url = new URL(base);
|
|
69
140
|
url.searchParams.set("airports", airports.join(","));
|
|
141
|
+
if (datis && datis.length) url.searchParams.set("datis", datis.join(","));
|
|
142
|
+
if (!notices) url.searchParams.set("notices", "false");
|
|
70
143
|
if (snapshot) url.searchParams.set("snapshot", "true");
|
|
71
144
|
if (sinceSeq != null && bootId) {
|
|
72
145
|
url.searchParams.set("since_seq", String(sinceSeq));
|
|
@@ -75,10 +148,10 @@ export function handshakeUrl(base, airports, {snapshot = true, sinceSeq = null,
|
|
|
75
148
|
return url.toString();
|
|
76
149
|
}
|
|
77
150
|
|
|
78
|
-
export function latestUrl(wsUrl, airports) {
|
|
151
|
+
export function latestUrl(wsUrl, airports, path = "/v1/metar/latest") {
|
|
79
152
|
const url = new URL(wsUrl);
|
|
80
153
|
url.protocol = url.protocol === "wss:" || url.protocol === "https:" ? "https:" : "http:";
|
|
81
|
-
url.pathname =
|
|
154
|
+
url.pathname = path;
|
|
82
155
|
url.search = new URLSearchParams({airports: airports.join(",")}).toString();
|
|
83
156
|
return url.toString();
|
|
84
157
|
}
|
|
@@ -95,6 +168,23 @@ export async function fetchLatest(wsUrl, token, airports) {
|
|
|
95
168
|
}
|
|
96
169
|
}
|
|
97
170
|
|
|
171
|
+
export function datisLatestUrl(wsUrl, icaos) {
|
|
172
|
+
return latestUrl(wsUrl, icaos, "/v1/datis/latest");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function fetchDatisLatest(wsUrl, token, icaos) {
|
|
176
|
+
if (!icaos || !icaos.length) return [];
|
|
177
|
+
try {
|
|
178
|
+
const headers = token ? {Authorization: "Bearer " + token} : {};
|
|
179
|
+
const response = await fetch(datisLatestUrl(wsUrl, icaos), {headers});
|
|
180
|
+
if (!response.ok) return [];
|
|
181
|
+
const body = await response.json();
|
|
182
|
+
return body.data || [];
|
|
183
|
+
} catch {
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
98
188
|
function isBrowser() {
|
|
99
189
|
return typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
100
190
|
}
|
|
@@ -125,8 +215,16 @@ export class MetarClient {
|
|
|
125
215
|
url = DEFAULT_URL,
|
|
126
216
|
snapshot = true,
|
|
127
217
|
resume = true,
|
|
218
|
+
state = null,
|
|
219
|
+
bbox = null,
|
|
220
|
+
platform = null,
|
|
128
221
|
onRecord = null,
|
|
129
222
|
onFrame = null,
|
|
223
|
+
datis = null,
|
|
224
|
+
datisTypes = null,
|
|
225
|
+
onDatis = null,
|
|
226
|
+
notices = true,
|
|
227
|
+
onFeedNotice = null,
|
|
130
228
|
} = {}) {
|
|
131
229
|
this.url = url;
|
|
132
230
|
this.token = resolveToken(token);
|
|
@@ -146,31 +244,84 @@ export class MetarClient {
|
|
|
146
244
|
this.lastLate = null;
|
|
147
245
|
this.lastRecovered = null;
|
|
148
246
|
this.lates = {};
|
|
247
|
+
this.lastError = null;
|
|
248
|
+
this.heldIcaos = this.airports.slice();
|
|
249
|
+
this.states = [];
|
|
250
|
+
this.bboxes = [];
|
|
251
|
+
this.platforms = [];
|
|
252
|
+
this._sessionOpen = false;
|
|
253
|
+
this._grantResumePending = false;
|
|
254
|
+
// 0.18.0: D-ATIS and feed notices.
|
|
255
|
+
this.datis = (datis || []).map((name) => String(name).toUpperCase());
|
|
256
|
+
this.datisTypes = datisTypes && datisTypes.length ? datisTypes.slice() : null;
|
|
257
|
+
this.onDatis = onDatis;
|
|
258
|
+
this.datisLatest = {};
|
|
259
|
+
this.heldDatis = this.datis.slice();
|
|
260
|
+
this.heldDatisTypes = [];
|
|
261
|
+
this.datisSupported = null; // null until the first subscription_updated
|
|
262
|
+
this.noticesEnabled = Boolean(notices);
|
|
263
|
+
this.notices = [];
|
|
264
|
+
this.onFeedNotice = onFeedNotice;
|
|
265
|
+
this._datisTypesPending = false;
|
|
266
|
+
this.fetchLatest = fetchLatest;
|
|
267
|
+
this.fetchDatisLatest = fetchDatisLatest;
|
|
268
|
+
if (state) this.states.push(String(state).toUpperCase());
|
|
269
|
+
if (bbox) this.bboxes.push(Object.assign({}, bbox));
|
|
270
|
+
if (platform) this.platforms.push(String(platform).toLowerCase());
|
|
149
271
|
}
|
|
150
272
|
|
|
151
273
|
handshakeUrl() {
|
|
274
|
+
const useResume = this.resume && !this.hasGrants();
|
|
152
275
|
return handshakeUrl(this.url, this.airports, {
|
|
153
|
-
snapshot: this.snapshot,
|
|
154
|
-
sinceSeq:
|
|
155
|
-
bootId:
|
|
276
|
+
snapshot: this.hasGrants() ? false : this.snapshot,
|
|
277
|
+
sinceSeq: useResume ? this.seq : null,
|
|
278
|
+
bootId: useResume ? this.bootId : null,
|
|
279
|
+
datis: this.datis,
|
|
280
|
+
notices: this.noticesEnabled,
|
|
156
281
|
});
|
|
157
282
|
}
|
|
158
283
|
|
|
284
|
+
hasGrants() {
|
|
285
|
+
return this.states.length > 0 || this.bboxes.length > 0 || this.platforms.length > 0;
|
|
286
|
+
}
|
|
287
|
+
|
|
159
288
|
handleFrame(message) {
|
|
160
289
|
const kind = message && message.type;
|
|
161
290
|
if (kind === "subscription_updated") {
|
|
162
291
|
if (message.boot_id) this.bootId = message.boot_id;
|
|
292
|
+
if (Array.isArray(message.icaos)) {
|
|
293
|
+
this.heldIcaos = message.icaos.map((item) => String(item).toUpperCase());
|
|
294
|
+
}
|
|
295
|
+
this._noteDatis(message);
|
|
296
|
+
if (this._sessionOpen) this._rememberGrantsFrom(message);
|
|
297
|
+
else if (this.hasGrants()) this._grantResumePending = true;
|
|
298
|
+
this._sessionOpen = true;
|
|
163
299
|
// Do not take seq from subscription_updated: it is the server's
|
|
164
300
|
// current seq, and would skip replay frames not yet received.
|
|
301
|
+
} else if (kind === "error") {
|
|
302
|
+
this.lastError = message;
|
|
303
|
+
// A server that knows D-ATIS but refused it (no datis scope, or the product is off).
|
|
304
|
+
if (message.code === "forbidden" || message.code === "unavailable") this.datisSupported = null;
|
|
165
305
|
} else if (kind === "resume") {
|
|
166
|
-
|
|
306
|
+
const metar = this.fetchLatest(this.url, this.token, this.heldIcaos.length ? this.heldIcaos : this.airports);
|
|
307
|
+
const datis = message.datis_fallback && this.heldDatis.length
|
|
308
|
+
? this.fetchDatisLatest(this.url, this.token, this.heldDatis)
|
|
309
|
+
: Promise.resolve([]);
|
|
310
|
+
return Promise.all([metar, datis]).then(([rows, datisRows]) => {
|
|
167
311
|
for (const rec of rows) {
|
|
168
312
|
const before = this.latest[rec.icao];
|
|
169
313
|
mergeRecord(this.latest, rec, false);
|
|
170
314
|
this._emit(rec.icao, before);
|
|
171
315
|
}
|
|
316
|
+
this._applyDatis(datisRows);
|
|
172
317
|
if (this.onFrame) this.onFrame(message);
|
|
173
318
|
});
|
|
319
|
+
} else if (kind === "new_datis_arrived") {
|
|
320
|
+
this._applyDatis(message.data || []);
|
|
321
|
+
if (message.cached !== true && message.seq != null) this._advanceSeq(message.seq);
|
|
322
|
+
} else if (kind === "feed_notice") {
|
|
323
|
+
this.notices = Array.isArray(message.notices) ? message.notices.slice() : [];
|
|
324
|
+
if (this.onFeedNotice) this.onFeedNotice(this.notices);
|
|
174
325
|
} else if (kind === "new_metar_arrived") {
|
|
175
326
|
const live = message.cached === false && message.replay === false;
|
|
176
327
|
const replayed = message.replay === true;
|
|
@@ -203,6 +354,75 @@ export class MetarClient {
|
|
|
203
354
|
if (this.onFrame) this.onFrame(message);
|
|
204
355
|
}
|
|
205
356
|
|
|
357
|
+
_noteDatis(message) {
|
|
358
|
+
const first = !this._sessionOpen;
|
|
359
|
+
if (Array.isArray(message.datis_icaos)) {
|
|
360
|
+
this.datisSupported = true;
|
|
361
|
+
this.heldDatis = message.datis_icaos.map((item) => String(item).toUpperCase());
|
|
362
|
+
if (Array.isArray(message.datis_types)) this.heldDatisTypes = message.datis_types.map(String);
|
|
363
|
+
// datis_types are subscribe-only: send the wanted types once per socket.
|
|
364
|
+
if (first && this.datisTypes) {
|
|
365
|
+
const want = [...this.datisTypes].sort().join(",");
|
|
366
|
+
const have = [...this.heldDatisTypes].sort().join(",");
|
|
367
|
+
if (want !== have) this._datisTypesPending = true;
|
|
368
|
+
}
|
|
369
|
+
} else if (this.datisSupported === true) {
|
|
370
|
+
this.heldDatis = [];
|
|
371
|
+
this.heldDatisTypes = [];
|
|
372
|
+
} else if (this.datis.length && first) {
|
|
373
|
+
// Asked for D-ATIS and the first answer has no datis_icaos: the server predates v0.44.0.
|
|
374
|
+
// A v0.44.0 server that refuses it follows with forbidden or unavailable, which resets this.
|
|
375
|
+
this.datisSupported = false;
|
|
376
|
+
this.lastError = {type: "error", code: "unsupported", message: DATIS_UNSUPPORTED};
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
_applyDatis(rows) {
|
|
381
|
+
for (const rec of rows || []) {
|
|
382
|
+
if (mergeDatis(this.datisLatest, rec) && this.onDatis) this.onDatis(this.datisLatest[datisKey(rec)]);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
_requireDatis() {
|
|
387
|
+
if (this.datisSupported === false) throw new Error(DATIS_UNSUPPORTED);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Add D-ATIS stations mid-session. Throws "server does not support D-ATIS" on an old server. */
|
|
391
|
+
subscribeDatis(icaos, {types = null, snapshot = null} = {}) {
|
|
392
|
+
this._requireDatis();
|
|
393
|
+
const codes = icaos.map((item) => String(item).toUpperCase());
|
|
394
|
+
for (const code of codes) if (!this.datis.includes(code)) this.datis.push(code);
|
|
395
|
+
const fields = {datis: codes};
|
|
396
|
+
if (types != null) {
|
|
397
|
+
fields.datis_types = types.slice();
|
|
398
|
+
this.datisTypes = types.slice();
|
|
399
|
+
}
|
|
400
|
+
if (snapshot != null) fields.snapshot = snapshot;
|
|
401
|
+
const ws = this._ws;
|
|
402
|
+
if (!ws || ws.readyState !== 1) return;
|
|
403
|
+
ws.send(JSON.stringify(this.subscribeBody(null, fields)));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** Drop D-ATIS stations mid-session; METAR stations are untouched. */
|
|
407
|
+
unsubscribeDatis(icaos) {
|
|
408
|
+
this._requireDatis();
|
|
409
|
+
const codes = [...new Set(icaos.map((item) => String(item).toUpperCase()))].sort();
|
|
410
|
+
this.datis = this.datis.filter((code) => !codes.includes(code));
|
|
411
|
+
for (const key of Object.keys(this.datisLatest)) {
|
|
412
|
+
if (codes.includes(key.split("/")[0])) delete this.datisLatest[key];
|
|
413
|
+
}
|
|
414
|
+
const ws = this._ws;
|
|
415
|
+
if (!ws || ws.readyState !== 1) return;
|
|
416
|
+
ws.send(JSON.stringify({action: "unsubscribe", datis: codes}));
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Turn feed_notice frames on or off for this socket (and later reconnects). */
|
|
420
|
+
setNotices(enabled) {
|
|
421
|
+
this.noticesEnabled = Boolean(enabled);
|
|
422
|
+
const ws = this._ws;
|
|
423
|
+
if (ws && ws.readyState === 1) ws.send(JSON.stringify({action: "subscribe", notices: this.noticesEnabled}));
|
|
424
|
+
}
|
|
425
|
+
|
|
206
426
|
_advanceSeq(seq) {
|
|
207
427
|
if (this.seq == null || seq > this.seq) this.seq = seq;
|
|
208
428
|
}
|
|
@@ -222,10 +442,117 @@ export class MetarClient {
|
|
|
222
442
|
}
|
|
223
443
|
}
|
|
224
444
|
|
|
445
|
+
subscribeBody(icaos, fields = {}) {
|
|
446
|
+
const body = {action: "subscribe"};
|
|
447
|
+
if (icaos != null) body.icaos = icaos.map((item) => String(item).toUpperCase());
|
|
448
|
+
if (fields.state != null) body.state = String(fields.state).toUpperCase();
|
|
449
|
+
if (fields.bbox != null) body.bbox = Object.assign({}, fields.bbox);
|
|
450
|
+
if (fields.platform != null) body.platform = String(fields.platform).toLowerCase();
|
|
451
|
+
if (fields.since_seq != null && fields.boot_id) {
|
|
452
|
+
body.since_seq = fields.since_seq;
|
|
453
|
+
body.boot_id = fields.boot_id;
|
|
454
|
+
}
|
|
455
|
+
if (fields.snapshot != null) body.snapshot = Boolean(fields.snapshot);
|
|
456
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
457
|
+
if (!["state", "bbox", "platform", "since_seq", "boot_id", "snapshot"].includes(key)) {
|
|
458
|
+
body[key] = value;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return body;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
reconnectSubscribeBodies() {
|
|
465
|
+
const states = this.states.slice();
|
|
466
|
+
const bboxes = this.bboxes.slice();
|
|
467
|
+
const platforms = this.platforms.slice();
|
|
468
|
+
const first = this.subscribeBody(this.airports.length ? this.airports : null, {
|
|
469
|
+
state: states.length ? states.shift() : null,
|
|
470
|
+
bbox: bboxes.length ? bboxes.shift() : null,
|
|
471
|
+
platform: platforms.length ? platforms.shift() : null,
|
|
472
|
+
since_seq: this.resume ? this.seq : null,
|
|
473
|
+
boot_id: this.resume ? this.bootId : null,
|
|
474
|
+
snapshot: this.snapshot ? true : null,
|
|
475
|
+
});
|
|
476
|
+
const bodies = [first];
|
|
477
|
+
while (states.length || bboxes.length || platforms.length) {
|
|
478
|
+
bodies.push(this.subscribeBody(null, {
|
|
479
|
+
state: states.length ? states.shift() : null,
|
|
480
|
+
bbox: bboxes.length ? bboxes.shift() : null,
|
|
481
|
+
platform: platforms.length ? platforms.shift() : null,
|
|
482
|
+
snapshot: true,
|
|
483
|
+
}));
|
|
484
|
+
}
|
|
485
|
+
return bodies;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
_rememberGrantsFrom(message) {
|
|
489
|
+
const regions = message.regions || [];
|
|
490
|
+
const platforms = message.platforms || [];
|
|
491
|
+
const states = [];
|
|
492
|
+
const bboxes = [];
|
|
493
|
+
for (const item of regions) {
|
|
494
|
+
const raw = String(item);
|
|
495
|
+
if (raw.toLowerCase().startsWith("state:")) states.push(raw.split(":").slice(1).join(":").toUpperCase());
|
|
496
|
+
else if (raw.toLowerCase().startsWith("bbox:")) {
|
|
497
|
+
const parts = raw.split(":").slice(1).join(":").split(",");
|
|
498
|
+
if (parts.length === 4) {
|
|
499
|
+
const nums = parts.map(Number);
|
|
500
|
+
if (nums.every((n) => Number.isFinite(n))) {
|
|
501
|
+
bboxes.push({south: nums[0], west: nums[1], north: nums[2], east: nums[3]});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
this.states = states;
|
|
507
|
+
this.bboxes = bboxes;
|
|
508
|
+
this.platforms = platforms.map((name) => String(name).toLowerCase());
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
_rememberOutgoingGrants(body) {
|
|
512
|
+
if (body.state && !this.states.includes(body.state)) this.states.push(body.state);
|
|
513
|
+
if (body.bbox) this.bboxes.push(Object.assign({}, body.bbox));
|
|
514
|
+
if (body.platform && !this.platforms.includes(body.platform)) this.platforms.push(body.platform);
|
|
515
|
+
if (Array.isArray(body.icaos)) {
|
|
516
|
+
for (const item of body.icaos) {
|
|
517
|
+
const code = String(item).toUpperCase();
|
|
518
|
+
if (!this.airports.includes(code)) this.airports.push(code);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
225
523
|
subscribe(icaos = [], fields = {}) {
|
|
226
524
|
const ws = this._ws;
|
|
227
525
|
if (!ws || ws.readyState !== 1) return;
|
|
228
|
-
|
|
526
|
+
const body = this.subscribeBody(icaos, fields);
|
|
527
|
+
this._rememberOutgoingGrants(body);
|
|
528
|
+
ws.send(JSON.stringify(body));
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
unsubscribe(icaos = null, fields = {}) {
|
|
532
|
+
const ws = this._ws;
|
|
533
|
+
if (!ws || ws.readyState !== 1) return;
|
|
534
|
+
const body = {action: "unsubscribe"};
|
|
535
|
+
if (icaos != null) body.icaos = icaos.map((item) => String(item).toUpperCase());
|
|
536
|
+
if (fields.state != null) body.state = String(fields.state).toUpperCase();
|
|
537
|
+
if (fields.bbox != null) body.bbox = Object.assign({}, fields.bbox);
|
|
538
|
+
if (fields.platform != null) body.platform = String(fields.platform).toLowerCase();
|
|
539
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
540
|
+
if (!["state", "bbox", "platform"].includes(key)) body[key] = value;
|
|
541
|
+
}
|
|
542
|
+
ws.send(JSON.stringify(body));
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
_flushGrantResume() {
|
|
546
|
+
const ws = this._ws;
|
|
547
|
+
if (this._datisTypesPending && ws && ws.readyState === 1) {
|
|
548
|
+
this._datisTypesPending = false;
|
|
549
|
+
ws.send(JSON.stringify({action: "subscribe", datis: this.datis.slice(), datis_types: (this.datisTypes || []).slice()}));
|
|
550
|
+
}
|
|
551
|
+
if (!this._grantResumePending || !ws || ws.readyState !== 1) return;
|
|
552
|
+
this._grantResumePending = false;
|
|
553
|
+
for (const body of this.reconnectSubscribeBodies()) {
|
|
554
|
+
ws.send(JSON.stringify(body));
|
|
555
|
+
}
|
|
229
556
|
}
|
|
230
557
|
|
|
231
558
|
stop() {
|
|
@@ -268,6 +595,9 @@ export class MetarClient {
|
|
|
268
595
|
return;
|
|
269
596
|
}
|
|
270
597
|
this._ws = ws;
|
|
598
|
+
this._sessionOpen = false;
|
|
599
|
+
this._grantResumePending = false;
|
|
600
|
+
this._datisTypesPending = false;
|
|
271
601
|
|
|
272
602
|
ws.addEventListener("open", () => {
|
|
273
603
|
openedAt = Date.now();
|
|
@@ -296,7 +626,9 @@ export class MetarClient {
|
|
|
296
626
|
clearTimeout(pongTimer);
|
|
297
627
|
pongTimer = null;
|
|
298
628
|
}
|
|
299
|
-
this.handleFrame(message);
|
|
629
|
+
const result = this.handleFrame(message);
|
|
630
|
+
if (result && typeof result.then === "function") result.then(() => this._flushGrantResume());
|
|
631
|
+
else this._flushGrantResume();
|
|
300
632
|
});
|
|
301
633
|
|
|
302
634
|
ws.addEventListener("close", (event) => {
|