@versatiles/google-cloud
Version:
A server for VersaTiles in Google Cloud Run
48 lines (47 loc) • 1.52 kB
JavaScript
import { parseContentEncoding } from './encoding.js';
import { readFileSync } from 'fs';
const { version } = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
export class ResponseHeaders {
#locked = false;
#headers = {
'server': 'versatiles-google-cloud v' + version,
'cache-control': 'max-age=86400', // Set default cache control header (1 day)
};
constructor(headers) {
if (headers) {
Object.entries(headers).forEach(([key, value]) => this.#headers[key] = value);
}
}
get(key) {
const value = this.#headers[key];
return (value === undefined) ? undefined : String(value);
}
set(key, value) {
if (this.#locked)
throw Error('Headers are locked. Probably because they have already been sent.');
this.#headers[key] = value;
return this;
}
remove(key) {
if (this.#locked)
throw Error('Headers are locked. Probably because they have already been sent.');
delete this.#headers[key];
return this;
}
toString() {
return JSON.stringify(this.#headers);
}
getHeaders() {
return this.#headers;
}
getContentEncoding() {
return parseContentEncoding(this.#headers['content-encoding']);
}
lock() {
this.#locked = true;
return this;
}
getMediaType() {
return String(this.#headers['content-type'] ?? '').replace(/\/.*/, '').toLowerCase();
}
}