instaserve 0.1.19 → 0.1.20

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.
@@ -1,9 +1,9 @@
1
- # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/alpine/.devcontainer/base.Dockerfile
2
-
3
- # [Choice] Alpine version: 3.16, 3.15, 3.14, 3.13
4
- ARG VARIANT="3.16"
5
- FROM mcr.microsoft.com/vscode/devcontainers/base:0-alpine-${VARIANT}
6
-
7
- # ** [Optional] Uncomment this section to install additional packages. **
8
- # RUN apk update \
9
- # && apk add --no-cache <your-package-list-here>
1
+ # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/alpine/.devcontainer/base.Dockerfile
2
+
3
+ # [Choice] Alpine version: 3.16, 3.15, 3.14, 3.13
4
+ ARG VARIANT="3.16"
5
+ FROM mcr.microsoft.com/vscode/devcontainers/base:0-alpine-${VARIANT}
6
+
7
+ # ** [Optional] Uncomment this section to install additional packages. **
8
+ # RUN apk update \
9
+ # && apk add --no-cache <your-package-list-here>
@@ -1,22 +1,22 @@
1
- // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2
- // https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/alpine
3
- {
4
- "name": "Alpine-NodeJS",
5
- "build": {
6
- "dockerfile": "Dockerfile",
7
- // Update 'VARIANT' to pick an Alpine version: 3.13, 3.14, 3.15, 3.16
8
- "args": { "VARIANT": "3.16" }
9
- },
10
-
11
- // Use 'forwardPorts' to make a list of ports inside the container available locally.
12
- // "forwardPorts": [],
13
-
14
- // Use 'postCreateCommand' to run commands after the container is created.
15
- // "postCreateCommand": "uname -a",
16
-
17
- // Replace when using a ptrace-based debugger like C++, Go, and Rust
18
- // "runArgs": [ "--init", "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
19
-
20
- // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
21
- "remoteUser": "vscode"
22
- }
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2
+ // https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/alpine
3
+ {
4
+ "name": "Alpine-NodeJS",
5
+ "build": {
6
+ "dockerfile": "Dockerfile",
7
+ // Update 'VARIANT' to pick an Alpine version: 3.13, 3.14, 3.15, 3.16
8
+ "args": { "VARIANT": "3.16" }
9
+ },
10
+
11
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
12
+ // "forwardPorts": [],
13
+
14
+ // Use 'postCreateCommand' to run commands after the container is created.
15
+ // "postCreateCommand": "uname -a",
16
+
17
+ // Replace when using a ptrace-based debugger like C++, Go, and Rust
18
+ // "runArgs": [ "--init", "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
19
+
20
+ // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
21
+ "remoteUser": "vscode"
22
+ }
package/README.md CHANGED
@@ -1,44 +1,44 @@
1
- # instaserve
2
- Instant web stack
3
-
4
- In any folder:
5
-
6
- > npx instaserve
7
- Starts a server in the current directory
8
- Create a public folder and add files for static file serving
9
-
10
- > npx instaserve create
11
- Creates an example routes.mjs file if none exists
12
-
13
- > npm run deno (deno)
14
- Starts a deno server using routes.mjs and static serving
15
-
16
- > npm run bun (bun)
17
- Starts a bun server
18
-
19
- > port=8080 routes=myroutes.mjs npx instaserve
20
- Use custom port and routes file
21
-
22
- ###Script usage
23
- ````
24
- import serve from 'instaserve'
25
- serve({
26
-
27
- // routes prefixed with "_" run on every request
28
-
29
- _log: (r, s) => console.log(r.method, r.url),
30
- _example: (r, s) => console.log('returning a falsy value (above) will stop processing'),
31
-
32
- api: (r, s, body) => s.end('an api response'),
33
-
34
- }, port) // port is optional (3000)
35
- ````
36
-
37
- ###Routes.mjs file example
38
- ````
39
- export default {
40
- _debug: ({ method, url }, s, data) => !console.log(method, url, data),
41
- _example: (r, s, data) => console.log('returning a falsy value (above) will stop the chain'),
42
- api: (r, s, data) => s.end('an example api response')
43
- }
1
+ # instaserve
2
+ Instant web stack
3
+
4
+ In any folder:
5
+
6
+ > npx instaserve
7
+ Starts a server in the current directory
8
+ Create a public folder and add files for static file serving
9
+
10
+ > npx instaserve create
11
+ Creates an example routes.mjs file if none exists
12
+
13
+ > npm run deno (deno)
14
+ Starts a deno server using routes.mjs and static serving
15
+
16
+ > npm run bun (bun)
17
+ Starts a bun server
18
+
19
+ > port=8080 routes=myroutes.mjs npx instaserve
20
+ Use custom port and routes file
21
+
22
+ ###Script usage
23
+ ````
24
+ import serve from 'instaserve'
25
+ serve({
26
+
27
+ // routes prefixed with "_" run on every request
28
+
29
+ _log: (r, s) => console.log(r.method, r.url),
30
+ _example: (r, s) => console.log('returning a falsy value (above) will stop processing'),
31
+
32
+ api: (r, s, body) => s.end('an api response'),
33
+
34
+ }, port) // port is optional (3000)
35
+ ````
36
+
37
+ ###Routes.mjs file example - data is request body + query string
38
+ ````
39
+ export default {
40
+ _debug: ({ method, url }, s, data) => !console.log(method, url, data),
41
+ _example: (r, s, data) => console.log('returning a falsy value (above) will stop the chain'),
42
+ api: (r, s, data) => s.end('an example api response')
43
+ }
44
44
  ````
