instaserve 0.1.1 → 0.1.5

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/README.md CHANGED
@@ -1,8 +1,40 @@
1
1
  # instaserve
2
2
  Instant web stack
3
3
 
4
- routes.mjs (created by default)
5
-
6
4
  > npx instaserve (node)
5
+ Starts a server in the current directory
6
+ Creates a routes.mjs file if none exists
7
+ Create a public folder and add files for static file serving
8
+
7
9
  > npm run deno (deno)
8
- > npm run bun (bun)
10
+ Starts a deno server using routes.mjs and static serving
11
+
12
+ > npm run bun (bun)
13
+ Starts a bun server
14
+
15
+ > debug=true npx instaserve
16
+ Show more request info
17
+
18
+ ###Script usage
19
+ ````
20
+ import serve from 'instaserve'
21
+ serve({
22
+
23
+ // routes prefixed with _ run on every request
24
+
25
+ _log: (r, s) => console.log(r.method, r.url),
26
+ _example: (r, s) => console.log('returning a falsy value (above) will stop processing'),
27
+
28
+ /api: (r, s, body) => s.end('an api response'),
29
+
30
+ }, port) // port is optional (3000)
31
+ ````
32
+
33
+ ###Routes.mjs file example
34
+ ````
35
+ export default {
36
+ _debug: ({ method, url }, s, data) => !console.log(method, url, data),
37
+ _example: (r, s, data) => console.log('returning a falsy value (above) will stop the chain'),
38
+ '/api': (r, s, data) => s.end('an example api response')
39
+ }
40
+ ````
package/module.mjs CHANGED
@@ -1,31 +1,37 @@
1
1
  import http from 'node:http'
2
2
  import fs from 'node:fs'
3
+ const debug = process.env.debug
3
4
 
4
- export default function(routes = {_debug: ({method, url}, s) => console.log(method, url)}, port = 3000) {
5
- http.createServer(async (r, s) => {
6
- try {
7
- let data = ''
8
- r.on('data', (s) => (data += s.toString()))
9
- r.on('end', (x) => {
10
- try {
11
- data = JSON.parse(data)
12
- } catch { }
13
- })
14
- const midware = Object.keys(routes)
15
- .filter((k) => k.startsWith('_'))
16
- .find((k) => routes[k](r, s, data))
17
- if (r.url == '/') r.url = '/index.html'
18
- const fn = `./public${r.url.replace('..', '')}`
19
- if (fs.existsSync(fn)) {
20
- if (fn.match(/sw\.js/)) s.writeHead(200, { 'Content-Type': 'application/javascript' })
21
- return s.end(fs.readFileSync(fn, 'utf-8'))
5
+ export default function (routes = { _debug: ({ method, url }, s) => console.log(method, url) }, port = 3000) {
6
+
7
+ const server = http.createServer(async (r, s) => {
8
+ let data = ''
9
+ r.on('data', (s) => data += s.toString().trim())
10
+ r.on('end', (x) => {
11
+ try {
12
+ if (debug) console.log(`parsing data: "${data}"`)
13
+ if(data) data = JSON.parse(data)
14
+ const midware = Object.keys(routes)
15
+ .filter((k) => k.startsWith('_'))
16
+ .find((k) => routes[k](r, s, data))
17
+ if (r.url == '/') r.url = '/index.html'
18
+ const fn = `./public${r.url.replace('..', '')}`
19
+ if (fs.existsSync(fn)) {
20
+ if (fn.match(/sw\.js/)) s.writeHead(200, { 'Content-Type': 'application/javascript' })
21
+ return s.end(fs.readFileSync(fn, 'utf-8'))
22
+ }
23
+ const url = '/' + r.url.split('/')[1]
24
+ if (routes[url]) return routes[url](r, s, data)
25
+ throw Error(r.url + ' not found')
26
+ } catch (e) {
27
+ console.log(e)
28
+ s.writeHead(404).end()
22
29
  }
23
- if (routes[r.url]) return routes[r.url](r, s, data)
24
- else s.writeHead(404).end()
25
- } catch (e) {
26
- console.log(e)
27
- s.writeHead(404).end()
28
- }
29
- })
30
- .listen(port, (x) => console.log('listening on ' + port))
31
- }
30
+ })
31
+ }).listen(port)
32
+
33
+ return {
34
+ stop: () => { server.close(); return true }
35
+ }
36
+
37
+ }
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
- "name": "instaserve",
3
- "version": "0.1.1",
4
- "description": "Instant web stack",
5
- "main": "server.mjs",
6
- "bin": "./server.mjs",
7
- "scripts": {
8
- "start": "node server.mjs",
9
- "deno": "deno run --unstable --allow-net --allow-read deno/server.js",
10
- "bun": "bun run bun/http.js"
11
- }
2
+ "name": "instaserve",
3
+ "version": "0.1.5",
4
+ "description": "Instant web stack",
5
+ "main": "module.mjs",
6
+ "bin": "./server.mjs",
7
+ "scripts": {
8
+ "start": "node server.mjs",
9
+ "deno": "deno run --unstable --allow-net --allow-read deno/server.js",
10
+ "bun": "bun run bun/http.js",
11
+ "test": "node --no-warnings test.mjs"
12
+ },
13
+ "dependencies": {
14
+ "instax": "^0.1.2"
15
+ }
12
16
  }
