kimi-plugin-cc 2.7.3 → 3.1.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 (317) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +3 -16
  3. package/README.md +428 -515
  4. package/assets/logo.svg +6 -0
  5. package/cordis.patch.yml +18 -0
  6. package/dist/background-spawn.js +14 -19
  7. package/dist/cli-client.js +163 -105
  8. package/dist/commands/ask.js +44 -96
  9. package/dist/commands/cancel.js +11 -59
  10. package/dist/commands/cli-helpers.js +102 -1
  11. package/dist/commands/models.js +2 -3
  12. package/dist/commands/pursue.js +8 -21
  13. package/dist/commands/registry.js +7 -26
  14. package/dist/commands/repair-sessions.js +121 -12
  15. package/dist/commands/replay.js +4 -3
  16. package/dist/commands/rescue.js +29 -87
  17. package/dist/commands/result.js +7 -1
  18. package/dist/commands/review.js +28 -18
  19. package/dist/commands/setup.js +813 -462
  20. package/dist/commands/status.js +2 -2
  21. package/dist/commands/swarm.js +112 -125
  22. package/dist/commands/worker.js +1 -2
  23. package/dist/companion.js +145 -19
  24. package/dist/git.js +1 -9
  25. package/dist/help.js +13 -9
  26. package/dist/hook-escape.js +7 -16
  27. package/dist/hooks/approval-hook.js +1766 -87
  28. package/dist/hooks/approval-policy.js +86 -19
  29. package/dist/hooks/config-safety.js +31 -64
  30. package/dist/hooks/hook-timing.js +48 -0
  31. package/dist/hooks/install-paths.js +179 -107
  32. package/dist/hooks/install.js +126 -53
  33. package/dist/hooks/managed-block.js +121 -127
  34. package/dist/job-store.js +206 -166
  35. package/dist/jobs.js +66 -19
  36. package/dist/kimi-command.js +74 -1
  37. package/dist/kimi-engine.js +198 -127
  38. package/dist/kimi-errors.js +15 -70
  39. package/dist/kimi-home.js +6 -0
  40. package/dist/kimi-journal.js +42 -0
  41. package/dist/kimi-releases.js +123 -0
  42. package/dist/kimi-timeouts.js +2 -16
  43. package/dist/kimi-version-probe.js +24 -108
  44. package/dist/logging.js +12 -7
  45. package/dist/mcp/binding.js +13 -4
  46. package/dist/mcp/job-control.js +390 -95
  47. package/dist/mcp/launcher.js +13 -7
  48. package/dist/mcp/operations.js +405 -179
  49. package/dist/mcp/protocol.js +124 -33
  50. package/dist/mcp/skills.js +130 -0
  51. package/dist/mcp/stdio.js +18 -4
  52. package/dist/model-command.js +5 -3
  53. package/dist/native-v2-preflight.js +243 -80
  54. package/dist/parsing.js +211 -243
  55. package/dist/paths.js +28 -10
  56. package/dist/plugin-data.js +28 -83
  57. package/dist/process-tree.js +35 -24
  58. package/dist/read-scope.js +435 -0
  59. package/dist/render.js +58 -46
  60. package/dist/rescue-approval.js +461 -84
  61. package/dist/session-references.js +216 -0
  62. package/dist/session-title.js +26 -6
  63. package/dist/stream-json.js +1 -3
  64. package/dist/surface-routing.js +108 -0
  65. package/dist/swarm-activation.js +74 -188
  66. package/dist/swarm-policy.js +2 -1
  67. package/dist/swarm-recovery.js +2 -1
  68. package/dist/util.js +28 -0
  69. package/dist/version.js +1 -1
  70. package/docs/migration.md +45 -206
  71. package/docs/models.md +24 -66
  72. package/docs/safety.md +198 -353
  73. package/docs/session-visibility.md +25 -13
  74. package/docs/swarm-recovery.md +26 -20
  75. package/dsh/index.json +54 -0
  76. package/mcp_config.json +10 -0
  77. package/package.json +23 -82
  78. package/plugin.json +19 -0
  79. package/plugins/kimi-codex/.codex-plugin/plugin.json +7 -7
  80. package/plugins/kimi-codex/.mcp.json +3 -3
  81. package/plugins/kimi-codex/dist/background-spawn.js +14 -19
  82. package/plugins/kimi-codex/dist/cli-client.js +163 -105
  83. package/plugins/kimi-codex/dist/commands/ask.js +44 -96
  84. package/plugins/kimi-codex/dist/commands/cancel.js +11 -59
  85. package/plugins/kimi-codex/dist/commands/cli-helpers.js +102 -1
  86. package/plugins/kimi-codex/dist/commands/models.js +2 -3
  87. package/plugins/kimi-codex/dist/commands/pursue.js +8 -21
  88. package/plugins/kimi-codex/dist/commands/registry.js +7 -26
  89. package/plugins/kimi-codex/dist/commands/repair-sessions.js +121 -12
  90. package/plugins/kimi-codex/dist/commands/replay.js +4 -3
  91. package/plugins/kimi-codex/dist/commands/rescue.js +29 -87
  92. package/plugins/kimi-codex/dist/commands/result.js +7 -1
  93. package/plugins/kimi-codex/dist/commands/review.js +28 -18
  94. package/plugins/kimi-codex/dist/commands/setup.js +813 -462
  95. package/plugins/kimi-codex/dist/commands/status.js +2 -2
  96. package/plugins/kimi-codex/dist/commands/swarm.js +112 -125
  97. package/plugins/kimi-codex/dist/commands/worker.js +1 -2
  98. package/plugins/kimi-codex/dist/companion.js +145 -19
  99. package/plugins/kimi-codex/dist/git.js +1 -9
  100. package/plugins/kimi-codex/dist/help.js +13 -9
  101. package/plugins/kimi-codex/dist/hook-escape.js +7 -16
  102. package/plugins/kimi-codex/dist/hooks/approval-hook.js +1766 -87
  103. package/plugins/kimi-codex/dist/hooks/approval-policy.js +86 -19
  104. package/plugins/kimi-codex/dist/hooks/config-safety.js +31 -64
  105. package/plugins/kimi-codex/dist/hooks/hook-timing.js +48 -0
  106. package/plugins/kimi-codex/dist/hooks/install-paths.js +179 -107
  107. package/plugins/kimi-codex/dist/hooks/install.js +126 -53
  108. package/plugins/kimi-codex/dist/hooks/managed-block.js +121 -127
  109. package/plugins/kimi-codex/dist/job-store.js +206 -166
  110. package/plugins/kimi-codex/dist/jobs.js +66 -19
  111. package/plugins/kimi-codex/dist/kimi-command.js +74 -1
  112. package/plugins/kimi-codex/dist/kimi-engine.js +198 -127
  113. package/plugins/kimi-codex/dist/kimi-errors.js +15 -70
  114. package/plugins/kimi-codex/dist/kimi-home.js +6 -0
  115. package/plugins/kimi-codex/dist/kimi-journal.js +42 -0
  116. package/plugins/kimi-codex/dist/kimi-releases.js +123 -0
  117. package/plugins/kimi-codex/dist/kimi-timeouts.js +2 -16
  118. package/plugins/kimi-codex/dist/kimi-version-probe.js +24 -108
  119. package/plugins/kimi-codex/dist/logging.js +12 -7
  120. package/plugins/kimi-codex/dist/mcp/binding.js +13 -4
  121. package/plugins/kimi-codex/dist/mcp/job-control.js +390 -95
  122. package/plugins/kimi-codex/dist/mcp/launcher.js +13 -7
  123. package/plugins/kimi-codex/dist/mcp/operations.js +405 -179
  124. package/plugins/kimi-codex/dist/mcp/protocol.js +124 -33
  125. package/plugins/kimi-codex/dist/mcp/skills.js +130 -0
  126. package/plugins/kimi-codex/dist/mcp/stdio.js +18 -4
  127. package/plugins/kimi-codex/dist/model-command.js +5 -3
  128. package/plugins/kimi-codex/dist/native-v2-preflight.js +243 -80
  129. package/plugins/kimi-codex/dist/parsing.js +211 -243
  130. package/plugins/kimi-codex/dist/paths.js +28 -10
  131. package/plugins/kimi-codex/dist/plugin-data.js +28 -83
  132. package/plugins/kimi-codex/dist/process-tree.js +35 -24
  133. package/plugins/kimi-codex/dist/read-scope.js +435 -0
  134. package/plugins/kimi-codex/dist/render.js +58 -46
  135. package/plugins/kimi-codex/dist/rescue-approval.js +461 -84
  136. package/plugins/kimi-codex/dist/session-references.js +216 -0
  137. package/plugins/kimi-codex/dist/session-title.js +26 -6
  138. package/plugins/kimi-codex/dist/stream-json.js +1 -3
  139. package/plugins/kimi-codex/dist/surface-routing.js +108 -0
  140. package/plugins/kimi-codex/dist/swarm-activation.js +74 -188
  141. package/plugins/kimi-codex/dist/swarm-policy.js +2 -1
  142. package/plugins/kimi-codex/dist/swarm-recovery.js +2 -1
  143. package/plugins/kimi-codex/dist/util.js +28 -0
  144. package/plugins/kimi-codex/dist/version.js +1 -1
  145. package/plugins/kimi-codex/scripts/companion.sh +4 -15
  146. package/plugins/kimi-codex/skills/kimi/SKILL.md +77 -0
  147. package/plugins/kimi-codex/skills/kimi/agents/openai.yaml +6 -0
  148. package/scripts/companion.sh +4 -15
  149. package/skills/kimi/SKILL.md +77 -0
  150. package/skills/kimi/agents/openai.yaml +6 -0
  151. package/CHANGELOG.md +0 -1333
  152. package/agents/kimi-ask.md +0 -89
  153. package/agents/kimi-challenge.md +0 -86
  154. package/agents/kimi-pursue.md +0 -93
  155. package/agents/kimi-rescue.md +0 -92
  156. package/agents/kimi-review.md +0 -86
  157. package/agents/kimi-swarm-write.md +0 -96
  158. package/agents/kimi-swarm.md +0 -86
  159. package/commands/README.md +0 -16
  160. package/commands/ask.md +0 -38
  161. package/commands/cancel.md +0 -17
  162. package/commands/challenge.md +0 -35
  163. package/commands/pursue.md +0 -41
  164. package/commands/replay.md +0 -13
  165. package/commands/rescue.md +0 -38
  166. package/commands/result.md +0 -13
  167. package/commands/review.md +0 -35
  168. package/commands/setup.md +0 -23
  169. package/commands/status.md +0 -13
  170. package/commands/swarm.md +0 -44
  171. package/dist/commands/not-implemented.js +0 -4
  172. package/dist/commands/review-gate.js +0 -390
  173. package/dist/config.js +0 -22
  174. package/dist/dsh/companion.js +0 -177
  175. package/dist/dsh/index.js +0 -169
  176. package/dist/hooks/review-gate-stop.js +0 -19
  177. package/dist/schemas/review-gate-output.js +0 -43
  178. package/docs/registry-releases.md +0 -228
  179. package/docs/upstream-2.4.1-certification.md +0 -54
  180. package/docs/upstream-2.5.0-certification.md +0 -52
  181. package/docs/upstream-2.5.1-certification.md +0 -47
  182. package/docs/upstream-2.5.2-certification.md +0 -40
  183. package/docs/upstream-2.5.3-certification.md +0 -73
  184. package/docs/upstream-2.6.0-certification.md +0 -65
  185. package/docs/upstream-2.6.2-certification.md +0 -144
  186. package/docs/upstream-2.6.3-certification.md +0 -100
  187. package/docs/upstream-2.7.2-certification.md +0 -113
  188. package/docs/upstream-2.7.3-certification.md +0 -83
  189. package/docs/windows-validation.md +0 -39
  190. package/hooks/README.md +0 -9
  191. package/hooks/hooks.json +0 -16
  192. package/plugins/kimi-codex/dist/commands/not-implemented.js +0 -4
  193. package/plugins/kimi-codex/dist/commands/review-gate.js +0 -390
  194. package/plugins/kimi-codex/dist/config.js +0 -22
  195. package/plugins/kimi-codex/dist/dsh/companion.js +0 -177
  196. package/plugins/kimi-codex/dist/dsh/index.js +0 -169
  197. package/plugins/kimi-codex/dist/hooks/review-gate-stop.js +0 -19
  198. package/plugins/kimi-codex/dist/schemas/review-gate-output.js +0 -43
  199. package/plugins/kimi-codex/scripts/review-gate-hook.sh +0 -32
  200. package/plugins/kimi-codex/skills/kimi-ask/SKILL.md +0 -35
  201. package/plugins/kimi-codex/skills/kimi-ask/agents/openai.yaml +0 -6
  202. package/plugins/kimi-codex/skills/kimi-cancel/SKILL.md +0 -25
  203. package/plugins/kimi-codex/skills/kimi-cancel/agents/openai.yaml +0 -6
  204. package/plugins/kimi-codex/skills/kimi-challenge/SKILL.md +0 -34
  205. package/plugins/kimi-codex/skills/kimi-challenge/agents/openai.yaml +0 -6
  206. package/plugins/kimi-codex/skills/kimi-pursue/SKILL.md +0 -32
  207. package/plugins/kimi-codex/skills/kimi-pursue/agents/openai.yaml +0 -6
  208. package/plugins/kimi-codex/skills/kimi-replay/SKILL.md +0 -25
  209. package/plugins/kimi-codex/skills/kimi-replay/agents/openai.yaml +0 -6
  210. package/plugins/kimi-codex/skills/kimi-rescue/SKILL.md +0 -34
  211. package/plugins/kimi-codex/skills/kimi-rescue/agents/openai.yaml +0 -6
  212. package/plugins/kimi-codex/skills/kimi-result/SKILL.md +0 -25
  213. package/plugins/kimi-codex/skills/kimi-result/agents/openai.yaml +0 -6
  214. package/plugins/kimi-codex/skills/kimi-review/SKILL.md +0 -34
  215. package/plugins/kimi-codex/skills/kimi-review/agents/openai.yaml +0 -6
  216. package/plugins/kimi-codex/skills/kimi-setup/SKILL.md +0 -34
  217. package/plugins/kimi-codex/skills/kimi-setup/agents/openai.yaml +0 -6
  218. package/plugins/kimi-codex/skills/kimi-status/SKILL.md +0 -25
  219. package/plugins/kimi-codex/skills/kimi-status/agents/openai.yaml +0 -6
  220. package/plugins/kimi-codex/skills/kimi-swarm/SKILL.md +0 -35
  221. package/plugins/kimi-codex/skills/kimi-swarm/agents/openai.yaml +0 -6
  222. package/plugins/kimi-codex/skills/kimi-swarm-write/SKILL.md +0 -36
  223. package/plugins/kimi-codex/skills/kimi-swarm-write/agents/openai.yaml +0 -6
  224. package/plugins/kimi-dsh/LICENSE +0 -201
  225. package/plugins/kimi-dsh/README.md +0 -83
  226. package/plugins/kimi-dsh/cordis.patch.yml +0 -45
  227. package/plugins/kimi-dsh/dist/background-spawn.js +0 -216
  228. package/plugins/kimi-dsh/dist/cli-cancellation.js +0 -43
  229. package/plugins/kimi-dsh/dist/cli-client.js +0 -1078
  230. package/plugins/kimi-dsh/dist/commands/ask.js +0 -336
  231. package/plugins/kimi-dsh/dist/commands/cancel.js +0 -216
  232. package/plugins/kimi-dsh/dist/commands/cli-helpers.js +0 -201
  233. package/plugins/kimi-dsh/dist/commands/models.js +0 -140
  234. package/plugins/kimi-dsh/dist/commands/not-implemented.js +0 -4
  235. package/plugins/kimi-dsh/dist/commands/pursue.js +0 -307
  236. package/plugins/kimi-dsh/dist/commands/recover-swarm.js +0 -140
  237. package/plugins/kimi-dsh/dist/commands/registry.js +0 -74
  238. package/plugins/kimi-dsh/dist/commands/repair-sessions.js +0 -160
  239. package/plugins/kimi-dsh/dist/commands/replay.js +0 -124
  240. package/plugins/kimi-dsh/dist/commands/rescue.js +0 -347
  241. package/plugins/kimi-dsh/dist/commands/result.js +0 -66
  242. package/plugins/kimi-dsh/dist/commands/review-gate.js +0 -390
  243. package/plugins/kimi-dsh/dist/commands/review.js +0 -220
  244. package/plugins/kimi-dsh/dist/commands/setup.js +0 -1129
  245. package/plugins/kimi-dsh/dist/commands/status.js +0 -27
  246. package/plugins/kimi-dsh/dist/commands/swarm.js +0 -849
  247. package/plugins/kimi-dsh/dist/commands/worker.js +0 -25
  248. package/plugins/kimi-dsh/dist/companion.js +0 -97
  249. package/plugins/kimi-dsh/dist/config.js +0 -22
  250. package/plugins/kimi-dsh/dist/dsh/companion.js +0 -177
  251. package/plugins/kimi-dsh/dist/dsh/index.js +0 -169
  252. package/plugins/kimi-dsh/dist/errors.js +0 -40
  253. package/plugins/kimi-dsh/dist/git.js +0 -224
  254. package/plugins/kimi-dsh/dist/help.js +0 -43
  255. package/plugins/kimi-dsh/dist/hook-escape.js +0 -71
  256. package/plugins/kimi-dsh/dist/hooks/approval-hook.js +0 -104
  257. package/plugins/kimi-dsh/dist/hooks/approval-policy.js +0 -233
  258. package/plugins/kimi-dsh/dist/hooks/config-safety.js +0 -665
  259. package/plugins/kimi-dsh/dist/hooks/install-paths.js +0 -428
  260. package/plugins/kimi-dsh/dist/hooks/install.js +0 -210
  261. package/plugins/kimi-dsh/dist/hooks/managed-block.js +0 -684
  262. package/plugins/kimi-dsh/dist/hooks/review-gate-stop.js +0 -19
  263. package/plugins/kimi-dsh/dist/job-id.js +0 -12
  264. package/plugins/kimi-dsh/dist/job-store.js +0 -659
  265. package/plugins/kimi-dsh/dist/jobs.js +0 -217
  266. package/plugins/kimi-dsh/dist/kimi-command.js +0 -71
  267. package/plugins/kimi-dsh/dist/kimi-engine.js +0 -675
  268. package/plugins/kimi-dsh/dist/kimi-errors.js +0 -260
  269. package/plugins/kimi-dsh/dist/kimi-home.js +0 -9
  270. package/plugins/kimi-dsh/dist/kimi-timeouts.js +0 -52
  271. package/plugins/kimi-dsh/dist/kimi-version-probe.js +0 -234
  272. package/plugins/kimi-dsh/dist/logging.js +0 -22
  273. package/plugins/kimi-dsh/dist/mcp/binding.js +0 -58
  274. package/plugins/kimi-dsh/dist/mcp/job-control.js +0 -345
  275. package/plugins/kimi-dsh/dist/mcp/launcher.js +0 -257
  276. package/plugins/kimi-dsh/dist/mcp/operations.js +0 -301
  277. package/plugins/kimi-dsh/dist/mcp/protocol.js +0 -275
  278. package/plugins/kimi-dsh/dist/mcp/server.js +0 -8
  279. package/plugins/kimi-dsh/dist/mcp/stdio.js +0 -78
  280. package/plugins/kimi-dsh/dist/mcp/supervised-child.js +0 -15
  281. package/plugins/kimi-dsh/dist/mcp/tool-result.js +0 -7
  282. package/plugins/kimi-dsh/dist/mcp/workspace.js +0 -34
  283. package/plugins/kimi-dsh/dist/model-command.js +0 -32
  284. package/plugins/kimi-dsh/dist/native-v2-preflight.js +0 -525
  285. package/plugins/kimi-dsh/dist/package.json +0 -1
  286. package/plugins/kimi-dsh/dist/parsing.js +0 -620
  287. package/plugins/kimi-dsh/dist/paths.js +0 -27
  288. package/plugins/kimi-dsh/dist/plugin-data.js +0 -98
  289. package/plugins/kimi-dsh/dist/process-tree.js +0 -586
  290. package/plugins/kimi-dsh/dist/prompt-size.js +0 -18
  291. package/plugins/kimi-dsh/dist/render.js +0 -177
  292. package/plugins/kimi-dsh/dist/rescue-approval.js +0 -865
  293. package/plugins/kimi-dsh/dist/schemas/review-gate-output.js +0 -43
  294. package/plugins/kimi-dsh/dist/session-recovery.js +0 -202
  295. package/plugins/kimi-dsh/dist/session-title.js +0 -293
  296. package/plugins/kimi-dsh/dist/stream-json.js +0 -346
  297. package/plugins/kimi-dsh/dist/swarm-activation.js +0 -1197
  298. package/plugins/kimi-dsh/dist/swarm-policy.js +0 -73
  299. package/plugins/kimi-dsh/dist/swarm-recovery.js +0 -165
  300. package/plugins/kimi-dsh/dist/swarm-tool-policy.js +0 -146
  301. package/plugins/kimi-dsh/dist/types.js +0 -1
  302. package/plugins/kimi-dsh/dist/vendor/shell-quote/LICENSE +0 -24
  303. package/plugins/kimi-dsh/dist/vendor/shell-quote/index.js +0 -1
  304. package/plugins/kimi-dsh/dist/vendor/shell-quote/parse.js +0 -229
  305. package/plugins/kimi-dsh/dist/vendor/smol-toml/LICENSE +0 -24
  306. package/plugins/kimi-dsh/dist/vendor/smol-toml/date.js +0 -156
  307. package/plugins/kimi-dsh/dist/vendor/smol-toml/error.js +0 -67
  308. package/plugins/kimi-dsh/dist/vendor/smol-toml/extract.js +0 -293
  309. package/plugins/kimi-dsh/dist/vendor/smol-toml/parse.js +0 -164
  310. package/plugins/kimi-dsh/dist/vendor/smol-toml/primitive.js +0 -151
  311. package/plugins/kimi-dsh/dist/vendor/smol-toml/struct.js +0 -156
  312. package/plugins/kimi-dsh/dist/vendor/smol-toml/util.js +0 -57
  313. package/plugins/kimi-dsh/dist/version.js +0 -7
  314. package/plugins/kimi-dsh/package.json +0 -78
  315. package/plugins/kimi-dsh/scripts/companion.sh +0 -37
  316. package/plugins/kimi-dsh/scripts/review-gate-hook.sh +0 -32
  317. package/scripts/review-gate-hook.sh +0 -32
