instaserve 1.0.20 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +1 -48
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,49 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import fs from 'node:fs'
|
|
4
|
-
import { pathToFileURL } from 'node:url'
|
|
5
|
-
import { resolve } from 'node:path'
|
|
6
|
-
|
|
7
|
-
const routesfile = resolve('routes.mjs')
|
|
8
|
-
const routesurl = pathToFileURL(routesfile).href
|
|
9
|
-
console.log(routesfile, routesurl)
|
|
10
|
-
|
|
11
|
-
if (!fs.existsSync(routesfile)) {
|
|
12
|
-
fs.writeFileSync(routesfile, `export default {
|
|
13
|
-
_debug: ({ r, s, db }) => console.log(r.url, r.method),
|
|
14
|
-
'/': ({ s }) => s.endJSON({ message: 'hello index' }),
|
|
15
|
-
'tables': name => ([k, v]) => k.match(name + ':')
|
|
16
|
-
}`)
|
|
17
|
-
}
|
|
18
|
-
const routes = (await import(routesurl)).default
|
|
19
|
-
routes['/sw.js'] = e => {
|
|
20
|
-
e.s.writeHead(201, { 'Content-Type': 'application/javascript' })
|
|
21
|
-
e.s.end('self.addEventListener("fetch",e=>{e.respondWith(caches.open("e").then(function(t){return t.match(e.request).then(function(n){return n||fetch(e.request).then(function(n){return e.request.url.match(/\.js$|\.css$/)&&t.put(e.request,n.clone()),n})})}))});')
|
|
22
|
-
}
|
|
23
|
-
const VastDB = (await import(`./vastdb.mjs`)).default
|
|
24
|
-
const db = new VastDB(routes)
|
|
25
|
-
console.log(db.filename, routes)
|
|
26
|
-
|
|
27
|
-
http
|
|
28
|
-
.createServer(async (r, s) => {
|
|
29
|
-
try {
|
|
30
|
-
let data = '';
|
|
31
|
-
r.on('data', (s) => (data += s.toString()));
|
|
32
|
-
r.on('end', (x) => {
|
|
33
|
-
try {
|
|
34
|
-
data = JSON.parse(data);
|
|
35
|
-
} catch { }
|
|
36
|
-
});
|
|
37
|
-
s.endJSON = o => s.end(JSON.stringify(o))
|
|
38
|
-
const midware = Object.keys(routes)
|
|
39
|
-
.filter((k) => k.startsWith('_'))
|
|
40
|
-
.map((k) => routes[k]({ r, s, data, db }));
|
|
41
|
-
if (midware.includes(true)) return;
|
|
42
|
-
if (routes[r.url]) return routes[r.url]({ r, s, data, db });
|
|
43
|
-
else s.writeHead(404).end()
|
|
44
|
-
} catch (e) {
|
|
45
|
-
console.log(e);
|
|
46
|
-
s.writeHead(404).end();
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
.listen(3000, (x) => console.log('listening on 3000'));
|
|
2
|
+
console.log(1)
|
package/package.json
CHANGED