mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Implement HEAD request support
* feat: Implement HEAD request support * feat: Integrate HEAD handler with other code * fix: Improve test by using arrayifyStream * fix: Use Promise chaining * refactor: Unwrap destroy stream promise
This commit is contained in:
parent
4d34cdd12f
commit
0644f8d245
@ -17,6 +17,12 @@
|
|||||||
"@id": "urn:solid-server:default:ResourceStore_Patching"
|
"@id": "urn:solid-server:default:ResourceStore_Patching"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"@type": "HeadOperationHandler",
|
||||||
|
"HeadOperationHandler:_store": {
|
||||||
|
"@id": "urn:solid-server:default:ResourceStore_Patching"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"@type": "PatchOperationHandler",
|
"@type": "PatchOperationHandler",
|
||||||
"PatchOperationHandler:_store": {
|
"PatchOperationHandler:_store": {
|
||||||
|
1
index.ts
1
index.ts
@ -42,6 +42,7 @@ export * from './src/logging/WinstonLoggerFactory';
|
|||||||
// LDP/Operations
|
// LDP/Operations
|
||||||
export * from './src/ldp/operations/DeleteOperationHandler';
|
export * from './src/ldp/operations/DeleteOperationHandler';
|
||||||
export * from './src/ldp/operations/GetOperationHandler';
|
export * from './src/ldp/operations/GetOperationHandler';
|
||||||
|
export * from './src/ldp/operations/HeadOperationHandler';
|
||||||
export * from './src/ldp/operations/Operation';
|
export * from './src/ldp/operations/Operation';
|
||||||
export * from './src/ldp/operations/OperationHandler';
|
export * from './src/ldp/operations/OperationHandler';
|
||||||
export * from './src/ldp/operations/PatchOperationHandler';
|
export * from './src/ldp/operations/PatchOperationHandler';
|
||||||
|
37
src/ldp/operations/HeadOperationHandler.ts
Normal file
37
src/ldp/operations/HeadOperationHandler.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { Readable } from 'stream';
|
||||||
|
import type { ResourceStore } from '../../storage/ResourceStore';
|
||||||
|
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||||
|
import type { Operation } from './Operation';
|
||||||
|
import { OperationHandler } from './OperationHandler';
|
||||||
|
import type { ResponseDescription } from './ResponseDescription';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles HEAD {@link Operation}s.
|
||||||
|
* Calls the getRepresentation function from a {@link ResourceStore}.
|
||||||
|
*/
|
||||||
|
export class HeadOperationHandler extends OperationHandler {
|
||||||
|
private readonly store: ResourceStore;
|
||||||
|
|
||||||
|
public constructor(store: ResourceStore) {
|
||||||
|
super();
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async canHandle(input: Operation): Promise<void> {
|
||||||
|
if (input.method !== 'HEAD') {
|
||||||
|
throw new UnsupportedHttpError('This handler only supports HEAD operations.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handle(input: Operation): Promise<ResponseDescription> {
|
||||||
|
const body = await this.store.getRepresentation(input.target, input.preferences);
|
||||||
|
|
||||||
|
// Close the Readable as we will not return it.
|
||||||
|
body.data.destroy();
|
||||||
|
body.data = new Readable();
|
||||||
|
body.data._read = function(): void {
|
||||||
|
body.data.push(null);
|
||||||
|
};
|
||||||
|
return { identifier: input.target, body };
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,7 @@ import {
|
|||||||
DeleteOperationHandler,
|
DeleteOperationHandler,
|
||||||
FileResourceStore,
|
FileResourceStore,
|
||||||
GetOperationHandler,
|
GetOperationHandler,
|
||||||
|
HeadOperationHandler,
|
||||||
InMemoryResourceStore,
|
InMemoryResourceStore,
|
||||||
InteractionController,
|
InteractionController,
|
||||||
MetadataController,
|
MetadataController,
|
||||||
@ -94,6 +95,7 @@ export const getPatchingStore = (store: ResourceStore): PatchingStore => {
|
|||||||
export const getOperationHandler = (store: ResourceStore): CompositeAsyncHandler<Operation, ResponseDescription> => {
|
export const getOperationHandler = (store: ResourceStore): CompositeAsyncHandler<Operation, ResponseDescription> => {
|
||||||
const handlers = [
|
const handlers = [
|
||||||
new GetOperationHandler(store),
|
new GetOperationHandler(store),
|
||||||
|
new HeadOperationHandler(store),
|
||||||
new PostOperationHandler(store),
|
new PostOperationHandler(store),
|
||||||
new PutOperationHandler(store),
|
new PutOperationHandler(store),
|
||||||
new PatchOperationHandler(store),
|
new PatchOperationHandler(store),
|
||||||
|
28
test/unit/ldp/operations/HeadOperationHandler.test.ts
Normal file
28
test/unit/ldp/operations/HeadOperationHandler.test.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import arrayifyStream from 'arrayify-stream';
|
||||||
|
import streamifyArray from 'streamify-array';
|
||||||
|
import { HeadOperationHandler } from '../../../../src/ldp/operations/HeadOperationHandler';
|
||||||
|
import type { Operation } from '../../../../src/ldp/operations/Operation';
|
||||||
|
import type { Representation } from '../../../../src/ldp/representation/Representation';
|
||||||
|
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
|
||||||
|
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
|
||||||
|
|
||||||
|
describe('A HeadOperationHandler', (): void => {
|
||||||
|
const store = {
|
||||||
|
getRepresentation: async(): Promise<Representation> => ({ binary: false, data: streamifyArray([ 1, 2, 3 ]) } as
|
||||||
|
Representation),
|
||||||
|
} as unknown as ResourceStore;
|
||||||
|
const handler = new HeadOperationHandler(store);
|
||||||
|
|
||||||
|
it('only supports HEAD operations.', async(): Promise<void> => {
|
||||||
|
await expect(handler.canHandle({ method: 'HEAD' } as Operation)).resolves.toBeUndefined();
|
||||||
|
await expect(handler.canHandle({ method: 'GET' } as Operation)).rejects.toThrow(UnsupportedHttpError);
|
||||||
|
await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the representation from the store with the input identifier and empty data.', async(): Promise<void> => {
|
||||||
|
const result = await handler.handle({ target: { path: 'url' }} as Operation);
|
||||||
|
expect(result.identifier.path).toBe('url');
|
||||||
|
expect(result.body?.binary).toBe(false);
|
||||||
|
await expect(arrayifyStream(result.body!.data)).resolves.toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user