@harperfast/harper 5.3.0-alpha.1 → 5.3.0-beta.1

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 (291) hide show
  1. package/bin/cliOperations.ts +12 -0
  2. package/bin/restart.ts +66 -6
  3. package/components/Application.ts +1134 -112
  4. package/components/OptionsWatcher.ts +368 -102
  5. package/components/Scope.ts +20 -1
  6. package/components/componentLoader.ts +43 -4
  7. package/components/deploymentOperations.ts +4 -1
  8. package/components/deploymentRecorder.ts +9 -2
  9. package/components/operations.js +284 -52
  10. package/components/operationsValidation.js +49 -2
  11. package/components/packageComponent.ts +25 -1
  12. package/components/requestRestart.ts +11 -0
  13. package/config/RootConfigWatcher.ts +191 -37
  14. package/config/configReadRetry.ts +62 -0
  15. package/config/configUtils.ts +78 -26
  16. package/config/parseConfigFile.ts +34 -0
  17. package/config/readConfigFileSync.ts +44 -0
  18. package/config/watcherArming.ts +59 -0
  19. package/config-root.schema.json +4 -0
  20. package/dataLayer/harperBridge/ResourceBridge.ts +28 -2
  21. package/dist/bin/cliOperations.js +13 -0
  22. package/dist/bin/cliOperations.js.map +1 -1
  23. package/dist/bin/restart.js +42 -6
  24. package/dist/bin/restart.js.map +1 -1
  25. package/dist/components/Application.d.ts +104 -9
  26. package/dist/components/Application.js +954 -102
  27. package/dist/components/Application.js.map +1 -1
  28. package/dist/components/OptionsWatcher.d.ts +4 -1
  29. package/dist/components/OptionsWatcher.js +378 -104
  30. package/dist/components/OptionsWatcher.js.map +1 -1
  31. package/dist/components/Scope.js +15 -1
  32. package/dist/components/Scope.js.map +1 -1
  33. package/dist/components/componentLoader.js +35 -3
  34. package/dist/components/componentLoader.js.map +1 -1
  35. package/dist/components/deploymentOperations.js +4 -1
  36. package/dist/components/deploymentOperations.js.map +1 -1
  37. package/dist/components/deploymentRecorder.d.ts +4 -2
  38. package/dist/components/deploymentRecorder.js +1 -0
  39. package/dist/components/deploymentRecorder.js.map +1 -1
  40. package/dist/components/operations.d.ts +28 -0
  41. package/dist/components/operations.js +263 -54
  42. package/dist/components/operations.js.map +1 -1
  43. package/dist/components/operationsValidation.js +48 -2
  44. package/dist/components/operationsValidation.js.map +1 -1
  45. package/dist/components/packageComponent.js +24 -0
  46. package/dist/components/packageComponent.js.map +1 -1
  47. package/dist/components/requestRestart.d.ts +1 -0
  48. package/dist/components/requestRestart.js +7 -0
  49. package/dist/components/requestRestart.js.map +1 -1
  50. package/dist/config/RootConfigWatcher.d.ts +2 -0
  51. package/dist/config/RootConfigWatcher.js +189 -35
  52. package/dist/config/RootConfigWatcher.js.map +1 -1
  53. package/dist/config/configReadRetry.d.ts +8 -0
  54. package/dist/config/configReadRetry.js +62 -0
  55. package/dist/config/configReadRetry.js.map +1 -0
  56. package/dist/config/configUtils.d.ts +10 -9
  57. package/dist/config/configUtils.js +63 -27
  58. package/dist/config/configUtils.js.map +1 -1
  59. package/dist/config/parseConfigFile.d.ts +4 -0
  60. package/dist/config/parseConfigFile.js +35 -0
  61. package/dist/config/parseConfigFile.js.map +1 -0
  62. package/dist/config/readConfigFileSync.d.ts +1 -0
  63. package/dist/config/readConfigFileSync.js +47 -0
  64. package/dist/config/readConfigFileSync.js.map +1 -0
  65. package/dist/config/watcherArming.d.ts +15 -0
  66. package/dist/config/watcherArming.js +59 -0
  67. package/dist/config/watcherArming.js.map +1 -0
  68. package/dist/dataLayer/harperBridge/ResourceBridge.js +21 -2
  69. package/dist/dataLayer/harperBridge/ResourceBridge.js.map +1 -1
  70. package/dist/index.d.ts +1 -0
  71. package/dist/index.js +4 -1
  72. package/dist/index.js.map +1 -1
  73. package/dist/json/systemSchema.json +3 -0
  74. package/dist/resources/DatabaseTransaction.d.ts +25 -0
  75. package/dist/resources/DatabaseTransaction.js +224 -18
  76. package/dist/resources/DatabaseTransaction.js.map +1 -1
  77. package/dist/resources/LMDBTransaction.d.ts +2 -1
  78. package/dist/resources/LMDBTransaction.js +22 -3
  79. package/dist/resources/LMDBTransaction.js.map +1 -1
  80. package/dist/resources/PrimaryRocksDatabase.js +22 -6
  81. package/dist/resources/PrimaryRocksDatabase.js.map +1 -1
  82. package/dist/resources/RecordEncoder.d.ts +1 -1
  83. package/dist/resources/RecordEncoder.js +21 -5
  84. package/dist/resources/RecordEncoder.js.map +1 -1
  85. package/dist/resources/Resource.js +97 -13
  86. package/dist/resources/Resource.js.map +1 -1
  87. package/dist/resources/ResourceInterface.d.ts +8 -0
  88. package/dist/resources/RocksIndexStore.js +2 -1
  89. package/dist/resources/RocksIndexStore.js.map +1 -1
  90. package/dist/resources/RocksTransactionLogStore.d.ts +10 -0
  91. package/dist/resources/RocksTransactionLogStore.js +104 -33
  92. package/dist/resources/RocksTransactionLogStore.js.map +1 -1
  93. package/dist/resources/Table.d.ts +58 -7
  94. package/dist/resources/Table.js +1095 -346
  95. package/dist/resources/Table.js.map +1 -1
  96. package/dist/resources/analytics/write.js +10 -3
  97. package/dist/resources/analytics/write.js.map +1 -1
  98. package/dist/resources/auditStore.d.ts +170 -0
  99. package/dist/resources/auditStore.js +457 -11
  100. package/dist/resources/auditStore.js.map +1 -1
  101. package/dist/resources/dataLoader.js +3 -4
  102. package/dist/resources/dataLoader.js.map +1 -1
  103. package/dist/resources/databases.d.ts +16 -13
  104. package/dist/resources/databases.js +624 -176
  105. package/dist/resources/databases.js.map +1 -1
  106. package/dist/resources/derivedIndexRegistry.d.ts +5 -0
  107. package/dist/resources/derivedIndexRegistry.js +68 -0
  108. package/dist/resources/derivedIndexRegistry.js.map +1 -0
  109. package/dist/resources/derivedIndexRuntime.d.ts +215 -0
  110. package/dist/resources/derivedIndexRuntime.js +2027 -0
  111. package/dist/resources/derivedIndexRuntime.js.map +1 -0
  112. package/dist/resources/graphql.js +3 -2
  113. package/dist/resources/graphql.js.map +1 -1
  114. package/dist/resources/indexes/HierarchicalNavigableSmallWorld.d.ts +102 -9
  115. package/dist/resources/indexes/HierarchicalNavigableSmallWorld.js +848 -39
  116. package/dist/resources/indexes/HierarchicalNavigableSmallWorld.js.map +1 -1
  117. package/dist/resources/indexes/hnswDerivedIndex.d.ts +67 -0
  118. package/dist/resources/indexes/hnswDerivedIndex.js +464 -0
  119. package/dist/resources/indexes/hnswDerivedIndex.js.map +1 -0
  120. package/dist/resources/indexes/hnswPlaneBinding.d.ts +65 -0
  121. package/dist/resources/indexes/hnswPlaneBinding.js +91 -0
  122. package/dist/resources/indexes/hnswPlaneBinding.js.map +1 -0
  123. package/dist/resources/nodeIdMapping.d.ts +5 -0
  124. package/dist/resources/nodeIdMapping.js +49 -0
  125. package/dist/resources/nodeIdMapping.js.map +1 -1
  126. package/dist/resources/recordLock.d.ts +47 -4
  127. package/dist/resources/recordLock.js +138 -7
  128. package/dist/resources/recordLock.js.map +1 -1
  129. package/dist/resources/recordLockCoordinator.d.ts +557 -0
  130. package/dist/resources/recordLockCoordinator.js +2565 -0
  131. package/dist/resources/recordLockCoordinator.js.map +1 -0
  132. package/dist/resources/replayLogs.js +5 -0
  133. package/dist/resources/replayLogs.js.map +1 -1
  134. package/dist/resources/replicatedApplyFailure.d.ts +16 -0
  135. package/dist/resources/replicatedApplyFailure.js +63 -0
  136. package/dist/resources/replicatedApplyFailure.js.map +1 -0
  137. package/dist/resources/scheduler/scheduler.js +3 -3
  138. package/dist/resources/scheduler/scheduler.js.map +1 -1
  139. package/dist/resources/search.d.ts +10 -4
  140. package/dist/resources/search.js +160 -40
  141. package/dist/resources/search.js.map +1 -1
  142. package/dist/resources/tracked.d.ts +5 -1
  143. package/dist/resources/tracked.js +74 -23
  144. package/dist/resources/tracked.js.map +1 -1
  145. package/dist/security/jsLoader.js +6 -4
  146. package/dist/security/jsLoader.js.map +1 -1
  147. package/dist/server/REST.js +33 -2
  148. package/dist/server/REST.js.map +1 -1
  149. package/dist/server/http.d.ts +5 -1
  150. package/dist/server/http.js +34 -2
  151. package/dist/server/http.js.map +1 -1
  152. package/dist/server/serverHelpers/Headers.d.ts +2 -0
  153. package/dist/server/serverHelpers/Headers.js +6 -0
  154. package/dist/server/serverHelpers/Headers.js.map +1 -1
  155. package/dist/server/serverHelpers/NodeAdapterResponse.d.ts +48 -0
  156. package/dist/server/serverHelpers/NodeAdapterResponse.js +220 -0
  157. package/dist/server/serverHelpers/NodeAdapterResponse.js.map +1 -0
  158. package/dist/server/serverHelpers/Request.d.ts +5 -10
  159. package/dist/server/serverHelpers/Request.js +38 -136
  160. package/dist/server/serverHelpers/Request.js.map +1 -1
  161. package/dist/server/serverHelpers/contentTypes.d.ts +2 -0
  162. package/dist/server/serverHelpers/contentTypes.js +189 -15
  163. package/dist/server/serverHelpers/contentTypes.js.map +1 -1
  164. package/dist/server/serverHelpers/serverUtilities.js +96 -17
  165. package/dist/server/serverHelpers/serverUtilities.js.map +1 -1
  166. package/dist/server/storageReclamation.js +1 -1
  167. package/dist/server/storageReclamation.js.map +1 -1
  168. package/dist/server/threads/isolatedApplications.d.ts +47 -0
  169. package/dist/server/threads/isolatedApplications.js +171 -0
  170. package/dist/server/threads/isolatedApplications.js.map +1 -0
  171. package/dist/server/threads/logRotationTransport.d.ts +1 -0
  172. package/dist/server/threads/logRotationTransport.js +33 -0
  173. package/dist/server/threads/logRotationTransport.js.map +1 -0
  174. package/dist/server/threads/manageThreads.d.ts +64 -6
  175. package/dist/server/threads/manageThreads.js +261 -12
  176. package/dist/server/threads/manageThreads.js.map +1 -1
  177. package/dist/server/threads/socketRouter.d.ts +1 -0
  178. package/dist/server/threads/socketRouter.js +196 -13
  179. package/dist/server/threads/socketRouter.js.map +1 -1
  180. package/dist/server/threads/threadServer.js +30 -7
  181. package/dist/server/threads/threadServer.js.map +1 -1
  182. package/dist/utility/errors/hdbError.d.ts +24 -0
  183. package/dist/utility/errors/hdbError.js +58 -1
  184. package/dist/utility/errors/hdbError.js.map +1 -1
  185. package/dist/utility/hdbTerms.d.ts +2 -0
  186. package/dist/utility/hdbTerms.js +2 -0
  187. package/dist/utility/hdbTerms.js.map +1 -1
  188. package/dist/utility/logging/harper_logger.js +217 -38
  189. package/dist/utility/logging/harper_logger.js.map +1 -1
  190. package/dist/utility/logging/logGenerationCoordinator.d.ts +35 -0
  191. package/dist/utility/logging/logGenerationCoordinator.js +184 -0
  192. package/dist/utility/logging/logGenerationCoordinator.js.map +1 -0
  193. package/dist/utility/logging/logRotation.d.ts +46 -0
  194. package/dist/utility/logging/logRotation.js +365 -0
  195. package/dist/utility/logging/logRotation.js.map +1 -0
  196. package/dist/utility/logging/logRotator.d.ts +1 -1
  197. package/dist/utility/logging/logRotator.js +172 -92
  198. package/dist/utility/logging/logRotator.js.map +1 -1
  199. package/dist/utility/npmUtilities.js +6 -4
  200. package/dist/utility/npmUtilities.js.map +1 -1
  201. package/dist/utility/watcherFallback.d.ts +0 -45
  202. package/dist/utility/watcherFallback.js +1 -125
  203. package/dist/utility/watcherFallback.js.map +1 -1
  204. package/dist/validation/configValidator.js +6 -3
  205. package/dist/validation/configValidator.js.map +1 -1
  206. package/index.ts +6 -0
  207. package/json/systemSchema.json +3 -0
  208. package/npm-shrinkwrap.json +131 -41
  209. package/package.json +10 -3
  210. package/resources/DESIGN.md +124 -19
  211. package/resources/DatabaseTransaction.ts +230 -17
  212. package/resources/LMDBTransaction.ts +21 -3
  213. package/resources/PrimaryRocksDatabase.ts +20 -7
  214. package/resources/RecordEncoder.ts +25 -5
  215. package/resources/Resource.ts +97 -13
  216. package/resources/ResourceInterface.ts +8 -0
  217. package/resources/RocksIndexStore.ts +2 -1
  218. package/resources/RocksTransactionLogStore.ts +111 -31
  219. package/resources/Table.ts +1224 -393
  220. package/resources/analytics/write.ts +10 -3
  221. package/resources/auditStore.ts +460 -11
  222. package/resources/dataLoader.ts +3 -4
  223. package/resources/databases.ts +610 -146
  224. package/resources/derivedIndexRegistry.ts +56 -0
  225. package/resources/derivedIndexRuntime.ts +2292 -0
  226. package/resources/graphql.ts +3 -2
  227. package/resources/indexes/HierarchicalNavigableSmallWorld.ts +905 -46
  228. package/resources/indexes/hnswDerivedIndex.ts +531 -0
  229. package/resources/indexes/hnswPlaneBinding.ts +174 -0
  230. package/resources/nodeIdMapping.ts +50 -0
  231. package/resources/recordLock.ts +173 -7
  232. package/resources/recordLockCoordinator.ts +3043 -0
  233. package/resources/replayLogs.ts +5 -0
  234. package/resources/replicatedApplyFailure.ts +77 -0
  235. package/resources/scheduler/scheduler.ts +4 -4
  236. package/resources/search.ts +169 -49
  237. package/resources/tracked.ts +73 -22
  238. package/security/jsLoader.ts +6 -4
  239. package/server/DESIGN.md +11 -0
  240. package/server/REST.ts +36 -3
  241. package/server/http.ts +34 -2
  242. package/server/serverHelpers/Headers.ts +5 -1
  243. package/server/serverHelpers/NodeAdapterResponse.ts +221 -0
  244. package/server/serverHelpers/Request.ts +33 -131
  245. package/server/serverHelpers/contentTypes.ts +188 -15
  246. package/server/serverHelpers/serverUtilities.ts +143 -24
  247. package/server/storageReclamation.ts +2 -2
  248. package/server/threads/isolatedApplications.ts +157 -0
  249. package/server/threads/logRotationTransport.ts +40 -0
  250. package/server/threads/manageThreads.js +254 -12
  251. package/server/threads/socketRouter.ts +217 -11
  252. package/server/threads/threadServer.js +30 -7
  253. package/studio/web/assets/{Chat-BnCBegQz.js → Chat-D3j-1yY1.js} +1 -1
  254. package/studio/web/assets/{FloatingChat-CoDW1ySS.js → FloatingChat-BxJGYcfB.js} +3 -3
  255. package/studio/web/assets/{apiToken-Bwk5BLXW.js → apiToken-CT55oWOe.js} +1 -1
  256. package/studio/web/assets/{applications-DHxGi7JH.js → applications-D9Ct9_vm.js} +1 -1
  257. package/studio/web/assets/{cssMode-s0cWI-_M.js → cssMode-DV8H7VwA.js} +1 -1
  258. package/studio/web/assets/{editor-DNcRHK54.js → editor-uatc0unt.js} +1 -1
  259. package/studio/web/assets/{html-Bdssedlg.js → html-Bm6D6paN.js} +1 -1
  260. package/studio/web/assets/{htmlMode-CoDlJ3fw.js → htmlMode-CEn7tpLG.js} +1 -1
  261. package/studio/web/assets/{index-D6sxmFLR.js → index-BIXW6Pu4.js} +5 -5
  262. package/studio/web/assets/{index.lazy-tmU5BS8s.js → index.lazy-UI7L-Vrk.js} +1 -1
  263. package/studio/web/assets/{javascript-B8meVSTH.js → javascript-CJ0G3AFZ.js} +1 -1
  264. package/studio/web/assets/{jsonMode-DpIPd35T.js → jsonMode-DQADAYEa.js} +1 -1
  265. package/studio/web/assets/{languageServices-C_5FMJzQ.js → languageServices-CAQJXWcI.js} +1 -1
  266. package/studio/web/assets/{lspLanguageFeatures-BIzNBzPK.js → lspLanguageFeatures-CCQ8P5sY.js} +1 -1
  267. package/studio/web/assets/{notifications-CQf18QKb.js → notifications-BbxTU6Aw.js} +1 -1
  268. package/studio/web/assets/{notifications-CvZivSbh.js → notifications-Cvb3P1lB.js} +1 -1
  269. package/studio/web/assets/{profile-DdOwtntb.js → profile-Yyb7gsvL.js} +1 -1
  270. package/studio/web/assets/{regions-n69fwagr.js → regions-OgjGHlU5.js} +1 -1
  271. package/studio/web/assets/{register-PfWTCXWB.js → register-6qwNEOY3.js} +2 -2
  272. package/studio/web/assets/{setComponentFile-Bg6O7X0S.js → setComponentFile-BilDMtgB.js} +1 -1
  273. package/studio/web/assets/{setup-CUx_aUDl.js → setup-J6qJ7OIU.js} +2 -2
  274. package/studio/web/assets/{status-D7BVKqX9.js → status-0RWGcfyD.js} +1 -1
  275. package/studio/web/assets/{toggleHighContrast-DBSyXzMr.js → toggleHighContrast-BIn-vErT.js} +1 -1
  276. package/studio/web/assets/{tsMode-BByKCjBS.js → tsMode-DgUXku4d.js} +1 -1
  277. package/studio/web/assets/{typescript-DDLnLpw9.js → typescript-C9orXcsM.js} +1 -1
  278. package/studio/web/assets/{useEntityRestURL-31CHGaHk.js → useEntityRestURL-BEoXXbUB.js} +1 -1
  279. package/studio/web/assets/{workers-pR3jRY9D.js → workers-JVzSDmgx.js} +1 -1
  280. package/studio/web/assets/{xml-2iRnMhQO.js → xml-Cq-S8S4X.js} +1 -1
  281. package/studio/web/assets/{yaml-Bf92gJpd.js → yaml-sfoRdh1M.js} +1 -1
  282. package/studio/web/index.html +1 -1
  283. package/utility/errors/hdbError.ts +54 -0
  284. package/utility/hdbTerms.ts +2 -0
  285. package/utility/logging/harper_logger.ts +209 -30
  286. package/utility/logging/logGenerationCoordinator.ts +196 -0
  287. package/utility/logging/logRotation.ts +367 -0
  288. package/utility/logging/logRotator.ts +196 -91
  289. package/utility/npmUtilities.ts +6 -4
  290. package/utility/watcherFallback.ts +0 -122
  291. package/validation/configValidator.ts +6 -3
