@blackglory/geyser-js 0.9.0 → 0.10.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/README.md CHANGED
@@ -25,29 +25,32 @@ class GeyserClient {
25
25
 
26
26
  close(): Promise<void>
27
27
 
28
- getAllRateLimiterIds(timeout?: number): Promise<string[]>
28
+ getAllRateLimiterIds(signal?: AbortSignal): Promise<string[]>
29
29
 
30
- getRateLimiter(rateLimiterId: string, timeout?: number): Promise<IRateLimiterConfig | null>
30
+ getRateLimiter(
31
+ rateLimiterId: string
32
+ , signal?: AbortSignal
33
+ ): Promise<IRateLimiterConfig | null>
31
34
 
32
35
  setRateLimiter(
33
36
  rateLimiterId: string
34
37
  , config: IRateLimiterConfig
35
- , timeout?: number
38
+ , signal?: AbortSignal
36
39
  ): Promise<void>
37
40
 
38
- removeRateLimiter(rateLimiterId: string, timeout?: number): Promise<void>
41
+ removeRateLimiter(rateLimiterId: string, signal?: AbortSignal): Promise<void>
39
42
 
40
43
  /**
41
44
  * 重置速率限制器的状态.
42
45
  *
43
46
  * @throws {RateLimiterNotFound}
44
47
  */
45
- resetRateLimiter(rateLimiterId: string, timeout?: number): Promise<void>
48
+ resetRateLimiter(rateLimiterId: string, signal?: AbortSignal): Promise<void>
46
49
 
47
50
  /**
48
51
  * @throws {RateLimiterNotFound}
49
52
  */
50
- acquireToken(rateLimiterId: string, timeout?: number): Promise<void>
53
+ acquireToken(rateLimiterId: string, signal?: AbortSignal): Promise<void>
51
54
  }
52
55
 
