@gravito/core 2.0.4 → 2.0.6

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.js CHANGED
@@ -1445,6 +1445,109 @@ var require_src = __commonJS((exports) => {
1445
1445
  trace: trace_api_1.trace
1446
1446
  };
1447
1447
  });
1448
+
1449
+ // src/compat/async-local-storage.ts
1450
+ var AsyncLocalStorageClass;
1451
+ var tryGetNodeAsyncHooks = () => {
1452
+ try {
1453
+ if (typeof window === "undefined" && typeof process !== "undefined" && true) {
1454
+ try {
1455
+ const module = __require("async_hooks");
1456
+ return module.AsyncLocalStorage;
1457
+ } catch (_e1) {
1458
+ try {
1459
+ return eval("require")("node:async_hooks").AsyncLocalStorage;
1460
+ } catch (_e2) {
1461
+ return null;
1462
+ }
1463
+ }
1464
+ }
1465
+ } catch (_e) {
1466
+ return null;
1467
+ }
1468
+ };
1469
+ AsyncLocalStorageClass = tryGetNodeAsyncHooks();
1470
+ if (!AsyncLocalStorageClass) {
1471
+ AsyncLocalStorageClass = class AsyncLocalStorage {
1472
+ store;
1473
+ run(store, fn) {
1474
+ const prev = this.store;
1475
+ this.store = store;
1476
+ try {
1477
+ return fn();
1478
+ } finally {
1479
+ this.store = prev;
1480
+ }
1481
+ }
1482
+ getStore() {
1483
+ return this.store;
1484
+ }
1485
+ disable() {
1486
+ this.store = undefined;
1487
+ }
1488
+ };
1489
+ }
1490
+ var AsyncLocalStorage = AsyncLocalStorageClass;
1491
+
1492
+ // src/compat/crypto.ts
1493
+ var randomUUIDFn;
1494
+ var randomBytesFn;
1495
+ var tryGetNodeCrypto = () => {
1496
+ try {
1497
+ if (typeof window === "undefined" && typeof process !== "undefined" && true) {
1498
+ return eval("require")("node:crypto");
1499
+ }
1500
+ } catch (_e) {
1501
+ return null;
1502
+ }
1503
+ };
1504
+ var nodeCrypto = tryGetNodeCrypto();
1505
+ if (nodeCrypto) {
1506
+ randomUUIDFn = nodeCrypto.randomUUID;
1507
+ randomBytesFn = nodeCrypto.randomBytes;
1508
+ } else {
1509
+ randomUUIDFn = () => {
1510
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1511
+ return globalThis.crypto.randomUUID();
1512
+ }
1513
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
1514
+ const r = Math.random() * 16 | 0;
1515
+ const v = c === "x" ? r : r & 3 | 8;
1516
+ return v.toString(16);
1517
+ });
1518
+ };
1519
+ randomBytesFn = (size) => {
1520
+ const bytes = new Uint8Array(size);
1521
+ if (typeof globalThis.crypto?.getRandomValues === "function") {
1522
+ globalThis.crypto.getRandomValues(bytes);
1523
+ } else {
1524
+ for (let i = 0;i < size; i++) {
1525
+ bytes[i] = Math.floor(Math.random() * 256);
1526
+ }
1527
+ }
1528
+ return {
1529
+ ...Array.from(bytes),
1530
+ length: size,
1531
+ [Symbol.iterator]: () => bytes[Symbol.iterator](),
1532
+ toString: (encoding) => {
1533
+ if (encoding === "base64") {
1534
+ let binary = "";
1535
+ for (let i = 0;i < bytes.byteLength; i++) {
1536
+ binary += String.fromCharCode(bytes[i]);
1537
+ }
1538
+ return btoa(binary);
1539
+ }
1540
+ if (encoding === "hex") {
1541
+ return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
1542
+ }
1543
+ return "";
1544
+ }
1545
+ };
1546
+ };
1547
+ }
1548
+ var randomUUID = randomUUIDFn;
1549
+ var randomBytes = randomBytesFn;
1550
+
1448
1551
  // src/ffi/hash-fallback.ts
1449
1552
  import { createHash, createHmac } from "crypto";
1450
1553
 
