instaserve 0.0.1 → 0.0.4

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/db.json ADDED
@@ -0,0 +1 @@
1
+ [["user:admin",{"type":"user","name":"admin","pass":"01b613da484bee91c3f3806b52a6f40fd61ade874b5ffc0f62a2091cce38158b","id":"user:admin"}]]
package/index.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
  import http from 'node:http'
2
2
  const routes = (await import(`${process.cwd()}/routes.mjs`)).default
3
- console.log(routes)
3
+ const VastDB = (await import(`./vastdb.mjs`)).default
4
+ const db = new VastDB(routes)
5
+ console.log(db.filename, db.views)
4
6
 
5
7
  http
6
8
  .createServer(async (r, s) => {
@@ -12,11 +14,12 @@ http
12
14
  data = JSON.parse(data);
13
15
  } catch { }
14
16
  });
17
+ s.endJSON = o => s.end(JSON.stringify(o))
15
18
  const midware = Object.keys(routes)
16
19
  .filter((k) => k.startsWith('_'))
17
- .map((k) => routes[k]({ r, s, data }));
20
+ .map((k) => routes[k]({ r, s, data, db }));
18
21
  if (midware.includes(true)) return;
19
- if (routes[r.url]) return routes[r.url]({ r, s, data });
22
+ if (routes[r.url]) return routes[r.url]({ r, s, data, db });
20
23
  else s.writeHead(404).end()
21
24
  } catch (e) {
22
25
  console.log(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instaserve",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "index.mjs",
6
6
  "bin": "./index.mjs"
package/routes.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  export default {
2
- _debug: ({ r, s }) => console.log(r.url, r.method),
3
- '/': ({ s }) => s.end('hello index')
2
+ _debug: ({ r, s, db }) => console.log(db, r.method),
3
+ '/': ({ s }) => s.endJSON({message: 'hello index'}),
4
+ 'tables': name => ([k, v]) => k.match(`${name}:`)
4
5
  }
package/vastdb.mjs ADDED
@@ -0,0 +1,40 @@
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
+ getView(name, param) {
23
+ console.log(name, param)
24
+ const res = [...this].filter(this.views[name](param));
25
+ return res.map(([k, v]) => v);
26
+ }
27
+ set(arr) {
28
+ if (!arr.push) arr = [arr];
29
+ for (const o of arr) {
30
+ if (!o.name || !o.type) return false;
31
+ if (!o.id) o.id = o.type + ':' + o.name;
32
+ }
33
+ arr.map((o) => super.set(o.id, o));
34
+ if(this.event) this.event(arr)
35
+ this.save();
36
+ }
37
+ save() {
38
+ fs.writeFileSync(this.filename, JSON.stringify([...this.entries()]));
39
+ }
40
+ }