package/bun/http.js CHANGED
File without changes
package/deno/server.js CHANGED
File without changes
package/module.mjs CHANGED
@@ -1,54 +1,62 @@
1
- import http from 'node:http'
2
- import fs from 'node:fs'
3
- const debug = process.env.debug
4
-
5
- function public_file(r, s) {
6
- if (r.url == '/') r.url = '/index.html'
7
- const fn = `./public${r.url.replace('..', '')}`
8
- if (fs.existsSync(fn)) {
9
- if (fn.match(/.js$/)) s.writeHead(200, { 'Content-Type': 'application/javascript' })
10
- return fs.readFileSync(fn, 'utf-8')
11
- }
12
- }
13
-
14
- export default function (routes, port = 3000, ip = '127.0.0.1') {
15
- const server = http.createServer(async (r, s) => {
16
- let data = ''
17
- r.on('data', (s) => data += s.toString().trim())
18
- r.on('end', (x) => {
19
- try {
20
- if (debug) console.log(`parsing data: "${data}"`)
21
- if (debug) console.log(`routes: "${JSON.stringify(routes)}"`)
22
- if (data) data = JSON.parse(data)
23
- const midware = Object.keys(routes)
24
- .filter((k) => k.startsWith('_'))
25
- .find((k) => routes[k](r, s, data))
26
-
27
- const fc = public_file(r, s)
28
- if(fc) return s.end(fc)
29
-
30
- const url = r.url.split('/')[1]
31
- if (routes[url]) {
32
- const resp = routes[url](r, s, data)
33
- if(debug) console.log(`route: ${url}, returned: ${JSON.stringify(resp)}`)
34
- return s.end(typeof resp === 'string' ? resp:JSON.stringify(resp))
35
- }
36
- throw Error(r.url + ' not found')
37
- } catch (e) {
38
- const err = JSON.stringify({error: e.message})
39
- console.log('Server error: ' + e)
40
- s.writeHead(404).end(err)
41
- }
42
- })
43
- }).listen(process.env.port || port, process.env.ip || ip)
44
-
45
- console.log(`started on: ${(process.env.ip || ip)}:${(process.env.port || port)}, using routes: ${Object.keys(routes)}`)
46
-
47
- return {
48
- routes: routes,
49
- port: port,
50
- server: server,
51
- stop: () => { server.close(); return true }
52
- }
53
-
54
- }
1
+ import http from 'node:http'
2
+ import fs from 'node:fs'
3
+
4
+ const debug = process.env.debug
5
+
6
+ function public_file(r, s) {
7
+ if (r.url == '/') r.url = '/index.html'
8
+ const fn = `./public${r.url.replace('..', '')}`
9
+ if (fs.existsSync(fn)) {
10
+ if (fn.match(/.js$/)) s.writeHead(200, { 'Content-Type': 'application/javascript' })
11
+ return fs.readFileSync(fn, 'utf-8')
12
+ }
13
+ }
14
+
15
+ export default function (routes, port = 3000, ip = '127.0.0.1') {
16
+ const server = http.createServer(async (r, s) => {
17
+ let data = ''
18
+ r.on('data', (s) => data += s.toString().trim())
19
+ r.on('end', (x) => {
20
+ try {
21
+ if (debug) console.log(`parsing data: "${data}"`)
22
+ if (debug) console.log(`routes: "${JSON.stringify(routes)}"`)
23
+ if (data) data = JSON.parse(data)
24
+
25
+ const midware = Object.keys(routes)
26
+ .filter((k) => k.startsWith('_'))
27
+ .find((k) => routes[k](r, s, data))
28
+
29
+ const fc = public_file(r, s)
30
+ if(fc) return s.end(fc)
31
+
32
+ if(r.url.match(/\?/)) {
33
+ const qs = r.url?.split('?')[1]
34
+ const o = JSON.parse('{"' + decodeURI(qs.replace(/&/g, "\",\"").replace(/=/g, "\":\"")) + '"}')
35
+ data = Object.assign(data || {}, o)
36
+ }
37
+
38
+ const url = r.url.split('/')[1].split('?')[0]
39
+ if (routes[url]) {
40
+ const resp = routes[url](r, s, data)
41
+ if(debug) console.log(`route: ${url}, returned: ${JSON.stringify(resp)}`)
42
+ return s.end(typeof resp === 'string' ? resp:JSON.stringify(resp))
43
+ }
44
+ throw Error(r.url + ' not found')
45
+ } catch (e) {
46
+ const err = JSON.stringify({error: e.message})
47
+ console.log('Server error: ' + e)
48
+ s.writeHead(404).end(err)
49
+ }
50
+ })
51
+ }).listen(process.env.port || port, process.env.ip || ip)
52
+
53
+ console.log(`started on: ${(process.env.ip || ip)}:${(process.env.port || port)}, using routes: ${Object.keys(routes)}`)
54
+
55
+ return {
56
+ routes: routes,
57
+ port: port,
58
+ server: server,
59
+ stop: () => { server.close(); return true }
60
+ }
61
+
62
+ }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
- {
2
- "name": "instaserve",
3
- "version": "0.1.19",
4
- "description": "Instant web stack",
5
- "main": "module.mjs",
6
- "bin": "./server.mjs",
7
- "scripts": {
8
- "start": "node server.mjs",
9
- "deno": "deno run --allow-env --unstable --allow-net --allow-read deno/server.js",
10
- "bun": "bun run bun/http.js",
11
- "test": "node --no-warnings test.mjs"
12
- }
13
- }
1
+ {
2
+ "name": "instaserve",
3
+ "version": "0.1.20",
4
+ "description": "Instant web stack",
5
+ "main": "module.mjs",
6
+ "bin": "./server.mjs",
7
+ "scripts": {
8
+ "start": "node server.mjs",
9
+ "deno": "deno run --allow-env --unstable --allow-net --allow-read deno/server.js",
10
+ "bun": "bun run bun/http.js",
11
+ "test": "node --no-warnings test.mjs"
12
+ }
13
+ }
File without changes
package/public/index.html CHANGED
File without changes
package/public/sw.js CHANGED
File without changes
package/public/test.html CHANGED
File without changes
package/routes.mjs CHANGED
@@ -1,6 +1,9 @@
1
+ import url from 'node:url'
2
+
1
3
  export default {
2
4
  _debug: ({method, url}, s) => !console.log(method, url),
3
5
  _example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
4
6
  api: (r, s) => 'an example api response',
5
- testerror: () => { throw new Error('this from testerror') }
7
+ testerror: () => { throw new Error('this from testerror') },
8
+ testdata: (r, s, c, d) => c
6
9
  }
