univo 0.2.11 → 0.2.13

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.
@@ -1,15 +1,15 @@
1
1
  import { Rpc } from './rpc.js';
2
- import './index.js';
2
+ import './shared/univo.Cj4s4jNq.js';
3
3
  import 'unstorage';
4
4
 
5
- type Protocol = "http" | "wss";
5
+ type Protocol = "http" | "wss" | "local";
6
6
  type Transport<R extends Rpc, P extends Protocol = Protocol> = {
7
7
  /**
8
8
  * The underlying transport protocol.
9
9
  */
10
10
  protocol: P;
11
11
  /**
12
- * Performs a JSON RPC request
12
+ * Performs an RPC request
13
13
  * @returns The RPC response
14
14
  */
15
15
  request: <M extends keyof R["request"]>(opts: {
@@ -22,6 +22,7 @@ type Transport<R extends Rpc, P extends Protocol = Protocol> = {
22
22
  */
23
23
  subscribe: <M extends keyof R["subscribe"]>(param: M, handler: (message: R["subscribe"][M]) => void) => Promise<() => Promise<void>>;
24
24
  };
25
+ declare function local<R extends Rpc>(rpc: R): Transport<R, "local">;
25
26
  declare function wss<R extends Rpc>(url: string, opts?: {
26
27
  quiet?: boolean;
27
28
  }): Transport<R, "wss">;
@@ -32,5 +33,5 @@ declare function http<R extends Rpc>(url: string, opts?: {
32
33
  signingKey?: string;
33
34
  }): Transport<R, "http">;
34
35
 
35
- export { http, wss };
36
+ export { http, local, wss };
36
37
  export type { Transport };
@@ -1,188 +1,4 @@
1
- import ws from 'ws';
2
- import { WebSocket } from 'partysocket';
3
- import { c as createException } from './shared/univo.C0flyVee.mjs';
4
- import { g as compress, j as raise, c as createLogger } from './shared/univo.Cz5ut_5D.mjs';
5
-
6
- function createSocket(url, opts) {
7
- const socket = new WebSocket(url, [], { WebSocket: ws });
8
- socket.binaryType = "arraybuffer";
9
- if (opts.onOpen) socket.onopen = opts.onOpen;
10
- if (opts.onError) socket.onerror = opts.onError;
11
- if (opts.onClose) socket.onclose = opts.onClose;
12
- if (opts.onMessage) socket.onmessage = opts.onMessage;
13
- return socket;
14
- }
15
- function wss(url, opts = {}) {
16
- if (!url.startsWith("wss://")) {
17
- throw new Error("Websocket connections must start with `wss://`");
18
- }
19
- const logger = createLogger({ quiet: opts.quiet ?? false });
20
- let id = 0;
21
- const requests = /* @__PURE__ */ new Map();
22
- const subscriptions = /* @__PURE__ */ new Map();
23
- const params = /* @__PURE__ */ new Map();
24
- const socket = createSocket(url, {
25
- async onError(cause) {
26
- logger.error(new Error("Socket error", { cause }));
27
- },
28
- async onOpen() {
29
- if (subscriptions.size === 0) {
30
- return;
31
- }
32
- const params2 = [];
33
- for (const [id2, subscription] of subscriptions) {
34
- params2.push(subscription.param);
35
- request({ method: "eth_unsubscribe", params: [id2] }).catch(() => {
36
- logger.warn("Failed to cancel old susbcription");
37
- });
38
- subscriptions.delete(id2);
39
- }
40
- for (const param of params2) {
41
- request({ method: "eth_subscribe", params: [param] }).then((id2) => {
42
- subscriptions.set(id2, { param, latest: Date.now() });
43
- });
44
- }
45
- },
46
- async onMessage(event) {
47
- const data = JSON.parse(event.data);
48
- if (data.method === "eth_subscription") {
49
- const subscription = subscriptions.get(data.params.subscription);
50
- if (subscription === void 0) {
51
- return await request({ method: "eth_unsubscribe", params: [data.params.subscription] }).catch(() => {
52
- logger.warn("Failed to unsubscribe stale subscription");
53
- });
54
- }
55
- const handlers = params.get(subscription.param);
56
- if (handlers === void 0) {
57
- return await request({ method: "eth_unsubscribe", params: [data.params.subscription] }).catch(() => {
58
- logger.warn("Failed to unsubscribe stale subscription");
59
- });
60
- }
61
- for (const handler2 of handlers) {
62
- try {
63
- handler2(data.params.result);
64
- } catch (cause) {
65
- logger.error(new Error("Handler error", { cause }));
66
- }
67
- }
68
- subscription.latest = Date.now();
69
- return;
70
- }
71
- const handler = requests.get(data.id);
72
- if (handler === void 0) {
73
- return logger.warn(`Received unknown response for request id ${data.id}...`);
74
- }
75
- return handler(data.result);
76
- }
77
- });
78
- const request = async (opts2) => {
79
- return await new Promise((resolve, reject) => {
80
- const body = Object.assign({ id: id++ }, opts2);
81
- const timeout = setTimeout(() => {
82
- requests.delete(body.id);
83
- reject(new Error("wss request timed out after 30 seconds"));
84
- }, 30 * 1e3);
85
- requests.set(body.id, (data) => {
86
- clearTimeout(timeout);
87
- requests.delete(body.id);
88
- resolve(data);
89
- });
90
- try {
91
- socket.send(JSON.stringify(body));
92
- } catch (cause) {
93
- clearTimeout(timeout);
94
- requests.delete(body.id);
95
- reject(new Error(ClientConnectionError, { cause }));
96
- }
97
- });
98
- };
99
- const subscribe = async (param, handler) => {
100
- const handlers = params.get(param);
101
- if (handlers === void 0) {
102
- const handlers2 = (/* @__PURE__ */ new Set()).add(handler);
103
- params.set(param, handlers2);
104
- const id2 = await request({ method: "eth_subscribe", params: [param] }).catch((cause) => {
105
- throw new Error(`Failed to initialise subscription for param ${param}`, { cause });
106
- });
107
- subscriptions.set(id2, { param, latest: Date.now() });
108
- } else {
109
- handlers.add(handler);
110
- }
111
- return async function unsubscribe() {
112
- const handlers2 = params.get(param);
113
- if (handlers2 === void 0) {
114
- return logger.warn("Param already unsubscribed...");
115
- }
116
- handlers2.delete(handler);
117
- if (handlers2.size === 0) {
118
- await request({ method: "eth_unsubscribe", params: [id] }).catch(() => {
119
- logger.warn("Failed to unsubscribe unused subscription");
120
- });
121
- }
122
- };
123
- };
124
- function healthcheck() {
125
- const params2 = [];
126
- for (const [id2, subscription] of subscriptions) {
127
- if (Date.now() - subscription.latest > 60 * 1e3) {
128
- params2.push(subscription.param);
129
- request({ method: "eth_unsubscribe", params: [id2] }).catch(() => {
130
- logger.warn("Failed to cancel old susbcription");
131
- });
132
- subscriptions.delete(id2);
133
- logger.warn(`Subscription ${id2} failed health check, reinitialising..`);
134
- }
135
- }
136
- for (const param of params2) {
137
- request({ method: "eth_subscribe", params: [param] }).then((id2) => {
138
- subscriptions.set(id2, { param, latest: Date.now() });
139
- });
140
- }
141
- }
142
- setInterval(healthcheck, 60 * 1e3);
143
- return { protocol: "wss", request, subscribe };
144
- }
145
- function http(url, opts = {}) {
146
- let id = 0;
147
- const request = async (options) => {
148
- let body = JSON.stringify({
149
- id: id++,
150
- jsonrpc: "2.0",
151
- method: options.method,
152
- params: options.params
153
- });
154
- const headers = new Headers();
155
- headers.set("Content-Type", "application/json");
156
- if (options.method.startsWith("private_")) {
157
- if (opts.signingKey === void 0) {
158
- throw new Error(ClientUnauthorizedError);
159
- }
160
- body = await compress(body).catch((cause) => raise(ClientCompressionError, { cause }));
161
- headers.set("Content-Encoding", "gzip");
162
- headers.set("Authorization", `Bearer ${opts.signingKey}`);
163
- }
164
- const res = await fetch(url, { headers, body, method: "POST", signal: AbortSignal.timeout(30 * 1e3) }).catch((cause) => {
165
- throw new Error(ClientConnectionError, { cause });
166
- });
167
- if (!res.ok || res.status < 200 || res.status >= 300) {
168
- throw new Error(ClientConnectionError);
169
- }
170
- const json = await res.json().catch((cause) => {
171
- throw new Error(ClientResponseError, { cause });
172
- });
173
- if (json.error) {
174
- throw new Error(json.error.message);
175
- }
176
- return json.result;
177
- };
178
- const subscribe = async () => {
179
- throw new Error("Unable to `subscribe` on `http` transport");
180
- };
181
- return { protocol: "http", request, subscribe };
182
- }
183
- const ClientCompressionError = createException("An error occurred when compressing the request");
184
- const ClientConnectionError = createException("An errored occurred when connecting to the server");
185
- const ClientResponseError = createException("An error occurred when reading the servers response");
186
- const ClientUnauthorizedError = createException("Attempted to execute a private method without providing a request signing key");
187
-
188
- export { http, wss };
1
+ import 'ws';
2
+ import 'partysocket';
3
+ export { h as http, l as local, w as wss } from './shared/univo.BwPUSTpl.mjs';
4
+ import './shared/univo.B_UsEiYb.mjs';
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "univo",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "0.2.11",
5
+ "version": "0.2.13",
6
6
  "sideEffects": false,
7
7
  "module": "./dist/index.mjs",
8
8
  "types": "./dist/index.d.ts",
@@ -43,11 +43,9 @@
43
43
  "ws": "^8.18.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@hono/node-server": "^1.14.1",
47
46
  "@types/node": "^20.11.27",
48
47
  "@types/ws": "^8.18.1",
49
48
  "changelogen": "^0.6.1",
50
- "hono": "^4.7.8",
51
49
  "jiti": "^2.4.2",
52
50
  "msw": "^2.10.2",
53
51
  "typescript": "^5.8.2",
@@ -1,21 +0,0 @@
1
- function createException(message) {
2
- return `univo:${message}`;
3
- }
4
- function catchException(unknown, exception) {
5
- if (unknown instanceof Error) {
6
- if (unknown.message === exception) {
7
- return true;
8
- }
9
- }
10
- return false;
11
- }
12
- function getException(unknown) {
13
- if (unknown instanceof Error) {
14
- if (unknown.message.startsWith("univo")) {
15
- const [_, message] = unknown.message.split(":");
16
- if (message) return message;
17
- }
18
- }
19
- }
20
-
21
- export { catchException as a, createException as c, getException as g };