Skip to content

Commit

Permalink
Merge 0d526ea into 9a1f324
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianNoise authored Jan 14, 2022
2 parents 9a1f324 + 0d526ea commit 5dcb3e7
Show file tree
Hide file tree
Showing 45 changed files with 1,917 additions and 19 deletions.
2 changes: 2 additions & 0 deletions config/ldp/metadata-parser/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
"import": [
"files-scs:config/ldp/metadata-parser/parsers/content-type.json",
"files-scs:config/ldp/metadata-parser/parsers/content-length.json",
"files-scs:config/ldp/metadata-parser/parsers/slug.json",
"files-scs:config/ldp/metadata-parser/parsers/link.json"
],
Expand All @@ -12,6 +13,7 @@
"@type": "ParallelHandler",
"handlers": [
{ "@id": "urn:solid-server:default:ContentTypeParser" },
{ "@id": "urn:solid-server:default:ContentLengthParser" },
{ "@id": "urn:solid-server:default:SlugParser" },
{ "@id": "urn:solid-server:default:LinkRelParser" }
]
Expand Down
10 changes: 10 additions & 0 deletions config/ldp/metadata-parser/parsers/content-length.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
"@graph": [
{
"comment": "Converts content-length headers into RDF metadata.",
"@id": "urn:solid-server:default:ContentLengthParser",
"@type": "ContentLengthParser"
}
]
}
50 changes: 50 additions & 0 deletions config/quota-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
"import": [
"files-scs:config/app/main/default.json",
"files-scs:config/app/init/default.json",
"files-scs:config/app/setup/required.json",
"files-scs:config/http/handler/default.json",
"files-scs:config/http/middleware/websockets.json",
"files-scs:config/http/server-factory/websockets.json",
"files-scs:config/http/static/default.json",
"files-scs:config/identity/access/public.json",
"files-scs:config/identity/email/default.json",
"files-scs:config/identity/handler/default.json",
"files-scs:config/identity/ownership/token.json",
"files-scs:config/identity/pod/static.json",
"files-scs:config/identity/registration/enabled.json",
"files-scs:config/ldp/authentication/dpop-bearer.json",
"files-scs:config/ldp/authorization/allow-all.json",
"files-scs:config/ldp/handler/default.json",
"files-scs:config/ldp/metadata-parser/default.json",
"files-scs:config/ldp/metadata-writer/default.json",
"files-scs:config/ldp/modes/default.json",
"files-scs:config/storage/backend/pod-quota-file.json",
"files-scs:config/storage/key-value/resource-store.json",
"files-scs:config/storage/middleware/default.json",
"files-scs:config/util/auxiliary/acl.json",
"files-scs:config/util/identifiers/suffix.json",
"files-scs:config/util/index/default.json",
"files-scs:config/util/logging/winston.json",
"files-scs:config/util/representation-conversion/default.json",
"files-scs:config/util/resource-locker/memory.json",
"files-scs:config/util/variables/default.json"
],
"@graph": [
{
"comment": "A single-pod server that stores its resources on disk while enforcing quota."
},
{
"@id": "urn:solid-server:default:QuotaStrategy",
"PodQuotaStrategy:_limit_amount": 7000,
"PodQuotaStrategy:_limit_unit": "bytes",
"GlobalQuotaStrategy:_limit_amount": 10000,
"GlobalQuotaStrategy:_limit_unit": "bytes"
},
{
"@id": "urn:solid-server:default:SizeReporter",
"FileSizeReporter:_ignoreFolders": [ "^/\\.internal$" ]
}
]
}
45 changes: 45 additions & 0 deletions config/storage/backend/data-accessors/global-quota-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
"comment": "DataAccessor configuration using a GlobalQuotaStrategy to enforce quota globally on the server",
"@graph": [
{
"comment": "DataAccessor that writes data to the disk with atomicity in mind",
"@id": "urn:solid-server:default:AtomicFileDataAccessor",
"@type": "AtomicFileDataAccessor",
"resourceMapper": { "@id": "urn:solid-server:default:FileIdentifierMapper" },
"rootFilePath": { "@id": "urn:solid-server:default:variable:rootFilePath" },
"tempFilePath": "/.internal/tempFiles/"
},

{
"comment": "Calculates the space already taken up by a resource",
"@id": "urn:solid-server:default:SizeReporter",
"@type": "FileSizeReporter",
"fileIdentifierMapper": { "@id": "urn:solid-server:default:FileIdentifierMapper" },
"rootFilePath": { "@id": "urn:solid-server:default:variable:rootFilePath" },
},

{
"comment": "Enforces quota globally for all data on the server",
"@id": "urn:solid-server:default:QuotaStrategy",
"@type": "GlobalQuotaStrategy",
"reporter": { "@id": "urn:solid-server:default:SizeReporter" },
"base": { "@id": "urn:solid-server:default:variable:baseUrl" }
},

{
"comment": "Validates the data being written to the server",
"@id": "urn:solid-server:default:QuotaValidator",
"@type": "QuotaValidator",
"strategy": { "@id": "urn:solid-server:default:QuotaStrategy" }
},

{
"comment": "Simple wrapper for another DataAccessor but adds validation",
"@id": "urn:solid-server:default:FileDataAccessor",
"@type": "ValidatingDataAccessor",
"accessor": { "@id": "urn:solid-server:default:AtomicFileDataAccessor" },
"validator": { "@id": "urn:solid-server:default:QuotaValidator" }
}
]
}
46 changes: 46 additions & 0 deletions config/storage/backend/data-accessors/pod-quota-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
"comment": "DataAccessor configuration using a PodQuotaStrategy to enforce pod quotas on the server",
"@graph": [
{
"comment": "DataAccessor that writes data to the disk with atomicity in mind",
"@id": "urn:solid-server:default:AtomicFileDataAccessor",
"@type": "AtomicFileDataAccessor",
"resourceMapper": { "@id": "urn:solid-server:default:FileIdentifierMapper" },
"rootFilePath": { "@id": "urn:solid-server:default:variable:rootFilePath" },
"tempFilePath": "/.internal/tempFiles/"
},

{
"comment": "Calculates the space already taken up by a resource",
"@id": "urn:solid-server:default:SizeReporter",
"@type": "FileSizeReporter",
"fileIdentifierMapper": { "@id": "urn:solid-server:default:FileIdentifierMapper" },
"rootFilePath": { "@id": "urn:solid-server:default:variable:rootFilePath" },
},

{
"comment": "Enforces quota for all data per pod on the server",
"@id": "urn:solid-server:default:QuotaStrategy",
"@type": "PodQuotaStrategy",
"reporter": { "@id": "urn:solid-server:default:SizeReporter" },
"accessor": { "@id": "urn:solid-server:default:AtomicFileDataAccessor" },
"identifierStrategy": { "@id": "urn:solid-server:default:IdentifierStrategy" }
},

{
"comment": "Validates the data being written to the server",
"@id": "urn:solid-server:default:QuotaValidator",
"@type": "QuotaValidator",
"strategy": { "@id": "urn:solid-server:default:QuotaStrategy" }
},

{
"comment": "Simple wrapper for another DataAccessor but adds validation",
"@id": "urn:solid-server:default:FileDataAccessor",
"@type": "ValidatingDataAccessor",
"accessor": { "@id": "urn:solid-server:default:AtomicFileDataAccessor" },
"validator": { "@id": "urn:solid-server:default:QuotaValidator" }
}
]
}
16 changes: 16 additions & 0 deletions config/storage/backend/global-quota-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
"import": [
"files-scs:config/storage/backend/data-accessors/global-quota-file.json"
],
"@graph": [
{
"comment": "A default store setup with a file system backend.",
"@id": "urn:solid-server:default:ResourceStore_Backend",
"@type": "DataAccessorBasedStore",
"identifierStrategy": { "@id": "urn:solid-server:default:IdentifierStrategy" },
"auxiliaryStrategy": { "@id": "urn:solid-server:default:AuxiliaryStrategy" },
"accessor": { "@id": "urn:solid-server:default:FileDataAccessor" }
}
]
}
16 changes: 16 additions & 0 deletions config/storage/backend/pod-quota-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
"import": [
"files-scs:config/storage/backend/data-accessors/pod-quota-file.json"
],
"@graph": [
{
"comment": "A default store setup with a file system backend.",
"@id": "urn:solid-server:default:ResourceStore_Backend",
"@type": "DataAccessorBasedStore",
"identifierStrategy": { "@id": "urn:solid-server:default:IdentifierStrategy" },
"auxiliaryStrategy": { "@id": "urn:solid-server:default:AuxiliaryStrategy" },
"accessor": { "@id": "urn:solid-server:default:FileDataAccessor" }
}
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"prepare": "npm run build",
"start": "node ./bin/server.js",
"start:file": "node ./bin/server.js -c config/file.json -f ./data",
"start:file:quota": "node ./bin/server.js -c config/quota-file.json -f ./data",
"test": "npm run test:ts && npm run jest",
"test:deploy": "test/deploy/validate-package.sh",
"test:ts": "tsc -p test --noEmit",
Expand Down
5 changes: 4 additions & 1 deletion src/http/auxiliary/ComposedAuxiliaryStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export class ComposedAuxiliaryStrategy implements AuxiliaryStrategy {

public async validate(representation: Representation): Promise<void> {
if (this.validator) {
return this.validator.handleSafe(representation);
await this.validator.handleSafe({
representation,
identifier: { path: representation.metadata.identifier.value },
});
}
}
}
8 changes: 5 additions & 3 deletions src/http/auxiliary/RdfValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { RepresentationConverter } from '../../storage/conversion/Represent
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { cloneRepresentation } from '../../util/ResourceUtil';
import type { Representation } from '../representation/Representation';
import type { ValidatorInput } from './Validator';
import { Validator } from './Validator';

/**
Expand All @@ -17,12 +18,11 @@ export class RdfValidator extends Validator {
this.converter = converter;
}

public async handle(representation: Representation): Promise<void> {
public async handle({ representation, identifier }: ValidatorInput): Promise<Representation> {
// If the data already is quads format we know it's RDF
if (representation.metadata.contentType === INTERNAL_QUADS) {
return;
return representation;
}
const identifier = { path: representation.metadata.identifier.value };
const preferences = { type: { [INTERNAL_QUADS]: 1 }};
let result;
try {
Expand All @@ -39,5 +39,7 @@ export class RdfValidator extends Validator {
}
// Drain stream to make sure data was parsed correctly
await arrayifyStream(result.data);

return representation;
}
}
8 changes: 7 additions & 1 deletion src/http/auxiliary/Validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { AsyncHandler } from '../../util/handlers/AsyncHandler';
import type { Representation } from '../representation/Representation';
import type { ResourceIdentifier } from '../representation/ResourceIdentifier';

export type ValidatorInput = {
representation: Representation;
identifier: ResourceIdentifier;
};

/**
* Generic interface for classes that validate Representations in some way.
*/
export abstract class Validator extends AsyncHandler<Representation> { }
export abstract class Validator extends AsyncHandler<ValidatorInput, Representation> { }
23 changes: 23 additions & 0 deletions src/http/input/metadata/ContentLengthParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getLoggerFor } from '../../../logging/LogUtil';
import type { HttpRequest } from '../../../server/HttpRequest';
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
import { MetadataParser } from './MetadataParser';

