@guren/server 2.14.0 → 2.16.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/{DocsViewerServiceProvider-qFCaeiUv.js → DocsViewerServiceProvider-D2cuVgtN.js} +7 -12
- package/dist/{Hash-CzrwKeDp.js → Hash-D2DFd8xX.js} +12 -57
- package/dist/{HttpException-BkS3euNG.js → HttpException-BlQ2yEmT.js} +2 -79
- package/dist/{Job-BVT813DO.js → Job-DxQK8h3P.js} +24 -179
- package/dist/{McpServiceProvider-BawsYjgN.js → McpServiceProvider-nNcbCIQX.js} +11 -20
- package/dist/{Random-DbIJ22KQ.js → Random-S0OvCuR8.js} +9 -106
- package/dist/agent/AgentApprovalRequested.d.ts +31 -0
- package/dist/agent/approval.d.ts +184 -0
- package/dist/agent/audit-emitter.d.ts +36 -0
- package/dist/agent/audit.d.ts +67 -0
- package/dist/agent/derive.d.ts +32 -66
- package/dist/agent/dispatch.d.ts +53 -63
- package/dist/agent/events.d.ts +11 -19
- package/dist/agent/meta-tools.d.ts +34 -0
- package/dist/agent/public.d.ts +14 -0
- package/dist/agent/public.js +2 -0
- package/dist/agent/redact.d.ts +16 -38
- package/dist/agent/scopes.d.ts +23 -69
- package/dist/auth/AuthManager.d.ts +19 -40
- package/dist/auth/AuthenticatableModel.d.ts +2 -2
- package/dist/auth/SessionGuard.d.ts +0 -1
- package/dist/auth/TokenGuard.d.ts +6 -26
- package/dist/auth/api-token.d.ts +16 -148
- package/dist/auth/context.d.ts +5 -10
- package/dist/auth/email-verification.d.ts +29 -127
- package/dist/auth/index.js +1 -1
- package/dist/auth/oauth/index.d.ts +35 -57
- package/dist/auth/password/DefaultHasher.d.ts +9 -5
- package/dist/auth/password/PasswordHasher.d.ts +7 -0
- package/dist/auth/password/hash-format.d.ts +11 -0
- package/dist/auth/password-reset.d.ts +16 -42
- package/dist/auth/providers/ModelUserProvider.d.ts +3 -4
- package/dist/auth/providers/UserProvider.d.ts +4 -4
- package/dist/auth/signed-token.d.ts +18 -0
- package/dist/auth/types.d.ts +9 -32
- package/dist/auth/utils.d.ts +6 -27
- package/dist/{auth-BSUc14CD.js → auth-CbdPH4Cd.js} +252 -496
- package/dist/authorization/Gate.d.ts +9 -145
- package/dist/authorization/Policy.d.ts +6 -53
- package/dist/authorization/index.js +1 -1
- package/dist/authorization/middleware.d.ts +16 -62
- package/dist/authorization/types.d.ts +8 -64
- package/dist/{authorization-CdfbCj8J.js → authorization-CdEWZZw3.js} +44 -309
- package/dist/broadcasting/BroadcastManager.d.ts +29 -149
- package/dist/broadcasting/channels/Channel.d.ts +1 -19
- package/dist/broadcasting/channels/PresenceChannel.d.ts +1 -55
- package/dist/broadcasting/channels/PrivateChannel.d.ts +1 -23
- package/dist/broadcasting/drivers/MemoryDriver.d.ts +18 -70
- package/dist/broadcasting/drivers/RedisDriver.d.ts +3 -80
- package/dist/broadcasting/drivers/index.d.ts +1 -1
- package/dist/broadcasting/index.d.ts +1 -1
- package/dist/broadcasting/index.js +1 -1
- package/dist/broadcasting/types.d.ts +7 -88
- package/dist/{broadcasting-BxU-LIfF.js → broadcasting-B5RzB0A2.js} +87 -414
- package/dist/cache/CacheManager.d.ts +8 -62
- package/dist/cache/TaggedCache.d.ts +2 -35
- package/dist/cache/index.js +1 -1
- package/dist/cache/stores/FileStore.d.ts +2 -36
- package/dist/cache/stores/MemoryStore.d.ts +2 -26
- package/dist/cache/stores/RedisStore.d.ts +1 -29
- package/dist/cache/types.d.ts +21 -128
- package/dist/{cache-BYnZf2FR.js → cache-BOOIr324.js} +19 -198
- package/dist/client-C4TEr_Dd.js +9 -0
- package/dist/console/Command.d.ts +17 -126
- package/dist/console/ConsoleKernel.d.ts +2 -74
- package/dist/console/Input.d.ts +5 -113
- package/dist/console/Output.d.ts +3 -99
- package/dist/console/types.d.ts +3 -33
- package/dist/container/Container.d.ts +12 -103
- package/dist/container/ServiceProvider.d.ts +17 -61
- package/dist/container/bindings.d.ts +10 -12
- package/dist/container/defineModule.d.ts +16 -54
- package/dist/container/definePlugin.d.ts +12 -45
- package/dist/container/types.d.ts +2 -34
- package/dist/context-CXk_umpT.js +21 -0
- package/dist/database/Factory.d.ts +4 -57
- package/dist/database/Seeder.d.ts +8 -46
- package/dist/database/SeederRunner.d.ts +3 -33
- package/dist/database/types.d.ts +3 -24
- package/dist/discovery/AutoDiscovery.d.ts +10 -82
- package/dist/dispatch-BAwzftws.js +238 -0
- package/dist/docs-viewer/DocsViewerServiceProvider.d.ts +2 -5
- package/dist/docs-viewer/endpoint.d.ts +5 -11
- package/dist/encryption/Encrypter.d.ts +5 -71
- package/dist/encryption/Hash.d.ts +7 -57
- package/dist/encryption/Random.d.ts +6 -99
- package/dist/encryption/index.js +3 -3
- package/dist/encryption/types.d.ts +10 -72
- package/dist/{encryption-DJtkkhiw.js → encryption-uwP3Di4D.js} +13 -79
- package/dist/{endpoint-D6GIqOpe.js → endpoint-BXipU2Jj.js} +6 -12
- package/dist/endpoint-Dsw3Acvq.js +26 -0
- package/dist/errors/ExceptionHandler.d.ts +3 -90
- package/dist/errors/HttpException.d.ts +2 -79
- package/dist/errors/debug-page.d.ts +3 -17
- package/dist/errors/error-page.d.ts +1 -0
- package/dist/errors/exceptions/AuthenticationException.d.ts +2 -24
- package/dist/errors/exceptions/AuthorizationException.d.ts +1 -24
- package/dist/errors/exceptions/MethodNotAllowedException.d.ts +1 -23
- package/dist/errors/exceptions/NotFoundHttpException.d.ts +1 -27
- package/dist/errors/exceptions/ValidationException.d.ts +2 -43
- package/dist/errors/types.d.ts +1 -25
- package/dist/{errors-BgeOl_DC.js → errors-JpkcCjjn.js} +3 -41
- package/dist/events/Event.d.ts +1 -28
- package/dist/events/EventManager.d.ts +5 -107
- package/dist/events/Listener.d.ts +7 -55
- package/dist/events/builtin.d.ts +0 -24
- package/dist/events/index.js +1 -1
- package/dist/events/types.d.ts +4 -22
- package/dist/{events-aIH_bWRx.js → events-DOmAHDeP.js} +11 -191
- package/dist/facades/index.d.ts +2 -14
- package/dist/health/HealthCheck.d.ts +0 -24
- package/dist/health/HealthManager.d.ts +0 -36
- package/dist/health/checks/CacheCheck.d.ts +2 -17
- package/dist/health/checks/CustomCheck.d.ts +0 -9
- package/dist/health/checks/DatabaseCheck.d.ts +2 -17
- package/dist/health/checks/MemoryCheck.d.ts +3 -12
- package/dist/health/checks/RedisCheck.d.ts +1 -13
- package/dist/health/checks/StorageCheck.d.ts +2 -17
- package/dist/health/index.js +1 -1
- package/dist/health/types.d.ts +4 -24
- package/dist/{health-VWdWXRwh.js → health-DQSvUplL.js} +1 -76
- package/dist/hot-disposables-B5BXqaIH.js +167 -0
- package/dist/hot-reload/hot-disposables.d.ts +22 -74
- package/dist/hot-reload/testing.d.ts +4 -9
- package/dist/http/Application.d.ts +62 -208
- package/dist/http/FormRequest.d.ts +4 -55
- package/dist/http/dev-assets.d.ts +16 -18
- package/dist/http/dev-banner.d.ts +2 -5
- package/dist/http/inertia-assets.d.ts +3 -6
- package/dist/http/middleware/capabilities.d.ts +27 -50
- package/dist/http/middleware/cors.d.ts +4 -29
- package/dist/http/middleware/csp.d.ts +2 -25
- package/dist/http/middleware/csrf.d.ts +20 -95
- package/dist/http/middleware/detect-locale.d.ts +7 -27
- package/dist/http/middleware/force-https.d.ts +6 -24
- package/dist/http/middleware/host-authorization.d.ts +2 -12
- package/dist/http/middleware/loopback-guard.d.ts +15 -33
- package/dist/http/middleware/rate-limit.d.ts +29 -126
- package/dist/http/middleware/redirect-safety.d.ts +2 -20
- package/dist/http/middleware/request-id.d.ts +1 -14
- package/dist/http/middleware/request-logging.d.ts +2 -13
- package/dist/http/middleware/response-headers.d.ts +5 -13
- package/dist/http/middleware/security-headers.d.ts +2 -16
- package/dist/http/middleware/session.d.ts +15 -34
- package/dist/http/middleware/validation-errors-cookie.d.ts +7 -18
- package/dist/http/middleware/validation.d.ts +8 -63
- package/dist/http/public-assets.d.ts +14 -0
- package/dist/http/request.d.ts +49 -48
- package/dist/http/resources/CursorPaginator.d.ts +5 -64
- package/dist/http/resources/Paginator.d.ts +7 -84
- package/dist/http/resources/Resource.d.ts +9 -65
- package/dist/http/resources/ResourceCollection.d.ts +5 -39
- package/dist/http/resources/types.d.ts +10 -30
- package/dist/http/static-documents.d.ts +21 -0
- package/dist/http/validation/Validator.d.ts +25 -85
- package/dist/http/validation/rules.d.ts +48 -147
- package/dist/http/validation/types.d.ts +16 -61
- package/dist/http/vite-assets.d.ts +5 -28
- package/dist/http/vite-dev-server.d.ts +6 -9
- package/dist/http/vite-manifest.d.ts +19 -33
- package/dist/i18n/I18nManager.d.ts +12 -78
- package/dist/i18n/Translator.d.ts +3 -54
- package/dist/i18n/index.js +1 -1
- package/dist/i18n/loaders/JsonLoader.d.ts +4 -31
- package/dist/i18n/loaders/MemoryLoader.d.ts +1 -21
- package/dist/i18n/pluralization.d.ts +4 -13
- package/dist/i18n/types.d.ts +17 -69
- package/dist/{i18n-mcWi7LCd.js → i18n-Cg1aajRP.js} +31 -225
- package/dist/index.d.ts +13 -4
- package/dist/index.js +1036 -3080
- package/dist/internal/agent-preflight.d.ts +9 -21
- package/dist/internal/request.d.ts +10 -17
- package/dist/internal/request.js +2 -2
- package/dist/internal/route-path.d.ts +9 -21
- package/dist/internal/route-path.js +9 -21
- package/dist/internal/zod-compat.d.ts +55 -122
- package/dist/internal/zod-compat.js +39 -70
- package/dist/internal/zod-json-schema.d.ts +18 -54
- package/dist/internal/zod-json-schema.js +46 -123
- package/dist/jsx-runtime.d.ts +3 -8
- package/dist/lambda/index.d.ts +17 -79
- package/dist/lambda/index.js +11 -59
- package/dist/logging/LogManager.d.ts +1 -51
- package/dist/logging/Logger.d.ts +8 -47
- package/dist/logging/channels/ConsoleChannel.d.ts +1 -3
- package/dist/logging/channels/DailyFileChannel.d.ts +1 -5
- package/dist/logging/channels/FileChannel.d.ts +1 -3
- package/dist/logging/daily-file-path.d.ts +15 -0
- package/dist/logging/index.d.ts +1 -0
- package/dist/logging/index.js +2 -2
- package/dist/logging/types.d.ts +13 -45
- package/dist/{logging-CPaRMuPP.js → logging-zOY1XCtq.js} +83 -129
- package/dist/{loopback-guard-D0Kc7-xc.js → loopback-guard-DT9gHF0F.js} +41 -92
- package/dist/mail/Mail.d.ts +10 -106
- package/dist/mail/MailManager.d.ts +2 -60
- package/dist/mail/index.js +1 -1
- package/dist/mail/transports/LogTransport.d.ts +1 -4
- package/dist/mail/transports/MemoryTransport.d.ts +1 -63
- package/dist/mail/transports/ResendTransport.d.ts +1 -23
- package/dist/mail/transports/SmtpTransport.d.ts +1 -31
- package/dist/mail/types.d.ts +28 -150
- package/dist/{mail-DfEfSQEr.js → mail-76DWy9lj.js} +31 -289
- package/dist/mcp/McpServiceProvider.d.ts +4 -10
- package/dist/mcp/create-mcp-server.d.ts +25 -57
- package/dist/mcp/endpoint.d.ts +12 -36
- package/dist/mcp/index.js +1 -1
- package/dist/mvc/Controller.d.ts +46 -253
- package/dist/mvc/Router.d.ts +50 -129
- package/dist/mvc/inertia/InertiaEngine.d.ts +18 -48
- package/dist/mvc/inertia/shared.d.ts +6 -19
- package/dist/mvc/mount-route.d.ts +2 -6
- package/dist/mvc/view.d.ts +1 -2
- package/dist/notifications/Notification.d.ts +8 -75
- package/dist/notifications/NotificationManager.d.ts +15 -88
- package/dist/notifications/channels/DatabaseChannel.d.ts +1 -41
- package/dist/notifications/channels/MailChannel.d.ts +2 -31
- package/dist/notifications/channels/MemoryChannel.d.ts +2 -62
- package/dist/notifications/channels/SlackChannel.d.ts +3 -48
- package/dist/notifications/index.js +1 -1
- package/dist/notifications/notifiable-type.d.ts +3 -6
- package/dist/notifications/registry.d.ts +8 -27
- package/dist/notifications/types.d.ts +16 -53
- package/dist/{notifications-BGqmUNZ8.js → notifications-okWFs9ly.js} +37 -335
- package/dist/providers/AuthServiceProvider.d.ts +1 -3
- package/dist/providers/AuthorizationServiceProvider.d.ts +2 -3
- package/dist/providers/BroadcastServiceProvider.d.ts +1 -3
- package/dist/providers/CacheServiceProvider.d.ts +1 -3
- package/dist/providers/EncryptionServiceProvider.d.ts +1 -3
- package/dist/providers/ErrorServiceProvider.d.ts +1 -4
- package/dist/providers/EventServiceProvider.d.ts +1 -3
- package/dist/providers/HealthServiceProvider.d.ts +1 -3
- package/dist/providers/I18nServiceProvider.d.ts +6 -10
- package/dist/providers/InertiaServiceProvider.d.ts +1 -4
- package/dist/providers/LogServiceProvider.d.ts +1 -3
- package/dist/providers/MailServiceProvider.d.ts +1 -3
- package/dist/providers/NotificationServiceProvider.d.ts +1 -3
- package/dist/providers/OAuthServiceProvider.d.ts +1 -3
- package/dist/providers/QueueServiceProvider.d.ts +1 -3
- package/dist/providers/SchedulingServiceProvider.d.ts +1 -3
- package/dist/providers/StorageServiceProvider.d.ts +1 -3
- package/dist/queue/FailedJobReporter.d.ts +1 -24
- package/dist/queue/Job.d.ts +12 -99
- package/dist/queue/QueueManager.d.ts +6 -57
- package/dist/queue/Worker.d.ts +4 -65
- package/dist/queue/drivers/MemoryDriver.d.ts +3 -44
- package/dist/queue/drivers/RedisDriver.d.ts +7 -65
- package/dist/queue/drivers/SqsDriver.d.ts +6 -57
- package/dist/queue/drivers/SyncDriver.d.ts +10 -13
- package/dist/queue/index.js +2 -2
- package/dist/queue/types.d.ts +10 -92
- package/dist/{queue-CXrRK85G.js → queue-CcXCjuJI.js} +32 -260
- package/dist/redis/RedisApiTokenStore.d.ts +4 -56
- package/dist/redis/RedisEmailVerificationStore.d.ts +4 -37
- package/dist/redis/RedisOAuthStateStore.d.ts +2 -10
- package/dist/redis/RedisPasswordResetStore.d.ts +4 -37
- package/dist/redis/RedisRateLimitStore.d.ts +16 -49
- package/dist/redis/RedisSessionStore.d.ts +3 -44
- package/dist/redis/client.d.ts +2 -32
- package/dist/redis/index.js +47 -232
- package/dist/redis/redis-values.d.ts +6 -12
- package/dist/redis/scan-keys.d.ts +1 -4
- package/dist/request-CLOYytGP.js +68 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.js +108 -74
- package/dist/scheduling/CronParser.d.ts +0 -29
- package/dist/scheduling/Schedule.d.ts +1 -153
- package/dist/scheduling/ScheduledTask.d.ts +0 -31
- package/dist/scheduling/Scheduler.d.ts +0 -70
- package/dist/scheduling/index.js +1 -1
- package/dist/scheduling/types.d.ts +4 -65
- package/dist/{scheduling-CPiEdtQd.js → scheduling-C6tcOtUl.js} +2 -287
- package/dist/storage/StorageManager.d.ts +0 -48
- package/dist/storage/drivers/LocalDriver.d.ts +1 -25
- package/dist/storage/drivers/MemoryDriver.d.ts +5 -35
- package/dist/storage/drivers/S3Driver.d.ts +6 -36
- package/dist/storage/drivers/s3-acl.d.ts +5 -10
- package/dist/storage/index.js +1 -1
- package/dist/storage/types.d.ts +27 -265
- package/dist/{storage-TLD9t0Q2.js → storage-BB59f07j.js} +17 -157
- package/dist/support/contained-path.d.ts +10 -20
- package/dist/support/error-polyfill.d.ts +4 -6
- package/dist/support/expiry.d.ts +17 -33
- package/dist/support/expiry.js +17 -33
- package/dist/support/import-map.d.ts +3 -7
- package/dist/support/inertia-defaults.d.ts +5 -10
- package/dist/support/redirect-target.d.ts +4 -6
- package/dist/support/stack-frames.d.ts +11 -50
- package/dist/support/trim-slashes.d.ts +4 -7
- package/dist/support/warn-once.d.ts +2 -6
- package/dist/{trim-slashes-ZJ_VsY5s.js → trim-slashes-BLY9hKAa.js} +4 -7
- package/dist/{vite-manifest-BGfWbPYp.js → vite-manifest-CTQp-SOo.js} +34 -58
- package/package.json +6 -2
- package/dist/client-Dd7vLcqp.js +0 -27
- package/dist/context-DoD0c7J_.js +0 -26
- package/dist/endpoint-D-20PiHY.js +0 -46
- package/dist/hot-disposables-WsTX2ySN.js +0 -281
- package/dist/request-CRVuIiQr.js +0 -78
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
import { a as parseRequestPayload } from "./request-
|
|
2
|
-
import { n as isHotReloadRuntime, t as claimHotDisposable } from "./hot-disposables-
|
|
3
|
-
import { c as randomHex } from "./Random-
|
|
1
|
+
import { a as parseRequestPayload } from "./request-CLOYytGP.js";
|
|
2
|
+
import { n as isHotReloadRuntime, t as claimHotDisposable } from "./hot-disposables-B5BXqaIH.js";
|
|
3
|
+
import { c as randomHex } from "./Random-S0OvCuR8.js";
|
|
4
4
|
//#region src/broadcasting/channels/Channel.ts
|
|
5
|
-
/**
|
|
6
|
-
* Base broadcast channel.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* const channel = new Channel('notifications', driver)
|
|
11
|
-
* await channel.broadcast('NewMessage', { content: 'Hello!' })
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
5
|
var Channel = class {
|
|
15
6
|
name;
|
|
16
7
|
driver;
|
|
@@ -18,97 +9,42 @@ var Channel = class {
|
|
|
18
9
|
this.name = name;
|
|
19
10
|
this.driver = driver;
|
|
20
11
|
}
|
|
21
|
-
/**
|
|
22
|
-
* Broadcast an event to this channel.
|
|
23
|
-
*/
|
|
24
12
|
async broadcast(event, data) {
|
|
25
13
|
await this.driver.publish(this.name, event, data);
|
|
26
14
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Subscribe to events on this channel.
|
|
29
|
-
* Returns an unsubscribe function.
|
|
30
|
-
*/
|
|
15
|
+
/** Returns an unsubscribe function. */
|
|
31
16
|
subscribe(callback) {
|
|
32
17
|
return this.driver.subscribe(this.name, (e) => {
|
|
33
18
|
callback(e.event, e.data);
|
|
34
19
|
});
|
|
35
20
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Get the full channel name for broadcasting.
|
|
38
|
-
*/
|
|
39
21
|
getChannelName() {
|
|
40
22
|
return this.name;
|
|
41
23
|
}
|
|
42
24
|
};
|
|
43
25
|
//#endregion
|
|
44
26
|
//#region src/broadcasting/channels/PrivateChannel.ts
|
|
45
|
-
/**
|
|
46
|
-
* Private broadcast channel.
|
|
47
|
-
*
|
|
48
|
-
* Requires authentication to subscribe.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```typescript
|
|
52
|
-
* const channel = new PrivateChannel('orders.123', driver)
|
|
53
|
-
* await channel.broadcast('OrderUpdated', { status: 'shipped' })
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
27
|
+
/** Broadcast channel that requires authentication to subscribe. */
|
|
56
28
|
var PrivateChannel = class PrivateChannel extends Channel {
|
|
57
|
-
/**
|
|
58
|
-
* Channel name prefix.
|
|
59
|
-
*/
|
|
60
29
|
static PREFIX = "private-";
|
|
61
30
|
constructor(name, driver) {
|
|
62
31
|
const channelName = name.startsWith(PrivateChannel.PREFIX) ? name : `${PrivateChannel.PREFIX}${name}`;
|
|
63
32
|
super(channelName, driver);
|
|
64
33
|
}
|
|
65
|
-
/**
|
|
66
|
-
* Get the channel name without the private prefix.
|
|
67
|
-
*/
|
|
68
34
|
getBaseName() {
|
|
69
35
|
return this.name.startsWith(PrivateChannel.PREFIX) ? this.name.slice(PrivateChannel.PREFIX.length) : this.name;
|
|
70
36
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Check if a channel name is a private channel.
|
|
73
|
-
*/
|
|
74
37
|
static isPrivateChannel(name) {
|
|
75
38
|
return name.startsWith(PrivateChannel.PREFIX);
|
|
76
39
|
}
|
|
77
|
-
/**
|
|
78
|
-
* Normalize a channel name to include the private prefix.
|
|
79
|
-
*/
|
|
80
40
|
static normalize(name) {
|
|
81
41
|
return name.startsWith(PrivateChannel.PREFIX) ? name : `${PrivateChannel.PREFIX}${name}`;
|
|
82
42
|
}
|
|
83
43
|
};
|
|
84
44
|
//#endregion
|
|
85
45
|
//#region src/broadcasting/channels/PresenceChannel.ts
|
|
86
|
-
/**
|
|
87
|
-
* Presence broadcast channel.
|
|
88
|
-
*
|
|
89
|
-
* Tracks members who have joined the channel.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```typescript
|
|
93
|
-
* const channel = new PresenceChannel('chat.1', driver)
|
|
94
|
-
*
|
|
95
|
-
* // Join channel
|
|
96
|
-
* await channel.join({ id: 1, info: { name: 'John' } })
|
|
97
|
-
*
|
|
98
|
-
* // Get members
|
|
99
|
-
* const members = channel.members()
|
|
100
|
-
*
|
|
101
|
-
* // Leave channel
|
|
102
|
-
* await channel.leave(1)
|
|
103
|
-
*
|
|
104
|
-
* // Broadcast
|
|
105
|
-
* await channel.broadcast('UserTyping', { userId: 1 })
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
46
|
+
/** Broadcast channel that tracks the members who have joined it. */
|
|
108
47
|
var PresenceChannel = class PresenceChannel extends Channel {
|
|
109
|
-
/**
|
|
110
|
-
* Channel name prefix.
|
|
111
|
-
*/
|
|
112
48
|
static PREFIX = "presence-";
|
|
113
49
|
presenceDriver;
|
|
114
50
|
constructor(name, driver) {
|
|
@@ -117,66 +53,36 @@ var PresenceChannel = class PresenceChannel extends Channel {
|
|
|
117
53
|
if (this.isPresenceDriver(driver)) this.presenceDriver = driver;
|
|
118
54
|
else throw new Error("Driver does not support presence channels. Ensure the driver implements PresenceBroadcastDriver interface.");
|
|
119
55
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Check if a driver supports presence.
|
|
122
|
-
*/
|
|
123
56
|
isPresenceDriver(driver) {
|
|
124
57
|
return "getMembers" in driver && "addMember" in driver && "removeMember" in driver;
|
|
125
58
|
}
|
|
126
|
-
/**
|
|
127
|
-
* Get members in this channel.
|
|
128
|
-
*/
|
|
129
59
|
members() {
|
|
130
60
|
return this.presenceDriver.getMembers(this.name);
|
|
131
61
|
}
|
|
132
|
-
/**
|
|
133
|
-
* Join the channel.
|
|
134
|
-
*/
|
|
135
62
|
async join(member) {
|
|
136
63
|
this.presenceDriver.addMember(this.name, member);
|
|
137
64
|
await this.broadcast("presence:joining", { member });
|
|
138
65
|
}
|
|
139
|
-
/**
|
|
140
|
-
* Leave the channel.
|
|
141
|
-
*/
|
|
142
66
|
async leave(memberId) {
|
|
143
67
|
const member = this.members().find((m) => m.id === memberId);
|
|
144
68
|
this.presenceDriver.removeMember(this.name, memberId);
|
|
145
69
|
if (member) await this.broadcast("presence:leaving", { member });
|
|
146
70
|
}
|
|
147
|
-
/**
|
|
148
|
-
* Check if a member is in the channel.
|
|
149
|
-
*/
|
|
150
71
|
hasMember(memberId) {
|
|
151
72
|
return this.members().some((m) => m.id === memberId);
|
|
152
73
|
}
|
|
153
|
-
/**
|
|
154
|
-
* Get member by ID.
|
|
155
|
-
*/
|
|
156
74
|
getMember(memberId) {
|
|
157
75
|
return this.members().find((m) => m.id === memberId);
|
|
158
76
|
}
|
|
159
|
-
/**
|
|
160
|
-
* Get member count.
|
|
161
|
-
*/
|
|
162
77
|
count() {
|
|
163
78
|
return this.members().length;
|
|
164
79
|
}
|
|
165
|
-
/**
|
|
166
|
-
* Get the channel name without the presence prefix.
|
|
167
|
-
*/
|
|
168
80
|
getBaseName() {
|
|
169
81
|
return this.name.startsWith(PresenceChannel.PREFIX) ? this.name.slice(PresenceChannel.PREFIX.length) : this.name;
|
|
170
82
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Check if a channel name is a presence channel.
|
|
173
|
-
*/
|
|
174
83
|
static isPresenceChannel(name) {
|
|
175
84
|
return name.startsWith(PresenceChannel.PREFIX);
|
|
176
85
|
}
|
|
177
|
-
/**
|
|
178
|
-
* Normalize a channel name to include the presence prefix.
|
|
179
|
-
*/
|
|
180
86
|
static normalize(name) {
|
|
181
87
|
return name.startsWith(PresenceChannel.PREFIX) ? name : `${PresenceChannel.PREFIX}${name}`;
|
|
182
88
|
}
|
|
@@ -184,43 +90,18 @@ var PresenceChannel = class PresenceChannel extends Channel {
|
|
|
184
90
|
//#endregion
|
|
185
91
|
//#region src/broadcasting/drivers/MemoryDriver.ts
|
|
186
92
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* Stores all subscriptions and presence data in memory.
|
|
190
|
-
* Useful for testing and single-server deployments.
|
|
191
|
-
*
|
|
192
|
-
* @example
|
|
193
|
-
* ```typescript
|
|
194
|
-
* const driver = new MemoryDriver()
|
|
195
|
-
*
|
|
196
|
-
* // Subscribe to a channel
|
|
197
|
-
* const unsubscribe = driver.subscribe('notifications', (event) => {
|
|
198
|
-
* console.log('Received:', event)
|
|
199
|
-
* })
|
|
200
|
-
*
|
|
201
|
-
* // Publish an event
|
|
202
|
-
* await driver.publish('notifications', 'NewMessage', { content: 'Hello!' })
|
|
203
|
-
*
|
|
204
|
-
* // Unsubscribe
|
|
205
|
-
* unsubscribe()
|
|
206
|
-
* ```
|
|
93
|
+
* Keeps subscriptions and presence data in memory — for testing and
|
|
94
|
+
* single-server deployments.
|
|
207
95
|
*/
|
|
208
96
|
var MemoryDriver = class {
|
|
209
|
-
/**
|
|
210
|
-
* Channel subscribers.
|
|
211
|
-
*/
|
|
212
97
|
subscribers = /* @__PURE__ */ new Map();
|
|
213
|
-
/**
|
|
214
|
-
* Presence channel members.
|
|
215
|
-
*/
|
|
216
98
|
presenceMembers = /* @__PURE__ */ new Map();
|
|
217
|
-
/**
|
|
218
|
-
* Published events (for testing).
|
|
219
|
-
*/
|
|
99
|
+
/** Kept for testing only, capped at `maxPublishedEvents`. */
|
|
220
100
|
publishedEvents = [];
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
101
|
+
maxPublishedEvents;
|
|
102
|
+
constructor(options = {}) {
|
|
103
|
+
this.maxPublishedEvents = Math.max(0, Math.floor(options.maxPublishedEvents ?? 1e3));
|
|
104
|
+
}
|
|
224
105
|
async publish(channel, event, data) {
|
|
225
106
|
const broadcastEvent = {
|
|
226
107
|
channel,
|
|
@@ -228,7 +109,7 @@ var MemoryDriver = class {
|
|
|
228
109
|
data,
|
|
229
110
|
timestamp: /* @__PURE__ */ new Date()
|
|
230
111
|
};
|
|
231
|
-
this.
|
|
112
|
+
this.recordPublishedEvent(broadcastEvent);
|
|
232
113
|
const callbacks = this.subscribers.get(channel);
|
|
233
114
|
if (callbacks) for (const callback of callbacks) try {
|
|
234
115
|
callback(broadcastEvent);
|
|
@@ -236,9 +117,11 @@ var MemoryDriver = class {
|
|
|
236
117
|
console.error(`Error in broadcast subscriber:`, error);
|
|
237
118
|
}
|
|
238
119
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
120
|
+
recordPublishedEvent(event) {
|
|
121
|
+
if (this.maxPublishedEvents === 0) return;
|
|
122
|
+
this.publishedEvents.push(event);
|
|
123
|
+
if (this.publishedEvents.length > this.maxPublishedEvents) this.publishedEvents.splice(0, this.publishedEvents.length - this.maxPublishedEvents);
|
|
124
|
+
}
|
|
242
125
|
subscribe(channel, callback) {
|
|
243
126
|
let callbacks = this.subscribers.get(channel);
|
|
244
127
|
if (!callbacks) {
|
|
@@ -250,9 +133,6 @@ var MemoryDriver = class {
|
|
|
250
133
|
this.unsubscribe(channel, callback);
|
|
251
134
|
};
|
|
252
135
|
}
|
|
253
|
-
/**
|
|
254
|
-
* Unsubscribe from a channel.
|
|
255
|
-
*/
|
|
256
136
|
unsubscribe(channel, callback) {
|
|
257
137
|
const callbacks = this.subscribers.get(channel);
|
|
258
138
|
if (callbacks) {
|
|
@@ -260,16 +140,10 @@ var MemoryDriver = class {
|
|
|
260
140
|
if (callbacks.size === 0) this.subscribers.delete(channel);
|
|
261
141
|
}
|
|
262
142
|
}
|
|
263
|
-
/**
|
|
264
|
-
* Get members of a presence channel.
|
|
265
|
-
*/
|
|
266
143
|
getMembers(channel) {
|
|
267
144
|
const members = this.presenceMembers.get(channel);
|
|
268
145
|
return members ? Array.from(members.values()) : [];
|
|
269
146
|
}
|
|
270
|
-
/**
|
|
271
|
-
* Add a member to a presence channel.
|
|
272
|
-
*/
|
|
273
147
|
addMember(channel, member) {
|
|
274
148
|
let members = this.presenceMembers.get(channel);
|
|
275
149
|
if (!members) {
|
|
@@ -278,9 +152,6 @@ var MemoryDriver = class {
|
|
|
278
152
|
}
|
|
279
153
|
members.set(member.id, member);
|
|
280
154
|
}
|
|
281
|
-
/**
|
|
282
|
-
* Remove a member from a presence channel.
|
|
283
|
-
*/
|
|
284
155
|
removeMember(channel, memberId) {
|
|
285
156
|
const members = this.presenceMembers.get(channel);
|
|
286
157
|
if (members) {
|
|
@@ -288,116 +159,51 @@ var MemoryDriver = class {
|
|
|
288
159
|
if (members.size === 0) this.presenceMembers.delete(channel);
|
|
289
160
|
}
|
|
290
161
|
}
|
|
291
|
-
/**
|
|
292
|
-
* Check if a channel has subscribers.
|
|
293
|
-
*/
|
|
294
162
|
hasSubscribers(channel) {
|
|
295
163
|
const callbacks = this.subscribers.get(channel);
|
|
296
164
|
return callbacks !== void 0 && callbacks.size > 0;
|
|
297
165
|
}
|
|
298
|
-
/**
|
|
299
|
-
* Get subscriber count for a channel.
|
|
300
|
-
*/
|
|
301
166
|
getSubscriberCount(channel) {
|
|
302
167
|
const callbacks = this.subscribers.get(channel);
|
|
303
168
|
return callbacks ? callbacks.size : 0;
|
|
304
169
|
}
|
|
305
|
-
/**
|
|
306
|
-
* Get all channels with subscribers.
|
|
307
|
-
*/
|
|
308
170
|
getChannels() {
|
|
309
171
|
return Array.from(this.subscribers.keys());
|
|
310
172
|
}
|
|
311
|
-
/**
|
|
312
|
-
* Get all presence channels.
|
|
313
|
-
*/
|
|
314
173
|
getPresenceChannels() {
|
|
315
174
|
return Array.from(this.presenceMembers.keys());
|
|
316
175
|
}
|
|
317
|
-
/**
|
|
318
|
-
* Get all published events (for testing).
|
|
319
|
-
*/
|
|
176
|
+
/** The newest `maxPublishedEvents` at most. */
|
|
320
177
|
getPublishedEvents() {
|
|
321
178
|
return [...this.publishedEvents];
|
|
322
179
|
}
|
|
323
|
-
/**
|
|
324
|
-
* Get published events for a specific channel.
|
|
325
|
-
*/
|
|
326
180
|
getPublishedEventsFor(channel) {
|
|
327
181
|
return this.publishedEvents.filter((e) => e.channel === channel);
|
|
328
182
|
}
|
|
329
|
-
/**
|
|
330
|
-
* Clear all data (for testing).
|
|
331
|
-
*/
|
|
332
183
|
clear() {
|
|
333
184
|
this.subscribers.clear();
|
|
334
185
|
this.presenceMembers.clear();
|
|
335
186
|
this.publishedEvents = [];
|
|
336
187
|
}
|
|
337
|
-
/**
|
|
338
|
-
* Clear published events only.
|
|
339
|
-
*/
|
|
340
188
|
clearPublishedEvents() {
|
|
341
189
|
this.publishedEvents = [];
|
|
342
190
|
}
|
|
343
191
|
};
|
|
344
192
|
//#endregion
|
|
345
193
|
//#region src/broadcasting/drivers/RedisDriver.ts
|
|
346
|
-
/**
|
|
347
|
-
* Redis broadcast driver.
|
|
348
|
-
*
|
|
349
|
-
* Uses Redis pub/sub for multi-server deployments.
|
|
350
|
-
*
|
|
351
|
-
* @example
|
|
352
|
-
* ```typescript
|
|
353
|
-
* import Redis from 'ioredis'
|
|
354
|
-
*
|
|
355
|
-
* const redis = new Redis()
|
|
356
|
-
* const driver = new RedisDriver(redis)
|
|
357
|
-
*
|
|
358
|
-
* // Subscribe to a channel
|
|
359
|
-
* driver.subscribe('notifications', (event) => {
|
|
360
|
-
* console.log('Received:', event)
|
|
361
|
-
* })
|
|
362
|
-
*
|
|
363
|
-
* // Publish an event
|
|
364
|
-
* await driver.publish('notifications', 'NewMessage', { content: 'Hello!' })
|
|
365
|
-
* ```
|
|
366
|
-
*/
|
|
194
|
+
/** Uses Redis pub/sub, for multi-server deployments. */
|
|
367
195
|
var RedisDriver = class {
|
|
368
|
-
/**
|
|
369
|
-
* Publisher Redis client.
|
|
370
|
-
*/
|
|
371
196
|
publisher;
|
|
372
|
-
/**
|
|
373
|
-
* Subscriber Redis client.
|
|
374
|
-
*/
|
|
375
197
|
subscriber;
|
|
376
|
-
/**
|
|
377
|
-
* Local subscribers.
|
|
378
|
-
*/
|
|
379
198
|
subscribers = /* @__PURE__ */ new Map();
|
|
380
|
-
/**
|
|
381
|
-
* Local presence members cache.
|
|
382
|
-
*/
|
|
383
199
|
localPresence = /* @__PURE__ */ new Map();
|
|
384
|
-
/**
|
|
385
|
-
* Presence key prefix.
|
|
386
|
-
*/
|
|
387
200
|
presencePrefix = "broadcasting:presence:";
|
|
388
|
-
/**
|
|
389
|
-
* Whether the driver is initialized.
|
|
390
|
-
*/
|
|
391
|
-
initialized = false;
|
|
392
201
|
constructor(redis, options = {}) {
|
|
393
202
|
this.publisher = redis;
|
|
394
203
|
this.subscriber = redis.duplicate();
|
|
395
204
|
if (options.presencePrefix) this.presencePrefix = options.presencePrefix;
|
|
396
205
|
this.setupSubscriber();
|
|
397
206
|
}
|
|
398
|
-
/**
|
|
399
|
-
* Setup the subscriber client.
|
|
400
|
-
*/
|
|
401
207
|
setupSubscriber() {
|
|
402
208
|
this.subscriber.on("message", (channel, message) => {
|
|
403
209
|
try {
|
|
@@ -413,11 +219,7 @@ var RedisDriver = class {
|
|
|
413
219
|
console.error(`Error parsing broadcast message:`, error);
|
|
414
220
|
}
|
|
415
221
|
});
|
|
416
|
-
this.initialized = true;
|
|
417
222
|
}
|
|
418
|
-
/**
|
|
419
|
-
* Publish an event to a channel.
|
|
420
|
-
*/
|
|
421
223
|
async publish(channel, event, data) {
|
|
422
224
|
const broadcastEvent = {
|
|
423
225
|
channel,
|
|
@@ -427,9 +229,6 @@ var RedisDriver = class {
|
|
|
427
229
|
};
|
|
428
230
|
await this.publisher.publish(channel, JSON.stringify(broadcastEvent));
|
|
429
231
|
}
|
|
430
|
-
/**
|
|
431
|
-
* Subscribe to a channel.
|
|
432
|
-
*/
|
|
433
232
|
subscribe(channel, callback) {
|
|
434
233
|
let callbacks = this.subscribers.get(channel);
|
|
435
234
|
const isNew = !callbacks;
|
|
@@ -445,9 +244,6 @@ var RedisDriver = class {
|
|
|
445
244
|
this.unsubscribe(channel, callback);
|
|
446
245
|
};
|
|
447
246
|
}
|
|
448
|
-
/**
|
|
449
|
-
* Unsubscribe from a channel.
|
|
450
|
-
*/
|
|
451
247
|
unsubscribe(channel, callback) {
|
|
452
248
|
const callbacks = this.subscribers.get(channel);
|
|
453
249
|
if (callbacks) {
|
|
@@ -460,16 +256,10 @@ var RedisDriver = class {
|
|
|
460
256
|
}
|
|
461
257
|
}
|
|
462
258
|
}
|
|
463
|
-
/**
|
|
464
|
-
* Get members of a presence channel.
|
|
465
|
-
*/
|
|
466
259
|
getMembers(channel) {
|
|
467
260
|
const members = this.localPresence.get(channel);
|
|
468
261
|
return members ? Array.from(members.values()) : [];
|
|
469
262
|
}
|
|
470
|
-
/**
|
|
471
|
-
* Get members asynchronously from Redis.
|
|
472
|
-
*/
|
|
473
263
|
async getMembersAsync(channel) {
|
|
474
264
|
const key = `${this.presencePrefix}${channel}`;
|
|
475
265
|
const data = await this.publisher.hgetall(key);
|
|
@@ -482,9 +272,6 @@ var RedisDriver = class {
|
|
|
482
272
|
this.localPresence.set(channel, localMembers);
|
|
483
273
|
return members;
|
|
484
274
|
}
|
|
485
|
-
/**
|
|
486
|
-
* Add a member to a presence channel.
|
|
487
|
-
*/
|
|
488
275
|
addMember(channel, member) {
|
|
489
276
|
let members = this.localPresence.get(channel);
|
|
490
277
|
if (!members) {
|
|
@@ -497,9 +284,6 @@ var RedisDriver = class {
|
|
|
497
284
|
console.error(`Error adding presence member to Redis:`, error);
|
|
498
285
|
});
|
|
499
286
|
}
|
|
500
|
-
/**
|
|
501
|
-
* Remove a member from a presence channel.
|
|
502
|
-
*/
|
|
503
287
|
removeMember(channel, memberId) {
|
|
504
288
|
const members = this.localPresence.get(channel);
|
|
505
289
|
if (members) {
|
|
@@ -511,16 +295,10 @@ var RedisDriver = class {
|
|
|
511
295
|
console.error(`Error removing presence member from Redis:`, error);
|
|
512
296
|
});
|
|
513
297
|
}
|
|
514
|
-
/**
|
|
515
|
-
* Check if a channel has subscribers.
|
|
516
|
-
*/
|
|
517
298
|
hasSubscribers(channel) {
|
|
518
299
|
const callbacks = this.subscribers.get(channel);
|
|
519
300
|
return callbacks !== void 0 && callbacks.size > 0;
|
|
520
301
|
}
|
|
521
|
-
/**
|
|
522
|
-
* Get subscriber count for a channel.
|
|
523
|
-
*/
|
|
524
302
|
getSubscriberCount(channel) {
|
|
525
303
|
const callbacks = this.subscribers.get(channel);
|
|
526
304
|
return callbacks ? callbacks.size : 0;
|
|
@@ -529,19 +307,11 @@ var RedisDriver = class {
|
|
|
529
307
|
//#endregion
|
|
530
308
|
//#region src/broadcasting/BroadcastManager.ts
|
|
531
309
|
/**
|
|
532
|
-
* Best-effort identity for the user behind a connection
|
|
533
|
-
*
|
|
534
|
-
* `
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
* permissive direction, chosen because a stream opened before sign-in has to
|
|
538
|
-
* stay attachable for authorize-after-login to work, and the two cases are
|
|
539
|
-
* indistinguishable from here.
|
|
540
|
-
*
|
|
541
|
-
* So the ownership check below is defence in depth, not the primary control:
|
|
542
|
-
* what actually stops an attach against someone else's stream is that client
|
|
543
|
-
* ids are unguessable. An app whose user objects carry none of these fields
|
|
544
|
-
* gets the unguessable id and no second layer.
|
|
310
|
+
* Best-effort identity for the user behind a connection: `getUser` is
|
|
311
|
+
* application-supplied, so this reads the conventional fields and gives up
|
|
312
|
+
* otherwise. `undefined` marks the stream unowned and therefore attachable, so
|
|
313
|
+
* a stream opened before sign-in still works for authorize-after-login; the
|
|
314
|
+
* ownership check is defence in depth behind unguessable client ids.
|
|
545
315
|
*/
|
|
546
316
|
function resolveClientUserId(user) {
|
|
547
317
|
if (typeof user !== "object" || user === null) return void 0;
|
|
@@ -552,79 +322,30 @@ function resolveClientUserId(user) {
|
|
|
552
322
|
candidate.userId
|
|
553
323
|
]) if (typeof value === "string" || typeof value === "number") return value;
|
|
554
324
|
}
|
|
555
|
-
/**
|
|
556
|
-
* Broadcast manager for real-time event broadcasting.
|
|
557
|
-
*
|
|
558
|
-
* @example
|
|
559
|
-
* ```typescript
|
|
560
|
-
* const broadcast = new BroadcastManager({
|
|
561
|
-
* default: 'memory',
|
|
562
|
-
* drivers: {
|
|
563
|
-
* memory: () => new MemoryDriver(),
|
|
564
|
-
* redis: () => new RedisDriver(redis),
|
|
565
|
-
* },
|
|
566
|
-
* })
|
|
567
|
-
*
|
|
568
|
-
* // Register channel authorizers
|
|
569
|
-
* broadcast
|
|
570
|
-
* .channel('public.*', () => true)
|
|
571
|
-
* .privateChannel('orders.{orderId}', async (channel, user) => {
|
|
572
|
-
* const orderId = channel.split('.')[1]
|
|
573
|
-
* return order.userId === user.id
|
|
574
|
-
* })
|
|
575
|
-
* .presenceChannel('chat.{roomId}', async (channel, user) => {
|
|
576
|
-
* return { id: user.id, info: { name: user.name } }
|
|
577
|
-
* })
|
|
578
|
-
*
|
|
579
|
-
* // Broadcast events
|
|
580
|
-
* await broadcast.broadcast('notifications', 'NewMessage', { content: 'Hello!' })
|
|
581
|
-
*
|
|
582
|
-
* // Or use channel helpers
|
|
583
|
-
* await broadcast.toChannel('notifications').broadcast('NewMessage', data)
|
|
584
|
-
* await broadcast.toPrivate('orders.123').broadcast('OrderUpdated', data)
|
|
585
|
-
* await broadcast.toPresence('chat.1').broadcast('UserTyping', data)
|
|
586
|
-
* ```
|
|
587
|
-
*/
|
|
325
|
+
/** Broadcast manager for real-time event broadcasting. */
|
|
588
326
|
var BroadcastManager = class {
|
|
589
|
-
/**
|
|
590
|
-
* Default driver name.
|
|
591
|
-
*/
|
|
592
327
|
defaultDriver = "memory";
|
|
593
|
-
/**
|
|
594
|
-
* Driver factories.
|
|
595
|
-
*/
|
|
596
328
|
driverFactories = /* @__PURE__ */ new Map();
|
|
597
|
-
/**
|
|
598
|
-
* Resolved drivers.
|
|
599
|
-
*/
|
|
600
329
|
resolvedDrivers = /* @__PURE__ */ new Map();
|
|
601
|
-
/**
|
|
602
|
-
* Channel registrations.
|
|
603
|
-
*/
|
|
604
330
|
channelRegistrations = [];
|
|
605
|
-
/**
|
|
606
|
-
* SSE clients.
|
|
607
|
-
*/
|
|
608
331
|
sseClients = /* @__PURE__ */ new Map();
|
|
332
|
+
wsClients = /* @__PURE__ */ new Map();
|
|
609
333
|
/**
|
|
610
|
-
*
|
|
334
|
+
* Driver-level unsubscribe functions, per client id and channel: the only
|
|
335
|
+
* handle on the subscription `driver().subscribe()` opened. Dropping it
|
|
336
|
+
* leaves the driver fanning out to a client that has already gone — and on
|
|
337
|
+
* Redis, keeps that channel's SUBSCRIBE open for as long as the process lives.
|
|
611
338
|
*/
|
|
612
|
-
|
|
339
|
+
driverSubscriptions = /* @__PURE__ */ new Map();
|
|
613
340
|
constructor(options = {}) {
|
|
614
341
|
if (options.default) this.defaultDriver = options.default;
|
|
615
342
|
if (options.drivers) for (const [name, factory] of Object.entries(options.drivers)) this.registerDriver(name, factory);
|
|
616
343
|
if (!this.driverFactories.has("memory")) this.registerDriver("memory", () => new MemoryDriver());
|
|
617
344
|
}
|
|
618
|
-
/**
|
|
619
|
-
* Register a driver factory.
|
|
620
|
-
*/
|
|
621
345
|
registerDriver(name, factory) {
|
|
622
346
|
this.driverFactories.set(name, factory);
|
|
623
347
|
return this;
|
|
624
348
|
}
|
|
625
|
-
/**
|
|
626
|
-
* Get a driver by name.
|
|
627
|
-
*/
|
|
628
349
|
driver(name) {
|
|
629
350
|
const driverName = name ?? this.defaultDriver;
|
|
630
351
|
const resolved = this.resolvedDrivers.get(driverName);
|
|
@@ -635,9 +356,6 @@ var BroadcastManager = class {
|
|
|
635
356
|
this.resolvedDrivers.set(driverName, driver);
|
|
636
357
|
return driver;
|
|
637
358
|
}
|
|
638
|
-
/**
|
|
639
|
-
* Register a public channel authorizer.
|
|
640
|
-
*/
|
|
641
359
|
channel(pattern, authorizer) {
|
|
642
360
|
this.channelRegistrations.push({
|
|
643
361
|
pattern,
|
|
@@ -646,9 +364,6 @@ var BroadcastManager = class {
|
|
|
646
364
|
});
|
|
647
365
|
return this;
|
|
648
366
|
}
|
|
649
|
-
/**
|
|
650
|
-
* Register a private channel authorizer.
|
|
651
|
-
*/
|
|
652
367
|
privateChannel(pattern, authorizer) {
|
|
653
368
|
this.channelRegistrations.push({
|
|
654
369
|
pattern: PrivateChannel.normalize(pattern),
|
|
@@ -657,9 +372,6 @@ var BroadcastManager = class {
|
|
|
657
372
|
});
|
|
658
373
|
return this;
|
|
659
374
|
}
|
|
660
|
-
/**
|
|
661
|
-
* Register a presence channel authorizer.
|
|
662
|
-
*/
|
|
663
375
|
presenceChannel(pattern, authorizer) {
|
|
664
376
|
this.channelRegistrations.push({
|
|
665
377
|
pattern: PresenceChannel.normalize(pattern),
|
|
@@ -668,33 +380,18 @@ var BroadcastManager = class {
|
|
|
668
380
|
});
|
|
669
381
|
return this;
|
|
670
382
|
}
|
|
671
|
-
/**
|
|
672
|
-
* Broadcast an event to a channel.
|
|
673
|
-
*/
|
|
674
383
|
async broadcast(channelName, event, data) {
|
|
675
384
|
await this.driver().publish(channelName, event, data);
|
|
676
385
|
}
|
|
677
|
-
/**
|
|
678
|
-
* Get a public channel.
|
|
679
|
-
*/
|
|
680
386
|
toChannel(name) {
|
|
681
387
|
return new Channel(name, this.driver());
|
|
682
388
|
}
|
|
683
|
-
/**
|
|
684
|
-
* Get a private channel.
|
|
685
|
-
*/
|
|
686
389
|
toPrivate(name) {
|
|
687
390
|
return new PrivateChannel(name, this.driver());
|
|
688
391
|
}
|
|
689
|
-
/**
|
|
690
|
-
* Get a presence channel.
|
|
691
|
-
*/
|
|
692
392
|
toPresence(name) {
|
|
693
393
|
return new PresenceChannel(name, this.driver());
|
|
694
394
|
}
|
|
695
|
-
/**
|
|
696
|
-
* Authorize a channel for a user.
|
|
697
|
-
*/
|
|
698
395
|
async authorize(channelName, user) {
|
|
699
396
|
const registration = this.findChannelRegistration(channelName);
|
|
700
397
|
if (!registration) return !(channelName.startsWith("private-") || channelName.startsWith("presence-"));
|
|
@@ -705,29 +402,19 @@ var BroadcastManager = class {
|
|
|
705
402
|
}
|
|
706
403
|
return await registration.authorizer(channelName, user) === true;
|
|
707
404
|
}
|
|
708
|
-
/**
|
|
709
|
-
* Find channel registration for a channel name.
|
|
710
|
-
*/
|
|
711
405
|
findChannelRegistration(channelName) {
|
|
712
406
|
return this.channelRegistrations.find((reg) => this.matchPattern(reg.pattern, channelName));
|
|
713
407
|
}
|
|
714
|
-
/**
|
|
715
|
-
* Match a channel name against a pattern.
|
|
716
|
-
*/
|
|
717
408
|
matchPattern(pattern, channelName) {
|
|
718
409
|
const regexPattern = pattern.replace(/\{[^}]+\}/g, "[^.]+").replace(/\*\*/g, ".+").replace(/\*/g, "[^.]+");
|
|
719
410
|
return new RegExp(`^${regexPattern}$`).test(channelName);
|
|
720
411
|
}
|
|
721
|
-
/**
|
|
722
|
-
* Create SSE middleware.
|
|
723
|
-
*/
|
|
724
412
|
sseMiddleware(options = {}) {
|
|
725
413
|
const pingInterval = options.pingInterval ?? 3e4;
|
|
726
414
|
const retry = options.retry ?? 3e3;
|
|
727
415
|
return async (ctx) => {
|
|
728
416
|
const clientId = this.generateClientId();
|
|
729
417
|
const encoder = new TextEncoder();
|
|
730
|
-
const manager = this;
|
|
731
418
|
const requestedChannels = (ctx.req.query("channels") ?? "").split(",").map((value) => value.trim()).filter(Boolean);
|
|
732
419
|
const user = options.getUser ? await options.getUser(ctx) : void 0;
|
|
733
420
|
const authorizedChannels = [];
|
|
@@ -749,7 +436,8 @@ var BroadcastManager = class {
|
|
|
749
436
|
pingTimer = null;
|
|
750
437
|
}
|
|
751
438
|
if (client) {
|
|
752
|
-
|
|
439
|
+
this.releaseDriverSubscriptions(client.id);
|
|
440
|
+
this.sseClients.delete(client.id);
|
|
753
441
|
client = null;
|
|
754
442
|
}
|
|
755
443
|
if (controller) {
|
|
@@ -760,7 +448,7 @@ var BroadcastManager = class {
|
|
|
760
448
|
}
|
|
761
449
|
};
|
|
762
450
|
const stream = new ReadableStream({
|
|
763
|
-
start(streamController) {
|
|
451
|
+
start: (streamController) => {
|
|
764
452
|
controller = streamController;
|
|
765
453
|
client = {
|
|
766
454
|
id: clientId,
|
|
@@ -772,13 +460,13 @@ var BroadcastManager = class {
|
|
|
772
460
|
},
|
|
773
461
|
close: cleanup
|
|
774
462
|
};
|
|
775
|
-
|
|
463
|
+
this.sseClients.set(clientId, client);
|
|
776
464
|
sendRaw(`retry: ${retry}\n\n`);
|
|
777
465
|
client.send("connected", {
|
|
778
466
|
clientId,
|
|
779
467
|
channels: authorizedChannels
|
|
780
468
|
});
|
|
781
|
-
for (const channelName of authorizedChannels)
|
|
469
|
+
for (const channelName of authorizedChannels) this.subscribeClient(clientId, channelName);
|
|
782
470
|
pingTimer = setInterval(() => {
|
|
783
471
|
try {
|
|
784
472
|
client?.send("ping", { time: Date.now() });
|
|
@@ -787,7 +475,7 @@ var BroadcastManager = class {
|
|
|
787
475
|
}
|
|
788
476
|
}, pingInterval);
|
|
789
477
|
},
|
|
790
|
-
cancel() {
|
|
478
|
+
cancel: () => {
|
|
791
479
|
cleanup();
|
|
792
480
|
}
|
|
793
481
|
});
|
|
@@ -799,9 +487,6 @@ var BroadcastManager = class {
|
|
|
799
487
|
} });
|
|
800
488
|
};
|
|
801
489
|
}
|
|
802
|
-
/**
|
|
803
|
-
* Create auth middleware for channel authorization.
|
|
804
|
-
*/
|
|
805
490
|
authMiddleware(options = {}) {
|
|
806
491
|
return async (ctx) => {
|
|
807
492
|
const user = await (options.getUser ?? ((c) => c.auth?.user))(ctx);
|
|
@@ -834,21 +519,52 @@ var BroadcastManager = class {
|
|
|
834
519
|
return ctx.json(results);
|
|
835
520
|
};
|
|
836
521
|
}
|
|
837
|
-
/**
|
|
838
|
-
* Subscribe a client to a channel.
|
|
839
|
-
*/
|
|
840
522
|
subscribeClient(clientId, channel) {
|
|
841
523
|
const client = this.sseClients.get(clientId);
|
|
842
524
|
if (!client) return false;
|
|
843
525
|
client.channels.add(channel);
|
|
844
|
-
this.
|
|
526
|
+
this.openDriverSubscription(clientId, channel, (event) => {
|
|
845
527
|
if (client.channels.has(channel)) client.send(event.event, event.data);
|
|
846
528
|
});
|
|
847
529
|
return true;
|
|
848
530
|
}
|
|
849
531
|
/**
|
|
850
|
-
*
|
|
532
|
+
* Open one driver subscription for a client/channel pair, keeping its
|
|
533
|
+
* unsubscribe handle so the pair can be torn down later.
|
|
534
|
+
*
|
|
535
|
+
* A pair that is already subscribed is left alone: a second `subscribe`
|
|
536
|
+
* would register a second callback and deliver every event twice.
|
|
537
|
+
*/
|
|
538
|
+
openDriverSubscription(clientId, channel, callback) {
|
|
539
|
+
let subscriptions = this.driverSubscriptions.get(clientId);
|
|
540
|
+
if (!subscriptions) {
|
|
541
|
+
subscriptions = /* @__PURE__ */ new Map();
|
|
542
|
+
this.driverSubscriptions.set(clientId, subscriptions);
|
|
543
|
+
}
|
|
544
|
+
if (subscriptions.has(channel)) return;
|
|
545
|
+
subscriptions.set(channel, this.driver().subscribe(channel, callback));
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Close the driver subscription for one client/channel pair.
|
|
549
|
+
*/
|
|
550
|
+
closeDriverSubscription(clientId, channel) {
|
|
551
|
+
const subscriptions = this.driverSubscriptions.get(clientId);
|
|
552
|
+
const unsubscribe = subscriptions?.get(channel);
|
|
553
|
+
if (!subscriptions || !unsubscribe) return;
|
|
554
|
+
subscriptions.delete(channel);
|
|
555
|
+
if (subscriptions.size === 0) this.driverSubscriptions.delete(clientId);
|
|
556
|
+
unsubscribe();
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Close every driver subscription a client holds.
|
|
851
560
|
*/
|
|
561
|
+
releaseDriverSubscriptions(clientId) {
|
|
562
|
+
const subscriptions = this.driverSubscriptions.get(clientId);
|
|
563
|
+
if (!subscriptions) return;
|
|
564
|
+
this.driverSubscriptions.delete(clientId);
|
|
565
|
+
for (const unsubscribe of subscriptions.values()) unsubscribe();
|
|
566
|
+
}
|
|
567
|
+
/** Returns the generated client ID. */
|
|
852
568
|
registerWebSocketClient(client) {
|
|
853
569
|
const clientId = this.generateClientId("ws");
|
|
854
570
|
this.wsClients.set(clientId, {
|
|
@@ -858,124 +574,81 @@ var BroadcastManager = class {
|
|
|
858
574
|
});
|
|
859
575
|
return clientId;
|
|
860
576
|
}
|
|
861
|
-
/**
|
|
862
|
-
* Remove a WebSocket client and close the underlying connection.
|
|
863
|
-
*/
|
|
864
577
|
removeWebSocketClient(clientId) {
|
|
865
578
|
const client = this.wsClients.get(clientId);
|
|
866
579
|
if (!client) return false;
|
|
580
|
+
this.releaseDriverSubscriptions(clientId);
|
|
867
581
|
this.wsClients.delete(clientId);
|
|
868
582
|
client.close();
|
|
869
583
|
return true;
|
|
870
584
|
}
|
|
871
|
-
/**
|
|
872
|
-
* Subscribe a WebSocket client to a channel.
|
|
873
|
-
*/
|
|
874
585
|
subscribeWebSocketClient(clientId, channel) {
|
|
875
586
|
const client = this.wsClients.get(clientId);
|
|
876
587
|
if (!client) return false;
|
|
877
588
|
client.channels.add(channel);
|
|
878
|
-
this.
|
|
589
|
+
this.openDriverSubscription(clientId, channel, async (event) => {
|
|
879
590
|
if (client.channels.has(channel)) await client.send(event.event, event.data);
|
|
880
591
|
});
|
|
881
592
|
return true;
|
|
882
593
|
}
|
|
883
|
-
/**
|
|
884
|
-
* Unsubscribe a WebSocket client from a channel.
|
|
885
|
-
*/
|
|
886
594
|
unsubscribeWebSocketClient(clientId, channel) {
|
|
887
595
|
const client = this.wsClients.get(clientId);
|
|
888
596
|
if (!client) return false;
|
|
889
597
|
client.channels.delete(channel);
|
|
598
|
+
this.closeDriverSubscription(clientId, channel);
|
|
890
599
|
return true;
|
|
891
600
|
}
|
|
892
|
-
/**
|
|
893
|
-
* Get a WebSocket client by ID.
|
|
894
|
-
*/
|
|
895
601
|
getWebSocketClient(clientId) {
|
|
896
602
|
return this.wsClients.get(clientId);
|
|
897
603
|
}
|
|
898
|
-
/**
|
|
899
|
-
* Get all WebSocket clients.
|
|
900
|
-
*/
|
|
901
604
|
getWebSocketClients() {
|
|
902
605
|
return Array.from(this.wsClients.values());
|
|
903
606
|
}
|
|
904
|
-
/**
|
|
905
|
-
* Unsubscribe a client from a channel.
|
|
906
|
-
*/
|
|
907
607
|
unsubscribeClient(clientId, channel) {
|
|
908
608
|
const client = this.sseClients.get(clientId);
|
|
909
609
|
if (!client) return false;
|
|
910
610
|
client.channels.delete(channel);
|
|
611
|
+
this.closeDriverSubscription(clientId, channel);
|
|
911
612
|
return true;
|
|
912
613
|
}
|
|
913
|
-
/**
|
|
914
|
-
* Get SSE client by ID.
|
|
915
|
-
*/
|
|
916
614
|
getClient(clientId) {
|
|
917
615
|
return this.sseClients.get(clientId);
|
|
918
616
|
}
|
|
919
|
-
/**
|
|
920
|
-
* Get all SSE clients.
|
|
921
|
-
*/
|
|
922
617
|
getClients() {
|
|
923
618
|
return Array.from(this.sseClients.values());
|
|
924
619
|
}
|
|
925
|
-
/**
|
|
926
|
-
* Get clients subscribed to a channel.
|
|
927
|
-
*/
|
|
928
620
|
getChannelClients(channel) {
|
|
929
621
|
return this.getClients().filter((client) => client.channels.has(channel));
|
|
930
622
|
}
|
|
931
623
|
/**
|
|
932
|
-
* Close every SSE connection this manager
|
|
933
|
-
*
|
|
934
|
-
*
|
|
935
|
-
*
|
|
936
|
-
*
|
|
937
|
-
* finishes — so on a hot reload those connections outlive the manager, and
|
|
938
|
-
* their timers go on pinging a stream nobody reads.
|
|
939
|
-
*
|
|
940
|
-
* `getClients()` returns a snapshot. A `Map` iterator does tolerate an entry
|
|
941
|
-
* deleting itself, which is all `cleanup()` does today — but not a `close()`
|
|
942
|
-
* that reaches another client, which would drop that one from the iteration
|
|
943
|
-
* unvisited and leave its timer running.
|
|
624
|
+
* Close every SSE connection this manager holds. Each owns a ping timer that
|
|
625
|
+
* is only cleared on stream cancel or request abort, and `Bun.serve().stop()`
|
|
626
|
+
* waits for in-flight requests while an SSE response never finishes — so on a
|
|
627
|
+
* hot reload the timers would go on pinging. `getClients()` is a snapshot: a
|
|
628
|
+
* `close()` reaching another client would drop it from a live iteration.
|
|
944
629
|
*/
|
|
945
630
|
disconnectAll() {
|
|
946
631
|
for (const client of this.getClients()) try {
|
|
947
632
|
client.close();
|
|
948
633
|
} catch {}
|
|
949
634
|
}
|
|
950
|
-
/**
|
|
951
|
-
* Generate a unique client ID.
|
|
952
|
-
*/
|
|
953
635
|
generateClientId(prefix = "sse") {
|
|
954
636
|
return `${prefix}_${randomHex(16)}`;
|
|
955
637
|
}
|
|
956
638
|
};
|
|
957
639
|
let globalBroadcastManager = null;
|
|
958
|
-
/**
|
|
959
|
-
* Set the global broadcast manager.
|
|
960
|
-
*/
|
|
961
640
|
function setBroadcastManager(manager) {
|
|
962
641
|
globalBroadcastManager = manager;
|
|
963
642
|
}
|
|
964
|
-
/**
|
|
965
|
-
* Get the global broadcast manager.
|
|
966
|
-
*/
|
|
967
643
|
function getBroadcastManager() {
|
|
968
644
|
if (!globalBroadcastManager) throw new Error("BroadcastManager not initialized. Call setBroadcastManager() first.");
|
|
969
645
|
return globalBroadcastManager;
|
|
970
646
|
}
|
|
971
647
|
/**
|
|
972
|
-
* Create a broadcast manager.
|
|
973
|
-
*
|
|
974
648
|
* Under `bun --hot`, the manager this one replaces has its SSE connections
|
|
975
|
-
* closed first, so their ping timers stop with it.
|
|
976
|
-
* rather than the constructor because the
|
|
977
|
-
*
|
|
978
|
-
* file. A bare `new BroadcastManager()` is left alone.
|
|
649
|
+
* closed first, so their ping timers stop with it. Claimed from the factory
|
|
650
|
+
* rather than the constructor because frame 2 of the stack must be the caller's
|
|
651
|
+
* provider; a bare `new BroadcastManager()` is left alone.
|
|
979
652
|
*/
|
|
980
653
|
function createBroadcastManager(options) {
|
|
981
654
|
const manager = new BroadcastManager(options);
|