-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.js
120 lines (104 loc) · 3.52 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Colors, hyper } from './deps.js'
import { create as createHyperDir, DIR } from './dir.js'
import * as Log from './log.js'
import config from './hyper.config.js'
const rgb = (code) => (text) => Colors.rgb24(text, code)
const style = {
storage: ['🗃', rgb(0x1286F6)],
cache: ['⭐️', rgb(0xBA46DF)],
data: ['💿', rgb(0x2AB52E)],
search: ['🔎', rgb(0xFE7621)],
queue: ['🗂', rgb(0xFC4059)],
}
export async function create({ domain, type, body = {} }) {
const port = Deno.env.get('PORT') || 6363
return await fetch(`http://localhost:${port}/${type}/${domain}`, {
method: 'PUT',
body: JSON.stringify(body),
}).then(async (res) =>
res.ok || res.status === 409
? Promise.resolve(await res.json())
: Promise.reject(await res.json())
)
}
export async function destroy({ domain, type }) {
const port = Deno.env.get('PORT') || 6363
return await fetch(`http://localhost:${port}/${type}/${domain}`, {
method: 'DELETE',
}).then(async (res) =>
res.ok || res.status === 404 // cast not found to success
? Promise.resolve(await res.json())
: Promise.reject(await res.json())
)
}
/**
* Start the hyper nano instance listening on port 6363 or PORT env variable,
* and optionally bootstrap services. By default, no bootstrapping is done.
* you can then consume your hyper instance using HTTP or `hyper-connect` (recommended)
*
* pass `http://localhost:[port]/[domain] to `hyper-connect` as your connection string
*
* @typedef {Object} Services
* @property {boolean} data
* @property {boolean} cache
* @property {boolean} storage
*
* @typedef {Object} MainArgs
* @property {string} [domain] - the name of the domain. defaults to `'test'`
* @property {boolean} [experimental] - must be `true` if using experimental features. defaults to `false`
* @property {Services} [purge] - which services to purge prior to creation. defaults to {} which is a noop
* @property {Services} [services] - which services to create. defaults to {} which is a noop
*
* @param {MainArgs} [args] - defaults to {}
* @returns {Promise<app>} - the opine app listening of port 6363 or PORT env variable
*/
export async function main(args = {}) {
const {
domain = 'test',
purge = {},
services = {},
experimental = false,
middleware = [],
} = args
Log.info(`storing hyper service data in ${DIR} 🗃`)
await createHyperDir()
Log.info(`starting hyper nano ⚡️`)
const app = await hyper({ ...config, middleware }).then((app) =>
new Promise((resolve) => setTimeout(() => resolve(app), 1000))
)
const doExperimental = [
...Object.values(purge),
...Object.values(services),
].reduce((acc, cur) => acc || cur, false)
if (doExperimental) {
if (!experimental) {
Log.error('the --experimental flag must be set')
return Deno.exit(1)
}
Log.warn(
`Using experimental features. These features can change at any time.`,
)
await Promise.all(
Object.keys(services).map(async (type) => {
// maybe purge
if (purge[type]) {
await destroy({ domain, type })
Log.info(
`Purged ${style[type][1](type)} ${style[type][0]} service in ${
Colors.green(domain)
} domain ✅`,
)
}
if (services[type]) {
await create({ domain, type })
Log.info(
`Created ${style[type][1](type)} ${style[type][0]} service in ${
Colors.green(domain)
} domain ⚡️`,
)
}
}),
)
}
return app
}