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