tempest-express-sdk 0.1.0 → 0.2.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.js CHANGED
@@ -1,4 +1,4 @@
1
- export { VERSION } from './chunk-2NB7ZA7G.js';
1
+ export { VERSION } from './chunk-6QNSLBL3.js';
2
2
  import { AsyncLocalStorage } from 'async_hooks';
3
3
  import { extendZodWithOpenApi, OpenAPIRegistry, OpenApiGeneratorV31, OpenApiGeneratorV3 } from '@asteasolutions/zod-to-openapi';
4
4
  export { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
@@ -7,6 +7,8 @@ export { z } from 'zod';
7
7
  import { Model, column, sql } from 'tempest-db-js';
8
8
  export { AsyncEngine, AsyncResult, AsyncSession, BaseRepository, Column, DeleteBuilder, InsertBuilder, Model, NoResultError, NodeSqliteDriver, PostgresDialect, RecordNotFound, SelectBuilder, SqliteDialect, SyncEngine, SyncSession, UpdateBuilder, and, belongsTo, column, columnsOf, createEngine, createSyncEngine, del, detectDialect, getDialect, hasMany, insert, join, loadRelations, not, or, parseDatabaseUrl, select, sql, update } from 'tempest-db-js';
9
9
  import { createHash, randomBytes, timingSafeEqual, randomUUID } from 'crypto';
10
+ import { mkdir, writeFile, readFile, rm } from 'fs/promises';
11
+ import { join, dirname } from 'path';
10
12
  import express2, { Router } from 'express';
11
13
  import { getAbsoluteFSPath } from 'swagger-ui-dist';
12
14
 
@@ -6750,6 +6752,846 @@ var AttemptThrottle = class {
6750
6752
  }
6751
6753
  };
6752
6754
 
