-
Notifications
You must be signed in to change notification settings - Fork 6
/
datfox-helper.js
executable file
·58 lines (55 loc) · 1.72 KB
/
datfox-helper.js
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
57
58
#!/usr/bin/env node
const path = require('path');
const browser = require('./src/browser');
const datApi = require('./src/api');
const DatGateway = require('./src/gateway');
const Library = require('./src/library');
const version = require('./package.json').version;
const libraryDir = path.join(process.cwd(), 'library');
const library = new Library(libraryDir);
library.init();
const gateway = new DatGateway(library);
const handlers = {
supportedActions: () => Promise.resolve(Object.keys(handlers)),
getVersion: () => Promise.resolve(version),
listLibrary: () => library.listLibrary(),
getOpenArchives: () => Promise.resolve(library.getOpenArchives()),
removeFromLibrary: ({ url }) => library.remove(url),
closeArchive: ({ url }) => library.close(url),
};
// collect available APIs
Object.assign(handlers, datApi({
getArchive: library.getArchive.bind(library),
createArchive: library.createArchive.bind(library),
forkArchive: library.forkArchive.bind(library),
}), {
startGateway: ({ port=3000 }) => {
return gateway.listen(port);
},
});
// make API available over native messaging via stdio
browser.onMessage.addListener((message) => {
const { id, action } = message;
if (handlers[action]) {
handlers[action](message).then((result) => {
browser.postMessage({
id,
action,
result,
});
}, (error) => {
browser.postMessage({
id,
action,
error: error.toString(),
});
});
} else {
browser.postMessage({
id,
action,
error: 'unhandled_message',
message,
});
}
});