-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathpackages.js
More file actions
140 lines (129 loc) · 3.45 KB
/
Copy pathpackages.js
File metadata and controls
140 lines (129 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
const fs = require('node:fs')
const Module = require('node:module')
const path = require('node:path')
const vm = require('node:vm')
const { isValidPackageName } = require('@socketsecurity/registry/lib/packages')
const {
isRelative,
normalizePath,
} = require('@socketsecurity/registry/lib/path')
const { findUpSync } = require('./fs')
const { createRequire, isBuiltin } = Module
// eslint-disable-next-line no-control-regex
const cjsPluginPrefixRegExp = /^\x00/
const cjsPluginSuffixRegExp =
/\?commonjs-(?:entry|es-import|exports|external|module|proxy|wrapped)$/
function getPackageName(string, start = 0) {
const end = getPackageNameEnd(string, start)
const name = string.slice(start, end)
return isValidPackageName(name) ? name : ''
}
function getPackageNameEnd(string, start = 0) {
if (isRelative(string)) {
return 0
}
const firstSlashIndex = string.indexOf('/', start)
if (firstSlashIndex === -1) {
return string.length
}
if (string.charCodeAt(start) !== 64 /*'@'*/) {
return firstSlashIndex
}
const secondSlashIndex = string.indexOf('/', firstSlashIndex + 1)
return secondSlashIndex === -1 ? string.length : secondSlashIndex
}
function resolveId(id_, req = require) {
const id = normalizeId(id_)
let resolvedId
if (typeof req === 'string') {
try {
req = createRequire(req)
} catch {}
}
if (req !== require) {
try {
resolvedId = normalizePath(req.resolve(id))
} catch {}
}
if (resolvedId === undefined) {
try {
resolvedId = normalizePath(require.resolve(id))
} catch {}
}
if (resolvedId === undefined) {
resolvedId = id
}
if (isValidPackageName(id)) {
return resolvedId
}
const mtsId = `${resolvedId}.mts`
return fs.existsSync(mtsId) ? mtsId : resolvedId
}
function isEsmId(id_, parentId_) {
if (isBuiltin(id_)) {
return false
}
const parentId = parentId_ ? resolveId(parentId_) : undefined
const resolvedId = resolveId(id_, parentId)
if (resolvedId.endsWith('.mjs') || resolvedId.endsWith('.mts')) {
return true
}
if (
resolvedId.endsWith('.cjs') ||
resolvedId.endsWith('.json') ||
resolvedId.endsWith('.ts')
) {
return false
}
let filepath
if (path.isAbsolute(resolvedId)) {
filepath = resolvedId
} else if (parentId && isRelative(resolvedId)) {
filepath = path.join(path.dirname(parentId), resolvedId)
}
if (!filepath) {
return false
}
const pkgJsonPath = findUpSync('package.json', {
cwd: path.dirname(resolvedId),
})
if (pkgJsonPath) {
const pkgJson = require(pkgJsonPath)
const { exports: entryExports } = pkgJson
if (
pkgJson.type === 'module' &&
!entryExports?.require &&
!entryExports?.node?.require &&
!entryExports?.node?.default?.endsWith?.('.cjs') &&
!entryExports?.['.']?.require &&
!entryExports?.['.']?.node?.require &&
!entryExports?.['.']?.node?.default?.endsWith?.('.cjs') &&
!entryExports?.['.']?.node?.default?.default?.endsWith?.('.cjs')
) {
return true
}
}
try {
// eslint-disable-next-line no-new
new vm.Script(fs.readFileSync(resolvedId, 'utf8'))
} catch (e) {
if (e instanceof SyntaxError) {
return true
}
}
return false
}
function normalizeId(id) {
return normalizePath(id)
.replace(cjsPluginPrefixRegExp, '')
.replace(cjsPluginSuffixRegExp, '')
}
module.exports = {
isBuiltin,
isEsmId,
getPackageName,
getPackageNameEnd,
normalizeId,
resolveId,
}