@inlang/sdk 0.26.5 → 0.28.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 (60) hide show
  1. package/dist/adapter/solidAdapter.test.js +52 -4
  2. package/dist/api.d.ts +5 -0
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/createMessageLintReportsQuery.d.ts +1 -1
  5. package/dist/createMessageLintReportsQuery.d.ts.map +1 -1
  6. package/dist/createMessageLintReportsQuery.js +30 -22
  7. package/dist/createMessagesQuery.d.ts.map +1 -1
  8. package/dist/createMessagesQuery.js +24 -1
  9. package/dist/createMessagesQuery.test.js +46 -2
  10. package/dist/createNodeishFsWithAbsolutePaths.d.ts +3 -3
  11. package/dist/createNodeishFsWithAbsolutePaths.d.ts.map +1 -1
  12. package/dist/createNodeishFsWithAbsolutePaths.js +9 -1
  13. package/dist/createNodeishFsWithAbsolutePaths.test.js +7 -1
  14. package/dist/createNodeishFsWithWatcher.d.ts +3 -3
  15. package/dist/createNodeishFsWithWatcher.d.ts.map +1 -1
  16. package/dist/createNodeishFsWithWatcher.js +3 -0
  17. package/dist/errors.d.ts +14 -0
  18. package/dist/errors.d.ts.map +1 -1
  19. package/dist/errors.js +12 -0
  20. package/dist/loadProject.d.ts.map +1 -1
  21. package/dist/loadProject.js +474 -39
  22. package/dist/loadProject.test.js +93 -14
  23. package/dist/messages/variant.test.js +3 -0
  24. package/dist/resolve-modules/plugins/resolvePlugins.test.js +4 -2
  25. package/dist/storage/helper.d.ts +5 -0
  26. package/dist/storage/helper.d.ts.map +1 -0
  27. package/dist/storage/helper.js +35 -0
  28. package/dist/storage/human-id/human-readable-id.d.ts +3 -0
  29. package/dist/storage/human-id/human-readable-id.d.ts.map +1 -0
  30. package/dist/storage/human-id/human-readable-id.js +20 -0
  31. package/dist/storage/human-id/words.d.ts +5 -0
  32. package/dist/storage/human-id/words.d.ts.map +1 -0
  33. package/dist/storage/human-id/words.js +1032 -0
  34. package/dist/storage/human-id/words.test.d.ts +2 -0
  35. package/dist/storage/human-id/words.test.d.ts.map +1 -0
  36. package/dist/storage/human-id/words.test.js +25 -0
  37. package/dist/test-utilities/createMessage.d.ts +1 -0
  38. package/dist/test-utilities/createMessage.d.ts.map +1 -1
  39. package/dist/test-utilities/createMessage.js +1 -0
  40. package/dist/test-utilities/createMessage.test.js +70 -55
  41. package/package.json +16 -7
  42. package/src/adapter/solidAdapter.test.ts +76 -4
  43. package/src/api.ts +6 -0
  44. package/src/createMessageLintReportsQuery.ts +47 -35
  45. package/src/createMessagesQuery.test.ts +54 -2
  46. package/src/createMessagesQuery.ts +30 -1
  47. package/src/createNodeishFsWithAbsolutePaths.test.ts +10 -3
  48. package/src/createNodeishFsWithAbsolutePaths.ts +14 -5
  49. package/src/createNodeishFsWithWatcher.ts +6 -3
  50. package/src/errors.ts +20 -0
  51. package/src/loadProject.test.ts +108 -14
  52. package/src/loadProject.ts +657 -60
  53. package/src/messages/variant.test.ts +3 -0
  54. package/src/resolve-modules/plugins/resolvePlugins.test.ts +4 -2
  55. package/src/storage/helper.ts +48 -0
  56. package/src/storage/human-id/human-readable-id.ts +27 -0
  57. package/src/storage/human-id/words.test.ts +27 -0
  58. package/src/storage/human-id/words.ts +1035 -0
  59. package/src/test-utilities/createMessage.test.ts +72 -54
  60. package/src/test-utilities/createMessage.ts +1 -0
@@ -1,9 +1,8 @@
1
1
  import { resolveModules } from "./resolve-modules/index.js";
2
2
  import { TypeCompiler, ValueErrorType } from "@sinclair/typebox/compiler";
