@develit-io/backend-sdk 12.2.1 → 12.3.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,5 @@
1
- export { u as uuidv4, a as uuidv5 } from './shared/backend-sdk.D_gzDKeC.mjs';
1
+ import { s as summarizeBinary, a as serializeLog } from './shared/backend-sdk.DANN3OYI.mjs';
2
+ export { u as uuidv4, b as uuidv5 } from './shared/backend-sdk.DANN3OYI.mjs';
2
3
  import { sql, inArray, eq, gte, lte, and, or } from 'drizzle-orm';
3
4
  import { text, integer } from 'drizzle-orm/sqlite-core';
4
5
  import { COUNTRY_CODES_2, CURRENCY_CODES, BANK_CODES } from '@develit-io/general-codes';
@@ -10,7 +11,7 @@ import { parse } from 'comment-json';
10
11
  import crypto$1 from 'node:crypto';
11
12
  import fs from 'node:fs';
12
13
  import path from 'node:path';
13
- import superjson from 'superjson';
14
+ import 'superjson';
14
15
 
15
16
  const ENVIRONMENT = ["dev", "test", "staging", "production"];
16
17
 
@@ -327,6 +328,8 @@ function redact(value, sensitiveKeys) {
327
328
  function redactValue(value, sensitiveKeys, seen) {
328
329
  if (value === null || value === void 0) return value;
329
330
  if (typeof value !== "object") return value;
331
+ const binary = summarizeBinary(value);
332
+ if (binary) return binary;
330
333
  const obj = value;
331
334
  if (seen.has(obj)) return REDACTED;
332
335
  seen.add(obj);
@@ -908,7 +911,7 @@ function develitWorker(Worker) {
908
911
  entrypoint: this.name,
909
912
  action: this.action,
910
913
  identifier: name,
911
- data: superjson.stringify(data)
914
+ data: serializeLog(data)
912
915
  });
913
916
  }