6755
+ // src/cache/manager.ts
6756
+ var MemoryCacheManager = class {
6757
+ store = /* @__PURE__ */ new Map();
6758
+ live(key) {
6759
+ const entry = this.store.get(key);
6760
+ if (!entry) return null;
6761
+ if (entry.expiresAt !== null && entry.expiresAt <= Date.now()) {
6762
+ this.store.delete(key);
6763
+ return null;
6764
+ }
6765
+ return entry;
6766
+ }
6767
+ async get(key) {
6768
+ const entry = this.live(key);
6769
+ return entry ? JSON.parse(entry.value) : null;
6770
+ }
6771
+ async set(key, value, ttlSeconds) {
6772
+ this.store.set(key, {
6773
+ value: JSON.stringify(value),
6774
+ expiresAt: ttlSeconds !== void 0 ? Date.now() + ttlSeconds * 1e3 : null
6775
+ });
6776
+ }
6777
+ async delete(key) {
6778
+ this.store.delete(key);
6779
+ }
6780
+ async has(key) {
6781
+ return this.live(key) !== null;
6782
+ }
6783
+ async clear() {
6784
+ this.store.clear();
6785
+ }
6786
+ };
6787
+ var RedisCacheManager = class {
6788
+ /**
6789
+ * @param client - A connected `redis` (node-redis v4) client or compatible.
6790
+ * @param prefix - Optional key prefix applied to every operation.
6791
+ */
6792
+ constructor(client, prefix = "") {
6793
+ this.client = client;
6794
+ this.prefix = prefix;
6795
+ }
6796
+ client;
6797
+ prefix;
6798
+ key(key) {
6799
+ return this.prefix ? `${this.prefix}${key}` : key;
6800
+ }
6801
+ async get(key) {
6802
+ const raw = await this.client.get(this.key(key));
6803
+ return raw === null ? null : JSON.parse(raw);
6804
+ }
6805
+ async set(key, value, ttlSeconds) {
6806
+ const payload = JSON.stringify(value);
6807
+ await this.client.set(
6808
+ this.key(key),
6809
+ payload,
6810
+ ttlSeconds !== void 0 ? { EX: ttlSeconds } : void 0
6811
+ );
6812
+ }
6813
+ async delete(key) {
6814
+ await this.client.del(this.key(key));
6815
+ }
6816
+ async has(key) {
6817
+ return await this.client.exists(this.key(key)) > 0;
6818
+ }
6819
+ async clear() {
6820
+ await this.client.flushDb();
6821
+ }
6822
+ };
6823
+
6824
+ // src/cache/cached.ts
6825
+ function cached3(fn, options) {
6826
+ return async (...args) => {
6827
+ const key = options.key(...args);
6828
+ const hit = await options.manager.get(key);
6829
+ if (hit !== null) return hit;
6830
+ const result = await fn(...args);
6831
+ await options.manager.set(key, result, options.ttlSeconds);
6832
+ return result;
6833
+ };
6834
+ }
6835
+
6836
+ // src/sessions/store.ts
6837
+ var MemorySessionStore = class {
6838
+ byHash = /* @__PURE__ */ new Map();
6839
+ live(session, idHash) {
6840
+ if (!session) return null;
6841
+ if (session.expiresAt <= Date.now()) {
6842
+ this.byHash.delete(idHash);
6843
+ return null;
6844
+ }
6845
+ return session;
6846
+ }
6847
+ async get(idHash) {
6848
+ return this.live(this.byHash.get(idHash), idHash);
6849
+ }
6850
+ async set(session) {
6851
+ this.byHash.set(session.idHash, session);
6852
+ }
6853
+ async delete(idHash) {
6854
+ this.byHash.delete(idHash);
6855
+ }
6856
+ async deleteByUser(userId) {
6857
+ let count = 0;
6858
+ for (const [hash, session] of this.byHash) {
6859
+ if (session.userId === userId) {
6860
+ this.byHash.delete(hash);
6861
+ count += 1;
6862
+ }
6863
+ }
6864
+ return count;
6865
+ }
6866
+ async listByUser(userId) {
6867
+ const now = Date.now();
6868
+ return [...this.byHash.values()].filter((s) => s.userId === userId && s.expiresAt > now).sort((a, b) => a.createdAt - b.createdAt);
6869
+ }
6870
+ };
6871
+
6872
+ // src/sessions/service.ts
6873
+ var SessionService = class {
6874
+ store;
6875
+ ttlSeconds;
6876
+ /**
6877
+ * @param options - Store and default TTL.
6878
+ */
6879
+ constructor(options) {
6880
+ this.store = options.store;
6881
+ this.ttlSeconds = options.ttlSeconds ?? 60 * 60 * 24 * 7;
6882
+ }
6883
+ /**
6884
+ * Create a session for `userId` and return its one-time cookie value.
6885
+ *
6886
+ * @param userId - The owning user id.
6887
+ * @param data - Arbitrary session payload.
6888
+ * @param ttlSeconds - Override the default lifetime.
6889
+ * @returns The plaintext token (set as a cookie) and the stored session.
6890
+ */
6891
+ async create(userId, data = {}, ttlSeconds) {
6892
+ const { plaintext, tokenHash } = generateOpaqueToken();
6893
+ const now = Date.now();
6894
+ const session = {
6895
+ idHash: tokenHash,
6896
+ userId,
6897
+ data,
6898
+ createdAt: now,
6899
+ expiresAt: now + (ttlSeconds ?? this.ttlSeconds) * 1e3
6900
+ };
6901
+ await this.store.set(session);
6902
+ return { token: plaintext, session };
6903
+ }
6904
+ /**
6905
+ * Resolve an opaque cookie value to its live session.
6906
+ *
6907
+ * @param token - The plaintext cookie value.
6908
+ * @returns The session, or `null` when missing/expired.
6909
+ */
6910
+ async resolve(token) {
6911
+ return this.store.get(hashOpaqueToken(token));
6912
+ }
6913
+ /**
6914
+ * Revoke a single session by its cookie value.
6915
+ *
6916
+ * @param token - The plaintext cookie value.
6917
+ */
6918
+ async destroy(token) {
6919
+ await this.store.delete(hashOpaqueToken(token));
6920
+ }
6921
+ /**
6922
+ * Revoke every session a user owns (global logout).
6923
+ *
6924
+ * @param userId - The user id.
6925
+ * @returns The number of sessions removed.
6926
+ */
6927
+ async destroyByUser(userId) {
6928
+ return this.store.deleteByUser(userId);
6929
+ }
6930
+ /**
6931
+ * List a user's live sessions (e.g. an "active devices" view).
6932
+ *
6933
+ * @param userId - The user id.
6934
+ * @returns The user's sessions, oldest first.
6935
+ */
6936
+ async listByUser(userId) {
6937
+ return this.store.listByUser(userId);
6938
+ }
6939
+ };
6940
+
6941
+ // src/sessions/middleware.ts
6942
+ function parseCookies(header) {
6943
+ const out = {};
6944
+ if (!header) return out;
6945
+ for (const part of header.split(";")) {
6946
+ const index = part.indexOf("=");
6947
+ if (index < 0) continue;
6948
+ const name = part.slice(0, index).trim();
6949
+ const value = part.slice(index + 1).trim();
6950
+ if (name) out[name] = decodeURIComponent(value);
6951
+ }
6952
+ return out;
6953
+ }
6954
+ function sessionCookie(req, cookieName) {
6955
+ return parseCookies(req.header("cookie") ?? void 0)[cookieName] ?? null;
6956
+ }
6957
+ function makeSessionMiddleware(service, options = {}) {
6958
+ const cookieName = options.cookieName ?? "sid";
6959
+ return (req, _res, next) => {
6960
+ const token = sessionCookie(req, cookieName);
6961
+ if (!token) {
6962
+ req.session = null;
6963
+ next();
6964
+ return;
6965
+ }
6966
+ service.resolve(token).then((session) => {
6967
+ req.session = session;
6968
+ next();
6969
+ }).catch(next);
6970
+ };
6971
+ }
6972
+
6973
+ // src/sse/eventStream.ts
6974
+ var ServerSentEvent = class {
6975
+ constructor(init) {
6976
+ this.init = init;
6977
+ }
6978
+ init;
6979
+ /**
6980
+ * Encode to the SSE wire format (terminated by a blank line).
6981
+ *
6982
+ * @returns The encoded event block.
6983
+ */
6984
+ encode() {
6985
+ const lines = [];
6986
+ if (this.init.event) lines.push(`event: ${this.init.event}`);
6987
+ if (this.init.id) lines.push(`id: ${this.init.id}`);
6988
+ if (this.init.retry !== void 0) lines.push(`retry: ${this.init.retry}`);
6989
+ for (const line of this.init.data.split("\n")) lines.push(`data: ${line}`);
6990
+ return `${lines.join("\n")}
6991
+
6992
+ `;
6993
+ }
6994
+ };
6995
+ function deferred() {
6996
+ let resolve;
6997
+ const promise = new Promise((r) => {
6998
+ resolve = r;
6999
+ });
7000
+ return { promise, resolve };
7001
+ }
7002
+ var EventStream = class {
7003
+ queue = [];
7004
+ waiter = null;
7005
+ closed = false;
7006
+ heartbeatSeconds;
7007
+ /**
7008
+ * @param options - Heartbeat configuration.
7009
+ */
7010
+ constructor(options = {}) {
7011
+ this.heartbeatSeconds = options.heartbeatSeconds ?? 15;
7012
+ }
7013
+ /** Enqueue raw SSE-encoded text and wake the iterator. */
7014
+ push(raw) {
7015
+ if (this.closed) return;
7016
+ this.queue.push(raw);
7017
+ this.waiter?.resolve();
7018
+ this.waiter = null;
7019
+ }
7020
+ /**
7021
+ * Publish a data payload as an SSE event.
7022
+ *
7023
+ * @param data - The payload; objects are JSON-encoded.
7024
+ * @param event - Optional event name.
7025
+ */
7026
+ publish(data, event) {
7027
+ const payload = typeof data === "string" ? data : JSON.stringify(data);
7028
+ this.push(
7029
+ new ServerSentEvent({ data: payload, ...event ? { event } : {} }).encode()
7030
+ );
7031
+ }
7032
+ /** Publish a pre-built {@link ServerSentEvent}. */
7033
+ publishEvent(event) {
7034
+ this.push(event.encode());
7035
+ }
7036
+ /** Close the stream; the iterator finishes after draining. */
7037
+ close() {
7038
+ this.closed = true;
7039
+ this.waiter?.resolve();
7040
+ this.waiter = null;
7041
+ }
7042
+ /**
7043
+ * Async iterator yielding encoded SSE chunks, with periodic heartbeats.
7044
+ *
7045
+ * @returns An async iterator of encoded event strings.
7046
+ */
7047
+ async *stream() {
7048
+ const heartbeatMs = this.heartbeatSeconds !== null ? this.heartbeatSeconds * 1e3 : null;
7049
+ while (!this.closed || this.queue.length > 0) {
7050
+ if (this.queue.length > 0) {
7051
+ yield this.queue.shift();
7052
+ continue;
7053
+ }
7054
+ if (this.closed) break;
7055
+ this.waiter = deferred();
7056
+ if (heartbeatMs === null) {
7057
+ await this.waiter.promise;
7058
+ } else {
7059
+ let timer;
7060
+ const heartbeat = new Promise((resolve) => {
7061
+ timer = setTimeout(resolve, heartbeatMs);
7062
+ });
7063
+ await Promise.race([this.waiter.promise, heartbeat]);
7064
+ if (timer) clearTimeout(timer);
7065
+ if (this.queue.length === 0 && !this.closed) yield ": ping\n\n";
7066
+ }
7067
+ }
7068
+ }
7069
+ };
7070
+ async function sseResponse(req, res, stream) {
7071
+ res.setHeader("Content-Type", "text/event-stream");
7072
+ res.setHeader("Cache-Control", "no-cache");
7073
+ res.setHeader("Connection", "keep-alive");
7074
+ res.flushHeaders?.();
7075
+ req.on("close", () => stream.close());
7076
+ for await (const chunk of stream.stream()) {
7077
+ if (res.writableEnded) break;
7078
+ res.write(chunk);
7079
+ }
7080
+ res.end();
7081
+ }
7082
+
7083
+ // src/sse/broker.ts
7084
+ var SSEBroker = class {
7085
+ /**
7086
+ * @param streamOptions - Options applied to every {@link EventStream} created
7087
+ * by {@link register} (e.g. heartbeat interval).
7088
+ */
7089
+ constructor(streamOptions = {}) {
7090
+ this.streamOptions = streamOptions;
7091
+ }
7092
+ streamOptions;
7093
+ channels = /* @__PURE__ */ new Map();
7094
+ /**
7095
+ * Register a new subscriber stream on `channel`.
7096
+ *
7097
+ * @param channel - The channel name.
7098
+ * @returns A fresh {@link EventStream} to serve to the subscriber.
7099
+ */
7100
+ register(channel) {
7101
+ const stream = new EventStream(this.streamOptions);
7102
+ const set = this.channels.get(channel) ?? /* @__PURE__ */ new Set();
7103
+ set.add(stream);
7104
+ this.channels.set(channel, set);
7105
+ return stream;
7106
+ }
7107
+ /**
7108
+ * Remove a subscriber stream from `channel` and close it.
7109
+ *
7110
+ * @param channel - The channel name.
7111
+ * @param stream - The stream to remove.
7112
+ */
7113
+ unregister(channel, stream) {
7114
+ const set = this.channels.get(channel);
7115
+ if (!set) return;
7116
+ set.delete(stream);
7117
+ stream.close();
7118
+ if (set.size === 0) this.channels.delete(channel);
7119
+ }
7120
+ /**
7121
+ * Number of live subscribers on `channel`.
7122
+ *
7123
+ * @param channel - The channel name.
7124
+ * @returns The subscriber count.
7125
+ */
7126
+ localSubscribers(channel) {
7127
+ return this.channels.get(channel)?.size ?? 0;
7128
+ }
7129
+ /**
7130
+ * Publish an event to every subscriber on `channel`.
7131
+ *
7132
+ * @param channel - The channel name.
7133
+ * @param data - The payload (objects are JSON-encoded).
7134
+ * @param event - Optional event name.
7135
+ * @returns The number of subscribers the event was delivered to.
7136
+ */
7137
+ publish(channel, data, event) {
7138
+ const set = this.channels.get(channel);
7139
+ if (!set) return 0;
7140
+ for (const stream of set) stream.publish(data, event);
7141
+ return set.size;
7142
+ }
7143
+ };
7144
+
7145
+ // src/websockets/schemas.ts
7146
+ var wsEnvelopeSchema = z.object({
7147
+ type: z.string().openapi({ description: "Message type discriminator." }),
7148
+ data: z.unknown().optional().openapi({ description: "Arbitrary payload." })
7149
+ }).openapi("WSEnvelope");
7150
+ var WebSocketHub = class {
7151
+ byId = /* @__PURE__ */ new Map();
7152
+ byUser = /* @__PURE__ */ new Map();
7153
+ maxPerUser;
7154
+ /**
7155
+ * @param options - Per-user connection cap.
7156
+ */
7157
+ constructor(options = {}) {
7158
+ this.maxPerUser = options.maxPerUser ?? 5;
7159
+ }
7160
+ /**
7161
+ * Register a new connection for `userId`, evicting the oldest if over cap.
7162
+ *
7163
+ * @param userId - The owning user id.
7164
+ * @param ws - The socket to register.
7165
+ * @returns The created connection record.
7166
+ */
7167
+ register(userId, ws) {
7168
+ const connection = {
7169
+ id: randomUUID(),
7170
+ userId,
7171
+ ws,
7172
+ topics: /* @__PURE__ */ new Set()
7173
+ };
7174
+ this.byId.set(connection.id, connection);
7175
+ const ids = this.byUser.get(userId) ?? /* @__PURE__ */ new Set();
7176
+ ids.add(connection.id);
7177
+ this.byUser.set(userId, ids);
7178
+ if (ids.size > this.maxPerUser) {
7179
+ const oldest = ids.values().next().value;
7180
+ if (oldest) this.unregister(oldest, 1008);
7181
+ }
7182
+ return connection;
7183
+ }
7184
+ /**
7185
+ * Remove a connection and close its socket.
7186
+ *
7187
+ * @param connectionId - The connection id.
7188
+ * @param code - Optional WebSocket close code.
7189
+ */
7190
+ unregister(connectionId, code) {
7191
+ const connection = this.byId.get(connectionId);
7192
+ if (!connection) return;
7193
+ this.byId.delete(connectionId);
7194
+ const ids = this.byUser.get(connection.userId);
7195
+ if (ids) {
7196
+ ids.delete(connectionId);
7197
+ if (ids.size === 0) this.byUser.delete(connection.userId);
7198
+ }
7199
+ try {
7200
+ connection.ws.close(code);
7201
+ } catch {
7202
+ }
7203
+ }
7204
+ /** Subscribe a connection to a topic. */
7205
+ subscribe(connectionId, topic) {
7206
+ this.byId.get(connectionId)?.topics.add(topic);
7207
+ }
7208
+ /** Unsubscribe a connection from a topic. */
7209
+ unsubscribe(connectionId, topic) {
7210
+ this.byId.get(connectionId)?.topics.delete(topic);
7211
+ }
7212
+ /** Serialize and send an envelope to a single connection. */
7213
+ deliver(connection, payload) {
7214
+ try {
7215
+ connection.ws.send(payload);
7216
+ return true;
7217
+ } catch {
7218
+ this.unregister(connection.id);
7219
+ return false;
7220
+ }
7221
+ }
7222
+ /**
7223
+ * Send an envelope to every connection of `userId`.
7224
+ *
7225
+ * @param userId - The target user.
7226
+ * @param envelope - The message envelope.
7227
+ * @returns The number of connections delivered to.
7228
+ */
7229
+ sendTo(userId, envelope) {
7230
+ const ids = this.byUser.get(userId);
7231
+ if (!ids) return 0;
7232
+ const payload = JSON.stringify(envelope);
7233
+ let count = 0;
7234
+ for (const id of [...ids]) {
7235
+ const connection = this.byId.get(id);
7236
+ if (connection && this.deliver(connection, payload)) count += 1;
7237
+ }
7238
+ return count;
7239
+ }
7240
+ /**
7241
+ * Broadcast an envelope to all connections, or only a topic's subscribers.
7242
+ *
7243
+ * @param envelope - The message envelope.
7244
+ * @param topic - Optional topic to scope the broadcast.
7245
+ * @returns The number of connections delivered to.
7246
+ */
7247
+ broadcast(envelope, topic) {
7248
+ const payload = JSON.stringify(envelope);
7249
+ let count = 0;
7250
+ for (const connection of [...this.byId.values()]) {
7251
+ if (topic && !connection.topics.has(topic)) continue;
7252
+ if (this.deliver(connection, payload)) count += 1;
7253
+ }
7254
+ return count;
7255
+ }
7256
+ /** The set of users with at least one live connection. */
7257
+ onlineUsers() {
7258
+ return new Set(this.byUser.keys());
7259
+ }
7260
+ /** Total live connection count. */
7261
+ connectionCount() {
7262
+ return this.byId.size;
7263
+ }
7264
+ /** Number of connections subscribed to `topic`. */
7265
+ topicCount(topic) {
7266
+ let count = 0;
7267
+ for (const connection of this.byId.values()) {
7268
+ if (connection.topics.has(topic)) count += 1;
7269
+ }
7270
+ return count;
7271
+ }
7272
+ };
7273
+
7274
+ // src/websockets/attach.ts
7275
+ function tokenFromUrl(url) {
7276
+ const query = url.includes("?") ? url.slice(url.indexOf("?") + 1) : "";
7277
+ return new URLSearchParams(query).get("token");
7278
+ }
7279
+ async function attachWebSocketHub(server, hub, options = {}) {
7280
+ let ws;
7281
+ try {
7282
+ ws = await import('ws');
7283
+ } catch (cause) {
7284
+ throw new Error(
7285
+ "attachWebSocketHub requires the 'ws' peer dependency. Install with `npm i ws`.",
7286
+ { cause }
7287
+ );
7288
+ }
7289
+ const path = options.path ?? "/ws";
7290
+ const heartbeatMs = (options.heartbeatSeconds ?? 30) * 1e3;
7291
+ const authenticate = options.authenticate ?? (() => "anonymous");
7292
+ const wss = new ws.WebSocketServer({ server, path });
7293
+ wss.on("connection", (socket, req) => {
7294
+ void (async () => {
7295
+ const userId = await authenticate({
7296
+ url: req.url ?? "",
7297
+ headers: req.headers
7298
+ });
7299
+ if (userId === null) {
7300
+ socket.close(1008);
7301
+ return;
7302
+ }
7303
+ const connection = hub.register(userId, socket);
7304
+ let alive = true;
7305
+ socket.on("pong", () => {
7306
+ alive = true;
7307
+ });
7308
+ socket.on("message", (data) => {
7309
+ options.onMessage?.(connection, String(data));
7310
+ });
7311
+ socket.on("close", () => {
7312
+ alive = false;
7313
+ hub.unregister(connection.id);
7314
+ });
7315
+ if (heartbeatMs > 0) {
7316
+ const timer = setInterval(() => {
7317
+ if (!alive) {
7318
+ clearInterval(timer);
7319
+ hub.unregister(connection.id);
7320
+ return;
7321
+ }
7322
+ alive = false;
7323
+ socket.ping();
7324
+ }, heartbeatMs);
7325
+ socket.on("close", () => clearInterval(timer));
7326
+ }
7327
+ })();
7328
+ });
7329
+ return wss;
7330
+ }
7331
+
7332
+ // src/queue/broker.ts
7333
+ var MemoryBroker = class {
7334
+ handlers = /* @__PURE__ */ new Map();
7335
+ async publish(queue, message) {
7336
+ const set = this.handlers.get(queue);
7337
+ if (!set) return;
7338
+ const payload = JSON.parse(JSON.stringify(message));
7339
+ for (const handler of [...set]) await handler(payload);
7340
+ }
7341
+ async subscribe(queue, handler) {
7342
+ const set = this.handlers.get(queue) ?? /* @__PURE__ */ new Set();
7343
+ set.add(handler);
7344
+ this.handlers.set(queue, set);
7345
+ return async () => {
7346
+ set.delete(handler);
7347
+ if (set.size === 0) this.handlers.delete(queue);
7348
+ };
7349
+ }
7350
+ async close() {
7351
+ this.handlers.clear();
7352
+ }
7353
+ };
7354
+ var RabbitBroker = class {
7355
+ /**
7356
+ * @param options - Connection URL and queue durability.
7357
+ */
7358
+ constructor(options) {
7359
+ this.options = options;
7360
+ this.durable = options.durable ?? true;
7361
+ }
7362
+ options;
7363
+ connection = null;
7364
+ channel = null;
7365
+ durable;
7366
+ /** Lazily connect and open a channel. */
7367
+ async ready() {
7368
+ if (this.channel) return this.channel;
7369
+ let amqp;
7370
+ try {
7371
+ amqp = await import('amqplib');
7372
+ } catch (cause) {
7373
+ throw new Error(
7374
+ "RabbitBroker requires the 'amqplib' peer dependency. Install with `npm i amqplib`.",
7375
+ { cause }
7376
+ );
7377
+ }
7378
+ this.connection = await amqp.connect(this.options.url);
7379
+ this.channel = await this.connection.createChannel();
7380
+ return this.channel;
7381
+ }
7382
+ async publish(queue, message) {
7383
+ const channel = await this.ready();
7384
+ await channel.assertQueue(queue, { durable: this.durable });
7385
+ channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
7386
+ persistent: this.durable
7387
+ });
7388
+ }
7389
+ async subscribe(queue, handler) {
7390
+ const channel = await this.ready();
7391
+ await channel.assertQueue(queue, { durable: this.durable });
7392
+ const { consumerTag } = await channel.consume(queue, (msg) => {
7393
+ if (!msg) return;
7394
+ void Promise.resolve(handler(JSON.parse(msg.content.toString()))).then(() => channel.ack(msg)).catch(() => channel.nack(msg, false, false));
7395
+ });
7396
+ return async () => {
7397
+ await channel.cancel(consumerTag);
7398
+ };
7399
+ }
7400
+ async close() {
7401
+ await this.channel?.close();
7402
+ await this.connection?.close();
7403
+ this.channel = null;
7404
+ this.connection = null;
7405
+ }
7406
+ };
7407
+
7408
+ // src/tasks/manager.ts
7409
+ var logger = new JSONLogger("tempest_express_sdk.tasks");
7410
+ var TaskManager = class {
7411
+ broker;
7412
+ queue;
7413
+ handlers = /* @__PURE__ */ new Map();
7414
+ unsubscribe = null;
7415
+ /**
7416
+ * @param options - Broker and queue name.
7417
+ */
7418
+ constructor(options = {}) {
7419
+ this.broker = options.broker ?? new MemoryBroker();
7420
+ this.queue = options.queue ?? "tasks";
7421
+ }
7422
+ /**
7423
+ * Register a handler for a named task.
7424
+ *
7425
+ * @param name - The task name.
7426
+ * @param handler - The handler invoked with the task payload.
7427
+ */
7428
+ register(name, handler) {
7429
+ this.handlers.set(name, handler);
7430
+ }
7431
+ /**
7432
+ * Enqueue a task by name.
7433
+ *
7434
+ * @param name - The registered task name.
7435
+ * @param payload - The JSON-serializable payload.
7436
+ */
7437
+ async enqueue(name, payload = {}) {
7438
+ const envelope = { name, payload };
7439
+ await this.broker.publish(this.queue, envelope);
7440
+ }
7441
+ /**
7442
+ * Start the worker: subscribe to the task queue and dispatch to handlers.
7443
+ * A task with no registered handler is logged and skipped.
7444
+ */
7445
+ async start() {
7446
+ if (this.unsubscribe) return;
7447
+ this.unsubscribe = await this.broker.subscribe(this.queue, async (message) => {
7448
+ const { name, payload } = message;
7449
+ const handler = this.handlers.get(name);
7450
+ if (!handler) {
7451
+ logger.warning("No handler for task", { task: name });
7452
+ return;
7453
+ }
7454
+ await handler(payload);
7455
+ });
7456
+ }
7457
+ /** Stop the worker (stops consuming; does not close the broker). */
7458
+ async stop() {
7459
+ await this.unsubscribe?.();
7460
+ this.unsubscribe = null;
7461
+ }
7462
+ };
7463
+
7464
+ // src/flags/backends.ts
7465
+ function coerceFlag(value) {
7466
+ if (typeof value === "boolean") return value;
7467
+ if (typeof value === "number") return value !== 0;
7468
+ if (typeof value === "string") {
7469
+ return ["1", "true", "on", "yes", "enabled"].includes(value.trim().toLowerCase());
7470
+ }
7471
+ return false;
7472
+ }
7473
+ var MemoryFeatureFlagBackend = class {
7474
+ flags = /* @__PURE__ */ new Map();
7475
+ /**
7476
+ * @param initial - Initial flag → enabled map.
7477
+ */
7478
+ constructor(initial = {}) {
7479
+ for (const [flag, enabled] of Object.entries(initial)) this.flags.set(flag, enabled);
7480
+ }
7481
+ /** Set or override a flag. */
7482
+ set(flag, enabled) {
7483
+ this.flags.set(flag, enabled);
7484
+ }
7485
+ resolve(flag) {
7486
+ return this.flags.has(flag) ? this.flags.get(flag) : null;
7487
+ }
7488
+ };
7489
+ var EnvFeatureFlagBackend = class {
7490
+ /**
7491
+ * @param env - Environment source (defaults to `process.env`).
7492
+ * @param prefix - Env var prefix. Default `FLAG_`.
7493
+ */
7494
+ constructor(env = process.env, prefix = "FLAG_") {
7495
+ this.env = env;
7496
+ this.prefix = prefix;
7497
+ }
7498
+ env;
7499
+ prefix;
7500
+ key(flag) {
7501
+ return this.prefix + flag.toUpperCase().replace(/[^A-Z0-9]+/g, "_");
7502
+ }
7503
+ resolve(flag) {
7504
+ const raw = this.env[this.key(flag)];
7505
+ return raw === void 0 ? null : coerceFlag(raw);
7506
+ }
7507
+ };
7508
+ var CompositeFeatureFlagBackend = class {
7509
+ /**
7510
+ * @param backends - Backends in priority order.
7511
+ */
7512
+ constructor(backends) {
7513
+ this.backends = backends;
7514
+ }
7515
+ backends;
7516
+ async resolve(flag, context) {
7517
+ for (const backend of this.backends) {
7518
+ const answer = await backend.resolve(flag, context);
7519
+ if (answer !== null) return answer;
7520
+ }
7521
+ return null;
7522
+ }
7523
+ };
7524
+
7525
+ // src/flags/service.ts
7526
+ var FeatureFlags = class {
7527
+ /**
7528
+ * @param backend - The resolving backend.
7529
+ * @param defaultEnabled - Value used when the backend returns `null`.
7530
+ */
7531
+ constructor(backend, defaultEnabled = false) {
7532
+ this.backend = backend;
7533
+ this.defaultEnabled = defaultEnabled;
7534
+ }
7535
+ backend;
7536
+ defaultEnabled;
7537
+ /**
7538
+ * Whether `flag` is enabled for the given context.
7539
+ *
7540
+ * @param flag - The flag name.
7541
+ * @param context - Optional evaluation context.
7542
+ * @returns `true` when enabled (or default when the backend is undecided).
7543
+ */
7544
+ async isEnabled(flag, context) {
7545
+ const answer = await this.backend.resolve(flag, context);
7546
+ return answer ?? this.defaultEnabled;
7547
+ }
7548
+ };
7549
+ function makeFlagGuard(flags, flag) {
7550
+ return (_req, _res, next) => {
7551
+ flags.isEnabled(flag).then((enabled) => {
7552
+ if (enabled) next();
7553
+ else next(new NotFoundException({ message: "Not found", details: { flag } }));
7554
+ }).catch(next);
7555
+ };
7556
+ }
7557
+ var LocalUploadStorage = class {
7558
+ root;
7559
+ baseUrl;
7560
+ /**
7561
+ * @param options - Filesystem root and public base URL.
7562
+ */
7563
+ constructor(options) {
7564
+ this.root = options.root;
7565
+ this.baseUrl = options.baseUrl ?? "";
7566
+ }
7567
+ async save(key, data, options = {}) {
7568
+ const target = join(this.root, key);
7569
+ await mkdir(dirname(target), { recursive: true });
7570
+ await writeFile(target, data);
7571
+ return {
7572
+ key,
7573
+ url: this.url(key),
7574
+ size: data.byteLength,
7575
+ ...options.contentType !== void 0 ? { contentType: options.contentType } : {}
7576
+ };
7577
+ }
7578
+ async read(key) {
7579
+ return readFile(join(this.root, key));
7580
+ }
7581
+ async delete(key) {
7582
+ await rm(join(this.root, key), { force: true });
7583
+ }
7584
+ url(key) {
7585
+ const base = this.baseUrl.replace(/\/$/, "");
7586
+ return base ? `${base}/${key}` : `/${key}`;
7587
+ }
7588
+ };
7589
+ function buildContentDisposition(filename, inline = false) {
7590
+ const disposition = inline ? "inline" : "attachment";
7591
+ const encoded = encodeURIComponent(filename);
7592
+ return `${disposition}; filename*=UTF-8''${encoded}`;
7593
+ }
7594
+
6753
7595
  // src/auth/schemas.ts