@@ -3197,7 +3300,7 @@ class Gravito {
3197
3300
  // package.json
3198
3301
  var package_default = {
3199
3302
  name: "@gravito/core",
3200
- version: "2.0.4",
3303
+ version: "2.0.6",
3201
3304
  description: "",
3202
3305
  module: "./dist/index.js",
3203
3306
  main: "./dist/index.js",
@@ -3206,8 +3309,8 @@ var package_default = {
3206
3309
  types: "./dist/index.d.ts",
3207
3310
  exports: {
3208
3311
  ".": {
3209
- browser: "./src/index.browser.ts",
3210
- bun: "./src/index.ts",
3312
+ browser: "./dist/index.browser.js",
3313
+ bun: "./dist/index.js",
3211
3314
  types: "./dist/index.d.ts",
3212
3315
  default: "./dist/index.js"
3213
3316
  },
@@ -3224,12 +3327,12 @@ var package_default = {
3224
3327
  default: "./dist/ffi/index.js"
3225
3328
  },
3226
3329
  "./compat/async-local-storage": {
3227
- browser: "./src/compat/async-local-storage.browser.ts",
3228
- default: "./src/compat/async-local-storage.ts"
3330
+ browser: "./dist/compat/async-local-storage.browser.js",
3331
+ default: "./dist/compat/async-local-storage.js"
3229
3332
  },
3230
3333
  "./compat/crypto": {
3231
- browser: "./src/compat/crypto.browser.ts",
3232
- default: "./src/compat/crypto.ts"
3334
+ browser: "./dist/compat/crypto.browser.js",
3335
+ default: "./dist/compat/crypto.js"
3233
3336
  }
3234
3337
  },
3235
3338
  files: [
@@ -3923,6 +4026,9 @@ import path2 from "path";
3923
4026
  import { pathToFileURL } from "url";
3924
4027
 
3925
4028
  // src/runtime/adapter-bun.ts
4029
+ function createEphemeralPort() {
4030
+ return Math.floor(Math.random() * 50000) + 1e4;
4031
+ }
3926
4032
  function createBunAdapter() {
3927
4033
  return {
3928
4034
  kind: "bun",
@@ -4130,11 +4236,26 @@ function createBunAdapter() {
4130
4236
  await fs2.rm(path2, { recursive: true, force: true });
4131
4237
  },
4132
4238
  serve(config2) {
4133
- return Bun.serve({
4134
- port: config2.port,
4135
- fetch: config2.fetch,
4136
- websocket: config2.websocket
4137
- });
4239
+ if (config2.port !== 0) {
4240
+ return Bun.serve({
4241
+ port: config2.port,
4242
+ fetch: config2.fetch,
4243
+ websocket: config2.websocket
4244
+ });
4245
+ }
4246
+ let lastError;
4247
+ for (let attempt = 0;attempt < 20; attempt++) {
4248
+ try {
4249
+ return Bun.serve({
4250
+ port: createEphemeralPort(),
4251
+ fetch: config2.fetch,
4252
+ websocket: config2.websocket
4253
+ });
4254
+ } catch (error) {
4255
+ lastError = error;
4256
+ }
4257
+ }
4258
+ throw lastError ?? new Error("[RuntimeAdapter] Failed to allocate an ephemeral port");
4138
4259
  }
4139
4260
  };
4140
4261
  }
@@ -5549,49 +5670,6 @@ class ConfigManager {
5549
5670
  }
5550
5671
  }
5551
5672
 
5552
- // src/compat/async-local-storage.ts
5553
- var AsyncLocalStorageClass;
5554
- var tryGetNodeAsyncHooks = () => {
5555
- try {
5556
- if (typeof window === "undefined" && typeof process !== "undefined" && true) {
5557
- try {
5558
- const module = __require("async_hooks");
5559
- return module.AsyncLocalStorage;
5560
- } catch (_e1) {
5561
- try {
5562
- return eval("require")("node:async_hooks").AsyncLocalStorage;
5563
- } catch (_e2) {
5564
- return null;
5565
- }
5566
- }
5567
- }
5568
- } catch (_e) {
5569
- return null;
5570
- }
5571
- };
5572
- AsyncLocalStorageClass = tryGetNodeAsyncHooks();
5573
- if (!AsyncLocalStorageClass) {
5574
- AsyncLocalStorageClass = class AsyncLocalStorage {
5575
- store = null;
5576
- run(store, fn) {
5577
- const prev = this.store;
5578
- this.store = store;
5579
- try {
5580
- return fn();
5581
- } finally {
5582
- this.store = prev;
5583
- }
5584
- }
5585
- getStore() {
5586
- return this.store || undefined;
5587
- }
5588
- disable() {
5589
- this.store = null;
5590
- }
5591
- };
5592
- }
5593
- var AsyncLocalStorage = AsyncLocalStorageClass;
5594
-
5595
5673
  // src/exceptions/CircularDependencyException.ts
5596
5674
  class CircularDependencyException extends Error {
5597
5675
  constructor(key, stack) {
@@ -8734,65 +8812,6 @@ class MigrationWarner {
8734
8812
  }
8735
8813
  }
8736
8814
 
8737
- // src/compat/crypto.ts
8738
- var randomUUIDFn;
8739
- var randomBytesFn;
8740
- var tryGetNodeCrypto = () => {
8741
- try {
8742
- if (typeof window === "undefined" && typeof process !== "undefined" && true) {
8743
- return eval("require")("node:crypto");
8744
- }
8745
- } catch (_e) {
8746
- return null;
8747
- }
8748
- };
8749
- var nodeCrypto = tryGetNodeCrypto();
8750
- if (nodeCrypto) {
8751
- randomUUIDFn = nodeCrypto.randomUUID;
8752
- randomBytesFn = nodeCrypto.randomBytes;
8753
- } else {
8754
- randomUUIDFn = () => {
8755
- if (typeof globalThis.crypto?.randomUUID === "function") {
8756
- return globalThis.crypto.randomUUID();
8757
- }
8758
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
8759
- const r = Math.random() * 16 | 0;
8760
- const v = c === "x" ? r : r & 3 | 8;
8761
- return v.toString(16);
8762
- });
8763
- };
8764
- randomBytesFn = (size) => {
8765
- const bytes = new Uint8Array(size);
8766
- if (typeof globalThis.crypto?.getRandomValues === "function") {
8767
- globalThis.crypto.getRandomValues(bytes);
8768
- } else {
8769
- for (let i = 0;i < size; i++) {
8770
- bytes[i] = Math.floor(Math.random() * 256);
8771
- }
8772
- }
8773
- return {
8774
- ...Array.from(bytes),
8775
- length: size,
8776
- [Symbol.iterator]: () => bytes[Symbol.iterator](),
8777
- toString: (encoding) => {
8778
- if (encoding === "base64") {
8779
- let binary = "";
8780
- for (let i = 0;i < bytes.byteLength; i++) {
8781
- binary += String.fromCharCode(bytes[i]);
8782
- }
8783
- return btoa(binary);
8784
- }
8785
- if (encoding === "hex") {
8786
- return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
8787
- }
8788
- return "";
8789
- }
8790
- };
8791
- };
8792
- }
8793
- var randomUUID = randomUUIDFn;
8794
- var randomBytes = randomBytesFn;
8795
-
8796
8815
  // src/reliability/RetryPolicy.ts
8797
8816
  class RetryEngine {
8798
8817
  calculateDelay(attemptCount, policy) {
@@ -12449,6 +12468,96 @@ class CompositeStrategy {
12449
12468
  return false;
12450
12469
  }
12451
12470
  }
12471
+ function createDefaultStrategies(_config) {
12472
+ return [new QueueDepthStrategy, new RateLimitStrategy, new PriorityRebalanceStrategy];
12473
+ }
12474
+ // src/events/MessageQueueBridge.ts
12475
+ class MessageQueueBridge {
12476
+ config;
12477
+ constructor(config3) {
12478
+ this.config = config3;
12479
+ if (!config3.hookManager) {
12480
+ throw new Error("MessageQueueBridge: hookManager is required");
12481
+ }
12482
+ if (!config3.eventBackend) {
12483
+ throw new Error("MessageQueueBridge: eventBackend is required");
12484
+ }
12485
+ if (!config3.dlqManager) {
12486
+ throw new Error("MessageQueueBridge: dlqManager is required");
12487
+ }
12488
+ }
12489
+ async dispatchWithQueue(eventName, args, options2) {
12490
+ const listeners = this.config.hookManager.getListeners(eventName);
12491
+ if (listeners.length === 0) {
12492
+ throw new Error(`[MessageQueueBridge] No listeners registered for event: ${eventName}`);
12493
+ }
12494
+ if (this.config.enableCircuitBreaker) {
12495
+ const breaker = this.config.hookManager.getCircuitBreaker?.(eventName);
12496
+ if (breaker?.getState?.() === "OPEN") {
12497
+ throw new Error(`[MessageQueueBridge] Circuit breaker is OPEN for event: ${eventName}`);
12498
+ }
12499
+ }
12500
+ const nowMs = Date.now();
12501
+ const task = {
12502
+ id: `queue-${nowMs}-${Math.random().toString(36).substr(2, 9)}`,
12503
+ hook: eventName,
12504
+ args,
12505
+ callbacks: listeners,
12506
+ options: options2 || {},
12507
+ createdAt: nowMs,
12508
+ enqueuedAt: nowMs,
12509
+ retryCount: 0
12510
+ };
12511
+ const jobId = task.id;
12512
+ await this.config.eventBackend.enqueue(task);
12513
+ console.info(`[MessageQueueBridge] Event "${eventName}" enqueued to Bull Queue (job: ${jobId})`);
12514
+ return jobId;
12515
+ }
12516
+ async processQueuedEvent(jobId, task) {
12517
+ let hasError = false;
12518
+ let lastError = null;
12519
+ for (const callback of task.callbacks) {
12520
+ try {
12521
+ await callback(task.args);
12522
+ } catch (error) {
12523
+ hasError = true;
12524
+ lastError = error;
12525
+ console.error(`[MessageQueueBridge] Callback failed for event "${task.hook}" (job: ${jobId}):`, error);
12526
+ }
12527
+ }
12528
+ if (hasError) {
12529
+ console.error(`[MessageQueueBridge] Event "${task.hook}" processing failed (job: ${jobId})`);
12530
+ throw lastError || new Error("Event processing failed");
12531
+ }
12532
+ console.info(`[MessageQueueBridge] Event "${task.hook}" processed successfully (job: ${jobId})`);
12533
+ }
12534
+ async handleJobFailure(jobId, task, error, retryCount = 0) {
12535
+ console.warn(`[MessageQueueBridge] Event "${task.hook}" failed after retries (job: ${jobId}), moving to DLQ`);
12536
+ try {
12537
+ const dlqId = await this.config.dlqManager.moveToDlq(task.hook, task.args, task.options, error, retryCount);
12538
+ console.info(`[MessageQueueBridge] Event moved to DLQ (dlq_id: ${dlqId})`);
12539
+ } catch (dlqError) {
12540
+ console.error(`[MessageQueueBridge] Failed to move event to DLQ:`, dlqError);
12541
+ }
12542
+ }
12543
+ async getEventStatus(eventId) {
12544
+ return {
12545
+ eventId,
12546
+ status: "pending",
12547
+ attempts: 0,
12548
+ createdAt: Date.now()
12549
+ };
12550
+ }
12551
+ getEventBackend() {
12552
+ return this.config.eventBackend;
12553
+ }
12554
+ getDLQManager() {
12555
+ return this.config.dlqManager;
12556
+ }
12557
+ getHookManager() {
12558
+ return this.config.hookManager;
12559
+ }
12560
+ }
12452
12561
  // src/events/RetryScheduler.ts
12453
12562
  class RetryScheduler {
12454
12563
  enabled;
@@ -13422,19 +13531,35 @@ class HttpTester {
13422
13531
  body
13423
13532
  });
13424
13533
  const response2 = await this.core.adapter.fetch(request);
13425
- const setCookie2 = response2.headers.get("Set-Cookie");
13426
- if (setCookie2) {
13427
- const parts = setCookie2.split(/,(?=[^;]+=[^;]+)/);
13428
- for (const part of parts) {
13429
- const cookiePair = part.split(";")[0].trim();
13430
- const [name, ...valueParts] = cookiePair.split("=");
13431
- if (name) {
13432
- this.cookies.set(name, valueParts.join("="));
13433
- }
13534
+ for (const setCookie2 of this.getSetCookieHeaders(response2.headers)) {
13535
+ const cookiePair = setCookie2.split(";")[0]?.trim();
13536
+ if (!cookiePair) {
13537
+ continue;
13538
+ }
13539
+ const [name, ...valueParts] = cookiePair.split("=");
13540
+ if (name) {
13541
+ this.cookies.set(name, valueParts.join("="));
13434
13542
  }
13435
13543
  }
13436
13544
  return new TestResponse(response2);
13437
13545
  }
13546
+ getSetCookieHeaders(headers) {
13547
+ const headersWithGetSetCookie = headers;
13548
+ const getSetCookie = headersWithGetSetCookie.getSetCookie?.();
13549
+ if (getSetCookie && getSetCookie.length > 0) {
13550
+ return getSetCookie;
13551
+ }
13552
+ const headerJson = headersWithGetSetCookie.toJSON?.();
13553
+ const jsonSetCookie = headerJson?.["set-cookie"];
13554
+ if (Array.isArray(jsonSetCookie)) {
13555
+ return jsonSetCookie;
13556
+ }
13557
+ if (typeof jsonSetCookie === "string") {
13558
+ return [jsonSetCookie];
13559
+ }
13560
+ const setCookie2 = headers.get("Set-Cookie");
13561
+ return setCookie2 ? [setCookie2] : [];
13562
+ }
13438
13563
  }
13439
13564
  function createHttpTester(core) {
13440
13565
  return new HttpTester(core);
@@ -13888,4 +14013,4 @@ export {
13888
14013
  Application
13889
14014
  };
13890
14015
 
13891
- //# debugId=E93F9E403DD7FBA964756E2164756E21
14016
+ //# debugId=B420456F9068F67364756E2164756E21