@arttecs/murmur 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +318 -2
- package/dist/channel.d.ts +22 -0
- package/dist/chunk-QZPDZV62.js +1086 -0
- package/dist/client.d.ts +206 -0
- package/dist/echo.d.ts +119 -0
- package/dist/echo.js +346 -0
- package/dist/envelope.d.ts +9 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -0
- package/dist/invoke-listener.d.ts +2 -0
- package/dist/stream.d.ts +34 -0
- package/dist/transport.d.ts +84 -0
- package/package.json +56 -4
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { Channel, type ChannelListeners } from './channel.js';
|
|
2
|
+
import { MurmurStream, type StreamOptions } from './stream.js';
|
|
3
|
+
import { Transport, type RetryOptions } from './transport.js';
|
|
4
|
+
/** Configures the built-in authorization request. */
|
|
5
|
+
export interface AuthOptions {
|
|
6
|
+
/** Extra headers or a provider called before each token request. */
|
|
7
|
+
headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
8
|
+
/** The browser's credential mode for the token request. */
|
|
9
|
+
credentials?: RequestCredentials;
|
|
10
|
+
}
|
|
11
|
+
/** A subscription token and its expiration. */
|
|
12
|
+
export interface AuthorizationToken {
|
|
13
|
+
/** The token used to authorize the SSE connection. */
|
|
14
|
+
token: string;
|
|
15
|
+
/** The token expiration in Unix seconds. */
|
|
16
|
+
expires_at: number;
|
|
17
|
+
/** The requested wire topics this custom token covers. Omit for batch-wide coverage. */
|
|
18
|
+
channels?: string[];
|
|
19
|
+
}
|
|
20
|
+
/** Issues tokens for all or a subset of the requested private topics. */
|
|
21
|
+
export type Authorizer = (channels: string[], signal: AbortSignal) => Promise<AuthorizationToken | AuthorizationToken[]>;
|
|
22
|
+
/** Configures the Murmur client. */
|
|
23
|
+
export interface MurmurOptions extends Partial<RetryOptions> {
|
|
24
|
+
/** The full server-sent events endpoint. */
|
|
25
|
+
url: string;
|
|
26
|
+
/** The endpoint that issues tokens for private topics. */
|
|
27
|
+
authEndpoint?: string;
|
|
28
|
+
/** Options for the built-in authorization request. */
|
|
29
|
+
auth?: AuthOptions;
|
|
30
|
+
/** Replaces the built-in authorization request. */
|
|
31
|
+
authorizer?: Authorizer;
|
|
32
|
+
/** The dotted namespace for unqualified event names. */
|
|
33
|
+
namespace?: string;
|
|
34
|
+
/** The minimum seconds remaining before a cached token can be reused. */
|
|
35
|
+
tokenRefreshMarginSeconds?: number;
|
|
36
|
+
/** The limit on consecutive independent stream attempts without attachment or replay progress. */
|
|
37
|
+
streamMaxFailures?: number;
|
|
38
|
+
/** The fetch implementation used for authorization requests. */
|
|
39
|
+
fetch?: typeof fetch;
|
|
40
|
+
/** The EventSource constructor used for streaming. */
|
|
41
|
+
EventSource?: typeof EventSource;
|
|
42
|
+
/** Sends browser credentials on channel and stream connections. */
|
|
43
|
+
withCredentials?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/** A point-in-time snapshot of the client and its shared connection. */
|
|
46
|
+
export interface MurmurStats {
|
|
47
|
+
/** Whether the shared connection is currently open. */
|
|
48
|
+
connected: boolean;
|
|
49
|
+
/** The number of times the shared connection opened. */
|
|
50
|
+
connections: number;
|
|
51
|
+
/** The number of failed shared connection attempts and unexpected drops. */
|
|
52
|
+
disconnections: number;
|
|
53
|
+
/** The number of data events passed to the shared channel dispatch. */
|
|
54
|
+
events: number;
|
|
55
|
+
/** The number of gap signals received on the shared connection. */
|
|
56
|
+
gaps: number;
|
|
57
|
+
/** The number of token authorization attempts, excluding cache hits. */
|
|
58
|
+
tokenRequests: number;
|
|
59
|
+
/** The number of token authorization attempts that failed before cancellation. */
|
|
60
|
+
tokenFailures: number;
|
|
61
|
+
/** The number of currently joined channels. */
|
|
62
|
+
channels: number;
|
|
63
|
+
/** The number of independent streams that have not closed. */
|
|
64
|
+
streams: number;
|
|
65
|
+
/** The current replay cursor of the shared connection. */
|
|
66
|
+
lastEventId: string | undefined;
|
|
67
|
+
/** The most recently announced ID of the shared SSE connection. */
|
|
68
|
+
connectionId: string | undefined;
|
|
69
|
+
/** The Unix time in milliseconds of the last data event passed to dispatch. */
|
|
70
|
+
lastEventAt: number | undefined;
|
|
71
|
+
}
|
|
72
|
+
/** The arguments delivered to listeners for each client event. */
|
|
73
|
+
type ClientEventArgs = {
|
|
74
|
+
/** Delivers a replay gap notification. */
|
|
75
|
+
gap: [];
|
|
76
|
+
/** Reports a shared connection that opened. */
|
|
77
|
+
connected: [];
|
|
78
|
+
/** Reports that the server finished attaching the shared connection. */
|
|
79
|
+
attached: [];
|
|
80
|
+
/** Reports a failed or dropped shared connection. */
|
|
81
|
+
disconnected: [];
|
|
82
|
+
/** Reports a channel denied authorization by the application. */
|
|
83
|
+
unauthorized: [status: number, channelName: string];
|
|
84
|
+
/** Reports a client protocol or configuration error. */
|
|
85
|
+
error: [error: Error];
|
|
86
|
+
};
|
|
87
|
+
/** The names of events emitted by the client. */
|
|
88
|
+
type ClientEvent = keyof ClientEventArgs;
|
|
89
|
+
/** A state reached while preparing a shared connection. */
|
|
90
|
+
type ConnectionPreparation = 'connecting' | 'disconnected' | 'failed';
|
|
91
|
+
/** A joined channel and its registered event callbacks. */
|
|
92
|
+
type Subscription = {
|
|
93
|
+
/** The subscription exposed to the application. */
|
|
94
|
+
channel: Channel;
|
|
95
|
+
/** The callbacks registered by wire event name. */
|
|
96
|
+
listeners: ChannelListeners;
|
|
97
|
+
/** The join state shared by every alias of this topic. */
|
|
98
|
+
topic: JoinedTopic;
|
|
99
|
+
};
|
|
100
|
+
/** The attachment state of a single wire topic. */
|
|
101
|
+
type JoinedTopic = {
|
|
102
|
+
/** Whether this topic is still waiting for its first attach marker. */
|
|
103
|
+
pending: boolean;
|
|
104
|
+
};
|
|
105
|
+
/** Multiplexes channel subscriptions and opens independent streams. */
|
|
106
|
+
export declare class Murmur {
|
|
107
|
+
protected readonly options: MurmurOptions;
|
|
108
|
+
/** The joined channels and their callbacks, indexed by subscription name and kind. */
|
|
109
|
+
protected readonly channels: Map<string, Subscription>;
|
|
110
|
+
/** The wire topics with at least one joined channel. */
|
|
111
|
+
protected readonly topics: Map<string, JoinedTopic>;
|
|
112
|
+
/** The callbacks registered for client events. */
|
|
113
|
+
protected readonly listeners: {
|
|
114
|
+
[Event in ClientEvent]: Set<(...args: ClientEventArgs[Event]) => void>;
|
|
115
|
+
};
|
|
116
|
+
/** Observers of shared connection preparation. */
|
|
117
|
+
protected readonly preparationListeners: Set<(state: ConnectionPreparation) => void>;
|
|
118
|
+
/** The transport for the shared event stream. */
|
|
119
|
+
protected readonly transport: Transport;
|
|
120
|
+
/** The active streams created by this client. */
|
|
121
|
+
protected readonly streams: Set<MurmurStream>;
|
|
122
|
+
/** The cumulative counters reported by stats(). */
|
|
123
|
+
protected readonly counts: {
|
|
124
|
+
connections: number;
|
|
125
|
+
disconnections: number;
|
|
126
|
+
events: number;
|
|
127
|
+
gaps: number;
|
|
128
|
+
tokenRequests: number;
|
|
129
|
+
tokenFailures: number;
|
|
130
|
+
};
|
|
131
|
+
/** The time the last valid shared data event was received. */
|
|
132
|
+
protected lastEventAt?: number;
|
|
133
|
+
/** The ID most recently announced by the shared connection. */
|
|
134
|
+
protected connectionId?: string;
|
|
135
|
+
/** Whether a protocol mismatch has already been reported. */
|
|
136
|
+
protected protocolMismatchReported: boolean;
|
|
137
|
+
/** The client's retry settings, also used as the defaults for streams. */
|
|
138
|
+
protected readonly retryOptions: RetryOptions;
|
|
139
|
+
/** The time that must remain on a cached token for it to be reused, in seconds. */
|
|
140
|
+
protected readonly tokenRefreshMarginSeconds: number;
|
|
141
|
+
/** The default limit on consecutive independent stream attempts without attachment or replay progress. */
|
|
142
|
+
protected readonly streamMaxFailures: number;
|
|
143
|
+
/** The reusable tokens associated with each authorized private topic. */
|
|
144
|
+
protected readonly cachedTokens: Map<string, AuthorizationToken[]>;
|
|
145
|
+
/** The number of pre-open failures that could indicate credential rejection since attachment. */
|
|
146
|
+
protected preOpenCredentialFailures: number;
|
|
147
|
+
/** The next credential-bearing failure that triggers an automatic refresh. */
|
|
148
|
+
protected nextCredentialRefresh: number;
|
|
149
|
+
/** The private topics waiting for explicit authorization or a new join. */
|
|
150
|
+
protected readonly deniedTopics: Set<string>;
|
|
151
|
+
/** The subscriptions sent on the attempt waiting for its attach marker. */
|
|
152
|
+
protected attemptSubscriptions: Subscription[];
|
|
153
|
+
/** Whether a channel change has queued a connection update. */
|
|
154
|
+
protected connectionQueued: boolean;
|
|
155
|
+
/** Whether the client has been permanently closed. */
|
|
156
|
+
protected closed: boolean;
|
|
157
|
+
/** Creates a client for the given streaming endpoint. */
|
|
158
|
+
constructor(options: MurmurOptions);
|
|
159
|
+
/** Joins a public channel. */
|
|
160
|
+
channel(name: string): Channel;
|
|
161
|
+
/** Joins a private channel and authorizes its topic before connecting. */
|
|
162
|
+
private(name: string): Channel;
|
|
163
|
+
/** Opens an independent stream using a server-issued subscription URL. */
|
|
164
|
+
stream(url: string, options?: StreamOptions): MurmurStream;
|
|
165
|
+
/** Leaves the subscriptions joined with the given name. */
|
|
166
|
+
leave(name: string): void;
|
|
167
|
+
/** Retries authorization for one denied private channel or all denied private channels. */
|
|
168
|
+
reauthorize(name?: string): void;
|
|
169
|
+
/** Registers a client event listener and returns a function that removes it. */
|
|
170
|
+
on<Event extends ClientEvent>(event: Event, listener: (...args: ClientEventArgs[Event]) => void): () => void;
|
|
171
|
+
/** Returns the server-issued ID used for Laravel sender exclusion. */
|
|
172
|
+
socketId(): string | undefined;
|
|
173
|
+
/** Returns a fresh snapshot of shared connection activity and active subscriptions. */
|
|
174
|
+
stats(): MurmurStats;
|
|
175
|
+
/** Closes channels and streams and cancels outstanding connection attempts. */
|
|
176
|
+
close(): void;
|
|
177
|
+
/** Creates a subscription or returns the existing channel for the same name and kind. */
|
|
178
|
+
protected join(key: string, topic: string): Channel;
|
|
179
|
+
/** Applies channel changes together when the current tick ends. */
|
|
180
|
+
protected scheduleConnection(): void;
|
|
181
|
+
/** Synchronizes the stream's named events with the active channel listeners. */
|
|
182
|
+
protected updateEvents(): void;
|
|
183
|
+
/** Builds the stream URL for the current subscriptions. */
|
|
184
|
+
protected connectionUrl(signal: AbortSignal, lastEventId: string | undefined): Promise<string | undefined>;
|
|
185
|
+
/** Reports a shared connection preparation state to the connector. */
|
|
186
|
+
protected reportPreparation(state: ConnectionPreparation): void;
|
|
187
|
+
/** Reuses valid credentials and authorizes missing private topics. */
|
|
188
|
+
protected authorize(topics: string[], signal: AbortSignal): Promise<void>;
|
|
189
|
+
/** Records valid tokens or marks only the requested private topics denied. */
|
|
190
|
+
protected authorizeTopics(topics: string[], signal: AbortSignal, request: () => Promise<unknown>, acceptsArray: boolean): Promise<void>;
|
|
191
|
+
/** Removes uncovered topics from the connection and notifies their listeners. */
|
|
192
|
+
protected deny(topics: string[], status: number): void;
|
|
193
|
+
/** Refreshes credentials on exponentially spaced pre-open failures that could indicate rejection. */
|
|
194
|
+
protected refreshAfterPreOpenError(url: string, status: number | undefined): void;
|
|
195
|
+
/** Requests an authorization response from the built-in endpoint. */
|
|
196
|
+
protected requestToken(topic: string, signal: AbortSignal): Promise<unknown>;
|
|
197
|
+
/** Dispatches a stream event to its channel or emits a gap notification. */
|
|
198
|
+
protected receive(event: string, data: string): void;
|
|
199
|
+
/** Acknowledges only the joins included on the connection that attached. */
|
|
200
|
+
protected receiveAttachment(): void;
|
|
201
|
+
/** Stores a valid server-issued connection ID and reports an unexpected protocol. */
|
|
202
|
+
protected receiveConnection(data: string): void;
|
|
203
|
+
/** Notifies listeners of a client event. */
|
|
204
|
+
protected emit<Event extends ClientEvent>(event: Event, ...args: ClientEventArgs[Event]): void;
|
|
205
|
+
}
|
|
206
|
+
export {};
|
package/dist/echo.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Channel } from './channel.js';
|
|
2
|
+
import { Murmur, type MurmurOptions } from './client.js';
|
|
3
|
+
/** Configures Murmur as a custom Laravel Echo broadcaster. */
|
|
4
|
+
export interface MurmurConnectorOptions extends Omit<MurmurOptions, 'namespace'> {
|
|
5
|
+
/** Echo's dotted event namespace, or false to disable its prefix. */
|
|
6
|
+
namespace?: string | false;
|
|
7
|
+
/** The CSRF token to use when window.Laravel does not supply one. */
|
|
8
|
+
csrfToken?: string | null;
|
|
9
|
+
/** The token sent as an Authorization header on token requests. */
|
|
10
|
+
bearerToken?: string | null;
|
|
11
|
+
}
|
|
12
|
+
/** The connection states Laravel Echo reports for a connector. */
|
|
13
|
+
export type MurmurConnectionStatus = 'connected' | 'disconnected' | 'connecting' | 'reconnecting' | 'failed';
|
|
14
|
+
/** Formats event names the way Laravel Echo does, without importing Echo. */
|
|
15
|
+
declare class EventFormatter {
|
|
16
|
+
protected readonly namespace: string | false;
|
|
17
|
+
/** Creates a formatter for a dotted namespace, or none when it is false or empty. */
|
|
18
|
+
constructor(namespace: string | false);
|
|
19
|
+
/** Resolves an Echo event name to the wire name Laravel broadcasts. */
|
|
20
|
+
format(event: string): string;
|
|
21
|
+
}
|
|
22
|
+
/** Adapts a Murmur subscription to the Echo channel API. */
|
|
23
|
+
declare class MurmurEchoChannel {
|
|
24
|
+
protected readonly channel: Channel;
|
|
25
|
+
protected readonly formatter: EventFormatter;
|
|
26
|
+
protected readonly onWhisper: () => void;
|
|
27
|
+
/** The literal Laravel notification event. */
|
|
28
|
+
readonly notificationCreatedEvent = ".Illuminate\\Notifications\\Events\\BroadcastNotificationCreated";
|
|
29
|
+
/** Echo callbacks keyed by their wire event name. */
|
|
30
|
+
protected readonly listeners: Map<string, Set<CallableFunction>>;
|
|
31
|
+
/** The callbacks notified when the channel attaches or recovers from a failure. */
|
|
32
|
+
protected readonly subscriptions: Set<CallableFunction>;
|
|
33
|
+
/** The callbacks notified of a denied private authorization request. */
|
|
34
|
+
protected readonly errors: Set<CallableFunction>;
|
|
35
|
+
/** Whether attachment has already been reported since this channel joined or the connection failed. */
|
|
36
|
+
protected subscribedNotified: boolean;
|
|
37
|
+
/** Whether the channel remains joined. */
|
|
38
|
+
protected active: boolean;
|
|
39
|
+
/** Wraps a topic subscription and formats event names as Echo does. */
|
|
40
|
+
constructor(channel: Channel, formatter: EventFormatter, onWhisper?: () => void);
|
|
41
|
+
/** Registers an Echo callback for a formatted event. */
|
|
42
|
+
listen(event: string, callback: CallableFunction): this;
|
|
43
|
+
/** Removes all callbacks for an event or the specified callback only. */
|
|
44
|
+
stopListening(event: string, callback?: CallableFunction): this;
|
|
45
|
+
/** Registers a callback for future server acknowledgments of this channel. */
|
|
46
|
+
subscribed(callback: CallableFunction): this;
|
|
47
|
+
/** Registers a callback for future private authorization failures. */
|
|
48
|
+
error(callback: CallableFunction): this;
|
|
49
|
+
/** Registers a callback for Laravel broadcast notifications. */
|
|
50
|
+
notification(callback: CallableFunction): this;
|
|
51
|
+
/** Removes the given Laravel notification callback. */
|
|
52
|
+
stopListeningForNotification(callback: CallableFunction): this;
|
|
53
|
+
/** Preserves fluent setup for unsupported client events and warns once. */
|
|
54
|
+
listenForWhisper(_event: string, _callback: CallableFunction): this;
|
|
55
|
+
/** Preserves fluent cleanup for unsupported client events. */
|
|
56
|
+
stopListeningForWhisper(_event: string, _callback?: CallableFunction): this;
|
|
57
|
+
/** Notifies subscription callbacks the first time this channel attaches after a join or failure. */
|
|
58
|
+
protected attached(): void;
|
|
59
|
+
/** Notifies this channel's error callbacks of its denied authorization status. */
|
|
60
|
+
protected unauthorized(status: number): void;
|
|
61
|
+
}
|
|
62
|
+
/** Adapts an authorized Murmur subscription to Echo's private channel API. */
|
|
63
|
+
declare class MurmurEchoPrivateChannel extends MurmurEchoChannel {
|
|
64
|
+
/** Preserves method chaining without sending an unsupported client event and warns once. */
|
|
65
|
+
whisper(_event: string, _data: Record<string, unknown>): this;
|
|
66
|
+
}
|
|
67
|
+
/** Connects Laravel Echo to a shared Murmur SSE connection. */
|
|
68
|
+
export declare class MurmurConnector {
|
|
69
|
+
protected readonly options: MurmurConnectorOptions;
|
|
70
|
+
/** The channel registry used by Echo to leave every channel. */
|
|
71
|
+
readonly channels: Partial<Record<string, MurmurEchoChannel>>;
|
|
72
|
+
/** The formatter shared by the connector's channels. */
|
|
73
|
+
protected readonly formatter: EventFormatter;
|
|
74
|
+
/** The client backing the current connector lifecycle. */
|
|
75
|
+
protected client: Murmur;
|
|
76
|
+
/** Whether the connector has an active client. */
|
|
77
|
+
protected active: boolean;
|
|
78
|
+
/** The last reported connection state. */
|
|
79
|
+
protected status: MurmurConnectionStatus;
|
|
80
|
+
/** Whether the unsupported client events warning was already printed. */
|
|
81
|
+
protected whisperWarned: boolean;
|
|
82
|
+
/** The observers of Echo connection status changes. */
|
|
83
|
+
protected readonly statusListeners: Set<(status: MurmurConnectionStatus) => void>;
|
|
84
|
+
/** Creates a connector from the options passed through Echo. */
|
|
85
|
+
constructor(options: MurmurConnectorOptions);
|
|
86
|
+
/** Returns the client for streams, statistics, and other advanced operations. */
|
|
87
|
+
get murmur(): Murmur;
|
|
88
|
+
/** Creates a fresh client if disconnected and opens SSE when a channel joins. */
|
|
89
|
+
connect(): void;
|
|
90
|
+
/** Returns or joins the public channel with the given name. */
|
|
91
|
+
channel(name: string): MurmurEchoChannel;
|
|
92
|
+
/** Returns or joins the private channel with the given name. */
|
|
93
|
+
privateChannel(name: string): MurmurEchoPrivateChannel;
|
|
94
|
+
/** Rejects encrypted private channels because Murmur does not support them. */
|
|
95
|
+
encryptedPrivateChannel(_name: string): never;
|
|
96
|
+
/** Rejects presence channels because Murmur does not support them. */
|
|
97
|
+
presenceChannel(_name: string): never;
|
|
98
|
+
/** Listens to an event on the given public channel. */
|
|
99
|
+
listen(name: string, event: string, callback: CallableFunction): MurmurEchoChannel;
|
|
100
|
+
/** Leaves the public and private variants of the given channel. */
|
|
101
|
+
leave(name: string): void;
|
|
102
|
+
/** Leaves exactly the channel registered under the supplied wire name. */
|
|
103
|
+
leaveChannel(name: string): void;
|
|
104
|
+
/** Returns the server-issued ID or an empty value for Echo's request interceptors. */
|
|
105
|
+
socketId(): string;
|
|
106
|
+
/** Returns the current Echo connection status. */
|
|
107
|
+
connectionStatus(): MurmurConnectionStatus;
|
|
108
|
+
/** Subscribes to status changes and returns a cleanup function. */
|
|
109
|
+
onConnectionChange(callback: (status: MurmurConnectionStatus) => void): () => void;
|
|
110
|
+
/** Closes the client, including streams and retries, and detaches all channels. */
|
|
111
|
+
disconnect(): void;
|
|
112
|
+
/** Records a channel and reports the pending subscription change. */
|
|
113
|
+
protected track(name: string, channel: MurmurEchoChannel): void;
|
|
114
|
+
/** Notifies observers when the connector changes state. */
|
|
115
|
+
protected setStatus(status: MurmurConnectionStatus): void;
|
|
116
|
+
/** Warns once per connector that client events are not delivered. */
|
|
117
|
+
protected warnAboutWhispers(): void;
|
|
118
|
+
}
|
|
119
|
+
export {};
|
package/dist/echo.js
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Murmur,
|
|
3
|
+
invokeListener
|
|
4
|
+
} from "./chunk-QZPDZV62.js";
|
|
5
|
+
|
|
6
|
+
// src/echo.ts
|
|
7
|
+
var EventFormatter = class {
|
|
8
|
+
/** Creates a formatter for a dotted namespace, or none when it is false or empty. */
|
|
9
|
+
constructor(namespace) {
|
|
10
|
+
this.namespace = namespace;
|
|
11
|
+
}
|
|
12
|
+
namespace;
|
|
13
|
+
/** Resolves an Echo event name to the wire name Laravel broadcasts. */
|
|
14
|
+
format(event) {
|
|
15
|
+
if (event.startsWith(".") || event.startsWith("\\")) {
|
|
16
|
+
return event.substring(1);
|
|
17
|
+
}
|
|
18
|
+
return (this.namespace ? `${this.namespace}.${event}` : event).replace(/\./g, "\\");
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var privatePrefix = "private-";
|
|
22
|
+
function resolveCsrfToken(options) {
|
|
23
|
+
const browser = typeof window === "undefined" ? void 0 : window;
|
|
24
|
+
return browser?.Laravel?.csrfToken || options.csrfToken || (typeof document !== "undefined" && typeof document.querySelector === "function" ? document.querySelector('meta[name="csrf-token"]')?.getAttribute("content") : null);
|
|
25
|
+
}
|
|
26
|
+
function authorization(options) {
|
|
27
|
+
const { auth, bearerToken } = options;
|
|
28
|
+
return {
|
|
29
|
+
...auth,
|
|
30
|
+
headers: async () => {
|
|
31
|
+
const supplied = typeof auth?.headers === "function" ? await auth.headers() : auth?.headers;
|
|
32
|
+
const headers = new Headers(supplied);
|
|
33
|
+
const csrfToken = resolveCsrfToken(options);
|
|
34
|
+
if (csrfToken) {
|
|
35
|
+
headers.set("X-CSRF-TOKEN", csrfToken);
|
|
36
|
+
}
|
|
37
|
+
if (bearerToken) {
|
|
38
|
+
headers.set("Authorization", `Bearer ${bearerToken}`);
|
|
39
|
+
}
|
|
40
|
+
return Object.fromEntries(headers);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
var MurmurEchoChannel = class {
|
|
45
|
+
/** Wraps a topic subscription and formats event names as Echo does. */
|
|
46
|
+
constructor(channel, formatter, onWhisper = () => {
|
|
47
|
+
}) {
|
|
48
|
+
this.channel = channel;
|
|
49
|
+
this.formatter = formatter;
|
|
50
|
+
this.onWhisper = onWhisper;
|
|
51
|
+
this.channel.onAttached(() => this.attached());
|
|
52
|
+
this.channel.error((status) => this.unauthorized(status));
|
|
53
|
+
}
|
|
54
|
+
channel;
|
|
55
|
+
formatter;
|
|
56
|
+
onWhisper;
|
|
57
|
+
/** The literal Laravel notification event. */
|
|
58
|
+
notificationCreatedEvent = ".Illuminate\\Notifications\\Events\\BroadcastNotificationCreated";
|
|
59
|
+
/** Echo callbacks keyed by their wire event name. */
|
|
60
|
+
listeners = /* @__PURE__ */ new Map();
|
|
61
|
+
/** The callbacks notified when the channel attaches or recovers from a failure. */
|
|
62
|
+
subscriptions = /* @__PURE__ */ new Set();
|
|
63
|
+
/** The callbacks notified of a denied private authorization request. */
|
|
64
|
+
errors = /* @__PURE__ */ new Set();
|
|
65
|
+
/** Whether attachment has already been reported since this channel joined or the connection failed. */
|
|
66
|
+
subscribedNotified = false;
|
|
67
|
+
/** Whether the channel remains joined. */
|
|
68
|
+
active = true;
|
|
69
|
+
/** Registers an Echo callback for a formatted event. */
|
|
70
|
+
listen(event, callback) {
|
|
71
|
+
if (!this.active) {
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
const name = this.formatter.format(event);
|
|
75
|
+
let callbacks = this.listeners.get(name);
|
|
76
|
+
if (!callbacks) {
|
|
77
|
+
const registered = /* @__PURE__ */ new Set();
|
|
78
|
+
this.listeners.set(name, registered);
|
|
79
|
+
this.channel.listen("." + name, (data) => {
|
|
80
|
+
for (const handler of registered) {
|
|
81
|
+
invokeListener(() => handler(data));
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
callbacks = registered;
|
|
85
|
+
}
|
|
86
|
+
callbacks.add(callback);
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/** Removes all callbacks for an event or the specified callback only. */
|
|
90
|
+
stopListening(event, callback) {
|
|
91
|
+
const name = this.formatter.format(event);
|
|
92
|
+
const callbacks = this.listeners.get(name);
|
|
93
|
+
if (!callbacks) {
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
if (callback) {
|
|
97
|
+
callbacks.delete(callback);
|
|
98
|
+
} else {
|
|
99
|
+
callbacks.clear();
|
|
100
|
+
}
|
|
101
|
+
if (callbacks.size === 0) {
|
|
102
|
+
this.listeners.delete(name);
|
|
103
|
+
this.channel.stopListening("." + name);
|
|
104
|
+
}
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
/** Registers a callback for future server acknowledgments of this channel. */
|
|
108
|
+
subscribed(callback) {
|
|
109
|
+
if (this.active) {
|
|
110
|
+
this.subscriptions.add(callback);
|
|
111
|
+
}
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
/** Registers a callback for future private authorization failures. */
|
|
115
|
+
error(callback) {
|
|
116
|
+
if (this.active) {
|
|
117
|
+
this.errors.add(callback);
|
|
118
|
+
}
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
/** Registers a callback for Laravel broadcast notifications. */
|
|
122
|
+
notification(callback) {
|
|
123
|
+
return this.listen(this.notificationCreatedEvent, callback);
|
|
124
|
+
}
|
|
125
|
+
/** Removes the given Laravel notification callback. */
|
|
126
|
+
stopListeningForNotification(callback) {
|
|
127
|
+
return this.stopListening(this.notificationCreatedEvent, callback);
|
|
128
|
+
}
|
|
129
|
+
/** Preserves fluent setup for unsupported client events and warns once. */
|
|
130
|
+
listenForWhisper(_event, _callback) {
|
|
131
|
+
this.onWhisper();
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/** Preserves fluent cleanup for unsupported client events. */
|
|
135
|
+
stopListeningForWhisper(_event, _callback) {
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
/** Notifies subscription callbacks the first time this channel attaches after a join or failure. */
|
|
139
|
+
attached() {
|
|
140
|
+
if (this.subscribedNotified) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
this.subscribedNotified = true;
|
|
144
|
+
for (const callback of [...this.subscriptions]) {
|
|
145
|
+
if (this.active) {
|
|
146
|
+
invokeListener(() => callback());
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** Notifies this channel's error callbacks of its denied authorization status. */
|
|
151
|
+
unauthorized(status) {
|
|
152
|
+
for (const callback of [...this.errors]) {
|
|
153
|
+
if (this.active) {
|
|
154
|
+
invokeListener(() => callback(status));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Allows the next successful attachment to notify listeners after a connection failure.
|
|
160
|
+
*
|
|
161
|
+
* @internal
|
|
162
|
+
*/
|
|
163
|
+
connectionLost() {
|
|
164
|
+
this.subscribedNotified = false;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Clears callbacks and disables listener registration on a departed channel.
|
|
168
|
+
*
|
|
169
|
+
* @internal
|
|
170
|
+
*/
|
|
171
|
+
detach() {
|
|
172
|
+
this.active = false;
|
|
173
|
+
for (const callbacks of this.listeners.values()) {
|
|
174
|
+
callbacks.clear();
|
|
175
|
+
}
|
|
176
|
+
this.listeners.clear();
|
|
177
|
+
this.subscriptions.clear();
|
|
178
|
+
this.errors.clear();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
var MurmurEchoPrivateChannel = class extends MurmurEchoChannel {
|
|
182
|
+
/** Preserves method chaining without sending an unsupported client event and warns once. */
|
|
183
|
+
whisper(_event, _data) {
|
|
184
|
+
this.onWhisper();
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
var MurmurConnector = class {
|
|
189
|
+
/** Creates a connector from the options passed through Echo. */
|
|
190
|
+
constructor(options) {
|
|
191
|
+
this.options = options;
|
|
192
|
+
if (typeof options.url !== "string" || options.url.trim() === "") {
|
|
193
|
+
throw new Error("Murmur requires a non-empty url option for its SSE endpoint.");
|
|
194
|
+
}
|
|
195
|
+
this.formatter = new EventFormatter(options.namespace ?? "App.Events");
|
|
196
|
+
this.connect();
|
|
197
|
+
}
|
|
198
|
+
options;
|
|
199
|
+
/** The channel registry used by Echo to leave every channel. */
|
|
200
|
+
channels = /* @__PURE__ */ Object.create(null);
|
|
201
|
+
/** The formatter shared by the connector's channels. */
|
|
202
|
+
formatter;
|
|
203
|
+
/** The client backing the current connector lifecycle. */
|
|
204
|
+
client;
|
|
205
|
+
/** Whether the connector has an active client. */
|
|
206
|
+
active = false;
|
|
207
|
+
/** The last reported connection state. */
|
|
208
|
+
status = "disconnected";
|
|
209
|
+
/** Whether the unsupported client events warning was already printed. */
|
|
210
|
+
whisperWarned = false;
|
|
211
|
+
/** The observers of Echo connection status changes. */
|
|
212
|
+
statusListeners = /* @__PURE__ */ new Set();
|
|
213
|
+
/** Returns the client for streams, statistics, and other advanced operations. */
|
|
214
|
+
get murmur() {
|
|
215
|
+
return this.client;
|
|
216
|
+
}
|
|
217
|
+
/** Creates a fresh client if disconnected and opens SSE when a channel joins. */
|
|
218
|
+
connect() {
|
|
219
|
+
if (this.active) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
this.client = new Murmur({
|
|
223
|
+
...this.options,
|
|
224
|
+
namespace: this.options.namespace === false ? "" : this.options.namespace,
|
|
225
|
+
auth: authorization(this.options)
|
|
226
|
+
});
|
|
227
|
+
this.active = true;
|
|
228
|
+
this.client.onConnectionPreparation((state) => {
|
|
229
|
+
if (this.active) {
|
|
230
|
+
this.setStatus(state);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
this.client.on("connected", () => this.setStatus("connected"));
|
|
234
|
+
this.client.on("disconnected", () => {
|
|
235
|
+
for (const channel of Object.values(this.channels)) {
|
|
236
|
+
channel?.connectionLost();
|
|
237
|
+
}
|
|
238
|
+
this.setStatus("reconnecting");
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/** Returns or joins the public channel with the given name. */
|
|
242
|
+
channel(name) {
|
|
243
|
+
if (name.startsWith(privatePrefix)) {
|
|
244
|
+
return this.privateChannel(name.slice(privatePrefix.length));
|
|
245
|
+
}
|
|
246
|
+
const existing = this.channels[name];
|
|
247
|
+
if (existing) {
|
|
248
|
+
return existing;
|
|
249
|
+
}
|
|
250
|
+
const channel = new MurmurEchoChannel(this.client.channel(name), this.formatter, () => this.warnAboutWhispers());
|
|
251
|
+
this.track(name, channel);
|
|
252
|
+
return channel;
|
|
253
|
+
}
|
|
254
|
+
/** Returns or joins the private channel with the given name. */
|
|
255
|
+
privateChannel(name) {
|
|
256
|
+
const topic = privatePrefix + name;
|
|
257
|
+
const existing = this.channels[topic];
|
|
258
|
+
if (existing instanceof MurmurEchoPrivateChannel) {
|
|
259
|
+
return existing;
|
|
260
|
+
}
|
|
261
|
+
const channel = new MurmurEchoPrivateChannel(this.client.private(name), this.formatter, () => this.warnAboutWhispers());
|
|
262
|
+
this.track(topic, channel);
|
|
263
|
+
return channel;
|
|
264
|
+
}
|
|
265
|
+
/** Rejects encrypted private channels because Murmur does not support them. */
|
|
266
|
+
encryptedPrivateChannel(_name) {
|
|
267
|
+
throw new Error("Murmur does not support encrypted private channels.");
|
|
268
|
+
}
|
|
269
|
+
/** Rejects presence channels because Murmur does not support them. */
|
|
270
|
+
presenceChannel(_name) {
|
|
271
|
+
throw new Error("Murmur does not support presence channels.");
|
|
272
|
+
}
|
|
273
|
+
/** Listens to an event on the given public channel. */
|
|
274
|
+
listen(name, event, callback) {
|
|
275
|
+
return this.channel(name).listen(event, callback);
|
|
276
|
+
}
|
|
277
|
+
/** Leaves the public and private variants of the given channel. */
|
|
278
|
+
leave(name) {
|
|
279
|
+
this.leaveChannel(name);
|
|
280
|
+
this.leaveChannel(privatePrefix + name);
|
|
281
|
+
}
|
|
282
|
+
/** Leaves exactly the channel registered under the supplied wire name. */
|
|
283
|
+
leaveChannel(name) {
|
|
284
|
+
const channel = this.channels[name];
|
|
285
|
+
if (!channel) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
channel.detach();
|
|
289
|
+
delete this.channels[name];
|
|
290
|
+
const isPrivate = channel instanceof MurmurEchoPrivateChannel;
|
|
291
|
+
this.client.leaveSubscription(isPrivate ? name.slice(privatePrefix.length) : name, isPrivate);
|
|
292
|
+
}
|
|
293
|
+
/** Returns the server-issued ID or an empty value for Echo's request interceptors. */
|
|
294
|
+
socketId() {
|
|
295
|
+
return this.active ? this.client.socketId() ?? "" : "";
|
|
296
|
+
}
|
|
297
|
+
/** Returns the current Echo connection status. */
|
|
298
|
+
connectionStatus() {
|
|
299
|
+
return this.status;
|
|
300
|
+
}
|
|
301
|
+
/** Subscribes to status changes and returns a cleanup function. */
|
|
302
|
+
onConnectionChange(callback) {
|
|
303
|
+
this.statusListeners.add(callback);
|
|
304
|
+
return () => {
|
|
305
|
+
this.statusListeners.delete(callback);
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
/** Closes the client, including streams and retries, and detaches all channels. */
|
|
309
|
+
disconnect() {
|
|
310
|
+
this.active = false;
|
|
311
|
+
this.client.close();
|
|
312
|
+
for (const name of Object.keys(this.channels)) {
|
|
313
|
+
this.channels[name]?.detach();
|
|
314
|
+
delete this.channels[name];
|
|
315
|
+
}
|
|
316
|
+
this.setStatus("disconnected");
|
|
317
|
+
}
|
|
318
|
+
/** Records a channel and reports the pending subscription change. */
|
|
319
|
+
track(name, channel) {
|
|
320
|
+
if (this.active) {
|
|
321
|
+
this.channels[name] = channel;
|
|
322
|
+
this.setStatus("connecting");
|
|
323
|
+
} else {
|
|
324
|
+
channel.detach();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/** Notifies observers when the connector changes state. */
|
|
328
|
+
setStatus(status) {
|
|
329
|
+
if (status !== this.status) {
|
|
330
|
+
this.status = status;
|
|
331
|
+
for (const callback of this.statusListeners) {
|
|
332
|
+
invokeListener(callback, status);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/** Warns once per connector that client events are not delivered. */
|
|
337
|
+
warnAboutWhispers() {
|
|
338
|
+
if (!this.whisperWarned) {
|
|
339
|
+
this.whisperWarned = true;
|
|
340
|
+
console.warn("Murmur does not support client events: whisper and listenForWhisper do nothing.");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
export {
|
|
345
|
+
MurmurConnector
|
|
346
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The wrapper the server puts around every event payload. */
|
|
2
|
+
export interface Envelope {
|
|
3
|
+
/** The topic the event was published to. */
|
|
4
|
+
topic: string;
|
|
5
|
+
/** The event payload. */
|
|
6
|
+
data: unknown;
|
|
7
|
+
}
|
|
8
|
+
/** Parses an event envelope, returning undefined when it is malformed. */
|
|
9
|
+
export declare function parseEnvelope(raw: string): Envelope | undefined;
|