Skip to content

Commit

Permalink
fix: Create container data before adding content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Dec 2, 2020
1 parent 82393ed commit c2b1891
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/storage/DataAccessorBasedStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Readable } from 'stream';
import { DataFactory } from 'n3';
import type { Quad } from 'rdf-js';
import { v4 as uuid } from 'uuid';
Expand All @@ -11,7 +10,6 @@ import { ConflictHttpError } from '../util/errors/ConflictHttpError';
import { MethodNotAllowedHttpError } from '../util/errors/MethodNotAllowedHttpError';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
import type { Guarded } from '../util/GuardedStream';
import {
ensureTrailingSlash,
getParentContainer,
Expand Down Expand Up @@ -68,13 +66,12 @@ export class DataAccessorBasedStore implements ResourceStore {

// Create the representation of a container
if (this.isExistingContainer(metadata)) {
// Generate the data stream before setting the content-type to prevent unnecessary triples
const data = guardedStreamFrom(metadata.quads());
metadata.contentType = INTERNAL_QUADS;
result = {
binary: false,
get data(): Guarded<Readable> {
// This allows other modules to still add metadata before the output data is written
return guardedStreamFrom(result.metadata.quads());
},
data,
metadata,
};

Expand Down
3 changes: 2 additions & 1 deletion test/unit/storage/DataAccessorBasedStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ describe('A DataAccessorBasedStore', (): void => {
it('will return a data stream that matches the metadata for containers.', async(): Promise<void> => {
const resourceID = { path: `${root}container/` };
accessor.data[resourceID.path] = { metadata: containerMetadata } as Representation;
const metaQuads = containerMetadata.quads();
const result = await store.getRepresentation(resourceID);
expect(result).toMatchObject({ binary: false });
expect(await arrayifyStream(result.data)).toBeRdfIsomorphic(containerMetadata.quads());
expect(await arrayifyStream(result.data)).toBeRdfIsomorphic(metaQuads);
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
});
});
Expand Down

0 comments on commit c2b1891

Please sign in to comment.