Skip to content

Commit

Permalink
refactor: fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Nov 25, 2023
1 parent c7c6c22 commit 6895df7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 83 deletions.
20 changes: 6 additions & 14 deletions lib/ast.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions lib/fetch.mjs
Original file line number Diff line number Diff line change
@@ -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();
});
}
50 changes: 15 additions & 35 deletions lib/utils.mjs
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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, {
Expand Down
32 changes: 14 additions & 18 deletions test/output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
32 changes: 16 additions & 16 deletions test/snapshots/output.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`␊
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`

> Snapshot 2
Expand Down Expand Up @@ -43,10 +43,10 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`␊
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`

> Snapshot 2
Expand Down Expand Up @@ -87,10 +87,10 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`␊
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`

> Snapshot 2
Expand Down Expand Up @@ -121,10 +121,10 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`␊
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`
\`\`\`kroki type=plantuml␊
A --> B␊
\`\`\`␊
`

> Snapshot 2
Expand Down
Binary file modified test/snapshots/output.mjs.snap
Binary file not shown.

0 comments on commit 6895df7

Please sign in to comment.