@@ -1,20 +1,19 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.OptionsWatcher = exports.CannotSetPropertyError = exports.KeyDoesNotExistError = exports.InvalidValueTypeError = exports.InvariantUninitializedOptionsWatcherError = exports.UninitializedOptionsWatcherError = exports.OptionsWatcherConfigFileError = void 0;
7
4
  const harper_logger_ts_1 = require("../utility/logging/harper_logger.js");
8
5
  const events_1 = require("events");
9
- const yaml_1 = __importDefault(require("yaml"));
10
6
  const promises_1 = require("node:fs/promises");
11
- const node_fs_1 = require("node:fs");
12
7
  const util_1 = require("util");
13
8
  const DEFAULT_CONFIG_ts_1 = require("./DEFAULT_CONFIG.js");
14
9
  const lodash_1 = require("lodash");
15
10
  const watcherFallback_ts_1 = require("../utility/watcherFallback.js");
16
11
  const watchPath_ts_1 = require("../utility/watchPath.js");
17
12
  const harperConfigEnvVars_ts_1 = require("../config/harperConfigEnvVars.js");
13
+ const readConfigFileSync_ts_1 = require("../config/readConfigFileSync.js");
14
+ const parseConfigFile_ts_1 = require("../config/parseConfigFile.js");
15
+ const configReadRetry_ts_1 = require("../config/configReadRetry.js");
16
+ const watcherArming_ts_1 = require("../config/watcherArming.js");
18
17
  // This is uniquely for errors coming from the chokidar watcher of the config file.
