Debian Codegraph @

node/is-core-module/index.js

raw
1 'use strict';
2
3 var hasOwn = require('hasown');
4
5 /** @type {(current: string, specifier: string) => boolean} */
6 function specifierIncluded(current, specifier) {
7 var nodeParts = current.split('.');
8 var parts = specifier.split(' ');
9 var op = parts.length > 1 ? parts[0] : '=';
10 var versionParts = (parts.length > 1 ? parts[1] : parts[0]).split('.');
11
12 for (var i = 0; i < 3; ++i) {
13 var cur = parseInt(nodeParts[i] || '0', 10);
14 var ver = parseInt(versionParts[i] || '0', 10);
15 if (cur === ver) {
16 continue; // eslint-disable-line no-restricted-syntax, no-continue
17 }
18 if (op === '<') {
19 return cur < ver;
20 }
21 if (op === '>=') {
22 return cur >= ver;
23 }
24 return false;
25 }
26 return op === '>=';
27 }
28
29 /** @type {(current: string, range: string) => boolean} */
30 function matchesRange(current, range) {
31 var specifiers = range.split(/ ?&& ?/);
32 if (specifiers.length === 0) {
33 return false;
34 }
35 for (var i = 0; i < specifiers.length; ++i) {
36 if (!specifierIncluded(current, specifiers[i])) {
37 return false;
38 }
39 }
40 return true;
41 }
42
43 /** @type {(nodeVersion: string | undefined, specifierValue: Data[Module]) => boolean} */
44 function versionIncluded(nodeVersion, specifierValue) {
45 if (typeof specifierValue === 'boolean') {
46 return specifierValue;
47 }
48
49 var current = typeof nodeVersion === 'undefined'
50 ? process.versions && process.versions.node
51 : nodeVersion;
52
53 if (typeof current !== 'string') {
54 throw new TypeError(typeof nodeVersion === 'undefined' ? 'Unable to determine current node version' : 'If provided, a valid node version is required');
55 }
56
57 if (specifierValue && typeof specifierValue === 'object') {
58 for (var i = 0; i < specifierValue.length; ++i) {
59 if (matchesRange(current, specifierValue[i])) {
60 return true;
61 }
62 }
63 return false;
64 }
65 return matchesRange(current, specifierValue);
66 }
67
68 /** @import { Data, Module } from '.' */
69
70 /** @type {Data} */
71 var data = require('./core.json');
72
73 /** @type {typeof import('.')} */
74 module.exports = function isCore(x, nodeVersion) {
75 return hasOwn(data, x) && versionIncluded(nodeVersion, data[/** @type {Module} */ (x)]);
76 };