3
- import { ProjectSettingsFileJSONSyntaxError, ProjectSettingsFileNotFoundError, ProjectSettingsInvalidError, PluginLoadMessagesError, PluginSaveMessagesError, LoadProjectInvalidArgument, } from "./errors.js";
3
+ import { ProjectSettingsFileJSONSyntaxError, ProjectSettingsFileNotFoundError, ProjectSettingsInvalidError, PluginSaveMessagesError, LoadProjectInvalidArgument, PluginLoadMessagesError, } from "./errors.js";
4
4
  import { createRoot, createSignal, createEffect } from "./reactivity/solid.js";
5
5
  import { createMessagesQuery } from "./createMessagesQuery.js";
6
- import { debounce } from "throttle-debounce";
7
6
  import { createMessageLintReportsQuery } from "./createMessageLintReportsQuery.js";
8
7
  import { ProjectSettings, Message } from "./versionedInterfaces.js";
9
8
  import { tryCatch } from "@inlang/result";
@@ -11,11 +10,15 @@ import { migrateIfOutdated } from "@inlang/project-settings/migration";
11
10
  import { createNodeishFsWithAbsolutePaths } from "./createNodeishFsWithAbsolutePaths.js";
12
11
  import { normalizePath } from "@lix-js/fs";
13
12
  import { isAbsolutePath } from "./isAbsolutePath.js";
14
- import { createNodeishFsWithWatcher } from "./createNodeishFsWithWatcher.js";
15
13
  import { maybeMigrateToDirectory } from "./migrations/migrateToDirectory.js";
14
+ import { stringifyMessage as stringifyMessage } from "./storage/helper.js";
15
+ import { humanIdHash } from "./storage/human-id/human-readable-id.js";
16
+ import { createNodeishFsWithWatcher } from "./createNodeishFsWithWatcher.js";
16
17
  import { maybeCreateFirstProjectId } from "./migrations/maybeCreateFirstProjectId.js";
17
18
  import { capture } from "./telemetry/capture.js";
18
19
  import { identifyProject } from "./telemetry/groupIdentify.js";
20
+ import _debug from "debug";
21
+ const debug = _debug("loadProject");
19
22
  const settingsCompiler = TypeCompiler.Compile(ProjectSettings);
20
23
  /**
21
24
  * @param projectPath - Absolute path to the inlang settings file.
@@ -27,6 +30,15 @@ const settingsCompiler = TypeCompiler.Compile(ProjectSettings);
27
30
  */
