Skip to content

Commit

Permalink
fix: updated onAbort handler usage (#28)
Browse files Browse the repository at this point in the history
* fix: add abort handler for get

* feat: add on abort handler for get requests

* fix: move onAborted handler at top
  • Loading branch information
bbtgnn authored Dec 14, 2023
1 parent 7fc4b1f commit 00ef8ae
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -144,25 +147,37 @@ const generateRoutes = (app: TemplatedApp) => {
});

app.get(path, async (res, req) => {
try {
const data: Record<string, unknown> = {};
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<string, unknown> = {};
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) => {
Expand Down

0 comments on commit 00ef8ae

Please sign in to comment.