@gravito/ripple 4.0.1 → 4.0.3
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 +26 -10
- package/dist/{ripple/src/OrbitRipple.d.ts → OrbitRipple.d.ts} +1 -0
- package/dist/{ripple/src/RippleServer.d.ts → RippleServer.d.ts} +32 -1
- package/dist/core/src/Application.d.ts +41 -0
- package/dist/core/src/HookManager.d.ts +33 -189
- package/dist/core/src/PlanetCore.d.ts +25 -1
- package/dist/core/src/Router.d.ts +32 -46
- package/dist/core/src/adapters/bun/BunContext.d.ts +6 -1
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +35 -0
- package/dist/core/src/adapters/bun/BunWebSocketHandler.d.ts +48 -0
- package/dist/core/src/adapters/bun/RadixRouter.d.ts +1 -0
- package/dist/core/src/adapters/bun/index.d.ts +7 -0
- package/dist/core/src/binary/BinaryUtils.d.ts +105 -0
- package/dist/core/src/binary/index.d.ts +5 -0
- package/dist/core/src/compat/async-local-storage.d.ts +7 -0
- package/dist/core/src/compat/crypto.d.ts +6 -0
- package/dist/core/src/engine/AOTRouter.d.ts +10 -0
- package/dist/core/src/engine/FastContext.d.ts +19 -1
- package/dist/core/src/engine/Gravito.d.ts +13 -18
- package/dist/core/src/engine/MinimalContext.d.ts +2 -0
- package/dist/core/src/engine/analyzer.d.ts +92 -6
- package/dist/core/src/engine/types.d.ts +3 -0
- package/dist/core/src/events/EventPriorityQueue.d.ts +18 -256
- package/dist/core/src/events/index.d.ts +12 -11
- package/dist/core/src/events/queue-core.d.ts +77 -0
- package/dist/core/src/events/task-executor.d.ts +51 -0
- package/dist/core/src/ffi/NativeAccelerator.d.ts +62 -0
- package/dist/core/src/ffi/NativeHasher.d.ts +139 -0
- package/dist/core/src/ffi/cbor-fallback.d.ts +96 -0
- package/dist/core/src/ffi/hash-fallback.d.ts +33 -0
- package/dist/core/src/ffi/index.d.ts +10 -0
- package/dist/core/src/ffi/types.d.ts +135 -0
- package/dist/core/src/helpers/Str.d.ts +15 -0
- package/dist/core/src/hooks/ActionManager.d.ts +132 -0
- package/dist/core/src/hooks/AsyncDetector.d.ts +84 -0
- package/dist/core/src/hooks/FilterManager.d.ts +71 -0
- package/dist/core/src/hooks/MigrationWarner.d.ts +24 -0
- package/dist/core/src/hooks/dlq-operations.d.ts +60 -0
- package/dist/core/src/hooks/types.d.ts +107 -0
- package/dist/core/src/http/CookieJar.d.ts +2 -2
- package/dist/core/src/http/types.d.ts +24 -3
- package/dist/core/src/index.d.ts +491 -14
- package/dist/core/src/observability/contracts.d.ts +137 -0
- package/dist/core/src/reliability/DeadLetterQueueManager.d.ts +2 -3
- package/dist/core/src/runtime/adapter-bun.d.ts +12 -0
- package/dist/core/src/runtime/adapter-deno.d.ts +12 -0
- package/dist/core/src/runtime/adapter-node.d.ts +12 -0
- package/dist/core/src/runtime/adapter-unknown.d.ts +13 -0
- package/dist/core/src/runtime/archive.d.ts +17 -0
- package/dist/core/src/runtime/compression.d.ts +21 -0
- package/dist/core/src/runtime/deep-equals.d.ts +56 -0
- package/dist/core/src/runtime/detection.d.ts +22 -0
- package/dist/core/src/runtime/escape.d.ts +34 -0
- package/dist/core/src/runtime/index.d.ts +44 -0
- package/dist/core/src/runtime/markdown.d.ts +44 -0
- package/dist/core/src/runtime/types.d.ts +436 -0
- package/dist/core/src/runtime-helpers.d.ts +67 -0
- package/dist/core/src/runtime.d.ts +10 -118
- package/dist/core/src/testing/HttpTester.d.ts +1 -0
- package/dist/core/src/transpiler-utils.d.ts +170 -0
- package/dist/{ripple/src/engines → engines}/BunEngine.d.ts +11 -0
- package/dist/index.js +1 -7502
- package/dist/index.js.map +3 -71
- package/package.json +6 -2
- package/dist/atlas/src/DB.d.ts +0 -348
- package/dist/atlas/src/OrbitAtlas.d.ts +0 -9
- package/dist/atlas/src/config/defineConfig.d.ts +0 -14
- package/dist/atlas/src/config/index.d.ts +0 -7
- package/dist/atlas/src/config/loadConfig.d.ts +0 -41
- package/dist/atlas/src/connection/Connection.d.ts +0 -112
- package/dist/atlas/src/connection/ConnectionManager.d.ts +0 -180
- package/dist/atlas/src/connection/ReplicaConnectionPool.d.ts +0 -54
- package/dist/atlas/src/drivers/BunSQLDriver.d.ts +0 -32
- package/dist/atlas/src/drivers/BunSQLPreparedStatement.d.ts +0 -118
- package/dist/atlas/src/drivers/MongoDBDriver.d.ts +0 -36
- package/dist/atlas/src/drivers/MySQLDriver.d.ts +0 -79
- package/dist/atlas/src/drivers/PostgresDriver.d.ts +0 -96
- package/dist/atlas/src/drivers/RedisDriver.d.ts +0 -43
- package/dist/atlas/src/drivers/SQLiteDriver.d.ts +0 -45
- package/dist/atlas/src/drivers/types.d.ts +0 -260
- package/dist/atlas/src/errors/index.d.ts +0 -45
- package/dist/atlas/src/grammar/Grammar.d.ts +0 -342
- package/dist/atlas/src/grammar/MongoGrammar.d.ts +0 -47
- package/dist/atlas/src/grammar/MySQLGrammar.d.ts +0 -54
- package/dist/atlas/src/grammar/NullGrammar.d.ts +0 -35
- package/dist/atlas/src/grammar/PostgresGrammar.d.ts +0 -62
- package/dist/atlas/src/grammar/SQLiteGrammar.d.ts +0 -32
- package/dist/atlas/src/index.d.ts +0 -79
- package/dist/atlas/src/migration/Migration.d.ts +0 -64
- package/dist/atlas/src/migration/MigrationRepository.d.ts +0 -65
- package/dist/atlas/src/migration/Migrator.d.ts +0 -110
- package/dist/atlas/src/migration/index.d.ts +0 -6
- package/dist/atlas/src/observability/AtlasMetrics.d.ts +0 -33
- package/dist/atlas/src/observability/AtlasObservability.d.ts +0 -15
- package/dist/atlas/src/observability/AtlasTracer.d.ts +0 -12
- package/dist/atlas/src/observability/index.d.ts +0 -9
- package/dist/atlas/src/orm/Repository.d.ts +0 -247
- package/dist/atlas/src/orm/index.d.ts +0 -6
- package/dist/atlas/src/orm/model/DirtyTracker.d.ts +0 -121
- package/dist/atlas/src/orm/model/Model.d.ts +0 -458
- package/dist/atlas/src/orm/model/ModelRegistry.d.ts +0 -20
- package/dist/atlas/src/orm/model/concerns/HasAttributes.d.ts +0 -150
- package/dist/atlas/src/orm/model/concerns/HasEvents.d.ts +0 -36
- package/dist/atlas/src/orm/model/concerns/HasPersistence.d.ts +0 -92
- package/dist/atlas/src/orm/model/concerns/HasRelationships.d.ts +0 -117
- package/dist/atlas/src/orm/model/concerns/HasSerialization.d.ts +0 -64
- package/dist/atlas/src/orm/model/concerns/applyMixins.d.ts +0 -15
- package/dist/atlas/src/orm/model/concerns/index.d.ts +0 -12
- package/dist/atlas/src/orm/model/decorators.d.ts +0 -138
- package/dist/atlas/src/orm/model/errors.d.ts +0 -52
- package/dist/atlas/src/orm/model/index.d.ts +0 -10
- package/dist/atlas/src/orm/model/relationships.d.ts +0 -207
- package/dist/atlas/src/orm/model/types.d.ts +0 -12
- package/dist/atlas/src/orm/schema/SchemaRegistry.d.ts +0 -124
- package/dist/atlas/src/orm/schema/SchemaSniffer.d.ts +0 -54
- package/dist/atlas/src/orm/schema/index.d.ts +0 -6
- package/dist/atlas/src/orm/schema/types.d.ts +0 -85
- package/dist/atlas/src/pool/AdaptivePoolManager.d.ts +0 -98
- package/dist/atlas/src/pool/PoolHealthChecker.d.ts +0 -91
- package/dist/atlas/src/pool/PoolStrategy.d.ts +0 -129
- package/dist/atlas/src/pool/PoolWarmer.d.ts +0 -92
- package/dist/atlas/src/query/Expression.d.ts +0 -60
- package/dist/atlas/src/query/NPlusOneDetector.d.ts +0 -10
- package/dist/atlas/src/query/QueryBuilder.d.ts +0 -643
- package/dist/atlas/src/query/RelationshipResolver.d.ts +0 -23
- package/dist/atlas/src/query/clauses/GroupByClause.d.ts +0 -51
- package/dist/atlas/src/query/clauses/HavingClause.d.ts +0 -70
- package/dist/atlas/src/query/clauses/JoinClause.d.ts +0 -87
- package/dist/atlas/src/query/clauses/LimitClause.d.ts +0 -82
- package/dist/atlas/src/query/clauses/OrderByClause.d.ts +0 -69
- package/dist/atlas/src/query/clauses/SelectClause.d.ts +0 -71
- package/dist/atlas/src/query/clauses/WhereClause.d.ts +0 -167
- package/dist/atlas/src/query/clauses/index.d.ts +0 -11
- package/dist/atlas/src/schema/Blueprint.d.ts +0 -276
- package/dist/atlas/src/schema/ColumnDefinition.d.ts +0 -154
- package/dist/atlas/src/schema/ForeignKeyDefinition.d.ts +0 -37
- package/dist/atlas/src/schema/MigrationGenerator.d.ts +0 -45
- package/dist/atlas/src/schema/Schema.d.ts +0 -131
- package/dist/atlas/src/schema/SchemaDiff.d.ts +0 -73
- package/dist/atlas/src/schema/TypeGenerator.d.ts +0 -57
- package/dist/atlas/src/schema/TypeWriter.d.ts +0 -42
- package/dist/atlas/src/schema/grammars/MySQLSchemaGrammar.d.ts +0 -23
- package/dist/atlas/src/schema/grammars/PostgresSchemaGrammar.d.ts +0 -26
- package/dist/atlas/src/schema/grammars/SQLiteSchemaGrammar.d.ts +0 -28
- package/dist/atlas/src/schema/grammars/SchemaGrammar.d.ts +0 -97
- package/dist/atlas/src/schema/grammars/index.d.ts +0 -7
- package/dist/atlas/src/schema/index.d.ts +0 -8
- package/dist/atlas/src/seed/Factory.d.ts +0 -90
- package/dist/atlas/src/seed/Seeder.d.ts +0 -28
- package/dist/atlas/src/seed/SeederRunner.d.ts +0 -74
- package/dist/atlas/src/seed/index.d.ts +0 -6
- package/dist/atlas/src/sharding/ShardingManager.d.ts +0 -59
- package/dist/atlas/src/types/index.d.ts +0 -1182
- package/dist/atlas/src/utils/CursorEncoding.d.ts +0 -63
- package/dist/atlas/src/utils/levenshtein.d.ts +0 -9
- package/dist/core/src/adapters/PhotonAdapter.d.ts +0 -175
- package/dist/core/src/adapters/photon-types.d.ts +0 -73
- package/dist/core/src/http/middleware/BodySizeLimit.d.ts +0 -16
- package/dist/core/src/http/middleware/Cors.d.ts +0 -24
- package/dist/core/src/http/middleware/Csrf.d.ts +0 -23
- package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +0 -28
- package/dist/core/src/http/middleware/SecurityHeaders.d.ts +0 -29
- package/dist/core/src/http/middleware/ThrottleRequests.d.ts +0 -18
- package/dist/core/src/instrumentation/index.d.ts +0 -35
- package/dist/core/src/instrumentation/opentelemetry.d.ts +0 -178
- package/dist/core/src/instrumentation/types.d.ts +0 -182
- package/dist/core/src/observability/Metrics.d.ts +0 -244
- package/dist/photon/src/index.d.ts +0 -89
- package/dist/photon/src/middleware/binary.d.ts +0 -31
- package/dist/photon/src/middleware/htmx.d.ts +0 -39
- package/dist/photon/src/middleware/ratelimit-redis.d.ts +0 -50
- package/dist/photon/src/middleware/ratelimit.d.ts +0 -161
- package/dist/photon/src/openapi.d.ts +0 -19
- /package/dist/{ripple/src/channels → channels}/Channel.d.ts +0 -0
- /package/dist/{ripple/src/channels → channels}/ChannelManager.d.ts +0 -0
- /package/dist/{ripple/src/channels → channels}/index.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/LocalDriver.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/NATSDriver.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/RedisDriver.d.ts +0 -0
- /package/dist/{ripple/src/drivers → drivers}/index.d.ts +0 -0
- /package/dist/{ripple/src/engines → engines}/IRippleEngine.d.ts +0 -0
- /package/dist/{ripple/src/engines → engines}/UWebSocketsEngine.d.ts +0 -0
- /package/dist/{ripple/src/engines → engines}/WsEngine.d.ts +0 -0
- /package/dist/{ripple/src/engines → engines}/index.d.ts +0 -0
- /package/dist/{ripple/src/errors → errors}/RippleError.d.ts +0 -0
- /package/dist/{ripple/src/errors → errors}/index.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/BroadcastEvent.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/BroadcastManager.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/Broadcaster.d.ts +0 -0
- /package/dist/{ripple/src/events → events}/index.d.ts +0 -0
- /package/dist/{ripple/src/health → health}/HealthChecker.d.ts +0 -0
- /package/dist/{ripple/src/health → health}/index.d.ts +0 -0
- /package/dist/{ripple/src/index.d.ts → index.d.ts} +0 -0
- /package/dist/{ripple/src/logging → logging}/Logger.d.ts +0 -0
- /package/dist/{ripple/src/logging → logging}/index.d.ts +0 -0
- /package/dist/{ripple/src/middleware → middleware}/InterceptorManager.d.ts +0 -0
- /package/dist/{ripple/src/observability → observability}/RippleMetrics.d.ts +0 -0
- /package/dist/{ripple/src/reliability → reliability}/AckManager.d.ts +0 -0
- /package/dist/{ripple/src/serializers → serializers}/ISerializer.d.ts +0 -0
- /package/dist/{ripple/src/serializers → serializers}/JsonSerializer.d.ts +0 -0
- /package/dist/{ripple/src/serializers → serializers}/ProtobufSerializer.d.ts +0 -0
- /package/dist/{ripple/src/serializers → serializers}/index.d.ts +0 -0
- /package/dist/{ripple/src/tracking → tracking}/ConnectionTracker.d.ts +0 -0
- /package/dist/{ripple/src/tracking → tracking}/SessionManager.d.ts +0 -0
- /package/dist/{ripple/src/tracking → tracking}/index.d.ts +0 -0
- /package/dist/{ripple/src/types.d.ts → types.d.ts} +0 -0
- /package/dist/{ripple/src/utils → utils}/MessageSerializer.d.ts +0 -0
- /package/dist/{ripple/src/utils → utils}/TokenBucket.d.ts +0 -0
- /package/dist/{ripple/src/utils → utils}/index.d.ts +0 -0
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type { Span } from '@opentelemetry/api';
|
|
2
2
|
import type { ActionCallback } from '../HookManager';
|
|
3
|
-
import { BackpressureManager } from './BackpressureManager';
|
|
4
|
-
import { CircuitBreaker } from './CircuitBreaker';
|
|
3
|
+
import type { BackpressureManager } from './BackpressureManager';
|
|
5
4
|
import type { DeadLetterQueue } from './DeadLetterQueue';
|
|
6
5
|
import type { EventBackend } from './EventBackend';
|
|
7
6
|
import type { EventOptions } from './EventOptions';
|
|
8
7
|
import type { EventMetrics } from './observability/EventMetrics';
|
|
9
8
|
import type { EventTracing } from './observability/EventTracing';
|
|
10
9
|
import type { OTelEventMetrics } from './observability/OTelEventMetrics';
|
|
11
|
-
import {
|
|
10
|
+
import type { PriorityStatistics } from './PriorityEscalationManager';
|
|
12
11
|
import type { RetryScheduler } from './RetryScheduler';
|
|
13
12
|
import type { BackpressureStrategy, EventQueueConfig, EventTask, MultiPriorityQueueDepth } from './types';
|
|
14
13
|
import type { WorkerPool } from './WorkerPool';
|
|
@@ -23,279 +22,42 @@ export type { EventTask, EventQueueConfig, BackpressureStrategy };
|
|
|
23
22
|
*
|
|
24
23
|
* Supports automatic priority escalation based on wait time.
|
|
25
24
|
*
|
|
25
|
+
* Composed of:
|
|
26
|
+
* - {@link QueueCore}: queue data structures and backpressure logic
|
|
27
|
+
* - {@link TaskExecutor}: task execution, retry, circuit breaker, DLQ
|
|
28
|
+
*
|
|
26
29
|
* @internal
|
|
27
30
|
*/
|
|
28
31
|
export declare class EventPriorityQueue implements EventBackend {
|
|
29
|
-
private
|
|
30
|
-
private
|
|
31
|
-
private normalPriority;
|
|
32
|
-
private lowPriority;
|
|
32
|
+
private queueCore;
|
|
33
|
+
private taskExecutor;
|
|
33
34
|
private processing;
|
|
34
|
-
private taskIdCounter;
|
|
35
|
-
private dlq?;
|
|
36
|
-
private persistentDLQHandler?;
|
|
37
|
-
private config;
|
|
38
|
-
private processingPartitions;
|
|
39
|
-
private eventCircuitBreakers;
|
|
40
|
-
private eventMetrics?;
|
|
41
|
-
private otelEventMetrics?;
|
|
42
|
-
private eventTracing?;
|
|
43
|
-
private currentDispatchSpan?;
|
|
44
|
-
private backpressureManager?;
|
|
45
35
|
private workerPool?;
|
|
46
|
-
private retryScheduler?;
|
|
47
|
-
private priorityStats?;
|
|
48
36
|
constructor(config?: EventQueueConfig);
|
|
49
|
-
/**
|
|
50
|
-
* Set the Dead Letter Queue for failed events.
|
|
51
|
-
*
|
|
52
|
-
* @param dlq - Dead Letter Queue instance
|
|
53
|
-
*/
|
|
54
37
|
setDeadLetterQueue(dlq: DeadLetterQueue): void;
|
|
55
|
-
/**
|
|
56
|
-
* Set the persistent DLQ handler for failed events.
|
|
57
|
-
*
|
|
58
|
-
* @param handler - Async handler function for persistent DLQ
|
|
59
|
-
*/
|
|
60
38
|
setPersistentDLQHandler(handler: (hook: string, args: unknown, options: EventOptions, error: Error, retryCount: number, firstFailedAt: number) => Promise<void>): void;
|
|
61
|
-
/**
|
|
62
|
-
* Set the EventMetrics instance for recording circuit breaker metrics.
|
|
63
|
-
*
|
|
64
|
-
* @param metrics - EventMetrics instance
|
|
65
|
-
* @internal
|
|
66
|
-
*/
|
|
67
39
|
setEventMetrics(metrics: EventMetrics): void;
|
|
68
|
-
/**
|
|
69
|
-
* Set the OTelEventMetrics instance for recording DLQ and backpressure metrics.
|
|
70
|
-
*
|
|
71
|
-
* @param metrics - OTelEventMetrics instance
|
|
72
|
-
* @internal
|
|
73
|
-
*/
|
|
74
40
|
setOTelEventMetrics(metrics: OTelEventMetrics): void;
|
|
75
|
-
/**
|
|
76
|
-
* Set the PriorityStatistics instance for tracking priority distribution.
|
|
77
|
-
*
|
|
78
|
-
* @param stats - PriorityStatistics instance
|
|
79
|
-
* @internal
|
|
80
|
-
*/
|
|
81
41
|
setPriorityStatistics(stats: PriorityStatistics): void;
|
|
82
|
-
/**
|
|
83
|
-
* Get the PriorityStatistics instance.
|
|
84
|
-
*
|
|
85
|
-
* @returns PriorityStatistics instance or undefined
|
|
86
|
-
* @internal
|
|
87
|
-
*/
|
|
88
42
|
getPriorityStatistics(): PriorityStatistics | undefined;
|
|
89
|
-
/**
|
|
90
|
-
* Set the EventTracing instance for distributed tracing.
|
|
91
|
-
*
|
|
92
|
-
* @param tracing - EventTracing instance
|
|
93
|
-
* @internal
|
|
94
|
-
*/
|
|
95
43
|
setEventTracing(tracing: EventTracing): void;
|
|
96
|
-
/**
|
|
97
|
-
* Set the current dispatch span for creating child spans.
|
|
98
|
-
*
|
|
99
|
-
* @param span - Parent dispatch span
|
|
100
|
-
* @internal
|
|
101
|
-
*/
|
|
102
44
|
setCurrentDispatchSpan(span: Span | undefined): void;
|
|
103
|
-
/**
|
|
104
|
-
* Get the current dispatch span.
|
|
105
|
-
*
|
|
106
|
-
* @returns Current dispatch span or undefined
|
|
107
|
-
* @internal
|
|
108
|
-
*/
|
|
109
45
|
getCurrentDispatchSpan(): Span | undefined;
|
|
110
|
-
/**
|
|
111
|
-
* Set the RetryScheduler for async distributed retries.
|
|
112
|
-
*
|
|
113
|
-
* @param scheduler - RetryScheduler instance
|
|
114
|
-
* @internal
|
|
115
|
-
*/
|
|
116
46
|
setRetryScheduler(scheduler: RetryScheduler): void;
|
|
117
|
-
/**
|
|
118
|
-
* Get the RetryScheduler instance.
|
|
119
|
-
*
|
|
120
|
-
* @returns RetryScheduler instance or undefined
|
|
121
|
-
* @internal
|
|
122
|
-
*/
|
|
123
47
|
getRetryScheduler(): RetryScheduler | undefined;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
* @internal
|
|
131
|
-
*/
|
|
132
|
-
private getOrCreateEventCircuitBreaker;
|
|
133
|
-
/**
|
|
134
|
-
* Enqueue an event task for processing.
|
|
135
|
-
*
|
|
136
|
-
* @param hook - Event hook name
|
|
137
|
-
* @param args - Event arguments
|
|
138
|
-
* @param callbacks - Callbacks to execute
|
|
139
|
-
* @param options - Event options
|
|
140
|
-
* @returns Task ID
|
|
141
|
-
*/
|
|
48
|
+
getCircuitBreaker(hook: string): import("@gravito/core").CircuitBreaker | undefined;
|
|
49
|
+
getCircuitBreakers(): Map<string, import("@gravito/core").CircuitBreaker>;
|
|
50
|
+
resetCircuitBreaker(hook: string): boolean;
|
|
51
|
+
setWorkerPool(pool: WorkerPool): void;
|
|
52
|
+
getWorkerPool(): WorkerPool | undefined;
|
|
53
|
+
getBackpressureManager(): BackpressureManager | undefined;
|
|
142
54
|
enqueue(task: EventTask): string;
|
|
143
55
|
enqueue(hook: string, args: unknown, callbacks: ActionCallback[], options: EventOptions): string;
|
|
144
|
-
|
|
145
|
-
* Handle backpressure when queue is full.
|
|
146
|
-
* @returns True if space was made, false if event should be dropped
|
|
147
|
-
*/
|
|
148
|
-
private handleBackpressure;
|
|
149
|
-
/**
|
|
150
|
-
* Drop the oldest event from the queue, prioritizing low priority events.
|
|
151
|
-
* Drops in order: LOW → NORMAL → HIGH → CRITICAL
|
|
152
|
-
*/
|
|
153
|
-
private dropOldest;
|
|
154
|
-
/**
|
|
155
|
-
* Process the next task in the queue.
|
|
156
|
-
* Tasks are processed in priority order: high > normal > low
|
|
157
|
-
* If WorkerPool is configured, tasks are submitted to the pool for concurrent execution.
|
|
158
|
-
*
|
|
159
|
-
* @internal
|
|
160
|
-
*/
|
|
161
|
-
private processNext;
|
|
162
|
-
/**
|
|
163
|
-
* Dequeue the next task based on priority and partition ordering.
|
|
164
|
-
* Priority order: CRITICAL > HIGH > NORMAL > LOW
|
|
165
|
-
*
|
|
166
|
-
* @returns Next task to process, or undefined if queue is empty
|
|
167
|
-
* @internal
|
|
168
|
-
*/
|
|
169
|
-
private dequeue;
|
|
170
|
-
/**
|
|
171
|
-
* Dequeue a task from a priority queue, respecting partition ordering.
|
|
172
|
-
*
|
|
173
|
-
* @param queue - Priority queue to dequeue from
|
|
174
|
-
* @returns Next task to process, or undefined if all tasks are blocked by partition locks
|
|
175
|
-
* @internal
|
|
176
|
-
*/
|
|
177
|
-
private dequeueFromPriority;
|
|
178
|
-
/**
|
|
179
|
-
* Execute an event task by running all its callbacks.
|
|
180
|
-
* Implements circuit breaker protection, retry logic with exponential backoff, and DLQ integration.
|
|
181
|
-
* Also handles partition ordering by acquiring and releasing partition locks.
|
|
182
|
-
*
|
|
183
|
-
* @param task - Event task to execute
|
|
184
|
-
* @internal
|
|
185
|
-
*/
|
|
186
|
-
private executeTask;
|
|
187
|
-
/**
|
|
188
|
-
* Calculate retry delay based on backoff strategy.
|
|
189
|
-
*
|
|
190
|
-
* @param retryCount - Current retry attempt number
|
|
191
|
-
* @param backoff - Backoff strategy
|
|
192
|
-
* @param initialDelay - Initial delay in ms
|
|
193
|
-
* @param maxDelay - Maximum delay in ms
|
|
194
|
-
* @returns Delay in milliseconds
|
|
195
|
-
* @internal
|
|
196
|
-
*/
|
|
197
|
-
private calculateRetryDelay;
|
|
198
|
-
/**
|
|
199
|
-
* Re-enqueue a task for retry.
|
|
200
|
-
*
|
|
201
|
-
* @param task - Task to retry
|
|
202
|
-
* @internal
|
|
203
|
-
*/
|
|
204
|
-
private enqueueRetry;
|
|
205
|
-
/**
|
|
206
|
-
* Execute a callback with timeout.
|
|
207
|
-
*
|
|
208
|
-
* @param callback - Callback to execute
|
|
209
|
-
* @param args - Arguments to pass to callback
|
|
210
|
-
* @param timeoutMs - Timeout in milliseconds
|
|
211
|
-
* @internal
|
|
212
|
-
*/
|
|
213
|
-
private executeWithTimeout;
|
|
214
|
-
/**
|
|
215
|
-
* Get the current depth of the queue.
|
|
216
|
-
*
|
|
217
|
-
* @returns Total number of tasks in the queue
|
|
218
|
-
*/
|
|
56
|
+
enqueueBatch(tasks: EventTask[]): string[];
|
|
219
57
|
getDepth(): number;
|
|
220
|
-
/**
|
|
221
|
-
* Get the depth of a specific priority queue.
|
|
222
|
-
*
|
|
223
|
-
* @param priority - Priority level
|
|
224
|
-
* @returns Number of tasks in the specified priority queue
|
|
225
|
-
*/
|
|
226
58
|
getDepthByPriority(priority: 'critical' | 'high' | 'normal' | 'low'): number;
|
|
227
|
-
/**
|
|
228
|
-
* Get the BackpressureManager instance if enabled.
|
|
229
|
-
*
|
|
230
|
-
* @returns BackpressureManager instance or undefined if not enabled
|
|
231
|
-
* @internal
|
|
232
|
-
*/
|
|
233
|
-
getBackpressureManager(): BackpressureManager | undefined;
|
|
234
|
-
/**
|
|
235
|
-
* Set the WorkerPool for concurrent task execution.
|
|
236
|
-
*
|
|
237
|
-
* @param pool - WorkerPool instance
|
|
238
|
-
* @internal
|
|
239
|
-
*/
|
|
240
|
-
setWorkerPool(pool: WorkerPool): void;
|
|
241
|
-
/**
|
|
242
|
-
* Get the WorkerPool instance if set.
|
|
243
|
-
*
|
|
244
|
-
* @returns WorkerPool instance or undefined if not set
|
|
245
|
-
* @internal
|
|
246
|
-
*/
|
|
247
|
-
getWorkerPool(): WorkerPool | undefined;
|
|
248
|
-
/**
|
|
249
|
-
* Clear all tasks from the queue.
|
|
250
|
-
*/
|
|
251
|
-
clear(): void;
|
|
252
|
-
/**
|
|
253
|
-
* Get circuit breaker for an event hook.
|
|
254
|
-
*
|
|
255
|
-
* @param hook - Event hook name
|
|
256
|
-
* @returns Circuit breaker instance or undefined
|
|
257
|
-
* @internal
|
|
258
|
-
*/
|
|
259
|
-
getCircuitBreaker(hook: string): CircuitBreaker | undefined;
|
|
260
|
-
/**
|
|
261
|
-
* Get all circuit breakers.
|
|
262
|
-
*
|
|
263
|
-
* @returns Map of circuit breakers keyed by hook name
|
|
264
|
-
* @internal
|
|
265
|
-
*/
|
|
266
|
-
getCircuitBreakers(): Map<string, CircuitBreaker>;
|
|
267
|
-
/**
|
|
268
|
-
* Reset a circuit breaker for an event hook.
|
|
269
|
-
*
|
|
270
|
-
* @param hook - Event hook name
|
|
271
|
-
* @returns True if reset, false if circuit breaker not found
|
|
272
|
-
* @internal
|
|
273
|
-
*/
|
|
274
|
-
resetCircuitBreaker(hook: string): boolean;
|
|
275
|
-
/**
|
|
276
|
-
* Batch enqueue multiple tasks (FS-102).
|
|
277
|
-
* Supports aggregation layer batch submission.
|
|
278
|
-
*
|
|
279
|
-
* @param tasks - Array of tasks to enqueue
|
|
280
|
-
* @returns Array of task IDs
|
|
281
|
-
* @internal
|
|
282
|
-
*/
|
|
283
|
-
enqueueBatch(tasks: EventTask[]): string[];
|
|
284
|
-
/**
|
|
285
|
-
* Get queue depth for each priority level (FS-103).
|
|
286
|
-
* Used by BackpressureManager for multi-priority depth monitoring.
|
|
287
|
-
*
|
|
288
|
-
* @returns Queue depth snapshot with depths for each priority
|
|
289
|
-
* @internal
|
|
290
|
-
*/
|
|
291
59
|
getQueueDepthByPriority(): MultiPriorityQueueDepth;
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
* synchronized with real-time queue state.
|
|
296
|
-
*
|
|
297
|
-
* @private
|
|
298
|
-
* @internal
|
|
299
|
-
*/
|
|
300
|
-
private syncBackpressure;
|
|
60
|
+
clear(): void;
|
|
61
|
+
dequeue(): EventTask | undefined;
|
|
62
|
+
private processNext;
|
|
301
63
|
}
|
|
@@ -2,24 +2,25 @@
|
|
|
2
2
|
* Event system exports for async event dispatch.
|
|
3
3
|
* @packageDocumentation
|
|
4
4
|
*/
|
|
5
|
+
export * from './EventBackend';
|
|
6
|
+
export type { EventOptions } from './EventOptions';
|
|
7
|
+
export { DEFAULT_EVENT_OPTIONS } from './EventOptions';
|
|
8
|
+
export type { BackpressureStrategy, EventQueueConfig, EventTask } from './types';
|
|
5
9
|
export type { BackpressureConfig, BackpressureDecision, BackpressureMetricsSnapshot, } from './BackpressureManager';
|
|
6
10
|
export { BackpressureManager, BackpressureState } from './BackpressureManager';
|
|
7
|
-
export type { CircuitBreakerOptions } from './CircuitBreaker';
|
|
11
|
+
export type { CircuitBreakerMetrics, CircuitBreakerMetricsRecorder, CircuitBreakerOptions, } from './CircuitBreaker';
|
|
8
12
|
export { CircuitBreaker, CircuitBreakerState } from './CircuitBreaker';
|
|
9
|
-
export type { DLQEntry, DLQFilter } from './DeadLetterQueue';
|
|
13
|
+
export type { DLQEntry, DLQEntryCallback, DLQEntrySource, DLQFilter } from './DeadLetterQueue';
|
|
10
14
|
export { DeadLetterQueue } from './DeadLetterQueue';
|
|
11
|
-
export * from './EventBackend';
|
|
12
|
-
export type { EventOptions } from './EventOptions';
|
|
13
|
-
export { DEFAULT_EVENT_OPTIONS } from './EventOptions';
|
|
14
15
|
export { EventPriorityQueue } from './EventPriorityQueue';
|
|
15
|
-
export
|
|
16
|
-
export {
|
|
17
|
-
export type { EventStatus, MessageQueueBridgeConfig } from './MessageQueueBridge';
|
|
16
|
+
export * from './FlowControlStrategy';
|
|
17
|
+
export { IdempotencyCache } from './IdempotencyCache';
|
|
18
18
|
export { MessageQueueBridge } from './MessageQueueBridge';
|
|
19
|
-
export
|
|
20
|
-
export
|
|
19
|
+
export type { PriorityStatistics } from './PriorityEscalationManager';
|
|
20
|
+
export { PriorityEscalationManager } from './PriorityEscalationManager';
|
|
21
|
+
export type { RetrySchedulerConfig } from './RetryScheduler';
|
|
21
22
|
export { RetryScheduler } from './RetryScheduler';
|
|
22
|
-
export type {
|
|
23
|
+
export type { DeadLetterDecision, MultiPriorityQueueDepth, WindowAdjustment } from './types';
|
|
23
24
|
export { WorkerPool } from './WorkerPool';
|
|
24
25
|
export type { WorkerPoolConfig, WorkerPoolStats, WorkerStats } from './WorkerPoolConfig';
|
|
25
26
|
export { WorkerPoolMetrics } from './WorkerPoolMetrics';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { BackpressureManager } from './BackpressureManager';
|
|
2
|
+
import type { DeadLetterQueue } from './DeadLetterQueue';
|
|
3
|
+
import type { OTelEventMetrics } from './observability/OTelEventMetrics';
|
|
4
|
+
import { type PriorityStatistics } from './PriorityEscalationManager';
|
|
5
|
+
import type { BackpressureStrategy, EventQueueConfig, EventTask, MultiPriorityQueueDepth } from './types';
|
|
6
|
+
/**
|
|
7
|
+
* Result of an enqueue operation.
|
|
8
|
+
*
|
|
9
|
+
* - `{ status: 'queued', id }` - Task successfully queued
|
|
10
|
+
* - `{ status: 'dropped' }` - Task dropped due to backpressure
|
|
11
|
+
* - `{ status: 'delayed', id, delayMs }` - Task should be re-enqueued after delayMs
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export type EnqueueResult = {
|
|
16
|
+
status: 'queued';
|
|
17
|
+
id: string;
|
|
18
|
+
} | {
|
|
19
|
+
status: 'dropped';
|
|
20
|
+
} | {
|
|
21
|
+
status: 'delayed';
|
|
22
|
+
id: string;
|
|
23
|
+
delayMs: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Priority queue core: manages event task queues and backpressure.
|
|
27
|
+
*
|
|
28
|
+
* Handles enqueueing, dequeuing, backpressure logic, and priority escalation.
|
|
29
|
+
* This class is used internally by EventPriorityQueue as a composition target.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare class QueueCore {
|
|
34
|
+
criticalPriority: EventTask[];
|
|
35
|
+
highPriority: EventTask[];
|
|
36
|
+
normalPriority: EventTask[];
|
|
37
|
+
lowPriority: EventTask[];
|
|
38
|
+
processingPartitions: Set<string>;
|
|
39
|
+
private taskIdCounter;
|
|
40
|
+
private dlq?;
|
|
41
|
+
private otelEventMetrics?;
|
|
42
|
+
private backpressureManager?;
|
|
43
|
+
private priorityStats?;
|
|
44
|
+
private config;
|
|
45
|
+
constructor(config?: EventQueueConfig);
|
|
46
|
+
setDeadLetterQueue(dlq: DeadLetterQueue): void;
|
|
47
|
+
setOTelEventMetrics(metrics: OTelEventMetrics): void;
|
|
48
|
+
setPriorityStatistics(stats: PriorityStatistics): void;
|
|
49
|
+
getPriorityStatistics(): PriorityStatistics | undefined;
|
|
50
|
+
getBackpressureManager(): BackpressureManager | undefined;
|
|
51
|
+
getConfig(): EventQueueConfig;
|
|
52
|
+
getDLQ(): DeadLetterQueue | undefined;
|
|
53
|
+
createTaskId(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Enqueue a task into the appropriate priority queue.
|
|
56
|
+
* Applies priority escalation and backpressure logic.
|
|
57
|
+
*/
|
|
58
|
+
enqueue(task: EventTask): EnqueueResult;
|
|
59
|
+
/**
|
|
60
|
+
* Dequeue the next task based on priority and partition ordering.
|
|
61
|
+
* Priority order: CRITICAL > HIGH > NORMAL > LOW
|
|
62
|
+
*/
|
|
63
|
+
dequeue(): EventTask | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Re-enqueue a task for retry.
|
|
66
|
+
*/
|
|
67
|
+
enqueueRetry(task: EventTask): void;
|
|
68
|
+
getDepth(): number;
|
|
69
|
+
getDepthByPriority(priority: 'critical' | 'high' | 'normal' | 'low'): number;
|
|
70
|
+
getQueueDepthByPriority(): MultiPriorityQueueDepth;
|
|
71
|
+
clear(): void;
|
|
72
|
+
syncBackpressure(): void;
|
|
73
|
+
private handleBackpressure;
|
|
74
|
+
private dropOldest;
|
|
75
|
+
private dequeueFromPriority;
|
|
76
|
+
}
|
|
77
|
+
export type { BackpressureStrategy, EventQueueConfig, EventTask };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Span } from '@opentelemetry/api';
|
|
2
|
+
import { CircuitBreaker } from './CircuitBreaker';
|
|
3
|
+
import type { DeadLetterQueue } from './DeadLetterQueue';
|
|
4
|
+
import type { EventOptions } from './EventOptions';
|
|
5
|
+
import type { EventMetrics } from './observability/EventMetrics';
|
|
6
|
+
import type { EventTracing } from './observability/EventTracing';
|
|
7
|
+
import type { OTelEventMetrics } from './observability/OTelEventMetrics';
|
|
8
|
+
import type { RetryScheduler } from './RetryScheduler';
|
|
9
|
+
import type { EventTask } from './types';
|
|
10
|
+
/**
|
|
11
|
+
* Executes event tasks with circuit breaker protection, retry logic,
|
|
12
|
+
* exponential backoff, DLQ integration, and distributed tracing.
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare class TaskExecutor {
|
|
17
|
+
private eventCircuitBreakers;
|
|
18
|
+
private eventMetrics?;
|
|
19
|
+
private otelEventMetrics?;
|
|
20
|
+
private eventTracing?;
|
|
21
|
+
private currentDispatchSpan?;
|
|
22
|
+
private retryScheduler?;
|
|
23
|
+
private dlq?;
|
|
24
|
+
private persistentDLQHandler?;
|
|
25
|
+
private enqueueRetryFn?;
|
|
26
|
+
setDeadLetterQueue(dlq: DeadLetterQueue): void;
|
|
27
|
+
setPersistentDLQHandler(handler: (hook: string, args: unknown, options: EventOptions, error: Error, retryCount: number, firstFailedAt: number) => Promise<void>): void;
|
|
28
|
+
setEventMetrics(metrics: EventMetrics): void;
|
|
29
|
+
setOTelEventMetrics(metrics: OTelEventMetrics): void;
|
|
30
|
+
setEventTracing(tracing: EventTracing): void;
|
|
31
|
+
setCurrentDispatchSpan(span: Span | undefined): void;
|
|
32
|
+
getCurrentDispatchSpan(): Span | undefined;
|
|
33
|
+
setRetryScheduler(scheduler: RetryScheduler): void;
|
|
34
|
+
getRetryScheduler(): RetryScheduler | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Set the callback to use when re-enqueueing a task after a retry delay.
|
|
37
|
+
*/
|
|
38
|
+
setEnqueueRetryFn(fn: (task: EventTask) => void): void;
|
|
39
|
+
getCircuitBreaker(hook: string): CircuitBreaker | undefined;
|
|
40
|
+
getCircuitBreakers(): Map<string, CircuitBreaker>;
|
|
41
|
+
resetCircuitBreaker(hook: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Execute an event task by running all its callbacks.
|
|
44
|
+
* Implements circuit breaker protection, retry logic, and DLQ integration.
|
|
45
|
+
*/
|
|
46
|
+
executeTask(task: EventTask, processingPartitions: Set<string>): Promise<void>;
|
|
47
|
+
private sendToDLQ;
|
|
48
|
+
private getOrCreateEventCircuitBreaker;
|
|
49
|
+
private calculateRetryDelay;
|
|
50
|
+
private executeWithTimeout;
|
|
51
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 原生加速器統一入口
|
|
3
|
+
* 遵循 Galaxy Architecture 的運行時自適應模式
|
|
4
|
+
* 與 RuntimeAdapter 模式保持一致
|
|
5
|
+
*
|
|
6
|
+
* 📍 FFI 暫時禁用
|
|
7
|
+
* Bun 的構建系統在生成 ffi/index.js 時會產生 CJS compatibility helpers,
|
|
8
|
+
* 這些 helpers 與 ESM export 語句導致模組解析失敗。
|
|
9
|
+
* 即使使用 ESM import 也無法避免此問題(Bun 內部機制)。
|
|
10
|
+
*
|
|
11
|
+
* 解決方案:禁用 FFI,使用 JavaScript fallback(性能影響極小)
|
|
12
|
+
* TODO: 當 Bun 改進其 ESM 模組構建時重新啟用
|
|
13
|
+
*/
|
|
14
|
+
import type { CborAccelerator, NativeAcceleratorStatus } from './types';
|
|
15
|
+
/**
|
|
16
|
+
* 原生加速器類別
|
|
17
|
+
* 提供運行時自適應的 CBOR 編碼/解碼加速
|
|
18
|
+
* - 在 Bun 環境下優先使用 C 編譯器實現(bun:ffi)
|
|
19
|
+
* - 在非 Bun 環境或 FFI 不可用時自動降級到 JavaScript 實現
|
|
20
|
+
*/
|
|
21
|
+
export declare class NativeAccelerator {
|
|
22
|
+
private static readonly DEBUG_ENV;
|
|
23
|
+
/**
|
|
24
|
+
* FFI 可用性緩存
|
|
25
|
+
* null: 未檢測, true: 可用, false: 不可用
|
|
26
|
+
*/
|
|
27
|
+
private static available;
|
|
28
|
+
/**
|
|
29
|
+
* 當前加速器實例緩存
|
|
30
|
+
*/
|
|
31
|
+
private static cborAccelerator;
|
|
32
|
+
/**
|
|
33
|
+
* 當前狀態
|
|
34
|
+
*/
|
|
35
|
+
private static status;
|
|
36
|
+
/**
|
|
37
|
+
* 檢測原生 FFI 是否可用
|
|
38
|
+
* ⚠️ 當前全部返回 false(FFI 已禁用)
|
|
39
|
+
*/
|
|
40
|
+
static isAvailable(): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* 取得 CBOR 加速器實例
|
|
43
|
+
* 優先使用 Native,失敗則降級到 Fallback
|
|
44
|
+
*/
|
|
45
|
+
static getCborAccelerator(): CborAccelerator;
|
|
46
|
+
/**
|
|
47
|
+
* 取得加速器狀態
|
|
48
|
+
*/
|
|
49
|
+
static getStatus(): NativeAcceleratorStatus;
|
|
50
|
+
/**
|
|
51
|
+
* 重置加速器狀態(用於測試)
|
|
52
|
+
*/
|
|
53
|
+
static reset(): void;
|
|
54
|
+
/**
|
|
55
|
+
* 檢查是否啟用調試模式
|
|
56
|
+
*/
|
|
57
|
+
private static isDebugEnabled;
|
|
58
|
+
/**
|
|
59
|
+
* 更新狀態
|
|
60
|
+
*/
|
|
61
|
+
private static updateStatus;
|
|
62
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 原生雜湊加速器
|
|
3
|
+
* 運行時自適應實現,遵循 Galaxy Architecture 的設計模式
|
|
4
|
+
*
|
|
5
|
+
* 架構:
|
|
6
|
+
* - 在 Bun 環境:使用 Bun.CryptoHasher(C 實現,高效)
|
|
7
|
+
* - 其他環境:自動降級到 node:crypto
|
|
8
|
+
* - 完全相同的 API 和結果保證
|
|
9
|
+
*
|
|
10
|
+
* 性能:
|
|
11
|
+
* - Bun 環境:2-3x 快於 node:crypto(消除 N-API 開銷)
|
|
12
|
+
* - 跨運行時:完全相容性
|
|
13
|
+
*
|
|
14
|
+
* 應用場景:
|
|
15
|
+
* - FileStore.hashKey() 熱路徑
|
|
16
|
+
* - Encrypter.hash() 加密雜湊
|
|
17
|
+
* - 任何需要高效雜湊計算的地方
|
|
18
|
+
*/
|
|
19
|
+
import type { NativeHasherStatus } from './types';
|
|
20
|
+
/**
|
|
21
|
+
* 原生雜湊加速器統一入口
|
|
22
|
+
* 提供運行時自適應的 SHA-256/HMAC-SHA256 計算
|
|
23
|
+
*
|
|
24
|
+
* 使用範例:
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // SHA-256
|
|
27
|
+
* const hash = NativeHasher.sha256('data')
|
|
28
|
+
*
|
|
29
|
+
* // HMAC-SHA256
|
|
30
|
+
* const hmac = NativeHasher.hmacSha256('secret', 'message')
|
|
31
|
+
*
|
|
32
|
+
* // 檢查狀態
|
|
33
|
+
* const status = NativeHasher.getStatus()
|
|
34
|
+
* console.log(`使用: ${status.runtime}`)
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class NativeHasher {
|
|
38
|
+
/**
|
|
39
|
+
* 當前加速器實例緩存
|
|
40
|
+
*/
|
|
41
|
+
private static accelerator;
|
|
42
|
+
/**
|
|
43
|
+
* 當前狀態緩存
|
|
44
|
+
*/
|
|
45
|
+
private static status;
|
|
46
|
+
/**
|
|
47
|
+
* 取得雜湊加速器實例
|
|
48
|
+
* 優先使用 Bun.CryptoHasher,失敗則降級到 node:crypto
|
|
49
|
+
*
|
|
50
|
+
* @returns 雜湊加速器實例
|
|
51
|
+
*/
|
|
52
|
+
private static getAccelerator;
|
|
53
|
+
/**
|
|
54
|
+
* 檢測 Bun 運行時和 CryptoHasher 可用性
|
|
55
|
+
*
|
|
56
|
+
* @returns Bun.CryptoHasher 是否可用
|
|
57
|
+
*/
|
|
58
|
+
private static isBunAvailable;
|
|
59
|
+
/**
|
|
60
|
+
* 更新狀態
|
|
61
|
+
*
|
|
62
|
+
* @param runtime - 當前使用的運行時
|
|
63
|
+
*/
|
|
64
|
+
private static updateStatus;
|
|
65
|
+
/**
|
|
66
|
+
* 計算 SHA-256 雜湊
|
|
67
|
+
* 支援字串和二進制輸入
|
|
68
|
+
*
|
|
69
|
+
* @param input - 要雜湊的數據(字串或 Uint8Array)
|
|
70
|
+
* @returns 十六進制編碼的 SHA-256 雜湊值(64 字元)
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const hash = NativeHasher.sha256('hello')
|
|
75
|
+
* // '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
|
|
76
|
+
*
|
|
77
|
+
* // 支援 Uint8Array
|
|
78
|
+
* const bytes = new TextEncoder().encode('hello')
|
|
79
|
+
* const hash2 = NativeHasher.sha256(bytes)
|
|
80
|
+
* // 相同結果
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
static sha256(input: string | Uint8Array): string;
|
|
84
|
+
/**
|
|
85
|
+
* 計算 HMAC-SHA256
|
|
86
|
+
* 用於認證消息完整性
|
|
87
|
+
*
|
|
88
|
+
* @param key - HMAC 密鑰
|
|
89
|
+
* @param data - 要雜湊的數據
|
|
90
|
+
* @returns 十六進制編碼的 HMAC-SHA256 值(64 字元)
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const hmac = NativeHasher.hmacSha256('secret', 'message')
|
|
95
|
+
* // '8b1a9953c4611296aed9e132b8502cf413b1b881fed3e8d26ab'...
|
|
96
|
+
*
|
|
97
|
+
* // 適用於加密驗證
|
|
98
|
+
* const iv = Buffer.from('...').toString('base64')
|
|
99
|
+
* const encrypted = '...'
|
|
100
|
+
* const mac = NativeHasher.hmacSha256(key, iv + encrypted)
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
static hmacSha256(key: string, data: string): string;
|
|
104
|
+
/**
|
|
105
|
+
* 取得加速器狀態
|
|
106
|
+
* 用於診斷和性能監測
|
|
107
|
+
*
|
|
108
|
+
* @returns 加速器狀態報告
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* const status = NativeHasher.getStatus()
|
|
113
|
+
* console.log(`可用: ${status.available}`)
|
|
114
|
+
* console.log(`運行時: ${status.runtime}`)
|
|
115
|
+
*
|
|
116
|
+
* // 條件邏輯
|
|
117
|
+
* if (status.runtime === 'bun-crypto-hasher') {
|
|
118
|
+
* console.log('使用 Bun 原生實現(高效)')
|
|
119
|
+
* } else {
|
|
120
|
+
* console.log('使用 node:crypto(相容性回退)')
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
static getStatus(): NativeHasherStatus;
|
|
125
|
+
/**
|
|
126
|
+
* 重置加速器狀態
|
|
127
|
+
* 主要用於測試目的,允許重新初始化運行時檢測
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* // 測試中
|
|
133
|
+
* NativeHasher.reset()
|
|
134
|
+
* // 下一次呼叫會重新偵測運行時
|
|
135
|
+
* const status = NativeHasher.getStatus()
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
static reset(): void;
|
|
139
|
+
}
|