Skip to content

Commit

Permalink
refactor: Use types to create vocabularies.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jan 2, 2021
1 parent 882c0fd commit 8e138c3
Show file tree
Hide file tree
Showing 41 changed files with 145 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/authorization/WebAclAuthorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { InternalServerError } from '../util/errors/InternalServerError';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { UnauthorizedHttpError } from '../util/errors/UnauthorizedHttpError';
import type { IdentifierStrategy } from '../util/identifiers/IdentifierStrategy';
import { ACL, FOAF } from '../util/UriConstants';
import { ACL, FOAF } from '../util/Vocabularies';
import type { AclManager } from './AclManager';
import type { AuthorizerArgs } from './Authorizer';
import { Authorizer } from './Authorizer';
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,6 @@ export * from './util/QuadUtil';
export * from './util/RecordObject';
export * from './util/SequenceHandler';
export * from './util/StreamUtil';
export * from './util/WaterfallHandler';
export * from './util/UriConstants';
export * from './util/UriUtil';
export * from './util/Vocabularies';
export * from './util/WaterfallHandler';
2 changes: 1 addition & 1 deletion src/init/AclInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { ensureTrailingSlash } from '../util/PathUtil';
import { guardedStreamFrom } from '../util/StreamUtil';
import { CONTENT_TYPE } from '../util/UriConstants';
import { CONTENT_TYPE } from '../util/Vocabularies';
import { Initializer } from './Initializer';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/init/RootContainerInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { ensureTrailingSlash } from '../util/PathUtil';
import { generateResourceQuads } from '../util/ResourceUtil';
import { guardedStreamFrom } from '../util/StreamUtil';
import { PIM, RDF } from '../util/UriConstants';
import { toCachedNamedNode } from '../util/UriUtil';
import { PIM, RDF } from '../util/Vocabularies';
import { Initializer } from './Initializer';
import namedNode = DataFactory.namedNode;

Expand Down
2 changes: 1 addition & 1 deletion src/ldp/http/metadata/LinkTypeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DataFactory } from 'n3';
import { getLoggerFor } from '../../../logging/LogUtil';
import type { HttpRequest } from '../../../server/HttpRequest';
import { parseParameters, splitAndClean, transformQuotedStrings } from '../../../util/HeaderUtil';
import { RDF } from '../../../util/UriConstants';
import { RDF } from '../../../util/Vocabularies';
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
import type { MetadataParser } from './MetadataParser';

Expand Down
2 changes: 1 addition & 1 deletion src/ldp/http/metadata/SlugParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getLoggerFor } from '../../../logging/LogUtil';
import type { HttpRequest } from '../../../server/HttpRequest';
import { BadRequestHttpError } from '../../../util/errors/BadRequestHttpError';
import { HTTP } from '../../../util/UriConstants';
import { HTTP } from '../../../util/Vocabularies';
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
import type { MetadataParser } from './MetadataParser';

Expand Down
2 changes: 1 addition & 1 deletion src/ldp/http/response/CreatedResponseDescription.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataFactory } from 'n3';
import { HTTP } from '../../../util/UriConstants';
import { HTTP } from '../../../util/Vocabularies';
import { RepresentationMetadata } from '../../representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../../representation/ResourceIdentifier';
import { ResponseDescription } from './ResponseDescription';
Expand Down
2 changes: 1 addition & 1 deletion src/storage/DataAccessorBasedStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { parseQuads } from '../util/QuadUtil';
import { generateResourceQuads } from '../util/ResourceUtil';
import { guardedStreamFrom } from '../util/StreamUtil';
import { CONTENT_TYPE, HTTP, LDP, PIM, RDF } from '../util/UriConstants';
import { CONTENT_TYPE, HTTP, LDP, PIM, RDF } from '../util/Vocabularies';
import type { DataAccessor } from './accessors/DataAccessor';
import type { ResourceStore } from './ResourceStore';

