From 6895df7e9f8239e27c1603c0006f3f443a09e889 Mon Sep 17 00:00:00 2001 From: Eric Chen Date: Sat, 25 Nov 2023 14:48:57 +0800 Subject: [PATCH] refactor: fetch --- lib/ast.mjs | 20 ++++--------- lib/fetch.mjs | 18 ++++++++++++ lib/utils.mjs | 50 ++++++++++----------------------- test/output.mjs | 32 +++++++++------------ test/snapshots/output.mjs.md | 32 ++++++++++----------- test/snapshots/output.mjs.snap | Bin 2579 -> 2577 bytes 6 files changed, 69 insertions(+), 83 deletions(-) create mode 100644 lib/fetch.mjs diff --git a/lib/ast.mjs b/lib/ast.mjs index f520d07..44f106e 100644 --- a/lib/ast.mjs +++ b/lib/ast.mjs @@ -1,15 +1,9 @@ function attrString(attr) { - const buff = []; - - for (const [name, value] of Object.entries(attr)) { - buff.push(`${name}="${value}"`); - } - - if (buff.length === 0) { - return ''; - } - - return ` ${buff.join(' ')}`; + return attr + ? Object.entries(attr) + .map(([name, value]) => `${name}="${value}"`) + .join(' ') + : ''; } // eslint-disable-next-line consistent-return @@ -20,9 +14,7 @@ function stringifier(buff, doc) { } case 'tag': { // eslint-disable-next-line no-param-reassign - buff += `<${doc.name}${doc.attr ? attrString(doc.attr) : ''}${ - doc.void ? ' />' : '>' - }`; + buff += `<${doc.name} ${attrString(doc.attr)}${doc.void ? ' />' : '>'}`; if (doc.void) { return buff; diff --git a/lib/fetch.mjs b/lib/fetch.mjs new file mode 100644 index 0000000..35a1d40 --- /dev/null +++ b/lib/fetch.mjs @@ -0,0 +1,18 @@ +import nodeFetch from 'node-fetch'; + +export function httpPost({ url, body, headers }) { + return nodeFetch(url, { + method: 'POST', + body, + headers: { + ...headers, + 'Content-Type': 'text/plain', + }, + }).then(async (response) => { + if (!response.ok) { + throw new Error(await response.text()); + } + + return response.arrayBuffer(); + }); +} diff --git a/lib/utils.mjs b/lib/utils.mjs index b55c0d1..01b142b 100644 --- a/lib/utils.mjs +++ b/lib/utils.mjs @@ -1,9 +1,10 @@ import { readFileSync } from 'node:fs'; import { parse } from 'markdown-code-block-meta'; -import nodeFetch from 'node-fetch'; import pMemoize from 'p-memoize'; +import { httpPost } from './fetch.mjs'; + export function isKroki(alias = []) { return ({ type, lang, meta, value }) => { return ( @@ -16,52 +17,31 @@ export function isKroki(alias = []) { }; } -const fail = readFileSync(new URL('fail.svg', import.meta.url), 'utf8'); +const failImage = new URL('fail.svg', import.meta.url); -function createFailImageBuffer(message) { - return Buffer.from(fail.replace('======', message.slice(0, 500))); +function createFailImage(message) { + return readFileSync(failImage, 'utf8').replace( + '======', + message.slice(0, 500), + ); } -function Fetch({ server, headers = {}, type, value }) { - return nodeFetch(`${server}/${type}/svg`, { - method: 'POST', +function Fetch({ server, headers, type, value }) { + return httpPost({ + url: `${server}/${type}/svg`, body: value, - headers: { - ...headers, - 'Content-Type': 'text/plain', - }, + headers, }) - .then( - (response) => { - if (!response.ok || response.statusCode >= 400) { - if (response.statusCode === 404) { - return 'Error: 404'; - } - - return response.text(); - } - - return response.arrayBuffer(); - }, - (error) => `Error: ${error.message}`, - ) - .then((data) => - typeof data === 'string' - ? createFailImageBuffer(data) - : Buffer.from(data), - ); + .catch((error) => createFailImage(error.message)) + .then((data) => Buffer.from(data)); } export const mime = 'image/svg+xml'; -function base64Url(base64) { - return `data:${mime};base64,${base64}`; -} - export function toDataURL(buffer) { const base64 = buffer.toString('base64'); - return base64Url(base64); + return `data:${mime};base64,${base64}`; } export const fetchData = pMemoize(Fetch, { diff --git a/test/output.mjs b/test/output.mjs index 52c5a48..111b0df 100644 --- a/test/output.mjs +++ b/test/output.mjs @@ -6,25 +6,21 @@ test.before((t) => { t.timeout(1000 ** 3); }); +const source = ` +\`\`\`kroki type=plantuml + A --> B +\`\`\` +`; + function macro(t, { output }) { - return TransformSnapshot( - t, - ` - \`\`\`kroki type=plantuml - A --> B - \`\`\` - `, - { - server: 'https://kroki.io', - output, - }, - ); + return TransformSnapshot(t, source, { + server: 'https://kroki.io', + output, + }); } -test('inline-svg', macro, { output: 'inline-svg' }); - -test('img-base64', macro, { output: 'img-base64' }); +const mode = ['inline-svg', 'img-base64', 'img-html-base64', 'object-base64']; -test('img-html-base64', macro, { output: 'img-html-base64' }); - -test('object-base64', macro, { output: 'object-base64' }); +for (const output of mode) { + test(output, macro, { output }); +} diff --git a/test/snapshots/output.mjs.md b/test/snapshots/output.mjs.md index c1053d6..c73ef34 100644 --- a/test/snapshots/output.mjs.md +++ b/test/snapshots/output.mjs.md @@ -9,10 +9,10 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `␊ - \`\`\`kroki type=plantuml␊ - A --> B␊ - \`\`\`␊ - ` + \`\`\`kroki type=plantuml␊ + A --> B␊ + \`\`\`␊ + ` > Snapshot 2 @@ -43,10 +43,10 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `␊ - \`\`\`kroki type=plantuml␊ - A --> B␊ - \`\`\`␊ - ` + \`\`\`kroki type=plantuml␊ + A --> B␊ + \`\`\`␊ + ` > Snapshot 2 @@ -87,10 +87,10 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `␊ - \`\`\`kroki type=plantuml␊ - A --> B␊ - \`\`\`␊ - ` + \`\`\`kroki type=plantuml␊ + A --> B␊ + \`\`\`␊ + ` > Snapshot 2 @@ -121,10 +121,10 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `␊ - \`\`\`kroki type=plantuml␊ - A --> B␊ - \`\`\`␊ - ` + \`\`\`kroki type=plantuml␊ + A --> B␊ + \`\`\`␊ + ` > Snapshot 2 diff --git a/test/snapshots/output.mjs.snap b/test/snapshots/output.mjs.snap index 4c5224c7478003428c1344858587fe10cea006f4..fa68fd85dee422652c61fa530955598de1751723 100644 GIT binary patch literal 2577 zcmV+s3hwnmRzVG0X0F+q_b zRo^T07axlV00000000B+T1#`=$PpGKYrT?dW4n@^a>3G7sibU)AV|=pEUAhwk>HY` zL{cJ&t*sa@;X%AYfCPccajSC6HMwqXIpmsK4ypWuJ>`(o{)gOhNDoMmlI+!bU$&eY zx<@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