Skip to content

Commit

Permalink
fix: do not mix async with callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
rorlic committed Dec 21, 2023
1 parent 61a47b2 commit 5699567
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/ldes-fragment-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ export class LdesFragmentController {
*/
public async parseJsonLd(content: string): Promise<RDF.Quad[]> {
const store = new Store();
const parser = new JsonLdParser({skipContextValidation: true});
parser.write(content);
parser.end();
const parser = new JsonLdParser();
parser.end(content);
await promisifyEventEmitter(store.import(parser));
const quads = store.getQuads(null, null, null, null);
return quads;
Expand Down Expand Up @@ -70,10 +69,11 @@ export class LdesFragmentController {
*/
public async postFragment(request: IPostRequest<RDF.Quad[], ICreateFragmentOptions>): Promise<IResponse<IFragmentInfo>> {
const response = await this.service.save(request.body, request.query, request.headers)
return {
const result = {
status: response.id ? 201 : 400,
body: response,
};
return result;
}

/**
Expand Down
21 changes: 13 additions & 8 deletions src/ldes-fragment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ export class LdesFragmentService {
const store = new Store(quads);
const fragmentId = this.fixUris(store);
const id = fragmentId?.replace(this.baseUrl.href, '/');
if (!id) return { id: ''};

headers = this.addCacheControlHeader(options, headers);
quads = store.getQuads(null,null,null,null);
this.repository.save(id, quads, headers);
return {...headers, id: id};
let result;
if (!id) {
result = { id: '' };
} else {
headers = this.addCacheControlHeader(options, headers);
quads = store.getQuads(null,null,null,null);
this.repository.save(id, quads, headers);
result = {...headers, id: id};
}

return result;
}


Expand Down Expand Up @@ -53,7 +59,6 @@ export class LdesFragmentService {
if (!nodeId) { // search for a view instead
nodeId = store.getObjects(null, this.treeView, null).shift()?.value;
}

if (!nodeId) return undefined;

const uris = [nodeId];
Expand All @@ -76,8 +81,8 @@ export class LdesFragmentService {
this.replaceQuads(store, oldObjects, newObjects);
});

const fragmentUrl = this.changeOrigin(new URL(nodeId), this.baseUrl);
return fragmentUrl.href;
const fragmentId = this.changeOrigin(new URL(nodeId), this.baseUrl).href;
return fragmentId;
}

public get(fragmentId: string): IFragment | undefined {
Expand Down
7 changes: 4 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ server.get('/*', async (request, reply) => {
const result = await controller.getFragment({ query: { id: request.url } }, baseUrl);
if (result.status === 302) { // redirect
respondWith(reply, result);
return;
}

const accepts = (request as any).accepts();
Expand All @@ -80,13 +81,13 @@ server.get('/*', async (request, reply) => {
respondWith(reply, response);
});

server.addContentTypeParser(mimeJsonLd, { parseAs: 'string' }, async (_, body: string, done) => {
server.addContentTypeParser(mimeJsonLd, { parseAs: 'string' }, async (_:any, body: string) => {
try {
const quads = await controller.parseJsonLd(body);
done(null, quads);
return quads;
} catch (err: any) {
err.statusCode = 400;
done(err, undefined);
throw err;
}
})

Expand Down

0 comments on commit 5699567

Please sign in to comment.