package/test.mjs CHANGED
@@ -1,48 +1,48 @@
1
- import serve from './module.mjs'
2
- import { get, te, tde } from '../instax/module.mjs'
3
-
4
- const port = 8080
5
- const server = serve({
6
- api: (r, s) => 'Hello!',
7
- api2: (r, s, data) => JSON.stringify(data)
8
- }, port)
9
- te(server.port, port)
10
-
11
- // Routes
12
- const resp = await get('http://localhost:8080/api')
13
- te(resp, 'Hello!')
14
- const resp2 = await get('http://localhost:8080/api2', {method: 'POST', body: JSON.stringify({a:1})})
15
- tde(resp2, {a:1})
16
-
17
- // Public
18
- const testhtml = await get('http://localhost:8080/test.html')
19
- te(testhtml, 'ok')
20
- te(server.stop(), true)
21
-
22
- // Test route returned values
23
- const db = {}
24
- const server2 = serve({
25
- _: ({url}) => console.log(url),
26
- __: ({headers: {host}, method, url}) => console.log(host, method, url),
27
- str: () => 'ok',
28
- obj: x => ({a: 'ok'}),
29
- undef: () => undefined,
30
- testerror: () => { throw new Error('this from testerror')}
31
- }, 8085)
32
- te(server2.port, 8085)
33
- te(server2.routes.str(), 'ok')
34
-
35
- const return_str = await get('http://localhost:8085/str')
36
- te(return_str, 'ok')
37
-
38
- const return_obj = await get('http://localhost:8085/obj')
39
- te(return_obj.a, 'ok')
40
-
41
- const return_undefined = await get('http://localhost:8085/undef')
42
- te(return_undefined, '')
43
-
44
- const test_error = await get('http://localhost:8085/testerror')
45
- te(test_error.error, 'this from testerror')
46
-
47
- server2.stop()
1
+ import serve from './module.mjs'
2
+ import { get, te, tde } from '../instax/module.mjs'
3
+
4
+ const port = 8080
5
+ const server = serve({
6
+ api: (r, s) => 'Hello!',
7
+ api2: (r, s, data) => JSON.stringify(data)
8
+ }, port)
9
+ te(server.port, port)
10
+
11
+ // Routes
12
+ const resp = await get('http://localhost:8080/api')
13
+ te(resp, 'Hello!')
14
+ const resp2 = await get('http://localhost:8080/api2', {method: 'POST', body: JSON.stringify({a:1})})
15
+ tde(resp2, {a:1})
16
+
17
+ // Public
18
+ const testhtml = await get('http://localhost:8080/test.html')
19
+ te(testhtml, 'ok')
20
+ te(server.stop(), true)
21
+
22
+ // Test route returned values
23
+ const db = {}
24
+ const server2 = serve({
25
+ _: ({url}) => console.log(url),
26
+ __: ({headers: {host}, method, url}) => console.log(host, method, url),
27
+ str: () => 'ok',
28
+ obj: x => ({a: 'ok'}),
29
+ undef: () => undefined,
30
+ testerror: () => { throw new Error('this from testerror')}
31
+ }, 8085)
32
+ te(server2.port, 8085)
33
+ te(server2.routes.str(), 'ok')
34
+
35
+ const return_str = await get('http://localhost:8085/str')
36
+ te(return_str, 'ok')
37
+
38
+ const return_obj = await get('http://localhost:8085/obj')
39
+ te(return_obj.a, 'ok')
40
+
41
+ const return_undefined = await get('http://localhost:8085/undef')
42
+ te(return_undefined, '')
43
+
44
+ const test_error = await get('http://localhost:8085/testerror')
45
+ te(test_error.error, 'this from testerror')
46
+
47
+ server2.stop()
48
48
  console.log('tests complete')