instaserve 0.0.27 → 0.0.28

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,11 @@
1
+ [
2
+ [
3
+ "user:admin",
4
+ {
5
+ "type": "user",
6
+ "name": "admin",
7
+ "pass": "01b613da484bee91c3f3806b52a6f40fd61ade874b5ffc0f62a2091cce38158b",
8
+ "id": "user:admin"
9
+ }
10
+ ]
11
+ ]
package/index.mjs CHANGED
@@ -1,62 +1,64 @@
1
- #!/usr/bin/env node
2
- import http from 'node:http'
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
- const indexfile = resolve('index.html')
10
-
11
- console.log(routesfile, routesurl)
12
-
13
- if (!fs.existsSync(routesfile)) {
14
- fs.writeFileSync(routesfile, `import fs from 'node:fs'
15
- export default {
16
- _debug: ({ r, s, db }) => console.log(r.url, r.method),
17
- '/': ({ s }) => s.end(fs.readFileSync('index.html', 'utf-8')),
18
- 'tables': name => ([k, v]) => k.match(name + ':')
19
- }`)
20
- }
21
- if (!fs.existsSync(indexfile)) {
22
- fs.writeFileSync(indexfile, `<!DOCTYPE html>
23
-
24
- <script>navigator.serviceWorker.register('sw.js')</script>
25
- <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css'>
26
- <script src='https://unpkg.com/enigmatic/enigmatic.js'></script>
27
-
28
- <div class='bg-yellow-200'>hello</div>`)
29
- }
30
-
31
- const routes = (await import(routesurl)).default
32
- routes['/sw.js'] = e => {
33
- e.s.writeHead(201, { 'Content-Type': 'application/javascript' })
34
- 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})})}))});')
35
- }
36
- const VastDB = (await import(`./vastdb.mjs`)).default
37
- const db = new VastDB(routes)
38
- console.log(db.filename, routes)
39
-
40
- http
41
- .createServer(async (r, s) => {
42
- try {
43
- let data = '';
44
- r.on('data', (s) => (data += s.toString()));
45
- r.on('end', (x) => {
46
- try {
47
- data = JSON.parse(data);
48
- } catch { }
49
- });
50
- s.endJSON = o => s.end(JSON.stringify(o))
51
- const midware = Object.keys(routes)
52
- .filter((k) => k.startsWith('_'))
53
- .map((k) => routes[k]({ r, s, data, db }));
54
- if (midware.includes(true)) return;
55
- if (routes[r.url]) return routes[r.url]({ r, s, data, db });
56
- else s.writeHead(404).end()
57
- } catch (e) {
58
- console.log(e);
59
- s.writeHead(404).end();
60
- }
61
- })
62
- .listen(3000, (x) => console.log('listening on 3000'));
1
+ #!/usr/bin/env node
2
+ import http from 'node:http'
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
+ const indexfile = resolve('index.html')
10
+
11
+ console.log(routesfile, routesurl)
12
+
13
+ if (!fs.existsSync(routesfile)) {
14
+ fs.writeFileSync(routesfile, `import fs from 'node:fs'
15
+ export default {
16
+ _debug: ({ r, s, db }) => console.log(r.url, r.method),
17
+ '/': e => e.s.end(fs.readFileSync('index.html', 'utf-8')),
18
+ '/post': ({ e, body }) => db.set(body),
19
+ '/tables': ({s}) => s.endJSON(db.getView(tables, 'user)),
20
+ 'tables': name => ([k, v]) => k.match(name + ':')
21
+ }`)
22
+ }
23
+ if (!fs.existsSync(indexfile)) {
24
+ fs.writeFileSync(indexfile, `<!DOCTYPE html>
25
+
26
+ <script>navigator.serviceWorker.register('sw.js')</script>
27
+ <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css'>
28
+ <script src='https://unpkg.com/enigmatic/enigmatic.js'></script>
29
+
30
+ <div class='bg-yellow-200'>hello</div>`)
31
+ }
32
+
33
+ const routes = (await import(routesurl)).default
34
+ routes['/sw.js'] = e => {
35
+ e.s.writeHead(201, { 'Content-Type': 'application/javascript' })
36
+ 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})})}))});')
37
+ }
38
+ const VastDB = (await import(`./vastdb.mjs`)).default
39
+ const db = new VastDB(routes)
40
+ console.log(db.filename, routes)
41
+
42
+ http
43
+ .createServer(async (r, s) => {
44
+ try {
45
+ let data = '';
46
+ r.on('data', (s) => (data += s.toString()));
47
+ r.on('end', (x) => {
48
+ try {
49
+ data = JSON.parse(data);
50
+ } catch { }
51
+ });
52
+ s.endJSON = o => s.end(JSON.stringify(o))
53
+ const midware = Object.keys(routes)
54
+ .filter((k) => k.startsWith('_'))
55
+ .map((k) => routes[k]({ r, s, data, db }));
56
+ if (midware.includes(true)) return;
57
+ if (routes[r.url]) return routes[r.url]({ r, s, data, db });
58
+ else s.writeHead(404).end()
59
+ } catch (e) {
60
+ console.log(e);
61
+ s.writeHead(404).end();
62
+ }
63
+ })
64
+ .listen(3000, (x) => console.log('listening on 3000'));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
- {
2
- "name": "instaserve",
3
- "version": "0.0.27",
4
- "description": "",
5
- "main": "index.mjs",
6
- "bin": "./index.mjs"
1
+ {
2
+ "name": "instaserve",
3
+ "version": "0.0.28",
4
+ "description": "",
5
+ "main": "index.mjs",
6
+ "bin": "./index.mjs"
7
7
  }
package/test.mjs ADDED
@@ -0,0 +1,14 @@
1
+ import Vast from './vastdb.mjs'
2
+ const db = new Vast()
3
+
4
+ db.views = {
5
+ tables: ([k, v]) => k.match('user')
6
+ }
7
+
8
+ db.triggers = [
9
+ record => record.value = 100
10
+ ]
11
+
12
+ //console.log(db.getView('tables'))
13
+ db.set({name: 'testrec', value: 0})
14
+ console.log(db)
package/vastdb.mjs CHANGED
@@ -1,42 +1,43 @@
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
- }
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
+ this.triggers = []
8
+ if (!fs.existsSync(this.filename)) {
9
+ this.set({
10
+ type: 'user',
11
+ name: 'admin',
12
+ pass: '01b613da484bee91c3f3806b52a6f40fd61ade874b5ffc0f62a2091cce38158b',
13
+ });
14
+ console.log('creating db');
15
+ } else {
16
+ const sf = JSON.parse(fs.readFileSync(this.filename, 'utf8'));
17
+ sf.map(([k, v]) => {
18
+ v.id = k;
19
+ this.set(v);
20
+ });
21
+ }
22
+ }
23
+ query(q = () => false) {
24
+ return [...this].filter(q).map(([k, v]) => v)
25
+ }
26
+ getView(name) {
27
+ if(!this.views[name]) throw `view ${name} does not exist`
28
+ return this.query(this.views[name])
29
+ }
30
+ set(arr) {
31
+ if (!arr.push) arr = [arr];
32
+ for (const o of arr) {
33
+ if (!o.name || !o.type) return false;
34
+ if (!o.id) o.id = o.type + ':' + o.name;
35
+ this.triggers.map(t => o = t(o))
36
+ }
37
+ arr.map((o) => super.set(o.id, o));
38
+ this.save();
39
+ }
40
+ save() {
41
+ fs.writeFileSync(this.filename, JSON.stringify([...this.entries()], null, 2));
42
+ }
43
+ }