Skip to content

Commit

Permalink
fix: typecheck Buffer without using Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 27, 2022
1 parent 3f3bd25 commit 69ee222
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
75 changes: 67 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@
body {
zoom: 120%;
margin: 1rem;
background-color: #0D0208;
color: #00FF41;
background-color: #f8f9fa;
}

code {
font-size: 16px;
white-space: pre-wrap;
}

samp {
color: #868e96;
}

output {
color: #15141A;
}

div {
margin-top: 12px;
margin-bottom: 12px;
border-bottom: 1px solid #008F11;
border-bottom: 1px solid #dee2e6;
}
</style>
</head>
Expand All @@ -33,22 +40,74 @@
<script>
const log = console.log
const devTools = document.getElementById("devtools")

const writeLog = el => str => {
const node = document.createElement(el)
const line = document.createElement("div")
node.appendChild(document.createTextNode(str))
devTools.appendChild(node).appendChild(line)
log(str)
}

window.console = {
log: function(str) {
log: function (str) {
const node = document.createElement("output")
const line = document.createElement("div")
node.appendChild(document.createTextNode(str))
devTools.appendChild(node).appendChild(line)
log(str)
}
}

window.console = {
log: writeLog('output'),
debug: writeLog('samp')
}

</script>
<script src="./dist/mql.js"></script>
<script>
console.log(`mql.version: ${mql.version}`)
mql('https://example.com').then(({ response, ...payload }) => {
console.log(JSON.stringify(payload, null, 2))
})
function getBrowser() {
var ua = navigator.userAgent,
tem, M = ua.match(
/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return {
name: 'IE',
version: (tem[1] || '')
};
}
if (M[1] === 'Chrome') {
tem = ua.match(/\bOPR|Edge\/(\d+)/)
if (tem != null) {
return {
name: 'Opera',
version: tem[1]
};
}
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null) {
M.splice(1, 1, tem[1]);
}

return `${M[0]} v${M[1]}`
}

const query = Object.fromEntries(new URL(document.location).searchParams)

console.log(`MQL v${mql.version} at ${getBrowser()}`)

const stderr = (payload) => console.debug(JSON.stringify(payload, null, 2))

const { url = 'https://example.com' } = query

mql(url).then(({
response,
...payload
}) => stderr(payload)).catch(stderr)

</script>
</body>

Expand Down
12 changes: 9 additions & 3 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const ENDPOINT = {

const isObject = input => input !== null && typeof input === 'object'

const isBuffer = input =>
input != null &&
input.constructor != null &&
typeof input.constructor.isBuffer === 'function' &&
input.constructor.isBuffer(input)

const parseBody = (input, error, url) => {
try {
return JSON.parse(input)
Expand Down Expand Up @@ -62,12 +68,12 @@ const factory = ({
} catch (err) {
const { response = {} } = err
const { statusCode, body: rawBody, headers, url: uri = apiUrl } = response
const isBuffer = Buffer.isBuffer(rawBody)
const isBodyBuffer = isBuffer(rawBody)

const body =
isObject(rawBody) && !isBuffer
isObject(rawBody) && !isBodyBuffer
? rawBody
: parseBody(isBuffer ? rawBody.toString() : rawBody, err, uri)
: parseBody(isBodyBuffer ? rawBody.toString() : rawBody, err, uri)

if (body.code === 'EFATALCLIENT' && retryCount++ < 2) {
return fetchFromApi(apiUrl, opts, retryCount)
Expand Down
5 changes: 4 additions & 1 deletion test/error/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ clients.forEach(({ constructor: mql, target }) => {
})

test(`${target} » EFATALCLIENT`, async t => {
const error = await t.throwsAsync(mql('https://example.com', { endpoint: 'https://notexist.dev' }), { instanceOf: mql.MicrolinkError })
const error = await t.throwsAsync(
mql('https://example.com', { endpoint: 'https://notexist.dev' }),
{ instanceOf: mql.MicrolinkError }
)

t.true(error.url === 'https://notexist.dev?url=https%3A%2F%2Fexample.com')
t.true(error.code === 'EFATALCLIENT')
Expand Down
35 changes: 29 additions & 6 deletions test/error/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ clients.forEach(({ constructor: mql, target }) => {

const endpoint = await listen(server)

const error = await t.throwsAsync(mql('https://microlink.io', { endpoint, retry: 0 }), {
instanceOf: mql.MicrolinkError
})
const error = await t.throwsAsync(
mql('https://microlink.io', { endpoint, retry: 0 }),
{
instanceOf: mql.MicrolinkError
}
)

t.true(!!error.url)
t.true(!!error.code)
Expand All @@ -28,10 +31,14 @@ clients.forEach(({ constructor: mql, target }) => {
t.true(!!error.message)
t.true(!!error.description)
})

test(`${target} » server side generic error`, async t => {
const error = await t.throwsAsync(mql('https://microlink.io', { ttl: 100, retry: 0 }), {
instanceOf: mql.MicrolinkError
})
const error = await t.throwsAsync(
mql('https://microlink.io', { ttl: 100, retry: 0 }),
{
instanceOf: mql.MicrolinkError
}
)

t.true(!!error.url)
t.true(!!error.code)
Expand All @@ -42,6 +49,7 @@ clients.forEach(({ constructor: mql, target }) => {
t.true(!!error.message)
t.true(!!error.description)
})

test(`${target} » server side timeout`, async t => {
const error = await t.throwsAsync(
mql('https://kikobeats.com', {
Expand All @@ -62,4 +70,19 @@ clients.forEach(({ constructor: mql, target }) => {
t.true(!!error.message)
t.true(!!error.description)
})

test(`${target} » EINVALURL`, async t => {
const error = await t.throwsAsync(mql('https://invalid-link', {}), {
instanceOf: mql.MicrolinkError
})

t.true(!!error.url)
t.true(!!error.code)
t.true(!!error.status)
t.true(!!error.more)
t.true(!!error.statusCode)
t.true(!!error.data)
t.true(!!error.message)
t.true(!!error.description)
})
})

0 comments on commit 69ee222

Please sign in to comment.