instaserve 0.0.13 → 0.0.16

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 CHANGED
@@ -1,41 +1,47 @@
1
- import http from 'node:http'
2
- import fs from 'node:fs'
3
- if (!fs.existsSync(process.cwd() + '/routes.mjs')) {
4
- fs.writeFileSync(process.cwd() + '/routes.mjs', `export default {
5
- _debug: ({ r, s, db }) => console.log(r.url, r.method),
6
- '/': ({ s }) => s.endJSON({ message: 'hello index' }),
7
- 'tables': name => ([k, v]) => k.match(name + ':')
8
- }`)
9
- }
10
- const routes = (await import(`${process.cwd()}/routes.mjs`)).default
11
- routes['/sw.js'] = e => {
12
- e.s.writeHead(201, { 'Content-Type': 'application/javascript' })
13
- 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})})}))});')
14
- }
15
- const VastDB = (await import(`./vastdb.mjs`)).default
16
- const db = new VastDB(routes)
17
- console.log(db.filename, routes)
18
-
19
- http
20
- .createServer(async (r, s) => {
21
- try {
22
- let data = '';
23
- r.on('data', (s) => (data += s.toString()));
24
- r.on('end', (x) => {
25
- try {
26
- data = JSON.parse(data);
27
- } catch { }
28
- });
29
- s.endJSON = o => s.end(JSON.stringify(o))
30
- const midware = Object.keys(routes)
31
- .filter((k) => k.startsWith('_'))
32
- .map((k) => routes[k]({ r, s, data, db }));
33
- if (midware.includes(true)) return;
34
- if (routes[r.url]) return routes[r.url]({ r, s, data, db });
35
- else s.writeHead(404).end()
36
- } catch (e) {
37
- console.log(e);
38
- s.writeHead(404).end();
39
- }
40
- })
41
- .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, fileURLToPath} from 'node:url'
5
+
6
+ const routesfile = process.cwd() + '/routes.mjs'
7
+ const routesurl = pathToFileURL(routesfile).href
8
+
9
+ if (!fs.existsSync(routesfile)) {
10
+ fs.writeFileSync(routesfile, `export default {
11
+ _debug: ({ r, s, db }) => console.log(r.url, r.method),
12
+ '/': ({ s }) => s.endJSON({ message: 'hello index' }),
13
+ 'tables': name => ([k, v]) => k.match(name + ':')
14
+ }`)
15
+ }
16
+ const routes = (await import(routesurl)).default
17
+ routes['/sw.js'] = e => {
18
+ e.s.writeHead(201, { 'Content-Type': 'application/javascript' })
19
+ 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})})}))});')
20
+ }
21
+ const VastDB = (await import(`./vastdb.mjs`)).default
22
+ const db = new VastDB(routes)
23
+ console.log(db.filename, routes)
24
+
25
+ http
26
+ .createServer(async (r, s) => {
27
+ try {
28
+ let data = '';
29
+ r.on('data', (s) => (data += s.toString()));
30
+ r.on('end', (x) => {
31
+ try {
32
+ data = JSON.parse(data);
33
+ } catch { }
34
+ });
35
+ s.endJSON = o => s.end(JSON.stringify(o))
36
+ const midware = Object.keys(routes)
37
+ .filter((k) => k.startsWith('_'))
38
+ .map((k) => routes[k]({ r, s, data, db }));
39
+ if (midware.includes(true)) return;
40
+ if (routes[r.url]) return routes[r.url]({ r, s, data, db });
41
+ else s.writeHead(404).end()
42
+ } catch (e) {
43
+ console.log(e);
44
+ s.writeHead(404).end();
45
+ }
46
+ })
47
+ .listen(3000, (x) => console.log('listening on 3000'));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
- {
2
- "name": "instaserve",
3
- "version": "0.0.13",
4
- "description": "",
5
- "main": "index.mjs",
6
- "bin": "./index.mjs"
1
+ {
2
+ "name": "instaserve",
3
+ "version": "0.0.16",
4
+ "description": "",
5
+ "main": "index.mjs",
6
+ "bin": "./index.mjs"
7
7
  }
package/routes.mjs CHANGED
@@ -1,5 +1,5 @@
1
- export default {
2
- _debug: ({ r, s, db }) => console.log(r.url, r.method),
3
- '/': ({ s }) => s.endJSON({ message: 'hello index' }),
4
- 'tables': name => ([k, v]) => k.match(`${name}:`)
1
+ export default {
2
+ _debug: ({ r, s, db }) => console.log(r.url, r.method),
3
+ '/': ({ s }) => s.endJSON({ message: 'hello index' }),
4
+ 'tables': name => ([k, v]) => k.match(`${name}:`)
5
5
  }
package/vastdb.mjs CHANGED
@@ -1,42 +1,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
- 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
+ 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
+ }