@sebspark/socket.io-gcp-pubsub-emitter 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,166 @@
1
+ import { Topic } from "@google-cloud/pubsub";
2
+
3
+ //#region src/typed-events.d.ts
4
+
5
+ /**
6
+ * An events map is an interface that maps event names to their value, which
7
+ * represents the type of the `on` listener.
8
+ */
9
+ interface EventsMap {
10
+ [event: string]: any;
11
+ }
12
+ /**
13
+ * The default events map, used if no EventsMap is given. Using this EventsMap
14
+ * is equivalent to accepting all event names, and any data.
15
+ */
16
+ interface DefaultEventsMap {
17
+ [event: string]: (...args: unknown[]) => void;
18
+ }
19
+ /**
20
+ * Returns a union type containing all the keys of an event map.
21
+ */
22
+ type EventNames<Map extends EventsMap> = keyof Map & (string | symbol);
23
+ /** The tuple type representing the parameters of an event listener */
24
+ type EventParams<Map extends EventsMap, Ev extends EventNames<Map>> = Parameters<Map[Ev]>;
25
+ /**
26
+ * Interface for classes that aren't `EventEmitter`s, but still expose a
27
+ * strictly typed `emit` method.
28
+ */
29
+ interface TypedEventBroadcaster<EmitEvents extends EventsMap> {
30
+ emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
31
+ }
32
+ //#endregion
33
+ //#region src/types.d.ts
34
+ type EmitterOptions = unknown;
35
+ interface BroadcastOptions {
36
+ nsp: string;
37
+ }
38
+ interface BroadcastFlags {
39
+ volatile?: boolean;
40
+ compress?: boolean;
41
+ }
42
+ //#endregion
43
+ //#region src/operator.d.ts
44
+ declare class BroadcastOperator<EmitEvents extends EventsMap> implements TypedEventBroadcaster<EmitEvents> {
45
+ private readonly topic;
46
+ private readonly broadcastOptions;
47
+ private readonly rooms;
48
+ private readonly exceptRooms;
49
+ private readonly flags;
50
+ constructor(topic: Topic, broadcastOptions: BroadcastOptions, rooms?: Set<string>, exceptRooms?: Set<string>, flags?: BroadcastFlags);
51
+ /**
52
+ * Emits to all clients.
53
+ *
54
+ * @return Always true
55
+ * @public
56
+ */
57
+ emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): true;
58
+ /**
59
+ * Targets a room when emitting.
60
+ *
61
+ * @param room
62
+ * @return a new BroadcastOperator instance
63
+ * @public
64
+ */
65
+ to(room: string | string[]): BroadcastOperator<EmitEvents>;
66
+ /**
67
+ * Targets a room when emitting.
68
+ *
69
+ * @param room
70
+ * @return a new BroadcastOperator instance
71
+ * @public
72
+ */
73
+ in(room: string | string[]): BroadcastOperator<EmitEvents>;
74
+ /**
75
+ * Excludes a room when emitting.
76
+ *
77
+ * @param room
78
+ * @return a new BroadcastOperator instance
79
+ * @public
80
+ */
81
+ except(room: string | string[]): BroadcastOperator<EmitEvents>;
82
+ /**
83
+ * Sets the compress flag.
84
+ *
85
+ * @param compress - if `true`, compresses the sending data
86
+ * @return a new BroadcastOperator instance
87
+ * @public
88
+ */
89
+ compress(compress: boolean): BroadcastOperator<EmitEvents>;
90
+ /**
91
+ * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
92
+ * receive messages (because of network slowness or other issues, or because they’re connected through long polling
93
+ * and is in the middle of a request-response cycle).
94
+ *
95
+ * @return a new BroadcastOperator instance
96
+ * @public
97
+ */
98
+ get volatile(): BroadcastOperator<EmitEvents>;
99
+ }
100
+ //#endregion
101
+ //#region src/emitter.d.ts
102
+ declare class Emitter<EmitEvents extends EventsMap = DefaultEventsMap> {
103
+ readonly topic: Topic;
104
+ readonly nsp: string;
105
+ private readonly opts;
106
+ private readonly broadcastOptions;
107
+ constructor(topic: Topic, opts?: EmitterOptions, nsp?: string);
108
+ /**
109
+ * Emits to all clients.
110
+ *
111
+ * @return Always true
112
+ * @public
113
+ */
114
+ emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): true;
115
+ /**
116
+ * Return a new emitter for the given namespace.
117
+ *
118
+ * @param nsp - namespace
119
+ * @public
120
+ */
121
+ of(nsp: string): Emitter<EmitEvents>;
122
+ /**
123
+ * Targets a room when emitting.
124
+ *
125
+ * @param room
126
+ * @return BroadcastOperator
127
+ * @public
128
+ */
129
+ to(room: string | string[]): BroadcastOperator<EmitEvents>;
130
+ /**
131
+ * Targets a room when emitting.
132
+ *
133
+ * @param room
134
+ * @return BroadcastOperator
135
+ * @public
136
+ */
137
+ in(room: string | string[]): BroadcastOperator<EmitEvents>;
138
+ /**
139
+ * Excludes a room when emitting.
140
+ *
141
+ * @param room
142
+ * @return BroadcastOperator
143
+ * @public
144
+ */
145
+ except(room: string | string[]): BroadcastOperator<EmitEvents>;
146
+ /**
147
+ * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
148
+ * receive messages (because of network slowness or other issues, or because they’re connected through long polling
149
+ * and is in the middle of a request-response cycle).
150
+ *
151
+ * @return BroadcastOperator
152
+ * @public
153
+ */
154
+ get volatile(): BroadcastOperator<EmitEvents>;
155
+ /**
156
+ * Sets the compress flag.
157
+ *
158
+ * @param compress - if `true`, compresses the sending data
159
+ * @return BroadcastOperator
160
+ * @public
161
+ */
162
+ compress(compress: boolean): BroadcastOperator<EmitEvents>;
163
+ }
164
+ //#endregion
165
+ export { Emitter };
166
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/typed-events.ts","../src/types.ts","../src/operator.ts","../src/emitter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAIA;AASiB,UATA,SAAA,CASgB;EAOrB,CAAA,KAAA,EAAA,MAAU,CAAA,EAAA,GAAA;AAGtB;;;;;AAGmB,UAbF,gBAAA,CAaE;EAAf,CAAA,KAAA,EAAA,MAAA,CAAA,EAAA,CAAA,GAAA,IAAA,EAAA,OAAA,EAAA,EAAA,GAAA,IAAA;;AAMJ;;;AACkB,KAbN,UAaM,CAAA,YAbiB,SAajB,CAAA,GAAA,MAboC,GAapC,GAAA,CA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#region src/types.ts
6
+ let PacketType = /* @__PURE__ */ function(PacketType$1) {
7
+ PacketType$1[PacketType$1["CONNECT"] = 0] = "CONNECT";
8
+ PacketType$1[PacketType$1["DISCONNECT"] = 1] = "DISCONNECT";
9
+ PacketType$1[PacketType$1["EVENT"] = 2] = "EVENT";
10
+ PacketType$1[PacketType$1["ACK"] = 3] = "ACK";
11
+ PacketType$1[PacketType$1["CONNECT_ERROR"] = 4] = "CONNECT_ERROR";
12
+ PacketType$1[PacketType$1["BINARY_EVENT"] = 5] = "BINARY_EVENT";
13
+ PacketType$1[PacketType$1["BINARY_ACK"] = 6] = "BINARY_ACK";
14
+ return PacketType$1;
15
+ }({});
16
+ const RESERVED_EVENTS = new Set([
17
+ "connect",
18
+ "connect_error",
19
+ "disconnect",
20
+ "disconnecting",
21
+ "newListener",
22
+ "removeListener"
23
+ ]);
24
+
25
+ //#endregion
26
+ //#region src/operator.ts
27
+ var BroadcastOperator = class BroadcastOperator {
28
+ constructor(topic, broadcastOptions, rooms = /* @__PURE__ */ new Set(), exceptRooms = /* @__PURE__ */ new Set(), flags = {}) {
29
+ this.topic = topic;
30
+ this.broadcastOptions = broadcastOptions;
31
+ this.rooms = rooms;
32
+ this.exceptRooms = exceptRooms;
33
+ this.flags = flags;
34
+ }
35
+ /**
36
+ * Emits to all clients.
37
+ *
38
+ * @return Always true
39
+ * @public
40
+ */
41
+ emit(ev, ...args) {
42
+ if (RESERVED_EVENTS.has(ev)) throw new Error(`"${ev}" is a reserved event name`);
43
+ const uid = randomBytes(8).toString("hex");
44
+ const nsp = this.broadcastOptions.nsp;
45
+ const data = [ev, ...args];
46
+ const packet = {
47
+ type: PacketType.EVENT,
48
+ data,
49
+ nsp
50
+ };
51
+ const opts = {
52
+ rooms: [...this.rooms],
53
+ flags: this.flags,
54
+ except: [...this.exceptRooms]
55
+ };
56
+ const clusterMessage = {
57
+ nsp,
58
+ uid,
59
+ type: MessageType.BROADCAST,
60
+ data: {
61
+ packet,
62
+ opts
63
+ }
64
+ };
65
+ const message = {
66
+ attributes: {
67
+ nsp,
68
+ uid
69
+ },
70
+ data: encode(clusterMessage)
71
+ };
72
+ this.topic.publishMessage(message);
73
+ return true;
74
+ }
75
+ /**
76
+ * Targets a room when emitting.
77
+ *
78
+ * @param room
79
+ * @return a new BroadcastOperator instance
80
+ * @public
81
+ */
82
+ to(room) {
83
+ const rooms = new Set(this.rooms);
84
+ if (Array.isArray(room)) for (const r of room) rooms.add(r);
85
+ else rooms.add(room);
86
+ return new BroadcastOperator(this.topic, this.broadcastOptions, rooms, this.exceptRooms, this.flags);
87
+ }
88
+ /**
89
+ * Targets a room when emitting.
90
+ *
91
+ * @param room
92
+ * @return a new BroadcastOperator instance
93
+ * @public
94
+ */
95
+ in(room) {
96
+ return this.to(room);
97
+ }
98
+ /**
99
+ * Excludes a room when emitting.
100
+ *
101
+ * @param room
102
+ * @return a new BroadcastOperator instance
103
+ * @public
104
+ */
105
+ except(room) {
106
+ const exceptRooms = new Set(this.exceptRooms);
107
+ if (Array.isArray(room)) for (const r of room) exceptRooms.add(r);
108
+ else exceptRooms.add(room);
109
+ return new BroadcastOperator(this.topic, this.broadcastOptions, this.rooms, exceptRooms, this.flags);
110
+ }
111
+ /**
112
+ * Sets the compress flag.
113
+ *
114
+ * @param compress - if `true`, compresses the sending data
115
+ * @return a new BroadcastOperator instance
116
+ * @public
117
+ */
118
+ compress(compress) {
119
+ const flags = Object.assign({}, this.flags, { compress });
120
+ return new BroadcastOperator(this.topic, this.broadcastOptions, this.rooms, this.exceptRooms, flags);
121
+ }
122
+ /**
123
+ * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
124
+ * receive messages (because of network slowness or other issues, or because they’re connected through long polling
125
+ * and is in the middle of a request-response cycle).
126
+ *
127
+ * @return a new BroadcastOperator instance
128
+ * @public
129
+ */
130
+ get volatile() {
131
+ const flags = Object.assign({}, this.flags, { volatile: true });
132
+ return new BroadcastOperator(this.topic, this.broadcastOptions, this.rooms, this.exceptRooms, flags);
133
+ }
134
+ };
135
+
136
+ //#endregion
137
+ //#region src/emitter.ts
138
+ var Emitter = class Emitter {
139
+ opts;
140
+ broadcastOptions;
141
+ constructor(topic, opts, nsp = "/") {
142
+ this.topic = topic;
143
+ this.nsp = nsp;
144
+ this.opts = opts || {};
145
+ this.broadcastOptions = { nsp };
146
+ }
147
+ /**
148
+ * Emits to all clients.
149
+ *
150
+ * @return Always true
151
+ * @public
152
+ */
153
+ emit(ev, ...args) {
154
+ return new BroadcastOperator(this.topic, this.broadcastOptions).emit(ev, ...args);
155
+ }
156
+ /**
157
+ * Return a new emitter for the given namespace.
158
+ *
159
+ * @param nsp - namespace
160
+ * @public
161
+ */
162
+ of(nsp) {
163
+ return new Emitter(this.topic, this.opts, (nsp[0] !== "/" ? "/" : "") + nsp);
164
+ }
165
+ /**
166
+ * Targets a room when emitting.
167
+ *
168
+ * @param room
169
+ * @return BroadcastOperator
170
+ * @public
171
+ */
172
+ to(room) {
173
+ return new BroadcastOperator(this.topic, this.broadcastOptions).to(room);
174
+ }
175
+ /**
176
+ * Targets a room when emitting.
177
+ *
178
+ * @param room
179
+ * @return BroadcastOperator
180
+ * @public
181
+ */
182
+ in(room) {
183
+ return new BroadcastOperator(this.topic, this.broadcastOptions).in(room);
184
+ }
185
+ /**
186
+ * Excludes a room when emitting.
187
+ *
188
+ * @param room
189
+ * @return BroadcastOperator
190
+ * @public
191
+ */
192
+ except(room) {
193
+ return new BroadcastOperator(this.topic, this.broadcastOptions).except(room);
194
+ }
195
+ /**
196
+ * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
197
+ * receive messages (because of network slowness or other issues, or because they’re connected through long polling
198
+ * and is in the middle of a request-response cycle).
199
+ *
200
+ * @return BroadcastOperator
201
+ * @public
202
+ */
203
+ get volatile() {
204
+ return new BroadcastOperator(this.topic, this.broadcastOptions).volatile;
205
+ }
206
+ /**
207
+ * Sets the compress flag.
208
+ *
209
+ * @param compress - if `true`, compresses the sending data
210
+ * @return BroadcastOperator
211
+ * @public
212
+ */
213
+ compress(compress) {
214
+ return new BroadcastOperator(this.topic, this.broadcastOptions).compress(compress);
215
+ }
216
+ };
217
+
218
+ //#endregion
219
+ export { Emitter };
220
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["RESERVED_EVENTS: ReadonlySet<string | symbol>","topic: Topic","broadcastOptions: BroadcastOptions","rooms: Set<string>","exceptRooms: Set<string>","flags: BroadcastFlags","packet: Packet","clusterMessage: ClusterMessage","message: Message","topic: Topic","nsp: string"],"sources":["../src/types.ts","../src/operator.ts","../src/emitter.ts"],"sourcesContent":["import type { Topic } from '@google-cloud/pubsub'\n\nexport enum RequestType {\n SOCKETS = 0,\n ALL_ROOMS = 1,\n REMOTE_JOIN = 2,\n REMOTE_LEAVE = 3,\n REMOTE_DISCONNECT = 4,\n REMOTE_FETCH = 5,\n SERVER_SIDE_EMIT = 6,\n}\n\nexport interface Parser {\n // biome-ignore lint/suspicious/noExplicitAny: It is any\n encode: (msg: any) => any\n}\n\nexport type EmitterOptions = unknown // No options yet\n\nexport interface BroadcastOptions {\n nsp: string\n}\n\nexport interface BroadcastFlags {\n volatile?: boolean\n compress?: boolean\n}\n\nexport enum PacketType {\n CONNECT = 0,\n DISCONNECT = 1,\n EVENT = 2,\n ACK = 3,\n CONNECT_ERROR = 4,\n BINARY_EVENT = 5,\n BINARY_ACK = 6,\n}\n\nexport const RESERVED_EVENTS: ReadonlySet<string | symbol> = new Set(<const>[\n 'connect',\n 'connect_error',\n 'disconnect',\n 'disconnecting',\n 'newListener',\n 'removeListener',\n])\n\nexport interface Packet {\n type: PacketType\n nsp: string\n // biome-ignore lint/suspicious/noExplicitAny: It is any\n data?: any\n id?: number\n attachments?: number\n}\n\nexport type Message = Parameters<Topic['publishMessage']>[0]\n","import { randomBytes } from 'node:crypto'\nimport type { Topic } from '@google-cloud/pubsub'\nimport { encode } from '@msgpack/msgpack'\nimport { type ClusterMessage, MessageType } from 'socket.io-adapter'\nimport type {\n EventNames,\n EventParams,\n EventsMap,\n TypedEventBroadcaster,\n} from './typed-events'\nimport {\n type BroadcastFlags,\n type BroadcastOptions,\n type Message,\n type Packet,\n PacketType,\n RESERVED_EVENTS,\n} from './types'\n\nexport class BroadcastOperator<EmitEvents extends EventsMap>\n implements TypedEventBroadcaster<EmitEvents>\n{\n constructor(\n private readonly topic: Topic,\n private readonly broadcastOptions: BroadcastOptions,\n private readonly rooms: Set<string> = new Set<string>(),\n private readonly exceptRooms: Set<string> = new Set<string>(),\n private readonly flags: BroadcastFlags = {}\n ) {}\n\n /**\n * Emits to all clients.\n *\n * @return Always true\n * @public\n */\n public emit<Ev extends EventNames<EmitEvents>>(\n ev: Ev,\n ...args: EventParams<EmitEvents, Ev>\n ): true {\n if (RESERVED_EVENTS.has(ev)) {\n throw new Error(`\"${ev as string}\" is a reserved event name`)\n }\n const uid = randomBytes(8).toString('hex')\n const nsp = this.broadcastOptions.nsp\n\n // set up packet object\n const data = [ev, ...args]\n const packet: Packet = {\n type: PacketType.EVENT,\n data,\n nsp,\n }\n\n const opts = {\n rooms: [...this.rooms],\n flags: this.flags,\n except: [...this.exceptRooms],\n }\n\n const clusterMessage: ClusterMessage = {\n nsp,\n uid,\n type: MessageType.BROADCAST,\n data: {\n packet,\n opts,\n },\n }\n\n const message: Message = {\n attributes: { nsp, uid },\n data: encode(clusterMessage),\n }\n\n this.topic.publishMessage(message)\n\n return true\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return a new BroadcastOperator instance\n * @public\n */\n public to(room: string | string[]): BroadcastOperator<EmitEvents> {\n const rooms = new Set(this.rooms)\n if (Array.isArray(room)) {\n for (const r of room) {\n rooms.add(r)\n }\n } else {\n rooms.add(room)\n }\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n rooms,\n this.exceptRooms,\n this.flags\n )\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return a new BroadcastOperator instance\n * @public\n */\n public in(room: string | string[]): BroadcastOperator<EmitEvents> {\n return this.to(room)\n }\n\n /**\n * Excludes a room when emitting.\n *\n * @param room\n * @return a new BroadcastOperator instance\n * @public\n */\n public except(room: string | string[]): BroadcastOperator<EmitEvents> {\n const exceptRooms = new Set(this.exceptRooms)\n if (Array.isArray(room)) {\n for (const r of room) {\n exceptRooms.add(r)\n }\n } else {\n exceptRooms.add(room)\n }\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n this.rooms,\n exceptRooms,\n this.flags\n )\n }\n\n /**\n * Sets the compress flag.\n *\n * @param compress - if `true`, compresses the sending data\n * @return a new BroadcastOperator instance\n * @public\n */\n public compress(compress: boolean): BroadcastOperator<EmitEvents> {\n const flags = Object.assign({}, this.flags, { compress })\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n this.rooms,\n this.exceptRooms,\n flags\n )\n }\n\n /**\n * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to\n * receive messages (because of network slowness or other issues, or because they’re connected through long polling\n * and is in the middle of a request-response cycle).\n *\n * @return a new BroadcastOperator instance\n * @public\n */\n public get volatile(): BroadcastOperator<EmitEvents> {\n const flags = Object.assign({}, this.flags, { volatile: true })\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n this.rooms,\n this.exceptRooms,\n flags\n )\n }\n}\n","import type { Topic } from '@google-cloud/pubsub'\nimport { BroadcastOperator } from './operator'\nimport type {\n DefaultEventsMap,\n EventNames,\n EventParams,\n EventsMap,\n} from './typed-events'\nimport type { BroadcastOptions, EmitterOptions } from './types'\n\nexport class Emitter<EmitEvents extends EventsMap = DefaultEventsMap> {\n private readonly opts: EmitterOptions\n private readonly broadcastOptions: BroadcastOptions\n\n constructor(\n readonly topic: Topic,\n opts?: EmitterOptions,\n readonly nsp: string = '/'\n ) {\n this.opts = opts || {}\n this.broadcastOptions = {\n nsp,\n }\n }\n\n /**\n * Emits to all clients.\n *\n * @return Always true\n * @public\n */\n public emit<Ev extends EventNames<EmitEvents>>(\n ev: Ev,\n ...args: EventParams<EmitEvents, Ev>\n ): true {\n return new BroadcastOperator<EmitEvents>(\n this.topic,\n this.broadcastOptions\n ).emit(ev, ...args)\n }\n\n /**\n * Return a new emitter for the given namespace.\n *\n * @param nsp - namespace\n * @public\n */\n public of(nsp: string): Emitter<EmitEvents> {\n return new Emitter(this.topic, this.opts, (nsp[0] !== '/' ? '/' : '') + nsp)\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return BroadcastOperator\n * @public\n */\n public to(room: string | string[]): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).to(room)\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return BroadcastOperator\n * @public\n */\n public in(room: string | string[]): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).in(room)\n }\n\n /**\n * Excludes a room when emitting.\n *\n * @param room\n * @return BroadcastOperator\n * @public\n */\n public except(room: string | string[]): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).except(room)\n }\n\n /**\n * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to\n * receive messages (because of network slowness or other issues, or because they’re connected through long polling\n * and is in the middle of a request-response cycle).\n *\n * @return BroadcastOperator\n * @public\n */\n public get volatile(): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).volatile\n }\n\n /**\n * Sets the compress flag.\n *\n * @param compress - if `true`, compresses the sending data\n * @return BroadcastOperator\n * @public\n */\n public compress(compress: boolean): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).compress(\n compress\n )\n }\n}\n"],"mappings":";;;;;AA4BA,IAAY,oDAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;;;AAGF,MAAaA,kBAAgD,IAAI,IAAW;CAC1E;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;;AC1BF,IAAa,oBAAb,MAAa,kBAEb;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;;;;;;;;;;CAW9E,IAAW,WAA0C;AACnD,SAAO,IAAI,kBAAkB,KAAK,OAAO,KAAK,iBAAiB,CAAC;;;;;;;;;CAUlE,AAAO,SAAS,UAAkD;AAChE,SAAO,IAAI,kBAAkB,KAAK,OAAO,KAAK,iBAAiB,CAAC,SAC9D,SACD"}
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
2
  "name": "@sebspark/socket.io-gcp-pubsub-emitter",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
