From cfb7baff524c3ad4bc7303c96f15ea3db571d8f4 Mon Sep 17 00:00:00 2001 From: ale Date: Fri, 15 Nov 2024 21:56:24 +0100 Subject: [PATCH] refactor blockcount --- lib/apiswagger.js | 9 ++++----- public/main.js | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/apiswagger.js b/lib/apiswagger.js index 261cbc9..b3a3ff0 100644 --- a/lib/apiswagger.js +++ b/lib/apiswagger.js @@ -5,6 +5,7 @@ module.exports = (app, client) => { { parser } = require('stream-json'), { streamArray } = require('stream-json/streamers/StreamArray'), { chain } = require('stream-chain'), + { stringer } = require('stream-json/jsonl/Stringer'), clean = str => { return str.replace(/[/\\^$+?()`'¡¿¨!"·%&=;,\|\[\]{}]+/gmi, '') } @@ -472,12 +473,10 @@ module.exports = (app, client) => { parser(), pick({ filter: 'hits.hits' }), streamArray(), - data => ({ instance: data.value._source.instance, comment: data.value._source.blocks.find(block => block.domain === clean(req.params.instance)).comment }) + data => ({ instance: data.value._source.instance, comment: data.value._source.blocks.find(block => block.domain === clean(req.params.instance)).comment }), + stringer({ objectMode: true }) ]) - pipeline.on('readable', () => res.write('[')) - pipeline.on('data', data => res.write(data.toString() + ',')) - pipeline.on('end', () => res.end(']')) - result.pipe(pipeline) + result.pipe(pipeline).pipe(res) } else { res.status(404).end() } diff --git a/public/main.js b/public/main.js index 5b1de35..a1e6265 100644 --- a/public/main.js +++ b/public/main.js @@ -37,9 +37,9 @@ document.addEventListener('DOMContentLoaded', function () { if (content && content.length > 0) { loading() fetch('/api/block_count/' + content).then(async function (result) { - var res = await result.json() - if (Array.isArray(res) && res?.length >= 0) { - document.getElementById('blockcount').innerText = res.length + var res = await result.text() + if (res?.length >= 0) { + document.getElementById('blockcount').innerText = res.split('\n').length document.getElementById('blockinstance').innerText = 'ing ' + content document.getElementById('blocktook').innerText = '' var list = document.getElementById('blocklist'), @@ -50,7 +50,7 @@ document.addEventListener('DOMContentLoaded', function () { while (list.hasChildNodes()) { list.removeChild(list.firstChild) } - res.map(function (instance, index) { + res.split('\n').map(JSON.parse).map(function (instance, index) { var li = document.createElement('li'), text = document.createTextNode((index + 1) + '. '), link = '' + instance.instance + '',