acrylblends 0.6.3 → 0.7.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.
Files changed (285) hide show
  1. package/README.md +4 -3
  2. package/docs/framework/README.md +2 -2
  3. package/docs/framework/UPSTREAM.md +1 -1
  4. package/docs/framework/guides/jsx.md +51 -1
  5. package/docs/framework/helpers/ssg.md +3 -6
  6. package/docs/framework/middleware/builtin/jsx-renderer.md +0 -3
  7. package/docs/framework/upstream.json +2 -2
  8. package/lib/accepts.d.ts +1 -1
  9. package/lib/acryl-base.d.ts +1 -1
  10. package/lib/adapter.d.ts +1 -1
  11. package/lib/{app-wFwpg-bt.d.ts → app-D8riatvv.d.ts} +1 -1
  12. package/lib/{base-ddiVmiE7.js → base-NSN_SNv-.js} +1 -0
  13. package/lib/basic-auth.d.ts +1 -1
  14. package/lib/bearer-auth.d.ts +1 -1
  15. package/lib/body-limit.d.ts +1 -1
  16. package/lib/cache.d.ts +1 -1
  17. package/lib/cache.js +4 -4
  18. package/lib/{client-BHTr318g.js → client-DDLXwhBo.js} +2 -125
  19. package/lib/client.d.ts +2 -2
  20. package/lib/client.js +2 -1
  21. package/lib/combine.d.ts +1 -1
  22. package/lib/{components-DYZdxJIJ.js → components-JXVm45kK.js} +1 -1
  23. package/lib/{components-Ce5FRyHU.js → components-cEW3ovK0.js} +3 -3
  24. package/lib/compress.d.ts +1 -1
  25. package/lib/conninfo.d.ts +2 -2
  26. package/lib/{context-DVQBjuns.d.ts → context-DS3fYyG-.d.ts} +1 -1
  27. package/lib/context-storage.d.ts +1 -1
  28. package/lib/cookie.d.ts +1 -1
  29. package/lib/cors.d.ts +1 -1
  30. package/lib/csrf.d.ts +1 -1
  31. package/lib/dev.d.ts +2 -2
  32. package/lib/etag.d.ts +1 -1
  33. package/lib/factory.d.ts +2 -2
  34. package/lib/{hooks-Bq5q9IMl.js → hooks-w72iLeFR.js} +6 -6
  35. package/lib/{index-CUTfU69R.d.ts → index-Bb7YJKCq.d.ts} +2 -2
  36. package/lib/{index-CsvB-F9j.d.ts → index-BvFh1Gfx.d.ts} +1 -1
  37. package/lib/{index-Cqcdluyn.d.ts → index-CTy4N7ng.d.ts} +2 -2
  38. package/lib/index.d.ts +4 -4
  39. package/lib/ip-restriction.d.ts +2 -2
  40. package/lib/jsx/dom/client.js +1 -1
  41. package/lib/jsx/dom/jsx-dev-runtime.js +1 -1
  42. package/lib/jsx/dom/jsx-runtime.js +1 -1
  43. package/lib/jsx/dom/server.js +2 -2
  44. package/lib/jsx/dom.js +4 -4
  45. package/lib/jsx/jsx-dev-runtime.js +1 -1
  46. package/lib/jsx/jsx-runtime.js +1 -1
  47. package/lib/jsx/streaming.js +1 -1
  48. package/lib/{jsx-dev-runtime-kAhR_4C7.js → jsx-dev-runtime-CWLRA2F3.js} +2 -2
  49. package/lib/jsx-renderer.d.ts +75 -0
  50. package/lib/jsx-renderer.js +99 -0
  51. package/lib/jsx.js +4 -4
  52. package/lib/jwk.d.ts +1 -1
  53. package/lib/jwt.d.ts +1 -1
  54. package/lib/language.d.ts +1 -1
  55. package/lib/logger.d.ts +1 -1
  56. package/lib/loopback-only.d.ts +1 -1
  57. package/lib/method-not-allowed.d.ts +2 -2
  58. package/lib/method-override.d.ts +2 -2
  59. package/lib/mount.d.ts +1 -1
  60. package/lib/node-server/conninfo.d.ts +1 -1
  61. package/lib/node-server/register.d.ts +1 -0
  62. package/lib/node-server/register.js +73 -0
  63. package/lib/node-server/serve-static.d.ts +1 -1
  64. package/lib/node-server/serve-static.js +1 -1
  65. package/lib/node-server.d.ts +1 -1
  66. package/lib/node-server.js +1 -1
  67. package/lib/powered-by.d.ts +1 -1
  68. package/lib/pretty-json.d.ts +1 -1
  69. package/lib/quick.d.ts +1 -1
  70. package/lib/request-id.d.ts +1 -1
  71. package/lib/request.d.ts +1 -1
  72. package/lib/route.d.ts +1 -1
  73. package/lib/schema-validator.d.ts +1 -1
  74. package/lib/secure-headers.d.ts +1 -1
  75. package/lib/{serve-static-Cw23shXv.js → serve-static-B14MF1Va.js} +1 -0
  76. package/lib/serve-static.d.ts +1 -1
  77. package/lib/ssg-BwjcA-Ge.js +389 -0
  78. package/lib/ssg.d.ts +156 -0
  79. package/lib/ssg.js +2 -0
  80. package/lib/streaming.d.ts +1 -1
  81. package/lib/testing.d.ts +3 -3
  82. package/lib/testing.js +1 -1
  83. package/lib/timeout.d.ts +1 -1
  84. package/lib/timing.d.ts +1 -1
  85. package/lib/tiny.d.ts +1 -1
  86. package/lib/trailing-slash.d.ts +1 -1
  87. package/lib/{types-nEL8VPDC.d.ts → types-Dv_nV2cv.d.ts} +1 -1
  88. package/lib/{types-CQiGmsWW.d.ts → types-zNxyEpG2.d.ts} +2 -2
  89. package/lib/types.d.ts +1 -1
  90. package/lib/utils/body.d.ts +1 -1
  91. package/lib/utils/ipaddr.d.ts +1 -1
  92. package/lib/utils-Cx61GFMC.js +125 -0
  93. package/lib/validator.d.ts +1 -1
  94. package/lib/ws.d.ts +1 -1
  95. package/package.json +13 -1
  96. package/plugins/acryl-inspect/upstream.json +1 -1
  97. package/plugins/acryl-node-server/src/register.ts +86 -0
  98. package/plugins/acryl-node-server/upstream.json +5 -3
  99. package/plugins/acryl-router/src/core/helper/ssg/index.ts +14 -0
  100. package/plugins/acryl-router/src/core/helper/ssg/middleware.ts +83 -0
  101. package/plugins/acryl-router/src/core/helper/ssg/plugins.ts +72 -0
  102. package/plugins/acryl-router/src/core/helper/ssg/ssg.ts +471 -0
  103. package/plugins/acryl-router/src/core/helper/ssg/utils.ts +118 -0
  104. package/plugins/acryl-router/src/core/jsx/dom/render.ts +6 -6
  105. package/plugins/acryl-router/src/core/middleware/cache/index.ts +4 -4
  106. package/plugins/acryl-router/src/core/middleware/jsx-renderer/index.ts +162 -0
  107. package/plugins/acryl-router/src/helpers/index.ts +1 -0
  108. package/plugins/acryl-router/src/middleware/index.ts +1 -0
  109. package/plugins/acryl-router/upstream.json +2 -2
  110. package/plugins/acryl-router-http/upstream.json +1 -1
  111. package/plugins/acryl-state/upstream.json +1 -1
  112. package/plugins/desktop/main.ts +4 -1
  113. package/plugins/desktop/upstream.json +1 -1
  114. package/plugins/desktop-web/main.ts +3 -1
  115. package/plugins/desktop-web/upstream.json +1 -1
  116. package/plugins/extensions/resolve.ts +4 -0
  117. package/shared/AGENTS.core.md +3 -0
  118. package/templates/agent/package.json +7 -3
  119. package/templates/agent/tsconfig.json +10 -2
  120. package/templates/blank/package.json +7 -3
  121. package/templates/blank/src/about.tsx +14 -0
  122. package/templates/blank/src/routes.ts +2 -0
  123. package/templates/blank/test/app.test.ts +15 -0
  124. package/templates/blank/tsconfig.json +3 -1
  125. package/templates/todo/package.json +7 -3
  126. package/templates/todo/tsconfig.json +3 -1
  127. package/templates/todo/upstream.json +1 -1
  128. package/plugins/acryl-inspect/tests/report.spec.ts +0 -69
  129. package/plugins/acryl-inspect/tests/service.spec.ts +0 -121
  130. package/plugins/acryl-node-server/tests/adapter/app.ts +0 -76
  131. package/plugins/acryl-node-server/tests/adapter/assets/.static/plain.txt +0 -1
  132. package/plugins/acryl-node-server/tests/adapter/assets/favicon.ico +0 -0
  133. package/plugins/acryl-node-server/tests/adapter/assets/secret.txt +0 -1
  134. package/plugins/acryl-node-server/tests/adapter/assets/static/acryl.html +0 -1
  135. package/plugins/acryl-node-server/tests/adapter/assets/static/admin/secret.txt +0 -1
  136. package/plugins/acryl-node-server/tests/adapter/assets/static/data.json +0 -5
  137. package/plugins/acryl-node-server/tests/adapter/assets/static/extensionless +0 -1
  138. package/plugins/acryl-node-server/tests/adapter/assets/static/foo..bar.txt +0 -0
  139. package/plugins/acryl-node-server/tests/adapter/assets/static/index.html +0 -1
  140. package/plugins/acryl-node-server/tests/adapter/assets/static/plain.txt +0 -1
  141. package/plugins/acryl-node-server/tests/adapter/assets/static-with-context-path/plain.txt +0 -1
  142. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin +0 -1
  143. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin.br +0 -1
  144. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt +0 -1
  145. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.br +0 -1
  146. package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.zst +0 -1
  147. package/plugins/acryl-node-server/tests/adapter/conninfo.test.ts +0 -34
  148. package/plugins/acryl-node-server/tests/adapter/content-length.test.ts +0 -248
  149. package/plugins/acryl-node-server/tests/adapter/early-hints.test.ts +0 -457
  150. package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-cert.pem +0 -23
  151. package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-key.pem +0 -27
  152. package/plugins/acryl-node-server/tests/adapter/headers.test.ts +0 -252
  153. package/plugins/acryl-node-server/tests/adapter/helpers/request.ts +0 -190
  154. package/plugins/acryl-node-server/tests/adapter/listener.test.ts +0 -727
  155. package/plugins/acryl-node-server/tests/adapter/request-client-disconnect.test.ts +0 -189
  156. package/plugins/acryl-node-server/tests/adapter/request.test.ts +0 -1230
  157. package/plugins/acryl-node-server/tests/adapter/response.test.ts +0 -285
  158. package/plugins/acryl-node-server/tests/adapter/serve-static.test.ts +0 -849
  159. package/plugins/acryl-node-server/tests/adapter/server-socket.test.ts +0 -790
  160. package/plugins/acryl-node-server/tests/adapter/server.test.ts +0 -1312
  161. package/plugins/acryl-node-server/tests/adapter/setup.ts +0 -16
  162. package/plugins/acryl-node-server/tests/adapter/url.test.ts +0 -76
  163. package/plugins/acryl-node-server/tests/adapter/utils/response.test.ts +0 -23
  164. package/plugins/acryl-node-server/tests/adapter/utils/stream.test.ts +0 -75
  165. package/plugins/acryl-node-server/tests/adapter/utils.test.ts +0 -235
  166. package/plugins/acryl-node-server/tests/adapter/websocket.test.ts +0 -260
  167. package/plugins/acryl-node-server/tests/server.spec.ts +0 -141
  168. package/plugins/acryl-router/src/core/app.test.ts +0 -3833
  169. package/plugins/acryl-router/src/core/client/client.test.ts +0 -2313
  170. package/plugins/acryl-router/src/core/client/types.test.ts +0 -149
  171. package/plugins/acryl-router/src/core/client/utils.test.ts +0 -405
  172. package/plugins/acryl-router/src/core/compose.test.ts +0 -751
  173. package/plugins/acryl-router/src/core/context.test.ts +0 -629
  174. package/plugins/acryl-router/src/core/helper/accepts/accepts.test.ts +0 -344
  175. package/plugins/acryl-router/src/core/helper/adapter/index.test.ts +0 -55
  176. package/plugins/acryl-router/src/core/helper/cookie/index.test.ts +0 -510
  177. package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +0 -506
  178. package/plugins/acryl-router/src/core/helper/css/index.test.tsx +0 -678
  179. package/plugins/acryl-router/src/core/helper/dev/index.test.ts +0 -181
  180. package/plugins/acryl-router/src/core/helper/factory/index.test.ts +0 -434
  181. package/plugins/acryl-router/src/core/helper/html/index.test.ts +0 -78
  182. package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +0 -356
  183. package/plugins/acryl-router/src/core/helper/route/index.test.ts +0 -259
  184. package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +0 -557
  185. package/plugins/acryl-router/src/core/helper/streaming/stream.test.ts +0 -128
  186. package/plugins/acryl-router/src/core/helper/streaming/text.test.ts +0 -34
  187. package/plugins/acryl-router/src/core/helper/testing/index.test.ts +0 -42
  188. package/plugins/acryl-router/src/core/helper/websocket/index.test.ts +0 -124
  189. package/plugins/acryl-router/src/core/http-exception.test.ts +0 -45
  190. package/plugins/acryl-router/src/core/jsx/base.test.tsx +0 -97
  191. package/plugins/acryl-router/src/core/jsx/children.test.ts +0 -52
  192. package/plugins/acryl-router/src/core/jsx/components.test.tsx +0 -936
  193. package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +0 -655
  194. package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +0 -128
  195. package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +0 -330
  196. package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +0 -184
  197. package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +0 -149
  198. package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +0 -172
  199. package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +0 -3681
  200. package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +0 -1344
  201. package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +0 -199
  202. package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +0 -859
  203. package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +0 -40
  204. package/plugins/acryl-router/src/core/jsx/index.test.tsx +0 -1744
  205. package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +0 -666
  206. package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +0 -85
  207. package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +0 -1281
  208. package/plugins/acryl-router/src/core/jsx/utils.test.ts +0 -366
  209. package/plugins/acryl-router/src/core/middleware/basic-auth/index.test.ts +0 -434
  210. package/plugins/acryl-router/src/core/middleware/bearer-auth/index.test.ts +0 -1018
  211. package/plugins/acryl-router/src/core/middleware/body-limit/index.test.ts +0 -273
  212. package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +0 -1211
  213. package/plugins/acryl-router/src/core/middleware/combine/index.test.ts +0 -355
  214. package/plugins/acryl-router/src/core/middleware/compress/index.test.ts +0 -535
  215. package/plugins/acryl-router/src/core/middleware/context-storage/index.test.ts +0 -43
  216. package/plugins/acryl-router/src/core/middleware/cors/index.test.ts +0 -532
  217. package/plugins/acryl-router/src/core/middleware/csrf/index.test.ts +0 -534
  218. package/plugins/acryl-router/src/core/middleware/etag/digest.test.ts +0 -21
  219. package/plugins/acryl-router/src/core/middleware/etag/index.test.ts +0 -503
  220. package/plugins/acryl-router/src/core/middleware/ip-restriction/index.test.ts +0 -217
  221. package/plugins/acryl-router/src/core/middleware/jwk/index.test.ts +0 -1135
  222. package/plugins/acryl-router/src/core/middleware/jwk/keys.test.json +0 -48
  223. package/plugins/acryl-router/src/core/middleware/jwt/index.test.ts +0 -802
  224. package/plugins/acryl-router/src/core/middleware/language/index.test.ts +0 -552
  225. package/plugins/acryl-router/src/core/middleware/logger/index.test.ts +0 -204
  226. package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.test.ts +0 -292
  227. package/plugins/acryl-router/src/core/middleware/method-override/index.test.ts +0 -221
  228. package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +0 -334
  229. package/plugins/acryl-router/src/core/middleware/powered-by/index.test.ts +0 -37
  230. package/plugins/acryl-router/src/core/middleware/pretty-json/index.test.ts +0 -218
  231. package/plugins/acryl-router/src/core/middleware/request-id/index.test.ts +0 -168
  232. package/plugins/acryl-router/src/core/middleware/secure-headers/index.test.ts +0 -671
  233. package/plugins/acryl-router/src/core/middleware/serve-static/index.test.ts +0 -450
  234. package/plugins/acryl-router/src/core/middleware/serve-static/path.test.ts +0 -50
  235. package/plugins/acryl-router/src/core/middleware/timeout/index.test.ts +0 -70
  236. package/plugins/acryl-router/src/core/middleware/timing/index.test.ts +0 -310
  237. package/plugins/acryl-router/src/core/middleware/trailing-slash/index.test.ts +0 -381
  238. package/plugins/acryl-router/src/core/preset/quick.test.ts +0 -42
  239. package/plugins/acryl-router/src/core/preset/tiny.test.ts +0 -42
  240. package/plugins/acryl-router/src/core/request.test.ts +0 -751
  241. package/plugins/acryl-router/src/core/router/common.case.test.ts +0 -1132
  242. package/plugins/acryl-router/src/core/router/linear-router/router.test.ts +0 -80
  243. package/plugins/acryl-router/src/core/router/pattern-router/router.test.ts +0 -46
  244. package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.test.ts +0 -154
  245. package/plugins/acryl-router/src/core/router/reg-exp-router/router.test.ts +0 -265
  246. package/plugins/acryl-router/src/core/router/smart-router/router.test.ts +0 -13
  247. package/plugins/acryl-router/src/core/router/trie-router/node.test.ts +0 -896
  248. package/plugins/acryl-router/src/core/router/trie-router/router.test.ts +0 -8
  249. package/plugins/acryl-router/src/core/types.test.ts +0 -4134
  250. package/plugins/acryl-router/src/core/utils/accept.test.ts +0 -299
  251. package/plugins/acryl-router/src/core/utils/basic-auth.test.ts +0 -56
  252. package/plugins/acryl-router/src/core/utils/body.test.ts +0 -496
  253. package/plugins/acryl-router/src/core/utils/buffer.test.ts +0 -147
  254. package/plugins/acryl-router/src/core/utils/color.test.ts +0 -23
  255. package/plugins/acryl-router/src/core/utils/concurrent.test.ts +0 -69
  256. package/plugins/acryl-router/src/core/utils/cookie.test.ts +0 -460
  257. package/plugins/acryl-router/src/core/utils/crypto.test.ts +0 -43
  258. package/plugins/acryl-router/src/core/utils/encode.test.ts +0 -61
  259. package/plugins/acryl-router/src/core/utils/filepath.test.ts +0 -78
  260. package/plugins/acryl-router/src/core/utils/html.test.ts +0 -16
  261. package/plugins/acryl-router/src/core/utils/ipaddr.test.ts +0 -149
  262. package/plugins/acryl-router/src/core/utils/jwt/jwa.test.ts +0 -86
  263. package/plugins/acryl-router/src/core/utils/jwt/jwt.test.ts +0 -1788
  264. package/plugins/acryl-router/src/core/utils/mime.test.ts +0 -64
  265. package/plugins/acryl-router/src/core/utils/stream.test.ts +0 -212
  266. package/plugins/acryl-router/src/core/utils/types.test.ts +0 -295
  267. package/plugins/acryl-router/src/core/utils/url.test.ts +0 -384
  268. package/plugins/acryl-router/src/core/validator/utils.test.ts +0 -82
  269. package/plugins/acryl-router/src/core/validator/validator.test.ts +0 -1488
  270. package/plugins/acryl-router/tests/docs-examples.spec.ts +0 -152
  271. package/plugins/acryl-router/tests/jsx.spec.tsx +0 -63
  272. package/plugins/acryl-router/tests/live-install.spec.ts +0 -64
  273. package/plugins/acryl-router/tests/loader.spec.ts +0 -72
  274. package/plugins/acryl-router/tests/loopback-only.spec.ts +0 -80
  275. package/plugins/acryl-router/tests/no-dom/consumer.ts +0 -11
  276. package/plugins/acryl-router/tests/no-dom/tsconfig.json +0 -13
  277. package/plugins/acryl-router/tests/service.spec.ts +0 -426
  278. package/plugins/acryl-router/tests/shadow-state.spec.ts +0 -79
  279. package/plugins/acryl-router-http/tests/adapter.spec.ts +0 -174
  280. package/plugins/acryl-router-http/tests/stream.spec.ts +0 -133
  281. package/plugins/acryl-router-http/tests/surfaces.spec.ts +0 -101
  282. package/plugins/acryl-state/src/react.ts +0 -11
  283. package/plugins/acryl-state/tests/cell.spec.ts +0 -71
  284. package/plugins/acryl-state/tests/react.spec.ts +0 -39
  285. package/plugins/acryl-state/tests/service.spec.ts +0 -132
