@nitra/nats 4.0.0 → 4.0.2

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @nitra/nats
2
2
 
3
- NATS JetStream helper для Node.js.
3
+ NATS JetStream helper для Node.js
4
4
  Простий API для публікації, обробки та моніторингу повідомлень у черзі з гнучким управлінням consumer-ами через конфігурацію та CLI інструменти.
5
5
 
6
6
  ---
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nitra/nats",
3
3
  "description": "nats helper",
4
- "version": "4.0.0",
4
+ "version": "4.0.2",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "src"
8
8
  ],
9
9
  "bin": {
10
- "nitra-nats": "./src/cli.js"
10
+ "nitra-nats": "src/cli.js"
11
11
  },
12
12
  "exports": {
13
13
  ".": "./src/index.js"
@@ -18,14 +18,14 @@
18
18
  ],
19
19
  "repository": {
20
20
  "type": "git",
21
- "url": "https://github.com/nitra/nats.git"
21
+ "url": "git+https://github.com/nitra/nats.git"
22
22
  },
23
23
  "bugs": "https://github.com/nitra/nats/issues",
24
24
  "homepage": "https://github.com/nitra/nats",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@nitra/check-env": "^4.1.0",
28
- "@nitra/pino": "^2.7.4",
28
+ "@nitra/pino": "^2.8.1",
29
29
  "js-yaml": "^4.1.1",
30
30
  "nats": "^2.29.3"
31
31
  },
package/src/cli.js CHANGED
@@ -1,16 +1,20 @@
1
1
  #!/usr/bin/env node
2
- import fs from 'fs'
3
- import path from 'path'
2
+ import { load } from 'js-yaml'
3
+ import { readFileSync } from 'node:fs'
4
+ import path from 'node:path'
4
5
  import { exit } from 'node:process'
5
- import yaml from 'js-yaml'
6
6
  import { ensureConsumer } from './consumer.js'
7
7
  import { checkSubjectFormat } from './utils.js'
8
8
 
9
+ /**
10
+ * Парсить YAML файл з конфігурацією consumer-а
11
+ * @returns {object} об'єкт consumer-а
12
+ */
9
13
  function parseConsumerYaml() {
10
14
  try {
11
15
  const filePath = path.resolve(process.cwd(), process.argv[2])
12
- const fileContents = fs.readFileSync(filePath, 'utf8')
13
- const data = yaml.load(fileContents)
16
+ const fileContents = readFileSync(filePath, 'utf8')
17
+ const data = load(fileContents)
14
18
 
15
19
  return {
16
20
  apiVersion: data.apiVersion,
@@ -34,7 +38,7 @@ function parseConsumerYaml() {
34
38
 
35
39
  /**
36
40
  * Валідує структуру парсеного consumer об'єкта
37
- * @param {Object} consumer - Об'єкт consumer для валідації
41
+ * @param {object} consumer - Об'єкт consumer для валідації
38
42
  * @returns {boolean} true якщо валідація пройшла успішно
39
43
  * @throws {Error} Якщо валідація не пройшла
40
44
  */
@@ -54,8 +58,12 @@ export function validateConsumer(consumer) {
54
58
  }
55
59
  }
56
60
 
57
- // Приклад використання
58
- if (import.meta.url === `file://${process.argv[1]}`) {
61
+ // Виконуємо код коли скрипт запущений напряму (включає сумісність з bunx)
62
+ if (
63
+ import.meta.url === `file://${process.argv[1]}` ||
64
+ process.argv[1].endsWith('nitra-nats') ||
65
+ import.meta.url.includes('cli.js')
66
+ ) {
59
67
  try {
60
68
  const consumer = parseConsumerYaml()
61
69
 
@@ -67,4 +75,7 @@ if (import.meta.url === `file://${process.argv[1]}`) {
67
75
  console.error('Помилка:', error.message)
68
76
  exit(1)
69
77
  }
78
+ } else {
79
+ console.error('Скрипт запущений як модуль, не можна використовувати CLI інструмент')
80
+ exit(1)
70
81
  }
package/src/nats.js CHANGED
@@ -12,7 +12,7 @@ if (env.NATS_FAKE_DATA) {
12
12
  nc = {
13
13
  jetstream: () => {},
14
14
  jetstreamManager: async () => {
15
- Promise.resolve()
15
+ {}
16
16
  }
17
17
  }
18
18
  } else {
package/src/publish.js CHANGED
@@ -1,7 +1,7 @@
1
- import { jc, js, stream } from './nats.js'
2
- import { checkSubjectFormat } from './utils.js'
3
1
  import { log } from '@nitra/pino'
4
2
  import { env } from 'node:process'
3
+ import { jc, js, stream } from './nats.js'
4
+ import { checkSubjectFormat } from './utils.js'
5
5
 
6
6
  /**
7
7
  * Публікує повідомлення у JetStream для вказаного subject.
@@ -9,10 +9,11 @@ import { env } from 'node:process'
9
9
  * @param {object} data - дані для публікації
10
10
  * @returns {Promise<void>}
11
11
  */
12
+ // oxlint-disable-next-line require-await
12
13
  export async function publish(subject, data) {
13
14
  // якщо задані демо дані, то ігноримо запис
14
15
  if (env.NATS_FAKE_DATA) {
15
- return Promise.resolve()
16
+ return
16
17
  }
17
18
 
18
19
  // перевіряємо чи subject відповідає формату project:subject
package/src/stream.js CHANGED
@@ -1,8 +1,9 @@
1
- import { jsm, stream } from './nats.js'
2
1
  import { log } from '@nitra/pino'
2
+ import { jsm, stream } from './nats.js'
3
3
 
4
4
  /**
5
5
  * Перевіряє наявність або створює JetStream stream.
6
+ * @param streamName
6
7
  * @returns {Promise<void>}
7
8
  */
8
9
  export async function ensureStream(streamName = stream) {
package/src/worker.js CHANGED
@@ -1,8 +1,7 @@
1
- import { js, jc, stream } from './nats.js'
2
- import { state } from './utils.js'
3
- import { exit } from 'node:process'
4
1
  import { log } from '@nitra/pino'
5
- import { env } from 'node:process'
2
+ import { env, exit } from 'node:process'
3
+ import { jc, js, stream } from './nats.js'
4
+ import { state } from './utils.js'
6
5
 
7
6
  /**
8
7
  * Зчитує одне повідомлення з JetStream для вказаного consumer-а.