instaserve 0.0.18 → 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 +2 -4
- package/db.json +0 -1
- package/routes.mjs +0 -5
- package/vastdb.mjs +0 -42
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
package/db.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[["user:admin",{"type":"user","name":"admin","pass":"01b613da484bee91c3f3806b52a6f40fd61ade874b5ffc0f62a2091cce38158b","id":"user:admin"}]]
|
package/routes.mjs
DELETED
package/vastdb.mjs
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs'
|
|
2
|
-
export default class VastDB extends Map {
|
|
3
|
-
constructor(v) {
|
|
4
|
-
super();
|
|
5
|
-
this.filename = `${process.cwd()}/db.json`;
|
|
6
|
-
this.views = v
|
|
7
|
-
if (!fs.existsSync(this.filename)) {
|
|
8
|
-
this.set({
|
|
9
|
-
type: 'user',
|
|
10
|
-
name: 'admin',
|
|
11
|
-
pass: '01b613da484bee91c3f3806b52a6f40fd61ade874b5ffc0f62a2091cce38158b',
|
|
12
|
-
});
|
|
13
|
-
console.log('creating db');
|
|
14
|
-
} else {
|
|
15
|
-
const sf = JSON.parse(fs.readFileSync(this.filename, 'utf8'));
|
|
16
|
-
sf.map(([k, v]) => {
|
|
17
|
-
v.id = k;
|
|
18
|
-
this.set(v);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
query(q = () => false) {
|
|
23
|
-
return [...this].filter(q).map(([k, v]) => v)
|
|
24
|
-
}
|
|
25
|
-
getView(name, param) {
|
|
26
|
-
const res = [...this].filter(this.views[name](param));
|
|
27
|
-
return res.map(([k, v]) => v);
|
|
28
|
-
}
|
|
29
|
-
set(arr) {
|
|
30
|
-
if (!arr.push) arr = [arr];
|
|
31
|
-
for (const o of arr) {
|
|
32
|
-
if (!o.name || !o.type) return false;
|
|
33
|
-
if (!o.id) o.id = o.type + ':' + o.name;
|
|
34
|
-
}
|
|
35
|
-
arr.map((o) => super.set(o.id, o));
|
|
36
|
-
if(this.event) this.event(arr)
|
|
37
|
-
this.save();
|
|
38
|
-
}
|
|
39
|
-
save() {
|
|
40
|
-
fs.writeFileSync(this.filename, JSON.stringify([...this.entries()]));
|
|
41
|
-
}
|
|
42
|
-
}
|