28
31
  export async function loadProject(args) {
29
32
  const projectPath = normalizePath(args.projectPath);
33
+ const messageStates = {
34
+ messageDirtyFlags: {},
35
+ messageLoadHash: {},
36
+ isSaving: false,
37
+ currentSaveMessagesViaPlugin: undefined,
38
+ sheduledSaveMessages: undefined,
39
+ isLoading: false,
40
+ sheduledLoadMessagesViaPlugin: undefined,
41
+ };
30
42
  // -- validation --------------------------------------------------------
31
43
  // the only place where throwing is acceptable because the project
32
44
  // won't even be loaded. do not throw anywhere else. otherwise, apps
@@ -100,8 +112,14 @@ export async function loadProject(args) {
100
112
  createEffect(() => (settingsValue = settings())); // workaround to not run effects twice (e.g. settings change + modules change) (I'm sure there exists a solid way of doing this, but I haven't found it yet)
101
113
  // please don't use this as source of truth, use the query instead
102
114
  // needed for granular linting
103
- const [messages, setMessages] = createSignal();
115
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars -- setMessages is not called directly we use the CRUD operations on the messageQuery to set the messages now
116
+ const [messages, setMessages] = createSignal([]);
117
+ const [loadMessagesViaPluginError, setLoadMessagesViaPluginError] = createSignal();
118
+ const [saveMessagesViaPluginError, setSaveMessagesViaPluginError] = createSignal();
119
+ const messagesQuery = createMessagesQuery(() => messages());
120
+ const messageLockDirPath = projectPath + "/messagelock";
104
121
  createEffect(() => {
122
+ // wait for first effect excution until modules are resolved
105
123
  const _resolvedModules = resolvedModules();
106
124
  if (!_resolvedModules)
107
125
  return;
@@ -109,24 +127,36 @@ export async function loadProject(args) {
109
127
  markInitAsFailed(undefined);
110
128
  return;
111
129
  }
112
- const loadAndSetMessages = async (fs) => {
113
- makeTrulyAsync(_resolvedModules.resolvedPluginApi.loadMessages({
114
- settings: settingsValue,
115
- nodeishFs: fs,
116
- }))
117
- .then((messages) => {
118
- setMessages(messages);
119
- markInitAsComplete();
120
- })
121
- .catch((err) => markInitAsFailed(new PluginLoadMessagesError({ cause: err })));
122
- };
130
+ const _settings = settings();
131
+ if (!_settings)
132
+ return;
133
+ // get plugin finding the plugin that provides loadMessages function
134
+ const loadMessagePlugin = _resolvedModules.plugins.find((plugin) => plugin.loadMessages !== undefined);
135
+ // TODO #1844 this watcher needs to get pruned when we have a change in the configs which will trigger this again
123
136
  const fsWithWatcher = createNodeishFsWithWatcher({
124
137
  nodeishFs: nodeishFs,
138
+ // this message is called whenever a file changes that was read earlier by this filesystem
139
+ // - the plugin loads messages -> reads the file messages.json -> start watching on messages.json -> updateMessages
125
140
  updateMessages: () => {
126
- loadAndSetMessages(nodeishFs);
141
+ // preserving console.logs as comments pending #
142
+ debug("load messages because of a change in the message.json files");
143
+ loadMessagesViaPlugin(fsWithWatcher, messageLockDirPath, messageStates, messagesQuery, settings(), // NOTE we bang here - we don't expect the settings to become null during the livetime of a project
144
+ loadMessagePlugin)
145
+ .catch((e) => setLoadMessagesViaPluginError(new PluginLoadMessagesError({ cause: e })))
146
+ .then(() => {
147
+ if (loadMessagesViaPluginError() !== undefined) {
148
+ setLoadMessagesViaPluginError(undefined);
149
+ }
150
+ });
127
151
  },
128
152
  });
129
- loadAndSetMessages(fsWithWatcher);
153
+ loadMessagesViaPlugin(fsWithWatcher, messageLockDirPath, messageStates, messagesQuery, _settings, loadMessagePlugin)
154
+ .then(() => {
155
+ markInitAsComplete();
156
+ })
157
+ .catch((err) => {
158
+ markInitAsFailed(new PluginLoadMessagesError({ cause: err }));
159
+ });
130
160
  });
131
161
  // -- installed items ----------------------------------------------------
132
162
  const installedMessageLintRules = () => {
@@ -140,6 +170,7 @@ export async function loadProject(args) {
140
170
  "Unknown module. You stumbled on a bug in inlang's source code. Please open an issue.",
141
171
  // default to warning, see https://github.com/opral/monorepo/issues/1254
142
172
  level: settingsValue["messageLintRuleLevels"]?.[rule.id] ?? "warning",
173
+ settingsSchema: rule.settingsSchema,
143
174
  }));
144
175
  };
145
176
  const installedPlugins = () => {
@@ -151,38 +182,79 @@ export async function loadProject(args) {
151
182
  description: plugin.description,
152
183
  module: resolvedModules()?.meta.find((m) => m.id.includes(plugin.id))?.module ??
153
184
  "Unknown module. You stumbled on a bug in inlang's source code. Please open an issue.",
185
+ settingsSchema: plugin.settingsSchema,
154
186
  }));
155
187
  };
156
188
  // -- app ---------------------------------------------------------------
157
189
  const initializeError = await initialized.catch((error) => error);
158
190
  const abortController = new AbortController();
