-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.ts
More file actions
286 lines (251 loc) · 7.58 KB
/
Copy pathindex.ts
File metadata and controls
286 lines (251 loc) · 7.58 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { existsSync, readFileSync, mkdirSync, writeFileSync } from 'fs'
import path from 'path'
import http from 'http'
import https from 'https'
import fs from 'fs/promises'
import sourceMap from 'source-map'
import { SourceItem, SourceResult } from '../types'
const NOT_FOUND = null
function createSourceMapConsumer(sourceMapCode: string) {
const consumer = new sourceMap.SourceMapConsumer(sourceMapCode)
return consumer
}
export const isHTTPSource = (sourcePath: string) =>
sourcePath.startsWith('http')
export async function getSourceMapFilePath(sourceJsPath: string) {
if (!isHTTPSource(sourceJsPath)) {
return getLocalSourceMapFilePath(sourceJsPath)
}
return getRemoteSourceMapFilePath(sourceJsPath)
}
export function getLocalSourceMapFilePath(sourceJsPath: string) {
// 文件不存在
if (!existsSync(sourceJsPath)) {
return NOT_FOUND
}
// 先直接判断是否存在.js.map文件存在
if (existsSync(`${sourceJsPath}.map`)) {
return `${sourceJsPath}.map`
}
// 获取代码里的 // #sourceMappingURL= 注释的内容
const jsCode = readFileSync(sourceJsPath, 'utf-8')
const flag = '//# sourceMappingURL='
const flagIdx = jsCode.lastIndexOf(flag)
if (flagIdx === -1) {
return NOT_FOUND
}
const sourceMappingURL = jsCode.slice(flagIdx + flag.length)
// 如果是http路径表明 是绝对路径 直接返回
if (isHTTPSource(sourceMappingURL)) {
return sourceMappingURL
}
// 否则拼接js资源的目录
return path.resolve(path.dirname(sourceJsPath), sourceMappingURL)
}
export async function getRemoteSourceMapFilePath(sourceJsPath: string) {
const context = await getRemoteSource(sourceJsPath)
if (context.code === 404) {
return NOT_FOUND
}
if ((await getRemoteSource(`${sourceJsPath}.map`)).code === 200) {
return `${sourceJsPath}.map`
}
const jsCode = context.body
const flag = '//# sourceMappingURL='
const flagIdx = jsCode.lastIndexOf(flag)
if (flagIdx === -1) {
return NOT_FOUND
}
const sourceMappingURL = jsCode.slice(flagIdx + flag.length)
if (isHTTPSource(sourceMappingURL)) {
return sourceMappingURL
}
return path.resolve(path.dirname(sourceJsPath), sourceMappingURL)
}
export function getRemoteSource(
url: string
): Promise<{ body: string; code?: number }> {
return new Promise((resolve, reject) => {
const httpURL = new URL(url)
// 区别https与http资源
const HTTP = url.startsWith('https://') ? https : http
// 通过回调的形式获取
HTTP.get(
!process.argv.includes('--no-strict-ssl')
? url
: {
hostname: httpURL.hostname,
path: httpURL.pathname,
port: 443,
method: 'GET',
rejectUnauthorized: false
},
(res) => {
// 设置可读流的字符编码
res.setEncoding('utf-8')
// 响应内容拼接
let content = ''
res.on('data', (chunk) => {
content += chunk
})
// 读完对外暴露内容和状态码
res.on('end', () => {
resolve({
body: content,
code: res.statusCode
})
})
res.on('error', (err) => {
reject(err)
})
}
)
})
}
/**
* 根据报错资源信息,获取对应源码信息
* @param url 报错资源
* @param line 行号
* @param column 列号
*/
export async function getErrorSourceResult(
url: string,
line: number,
column: number
): Promise<SourceResult> {
const unknown: SourceResult = {
sourceCode: '',
source: '',
line: 0,
column: 0
}
const sourceMapURL = await getSourceMapFilePath(url)
if (!sourceMapURL) {
return unknown
}
return getErrorSourceResultBySourceMapUrl(sourceMapURL, line, column)
}
export async function getErrorSourceResultBySourceMapUrl(
sourceMapURL: string,
line: number,
column: number
): Promise<SourceResult> {
// sourceMap 内容
const sourceMapCode = await getSourceCode(sourceMapURL)
return getErrorSourceResultBySourceMapCode(sourceMapCode, line, column)
}
export async function getSourceCode(url: string) {
const code = await (isHTTPSource(url)
? getRemoteSource(url).then((v) => v.body)
: fs.readFile(url, 'utf-8'))
return code
}
export async function getErrorSourceResultBySourceMapCode(
sourceMapCode: string,
line: number,
column: number
): Promise<SourceResult> {
const consumer = await createSourceMapConsumer(sourceMapCode)
const { name, ...rest } = consumer.originalPositionFor({
line,
column
})
const sourceCode = consumer.sourceContentFor(rest.source!)
return {
...rest,
sourceCode
} as SourceResult
}
export const underlineStr = (v: any) => `\x1B[4m${v}\x1B[24m`
export const yellowStr = (v: any) => `\x1B[33m${v}\x1B[39m`
export const redStr = (v: any) => `\x1B[31m${v}\x1B[39m`
/**
* @param result
* @param showMaxLine 控制显示的行数
*/
export function printResult(result: SourceResult, showMaxLine = 5) {
const { sourceCode, source, line, column } = result
// 源码拆成数租
const lines = sourceCode.split('\n')
// 打印错误路径
console.log(`error in ${source}:${line}:${column}`)
console.log()
// 计算要展示的行的起始位置,起始行号不能小于1
const startLine = Math.max(1, line - Math.floor(showMaxLine / 2))
// 结束位置不能大于总行数
const endLine = Math.min(lines.length, startLine + showMaxLine - 1)
const showCode = lines
// 截取需要展示的内容
.slice(startLine - 1, endLine)
.map(
(v, idx) =>
// 加上黄色行号
`${yellowStr(startLine + idx)} ${
// 针对错误的行进行下划线+红色展示
idx + startLine === line
? // 从错误的列号开始展示
v.slice(0, column - 1) + redStr(underlineStr(v.slice(column - 1)))
: v
}`
)
.join('\n')
console.log(showCode)
}
export function outPutResult(
outputDir: string,
logFilename: string,
result: SourceResult,
showMaxLine = 5
) {
const outputFile = path.resolve(process.cwd(), outputDir, logFilename)
console.log(`error output in file:
${yellowStr(outputFile)}`)
const { sourceCode, source, line, column } = result
const lines = sourceCode.split('\n')
const error = []
error.push(`error in ${source}`, `line:${line} column:${column}`, '')
const startLine = Math.max(1, line - Math.floor(showMaxLine / 2))
const endLine = Math.min(lines.length, startLine + showMaxLine - 1)
const showCode = lines
.slice(startLine - 1, endLine)
.map(
(v, idx) =>
`${idx + startLine === line ? '❌' : ''}${startLine + idx} ${v}`
)
error.push(...showCode)
if (!existsSync(path.dirname(outputFile))) {
mkdirSync(path.dirname(outputFile), { recursive: true })
}
fs.writeFile(outputFile, error.join('\n'))
}
/**
* 通过sourceMap内容获取源文件
*/
export async function getSourcesBySourceMapCode(sourceMapCode: string) {
const consumer = await createSourceMapConsumer(sourceMapCode)
const { sources } = consumer
const result = sources.map((source) => {
return {
source,
code: consumer.sourceContentFor(source)!
}
})
return result
}
export async function outPutSources(
sources: SourceItem[],
outPutDir = 'source-map-result/project'
) {
for (const sourceItem of sources) {
const { source, code } = sourceItem
const filepath = path.resolve(
process.cwd(),
outPutDir,
source.replace(/^(\.\.\/)+/g, '')
)
if (!existsSync(path.dirname(filepath))) {
mkdirSync(path.dirname(filepath), { recursive: true })
}
writeFileSync(filepath, code, 'utf-8')
}
}