/
HeadOperationHandler.ts
37 lines (32 loc) · 1.21 KB
/
HeadOperationHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 };
}
}