Expand Down
2 changes: 1 addition & 1 deletion src/storage/accessors/FileDataAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type { Guarded } from '../../util/GuardedStream';
import { isContainerIdentifier } from '../../util/PathUtil';
import { parseQuads, pushQuad, serializeQuads } from '../../util/QuadUtil';
import { generateContainmentQuads, generateResourceQuads } from '../../util/ResourceUtil';
import { CONTENT_TYPE, DCTERMS, LDP, POSIX, RDF, XSD } from '../../util/UriConstants';
import { toCachedNamedNode, toLiteral } from '../../util/UriUtil';
import { CONTENT_TYPE, DCTERMS, LDP, POSIX, RDF, XSD } from '../../util/Vocabularies';
import type { FileIdentifierMapper, ResourceLink } from '../mapping/FileIdentifierMapper';
import type { DataAccessor } from './DataAccessor';

Expand Down
2 changes: 1 addition & 1 deletion src/storage/accessors/SparqlDataAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import { guardStream } from '../../util/GuardedStream';
import type { Guarded } from '../../util/GuardedStream';
import type { IdentifierStrategy } from '../../util/identifiers/IdentifierStrategy';
import { isContainerIdentifier } from '../../util/PathUtil';
import { CONTENT_TYPE, LDP } from '../../util/UriConstants';
import { toCachedNamedNode } from '../../util/UriUtil';
import { CONTENT_TYPE, LDP } from '../../util/Vocabularies';
import type { DataAccessor } from './DataAccessor';

const { defaultGraph, namedNode, quad, variable } = DataFactory;
Expand Down
2 changes: 1 addition & 1 deletion src/storage/conversion/QuadToRdfConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RepresentationMetadata } from '../../ldp/representation/RepresentationM
import type { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { guardStream } from '../../util/GuardedStream';
import { CONTENT_TYPE } from '../../util/UriConstants';
import { CONTENT_TYPE } from '../../util/Vocabularies';
import { matchingMediaTypes } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
Expand Down
2 changes: 1 addition & 1 deletion src/storage/conversion/RdfToQuadConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RepresentationMetadata } from '../../ldp/representation/RepresentationM
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { pipeSafely } from '../../util/StreamUtil';
import { CONTENT_TYPE } from '../../util/UriConstants';
import { CONTENT_TYPE } from '../../util/Vocabularies';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';

Expand Down
2 changes: 1 addition & 1 deletion src/storage/patch/SparqlUpdatePatchHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { guardStream } from '../../util/GuardedStream';
import type { ResourceLocker } from '../../util/locking/ResourceLocker';
import { CONTENT_TYPE } from '../../util/UriConstants';
import { CONTENT_TYPE } from '../../util/Vocabularies';
import type { ResourceStore } from '../ResourceStore';
import { PatchHandler } from './PatchHandler';

Expand Down
2 changes: 1 addition & 1 deletion src/util/ResourceUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { DataFactory } from 'n3';
import type { NamedNode, Quad } from 'rdf-js';
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
import { pushQuad } from './QuadUtil';
import { LDP, RDF } from './UriConstants';
import { toCachedNamedNode } from './UriUtil';
import { LDP, RDF } from './Vocabularies';

/**
* Helper function to generate type quads for a Container or Resource.
Expand Down
72 changes: 0 additions & 72 deletions src/util/UriConstants.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/UriUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataFactory } from 'n3';
import type { Literal, NamedNode, Term } from 'rdf-js';
import { CONTENT_TYPE } from './UriConstants';
import { CONTENT_TYPE } from './Vocabularies';

const { namedNode, literal } = DataFactory;

Expand Down
89 changes: 89 additions & 0 deletions src/util/Vocabularies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable @typescript-eslint/naming-convention, function-paren-newline */

This comment has been minimized.

Copy link
@RubenVerborgh

RubenVerborgh Jan 2, 2021

Author Member

@joachimvh You might want to take a moment to appreciate these strongly typed namespaces.

This comment has been minimized.

Copy link
@joachimvh

joachimvh Jan 4, 2021

Member

I just spent way too much time deconstructing why and how this works 😅 . But yes, pretty cool ^^.

The one thing I'm wondering is if why Namespace is also a PrefixResolver? I think we always want to use constants and not use strings anywhere so why expose that function?

This comment has been minimized.

Copy link
@RubenVerborgh

