device-eol-check 0.0.0-stage → 0.1.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.
Files changed (6) hide show
  1. package/LICENSE +27 -0
  2. package/README.md +112 -2
  3. package/cli.js +163 -0
  4. package/data.json +17829 -0
  5. package/index.js +270 -0
  6. package/package.json +44 -4
package/index.js ADDED
@@ -0,0 +1,270 @@
1
+ /**
2
+ * device-eol-check — read a device inventory and say which models are past the
3
+ * vendor's end of firmware support, with the vendor's own sentence as evidence.
4
+ *
5
+ * Two halves, usable separately:
6
+ * lookup(model, { today }) — what do our records say about one model?
7
+ * scanText(text, { today }) — which known models does this file name?
8
+ * scanDir(root, { today }) — the same over every inventory-ish file
9
+ *
10
+ * Nothing here computes a date. Every verdict carries `source`, `source_quote`
11
+ * and `page` from data.json, which tools/packagedata.py rebuilds out of
12
+ * referencesource.org's own records. Where a record states no date, the verdict
13
+ * says so in words; it is never turned into a zero, a guess or a "supported".
14
+ */
15
+ 'use strict';
16
+
17
+ const fs = require('fs');
18
+ const path = require('path');
19
+
20
+ const data = require('./data.json');
21
+ const devices = data.devices;
22
+
23
+ /* ------------------------------------------------------------------ keys */
24
+
25
+ /** A model name reduced to what an inventory file can be matched on. */
26
+ function modelKey(model) {
27
+ return String(model == null ? '' : model)
28
+ .toLowerCase()
29
+ .replace(/[^0-9a-z.]+/g, ' ')
30
+ .replace(/\s+/g, ' ')
31
+ .trim();
32
+ }
33
+
34
+ /** The key without a trailing hardware revision: `archer a10 1.0` -> `archer a10`. */
35
+ function baseKey(key) {
36
+ const out = key.replace(/\s(?:v)?\d+(?:\.\d+)+$/, '').trim();
37
+ return out || key;
38
+ }
39
+
40
+ const byKey = new Map();
41
+ const byBase = new Map();
42
+ for (const d of devices) {
43
+ if (!byKey.has(d.key)) byKey.set(d.key, []);
44
+ byKey.get(d.key).push(d);
45
+ if (!byBase.has(d.base)) byBase.set(d.base, []);
46
+ byBase.get(d.base).push(d);
47
+ }
48
+
49
+ /* --------------------------------------------------------------- verdict */
50
+
51
+ /**
52
+ * The status of one device record against a date.
53
+ *
54
+ * Statuses:
55
+ * The key is `verdict`, not `status`: `status` is the vendor's own word for
56
+ * the model ("End-of-Life") and is left exactly as the record states it.
57
+ *
58
+ * support-ended the date the record states has passed
59
+ * supported-until that date is in the future
60
+ * end-of-life-no-date the vendor lists the model as end-of-life and
61
+ * states no date on the page we read
62
+ * date-unreadable the record holds the vendor's characters but this
63
+ * build could not order them (see build_data.py)
64
+ *
65
+ * `failed` is true for support-ended and for end-of-life-no-date: a model the
66
+ * vendor has declared end-of-life is the thing worth failing a build on, dated
67
+ * or not. date-unreadable is NEVER a failure and never a pass — it is reported
68
+ * as unreadable, because a value nobody could read must not arrive looking
69
+ * measured.
70
+ */
71
+ function verdictFor(d, today) {
72
+ if (d.end_of_support) {
73
+ if (d.end_of_support <= today) {
74
+ return { verdict: 'support-ended', failed: true };
75
+ }
76
+ return { verdict: 'supported-until', failed: false };
77
+ }
78
+ if (d.end_of_support_as_printed) {
79
+ return { verdict: 'date-unreadable', failed: false, unreadable: true };
80
+ }
81
+ if (d.status) {
82
+ return { verdict: 'end-of-life-no-date', failed: true };
83
+ }
84
+ return { verdict: 'no-date-and-no-status', failed: false, unreadable: true };
85
+ }
86
+
87
+ function todayISO() {
88
+ return new Date().toISOString().slice(0, 10);
89
+ }
90
+
91
+ /**
92
+ * What our records say about one model string.
93
+ *
94
+ * `match` is `exact` when the inventory named the hardware revision too, and
95
+ * `base` when it named only the model — in which case every revision the
96
+ * records hold is returned, because picking one would be inventing a fact.
97
+ * `null` means the records do not hold this model, which is reported and is
98
+ * never read as "supported".
99
+ */
100
+ function lookup(model, opts) {
101
+ const o = opts || {};
102
+ const today = o.today || todayISO();
103
+ const key = modelKey(model);
104
+ if (!key) return null;
105
+ let hits = byKey.get(key);
106
+ let match = 'exact';
107
+ if (!hits) {
108
+ hits = byBase.get(baseKey(key));
109
+ match = 'base';
110
+ }
111
+ if (!hits || !hits.length) return null;
112
+ const records = hits.map((d) => Object.assign({}, d, verdictFor(d, today)));
113
+ const failed = records.filter((r) => r.failed);
114
+ const unreadable = records.filter((r) => r.unreadable);
115
+ return {
116
+ asked: String(model),
117
+ key,
118
+ match,
119
+ revisions: records.length,
120
+ failed: failed.length > 0,
121
+ // the record a one-line report should print: the worst verdict, so a
122
+ // fleet file naming a model whose one revision is dead is not reported as
123
+ // supported because another revision lives
124
+ worst: failed.length ? failed[0] : (records.find((r) => !r.unreadable) || records[0]),
125
+ unreadable: !failed.length && unreadable.length === records.length,
126
+ records,
127
+ };
128
+ }
129
+
130
+ /* ------------------------------------------------------------------ scan */
131
+
132
+ /** Model keys long enough and distinctive enough to hunt for inside text. */
133
+ function scannable(key) {
134
+ if (key.replace(/\s/g, '').length < 4) return false;
135
+ return /[a-z]/.test(key) && /\d/.test(key);
136
+ }
137
+
138
+ const SCAN_KEYS = [];
139
+ for (const key of byKey.keys()) if (scannable(key)) SCAN_KEYS.push(key);
140
+ for (const base of byBase.keys()) if (scannable(base) && !byKey.has(base)) SCAN_KEYS.push(base);
141
+ const SCAN_SET = new Set(SCAN_KEYS);
142
+ const MAX_TOKENS = Math.max(
143
+ 1,
144
+ ...SCAN_KEYS.map((k) => k.split(' ').length)
145
+ );
146
+
147
+ /**
148
+ * Every known model named anywhere in a piece of text, in the order found.
149
+ *
150
+ * Format-free on purpose: a NetBox CSV export, an Ansible inventory, a
151
+ * Nautobot JSON dump, a spreadsheet saved as text and a hand-typed list all
152
+ * name the model, and none of them agree on where. So the text is tokenised
153
+ * and the longest known model name is matched at each position, never
154
+ * overlapping — which is why `Archer A10 1.0` wins over `Archer A10` when the
155
+ * file records the revision.
156
+ */
157
+ function scanText(text, opts) {
158
+ const o = opts || {};
159
+ const today = o.today || todayISO();
160
+ const s = String(text == null ? '' : text).toLowerCase();
161
+ const tokens = [];
162
+ const re = /[0-9a-z]+(?:\.[0-9]+)*/g;
163
+ let m;
164
+ while ((m = re.exec(s)) !== null) tokens.push({ t: m[0], at: m.index });
165
+
166
+ const found = [];
167
+ const seen = new Set();
168
+ let i = 0;
169
+ while (i < tokens.length) {
170
+ let hitLen = 0;
171
+ let hitKey = '';
172
+ for (let n = Math.min(MAX_TOKENS, tokens.length - i); n >= 1; n--) {
173
+ const key = tokens.slice(i, i + n).map((x) => x.t).join(' ');
174
+ if (SCAN_SET.has(key)) {
175
+ hitLen = n;
176
+ hitKey = key;
177
+ break;
178
+ }
179
+ }
180
+ if (hitLen) {
181
+ if (!seen.has(hitKey)) {
182
+ seen.add(hitKey);
183
+ const r = lookup(hitKey, { today });
184
+ if (r) found.push(Object.assign({ at: tokens[i].at }, r));
185
+ }
186
+ i += hitLen;
187
+ } else {
188
+ i += 1;
189
+ }
190
+ }
191
+ return found;
192
+ }
193
+
194
+ const SKIP_DIRS = new Set(['.git', 'node_modules', '.venv', 'venv', 'dist', 'build', '__pycache__']);
195
+ const TEXT_EXT = new Set([
196
+ '.csv', '.tsv', '.txt', '.json', '.yaml', '.yml', '.ini', '.cfg', '.conf',
197
+ '.md', '.tf', '.toml', '.xml', '.log', '',
198
+ ]);
199
+
200
+ /** scanText over every inventory-shaped text file under a directory. */
201
+ function scanDir(root, opts) {
202
+ const o = opts || {};
203
+ const out = [];
204
+ const walk = (dir, depth) => {
205
+ if (depth > 8) return;
206
+ let names;
207
+ try {
208
+ names = fs.readdirSync(dir, { withFileTypes: true });
209
+ } catch (e) {
210
+ return;
211
+ }
212
+ for (const e of names) {
213
+ const p = path.join(dir, e.name);
214
+ if (e.isDirectory()) {
215
+ if (!SKIP_DIRS.has(e.name)) walk(p, depth + 1);
216
+ continue;
217
+ }
218
+ if (!e.isFile()) continue;
219
+ if (!TEXT_EXT.has(path.extname(e.name).toLowerCase())) continue;
220
+ let st;
221
+ try {
222
+ st = fs.statSync(p);
223
+ } catch (err) {
224
+ continue;
225
+ }
226
+ if (st.size > 8 * 1024 * 1024) continue;
227
+ let text;
228
+ try {
229
+ text = fs.readFileSync(p, 'utf8');
230
+ } catch (err) {
231
+ continue;
232
+ }
233
+ for (const hit of scanText(text, o)) out.push(Object.assign({ file: p }, hit));
234
+ }
235
+ };
236
+ const st = fs.statSync(root);
237
+ if (st.isFile()) {
238
+ for (const hit of scanText(fs.readFileSync(root, 'utf8'), o)) {
239
+ out.push(Object.assign({ file: root }, hit));
240
+ }
241
+ return out;
242
+ }
243
+ walk(root, 0);
244
+ return out;
245
+ }
246
+
247
+ /** What this package can and cannot answer, in its own numbers. */
248
+ function denominator() {
249
+ return {
250
+ built: data.built,
251
+ devices: data.counts.devices,
252
+ with_a_date: data.counts.with_an_end_of_support_date,
253
+ end_of_life_with_no_date_stated: data.counts.end_of_life_with_no_date_stated,
254
+ date_stated_but_unreadable: data.counts.date_stated_but_unreadable,
255
+ vendors: data.vendors,
256
+ assets: data.assets,
257
+ };
258
+ }
259
+
260
+ module.exports = {
261
+ lookup,
262
+ scanText,
263
+ scanDir,
264
+ denominator,
265
+ modelKey,
266
+ baseKey,
267
+ scannable,
268
+ verdictFor,
269
+ data,
270
+ };
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "device-eol-check",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Read a device inventory file and say which router, access point and firewall models are past the vendor's end of firmware support — every date with the vendor's own sentence and the page it was read from",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "device-eol-check": "cli.js"
8
+ },
9
+ "files": [
10
+ "index.js",
11
+ "cli.js",
12
+ "data.json",
13
+ "README.md",
14
+ "LICENSE"
15
+ ],
16
+ "scripts": {
17
+ "test": "node test.js"
18
+ },
19
+ "keywords": [
20
+ "asus",
21
+ "ansible",
22
+ "cisco-meraki",
23
+ "ci",
24
+ "device-inventory",
25
+ "end-of-life",
26
+ "end-of-support",
27
+ "eol",
28
+ "firewall",
29
+ "firmware",
30
+ "inventory",
31
+ "linksys",
32
+ "nautobot",
33
+ "netbox",
34
+ "network-automation",
35
+ "network-device",
36
+ "palo-alto",
37
+ "router",
38
+ "sonicwall",
39
+ "tp-link"
40
+ ],
41
+ "homepage": "https://referencesource.org/router-firmware-support-status/",
42
+ "license": "CC0-1.0",
43
+ "engines": {
44
+ "node": ">=14"
45
+ }
46
+ }