screeps-connectivity 0.15.1 → 0.16.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.16.0
4
+
5
+ ### Minor Changes
6
+
7
+ - f218429: Embedded clients are now configured from the first frame with no `/api/version` round-trip: both the xxscreeps mod and the classic server mod prefetch the version payload and inline it into the page (`window.__SCREEPS_BOOTSTRAP__`), and the client seeds it into both the pre-login UI and the connection. `ScreepsClient` gains an `initialVersion` option and `ServerStore` a `seedVersion()` method to support this.
8
+
9
+ ## 0.15.2
10
+
11
+ ### Patch Changes
12
+
13
+ - b39a0c1: Fix duplicated console output when multiple parts of the app subscribe to the same `UserStore` channel. `subscribe()` now installs a single shared, ref-counted socket subscription and listener per channel instead of one listener per caller, so each incoming frame is processed and re-emitted exactly once. The server subscription and listener are torn down only after the last subscriber disposes.
14
+
3
15
  ## 0.15.1
4
16
 
5
17
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screeps-connectivity",
3
- "version": "0.15.1",
3
+ "version": "0.16.0",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "repository": {
@@ -14,6 +14,7 @@ import type { AuthStrategy } from './http/auth/AuthStrategy.js'
14
14
  import type { StorageAdapter } from './storage/StorageAdapter.js'
15
15
  import type { Subscription } from './subscription/index.js'
16
16
  import type { ApiRoomDecorationsResponse } from './types/api.js'
17
+ import type { ServerVersion } from './types/game.js'
17
18
 
18
19
  type WsConstructor = typeof globalThis.WebSocket
19
20
 
@@ -49,6 +50,12 @@ export interface ScreepsClientOptions {
49
50
  * active regardless, so enabling this is a pure opt-in bandwidth trade.
50
51
  */
51
52
  gzip?: boolean
53
+ /**
54
+ * A pre-known `/api/version` response, seeded into the server store so `connect()`
55
+ * skips the initial version fetch. Used by embedded clients whose host mod inlines
56
+ * the payload into the page. Falls back to a normal fetch if omitted.
57
+ */
58
+ initialVersion?: ServerVersion
52
59
  }
53
60
 
54
61
  export class ScreepsClient {
@@ -100,6 +107,8 @@ export class ScreepsClient {
100
107
  navigation: new NavigationStore(50, this.logger.child('navigation')),
101
108
  }
102
109
 
110
+ if (opts.initialVersion) this.stores.server.seedVersion(opts.initialVersion)
111
+
103
112
  this.tokenRefreshIntervalMs = opts.tokenRefresh === false
104
113
  ? null
105
114
  : (opts.tokenRefresh?.intervalMs ?? 30_000)
@@ -43,9 +43,27 @@ export class ServerStore extends TypedStore<ServerStoreEvents> {
43
43
  this.socketSubs.length = 0
44
44
  }
45
45
 
46
+ /**
47
+ * Seed the server version without a network request. Used when the version is
48
+ * already known at construction time — e.g. an embedded client whose host mod
49
+ * inlines the `/api/version` payload into the page, sparing the initial fetch.
50
+ * Does not emit `server:version` (listeners aren't attached yet at that point);
51
+ * the value surfaces via the cache-hit path on the first `version()` call.
52
+ */
53
+ seedVersion(version: ServerVersion): void {
54
+ this._version = version
55
+ this.cache.set('server/version', version, 5 * 60_000)
56
+ }
57
+
46
58
  async version(): Promise<ServerVersion> {
47
59
  const cached = this.cache.get<ServerVersion>('server/version')
48
- if (cached) return cached
60
+ if (cached) {
61
+ // Emit on cache hits too so a seeded version (see `seedVersion`) reaches
62
+ // listeners that only attach after construction. Idempotent for consumers.
63
+ this._version = cached
64
+ this.emit('server:version', cached)
65
+ return cached
66
+ }
49
67
  const res = await this.http.request<ServerVersion>('GET', '/api/version')
50
68
  this._version = res
51
69
  this.cache.set('server/version', res, 5 * 60_000)
@@ -22,6 +22,15 @@ export class UserStore extends TypedStore<UserStoreEvents> {
22
22
  private _worldStatus: WorldStatus | null = null
23
23
  get worldStatusValue(): WorldStatus | null { return this._worldStatus }
24
24
  private _mePromise: Promise<UserInfo> | null = null
25
+ // Ref-counted store-level fan-out, keyed by channel. SocketClient.subscribe() already ref-counts the
26
+ // server subscription, but SocketClient.on() installs one listener per call — so a shared listener
27
+ // must be installed once and reused, otherwise N callers process (and re-emit) every frame N times.
28
+ private readonly channelSubs = new Map<string, {
29
+ refCount: number
30
+ disposed: boolean
31
+ socketSub: Subscription | null
32
+ listenerSub: Subscription | null
33
+ }>()
25
34
 
26
35
  constructor(http: HttpClient, socket: SocketClient, cache: Cache, logger?: Logger, maxConsoleSize = 100) {
27
36
  super(logger)
@@ -84,56 +93,70 @@ export class UserStore extends TypedStore<UserStoreEvents> {
84
93
 
85
94
  subscribe(channel: 'console' | 'cpu' | 'code' | 'set-active-branch'): Subscription {
86
95
  this.logger.log('subscribe', channel)
87
- let socketSub: Subscription | null = null
88
- let listenerSub: Subscription | null = null
89
- let disposed = false
90
96
 
91
- const setup = async () => {
92
- try {
93
- const uid = this._userId ?? (await this.me())._id
94
- if (disposed) return
95
- const fullChannel = `user:${uid}/${channel}`
96
- socketSub = this.socket.subscribe(fullChannel)
97
- listenerSub = this.socket.on(fullChannel, (data) => {
98
- if (channel === 'cpu') {
99
- this._cpu = data as CpuStats
100
- this.emit('user:cpu', this._cpu)
101
- } else if (channel === 'console') {
102
- const raw = data as { messages?: ConsoleMessage, error?: string }
103
- const msg: ConsoleMessage = {
104
- log: raw.messages?.log ?? [],
105
- results: raw.messages?.results ?? [],
106
- error: raw.messages?.error ?? [],
107
- }
108
- if (raw.error) {
109
- msg.error.push(raw.error)
110
- }
111
- this.console.push(msg)
112
- if (this.console.length > this.maxConsoleSize) {
113
- this.console.splice(0, this.console.length - this.maxConsoleSize)
97
+ // First caller for this channel installs the shared socket subscription + listener; later callers
98
+ // only bump the ref count so the frame is processed and re-emitted exactly once.
99
+ let entry = this.channelSubs.get(channel)
100
+ if (entry) {
101
+ entry.refCount++
102
+ } else {
103
+ entry = { refCount: 1, disposed: false, socketSub: null, listenerSub: null }
104
+ this.channelSubs.set(channel, entry)
105
+ const created = entry
106
+ const setup = async () => {
107
+ try {
108
+ const uid = this._userId ?? (await this.me())._id
109
+ if (created.disposed) return
110
+ const fullChannel = `user:${uid}/${channel}`
111
+ created.socketSub = this.socket.subscribe(fullChannel)
112
+ created.listenerSub = this.socket.on(fullChannel, (data) => {
113
+ if (channel === 'cpu') {
114
+ this._cpu = data as CpuStats
115
+ this.emit('user:cpu', this._cpu)
116
+ } else if (channel === 'console') {
117
+ const raw = data as { messages?: ConsoleMessage, error?: string }
118
+ const msg: ConsoleMessage = {
119
+ log: raw.messages?.log ?? [],
120
+ results: raw.messages?.results ?? [],
121
+ error: raw.messages?.error ?? [],
122
+ }
123
+ if (raw.error) {
124
+ msg.error.push(raw.error)
125
+ }
126
+ this.console.push(msg)
127
+ if (this.console.length > this.maxConsoleSize) {
128
+ this.console.splice(0, this.console.length - this.maxConsoleSize)
129
+ }
130
+ this.emit('user:console', { messages: msg })
131
+ } else if (channel === 'code') {
132
+ this.emit('user:code', data as { branch: string; modules: Record<string, string> })
133
+ } else if (channel === 'set-active-branch') {
134
+ this.emit('user:setActiveBranch', data as { activeName: 'activeWorld' | 'activeSim'; branch: string })
114
135
  }
115
- this.emit('user:console', { messages: msg })
116
- } else if (channel === 'code') {
117
- this.emit('user:code', data as { branch: string; modules: Record<string, string> })
118
- } else if (channel === 'set-active-branch') {
119
- this.emit('user:setActiveBranch', data as { activeName: 'activeWorld' | 'activeSim'; branch: string })
136
+ })
137
+ } catch (err) {
138
+ if (!created.disposed) {
139
+ this.dispatchEvent(new ErrorEvent('error', { error: err instanceof Error ? err : new Error(String(err)) }))
120
140
  }
121
- })
122
- } catch (err) {
123
- if (!disposed) {
124
- this.dispatchEvent(new ErrorEvent('error', { error: err instanceof Error ? err : new Error(String(err)) }))
125
141
  }
126
142
  }
143
+ void setup()
127
144
  }
128
145
 
129
- void setup()
130
-
146
+ let disposed = false
131
147
  return {
132
148
  dispose: () => {
133
- this.logger.log('unsubscribe', channel)
149
+ if (disposed) return
134
150
  disposed = true
135
- socketSub?.dispose()
136
- listenerSub?.dispose()
151
+ this.logger.log('unsubscribe', channel)
152
+ const current = this.channelSubs.get(channel)
153
+ if (!current) return
154
+ if (--current.refCount <= 0) {
155
+ current.disposed = true
156
+ current.socketSub?.dispose()
157
+ current.listenerSub?.dispose()
158
+ this.channelSubs.delete(channel)
159
+ }
137
160
  },
138
161
  }
139
162
  }
@@ -31,6 +31,18 @@ describe('ServerStore', () => {
31
31
  expect(http.request).toHaveBeenCalledOnce()
32
32
  })
33
33
 
34
+ it('serves a seeded version without fetching and emits server:version', async () => {
35
+ const { store, http } = makeStore()
36
+ const spy = vi.fn()
37
+ store.on('server:version', spy)
38
+ store.seedVersion({ ...mockVersion })
39
+ const v = await store.version()
40
+ expect(v.protocol).toBe(13)
41
+ expect(http.request).not.toHaveBeenCalled()
42
+ expect(spy).toHaveBeenCalledWith(expect.objectContaining({ protocol: 13 }))
43
+ expect(store.versionInfo?.protocol).toBe(13)
44
+ })
45
+
34
46
  it('emits server:connected when socket fires connected event', () => {
35
47
  const { socket } = makeStore()
36
48
  let connectedCb: (data: unknown) => void = () => {}
@@ -133,4 +133,49 @@ describe('UserStore', () => {
133
133
  sub.dispose()
134
134
  expect(mockDispose).toHaveBeenCalled()
135
135
  })
136
+
137
+ it('multiple console subscribers share one socket listener and do not double output', async () => {
138
+ const { store, socket } = makeStore()
139
+ await store.me()
140
+ const handlers: Array<(data: unknown) => void> = []
141
+ ;(socket.on as ReturnType<typeof vi.fn>).mockImplementation((_ch: string, cb: (data: unknown) => void) => {
142
+ handlers.push(cb)
143
+ return { dispose: vi.fn() }
144
+ })
145
+ const eventSpy = vi.fn()
146
+ store.on('user:console', eventSpy)
147
+
148
+ // Two independent parts of the app subscribe to the same channel (e.g. the console panel and the
149
+ // custom-UI store). Regression: this used to install two socket listeners, doubling every frame.
150
+ store.subscribe('console')
151
+ store.subscribe('console')
152
+ await new Promise(r => setTimeout(r, 0))
153
+
154
+ // Exactly one socket listener and one server subscription regardless of caller count.
155
+ expect(handlers).toHaveLength(1)
156
+ expect(socket.subscribe).toHaveBeenCalledTimes(1)
157
+
158
+ // One incoming frame produces exactly one console entry and one event.
159
+ handlers[0]!({ messages: { log: ['line1'], results: [] } })
160
+ expect(store.console).toHaveLength(1)
161
+ expect(eventSpy).toHaveBeenCalledTimes(1)
162
+ })
163
+
164
+ it('console socket subscription is ref-counted and dropped only after the last subscriber', async () => {
165
+ const { store, socket } = makeStore()
166
+ await store.me()
167
+ const mockDispose = vi.fn()
168
+ ;(socket.subscribe as ReturnType<typeof vi.fn>).mockReturnValue({ dispose: mockDispose })
169
+
170
+ const a = store.subscribe('console')
171
+ const b = store.subscribe('console')
172
+ await new Promise(r => setTimeout(r, 0))
173
+ expect(socket.subscribe).toHaveBeenCalledTimes(1)
174
+
175
+ a.dispose()
176
+ expect(mockDispose).not.toHaveBeenCalled() // second subscriber still active
177
+
178
+ b.dispose()
179
+ expect(mockDispose).toHaveBeenCalledTimes(1)
180
+ })
136
181
  })