reachable-url 1.6.11 → 1.7.1

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 (2) hide show
  1. package/index.js +32 -8
  2. package/package.json +11 -1
package/index.js CHANGED
@@ -2,7 +2,22 @@
2
2
 
3
3
  const pReflect = require('p-reflect')
4
4
  const { URL } = require('url')
5
- const got = require('got')
5
+
6
+ const got = require('got').extend({
7
+ decompress: false,
8
+ responseType: 'buffer',
9
+ retry: 1,
10
+ headers: {
11
+ Range: 'bytes=0-0'
12
+ },
13
+ hooks: {
14
+ beforeRetry: [
15
+ options => {
16
+ delete options.headers.range
17
+ }
18
+ ]
19
+ }
20
+ })
6
21
 
7
22
  const mergeResponse = (responseOrigin = {}, responseDestination = {}) => ({
8
23
  statusMessage: 'Not Found',
@@ -12,12 +27,8 @@ const mergeResponse = (responseOrigin = {}, responseDestination = {}) => ({
12
27
  ...responseDestination
13
28
  })
14
29
 
15
- const reachableUrl = async (url, opts = {}) => {
16
- const req = got(url, {
17
- ...opts,
18
- retry: 0,
19
- responseType: 'buffer'
20
- })
30
+ const reachableUrl = async (url, opts) => {
31
+ const req = got(url, opts)
21
32
 
22
33
  const redirectStatusCodes = []
23
34
  const redirectUrls = []
@@ -25,7 +36,6 @@ const reachableUrl = async (url, opts = {}) => {
25
36
 
26
37
  req.on('response', res => {
27
38
  response = res
28
- response.once('data', () => req.cancel())
29
39
  })
30
40
 
31
41
  req.on('redirect', res => {
@@ -37,6 +47,20 @@ const reachableUrl = async (url, opts = {}) => {
37
47
 
38
48
  const mergedResponse = mergeResponse(isFulfilled ? value : error.response, response)
39
49
 
50
+ if (mergedResponse.statusCode === 206) {
51
+ const contentRange = mergedResponse.headers['content-range']
52
+ if (typeof contentRange === 'string') {
53
+ let contentLength = contentRange.split('/')
54
+ if (contentLength.length > 1) {
55
+ contentLength = contentLength[contentLength.length - 1]
56
+ mergedResponse.statusCode = 200
57
+ mergedResponse.statusMessage = 'OK'
58
+ mergedResponse.headers['content-length'] = contentLength
59
+ mergedResponse.headers['content-range'] = undefined
60
+ }
61
+ }
62
+ }
63
+
40
64
  return {
41
65
  url,
42
66
  ...mergedResponse,
package/package.json CHANGED
@@ -2,13 +2,23 @@
2
2
  "name": "reachable-url",
3
3
  "description": "Given an url resolve it as fast as possible",
4
4
  "homepage": "https://nicedoc.io/Kikobeats/reachable-url",
5
- "version": "1.6.11",
5
+ "version": "1.7.1",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "josefrancisco.verdu@gmail.com",
9
9
  "name": "Kiko Beats",
10
10
  "url": "https://kikobeats.com"
11
11
  },
12
+ "contributors": [
13
+ {
14
+ "name": "Shu Ding",
15
+ "email": "g@shud.in"
16
+ },
17
+ {
18
+ "name": "Kenrick",
19
+ "email": "kenrick95@gmail.com"
20
+ }
21
+ ],
12
22
  "repository": {
13
23
  "type": "git",
14
24
  "url": "git+https://github.com/Kikobeats/reachable-url.git"