mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Specifiy constants in separate file
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { BodyParser } from './BodyParser';
|
||||
import { DATA_TYPE_QUAD } from '../../util/ContentTypes';
|
||||
import { HttpRequest } from '../../server/HttpRequest';
|
||||
import { PassThrough } from 'stream';
|
||||
import { QuadRepresentation } from '../representation/QuadRepresentation';
|
||||
@@ -53,7 +54,7 @@ export class SimpleBodyParser extends BodyParser {
|
||||
data.on('error', (error): boolean => errorStream.emit('error', new UnsupportedHttpError(error.message)));
|
||||
|
||||
return {
|
||||
dataType: 'quad',
|
||||
dataType: DATA_TYPE_QUAD,
|
||||
data: errorStream,
|
||||
metadata,
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { DATA_TYPE_BINARY } from '../../util/ContentTypes';
|
||||
import { HttpError } from '../../util/errors/HttpError';
|
||||
import { HttpResponse } from '../../server/HttpResponse';
|
||||
import { ResponseDescription } from '../operations/ResponseDescription';
|
||||
@@ -11,8 +12,8 @@ export class SimpleResponseWriter extends ResponseWriter {
|
||||
public async canHandle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> {
|
||||
if (!(input.result instanceof Error)) {
|
||||
const dataType = input.result.body?.dataType;
|
||||
if (dataType && dataType !== 'binary' && dataType !== 'string') {
|
||||
throw new UnsupportedHttpError('Only string or binary results are supported.');
|
||||
if (dataType && dataType !== DATA_TYPE_BINARY) {
|
||||
throw new UnsupportedHttpError('Only binary results are supported.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import { ResourceStore } from './ResourceStore';
|
||||
import streamifyArray from 'streamify-array';
|
||||
import { StreamWriter } from 'n3';
|
||||
import { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTypeHttpError';
|
||||
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../util/ContentTypes';
|
||||
|
||||
/**
|
||||
* Resource store storing its data as Quads in an in-memory map.
|
||||
@@ -113,7 +114,7 @@ export class SimpleResourceStore implements ResourceStore {
|
||||
* @returns Promise of array of Quads pulled from the stream.
|
||||
*/
|
||||
private async parseRepresentation(representation: Representation): Promise<Quad[]> {
|
||||
if (representation.dataType !== 'quad') {
|
||||
if (representation.dataType !== DATA_TYPE_QUAD) {
|
||||
throw new UnsupportedMediaTypeHttpError('SimpleResourceStore only supports quad representations.');
|
||||
}
|
||||
return arrayifyStream(representation.data);
|
||||
@@ -134,7 +135,7 @@ export class SimpleResourceStore implements ResourceStore {
|
||||
*/
|
||||
private generateRepresentation(data: Quad[], preferences: RepresentationPreferences): Representation {
|
||||
// Always return turtle unless explicitly asked for quads
|
||||
if (preferences.type?.some((preference): boolean => preference.value.includes('internal/quads'))) {
|
||||
if (preferences.type?.some((preference): boolean => preference.value.includes(CONTENT_TYPE_QUADS))) {
|
||||
return this.generateQuadRepresentation(data);
|
||||
}
|
||||
return this.generateBinaryRepresentation(data);
|
||||
@@ -148,7 +149,7 @@ export class SimpleResourceStore implements ResourceStore {
|
||||
*/
|
||||
private generateBinaryRepresentation(data: Quad[]): BinaryRepresentation {
|
||||
return {
|
||||
dataType: 'binary',
|
||||
dataType: DATA_TYPE_BINARY,
|
||||
data: streamifyArray([ ...data ]).pipe(new StreamWriter({ format: 'text/turtle' })),
|
||||
metadata: { raw: [], profiles: [], contentType: 'text/turtle' },
|
||||
};
|
||||
@@ -162,7 +163,7 @@ export class SimpleResourceStore implements ResourceStore {
|
||||
*/
|
||||
private generateQuadRepresentation(data: Quad[]): QuadRepresentation {
|
||||
return {
|
||||
dataType: 'quad',
|
||||
dataType: DATA_TYPE_QUAD,
|
||||
data: streamifyArray([ ...data ]),
|
||||
metadata: { raw: [], profiles: []},
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@ import { someTerms } from 'rdf-terms';
|
||||
import { SparqlUpdatePatch } from '../../ldp/http/SparqlUpdatePatch';
|
||||
import { Store } from 'n3';
|
||||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../util/ContentTypes';
|
||||
|
||||
/**
|
||||
* PatchHandler that supports specific types of SPARQL updates.
|
||||
@@ -55,7 +56,7 @@ export class SimpleSparqlUpdatePatchHandler extends PatchHandler {
|
||||
|
||||
const lock = await this.locker.acquire(input.identifier);
|
||||
const quads = await this.source.getRepresentation(input.identifier,
|
||||
{ type: [{ value: 'internal/quads', weight: 1 }]});
|
||||
{ type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]});
|
||||
const store = new Store<BaseQuad>();
|
||||
const importEmitter = store.import(quads.data);
|
||||
await new Promise((resolve, reject): void => {
|
||||
@@ -66,11 +67,11 @@ export class SimpleSparqlUpdatePatchHandler extends PatchHandler {
|
||||
store.addQuads(inserts);
|
||||
const representation: Representation = {
|
||||
data: store.match() as Readable,
|
||||
dataType: 'quad',
|
||||
dataType: DATA_TYPE_QUAD,
|
||||
metadata: {
|
||||
raw: [],
|
||||
profiles: [],
|
||||
contentType: 'internal/quads',
|
||||
contentType: CONTENT_TYPE_QUADS,
|
||||
},
|
||||
};
|
||||
await this.source.setRepresentation(input.identifier, representation);
|
||||
|
||||
4
src/util/ContentTypes.ts
Normal file
4
src/util/ContentTypes.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export const DATA_TYPE_BINARY = 'binary';
|
||||
export const DATA_TYPE_QUAD = 'quad';
|
||||
|
||||
export const CONTENT_TYPE_QUADS = 'internal/quads';
|
||||
Reference in New Issue
Block a user