removed url support in node.source

Massimo Melina committed Jan 22, 2022 at 21:53 UTC 546dfe9be0b7dfdc2e3b4b69c6f134719fc2ae55
6 files changed +43 -26
README.md
+1 -1
@@ -87,7 +87,7 @@ The virtual file system is a tree of files and folders, collectively called *nod
87 By default a node is folder, unless you provide for it a source that's a file.
88 Valid keys in a node are:
89 - `name`: how to display it. If not provided HFS will infer it from the source.
90 -- `source`: where to get its content from. Absolute or relative file path, or even http url.
90 +- `source`: absolute or relative path of where to get the content
91 - `children`: just for folders, specify its virtual children.
92 Value is a list and its entries are nodes.
93 - `hidden`: this must not be listed, but it's still downloadable.
package-lock.json
+25 -9
@@ -14,7 +14,6 @@
14 "buffer-crc32": "https://github.com/rejetto/buffer-crc32.git",
15 "fast-glob": "^3.2.7",
16 "koa": "^2.13.4",
17 - "koa-better-http-proxy": "^0.2.9",
17 "koa-bodyparser": "^4.3.0",
18 "koa-compress": "^5.1.0",
19 "koa-mount": "^4.0.0",
@@ -45,6 +44,7 @@
44 "@types/mocha": "^9.0.0",
45 "@types/node": "^16.11.12",
46 "axios": "^0.24.0",
47 + "koa-better-http-proxy": "^0.2.9",
48 "mocha": "^9.1.3",
49 "nm-prune": "^5.0.0",
50 "nodemon": "^2.0.15",
@@ -853,7 +853,8 @@
853 "node_modules/async": {
854 "version": "1.0.0",
855 "resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz",
856 - "integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k="
856 + "integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=",
857 + "dev": true
858 },
859 "node_modules/axios": {
860 "version": "0.24.0",
@@ -1228,6 +1229,7 @@
1229 "version": "1.0.3",
1230 "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz",
1231 "integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=",
1232 + "dev": true,
1233 "engines": {
1234 "node": ">=0.1.90"
1235 }
@@ -1334,6 +1336,7 @@
1336 "version": "1.0.3",
1337 "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz",
1338 "integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI=",
1339 + "dev": true,
1340 "engines": {
1341 "node": ">=0.4.0"
1342 }
@@ -1540,7 +1543,8 @@
1543 "node_modules/es6-promise": {
1544 "version": "3.3.1",
1545 "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz",
1543 - "integrity": "sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM="
1546 + "integrity": "sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM=",
1547 + "dev": true
1548 },
1549 "node_modules/escalade": {
1550 "version": "3.1.1",
@@ -1578,6 +1582,7 @@
1582 "version": "0.1.8",
1583 "resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz",
1584 "integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A=",
1585 + "dev": true,
1586 "engines": {
1587 "node": "> 0.1.90"
1588 }
@@ -2345,6 +2350,7 @@
2350 "version": "0.2.9",
2351 "resolved": "https://registry.npmjs.org/koa-better-http-proxy/-/koa-better-http-proxy-0.2.9.tgz",
2352 "integrity": "sha512-v33AKm5WZw09cI0FSLbEPG0l7sAMDuwvm9ACgFp21GEQ2ekrx2ODQZ9N6dohi6XSMlLafJvwfoHKdeoE6oH9vQ==",
2353 + "dev": true,
2354 "dependencies": {
2355 "es6-promise": "^3.3.1",
2356 "raw-body": "^2.2.0",
@@ -3736,6 +3742,7 @@
3742 "version": "0.0.10",
3743 "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz",
3744 "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=",
3745 + "dev": true,
3746 "engines": {
3747 "node": "*"
3748 }
@@ -4130,6 +4137,7 @@
4137 "version": "2.4.5",
4138 "resolved": "https://registry.npmjs.org/winston/-/winston-2.4.5.tgz",
4139 "integrity": "sha512-TWoamHt5yYvsMarGlGEQE59SbJHqGsZV8/lwC+iCcGeAe0vUaOh+Lv6SYM17ouzC/a/LB1/hz/7sxFBtlu1l4A==",
4140 + "dev": true,
4141 "dependencies": {
4142 "async": "~1.0.0",
4143 "colors": "1.0.x",
@@ -4898,7 +4906,8 @@
4906 "async": {
4907 "version": "1.0.0",
4908 "resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz",
4901 - "integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k="
4909 + "integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=",
4910 + "dev": true
4911 },
4912 "axios": {
4913 "version": "0.24.0",
@@ -5168,7 +5177,8 @@
5177 "colors": {
5178 "version": "1.0.3",
5179 "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz",
5171 - "integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs="
5180 + "integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=",
5181 + "dev": true
5182 },
5183 "compressible": {
5184 "version": "2.0.18",
@@ -5253,7 +5263,8 @@
5263 "cycle": {
5264 "version": "1.0.3",
5265 "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz",
5256 - "integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI="
5266 + "integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI=",
5267 + "dev": true
5268 },
5269 "debug": {
5270 "version": "4.3.3",
@@ -5412,7 +5423,8 @@
5423 "es6-promise": {
5424 "version": "3.3.1",
5425 "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz",
5415 - "integrity": "sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM="
5426 + "integrity": "sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM=",
5427 + "dev": true
5428 },
5429 "escalade": {
5430 "version": "3.1.1",
@@ -5440,7 +5452,8 @@
5452 "eyes": {
5453 "version": "0.1.8",
5454 "resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz",
5443 - "integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A="
5455 + "integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A=",
5456 + "dev": true
5457 },
5458 "fast-glob": {
5459 "version": "3.2.7",
@@ -6001,6 +6014,7 @@
6014 "version": "0.2.9",
6015 "resolved": "https://registry.npmjs.org/koa-better-http-proxy/-/koa-better-http-proxy-0.2.9.tgz",
6016 "integrity": "sha512-v33AKm5WZw09cI0FSLbEPG0l7sAMDuwvm9ACgFp21GEQ2ekrx2ODQZ9N6dohi6XSMlLafJvwfoHKdeoE6oH9vQ==",
6017 + "dev": true,
6018 "requires": {
6019 "es6-promise": "^3.3.1",
6020 "raw-body": "^2.2.0",
@@ -7033,7 +7047,8 @@
7047 "stack-trace": {
7048 "version": "0.0.10",
7049 "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz",
7036 - "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA="
7050 + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=",
7051 + "dev": true
7052 },
7053 "statuses": {
7054 "version": "1.5.0",
@@ -7308,6 +7323,7 @@
7323 "version": "2.4.5",
7324 "resolved": "https://registry.npmjs.org/winston/-/winston-2.4.5.tgz",
7325 "integrity": "sha512-TWoamHt5yYvsMarGlGEQE59SbJHqGsZV8/lwC+iCcGeAe0vUaOh+Lv6SYM17ouzC/a/LB1/hz/7sxFBtlu1l4A==",
7326 + "dev": true,
7327 "requires": {
7328 "async": "~1.0.0",
7329 "colors": "1.0.x",
package.json
+1 -1
@@ -34,7 +34,6 @@
34 "buffer-crc32": "https://github.com/rejetto/buffer-crc32.git",
35 "fast-glob": "^3.2.7",
36 "koa": "^2.13.4",
37 - "koa-better-http-proxy": "^0.2.9",
37 "koa-bodyparser": "^4.3.0",
38 "koa-compress": "^5.1.0",
39 "koa-mount": "^4.0.0",
@@ -62,6 +61,7 @@
61 "@types/mocha": "^9.0.0",
62 "@types/node": "^16.11.12",
63 "axios": "^0.24.0",
64 + "koa-better-http-proxy": "^0.2.9",
65 "mocha": "^9.1.3",
66 "nm-prune": "^5.0.0",
67 "nodemon": "^2.0.15",
src/api.file_list.ts
-2
@@ -71,8 +71,6 @@ export interface DirEntry { n:string, s?:number, m?:Date, c?:Date }
71 async function nodeToDirEntry(node: VfsNode): Promise<DirEntry | null> {
72 try {
73 let { name, source, default:def } = node
74 - if (source?.includes('//'))
75 - return { n: name || source }
74 if (source) {
75 if (!name)
76 name = basename(source)
src/middlewares.ts
+1 -3
@@ -10,7 +10,6 @@ import { zipStreamFromFolder } from './zip'
10 import { serveFileNode } from './serveFile'
11 import { serveFrontend } from './serveFrontend'
12 import mount from 'koa-mount'
13 -import proxy from 'koa-better-http-proxy'
13 import { Readable } from 'stream'
14
15 export const gzipper = compress({
@@ -76,7 +75,6 @@ export const frontendAndSharedFiles: Koa.Middleware = async (ctx, next) => {
75 return serveFrontend(ctx, next)
76 }
77 if (source)
79 - return source.includes('//') ? mount(path,proxy(source,{}))(ctx,next)
80 - : serveFileNode(node)(ctx,next)
78 + return serveFileNode(node)(ctx,next)
79 return next()
80 }
src/serveFrontend.ts
+15 -10
@@ -1,4 +1,3 @@
1 -import proxy from 'koa-better-http-proxy'
1 import Koa from 'koa'
2 import fs from 'fs/promises'
3 import { FRONTEND_URI, METHOD_NOT_ALLOWED, NO_CONTENT, PLUGINS_PUB_URI } from './const'
@@ -10,16 +9,22 @@ import { ApiError } from './apis'
9 const proxyPort = process.env.FRONTEND_PROXY
10 export const serveFrontend = proxyPort ? serveProxyFrontend() : serveStaticFrontend()
11
13 -function serveProxyFrontend() {
12 +function serveProxyFrontend() { // used for development
13 console.debug('fronted: proxied')
15 - return proxy('localhost:'+proxyPort, {
16 - filter: ctx => ctx.method === 'GET' || (ctx.status = METHOD_NOT_ALLOWED) && false,
17 - proxyReqPathResolver: (ctx) => ctx.path.endsWith('/') ? '/' : ctx.path,
18 - userResDecorator(res, data, ctx) {
19 - return ctx.url.endsWith('/') ? treatIndex(ctx, data.toString('utf8'))
20 - : data
21 - }
22 - })
14 + let proxy: Koa.Middleware
15 + import('koa-better-http-proxy').then(lib =>
16 + proxy = lib.default('localhost:'+proxyPort, {
17 + filter: ctx => ctx.method === 'GET' || (ctx.status = METHOD_NOT_ALLOWED) && false,
18 + proxyReqPathResolver: (ctx) => ctx.path.endsWith('/') ? '/' : ctx.path,
19 + userResDecorator(res, data, ctx) {
20 + return ctx.url.endsWith('/') ? treatIndex(ctx, data.toString('utf8'))
21 + : data
22 + }
23 + })
24 + )
25 + return function() { //@ts-ignore
26 + return proxy.apply(this,arguments)
27 + }
28 }
29
30 function serveStaticFrontend() : Koa.Middleware {