/**
* Parser for the `content-length` header.
*/
export class ContentLengthParser extends MetadataParser {
protected readonly logger = getLoggerFor(this);

public async handle(input: { request: HttpRequest; metadata: RepresentationMetadata }): Promise<void> {
const contentLength = input.request.headers['content-length'];
if (contentLength) {
const length = /^\s*(\d+)\s*(?:;.*)?$/u.exec(contentLength)?.[1];
if (length) {
input.metadata.contentLength = Number(length);
} else {
this.logger.warn(`Invalid content-length header found: ${contentLength}.`);
}
}
}
}
18 changes: 16 additions & 2 deletions src/http/representation/RepresentationMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { DataFactory, Store } from 'n3';
import type { BlankNode, DefaultGraph, Literal, NamedNode, Quad, Term } from 'rdf-js';
import { getLoggerFor } from '../../logging/LogUtil';
import { InternalServerError } from '../../util/errors/InternalServerError';
import { toNamedTerm, toObjectTerm, toCachedNamedNode, isTerm } from '../../util/TermUtil';
import { CONTENT_TYPE, CONTENT_TYPE_TERM } from '../../util/Vocabularies';
import { toNamedTerm, toObjectTerm, toCachedNamedNode, isTerm, toLiteral } from '../../util/TermUtil';
import { CONTENT_TYPE, CONTENT_TYPE_TERM, CONTENT_LENGTH_TERM, XSD } from '../../util/Vocabularies';
import type { ResourceIdentifier } from './ResourceIdentifier';
import { isResourceIdentifier } from './ResourceIdentifier';