6754
7596
  var signupSchema = z.object({
6755
7597
  email: z.string().email().openapi({ description: "Login identifier (email)." }),
@@ -6996,7 +7838,7 @@ function makeAuthRouter(options) {
6996
7838
  });
6997
7839
  return router;
6998
7840
  }
6999
- var logger = new JSONLogger("tempest_express_sdk.api.handlers");
7841
+ var logger2 = new JSONLogger("tempest_express_sdk.api.handlers");
7000
7842
  var REQUEST_ID_HEADER = "X-Request-ID";
7001
7843
  function requestIdMiddleware() {
7002
7844
  return (req, res, next) => {
@@ -7037,7 +7879,7 @@ function makeAppExceptionHandler(options = {}) {
7037
7879
  return;
7038
7880
  }
7039
7881
  const isServerError = exc.statusCode >= 500;
7040
- logger.log(isServerError ? serverErrorLevel : "info", "AppException handled", {
7882
+ logger2.log(isServerError ? serverErrorLevel : "info", "AppException handled", {
7041
7883
  path: req.path,
7042
7884
  method: req.method,
7043
7885
  statusCode: exc.statusCode,
@@ -7061,7 +7903,7 @@ function makeUnhandledExceptionHandler(options = {}) {
7061
7903
  const { includeStack = false, logLevel = "error" } = options;
7062
7904
  return (err, req, res, _next) => {
7063
7905
  const error = err instanceof Error ? err : new Error(String(err));
7064
- logger.log(logLevel, "Unhandled exception", {
7906
+ logger2.log(logLevel, "Unhandled exception", {
7065
7907
  path: req.path,
7066
7908
  method: req.method,
7067
7909
  [HTTP_500_MARKER]: true,
@@ -7196,7 +8038,7 @@ function makeHealthRouter(options = {}) {
7196
8038
  });
7197
8039
  return router;
7198
8040
  }
7199
- var logger2 = new JSONLogger("tempest_express_sdk.api.server");
8041
+ var logger3 = new JSONLogger("tempest_express_sdk.api.server");
7200
8042
  function corsMiddleware(origins) {
7201
8043
  const allowAll = origins === "*";
7202
8044
  const allowList = new Set(Array.isArray(origins) ? origins : [origins]);
@@ -7255,12 +8097,12 @@ function runServer(app, options = {}) {
7255
8097
  const port = options.port ?? 8e3;
7256
8098
  return new Promise((resolve) => {
7257
8099
  const server = app.listen(port, host, () => {
7258
- logger2.info("Server listening", { host, port });
8100
+ logger3.info("Server listening", { host, port });
7259
8101
  resolve(server);
7260
8102
  });
7261
8103
  });
7262
8104
  }
7263
8105
 
7264
- export { AppException, AttemptThrottle, BaseController, BaseModel, BaseService, CEP_PATTERN, CNPJ_PATTERN, CPF_PATTERN, ConflictException, DEFAULT_LOCALE, ExpiredTokenException, ForbiddenException, HTTP_500_MARKER, InvalidTokenException, JSONLogger, JWTUtils, MemoryThrottleBackend, MessageCatalog, NotFoundException, PHONE_BR_PATTERN, PasswordUtils, REQUEST_ID_HEADER, Region, TooManyRequestsException, UF, UnauthorizedException, UserAuthService, ValidationException, authResponseSchema, baseAppSettingsSchema, baseAppSettingsShape, baseResponseSchema, bearerToken, cepField, citiesByUf, cnpjField, configureLogging, corsSettingsShape, cpfField, cpfOrCnpjField, createApp, createOpenApiRegistry, createdByColumn, cursorPaginationFilterSchema, cursorPaginationSchema, databaseSettingsShape, decodeCursor, defaultMessageCatalog, defineEnum, deletedAtColumn, encodeCursor, generateOpaqueToken, generateOpenApiDocument, getAuth, getConditions, getPaginationConditions, getRequestId, getState, hashOpaqueToken, isValidCep, isValidCity, isValidCnpj, isValidCpf, isValidCpfCnpj, isValidPhoneBr, isValidUf, listStates, loadSettings, loginSchema, makeAppExceptionHandler, makeAuthRouter, makeHealthRouter, makeJwtAuthMiddleware, makeUnhandledExceptionHandler, modifyDict, mountOpenApiJson, mountRedoc, mountSwaggerUi, normalizeCep, normalizeCnpj, normalizeCpf, normalizeCpfCnpj, normalizePhoneBr, normalizeUf, notFoundHandler, onlyDigits, paginationFilterSchema, paginationSchema, parseAcceptLanguage, phoneBrField, refreshSchema, registerExceptionHandlers, requestIdMiddleware, requireRoles, runServer, runWithRequestContext, serverSettingsShape, setRequestId, signupSchema, statesByRegion, tableNameFor, toDict, toUtc, tokenPairSchema, ufField, updatedByColumn, userPublicSchema, utcnow, verifyOpaqueToken };
8106
+ export { AppException, AttemptThrottle, BaseController, BaseModel, BaseService, CEP_PATTERN, CNPJ_PATTERN, CPF_PATTERN, CompositeFeatureFlagBackend, ConflictException, DEFAULT_LOCALE, EnvFeatureFlagBackend, EventStream, ExpiredTokenException, FeatureFlags, ForbiddenException, HTTP_500_MARKER, InvalidTokenException, JSONLogger, JWTUtils, LocalUploadStorage, MemoryBroker, MemoryCacheManager, MemoryFeatureFlagBackend, MemorySessionStore, MemoryThrottleBackend, MessageCatalog, NotFoundException, PHONE_BR_PATTERN, PasswordUtils, REQUEST_ID_HEADER, RabbitBroker, RedisCacheManager, Region, SSEBroker, ServerSentEvent, SessionService, TaskManager, TooManyRequestsException, UF, UnauthorizedException, UserAuthService, ValidationException, WebSocketHub, attachWebSocketHub, authResponseSchema, baseAppSettingsSchema, baseAppSettingsShape, baseResponseSchema, bearerToken, buildContentDisposition, cached3 as cached, cepField, citiesByUf, cnpjField, coerceFlag, configureLogging, corsSettingsShape, cpfField, cpfOrCnpjField, createApp, createOpenApiRegistry, createdByColumn, cursorPaginationFilterSchema, cursorPaginationSchema, databaseSettingsShape, decodeCursor, defaultMessageCatalog, defineEnum, deletedAtColumn, encodeCursor, generateOpaqueToken, generateOpenApiDocument, getAuth, getConditions, getPaginationConditions, getRequestId, getState, hashOpaqueToken, isValidCep, isValidCity, isValidCnpj, isValidCpf, isValidCpfCnpj, isValidPhoneBr, isValidUf, listStates, loadSettings, loginSchema, makeAppExceptionHandler, makeAuthRouter, makeFlagGuard, makeHealthRouter, makeJwtAuthMiddleware, makeSessionMiddleware, makeUnhandledExceptionHandler, modifyDict, mountOpenApiJson, mountRedoc, mountSwaggerUi, normalizeCep, normalizeCnpj, normalizeCpf, normalizeCpfCnpj, normalizePhoneBr, normalizeUf, notFoundHandler, onlyDigits, paginationFilterSchema, paginationSchema, parseAcceptLanguage, parseCookies, phoneBrField, refreshSchema, registerExceptionHandlers, requestIdMiddleware, requireRoles, runServer, runWithRequestContext, serverSettingsShape, sessionCookie, setRequestId, signupSchema, sseResponse, statesByRegion, tableNameFor, toDict, toUtc, tokenFromUrl, tokenPairSchema, ufField, updatedByColumn, userPublicSchema, utcnow, verifyOpaqueToken, wsEnvelopeSchema };
7265
8107
  //# sourceMappingURL=index.js.map
7266
8108
  //# sourceMappingURL=index.js.map