screeps-connectivity 0.14.0 → 0.15.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.15.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 66f88f8: Code panel branch management: create a new branch (clones the selected branch, with an inline name input) and set the selected branch to run on the server. The active-branch indicator now stays live via a new `set-active-branch` WebSocket subscription — `UserStore.subscribe('set-active-branch')` emits a `user:setActiveBranch` event whenever the active branch changes, including from another client or session.
8
+
9
+ The code panel can now add and delete modules: an add button in the module list opens an inline name input, and hovering a module reveals a delete button (the `main` entry module is protected). Both changes are staged locally and persisted on the next Save.
10
+
11
+ Also fixes a stale-response race in the code panel where switching branches while a previous branch's code fetch was still in flight could leave the editor showing the wrong branch's files.
12
+
13
+ - 66f88f8: WebSocket compression support (opt-in): setting the new `ScreepsClientOptions.gzip` to `true` sends `gzip on` after auth, so the server deflates event frames (room updates, map stats, memory) and only transmits the compressed `gz:` form when it's actually smaller. Defaults to `false` to match the official client, which never enables it. The `gz:` decode path is always active regardless, so this is a pure opt-in bandwidth trade with no downside on small control frames.
14
+
3
15
  ## 0.14.0
4
16
 
5
17
  ### Minor Changes
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2024-2026 Bastian Hoyer
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screeps-connectivity",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "repository": {
@@ -24,22 +24,22 @@
24
24
  "require": "./dist/file-storage.cjs"
25
25
  }
26
26
  },
27
+ "devDependencies": {
28
+ "@eslint/js": "^10.0.1",
29
+ "@types/node": "^26.1.0",
30
+ "@typescript-eslint/eslint-plugin": "^8.63.0",
31
+ "@typescript-eslint/parser": "^8.63.0",
32
+ "eslint": "^10.6.0",
33
+ "fake-indexeddb": "^6.0.0",
34
+ "globals": "^17.7.0",
35
+ "tsup": "^8.0.0",
36
+ "typescript": "^6.0.3",
37
+ "vitest": "^4.1.10"
38
+ },
27
39
  "scripts": {
28
40
  "build": "tsup",
29
41
  "test": "vitest run",
30
42
  "test:watch": "vitest",
31
43
  "lint": "eslint src tests"
32
- },
33
- "devDependencies": {
34
- "@eslint/js": "^10.0.1",
35
- "@types/node": "^25.7.0",
36
- "@typescript-eslint/eslint-plugin": "^8.0.0",
37
- "@typescript-eslint/parser": "^8.0.0",
38
- "eslint": "^9.0.0",
39
- "fake-indexeddb": "^6.0.0",
40
- "globals": "^17.6.0",
41
- "tsup": "^8.0.0",
42
- "typescript": "^5.5.0",
43
- "vitest": "^4.1.6"
44
44
  }
45
- }
45
+ }
@@ -41,6 +41,14 @@ export interface ScreepsClientOptions {
41
41
  tokenRefresh?: TokenRefreshOptions | false
42
42
  /** Override the /api/game/room-decorations response with static data (useful for dev/testing when the server doesn't support the endpoint). */
43
43
  decorationsMock?: ApiRoomDecorationsResponse
44
+ /**
45
+ * Enable WebSocket message compression by sending `gzip on` after auth. The
46
+ * server then deflates event frames (room updates, etc.) and only sends the
47
+ * compressed `gz:` form when it's smaller. Defaults to `false` — matching the
48
+ * official client, which never enables it. The `gz:` decode path is always
49
+ * active regardless, so enabling this is a pure opt-in bandwidth trade.
50
+ */
51
+ gzip?: boolean
44
52
  }
45
53
 
