Skip to content

Commit 2d718bc

Browse files
committed
Move to number subprotocol
1 parent 7d9e526 commit 2d718bc

File tree

30 files changed

+114
-155
lines changed

30 files changed

+114
-155
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let userId = document.querySelector('meta[name=user]').content
5454
let token = document.querySelector('meta[name=token]').content
5555

5656
const client = new CrossTabClient({
57-
subprotocol: '1.0.0',
57+
subprotocol: 1,
5858
server: 'wss://example.com:1337',
5959
userId,
6060
token

attention/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function createClient(): Promise<CrossTabClient> {
2727
let pair = new TestPair()
2828
let client = new CrossTabClient({
2929
server: pair.left,
30-
subprotocol: '1.0.0',
30+
subprotocol: 10,
3131
userId: '10'
3232
})
3333

badge/index.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function createTest(override?: Partial<BadgeOptions>): Promise<TestPair> {
4242
let pair = new TestPair()
4343
let client = new CrossTabClient<object, TestLog<ClientMeta>>({
4444
server: pair.left,
45-
subprotocol: '1.0.0',
45+
subprotocol: 10,
4646
time: new TestTime(),
4747
userId: '1'
4848
})
@@ -153,17 +153,17 @@ it('shows error', async () => {
153153
it('shows server errors', async () => {
154154
let test = await createTest()
155155
let protocol = new LoguxError('wrong-protocol', {
156-
supported: '1.0.0',
157-
used: '0.1.0'
156+
supported: 5,
157+
used: 4
158158
})
159159
emit(test.leftNode, 'error', protocol)
160160
expect(badgeStyle().display).toBe('block')
161161
expect(badgeStyle().backgroundColor).toBe('#000')
162162
expect(getBadgeMessage()).toEqual(badgeEn.protocolError)
163163

164164
let subprotocol = new LoguxError('wrong-subprotocol', {
165-
supported: '1.0.0',
166-
used: '0.1.0'
165+
supported: 11,
166+
used: 10
167167
})
168168
emit(test.leftNode, 'error', subprotocol)
169169
expect(badgeStyle().display).toBe('block')
@@ -228,7 +228,7 @@ it('removes badge from DOM', () => {
228228
let pair = new TestPair()
229229
let client = new CrossTabClient({
230230
server: pair.left,
231-
subprotocol: '1.0.0',
231+
subprotocol: 10,
232232
time: new TestTime(),
233233
userId: '10'
234234
})

client/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Action } from '@logux/core'
44
import { Client } from '../index.js'
55

66
let client = new Client({
7-
subprotocol: '1.0.0',
7+
subprotocol: 10,
88
server: 'ws://localhost',
99
userId: '10'
1010
})
@@ -13,7 +13,7 @@ let client = new Client({
1313
client.log.add({ type: 'A' }, { tab: 1 })
1414

1515
new Client({
16-
subprotocol: '1.0.0',
16+
subprotocol: 10,
1717
server: 'ws://localhost',
1818
// THROWS Type 'boolean' is not assignable to type 'string'.
1919
userId: false

client/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ export interface ClientOptions {
7979
store?: LogStore
8080

8181
/**
82-
* Client subprotocol version in SemVer format.
82+
* Client subprotocol version.
8383
*/
84-
subprotocol: string
84+
subprotocol: number
8585

8686
/**
8787
* Test time to test client.
@@ -119,7 +119,7 @@ export interface ClientOptions {
119119
*
120120
* const client = new Client({
121121
* credentials: token,
122-
* subprotocol: '1.0.0',
122+
* subprotocol: 1,
123123
* server: 'wss://example.com:1337',
124124
* userId: userId
125125
* })

client/index.test.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async function createDialog(
7272

7373
let client = new Client<object, TestLog>({
7474
server: pair.left,
75-
subprotocol: '1.0.0',
75+
subprotocol: 10,
7676
time: new TestTime(),
7777
userId: '10',
7878
...opts
@@ -114,7 +114,7 @@ async function createDialog(
114114
function createClient(): Client<object, TestLog> {
115115
let client = new Client<object, TestLog>({
116116
server: 'wss://localhost:1337',
117-
subprotocol: '1.0.0',
117+
subprotocol: 10,
118118
time: new TestTime(),
119119
userId: '10'
120120
})
@@ -126,16 +126,16 @@ function createClient(): Client<object, TestLog> {
126126
it('saves options', () => {
127127
let client = new Client({
128128
server: 'wss://localhost:1337',
129-
subprotocol: '1.0.0',
129+
subprotocol: 10,
130130
userId: '10'
131131
})
132-
expect(client.options.subprotocol).toBe('1.0.0')
132+
expect(client.options.subprotocol).toBe(10)
133133
})
134134

135135
it('throws on missed server', () => {
136136
expect(() => {
137137
// @ts-expect-error
138-
new Client({ subprotocol: '1.0.0', userId: '10' })
138+
new Client({ subprotocol: 10, userId: '10' })
139139
}).toThrow(/server/)
140140
})
141141

@@ -149,15 +149,15 @@ it('throws on missed subprotocol', () => {
149149
it('throws on missed user ID', () => {
150150
expect(() => {
151151
// @ts-expect-error
152-
new Client({ server: 'wss://localhost:1337', subprotocol: '1.0.0' })
152+
new Client({ server: 'wss://localhost:1337', subprotocol: 10 })
153153
}).toThrow(/userId/)
154154
})
155155

156156
it('throws on colon in user ID', () => {
157157
expect(() => {
158158
new Client({
159159
server: 'wss://localhost:1337',
160-
subprotocol: '1.0.0',
160+
subprotocol: 10,
161161
userId: 'admin:1'
162162
})
163163
}).toThrow(/colon/)
@@ -167,7 +167,7 @@ it('throws on false in user ID', () => {
167167
expect(() => {
168168
new Client({
169169
server: 'wss://localhost:1337',
170-
subprotocol: '1.0.0',
170+
subprotocol: 10,
171171
// @ts-expect-error
172172
userId: false
173173
})
@@ -178,7 +178,7 @@ it('throws on non-string in user ID', () => {
178178
expect(() => {
179179
new Client({
180180
server: 'wss://localhost:1337',
181-
subprotocol: '1.0.0',
181+
subprotocol: 10,
182182
// @ts-expect-error
183183
userId: 10
184184
})
@@ -230,7 +230,7 @@ it('ignores WS in development', async () => {
230230
it('uses user ID in node ID', () => {
231231
let client1 = new Client({
232232
server: 'wss://localhost:1337',
233-
subprotocol: '1.0.0',
233+
subprotocol: 10,
234234
userId: '10'
235235
})
236236
expect(client1.clientId).toMatch(/^10:[\w-]{8}$/)
@@ -239,7 +239,7 @@ it('uses user ID in node ID', () => {
239239

240240
let client2 = new Client({
241241
server: 'wss://localhost:1337',
242-
subprotocol: '1.0.0',
242+
subprotocol: 10,
243243
userId: '10'
244244
})
245245
expect(client2.nodeId).toEqual(client2.clientId + ':' + client2.tabId)
@@ -248,7 +248,7 @@ it('uses user ID in node ID', () => {
248248
it('uses node ID in ID generator', () => {
249249
let client = new Client({
250250
server: 'wss://localhost:1337',
251-
subprotocol: '1.0.0',
251+
subprotocol: 10,
252252
time: new TestTime(),
253253
userId: '10'
254254
})
@@ -261,7 +261,7 @@ it('uses custom store', () => {
261261
let client = new Client({
262262
server: 'wss://localhost:1337',
263263
store,
264-
subprotocol: '1.0.0',
264+
subprotocol: 10,
265265
userId: '10'
266266
})
267267
expect(client.log.store).toBe(store)
@@ -273,7 +273,7 @@ it('sends options to connection', () => {
273273
maxDelay: 500,
274274
minDelay: 100,
275275
server: 'wss://localhost:1337',
276-
subprotocol: '1.0.0',
276+
subprotocol: 10,
277277
userId: '10'
278278
})
279279
expect(privateMethods(client.node.connection).options).toEqual({
@@ -290,12 +290,12 @@ it('sends options to node', () => {
290290
let client = new Client({
291291
ping: 1000,
292292
server: 'wss://localhost:1337',
293-
subprotocol: '1.0.0',
293+
subprotocol: 10,
294294
timeout: 2000,
295295
token: 'token',
296296
userId: '10'
297297
})
298-
expect(client.node.options.subprotocol).toBe('1.0.0')
298+
expect(client.node.options.subprotocol).toBe(10)
299299
expect(client.node.options.token).toBe('token')
300300
expect(client.node.options.timeout).toBe(2000)
301301
expect(client.node.options.ping).toBe(1000)
@@ -407,7 +407,7 @@ it('cleans other tab action after timeout', async () => {
407407
it('adds current subprotocol to meta', async () => {
408408
let client = createClient()
409409
await client.log.add({ type: 'A' }, { reasons: ['test'] })
410-
expect(client.log.entries()[0][1].subprotocol).toBe('1.0.0')
410+
expect(client.log.entries()[0][1].subprotocol).toBe(10)
411411
})
412412

413413
it('adds current subprotocol only to own actions', async () => {
@@ -421,11 +421,8 @@ it('adds current subprotocol only to own actions', async () => {
421421

422422
it('allows to override subprotocol in meta', async () => {
423423
let client = createClient()
424-
await client.log.add(
425-
{ type: 'A' },
426-
{ reasons: ['test'], subprotocol: '0.1.0' }
427-
)
428-
expect(client.log.entries()[0][1].subprotocol).toBe('0.1.0')
424+
await client.log.add({ type: 'A' }, { reasons: ['test'], subprotocol: 9 })
425+
expect(client.log.entries()[0][1].subprotocol).toBe(9)
429426
})
430427

431428
it('sends only special actions', async () => {
@@ -490,7 +487,7 @@ it('compresses subprotocol', async () => {
490487
{
491488
id: '1 10:client:id 0',
492489
reasons: ['test'],
493-
subprotocol: '1.0.0',
490+
subprotocol: 10,
494491
sync: true
495492
}
496493
),
@@ -499,7 +496,7 @@ it('compresses subprotocol', async () => {
499496
{
500497
id: '2 10:client:id 0',
501498
reasons: ['test'],
502-
subprotocol: '2.0.0',
499+
subprotocol: 11,
503500
sync: true
504501
}
505502
)
@@ -516,7 +513,7 @@ it('compresses subprotocol', async () => {
516513
{ type: 'a' },
517514
{
518515
id: [2, '10:client:id', 0],
519-
subprotocol: '2.0.0',
516+
subprotocol: 11,
520517
time: 2
521518
}
522519
]
@@ -885,13 +882,7 @@ it('works with unsubscribe in offline', async () => {
885882
await delay(10)
886883

887884
expect(pair.leftSent).toEqual([
888-
[
889-
'connect',
890-
client.node.localProtocol,
891-
'10:1:1',
892-
0,
893-
{ subprotocol: '1.0.0' }
894-
],
885+
['connect', client.node.localProtocol, '10:1:1', 0, { subprotocol: 10 }],
895886
[
896887
'sync',
897888
3,

client/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Client } from '../index.js'
55

66
let client = new Client({
77
server: 'ws://localhost',
8-
subprotocol: '1.0.0',
8+
subprotocol: 10,
99
userId: '10'
1010
})
1111

confirm/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function createClient(): Promise<CrossTabClient> {
1717

1818
let client = new CrossTabClient({
1919
server: pair.left,
20-
subprotocol: '1.0.0',
20+
subprotocol: 10,
2121
userId: '10'
2222
})
2323

cross-tab-client/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CrossTabClient } from '../index.js'
22

33
let client = new CrossTabClient({
4-
subprotocol: '1.0.0',
4+
subprotocol: 10,
55
server: 'ws://localhost',
66
userId: '10'
77
})

cross-tab-client/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { ClientActionListener, ClientMeta } from '../client/index.js'
1717
* const token = document.querySelector('meta[name=token]').content
1818
*
1919
* const client = new CrossTabClient({
20-
* subprotocol: '1.0.0',
20+
* subprotocol: 1,
2121
* server: 'wss://example.com:1337',
2222
* userId,
2323
* token

0 commit comments

Comments
 (0)