@@ -1,156 +0,0 @@
1
- /*!
2
- * Copyright (c) Squirrel Chat et al., All rights reserved.
3
- * SPDX-License-Identifier: BSD-3-Clause
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * 1. Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright notice,
11
- * this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. Neither the name of the copyright holder nor the names of its contributors
14
- * may be used to endorse or promote products derived from this software without
15
- * specific prior written permission.
16
- *
17
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
- */
28
- let DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[Tt ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|z|[-+]\d{2}:\d{2})?$/i;
29
- export class TomlDate extends Date {
30
- #hasDate = false;
31
- #hasTime = false;
32
- #offset = null;
33
- constructor(date, fasttype, unsafeDelim) {
34
- let hasDate = true;
35
- let hasTime = true;
36
- let offset = 'Z';
37
- let c;
38
- if (typeof date === 'string') {
39
- if (fasttype)
40
- prep: {
41
- // Date-time
42
- if (fasttype < 3) {
43
- if (+date.slice(11, 13) > 23) {
44
- date = '';
45
- break prep;
46
- }
47
- // Local
48
- if (fasttype === 2) {
49
- offset = null;
50
- date += 'Z';
51
- }
52
- // Offset; keep track of offset if not Z
53
- else if ((c = date.charCodeAt(date.length - 1)) !== 0x5a /* Z */ && c !== 0x7a /* z */) {
54
- offset = date.slice(date.length - 6);
55
- }
56
- if (unsafeDelim)
57
- date = date.slice(0, 10) + 'T' + date.slice(11);
58
- }
59
- // Time
60
- else if (fasttype === 4) {
61
- date = +date.slice(0, 2) > 23 ? '' : `0000-01-01T${date}Z`;
62
- }
63
- hasDate = fasttype !== 4;
64
- hasTime = fasttype !== 3;
65
- }
66
- else {
67
- let match = date.match(DATE_TIME_RE);
68
- if (match) {
69
- if (!match[1]) {
70
- hasDate = false;
71
- date = `0000-01-01T${date}`;
72
- }
73
- hasTime = !!match[2];
74
- // Make sure to use T instead of a space. Breaks in case of extreme values otherwise.
75
- hasTime && date[10] === ' ' && (date = date.replace(' ', 'T'));
76
- // Do not allow rollover hours.
77
- if (match[2] && +match[2] > 23) {
78
- date = '';
79
- }
80
- else {
81
- offset = match[3] || null;
82
- if (!offset && hasTime)
83
- date += 'Z';
84
- }
85
- }
86
- else {
87
- date = '';
88
- }
89
- }
90
- }
91
- super(date);
92
- if (!isNaN(this.getTime())) {
93
- this.#hasDate = hasDate;
94
- this.#hasTime = hasTime;
95
- this.#offset = offset;
96
- }
97
- }
98
- isDateTime() {
99
- return this.#hasDate && this.#hasTime;
100
- }
101
- isLocal() {
102
- return !this.#hasDate || !this.#hasTime || !this.#offset;
103
- }
104
- isDate() {
105
- return this.#hasDate && !this.#hasTime;
106
- }
107
- isTime() {
108
- return this.#hasTime && !this.#hasDate;
109
- }
110
- isValid() {
111
- return this.#hasDate || this.#hasTime;
112
- }
113
- toISOString() {
114
- let iso = super.toISOString();
115
- // Local Date
116
- if (this.isDate())
117
- return iso.slice(0, 10);
118
- // Local Time
119
- if (this.isTime())
120
- return iso.slice(11, 23);
121
- // Local DateTime
122
- if (this.#offset === null)
123
- return iso.slice(0, -1);
124
- // Offset DateTime
125
- if (this.#offset === 'Z' || this.#offset === 'z')
126
- return iso;
127
- // This part is quite annoying: JS strips the original timezone from the ISO string representation
128
- // Instead of using a "modified" date and "Z", we restore the representation "as authored"
129
- let offset = (+(this.#offset.slice(1, 3)) * 60) + +(this.#offset.slice(4, 6));
130
- offset = this.#offset[0] === '-' ? offset : -offset;
131
- let offsetDate = new Date(this.getTime() - (offset * 60e3));
132
- return offsetDate.toISOString().slice(0, -1) + this.#offset;
133
- }
134
- static wrapAsOffsetDateTime(jsDate, offset = 'Z') {
135
- let date = new TomlDate(jsDate);
136
- date.#offset = offset;
137
- return date;
138
- }
139
- static wrapAsLocalDateTime(jsDate) {
140
- let date = new TomlDate(jsDate);
141
- date.#offset = null;
142
- return date;
143
- }
144
- static wrapAsLocalDate(jsDate) {
145
- let date = new TomlDate(jsDate);
146
- date.#hasTime = false;
147
- date.#offset = null;
148
- return date;
149
- }
150
- static wrapAsLocalTime(jsDate) {
151
- let date = new TomlDate(jsDate);
152
- date.#hasDate = false;
153
- date.#offset = null;
154
- return date;
155
- }
156
- }
@@ -1,67 +0,0 @@
1
- /*!
2
- * Copyright (c) Squirrel Chat et al., All rights reserved.
3
- * SPDX-License-Identifier: BSD-3-Clause
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * 1. Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright notice,
11
- * this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. Neither the name of the copyright holder nor the names of its contributors
14
- * may be used to endorse or promote products derived from this software without
15
- * specific prior written permission.
16
- *
17
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
- */
28
- function getLineColFromPtr(string, ptr) {
29
- let lines = string.slice(0, ptr).split(/\r?\n/);
30
- return [lines.length, lines.pop().length + 1];
31
- }
32
- function makeCodeBlock(string, line, column) {
33
- let lines = string.split(/\r?\n/);
34
- let codeblock = '';
35
- let numberLen = (Math.log10(line + 1) | 0) + 1;
36
- for (let i = line - 1; i <= line + 1; i++) {
37
- let l = lines[i - 1];
38
- if (!l)
39
- continue;
40
- codeblock += i.toString().padEnd(numberLen, ' ');
41
- codeblock += ': ';
42
- codeblock += l;
43
- codeblock += '\n';
44
- if (i === line) {
45
- codeblock += ' '.repeat(numberLen + column + 2);
46
- codeblock += '^\n';
47
- }
48
- }
49
- return codeblock;
50
- }
51
- export class TomlError extends Error {
52
- line;
53
- column;
54
- codeblock;
55
- constructor(message, options) {
56
- const [line, column] = getLineColFromPtr(options.toml, options.ptr);
57
- const codeblock = makeCodeBlock(options.toml, line, column);
58
- super(`Invalid TOML document: ${message}\n\n${codeblock}`, options);
59
- this.line = line;
60
- this.column = column;
61
- this.codeblock = codeblock;
62
- }
63
- /** @internal */
64
- static x(message, ctx, ptr) {
65
- throw new TomlError(message, { toml: ctx.s, ptr: ptr ?? ctx.p });
66
- }
67
- }
@@ -1,293 +0,0 @@
1
- /*!
2
- * Copyright (c) Squirrel Chat et al., All rights reserved.
3
- * SPDX-License-Identifier: BSD-3-Clause
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * 1. Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright notice,
11
- * this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. Neither the name of the copyright holder nor the names of its contributors
14
- * may be used to endorse or promote products derived from this software without
15
- * specific prior written permission.
16
- *
17
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
- */
28
- import { parseString } from './primitive.js';
29
- import { parseArray, parseInlineTable } from './struct.js';
30
- import { TomlError } from './error.js';
31
- import { TomlDate } from './date.js';
32
- function isDigit(char, base = 10) {
33
- return base === 16
34
- ? (char > 0x2f && char < 0x3a) || (char > 0x40 && char < 0x47) || (char > 0x60 && char < 0x67)
35
- : (char > 0x2f && char < 0x30 + base);
36
- }
37
- function isEndOfValue(char, delim) {
38
- // Whitespace -- we're permissive on `\r` for performance; it'll be dealt with later anyway
39
- return char === 0x20 || char === 0x9 /* \t */ || char === 0xa /* \n */ || char === 0xd /* \r */ ||
40
- // Structure end or next value delimiter
41
- (delim && (char === delim || char === 0x2c /* , */)) ||
42
- // Comment
43
- char === 0x23; /* # */
44
- }
45
- /** @internal */
46
- export function extractValue(ctx, end) {
47
- let errPtr = ctx.p;
48
- let c = ctx.s.charCodeAt(ctx.p);
49
- // Structs
50
- if (c === 0x5b /* [ */ || c === 0x7b /* { */) {
51
- ctx.d-- || TomlError.x('document contains excessively nested structures. aborting.', ctx);
52
- let value = c === 0x5b /* [ */
53
- ? parseArray(ctx)
54
- : parseInlineTable(ctx);
55
- ctx.d++;
56
- return value;
57
- }
58
- // Strings
59
- if (c === 0x22 /* " */ || c === 0x27 /* ' */) {
60
- return parseString(ctx);
61
- }
62
- // Booleans
63
- // We can fast-path because the first character is enough to know the only possible value
64
- if (c === 0x74 /* t */) { // Only possible valid value is `true`
65
- if (ctx.s.charCodeAt(++ctx.p) !== 0x72 || ctx.s.charCodeAt(++ctx.p) !== 0x75 || ctx.s.charCodeAt(++ctx.p) !== 0x65)
66
- TomlError.x('invalid value', ctx, errPtr);
67
- return ctx.p++, true;
68
- }
69
- if (c === 0x66 /* f */) { // Only possible valid value is `false`
70
- if (ctx.s.charCodeAt(++ctx.p) !== 0x61 || ctx.s.charCodeAt(++ctx.p) !== 0x6c || ctx.s.charCodeAt(++ctx.p) !== 0x73 || ctx.s.charCodeAt(++ctx.p) !== 0x65)
71
- TomlError.x('invalid value', ctx, errPtr);
72
- return ctx.p++, false;
73
- }
74
- if (c === 0x2b /* + */ || c === 0x2d /* - */) {
75
- return parseNumber(ctx, ctx.p, ctx.s.charCodeAt(++ctx.p), 0x2c - c, end);
76
- }
77
- // Rough heuristic, but `parseDate` falls back to number parsing if it's a false positive.
78
- // e.g.: `a = 1\n-------------- = 1` would match this heuristic, but gracefully fallback to numbers.
79
- if (ctx.s.charCodeAt(ctx.p + 4) === 0x2d /* - */ && ctx.s.charCodeAt(ctx.p + 7) === 0x2d /* - */) {
80
- return parseDate(ctx, c, end);
81
- }
82
- // Same logic as above; `parseTime` falls back to number parsing if it's a false positive.
83
- if (ctx.s.charCodeAt(ctx.p + 2) === 0x3a /* : */) {
84
- return parseTime(ctx, c, end);
85
- }
86
- return parseNumber(ctx, ctx.p, c, 0, end);
87
- }
88
- // State:
89
- // 0: init
90
- // 1: integer
91
- // 2: fractional dot
92
- // 3: fractional
93
- // 4: exponent letter
94
- // 5: exponent
95
- // 6: exponent sign
96
- // 7: exponent with sign
97
- // >9: underscore; see below
98
- // -> 12: was integer
99
- // -> 14: was fractional
100
- // -> 16: was exponent
101
- // states 2-5, 14, 16 are ONLY permitted iif base === 10
102
- function parseNumber(ctx, startPtr, startChr, sign, endChr) {
103
- let c = startChr;
104
- let state = 0;
105
- let hasUnderscores = false;
106
- // (+/-)inf
107
- if (c === 0x69 /* i */) {
108
- if (ctx.s.charCodeAt(++ctx.p) !== 0x6e || ctx.s.charCodeAt(++ctx.p) !== 0x66)
109
- TomlError.x('invalid value', ctx, startPtr);
110
- return ctx.p++, (sign || 1) / 0;
111
- }
112
- // (+/-)nan
113
- if (c === 0x6e /* n */) {
114
- if (ctx.s.charCodeAt(++ctx.p) !== 0x61 || ctx.s.charCodeAt(++ctx.p) !== 0x6e)
115
- TomlError.x('invalid value', ctx, startPtr);
116
- return ctx.p++, NaN;
117
- }
118
- // Leading zero
119
- // Only allowed cases: `0<EOV>`, `0.(...)`, `0e(...)`, `0x(...)`, `0b(...)`, `0o(...)`
120
- // FWIW, `0e(...)` is a stupid case, but it's not banned per-se so we have to parse it
121
- if (c === 0x30 /* 0 */) {
122
- if (++ctx.p >= ctx.s.length || isEndOfValue(c = ctx.s.charCodeAt(ctx.p), endChr))
123
- return ctx.bi === true ? 0n : 0; // note: conveniently deals with `-0`
124
- if (!sign) {
125
- if (c === 0x78 /* x */)
126
- return parseIntegerBaseN(ctx, startPtr, 16, endChr);
127
- else if (c === 0x62 /* b */)
128
- return parseIntegerBaseN(ctx, startPtr, 2, endChr);
129
- else if (c === 0x6f /* o */)
130
- return parseIntegerBaseN(ctx, startPtr, 8, endChr);
131
- }
132
- if (c === 0x2e /* . */)
133
- state = 2;
134
- else if (c === 0x65 /* e */ || c === 0x45 /* E */)
135
- state = 4;
136
- else
137
- TomlError.x('illegal leading zero', ctx, startPtr);
138
- }
139
- // If the 1st char is not a digit by now, then it's not a valid TOML value at all
140
- else if (!isDigit(c))
141
- TomlError.x('invalid value', ctx, startPtr);
142
- while (++ctx.p < ctx.s.length && (c = ctx.s.charCodeAt(ctx.p), !isEndOfValue(c, endChr))) {
143
- if (!state)
144
- state = 1; // Detects single-digit numbers we can use a fast parse path for
145
- // The way the states are numbered is not random: underscores are always permitted in odd-numbered states and
146
- // never permitted in even-numbered ones.
147
- if (c === 0x5f /* _ */) {
148
- if (!(state & 1))
149
- TomlError.x('illegal underscore', ctx);
150
- state += 11; // 11 is a marker that makes the state even and greater than 9; see the numbering + rationale above
151
- hasUnderscores = true;
152
- }
153
- // Transition to fractional part.
154
- else if (state === 1 && c === 0x2e /* . */)
155
- state = 2;
156
- // Transition to exponent part.
157
- else if ((state === 1 || state === 3) && (c === 0x65 /* e */ || c === 0x45 /* E */))
158
- state = 4;
159
- // + and - are permitted in state 4 only (handled before entering the function for state 0)
160
- else if (state === 4 && (c === 0x2b /* + */ || c === 0x2d /* - */)) {
161
- state = 6;
162
- }
163
- // All special cases have been handled; only digits are allowed here
164
- else if (!isDigit(c))
165
- TomlError.x(`illegal character in numeric literal`, ctx);
166
- // Clear state flags
167
- else if (state > 9)
168
- state -= 11;
169
- else if (!(state & 1))
170
- state++;
171
- }
172
- // Single-char number; we can fast-path these very easily.
173
- if (!state) {
174
- let val = (startChr - 0x30 /* 0 */) * (sign || 1);
175
- return ctx.bi === true ? BigInt(val) : val;
176
- }
177
- // Even-numbered states absolutely require a digit next; not even end of value is permitted
178
- if (!(state & 1))
179
- TomlError.x('unfinished numeric value', ctx, startPtr);
180
- let str = ctx.s.slice(startPtr, ctx.p);
181
- if (hasUnderscores)
182
- str = str.replaceAll('_', ''); // perf: replaceAll 1.25x faster than replace with a regex
183
- return state > 1
184
- ? parseFloat(str)
185
- : parseInteger(ctx, str, 10, startPtr);
186
- }
187
- function parseIntegerBaseN(ctx, startPtr, base, endChr) {
188
- let c, underscore = 1;
189
- while (++ctx.p < ctx.s.length && (c = ctx.s.charCodeAt(ctx.p), !isEndOfValue(c, endChr))) {
190
- if (c === 0x5f /* _ */) {
191
- if (underscore & 1)
192
- TomlError.x('illegal underscore', ctx);
193
- underscore = 3;
194
- }
195
- // We only need to check if the number is a valid digit, nothing else is permitted
196
- else if (!isDigit(c, base))
197
- TomlError.x(`illegal character in numeric literal`, ctx);
198
- // Clear underscore flag
199
- else if (underscore & 1)
200
- underscore--;
201
- }
202
- // Trailing underscore is not allowed
203
- if (underscore & 1)
204
- TomlError.x('unfinished numeric value', ctx);
205
- let str = ctx.s.slice(startPtr + 2, ctx.p);
206
- if (underscore)
207
- str = str.replaceAll('_', ''); // perf: replaceAll 1.25x faster than replace with a regex
208
- return parseInteger(ctx, str, base, startPtr);
209
- }
210
- function parseInteger(ctx, str, base, startPtr) {
211
- if (ctx.bi !== true)
212
- int: {
213
- let val = parseInt(str, base);
214
- if (!Number.isSafeInteger(val)) {
215
- if (ctx.bi)
216
- break int;
217
- TomlError.x('integer value cannot be represented losslessly', ctx, startPtr);
218
- }
219
- return val;
220
- }
221
- return base === 10 ? BigInt(str) : BigInt((base === 2 ? '0b' : base === 8 ? '0o' : '0x') + str);
222
- }
223
- function parseDate(ctx, c, endChr) {
224
- let startPtr = ctx.p++, unsafeSeparator;
225
- if (!isDigit(c) ||
226
- !isDigit(ctx.s.charCodeAt(ctx.p++)) ||
227
- !isDigit(ctx.s.charCodeAt(ctx.p++)) ||
228
- !isDigit(ctx.s.charCodeAt(ctx.p++))) {
229
- return parseNumber(ctx, ctx.p = startPtr, c, 0, endChr);
230
- }
231
- ctx.p += 5;
232
- if (!isDigit(ctx.s.charCodeAt(ctx.p++)))
233
- TomlError.x('invalid date-time: date part is malformed', ctx, startPtr);
234
- if (ctx.p >= ctx.s.length || (((c = ctx.s.charCodeAt(ctx.p)) !== 0x20 || (unsafeSeparator = true, !isDigit(ctx.s.charCodeAt(ctx.p + 1)))) && c !== 0x54 /* T */ && c !== 0x74 /* t */)) {
235
- let t = ctx.s.slice(startPtr, ctx.p);
236
- return readDate(ctx, t, 3, false, startPtr);
237
- }
238
- if (ctx.s.charCodeAt(ctx.p += 3) !== 0x3a /* : */)
239
- TomlError.x('invalid date-time: time part is malformed', ctx, startPtr);
240
- if (ctx.s.charCodeAt(ctx.p += 3) === 0x3a /* : */)
241
- ctx.p += 3;
242
- if (ctx.s.charCodeAt(ctx.p) === 0x2e /* . */)
243
- while (isDigit(ctx.s.charCodeAt(++ctx.p)))
244
- ;
245
- if (c = ctx.s.charCodeAt(ctx.p)) {
246
- if (c === 0x5a /* Z */ || c === 0x7a /* z */) {
247
- let t = ctx.s.slice(startPtr, ++ctx.p);
248
- return readDate(ctx, t, 1, unsafeSeparator, startPtr, '[+00:00]');
249
- }
250
- if (c === 0x2b /* + */ || c === 0x2d /* - */) {
251
- // Temporal's ZonedDateTime is weird asf when it comes to dealing with traditional offsets...
252
- // It's 1.2x faster to allocate a new string to pass to ZDT than use Instant.toZonedDateTimeISO
253
- let t = ctx.s.slice(startPtr, ctx.p += 6);
254
- return readDate(ctx, t, 1, unsafeSeparator, startPtr, !ctx.ld && ('[' + ctx.s.slice(ctx.p - 6, ctx.p) + ']'));
255
- }
256
- }
257
- let t = ctx.s.slice(startPtr, ctx.p);
258
- return readDate(ctx, t, 2, unsafeSeparator, startPtr);
259
- }
260
- function parseTime(ctx, c, endChr) {
261
- let start = ctx.p;
262
- if (!isDigit(c) || !isDigit(ctx.s.charCodeAt(++ctx.p))) {
263
- return parseNumber(ctx, --ctx.p, c, 0, endChr);
264
- }
265
- if (ctx.s.charCodeAt(ctx.p += 4) === 0x3a /* : */)
266
- ctx.p += 3;
267
- if (ctx.s.charCodeAt(ctx.p) === 0x2e /* . */)
268
- while (isDigit(ctx.s.charCodeAt(++ctx.p)))
269
- ;
270
- let t = ctx.s.slice(start, ctx.p);
271
- return readDate(ctx, t, 4, false, start);
272
- }
273
- function readDate(ctx, str, type, unsafeDelim, errPtr, temporalSuffix) {
274
- if (ctx.ld) {
275
- let date = new TomlDate(str, type, unsafeDelim);
276
- if (!date.isValid())
277
- TomlError.x('invalid date', ctx, errPtr);
278
- return date;
279
- }
280
- try {
281
- if (temporalSuffix)
282
- str += temporalSuffix;
283
- switch (type) {
284
- case 1: return Temporal.ZonedDateTime.from(str);
285
- case 2: return Temporal.PlainDateTime.from(str);
286
- case 3: return Temporal.PlainDate.from(str);
287
- case 4: return Temporal.PlainTime.from(str);
288
- }
289
- }
290
- catch (e) {
291
- TomlError.x(e instanceof Error ? e.message : '' + e, ctx, errPtr);
292
- }
293
- }
@@ -1,164 +0,0 @@
1
- /*!
2
- * Copyright (c) Squirrel Chat et al., All rights reserved.
3
- * SPDX-License-Identifier: BSD-3-Clause
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * 1. Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright notice,
11
- * this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. Neither the name of the copyright holder nor the names of its contributors
14
- * may be used to endorse or promote products derived from this software without
15
- * specific prior written permission.
16
- *
17
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
- */
28
- import { parseKey } from './struct.js';
29
- import { extractValue } from './extract.js';
30
- import { skipVoid } from './util.js';
31
- import { TomlError } from './error.js';
32
- function peekTable(ctx, key, table, meta, type) {
33
- let t = table;
34
- let m = meta;
35
- let k;
36
- let hasOwn = false;
37
- let state;
38
- for (let i = 0; i < key.length; i++) {
39
- if (i) {
40
- t = hasOwn ? t[k] : (t[k] = Object.create(null));
41
- m = (state = m[k]).c;
42
- if (type === 0 /* Type.DOTTED */ && (state.t === 1 /* Type.EXPLICIT */ || state.t === 2 /* Type.ARRAY */)) {
43
- return null;
44
- }
45
- if (state.t === 2 /* Type.ARRAY */) {
46
- let l = t.length - 1;
47
- t = t[l];
48
- m = m[l].c;
49
- }
50
- }
51
- k = key[i];
52
- if ((hasOwn = Object.hasOwn(t, k)) && m[k]?.t === 0 /* Type.DOTTED */ && m[k]?.d) {
53
- return null;
54
- }
55
- if (!hasOwn) {
56
- let unsafe = k === '__proto__';
57
- if (ctx.uk && (unsafe || k === 'constructor'))
58
- return false;
59
- if (unsafe) {
60
- Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
61
- Object.defineProperty(m, k, { enumerable: true, configurable: true, writable: true });
62
- }
63
- m[k] = {
64
- t: i < key.length - 1 && type === 2 /* Type.ARRAY */
65
- ? 3 /* Type.ARRAY_DOTTED */ : type,
66
- d: false,
67
- i: 0,
68
- c: Object.create(null),
69
- };
70
- }
71
- }
72
- state = m[k];
73
- if (state.t !== type && !(type === 1 /* Type.EXPLICIT */ && state.t === 3 /* Type.ARRAY_DOTTED */)) {
74
- // Bad key type!
75
- return null;
76
- }
77
- if (type === 2 /* Type.ARRAY */) {
78
- if (!state.d) {
79
- state.d = true;
80
- t[k] = [];
81
- }
82
- t[k].push(t = Object.create(null));
83
- state.c[state.i++] = (state = { t: 1 /* Type.EXPLICIT */, d: false, i: 0, c: Object.create(null) });
84
- }
85
- if (state.d) {
86
- // Redefining a table!
87
- return null;
88
- }
89
- state.d = true;
90
- if (type === 1 /* Type.EXPLICIT */) {
91
- t = hasOwn ? t[k] : (t[k] = Object.create(null));
92
- }
93
- else if (type === 0 /* Type.DOTTED */ && hasOwn) {
94
- return null;
95
- }
96
- return [k, t, state.c];
97
- }
98
- function validateTablePeek(ctx, peek, ptr) {
99
- if (peek === null || ctx.uk === 2)
100
- TomlError.x(peek === null
101
- ? 'trying to redefine an already defined table or value'
102
- : 'document contains an unsafe property', ctx, ptr);
103
- }
104
- export function parse(toml, options = {}) {
105
- let ctx = {
106
- s: toml,
107
- p: 0,
108
- d: options.maxDepth ?? 1000,
109
- bi: options.integersAsBigInt ?? false,
110
- ld: options.useLegacyDate ?? true,
111
- uk: options.unsafeKeyBehaviour === 'throw' ? 2 : options.unsafeKeyBehaviour === 'drop' ? 1 : 0,
112
- };
113
- let res = Object.create(null);
114
- let meta = Object.create(null);
115
- let tmp;
116
- let skipping = false;
117
- let tbl = res;
118
- let m = meta;
119
- // BOM is allowed, skip.
120
- // JS is UTF-16, so we have to check for the UTF-16 BOM instead of the UTF-8 BOM sequence!
121
- if (toml.charCodeAt(0) === 0xfeff)
122
- ctx.p++;
123
- skipVoid(ctx);
124
- while (ctx.p < toml.length) {
125
- if (toml.charCodeAt(ctx.p) === 0x5b /* [ */) {
126
- let isTableArray = toml.charCodeAt(++ctx.p) === 0x5b; /* [ */
127
- tmp = ctx.p += +isTableArray;
128
- skipping = false;
129
- let k = parseKey(ctx, 0x5d /* ] */);
130
- if (isTableArray) {
131
- if (toml.charCodeAt(ctx.p) !== 0x5d /* ] */) {
132
- TomlError.x('expected end of table array declaration', ctx);
133
- }
134
- ctx.p++;
135
- }
136
- let p = peekTable(ctx, k, res, meta, isTableArray ? 2 /* Type.ARRAY */ : 1 /* Type.EXPLICIT */);
137
- if (!p) {
138
- validateTablePeek(ctx, p, tmp);
139
- skipping = true;
140
- }
141
- else {
142
- m = p[2];
143
- tbl = p[1];
144
- }
145
- }
146
- else {
147
- tmp = ctx.p;
148
- let k = parseKey(ctx);
149
- let p = peekTable(ctx, k, tbl, m, 0 /* Type.DOTTED */);
150
- if (!p && !skipping)
151
- validateTablePeek(ctx, p, tmp);
152
- skipVoid(ctx, true, true);
153
- let v = extractValue(ctx, void 0);
154
- if (p && !skipping)
155
- p[1][p[0]] = v;
156
- }
157
- skipVoid(ctx, true);
158
- if (ctx.p < toml.length && (tmp = toml.charCodeAt(ctx.p)) !== 0xa /* \n */ && (tmp !== 0xd /* \r */ || toml.charCodeAt(ctx.p + 1) !== 0xa /* \n */)) {
159
- TomlError.x('each key-value declaration must be followed by an end-of-line', ctx);
160
- }
161
- skipVoid(ctx);
162
- }
163
- return res;
164
- }