Skip to content

Commit b90157b

Browse files
authored
Merge pull request #352 from microlinkhq/next
refactor: remove redundant code
2 parents 2f6e361 + 6df9663 commit b90157b

File tree

4 files changed

+96
-42
lines changed

4 files changed

+96
-42
lines changed

src/avatar/resolve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const debug = require('debug-logfmt')('unavatar:resolve')
44
const reachableUrl = require('../util/reachable-url')
55
const isAbsoluteUrl = require('is-absolute-url')
6-
const dataUriRegex = require('data-uri-regex')
76
const memoizeOne = require('async-memoize-one')
7+
const dataUriRegex = require('data-uri-regex')
88
const { getTtl } = require('../send/cache')
99
const isUrlHttp = require('is-url-http')
1010
const pTimeout = require('p-timeout')

src/send/index.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,21 @@
22

33
const { dataUriToBuffer } = require('data-uri-to-buffer')
44
const { pickBy } = require('lodash')
5+
const send = require('send-http')
56

67
const got = require('../util/got')
78

89
const { ALLOWED_REQ_HEADERS } = require('../constant')
910

1011
const pickHeaders = headers =>
11-
pickBy(headers, (value, key) => ALLOWED_REQ_HEADERS.includes(key))
12+
pickBy(headers, (_, key) => ALLOWED_REQ_HEADERS.includes(key))
1213