914
917
  transformLogData(data) {
@@ -7,8 +7,9 @@ import 'node:crypto';
7
7
  import 'node:fs';
8
8
  import 'node:path';
9
9
  import 'drizzle-orm';
10
- import { u as uuidv4 } from './shared/backend-sdk.D_gzDKeC.mjs';
10
+ import { u as uuidv4 } from './shared/backend-sdk.DANN3OYI.mjs';
11
11
  import { verifyPayloadSignature } from './utils/signature.util.mjs';
12
+ import 'superjson';
12
13
 
13
14
  const validateBearerScheme = (header) => {
14
15
  return header.startsWith("Bearer ") && header.length > 7 && !header.slice(7).includes(" ");
@@ -0,0 +1,161 @@
1
+ import superjson from 'superjson';
2
+
3
+ const uuidv4 = () => crypto.randomUUID();
4
+ function parseUUID(uuid) {
5
+ const hex = uuid.replace(/-/g, "");
6
+ const bytes = new Uint8Array(16);
7
+ for (let i = 0; i < 16; i++) {
8
+ bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
9
+ }
10
+ return bytes;
11
+ }
12
+ const uuidv5 = async (name, namespace) => {
13
+ const namespaceBytes = parseUUID(namespace);
14
+ const nameBytes = new TextEncoder().encode(name);
15
+ const data = new Uint8Array(namespaceBytes.length + nameBytes.length);
16
+ data.set(namespaceBytes);
17
+ data.set(nameBytes, namespaceBytes.length);
18
+ const hashBuffer = await crypto.subtle.digest("SHA-1", data);
19
+ const bytes = new Uint8Array(hashBuffer);
20
+ bytes[6] = bytes[6] & 15 | 80;
21
+ bytes[8] = bytes[8] & 63 | 128;
22
+ const hex = [...bytes.slice(0, 16)].map((b) => b.toString(16).padStart(2, "0")).join("");
23
+ return [
24
+ hex.slice(0, 8),
25
+ hex.slice(8, 12),
26
+ hex.slice(12, 16),
27
+ hex.slice(16, 20),
28
+ hex.slice(20, 32)
29
+ ].join("-");
30
+ };
31
+
32
+ function summarizeBinary(value) {
33
+ if (value === null || typeof value !== "object") return void 0;
34
+ if (ArrayBuffer.isView(value)) {
35
+ return {
36
+ _type: value.constructor?.name ?? "TypedArray",
37
+ byteLength: value.byteLength
38
+ };
39
+ }
40
+ if (value instanceof ArrayBuffer) {
41
+ return { _type: "ArrayBuffer", byteLength: value.byteLength };
42
+ }
43
+ if (typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer) {
44
+ return { _type: "SharedArrayBuffer", byteLength: value.byteLength };
45
+ }
46
+ if (typeof File !== "undefined" && value instanceof File) {
47
+ return {
48
+ _type: "File",
49
+ name: value.name,
50
+ size: value.size,
51
+ type: value.type
52
+ };
53
+ }
54
+ if (typeof Blob !== "undefined" && value instanceof Blob) {
55
+ return { _type: "Blob", size: value.size, type: value.type };
56
+ }
57
+ if (typeof ReadableStream !== "undefined" && value instanceof ReadableStream) {
58
+ return { _type: "ReadableStream" };
59
+ }
60
+ return void 0;
61
+ }
62
+
63
+ const DEFAULT_LOG_BOUND_LIMITS = {
64
+ maxStringLength: 2048,
65
+ maxArrayLength: 100,
66
+ maxObjectKeys: 200,
67
+ maxDepth: 12
68
+ };
69
+ const MAX_LOG_BYTES = 64 * 1024;
70
+ const encoder = new TextEncoder();
71
+ function truncateString(value, max) {
72
+ if (value.length <= max) return value;
73
+ return `${value.slice(0, max)}\u2026[truncated, ${value.length} chars total]`;
74
+ }
75
+ function boundValue(value, limits, depth, seen) {
76
+ if (value === null || value === void 0) return value;
77
+ const type = typeof value;
78
+ if (type === "string")
79
+ return truncateString(value, limits.maxStringLength);
80
+ if (type !== "object") return value;
81
+ const binary = summarizeBinary(value);
82
+ if (binary) return binary;
83
+ const obj = value;
84
+ if (seen.has(obj)) return "[Circular]";
85
+ if (value instanceof Error) {
86
+ return {
87
+ name: value.name,
88
+ message: truncateString(value.message ?? "", limits.maxStringLength),
89
+ stack: truncateString(value.stack ?? "", limits.maxStringLength)
90
+ };
91
+ }
92
+ if (value instanceof Date || value instanceof RegExp || value instanceof URL) {
93
+ return value;
94
+ }
95
+ if (depth >= limits.maxDepth) return "[truncated: max depth]";
96
+ seen.add(obj);
97
+ if (Array.isArray(value)) {
98
+ const kept = value.slice(0, limits.maxArrayLength).map((item) => boundValue(item, limits, depth + 1, seen));
99
+ if (value.length > limits.maxArrayLength) {
100
+ kept.push(`\u2026[+${value.length - limits.maxArrayLength} more items]`);
101
+ }
102
+ return kept;
103
+ }
104
+ if (value instanceof Map) {
105
+ const out = /* @__PURE__ */ new Map();
106
+ let i = 0;
107
+ for (const [key, item] of value) {
108
+ if (i++ >= limits.maxObjectKeys) break;
109
+ out.set(key, boundValue(item, limits, depth + 1, seen));
110
+ }
111
+ if (value.size > limits.maxObjectKeys) {
112
+ out.set("\u2026", `[+${value.size - limits.maxObjectKeys} more entries]`);
113
+ }
114
+ return out;
115
+ }
116
+ if (value instanceof Set) {
117
+ const out = /* @__PURE__ */ new Set();
118
+ let i = 0;
119
+ for (const item of value) {
120
+ if (i++ >= limits.maxArrayLength) break;
121
+ out.add(boundValue(item, limits, depth + 1, seen));
122
+ }
123
+ if (value.size > limits.maxArrayLength) {
124
+ out.add(`\u2026[+${value.size - limits.maxArrayLength} more items]`);
125
+ }
126
+ return out;
127
+ }
128
+ const record = value;
129
+ const keys = Object.keys(record);
130
+ const result = {};
131
+ for (const key of keys.slice(0, limits.maxObjectKeys)) {
132
+ result[key] = boundValue(record[key], limits, depth + 1, seen);
133
+ }
134
+ if (keys.length > limits.maxObjectKeys) {
135
+ result["\u2026"] = `[+${keys.length - limits.maxObjectKeys} more keys]`;
136
+ }
137
+ return result;
138
+ }
139
+ function boundLogData(data, limits = DEFAULT_LOG_BOUND_LIMITS) {
140
+ return boundValue(data, limits, 0, /* @__PURE__ */ new WeakSet());
141
+ }
142
+ function serializeLog(data, limits = DEFAULT_LOG_BOUND_LIMITS) {
143
+ try {
144
+ const serialized = superjson.stringify(boundLogData(data, limits));
145
+ const byteLength = encoder.encode(serialized).length;
146
+ if (byteLength <= MAX_LOG_BYTES) return serialized;
147
+ return superjson.stringify({
148
+ _truncated: true,
149
+ reason: "log entry exceeded size cap",
150
+ bytes: byteLength,
151
+ preview: serialized.slice(0, 2048)
152
+ });
153
+ } catch (error) {
154
+ return superjson.stringify({
155
+ _logSerializationError: true,
156
+ message: error instanceof Error ? error.message : String(error)
157
+ });
158
+ }
159
+ }
160
+
161
+ export { serializeLog as a, uuidv5 as b, summarizeBinary as s, uuidv4 as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develit-io/backend-sdk",
3
- "version": "12.2.1",
3
+ "version": "12.3.0",
4
4
  "description": "Develit Backend SDK",
5
5
  "author": "Develit.io",
6
6
  "license": "ISC",
@@ -1,30 +0,0 @@
1
- const uuidv4 = () => crypto.randomUUID();
2
- function parseUUID(uuid) {
3
- const hex = uuid.replace(/-/g, "");
4
- const bytes = new Uint8Array(16);
5
- for (let i = 0; i < 16; i++) {
6
- bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
7
- }
8
- return bytes;
9
- }
10
- const uuidv5 = async (name, namespace) => {
11
- const namespaceBytes = parseUUID(namespace);
12
- const nameBytes = new TextEncoder().encode(name);
13
- const data = new Uint8Array(namespaceBytes.length + nameBytes.length);
14
- data.set(namespaceBytes);
15
- data.set(nameBytes, namespaceBytes.length);
16
- const hashBuffer = await crypto.subtle.digest("SHA-1", data);
17
- const bytes = new Uint8Array(hashBuffer);
18
- bytes[6] = bytes[6] & 15 | 80;
19
- bytes[8] = bytes[8] & 63 | 128;
20
- const hex = [...bytes.slice(0, 16)].map((b) => b.toString(16).padStart(2, "0")).join("");
21
- return [
22
- hex.slice(0, 8),
23
- hex.slice(8, 12),
24
- hex.slice(12, 16),
25
- hex.slice(16, 20),
26
- hex.slice(20, 32)
27
- ].join("-");
28
- };
29
-
30
- export { uuidv5 as a, uuidv4 as u };