From 00ef8ae3428a219222da798a999c736a5ead4243 Mon Sep 17 00:00:00 2001 From: Giovanni Abbatepaolo <30571828+bbtgnn@users.noreply.github.com> Date: Thu, 14 Dec 2023 09:54:57 +0100 Subject: [PATCH] fix: updated onAbort handler usage (#28) * fix: add abort handler for get * feat: add on abort handler for get requests * fix: move onAborted handler at top --- src/index.ts | 93 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 39 deletions(-) diff --git a/src/index.ts b/src/index.ts index 70a01c3..66b51be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -113,29 +113,32 @@ const generateRoutes = (app: TemplatedApp) => { const s = new Slangroom([http, wallet]); app.post(path, (res, req) => { + /** + * Code may break on `slangroom.execute` + * so it's important to attach the `onAborted` handler before everything else + */ + res.onAborted(() => { + res.writeStatus('500').writeHeader('Content-Type', 'application/json').end('Aborted'); + }); + res.cork(() => { try { - res - .onData(async (d) => { - try { - const data = handleArrayBuffer(d); - validateData(schema, data); - const { result, logs } = await s.execute(contract, { keys, data, conf }); - res - .writeStatus('200 OK') - .writeHeader('Content-Type', 'application/json') - .end(JSON.stringify(result)); - } catch (e) { - L.error(e); - res - .writeStatus('500') - .writeHeader('Content-Type', 'application/json') - .end(e.message); - } - }) - .onAborted(() => { - res.writeStatus('500').writeHeader('Content-Type', 'application/json').end(logs); - }); + res.onData(async (d) => { + try { + const data = handleArrayBuffer(d); + validateData(schema, data); + + const { result } = await s.execute(contract, { keys, data, conf }); + + res + .writeStatus('200 OK') + .writeHeader('Content-Type', 'application/json') + .end(JSON.stringify(result)); + } catch (e) { + L.error(e); + res.writeStatus('500').writeHeader('Content-Type', 'application/json').end(e.message); + } + }); } catch (e) { L.error(e); res.writeStatus('500').writeHeader('Content-Type', 'application/json').end(e.message); @@ -144,25 +147,37 @@ const generateRoutes = (app: TemplatedApp) => { }); app.get(path, async (res, req) => { - try { - const data: Record = {}; - const q = req.getQuery(); - if (q) { - q.split('&').map((r) => { - const [k, v] = r.split('='); - data[k] = v; - }); + /** + * Code may break on `slangroom.execute` + * so it's important to attach the `onAborted` handler before everything else + */ + res.onAborted(() => { + res.writeStatus('500').writeHeader('Content-Type', 'application/json').end('Aborted'); + }); + + res.cork(async () => { + try { + const data: Record = {}; + const q = req.getQuery(); + if (q) { + q.split('&').map((r) => { + const [k, v] = r.split('='); + data[k] = v; + }); + } + validateData(schema, data); + + const { result } = await s.execute(contract, { keys, conf, data }); + + res + .writeStatus('200 OK') + .writeHeader('Content-Type', 'application/json') + .end(JSON.stringify(result)); + } catch (e) { + L.error(e); + res.writeStatus('500').writeHeader('Content-Type', 'application/json').end(e.message); } - validateData(schema, data); - const { result } = await s.execute(contract, { keys, conf, data }); - res - .writeStatus('200 OK') - .writeHeader('Content-Type', 'application/json') - .end(JSON.stringify(result)); - } catch (e) { - L.error(e); - res.writeStatus('500').writeHeader('Content-Type', 'application/json').end(e.message); - } + }); }); app.get(path + '/raw', (res, req) => {