Expand Down Expand Up @@ -316,4 +316,18 @@ export class RepresentationMetadata {
public set contentType(input) {
this.set(CONTENT_TYPE_TERM, input);
}

/**
* Shorthand for the CONTENT_LENGTH predicate.
*/
public get contentLength(): number | undefined {
const length = this.get(CONTENT_LENGTH_TERM);
return length?.value ? Number(length.value) : undefined;
}

public set contentLength(input) {
if (input) {
this.set(CONTENT_LENGTH_TERM, toLiteral(input, XSD.terms.integer));
}
}
}
20 changes: 19 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export * from './authorization/permissions/MethodModesExtractor';
export * from './authorization/permissions/SparqlPatchModesExtractor';

// Authorization
export * from './authorization/OwnerPermissionReader';
export * from './authorization/AllStaticReader';
export * from './authorization/Authorizer';
export * from './authorization/AuxiliaryReader';
export * from './authorization/OwnerPermissionReader';
export * from './authorization/PathBasedReader';
export * from './authorization/PermissionBasedAuthorizer';
export * from './authorization/PermissionReader';
Expand Down Expand Up @@ -57,6 +57,7 @@ export * from './http/input/identifier/OriginalUrlExtractor';
export * from './http/input/identifier/TargetExtractor';

// HTTP/Input/Metadata
export * from './http/input/metadata/ContentLengthParser';
export * from './http/input/metadata/ContentTypeParser';
export * from './http/input/metadata/LinkRelParser';
export * from './http/input/metadata/MetadataParser';
Expand Down Expand Up @@ -248,10 +249,14 @@ export * from './server/util/RedirectAllHttpHandler';
export * from './server/util/RouterHandler';