package/routes.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  export default {
2
- _debug: ({ method, url }, s, data) => !console.log(method, url, data),
3
- _example: (r, s, data) => console.log('returning a falsy value (above) will stop the chain'),
4
- '/api': (r, s, data) => s.end('an example api response')
5
- }
2
+ _debug: ({method, url}, s) => !console.log(method, url),
3
+ _example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
4
+ '/api': (r, s) => s.end('an example api response')
5
+ }
package/server.mjs CHANGED
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import http from 'node:http'
3
+ import server from './module.mjs'
4
4
  import { pathToFileURL } from 'node:url'
5
5
  import { resolve } from 'node:path'
6
6
  import fs from 'node:fs'
7
-
8
7
  const routesfile = resolve('routes.mjs')
9
8
 
10
9
  if (!fs.existsSync(routesfile)) {
@@ -18,31 +17,4 @@ if (!fs.existsSync(routesfile)) {
18
17
  const routesurl = pathToFileURL(routesfile).href
19
18
  console.log(routesfile, routesurl)
20
19
  const routes = (await import(routesurl)).default
21
-
22
- http
23
- .createServer(async (r, s) => {
24
- try {
25
- let data = ''
26
- r.on('data', (s) => (data += s.toString()))
27
- r.on('end', (x) => {
28
- try {
29
- data = JSON.parse(data)
30
- } catch {}
31
- });
32
- const midware = Object.keys(routes)
33
- .filter((k) => k.startsWith('_'))
34
- .find((k) => routes[k](r, s, data));
35
- if (r.url == '/') r.url = '/index.html'
36
- const fn = `./public${r.url.replace('..', '')}`
37
- if (fs.existsSync(fn)) {
38
- if (fn.match(/sw\.js/)) s.writeHead(200, { 'Content-Type': 'application/javascript' })
39
- return s.end(fs.readFileSync(fn, 'utf-8'))
40
- }
41
- if (routes[r.url]) return routes[r.url](r, s, data)
42
- else s.writeHead(404).end()
43
- } catch (e) {
44
- console.log(e)
45
- s.writeHead(404).end()
46
- }
47
- })
48
- .listen(3000, (x) => console.log('listening on 3000'))
20
+ server(routes)
package/test.mjs ADDED
@@ -0,0 +1,12 @@
1
+ import serve from './module.mjs'
2
+ import { get, te } from 'instax'
3
+
4
+ const server = serve({
5
+ '/api': (r, s) => s.end('Hello!')
6
+ }, 3001)
7
+
8
+ const resp = await get('http://localhost:3001/api')
9
+ te(resp, 'Hello!')
10
+
11
+ te(server.stop(), true)
12
+ console.log('closing server')
package/testmodule.mjs DELETED
@@ -1,2 +0,0 @@
1
- import server from './module.mjs'
2
- server()