159
- const hasWatcher = nodeishFs.watch("/", { signal: abortController.signal }) !== undefined;
160
- const messagesQuery = createMessagesQuery(() => messages() || []);
161
- const lintReportsQuery = createMessageLintReportsQuery(messagesQuery, settings, installedMessageLintRules, resolvedModules, hasWatcher);
162
- const debouncedSave = skipFirst(debounce(500, async (newMessages) => {
163
- try {
164
- if (JSON.stringify(newMessages) !== JSON.stringify(messages())) {
165
- await resolvedModules()?.resolvedPluginApi.saveMessages({
166
- settings: settingsValue,
167
- messages: newMessages,
191
+ nodeishFs.watch("/", { signal: abortController.signal }) !== undefined;
192
+ // map of message id => dispose function from createRoot for each message
193
+ const trackedMessages = new Map();
194
+ let initialSetup = true;
195
+ // -- subscribe to all messages and write to files on signal -------------
196
+ createEffect(() => {
197
+ // debug("Outer createEffect")
198
+ const _resolvedModules = resolvedModules();
199
+ if (!_resolvedModules)
200
+ return;
201
+ const currentMessageIds = new Set(messagesQuery.includedMessageIds());
202
+ const deletedTrackedMessages = [...trackedMessages].filter((tracked) => !currentMessageIds.has(tracked[0]));
203
+ const saveMessagesPlugin = _resolvedModules.plugins.find((plugin) => plugin.saveMessages !== undefined);
204
+ const loadMessagesPlugin = _resolvedModules.plugins.find((plugin) => plugin.loadMessages !== undefined);
205
+ for (const messageId of currentMessageIds) {
206
+ if (!trackedMessages.has(messageId)) {
207
+ // we create a new root to be able to cleanup an effect for a message that got deleted
208
+ createRoot((dispose) => {
209
+ createEffect(() => {
210
+ // debug("Inner createEffect", messageId)
211
+ const message = messagesQuery.get({ where: { id: messageId } });
212
+ if (!message) {
213
+ return;
214
+ }
215
+ if (!trackedMessages?.has(messageId)) {
216
+ // initial effect execution - add dispose function
217
+ trackedMessages?.set(messageId, dispose);
218
+ }
219
+ // don't trigger saves or set dirty flags during initial setup
220
+ if (!initialSetup) {
221
+ messageStates.messageDirtyFlags[message.id] = true;
222
+ saveMessagesViaPlugin(fs, messageLockDirPath, messageStates, messagesQuery, settings(), saveMessagesPlugin, loadMessagesPlugin)
223
+ .catch((e) => setSaveMessagesViaPluginError(new PluginSaveMessagesError({ cause: e })))
224
+ .then(() => {
225
+ if (saveMessagesViaPluginError() !== undefined) {
226
+ setSaveMessagesViaPluginError(undefined);
227
+ }
228
+ });
229
+ }
230
+ });
168
231
  });
169
232
  }
170
233
  }
171
- catch (err) {
172
- throw new PluginSaveMessagesError({
173
- cause: err,
174
- });
234
+ for (const deletedMessage of deletedTrackedMessages) {
235
+ const deletedMessageId = deletedMessage[0];
236
+ // call dispose to cleanup the effect
237
+ const messageEffectDisposeFunction = trackedMessages.get(deletedMessageId);
238
+ if (messageEffectDisposeFunction) {
239
+ messageEffectDisposeFunction();
240
+ trackedMessages.delete(deletedMessageId);
241
+ }
242
+ // mark the deleted message as dirty to force a save
243
+ messageStates.messageDirtyFlags[deletedMessageId] = true;
175
244
  }
176
- const abortController = new AbortController();
177
- if (newMessages.length !== 0 &&
178
- JSON.stringify(newMessages) !== JSON.stringify(messages()) &&
179
- nodeishFs.watch("/", { signal: abortController.signal }) !== undefined) {
180
- setMessages(newMessages);
245
+ if (deletedTrackedMessages.length > 0) {
246
+ // we keep track of the latest save within the loadProject call to await it at the end - this is not used in subsequetial upserts
247
+ saveMessagesViaPlugin(nodeishFs, messageLockDirPath, messageStates, messagesQuery, settings(), saveMessagesPlugin, loadMessagesPlugin)
248
+ .catch((e) => setSaveMessagesViaPluginError(new PluginSaveMessagesError({ cause: e })))
249
+ .then(() => {
250
+ if (saveMessagesViaPluginError() !== undefined) {
251
+ setSaveMessagesViaPluginError(undefined);
252
+ }
253
+ });
181
254
  }
182
- }, { atBegin: false }));
183
- createEffect(() => {
184
- debouncedSave(messagesQuery.getAll());
255
+ initialSetup = false;
185
256
  });
257
+ const lintReportsQuery = createMessageLintReportsQuery(messagesQuery, settings, installedMessageLintRules, resolvedModules);
186
258
  /**
187
259
  * Utility to escape reactive tracking and avoid multiple calls to
188
260
  * the capture event.
@@ -221,6 +293,10 @@ export async function loadProject(args) {
221
293
  errors: createSubscribable(() => [
222
294
  ...(initializeError ? [initializeError] : []),
223
295
  ...(resolvedModules() ? resolvedModules().errors : []),
296
+ ...(loadMessagesViaPluginError() ? [loadMessagesViaPluginError()] : []),
297
+ ...(saveMessagesViaPluginError() ? [saveMessagesViaPluginError()] : []),
298
+ // have a query error exposed
299
+ //...(lintErrors() ?? []),
224
300
  ]),
225
301
  settings: createSubscribable(() => settings()),
226
302
  setSettings,
@@ -232,7 +308,6 @@ export async function loadProject(args) {
232
308
  };
233
309
  });
234
310
  }
235
- //const x = {} as InlangProject
236
311
  // ------------------------------------------------------------------------------------------------
237
312
  const loadSettings = async (args) => {
238
313
  const { data: settingsFile, error: settingsFileError } = await tryCatch(async () => await args.nodeishFs.readFile(args.settingsFilePath, { encoding: "utf-8" }));
@@ -319,3 +394,363 @@ export function createSubscribable(signal) {
319
394
  },
320
395
  });
321
396
  }
397
+ // --- serialization of loading / saving messages.
398
+ // 1. A plugin saveMessage call can not be called simultaniously to avoid side effects - its an async function not controlled by us
399
+ // 2. loading and saving must not run in "parallel".
400
+ // - json plugin exports into separate file per language.
401
+ // - saving a message in two different languages would lead to a write in de.json first
402
+ // - This will leads to a load of the messages and since en.json has not been saved yet the english variant in the message would get overritten with the old state again
403
+ /**
404
+ * Messsage that loads messages from a plugin - this method synchronizes with the saveMessage funciton.
405
+ * If a save is in progress loading will wait until saving is done. If another load kicks in during this load it will queue the
406
+ * load and execute it at the end of this load. subsequential loads will not be queued but the same promise will be reused
407
+ *
408
+ * - NOTE: this means that the parameters used to load like settingsValue and loadPlugin might not take into account. this has to be refactored
409
+ * with the loadProject restructuring
410
+ * @param fs
411
+ * @param messagesQuery
412
+ * @param settingsValue
413
+ * @param loadPlugin
414
+ * @returns void - updates the files and messages in of the project in place
415
+ */
416
+ async function loadMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, loadPlugin) {
417
+ const experimentalAliases = !!settingsValue.experimental?.aliases;
418
+ // loading is an asynchronous process - check if another load is in progress - queue this call if so
419
+ if (messageState.isLoading) {
420
+ if (!messageState.sheduledLoadMessagesViaPlugin) {
421
+ messageState.sheduledLoadMessagesViaPlugin = createAwaitable();
422
+ }
423
+ // another load will take place right after the current one - its goingt to be idempotent form the current requested one - don't reschedule
424
+ return messageState.sheduledLoadMessagesViaPlugin[0];
425
+ }
426
+ // set loading flag
427
+ messageState.isLoading = true;
428
+ let lockTime = undefined;
429
+ try {
430
+ lockTime = await acquireFileLock(fs, lockDirPath, "loadMessage");
431
+ const loadedMessages = await makeTrulyAsync(loadPlugin.loadMessages({
432
+ settings: settingsValue,
433
+ nodeishFs: fs,
434
+ }));
435
+ for (const loadedMessage of loadedMessages) {
436
+ const loadedMessageClone = structuredClone(loadedMessage);
437
+ const currentMessages = messagesQuery
438
+ .getAll()
439
+ // TODO #1585 here we match using the id to support legacy load message plugins - after we introduced import / export methods we will use importedMessage.alias
440
+ .filter((message) => (experimentalAliases ? message.alias["default"] : message.id) === loadedMessage.id);
441
+ if (currentMessages.length > 1) {
442
+ // NOTE: if we happen to find two messages witht the sam alias we throw for now
443
+ // - this could be the case if one edits the aliase manualy
444
+ throw new Error("more than one message with the same id or alias found ");
445
+ }
446
+ else if (currentMessages.length === 1) {
447
+ // update message in place - leave message id and alias untouched
448
+ loadedMessageClone.alias = {};
449
+ // TODO #1585 we have to map the id of the importedMessage to the alias and fill the id property with the id of the existing message - change when import mesage provides importedMessage.alias
450
+ if (experimentalAliases) {
451
+ loadedMessageClone.alias["default"] = loadedMessageClone.id;
452
+ loadedMessageClone.id = currentMessages[0].id;
453
+ }
454
+ // NOTE stringifyMessage encodes messages independent from key order!
455
+ const importedEnecoded = stringifyMessage(loadedMessageClone);
456
+ // NOTE could use hash instead of the whole object JSON to save memory...
457
+ if (messageState.messageLoadHash[loadedMessageClone.id] === importedEnecoded) {
458
+ debug("skipping upsert!");
459
+ continue;
460
+ }
461
+ // This logic is preventing cycles - could also be handled if update api had a parameter for who triggered update
462
+ // e.g. when FS was updated, we don't need to write back to FS
463
+ // update is synchronous, so update effect will be triggered immediately
464
+ // NOTE: this might trigger a save before we have the chance to delete - but since save is async and waits for the lock acquired by this method - its save to set the flags afterwards
465
+ messagesQuery.update({ where: { id: loadedMessageClone.id }, data: loadedMessageClone });
466
+ // we load a fresh version - lets delete dirty flag that got created by the update
467
+ delete messageState.messageDirtyFlags[loadedMessageClone.id];
468
+ // NOTE could use hash instead of the whole object JSON to save memory...
469
+ messageState.messageLoadHash[loadedMessageClone.id] = importedEnecoded;
470
+ }
471
+ else {
472
+ // message with the given alias does not exist so far
473
+ loadedMessageClone.alias = {};
474
+ // TODO #1585 we have to map the id of the importedMessage to the alias - change when import mesage provides importedMessage.alias
475
+ if (experimentalAliases) {
476
+ loadedMessageClone.alias["default"] = loadedMessageClone.id;
477
+ let currentOffset = 0;
478
+ let messsageId;
479
+ do {
480
+ messsageId = humanIdHash(loadedMessageClone.id, currentOffset);
481
+ if (messagesQuery.get({ where: { id: messsageId } })) {
482
+ currentOffset += 1;
483
+ messsageId = undefined;
484
+ }
485
+ } while (messsageId === undefined);
486
+ // create a humanId based on a hash of the alias
487
+ loadedMessageClone.id = messsageId;
488
+ }
489
+ const importedEnecoded = stringifyMessage(loadedMessageClone);
490
+ // add the message - this will trigger an async file creation in the backgound!
491
+ messagesQuery.create({ data: loadedMessageClone });
492
+ // we load a fresh version - lets delete dirty flag that got created by the create method
493
+ delete messageState.messageDirtyFlags[loadedMessageClone.id];
494
+ messageState.messageLoadHash[loadedMessageClone.id] = importedEnecoded;
495
+ }
496
+ }
497
+ await releaseLock(fs, lockDirPath, "loadMessage", lockTime);
498
+ lockTime = undefined;
499
+ debug("loadMessagesViaPlugin: " + loadedMessages.length + " Messages processed ");
500
+ messageState.isLoading = false;
501
+ }
502
+ finally {
503
+ if (lockTime !== undefined) {
504
+ await releaseLock(fs, lockDirPath, "loadMessage", lockTime);
505
+ }
506
+ messageState.isLoading = false;
507
+ }
508
+ const executingScheduledMessages = messageState.sheduledLoadMessagesViaPlugin;
509
+ if (executingScheduledMessages) {
510
+ // a load has been requested during the load - executed it
511
+ // reset sheduling to except scheduling again
512
+ messageState.sheduledLoadMessagesViaPlugin = undefined;
513
+ // recall load unawaited to allow stack to pop
514
+ loadMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, loadPlugin)
515
+ .then(() => {
516
+ // resolve the scheduled load message promise
517
+ executingScheduledMessages[1]();
518
+ })
519
+ .catch((e) => {
520
+ // reject the scheduled load message promise
521
+ executingScheduledMessages[2](e);
522
+ });
523
+ }
524
+ }
525
+ async function saveMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, savePlugin, loadPlugin) {
526
+ // queue next save if we have a save ongoing
527
+ if (messageState.isSaving) {
528
+ if (!messageState.sheduledSaveMessages) {
529
+ messageState.sheduledSaveMessages = createAwaitable();
530
+ }
531
+ return messageState.sheduledSaveMessages[0];
532
+ }
533
+ // set isSavingFlag
534
+ messageState.isSaving = true;
535
+ messageState.currentSaveMessagesViaPlugin = (async function () {
536
+ const saveMessageHashes = {};
537
+ // check if we have any dirty message - witho
538
+ if (Object.keys(messageState.messageDirtyFlags).length == 0) {
539
+ // nothing to save :-)
540
+ debug("save was skipped - no messages marked as dirty... build!");
541
+ messageState.isSaving = false;
542
+ return;
543
+ }
544
+ let messageDirtyFlagsBeforeSave;
545
+ let lockTime;
546
+ try {
547
+ lockTime = await acquireFileLock(fs, lockDirPath, "saveMessage");
548
+ // since it may takes some time to acquire the lock we check if the save is required still (loadMessage could have happend in between)
549
+ if (Object.keys(messageState.messageDirtyFlags).length == 0) {
550
+ debug("save was skipped - no messages marked as dirty... releasing lock again");
551
+ messageState.isSaving = false;
552
+ // release lock in finally block
553
+ return;
554
+ }
555
+ const currentMessages = messagesQuery.getAll();
556
+ const messagesToExport = [];
557
+ for (const message of currentMessages) {
558
+ if (messageState.messageDirtyFlags[message.id]) {
559
+ const importedEnecoded = stringifyMessage(message);
560
+ // NOTE: could use hash instead of the whole object JSON to save memory...
561
+ saveMessageHashes[message.id] = importedEnecoded;
562
+ }
563
+ const fixedExportMessage = { ...message };
564
+ // TODO #1585 here we match using the id to support legacy load message plugins - after we introduced import / export methods we will use importedMessage.alias
565
+ if (settingsValue.experimental?.aliases) {
566
+ fixedExportMessage.id = fixedExportMessage.alias["default"] ?? fixedExportMessage.id;
567
+ }
568
+ messagesToExport.push(fixedExportMessage);
569
+ }
570
+ // wa are about to save the messages to the plugin - reset all flags now
571
+ messageDirtyFlagsBeforeSave = { ...messageState.messageDirtyFlags };
572
+ messageState.messageDirtyFlags = {};
573
+ // NOTE: this assumes that the plugin will handle message ordering
574
+ await savePlugin.saveMessages({
575
+ settings: settingsValue,
576
+ messages: messagesToExport,
577
+ nodeishFs: fs,
578
+ });
579
+ for (const [messageId, messageHash] of Object.entries(saveMessageHashes)) {
580
+ messageState.messageLoadHash[messageId] = messageHash;
581
+ }
582
+ if (lockTime !== undefined) {
583
+ await releaseLock(fs, lockDirPath, "saveMessage", lockTime);
584
+ lockTime = undefined;
585
+ }
586
+ // if there is a queued load, allow it to take the lock before we run additional saves.
587
+ if (messageState.sheduledLoadMessagesViaPlugin) {
588
+ debug("saveMessagesViaPlugin calling queued loadMessagesViaPlugin to share lock");
589
+ await loadMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, loadPlugin);
590
+ }
591
+ messageState.isSaving = false;
592
+ }
593
+ catch (err) {
594
+ // something went wrong - add dirty flags again
595
+ if (messageDirtyFlagsBeforeSave !== undefined) {
596
+ for (const dirtyMessageId of Object.keys(messageDirtyFlagsBeforeSave)) {
597
+ messageState.messageDirtyFlags[dirtyMessageId] = true;
598
+ }
599
+ }
600
+ if (lockTime !== undefined) {
601
+ await releaseLock(fs, lockDirPath, "saveMessage", lockTime);
602
+ lockTime = undefined;
603
+ }
604
+ messageState.isSaving = false;
605
+ // ok an error
606
+ throw new PluginSaveMessagesError({
607
+ cause: err,
608
+ });
609
+ }
610
+ finally {
611
+ if (lockTime !== undefined) {
612
+ await releaseLock(fs, lockDirPath, "saveMessage", lockTime);
613
+ lockTime = undefined;
614
+ }
615
+ messageState.isSaving = false;
616
+ }
617
+ })();
618
+ await messageState.currentSaveMessagesViaPlugin;
619
+ if (messageState.sheduledSaveMessages) {
620
+ const executingSheduledSaveMessages = messageState.sheduledSaveMessages;
621
+ messageState.sheduledSaveMessages = undefined;
622
+ saveMessagesViaPlugin(fs, lockDirPath, messageState, messagesQuery, settingsValue, savePlugin, loadPlugin)
623
+ .then(() => {
624
+ executingSheduledSaveMessages[1]();
625
+ })
626
+ .catch((e) => {
627
+ executingSheduledSaveMessages[2](e);
628
+ });
629
+ }
630
+ }
631
+ const maxRetries = 5;
632
+ const nProbes = 50;
633
+ const probeInterval = 100;
634
+ async function acquireFileLock(fs, lockDirPath, lockOrigin, tryCount = 0) {
635
+ if (tryCount > maxRetries) {
636
+ throw new Error(lockOrigin + " exceeded maximum Retries (5) to acquire lockfile " + tryCount);
637
+ }
638
+ try {
639
+ debug(lockOrigin + " tries to acquire a lockfile Retry Nr.: " + tryCount);
640
+ await fs.mkdir(lockDirPath);
641
+ const stats = await fs.stat(lockDirPath);
642
+ debug(lockOrigin + " acquired a lockfile Retry Nr.: " + tryCount);
643
+ return stats.mtimeMs;
644
+ }
645
+ catch (error) {
646
+ if (error.code !== "EEXIST") {
647
+ // we only expect the error that the file exists already (locked by other process)
648
+ throw error;
649
+ }
650
+ }
651
+ let currentLockTime;
652
+ try {
653
+ const stats = await fs.stat(lockDirPath);
654
+ currentLockTime = stats.mtimeMs;
655
+ }
656
+ catch (fstatError) {
657
+ if (fstatError.code === "ENOENT") {
658
+ // lock file seems to be gone :) - lets try again
659
+ debug(lockOrigin + " tryCount++ lock file seems to be gone :) - lets try again " + tryCount);
660
+ return acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
661
+ }
662
+ throw fstatError;
663
+ }
664
+ debug(lockOrigin +
665
+ " tries to acquire a lockfile - lock currently in use... starting probe phase " +
666
+ tryCount);
667
+ return new Promise((resolve, reject) => {
668
+ let probeCounts = 0;
669
+ const scheduleProbationTimeout = () => {
670
+ setTimeout(async () => {
671
+ probeCounts += 1;
672
+ let lockFileStats = undefined;
673
+ try {
674
+ debug(lockOrigin + " tries to acquire a lockfile - check if the lock is free now " + tryCount);
675
+ // alright lets give it another try
676
+ lockFileStats = await fs.stat(lockDirPath);
677
+ }
678
+ catch (fstatError) {
679
+ if (fstatError.code === "ENOENT") {
680
+ debug(lockOrigin +
681
+ " tryCount++ in Promise - tries to acquire a lockfile - lock file seems to be free now - try to acquire " +
682
+ tryCount);
683
+ const lock = acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
684
+ return resolve(lock);
685
+ }
686
+ return reject(fstatError);
687
+ }
688
+ // still the same locker! -
689
+ if (lockFileStats.mtimeMs === currentLockTime) {
690
+ if (probeCounts >= nProbes) {
691
+ // ok maximum lock time ran up (we waitetd nProbes * probeInterval) - we consider the lock to be stale
692
+ debug(lockOrigin +
693
+ " tries to acquire a lockfile - lock not free - but stale lets drop it" +
694
+ tryCount);
695
+ try {
696
+ await fs.rmdir(lockDirPath);
697
+ }
698
+ catch (rmLockError) {
699
+ if (rmLockError.code === "ENOENT") {
700
+ // lock already gone?
701
+ // Option 1: The "stale process" decided to get rid of it
702
+ // Option 2: Another process acquiring the lock and detected a stale one as well
703
+ }
704
+ return reject(rmLockError);
705
+ }
706
+ try {
707
+ debug(lockOrigin +
708
+ " tryCount++ same locker - try to acquire again after removing stale lock " +
709
+ tryCount);
710
+ const lock = await acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
711
+ return resolve(lock);
712
+ }
713
+ catch (lockAquireException) {
714
+ return reject(lockAquireException);
715
+ }
716
+ }
717
+ else {
718
+ // lets schedule a new probation
719
+ return scheduleProbationTimeout();
720
+ }
721
+ }
722
+ else {
723
+ try {
724
+ debug(lockOrigin + " tryCount++ different locker - try to acquire again " + tryCount);
725
+ const lock = await acquireFileLock(fs, lockDirPath, lockOrigin, tryCount + 1);
726
+ return resolve(lock);
727
+ }
728
+ catch (error) {
729
+ return reject(error);
730
+ }
731
+ }
732
+ }, probeInterval);
733
+ };
734
+ scheduleProbationTimeout();
735
+ });
736
+ }
737
+ async function releaseLock(fs, lockDirPath, lockOrigin, lockTime) {
738
+ debug(lockOrigin + " releasing the lock ");
739
+ try {
740
+ const stats = await fs.stat(lockDirPath);
741
+ if (stats.mtimeMs === lockTime) {
742
+ // this can be corrupt as welll since the last getStat and the current a modification could have occured :-/
743
+ await fs.rmdir(lockDirPath);
744
+ }
745
+ }
746
+ catch (statError) {
747
+ debug(lockOrigin + " couldn't release the lock");
748
+ if (statError.code === "ENOENT") {
749
+ // ok seeks like the log was released by someone else
750
+ debug(lockOrigin + " WARNING - the lock was released by a different process");
751
+ return;
752
+ }
753
+ debug(statError);
754
+ throw statError;
755
+ }
756
+ }