RubenVerborgh Jan 4, 2021

Author Member

I bet it's easier to write than to read 😅

I think it's just nice to be able to FOAF('Person'), but not a hard requirement for me indeed. It's basically a way of exporting the prefix in an actionable way.


type PrefixResolver<T> = (localName: string) => T;
type RecordOf<TKey extends any[], TValue> = Record<TKey[number], TValue>;

export type Namespace<TKey extends any[], TValue> =
PrefixResolver<TValue> & RecordOf<TKey, TValue>;

/**
* Creates a function that expands local names from the given base URI,
* and exports the given local names as properties on the returned object.
*/
export const createNamespace = <T extends string>(baseUri: string, ...localNames: T[]):
Namespace<typeof localNames, string> => {
// Create a function that expands local names
const expanded = {} as Record<string, string>;
const namespace = ((localName: string): string => {
if (!(localName in expanded)) {
expanded[localName] = `${baseUri}${localName}`;
}
return expanded[localName];
}) as Namespace<typeof localNames, string>;

// Expose the listed local names as properties
for (const localName of localNames) {
(namespace as RecordOf<typeof localNames, string>)[localName] = namespace(localName);
}
return namespace;
};

export const ACL = createNamespace('http://www.w3.org/ns/auth/acl#',
'accessTo',
'agent',
'agentClass',
'default',
'mode',

'Write',
'Read',
'Append',
'Control',
);

export const DCTERMS = createNamespace('http://purl.org/dc/terms/',
'modified',
);

export const FOAF = createNamespace('http://xmlns.com/foaf/0.1/',
'Agent',
'AuthenticatedAgent',
);

export const HTTP = createNamespace('urn:solid:http:',
'location',
'slug',
);

export const LDP = createNamespace('http://www.w3.org/ns/ldp#',
'contains',

'BasicContainer',
'Container',
'Resource',
);

export const MA = createNamespace('http://www.w3.org/ns/ma-ont#',
'format',
);

export const PIM = createNamespace('http://www.w3.org/ns/pim/space#',
'Storage',
);

export const POSIX = createNamespace('http://www.w3.org/ns/posix/stat#',
'mtime',
'size',
);

export const RDF = createNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'type',
);

export const XSD = createNamespace('http://www.w3.org/2001/XMLSchema#',
'dateTime',
'integer',
);

// Alias for most commonly used URI
export const CONTENT_TYPE = MA.format;
2 changes: 1 addition & 1 deletion test/integration/LdpHandlerWithAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from 'path';
import type { HttpHandler, Initializer, ResourceStore } from '../../src/';
import { RepresentationMetadata } from '../../src/ldp/representation/RepresentationMetadata';
import { guardStream } from '../../src/util/GuardedStream';
import { CONTENT_TYPE, LDP } from '../../src/util/UriConstants';
import { CONTENT_TYPE, LDP } from '../../src/util/Vocabularies';
import { AclHelper, ResourceHelper } from '../util/TestHelpers';
import { BASE, getTestFolder, createFolder, removeFolder, instantiateFromConfig } from './Config';

Expand Down
2 changes: 1 addition & 1 deletion test/integration/LdpHandlerWithoutAuth.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { HttpHandler, Initializer, ResourceStore } from '../../src/';
import { LDP } from '../../src/util/UriConstants';
import { LDP } from '../../src/util/Vocabularies';
import { ResourceHelper } from '../util/TestHelpers';
import { BASE, getTestFolder, createFolder, removeFolder, instantiateFromConfig } from './Config';

Expand Down
2 changes: 1 addition & 1 deletion test/integration/LockingResourceStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { ResourceLocker } from '../../src/util/locking/ResourceLocker';
import { SingleThreadedResourceLocker } from '../../src/util/locking/SingleThreadedResourceLocker';
import { WrappedExpiringResourceLocker } from '../../src/util/locking/WrappedExpiringResourceLocker';
import { guardedStreamFrom } from '../../src/util/StreamUtil';
import { CONTENT_TYPE } from '../../src/util/UriConstants';
import { CONTENT_TYPE } from '../../src/util/Vocabularies';
import { BASE } from './Config';

