Skip to content

Commit 2bb6eba

Browse files
authored
Simplifies the format for client IDs. (#2072)
1 parent 77e4177 commit 2bb6eba

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

pkgs/sse/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.1.8
2+
3+
- Simplify the format of client ID strings.
4+
15
## 4.1.7
26

37
- Move to `dart-lang/tools` monorepo.

pkgs/sse/lib/client/sse_client.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:pool/pool.dart';
1111
import 'package:stream_channel/stream_channel.dart';
1212
import 'package:web/web.dart';
1313

14-
import '../src/util/uuid.dart';
14+
import '../src/util/id.dart';
1515

1616
/// Limit for the number of concurrent outgoing requests.
1717
///
@@ -21,7 +21,7 @@ import '../src/util/uuid.dart';
2121
/// Note Chrome's limit is 6000. So this gives us plenty of headroom.
2222
final _requestPool = Pool(1000);
2323

24-
/// A client for bi-directional sse communication.
24+
/// A client for bi-directional SSE communication.
2525
///
2626
/// The client can send any JSON-encodable messages to the server by adding
2727
/// them to the [sink] and listen to messages from the server on the [stream].
@@ -48,9 +48,8 @@ class SseClient extends StreamChannelMixin<String?> {
4848
/// incoming bi-directional SSE connections. [debugKey] is an optional key
4949
/// that can be used to identify the SSE connection.
5050
SseClient(String serverUrl, {String? debugKey})
51-
: _clientId = debugKey == null
52-
? generateUuidV4()
53-
: '$debugKey-${generateUuidV4()}' {
51+
: _clientId =
52+
debugKey == null ? generateId() : '$debugKey-${generateId()}' {
5453
_serverUrl = '$serverUrl?sseClientId=$_clientId';
5554
_eventSource =
5655
EventSource(_serverUrl, EventSourceInit(withCredentials: true));

pkgs/sse/lib/src/util/id.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:math' show Random;
6+
7+
/// Generates a pseudo-random ID string with 32 bits of entropy.
8+
String generateId() {
9+
final chars = List<int>.filled(6, 0);
10+
final random = Random();
11+
var bits = random.nextInt(0x100000000);
12+
for (var i = 0; i < 6; i++) {
13+
chars[i] = _base64Chars.codeUnitAt(bits & 0x3F);
14+
bits >>>= 6;
15+
}
16+
return String.fromCharCodes(chars);
17+
}
18+
19+
// A standard encoding of 6 bits per character, without any non-ASCII,
20+
// non-printable or disallowed characters.
21+
const _base64Chars =
22+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

pkgs/sse/lib/src/util/uuid.dart

Lines changed: 0 additions & 32 deletions
This file was deleted.

pkgs/sse/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sse
2-
version: 4.1.7
2+
version: 4.1.8
33
description: >-
44
Provides client and server functionality for setting up bi-directional
55
communication through Server Sent Events (SSE) and corresponding POST

0 commit comments

Comments
 (0)