nephele
Version:
Highly customizable and extensible WebDAV server for Node.js and Express.
125 lines • 4.53 kB
JavaScript
import path from 'node:path';
export const defaults = {
compression: false,
minLockTimeout: 1000 * 10,
maxLockTimeout: 1000 * 60 * 60 * 18,
errorHandler: async (code, message, _request, response, error) => {
if (code < 400) {
if (response.headersSent || response.destroyed) {
response.end();
return;
}
response.status(code);
if (error && 'etag' in error && error.etag) {
response.set({
ETag: error.etag,
});
}
if (error && 'lastModified' in error && error.lastModified) {
response.set({
'Last-Modified': error.lastModified.toUTCString(),
});
}
response.end();
return;
}
if (error) {
response.locals.errors.push(error);
}
if (code === 500 && error) {
response.locals.debug('Unknown Error: %o', error);
}
if (response.headersSent || response.destroyed) {
response.end();
return;
}
let body = `Error ${code}: ${message}`;
let contentType = 'text/plain';
if (process.env.NODE_ENV !== 'production') {
body = JSON.stringify({
code,
message,
...(error
? {
errorMessage: error.message,
stack: error.stack,
error,
}
: {}),
});
contentType = 'application/json';
}
if (!response.headersSent) {
response.status(code);
response.set({
'Content-Type': `${contentType}; charset=utf-8`,
'Content-Length': body.length,
});
}
response.send(body);
},
};
export function _getAdapter(unencodedPath, config) {
if ('getComplianceClasses' in config) {
return { adapter: config, baseUrl: '/' };
}
else {
const keys = Object.keys(config).sort((a, b) => b.length - a.length);
const key = keys.find((key) => (unencodedPath || '/').startsWith(key));
if (!key) {
throw new Error(`Adapter not found for route "${unencodedPath}". You should always have an adapter for the root path "/".`);
}
return { adapter: config[key], baseUrl: key };
}
}
export async function getAdapter(unencodedPath, config, environment) {
let adapter = _getAdapter(unencodedPath, config);
let plugins = environment.plugins;
if (plugins == null) {
const parsedPlugins = getPlugins(unencodedPath, environment.response.locals.pluginsConfig);
plugins = parsedPlugins.plugins;
const baseUrl = new URL(path.join(environment.request.baseUrl || '/', parsedPlugins.baseUrl), `${environment.request.protocol}://${environment.request.headers.host}`);
plugins.forEach((plugin) => (plugin.baseUrl = baseUrl));
}
for (let plugin of plugins) {
if ('prepareAdapter' in plugin && plugin.prepareAdapter) {
const result = await plugin.prepareAdapter(environment.request, environment.response, adapter.adapter);
if (result != null) {
adapter.adapter = result;
}
}
}
return adapter;
}
export function getAuthenticator(unencodedPath, config) {
if ('authenticate' in config) {
return config;
}
else {
const keys = Object.keys(config).sort((a, b) => b.length - a.length);
const key = keys.find((key) => (unencodedPath || '/').startsWith(key));
if (!key) {
throw new Error(`Authenticator not found for route "${unencodedPath}". You should always have an authenticator for the root path "/".`);
}
return config[key];
}
}
export function getPlugins(unencodedPath, config) {
if (Array.isArray(config)) {
return { plugins: config, baseUrl: '/' };
}
else if (config != null) {
const keys = Object.keys(config).sort((a, b) => b.length - a.length);
const key = keys.find((key) => (unencodedPath || '/').startsWith(key));
if (!key) {
return { plugins: [], baseUrl: '/' };
}
else {
return { plugins: config[key], baseUrl: key };
}
}
else {
return { plugins: [], baseUrl: '/' };
}
}
//# sourceMappingURL=Options.js.map