19
18
  class OptionsWatcherConfigFileError extends Error {
20
19
  constructor(configFilePath, error) {
@@ -74,13 +73,22 @@ class OptionsWatcher extends events_1.EventEmitter {
74
73
  #scopedConfig;
75
74
  #rootConfig;
76
75
  #isRootConfig;
76
+ #synchronousRead;
77
+ #readRetry = new configReadRetry_ts_1.ConfigReadRetry();
78
+ #armGate = new watcherArming_ts_1.ArmGate();
77
79
  #name;
78
80
  #logger;
79
81
  #usingPolling;
80
82
  #closed;
81
83
  #openCount = 0;
84
+ #readCount = 0;
85
+ #readSequence = 0;
86
+ #appliedSequence = 0;
87
+ #scopeConfigured = false;
88
+ #armAbsence;
89
+ #readyEmitted = false;
90
+ #envComposeError;
82
91
  #pendingReads = new Set();
83
- #partialRead;
84
92
  ready;
85
93
  constructor(name, filePath, logger, isRootConfig) {
86
94
  super();
@@ -88,11 +96,12 @@ class OptionsWatcher extends events_1.EventEmitter {
88
96
  this.#filePath = filePath;
89
97
  const watchTarget = (0, watchPath_ts_1.resolveWatchTarget)(filePath);
90
98
  this.#watchPath = watchTarget.path;
91
- this.#partialRead = new watcherFallback_ts_1.PartialReadRetry(filePath);
92
99
  // Root-config watchers must see runtime env config (HARPER_SET_CONFIG et al.)
93
100
  // even when it hasn't been flushed to disk yet — see #handleChange (#1618).
94
101
  // Application scopes watch their own config.yaml and are never overlaid.
95
- this.#isRootConfig = isRootConfig ?? (0, harperConfigEnvVars_ts_1.isRootConfigFilename)(filePath);
102
+ const rootConfigFile = (0, harperConfigEnvVars_ts_1.isRootConfigFilename)(filePath);
103
+ this.#isRootConfig = isRootConfig ?? rootConfigFile;
104
+ this.#synchronousRead = this.#isRootConfig;
96
105
  this.#logger = logger || (0, harper_logger_ts_1.loggerWithTag)(name);
97
106
  this.#usingPolling = watchTarget.mustPoll;
98
107
  this.#closed = false;
@@ -100,7 +109,7 @@ class OptionsWatcher extends events_1.EventEmitter {
100
109
  this.#openWatcher();
101
110
  }
102
111
  #openWatcher() {
103
- this.#openCount++;
112
+ const generation = ++this.#openCount;
104
113
  this.#watcher = (0, watcherFallback_ts_1.guardedWatch)(this.#watchPath, {
105
114
  persistent: false,
106
115
  ...(this.#usingPolling ? watcherFallback_ts_1.POLLING_FALLBACK_OPTIONS : {}),
@@ -109,67 +118,124 @@ class OptionsWatcher extends events_1.EventEmitter {
109
118
  .on('change', this.#handleChange.bind(this))
110
119
  .on('error', this.#handleError.bind(this))
111
120
  .on('unlink', this.#handleUnlink.bind(this))
112
- .on('ready', this.#handleChange.bind(this));
121
+ // Generation-bound: `#armGate.reset()` runs before the failed watcher is closed, so a
122
+ // `ready` still queued on it would arm the gate on the replacement's behalf and the
123
+ // replacement's own `ready` would then be a no-op — leaving its scan window unre-read.
124
+ .on('ready', () => this.#handleArmed(generation));
125
+ }
126
+ // Every root-declared plugin gets its own root-config watcher and each reads synchronously, so
127
+ // each has the unarmed window DESIGN.md's "`ready` means the watcher is armed" describes. The
128
+ // write that lands in it is reported by no event, so only this re-read can deliver it.
129
+ #handleArmed(generation) {
130
+ if (this.#closed || generation !== this.#openCount)
131
+ return;
132
+ this.#armGate.arm(() => this.#read(true, true));
113
133
  }
114
- // Root config only: see the descriptor-lifetime invariant on atomicWriteFile (DESIGN.md).
115
134
  #handleChange() {
116
- if (this.#isRootConfig) {
117
- this.#applyRead(() => (0, node_fs_1.readFileSync)(this.#filePath, 'utf-8'));
135
+ this.#read(true);
136
+ }
137
+ // While `ready` is outstanding the ladder is the only thing that can settle it, so its timer has
138
+ // to keep the thread alive; afterwards it must not, or a config file nobody is reading would
139
+ // hold a worker open.
140
+ #schedule() {
141
+ return this.#readRetry.schedule(() => this.#read(false), !this.#readyEmitted);
142
+ }
143
+ #read(waitForLock, arming = false) {
144
+ // A queued chokidar callback can still land after close(), and by then removeAllListeners()
145
+ // has run — emitting into an EventEmitter with no 'error' listener would throw out of it.
146
+ if (this.#closed)
147
+ return;
148
+ this.#readCount++;
149
+ if (this.#synchronousRead) {
150
+ try {
151
+ let contents;
152
+ try {
153
+ contents = (0, readConfigFileSync_ts_1.readConfigFileSync)(this.#filePath, waitForLock);
154
+ }
155
+ catch (error) {
156
+ this.#handleReadError(error, arming);
157
+ return;
158
+ }
159
+ this.#applyContents(contents);
160
+ }
161
+ catch (error) {
162
+ // A listener of what `#applyContents` emitted may have closed the watcher, after
163
+ // which `emit('error')` has no listener left and would throw out of here.
164
+ if (!this.#closed)
165
+ this.#surfaceFailure(error);
166
+ }
118
167
  return;
119
168
  }
169
+ // The `#closed` check above cannot cover the completion: close() may land while this read is
170
+ // in flight, and by then its retry state is cancelled and its listeners are gone. Nothing
171
+ // orders these against each other either — chokidar does not await one read before starting
172
+ // the next — so an older one completing last would put the file's previous contents back
173
+ // with no event left to correct it.
174
+ const sequence = ++this.#readSequence;
175
+ // `<=`, not `<`: a deletion supersedes the reads already in flight without being a read of
176
+ // its own, and says so by claiming the sequence they were issued under.
177
+ const outranked = () => this.#closed || sequence <= this.#appliedSequence;
120
178
  const read = (0, promises_1.readFile)(this.#filePath, 'utf-8')
121
- .then((contents) => this.#applyRead(() => contents))
122
- .catch((error) => this.#recoverOrReport(error))
179
+ .then((contents) => {
180
+ if (outranked())
181
+ return;
182
+ this.#appliedSequence = sequence;
183
+ this.#applyContents(contents);
184
+ }, (error) => {
185
+ if (outranked())
186
+ return;
187
+ this.#appliedSequence = sequence;
188
+ this.#handleReadError(error, arming);
189
+ })
190
+ .catch((error) => {
191
+ if (!this.#closed)
192
+ this.#surfaceFailure(error);
193
+ })
123
194
  .finally(() => {
124
195
  this.#pendingReads.delete(read);
125
196
  });
126
197
  this.#pendingReads.add(read);
127
198
  }
128
- #applyRead(read) {
199
+ #applyContents(contents) {
200
+ // An empty read is a writer's truncate window, not an emptied config, and falling through
201
+ // would emit `remove` — see DESIGN.md, "An empty read is a writer mid-write, not an empty
202
+ // config". Both read paths can land in that window; only the synchronous one is likely to.
203
+ if (!contents) {
204
+ if (this.#schedule())
205
+ return;
206
+ // Past the ladder the file is empty rather than mid-write, and an empty file carries no
207
+ // scope: keep what is already applied instead of reading it as a removal, and settle the
208
+ // boot barrier on the defaults rather than leaving `Scope.ready` pending forever.
209
+ this.#logger.warn?.(`Configuration file ${this.#filePath} is empty.`);
210
+ this.#settleUnconfigured();
211
+ return;
212
+ }
129
213
  let parsed;
130
214
  try {
131
- parsed = yaml_1.default.parse(read());
215
+ parsed = (0, parseConfigFile_ts_1.parseConfigFile)(contents, this.#filePath);
132
216
  }
133
217
  catch (error) {
134
- // A read or parse that fails while the file is being replaced is the same event as an
135
- // incomplete one, and #handleReadError's ENOENT arm would answer it with a `remove`
136
- // that restarts the scope. Re-read first; only an exhausted budget means it is real.
137
- this.#recoverOrReport(error);
138
- return;
218
+ // A prefix of the document is as much a mid-write read as an empty one, and the event
219
+ // carrying the rest is the one chokidar throttles away — same ladder, same reason. Past it
220
+ // the file really is malformed, which `#read` routes to `#surfaceFailure`.
221
+ if (!this.#closed && this.#schedule())
222
+ return;
223
+ throw error;
139
224
  }
140
- // Tested on the file's own parse, before any env overlay: `''`, `'\n'` and a truncated
141
- // document all parse to null, and an env-configured deployment would otherwise overlay
142
- // one into a valid-looking object and adopt it. A file that is still unusable once the
143
- // budget is spent is taken at face value, so emptying one still reaches `remove`.
225
+ // Judged on the file's own parse, before the overlay below: a truncated document, a lone
226
+ // `\n` and a file of nothing but comments all parse to `null` rather than throwing, and
227
+ // `overlayRootEnvConfig` turns any of them into a non-null object whenever a config env var
228
+ // is set — the norm in containers — so overlaying first would launder a half-written file
229
+ // into a valid-looking env-only config and wipe the file's own options. Past the ladder it
230
+ // is an empty file, which `#applyContents` already keeps rather than reads as a removal.
144
231
  if (!parsed || typeof parsed !== 'object') {
145
- if (this.#partialRead.schedule(() => this.#handleChange()))
232
+ if (this.#schedule())
146
233
  return;
147
- this.#partialRead.gaveUp();
148
- }
149
- else {
150
- this.#partialRead.settled();
151
- }
152
- try {
153
- this.#applyParsed(this.#overlayEnvConfig(parsed));
154
- }
155
- catch (error) {
156
- // Applying is past the point where an incomplete file could explain a failure, so a
157
- // listener's throw keeps the error route rather than being retried.
158
- this.emit('error', error);
159
- }
160
- }
161
- #recoverOrReport(error) {
162
- if (!(0, watcherFallback_ts_1.isPartialReadError)(error))
163
- return this.#handleReadError(error);
164
- if (this.#partialRead.schedule(() => this.#handleChange()))
234
+ this.#logger.warn?.(`Configuration file ${this.#filePath} is empty.`);
235
+ this.#settleUnconfigured();
165
236
  return;
166
- // Same give-up as the unusable-parse case, so the budget is restored for the repair: the
167
- // write that fixes the file can itself be read mid-write. The error still takes the
168
- // scope's own route.
169
- this.#partialRead.gaveUp(error);
170
- this.#handleReadError(error);
171
- }
172
- #overlayEnvConfig(parsed) {
237
+ }
238
+ this.#readRetry.reset();
173
239
  // The on-disk root config is not guaranteed to include runtime env config at
174
240
  // boot: the file flush races component loading, so a scope's boot-time reads
175
241
  // (e.g. an `enabled` gate in handleApplication) could observe pre-env values
@@ -177,36 +243,46 @@ class OptionsWatcher extends events_1.EventEmitter {
177
243
  // config onto EVERY root-config read so scope.options matches the resolved
178
244
  // view (#1618). Non-root scopes and the no-env-vars case are untouched
179
245
  // (overlayRootEnvConfig is a no-op there).
180
- return this.#isRootConfig ? (0, harperConfigEnvVars_ts_1.overlayRootEnvConfig)(parsed) : parsed;
181
- }
182
- #applyParsed(parsed) {
246
+ if (this.#isRootConfig) {
247
+ try {
248
+ parsed = (0, harperConfigEnvVars_ts_1.overlayRootEnvConfig)(parsed);
249
+ }
250
+ catch (error) {
251
+ this.#envComposeError = error;
252
+ if (this.#readyEmitted)
253
+ this.#reportEnvComposeFailure();
254
+ else
255
+ this.#settleUnconfigured();
256
+ return;
257
+ }
258
+ }
183
259
  this.#rootConfig = parsed && typeof parsed === 'object' ? parsed : undefined;
184
260
  // If the extension is in the config file
185
261
  if (this.#rootConfig && this.#name in this.#rootConfig) {
186
- // If a config object does not exist
187
- if (!this.#scopedConfig) {
188
- // set it
189
- this.#scopedConfig = this.#rootConfig[this.#name];
190
- // and emit a ready event
191
- this.emit('ready', this.#scopedConfig);
192
- }
193
- else {
194
- // Otherwise, merge the new config with the old config
195
- this.#merge(this.#rootConfig[this.#name], this.#scopedConfig);
196
- }
262
+ this.#applyScopedConfig(this.#rootConfig[this.#name]);
197
263
  }
198
264
  else {
199
265
  // Otherwise, if the extension is not in the config file
200
266
  // This means the plugin was removed from the config file
201
- if (this.#scopedConfig) {
202
- // and a config exists, remove it
267
+ // Presence, not truthiness: `myPlugin:` with nothing under it is a configured scope, and
268
+ // deleting that block has to reach `Scope` as a removal like any other.
269
+ if (this.#scopeConfigured) {
270
+ this.#scopeConfigured = false;
203
271
  this.#scopedConfig = undefined;
204
- this.emit('remove');
272
+ this.#emitRemove();
273
+ }
274
+ // The scope may be added back later, but this read is still a terminal outcome: with
275
+ // nothing ever applied, neither branch above emits, and `Scope.ready` would stay pending
276
+ // for a config that read perfectly well. The read succeeded, so only the scope falls back
277
+ // to its default — `#settleUnconfigured`'s full reset would discard the root config this
278
+ // very read produced.
279
+ if (!this.#readyEmitted) {
280
+ this.#scopedConfig = (0, lodash_1.cloneDeep)(DEFAULT_CONFIG_ts_1.DEFAULT_CONFIG[this.#name]);
281
+ this.#emitReady(this.#scopedConfig);
205
282
  }
206
- // Otherwise do nothing - the user may add the config back in later
207
283
  }
208
284
  }
209
- #handleReadError(error) {
285
+ #handleReadError(error, arming = false) {
210
286
  // If the config file does not exist
211
287
  if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
212
288
  // A readFile ENOENT here is the install window (file not written yet) or a
@@ -216,23 +292,114 @@ class OptionsWatcher extends events_1.EventEmitter {
216
292
  // through to the original ENOENT handling with #rootConfig untouched, so a
217
293
  // first boot still emits `ready` (not `remove`, which nothing consumes at
218
294
  // boot → `ready` would hang forever).
295
+ // A ladder armed by an earlier empty read is spent here, and every other terminal path
296
+ // clears its deadline; leaving it armed costs the next mid-write read its whole budget.
297
+ this.#readRetry.reset();
219
298
  if (this.#applyEnvOnlyConfig())
220
299
  return;
221
300
  // And a config already exists, reset it to the default
222
301
  if (this.#rootConfig) {
302
+ // The arm gate's job is the *write* that landed while the watch was unarmed, and
303
+ // answering its ENOENT with a removal reports the deletion ahead of the `unlink`
304
+ // that would confirm it — on a platform with an arming grace, ahead of chokidar
305
+ // having finished its own teardown, so a config recreated on the strength of that
306
+ // early `remove` lands where its `add` is not observed at all. It cannot simply be
307
+ // dropped either: the unarmed window is exactly where an `unlink` can go missing.
308
+ // So it goes back to the loop once, and chokidar's own `unlink` cancels it.
309
+ if (arming) {
310
+ // Every resolution of the deferral recomposes and reports the env state it finds,
311
+ // so a failure held across it is one that may no longer be true — and the next
312
+ // path to report would attach it to an unrelated event.
313
+ this.#envComposeError = undefined;
314
+ this.#deferAbsenceCheck();
315
+ return;
316
+ }
223
317
  this.#resetConfig();
224
- this.emit('remove');
318
+ this.#emitRemove();
225
319
  }
226
320
  else {
227
321
  // Otherwise, if no config exists, then just set to default and emit ready
228
322
  this.#resetConfig();
229
- this.emit('ready');
323
+ this.#emitReady();
230
324
  }
325
+ this.#reportEnvComposeFailure();
326
+ return;
327
+ }
328
+ // A failure that outlives the read emits no new watcher event when it clears, so without
329
+ // this the scope would hold a stale config until the next write. Both read paths: an
330
+ // application config on SMB or under an editor's replacement returns a transient
331
+ // EBUSY/EIO just the same, and only the read for it blocks — the ladder never does.
332
+ if (!this.#closed && this.#schedule())
333
+ return;
334
+ this.#surfaceFailure(error);
335
+ }
336
+ // A failure before anything has been applied is the boot window, where `error` alone strands the
337
+ // component: `Scope` logs it and componentLoader waits on `Scope.ready` with no timeout. Fall
338
+ // back to the defaults exactly as the ENOENT branch above does — and emit `ready` first, since
339
+ // `error` settles the `once(..., 'ready')` promises both of them await.
340
+ #surfaceFailure(error) {
341
+ this.#settleUnconfigured();
342
+ this.#emitError(error);
343
+ }
344
+ // Settling the barrier removes the `error` listener `once(this, 'ready')` attached, and an emit
345
+ // with none left throws the error back at the caller — here, chokidar's dispatch or a retry
346
+ // timer, where nothing can absorb it. A consumer that throws is no different.
347
+ #emitError(error) {
348
+ if (this.listenerCount('error') === 0) {
349
+ this.#logger.error?.(`The configuration for '${this.#name}' at ${this.#filePath} could not be read`, error);
350
+ return;
351
+ }
352
+ try {
353
+ this.emit('error', error);
354
+ }
355
+ catch (listenerError) {
356
+ this.#logger.error?.('A configuration error listener failed', listenerError);
357
+ }
358
+ }
359
+ #emitRemove() {
360
+ try {
361
+ this.emit('remove');
362
+ }
363
+ catch (error) {
364
+ this.#logger.error?.('A configuration removal listener failed', error);
365
+ }
366
+ }
367
+ // `#readyEmitted`, not the config's truthiness: a scope absent from a config that read fine
368
+ // leaves `#rootConfig` set with no `ready` behind it, and a later failure would return here
369
+ // with `Scope.ready` still pending.
370
+ #settleUnconfigured() {
371
+ if (this.#readyEmitted)
231
372
  return;
373
+ // A file the watcher cannot use does not unset env config, exactly as on the ENOENT path
374
+ // (#1618) — but the barrier still has to settle, including when composing that env config is
375
+ // itself what failed.
376
+ if (this.#applyEnvOnlyConfig()) {
377
+ if (!this.#readyEmitted)
378
+ this.#emitReady(this.#scopedConfig);
379
+ }
380
+ else {
381
+ this.#resetConfig();
382
+ this.#emitReady(this.#scopedConfig);
383
+ }
384
+ // Also on the env-only branch: the caller may be `#handleChange`'s merge-shape failure,
385
+ // whose error composing `undefined` never reproduces.
386
+ this.#reportEnvComposeFailure();
387
+ }
388
+ #emitReady(...args) {
389
+ this.#readyEmitted = true;
390
+ try {
391
+ this.emit('ready', ...args);
392
+ }
393
+ catch (error) {
394
+ // The failure paths emit `ready` from a ladder timer and from chokidar's error dispatch,
395
+ // where a throwing listener is an uncaught exception that takes the worker down.
396
+ this.#logger.error?.('A configuration listener failed', error);
232
397
  }
233
- this.emit('error', error);
234
398
  }
235
399
  #handleError(error) {
400
+ // A queued chokidar error can land after close(), which has dropped every listener.
401
+ if (this.#closed)
402
+ return;
236
403
  // See EntryHandler.#handleWatcherError: a lost native watch handle is benign
237
404
  // and must not be surfaced to consumers as a config-watch failure.
238
405
  if ((0, watcherFallback_ts_1.claimLostNativeWatchError)(error))
@@ -244,6 +411,9 @@ class OptionsWatcher extends events_1.EventEmitter {
244
411
  if (!this.#usingPolling) {
245
412
  (0, watcherFallback_ts_1.warnWatcherFallback)(this.#filePath);
246
413
  this.#usingPolling = true;
414
+ // The generation that just failed no longer speaks for the watch; the replacement arms
415
+ // on its own scan, and re-reads then as the first one did.
416
+ this.#armGate.reset();
247
417
  // Start close() from a microtask, not directly here, so a synchronous throw
248
418
  // can't escape this 'error' listener as an uncaught exception.
249
419
  Promise.resolve()
@@ -257,11 +427,35 @@ class OptionsWatcher extends events_1.EventEmitter {
257
427
  })
258
428
  .catch((error) => this.#logger.error?.(`Could not reopen the ${this.#filePath} watch on polling:`, error));
259
429
  }
430
+ else {
431
+ // Already polling — the replacement failed too, or the watch was polling from
432
+ // construction (`mustPoll`) and never had a fallback to take. Either way the branch
433
+ // above reopens only once, so no read will ever run: settle the barrier rather than
434
+ // leave `Scope.ready` pending forever.
435
+ this.#settleUnconfigured();
436
+ }
260
437
  return;
261
438
  }
262
- this.emit('error', new OptionsWatcherConfigFileError(this.#filePath, error));
439
+ // Terminal for this scope: `componentLoader` awaits `Scope.ready` with no timeout, and an
440
+ // `error` emitted while it is pending rejects that barrier instead of settling it — the
441
+ // asymmetry with `RootConfigWatcher.handleError` the boot-barrier contract cannot afford.
442
+ const watcherError = new OptionsWatcherConfigFileError(this.#filePath, error);
443
+ this.#settleUnconfigured();
444
+ this.#emitError(watcherError);
263
445
  }
264
446
  #handleUnlink(path) {
447
+ // The deletion settles what a pending read was retrying, and a rung landing after this
448
+ // would find ENOENT and emit a second `remove` at consumers that treat it as teardown.
449
+ // Same for the arming re-read's deferred absence check: this is the event it was waiting on.
450
+ // Cancelling the ladder covers the rung not yet armed; a rung already in flight on the
451
+ // asynchronous path is outranked instead, or its ENOENT would report the same deletion again
452
+ // — after the settle below, as a `remove` that asks `Scope` to restart a scope that just
453
+ // booted on the defaults.
454
+ this.#readRetry.reset();
455
+ this.#appliedSequence = this.#readSequence;
456
+ if (this.#armAbsence)
457
+ clearImmediate(this.#armAbsence);
458
+ this.#armAbsence = undefined;
265
459
  // A real deletion still leaves env-var config in force: an env-defined scope must
266
460
  // survive it exactly as on the ENOENT read path — same fallback, same error routing
267
461
  // (#1618, #1726 review).
@@ -271,16 +465,24 @@ class OptionsWatcher extends events_1.EventEmitter {
271
465
  }
272
466
  this.#logger.warn?.(`Configuration file ${path} was deleted. Reverting to default configuration. Recreate it to restore the options watcher.`);
273
467
  this.#resetConfig();
274
- this.emit('remove');
468
+ // Before the first `ready` there is nothing to remove and nothing to hear it: `Scope.ready`
469
+ // is still pending, so a `remove` here asks for a restart of a scope that never booted while
470
+ // the `#readRetry.reset()` above cancelled the only thing left to settle the barrier. Same
471
+ // ruling as the ENOENT read path, which already boots on the defaults rather than reporting
472
+ // a removal `ready` would then wait behind forever.
473
+ if (this.#readyEmitted)
474
+ this.#emitRemove();
475
+ else
476
+ this.#emitReady(this.#scopedConfig);
477
+ this.#reportEnvComposeFailure();
275
478
  }
276
479
  /**
277
480
  * Shared fallback for the ENOENT read path and `#handleUnlink`: when config env vars
278
- * define this scope, apply the env-only overlay (first application → `ready`; already
279
- * configured → `merge`, never reset). Returns true when the event was handled —
280
- * including the malformed-env case, which routes to `error` like the file-read path
281
- * rather than an unhandled rejection. Returns false (root config untouched) when this
282
- * is not a root config or the env config does not provide the scope, so callers keep
283
- * their own reset semantics.
481
+ * define this scope, apply the env-only overlay (first source application → `ready`;
482
+ * already configured → `merge`, never reset). Returns false — root config untouched, so
483
+ * callers keep their own reset semantics — when this is not a root config, when the env
484
+ * config does not provide the scope, and when composing it threw, which is held for
485
+ * `#reportEnvComposeFailure` rather than emitted here.
284
486
  */
285
487
  #applyEnvOnlyConfig() {
286
488
  if (!this.#isRootConfig)
@@ -290,25 +492,71 @@ class OptionsWatcher extends events_1.EventEmitter {
290
492
  composed = (0, harperConfigEnvVars_ts_1.overlayRootEnvConfig)(undefined);
291
493
  }
292
494
  catch (composeError) {
293
- this.emit('error', composeError);
294
- return true;
495
+ // Env config that cannot be composed is not env config: `false` puts every caller on the
496
+ // path it already takes when there is none, so the barrier still settles on the defaults
497
+ // and a deletion still emits `remove`. The failure is held for `#reportEnvComposeFailure`,
498
+ // which callers run *after* settling — an `error` emitted first settles
499
+ // `once(this, 'ready')` by rejection instead.
500
+ this.#envComposeError = composeError;
501
+ return false;
295
502
  }
296
503
  if (!composed || !(this.#name in composed))
297
504
  return false;
298
505
  this.#rootConfig = composed;
299
- if (!this.#scopedConfig) {
300
- this.#scopedConfig = composed[this.#name];
301
- this.emit('ready', this.#scopedConfig);
506
+ this.#applyScopedConfig(composed[this.#name]);
507
+ return true;
508
+ }
509
+ #reportEnvComposeFailure() {
510
+ if (this.#envComposeError === undefined)
511
+ return;
512
+ const error = this.#envComposeError;
513
+ this.#envComposeError = undefined;
514
+ this.#emitError(error);
515
+ }
516
+ // A scope with no source config takes `ready`, one with a prior source value takes the granular `change`
517
+ // events `#merge` derives. `ready` is emitted more than once: a scope can go back to having no
518
+ // config of its own — see `Scope.#handleOptionsWatcherReady` for what a repeat means there.
519
+ #applyScopedConfig(next) {
520
+ if (!this.#scopeConfigured) {
521
+ this.#scopeConfigured = true;
522
+ this.#scopedConfig = next;
523
+ this.#emitReady(this.#scopedConfig);
524
+ return;
302
525
  }
303
- else {
304
- this.#merge(composed[this.#name], this.#scopedConfig);
526
+ if (this.#scopedConfig) {
527
+ return this.#merge(next, this.#scopedConfig);
305
528
  }
306
- return true;
529
+ // A falsy scope value is still a configured scope — `myPlugin:` with nothing under it is
530
+ // the idiomatic "enable with defaults" — and `#merge` cannot diff from one, because
531
+ // `#setValue` needs a value to walk. Re-reading it is not a transition (every ladder rung
532
+ // and rename-burst re-read would otherwise look like one), but *filling it in* is a change
533
+ // like any other, not the unconfigured → configured `ready` that `Scope` answers with a
534
+ // restart.
535
+ if ((0, util_1.isDeepStrictEqual)(next, this.#scopedConfig))
536
+ return;
537
+ this.#scopedConfig = next;
538
+ this.#emitChange([], next);
307
539
  }
540
+ // Cloned, never aliased: `#merge` writes into `#scopedConfig` in place, so a scope that starts
541
+ // on the defaults and is then configured would otherwise write the app's values into the
542
+ // module-level `DEFAULT_CONFIG` every later reset hands out. And not configured: the six scopes
543
+ // `DEFAULT_CONFIG` names would otherwise have the next read of an unchanged file look like the
544
+ // block being deleted.
308
545
  #resetConfig() {
309
- this.#rootConfig = DEFAULT_CONFIG_ts_1.DEFAULT_CONFIG;
546
+ this.#scopeConfigured = false;
547
+ this.#rootConfig = (0, lodash_1.cloneDeep)(DEFAULT_CONFIG_ts_1.DEFAULT_CONFIG);
310
548
  this.#scopedConfig = this.#rootConfig[this.#name];
311
549
  }
550
+ #deferAbsenceCheck() {
551
+ if (this.#armAbsence)
552
+ return;
553
+ this.#armAbsence = setImmediate(() => {
554
+ this.#armAbsence = undefined;
555
+ if (!this.#closed)
556
+ this.#read(true);
557
+ });
558
+ this.#armAbsence.unref?.();
559
+ }
312
560
  /**
313
561
  * This merge algorithm is best thought off as a diff and overwrite.
314
562
  * The new config object will completely overwrite the old config object,
@@ -390,7 +638,7 @@ class OptionsWatcher extends events_1.EventEmitter {
390
638
  }
391
639
  if (keys.length === 0) {
392
640
  this.#scopedConfig = value;
393
- this.emit('change', keys, value, this.#scopedConfig);
641
+ this.#emitChange(keys, value);
394
642
  return;
395
643
  }
396
644
  let obj = this.#scopedConfig;
@@ -404,14 +652,19 @@ class OptionsWatcher extends events_1.EventEmitter {
404
652
  throw new CannotSetPropertyError(keys);
405
653
  }
406
654
  obj[keys[keys.length - 1]] = value;
407
- this.emit('change', keys, value, this.#scopedConfig);
655
+ this.#emitChange(keys, value);
408
656
  }
409
- // Test-only: run the change handler directly, since the read's timing relative to its caller
410
- // is the behaviour under test and a chokidar event cannot be observed at that granularity.
411
- // Resolves once the read has landed, which for the root config has already happened.
412
- _handleChangeForTests() {
413
- this.#handleChange();
414
- return Promise.allSettled([...this.#pendingReads]);
657
+ // `#merge` runs inside chokidar's own dispatch on the unlink and env-fallback paths, where a
658
+ // plugin's `change` handler throwing would leave the worker with an uncaught exception on a
659
+ // config event the watcher had just decided to survive. It still reaches consumers as `error`
660
+ // — a listener fault is not a read fault, which is why it must not reach `#handleReadError`.
661
+ #emitChange(keys, value) {
662
+ try {
663
+ this.emit('change', keys, value, this.#scopedConfig);
664
+ }
665
+ catch (error) {
666
+ this.#emitError(error);
667
+ }
415
668
  }
416
669
  // Test-only: simulate the underlying chokidar watcher emitting an error.
417
670
  // Exposed so the polling-fallback path can be exercised without triggering a
@@ -419,24 +672,45 @@ class OptionsWatcher extends events_1.EventEmitter {
419
672
  _simulateWatcherErrorForTests(error) {
420
673
  this.#handleError(error);
421
674
  }
422
- // Test-only: whether the watcher has fallen back to polling.
675
+ // Test-only: a real unlink cannot be ordered against a read already in flight — see DESIGN.md,
676
+ // "Root config watchers must read synchronously".
677
+ _simulateUnlinkForTests(path) {
678
+ this.#handleUnlink(path);
679
+ }
423
680
  get _usingPollingForTests() {
424
681
  return this.#usingPolling;
425
682
  }
426
- // Test-only: number of times the underlying watcher has been (re)opened.
427
- // Used to assert that a close()-during-fallback race didn't install a
428
- // replacement watcher.
683
+ // Used to assert that a close()-during-fallback race didn't install a replacement watcher.
429
684
  get _openCountForTests() {
430
685
  return this.#openCount;
431
686
  }
687
+ // Distinguishes a ladder rung from a watcher event.
688
+ get _readCountForTests() {
689
+ return this.#readCount;
690
+ }
691
+ get _armedForTests() {
692
+ return this.#armGate.armed;
693
+ }
694
+ // Test-only: read now, rather than at whatever granularity a chokidar event would arrive.
695
+ _refreshForTests(arming = false) {
696
+ this.#read(true, arming);
697
+ return Promise.allSettled([...this.#pendingReads]);
698
+ }
432
699
  /**
433
700
  * Closes the underlying file watcher and drains any pending config-file reads.
434
701
  * Emits `close` synchronously, removes all listeners, then returns a Promise that
435
702
  * resolves once the chokidar watcher has fully stopped and all in-flight reads settle.
436
703
  */
437
704
  close() {
705
+ // Terminal like every other outcome, and `Scope.ready` has no timeout. Before `#closed` and
706
+ // through `#emitReady`, so a listener that throws cannot skip the teardown below it.
707
+ this.#settleUnconfigured();
438
708
  this.#closed = true;
439
- this.#partialRead.cancel();
709
+ this.#readRetry.cancel();
710
+ this.#armGate.cancel();
711
+ if (this.#armAbsence)
712
+ clearImmediate(this.#armAbsence);
713
+ this.#armAbsence = undefined;
440
714
  const pendingReads = [...this.#pendingReads];
441
715
  const watcherClose = Promise.resolve(this.#watcher.close()).catch(() => { });
442
716
  this.emit('close');