53
56
  /**
@@ -12,11 +12,11 @@ export declare class GeyserClient {
12
12
  static create(options: IGeyserClientOptions): Promise<GeyserClient>;
13
13
  private constructor();
14
14
  close(): Promise<void>;
15
- getAllRateLimiterIds(timeout?: number): Promise<string[]>;
16
- getRateLimiter(rateLimiterId: string, timeout?: number): Promise<IRateLimiterConfig | null>;
17
- setRateLimiter(rateLimiterId: string, config: IRateLimiterConfig, timeout?: number): Promise<void>;
18
- removeRateLimiter(rateLimiterId: string, timeout?: number): Promise<void>;
19
- resetRateLimiter(rateLimiterId: string, timeout?: number): Promise<void>;
20
- acquireToken(rateLimiterId: string, timeout?: number): Promise<void>;
15
+ getAllRateLimiterIds(signal?: AbortSignal): Promise<string[]>;
16
+ getRateLimiter(rateLimiterId: string, signal?: AbortSignal): Promise<IRateLimiterConfig | null>;
17
+ setRateLimiter(rateLimiterId: string, config: IRateLimiterConfig, signal?: AbortSignal): Promise<void>;
18
+ removeRateLimiter(rateLimiterId: string, signal?: AbortSignal): Promise<void>;
19
+ resetRateLimiter(rateLimiterId: string, signal?: AbortSignal): Promise<void>;
20
+ acquireToken(rateLimiterId: string, signal?: AbortSignal): Promise<void>;
21
21
  private withTimeout;
22
22
  }
@@ -1,44 +1,43 @@
1
1
  import { createRPCClient } from "./utils/rpc-client.js";
2
- import { timeoutSignal, withAbortSignal } from 'extra-abort';
2
+ import { raceAbortSignals, timeoutSignal } from 'extra-abort';
3
+ import { isntUndefined } from '@blackglory/prelude';
3
4
  export { RateLimiterNotFound } from './contract.js';
4
5
  export class GeyserClient {
6
+ static async create(options) {
7
+ const { client, close } = await createRPCClient(options.server, options.retryIntervalForReconnection);
8
+ return new GeyserClient(client, close, options.timeout);
9
+ }
5
10
  constructor(client, closeClients, timeout) {
6
11
  this.client = client;
7
12
  this.closeClients = closeClients;
8
13
  this.timeout = timeout;
9
14
  }
10
- static async create(options) {
11
- const { client, close } = await createRPCClient(options.server, options.retryIntervalForReconnection);
12
- return new GeyserClient(client, close, options.timeout);
13
- }
14
15
  async close() {
15
16
  await this.closeClients();
16
17
  }
17
- async getAllRateLimiterIds(timeout) {
18
- return await this.withTimeout(() => this.client.getAllRateLimiterIds(), timeout !== null && timeout !== void 0 ? timeout : this.timeout);
18
+ async getAllRateLimiterIds(signal) {
19
+ return await this.client.getAllRateLimiterIds(this.withTimeout(signal));
19
20
  }
20
- async getRateLimiter(rateLimiterId, timeout) {
21
- return await this.withTimeout(() => this.client.getRateLimiter(rateLimiterId), timeout !== null && timeout !== void 0 ? timeout : this.timeout);
21
+ async getRateLimiter(rateLimiterId, signal) {
22
+ return await this.client.getRateLimiter(rateLimiterId, this.withTimeout(signal));
22
23
  }
23
- async setRateLimiter(rateLimiterId, config, timeout) {
24
- await this.withTimeout(() => this.client.setRateLimiter(rateLimiterId, config), timeout !== null && timeout !== void 0 ? timeout : this.timeout);
24
+ async setRateLimiter(rateLimiterId, config, signal) {
25
+ await this.client.setRateLimiter(rateLimiterId, config, this.withTimeout(signal));
25
26
  }
26
- async removeRateLimiter(rateLimiterId, timeout) {
27
- await this.withTimeout(() => this.client.removeRateLimiter(rateLimiterId), timeout !== null && timeout !== void 0 ? timeout : this.timeout);
27
+ async removeRateLimiter(rateLimiterId, signal) {
28
+ await this.client.removeRateLimiter(rateLimiterId, this.withTimeout(signal));
28
29
  }
29
- async resetRateLimiter(rateLimiterId, timeout) {
30
- await this.withTimeout(() => this.client.removeRateLimiter(rateLimiterId), timeout !== null && timeout !== void 0 ? timeout : this.timeout);
30
+ async resetRateLimiter(rateLimiterId, signal) {
31
+ await this.client.removeRateLimiter(rateLimiterId, this.withTimeout(signal));
31
32
  }
32
- async acquireToken(rateLimiterId, timeout) {
33
- await this.withTimeout(() => this.client.acquireToken(rateLimiterId), timeout !== null && timeout !== void 0 ? timeout : this.timeout);
33
+ async acquireToken(rateLimiterId, signal) {
34
+ await this.client.acquireToken(rateLimiterId, this.withTimeout(signal));
34
35
  }
35
- async withTimeout(fn, timeout = this.timeout) {
36
- if (timeout) {
37
- return await withAbortSignal(timeoutSignal(timeout), fn);
38
- }
39
- else {
40
- return await fn();
41
- }
36
+ withTimeout(signal) {
37
+ return raceAbortSignals([
38
+ isntUndefined(this.timeout) && timeoutSignal(this.timeout),
39
+ signal
40
+ ]);
42
41
  }
43
42
  }
44
43
  //# sourceMappingURL=geyser-client.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"geyser-client.js","sourceRoot":"","sources":["../src/geyser-client.ts"],"names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ACH,CAAC;IAED,KAAK,CAAC,iBAAiB,CACrB,aAAqB,EACrB,MAAoB;QAEpB,MAAM,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;IAC9E,CAAC;IAOD,KAAK,CAAC,gBAAgB,CACpB,aAAqB,EACrB,MAAoB;QAEpB,MAAM,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;IAC9E,CAAC;IAKD,KAAK,CAAC,YAAY,CAAC,aAAqB,EAAE,MAAoB;QAC5D,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;IACzE,CAAC;IAEO,WAAW,CAAC,MAAoB;QACtC,OAAO,gBAAgB,CAAC;YACtB,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC;YAC1D,MAAM;SACP,CAAC,CAAA;IACJ,CAAC;CACF"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@blackglory/geyser-js",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
7
- "lib"
7
+ "lib",
8
+ "src"
8
9
  ],
9
10
  "type": "module",
10
11
  "main": "lib/index.js",
@@ -17,22 +18,20 @@
17
18
  "license": "MIT",
18
19
  "sideEffects": false,
19
20
  "engines": {
20
- "node": ">=16"
21
+ "node": ">=22"
21
22
  },
22
23
  "scripts": {
23
24
  "prepare": "ts-patch install -s",
24
- "lint": "eslint --ext .js,.jsx,.ts,.tsx --quiet src __tests__",
25
- "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --no-cache --runInBand --config jest.config.cjs",
26
- "test:debug": "cross-env NODE_OPTIONS=--experimental-vm-modules node --inspect-brk node_modules/.bin/jest --runInBand jest.config.cjs",
27
- "test:coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage --config jest.config.cjs",
25
+ "lint": "eslint --quiet src __tests__",
26
+ "test": "vitest --run",
28
27
  "prepublishOnly": "run-s prepare clean build",
29
28
  "clean": "rimraf lib",
30
29
  "build": "tsc --project tsconfig.build.json",
31
30
  "docker:test": "run-s docker:test:clean docker:test:build docker:test:run docker:test:clean",
32
31
  "docker:coverage": "run-s docker:test:clean docker:test:build docker:test:coverage docker:test:clean",
33
- "docker:test:build": "docker-compose --project-name estore-js --file docker-compose.test.yml build",
34
- "docker:test:run": "docker-compose --project-name estore-js --file docker-compose.test.yml run --rm test",
35
- "docker:test:clean": "docker-compose --project-name estore-js --file docker-compose.test.yml down",
32
+ "docker:test:build": "docker compose --project-name estore-js --file docker-compose.test.yml build",
33
+ "docker:test:run": "docker compose --project-name estore-js --file docker-compose.test.yml run --no-TTY --rm test",
34
+ "docker:test:clean": "docker compose --project-name estore-js --file docker-compose.test.yml down",
36
35
  "release": "standard-version"
37
36
  },
38
37
  "husky": {
@@ -42,39 +41,37 @@
42
41
  }
43
42
  },
44
43
  "devDependencies": {
45
- "@blackglory/jest-resolver": "^0.3.0",
46
- "@commitlint/cli": "^17.5.1",
47
- "@commitlint/config-conventional": "^17.4.4",
48
- "@types/jest": "^29.5.0",
49
- "@types/ws": "^8.5.4",
50
- "@typescript-eslint/eslint-plugin": "^5.57.0",
51
- "@typescript-eslint/parser": "^5.57.0",
44
+ "@commitlint/cli": "^19.8.1",
45
+ "@commitlint/config-conventional": "^19.8.1",
46
+ "@eslint/js": "^9.27.0",
47
+ "@types/ws": "^8.18.1",
52
48
  "cross-env": "^7.0.3",
53
- "eslint": "^8.36.0",
49
+ "eslint": "^9.27.0",
54
50
  "husky": "4",
55
- "jest": "^29.5.0",
56
- "jest-resolve": "^29.5.0",
57
- "msw": "^1.2.1",
51
+ "msw": "^2.8.6",
58
52
  "npm-run-all": "^4.1.5",
59
- "return-style": "^3.0.0",
60
- "rimraf": "^3.0.2",
53
+ "return-style": "^3.0.1",
54
+ "rimraf": "^6.0.1",
61
55
  "standard-version": "^9.3.2",
62
- "ts-jest": "^29.0.5",
63
- "ts-patch": "^2.1.0",
64
- "tslib": "^2.5.0",
65
- "typescript": "4.8",
66
- "typescript-transform-paths": "^3.4.6"
56
+ "ts-patch": "^3.3.0",
57
+ "tslib": "^2.8.1",
58
+ "typescript": "5.8.3",
59
+ "typescript-eslint": "^8.33.0",
60
+ "typescript-transform-paths": "^3.5.5",
61
+ "vite": "^6.3.5",
62
+ "vite-tsconfig-paths": "^5.1.4",
63
+ "vitest": "^3.1.4"
67
64
  },
68
65
  "dependencies": {
69
- "@blackglory/errors": "^3.0.0",
70
- "@blackglory/prelude": "^0.3.1",
71
- "@delight-rpc/extra-native-websocket": "^0.5.2",
72
- "@delight-rpc/extra-websocket": "^0.6.2",
66
+ "@blackglory/errors": "^3.0.3",
67
+ "@blackglory/prelude": "^0.4.0",
68
+ "@delight-rpc/extra-native-websocket": "^0.6.0",
69
+ "@delight-rpc/extra-websocket": "^0.7.0",
73
70
  "delight-rpc": "^6.1.2",
74
- "extra-abort": "^0.3.6",
75
- "extra-native-websocket": "^0.3.1",
76
- "extra-websocket": "^0.3.1",
77
- "justypes": "^4.2.0",
78
- "ws": "^8.13.0"
71
+ "extra-abort": "^0.4.0",
72
+ "extra-native-websocket": "^0.4.1",
73
+ "extra-websocket": "^0.4.2",
74
+ "justypes": "^4.4.1",
75
+ "ws": "^8.18.2"
79
76
  }
80
77
  }
@@ -0,0 +1,34 @@
1
+ import { JSONObject } from 'justypes'
2
+ import { CustomError } from '@blackglory/errors'
3
+
4
+ export const expectedVersion = '^0.5.0'
5
+
6
+ export interface IRateLimiterConfig extends JSONObject {
7
+ duration: number | null
8
+ limit: number | null
9
+ }
10
+
11
+ export interface IAPI {
12
+ getAllRateLimiterIds(): string[]
13
+
14
+ getRateLimiter(rateLimiterId: string): IRateLimiterConfig | null
15
+ setRateLimiter(rateLimiterId: string, config: IRateLimiterConfig): null
16
+ removeRateLimiter(rateLimiterId: string): null
17
+
18
+ /**
19
+ * 重置速率限制器的状态.
20
+ *
21
+ * @throws {RateLimiterNotFound}
22
+ */
23
+ resetRateLimiter(rateLimiterId: string): null
24
+
25
+ /**
26
+ * @throws {RateLimiterNotFound}
27
+ */
28
+ acquireToken(rateLimiterId: string): null
29
+ }
30
+
31
+ /**
32
+ * 速率限制器在未经配置的情况下, 相当于不存在.
33
+ */
34
+ export class RateLimiterNotFound extends CustomError {}
@@ -0,0 +1,91 @@
1
+ import { createRPCClient } from '@utils/rpc-client.js'
2
+ import { ClientProxy } from 'delight-rpc'
3
+ import { raceAbortSignals, timeoutSignal } from 'extra-abort'
4
+ import { IAPI, IRateLimiterConfig } from './contract.js'
5
+ import { isntUndefined } from '@blackglory/prelude'
6
+ export { IRateLimiterConfig, RateLimiterNotFound } from './contract.js'
7
+
8
+ export interface IGeyserClientOptions {
9
+ server: string
10
+ timeout?: number
11
+ retryIntervalForReconnection?: number
12
+ }
13
+
14
+ export class GeyserClient {
15
+ static async create(options: IGeyserClientOptions): Promise<GeyserClient> {
16
+ const { client, close } = await createRPCClient(
17
+ options.server
18
+ , options.retryIntervalForReconnection
19
+ )
20
+ return new GeyserClient(client, close, options.timeout)
21
+ }
22
+
23
+ private constructor(
24
+ private client: ClientProxy<IAPI>
25
+ , private closeClients: () => Promise<void>
26
+ , private timeout?: number
27
+ ) {}
28
+
29
+ async close(): Promise<void> {
30
+ await this.closeClients()
31
+ }
32
+
33
+ async getAllRateLimiterIds(signal?: AbortSignal): Promise<string[]> {
34
+ return await this.client.getAllRateLimiterIds(this.withTimeout(signal))
35
+ }
36
+
37
+ async getRateLimiter(
38
+ rateLimiterId: string
39
+ , signal?: AbortSignal
40
+ ): Promise<IRateLimiterConfig | null> {
41
+ return await this.client.getRateLimiter(
42
+ rateLimiterId
43
+ , this.withTimeout(signal)
44
+ )
45
+ }
46
+
47
+ async setRateLimiter(
48
+ rateLimiterId: string
49
+ , config: IRateLimiterConfig
50
+ , signal?: AbortSignal
51
+ ): Promise<void> {
52
+ await this.client.setRateLimiter(
53
+ rateLimiterId
54
+ , config
55
+ , this.withTimeout(signal)
56
+ )
57
+ }
58
+
59
+ async removeRateLimiter(
60
+ rateLimiterId: string
61
+ , signal?: AbortSignal
62
+ ): Promise<void> {
63
+ await this.client.removeRateLimiter(rateLimiterId, this.withTimeout(signal))
64
+ }
65
+
66
+ /**
67
+ * 重置速率限制器的状态.
68
+ *
69
+ * @throws {RateLimiterNotFound}
70
+ */
71
+ async resetRateLimiter(
72
+ rateLimiterId: string
73
+ , signal?: AbortSignal
74
+ ): Promise<void> {
75
+ await this.client.removeRateLimiter(rateLimiterId, this.withTimeout(signal))
76
+ }
77
+
78
+ /**
79
+ * @throws {RateLimiterNotFound}
80
+ */
81
+ async acquireToken(rateLimiterId: string, signal?: AbortSignal): Promise<void> {
82
+ await this.client.acquireToken(rateLimiterId, this.withTimeout(signal))
83
+ }
84
+
85
+ private withTimeout(signal?: AbortSignal): AbortSignal {
86
+ return raceAbortSignals([
87
+ isntUndefined(this.timeout) && timeoutSignal(this.timeout)
88
+ , signal
89
+ ])
90
+ }
91
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './geyser-client.js'
@@ -0,0 +1,27 @@
1
+ import { IAPI, expectedVersion } from '@src/contract.js'
2
+ import { ClientProxy } from 'delight-rpc'
3
+ import { createClient } from '@delight-rpc/extra-native-websocket'
4
+ import { ExtraNativeWebSocket, autoReconnect } from 'extra-native-websocket'
5
+
6
+ export async function createRPCClient(
7
+ url: string
8
+ , retryIntervalForReconnection?: number
9
+ ): Promise<{
10
+ client: ClientProxy<IAPI>
11
+ close: () => Promise<void>
12
+ }> {
13
+ const ws = new ExtraNativeWebSocket(() => new WebSocket(url))
14
+ const cancelAutoReconnect = autoReconnect(ws, retryIntervalForReconnection)
15
+ await ws.connect()
16
+
17
+ const [client, closeClient] = createClient<IAPI>(ws, { expectedVersion })
18
+
19
+ return {
20
+ client
21
+ , close: async () => {
22
+ closeClient()
23
+ cancelAutoReconnect()
24
+ await ws.close()
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,28 @@
1
+ import { IAPI, expectedVersion } from '@src/contract.js'
2
+ import { ClientProxy } from 'delight-rpc'
3
+ import { createClient } from '@delight-rpc/extra-websocket'
4
+ import { WebSocket } from 'ws'
5
+ import { ExtraWebSocket, autoReconnect } from 'extra-websocket'
6
+
7
+ export async function createRPCClient(
8
+ url: string
9
+ , retryIntervalForReconnection?: number
10
+ ): Promise<{
11
+ client: ClientProxy<IAPI>
12
+ close: () => Promise<void>
13
+ }> {
14
+ const ws = new ExtraWebSocket(() => new WebSocket(url))
15
+ const cancelAutoReconnect = autoReconnect(ws, retryIntervalForReconnection)
16
+ await ws.connect()
17
+
18
+ const [client, closeClient] = createClient<IAPI>(ws, { expectedVersion })
19
+
20
+ return {
21
+ client
22
+ , close: async () => {
23
+ closeClient()
24
+ cancelAutoReconnect()
25
+ await ws.close()
26
+ }
27
+ }
28
+ }