nuxt-ai-ready 2.5.3 → 3.0.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 (286) hide show
  1. package/README.md +13 -12
  2. package/dist/chunks/agent-skills.mjs +35 -12
  3. package/dist/chunks/prerender.mjs +35 -24
  4. package/dist/cli.mjs +5 -2
  5. package/dist/module.d.mts +4 -0
  6. package/dist/module.json +2 -2
  7. package/dist/module.mjs +8 -4
  8. package/dist/runtime/app/index.d.ts +3 -0
  9. package/dist/runtime/app/index.js +2 -0
  10. package/dist/runtime/cache-control.d.ts +2 -0
  11. package/dist/runtime/cache-control.js +11 -0
  12. package/dist/runtime/llms-txt-utils.js +1 -1
  13. package/dist/runtime/server/db/context.d.ts +13 -0
  14. package/dist/runtime/server/db/drizzle/client.d.ts +6 -7
  15. package/dist/runtime/server/db/drizzle/client.js +13 -2
  16. package/dist/runtime/server/db/drizzle/providers/bun.d.ts +2 -2
  17. package/dist/runtime/server/db/drizzle/providers/bun.js +2 -2
  18. package/dist/runtime/server/db/drizzle/providers/d1.d.ts +2 -2
  19. package/dist/runtime/server/db/drizzle/providers/d1.js +3 -2
  20. package/dist/runtime/server/db/drizzle/providers/libsql.d.ts +2 -2
  21. package/dist/runtime/server/db/drizzle/providers/libsql.js +2 -2
  22. package/dist/runtime/server/db/drizzle/providers/neon.d.ts +2 -2
  23. package/dist/runtime/server/db/drizzle/providers/neon.js +2 -2
  24. package/dist/runtime/server/db/drizzle/providers/node-sqlite.d.ts +2 -2
  25. package/dist/runtime/server/db/drizzle/providers/node-sqlite.js +2 -2
  26. package/dist/runtime/server/db/drizzle/providers/postgres.d.ts +2 -2
  27. package/dist/runtime/server/db/drizzle/providers/postgres.js +2 -2
  28. package/dist/runtime/server/db/drizzle/queries.d.ts +27 -27
  29. package/dist/runtime/server/db/drizzle/queries.js +6 -5
  30. package/dist/runtime/server/db/drizzle/raw.d.ts +2 -6
  31. package/dist/runtime/server/db/index.d.ts +1 -1
  32. package/dist/runtime/server/db/index.js +1 -1
  33. package/dist/runtime/server/db/queries.d.ts +36 -36
  34. package/dist/runtime/server/db/queries.js +8 -9
  35. package/dist/runtime/server/db/seed.d.ts +2 -2
  36. package/dist/runtime/server/db/seed.js +4 -3
  37. package/dist/runtime/server/db/shared.d.ts +8 -0
  38. package/dist/runtime/server/db/shared.js +8 -2
  39. package/dist/runtime/server/index.d.ts +2 -0
  40. package/dist/runtime/server/index.js +17 -0
  41. package/dist/runtime/server/mcp/tools/get-page-markdown.js +1 -1
  42. package/dist/runtime/server/mcp/tools/list-pages.js +1 -1
  43. package/dist/runtime/server/mcp/tools/search-pages.js +1 -1
  44. package/dist/runtime/server/middleware/markdown.prerender.js +1 -1
  45. package/dist/runtime/server/plugins/db-lifecycle-nitro3.d.ts +2 -0
  46. package/dist/runtime/server/plugins/db-lifecycle-nitro3.js +16 -0
  47. package/dist/runtime/server/plugins/html-capture.prerender.js +6 -4
  48. package/dist/runtime/server/plugins/link-header-nitro3.d.ts +2 -0
  49. package/dist/runtime/server/plugins/link-header-nitro3.js +20 -0
  50. package/dist/runtime/server/plugins/link-header.d.ts +6 -0
  51. package/dist/runtime/server/plugins/link-header.js +7 -11
  52. package/dist/runtime/server/plugins/markdown-negotiation.js +8 -1
  53. package/dist/runtime/server/plugins/sitemap-seeder.js +3 -3
  54. package/dist/runtime/server/routes/agent-skills-artifact.d.ts +1 -1
  55. package/dist/runtime/server/routes/agent-skills-artifact.js +10 -10
  56. package/dist/runtime/server/routes/agent-skills-index.d.ts +1 -1
  57. package/dist/runtime/server/routes/agent-skills-index.js +7 -6
  58. package/dist/runtime/server/routes/ai-catalog.d.ts +2 -1
  59. package/dist/runtime/server/routes/ai-catalog.js +7 -8
  60. package/dist/runtime/server/routes/api-catalog.d.ts +3 -1
  61. package/dist/runtime/server/routes/api-catalog.js +12 -12
  62. package/dist/runtime/server/routes/llms-full.txt.get.js +1 -1
  63. package/dist/runtime/server/routes/mcp-server-card.d.ts +1 -1
  64. package/dist/runtime/server/routes/mcp-server-card.js +7 -8
  65. package/dist/runtime/server/utils/checkStale.d.ts +2 -2
  66. package/dist/runtime/server/utils/cloudflare-prerendered.d.ts +3 -0
  67. package/dist/runtime/server/utils/cloudflare-prerendered.js +23 -0
  68. package/dist/runtime/server/utils/cloudflare.d.ts +5 -5
  69. package/dist/runtime/server/utils/cloudflare.js +31 -7
  70. package/dist/runtime/server/utils/content-negotiation.d.ts +5 -3
  71. package/dist/runtime/server/utils/content-negotiation.js +8 -7
  72. package/dist/runtime/server/utils/context.d.ts +3 -3
  73. package/dist/runtime/server/utils/context.js +1 -1
  74. package/dist/runtime/server/utils/discovery-response.d.ts +2 -0
  75. package/dist/runtime/server/utils/link-header.d.ts +5 -3
  76. package/dist/runtime/server/utils/link-header.js +26 -2
  77. package/dist/runtime/server/utils/markdown-request.d.ts +4 -3
  78. package/dist/runtime/server/utils/markdown-request.js +15 -13
  79. package/dist/runtime/server/utils/negotiation-decision.d.ts +4 -0
  80. package/dist/runtime/server/utils/negotiation-decision.js +6 -5
  81. package/dist/runtime/server/utils/negotiation-response.js +47 -22
  82. package/dist/runtime/server/utils/prerender-html.d.ts +2 -1
  83. package/dist/runtime/server/utils/prerender-html.js +3 -1
  84. package/dist/runtime/types.d.ts +15 -7
  85. package/dist/shared/nuxt-ai-ready.B3hAuOLQ.mjs +29 -0
  86. package/dist/shared/{nuxt-ai-ready.CLSWou20.mjs → nuxt-ai-ready.C34F0LJo.mjs} +560 -219
  87. package/package.json +85 -20
  88. package/skills/nuxt-ai-ready/SKILL.md +26 -12
  89. package/skills/nuxt-ai-ready/references/runtime-indexing.md +7 -6
  90. package/dist/runtime/server/db/drizzle/providers/sqlite.d.ts +0 -8
  91. package/dist/runtime/server/db/drizzle/providers/sqlite.js +0 -15
  92. package/node_modules/@mdream/js/LICENSE.md +0 -9
  93. package/node_modules/@mdream/js/README.md +0 -792
  94. package/node_modules/@mdream/js/bin/mdream.mjs +0 -2
  95. package/node_modules/@mdream/js/dist/_chunks/const.mjs +0 -134
  96. package/node_modules/@mdream/js/dist/_chunks/index.d.mts +0 -9
  97. package/node_modules/@mdream/js/dist/_chunks/markdown-processor.mjs +0 -1968
  98. package/node_modules/@mdream/js/dist/_chunks/minimal.d.mts +0 -6
  99. package/node_modules/@mdream/js/dist/_chunks/parse.mjs +0 -2756
  100. package/node_modules/@mdream/js/dist/_chunks/plugins.mjs +0 -725
  101. package/node_modules/@mdream/js/dist/_chunks/resolve-plugins.mjs +0 -44
  102. package/node_modules/@mdream/js/dist/_chunks/stream.mjs +0 -37
  103. package/node_modules/@mdream/js/dist/_chunks/types.d.mts +0 -473
  104. package/node_modules/@mdream/js/dist/cli.d.mts +0 -1
  105. package/node_modules/@mdream/js/dist/cli.mjs +0 -32
  106. package/node_modules/@mdream/js/dist/core.d.mts +0 -15
  107. package/node_modules/@mdream/js/dist/core.mjs +0 -28
  108. package/node_modules/@mdream/js/dist/index.d.mts +0 -4
  109. package/node_modules/@mdream/js/dist/index.mjs +0 -58
  110. package/node_modules/@mdream/js/dist/llms-txt.d.mts +0 -86
  111. package/node_modules/@mdream/js/dist/llms-txt.mjs +0 -308
  112. package/node_modules/@mdream/js/dist/negotiate.d.mts +0 -35
  113. package/node_modules/@mdream/js/dist/negotiate.mjs +0 -121
  114. package/node_modules/@mdream/js/dist/parse.d.mts +0 -90
  115. package/node_modules/@mdream/js/dist/parse.mjs +0 -2
  116. package/node_modules/@mdream/js/dist/plugins.d.mts +0 -85
  117. package/node_modules/@mdream/js/dist/plugins.mjs +0 -2
  118. package/node_modules/@mdream/js/dist/preset/minimal.d.mts +0 -2
  119. package/node_modules/@mdream/js/dist/preset/minimal.mjs +0 -33
  120. package/node_modules/@mdream/js/dist/splitter.d.mts +0 -18
  121. package/node_modules/@mdream/js/dist/splitter.mjs +0 -194
  122. package/node_modules/@mdream/js/package.json +0 -103
  123. package/node_modules/cac/LICENSE +0 -21
  124. package/node_modules/cac/README.md +0 -492
  125. package/node_modules/cac/dist/index.d.ts +0 -196
  126. package/node_modules/cac/dist/index.js +0 -638
  127. package/node_modules/cac/package.json +0 -72
  128. package/node_modules/mdream/LICENSE.md +0 -9
  129. package/node_modules/mdream/README.md +0 -1091
  130. package/node_modules/mdream/bin/mdream.mjs +0 -46
  131. package/node_modules/mdream/dist/browser.d.mts +0 -105
  132. package/node_modules/mdream/dist/browser.mjs +0 -262
  133. package/node_modules/mdream/dist/edge.d.mts +0 -100
  134. package/node_modules/mdream/dist/edge.mjs +0 -250
  135. package/node_modules/mdream/dist/iife.js +0 -605
  136. package/node_modules/mdream/dist/index.d.mts +0 -89
  137. package/node_modules/mdream/dist/index.mjs +0 -184
  138. package/node_modules/mdream/dist/wasm.d.mts +0 -100
  139. package/node_modules/mdream/dist/wasm.mjs +0 -140
  140. package/node_modules/mdream/dist/worker.d.mts +0 -101
  141. package/node_modules/mdream/dist/worker.mjs +0 -217
  142. package/node_modules/mdream/napi/index.d.mts +0 -127
  143. package/node_modules/mdream/napi/index.d.ts +0 -127
  144. package/node_modules/mdream/napi/index.mjs +0 -725
  145. package/node_modules/mdream/napi/rust.android-arm-eabi.node +0 -0
  146. package/node_modules/mdream/napi/rust.android-arm64.node +0 -0
  147. package/node_modules/mdream/napi/rust.darwin-arm64.node +0 -0
  148. package/node_modules/mdream/napi/rust.darwin-x64.node +0 -0
  149. package/node_modules/mdream/napi/rust.freebsd-x64.node +0 -0
  150. package/node_modules/mdream/napi/rust.linux-arm-gnueabihf.node +0 -0
  151. package/node_modules/mdream/napi/rust.linux-arm64-gnu.node +0 -0
  152. package/node_modules/mdream/napi/rust.linux-arm64-musl.node +0 -0
  153. package/node_modules/mdream/napi/rust.linux-x64-gnu.node +0 -0
  154. package/node_modules/mdream/napi/rust.linux-x64-musl.node +0 -0
  155. package/node_modules/mdream/napi/rust.wasi.cjs +0 -2144
  156. package/node_modules/mdream/napi/rust.wasm32-wasi.wasm +0 -0
  157. package/node_modules/mdream/napi/rust.win32-arm64-msvc.node +0 -0
  158. package/node_modules/mdream/napi/rust.win32-x64-msvc.node +0 -0
  159. package/node_modules/mdream/node_modules/@emnapi/core/LICENSE +0 -21
  160. package/node_modules/mdream/node_modules/@emnapi/core/README.md +0 -1
  161. package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.d.ts +0 -392
  162. package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.full.d.ts +0 -392
  163. package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.full.js +0 -8351
  164. package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.js +0 -7268
  165. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/async-work.d.ts +0 -5
  166. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/async-work.js +0 -843
  167. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/index.d.ts +0 -4
  168. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/index.js +0 -3
  169. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/threadsafe-function.d.ts +0 -5
  170. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/threadsafe-function.js +0 -1010
  171. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/v8.d.ts +0 -5
  172. package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/v8.js +0 -1446
  173. package/node_modules/mdream/node_modules/@emnapi/core/node_modules/@emnapi/wasi-threads/LICENSE +0 -21
  174. package/node_modules/mdream/node_modules/@emnapi/core/node_modules/@emnapi/wasi-threads/README.md +0 -203
  175. package/node_modules/mdream/node_modules/@emnapi/core/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +0 -236
  176. package/node_modules/mdream/node_modules/@emnapi/core/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +0 -1086
  177. package/node_modules/mdream/node_modules/@emnapi/core/node_modules/@emnapi/wasi-threads/package.json +0 -35
  178. package/node_modules/mdream/node_modules/@emnapi/core/node_modules/@emnapi/wasi-threads/rollup.config.js +0 -6
  179. package/node_modules/mdream/node_modules/@emnapi/core/package.json +0 -39
  180. package/node_modules/mdream/node_modules/@emnapi/core/rollup.config.js +0 -60
  181. package/node_modules/mdream/node_modules/@emnapi/runtime/LICENSE +0 -21
  182. package/node_modules/mdream/node_modules/@emnapi/runtime/README.md +0 -1
  183. package/node_modules/mdream/node_modules/@emnapi/runtime/dist/emnapi.d.ts +0 -833
  184. package/node_modules/mdream/node_modules/@emnapi/runtime/dist/emnapi.js +0 -2306
  185. package/node_modules/mdream/node_modules/@emnapi/runtime/dist/emnapi.js.map +0 -1
  186. package/node_modules/mdream/node_modules/@emnapi/runtime/index.cjs +0 -63
  187. package/node_modules/mdream/node_modules/@emnapi/runtime/package.json +0 -39
  188. package/node_modules/mdream/node_modules/@emnapi/runtime/rollup.config.js +0 -25
  189. package/node_modules/mdream/node_modules/@emnapi/wasi-threads/LICENSE +0 -21
  190. package/node_modules/mdream/node_modules/@emnapi/wasi-threads/README.md +0 -203
  191. package/node_modules/mdream/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +0 -240
  192. package/node_modules/mdream/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +0 -1118
  193. package/node_modules/mdream/node_modules/@emnapi/wasi-threads/package.json +0 -35
  194. package/node_modules/mdream/node_modules/@emnapi/wasi-threads/rollup.config.js +0 -6
  195. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/emnapi-plugins.cjs +0 -1472
  196. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/emnapi-plugins.js +0 -1470
  197. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/fs-proxy.cjs +0 -237
  198. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/fs.js +0 -17578
  199. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/fs-proxy.js +0 -317
  200. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/package.json +0 -67
  201. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/runtime.cjs +0 -39
  202. package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/runtime.js +0 -36
  203. package/node_modules/mdream/node_modules/@tybys/wasm-util/LICENSE +0 -21
  204. package/node_modules/mdream/node_modules/@tybys/wasm-util/README.md +0 -193
  205. package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/tsdoc-metadata.json +0 -11
  206. package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +0 -423
  207. package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +0 -2932
  208. package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +0 -2932
  209. package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +0 -1
  210. package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.js +0 -2954
  211. package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +0 -1
  212. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/asyncify.js +0 -158
  213. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/index.js +0 -13
  214. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/jspi.js +0 -46
  215. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/load.js +0 -97
  216. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/memory.js +0 -35
  217. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/error.js +0 -103
  218. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/fd.js +0 -268
  219. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/fs.js +0 -3
  220. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +0 -192
  221. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/path.js +0 -316
  222. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +0 -1599
  223. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/rights.js +0 -141
  224. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/types.js +0 -220
  225. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/util.js +0 -128
  226. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/webassembly.js +0 -13
  227. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/asyncify.mjs +0 -153
  228. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/index.mjs +0 -9
  229. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/jspi.mjs +0 -39
  230. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/load.mjs +0 -89
  231. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/memory.mjs +0 -28
  232. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/error.mjs +0 -97
  233. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/fd.mjs +0 -256
  234. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/fs.mjs +0 -1
  235. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +0 -186
  236. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/path.mjs +0 -310
  237. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +0 -1593
  238. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/rights.mjs +0 -135
  239. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/types.mjs +0 -216
  240. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/util.mjs +0 -113
  241. package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/webassembly.mjs +0 -9
  242. package/node_modules/mdream/node_modules/@tybys/wasm-util/package.json +0 -58
  243. package/node_modules/mdream/node_modules/tslib/CopyrightNotice.txt +0 -15
  244. package/node_modules/mdream/node_modules/tslib/LICENSE.txt +0 -12
  245. package/node_modules/mdream/node_modules/tslib/README.md +0 -164
  246. package/node_modules/mdream/node_modules/tslib/SECURITY.md +0 -41
  247. package/node_modules/mdream/node_modules/tslib/modules/index.d.ts +0 -38
  248. package/node_modules/mdream/node_modules/tslib/modules/index.js +0 -70
  249. package/node_modules/mdream/node_modules/tslib/modules/package.json +0 -3
  250. package/node_modules/mdream/node_modules/tslib/package.json +0 -47
  251. package/node_modules/mdream/node_modules/tslib/tslib.d.ts +0 -460
  252. package/node_modules/mdream/node_modules/tslib/tslib.es6.html +0 -1
  253. package/node_modules/mdream/node_modules/tslib/tslib.es6.js +0 -402
  254. package/node_modules/mdream/node_modules/tslib/tslib.es6.mjs +0 -401
  255. package/node_modules/mdream/node_modules/tslib/tslib.html +0 -1
  256. package/node_modules/mdream/node_modules/tslib/tslib.js +0 -484
  257. package/node_modules/mdream/package.json +0 -110
  258. package/node_modules/mdream/skills/mdream/SKILL.md +0 -124
  259. package/node_modules/mdream/skills/mdream/references/runtimes.md +0 -49
  260. package/node_modules/mdream/wasm/mdream_edge.d.ts +0 -90
  261. package/node_modules/mdream/wasm/mdream_edge.js +0 -517
  262. package/node_modules/mdream/wasm/mdream_edge_bg.wasm +0 -0
  263. package/node_modules/mdream/wasm/mdream_edge_bg.wasm.d.ts +0 -20
  264. package/node_modules/mdream/wasm/package.json +0 -17
  265. package/node_modules/mdream/wasm-bundler/mdream_edge.d.ts +0 -45
  266. package/node_modules/mdream/wasm-bundler/mdream_edge.js +0 -13
  267. package/node_modules/mdream/wasm-bundler/mdream_edge_bg.js +0 -413
  268. package/node_modules/mdream/wasm-bundler/mdream_edge_bg.wasm +0 -0
  269. package/node_modules/mdream/wasm-bundler/mdream_edge_bg.wasm.d.ts +0 -20
  270. package/node_modules/mdream/wasm-bundler/package.json +0 -19
  271. package/node_modules/pathe/LICENSE +0 -70
  272. package/node_modules/pathe/README.md +0 -73
  273. package/node_modules/pathe/dist/index.cjs +0 -39
  274. package/node_modules/pathe/dist/index.d.cts +0 -47
  275. package/node_modules/pathe/dist/index.d.mts +0 -47
  276. package/node_modules/pathe/dist/index.d.ts +0 -47
  277. package/node_modules/pathe/dist/index.mjs +0 -19
  278. package/node_modules/pathe/dist/shared/pathe.BSlhyZSM.cjs +0 -266
  279. package/node_modules/pathe/dist/shared/pathe.M-eThtNZ.mjs +0 -249
  280. package/node_modules/pathe/dist/utils.cjs +0 -82
  281. package/node_modules/pathe/dist/utils.d.cts +0 -32
  282. package/node_modules/pathe/dist/utils.d.mts +0 -32
  283. package/node_modules/pathe/dist/utils.d.ts +0 -32
  284. package/node_modules/pathe/dist/utils.mjs +0 -77
  285. package/node_modules/pathe/package.json +0 -61
  286. package/node_modules/pathe/utils.d.ts +0 -1
