Skip to content

Commit bedf535

Browse files
author
Amin Mahboubi
authored
replace uuid with nanoid (#518)
* replace uuid with nanoid * embed nanoid
1 parent a6e64c7 commit bedf535

5 files changed

Lines changed: 22 additions & 15 deletions

File tree

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@
3838
"@babel/runtime": "^7.12.1",
3939
"@types/jsonwebtoken": "^8.5.0",
4040
"@types/seamless-immutable": "7.1.13",
41-
"@types/uuid": "^8.3.0",
4241
"@types/ws": "^7.2.7",
4342
"axios": "^0.21.0",
4443
"base64-js": "^1.3.1",
4544
"form-data": "^3.0.0",
4645
"isomorphic-ws": "^4.0.1",
4746
"jsonwebtoken": "^8.5.1",
4847
"seamless-immutable": "^7.1.4",
49-
"uuid": "^8.3.1",
5048
"ws": "^7.3.1"
5149
},
5250
"devDependencies": {
@@ -109,7 +107,8 @@
109107
"rollup-plugin-replace": "^2.1.0",
110108
"rollup-plugin-terser": "^7.0.2",
111109
"sinon": "^9.2.0",
112-
"typescript": "^4.0.3"
110+
"typescript": "^4.0.3",
111+
"uuid": "^8.3.1"
113112
},
114113
"scripts": {
115114
"start": "yarn run compile -w",

rollup.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const externalPackages = [
1515
'form-data',
1616
'isomorphic-ws',
1717
'seamless-immutable',
18-
'uuid',
1918
'base64-js',
2019
/@babel\/runtime/,
2120
];

src/client.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33

44
import axios, { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios';
55
import https from 'https';
6-
import { v4 as uuidv4 } from 'uuid';
76
import WebSocket from 'isomorphic-ws';
7+
88
import { Channel } from './channel';
99
import { ClientState } from './client_state';
1010
import { StableWSConnection } from './connection';
1111
import { isValidEventType } from './events';
1212
import { JWTUserToken, DevToken, CheckSignature } from './signing';
1313
import { TokenManager } from './token_manager';
14-
import { isFunction, addFileToFormData, chatCodes, normalizeQuerySort } from './utils';
14+
import {
15+
isFunction,
16+
addFileToFormData,
17+
chatCodes,
18+
normalizeQuerySort,
19+
randomId,
20+
} from './utils';
1521

1622
import {
1723
APIResponse,
@@ -145,7 +151,6 @@ export class StreamChat<
145151
user?: UserResponse<UserType>;
146152
userAgent?: string;
147153
userID?: string;
148-
UUID?: string;
149154
wsBaseURL?: string;
150155
wsConnection: StableWSConnection<ChannelType, CommandType, UserType> | null;
151156
wsPromise: ConnectAPIResponse<ChannelType, CommandType, UserType> | null;
@@ -301,8 +306,7 @@ export class StreamChat<
301306
}
302307

303308
_setupConnection = () => {
304-
this.UUID = uuidv4();
305-
this.clientID = `${this.userID}--${this.UUID}`;
309+
this.clientID = `${this.userID}--${randomId()}`;
306310
this.wsPromise = this.connect();
307311
this._startCleaning();
308312
return this.wsPromise;
@@ -466,7 +470,7 @@ export class StreamChat<
466470

467471
setAnonymousUser = () => {
468472
this.anonymous = true;
469-
this.userID = uuidv4();
473+
this.userID = randomId();
470474
const anonymousUser = {
471475
id: this.userID,
472476
anon: true,

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,13 @@ export function normalizeQuerySort<T extends QuerySort>(sort: T) {
9696
}
9797
return sortFields;
9898
}
99+
100+
/** adopted from https://github.com/ai/nanoid/blob/master/non-secure/index.js */
101+
const alphabet = 'ModuleSymbhasOwnPr0123456789ABCDEFGHNRVfgctiUvzKqYTJkLxpZXIjQW';
102+
export function randomId() {
103+
let id = '';
104+
for (let i = 0; i < 21; i++) {
105+
id += alphabet[(Math.random() * 64) | 0];
106+
}
107+
return id;
108+
}

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,11 +1407,6 @@
14071407
resolved "https://registry.yarnpkg.com/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-6.0.2.tgz#3a84cf5ec3249439015e14049bd3161419bf9eae"
14081408
integrity sha512-dIPoZ3g5gcx9zZEszaxLSVTvMReD3xxyyDnQUjA6IYDG9Ba2AV0otMPs+77sG9ojB4Qr2N2Vk5RnKeuA0X/0bg==
14091409

1410-
"@types/uuid@^8.3.0":
1411-
version "8.3.0"
1412-
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f"
1413-
integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==
1414-
14151410
"@types/ws@^7.2.7":
14161411
version "7.2.7"
14171412
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-7.2.7.tgz#362ad1a1d62721bdb725e72c8cccf357078cf5a3"

0 commit comments

Comments
 (0)