-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-ics.js
More file actions
64 lines (56 loc) · 1.67 KB
/
server-ics.js
File metadata and controls
64 lines (56 loc) · 1.67 KB
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
59
60
61
62
63
64
const { curry } = require('ramda')
const IC = require('ic-js')
const { ADMIN, ADMIN_HOME, PARTY_MODE } = process.env
// VERY basic cacheing of server index.ic
// this is not meant to scale
const serverIcs = {}
class ServerIcs {
constructor (fileSystem) {
if (!fileSystem) throw new Error('fileSystem is required')
this.fileSystem = fileSystem
this.serverIcs = {}
}
clearServerIc (filePrefix) {
delete this.serverIcs[filePrefix]
}
clearServerAdminIc () {
this.clearServerIc(ADMIN_HOME)
}
async getServerAdminIc () {
if (!ADMIN || !ADMIN_HOME) return
return this.serverIc(ADMIN_HOME)
}
async serverIc (filePrefix, icOpts = {}) {
const optsKeys = Object.keys(icOpts)
if (this.serverIcs[filePrefix] && optsKeys.length === 0) return this.serverIcs[filePrefix]
const files = await this.fileSystem.readDir(filePrefix + '/')
const allFiles = await Promise.all(files.map(file => {
return this.fileSystem.readFile(`${filePrefix}/${file}/index.ic`)
.then(str => {
if (!str) return null
if (str.startsWith('_\n')) return str.replace(/^_/, `_${file}`)
return `_${file}\n${str}`
})
}))
const icStr = allFiles
.filter(Boolean)
.join('\n')
const ic = new IC(icOpts)
ic.created = Date.now()
await ic.import(icStr)
if (optsKeys.length > 0) return ic
this.serverIcs[filePrefix] = ic
return ic
}
async getDomainAdmin (host) {
const adminIc = await this.getServerAdminIc()
if (adminIc) {
const admin = adminIc.findTagged(['admin', host])
if (admin[0]){
return admin[0]
}
}
return ADMIN
}
}
module.exports = ServerIcs