@juzi/file-box 1.7.8 → 1.7.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/config.d.ts +0 -6
- package/dist/cjs/src/config.d.ts.map +1 -1
- package/dist/cjs/src/config.js +1 -6
- package/dist/cjs/src/config.js.map +1 -1
- package/dist/cjs/src/file-box.d.ts +3 -4
- package/dist/cjs/src/file-box.d.ts.map +1 -1
- package/dist/cjs/src/file-box.js +9 -3
- package/dist/cjs/src/file-box.js.map +1 -1
- package/dist/cjs/src/file-box.type.d.ts +0 -3
- package/dist/cjs/src/file-box.type.d.ts.map +1 -1
- package/dist/cjs/src/misc.d.ts +2 -5
- package/dist/cjs/src/misc.d.ts.map +1 -1
- package/dist/cjs/src/misc.js +25 -35
- package/dist/cjs/src/misc.js.map +1 -1
- package/dist/cjs/src/pure-functions/sized-chunk-transformer.d.ts +0 -1
- package/dist/cjs/src/pure-functions/sized-chunk-transformer.d.ts.map +1 -1
- package/dist/cjs/src/pure-functions/sized-chunk-transformer.js +1 -2
- package/dist/cjs/src/pure-functions/sized-chunk-transformer.js.map +1 -1
- package/dist/cjs/src/qrcode.d.ts +0 -2
- package/dist/cjs/src/qrcode.d.ts.map +1 -1
- package/dist/cjs/src/qrcode.js +2 -3
- package/dist/cjs/src/qrcode.js.map +1 -1
- package/dist/cjs/src/urn-registry/random-uuid.d.ts +1 -1
- package/dist/cjs/src/urn-registry/random-uuid.d.ts.map +1 -1
- package/dist/cjs/src/urn-registry/uniform-resource-name-registry.d.ts +0 -2
- package/dist/cjs/src/urn-registry/uniform-resource-name-registry.d.ts.map +1 -1
- package/dist/cjs/src/urn-registry/uuid-to-big-int.js +1 -2
- package/dist/cjs/src/urn-registry/uuid-to-big-int.js.map +1 -1
- package/dist/cjs/src/version.d.ts.map +1 -1
- package/dist/cjs/src/version.js +1 -1
- package/dist/cjs/src/version.js.map +1 -1
- package/dist/esm/src/config.d.ts +0 -6
- package/dist/esm/src/config.d.ts.map +1 -1
- package/dist/esm/src/config.js +0 -5
- package/dist/esm/src/config.js.map +1 -1
- package/dist/esm/src/file-box.d.ts +3 -4
- package/dist/esm/src/file-box.d.ts.map +1 -1
- package/dist/esm/src/file-box.js +9 -3
- package/dist/esm/src/file-box.js.map +1 -1
- package/dist/esm/src/file-box.type.d.ts +0 -3
- package/dist/esm/src/file-box.type.d.ts.map +1 -1
- package/dist/esm/src/misc.d.ts +2 -5
- package/dist/esm/src/misc.d.ts.map +1 -1
- package/dist/esm/src/misc.js +21 -30
- package/dist/esm/src/misc.js.map +1 -1
- package/dist/esm/src/pure-functions/sized-chunk-transformer.d.ts +0 -1
- package/dist/esm/src/pure-functions/sized-chunk-transformer.d.ts.map +1 -1
- package/dist/esm/src/qrcode.d.ts +0 -2
- package/dist/esm/src/qrcode.d.ts.map +1 -1
- package/dist/esm/src/urn-registry/random-uuid.d.ts +1 -1
- package/dist/esm/src/urn-registry/random-uuid.d.ts.map +1 -1
- package/dist/esm/src/urn-registry/uniform-resource-name-registry.d.ts +0 -2
- package/dist/esm/src/urn-registry/uniform-resource-name-registry.d.ts.map +1 -1
- package/dist/esm/src/version.d.ts.map +1 -1
- package/dist/esm/src/version.js +1 -1
- package/dist/esm/src/version.js.map +1 -1
- package/package.json +1 -1
- package/src/config.ts +0 -6
- package/src/file-box.ts +11 -3
- package/src/misc.ts +17 -31
- package/src/version.ts +1 -1
package/src/misc.ts
CHANGED
|
@@ -16,11 +16,6 @@ import {
|
|
|
16
16
|
HTTP_REQUEST_TIMEOUT,
|
|
17
17
|
HTTP_RESPONSE_TIMEOUT,
|
|
18
18
|
NO_SLICE_DOWN,
|
|
19
|
-
PROXY_HOST,
|
|
20
|
-
PROXY_PASSWORD,
|
|
21
|
-
PROXY_PORT,
|
|
22
|
-
PROXY_TYPE,
|
|
23
|
-
PROXY_USERNAME,
|
|
24
19
|
} from './config.js'
|
|
25
20
|
|
|
26
21
|
const protocolMap: {
|
|
@@ -46,7 +41,7 @@ export function dataUrlToBase64 (dataUrl: string): string {
|
|
|
46
41
|
*
|
|
47
42
|
* @credit https://stackoverflow.com/a/43632171/1123955
|
|
48
43
|
*/
|
|
49
|
-
export async function httpHeadHeader (url: string): Promise<http.IncomingHttpHeaders> {
|
|
44
|
+
export async function httpHeadHeader (url: string, proxyUrl?: string): Promise<http.IncomingHttpHeaders> {
|
|
50
45
|
const originUrl = url
|
|
51
46
|
let REDIRECT_TTL = 7
|
|
52
47
|
|
|
@@ -57,7 +52,7 @@ export async function httpHeadHeader (url: string): Promise<http.IncomingHttpHea
|
|
|
57
52
|
|
|
58
53
|
const res = await fetch(url, {
|
|
59
54
|
method: 'HEAD',
|
|
60
|
-
})
|
|
55
|
+
}, proxyUrl)
|
|
61
56
|
res.destroy()
|
|
62
57
|
|
|
63
58
|
if (!/^3/.test(String(res.statusCode))) {
|
|
@@ -94,7 +89,7 @@ export function httpHeaderToFileName (headers: http.IncomingHttpHeaders): null |
|
|
|
94
89
|
return null
|
|
95
90
|
}
|
|
96
91
|
|
|
97
|
-
export async function httpStream (url: string, headers: http.OutgoingHttpHeaders = {}): Promise<Readable> {
|
|
92
|
+
export async function httpStream (url: string, headers: http.OutgoingHttpHeaders = {}, proxyUrl?: string): Promise<Readable> {
|
|
98
93
|
const headHeaders = await httpHeadHeader(url)
|
|
99
94
|
if (headHeaders.location) {
|
|
100
95
|
url = headHeaders.location
|
|
@@ -110,21 +105,21 @@ export async function httpStream (url: string, headers: http.OutgoingHttpHeaders
|
|
|
110
105
|
const fileSize = Number(headHeaders['content-length'])
|
|
111
106
|
|
|
112
107
|
if (!NO_SLICE_DOWN && headHeaders['accept-ranges'] === 'bytes' && fileSize > HTTP_CHUNK_SIZE) {
|
|
113
|
-
return await downloadFileInChunks(url, options, fileSize, HTTP_CHUNK_SIZE)
|
|
108
|
+
return await downloadFileInChunks(url, options, fileSize, HTTP_CHUNK_SIZE, proxyUrl)
|
|
114
109
|
} else {
|
|
115
|
-
return await fetch(url, options)
|
|
110
|
+
return await fetch(url, options, proxyUrl)
|
|
116
111
|
}
|
|
117
112
|
}
|
|
118
113
|
|
|
119
|
-
async function fetch (url: string, options: http.RequestOptions): Promise<http.IncomingMessage> {
|
|
114
|
+
async function fetch (url: string, options: http.RequestOptions, proxyUrl?: string): Promise<http.IncomingMessage> {
|
|
120
115
|
const { protocol } = new URL(url)
|
|
121
116
|
const { request, agent } = getProtocol(protocol)
|
|
122
117
|
const opts: http.RequestOptions = {
|
|
123
118
|
agent,
|
|
124
119
|
...options,
|
|
125
120
|
}
|
|
126
|
-
setProxy(opts)
|
|
127
|
-
const req = request(url, opts)
|
|
121
|
+
setProxy(opts, proxyUrl)
|
|
122
|
+
const req = request(url, opts)
|
|
128
123
|
req
|
|
129
124
|
.on('error', () => {
|
|
130
125
|
req.destroy()
|
|
@@ -132,15 +127,18 @@ async function fetch (url: string, options: http.RequestOptions): Promise<http.I
|
|
|
132
127
|
.setTimeout(HTTP_REQUEST_TIMEOUT, () => {
|
|
133
128
|
req.emit('error', new Error(`FileBox: Http request timeout (${HTTP_REQUEST_TIMEOUT})!`))
|
|
134
129
|
})
|
|
130
|
+
.end()
|
|
135
131
|
const responseEvents = await once(req, 'response')
|
|
136
132
|
const res = responseEvents[0] as http.IncomingMessage
|
|
137
133
|
res
|
|
138
134
|
.on('error', () => {
|
|
139
135
|
res.destroy()
|
|
140
136
|
})
|
|
141
|
-
|
|
137
|
+
if (res.socket) {
|
|
138
|
+
res.setTimeout(HTTP_RESPONSE_TIMEOUT, () => {
|
|
142
139
|
res.emit('error', new Error(`FileBox: Http response timeout (${HTTP_RESPONSE_TIMEOUT})!`))
|
|
143
140
|
})
|
|
141
|
+
}
|
|
144
142
|
return res
|
|
145
143
|
}
|
|
146
144
|
|
|
@@ -149,6 +147,7 @@ async function downloadFileInChunks (
|
|
|
149
147
|
options: http.RequestOptions,
|
|
150
148
|
fileSize: number,
|
|
151
149
|
chunkSize = HTTP_CHUNK_SIZE,
|
|
150
|
+
proxyUrl?: string,
|
|
152
151
|
): Promise<Readable> {
|
|
153
152
|
const tmpFile = join(tmpdir(), `filebox-${randomUUID()}`)
|
|
154
153
|
const writeStream = createWriteStream(tmpFile)
|
|
@@ -171,7 +170,7 @@ async function downloadFileInChunks (
|
|
|
171
170
|
requestOptions.headers['Range'] = range
|
|
172
171
|
|
|
173
172
|
try {
|
|
174
|
-
const res = await fetch(url, requestOptions)
|
|
173
|
+
const res = await fetch(url, requestOptions, proxyUrl)
|
|
175
174
|
assert(allowStatusCode.includes(res.statusCode ?? 0), `Request failed with status code ${res.statusCode}`)
|
|
176
175
|
assert(Number(res.headers['content-length']) > 0, 'Server returned 0 bytes of data')
|
|
177
176
|
for await (const chunk of res) {
|
|
@@ -210,22 +209,9 @@ export async function streamToBuffer (stream: Readable): Promise<Buffer> {
|
|
|
210
209
|
return Buffer.concat(chunks)
|
|
211
210
|
}
|
|
212
211
|
|
|
213
|
-
function
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const proxyPort = PROXY_PORT
|
|
217
|
-
const proxyUsername = PROXY_USERNAME
|
|
218
|
-
const proxyPassword = PROXY_PASSWORD
|
|
219
|
-
if (proxyType === 'http') {
|
|
220
|
-
return `http://${proxyUsername}:${proxyPassword}@${proxyHost}:${proxyPort}`
|
|
221
|
-
}
|
|
222
|
-
return ''
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function setProxy (options: RequestOptions): void {
|
|
226
|
-
const url = getProxyUrl()
|
|
227
|
-
if (url) {
|
|
228
|
-
const agent = new HttpsProxyAgent(url)
|
|
212
|
+
function setProxy (options: RequestOptions, proxyUrl?: string): void {
|
|
213
|
+
if (proxyUrl) {
|
|
214
|
+
const agent = new HttpsProxyAgent(proxyUrl)
|
|
229
215
|
options.agent = agent
|
|
230
216
|
}
|
|
231
217
|
}
|
package/src/version.ts
CHANGED