-
-
Notifications
You must be signed in to change notification settings - Fork 756
Expand file tree
/
Copy pathfile.js
More file actions
181 lines (153 loc) · 4.67 KB
/
file.js
File metadata and controls
181 lines (153 loc) · 4.67 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
'use strict'
const { readFile } = require('node:fs/promises')
const { fileURLToPath } = require('node:url')
function createAbortController () {
let aborted = false
let reason = null
return {
resume () {},
pause () {},
get paused () {
return false
},
get aborted () {
return aborted
},
get reason () {
return reason
},
abort (err) {
if (aborted) {
return
}
aborted = true
reason = err ?? new Error('Request aborted')
}
}
}
function toFileURL (opts) {
if (opts == null || typeof opts !== 'object') {
return null
}
if (opts.origin != null) {
try {
const origin = opts.origin instanceof URL ? opts.origin : new URL(String(opts.origin))
if (origin.protocol === 'file:') {
return new URL(opts.path ?? '', origin)
}
} catch {
// Ignore invalid origin and try path.
}
}
if (typeof opts.path === 'string' && opts.path.startsWith('file:')) {
try {
return new URL(opts.path)
} catch {
return null
}
}
return null
}
function toRawHeaders (headers) {
const rawHeaders = []
for (const [name, value] of Object.entries(headers)) {
rawHeaders.push(Buffer.from(name), Buffer.from(String(value)))
}
return rawHeaders
}
/**
* @param {import('../../types/interceptors').FileInterceptorOpts} [opts]
*/
function createFileInterceptor (opts = {}) {
const {
allow = () => false,
contentType,
read = readFile,
resolvePath = fileURLToPath
} = opts
if (typeof allow !== 'function') {
throw new TypeError('file interceptor: opts.allow must be a function')
}
if (contentType != null && typeof contentType !== 'function') {
throw new TypeError('file interceptor: opts.contentType must be a function')
}
if (typeof read !== 'function') {
throw new TypeError('file interceptor: opts.read must be a function')
}
if (typeof resolvePath !== 'function') {
throw new TypeError('file interceptor: opts.resolvePath must be a function')
}
return dispatch => {
return function fileInterceptorDispatch (dispatchOpts, handler) {
const fileURL = toFileURL(dispatchOpts)
if (!fileURL) {
return dispatch(dispatchOpts, handler)
}
const controller = createAbortController()
try {
handler.onConnect?.((err) => controller.abort(err))
handler.onRequestStart?.(controller, null)
} catch (err) {
handler.onResponseError?.(controller, err)
handler.onError?.(err)
return true
}
if (controller.aborted) {
return true
}
;(async () => {
try {
const method = String(dispatchOpts.method || 'GET').toUpperCase()
if (method !== 'GET' && method !== 'HEAD') {
throw new TypeError(`Method ${method} is not supported for file URLs.`)
}
const path = resolvePath(fileURL)
const allowed = await allow({ path, url: fileURL, method, opts: dispatchOpts })
if (!allowed) {
throw new Error(`Access to ${fileURL.href} is not allowed by file interceptor.`)
}
const fileContent = await read(path)
const chunk = Buffer.isBuffer(fileContent) ? fileContent : Buffer.from(fileContent)
const headers = {
'content-length': String(chunk.length)
}
if (contentType) {
const value = await contentType({ path, url: fileURL, method, opts: dispatchOpts })
if (typeof value === 'string' && value.length > 0) {
headers['content-type'] = value
}
}
if (typeof handler.onResponseStart === 'function') {
handler.onResponseStart(controller, 200, headers, 'OK')
} else {
if (typeof handler.onHeaders === 'function') {
handler.onHeaders(200, toRawHeaders(headers), () => {}, 'OK')
}
}
if (!controller.aborted && method !== 'HEAD') {
if (typeof handler.onResponseData === 'function') {
handler.onResponseData(controller, chunk)
} else {
handler.onData?.(chunk)
}
}
if (!controller.aborted) {
if (typeof handler.onResponseEnd === 'function') {
handler.onResponseEnd(controller, {})
} else {
handler.onComplete?.([])
}
}
} catch (err) {
if (typeof handler.onResponseError === 'function') {
handler.onResponseError(controller, err)
} else {
handler.onError?.(err)
}
}
})()
return true
}
}
}
module.exports = createFileInterceptor