acrylblends 0.3.0 → 0.4.0
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/dist/index.d.ts +6 -1
- package/dist/index.js +28 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/plugins/acryl-inspect/plugin.json +2 -1
- package/plugins/acryl-inspect/src/report.ts +16 -13
- package/plugins/acryl-inspect/src/service.ts +9 -7
- package/plugins/acryl-inspect/tests/report.spec.ts +7 -8
- package/plugins/acryl-inspect/tests/service.spec.ts +11 -10
- package/plugins/acryl-inspect/upstream.json +2 -2
- package/plugins/acryl-node-server/LICENSE +26 -0
- package/plugins/acryl-node-server/plugin.json +2 -1
- package/plugins/acryl-node-server/src/adapter/conninfo.ts +23 -0
- package/plugins/acryl-node-server/src/adapter/early-hints.ts +55 -0
- package/plugins/acryl-node-server/src/adapter/error.ts +11 -0
- package/plugins/acryl-node-server/src/adapter/headers.ts +249 -0
- package/plugins/acryl-node-server/src/adapter/index.ts +6 -0
- package/plugins/acryl-node-server/src/adapter/listener.ts +540 -0
- package/plugins/acryl-node-server/src/adapter/request.ts +797 -0
- package/plugins/acryl-node-server/src/adapter/response.ts +186 -0
- package/plugins/acryl-node-server/src/adapter/serve-static.ts +238 -0
- package/plugins/acryl-node-server/src/adapter/server.ts +36 -0
- package/plugins/acryl-node-server/src/adapter/types.ts +82 -0
- package/plugins/acryl-node-server/src/adapter/url.ts +42 -0
- package/plugins/acryl-node-server/src/adapter/utils/response/constants.ts +1 -0
- package/plugins/acryl-node-server/src/adapter/utils/response.ts +4 -0
- package/plugins/acryl-node-server/src/adapter/utils/stream.ts +88 -0
- package/plugins/acryl-node-server/src/adapter/utils.ts +96 -0
- package/plugins/acryl-node-server/src/adapter/websocket-types.ts +45 -0
- package/plugins/acryl-node-server/src/adapter/websocket.ts +375 -0
- package/plugins/acryl-node-server/src/index.ts +19 -6
- package/plugins/acryl-node-server/tests/adapter/app.ts +76 -0
- package/plugins/acryl-node-server/tests/adapter/assets/.static/plain.txt +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/favicon.ico +0 -0
- package/plugins/acryl-node-server/tests/adapter/assets/secret.txt +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/acryl.html +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/admin/secret.txt +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/data.json +5 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/extensionless +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/foo..bar.txt +0 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/index.html +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/plain.txt +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-context-path/plain.txt +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin.br +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.br +1 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.zst +1 -0
- package/plugins/acryl-node-server/tests/adapter/conninfo.test.ts +34 -0
- package/plugins/acryl-node-server/tests/adapter/content-length.test.ts +248 -0
- package/plugins/acryl-node-server/tests/adapter/early-hints.test.ts +457 -0
- package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-cert.pem +23 -0
- package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-key.pem +27 -0
- package/plugins/acryl-node-server/tests/adapter/headers.test.ts +252 -0
- package/plugins/acryl-node-server/tests/adapter/helpers/request.ts +190 -0
- package/plugins/acryl-node-server/tests/adapter/listener.test.ts +727 -0
- package/plugins/acryl-node-server/tests/adapter/request-client-disconnect.test.ts +189 -0
- package/plugins/acryl-node-server/tests/adapter/request.test.ts +1230 -0
- package/plugins/acryl-node-server/tests/adapter/response.test.ts +285 -0
- package/plugins/acryl-node-server/tests/adapter/serve-static.test.ts +849 -0
- package/plugins/acryl-node-server/tests/adapter/server-socket.test.ts +790 -0
- package/plugins/acryl-node-server/tests/adapter/server.test.ts +1302 -0
- package/plugins/acryl-node-server/tests/adapter/setup.ts +16 -0
- package/plugins/acryl-node-server/tests/adapter/url.test.ts +76 -0
- package/plugins/acryl-node-server/tests/adapter/utils/response.test.ts +23 -0
- package/plugins/acryl-node-server/tests/adapter/utils/stream.test.ts +75 -0
- package/plugins/acryl-node-server/tests/adapter/utils.test.ts +235 -0
- package/plugins/acryl-node-server/tests/adapter/websocket.test.ts +260 -0
- package/plugins/acryl-node-server/tests/server.spec.ts +44 -8
- package/plugins/acryl-node-server/upstream.json +3 -3
- package/plugins/acryl-router/LICENSE +26 -0
- package/plugins/acryl-router/plugin.json +2 -1
- package/plugins/acryl-router/src/client.ts +2 -0
- package/plugins/acryl-router/src/connection.ts +42 -0
- package/plugins/acryl-router/src/core/app-base.ts +544 -0
- package/plugins/acryl-router/src/core/app.test.ts +3833 -0
- package/plugins/acryl-router/src/core/app.ts +34 -0
- package/plugins/acryl-router/src/core/client/client.test.ts +2313 -0
- package/plugins/acryl-router/src/core/client/client.ts +249 -0
- package/plugins/acryl-router/src/core/client/fetch-result-please.ts +95 -0
- package/plugins/acryl-router/src/core/client/index.ts +17 -0
- package/plugins/acryl-router/src/core/client/types.test.ts +149 -0
- package/plugins/acryl-router/src/core/client/types.ts +393 -0
- package/plugins/acryl-router/src/core/client/utils.test.ts +405 -0
- package/plugins/acryl-router/src/core/client/utils.ts +117 -0
- package/plugins/acryl-router/src/core/compose.test.ts +751 -0
- package/plugins/acryl-router/src/core/compose.ts +73 -0
- package/plugins/acryl-router/src/core/context.test.ts +629 -0
- package/plugins/acryl-router/src/core/context.ts +781 -0
- package/plugins/acryl-router/src/core/helper/accepts/accepts.test.ts +344 -0
- package/plugins/acryl-router/src/core/helper/accepts/accepts.ts +96 -0
- package/plugins/acryl-router/src/core/helper/accepts/index.ts +6 -0
- package/plugins/acryl-router/src/core/helper/adapter/index.test.ts +55 -0
- package/plugins/acryl-router/src/core/helper/adapter/index.ts +90 -0
- package/plugins/acryl-router/src/core/helper/conninfo/index.ts +6 -0
- package/plugins/acryl-router/src/core/helper/conninfo/types.ts +45 -0
- package/plugins/acryl-router/src/core/helper/cookie/index.test.ts +510 -0
- package/plugins/acryl-router/src/core/helper/cookie/index.ts +146 -0
- package/plugins/acryl-router/src/core/helper/factory/index.test.ts +434 -0
- package/plugins/acryl-router/src/core/helper/factory/index.ts +375 -0
- package/plugins/acryl-router/src/core/helper/html/index.test.ts +78 -0
- package/plugins/acryl-router/src/core/helper/html/index.ts +57 -0
- package/plugins/acryl-router/src/core/helper/route/index.test.ts +259 -0
- package/plugins/acryl-router/src/core/helper/route/index.ts +144 -0
- package/plugins/acryl-router/src/core/helper/streaming/index.ts +9 -0
- package/plugins/acryl-router/src/core/helper/streaming/sse.ts +101 -0
- package/plugins/acryl-router/src/core/helper/streaming/stream.test.ts +128 -0
- package/plugins/acryl-router/src/core/helper/streaming/stream.ts +45 -0
- package/plugins/acryl-router/src/core/helper/streaming/text.test.ts +34 -0
- package/plugins/acryl-router/src/core/helper/streaming/text.ts +15 -0
- package/plugins/acryl-router/src/core/helper/streaming/utils.ts +11 -0
- package/plugins/acryl-router/src/core/helper/testing/index.test.ts +42 -0
- package/plugins/acryl-router/src/core/helper/testing/index.ts +28 -0
- package/plugins/acryl-router/src/core/helper/websocket/index.test.ts +124 -0
- package/plugins/acryl-router/src/core/helper/websocket/index.ts +140 -0
- package/plugins/acryl-router/src/core/http-exception.test.ts +45 -0
- package/plugins/acryl-router/src/core/http-exception.ts +77 -0
- package/plugins/acryl-router/src/core/index.ts +53 -0
- package/plugins/acryl-router/src/core/middleware/basic-auth/index.test.ts +434 -0
- package/plugins/acryl-router/src/core/middleware/basic-auth/index.ts +152 -0
- package/plugins/acryl-router/src/core/middleware/bearer-auth/index.test.ts +1018 -0
- package/plugins/acryl-router/src/core/middleware/bearer-auth/index.ts +220 -0
- package/plugins/acryl-router/src/core/middleware/body-limit/index.test.ts +273 -0
- package/plugins/acryl-router/src/core/middleware/body-limit/index.ts +110 -0
- package/plugins/acryl-router/src/core/middleware/combine/index.test.ts +355 -0
- package/plugins/acryl-router/src/core/middleware/combine/index.ts +171 -0
- package/plugins/acryl-router/src/core/middleware/compress/index.test.ts +535 -0
- package/plugins/acryl-router/src/core/middleware/compress/index.ts +142 -0
- package/plugins/acryl-router/src/core/middleware/context-storage/index.test.ts +43 -0
- package/plugins/acryl-router/src/core/middleware/context-storage/index.ts +58 -0
- package/plugins/acryl-router/src/core/middleware/cors/index.test.ts +532 -0
- package/plugins/acryl-router/src/core/middleware/cors/index.ts +163 -0
- package/plugins/acryl-router/src/core/middleware/csrf/index.test.ts +534 -0
- package/plugins/acryl-router/src/core/middleware/csrf/index.ts +150 -0
- package/plugins/acryl-router/src/core/middleware/etag/digest.test.ts +21 -0
- package/plugins/acryl-router/src/core/middleware/etag/digest.ts +95 -0
- package/plugins/acryl-router/src/core/middleware/etag/index.test.ts +503 -0
- package/plugins/acryl-router/src/core/middleware/etag/index.ts +135 -0
- package/plugins/acryl-router/src/core/middleware/ip-restriction/index.test.ts +217 -0
- package/plugins/acryl-router/src/core/middleware/ip-restriction/index.ts +278 -0
- package/plugins/acryl-router/src/core/middleware/jwk/index.test.ts +1135 -0
- package/plugins/acryl-router/src/core/middleware/jwk/index.ts +1 -0
- package/plugins/acryl-router/src/core/middleware/jwk/jwk.ts +195 -0
- package/plugins/acryl-router/src/core/middleware/jwk/keys.test.json +48 -0
- package/plugins/acryl-router/src/core/middleware/jwt/index.test.ts +802 -0
- package/plugins/acryl-router/src/core/middleware/jwt/index.ts +9 -0
- package/plugins/acryl-router/src/core/middleware/jwt/jwt.ts +189 -0
- package/plugins/acryl-router/src/core/middleware/language/index.test.ts +552 -0
- package/plugins/acryl-router/src/core/middleware/language/index.ts +12 -0
- package/plugins/acryl-router/src/core/middleware/language/language.ts +314 -0
- package/plugins/acryl-router/src/core/middleware/logger/index.test.ts +204 -0
- package/plugins/acryl-router/src/core/middleware/logger/index.ts +95 -0
- package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.test.ts +292 -0
- package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.ts +141 -0
- package/plugins/acryl-router/src/core/middleware/method-override/index.test.ts +221 -0
- package/plugins/acryl-router/src/core/middleware/method-override/index.ts +147 -0
- package/plugins/acryl-router/src/core/middleware/powered-by/index.test.ts +37 -0
- package/plugins/acryl-router/src/core/middleware/powered-by/index.ts +35 -0
- package/plugins/acryl-router/src/core/middleware/pretty-json/index.test.ts +218 -0
- package/plugins/acryl-router/src/core/middleware/pretty-json/index.ts +67 -0
- package/plugins/acryl-router/src/core/middleware/request-id/index.test.ts +168 -0
- package/plugins/acryl-router/src/core/middleware/request-id/index.ts +7 -0
- package/plugins/acryl-router/src/core/middleware/request-id/request-id.ts +59 -0
- package/plugins/acryl-router/src/core/middleware/secure-headers/index.test.ts +671 -0
- package/plugins/acryl-router/src/core/middleware/secure-headers/index.ts +8 -0
- package/plugins/acryl-router/src/core/middleware/secure-headers/permissions-policy.ts +101 -0
- package/plugins/acryl-router/src/core/middleware/secure-headers/secure-headers.ts +340 -0
- package/plugins/acryl-router/src/core/middleware/serve-static/index.test.ts +450 -0
- package/plugins/acryl-router/src/core/middleware/serve-static/index.ts +135 -0
- package/plugins/acryl-router/src/core/middleware/serve-static/path.test.ts +50 -0
- package/plugins/acryl-router/src/core/middleware/serve-static/path.ts +25 -0
- package/plugins/acryl-router/src/core/middleware/timeout/index.test.ts +70 -0
- package/plugins/acryl-router/src/core/middleware/timeout/index.ts +58 -0
- package/plugins/acryl-router/src/core/middleware/timing/index.test.ts +310 -0
- package/plugins/acryl-router/src/core/middleware/timing/index.ts +8 -0
- package/plugins/acryl-router/src/core/middleware/timing/timing.ts +256 -0
- package/plugins/acryl-router/src/core/middleware/trailing-slash/index.test.ts +381 -0
- package/plugins/acryl-router/src/core/middleware/trailing-slash/index.ts +156 -0
- package/plugins/acryl-router/src/core/request/constants.ts +1 -0
- package/plugins/acryl-router/src/core/request.test.ts +751 -0
- package/plugins/acryl-router/src/core/request.ts +502 -0
- package/plugins/acryl-router/src/core/router/common.case.test.ts +1132 -0
- package/plugins/acryl-router/src/core/router/linear-router/index.ts +6 -0
- package/plugins/acryl-router/src/core/router/linear-router/router.test.ts +80 -0
- package/plugins/acryl-router/src/core/router/linear-router/router.ts +152 -0
- package/plugins/acryl-router/src/core/router/pattern-router/index.ts +6 -0
- package/plugins/acryl-router/src/core/router/pattern-router/router.test.ts +46 -0
- package/plugins/acryl-router/src/core/router/pattern-router/router.ts +54 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/index.ts +7 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/matcher.ts +33 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/node.ts +170 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.test.ts +154 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.ts +165 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/router.test.ts +265 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/router.ts +174 -0
- package/plugins/acryl-router/src/core/router/reg-exp-router/trie.ts +84 -0
- package/plugins/acryl-router/src/core/router/smart-router/index.ts +6 -0
- package/plugins/acryl-router/src/core/router/smart-router/router.test.ts +13 -0
- package/plugins/acryl-router/src/core/router/smart-router/router.ts +70 -0
- package/plugins/acryl-router/src/core/router/trie-router/index.ts +6 -0
- package/plugins/acryl-router/src/core/router/trie-router/node.test.ts +896 -0
- package/plugins/acryl-router/src/core/router/trie-router/node.ts +221 -0
- package/plugins/acryl-router/src/core/router/trie-router/router.test.ts +8 -0
- package/plugins/acryl-router/src/core/router/trie-router/router.ts +18 -0
- package/plugins/acryl-router/src/core/router/utils.ts +1 -0
- package/plugins/acryl-router/src/core/router.ts +103 -0
- package/plugins/acryl-router/src/core/types.test.ts +4134 -0
- package/plugins/acryl-router/src/core/types.ts +2782 -0
- package/plugins/acryl-router/src/core/utils/accept.test.ts +299 -0
- package/plugins/acryl-router/src/core/utils/accept.ts +268 -0
- package/plugins/acryl-router/src/core/utils/basic-auth.test.ts +56 -0
- package/plugins/acryl-router/src/core/utils/basic-auth.ts +26 -0
- package/plugins/acryl-router/src/core/utils/body.test.ts +496 -0
- package/plugins/acryl-router/src/core/utils/body.ts +263 -0
- package/plugins/acryl-router/src/core/utils/buffer.test.ts +147 -0
- package/plugins/acryl-router/src/core/utils/buffer.ts +117 -0
- package/plugins/acryl-router/src/core/utils/color.test.ts +23 -0
- package/plugins/acryl-router/src/core/utils/color.ts +56 -0
- package/plugins/acryl-router/src/core/utils/compress.ts +10 -0
- package/plugins/acryl-router/src/core/utils/concurrent.test.ts +69 -0
- package/plugins/acryl-router/src/core/utils/concurrent.ts +55 -0
- package/plugins/acryl-router/src/core/utils/constants.ts +4 -0
- package/plugins/acryl-router/src/core/utils/cookie.test.ts +460 -0
- package/plugins/acryl-router/src/core/utils/cookie.ts +281 -0
- package/plugins/acryl-router/src/core/utils/crypto.test.ts +43 -0
- package/plugins/acryl-router/src/core/utils/crypto.ts +58 -0
- package/plugins/acryl-router/src/core/utils/encode.test.ts +61 -0
- package/plugins/acryl-router/src/core/utils/encode.ts +34 -0
- package/plugins/acryl-router/src/core/utils/filepath.test.ts +78 -0
- package/plugins/acryl-router/src/core/utils/filepath.ts +60 -0
- package/plugins/acryl-router/src/core/utils/handler.ts +15 -0
- package/plugins/acryl-router/src/core/utils/headers.ts +355 -0
- package/plugins/acryl-router/src/core/utils/html.test.ts +16 -0
- package/plugins/acryl-router/src/core/utils/html.ts +182 -0
- package/plugins/acryl-router/src/core/utils/http-status.ts +72 -0
- package/plugins/acryl-router/src/core/utils/ipaddr.test.ts +149 -0
- package/plugins/acryl-router/src/core/utils/ipaddr.ts +374 -0
- package/plugins/acryl-router/src/core/utils/jwt/index.ts +7 -0
- package/plugins/acryl-router/src/core/utils/jwt/jwa.test.ts +86 -0
- package/plugins/acryl-router/src/core/utils/jwt/jwa.ts +38 -0
- package/plugins/acryl-router/src/core/utils/jwt/jws.ts +235 -0
- package/plugins/acryl-router/src/core/utils/jwt/jwt.test.ts +1788 -0
- package/plugins/acryl-router/src/core/utils/jwt/jwt.ts +292 -0
- package/plugins/acryl-router/src/core/utils/jwt/types.ts +157 -0
- package/plugins/acryl-router/src/core/utils/jwt/utf8.ts +7 -0
- package/plugins/acryl-router/src/core/utils/mime.test.ts +64 -0
- package/plugins/acryl-router/src/core/utils/mime.ts +94 -0
- package/plugins/acryl-router/src/core/utils/stream.test.ts +212 -0
- package/plugins/acryl-router/src/core/utils/stream.ts +107 -0
- package/plugins/acryl-router/src/core/utils/types.test.ts +295 -0
- package/plugins/acryl-router/src/core/utils/types.ts +123 -0
- package/plugins/acryl-router/src/core/utils/url.test.ts +384 -0
- package/plugins/acryl-router/src/core/utils/url.ts +337 -0
- package/plugins/acryl-router/src/core/validator/index.ts +8 -0
- package/plugins/acryl-router/src/core/validator/utils.test.ts +82 -0
- package/plugins/acryl-router/src/core/validator/utils.ts +69 -0
- package/plugins/acryl-router/src/core/validator/validator.test.ts +1488 -0
- package/plugins/acryl-router/src/core/validator/validator.ts +185 -0
- package/plugins/acryl-router/src/helpers/index.ts +13 -3
- package/plugins/acryl-router/src/helpers/schema-validator.ts +35 -0
- package/plugins/acryl-router/src/index.ts +6 -6
- package/plugins/acryl-router/src/middleware/index.ts +25 -6
- package/plugins/acryl-router/src/middleware/loopback-only.ts +19 -13
- package/plugins/acryl-router/src/service.ts +256 -64
- package/plugins/acryl-router/tests/docs-examples.spec.ts +152 -0
- package/plugins/acryl-router/tests/live-install.spec.ts +2 -2
- package/plugins/acryl-router/tests/loader.spec.ts +3 -2
- package/plugins/acryl-router/tests/service.spec.ts +264 -66
- package/plugins/acryl-router/tests/shadow-state.spec.ts +79 -0
- package/plugins/acryl-router/upstream.json +2 -2
- package/plugins/acryl-router-http/plugin.json +2 -1
- package/plugins/acryl-router-http/src/bridge.ts +3 -1
- package/plugins/acryl-router-http/tests/adapter.spec.ts +16 -12
- package/plugins/acryl-router-http/tests/surfaces.spec.ts +12 -10
- package/plugins/acryl-router-http/upstream.json +3 -3
- package/plugins/acryl-state/NOTICE +30 -0
- package/plugins/acryl-state/plugin.json +1 -1
- package/plugins/acryl-state/src/cell.ts +1 -1
- package/plugins/acryl-state/src/service.ts +33 -15
- package/plugins/acryl-state/upstream.json +6 -4
- package/plugins/builder/script.ts +4 -7
- package/plugins/extensions/index.ts +2 -2
- package/plugins/extensions/resolve.ts +28 -3
- package/plugins/openai/prompt.ts +9 -7
- package/plugins/tsconfig.json +5 -4
- package/templates/agent/src/routes.ts +37 -27
- package/templates/agent/test/model.test.ts +2 -1
- package/templates/agent/tsconfig.json +8 -5
- package/templates/blank/src/routes.ts +6 -4
- package/templates/blank/tsconfig.json +2 -4
- package/templates/todo/src/page.ts +1 -1
- package/templates/todo/src/todo.ts +56 -29
- package/templates/todo/tsconfig.json +2 -4
- package/templates/todo/upstream.json +2 -2
- package/plugins/acryl-router/src/compose.ts +0 -21
- package/plugins/acryl-router/src/context.ts +0 -59
- package/plugins/acryl-router/src/helpers/read-body.ts +0 -59
- package/plugins/acryl-router/src/helpers/sse.ts +0 -83
- package/plugins/acryl-router/src/helpers/validate.ts +0 -44
- package/plugins/acryl-router/src/http-error.ts +0 -21
- package/plugins/acryl-router/src/middleware/body-limit.ts +0 -25
- package/plugins/acryl-router/src/middleware/cors.ts +0 -46
- package/plugins/acryl-router/src/middleware/logger.ts +0 -21
- package/plugins/acryl-router/src/middleware/request-id.ts +0 -24
- package/plugins/acryl-router/src/middleware/secure-headers.ts +0 -23
- package/plugins/acryl-router/src/pattern.ts +0 -89
- package/plugins/acryl-router/src/registrar.ts +0 -90
- package/plugins/acryl-router/src/table.ts +0 -156
- package/plugins/acryl-router/src/types.ts +0 -73
- package/plugins/acryl-router/src/util.ts +0 -23
- package/plugins/acryl-router/tests/api.spec.ts +0 -223
- package/plugins/acryl-router/tests/conformance.ts +0 -170
- package/plugins/acryl-router/tests/helpers.spec.ts +0 -172
- package/plugins/acryl-router/tests/middleware.spec.ts +0 -183
- package/plugins/acryl-router/tests/table.spec.ts +0 -47
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import { serveStatic as baseServeStatic } from './index.ts'
|
|
2
|
+
import { App } from '../../app.ts'
|
|
3
|
+
|
|
4
|
+
describe('Serve Static Middleware with a Blob body', () => {
|
|
5
|
+
it('Should serve content returned as a Blob', async () => {
|
|
6
|
+
const app = new App()
|
|
7
|
+
app.use(
|
|
8
|
+
'/static/*',
|
|
9
|
+
baseServeStatic({
|
|
10
|
+
getContent: async (path) => new Blob([`Hello in ${path}`]),
|
|
11
|
+
})
|
|
12
|
+
)
|
|
13
|
+
const res = await app.request('http://localhost/static/hello.txt')
|
|
14
|
+
expect(res.status).toBe(200)
|
|
15
|
+
expect(await res.text()).toBe('Hello in static/hello.txt')
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
describe('Serve Static Middleware', () => {
|
|
20
|
+
const app = new App()
|
|
21
|
+
const getContent = vi.fn(async (path) => {
|
|
22
|
+
if (path.endsWith('not-found.txt')) {
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
return `Hello in ${path}`
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const serveStatic = baseServeStatic({
|
|
29
|
+
getContent,
|
|
30
|
+
isDir: (path) => {
|
|
31
|
+
if (path === 'static/sub' || path === 'static/hello.world') {
|
|
32
|
+
return true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
onFound: (path, c) => {
|
|
36
|
+
if (path.endsWith('hello.html')) {
|
|
37
|
+
c.header('X-Custom', `Found the file at ${path}`)
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
app.get('/static/*', serveStatic)
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
getContent.mockClear()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it.each(['GET', 'HEAD'])(
|
|
49
|
+
'Should serve files for %s requests registered with use()',
|
|
50
|
+
async (method) => {
|
|
51
|
+
const app = new App().use('/static/*', serveStatic)
|
|
52
|
+
|
|
53
|
+
const res = await app.request('/static/hello.html', { method })
|
|
54
|
+
|
|
55
|
+
expect(res.status).toBe(200)
|
|
56
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
|
|
57
|
+
expect(await res.text()).toBe(method === 'HEAD' ? '' : 'Hello in static/hello.html')
|
|
58
|
+
expect(getContent).toBeCalledTimes(1)
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
it.each(['POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'PROPFIND'])(
|
|
63
|
+
'Should pass %s requests to the next handler without accessing files',
|
|
64
|
+
async (method) => {
|
|
65
|
+
const isDir = vi.fn(() => false)
|
|
66
|
+
const rewriteRequestPath = vi.fn((path: string) => path)
|
|
67
|
+
const onFound = vi.fn()
|
|
68
|
+
const onNotFound = vi.fn()
|
|
69
|
+
const app = new App().use(
|
|
70
|
+
'/static/*',
|
|
71
|
+
baseServeStatic({ getContent, isDir, rewriteRequestPath, onFound, onNotFound })
|
|
72
|
+
)
|
|
73
|
+
app.on(method, '/static/hello.html', (c) => c.text('Handled downstream', 202))
|
|
74
|
+
|
|
75
|
+
const res = await app.request('/static/hello.html', { method })
|
|
76
|
+
|
|
77
|
+
expect(res.status).toBe(202)
|
|
78
|
+
expect(await res.text()).toBe('Handled downstream')
|
|
79
|
+
expect(rewriteRequestPath).not.toBeCalled()
|
|
80
|
+
expect(isDir).not.toBeCalled()
|
|
81
|
+
expect(getContent).not.toBeCalled()
|
|
82
|
+
expect(onFound).not.toBeCalled()
|
|
83
|
+
expect(onNotFound).not.toBeCalled()
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
it.each(['/static/hello.html', '/static/not-found.txt'])(
|
|
88
|
+
'Should return 404 for an unhandled POST request regardless of file existence - %s',
|
|
89
|
+
async (path) => {
|
|
90
|
+
const app = new App().use('/static/*', serveStatic)
|
|
91
|
+
|
|
92
|
+
const res = await app.request(path, { method: 'POST' })
|
|
93
|
+
|
|
94
|
+
expect(res.status).toBe(404)
|
|
95
|
+
expect(await res.text()).toBe('404 Not Found')
|
|
96
|
+
expect(getContent).not.toBeCalled()
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
it('Should return 200 response - /static/hello.html', async () => {
|
|
101
|
+
const res = await app.request('/static/hello.html')
|
|
102
|
+
expect(res.status).toBe(200)
|
|
103
|
+
expect(res.headers.get('Content-Encoding')).toBeNull()
|
|
104
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
|
|
105
|
+
expect(await res.text()).toBe('Hello in static/hello.html')
|
|
106
|
+
expect(res.headers.get('X-Custom')).toBe('Found the file at static/hello.html')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('Should return 200 response - /static/sub', async () => {
|
|
110
|
+
const res = await app.request('/static/sub')
|
|
111
|
+
expect(res.status).toBe(200)
|
|
112
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
|
|
113
|
+
expect(await res.text()).toBe('Hello in static/sub/index.html')
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('Should return 200 response - /static/hello.world', async () => {
|
|
117
|
+
const res = await app.request('/static/hello.world')
|
|
118
|
+
expect(res.status).toBe(200)
|
|
119
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
|
|
120
|
+
expect(await res.text()).toBe('Hello in static/hello.world/index.html')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it.each([
|
|
124
|
+
['/static/%E7%82%8E.txt', 'static/炎.txt'],
|
|
125
|
+
['/static/hello%20world.txt', 'static/hello world.txt'],
|
|
126
|
+
])('Should decode URI strings - %s', async (url, path) => {
|
|
127
|
+
const res = await app.request(url)
|
|
128
|
+
expect(res.status).toBe(200)
|
|
129
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)
|
|
130
|
+
expect(await res.text()).toBe(`Hello in ${path}`)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it.each(['/static/100%25.txt', '/static/100%25/hello.txt', '/static/%2Fadmin/secret.txt'])(
|
|
134
|
+
'Should skip paths containing percent signs by default - %s',
|
|
135
|
+
async (path) => {
|
|
136
|
+
const onNotFound = vi.fn()
|
|
137
|
+
const app = new App().use('*', baseServeStatic({ getContent, onNotFound }))
|
|
138
|
+
|
|
139
|
+
const res = await app.request(path)
|
|
140
|
+
|
|
141
|
+
expect(res.status).toBe(404)
|
|
142
|
+
expect(getContent).not.toBeCalled()
|
|
143
|
+
expect(onNotFound).toHaveBeenCalledWith(path, expect.anything())
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
it.each([
|
|
148
|
+
['/static/100%25.txt', 'static/100%.txt'],
|
|
149
|
+
['/static/100%25/hello.txt', 'static/100%/hello.txt'],
|
|
150
|
+
['/static/%2Fadmin/secret.txt', 'static/%2Fadmin/secret.txt'],
|
|
151
|
+
])('Should allow percent signs when opted in - %s', async (url, path) => {
|
|
152
|
+
const app = new App().use('*', baseServeStatic({ getContent, allowPercentInPath: true }))
|
|
153
|
+
|
|
154
|
+
const res = await app.request(url)
|
|
155
|
+
|
|
156
|
+
expect(res.status).toBe(200)
|
|
157
|
+
expect(await res.text()).toBe(`Hello in ${path}`)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('Should not bypass authentication through a second decode', async () => {
|
|
161
|
+
const app = new App()
|
|
162
|
+
app.get('/static/admin/*', (c) => c.text('Unauthorized', 401))
|
|
163
|
+
app.use('/static/*', baseServeStatic({ getContent }))
|
|
164
|
+
|
|
165
|
+
const protectedRes = await app.request('/static/admin/secret.txt')
|
|
166
|
+
expect(protectedRes.status).toBe(401)
|
|
167
|
+
|
|
168
|
+
const res = await app.request('/static/%%36%31dmin/secret.txt')
|
|
169
|
+
expect(res.status).toBe(404)
|
|
170
|
+
expect(getContent).not.toBeCalled()
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('Should return 404 response - /static/not-found.txt', async () => {
|
|
174
|
+
const res = await app.request('/static/not-found.txt')
|
|
175
|
+
expect(res.status).toBe(404)
|
|
176
|
+
expect(res.headers.get('Content-Encoding')).toBeNull()
|
|
177
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)
|
|
178
|
+
expect(await res.text()).toBe('404 Not Found')
|
|
179
|
+
expect(getContent).toBeCalledTimes(1)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('Should return 200 response for empty string content - /static/empty.txt', async () => {
|
|
183
|
+
const onNotFound = vi.fn()
|
|
184
|
+
const app = new App().use(
|
|
185
|
+
'*',
|
|
186
|
+
baseServeStatic({
|
|
187
|
+
getContent: async () => '',
|
|
188
|
+
onNotFound,
|
|
189
|
+
})
|
|
190
|
+
)
|
|
191
|
+
const res = await app.request('/static/empty.txt')
|
|
192
|
+
expect(res.status).toBe(200)
|
|
193
|
+
expect(await res.text()).toBe('')
|
|
194
|
+
expect(onNotFound).not.toBeCalled()
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('Should not allow a directory traversal - /static/%2e%2e/static/hello.html', async () => {
|
|
198
|
+
const res = await app.fetch({
|
|
199
|
+
method: 'GET',
|
|
200
|
+
url: 'http://localhost/static/%2e%2e/static/hello.html',
|
|
201
|
+
} as Request)
|
|
202
|
+
expect(res.status).toBe(404)
|
|
203
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)
|
|
204
|
+
expect(await res.text()).toBe('404 Not Found')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('Should not allow a backslash separator injection - /static/admin%5Csecret.txt', async () => {
|
|
208
|
+
const res = await app.fetch({
|
|
209
|
+
method: 'GET',
|
|
210
|
+
url: 'http://localhost/static/admin%5Csecret.txt',
|
|
211
|
+
} as Request)
|
|
212
|
+
expect(res.status).toBe(404)
|
|
213
|
+
expect(await res.text()).toBe('404 Not Found')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('Should return a pre-compressed zstd response - /static/hello.html', async () => {
|
|
217
|
+
const app = new App().use(
|
|
218
|
+
'*',
|
|
219
|
+
baseServeStatic({
|
|
220
|
+
getContent,
|
|
221
|
+
precompressed: true,
|
|
222
|
+
})
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
const res = await app.request('/static/hello.html', {
|
|
226
|
+
headers: { 'Accept-Encoding': 'zstd' },
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
expect(res.status).toBe(200)
|
|
230
|
+
expect(res.headers.get('Content-Encoding')).toBe('zstd')
|
|
231
|
+
expect(res.headers.get('Vary')).toBe('Accept-Encoding')
|
|
232
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
|
|
233
|
+
expect(await res.text()).toBe('Hello in static/hello.html.zst')
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('Should return a pre-compressed brotli response - /static/hello.html', async () => {
|
|
237
|
+
const app = new App().use(
|
|
238
|
+
'*',
|
|
239
|
+
baseServeStatic({
|
|
240
|
+
getContent,
|
|
241
|
+
precompressed: true,
|
|
242
|
+
})
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
const res = await app.request('/static/hello.html', {
|
|
246
|
+
headers: { 'Accept-Encoding': 'wompwomp, gzip, br, deflate, zstd' },
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
expect(res.status).toBe(200)
|
|
250
|
+
expect(res.headers.get('Content-Encoding')).toBe('br')
|
|
251
|
+
expect(res.headers.get('Vary')).toBe('Accept-Encoding')
|
|
252
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
|
|
253
|
+
expect(await res.text()).toBe('Hello in static/hello.html.br')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('Should return a pre-compressed brotli response - /static/hello.unknown', async () => {
|
|
257
|
+
const app = new App().use(
|
|
258
|
+
'*',
|
|
259
|
+
baseServeStatic({
|
|
260
|
+
getContent,
|
|
261
|
+
precompressed: true,
|
|
262
|
+
})
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
const res = await app.request('/static/hello.unknown', {
|
|
266
|
+
headers: { 'Accept-Encoding': 'wompwomp, gzip, br, deflate, zstd' },
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
expect(res.status).toBe(200)
|
|
270
|
+
expect(res.headers.get('Content-Encoding')).toBe('br')
|
|
271
|
+
expect(res.headers.get('Vary')).toBe('Accept-Encoding')
|
|
272
|
+
expect(res.headers.get('Content-Type')).toBe('application/octet-stream')
|
|
273
|
+
expect(await res.text()).toBe('Hello in static/hello.unknown.br')
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('Should return a pre-compressed response when Accept-Encoding has q-values', async () => {
|
|
277
|
+
const app = new App().use(
|
|
278
|
+
'*',
|
|
279
|
+
baseServeStatic({
|
|
280
|
+
getContent,
|
|
281
|
+
precompressed: true,
|
|
282
|
+
})
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
const res = await app.request('/static/hello.html', {
|
|
286
|
+
headers: { 'Accept-Encoding': 'br;q=0, zstd;q=1.0, gzip;q=0.5' },
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
expect(res.status).toBe(200)
|
|
290
|
+
expect(res.headers.get('Content-Encoding')).toBe('zstd')
|
|
291
|
+
expect(res.headers.get('Vary')).toBe('Accept-Encoding')
|
|
292
|
+
expect(await res.text()).toBe('Hello in static/hello.html.zst')
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
it('Should not return a pre-compressed response - /static/not-found.txt', async () => {
|
|
296
|
+
const app = new App().use(
|
|
297
|
+
'*',
|
|
298
|
+
baseServeStatic({
|
|
299
|
+
getContent,
|
|
300
|
+
precompressed: true,
|
|
301
|
+
})
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
const res = await app.request('/static/not-found.txt', {
|
|
305
|
+
headers: { 'Accept-Encoding': 'gzip, zstd, br' },
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
expect(res.status).toBe(404)
|
|
309
|
+
expect(res.headers.get('Content-Encoding')).toBeNull()
|
|
310
|
+
expect(res.headers.get('Vary')).toBeNull()
|
|
311
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)
|
|
312
|
+
expect(await res.text()).toBe('404 Not Found')
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
it('Should not return a pre-compressed response - /static/hello.html', async () => {
|
|
316
|
+
const app = new App().use(
|
|
317
|
+
'*',
|
|
318
|
+
baseServeStatic({
|
|
319
|
+
getContent,
|
|
320
|
+
precompressed: true,
|
|
321
|
+
})
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
const res = await app.request('/static/hello.html', {
|
|
325
|
+
headers: { 'Accept-Encoding': 'wompwomp, unknown' },
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
expect(res.status).toBe(200)
|
|
329
|
+
expect(res.headers.get('Content-Encoding')).toBeNull()
|
|
330
|
+
expect(res.headers.get('Vary')).toBeNull()
|
|
331
|
+
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
|
|
332
|
+
expect(await res.text()).toBe('Hello in static/hello.html')
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it('Should not find pre-compressed files - /static/hello.jpg', async () => {
|
|
336
|
+
const app = new App().use(
|
|
337
|
+
'*',
|
|
338
|
+
baseServeStatic({
|
|
339
|
+
getContent,
|
|
340
|
+
precompressed: true,
|
|
341
|
+
})
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
const res = await app.request('/static/hello.jpg', {
|
|
345
|
+
headers: { 'Accept-Encoding': 'gzip, br, deflate, zstd' },
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
expect(res.status).toBe(200)
|
|
349
|
+
expect(res.headers.get('Content-Encoding')).toBeNull()
|
|
350
|
+
expect(res.headers.get('Vary')).toBeNull()
|
|
351
|
+
expect(res.headers.get('Content-Type')).toMatch(/^image\/jpeg/)
|
|
352
|
+
expect(await res.text()).toBe('Hello in static/hello.jpg')
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
it('Should return response object content as-is', async () => {
|
|
356
|
+
const body = new ReadableStream()
|
|
357
|
+
const response = new Response(body)
|
|
358
|
+
const app = new App().use(
|
|
359
|
+
'*',
|
|
360
|
+
baseServeStatic({
|
|
361
|
+
getContent: async () => {
|
|
362
|
+
return response
|
|
363
|
+
},
|
|
364
|
+
})
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
const res = await app.fetch({
|
|
368
|
+
method: 'GET',
|
|
369
|
+
url: 'http://localhost',
|
|
370
|
+
} as Request)
|
|
371
|
+
expect(res.status).toBe(200)
|
|
372
|
+
expect(res.body).toBe(body)
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
describe('Changing root path', () => {
|
|
376
|
+
it('Should return the content with absolute root path', async () => {
|
|
377
|
+
const app = new App()
|
|
378
|
+
const serveStatic = baseServeStatic({
|
|
379
|
+
getContent,
|
|
380
|
+
root: '/home/acryl/child',
|
|
381
|
+
})
|
|
382
|
+
app.get('/static/*', serveStatic)
|
|
383
|
+
|
|
384
|
+
const res = await app.request('/static/html/hello.html')
|
|
385
|
+
expect(await res.text()).toBe('Hello in /home/acryl/child/static/html/hello.html')
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
it('Should traverse the directories with absolute root path', async () => {
|
|
389
|
+
const app = new App()
|
|
390
|
+
const serveStatic = baseServeStatic({
|
|
391
|
+
getContent,
|
|
392
|
+
root: '/home/acryl/../parent',
|
|
393
|
+
})
|
|
394
|
+
app.get('/static/*', serveStatic)
|
|
395
|
+
|
|
396
|
+
const res = await app.request('/static/html/hello.html')
|
|
397
|
+
expect(await res.text()).toBe('Hello in /home/parent/static/html/hello.html')
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
it('Should treat the root path includes .. as relative path', async () => {
|
|
401
|
+
const app = new App()
|
|
402
|
+
const serveStatic = baseServeStatic({
|
|
403
|
+
getContent,
|
|
404
|
+
root: '../home/acryl',
|
|
405
|
+
})
|
|
406
|
+
app.get('/static/*', serveStatic)
|
|
407
|
+
|
|
408
|
+
const res = await app.request('/static/html/hello.html')
|
|
409
|
+
expect(await res.text()).toBe('Hello in ../home/acryl/static/html/hello.html')
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
it('Should not allow directory traversal with . as relative path', async () => {
|
|
413
|
+
const app = new App()
|
|
414
|
+
const serveStatic = baseServeStatic({
|
|
415
|
+
getContent,
|
|
416
|
+
root: '.',
|
|
417
|
+
})
|
|
418
|
+
app.get('*', serveStatic)
|
|
419
|
+
|
|
420
|
+
const res = await app.request('/etc/passwd')
|
|
421
|
+
expect(await res.text()).toBe('Hello in etc/passwd')
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
it('Should not allow bypass via path mismatch between middleware and serveStatic', async () => {
|
|
425
|
+
const app = new App()
|
|
426
|
+
|
|
427
|
+
app.use('/admin/*', async (c, next) => {
|
|
428
|
+
c.header('X-Authorized', 'true')
|
|
429
|
+
await next()
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
const serveStatic = baseServeStatic({
|
|
433
|
+
getContent,
|
|
434
|
+
root: '.',
|
|
435
|
+
})
|
|
436
|
+
app.use('/*', serveStatic)
|
|
437
|
+
|
|
438
|
+
const res = await app.request('/admin/secret.txt')
|
|
439
|
+
expect(res.headers.get('X-Authorized')).toBe('true')
|
|
440
|
+
expect(await res.text()).toBe('Hello in admin/secret.txt')
|
|
441
|
+
|
|
442
|
+
const res2 = await app.request('/admin%2Fsecret.txt')
|
|
443
|
+
expect(res2.headers.get('X-Authorized')).toBeNull()
|
|
444
|
+
expect(res2.status).toBe(404)
|
|
445
|
+
|
|
446
|
+
const res3 = await app.request('//admin/secret.txt')
|
|
447
|
+
expect(res3.status).toBe(404)
|
|
448
|
+
})
|
|
449
|
+
})
|
|
450
|
+
})
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* Serve Static Middleware for App.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Context, Data } from '../../context.ts'
|
|
7
|
+
import type { Env, MiddlewareHandler } from '../../types.ts'
|
|
8
|
+
import { parseAccept } from '../../utils/accept.ts'
|
|
9
|
+
import { COMPRESSIBLE_CONTENT_TYPE_REGEX } from '../../utils/compress.ts'
|
|
10
|
+
import { getMimeType } from '../../utils/mime.ts'
|
|
11
|
+
import { tryDecodeURI } from '../../utils/url.ts'
|
|
12
|
+
import { defaultJoin } from './path.ts'
|
|
13
|
+
|
|
14
|
+
export type ServeStaticOptions<E extends Env = Env> = {
|
|
15
|
+
root?: string
|
|
16
|
+
path?: string
|
|
17
|
+
precompressed?: boolean
|
|
18
|
+
/** Allow percent signs in the routed request path. Defaults to false. */
|
|
19
|
+
allowPercentInPath?: boolean
|
|
20
|
+
mimes?: Record<string, string>
|
|
21
|
+
rewriteRequestPath?: (path: string) => string
|
|
22
|
+
onFound?: (path: string, c: Context<E>) => void | Promise<void>
|
|
23
|
+
onNotFound?: (path: string, c: Context<E>) => void | Promise<void>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ENCODINGS = {
|
|
27
|
+
br: '.br',
|
|
28
|
+
zstd: '.zst',
|
|
29
|
+
gzip: '.gz',
|
|
30
|
+
} as const
|
|
31
|
+
const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS) as (keyof typeof ENCODINGS)[]
|
|
32
|
+
|
|
33
|
+
const DEFAULT_DOCUMENT = 'index.html'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* This middleware is not directly used by the user. Create a wrapper specifying `getContent()` by the environment such as Deno or Bun.
|
|
37
|
+
*/
|
|
38
|
+
export const serveStatic = <E extends Env = Env>(
|
|
39
|
+
options: ServeStaticOptions<E> & {
|
|
40
|
+
getContent: (path: string, c: Context<E>) => Promise<Data | Response | null>
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* `join` option according to the runtime. Example `import { join } from 'node:path`. If not specified, it will fall back to the default join function.`
|
|
44
|
+
*/
|
|
45
|
+
join?: (...paths: string[]) => string
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated Currently, `pathResolve` is no longer used.
|
|
48
|
+
*/
|
|
49
|
+
pathResolve?: (path: string) => string
|
|
50
|
+
isDir?: (path: string) => boolean | undefined | Promise<boolean | undefined>
|
|
51
|
+
}
|
|
52
|
+
): MiddlewareHandler => {
|
|
53
|
+
const root = options.root ?? './'
|
|
54
|
+
const optionPath = options.path
|
|
55
|
+
const join = options.join ?? defaultJoin
|
|
56
|
+
|
|
57
|
+
return async (c, next) => {
|
|
58
|
+
// Do nothing if Response is already set
|
|
59
|
+
if (c.finalized) {
|
|
60
|
+
return next()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (c.req.method !== 'GET' && c.req.method !== 'HEAD') {
|
|
64
|
+
return next()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let filename: string
|
|
68
|
+
|
|
69
|
+
if (options.path) {
|
|
70
|
+
filename = options.path
|
|
71
|
+
} else {
|
|
72
|
+
try {
|
|
73
|
+
if (!options.allowPercentInPath && c.req.path.includes('%')) {
|
|
74
|
+
throw new Error()
|
|
75
|
+
}
|
|
76
|
+
filename = tryDecodeURI(c.req.path)
|
|
77
|
+
if (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}|\\/.test(filename)) {
|
|
78
|
+
throw new Error()
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
await options.onNotFound?.(c.req.path, c)
|
|
82
|
+
return next()
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let path = join(
|
|
87
|
+
root,
|
|
88
|
+
!optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
if (options.isDir && (await options.isDir(path))) {
|
|
92
|
+
path = join(path, DEFAULT_DOCUMENT)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const getContent = options.getContent
|
|
96
|
+
let content = await getContent(path, c)
|
|
97
|
+
|
|
98
|
+
if (content instanceof Response) {
|
|
99
|
+
return c.newResponse(content.body, content)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (content != null) {
|
|
103
|
+
const mimeType = (options.mimes && getMimeType(path, options.mimes)) || getMimeType(path)
|
|
104
|
+
c.header('Content-Type', mimeType || 'application/octet-stream')
|
|
105
|
+
|
|
106
|
+
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) {
|
|
107
|
+
const acceptEncodingSet = new Set(
|
|
108
|
+
parseAccept(c.req.header('Accept-Encoding') ?? '')
|
|
109
|
+
.filter((encoding) => encoding.q > 0)
|
|
110
|
+
.map((encoding) => encoding.type)
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
for (const encoding of ENCODINGS_ORDERED_KEYS) {
|
|
114
|
+
if (!acceptEncodingSet.has(encoding)) {
|
|
115
|
+
continue
|
|
116
|
+
}
|
|
117
|
+
const compressedContent = (await getContent(path + ENCODINGS[encoding], c)) as Data | null
|
|
118
|
+
|
|
119
|
+
if (compressedContent) {
|
|
120
|
+
content = compressedContent
|
|
121
|
+
c.header('Content-Encoding', encoding)
|
|
122
|
+
c.header('Vary', 'Accept-Encoding', { append: true })
|
|
123
|
+
break
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
await options.onFound?.(path, c)
|
|
128
|
+
return c.body(content)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
await options.onNotFound?.(path, c)
|
|
132
|
+
await next()
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { join as posixJoin } from 'node:path/posix'
|
|
2
|
+
import { defaultJoin } from './path.ts'
|
|
3
|
+
|
|
4
|
+
describe('defaultJoin', () => {
|
|
5
|
+
describe('Comparison with node:path/posix.join', () => {
|
|
6
|
+
it('Should behave like path.posix.join for all path operations', () => {
|
|
7
|
+
const testCases = [
|
|
8
|
+
// Basic path joining
|
|
9
|
+
['/home/yusuke/work/app/public', 'static/main.html'],
|
|
10
|
+
['public', 'sub/', 'file.html'],
|
|
11
|
+
['', 'file.html'],
|
|
12
|
+
['public', ''],
|
|
13
|
+
['public/', 'file.html'],
|
|
14
|
+
['public', 'static', 'main.html'],
|
|
15
|
+
['assets', 'images', 'logo.png'],
|
|
16
|
+
|
|
17
|
+
// Parent directory references
|
|
18
|
+
['public', '../parent/file.html'],
|
|
19
|
+
['public', '../../grandparent/file.html'],
|
|
20
|
+
['/abs/path', '../relative.html'],
|
|
21
|
+
['a/b', '../c'],
|
|
22
|
+
['a/b/c', '../../d'],
|
|
23
|
+
|
|
24
|
+
// Current directory references
|
|
25
|
+
['./public', 'static/main.html'],
|
|
26
|
+
['public', './file.html'],
|
|
27
|
+
['./public', '/absolute/path.html'],
|
|
28
|
+
['.', 'file.html'],
|
|
29
|
+
['public/.', 'file.html'],
|
|
30
|
+
|
|
31
|
+
// Edge cases
|
|
32
|
+
[],
|
|
33
|
+
['.'],
|
|
34
|
+
[''],
|
|
35
|
+
['/'],
|
|
36
|
+
['a', 'b', 'c'],
|
|
37
|
+
|
|
38
|
+
// Backslash handling (security)
|
|
39
|
+
['static', 'test\\file.txt'],
|
|
40
|
+
['public', 'path\\with\\backslash'],
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
testCases.forEach(([...args]) => {
|
|
44
|
+
const expected = posixJoin(...args)
|
|
45
|
+
const actual = defaultJoin(...args)
|
|
46
|
+
expect(actual).toBe(expected)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `defaultJoin` does not support Windows paths and always uses `/` separators.
|
|
3
|
+
* If you need Windows path support, please use `join` exported from `node:path` etc. instead.
|
|
4
|
+
*/
|
|
5
|
+
export const defaultJoin = (...paths: string[]): string => {
|
|
6
|
+
// Join non-empty paths with '/'
|
|
7
|
+
let result = paths.filter((p) => p !== '').join('/')
|
|
8
|
+
|
|
9
|
+
// Normalize multiple slashes to single slash
|
|
10
|
+
result = result.replace(/(?<=\/)\/+/g, '')
|
|
11
|
+
|
|
12
|
+
// Handle path resolution (. and ..)
|
|
13
|
+
const segments = result.split('/')
|
|
14
|
+
const resolved = []
|
|
15
|
+
|
|
16
|
+
for (const segment of segments) {
|
|
17
|
+
if (segment === '..' && resolved.length > 0 && resolved.at(-1) !== '..') {
|
|
18
|
+
resolved.pop()
|
|
19
|
+
} else if (segment !== '.') {
|
|
20
|
+
resolved.push(segment)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return resolved.join('/') || '.'
|
|
25
|
+
}
|