46
54
  export class ScreepsClient {
@@ -77,7 +85,7 @@ export class ScreepsClient {
77
85
  this.logger.log(`[screeps:client] init ${opts.url}`)
78
86
  this.cache = new Cache(namespace, opts.storage ?? null)
79
87
  this.http = new HttpClient({ url: opts.url, auth: opts.auth, logger: this.logger.child('http'), serverPassword: opts.serverPassword, decorationsMock: opts.decorationsMock })
80
- this.socket = new SocketClient({ url: opts.url, WebSocket: opts.WebSocket, logger: this.logger.child('socket') })
88
+ this.socket = new SocketClient({ url: opts.url, WebSocket: opts.WebSocket, logger: this.logger.child('socket'), gzip: opts.gzip })
81
89
  const map2Storage = new Map2Storage({
82
90
  adapter: opts.storage ?? null,
83
91
  namespace,
@@ -8,6 +8,7 @@ export class SocketClient {
8
8
  private readonly wsUrl: string
9
9
  private readonly WS: WsConstructor
10
10
  private readonly logger: Logger
11
+ private readonly gzip: boolean
11
12
  private ws: WebSocket | null = null
12
13
  private token: string | null = null
13
14
  private authed = false
@@ -22,11 +23,14 @@ export class SocketClient {
22
23
  private readonly MAX_DELAY_MS = 60_000
23
24
  private _intentionalClose = false
24
25
 
25
- constructor(opts: { url: string; WebSocket?: WsConstructor; logger?: Logger }) {
26
+ constructor(opts: { url: string; WebSocket?: WsConstructor; logger?: Logger; gzip?: boolean }) {
26
27
  const base = opts.url.replace(/^http/, 'ws').replace(/\/$/, '')
27
28
  this.wsUrl = `${base}/socket/websocket`
28
29
  this.WS = opts.WebSocket ?? globalThis.WebSocket
29
30
  this.logger = opts.logger ?? Logger.create()
31
+ // Off by default — matches the official client, which never sends `gzip on`.
32
+ // The decode path is always available; opt in explicitly to enable it.
33
+ this.gzip = opts.gzip ?? false
30
34
  }
31
35
 
32
36
  get isConnected(): boolean {
@@ -64,6 +68,11 @@ export class SocketClient {
64
68
  this.token = cmd.token
65
69
  this.emit('socket:tokenRefresh', { token: cmd.token })
66
70
  }
71
+ // Ask the server to deflate outbound event frames before any
72
+ // subscribe is flushed, so the first updates already arrive as gz:.
73
+ // The server only sends the compressed form when it's actually
74
+ // smaller, so this never enlarges small control frames.
75
+ if (this.gzip) this.rawSend('gzip on')
67
76
  while (this.queue.length) this.rawSend(this.queue.shift()!)
68
77
  this.emit('connected', {})
69
78
  resolve()
@@ -82,7 +82,7 @@ export class UserStore extends TypedStore<UserStoreEvents> {
82
82
  return this.worldStatus()
83
83
  }
84
84
 
85
- subscribe(channel: 'console' | 'cpu' | 'code'): Subscription {
85
+ subscribe(channel: 'console' | 'cpu' | 'code' | 'set-active-branch'): Subscription {
86
86
  this.logger.log('subscribe', channel)
87
87
  let socketSub: Subscription | null = null
88
88
  let listenerSub: Subscription | null = null
@@ -115,6 +115,8 @@ export class UserStore extends TypedStore<UserStoreEvents> {
115
115
  this.emit('user:console', { messages: msg })
116
116
  } else if (channel === 'code') {
117
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 })
118
120
  }
119
121
  })
120
122
  } catch (err) {
@@ -40,6 +40,7 @@ export interface UserStoreEvents {
40
40
  'user:cpu': CpuStats
41
41
  'user:console': { messages: ConsoleMessage }
42
42
  'user:code': { branch: string; modules: Record<string, string> }
43
+ 'user:setActiveBranch': { activeName: 'activeWorld' | 'activeSim'; branch: string }
43
44
  'user:stream': Record<string, unknown>
44
45
  'user:memory': { path: string; shard: string | null; value: unknown }
45
46
  'user:mapVisual': { shard: string | null; data: string }
@@ -58,6 +58,31 @@ describe('SocketClient', () => {
58
58
  await expect(connectClient(client)).resolves.toBeDefined()
59
59
  })
60
60
 
61
+ it('does not send gzip on by default', async () => {
62
+ const client = makeClient()
63
+ const ws = await connectClient(client)
64
+ expect(ws.sent).not.toContain('gzip on')
65
+ })
66
+
67
+ it('sends gzip on when explicitly enabled, before any subscribe', async () => {
68
+ const client = new SocketClient({ url: 'https://screeps.com', gzip: true, WebSocket: MockWS as unknown as typeof WebSocket })
69
+ const connectPromise = client.connect('tok')
70
+ const ws = MockWS.instances[0]
71
+ ws.simulateOpen()
72
+ ws.simulateMessage('auth ok newtoken')
73
+ await connectPromise
74
+ expect(ws.sent).toContain('gzip on')
75
+ // gzip on must precede subscribes so the first updates already arrive as gz:
76
+ client.subscribe('room:shard0/W7N7')
77
+ expect(ws.sent.indexOf('gzip on')).toBeLessThan(ws.sent.indexOf('subscribe room:shard0/W7N7'))
78
+ })
79
+
80
+ it('explicit gzip:false does not send gzip on', async () => {
81
+ const client = new SocketClient({ url: 'https://screeps.com', gzip: false, WebSocket: MockWS as unknown as typeof WebSocket })
82
+ const ws = await connectClient(client)
83
+ expect(ws.sent).not.toContain('gzip on')
84
+ })
85
+
61
86
  it('subscribe sends subscribe message when authed', async () => {
62
87
  const client = makeClient()
63
88
  const ws = await connectClient(client)
package/tsconfig.json CHANGED
@@ -11,7 +11,8 @@
11
11
  "sourceMap": true,
12
12
  "outDir": "./dist",
13
13
  "rootDir": "./src",
14
- "verbatimModuleSyntax": true
14
+ "verbatimModuleSyntax": true,
15
+ "ignoreDeprecations": "6.0"
15
16
  },
16
17
  "include": ["src"]
17
18
  }