// Storage/Accessors
export * from './storage/accessors/AtomicDataAccessor';
export * from './storage/accessors/AtomicFileDataAccessor';
export * from './storage/accessors/DataAccessor';
export * from './storage/accessors/FileDataAccessor';
export * from './storage/accessors/InMemoryDataAccessor';
export * from './storage/accessors/PassthroughDataAccessor';
export * from './storage/accessors/SparqlDataAccessor';
export * from './storage/accessors/ValidatingDataAccessor';

// Storage/Conversion
export * from './storage/conversion/BaseTypedRepresentationConverter';
Expand Down Expand Up @@ -295,13 +300,26 @@ export * from './storage/patch/RepresentationPatcher';
export * from './storage/patch/RepresentationPatchHandler';
export * from './storage/patch/SparqlUpdatePatcher';

// Storage/Quota
export * from './storage/quota/GlobalQuotaStrategy';
export * from './storage/quota/PodQuotaStrategy';
export * from './storage/quota/QuotaStrategy';

// Storage/Routing
export * from './storage/routing/BaseUrlRouterRule';
export * from './storage/routing/ConvertingRouterRule';
export * from './storage/routing/PreferenceSupport';
export * from './storage/routing/RegexRouterRule';
export * from './storage/routing/RouterRule';

// Storage/Size-Reporter
export * from './storage/size-reporter/FileSizeReporter';
export * from './storage/size-reporter/Size';
export * from './storage/size-reporter/SizeReporter';

// Storage/Validators
export * from './storage/validators/QuotaValidator';

// Storage
export * from './storage/AtomicResourceStore';
export * from './storage/BaseResourceStore';
Expand Down
10 changes: 10 additions & 0 deletions src/storage/accessors/AtomicDataAccessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { DataAccessor } from './DataAccessor';

/**
* The AtomicDataAccessor interface has identical function signatures as
* the DataAccessor, with the additional constraint that every function call
* must be atomic in its effect: either the call fully succeeds, reaching the
* desired new state; or it fails, upon which the resulting state remains
* identical to the one before the call.
*/
export interface AtomicDataAccessor extends DataAccessor { }
Loading

0 comments on commit 5dcb3e7

Please sign in to comment.