@@ -1,1593 +0,0 @@
1
- import { _WebAssembly } from "../webassembly.mjs";
2
- import { resolve } from "./path.mjs";
3
- // Linux fcntl flag bits ↔ Windows libuv flag bits. `pathOpen()` builds
4
- // `flagsRes` using Linux constants (O_CREAT=0x40, O_EXCL=0x80,
5
- // O_APPEND=0x400). Windows libuv expects different bits (O_CREAT=0x100,
6
- // O_EXCL=0x400, O_APPEND=0x8). Without translation,
7
- // `fs.openSync(path, 0x241 /* L:WRONLY|CREAT|TRUNC */)` is rejected
8
- // with EINVAL because Linux's 0x40 happens to mean O_TEMPORARY on
9
- // Windows — and O_TEMPORARY without O_CREAT is invalid.
10
- const _isWin32Flags = typeof process !== 'undefined' && process.platform === 'win32';
11
- function _toWinOpenFlags(f) {
12
- let r = f & 3; // RDONLY/WRONLY/RDWR — same on both
13
- if ((f & 0x40) !== 0)
14
- r |= 0x100; // O_CREAT: Linux 0x40 -> Windows 0x100
15
- if ((f & 0x80) !== 0)
16
- r |= 0x400; // O_EXCL: Linux 0x80 -> Windows 0x400
17
- if ((f & 0x200) !== 0)
18
- r |= 0x200; // O_TRUNC: same value on both
19
- if ((f & 0x400) !== 0)
20
- r |= 0x8; // O_APPEND: Linux 0x400 -> Windows 0x8
21
- return r;
22
- }
23
- import { WasiErrno, WasiRights, FileControlFlag, WasiFileControlFlag, WasiFdFlag, WasiFileType, WasiClockid, WasiFstFlag, WasiEventType, WasiSubclockflags } from "./types.mjs";
24
- import { concatBuffer, toFileStat, AsyncTable, SyncTable } from "./fd.mjs";
25
- import { WasiError } from "./error.mjs";
26
- import { isPromiseLike, sleepBreakIf, unsharedSlice } from "./util.mjs";
27
- import { getRights } from "./rights.mjs";
28
- import { extendMemory } from "../memory.mjs";
29
- import { wrapAsyncImport } from "../jspi.mjs";
30
- function copyMemory(targets, src) {
31
- if (targets.length === 0 || src.length === 0)
32
- return 0;
33
- let copied = 0;
34
- let left = src.length - copied;
35
- for (let i = 0; i < targets.length; ++i) {
36
- const target = targets[i];
37
- if (left < target.length) {
38
- target.set(src.subarray(copied, copied + left), 0);
39
- copied += left;
40
- left = 0;
41
- return copied;
42
- }
43
- target.set(src.subarray(copied, copied + target.length), 0);
44
- copied += target.length;
45
- left -= target.length;
46
- }
47
- return copied;
48
- }
49
- const _memory = new WeakMap();
50
- const _wasi = new WeakMap();
51
- const _fs = new WeakMap();
52
- export function ensureMemoryFor(memory, end) {
53
- let buffer = memory.buffer;
54
- if (end > buffer.byteLength) {
55
- memory.grow(0);
56
- buffer = memory.buffer;
57
- }
58
- return buffer;
59
- }
60
- function getMemory(wasi, end = 0) {
61
- const memory = _memory.get(wasi);
62
- ensureMemoryFor(memory, end);
63
- return memory;
64
- }
65
- function getFs(wasi) {
66
- const fs = _fs.get(wasi);
67
- if (!fs)
68
- throw new Error('filesystem is unavailable');
69
- return fs;
70
- }
71
- function handleError(err) {
72
- if (err instanceof WasiError) {
73
- if (process.env.NODE_ENV !== 'production') {
74
- console.warn(err);
75
- }
76
- return err.errno;
77
- }
78
- switch (err.code) {
79
- case 'ENOENT': return WasiErrno.ENOENT;
80
- case 'EBADF': return WasiErrno.EBADF;
81
- case 'EINVAL': return WasiErrno.EINVAL;
82
- case 'EPERM': return WasiErrno.EPERM;
83
- case 'EPROTO': return WasiErrno.EPROTO;
84
- case 'EEXIST': return WasiErrno.EEXIST;
85
- case 'ENOTDIR': return WasiErrno.ENOTDIR;
86
- case 'EMFILE': return WasiErrno.EMFILE;
87
- case 'EACCES': return WasiErrno.EACCES;
88
- case 'EISDIR': return WasiErrno.EISDIR;
89
- case 'ENOTEMPTY': return WasiErrno.ENOTEMPTY;
90
- case 'ENOSYS': return WasiErrno.ENOSYS;
91
- }
92
- throw err;
93
- }
94
- function defineName(name, f) {
95
- Object.defineProperty(f, 'name', { value: name });
96
- return f;
97
- }
98
- function tryCall(f, wasi, args) {
99
- let r;
100
- try {
101
- r = f.apply(wasi, args);
102
- }
103
- catch (err) {
104
- return handleError(err);
105
- }
106
- if (isPromiseLike(r)) {
107
- return r.then(_ => _, handleError);
108
- }
109
- return r;
110
- }
111
- function syscallWrap(self, name, f) {
112
- let debug = false;
113
- const NODE_DEBUG_NATIVE = (() => {
114
- try {
115
- return process.env.NODE_DEBUG_NATIVE;
116
- }
117
- catch (_) {
118
- return undefined;
119
- }
120
- })();
121
- if (typeof NODE_DEBUG_NATIVE === 'string' && NODE_DEBUG_NATIVE.split(',').includes('wasi')) {
122
- debug = true;
123
- }
124
- return debug
125
- ? defineName(name, function () {
126
- const args = Array.prototype.slice.call(arguments);
127
- let debugArgs = [`${name}(${Array.from({ length: arguments.length }).map(() => '%d').join(', ')})`];
128
- debugArgs = debugArgs.concat(args);
129
- console.debug.apply(console, debugArgs);
130
- return tryCall(f, self, args);
131
- })
132
- : defineName(name, function () {
133
- return tryCall(f, self, arguments);
134
- });
135
- }
136
- function resolvePathSync(fs, fileDescriptor, path, flags) {
137
- let resolvedPath = resolve(fileDescriptor.realPath, path);
138
- if ((flags & 1) === 1) {
139
- try {
140
- resolvedPath = fs.readlinkSync(resolvedPath);
141
- }
142
- catch (err) {
143
- if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
144
- throw err;
145
- }
146
- }
147
- }
148
- return resolvedPath;
149
- }
150
- async function resolvePathAsync(fs, fileDescriptor, path, flags) {
151
- let resolvedPath = resolve(fileDescriptor.realPath, path);
152
- if ((flags & 1) === 1) {
153
- try {
154
- resolvedPath = await fs.promises.readlink(resolvedPath);
155
- }
156
- catch (err) {
157
- if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
158
- throw err;
159
- }
160
- }
161
- }
162
- return resolvedPath;
163
- }
164
- // eslint-disable-next-line spaced-comment
165
- const encoder = /*#__PURE__*/ new TextEncoder();
166
- // eslint-disable-next-line spaced-comment
167
- const decoder = /*#__PURE__*/ new TextDecoder();
168
- const INT64_MAX = (BigInt(1) << BigInt(63)) - BigInt(1);
169
- function readStdin() {
170
- const value = window.prompt();
171
- if (value === null)
172
- return new Uint8Array();
173
- const buffer = new TextEncoder().encode(value + '\n');
174
- return buffer;
175
- }
176
- function validateFstFlagsOrReturn(flags) {
177
- return (Boolean((flags) & ~(WasiFstFlag.SET_ATIM | WasiFstFlag.SET_ATIM_NOW |
178
- WasiFstFlag.SET_MTIM | WasiFstFlag.SET_MTIM_NOW)) ||
179
- ((flags) & (WasiFstFlag.SET_ATIM | WasiFstFlag.SET_ATIM_NOW)) ===
180
- (WasiFstFlag.SET_ATIM | WasiFstFlag.SET_ATIM_NOW) ||
181
- ((flags) & (WasiFstFlag.SET_MTIM | WasiFstFlag.SET_MTIM_NOW)) ===
182
- (WasiFstFlag.SET_MTIM | WasiFstFlag.SET_MTIM_NOW));
183
- }
184
- export class WASI {
185
- constructor(args, env, fds, asyncFs, fs, asyncify) {
186
- this.args_get = syscallWrap(this, 'args_get', function (argv, argv_buf) {
187
- argv = Number(argv);
188
- argv_buf = Number(argv_buf);
189
- if (argv === 0 || argv_buf === 0) {
190
- return WasiErrno.EINVAL;
191
- }
192
- const wasi = _wasi.get(this);
193
- const args = wasi.args;
194
- const argsSize = encoder.encode(args.join('\0') + '\0').length;
195
- const { HEAPU8, view } = getMemory(this, Math.max(argv + args.length * 4, argv_buf + argsSize));
196
- for (let i = 0; i < args.length; ++i) {
197
- const arg = args[i];
198
- view.setInt32(argv, argv_buf, true);
199
- argv += 4;
200
- const data = encoder.encode(arg + '\0');
201
- HEAPU8.set(data, argv_buf);
202
- argv_buf += data.length;
203
- }
204
- return WasiErrno.ESUCCESS;
205
- });
206
- this.args_sizes_get = syscallWrap(this, 'args_sizes_get', function (argc, argv_buf_size) {
207
- argc = Number(argc);
208
- argv_buf_size = Number(argv_buf_size);
209
- if (argc === 0 || argv_buf_size === 0) {
210
- return WasiErrno.EINVAL;
211
- }
212
- const { view } = getMemory(this, Math.max(argc + 4, argv_buf_size + 4));
213
- const wasi = _wasi.get(this);
214
- const args = wasi.args;
215
- view.setUint32(argc, args.length, true);
216
- view.setUint32(argv_buf_size, encoder.encode(args.join('\0') + '\0').length, true);
217
- return WasiErrno.ESUCCESS;
218
- });
219
- this.environ_get = syscallWrap(this, 'environ_get', function (environ, environ_buf) {
220
- environ = Number(environ);
221
- environ_buf = Number(environ_buf);
222
- if (environ === 0 || environ_buf === 0) {
223
- return WasiErrno.EINVAL;
224
- }
225
- const wasi = _wasi.get(this);
226
- const env = wasi.env;
227
- const envSize = encoder.encode(env.join('\0') + '\0').length;
228
- const { HEAPU8, view } = getMemory(this, Math.max(environ + env.length * 4, environ_buf + envSize));
229
- for (let i = 0; i < env.length; ++i) {
230
- const pair = env[i];
231
- view.setInt32(environ, environ_buf, true);
232
- environ += 4;
233
- const data = encoder.encode(pair + '\0');
234
- HEAPU8.set(data, environ_buf);
235
- environ_buf += data.length;
236
- }
237
- return WasiErrno.ESUCCESS;
238
- });
239
- this.environ_sizes_get = syscallWrap(this, 'environ_sizes_get', function (len, buflen) {
240
- len = Number(len);
241
- buflen = Number(buflen);
242
- if (len === 0 || buflen === 0) {
243
- return WasiErrno.EINVAL;
244
- }
245
- const { view } = getMemory(this, Math.max(len + 4, buflen + 4));
246
- const wasi = _wasi.get(this);
247
- view.setUint32(len, wasi.env.length, true);
248
- view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
249
- return WasiErrno.ESUCCESS;
250
- });
251
- this.clock_res_get = syscallWrap(this, 'clock_res_get', function (id, resolution) {
252
- resolution = Number(resolution);
253
- if (resolution === 0) {
254
- return WasiErrno.EINVAL;
255
- }
256
- const { view } = getMemory(this, resolution + 8);
257
- switch (id) {
258
- case WasiClockid.REALTIME:
259
- view.setBigUint64(resolution, BigInt(1000000), true);
260
- return WasiErrno.ESUCCESS;
261
- case WasiClockid.MONOTONIC:
262
- case WasiClockid.PROCESS_CPUTIME_ID:
263
- case WasiClockid.THREAD_CPUTIME_ID:
264
- view.setBigUint64(resolution, BigInt(1000), true);
265
- return WasiErrno.ESUCCESS;
266
- default: return WasiErrno.EINVAL;
267
- }
268
- });
269
- this.clock_time_get = syscallWrap(this, 'clock_time_get', function (id, _percision, time) {
270
- time = Number(time);
271
- if (time === 0) {
272
- return WasiErrno.EINVAL;
273
- }
274
- const { view } = getMemory(this, time + 8);
275
- switch (id) {
276
- case WasiClockid.REALTIME:
277
- view.setBigUint64(time, BigInt(Date.now()) * BigInt(1000000), true);
278
- return WasiErrno.ESUCCESS;
279
- case WasiClockid.MONOTONIC:
280
- case WasiClockid.PROCESS_CPUTIME_ID:
281
- case WasiClockid.THREAD_CPUTIME_ID: {
282
- const t = performance.now() / 1000;
283
- const s = Math.trunc(t);
284
- const ms = Math.floor((t - s) * 1000);
285
- const result = BigInt(s) * BigInt(1000000000) + BigInt(ms) * BigInt(1000000);
286
- view.setBigUint64(time, result, true);
287
- return WasiErrno.ESUCCESS;
288
- }
289
- default: return WasiErrno.EINVAL;
290
- }
291
- });
292
- this.fd_advise = syscallWrap(this, 'fd_advise', function (_fd, _offset, _len, _advice) {
293
- return WasiErrno.ENOSYS;
294
- });
295
- this.fd_fdstat_get = syscallWrap(this, 'fd_fdstat_get', function (fd, fdstat) {
296
- fdstat = Number(fdstat);
297
- if (fdstat === 0) {
298
- return WasiErrno.EINVAL;
299
- }
300
- const wasi = _wasi.get(this);
301
- const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
302
- const { view } = getMemory(this, fdstat + 24);
303
- view.setUint16(fdstat, fileDescriptor.type, true);
304
- view.setUint16(fdstat + 2, 0, true);
305
- view.setBigUint64(fdstat + 8, fileDescriptor.rightsBase, true);
306
- view.setBigUint64(fdstat + 16, fileDescriptor.rightsInheriting, true);
307
- return WasiErrno.ESUCCESS;
308
- });
309
- this.fd_fdstat_set_flags = syscallWrap(this, 'fd_fdstat_set_flags', function (_fd, _flags) {
310
- return WasiErrno.ENOSYS;
311
- });
312
- this.fd_fdstat_set_rights = syscallWrap(this, 'fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
313
- const wasi = _wasi.get(this);
314
- const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
315
- if ((rightsBase | fileDescriptor.rightsBase) > fileDescriptor.rightsBase) {
316
- return WasiErrno.ENOTCAPABLE;
317
- }
318
- if ((rightsInheriting | fileDescriptor.rightsInheriting) >
319
- fileDescriptor.rightsInheriting) {
320
- return WasiErrno.ENOTCAPABLE;
321
- }
322
- fileDescriptor.rightsBase = rightsBase;
323
- fileDescriptor.rightsInheriting = rightsInheriting;
324
- return WasiErrno.ESUCCESS;
325
- });
326
- this.fd_prestat_get = syscallWrap(this, 'fd_prestat_get', function (fd, prestat) {
327
- prestat = Number(prestat);
328
- if (prestat === 0) {
329
- return WasiErrno.EINVAL;
330
- }
331
- const wasi = _wasi.get(this);
332
- let fileDescriptor;
333
- try {
334
- fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
335
- }
336
- catch (err) {
337
- if (err instanceof WasiError)
338
- return err.errno;
339
- throw err;
340
- }
341
- if (fileDescriptor.preopen !== 1)
342
- return WasiErrno.EINVAL;
343
- const { view } = getMemory(this, prestat + 8);
344
- // preopen type is dir(0)
345
- view.setUint32(prestat, 0, true);
346
- view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length, true);
347
- return WasiErrno.ESUCCESS;
348
- });
349
- this.fd_prestat_dir_name = syscallWrap(this, 'fd_prestat_dir_name', function (fd, path, path_len) {
350
- path = Number(path);
351
- path_len = Number(path_len);
352
- if (path === 0) {
353
- return WasiErrno.EINVAL;
354
- }
355
- const wasi = _wasi.get(this);
356
- const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
357
- if (fileDescriptor.preopen !== 1)
358
- return WasiErrno.EBADF;
359
- const buffer = encoder.encode(fileDescriptor.path);
360
- const size = buffer.length;
361
- if (size > path_len)
362
- return WasiErrno.ENOBUFS;
363
- const { HEAPU8 } = getMemory(this, path + size);
364
- HEAPU8.set(buffer, path);
365
- return WasiErrno.ESUCCESS;
366
- });
367
- this.fd_seek = syscallWrap(this, 'fd_seek', function (fd, offset, whence, newOffset) {
368
- newOffset = Number(newOffset);
369
- if (newOffset === 0) {
370
- return WasiErrno.EINVAL;
371
- }
372
- if (fd === 0 || fd === 1 || fd === 2)
373
- return WasiErrno.ESUCCESS;
374
- const wasi = _wasi.get(this);
375
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SEEK, BigInt(0));
376
- const r = fileDescriptor.seek(offset, whence);
377
- const { view } = getMemory(this, newOffset + 8);
378
- view.setBigUint64(newOffset, r, true);
379
- return WasiErrno.ESUCCESS;
380
- });
381
- this.fd_tell = syscallWrap(this, 'fd_tell', function (fd, offset) {
382
- const wasi = _wasi.get(this);
383
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
384
- const pos = BigInt(fileDescriptor.pos);
385
- const { view } = getMemory(this, Number(offset) + 8);
386
- view.setBigUint64(Number(offset), pos, true);
387
- return WasiErrno.ESUCCESS;
388
- });
389
- this.poll_oneoff = syscallWrap(this, 'poll_oneoff', function (in_ptr, out_ptr, nsubscriptions, nevents) {
390
- in_ptr = Number(in_ptr);
391
- out_ptr = Number(out_ptr);
392
- nevents = Number(nevents);
393
- nsubscriptions = Number(nsubscriptions);
394
- nsubscriptions = nsubscriptions >>> 0;
395
- if (in_ptr === 0 || out_ptr === 0 || nsubscriptions === 0 || nevents === 0) {
396
- return WasiErrno.EINVAL;
397
- }
398
- const { view } = getMemory(this, Math.max(in_ptr + nsubscriptions * 48, out_ptr + nsubscriptions * 32, nevents + 4));
399
- view.setUint32(nevents, 0, true);
400
- let i = 0;
401
- let timer_userdata = BigInt(0);
402
- let cur_timeout = BigInt(0);
403
- let has_timeout = 0;
404
- let min_timeout = BigInt(0);
405
- let sub;
406
- const subscriptions = Array(nsubscriptions);
407
- for (i = 0; i < nsubscriptions; i++) {
408
- sub = in_ptr + i * 48;
409
- const userdata = view.getBigUint64(sub, true);
410
- const type = view.getUint8(sub + 8);
411
- const clockIdOrFd = view.getUint32(sub + 16, true);
412
- const timeout = view.getBigUint64(sub + 24, true);
413
- const precision = view.getBigUint64(sub + 32, true);
414
- const flags = view.getUint16(sub + 40, true);
415
- subscriptions[i] = {
416
- userdata,
417
- type,
418
- u: {
419
- clock: {
420
- clock_id: clockIdOrFd,
421
- timeout,
422
- precision,
423
- flags
424
- },
425
- fd_readwrite: {
426
- fd: clockIdOrFd
427
- }
428
- }
429
- };
430
- }
431
- const fdevents = [];
432
- for (i = 0; i < nsubscriptions; i++) {
433
- sub = subscriptions[i];
434
- switch (sub.type) {
435
- case WasiEventType.CLOCK: {
436
- if (sub.u.clock.flags === WasiSubclockflags.ABSTIME) {
437
- /* Convert absolute time to relative delay. */
438
- const now = BigInt(Date.now()) * BigInt(1000000);
439
- cur_timeout = sub.u.clock.timeout - now;
440
- }
441
- else {
442
- cur_timeout = sub.u.clock.timeout;
443
- }
444
- if (has_timeout === 0 || cur_timeout < min_timeout) {
445
- min_timeout = cur_timeout;
446
- timer_userdata = sub.userdata;
447
- has_timeout = 1;
448
- }
449
- break;
450
- }
451
- case WasiEventType.FD_READ:
452
- case WasiEventType.FD_WRITE:
453
- fdevents.push(sub);
454
- break;
455
- default: return WasiErrno.EINVAL;
456
- }
457
- }
458
- if (fdevents.length > 0) {
459
- for (i = 0; i < fdevents.length; i++) {
460
- const fdevent = fdevents[i];
461
- const event = out_ptr + 32 * i;
462
- view.setBigUint64(event, fdevent.userdata, true);
463
- view.setUint32(event + 8, WasiErrno.ENOSYS, true);
464
- view.setUint32(event + 12, fdevent.type, true);
465
- view.setBigUint64(event + 16, BigInt(0), true);
466
- view.setUint16(event + 24, 0, true);
467
- view.setUint32(nevents, 1, true);
468
- }
469
- view.setUint32(nevents, fdevents.length, true);
470
- return WasiErrno.ESUCCESS;
471
- }
472
- if (has_timeout) {
473
- const delay = Number(min_timeout / BigInt(1000000));
474
- // if (isMainThread || typeof SharedArrayBuffer !== 'function') {
475
- sleepBreakIf(delay, () => false);
476
- // } else {
477
- // const buf = new SharedArrayBuffer(4)
478
- // const arr = new Int32Array(buf)
479
- // postMsg({
480
- // __tybys_wasm_util_wasi__: {
481
- // type: 'set-timeout',
482
- // payload: {
483
- // buffer: buf,
484
- // delay
485
- // }
486
- // }
487
- // })
488
- // Atomics.wait(arr, 0, 0)
489
- // }
490
- const event = out_ptr;
491
- view.setBigUint64(event, timer_userdata, true);
492
- view.setUint32(event + 8, WasiErrno.ESUCCESS, true);
493
- view.setUint32(event + 12, WasiEventType.CLOCK, true);
494
- view.setUint32(nevents, 1, true);
495
- }
496
- return WasiErrno.ESUCCESS;
497
- });
498
- this.proc_exit = syscallWrap(this, 'proc_exit', function (rval) {
499
- if ((typeof process === 'object') && (process !== null) && (typeof process.exit === 'function')) {
500
- process.exit(rval);
501
- }
502
- return WasiErrno.ESUCCESS;
503
- });
504
- this.proc_raise = syscallWrap(this, 'proc_raise', function (_sig) {
505
- return WasiErrno.ENOSYS;
506
- });
507
- this.sched_yield = syscallWrap(this, 'sched_yield', function () {
508
- return WasiErrno.ESUCCESS;
509
- });
510
- this.random_get = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function'
511
- ? syscallWrap(this, 'random_get', function (buf, buf_len) {
512
- buf = Number(buf);
513
- if (buf === 0) {
514
- return WasiErrno.EINVAL;
515
- }
516
- buf_len = Number(buf_len);
517
- const { HEAPU8, view } = getMemory(this, buf + buf_len);
518
- if ((typeof SharedArrayBuffer === 'function' && HEAPU8.buffer instanceof SharedArrayBuffer) ||
519
- (Object.prototype.toString.call(HEAPU8.buffer) === '[object SharedArrayBuffer]')) {
520
- for (let i = buf; i < buf + buf_len; ++i) {
521
- view.setUint8(i, Math.floor(Math.random() * 256));
522
- }
523
- return WasiErrno.ESUCCESS;
524
- }
525
- let pos;
526
- const stride = 65536;
527
- for (pos = 0; pos + stride < buf_len; pos += stride) {
528
- crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + pos + stride));
529
- }
530
- crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + buf_len));
531
- return WasiErrno.ESUCCESS;
532
- })
533
- : syscallWrap(this, 'random_get', function (buf, buf_len) {
534
- buf = Number(buf);
535
- if (buf === 0) {
536
- return WasiErrno.EINVAL;
537
- }
538
- buf_len = Number(buf_len);
539
- const { view } = getMemory(this, buf + buf_len);
540
- for (let i = buf; i < buf + buf_len; ++i) {
541
- view.setUint8(i, Math.floor(Math.random() * 256));
542
- }
543
- return WasiErrno.ESUCCESS;
544
- });
545
- this.sock_recv = syscallWrap(this, 'sock_recv', function () {
546
- return WasiErrno.ENOTSUP;
547
- });
548
- this.sock_send = syscallWrap(this, 'sock_send', function () {
549
- return WasiErrno.ENOTSUP;
550
- });
551
- this.sock_shutdown = syscallWrap(this, 'sock_shutdown', function () {
552
- return WasiErrno.ENOTSUP;
553
- });
554
- this.sock_accept = syscallWrap(this, 'sock_accept', function () {
555
- return WasiErrno.ENOTSUP;
556
- });
557
- _wasi.set(this, {
558
- fds,
559
- args,
560
- env
561
- });
562
- if (fs)
563
- _fs.set(this, fs);
564
- // eslint-disable-next-line @typescript-eslint/no-this-alias
565
- const _this = this;
566
- function defineImport(name, syncVersion, asyncVersion, parameterType, returnType) {
567
- if (asyncFs) {
568
- if (asyncify) {
569
- _this[name] = asyncify.wrapImportFunction(syscallWrap(_this, name, asyncVersion));
570
- }
571
- else {
572
- _this[name] = wrapAsyncImport(syscallWrap(_this, name, asyncVersion), parameterType, returnType);
573
- }
574
- }
575
- else {
576
- _this[name] = syscallWrap(_this, name, syncVersion);
577
- }
578
- }
579
- defineImport('fd_allocate', function fd_allocate(fd, offset, len) {
580
- const wasi = _wasi.get(this);
581
- const fs = getFs(this);
582
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
583
- const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
584
- if (stat.size < offset + len) {
585
- fs.ftruncateSync(fileDescriptor.fd, Number(offset + len));
586
- }
587
- return WasiErrno.ESUCCESS;
588
- }, async function fd_allocate(fd, offset, len) {
589
- const wasi = _wasi.get(this);
590
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
591
- const h = fileDescriptor.fd;
592
- const stat = await h.stat({ bigint: true });
593
- if (stat.size < offset + len) {
594
- await h.truncate(Number(offset + len));
595
- }
596
- return WasiErrno.ESUCCESS;
597
- }, ['i32', 'i64', 'f64'], ['i32']);
598
- defineImport('fd_close', function fd_close(fd) {
599
- const wasi = _wasi.get(this);
600
- const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
601
- const fs = getFs(this);
602
- fs.closeSync(fileDescriptor.fd);
603
- wasi.fds.remove(fd);
604
- return WasiErrno.ESUCCESS;
605
- }, async function fd_close(fd) {
606
- const wasi = _wasi.get(this);
607
- const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
608
- await fileDescriptor.fd.close();
609
- wasi.fds.remove(fd);
610
- return WasiErrno.ESUCCESS;
611
- }, ['i32'], ['i32']);
612
- defineImport('fd_datasync', function fd_datasync(fd) {
613
- const wasi = _wasi.get(this);
614
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
615
- const fs = getFs(this);
616
- fs.fdatasyncSync(fileDescriptor.fd);
617
- return WasiErrno.ESUCCESS;
618
- }, async function fd_datasync(fd) {
619
- const wasi = _wasi.get(this);
620
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
621
- await fileDescriptor.fd.datasync();
622
- return WasiErrno.ESUCCESS;
623
- }, ['i32'], ['i32']);
624
- defineImport('fd_filestat_get', function fd_filestat_get(fd, buf) {
625
- buf = Number(buf);
626
- if (buf === 0)
627
- return WasiErrno.EINVAL;
628
- const wasi = _wasi.get(this);
629
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
630
- const fs = getFs(this);
631
- const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
632
- const { view } = getMemory(this, buf + 64);
633
- toFileStat(view, buf, stat);
634
- return WasiErrno.ESUCCESS;
635
- }, async function fd_filestat_get(fd, buf) {
636
- buf = Number(buf);
637
- if (buf === 0)
638
- return WasiErrno.EINVAL;
639
- const wasi = _wasi.get(this);
640
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
641
- const h = fileDescriptor.fd;
642
- const stat = await h.stat({ bigint: true });
643
- const { view } = getMemory(this, buf + 64);
644
- toFileStat(view, buf, stat);
645
- return WasiErrno.ESUCCESS;
646
- }, ['i32', 'i32'], ['i32']);
647
- defineImport('fd_filestat_set_size', function fd_filestat_set_size(fd, size) {
648
- const wasi = _wasi.get(this);
649
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
650
- const fs = getFs(this);
651
- fs.ftruncateSync(fileDescriptor.fd, Number(size));
652
- return WasiErrno.ESUCCESS;
653
- }, async function fd_filestat_set_size(fd, size) {
654
- const wasi = _wasi.get(this);
655
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
656
- const h = fileDescriptor.fd;
657
- await h.truncate(Number(size));
658
- return WasiErrno.ESUCCESS;
659
- }, ['i32', 'i64'], ['i32']);
660
- function fdFilestatGetTimes(fd, atim, mtim, flags) {
661
- const wasi = _wasi.get(this);
662
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_TIMES, BigInt(0));
663
- if ((flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
664
- atim = BigInt(Date.now() * 1000000);
665
- }
666
- if ((flags & WasiFstFlag.SET_MTIM_NOW) === WasiFstFlag.SET_MTIM_NOW) {
667
- mtim = BigInt(Date.now() * 1000000);
668
- }
669
- return { fileDescriptor, atim, mtim };
670
- }
671
- defineImport('fd_filestat_set_times', function fd_filestat_set_times(fd, atim, mtim, flags) {
672
- if (validateFstFlagsOrReturn(flags)) {
673
- return WasiErrno.EINVAL;
674
- }
675
- const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
676
- const fs = getFs(this);
677
- fs.futimesSync(fileDescriptor.fd, Number(atimRes), Number(mtimRes));
678
- return WasiErrno.ESUCCESS;
679
- }, async function fd_filestat_set_times(fd, atim, mtim, flags) {
680
- if (validateFstFlagsOrReturn(flags)) {
681
- return WasiErrno.EINVAL;
682
- }
683
- const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
684
- const h = fileDescriptor.fd;
685
- await h.utimes(Number(atimRes), Number(mtimRes));
686
- return WasiErrno.ESUCCESS;
687
- }, ['i32', 'i64', 'i64', 'i32'], ['i32']);
688
- defineImport('fd_pread', function fd_pread(fd, iovs, iovslen, offset, size) {
689
- iovs = Number(iovs);
690
- size = Number(size);
691
- if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
692
- return WasiErrno.EINVAL;
693
- }
694
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
695
- const wasi = _wasi.get(this);
696
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
697
- if (!iovslen) {
698
- view.setUint32(size, 0, true);
699
- return WasiErrno.ESUCCESS;
700
- }
701
- let totalSize = 0;
702
- const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
703
- const offset = iovs + (i * 8);
704
- const buf = view.getInt32(offset, true);
705
- const bufLen = view.getUint32(offset + 4, true);
706
- totalSize += bufLen;
707
- return HEAPU8.subarray(buf, buf + bufLen);
708
- });
709
- let nread = 0;
710
- const buffer = (() => {
711
- try {
712
- return new Uint8Array(new SharedArrayBuffer(totalSize));
713
- }
714
- catch (_) {
715
- return new Uint8Array(totalSize);
716
- }
717
- })();
718
- buffer._isBuffer = true;
719
- const fs = getFs(this);
720
- const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
721
- nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
722
- view.setUint32(size, nread, true);
723
- return WasiErrno.ESUCCESS;
724
- }, async function (fd, iovs, iovslen, offset, size) {
725
- iovs = Number(iovs);
726
- size = Number(size);
727
- if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
728
- return WasiErrno.EINVAL;
729
- }
730
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
731
- const wasi = _wasi.get(this);
732
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
733
- if (!iovslen) {
734
- view.setUint32(size, 0, true);
735
- return WasiErrno.ESUCCESS;
736
- }
737
- let totalSize = 0;
738
- const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
739
- const offset = iovs + (i * 8);
740
- const buf = view.getInt32(offset, true);
741
- const bufLen = view.getUint32(offset + 4, true);
742
- totalSize += bufLen;
743
- return HEAPU8.subarray(buf, buf + bufLen);
744
- });
745
- let nread = 0;
746
- const buffer = new Uint8Array(totalSize);
747
- buffer._isBuffer = true;
748
- const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(offset));
749
- nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
750
- view.setUint32(size, nread, true);
751
- return WasiErrno.ESUCCESS;
752
- }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
753
- defineImport('fd_pwrite', function fd_pwrite(fd, iovs, iovslen, offset, size) {
754
- iovs = Number(iovs);
755
- size = Number(size);
756
- if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
757
- return WasiErrno.EINVAL;
758
- }
759
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
760
- const wasi = _wasi.get(this);
761
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
762
- if (!iovslen) {
763
- view.setUint32(size, 0, true);
764
- return WasiErrno.ESUCCESS;
765
- }
766
- const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
767
- const offset = iovs + (i * 8);
768
- const buf = view.getInt32(offset, true);
769
- const bufLen = view.getUint32(offset + 4, true);
770
- return HEAPU8.subarray(buf, buf + bufLen);
771
- }));
772
- const fs = getFs(this);
773
- const nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
774
- view.setUint32(size, nwritten, true);
775
- return WasiErrno.ESUCCESS;
776
- }, async function fd_pwrite(fd, iovs, iovslen, offset, size) {
777
- iovs = Number(iovs);
778
- size = Number(size);
779
- if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
780
- return WasiErrno.EINVAL;
781
- }
782
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
783
- const wasi = _wasi.get(this);
784
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
785
- if (!iovslen) {
786
- view.setUint32(size, 0, true);
787
- return WasiErrno.ESUCCESS;
788
- }
789
- const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
790
- const offset = iovs + (i * 8);
791
- const buf = view.getInt32(offset, true);
792
- const bufLen = view.getUint32(offset + 4, true);
793
- return HEAPU8.subarray(buf, buf + bufLen);
794
- }));
795
- const { bytesWritten } = await fileDescriptor.fd.write(buffer, 0, buffer.length, Number(offset));
796
- view.setUint32(size, bytesWritten, true);
797
- return WasiErrno.ESUCCESS;
798
- }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
799
- defineImport('fd_read', function fd_read(fd, iovs, iovslen, size) {
800
- iovs = Number(iovs);
801
- size = Number(size);
802
- if ((iovs === 0 && iovslen) || size === 0) {
803
- return WasiErrno.EINVAL;
804
- }
805
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
806
- const wasi = _wasi.get(this);
807
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
808
- if (!iovslen) {
809
- view.setUint32(size, 0, true);
810
- return WasiErrno.ESUCCESS;
811
- }
812
- let totalSize = 0;
813
- const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
814
- const offset = iovs + (i * 8);
815
- const buf = view.getInt32(offset, true);
816
- const bufLen = view.getUint32(offset + 4, true);
817
- totalSize += bufLen;
818
- return HEAPU8.subarray(buf, buf + bufLen);
819
- });
820
- let buffer;
821
- let nread = 0;
822
- if (fd === 0) {
823
- if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
824
- return WasiErrno.ENOTSUP;
825
- }
826
- buffer = readStdin();
827
- nread = buffer ? copyMemory(ioVecs, buffer) : 0;
828
- }
829
- else {
830
- buffer = (() => {
831
- try {
832
- return new Uint8Array(new SharedArrayBuffer(totalSize));
833
- }
834
- catch (_) {
835
- return new Uint8Array(totalSize);
836
- }
837
- })();
838
- buffer._isBuffer = true;
839
- const fs = getFs(this);
840
- const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
841
- nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
842
- fileDescriptor.pos += BigInt(nread);
843
- }
844
- view.setUint32(size, nread, true);
845
- return WasiErrno.ESUCCESS;
846
- }, async function fd_read(fd, iovs, iovslen, size) {
847
- iovs = Number(iovs);
848
- size = Number(size);
849
- if ((iovs === 0 && iovslen) || size === 0) {
850
- return WasiErrno.EINVAL;
851
- }
852
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
853
- const wasi = _wasi.get(this);
854
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
855
- if (!iovslen) {
856
- view.setUint32(size, 0, true);
857
- return WasiErrno.ESUCCESS;
858
- }
859
- let totalSize = 0;
860
- const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
861
- const offset = iovs + (i * 8);
862
- const buf = view.getInt32(offset, true);
863
- const bufLen = view.getUint32(offset + 4, true);
864
- totalSize += bufLen;
865
- return HEAPU8.subarray(buf, buf + bufLen);
866
- });
867
- let buffer;
868
- let nread = 0;
869
- if (fd === 0) {
870
- if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
871
- return WasiErrno.ENOTSUP;
872
- }
873
- buffer = readStdin();
874
- nread = buffer ? copyMemory(ioVecs, buffer) : 0;
875
- }
876
- else {
877
- buffer = new Uint8Array(totalSize);
878
- buffer._isBuffer = true;
879
- const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(fileDescriptor.pos));
880
- nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
881
- fileDescriptor.pos += BigInt(nread);
882
- }
883
- view.setUint32(size, nread, true);
884
- return WasiErrno.ESUCCESS;
885
- }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
886
- defineImport('fd_readdir', function fd_readdir(fd, buf, buf_len, cookie, bufused) {
887
- buf = Number(buf);
888
- buf_len = Number(buf_len);
889
- bufused = Number(bufused);
890
- if (buf === 0 || bufused === 0)
891
- return WasiErrno.ESUCCESS;
892
- const wasi = _wasi.get(this);
893
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
894
- const fs = getFs(this);
895
- const entries = fs.readdirSync(fileDescriptor.realPath, { withFileTypes: true });
896
- const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
897
- let bufferUsed = 0;
898
- for (let i = Number(cookie); i < entries.length; i++) {
899
- const nameData = encoder.encode(entries[i].name);
900
- const entryInfo = fs.statSync(resolve(fileDescriptor.realPath, entries[i].name), { bigint: true });
901
- const entryData = new Uint8Array(24 + nameData.byteLength);
902
- const entryView = new DataView(entryData.buffer);
903
- entryView.setBigUint64(0, BigInt(i + 1), true);
904
- entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
905
- entryView.setUint32(16, nameData.byteLength, true);
906
- let type;
907
- if (entries[i].isFile()) {
908
- type = WasiFileType.REGULAR_FILE;
909
- }
910
- else if (entries[i].isDirectory()) {
911
- type = WasiFileType.DIRECTORY;
912
- }
913
- else if (entries[i].isSymbolicLink()) {
914
- type = WasiFileType.SYMBOLIC_LINK;
915
- }
916
- else if (entries[i].isCharacterDevice()) {
917
- type = WasiFileType.CHARACTER_DEVICE;
918
- }
919
- else if (entries[i].isBlockDevice()) {
920
- type = WasiFileType.BLOCK_DEVICE;
921
- }
922
- else if (entries[i].isSocket()) {
923
- type = WasiFileType.SOCKET_STREAM;
924
- }
925
- else {
926
- type = WasiFileType.UNKNOWN;
927
- }
928
- entryView.setUint8(20, type);
929
- entryData.set(nameData, 24);
930
- const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
931
- HEAPU8.set(data, buf + bufferUsed);
932
- bufferUsed += data.byteLength;
933
- }
934
- view.setUint32(bufused, bufferUsed, true);
935
- return WasiErrno.ESUCCESS;
936
- }, async function fd_readdir(fd, buf, buf_len, cookie, bufused) {
937
- buf = Number(buf);
938
- buf_len = Number(buf_len);
939
- bufused = Number(bufused);
940
- if (buf === 0 || bufused === 0)
941
- return WasiErrno.ESUCCESS;
942
- const wasi = _wasi.get(this);
943
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
944
- const fs = getFs(this);
945
- const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
946
- const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
947
- let bufferUsed = 0;
948
- for (let i = Number(cookie); i < entries.length; i++) {
949
- const nameData = encoder.encode(entries[i].name);
950
- const entryInfo = await fs.promises.stat(resolve(fileDescriptor.realPath, entries[i].name), { bigint: true });
951
- const entryData = new Uint8Array(24 + nameData.byteLength);
952
- const entryView = new DataView(entryData.buffer);
953
- entryView.setBigUint64(0, BigInt(i + 1), true);
954
- entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
955
- entryView.setUint32(16, nameData.byteLength, true);
956
- let type;
957
- if (entries[i].isFile()) {
958
- type = WasiFileType.REGULAR_FILE;
959
- }
960
- else if (entries[i].isDirectory()) {
961
- type = WasiFileType.DIRECTORY;
962
- }
963
- else if (entries[i].isSymbolicLink()) {
964
- type = WasiFileType.SYMBOLIC_LINK;
965
- }
966
- else if (entries[i].isCharacterDevice()) {
967
- type = WasiFileType.CHARACTER_DEVICE;
968
- }
969
- else if (entries[i].isBlockDevice()) {
970
- type = WasiFileType.BLOCK_DEVICE;
971
- }
972
- else if (entries[i].isSocket()) {
973
- type = WasiFileType.SOCKET_STREAM;
974
- }
975
- else {
976
- type = WasiFileType.UNKNOWN;
977
- }
978
- entryView.setUint8(20, type);
979
- entryData.set(nameData, 24);
980
- const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
981
- HEAPU8.set(data, buf + bufferUsed);
982
- bufferUsed += data.byteLength;
983
- }
984
- view.setUint32(bufused, bufferUsed, true);
985
- return WasiErrno.ESUCCESS;
986
- }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
987
- defineImport('fd_renumber', function fd_renumber(from, to) {
988
- const wasi = _wasi.get(this);
989
- wasi.fds.renumber(to, from);
990
- return WasiErrno.ESUCCESS;
991
- }, async function fd_renumber(from, to) {
992
- const wasi = _wasi.get(this);
993
- await wasi.fds.renumber(to, from);
994
- return WasiErrno.ESUCCESS;
995
- }, ['i32', 'i32'], ['i32']);
996
- defineImport('fd_sync', function fd_sync(fd) {
997
- const wasi = _wasi.get(this);
998
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
999
- const fs = getFs(this);
1000
- fs.fsyncSync(fileDescriptor.fd);
1001
- return WasiErrno.ESUCCESS;
1002
- }, async function fd_sync(fd) {
1003
- const wasi = _wasi.get(this);
1004
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
1005
- await fileDescriptor.fd.sync();
1006
- return WasiErrno.ESUCCESS;
1007
- }, ['i32'], ['i32']);
1008
- defineImport('fd_write', function fd_write(fd, iovs, iovslen, size) {
1009
- iovs = Number(iovs);
1010
- size = Number(size);
1011
- if ((iovs === 0 && iovslen) || size === 0) {
1012
- return WasiErrno.EINVAL;
1013
- }
1014
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1015
- const wasi = _wasi.get(this);
1016
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
1017
- if (!iovslen) {
1018
- view.setUint32(size, 0, true);
1019
- return WasiErrno.ESUCCESS;
1020
- }
1021
- const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1022
- const offset = iovs + (i * 8);
1023
- const buf = view.getInt32(offset, true);
1024
- const bufLen = view.getUint32(offset + 4, true);
1025
- return HEAPU8.subarray(buf, buf + bufLen);
1026
- }));
1027
- let nwritten;
1028
- if (fd === 1 || fd === 2) {
1029
- nwritten = fileDescriptor.write(buffer);
1030
- }
1031
- else {
1032
- const fs = getFs(this);
1033
- nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
1034
- fileDescriptor.pos += BigInt(nwritten);
1035
- }
1036
- view.setUint32(size, nwritten, true);
1037
- return WasiErrno.ESUCCESS;
1038
- }, async function fd_write(fd, iovs, iovslen, size) {
1039
- iovs = Number(iovs);
1040
- size = Number(size);
1041
- if ((iovs === 0 && iovslen) || size === 0) {
1042
- return WasiErrno.EINVAL;
1043
- }
1044
- const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1045
- const wasi = _wasi.get(this);
1046
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
1047
- if (!iovslen) {
1048
- view.setUint32(size, 0, true);
1049
- return WasiErrno.ESUCCESS;
1050
- }
1051
- const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1052
- const offset = iovs + (i * 8);
1053
- const buf = view.getInt32(offset, true);
1054
- const bufLen = view.getUint32(offset + 4, true);
1055
- return HEAPU8.subarray(buf, buf + bufLen);
1056
- }));
1057
- let nwritten;
1058
- if (fd === 1 || fd === 2) {
1059
- nwritten = fileDescriptor.write(buffer);
1060
- }
1061
- else {
1062
- nwritten = await (await (fileDescriptor.fd.write(buffer, 0, buffer.length, Number(fileDescriptor.pos)))).bytesWritten;
1063
- fileDescriptor.pos += BigInt(nwritten);
1064
- }
1065
- view.setUint32(size, nwritten, true);
1066
- return WasiErrno.ESUCCESS;
1067
- }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1068
- defineImport('path_create_directory', function path_create_directory(fd, path, path_len) {
1069
- path = Number(path);
1070
- path_len = Number(path_len);
1071
- if (path === 0) {
1072
- return WasiErrno.EINVAL;
1073
- }
1074
- const { HEAPU8 } = getMemory(this, path + path_len);
1075
- const wasi = _wasi.get(this);
1076
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1077
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1078
- pathString = resolve(fileDescriptor.realPath, pathString);
1079
- const fs = getFs(this);
1080
- fs.mkdirSync(pathString);
1081
- return WasiErrno.ESUCCESS;
1082
- }, async function path_create_directory(fd, path, path_len) {
1083
- path = Number(path);
1084
- path_len = Number(path_len);
1085
- if (path === 0) {
1086
- return WasiErrno.EINVAL;
1087
- }
1088
- const { HEAPU8 } = getMemory(this, path + path_len);
1089
- const wasi = _wasi.get(this);
1090
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1091
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1092
- pathString = resolve(fileDescriptor.realPath, pathString);
1093
- const fs = getFs(this);
1094
- await fs.promises.mkdir(pathString);
1095
- return WasiErrno.ESUCCESS;
1096
- }, ['i32', 'i32', 'i32'], ['i32']);
1097
- defineImport('path_filestat_get', function path_filestat_get(fd, flags, path, path_len, filestat) {
1098
- path = Number(path);
1099
- path_len = Number(path_len);
1100
- filestat = Number(filestat);
1101
- if (path === 0 || filestat === 0) {
1102
- return WasiErrno.EINVAL;
1103
- }
1104
- const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
1105
- const wasi = _wasi.get(this);
1106
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1107
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1108
- const fs = getFs(this);
1109
- pathString = resolve(fileDescriptor.realPath, pathString);
1110
- let stat;
1111
- if ((flags & 1) === 1) {
1112
- stat = fs.statSync(pathString, { bigint: true });
1113
- }
1114
- else {
1115
- stat = fs.lstatSync(pathString, { bigint: true });
1116
- }
1117
- toFileStat(view, filestat, stat);
1118
- return WasiErrno.ESUCCESS;
1119
- }, async function path_filestat_get(fd, flags, path, path_len, filestat) {
1120
- path = Number(path);
1121
- path_len = Number(path_len);
1122
- filestat = Number(filestat);
1123
- if (path === 0 || filestat === 0) {
1124
- return WasiErrno.EINVAL;
1125
- }
1126
- const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
1127
- const wasi = _wasi.get(this);
1128
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1129
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1130
- const fs = getFs(this);
1131
- pathString = resolve(fileDescriptor.realPath, pathString);
1132
- let stat;
1133
- if ((flags & 1) === 1) {
1134
- stat = await fs.promises.stat(pathString, { bigint: true });
1135
- }
1136
- else {
1137
- stat = await fs.promises.lstat(pathString, { bigint: true });
1138
- }
1139
- toFileStat(view, filestat, stat);
1140
- return WasiErrno.ESUCCESS;
1141
- }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1142
- defineImport('path_filestat_set_times', function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1143
- path = Number(path);
1144
- path_len = Number(path_len);
1145
- if (path === 0)
1146
- return WasiErrno.EINVAL;
1147
- const { HEAPU8 } = getMemory(this, path + path_len);
1148
- const wasi = _wasi.get(this);
1149
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1150
- if (validateFstFlagsOrReturn(fst_flags)) {
1151
- return WasiErrno.EINVAL;
1152
- }
1153
- const fs = getFs(this);
1154
- const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1155
- if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
1156
- atim = BigInt(Date.now() * 1000000);
1157
- }
1158
- if ((fst_flags & WasiFstFlag.SET_MTIM_NOW) === WasiFstFlag.SET_MTIM_NOW) {
1159
- mtim = BigInt(Date.now() * 1000000);
1160
- }
1161
- fs.utimesSync(resolvedPath, Number(atim), Number(mtim));
1162
- return WasiErrno.ESUCCESS;
1163
- }, async function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1164
- path = Number(path);
1165
- path_len = Number(path_len);
1166
- if (path === 0)
1167
- return WasiErrno.EINVAL;
1168
- const { HEAPU8 } = getMemory(this, path + path_len);
1169
- const wasi = _wasi.get(this);
1170
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1171
- if (validateFstFlagsOrReturn(fst_flags)) {
1172
- return WasiErrno.EINVAL;
1173
- }
1174
- const fs = getFs(this);
1175
- const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1176
- if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
1177
- atim = BigInt(Date.now() * 1000000);
1178
- }
1179
- if ((fst_flags & WasiFstFlag.SET_MTIM_NOW) === WasiFstFlag.SET_MTIM_NOW) {
1180
- mtim = BigInt(Date.now() * 1000000);
1181
- }
1182
- await fs.promises.utimes(resolvedPath, Number(atim), Number(mtim));
1183
- return WasiErrno.ESUCCESS;
1184
- }, ['i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32'], ['i32']);
1185
- defineImport('path_link', function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1186
- old_path = Number(old_path);
1187
- old_path_len = Number(old_path_len);
1188
- new_path = Number(new_path);
1189
- new_path_len = Number(new_path_len);
1190
- if (old_path === 0 || new_path === 0) {
1191
- return WasiErrno.EINVAL;
1192
- }
1193
- const wasi = _wasi.get(this);
1194
- let oldWrap;
1195
- let newWrap;
1196
- if (old_fd === new_fd) {
1197
- oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE | WasiRights.PATH_LINK_TARGET, BigInt(0));
1198
- }
1199
- else {
1200
- oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
1201
- newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
1202
- }
1203
- const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1204
- const fs = getFs(this);
1205
- const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1206
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1207
- fs.linkSync(resolvedOldPath, resolvedNewPath);
1208
- return WasiErrno.ESUCCESS;
1209
- }, async function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1210
- old_path = Number(old_path);
1211
- old_path_len = Number(old_path_len);
1212
- new_path = Number(new_path);
1213
- new_path_len = Number(new_path_len);
1214
- if (old_path === 0 || new_path === 0) {
1215
- return WasiErrno.EINVAL;
1216
- }
1217
- const wasi = _wasi.get(this);
1218
- let oldWrap;
1219
- let newWrap;
1220
- if (old_fd === new_fd) {
1221
- oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE | WasiRights.PATH_LINK_TARGET, BigInt(0));
1222
- }
1223
- else {
1224
- oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
1225
- newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
1226
- }
1227
- const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1228
- const fs = getFs(this);
1229
- const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1230
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1231
- await fs.promises.link(resolvedOldPath, resolvedNewPath);
1232
- return WasiErrno.ESUCCESS;
1233
- }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1234
- function pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags) {
1235
- const read = (fs_rights_base & (WasiRights.FD_READ |
1236
- WasiRights.FD_READDIR)) !== BigInt(0);
1237
- const write = (fs_rights_base & (WasiRights.FD_DATASYNC |
1238
- WasiRights.FD_WRITE |
1239
- WasiRights.FD_ALLOCATE |
1240
- WasiRights.FD_FILESTAT_SET_SIZE)) !== BigInt(0);
1241
- let flags = write ? read ? FileControlFlag.O_RDWR : FileControlFlag.O_WRONLY : FileControlFlag.O_RDONLY;
1242
- let needed_base = WasiRights.PATH_OPEN;
1243
- let needed_inheriting = fs_rights_base | fs_rights_inheriting;
1244
- if ((o_flags & WasiFileControlFlag.O_CREAT) !== 0) {
1245
- flags |= FileControlFlag.O_CREAT;
1246
- needed_base |= WasiRights.PATH_CREATE_FILE;
1247
- }
1248
- if ((o_flags & WasiFileControlFlag.O_DIRECTORY) !== 0) {
1249
- flags |= FileControlFlag.O_DIRECTORY;
1250
- }
1251
- if ((o_flags & WasiFileControlFlag.O_EXCL) !== 0) {
1252
- flags |= FileControlFlag.O_EXCL;
1253
- }
1254
- if ((o_flags & WasiFileControlFlag.O_TRUNC) !== 0) {
1255
- flags |= FileControlFlag.O_TRUNC;
1256
- needed_base |= WasiRights.PATH_FILESTAT_SET_SIZE;
1257
- }
1258
- if ((fs_flags & WasiFdFlag.APPEND) !== 0) {
1259
- flags |= FileControlFlag.O_APPEND;
1260
- }
1261
- if ((fs_flags & WasiFdFlag.DSYNC) !== 0) {
1262
- // flags |= FileControlFlag.O_DSYNC;
1263
- needed_inheriting |= WasiRights.FD_DATASYNC;
1264
- }
1265
- if ((fs_flags & WasiFdFlag.NONBLOCK) !== 0) {
1266
- flags |= FileControlFlag.O_NONBLOCK;
1267
- }
1268
- if ((fs_flags & WasiFdFlag.RSYNC) !== 0) {
1269
- flags |= FileControlFlag.O_SYNC;
1270
- needed_inheriting |= WasiRights.FD_SYNC;
1271
- }
1272
- if ((fs_flags & WasiFdFlag.SYNC) !== 0) {
1273
- flags |= FileControlFlag.O_SYNC;
1274
- needed_inheriting |= WasiRights.FD_SYNC;
1275
- }
1276
- if (write && (flags & (FileControlFlag.O_APPEND | FileControlFlag.O_TRUNC)) === 0) {
1277
- needed_inheriting |= WasiRights.FD_SEEK;
1278
- }
1279
- return { flags, needed_base, needed_inheriting };
1280
- }
1281
- defineImport('path_open', function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
1282
- path = Number(path);
1283
- fd = Number(fd);
1284
- if (path === 0 || fd === 0) {
1285
- return WasiErrno.EINVAL;
1286
- }
1287
- path_len = Number(path_len);
1288
- fs_rights_base = BigInt(fs_rights_base);
1289
- fs_rights_inheriting = BigInt(fs_rights_inheriting);
1290
- const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1291
- const wasi = _wasi.get(this);
1292
- const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1293
- const memory = getMemory(this, Math.max(path + path_len, fd + 4));
1294
- const HEAPU8 = memory.HEAPU8;
1295
- const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1296
- const fs = getFs(this);
1297
- const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
1298
- const r = fs.openSync(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
1299
- const filetype = wasi.fds.getFileTypeByFd(r);
1300
- if ((filetype !== WasiFileType.DIRECTORY) &&
1301
- ((o_flags & WasiFileControlFlag.O_DIRECTORY) !== 0 ||
1302
- (resolved_path.endsWith('/')))) {
1303
- return WasiErrno.ENOTDIR;
1304
- }
1305
- const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flagsRes, filetype);
1306
- const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
1307
- const stat = fs.fstatSync(r, { bigint: true });
1308
- if (stat.isFile()) {
1309
- wrap.size = stat.size;
1310
- if ((flagsRes & FileControlFlag.O_APPEND) !== 0) {
1311
- wrap.pos = stat.size;
1312
- }
1313
- }
1314
- const view = memory.view;
1315
- view.setInt32(fd, wrap.id, true);
1316
- return WasiErrno.ESUCCESS;
1317
- }, async function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
1318
- path = Number(path);
1319
- fd = Number(fd);
1320
- if (path === 0 || fd === 0) {
1321
- return WasiErrno.EINVAL;
1322
- }
1323
- path_len = Number(path_len);
1324
- fs_rights_base = BigInt(fs_rights_base);
1325
- fs_rights_inheriting = BigInt(fs_rights_inheriting);
1326
- const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1327
- const wasi = _wasi.get(this);
1328
- const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1329
- const memory = getMemory(this, Math.max(path + path_len, fd + 4));
1330
- const HEAPU8 = memory.HEAPU8;
1331
- const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1332
- const fs = getFs(this);
1333
- const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
1334
- const r = await fs.promises.open(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
1335
- const filetype = await wasi.fds.getFileTypeByFd(r);
1336
- if ((o_flags & WasiFileControlFlag.O_DIRECTORY) !== 0 && filetype !== WasiFileType.DIRECTORY) {
1337
- return WasiErrno.ENOTDIR;
1338
- }
1339
- const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r.fd, flagsRes, filetype);
1340
- const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
1341
- const stat = await r.stat({ bigint: true });
1342
- if (stat.isFile()) {
1343
- wrap.size = stat.size;
1344
- if ((flagsRes & FileControlFlag.O_APPEND) !== 0) {
1345
- wrap.pos = stat.size;
1346
- }
1347
- }
1348
- const view = memory.view;
1349
- view.setInt32(fd, wrap.id, true);
1350
- return WasiErrno.ESUCCESS;
1351
- }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32', 'i32'], ['i32']);
1352
- defineImport('path_readlink', function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1353
- path = Number(path);
1354
- path_len = Number(path_len);
1355
- buf = Number(buf);
1356
- buf_len = Number(buf_len);
1357
- bufused = Number(bufused);
1358
- if (path === 0 || buf === 0 || bufused === 0) {
1359
- return WasiErrno.EINVAL;
1360
- }
1361
- const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
1362
- const wasi = _wasi.get(this);
1363
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1364
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1365
- pathString = resolve(fileDescriptor.realPath, pathString);
1366
- const fs = getFs(this);
1367
- const link = fs.readlinkSync(pathString);
1368
- const linkData = encoder.encode(link);
1369
- const len = Math.min(linkData.length, buf_len);
1370
- if (len >= buf_len)
1371
- return WasiErrno.ENOBUFS;
1372
- HEAPU8.set(linkData.subarray(0, len), buf);
1373
- HEAPU8[buf + len] = 0;
1374
- view.setUint32(bufused, len, true);
1375
- return WasiErrno.ESUCCESS;
1376
- }, async function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1377
- path = Number(path);
1378
- path_len = Number(path_len);
1379
- buf = Number(buf);
1380
- buf_len = Number(buf_len);
1381
- bufused = Number(bufused);
1382
- if (path === 0 || buf === 0 || bufused === 0) {
1383
- return WasiErrno.EINVAL;
1384
- }
1385
- const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
1386
- const wasi = _wasi.get(this);
1387
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1388
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1389
- pathString = resolve(fileDescriptor.realPath, pathString);
1390
- const fs = getFs(this);
1391
- const link = await fs.promises.readlink(pathString);
1392
- const linkData = encoder.encode(link);
1393
- const len = Math.min(linkData.length, buf_len);
1394
- if (len >= buf_len)
1395
- return WasiErrno.ENOBUFS;
1396
- HEAPU8.set(linkData.subarray(0, len), buf);
1397
- HEAPU8[buf + len] = 0;
1398
- view.setUint32(bufused, len, true);
1399
- return WasiErrno.ESUCCESS;
1400
- }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1401
- defineImport('path_remove_directory', function path_remove_directory(fd, path, path_len) {
1402
- path = Number(path);
1403
- path_len = Number(path_len);
1404
- if (path === 0) {
1405
- return WasiErrno.EINVAL;
1406
- }
1407
- const { HEAPU8 } = getMemory(this, path + path_len);
1408
- const wasi = _wasi.get(this);
1409
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1410
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1411
- pathString = resolve(fileDescriptor.realPath, pathString);
1412
- const fs = getFs(this);
1413
- fs.rmdirSync(pathString);
1414
- return WasiErrno.ESUCCESS;
1415
- }, async function path_remove_directory(fd, path, path_len) {
1416
- path = Number(path);
1417
- path_len = Number(path_len);
1418
- if (path === 0) {
1419
- return WasiErrno.EINVAL;
1420
- }
1421
- const { HEAPU8 } = getMemory(this, path + path_len);
1422
- const wasi = _wasi.get(this);
1423
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1424
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1425
- pathString = resolve(fileDescriptor.realPath, pathString);
1426
- const fs = getFs(this);
1427
- await fs.promises.rmdir(pathString);
1428
- return WasiErrno.ESUCCESS;
1429
- }, ['i32', 'i32', 'i32'], ['i32']);
1430
- defineImport('path_rename', function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
1431
- old_path = Number(old_path);
1432
- old_path_len = Number(old_path_len);
1433
- new_path = Number(new_path);
1434
- new_path_len = Number(new_path_len);
1435
- if (old_path === 0 || new_path === 0) {
1436
- return WasiErrno.EINVAL;
1437
- }
1438
- const wasi = _wasi.get(this);
1439
- let oldWrap;
1440
- let newWrap;
1441
- if (old_fd === new_fd) {
1442
- oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE | WasiRights.PATH_RENAME_TARGET, BigInt(0));
1443
- }
1444
- else {
1445
- oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
1446
- newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1447
- }
1448
- const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1449
- const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1450
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1451
- const fs = getFs(this);
1452
- fs.renameSync(resolvedOldPath, resolvedNewPath);
1453
- return WasiErrno.ESUCCESS;
1454
- }, async function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
1455
- old_path = Number(old_path);
1456
- old_path_len = Number(old_path_len);
1457
- new_path = Number(new_path);
1458
- new_path_len = Number(new_path_len);
1459
- if (old_path === 0 || new_path === 0) {
1460
- return WasiErrno.EINVAL;
1461
- }
1462
- const wasi = _wasi.get(this);
1463
- let oldWrap;
1464
- let newWrap;
1465
- if (old_fd === new_fd) {
1466
- oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE | WasiRights.PATH_RENAME_TARGET, BigInt(0));
1467
- }
1468
- else {
1469
- oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
1470
- newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1471
- }
1472
- const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1473
- const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1474
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1475
- const fs = getFs(this);
1476
- await fs.promises.rename(resolvedOldPath, resolvedNewPath);
1477
- return WasiErrno.ESUCCESS;
1478
- }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1479
- defineImport('path_symlink', function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
1480
- old_path = Number(old_path);
1481
- old_path_len = Number(old_path_len);
1482
- new_path = Number(new_path);
1483
- new_path_len = Number(new_path_len);
1484
- if (old_path === 0 || new_path === 0) {
1485
- return WasiErrno.EINVAL;
1486
- }
1487
- const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1488
- const wasi = _wasi.get(this);
1489
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
1490
- const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
1491
- if (oldPath.length > 0 && oldPath[0] === '/') {
1492
- return WasiErrno.EPERM;
1493
- }
1494
- let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
1495
- newPath = resolve(fileDescriptor.realPath, newPath);
1496
- const fs = getFs(this);
1497
- fs.symlinkSync(oldPath, newPath);
1498
- return WasiErrno.ESUCCESS;
1499
- }, async function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
1500
- old_path = Number(old_path);
1501
- old_path_len = Number(old_path_len);
1502
- new_path = Number(new_path);
1503
- new_path_len = Number(new_path_len);
1504
- if (old_path === 0 || new_path === 0) {
1505
- return WasiErrno.EINVAL;
1506
- }
1507
- const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1508
- const wasi = _wasi.get(this);
1509
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
1510
- const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
1511
- let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
1512
- newPath = resolve(fileDescriptor.realPath, newPath);
1513
- const fs = getFs(this);
1514
- await fs.promises.symlink(oldPath, newPath);
1515
- return WasiErrno.ESUCCESS;
1516
- }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1517
- defineImport('path_unlink_file', function path_unlink_file(fd, path, path_len) {
1518
- path = Number(path);
1519
- path_len = Number(path_len);
1520
- if (path === 0) {
1521
- return WasiErrno.EINVAL;
1522
- }
1523
- const { HEAPU8 } = getMemory(this, path + path_len);
1524
- const wasi = _wasi.get(this);
1525
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1526
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1527
- pathString = resolve(fileDescriptor.realPath, pathString);
1528
- const fs = getFs(this);
1529
- fs.unlinkSync(pathString);
1530
- return WasiErrno.ESUCCESS;
1531
- }, async function path_unlink_file(fd, path, path_len) {
1532
- path = Number(path);
1533
- path_len = Number(path_len);
1534
- if (path === 0) {
1535
- return WasiErrno.EINVAL;
1536
- }
1537
- const { HEAPU8 } = getMemory(this, path + path_len);
1538
- const wasi = _wasi.get(this);
1539
- const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1540
- let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1541
- pathString = resolve(fileDescriptor.realPath, pathString);
1542
- const fs = getFs(this);
1543
- await fs.promises.unlink(pathString);
1544
- return WasiErrno.ESUCCESS;
1545
- }, ['i32', 'i32', 'i32'], ['i32']);
1546
- this._setMemory = function setMemory(m) {
1547
- if (!(m instanceof _WebAssembly.Memory)) {
1548
- throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
1549
- }
1550
- _memory.set(_this, extendMemory(m));
1551
- };
1552
- }
1553
- static createSync(args, env, preopens, stdio, fs, print, printErr) {
1554
- const fds = new SyncTable({
1555
- size: 3,
1556
- in: stdio[0],
1557
- out: stdio[1],
1558
- err: stdio[2],
1559
- fs,
1560
- print,
1561
- printErr
1562
- });
1563
- const _this = new WASI(args, env, fds, false, fs);
1564
- if (preopens.length > 0) {
1565
- for (let i = 0; i < preopens.length; ++i) {
1566
- const realPath = fs.realpathSync(preopens[i].realPath, 'utf8');
1567
- const fd = fs.openSync(realPath, 'r', 0o666);
1568
- fds.insertPreopen(fd, preopens[i].mappedPath, realPath);
1569
- }
1570
- }
1571
- return _this;
1572
- }
1573
- static async createAsync(args, env, preopens, stdio, fs, print, printErr, asyncify) {
1574
- const fds = new AsyncTable({
1575
- size: 3,
1576
- in: stdio[0],
1577
- out: stdio[1],
1578
- err: stdio[2],
1579
- print,
1580
- printErr
1581
- });
1582
- const _this = new WASI(args, env, fds, true, fs, asyncify);
1583
- if (preopens.length > 0) {
1584
- for (let i = 0; i < preopens.length; ++i) {
1585
- const entry = preopens[i];
1586
- const realPath = await fs.promises.realpath(entry.realPath);
1587
- const fd = await fs.promises.open(realPath, 'r', 0o666);
1588
- await fds.insertPreopen(fd, entry.mappedPath, realPath);
1589
- }
1590
- }
1591
- return _this;
1592
- }
1593
- }