bxo 0.0.10-dev.1 โ 0.0.10-dev.2
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/example/websocket-example.ts +28 -32
- package/package.json +1 -1
- package/src/index.ts +4 -50
|
@@ -143,19 +143,18 @@ async function main() {
|
|
|
143
143
|
console.log(` ๐ Search Params:`, data.searchParams);
|
|
144
144
|
console.log(` ๐ Authorization:`, data.authorization || 'None');
|
|
145
145
|
|
|
146
|
-
// Use
|
|
147
|
-
console.log(` ๐ช Session Cookie:`, data.
|
|
148
|
-
console.log(` ๐ Room Param:`, data.
|
|
149
|
-
console.log(` ๐ค Username:`, data.
|
|
150
|
-
console.log(` ๐ Room:`, data.
|
|
151
|
-
console.log(` ๐ Has Auth:`, data.
|
|
152
|
-
console.log(` ๐ Auth Type:`, data.
|
|
153
|
-
console.log(` ๐ซ Token:`, data.
|
|
146
|
+
// Use direct property access
|
|
147
|
+
console.log(` ๐ช Session Cookie:`, data.cookies.sessionId);
|
|
148
|
+
console.log(` ๐ Room Param:`, data.searchParams.room);
|
|
149
|
+
console.log(` ๐ค Username:`, data.searchParams.user || data.searchParams.username);
|
|
150
|
+
console.log(` ๐ Room:`, data.searchParams.room);
|
|
151
|
+
console.log(` ๐ Has Auth:`, !!(data.authorization || data.searchParams.token || data.searchParams.auth));
|
|
152
|
+
console.log(` ๐ Auth Type:`, data.authorization ? (data.authorization.toLowerCase().startsWith('bearer ') ? 'bearer' : 'basic') : 'none');
|
|
153
|
+
console.log(` ๐ซ Token:`, data.searchParams.token || data.searchParams.auth);
|
|
154
154
|
|
|
155
|
-
// Check for authentication using
|
|
156
|
-
if (data.
|
|
157
|
-
|
|
158
|
-
console.log(` โ
Authenticated user detected (${authType})`);
|
|
155
|
+
// Check for authentication using direct property access
|
|
156
|
+
if (data.authorization || data.searchParams.token || data.searchParams.auth) {
|
|
157
|
+
console.log(` โ
Authenticated user detected`);
|
|
159
158
|
ws.send(`Welcome authenticated user! Your WebSocket ID is: ${data.id}`);
|
|
160
159
|
} else {
|
|
161
160
|
ws.send(`Welcome! Your WebSocket ID is: ${data.id}`);
|
|
@@ -169,10 +168,10 @@ async function main() {
|
|
|
169
168
|
console.log(` ๐ Search params:`, data.searchParams);
|
|
170
169
|
console.log(` ๐ Auth:`, data.authorization || 'None');
|
|
171
170
|
|
|
172
|
-
// Use
|
|
173
|
-
const username = data.
|
|
174
|
-
const room = data.
|
|
175
|
-
const sessionId = data.
|
|
171
|
+
// Use direct property access
|
|
172
|
+
const username = data.searchParams.user || data.searchParams.username || 'Anonymous';
|
|
173
|
+
const room = data.searchParams.room;
|
|
174
|
+
const sessionId = data.cookies.sessionId;
|
|
176
175
|
|
|
177
176
|
console.log(` ๐ค Username: ${username}`);
|
|
178
177
|
console.log(` ๐ Room: ${room || 'None'}`);
|
|
@@ -181,7 +180,7 @@ async function main() {
|
|
|
181
180
|
// Echo the message back with client ID and context
|
|
182
181
|
let response = `[${data.id}] Echo: ${message}`;
|
|
183
182
|
|
|
184
|
-
// Add context using
|
|
183
|
+
// Add context using direct property access
|
|
185
184
|
if (room) {
|
|
186
185
|
response += ` (Room: ${room})`;
|
|
187
186
|
}
|
|
@@ -219,10 +218,10 @@ async function main() {
|
|
|
219
218
|
console.log(` ๐ Search Params:`, data.searchParams);
|
|
220
219
|
console.log(` ๐ Authorization:`, data.authorization || 'None');
|
|
221
220
|
|
|
222
|
-
// Use
|
|
223
|
-
const username = data.
|
|
224
|
-
const sessionId = data.
|
|
225
|
-
const hasAuth = data.
|
|
221
|
+
// Use direct property access
|
|
222
|
+
const username = data.searchParams.user || data.searchParams.username || 'Anonymous';
|
|
223
|
+
const sessionId = data.cookies.sessionId;
|
|
224
|
+
const hasAuth = !!(data.authorization || data.searchParams.token || data.searchParams.auth);
|
|
226
225
|
|
|
227
226
|
console.log(` ๐ค Username: ${username}`);
|
|
228
227
|
console.log(` ๐ช Session: ${sessionId || 'None'}`);
|
|
@@ -234,8 +233,8 @@ async function main() {
|
|
|
234
233
|
message(ws, message) {
|
|
235
234
|
const data = ws.data; // Fully typed WebSocketData
|
|
236
235
|
const room = data.path.split('/').pop() || 'unknown';
|
|
237
|
-
const username = data.
|
|
238
|
-
const sessionId = data.
|
|
236
|
+
const username = data.searchParams.user || data.searchParams.username || 'Anonymous';
|
|
237
|
+
const sessionId = data.cookies.sessionId;
|
|
239
238
|
|
|
240
239
|
console.log(`๐ฌ Message in room ${room} from ${data.id} (${username}):`, message);
|
|
241
240
|
console.log(` ๐ช Client cookies:`, data.cookies);
|
|
@@ -263,15 +262,12 @@ async function main() {
|
|
|
263
262
|
console.log(` โข ws.data.searchParams - Query parameters from URL`);
|
|
264
263
|
console.log(` โข ws.data.authorization - Authorization header`);
|
|
265
264
|
console.log(` โข Client IP, User Agent, Origin tracking`);
|
|
266
|
-
console.log(`\n๐ง
|
|
267
|
-
console.log(` โข ws.data.
|
|
268
|
-
console.log(` โข ws.data.
|
|
269
|
-
console.log(` โข ws.data.
|
|
270
|
-
console.log(` โข ws.data.
|
|
271
|
-
console.log(` โข ws.data.
|
|
272
|
-
console.log(` โข ws.data.getUsername() - Get username from params`);
|
|
273
|
-
console.log(` โข ws.data.getRoom() - Get room from params`);
|
|
274
|
-
console.log(` โข ws.data.getUser() - Get user from params`);
|
|
265
|
+
console.log(`\n๐ง Direct Property Access:`);
|
|
266
|
+
console.log(` โข ws.data.cookies.name - Get specific cookie`);
|
|
267
|
+
console.log(` โข ws.data.searchParams.name - Get specific search param`);
|
|
268
|
+
console.log(` โข ws.data.authorization - Check authorization header`);
|
|
269
|
+
console.log(` โข ws.data.id - Short client identifier`);
|
|
270
|
+
console.log(` โข ws.data.connectionId - Detailed connection ID`);
|
|
275
271
|
console.log(`\n๐งช Test URLs:`);
|
|
276
272
|
console.log(` โข Basic: ws://localhost:${app.server?.port}/ws`);
|
|
277
273
|
console.log(` โข With params: ws://localhost:${app.server?.port}/ws?room=test&user=john&theme=dark`);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -108,15 +108,8 @@ export type WebSocketClientInfo = {
|
|
|
108
108
|
|
|
109
109
|
// WebSocket data interface - this is what you get when accessing ws.data
|
|
110
110
|
export interface WebSocketData extends WebSocketClientInfo {
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
getSearchParam(name: string): string | string[] | undefined;
|
|
114
|
-
hasAuth(): boolean;
|
|
115
|
-
getAuthType(): 'bearer' | 'basic' | 'custom' | 'none';
|
|
116
|
-
getToken(): string | undefined;
|
|
117
|
-
getUsername(): string | undefined;
|
|
118
|
-
getRoom(): string | undefined;
|
|
119
|
-
getUser(): string | undefined;
|
|
111
|
+
// Simple interface with just the data properties
|
|
112
|
+
// No convenience methods - use direct property access
|
|
120
113
|
}
|
|
121
114
|
|
|
122
115
|
// WebSocket handler types
|
|
@@ -530,48 +523,9 @@ export default class BXO {
|
|
|
530
523
|
authorization: authHeader || undefined // Authorization header
|
|
531
524
|
};
|
|
532
525
|
|
|
533
|
-
// Create
|
|
526
|
+
// Create simple WebSocket data object
|
|
534
527
|
const clientInfo: WebSocketData = {
|
|
535
|
-
...baseClientInfo
|
|
536
|
-
|
|
537
|
-
// Convenience methods
|
|
538
|
-
getCookie(name: string): string | undefined {
|
|
539
|
-
return this.cookies[name];
|
|
540
|
-
},
|
|
541
|
-
|
|
542
|
-
getSearchParam(name: string): string | string[] | undefined {
|
|
543
|
-
return this.searchParams[name];
|
|
544
|
-
},
|
|
545
|
-
|
|
546
|
-
hasAuth(): boolean {
|
|
547
|
-
return !!this.authorization || !!this.searchParams.token || !!this.searchParams.auth;
|
|
548
|
-
},
|
|
549
|
-
|
|
550
|
-
getAuthType(): 'bearer' | 'basic' | 'custom' | 'none' {
|
|
551
|
-
if (!this.authorization) return 'none';
|
|
552
|
-
if (this.authorization.toLowerCase().startsWith('bearer ')) return 'bearer';
|
|
553
|
-
if (this.authorization.toLowerCase().startsWith('basic ')) return 'basic';
|
|
554
|
-
return 'custom';
|
|
555
|
-
},
|
|
556
|
-
|
|
557
|
-
getToken(): string | undefined {
|
|
558
|
-
if (this.authorization?.toLowerCase().startsWith('bearer ')) {
|
|
559
|
-
return this.authorization.substring(7);
|
|
560
|
-
}
|
|
561
|
-
return this.searchParams.token as string || this.searchParams.auth as string;
|
|
562
|
-
},
|
|
563
|
-
|
|
564
|
-
getUsername(): string | undefined {
|
|
565
|
-
return this.searchParams.user as string || this.searchParams.username as string;
|
|
566
|
-
},
|
|
567
|
-
|
|
568
|
-
getRoom(): string | undefined {
|
|
569
|
-
return this.searchParams.room as string;
|
|
570
|
-
},
|
|
571
|
-
|
|
572
|
-
getUser(): string | undefined {
|
|
573
|
-
return this.searchParams.user as string;
|
|
574
|
-
}
|
|
528
|
+
...baseClientInfo
|
|
575
529
|
};
|
|
576
530
|
|
|
577
531
|
const success = server.upgrade(req, {
|