http-air 1.3.1 → 1.3.3
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 +0 -33
- package/lib/client/client.d.ts +1 -2
- package/lib/client/client.js +0 -3
- package/lib/client/index.d.ts +1 -1
- package/lib/client/rpc.d.ts +0 -3
- package/lib/client/rpc.js +0 -21
- package/lib/server/events.js +3 -3
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -4,7 +4,6 @@ HTTP library for batched RPC calls and real-time server-push over a single endpo
|
|
|
4
4
|
|
|
5
5
|
- **RPC** — calls made in the same tick are grouped into one HTTP request. The server executes them concurrently and streams each result back as it resolves, matched to the original call by index. The client resolves each promise individually as its result arrives.
|
|
6
6
|
- **Events** — server pushes named events to subscribed clients over a persistent HTTP connection, with heartbeat and auto-reconnect.
|
|
7
|
-
- **SWR cache** — RPC calls support stale-while-revalidate: return a cached value immediately and revalidate in the background, with a cancel handle.
|
|
8
7
|
|
|
9
8
|
## Why http-air
|
|
10
9
|
|
|
@@ -209,38 +208,6 @@ const [r1, r2, r3] = await Promise.all([
|
|
|
209
208
|
])
|
|
210
209
|
```
|
|
211
210
|
|
|
212
|
-
### SWR (stale-while-revalidate)
|
|
213
|
-
|
|
214
|
-
`callSwr` returns a tuple `[stale, fresh, cancel]` — immediately a cached (stale) value and a background revalidation promise. If no cache exists both point to the same request. If the cached value is within `ttl` (ms) no request is made.
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
const [stale, fresh, cancel] = client.callSwr('getUser', [42], 5000)
|
|
218
|
-
|
|
219
|
-
// render immediately with cached data
|
|
220
|
-
const user = await stale
|
|
221
|
-
|
|
222
|
-
// update once revalidated
|
|
223
|
-
const updated = await fresh
|
|
224
|
-
|
|
225
|
-
// cancel the background revalidation (e.g. component unmounted)
|
|
226
|
-
cancel()
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
Works well inside a React `useEffect`:
|
|
230
|
-
|
|
231
|
-
```ts
|
|
232
|
-
useEffect(() => {
|
|
233
|
-
const [stale, fresh, cancel] = rpcClient.callSwr('getUser', [id], 5_000)
|
|
234
|
-
stale.then(setUser)
|
|
235
|
-
fresh.then(setUser)
|
|
236
|
-
return cancel
|
|
237
|
-
}, [id])
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
`Client` exposes the same method as `callRpcSwr`.
|
|
241
|
-
|
|
242
|
-
---
|
|
243
|
-
|
|
244
211
|
### Events
|
|
245
212
|
|
|
246
213
|
Connects automatically on the first `subscribe()` call and disconnects when all subscriptions are removed. Multiple subscribe/unsubscribe calls in the same tick are batched. Auto-reconnect is always enabled.
|
package/lib/client/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RpcClientConfig
|
|
1
|
+
import { RpcClientConfig } from './rpc';
|
|
2
2
|
export interface ClientConfig {
|
|
3
3
|
/** Endpoint URL. Defaults to current page location */
|
|
4
4
|
url?: string;
|
|
@@ -19,7 +19,6 @@ export declare class Client {
|
|
|
19
19
|
private events;
|
|
20
20
|
constructor(config?: RpcClientConfig);
|
|
21
21
|
callRpc(method: string, params: any[]): Promise<any>;
|
|
22
|
-
callRpcSwr(method: string, params: any[], ttl: number): SwrResult;
|
|
23
22
|
subscribeEvent(eventName: string | string[], handler: (data: any) => void): () => void;
|
|
24
23
|
unsubscribeEvent(eventName: string | string[], handler: (data: any) => void): void;
|
|
25
24
|
disconnect(): void;
|
package/lib/client/client.js
CHANGED
|
@@ -28,9 +28,6 @@ class Client {
|
|
|
28
28
|
callRpc(method, params) {
|
|
29
29
|
return this.rpc.call(method, params);
|
|
30
30
|
}
|
|
31
|
-
callRpcSwr(method, params, ttl) {
|
|
32
|
-
return this.rpc.callSwr(method, params, ttl);
|
|
33
|
-
}
|
|
34
31
|
subscribeEvent(eventName, handler) {
|
|
35
32
|
return this.events.subscribe(eventName, handler);
|
|
36
33
|
}
|
package/lib/client/index.d.ts
CHANGED
package/lib/client/rpc.d.ts
CHANGED
|
@@ -8,7 +8,6 @@ export interface RpcClientConfig extends ClientConfig {
|
|
|
8
8
|
/** Called on each response message (result or error) */
|
|
9
9
|
onResponse?: (resp: ResponseMessage) => void;
|
|
10
10
|
}
|
|
11
|
-
export type SwrResult = [stale: Promise<any>, fresh: Promise<any>, cancel: () => void];
|
|
12
11
|
export declare class RpcClient {
|
|
13
12
|
private url;
|
|
14
13
|
private fetchFn;
|
|
@@ -16,9 +15,7 @@ export declare class RpcClient {
|
|
|
16
15
|
private deduplicate;
|
|
17
16
|
private batch;
|
|
18
17
|
private batcher;
|
|
19
|
-
private swrCache;
|
|
20
18
|
constructor(config?: RpcClientConfig);
|
|
21
19
|
call(method: string, params: any[]): Promise<any>;
|
|
22
|
-
callSwr(method: string, params: any[], ttl: number): SwrResult;
|
|
23
20
|
private send;
|
|
24
21
|
}
|
package/lib/client/rpc.js
CHANGED
|
@@ -17,7 +17,6 @@ class RpcClient {
|
|
|
17
17
|
deduplicate;
|
|
18
18
|
batch;
|
|
19
19
|
batcher;
|
|
20
|
-
swrCache = new Map();
|
|
21
20
|
constructor(config = {}) {
|
|
22
21
|
const { url, fetchFn } = (0, client_1.resolveConfig)(config);
|
|
23
22
|
this.url = url;
|
|
@@ -34,26 +33,6 @@ class RpcClient {
|
|
|
34
33
|
this.batcher.flush();
|
|
35
34
|
return dp.promise;
|
|
36
35
|
}
|
|
37
|
-
callSwr(method, params, ttl) {
|
|
38
|
-
const key = method + JSON.stringify(params);
|
|
39
|
-
const cached = this.swrCache.get(key);
|
|
40
|
-
let cancelled = false;
|
|
41
|
-
const cancel = () => { cancelled = true; };
|
|
42
|
-
const revalidate = () => this.call(method, params).then(value => {
|
|
43
|
-
if (!cancelled)
|
|
44
|
-
this.swrCache.set(key, { value, fetchedAt: Date.now() });
|
|
45
|
-
return value;
|
|
46
|
-
});
|
|
47
|
-
if (!cached) {
|
|
48
|
-
const fresh = revalidate();
|
|
49
|
-
return [fresh, fresh, cancel];
|
|
50
|
-
}
|
|
51
|
-
const stale = Promise.resolve(cached.value);
|
|
52
|
-
if (Date.now() - cached.fetchedAt < ttl) {
|
|
53
|
-
return [stale, stale, cancel];
|
|
54
|
-
}
|
|
55
|
-
return [stale, revalidate(), cancel];
|
|
56
|
-
}
|
|
57
36
|
send(items) {
|
|
58
37
|
const requests = [];
|
|
59
38
|
const deferredGroups = [];
|
package/lib/server/events.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventsServer = void 0;
|
|
4
|
-
const
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
5
|
const json_stream_1 = require("../shared/json-stream");
|
|
6
6
|
const stream_constants_1 = require("../shared/stream-constants");
|
|
7
7
|
class EventsServer {
|
|
8
|
-
serverId = (0,
|
|
8
|
+
serverId = (0, crypto_1.randomBytes)(16).toString('base64url');
|
|
9
9
|
responsesMap = new Map();
|
|
10
10
|
messageQueueMap = new Map();
|
|
11
11
|
drainingSet = new Set();
|
|
@@ -89,7 +89,7 @@ class EventsServer {
|
|
|
89
89
|
handleConnect(req, res) {
|
|
90
90
|
let sessionId = req.getHeader(stream_constants_1.SESSION_ID_KEY).trim();
|
|
91
91
|
if (!sessionId.startsWith(stream_constants_1.SESSION_ID_PREFIX)) {
|
|
92
|
-
sessionId = stream_constants_1.SESSION_ID_PREFIX + (0,
|
|
92
|
+
sessionId = stream_constants_1.SESSION_ID_PREFIX + (0, crypto_1.randomBytes)(60).toString('base64url');
|
|
93
93
|
}
|
|
94
94
|
const oldRes = this.responsesMap.get(sessionId);
|
|
95
95
|
if (oldRes) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "http-air",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "HTTP library for batched RPC calls and real-time server-push over a single endpoint.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": "https://github.com/yosbelms/http-air",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"author": "",
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"nanoid": "^5.1.7",
|
|
27
26
|
"p-defer": "^3.0.0",
|
|
28
27
|
"p-retry": "^4.6.2"
|
|
29
28
|
},
|