-
Notifications
You must be signed in to change notification settings - Fork 3
/
testserver-home.mjs
56 lines (50 loc) · 1.63 KB
/
testserver-home.mjs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* This file requires the --experimental-specifier-resolution=node option.
*
* sudo node --experimental-specifier-resolution=node testserver-home.mjs
*/
import { hostname } from 'node:os';
import express from 'express';
import createDebug from 'debug';
import { homedir as userHomePath } from 'userhomepath';
import server from './packages/nephele/dist/index.js';
import FileSystemAdapter from './packages/adapter-file-system/dist/index.js';
import VirtualAdapter from './packages/adapter-virtual/dist/index.js';
import PamAuthenticator from './packages/authenticator-pam/dist/index.js';
const debug = createDebug('nephele:testserver-home');
const app = express();
const host = hostname();
const port = 8080;
const instanceDate = new Date();
app.use(
'/',
server({
adapter: async (_request, response) => {
if (response.locals.user == null) {
return new VirtualAdapter({
files: {
properties: {
creationdate: instanceDate,
getlastmodified: instanceDate,
owner: 'root',
},
locks: {},
children: [],
},
});
}
try {
const homedir = await userHomePath(response.locals.user.username);
return new FileSystemAdapter({ root: homedir });
} catch (e) {
throw new Error("Couldn't mount user directory as server root.");
}
},
authenticator: new PamAuthenticator(),
}),
);
app.listen(port, () => {
debug(`Listening on ${host}:${port}.`);
debug(`Serving files from user home directories.`);
console.log(`Example Nephele WebDAV server listening on port ${port}`);
});