From 69ee2222c4f23cfb9f703369cac7e69bc6b79bef Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Fri, 27 May 2022 12:47:36 +0200 Subject: [PATCH] fix: typecheck Buffer without using Buffer inspired by https://github.com/feross/is-buffer --- index.html | 75 +++++++++++++++++++++++++++++++++++++++----- src/factory.js | 12 +++++-- test/error/client.js | 5 ++- test/error/server.js | 35 +++++++++++++++++---- 4 files changed, 109 insertions(+), 18 deletions(-) diff --git a/index.html b/index.html index c150a54..d215797 100644 --- a/index.html +++ b/index.html @@ -11,8 +11,7 @@ body { zoom: 120%; margin: 1rem; - background-color: #0D0208; - color: #00FF41; + background-color: #f8f9fa; } code { @@ -20,10 +19,18 @@ 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; } @@ -33,8 +40,17 @@ diff --git a/src/factory.js b/src/factory.js index dbee6c2..0a4a107 100644 --- a/src/factory.js +++ b/src/factory.js @@ -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) @@ -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) diff --git a/test/error/client.js b/test/error/client.js index 2ad7f9e..8975a2a 100644 --- a/test/error/client.js +++ b/test/error/client.js @@ -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') diff --git a/test/error/server.js b/test/error/server.js index 6978e4c..7f75ffa 100644 --- a/test/error/server.js +++ b/test/error/server.js @@ -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) @@ -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) @@ -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', { @@ -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) + }) })