signalbird 2.6.0 → 2.9.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.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createHmac, timingSafeEqual } from 'crypto';
1
+ import { createHash, createHmac, timingSafeEqual } from 'crypto';
2
2
 
3
3
  // src/node/types.ts
4
4
  var SECRET_PREFIX = "sb_secret_live_";
@@ -15,7 +15,7 @@ var SignalbirdError = class extends Error {
15
15
  var DEFAULT_BASE_URL = "https://live.signalbird.io/api";
16
16
 
17
17
  // src/shared/version.ts
18
- var SDK_VERSION = "2.6.0" ;
18
+ var SDK_VERSION = "2.9.0" ;
19
19
  var SDK_HEADER = "X-Signalbird-Sdk";
20
20
  function sdkHeaderValue(platform) {
21
21
  return `${platform}/${SDK_VERSION}`;
@@ -34,7 +34,69 @@ function noteSdkStatus(headers) {
34
34
  }
35
35
  }
36
36
 
37
- // src/node/client.ts
37
+ // src/shared/serialize.ts
38
+ var MAX_MESSAGE_LENGTH = 4e3;
39
+ function truncateMessage(message) {
40
+ const text = typeof message === "string" ? message : String(message);
41
+ if (text.length <= MAX_MESSAGE_LENGTH) {
42
+ return text;
43
+ }
44
+ return Array.from(text).slice(0, MAX_MESSAGE_LENGTH).join("");
45
+ }
46
+ function toJsonSafe(value) {
47
+ const ancestors = [];
48
+ const walk = (current) => {
49
+ if (typeof current === "bigint") {
50
+ return current.toString();
51
+ }
52
+ if (current === null || typeof current !== "object") {
53
+ return current;
54
+ }
55
+ if (ancestors.includes(current)) {
56
+ return "[Circular]";
57
+ }
58
+ if (current instanceof Error) {
59
+ return { name: current.name, message: current.message, stack: current.stack };
60
+ }
61
+ const withToJson = current;
62
+ if (typeof withToJson.toJSON === "function") {
63
+ try {
64
+ ancestors.push(current);
65
+ return walk(withToJson.toJSON());
66
+ } catch {
67
+ return "[Unserializable]";
68
+ } finally {
69
+ ancestors.pop();
70
+ }
71
+ }
72
+ ancestors.push(current);
73
+ try {
74
+ if (Array.isArray(current)) {
75
+ return current.map((item) => walk(item));
76
+ }
77
+ const out = {};
78
+ for (const key of Object.keys(current)) {
79
+ let item;
80
+ try {
81
+ item = current[key];
82
+ } catch {
83
+ item = "[Unserializable]";
84
+ }
85
+ out[key] = walk(item);
86
+ }
87
+ return out;
88
+ } finally {
89
+ ancestors.pop();
90
+ }
91
+ };
92
+ return walk(value);
93
+ }
94
+ function safeContext(context) {
95
+ if (context === void 0 || context === null) {
96
+ return void 0;
97
+ }
98
+ return toJsonSafe(context);
99
+ }
38
100
  var SignalbirdClient = class {
39
101
  constructor(config) {
40
102
  this.config = config;
@@ -79,13 +141,19 @@ var SignalbirdClient = class {
79
141
  critical: (message, context) => this.critical(key, message, context)
80
142
  };
81
143
  }
82
- /** Tek kayıt gönderir. */
144
+ /**
145
+ * Tek kayıt gönderir.
146
+ *
147
+ * `message` 4000 kod noktasına kırpılır (API sınırı; aşan satır 422 alırdı)
148
+ * ve `context` güvenli kopyaya çevrilir: döngüsel nesne ya da BigInt artık
149
+ * `NETWORK_ERROR` gibi görünen bir istisnaya dönüşmez.
150
+ */
83
151
  async log(input) {
84
152
  return this.send("/v1/radio/log", {
85
153
  key: input.key,
86
- message: input.message,
154
+ message: truncateMessage(input.message),
87
155
  level: input.level,
88
- context: input.context,
156
+ context: safeContext(input.context),
89
157
  source: input.source ?? this.source
90
158
  });
91
159
  }
@@ -94,14 +162,17 @@ var SignalbirdClient = class {
94
162
  *
95
163
  * Kısmi başarı normaldir (kota tam ortada dolabilir), o yüzden sonuç tek bir
96
164
  * durum değil satır satır döner.
165
+ *
166
+ * Doğrulama ise satır satır DEĞİLDİR: tek bir geçersiz satır (4000'i aşan
167
+ * mesaj) bütün paketi 422 ile düşürür. Bu yüzden kırpma burada da yapılır.
97
168
  */
98
169
  async batch(events) {
99
170
  const payload = {
100
171
  events: events.slice(0, 100).map((event) => ({
101
172
  key: event.key,
102
- message: event.message,
173
+ message: truncateMessage(event.message),
103
174
  level: event.level,
104
- context: event.context,
175
+ context: safeContext(event.context),
105
176
  source: event.source ?? this.source
106
177
  }))
107
178
  };
@@ -138,6 +209,20 @@ var SignalbirdClient = class {
138
209
  critical(key, message, context) {
139
210
  return this.log({ key, message, level: "critical", context });
140
211
  }
212
+ /**
213
+ * Kimlik doğrulama hash'i (CONTRACT §15.2) - sohbet/push ziyaretçisinin
214
+ * `external_id`'sini GÜVENİLİR kılar.
215
+ *
216
+ * identity_hash = hex(HMAC-SHA256(hex(SHA-256(secretKey)), externalId))
217
+ *
218
+ * Anahtar gizli anahtarın kendisi değil SHA-256 özetidir: sunucu anahtarı
219
+ * düz saklamaz. Hash sunucuda üretilir, sayfaya `external_id` ile birlikte
220
+ * yazılır; gizli anahtar istemciye İNMEZ.
221
+ */
222
+ identityHash(externalId) {
223
+ const key = createHash("sha256").update(this.config.domainKey).digest("hex");
224
+ return createHmac("sha256", key).update(String(externalId).trim()).digest("hex");
225
+ }
141
226
  /**
142
227
  * Yakalanmamış hataları Telsiz'e bağlar.
143
228
  *
@@ -174,7 +259,7 @@ var SignalbirdClient = class {
174
259
  if (!response) {
175
260
  return { ok: false, code: "NETWORK_ERROR" };
176
261
  }
177
- if (!response.ok) {
262
+ if (!response.ok || response.body?.ok === false) {
178
263
  const code = response.body?.code ?? "UNKNOWN";
179
264
  if (this.throwOnError) {
180
265
  throw new SignalbirdError(`Signalbird: ${code}`, response.status, code);
@@ -246,7 +331,11 @@ var SignalbirdMessaging = class {
246
331
  }
247
332
  // ── Gönderim ──────────────────────────────────────────────────────────
248
333
  sendEmail(input) {
249
- return this.request("POST", "/v1/email/send", input);
334
+ const { template_hash: _ignored, ...body } = input;
335
+ if (_ignored !== void 0 && this.debug) {
336
+ console.warn("[signalbird] sendEmail: template_hash kullan\u0131mdan kalkt\u0131 ve g\xF6nderilmiyor; template ya da template_id kullan\u0131n.");
337
+ }
338
+ return this.request("POST", "/v1/email/send", body);
250
339
  }
251
340
  sendSms(input) {
252
341
  return this.request("POST", "/v1/sms/send", input);