- "main": "dist/index.js",
7
- "types": "dist/index.d.ts",
6
+ "main": "dist/index.mjs",
7
+ "types": "dist/index.d.mts",
8
8
  "exports": {
9
9
  ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
10
+ "types": "./dist/index.d.mts",
11
+ "import": "./dist/index.mjs"
12
12
  }
13
13
  },
14
14
  "files": [
15
15
  "dist"
16
16
  ],
17
17
  "scripts": {
18
- "build": "tsup src/index.ts --config ./tsup.config.ts",
18
+ "build": "spark-build src/index.ts",
19
19
  "dev": "tsc --watch --noEmit",
20
20
  "lint": "biome check .",
21
21
  "test": "vitest run --passWithNoTests --coverage",
package/dist/index.d.ts DELETED
@@ -1,160 +0,0 @@
1
- import { Topic } from '@google-cloud/pubsub';
2
-
3
- /**
4
- * An events map is an interface that maps event names to their value, which
5
- * represents the type of the `on` listener.
6
- */
7
- interface EventsMap {
8
- [event: string]: any;
9
- }
10
- /**
11
- * The default events map, used if no EventsMap is given. Using this EventsMap
12
- * is equivalent to accepting all event names, and any data.
13
- */
14
- interface DefaultEventsMap {
15
- [event: string]: (...args: unknown[]) => void;
16
- }
17
- /**
18
- * Returns a union type containing all the keys of an event map.
19
- */
20
- type EventNames<Map extends EventsMap> = keyof Map & (string | symbol);
21
- /** The tuple type representing the parameters of an event listener */
22
- type EventParams<Map extends EventsMap, Ev extends EventNames<Map>> = Parameters<Map[Ev]>;
23
- /**
24
- * Interface for classes that aren't `EventEmitter`s, but still expose a
25
- * strictly typed `emit` method.
26
- */
27
- interface TypedEventBroadcaster<EmitEvents extends EventsMap> {
28
- emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
29
- }
30
-
31
- type EmitterOptions = unknown;
32
- interface BroadcastOptions {
33
- nsp: string;
34
- }
35
- interface BroadcastFlags {
36
- volatile?: boolean;
37
- compress?: boolean;
38
- }
39
-
40
- declare class BroadcastOperator<EmitEvents extends EventsMap> implements TypedEventBroadcaster<EmitEvents> {
41
- private readonly topic;
42
- private readonly broadcastOptions;
43
- private readonly rooms;
44
- private readonly exceptRooms;
45
- private readonly flags;
46
- constructor(topic: Topic, broadcastOptions: BroadcastOptions, rooms?: Set<string>, exceptRooms?: Set<string>, flags?: BroadcastFlags);
47
- /**
48
- * Emits to all clients.
49
- *
50
- * @return Always true
51
- * @public
52
- */
53
- emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): true;
54
- /**
55
- * Targets a room when emitting.
56
- *
57
- * @param room
58
- * @return a new BroadcastOperator instance
59
- * @public
60
- */
61
- to(room: string | string[]): BroadcastOperator<EmitEvents>;
62
- /**
63
- * Targets a room when emitting.
64
- *
65
- * @param room
66
- * @return a new BroadcastOperator instance
67
- * @public
68
- */
69
- in(room: string | string[]): BroadcastOperator<EmitEvents>;
70
- /**
71
- * Excludes a room when emitting.
72
- *
73
- * @param room
74
- * @return a new BroadcastOperator instance
75
- * @public
76
- */
77
- except(room: string | string[]): BroadcastOperator<EmitEvents>;
78
- /**
79
- * Sets the compress flag.
80
- *
81
- * @param compress - if `true`, compresses the sending data
82
- * @return a new BroadcastOperator instance
83
- * @public
84
- */
85
- compress(compress: boolean): BroadcastOperator<EmitEvents>;
86
- /**
87
- * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
88
- * receive messages (because of network slowness or other issues, or because they’re connected through long polling
89
- * and is in the middle of a request-response cycle).
90
- *
91
- * @return a new BroadcastOperator instance
92
- * @public
93
- */
94
- get volatile(): BroadcastOperator<EmitEvents>;
95
- }
96
-
97
- declare class Emitter<EmitEvents extends EventsMap = DefaultEventsMap> {
98
- readonly topic: Topic;
99
- readonly nsp: string;
100
- private readonly opts;
101
- private readonly broadcastOptions;
102
- constructor(topic: Topic, opts?: EmitterOptions, nsp?: string);
103
- /**
104
- * Emits to all clients.
105
- *
106
- * @return Always true
107
- * @public
108
- */
109
- emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): true;
110
- /**
111
- * Return a new emitter for the given namespace.
112
- *
113
- * @param nsp - namespace
114
- * @public
115
- */
116
- of(nsp: string): Emitter<EmitEvents>;
117
- /**
118
- * Targets a room when emitting.
119
- *
120
- * @param room
121
- * @return BroadcastOperator
122
- * @public
123
- */
124
- to(room: string | string[]): BroadcastOperator<EmitEvents>;
125
- /**
126
- * Targets a room when emitting.
127
- *
128
- * @param room
129
- * @return BroadcastOperator
130
- * @public
131
- */
132
- in(room: string | string[]): BroadcastOperator<EmitEvents>;
133
- /**
134
- * Excludes a room when emitting.
135
- *
136
- * @param room
137
- * @return BroadcastOperator
138
- * @public
139
- */
140
- except(room: string | string[]): BroadcastOperator<EmitEvents>;
141
- /**
142
- * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
143
- * receive messages (because of network slowness or other issues, or because they’re connected through long polling
144
- * and is in the middle of a request-response cycle).
145
- *
146
- * @return BroadcastOperator
147
- * @public
148
- */
149
- get volatile(): BroadcastOperator<EmitEvents>;
150
- /**
151
- * Sets the compress flag.
152
- *
153
- * @param compress - if `true`, compresses the sending data
154
- * @return BroadcastOperator
155
- * @public
156
- */
157
- compress(compress: boolean): BroadcastOperator<EmitEvents>;
158
- }
159
-
160
- export { Emitter };
package/dist/index.js DELETED
@@ -1,249 +0,0 @@
1
- // src/operator.ts
2
- import { randomBytes } from "crypto";
3
- import { encode } from "@msgpack/msgpack";
4
- import { MessageType } from "socket.io-adapter";
5
-
6
- // src/types.ts
7
- var RESERVED_EVENTS = /* @__PURE__ */ new Set([
8
- "connect",
9
- "connect_error",
10
- "disconnect",
11
- "disconnecting",
12
- "newListener",
13
- "removeListener"
14
- ]);
15
-
16
- // src/operator.ts
17
- var BroadcastOperator = class _BroadcastOperator {
18
- constructor(topic, broadcastOptions, rooms = /* @__PURE__ */ new Set(), exceptRooms = /* @__PURE__ */ new Set(), flags = {}) {
19
- this.topic = topic;
20
- this.broadcastOptions = broadcastOptions;
21
- this.rooms = rooms;
22
- this.exceptRooms = exceptRooms;
23
- this.flags = flags;
24
- }
25
- /**
26
- * Emits to all clients.
27
- *
28
- * @return Always true
29
- * @public
30
- */
31
- emit(ev, ...args) {
32
- if (RESERVED_EVENTS.has(ev)) {
33
- throw new Error(`"${ev}" is a reserved event name`);
34
- }
35
- const uid = randomBytes(8).toString("hex");
36
- const nsp = this.broadcastOptions.nsp;
37
- const data = [ev, ...args];
38
- const packet = {
39
- type: 2 /* EVENT */,
40
- data,
41
- nsp
42
- };
43
- const opts = {
44
- rooms: [...this.rooms],
45
- flags: this.flags,
46
- except: [...this.exceptRooms]
47
- };
48
- const clusterMessage = {
49
- nsp,
50
- uid,
51
- type: MessageType.BROADCAST,
52
- data: {
53
- packet,
54
- opts
55
- }
56
- };
57
- const message = {
58
- attributes: { nsp, uid },
59
- data: encode(clusterMessage)
60
- };
61
- this.topic.publishMessage(message);
62
- return true;
63
- }
64
- /**
65
- * Targets a room when emitting.
66
- *
67
- * @param room
68
- * @return a new BroadcastOperator instance
69
- * @public
70
- */
71
- to(room) {
72
- const rooms = new Set(this.rooms);
73
- if (Array.isArray(room)) {
74
- for (const r of room) {
75
- rooms.add(r);
76
- }
77
- } else {
78
- rooms.add(room);
79
- }
80
- return new _BroadcastOperator(
81
- this.topic,
82
- this.broadcastOptions,
83
- rooms,
84
- this.exceptRooms,
85
- this.flags
86
- );
87
- }
88
- /**
89
- * Targets a room when emitting.
90
- *
91
- * @param room
92
- * @return a new BroadcastOperator instance
93
- * @public
94
- */
95
- in(room) {
96
- return this.to(room);
97
- }
98
- /**
99
- * Excludes a room when emitting.
100
- *
101
- * @param room
102
- * @return a new BroadcastOperator instance
103
- * @public
104
- */
105
- except(room) {
106
- const exceptRooms = new Set(this.exceptRooms);
107
- if (Array.isArray(room)) {
108
- for (const r of room) {
109
- exceptRooms.add(r);
110
- }
111
- } else {
112
- exceptRooms.add(room);
113
- }
114
- return new _BroadcastOperator(
115
- this.topic,
116
- this.broadcastOptions,
117
- this.rooms,
118
- exceptRooms,
119
- this.flags
120
- );
121
- }
122
- /**
123
- * Sets the compress flag.
124
- *
125
- * @param compress - if `true`, compresses the sending data
126
- * @return a new BroadcastOperator instance
127
- * @public
128
- */
129
- compress(compress) {
130
- const flags = Object.assign({}, this.flags, { compress });
131
- return new _BroadcastOperator(
132
- this.topic,
133
- this.broadcastOptions,
134
- this.rooms,
135
- this.exceptRooms,
136
- flags
137
- );
138
- }
139
- /**
140
- * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
141
- * receive messages (because of network slowness or other issues, or because they’re connected through long polling
142
- * and is in the middle of a request-response cycle).
143
- *
144
- * @return a new BroadcastOperator instance
145
- * @public
146
- */
147
- get volatile() {
148
- const flags = Object.assign({}, this.flags, { volatile: true });
149
- return new _BroadcastOperator(
150
- this.topic,
151
- this.broadcastOptions,
152
- this.rooms,
153
- this.exceptRooms,
154
- flags
155
- );
156
- }
157
- };
158
-
159
- // src/emitter.ts
160
- var Emitter = class _Emitter {
161
- constructor(topic, opts, nsp = "/") {
162
- this.topic = topic;
163
- this.nsp = nsp;
164
- this.opts = opts || {};
165
- this.broadcastOptions = {
166
- nsp
167
- };
168
- }
169
- opts;
170
- broadcastOptions;
171
- /**
172
- * Emits to all clients.
173
- *
174
- * @return Always true
175
- * @public
176
- */
177
- emit(ev, ...args) {
178
- return new BroadcastOperator(
179
- this.topic,
180
- this.broadcastOptions
181
- ).emit(ev, ...args);
182
- }
183
- /**
184
- * Return a new emitter for the given namespace.
185
- *
186
- * @param nsp - namespace
187
- * @public
188
- */
189
- of(nsp) {
190
- return new _Emitter(this.topic, this.opts, (nsp[0] !== "/" ? "/" : "") + nsp);
191
- }
192
- /**
193
- * Targets a room when emitting.
194
- *
195
- * @param room
196
- * @return BroadcastOperator
197
- * @public
198
- */
199
- to(room) {
200
- return new BroadcastOperator(this.topic, this.broadcastOptions).to(room);
201
- }
202
- /**
203
- * Targets a room when emitting.
204
- *
205
- * @param room
206
- * @return BroadcastOperator
207
- * @public
208
- */
209
- in(room) {
210
- return new BroadcastOperator(this.topic, this.broadcastOptions).in(room);
211
- }
212
- /**
213
- * Excludes a room when emitting.
214
- *
215
- * @param room
216
- * @return BroadcastOperator
217
- * @public
218
- */
219
- except(room) {
220
- return new BroadcastOperator(this.topic, this.broadcastOptions).except(room);
221
- }
222
- /**
223
- * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
224
- * receive messages (because of network slowness or other issues, or because they’re connected through long polling
225
- * and is in the middle of a request-response cycle).
226
- *
227
- * @return BroadcastOperator
228
- * @public
229
- */
230
- get volatile() {
231
- return new BroadcastOperator(this.topic, this.broadcastOptions).volatile;
232
- }
233
- /**
234
- * Sets the compress flag.
235
- *
236
- * @param compress - if `true`, compresses the sending data
237
- * @return BroadcastOperator
238
- * @public
239
- */
240
- compress(compress) {
241
- return new BroadcastOperator(this.topic, this.broadcastOptions).compress(
242
- compress
243
- );
244
- }
245
- };
246
- export {
247
- Emitter
248
- };
249
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/operator.ts","../src/types.ts","../src/emitter.ts"],"sourcesContent":["import { randomBytes } from 'node:crypto'\nimport type { Topic } from '@google-cloud/pubsub'\nimport { encode } from '@msgpack/msgpack'\nimport { type ClusterMessage, MessageType } from 'socket.io-adapter'\nimport type {\n EventNames,\n EventParams,\n EventsMap,\n TypedEventBroadcaster,\n} from './typed-events'\nimport {\n type BroadcastFlags,\n type BroadcastOptions,\n type Message,\n type Packet,\n PacketType,\n RESERVED_EVENTS,\n} from './types'\n\nexport class BroadcastOperator<EmitEvents extends EventsMap>\n implements TypedEventBroadcaster<EmitEvents>\n{\n constructor(\n private readonly topic: Topic,\n private readonly broadcastOptions: BroadcastOptions,\n private readonly rooms: Set<string> = new Set<string>(),\n private readonly exceptRooms: Set<string> = new Set<string>(),\n private readonly flags: BroadcastFlags = {}\n ) {}\n\n /**\n * Emits to all clients.\n *\n * @return Always true\n * @public\n */\n public emit<Ev extends EventNames<EmitEvents>>(\n ev: Ev,\n ...args: EventParams<EmitEvents, Ev>\n ): true {\n if (RESERVED_EVENTS.has(ev)) {\n throw new Error(`\"${ev as string}\" is a reserved event name`)\n }\n const uid = randomBytes(8).toString('hex')\n const nsp = this.broadcastOptions.nsp\n\n // set up packet object\n const data = [ev, ...args]\n const packet: Packet = {\n type: PacketType.EVENT,\n data,\n nsp,\n }\n\n const opts = {\n rooms: [...this.rooms],\n flags: this.flags,\n except: [...this.exceptRooms],\n }\n\n const clusterMessage: ClusterMessage = {\n nsp,\n uid,\n type: MessageType.BROADCAST,\n data: {\n packet,\n opts,\n },\n }\n\n const message: Message = {\n attributes: { nsp, uid },\n data: encode(clusterMessage),\n }\n\n this.topic.publishMessage(message)\n\n return true\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return a new BroadcastOperator instance\n * @public\n */\n public to(room: string | string[]): BroadcastOperator<EmitEvents> {\n const rooms = new Set(this.rooms)\n if (Array.isArray(room)) {\n for (const r of room) {\n rooms.add(r)\n }\n } else {\n rooms.add(room)\n }\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n rooms,\n this.exceptRooms,\n this.flags\n )\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return a new BroadcastOperator instance\n * @public\n */\n public in(room: string | string[]): BroadcastOperator<EmitEvents> {\n return this.to(room)\n }\n\n /**\n * Excludes a room when emitting.\n *\n * @param room\n * @return a new BroadcastOperator instance\n * @public\n */\n public except(room: string | string[]): BroadcastOperator<EmitEvents> {\n const exceptRooms = new Set(this.exceptRooms)\n if (Array.isArray(room)) {\n for (const r of room) {\n exceptRooms.add(r)\n }\n } else {\n exceptRooms.add(room)\n }\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n this.rooms,\n exceptRooms,\n this.flags\n )\n }\n\n /**\n * Sets the compress flag.\n *\n * @param compress - if `true`, compresses the sending data\n * @return a new BroadcastOperator instance\n * @public\n */\n public compress(compress: boolean): BroadcastOperator<EmitEvents> {\n const flags = Object.assign({}, this.flags, { compress })\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n this.rooms,\n this.exceptRooms,\n flags\n )\n }\n\n /**\n * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to\n * receive messages (because of network slowness or other issues, or because they’re connected through long polling\n * and is in the middle of a request-response cycle).\n *\n * @return a new BroadcastOperator instance\n * @public\n */\n public get volatile(): BroadcastOperator<EmitEvents> {\n const flags = Object.assign({}, this.flags, { volatile: true })\n return new BroadcastOperator(\n this.topic,\n this.broadcastOptions,\n this.rooms,\n this.exceptRooms,\n flags\n )\n }\n}\n","import type { Topic } from '@google-cloud/pubsub'\n\nexport enum RequestType {\n SOCKETS = 0,\n ALL_ROOMS = 1,\n REMOTE_JOIN = 2,\n REMOTE_LEAVE = 3,\n REMOTE_DISCONNECT = 4,\n REMOTE_FETCH = 5,\n SERVER_SIDE_EMIT = 6,\n}\n\nexport interface Parser {\n // biome-ignore lint/suspicious/noExplicitAny: It is any\n encode: (msg: any) => any\n}\n\nexport type EmitterOptions = unknown // No options yet\n\nexport interface BroadcastOptions {\n nsp: string\n}\n\nexport interface BroadcastFlags {\n volatile?: boolean\n compress?: boolean\n}\n\nexport enum PacketType {\n CONNECT = 0,\n DISCONNECT = 1,\n EVENT = 2,\n ACK = 3,\n CONNECT_ERROR = 4,\n BINARY_EVENT = 5,\n BINARY_ACK = 6,\n}\n\nexport const RESERVED_EVENTS: ReadonlySet<string | symbol> = new Set(<const>[\n 'connect',\n 'connect_error',\n 'disconnect',\n 'disconnecting',\n 'newListener',\n 'removeListener',\n])\n\nexport interface Packet {\n type: PacketType\n nsp: string\n // biome-ignore lint/suspicious/noExplicitAny: It is any\n data?: any\n id?: number\n attachments?: number\n}\n\nexport type Message = Parameters<Topic['publishMessage']>[0]\n","import type { Topic } from '@google-cloud/pubsub'\nimport { BroadcastOperator } from './operator'\nimport type {\n DefaultEventsMap,\n EventNames,\n EventParams,\n EventsMap,\n} from './typed-events'\nimport type { BroadcastOptions, EmitterOptions } from './types'\n\nexport class Emitter<EmitEvents extends EventsMap = DefaultEventsMap> {\n private readonly opts: EmitterOptions\n private readonly broadcastOptions: BroadcastOptions\n\n constructor(\n readonly topic: Topic,\n opts?: EmitterOptions,\n readonly nsp: string = '/'\n ) {\n this.opts = opts || {}\n this.broadcastOptions = {\n nsp,\n }\n }\n\n /**\n * Emits to all clients.\n *\n * @return Always true\n * @public\n */\n public emit<Ev extends EventNames<EmitEvents>>(\n ev: Ev,\n ...args: EventParams<EmitEvents, Ev>\n ): true {\n return new BroadcastOperator<EmitEvents>(\n this.topic,\n this.broadcastOptions\n ).emit(ev, ...args)\n }\n\n /**\n * Return a new emitter for the given namespace.\n *\n * @param nsp - namespace\n * @public\n */\n public of(nsp: string): Emitter<EmitEvents> {\n return new Emitter(this.topic, this.opts, (nsp[0] !== '/' ? '/' : '') + nsp)\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return BroadcastOperator\n * @public\n */\n public to(room: string | string[]): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).to(room)\n }\n\n /**\n * Targets a room when emitting.\n *\n * @param room\n * @return BroadcastOperator\n * @public\n */\n public in(room: string | string[]): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).in(room)\n }\n\n /**\n * Excludes a room when emitting.\n *\n * @param room\n * @return BroadcastOperator\n * @public\n */\n public except(room: string | string[]): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).except(room)\n }\n\n /**\n * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to\n * receive messages (because of network slowness or other issues, or because they’re connected through long polling\n * and is in the middle of a request-response cycle).\n *\n * @return BroadcastOperator\n * @public\n */\n public get volatile(): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).volatile\n }\n\n /**\n * Sets the compress flag.\n *\n * @param compress - if `true`, compresses the sending data\n * @return BroadcastOperator\n * @public\n */\n public compress(compress: boolean): BroadcastOperator<EmitEvents> {\n return new BroadcastOperator(this.topic, this.broadcastOptions).compress(\n compress\n )\n }\n}\n"],"mappings":";AAAA,SAAS,mBAAmB;AAE5B,SAAS,cAAc;AACvB,SAA8B,mBAAmB;;;ACmC1C,IAAM,kBAAgD,oBAAI,IAAW;AAAA,EAC1E;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;;;AD1BM,IAAM,oBAAN,MAAM,mBAEb;AAAA,EACE,YACmB,OACA,kBACA,QAAqB,oBAAI,IAAY,GACrC,cAA2B,oBAAI,IAAY,GAC3C,QAAwB,CAAC,GAC1C;AALiB;AACA;AACA;AACA;AACA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQI,KACL,OACG,MACG;AACN,QAAI,gBAAgB,IAAI,EAAE,GAAG;AAC3B,YAAM,IAAI,MAAM,IAAI,EAAY,4BAA4B;AAAA,IAC9D;AACA,UAAM,MAAM,YAAY,CAAC,EAAE,SAAS,KAAK;AACzC,UAAM,MAAM,KAAK,iBAAiB;AAGlC,UAAM,OAAO,CAAC,IAAI,GAAG,IAAI;AACzB,UAAM,SAAiB;AAAA,MACrB;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAEA,UAAM,OAAO;AAAA,MACX,OAAO,CAAC,GAAG,KAAK,KAA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