instaserve 0.1.19 → 0.1.22
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/.devcontainer/Dockerfile +9 -9
- package/.devcontainer/devcontainer.json +22 -22
- package/README.md +43 -43
- package/bun/http.js +0 -0
- package/deno/server.js +0 -0
- package/module.mjs +63 -54
- package/package.json +13 -13
- package/public/enigmatic.js +0 -0
- package/public/index.html +0 -0
- package/public/sw.js +0 -0
- package/public/test.html +0 -0
- package/routes.mjs +6 -3
- package/test.mjs +47 -47
package/.devcontainer/Dockerfile
CHANGED
|
@@ -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,63 @@
|
|
|
1
|
-
import http from 'node:http'
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
r.on('
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (debug) console.log(`
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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 sdata = ''
|
|
18
|
+
r.on('data', (s) => sdata += 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
|
+
|
|
24
|
+
// Compose data object
|
|
25
|
+
const data = sdata ? JSON.parse(sdata) : {}
|
|
26
|
+
const qs = r.url.split('?')
|
|
27
|
+
if(qs && qs[1]) {
|
|
28
|
+
const o = JSON.parse('{"' + decodeURI(qs[1].replace(/&/g, "\",\"").replace(/=/g, "\":\"")) + '"}')
|
|
29
|
+
Object.assign(data, o)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const midware = Object.keys(routes)
|
|
33
|
+
.filter((k) => k.startsWith('_'))
|
|
34
|
+
.find((k) => routes[k](r, s, data))
|
|
35
|
+
|
|
36
|
+
const fc = public_file(r, s)
|
|
37
|
+
if(fc) return s.end(fc)
|
|
38
|
+
|
|
39
|
+
const url = r.url.split('/')[1].split('?')[0]
|
|
40
|
+
if (routes[url]) {
|
|
41
|
+
const resp = routes[url](r, s, data)
|
|
42
|
+
if(debug) console.log(`route: ${url}, returned: ${JSON.stringify(resp)}`)
|
|
43
|
+
return s.end(typeof resp === 'string' ? resp:JSON.stringify(resp))
|
|
44
|
+
}
|
|
45
|
+
throw Error(r.url + ' not found')
|
|
46
|
+
} catch (e) {
|
|
47
|
+
const err = JSON.stringify({error: e.message})
|
|
48
|
+
console.log('Server error: ' + e)
|
|
49
|
+
s.writeHead(404).end(err)
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
}).listen(process.env.port || port, process.env.ip || ip)
|
|
53
|
+
|
|
54
|
+
console.log(`started on: ${(process.env.ip || ip)}:${(process.env.port || port)}, using routes: ${Object.keys(routes)}`)
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
routes: routes,
|
|
58
|
+
port: port,
|
|
59
|
+
server: server,
|
|
60
|
+
stop: () => { server.close(); return true }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "instaserve",
|
|
3
|
-
"version": "0.1.
|
|
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.22",
|
|
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
|
+
}
|
package/public/enigmatic.js
CHANGED
|
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
|
-
_debug: ({method, url}, s) =>
|
|
4
|
+
_debug: ({method, url}, s, data) => { console.log(method, url, data) },
|
|
3
5
|
_example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
|
|
4
|
-
api: (r, s) => 'an example api response',
|
|
5
|
-
testerror: () => { throw new Error('this from testerror') }
|
|
6
|
+
api: (r, s, data) => 'an example api response, data:' + JSON.stringify(data),
|
|
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')
|