13-
const sendJson = (res, data) => {
14-
const str = JSON.stringify(data)
15-
res.setHeader('Content-Type', 'application/json; charset=utf-8')
16-
res.setHeader('Content-Length', Buffer.byteLength(str))
17-
return res.end(str)
18-
}
19-
20-
const sendAvatar = ({ req, res, type, data }) => {
21-
if (!data) return res.end()
22-
return type === 'buffer'
23-
? res.end(dataUriToBuffer(data))
24-
: got.stream(data, { headers: pickHeaders(req.headers) }).pipe(res)
25-
}
26-
27-
const send = ({ type, data, req, res }) => {
14+
module.exports = ({ type, data, req, res }) => {
2815
const { query } = req
29-
res.statusCode = data ? 200 : 404
16+
const statusCode = data ? 200 : 404
3017
return query.json
31-
? sendJson(res, { url: data })
32-
: sendAvatar({ req, res, type, data })
18+
? send(res, statusCode, { url: data })
19+
: type === 'buffer'
20+
? send(res, statusCode, dataUriToBuffer(data))
21+
: got.stream(data, { headers: pickHeaders(req.headers) }).pipe(res)
3322
}
34-
35-
module.exports = send

test/providers.js renamed to test/endpoints.js

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,145 +7,176 @@ const { runServer } = require('./helpers')
77

88
const isCI = !!process.env.CI
99

10+
test('ping', async t => {
11+
const serverUrl = await runServer(t)
12+
const { body, statusCode } = await got('ping', {
13+
prefixUrl: serverUrl,
14+
responseType: 'text'
15+
})
16+
t.is(statusCode, 200)
17+
t.is(body, 'pong')
18+
})
19+
1020
test('youtube', async t => {
1121
const serverUrl = await runServer(t)
12-
const { body } = await got('youtube/natelive7?json', {
22+
const { body, statusCode } = await got('youtube/natelive7?json', {
1323
prefixUrl: serverUrl
1424
})
25+
t.is(statusCode, 200)
1526
t.true(body.url.includes('images.weserv.nl'))
1627
})
1728

1829
test('gitlab', async t => {
1930
const serverUrl = await runServer(t)
20-
const { body } = await got('gitlab/kikobeats?json', {
31+
const { body, statusCode } = await got('gitlab/kikobeats?json', {
2132
prefixUrl: serverUrl
2233
})
34+
t.is(statusCode, 200)
2335
t.true(body.url.includes('images.weserv.nl'))
2436
})
2537

2638
test('github', async t => {
2739
const serverUrl = await runServer(t)
28-
const { body } = await got('github/kikobeats?json', {
40+
const { body, statusCode } = await got('github/kikobeats?json', {
2941
prefixUrl: serverUrl
3042
})
43+
t.is(statusCode, 200)
3144
t.true(body.url.includes('images.weserv.nl'))
3245
})
3346

3447
test('twitter', async t => {
3548
const serverUrl = await runServer(t)
36-
const { body } = await got('twitter/kikobeats?json', {
49+
const { body, statusCode } = await got('twitter/kikobeats?json', {
3750
prefixUrl: serverUrl
3851
})
52+
t.is(statusCode, 200)
3953
t.true(body.url.includes('images.weserv.nl'))
4054
})
4155

4256
test('soundcloud', async t => {
4357
const serverUrl = await runServer(t)
44-
const { body } = await got('soundcloud/kikobeats?json', {
58+
const { body, statusCode } = await got('soundcloud/kikobeats?json', {
4559
prefixUrl: serverUrl
4660
})
61+
t.is(statusCode, 200)
4762
t.true(body.url.includes('images.weserv.nl'))
4863
})
4964
//
5065
test('deviantart', async t => {
5166
const serverUrl = await runServer(t)
52-
const { body } = await got('deviantart/spyed?json', {
67+
const { body, statusCode } = await got('deviantart/spyed?json', {
5368
prefixUrl: serverUrl
5469
})
70+
t.is(statusCode, 200)
5571
t.true(body.url.includes('images.weserv.nl'))
5672
})
5773

5874
test('dribbble', async t => {
5975
const serverUrl = await runServer(t)
60-
const { body } = await got('dribbble/omidnikrah?json', {
76+
const { body, statusCode } = await got('dribbble/omidnikrah?json', {
6177
prefixUrl: serverUrl
6278
})
79+
t.is(statusCode, 200)
6380
t.true(body.url.includes('images.weserv.nl'))
6481
})
6582

6683
test('duckduckgo', async t => {
6784
const serverUrl = await runServer(t)
68-
const { body } = await got('duckduckgo/google.com?json', {
85+
const { body, statusCode } = await got('duckduckgo/google.com?json', {
6986
prefixUrl: serverUrl
7087
})
88+
t.is(statusCode, 200)
7189
t.true(body.url.includes('images.weserv.nl'))
7290
})
7391

7492
test('google', async t => {
7593
const serverUrl = await runServer(t)
76-
const { body } = await got('google/teslahunt.io?json', {
94+
const { body, statusCode } = await got('google/teslahunt.io?json', {
7795
prefixUrl: serverUrl
7896
})
97+
t.is(statusCode, 200)
7998
t.true(body.url.includes('images.weserv.nl'))
8099
})
81100

82101
test('gravatar', async t => {
83102
const serverUrl = await runServer(t)
84-
const { body } = await got('gravatar/sindresorhus@gmail.com?json', {
85-
prefixUrl: serverUrl
86-
})
103+
const { body, statusCode } = await got(
104+
'gravatar/sindresorhus@gmail.com?json',
105+
{
106+
prefixUrl: serverUrl
107+
}
108+
)
109+
t.is(statusCode, 200)
87110
t.true(body.url.includes('images.weserv.nl'))
88111
})
89112

90113
test('telegram', async t => {
91114
const serverUrl = await runServer(t)
92-
const { body } = await got('telegram/drsdavidsoft?json', {
115+
const { body, statusCode } = await got('telegram/drsdavidsoft?json', {
93116
prefixUrl: serverUrl
94117
})
118+
t.is(statusCode, 200)
95119
t.true(body.url.includes('images.weserv.nl'))
96120
})
97121
;(isCI ? test.skip : test)('substack', async t => {
98122
const serverUrl = await runServer(t)
99-
const { body } = await got('substack/bankless?json', {
123+
const { body, statusCode } = await got('substack/bankless?json', {
100124
prefixUrl: serverUrl
101125
})
126+
t.is(statusCode, 200)
102127
t.true(body.url.includes('images.weserv.nl'))
103128
})
104129
//
105130
;(isCI ? test.skip : test)('reddit', async t => {
106131
const serverUrl = await runServer(t)
107-
const { body } = await got('reddit/kikobeats?json', {
132+
const { body, statusCode } = await got('reddit/kikobeats?json', {
108133
prefixUrl: serverUrl
109134
})
135+
t.is(statusCode, 200)
110136
t.true(body.url.includes('images.weserv.nl'))
111137
})
112138
//
113139
;(isCI ? test.skip : test)('instagram', async t => {
114140
const serverUrl = await runServer(t)
115-
const { body } = await got('instagram/willsmith?json', {
141+
const { body, statusCode } = await got('instagram/willsmith?json', {
116142
prefixUrl: serverUrl
117143
})
144+
t.is(statusCode, 200)
118145
t.true(body.url.includes('images.weserv.nl'))
119146
})
120147

121148
test('twitch', async t => {
122149
const serverUrl = await runServer(t)
123-
const { body } = await got('twitch/midudev?json', {
150+
const { body, statusCode } = await got('twitch/midudev?json', {
124151
prefixUrl: serverUrl
125152
})
153+
t.is(statusCode, 200)
126154
t.true(body.url.includes('images.weserv.nl'))
127155
})
128156

129157
test('microlink', async t => {
130158
const serverUrl = await runServer(t)
131-
const { body } = await got('microlink/teslahunt.io?json', {
159+
const { body, statusCode } = await got('microlink/teslahunt.io?json', {
132160
prefixUrl: serverUrl
133161
})
162+
t.is(statusCode, 200)
134163
t.true(body.url.includes('images.weserv.nl'))
135164
})
136165

137166
test('readcv', async t => {
138167
const serverUrl = await runServer(t)
139-
const { body } = await got('readcv/elenatorro?json', {
168+
const { body, statusCode } = await got('readcv/elenatorro?json', {
140169
prefixUrl: serverUrl
141170
})
171+
t.is(statusCode, 200)
142172
t.true(body.url.includes('images.weserv.nl'))
143173
})
144174
//
145175
test.skip('tiktok', async t => {
146176
const serverUrl = await runServer(t)
147-
const { body } = await got('tiktok/carlosazaustre?json', {
177+
const { body, statusCode } = await got('tiktok/carlosazaustre?json', {
148178
prefixUrl: serverUrl
149179
})
180+
t.is(statusCode, 200)
150181
t.true(body.url.includes('images.weserv.nl'))
151182
})

test/query-parameters.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { dataUriToBuffer } = require('data-uri-to-buffer')
34
const test = require('ava')
45
const got = require('got')
56
const ms = require('ms')
@@ -36,6 +37,41 @@ test('fallback', async t => {
3637
t.is(headers['content-type'], 'application/json; charset=utf-8')
3738
})
3839

40+
test('fallback # data uri', async t => {
41+
{
42+
const dataURI =
43+
'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
44+
const serverUrl = await runServer(t)
45+
46+
const { headers, body } = await got(
47+
`github/__notexistprofile__?fallback=${encodeURIComponent(dataURI)}&json`,
48+
{
49+
prefixUrl: serverUrl,
50+
responseType: 'json'
51+
}
52+
)
53+
54+
t.is(body.url, dataURI)
55+
t.is(headers['content-type'], 'application/json; charset=utf-8')
56+
}
57+
{
58+
const dataURI =
59+
'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
60+
const serverUrl = await runServer(t)
61+
62+
const { headers, body } = await got(
63+
`github/__notexistprofile__?fallback=${encodeURIComponent(dataURI)}`,
64+
{
65+
prefixUrl: serverUrl,
66+
responseType: 'buffer'
67+
}
68+
)
69+
70+
t.true(body.equals(dataUriToBuffer(dataURI)))
71+
t.is(headers['content-type'], 'application/octet-stream')
72+
}
73+
})
74+
3975
test('fallback # use default value if fallback provided is not reachable', async t => {
4076
const serverUrl = await runServer(t)
4177

0 commit comments

Comments
 (0)