UNPKG

914 BJavaScriptView Raw
1"use strict";
2
3let ports, services;
4
5// port -> service
6module.exports.getService = (port, protocol = "tcp") => {
7 if (typeof port !== "number") throw new Error("expected a 'number'");
8 if (!ports) ports = require("./ports.json");
9 return ports[`${port}/${protocol}`] || null;
10};
11
12// service -> port
13module.exports.getPort = (service, protocol = "tcp") => {
14 if (typeof service !== "string") throw new Error("expected a 'string'");
15 if (!services) services = require("./services.json");
16
17 // services are always lowercase
18 const entry = services[service.toLowerCase()];
19 if (!entry) return null;
20
21 // filter non-matching protocols
22 const port = entry.ports.filter(port => /\w+$/.exec(port)[0] === protocol)[0];
23 if (!port) return null;
24
25 // return the first matching port
26 return {
27 port: Number(/^\d+/.exec(port)[0]),
28 protocol: /\w+$/.exec(port)[0],
29 description: entry.description
30 };
31};