@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.
Files changed (61) hide show
  1. package/dist/cjs/src/config.d.ts +0 -6
  2. package/dist/cjs/src/config.d.ts.map +1 -1
  3. package/dist/cjs/src/config.js +1 -6
  4. package/dist/cjs/src/config.js.map +1 -1
  5. package/dist/cjs/src/file-box.d.ts +3 -4
  6. package/dist/cjs/src/file-box.d.ts.map +1 -1
  7. package/dist/cjs/src/file-box.js +9 -3
  8. package/dist/cjs/src/file-box.js.map +1 -1
  9. package/dist/cjs/src/file-box.type.d.ts +0 -3
  10. package/dist/cjs/src/file-box.type.d.ts.map +1 -1
  11. package/dist/cjs/src/misc.d.ts +2 -5
  12. package/dist/cjs/src/misc.d.ts.map +1 -1
  13. package/dist/cjs/src/misc.js +25 -35
  14. package/dist/cjs/src/misc.js.map +1 -1
  15. package/dist/cjs/src/pure-functions/sized-chunk-transformer.d.ts +0 -1
  16. package/dist/cjs/src/pure-functions/sized-chunk-transformer.d.ts.map +1 -1
  17. package/dist/cjs/src/pure-functions/sized-chunk-transformer.js +1 -2
  18. package/dist/cjs/src/pure-functions/sized-chunk-transformer.js.map +1 -1
  19. package/dist/cjs/src/qrcode.d.ts +0 -2
  20. package/dist/cjs/src/qrcode.d.ts.map +1 -1
  21. package/dist/cjs/src/qrcode.js +2 -3
  22. package/dist/cjs/src/qrcode.js.map +1 -1
  23. package/dist/cjs/src/urn-registry/random-uuid.d.ts +1 -1
  24. package/dist/cjs/src/urn-registry/random-uuid.d.ts.map +1 -1
  25. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.d.ts +0 -2
  26. package/dist/cjs/src/urn-registry/uniform-resource-name-registry.d.ts.map +1 -1
  27. package/dist/cjs/src/urn-registry/uuid-to-big-int.js +1 -2
  28. package/dist/cjs/src/urn-registry/uuid-to-big-int.js.map +1 -1
  29. package/dist/cjs/src/version.d.ts.map +1 -1
  30. package/dist/cjs/src/version.js +1 -1
  31. package/dist/cjs/src/version.js.map +1 -1
  32. package/dist/esm/src/config.d.ts +0 -6
  33. package/dist/esm/src/config.d.ts.map +1 -1
  34. package/dist/esm/src/config.js +0 -5
  35. package/dist/esm/src/config.js.map +1 -1
  36. package/dist/esm/src/file-box.d.ts +3 -4
  37. package/dist/esm/src/file-box.d.ts.map +1 -1
  38. package/dist/esm/src/file-box.js +9 -3
  39. package/dist/esm/src/file-box.js.map +1 -1
  40. package/dist/esm/src/file-box.type.d.ts +0 -3
  41. package/dist/esm/src/file-box.type.d.ts.map +1 -1
  42. package/dist/esm/src/misc.d.ts +2 -5
  43. package/dist/esm/src/misc.d.ts.map +1 -1
  44. package/dist/esm/src/misc.js +21 -30
  45. package/dist/esm/src/misc.js.map +1 -1
  46. package/dist/esm/src/pure-functions/sized-chunk-transformer.d.ts +0 -1
  47. package/dist/esm/src/pure-functions/sized-chunk-transformer.d.ts.map +1 -1
  48. package/dist/esm/src/qrcode.d.ts +0 -2
  49. package/dist/esm/src/qrcode.d.ts.map +1 -1
  50. package/dist/esm/src/urn-registry/random-uuid.d.ts +1 -1
  51. package/dist/esm/src/urn-registry/random-uuid.d.ts.map +1 -1
  52. package/dist/esm/src/urn-registry/uniform-resource-name-registry.d.ts +0 -2
  53. package/dist/esm/src/urn-registry/uniform-resource-name-registry.d.ts.map +1 -1
  54. package/dist/esm/src/version.d.ts.map +1 -1
  55. package/dist/esm/src/version.js +1 -1
  56. package/dist/esm/src/version.js.map +1 -1
  57. package/package.json +1 -1
  58. package/src/config.ts +0 -6
  59. package/src/file-box.ts +11 -3
  60. package/src/misc.ts +17 -31
  61. 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).end()
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
- .setTimeout(HTTP_RESPONSE_TIMEOUT, () => {
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 getProxyUrl () {
214
- const proxyType = PROXY_TYPE
215
- const proxyHost = PROXY_HOST
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
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * This file was auto generated from scripts/generate-version.sh
3
3
  */
4
- export const VERSION: string = '1.7.8'
4
+ export const VERSION: string = '1.7.10'