instaserve 0.1.5 → 0.1.6
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 -0
- package/.devcontainer/devcontainer.json +22 -0
- package/README.md +39 -39
- package/module.mjs +40 -37
- package/package.json +16 -16
- package/routes.mjs +4 -4
- package/server.mjs +19 -19
- package/test.mjs +15 -12
|
@@ -0,0 +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>
|
|
@@ -0,0 +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
|
+
}
|
package/README.md
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
# instaserve
|
|
2
|
-
Instant web stack
|
|
3
|
-
|
|
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
|
-
|
|
9
|
-
> npm run deno (deno)
|
|
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
|
-
|
|
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
|
-
|
|
39
|
-
}
|
|
1
|
+
# instaserve
|
|
2
|
+
Instant web stack
|
|
3
|
+
|
|
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
|
+
|
|
9
|
+
> npm run deno (deno)
|
|
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
40
|
````
|
package/module.mjs
CHANGED
|
@@ -1,37 +1,40 @@
|
|
|
1
|
-
import http from 'node:http'
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
const debug = process.env.debug
|
|
4
|
-
|
|
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 =
|
|
24
|
-
if (routes[url])
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
1
|
+
import http from 'node:http'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
const debug = process.env.debug
|
|
4
|
+
|
|
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]) {
|
|
25
|
+
const resp = routes[url](r, s, data)
|
|
26
|
+
return s.end(resp)
|
|
27
|
+
}
|
|
28
|
+
throw Error(r.url + ' not found')
|
|
29
|
+
} catch (e) {
|
|
30
|
+
console.log(e)
|
|
31
|
+
s.writeHead(404).end()
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
}).listen(port)
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
stop: () => { server.close(); return true }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
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 --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
|
-
}
|
|
16
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "instaserve",
|
|
3
|
+
"version": "0.1.6",
|
|
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
|
+
}
|
|
16
|
+
}
|
package/routes.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export default {
|
|
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
|
-
|
|
1
|
+
export default {
|
|
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: () => 'an example api response'
|
|
5
5
|
}
|
package/server.mjs
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import server from './module.mjs'
|
|
4
|
-
import { pathToFileURL } from 'node:url'
|
|
5
|
-
import { resolve } from 'node:path'
|
|
6
|
-
import fs from 'node:fs'
|
|
7
|
-
const routesfile = resolve('routes.mjs')
|
|
8
|
-
|
|
9
|
-
if (!fs.existsSync(routesfile)) {
|
|
10
|
-
fs.writeFileSync(routesfile, `export default {
|
|
11
|
-
_debug: ({method, url}, s) => !console.log(method, url),
|
|
12
|
-
_example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
|
|
13
|
-
|
|
14
|
-
}`)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const routesurl = pathToFileURL(routesfile).href
|
|
18
|
-
console.log(routesfile, routesurl)
|
|
19
|
-
const routes = (await import(routesurl)).default
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import server from './module.mjs'
|
|
4
|
+
import { pathToFileURL } from 'node:url'
|
|
5
|
+
import { resolve } from 'node:path'
|
|
6
|
+
import fs from 'node:fs'
|
|
7
|
+
const routesfile = resolve('routes.mjs')
|
|
8
|
+
|
|
9
|
+
if (!fs.existsSync(routesfile)) {
|
|
10
|
+
fs.writeFileSync(routesfile, `export default {
|
|
11
|
+
_debug: ({method, url}, s) => !console.log(method, url),
|
|
12
|
+
_example: (r, s) => console.log('returning a falsy value (above) will stop the chain'),
|
|
13
|
+
api: (r, s) => 'an example api response'
|
|
14
|
+
}`)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const routesurl = pathToFileURL(routesfile).href
|
|
18
|
+
console.log(routesfile, routesurl)
|
|
19
|
+
const routes = (await import(routesurl)).default
|
|
20
20
|
server(routes)
|
package/test.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import serve from './module.mjs'
|
|
2
|
-
import { get, te } from 'instax'
|
|
3
|
-
|
|
4
|
-
const server = serve({
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import serve from './module.mjs'
|
|
2
|
+
import { get, te, tde } from '../instax/module.mjs'
|
|
3
|
+
|
|
4
|
+
const server = serve({
|
|
5
|
+
api: (r, s) => 'Hello!',
|
|
6
|
+
api2: (r, s, data) => JSON.stringify(data)
|
|
7
|
+
}, 3001)
|
|
8
|
+
|
|
9
|
+
const resp = await get('http://localhost:3001/api')
|
|
10
|
+
te(resp, 'Hello!')
|
|
11
|
+
const resp2 = await get('http://localhost:3001/api2', {method: 'POST', body: JSON.stringify({a:1})})
|
|
12
|
+
tde(resp2, {a:1})
|
|
13
|
+
|
|
14
|
+
te(server.stop(), true)
|
|
15
|
+
console.log('tests complete')
|