describe('A LockingResourceStore', (): void => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/LpdHandlerOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { namedNode, quad } from '@rdfjs/data-model';
import { Parser } from 'n3';
import type { MockResponse } from 'node-mocks-http';
import type { HttpHandler } from '../../src/server/HttpHandler';
import { LDP } from '../../src/util/UriConstants';
import { LDP } from '../../src/util/Vocabularies';
import { performRequest } from '../util/Util';
import { BASE, instantiateFromConfig } from './Config';

Expand Down
2 changes: 1 addition & 1 deletion test/integration/RepresentationConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChainedConverter } from '../../src/storage/conversion/ChainedConverter'
import { QuadToRdfConverter } from '../../src/storage/conversion/QuadToRdfConverter';
import { RdfToQuadConverter } from '../../src/storage/conversion/RdfToQuadConverter';
import { guardedStreamFrom, readableToString } from '../../src/util/StreamUtil';
import { CONTENT_TYPE } from '../../src/util/UriConstants';
import { CONTENT_TYPE } from '../../src/util/Vocabularies';

describe('A ChainedConverter', (): void => {
const converters = [
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ldp/http/BasicResponseWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RepresentationMetadata } from '../../../../src/ldp/representation/Repre
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
import { CONTENT_TYPE } from '../../../../src/util/UriConstants';
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';

describe('A BasicResponseWriter', (): void => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ldp/http/metadata/BasicMetadataExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BasicMetadataExtractor } from '../../../../../src/ldp/http/metadata/Bas
import type { MetadataParser } from '../../../../../src/ldp/http/metadata/MetadataParser';
import type { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata';
import type { HttpRequest } from '../../../../../src/server/HttpRequest';
import { RDF } from '../../../../../src/util/UriConstants';
import { RDF } from '../../../../../src/util/Vocabularies';

class BasicParser implements MetadataParser {
private readonly header: string;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ldp/http/metadata/LinkRelMetadataWriter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createResponse } from 'node-mocks-http';
import { LinkRelMetadataWriter } from '../../../../../src/ldp/http/metadata/LinkRelMetadataWriter';
import { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata';
import { LDP, RDF } from '../../../../../src/util/UriConstants';
import { toCachedNamedNode } from '../../../../../src/util/UriUtil';
import { LDP, RDF } from '../../../../../src/util/Vocabularies';

describe('A LinkRelMetadataWriter', (): void => {
const writer = new LinkRelMetadataWriter({ [RDF.type]: 'type', dummy: 'dummy' });
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ldp/http/metadata/LinkTypeParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LinkTypeParser } from '../../../../../src/ldp/http/metadata/LinkTypeParser';
import { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata';
import type { HttpRequest } from '../../../../../src/server/HttpRequest';
import { RDF } from '../../../../../src/util/UriConstants';
import { RDF } from '../../../../../src/util/Vocabularies';

describe('A LinkTypeParser', (): void => {
const parser = new LinkTypeParser();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ldp/http/metadata/MappedMetadataWriter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createResponse } from 'node-mocks-http';
import { MappedMetadataWriter } from '../../../../../src/ldp/http/metadata/MappedMetadataWriter';
import { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata';
import { CONTENT_TYPE } from '../../../../../src/util/UriConstants';
import { CONTENT_TYPE } from '../../../../../src/util/Vocabularies';

describe('A MappedMetadataWriter', (): void => {
const writer = new MappedMetadataWriter({ [CONTENT_TYPE]: 'content-type', dummy: 'dummy' });
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ldp/http/metadata/SlugParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SlugParser } from '../../../../../src/ldp/http/metadata/SlugParser';
import { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata';
import type { HttpRequest } from '../../../../../src/server/HttpRequest';
import { BadRequestHttpError } from '../../../../../src/util/errors/BadRequestHttpError';
import { HTTP } from '../../../../../src/util/UriConstants';
import { HTTP } from '../../../../../src/util/Vocabularies';

describe('A SlugParser', (): void => {
const parser = new SlugParser();
Expand Down

0 comments on commit 8e138c3

Please sign in to comment.