From c2b189184be8390e2335e60e64bbdab0cfee0863 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Wed, 2 Dec 2020 14:03:16 +0100 Subject: [PATCH] fix: Create container data before adding content-type --- src/storage/DataAccessorBasedStore.ts | 9 +++------ test/unit/storage/DataAccessorBasedStore.test.ts | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/storage/DataAccessorBasedStore.ts b/src/storage/DataAccessorBasedStore.ts index adf0f25da..f7514f70f 100644 --- a/src/storage/DataAccessorBasedStore.ts +++ b/src/storage/DataAccessorBasedStore.ts @@ -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'; @@ -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, @@ -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 { - // This allows other modules to still add metadata before the output data is written - return guardedStreamFrom(result.metadata.quads()); - }, + data, metadata, }; diff --git a/test/unit/storage/DataAccessorBasedStore.test.ts b/test/unit/storage/DataAccessorBasedStore.test.ts index 905be3281..9d1721372 100644 --- a/test/unit/storage/DataAccessorBasedStore.test.ts +++ b/test/unit/storage/DataAccessorBasedStore.test.ts @@ -108,9 +108,10 @@ describe('A DataAccessorBasedStore', (): void => { it('will return a data stream that matches the metadata for containers.', async(): Promise => { 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); }); });