@@ -1,849 +0,0 @@
1
- import { Acryl } from '../../../acryl-router/src/index.ts'
2
- import type * as fs from 'node:fs'
3
- import {
4
- chmodSync,
5
- createReadStream,
6
- mkdirSync,
7
- mkdtempSync,
8
- rmSync,
9
- statSync,
10
- symlinkSync,
11
- writeFileSync,
12
- } from 'node:fs'
13
- import { tmpdir } from 'node:os'
14
- import path from 'node:path'
15
- import { serveStatic } from '../../src/adapter/serve-static.ts'
16
- import { createAdaptorServer } from '../../src/adapter/server.ts'
17
- import { requestServer } from './helpers/request.ts'
18
-
19
- vi.mock('node:fs', async (importOriginal) => {
20
- const originalFs = await importOriginal<typeof fs>()
21
- return {
22
- ...originalFs,
23
- statSync: vi.fn(originalFs.statSync),
24
- createReadStream: vi.fn(originalFs.createReadStream),
25
- }
26
- })
27
-
28
- describe('Serve Static Middleware', () => {
29
- const app = new Acryl<{
30
- Variables: {
31
- path: string
32
- }
33
- }>()
34
-
35
- app.use(
36
- '/static/*',
37
- serveStatic({
38
- root: './tests/adapter/assets',
39
- onFound: (path, c) => {
40
- c.header('X-Custom', `Found the file at ${path}`)
41
- if (c.req.query('type')) {
42
- c.header('Content-Type', c.req.query('type'))
43
- }
44
- },
45
- })
46
- )
47
- app.use('/favicon.ico', serveStatic({ path: './tests/adapter/assets/favicon.ico' }))
48
- app.use(
49
- '/dot-static/*',
50
- serveStatic({
51
- root: './tests/adapter/assets',
52
- rewriteRequestPath: (path) => path.replace(/^\/dot-static/, '/.static'),
53
- })
54
- )
55
-
56
- app.use(
57
- '/static-with-context-path-route/*',
58
- async (c, next) => {
59
- c.set('path', '/static-with-context-path')
60
- await next()
61
- },
62
- serveStatic({
63
- root: './tests/adapter/assets',
64
- rewriteRequestPath: (path, c) =>
65
- path.replace('static-with-context-path-route', c.get('path')),
66
- })
67
- )
68
-
69
- let notFoundMessage = ''
70
- app.use(
71
- '/on-not-found/*',
72
- serveStatic({
73
- root: './not-found',
74
- onNotFound: (path, c) => {
75
- notFoundMessage = `${path} is not found, request to ${c.req.path}`
76
- },
77
- })
78
- )
79
-
80
- app.use(
81
- '/static-with-precompressed/*',
82
- serveStatic({
83
- root: './tests/adapter/assets',
84
- precompressed: true,
85
- })
86
- )
87
-
88
- const server = createAdaptorServer(app)
89
-
90
- describe('HTTP methods', () => {
91
- beforeEach(() => {
92
- vi.mocked(statSync).mockClear()
93
- vi.mocked(createReadStream).mockClear()
94
- })
95
-
96
- it.each(['POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'PROPFIND'])(
97
- 'Should pass %s requests to the next handler without accessing files',
98
- async (method) => {
99
- const rewriteRequestPath = vi.fn((path: string) => path)
100
- const onFound = vi.fn()
101
- const onNotFound = vi.fn()
102
- const app = new Acryl().use(
103
- '/static/*',
104
- serveStatic({ root: './tests/adapter/assets', rewriteRequestPath, onFound, onNotFound })
105
- )
106
- app.on(method, '/static/plain.txt', (c) => c.text('Handled downstream', 202))
107
-
108
- const res = await requestServer(createAdaptorServer(app), {
109
- method,
110
- path: '/static/plain.txt',
111
- })
112
-
113
- expect(res.status).toBe(202)
114
- expect(await res.text()).toBe('Handled downstream')
115
- expect(rewriteRequestPath).not.toHaveBeenCalled()
116
- expect(statSync).not.toHaveBeenCalled()
117
- expect(createReadStream).not.toHaveBeenCalled()
118
- expect(onFound).not.toHaveBeenCalled()
119
- expect(onNotFound).not.toHaveBeenCalled()
120
- }
121
- )
122
-
123
- it.each(['/static/plain.txt', '/static/does-not-exist.html'])(
124
- 'Should return 404 for an unhandled POST request regardless of file existence - %s',
125
- async (path) => {
126
- const res = await requestServer(server, { method: 'POST', path })
127
-
128
- expect(res.status).toBe(404)
129
- expect(await res.text()).toBe('404 Not Found')
130
- expect(statSync).not.toHaveBeenCalled()
131
- expect(createReadStream).not.toHaveBeenCalled()
132
- }
133
- )
134
- })
135
-
136
- it('Should return index.html', async () => {
137
- const res = await requestServer(server, { method: 'GET', path: '/static/' })
138
- expect(res.status).toBe(200)
139
- expect(await res.text()).toBe('<h1>Hello Acryl</h1>')
140
- expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
141
- expect(res.headers.get('x-custom')).toMatch(
142
- /Found the file at tests[\/\\]adapter[\/\\]assets[\/\\]static[\/\\]index\.html$/
143
- )
144
- })
145
-
146
- it('Should return acryl.html', async () => {
147
- const res = await requestServer(server, { method: 'GET', path: '/static/acryl.html' })
148
- expect(res.status).toBe(200)
149
- expect(await res.text()).toBe('<h1>This is Acryl.html</h1>')
150
- expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
151
- })
152
-
153
- it('Should return correct headers for icons', async () => {
154
- const res = await requestServer(server, { method: 'GET', path: '/favicon.ico' })
155
- expect(res.status).toBe(200)
156
- expect(res.headers.get('content-type')).toBe('image/x-icon')
157
- })
158
-
159
- it('Should return correct headers and data for json files', async () => {
160
- const res = await requestServer(server, { method: 'GET', path: '/static/data.json' })
161
- expect(res.status).toBe(200)
162
- expect(await res.json()).toEqual({
163
- id: 1,
164
- name: 'Foo Bar',
165
- flag: true,
166
- })
167
- expect(res.headers.get('content-type')).toBe('application/json')
168
- })
169
-
170
- it('Should return correct headers and data for text', async () => {
171
- const res = await requestServer(server, { method: 'GET', path: '/static/plain.txt' })
172
- const stats = statSync(path.join(__dirname, 'assets', 'static', 'plain.txt'))
173
- expect(res.status).toBe(200)
174
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
175
- expect(res.headers.get('last-modified')).toBe(stats.mtime.toUTCString())
176
- expect(await res.text()).toBe('This is plain.txt')
177
- })
178
-
179
- it('Should return 404 for non-existent files', async () => {
180
- const res = await requestServer(server, {
181
- method: 'GET',
182
- path: '/static/does-not-exist.html',
183
- })
184
- expect(res.status).toBe(404)
185
- expect(res.headers.get('content-type')).toBe('text/plain; charset=UTF-8')
186
- expect(await res.text()).toBe('404 Not Found')
187
- })
188
-
189
- it('Should return 200 with rewriteRequestPath', async () => {
190
- const res = await requestServer(server, { method: 'GET', path: '/dot-static/plain.txt' })
191
- expect(res.status).toBe(200)
192
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
193
- expect(await res.text()).toBe('This is plain.txt')
194
- })
195
-
196
- it('Should return 404 with rewriteRequestPath', async () => {
197
- const res = await requestServer(server, {
198
- method: 'GET',
199
- path: '/dot-static/does-no-exists.txt',
200
- })
201
- expect(res.status).toBe(404)
202
- })
203
-
204
- it('Should return 200 with rewriteRequestPath with the context', async () => {
205
- const res = await requestServer(server, {
206
- method: 'GET',
207
- path: '/static-with-context-path-route/plain.txt',
208
- })
209
- expect(res.status).toBe(200)
210
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
211
- expect(await res.text()).toBe('This is plain.txt')
212
- })
213
-
214
- it('Should return 200 response to HEAD request', async () => {
215
- const res = await requestServer(server, { method: 'HEAD', path: '/static/plain.txt' })
216
- const stats = statSync(path.join(__dirname, 'assets', 'static', 'plain.txt'))
217
- expect(res.status).toBe(200)
218
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
219
- expect(res.headers.get('content-length')).toBe('17')
220
- expect(res.headers.get('last-modified')).toBe(stats.mtime.toUTCString())
221
- expect(res.body).toBeNull()
222
- })
223
-
224
- it('Should return correct headers and data with range headers', async () => {
225
- let res = await requestServer(server, {
226
- method: 'GET',
227
- path: '/static/plain.txt',
228
- headers: { range: '0-9' },
229
- })
230
- const stats = statSync(path.join(__dirname, 'assets', 'static', 'plain.txt'))
231
- expect(res.status).toBe(206)
232
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
233
- expect(res.headers.get('content-length')).toBe('10')
234
- expect(res.headers.get('content-range')).toBe('bytes 0-9/17')
235
- expect(res.headers.get('last-modified')).toBe(stats.mtime.toUTCString())
236
- expect(res.headers.get('date')).not.toBe(stats.mtime.toUTCString())
237
- expect(res.headers.get('date')).not.toBe(stats.birthtime.toUTCString())
238
- let text = await res.text()
239
- expect(text.length).toBe(10)
240
- expect(text).toBe('This is pl')
241
-
242
- res = await requestServer(server, {
243
- method: 'GET',
244
- path: '/static/plain.txt',
245
- headers: { range: '10-16' },
246
- })
247
- expect(res.status).toBe(206)
248
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
249
- expect(res.headers.get('content-length')).toBe('7')
250
- expect(res.headers.get('content-range')).toBe('bytes 10-16/17')
251
- text = await res.text()
252
- expect(text.length).toBe(7)
253
- expect(text).toBe('ain.txt')
254
- })
255
-
256
- it('Should return correct headers and data if client range exceeds the data size', async () => {
257
- const res = await requestServer(server, {
258
- method: 'GET',
259
- path: '/static/plain.txt',
260
- headers: { range: '0-20' },
261
- })
262
- expect(res.status).toBe(206)
263
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
264
- expect(res.headers.get('content-length')).toBe('17')
265
- expect(res.headers.get('content-range')).toBe('bytes 0-16/17')
266
- const text = await res.text()
267
- expect(text.length).toBe(17)
268
- expect(text).toBe('This is plain.txt')
269
- })
270
-
271
- it('Should handle invalid range header gracefully without NaN error', async () => {
272
- const res = await requestServer(server, {
273
- method: 'GET',
274
- path: '/static/plain.txt',
275
- headers: { range: 'hello' },
276
- })
277
- expect(res.status).toBe(206)
278
- expect(res.headers.get('content-length')).toBe('17')
279
- expect(res.headers.get('content-range')).toBe('bytes 0-16/17')
280
- })
281
-
282
- it('Should return the last N bytes for a suffix range', async () => {
283
- const res = await requestServer(server, {
284
- method: 'GET',
285
- path: '/static/plain.txt',
286
- headers: { range: 'bytes=-5' },
287
- })
288
- expect(res.status).toBe(206)
289
- expect(res.headers.get('content-length')).toBe('5')
290
- expect(res.headers.get('content-range')).toBe('bytes 12-16/17')
291
- expect(await res.text()).toBe('n.txt')
292
- })
293
-
294
- it('Should return the whole file for a suffix range exceeding the file size', async () => {
295
- const res = await requestServer(server, {
296
- method: 'GET',
297
- path: '/static/plain.txt',
298
- headers: { range: 'bytes=-100' },
299
- })
300
- expect(res.status).toBe(206)
301
- expect(res.headers.get('content-range')).toBe('bytes 0-16/17')
302
- expect(await res.text()).toBe('This is plain.txt')
303
- })
304
-
305
- it('Should return exactly 1 byte for range bytes=0-0', async () => {
306
- const res = await requestServer(server, {
307
- method: 'GET',
308
- path: '/static/plain.txt',
309
- headers: { range: 'bytes=0-0' },
310
- })
311
- expect(res.status).toBe(206)
312
- expect(res.headers.get('content-length')).toBe('1')
313
- expect(res.headers.get('content-range')).toBe('bytes 0-0/17')
314
- expect(await res.text()).toBe('T')
315
- })
316
-
317
- it('Should return 416 when the range start is beyond the end of the file', async () => {
318
- const res = await requestServer(server, {
319
- method: 'GET',
320
- path: '/static/plain.txt',
321
- headers: { range: 'bytes=100-200' },
322
- })
323
- expect(res.status).toBe(416)
324
- expect(res.headers.get('content-range')).toBe('bytes */17')
325
- })
326
-
327
- it('Should return 416 when the range start is beyond the file size, even if the window is small', async () => {
328
- const res = await requestServer(server, {
329
- method: 'GET',
330
- path: '/static/plain.txt',
331
- headers: { range: 'bytes=20-25' },
332
- })
333
- expect(res.status).toBe(416)
334
- expect(res.headers.get('content-range')).toBe('bytes */17')
335
- })
336
-
337
- it('Should return 416 when the range start is after the range end', async () => {
338
- const res = await requestServer(server, {
339
- method: 'GET',
340
- path: '/static/plain.txt',
341
- headers: { range: 'bytes=10-5' },
342
- })
343
- expect(res.status).toBe(416)
344
- expect(res.headers.get('content-range')).toBe('bytes */17')
345
- })
346
-
347
- it('Should return 416 for a zero-length suffix range', async () => {
348
- const res = await requestServer(server, {
349
- method: 'GET',
350
- path: '/static/plain.txt',
351
- headers: { range: 'bytes=-0' },
352
- })
353
- expect(res.status).toBe(416)
354
- expect(res.headers.get('content-range')).toBe('bytes */17')
355
- })
356
-
357
- it.each(['bytes=0-1x', 'bytes=x-1', 'bytes=0-1-2', 'bytes=-5-extra'])(
358
- 'Should treat a malformed range as the whole file: %s',
359
- async (range) => {
360
- const res = await requestServer(server, {
361
- method: 'GET',
362
- path: '/static/plain.txt',
363
- headers: { range },
364
- })
365
- expect(res.status).toBe(206)
366
- expect(res.headers.get('content-range')).toBe('bytes 0-16/17')
367
- expect(await res.text()).toBe('This is plain.txt')
368
- }
369
- )
370
-
371
- it('Should return 416 instead of crashing for a range request on an empty file', async () => {
372
- const res = await requestServer(server, {
373
- method: 'GET',
374
- path: '/static/foo..bar.txt',
375
- headers: { range: 'bytes=0-0' },
376
- })
377
- expect(res.status).toBe(416)
378
- expect(res.headers.get('content-range')).toBe('bytes */0')
379
- })
380
-
381
- it('Should return 416 instead of crashing for a malformed range on an empty file', async () => {
382
- const res = await requestServer(server, {
383
- method: 'GET',
384
- path: '/static/foo..bar.txt',
385
- headers: { range: 'hello' },
386
- })
387
- expect(res.status).toBe(416)
388
- expect(res.headers.get('content-range')).toBe('bytes */0')
389
- })
390
-
391
- it('Should handle the `onNotFound` option', async () => {
392
- const res = await requestServer(server, { method: 'GET', path: '/on-not-found/foo.txt' })
393
- expect(res.status).toBe(404)
394
- expect(notFoundMessage).toMatch(
395
- /not-found[\/\\]on-not-found[\/\\]foo\.txt is not found, request to \/on-not-found\/foo\.txt$/
396
- )
397
- })
398
-
399
- it('Should handle the `onFound` option', async () => {
400
- const res = await requestServer(server, {
401
- method: 'GET',
402
- path: '/static/data.json?type=application/json;%20charset=utf-8',
403
- })
404
- expect(res.status).toBe(200)
405
- expect(res.headers.get('content-type')).toBe('application/json; charset=utf-8')
406
- })
407
-
408
- it('Should handle double dots in URL', async () => {
409
- const res = await requestServer(server, { method: 'GET', path: '/static/../secret.txt' })
410
- expect(res.status).toBe(404)
411
- })
412
-
413
- // Skip on Windows as symlink behavior is different
414
- ;(process.platform === 'win32' ? it.skip : it)('Should follow symlinks', async () => {
415
- const symlinkPath = path.join(__dirname, 'assets', 'static', 'symlink.html')
416
- const symlinkTarget = path.join(__dirname, 'assets', 'static', 'index.html')
417
- try {
418
- // force: true so it doesn't throw if the symlink doesn't exist
419
- rmSync(symlinkPath, { force: true })
420
- symlinkSync(symlinkTarget, symlinkPath)
421
-
422
- const res = await requestServer(server, { method: 'GET', path: '/static/symlink.html' })
423
- expect(res.status).toBe(200)
424
- expect(await res.text()).toBe('<h1>Hello Acryl</h1>')
425
- } finally {
426
- rmSync(symlinkPath, { force: true })
427
- }
428
- })
429
-
430
- it('Should handle URIError thrown while decoding URI component', async () => {
431
- const res = await requestServer(server, { method: 'GET', path: '/static/%c0%afsecret.txt' })
432
- expect(res.status).toBe(404)
433
- })
434
-
435
- it('Should handle an extension less files', async () => {
436
- const res = await requestServer(server, { method: 'GET', path: '/static/extensionless' })
437
- expect(res.status).toBe(200)
438
- expect(res.headers.get('content-type')).toBe('application/octet-stream')
439
- expect(await res.text()).toBe('Extensionless')
440
- })
441
-
442
- it('Should return a pre-compressed zstd response - /static-with-precompressed/hello.txt', async () => {
443
- // Check if it returns a normal response
444
- let res = await requestServer(server, {
445
- method: 'GET',
446
- path: '/static-with-precompressed/hello.txt',
447
- })
448
- let stats = statSync(path.join(__dirname, 'assets', 'static-with-precompressed', 'hello.txt'))
449
- expect(res.status).toBe(200)
450
- expect(res.headers.get('content-length')).toBe('20')
451
- expect(res.headers.get('last-modified')).toBe(stats.mtime.toUTCString())
452
- expect(await res.text()).toBe('Hello Not Compressed')
453
-
454
- res = await requestServer(server, {
455
- method: 'GET',
456
- path: '/static-with-precompressed/hello.txt',
457
- headers: { 'accept-encoding': 'zstd' },
458
- })
459
- stats = statSync(path.join(__dirname, 'assets', 'static-with-precompressed', 'hello.txt.zst'))
460
- expect(res.status).toBe(200)
461
- expect(res.headers.get('content-length')).toBe('21')
462
- expect(res.headers.get('content-encoding')).toBe('zstd')
463
- expect(res.headers.get('last-modified')).toBe(stats.mtime.toUTCString())
464
- expect(res.headers.get('vary')).toBe('Accept-Encoding')
465
- // the .zst asset is not really compressed, so the raw body can be asserted directly
466
- expect(await res.text()).toBe('Hello zstd Compressed')
467
- })
468
-
469
- it('Should return a pre-compressed brotli response - /static-with-precompressed/hello.txt', async () => {
470
- const res = await requestServer(server, {
471
- method: 'GET',
472
- path: '/static-with-precompressed/hello.txt',
473
- headers: { 'accept-encoding': 'wompwomp, gzip, br, deflate, zstd' },
474
- })
475
- expect(res.status).toBe(200)
476
- expect(res.headers.get('content-length')).toBe('19')
477
- expect(res.headers.get('content-encoding')).toBe('br')
478
- expect(res.headers.get('vary')).toBe('Accept-Encoding')
479
- // the .br asset is not really compressed, so the raw body can be asserted directly
480
- expect(await res.text()).toBe('Hello br Compressed')
481
- })
482
-
483
- it('Should not return a pre-compressed response - /static-with-precompressed/hello.txt', async () => {
484
- const res = await requestServer(server, {
485
- method: 'GET',
486
- path: '/static-with-precompressed/hello.txt',
487
- headers: { 'accept-encoding': 'wompwomp, unknown' },
488
- })
489
- expect(res.status).toBe(200)
490
- expect(res.headers.get('content-encoding')).toBeNull()
491
- expect(res.headers.get('vary')).toBeNull()
492
- expect(await res.text()).toBe('Hello Not Compressed')
493
- })
494
-
495
- it('Should return a pre-compressed response for an octet-stream file - /static-with-precompressed/hello.bin', async () => {
496
- const res = await requestServer(server, {
497
- method: 'GET',
498
- path: '/static-with-precompressed/hello.bin',
499
- headers: { 'accept-encoding': 'gzip, br, zstd' },
500
- })
501
- expect(res.status).toBe(200)
502
- expect(res.headers.get('content-type')).toBe('application/octet-stream')
503
- expect(res.headers.get('content-length')).toBe('19')
504
- expect(res.headers.get('content-encoding')).toBe('br')
505
- expect(res.headers.get('vary')).toBe('Accept-Encoding')
506
- // the .br asset is not really compressed, so the raw body can be asserted directly
507
- expect(await res.text()).toBe('Hello br Compressed')
508
- })
509
-
510
- describe('Absolute path', () => {
511
- const rootPaths = [
512
- path.join(__dirname, 'assets'),
513
- __dirname + path.sep + '..' + path.sep + 'adapter' + path.sep + 'assets',
514
- ]
515
- rootPaths.forEach((root) => {
516
- describe(root, () => {
517
- const app = new Acryl()
518
- const server = createAdaptorServer(app)
519
- app.use('/static/*', serveStatic({ root }))
520
- app.use('/favicon.ico', serveStatic({ path: root + path.sep + 'favicon.ico' }))
521
-
522
- it('Should return index.html', async () => {
523
- const res = await requestServer(server, { method: 'GET', path: '/static' })
524
- expect(res.status).toBe(200)
525
- expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
526
- expect(await res.text()).toBe('<h1>Hello Acryl</h1>')
527
- })
528
-
529
- it('Should return correct headers and data for text', async () => {
530
- const res = await requestServer(server, { method: 'GET', path: '/static/plain.txt' })
531
- expect(res.status).toBe(200)
532
- expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
533
- expect(await res.text()).toBe('This is plain.txt')
534
- })
535
- it('Should return correct headers for icons', async () => {
536
- const res = await requestServer(server, { method: 'GET', path: '/favicon.ico' })
537
- expect(res.status).toBe(200)
538
- expect(res.headers.get('content-type')).toBe('image/x-icon')
539
- })
540
- })
541
- })
542
- })
543
-
544
- describe('Root and path combination tests', () => {
545
- const rootPaths = [
546
- path.join(__dirname, 'assets'),
547
- path.join(__dirname, 'assets'),
548
- __dirname + path.sep + '..' + path.sep + 'adapter' + path.sep + 'assets',
549
- ]
550
- const optionPaths = ['favicon.ico', '/favicon.ico']
551
- rootPaths.forEach((root) => {
552
- optionPaths.forEach((optionPath) => {
553
- describe(`${root} + ${optionPath}`, () => {
554
- const app = new Acryl()
555
- const server = createAdaptorServer(app)
556
-
557
- app.use(
558
- '/favicon.ico',
559
- serveStatic({
560
- root,
561
- path: optionPath,
562
- })
563
- )
564
-
565
- it('Should return 200 response if both root and path set', async () => {
566
- const res = await requestServer(server, { method: 'GET', path: '/favicon.ico' })
567
- expect(res.status).toBe(200)
568
- expect(res.headers.get('content-type')).toBe('image/x-icon')
569
- })
570
- })
571
- })
572
- })
573
- })
574
-
575
- describe('Path traversal security tests', () => {
576
- const app = new Acryl()
577
- const server = createAdaptorServer(app)
578
- app.use('/static/*', serveStatic({ root: './tests/adapter/assets' }))
579
-
580
- it('Should prevent path traversal attacks with double dots', async () => {
581
- const res = await requestServer(server, { method: 'GET', path: '/static/../secret.txt' })
582
- expect(res.status).toBe(404)
583
- })
584
-
585
- it('Should prevent path traversal attacks with multiple levels', async () => {
586
- const res = await requestServer(server, {
587
- method: 'GET',
588
- path: '/static/../../package.json',
589
- })
590
- expect(res.status).toBe(404)
591
- })
592
-
593
- it('Should prevent path traversal attacks with mixed separators', async () => {
594
- const res = await requestServer(server, {
595
- method: 'GET',
596
- path: '/static/..\\..\\package.json',
597
- })
598
- expect(res.status).toBe(404)
599
- })
600
-
601
- it('Should prevent path traversal attacks with encoded dots', async () => {
602
- const res = await requestServer(server, {
603
- method: 'GET',
604
- path: '/static/%2e%2e%2fsecret.txt',
605
- })
606
- expect(res.status).toBe(404)
607
- })
608
-
609
- it('Should accept filename with double dots', async () => {
610
- const res = await requestServer(server, { method: 'GET', path: '/static/foo..bar.txt' })
611
- expect(res.status).toBe(200)
612
- })
613
- })
614
-
615
- describe('Path mismatch security tests', () => {
616
- const app = new Acryl()
617
- const server = createAdaptorServer(app)
618
-
619
- app.use('/static/admin/*', async (c, next) => {
620
- c.header('X-Authorized', 'true')
621
- await next()
622
- })
623
-
624
- app.use('/static/*', serveStatic({ root: './tests/adapter/assets' }))
625
-
626
- it('Should not allow bypass via path mismatch between middleware and serveStatic', async () => {
627
- const res = await requestServer(server, { method: 'GET', path: '/static/admin/secret.txt' })
628
- expect(res.headers.get('x-authorized')).toBe('true')
629
- expect(await res.text()).toBe('secret')
630
-
631
- const res2 = await requestServer(server, {
632
- method: 'GET',
633
- path: '/static/admin%2Fsecret.txt',
634
- })
635
- expect(res2.status).toBe(404)
636
- expect(res2.headers.get('x-authorized')).toBeNull()
637
- expect(await res2.text()).not.toBe('secret')
638
-
639
- const res3 = await requestServer(server, {
640
- method: 'GET',
641
- path: '/static//admin/secret.txt',
642
- })
643
- expect(res3.status).toBe(404)
644
-
645
- const res4 = await requestServer(server, {
646
- method: 'GET',
647
- path: '/static/admin%5Csecret.txt',
648
- })
649
- expect(res4.status).toBe(404)
650
- expect(res4.headers.get('x-authorized')).toBeNull()
651
- expect(await res4.text()).not.toBe('secret')
652
- })
653
- })
654
-
655
- describe('Percent signs in request paths', () => {
656
- const root = mkdtempSync(path.join(tmpdir(), 'node-server-serve-static-'))
657
- const staticRoot = path.join(root, 'static')
658
- for (const filePath of [
659
- '100%.txt',
660
- '100%/hello.txt',
661
- '%2Fadmin/secret.txt',
662
- 'hello world.txt',
663
- '炎.txt',
664
- ]) {
665
- const fullPath = path.join(staticRoot, filePath)
666
- mkdirSync(path.dirname(fullPath), { recursive: true })
667
- writeFileSync(fullPath, `Hello in static/${filePath}`)
668
- }
669
-
670
- afterAll(() => rmSync(root, { recursive: true, force: true }))
671
-
672
- it.each([
673
- ['/static/%E7%82%8E.txt', 'static/炎.txt'],
674
- ['/static/hello%20world.txt', 'static/hello world.txt'],
675
- ])('Should decode URI strings - %s', async (url, filePath) => {
676
- const fileApp = new Acryl().use('/static/*', serveStatic({ root }))
677
- const res = await requestServer(createAdaptorServer(fileApp), {
678
- method: 'GET',
679
- path: url,
680
- })
681
- expect(res.status).toBe(200)
682
- expect(res.headers.get('content-type')).toMatch(/^text\/plain/)
683
- expect(await res.text()).toBe(`Hello in ${filePath}`)
684
- })
685
-
686
- it.each(['/static/100%25.txt', '/static/100%25/hello.txt', '/static/%2Fadmin/secret.txt'])(
687
- 'Should skip paths containing percent signs by default - %s',
688
- async (url) => {
689
- const onNotFound = vi.fn()
690
- const rewriteRequestPath = vi.fn((filePath: string) => filePath)
691
- const fileApp = new Acryl().use(
692
- '/static/*',
693
- serveStatic({ root, onNotFound, rewriteRequestPath })
694
- )
695
-
696
- const res = await requestServer(createAdaptorServer(fileApp), { method: 'GET', path: url })
697
-
698
- expect(res.status).toBe(404)
699
- expect(rewriteRequestPath).not.toHaveBeenCalled()
700
- expect(onNotFound).toHaveBeenCalledWith(url, expect.anything())
701
- }
702
- )
703
-
704
- it.each([
705
- ['/static/100%25.txt', 'static/100%.txt'],
706
- ['/static/100%25/hello.txt', 'static/100%/hello.txt'],
707
- ['/static/%2Fadmin/secret.txt', 'static/%2Fadmin/secret.txt'],
708
- ])('Should allow percent signs when opted in - %s', async (url, filePath) => {
709
- const fileApp = new Acryl().use('/static/*', serveStatic({ root, allowPercentInPath: true }))
710
-
711
- const res = await requestServer(createAdaptorServer(fileApp), { method: 'GET', path: url })
712
-
713
- expect(res.status).toBe(200)
714
- expect(await res.text()).toBe(`Hello in ${filePath}`)
715
- })
716
-
717
- it('Should not bypass authentication through a second decode', async () => {
718
- const protectedApp = new Acryl()
719
- const rewriteRequestPath = vi.fn((filePath: string) => filePath)
720
- protectedApp.use('/static/admin/*', async (c) => c.text('Unauthorized', 401))
721
- protectedApp.use('/static/*', serveStatic({ root: './tests/adapter/assets', rewriteRequestPath }))
722
- const protectedServer = createAdaptorServer(protectedApp)
723
-
724
- for (const url of ['/static/admin/secret.txt', '/static/%61dmin/secret.txt']) {
725
- const protectedRes = await requestServer(protectedServer, { method: 'GET', path: url })
726
- expect(protectedRes.status).toBe(401)
727
- expect(await protectedRes.text()).toBe('Unauthorized')
728
- }
729
-
730
- const res = await requestServer(protectedServer, {
731
- method: 'GET',
732
- path: '/static/%%36%31dmin/secret.txt',
733
- })
734
- expect(res.status).toBe(404)
735
- expect(rewriteRequestPath).not.toHaveBeenCalled()
736
- expect(await res.text()).not.toBe('secret')
737
- })
738
-
739
- it('Should pass the routed path and context to onNotFound, then continue', async () => {
740
- const fileApp = new Acryl()
741
- let hookPath: string | undefined
742
- let contextPath: string | undefined
743
- fileApp.use(
744
- '/static/*',
745
- serveStatic({
746
- root,
747
- onNotFound: (requestPath, c) => {
748
- hookPath = requestPath
749
- contextPath = c.req.path
750
- },
751
- })
752
- )
753
- fileApp.get('/static/*', (c) => c.text('next handler', 202))
754
-
755
- const res = await requestServer(createAdaptorServer(fileApp), {
756
- method: 'GET',
757
- path: '/static/100%25.txt',
758
- })
759
- expect(res.status).toBe(202)
760
- expect(await res.text()).toBe('next handler')
761
- expect(hookPath).toBe('/static/100%25.txt')
762
- expect(contextPath).toBe(hookPath)
763
- })
764
-
765
- it.each(['/static/%2e%2e%5C100%.txt', '/static//100%.txt', '/static/%5C100%.txt'])(
766
- 'Should retain path validation when opted in: %s',
767
- async (requestPath) => {
768
- const fileApp = new Acryl()
769
- const rewriteRequestPath = vi.fn((requestPath: string) => requestPath)
770
- const onNotFound = vi.fn()
771
- fileApp.use(
772
- '/static/*',
773
- serveStatic({ root, allowPercentInPath: true, rewriteRequestPath, onNotFound })
774
- )
775
- const res = await requestServer(createAdaptorServer(fileApp), {
776
- method: 'GET',
777
- path: requestPath,
778
- })
779
- expect(res.status).toBe(404)
780
- expect(rewriteRequestPath).not.toHaveBeenCalled()
781
- expect(onNotFound).toHaveBeenCalledOnce()
782
- }
783
- )
784
-
785
- it('Should preserve the fixed path option for percent filenames', async () => {
786
- const fileApp = new Acryl()
787
- fileApp.use('/static/*', serveStatic({ root, path: 'static/100%.txt' }))
788
- const res = await requestServer(createAdaptorServer(fileApp), {
789
- method: 'GET',
790
- path: '/static/100%25.txt',
791
- })
792
- expect(res.status).toBe(200)
793
- expect(await res.text()).toBe('Hello in static/100%.txt')
794
- })
795
- })
796
-
797
- describe('Stream error handling', () => {
798
- const testFile = path.join(__dirname, 'assets', 'static', 'plain.txt')
799
- console.log(testFile)
800
- let originalMode: number
801
-
802
- beforeEach(() => {
803
- const stats = statSync(testFile)
804
- originalMode = stats.mode
805
- // Remove read permission to trigger stream error
806
- chmodSync(testFile, 0o000)
807
- })
808
-
809
- afterEach(() => {
810
- chmodSync(testFile, originalMode)
811
- })
812
-
813
- // Skip on Windows as chmod doesn't work for file permissions
814
- ;(process.platform === 'win32' ? it.skip : it)(
815
- 'Should handle read permission errors gracefully',
816
- async () => {
817
- const app = new Acryl()
818
- app.use('/static/*', serveStatic({ root: './tests/adapter/assets' }))
819
- const server = createAdaptorServer(app)
820
- await expect(
821
- requestServer(server, { method: 'GET', path: '/static/plain.txt' })
822
- ).rejects.toThrow()
823
- }
824
- )
825
- })
826
- })
827
-
828
- describe('Serve Static Middleware with wrong path', () => {
829
- it('Should show an error when the path is wrong', async () => {
830
- const logSpy = vi.spyOn(console, 'error')
831
-
832
- const app = new Acryl<{
833
- Variables: {
834
- path: string
835
- }
836
- }>()
837
-
838
- app.use(
839
- '*',
840
- serveStatic({
841
- root: './public',
842
- })
843
- )
844
-
845
- expect(logSpy).toHaveBeenCalledWith(
846
- "serveStatic: root path './public' is not found, are you sure it's correct?"
847
- )
848
- })
849
- })