@whiteplusms/dsh-redis 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +184 -2
- package/README.zh.md +185 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +1 -0
- package/lib/client/index.d.ts +4 -0
- package/lib/client.js +7684 -0
- package/lib/connection-errors.d.ts +66 -0
- package/lib/connection-errors.js +21 -0
- package/lib/domain.d.ts +451 -0
- package/lib/domain.js +20 -0
- package/lib/host/ai-answer.d.ts +7 -0
- package/lib/host/ai-answer.js +132 -0
- package/lib/host/ai-budget.d.ts +43 -0
- package/lib/host/ai-budget.js +78 -0
- package/lib/host/ai-connection-tools.d.ts +8 -0
- package/lib/host/ai-connection-tools.js +49 -0
- package/lib/host/ai-hash-filter.d.ts +13 -0
- package/lib/host/ai-hash-filter.js +97 -0
- package/lib/host/ai-hash-pages.d.ts +15 -0
- package/lib/host/ai-hash-pages.js +42 -0
- package/lib/host/ai-key-pages.d.ts +14 -0
- package/lib/host/ai-key-pages.js +27 -0
- package/lib/host/ai-locate-tool.d.ts +6 -0
- package/lib/host/ai-locate-tool.js +234 -0
- package/lib/host/ai-match-tools.d.ts +7 -0
- package/lib/host/ai-match-tools.js +112 -0
- package/lib/host/ai-page-coordinator.d.ts +21 -0
- package/lib/host/ai-page-coordinator.js +81 -0
- package/lib/host/ai-page-store.d.ts +40 -0
- package/lib/host/ai-page-store.js +104 -0
- package/lib/host/ai-profile-tools.d.ts +7 -0
- package/lib/host/ai-profile-tools.js +44 -0
- package/lib/host/ai-query-scope.d.ts +42 -0
- package/lib/host/ai-query-scope.js +104 -0
- package/lib/host/ai-reader.d.ts +219 -0
- package/lib/host/ai-reader.js +1221 -0
- package/lib/host/ai-report-samples.d.ts +26 -0
- package/lib/host/ai-report-samples.js +77 -0
- package/lib/host/ai-report-tools.d.ts +11 -0
- package/lib/host/ai-report-tools.js +70 -0
- package/lib/host/ai-set-pages.d.ts +15 -0
- package/lib/host/ai-set-pages.js +40 -0
- package/lib/host/ai-stream-pages.d.ts +15 -0
- package/lib/host/ai-stream-pages.js +42 -0
- package/lib/host/ai-tools.d.ts +4 -0
- package/lib/host/ai-tools.js +106 -0
- package/lib/host/ai-ui-tools.d.ts +10 -0
- package/lib/host/ai-ui-tools.js +72 -0
- package/lib/host/ai-value.d.ts +12 -0
- package/lib/host/ai-value.js +15 -0
- package/lib/host/ai-write-tools.d.ts +5 -0
- package/lib/host/ai-write-tools.js +101 -0
- package/lib/host/ai-writer.d.ts +25 -0
- package/lib/host/ai-writer.js +77 -0
- package/lib/host/ai-zset-pages.d.ts +15 -0
- package/lib/host/ai-zset-pages.js +40 -0
- package/lib/host/byte-identity.d.ts +3 -0
- package/lib/host/byte-identity.js +12 -0
- package/lib/host/client-list.d.ts +13 -0
- package/lib/host/client-list.js +35 -0
- package/lib/host/connection-service.d.ts +71 -0
- package/lib/host/connection-service.js +468 -0
- package/lib/host/database-reader.d.ts +10 -0
- package/lib/host/database-reader.js +62 -0
- package/lib/host/hash-reader.d.ts +12 -0
- package/lib/host/hash-reader.js +91 -0
- package/lib/host/key-reader.d.ts +20 -0
- package/lib/host/key-reader.js +65 -0
- package/lib/host/list-reader.d.ts +8 -0
- package/lib/host/list-reader.js +61 -0
- package/lib/host/match-preset-store.d.ts +36 -0
- package/lib/host/match-preset-store.js +201 -0
- package/lib/host/profile-snapshots.d.ts +32 -0
- package/lib/host/profile-snapshots.js +187 -0
- package/lib/host/profile-store.d.ts +15 -0
- package/lib/host/profile-store.js +120 -0
- package/lib/host/read-context.d.ts +23 -0
- package/lib/host/read-context.js +41 -0
- package/lib/host/redis-transport.d.ts +34 -0
- package/lib/host/redis-transport.js +262 -0
- package/lib/host/redis-write-transaction.d.ts +28 -0
- package/lib/host/redis-write-transaction.js +180 -0
- package/lib/host/resp.d.ts +38 -0
- package/lib/host/resp.js +174 -0
- package/lib/host/scan-cursors.d.ts +25 -0
- package/lib/host/scan-cursors.js +59 -0
- package/lib/host/selected-reader.d.ts +22 -0
- package/lib/host/selected-reader.js +84 -0
- package/lib/host/set-reader.d.ts +12 -0
- package/lib/host/set-reader.js +85 -0
- package/lib/host/stream-groups.d.ts +17 -0
- package/lib/host/stream-groups.js +42 -0
- package/lib/host/stream-reader.d.ts +12 -0
- package/lib/host/stream-reader.js +105 -0
- package/lib/host/string-reader.d.ts +9 -0
- package/lib/host/string-reader.js +81 -0
- package/lib/host/ui-action-channel.d.ts +4 -0
- package/lib/host/ui-action-channel.js +182 -0
- package/lib/host/ui-action-coordinator.d.ts +28 -0
- package/lib/host/ui-action-coordinator.js +188 -0
- package/lib/host/value-prefix.d.ts +5 -0
- package/lib/host/value-prefix.js +11 -0
- package/lib/host/write-bytes.d.ts +3 -0
- package/lib/host/write-bytes.js +9 -0
- package/lib/host/write-history.d.ts +18 -0
- package/lib/host/write-history.js +79 -0
- package/lib/host/write-proposals.d.ts +46 -0
- package/lib/host/write-proposals.js +132 -0
- package/lib/host/zset-reader.d.ts +12 -0
- package/lib/host/zset-reader.js +103 -0
- package/lib/host/zset-values.d.ts +6 -0
- package/lib/host/zset-values.js +26 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +5 -0
- package/lib/redis-answer.d.ts +32 -0
- package/lib/redis-answer.js +215 -0
- package/lib/redis-databases.d.ts +3 -0
- package/lib/redis-databases.js +21 -0
- package/lib/redis-read-tools.d.ts +2 -0
- package/lib/redis-read-tools.js +7 -0
- package/lib/redis-result.d.ts +4 -0
- package/lib/redis-result.js +18 -0
- package/lib/redis-score.d.ts +2 -0
- package/lib/redis-score.js +6 -0
- package/lib/remote-service.d.ts +69 -0
- package/lib/remote-service.js +187 -0
- package/lib/runtime.d.ts +3 -0
- package/lib/runtime.js +29 -0
- package/lib/stream-id.d.ts +7 -0
- package/lib/stream-id.js +19 -0
- package/lib/typert.host.d.ts +13 -0
- package/lib/typert.host.js +7 -0
- package/lib/typert.remote-client.d.ts +98 -0
- package/lib/typert.remote-client.js +3 -0
- package/lib/typert.shared.d.ts +62 -0
- package/lib/typert.shared.js +735 -0
- package/lib/ui-action-selection.d.ts +3 -0
- package/lib/ui-action-selection.js +12 -0
- package/licenses/THIRD_PARTY_NOTICES.txt +9 -0
- package/licenses/deepseek-harness-MIT.txt +21 -0
- package/licenses/dsh-database-MIT.txt +21 -0
- package/licenses/dsh-git-graph-MIT.txt +21 -0
- package/licenses/lucide-react-LICENSE.txt +43 -0
- package/licenses/phosphor-icons-MIT.txt +21 -0
- package/package.json +132 -3
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
function sameTarget(left, right) {
|
|
3
|
+
return left === null ? right === null : right !== null && left.id === right.id && left.generation === right.generation && left.db === right.db;
|
|
4
|
+
}
|
|
5
|
+
/** Actions are plain JSON identities. Freeze the cloned graph shared with the receiver. */
|
|
6
|
+
function freezeSnapshot(value) {
|
|
7
|
+
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
8
|
+
Object.freeze(value);
|
|
9
|
+
for (const nested of Object.values(value))
|
|
10
|
+
freezeSnapshot(nested);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
/** Coordinates one bounded action per trusted session; actual UI work belongs to the receiver. */
|
|
15
|
+
export class UiActionCoordinator {
|
|
16
|
+
sessions = new Map();
|
|
17
|
+
register(sessionId, receive) {
|
|
18
|
+
let session = this.sessions.get(sessionId);
|
|
19
|
+
if (session === undefined) {
|
|
20
|
+
session = { receivers: new Set(), selections: new Map(), pending: null };
|
|
21
|
+
this.sessions.set(sessionId, session);
|
|
22
|
+
}
|
|
23
|
+
if (session.receivers.has(receive))
|
|
24
|
+
throw new Error('界面接收者已注册。');
|
|
25
|
+
const owner = session;
|
|
26
|
+
owner.receivers.add(receive);
|
|
27
|
+
// A newly opened second view makes the destination ambiguous even while waiting for an ack.
|
|
28
|
+
if (owner.receivers.size > 1)
|
|
29
|
+
owner.pending?.finish('ambiguous');
|
|
30
|
+
return () => {
|
|
31
|
+
if (this.sessions.get(sessionId) !== owner || !owner.receivers.delete(receive))
|
|
32
|
+
return;
|
|
33
|
+
owner.selections.delete(receive);
|
|
34
|
+
owner.pending?.finish('disconnected');
|
|
35
|
+
if (owner.receivers.size === 0 && owner.pending === null)
|
|
36
|
+
this.sessions.delete(sessionId);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** State belongs to a particular registered stream, never to a model argument. */
|
|
40
|
+
setSelectionState(sessionId, receive, revision, synchronized) {
|
|
41
|
+
const session = this.sessions.get(sessionId);
|
|
42
|
+
if (session === undefined || !session.receivers.has(receive))
|
|
43
|
+
return;
|
|
44
|
+
session.selections.set(receive, { revision, synchronized });
|
|
45
|
+
}
|
|
46
|
+
selectionRevision(sessionId) {
|
|
47
|
+
const session = this.sessions.get(sessionId);
|
|
48
|
+
if (session === undefined || session.receivers.size !== 1)
|
|
49
|
+
return null;
|
|
50
|
+
const receiver = session.receivers.values().next().value;
|
|
51
|
+
const selection = receiver === undefined ? undefined : session.selections.get(receiver);
|
|
52
|
+
return selection?.synchronized === true ? selection.revision : null;
|
|
53
|
+
}
|
|
54
|
+
isUniqueReceiver(sessionId, receive) {
|
|
55
|
+
const session = this.sessions.get(sessionId);
|
|
56
|
+
return session !== undefined && session.receivers.size === 1 && session.receivers.has(receive);
|
|
57
|
+
}
|
|
58
|
+
/** A commit is valid only for the still-pending request on its unique stream. */
|
|
59
|
+
canCommit(sessionId, receive, request) {
|
|
60
|
+
const session = this.sessions.get(sessionId), pending = session?.pending;
|
|
61
|
+
return session !== undefined && session.receivers.size === 1 && session.receivers.has(receive) && pending?.receiver === receive && pending.request === request && Date.now() < request.expiresAt && pending.current();
|
|
62
|
+
}
|
|
63
|
+
/** Resolve an unprepared action only while its original selection remains current. */
|
|
64
|
+
activeRequest(sessionId, requestId) {
|
|
65
|
+
const pending = this.sessions.get(sessionId)?.pending;
|
|
66
|
+
if (pending === null || pending === undefined || pending.prepared || pending.request.requestId !== requestId || pending.request.target === null || this.selectionRevision(sessionId) !== pending.request.selectionRevision || !this.canCommit(sessionId, pending.receiver, pending.request))
|
|
67
|
+
return null;
|
|
68
|
+
return pending.request;
|
|
69
|
+
}
|
|
70
|
+
/** A UI action may sample its point once, even if that sample is cancelled. */
|
|
71
|
+
claimPoint(sessionId, request) {
|
|
72
|
+
const pending = this.sessions.get(sessionId)?.pending;
|
|
73
|
+
if (pending === null || pending === undefined || pending.pointClaimed || this.activeRequest(sessionId, request.requestId) !== request)
|
|
74
|
+
return false;
|
|
75
|
+
pending.pointClaimed = true;
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
/** The Host marks the bound point sample only after a successful, current read. */
|
|
79
|
+
markPointSucceeded(sessionId, request) {
|
|
80
|
+
const pending = this.sessions.get(sessionId)?.pending;
|
|
81
|
+
if (pending === null || pending === undefined || !pending.pointClaimed || this.activeRequest(sessionId, request.requestId) !== request)
|
|
82
|
+
return false;
|
|
83
|
+
pending.pointSucceeded = true;
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
pointSucceeded(sessionId, request) {
|
|
87
|
+
const pending = this.sessions.get(sessionId)?.pending;
|
|
88
|
+
return pending !== null && pending !== undefined && pending.request === request && pending.pointSucceeded;
|
|
89
|
+
}
|
|
90
|
+
/** Once Host has prepared an action, interruptions cannot prove UI application either way. */
|
|
91
|
+
markPrepared(sessionId, receive, request) {
|
|
92
|
+
const pending = this.sessions.get(sessionId)?.pending;
|
|
93
|
+
if (pending?.receiver === receive && pending.request === request)
|
|
94
|
+
pending.prepared = true;
|
|
95
|
+
}
|
|
96
|
+
async dispatch(sessionId, action, target, selectionRevision, isCurrent, signal, timeoutMs = 10000) {
|
|
97
|
+
if (!Number.isSafeInteger(selectionRevision) || selectionRevision < 0 || !Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 10000)
|
|
98
|
+
throw new RangeError('界面动作期限或选择版本无效。');
|
|
99
|
+
if (signal.aborted)
|
|
100
|
+
return 'cancelled';
|
|
101
|
+
const session = this.sessions.get(sessionId);
|
|
102
|
+
if (session === undefined || session.receivers.size === 0)
|
|
103
|
+
return 'no_receiver';
|
|
104
|
+
if (session.receivers.size !== 1)
|
|
105
|
+
return 'ambiguous';
|
|
106
|
+
if (session.pending !== null)
|
|
107
|
+
return 'busy';
|
|
108
|
+
if (!isCurrent())
|
|
109
|
+
return 'stale';
|
|
110
|
+
const receive = session.receivers.values().next().value;
|
|
111
|
+
if (receive === undefined)
|
|
112
|
+
return 'no_receiver';
|
|
113
|
+
const request = freezeSnapshot({ requestId: randomUUID(), action: structuredClone(action), target: target === null ? null : { ...target }, selectionRevision, expiresAt: Date.now() + timeoutMs });
|
|
114
|
+
const controller = new AbortController();
|
|
115
|
+
return new Promise(resolve => {
|
|
116
|
+
let finished = false;
|
|
117
|
+
const finish = (outcome) => {
|
|
118
|
+
if (finished)
|
|
119
|
+
return;
|
|
120
|
+
finished = true;
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
signal.removeEventListener('abort', cancelled);
|
|
123
|
+
if (session.pending === pending)
|
|
124
|
+
session.pending = null;
|
|
125
|
+
const result = pending.prepared && outcome !== 'applied' ? 'unknown' : outcome;
|
|
126
|
+
if (result !== 'applied')
|
|
127
|
+
controller.abort();
|
|
128
|
+
if (this.sessions.get(sessionId) === session && session.receivers.size === 0)
|
|
129
|
+
this.sessions.delete(sessionId);
|
|
130
|
+
resolve(result);
|
|
131
|
+
};
|
|
132
|
+
const pending = { finish, request, receiver: receive, current: isCurrent, prepared: false, pointClaimed: false, pointSucceeded: false }, cancelled = () => finish('cancelled');
|
|
133
|
+
const timer = setTimeout(() => finish('timeout'), timeoutMs);
|
|
134
|
+
session.pending = pending;
|
|
135
|
+
signal.addEventListener('abort', cancelled, { once: true });
|
|
136
|
+
if (signal.aborted) {
|
|
137
|
+
finish('cancelled');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
void Promise.resolve().then(() => {
|
|
141
|
+
if (finished)
|
|
142
|
+
return;
|
|
143
|
+
return receive(request, controller.signal);
|
|
144
|
+
}).then(ack => {
|
|
145
|
+
if (finished || ack === undefined)
|
|
146
|
+
return;
|
|
147
|
+
if (Date.now() >= request.expiresAt) {
|
|
148
|
+
finish('timeout');
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (!isCurrent()) {
|
|
152
|
+
finish('stale');
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (session.receivers.size !== 1 || !session.receivers.has(receive)) {
|
|
156
|
+
finish('ambiguous');
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (ack.requestId !== request.requestId || !sameTarget(ack.target, request.target) || ack.selectionRevision !== request.selectionRevision) {
|
|
160
|
+
finish('stale');
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (ack.outcome !== 'applied' && ack.outcome !== 'unsupported' && ack.outcome !== 'failed' && ack.outcome !== 'stale' && ack.outcome !== 'ambiguous') {
|
|
164
|
+
finish('failed');
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
finish(ack.outcome);
|
|
168
|
+
}).catch(() => finish('failed'));
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/** Selection or target changes cannot invalidate an action without a target, such as showing the pane. */
|
|
172
|
+
invalidateSession(sessionId) {
|
|
173
|
+
const pending = this.sessions.get(sessionId)?.pending;
|
|
174
|
+
if (pending !== undefined && pending !== null && pending.request.target !== null)
|
|
175
|
+
pending.finish('stale');
|
|
176
|
+
}
|
|
177
|
+
disposeSession(sessionId) {
|
|
178
|
+
const session = this.sessions.get(sessionId);
|
|
179
|
+
if (session === undefined)
|
|
180
|
+
return;
|
|
181
|
+
this.sessions.delete(sessionId);
|
|
182
|
+
session.pending?.finish('disconnected');
|
|
183
|
+
session.receivers.clear();
|
|
184
|
+
session.selections.clear();
|
|
185
|
+
}
|
|
186
|
+
dispose() { for (const sessionId of this.sessions.keys())
|
|
187
|
+
this.disposeSession(sessionId); }
|
|
188
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type BytePrefix } from '../domain.js';
|
|
2
|
+
/** Shared collection-cell prefix. The RESP receive cap applies before this
|
|
3
|
+
* projection, and the complete outer JSON page has its own separate budget.
|
|
4
|
+
*/
|
|
5
|
+
export declare function valuePrefix(raw: Buffer): BytePrefix;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MAX_SCAN_JSON_BYTES, MAX_VALUE_BYTES } from "../domain.js";
|
|
2
|
+
import { RedisFrameError } from "./resp.js";
|
|
3
|
+
/** Shared collection-cell prefix. The RESP receive cap applies before this
|
|
4
|
+
* projection, and the complete outer JSON page has its own separate budget.
|
|
5
|
+
*/
|
|
6
|
+
export function valuePrefix(raw) {
|
|
7
|
+
if (raw.length > MAX_SCAN_JSON_BYTES)
|
|
8
|
+
throw new RedisFrameError('oversize');
|
|
9
|
+
const bytes = raw.subarray(0, MAX_VALUE_BYTES);
|
|
10
|
+
return { base64: bytes.toString('base64'), byteLength: bytes.length, fullByteLength: raw.length, suffix: raw.subarray(bytes.length, bytes.length + 3).toString('base64') };
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MAX_REDIS_WRITE_BYTES } from "../domain.js";
|
|
2
|
+
export const MAX_WRITE_BASE64 = Math.ceil(MAX_REDIS_WRITE_BYTES / 3) * 4;
|
|
3
|
+
/** Preserve exact Redis byte identity, including an empty value. */
|
|
4
|
+
export function decodeWriteBytes(value) {
|
|
5
|
+
if (value.length > MAX_WRITE_BASE64)
|
|
6
|
+
return null;
|
|
7
|
+
const bytes = Buffer.from(value, 'base64');
|
|
8
|
+
return bytes.length <= MAX_REDIS_WRITE_BYTES && bytes.toString('base64') === value ? bytes : null;
|
|
9
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DatabaseTarget, RedisWriteIdentity, RedisWriteRecord } from '../domain.js';
|
|
2
|
+
import type { RedisWriteResult } from './redis-write-transaction.js';
|
|
3
|
+
export interface RedisWriteRecordHandle {
|
|
4
|
+
submitted(): void;
|
|
5
|
+
finish(result: RedisWriteResult, readbackAt?: string): void;
|
|
6
|
+
}
|
|
7
|
+
/** Bounded Host memory only: no credentials, old values or new values are stored. */
|
|
8
|
+
export declare class RedisWriteHistory {
|
|
9
|
+
private readonly entries;
|
|
10
|
+
private timer;
|
|
11
|
+
private disposed;
|
|
12
|
+
start(sessionId: string, id: string, target: DatabaseTarget, identity: RedisWriteIdentity): RedisWriteRecordHandle;
|
|
13
|
+
/** A caller may only inspect records from its own trusted session. */
|
|
14
|
+
get(sessionId: string, id: string): RedisWriteRecord | null;
|
|
15
|
+
private prune;
|
|
16
|
+
private schedule;
|
|
17
|
+
dispose(): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { decodeWriteBytes } from "./write-bytes.js";
|
|
2
|
+
const MAX_RECORDS = 128;
|
|
3
|
+
const MAX_SESSION_RECORDS = 16;
|
|
4
|
+
const RETENTION_MS = 10 * 60 * 1000;
|
|
5
|
+
function copy(record) {
|
|
6
|
+
return { ...record, target: { ...record.target }, identity: { ...record.identity } };
|
|
7
|
+
}
|
|
8
|
+
/** Bounded Host memory only: no credentials, old values or new values are stored. */
|
|
9
|
+
export class RedisWriteHistory {
|
|
10
|
+
entries = new Map();
|
|
11
|
+
timer;
|
|
12
|
+
disposed = false;
|
|
13
|
+
start(sessionId, id, target, identity) {
|
|
14
|
+
if (this.disposed || sessionId.length === 0 || sessionId.length > 256 || !/^[0-9a-f-]{36}$/u.test(id))
|
|
15
|
+
throw new Error('Redis 操作记录身份无效。');
|
|
16
|
+
if (decodeWriteBytes(identity.keyBase64) === null || (identity.type === 'string' ? identity.fieldBase64 !== null : identity.type !== 'hash' || identity.fieldBase64 === null || decodeWriteBytes(identity.fieldBase64) === null))
|
|
17
|
+
throw new Error('Redis 操作目标无效。');
|
|
18
|
+
this.prune();
|
|
19
|
+
if (this.entries.has(id) || this.entries.size >= MAX_RECORDS || [...this.entries.values()].filter(entry => entry.sessionId === sessionId).length >= MAX_SESSION_RECORDS)
|
|
20
|
+
throw new Error('Redis 操作记录已存在或达到容量上限。');
|
|
21
|
+
const now = new Date().toISOString();
|
|
22
|
+
const entry = { sessionId, record: { id, target: { id: target.id, generation: target.generation, db: target.db }, identity: { type: identity.type, keyBase64: identity.keyBase64, fieldBase64: identity.fieldBase64 }, state: 'pending', startedAt: now, updatedAt: now, readbackAt: null }, deadline: null };
|
|
23
|
+
this.entries.set(id, entry);
|
|
24
|
+
const current = () => !this.disposed && this.entries.get(id) === entry;
|
|
25
|
+
return {
|
|
26
|
+
submitted: () => {
|
|
27
|
+
if (!current() || entry.record.state !== 'pending')
|
|
28
|
+
throw new Error('Redis 操作记录不可提交。');
|
|
29
|
+
entry.record = { ...entry.record, state: 'submitting', updatedAt: new Date().toISOString() };
|
|
30
|
+
},
|
|
31
|
+
finish: (result, readbackAt) => {
|
|
32
|
+
if (!current() || entry.deadline !== null)
|
|
33
|
+
return;
|
|
34
|
+
const didSubmit = entry.record.state === 'submitting';
|
|
35
|
+
const timestamp = readbackAt === undefined ? NaN : Date.parse(readbackAt);
|
|
36
|
+
const validReadback = Number.isFinite(timestamp) && new Date(timestamp).toISOString() === readbackAt;
|
|
37
|
+
// Once dispatch may have happened, a generic pre-dispatch result is unsafe.
|
|
38
|
+
// Conversely, a claimed successful write without dispatch evidence is unknown.
|
|
39
|
+
const state = didSubmit && result === 'not-executed' || !didSubmit && result.startsWith('written-') ? 'unknown' : result === 'written-verified' && !validReadback ? 'written-unverified' : result;
|
|
40
|
+
entry.record = { ...entry.record, state, readbackAt: validReadback && (state === 'written-verified' || state === 'written-mismatch') ? readbackAt : null, updatedAt: new Date().toISOString() };
|
|
41
|
+
entry.deadline = performance.now() + RETENTION_MS;
|
|
42
|
+
this.schedule();
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** A caller may only inspect records from its own trusted session. */
|
|
47
|
+
get(sessionId, id) {
|
|
48
|
+
this.prune();
|
|
49
|
+
this.schedule();
|
|
50
|
+
const entry = this.entries.get(id);
|
|
51
|
+
return entry?.sessionId === sessionId ? copy(entry.record) : null;
|
|
52
|
+
}
|
|
53
|
+
prune() {
|
|
54
|
+
const now = performance.now();
|
|
55
|
+
for (const [id, entry] of this.entries)
|
|
56
|
+
if (entry.deadline !== null && entry.deadline <= now)
|
|
57
|
+
this.entries.delete(id);
|
|
58
|
+
}
|
|
59
|
+
schedule() {
|
|
60
|
+
if (this.timer !== undefined)
|
|
61
|
+
clearTimeout(this.timer);
|
|
62
|
+
this.timer = undefined;
|
|
63
|
+
let earliest = Infinity;
|
|
64
|
+
for (const entry of this.entries.values())
|
|
65
|
+
if (entry.deadline !== null)
|
|
66
|
+
earliest = Math.min(earliest, entry.deadline);
|
|
67
|
+
if (earliest === Infinity)
|
|
68
|
+
return;
|
|
69
|
+
this.timer = setTimeout(() => { this.timer = undefined; this.prune(); this.schedule(); }, Math.max(0, earliest - performance.now()));
|
|
70
|
+
this.timer.unref();
|
|
71
|
+
}
|
|
72
|
+
dispose() {
|
|
73
|
+
this.disposed = true;
|
|
74
|
+
this.entries.clear();
|
|
75
|
+
if (this.timer !== undefined)
|
|
76
|
+
clearTimeout(this.timer);
|
|
77
|
+
this.timer = undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { DatabaseTarget } from '../domain.js';
|
|
2
|
+
/** Canonical bytes and connection identity returned by the Host's preflight. */
|
|
3
|
+
export interface RedisWriteDraft {
|
|
4
|
+
readonly target: DatabaseTarget;
|
|
5
|
+
readonly host: string;
|
|
6
|
+
readonly port: number;
|
|
7
|
+
readonly type: 'string' | 'hash';
|
|
8
|
+
readonly keyTtl: 'none';
|
|
9
|
+
readonly fieldTtl: 'none' | null;
|
|
10
|
+
readonly keyBase64: string;
|
|
11
|
+
readonly fieldBase64: string | null;
|
|
12
|
+
readonly beforeBase64: string;
|
|
13
|
+
readonly afterBase64: string;
|
|
14
|
+
}
|
|
15
|
+
export interface RedisWriteProposal extends RedisWriteDraft {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly expiresAt: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ConsumedRedisProposal {
|
|
20
|
+
readonly proposal: RedisWriteProposal;
|
|
21
|
+
isCurrent(): boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Host-only, per-session write switch and short-lived one-shot proposal. */
|
|
24
|
+
export declare class RedisWriteProposals {
|
|
25
|
+
private readonly enabled;
|
|
26
|
+
private readonly turns;
|
|
27
|
+
private readonly entries;
|
|
28
|
+
private timer;
|
|
29
|
+
private disposed;
|
|
30
|
+
/** Called only from a user-controlled Host/client entry, never a model tool. */
|
|
31
|
+
enable(sessionId: string, target: DatabaseTarget): void;
|
|
32
|
+
disable(sessionId: string): void;
|
|
33
|
+
/** Only a trusted Host lifecycle event may establish the current turn. */
|
|
34
|
+
beginTurn(sessionId: string, turnId: string): symbol;
|
|
35
|
+
/** A late end event for an older turn must not remove a newer proposal. */
|
|
36
|
+
endTurn(sessionId: string, turnId: string): void;
|
|
37
|
+
isEnabled(sessionId: string, target: DatabaseTarget): boolean;
|
|
38
|
+
/** The token was captured before asynchronous preflight and cannot survive re-enabling. */
|
|
39
|
+
isCurrent(sessionId: string, turnId: string, token: symbol, target: DatabaseTarget): boolean;
|
|
40
|
+
/** Issuing another proposal invalidates the previous one in this session. */
|
|
41
|
+
issue(sessionId: string, turnId: string, token: symbol, draft: RedisWriteDraft): RedisWriteProposal;
|
|
42
|
+
/** Consume synchronously after the DSH policy permits this exact call. */
|
|
43
|
+
consume(sessionId: string, turnId: string, requested: RedisWriteProposal): ConsumedRedisProposal | null;
|
|
44
|
+
private schedule;
|
|
45
|
+
dispose(): void;
|
|
46
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { decodeWriteBytes } from "./write-bytes.js";
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
const MAX_SESSIONS = 64;
|
|
4
|
+
const LIFETIME_MS = 5 * 60_000;
|
|
5
|
+
function sameTarget(a, b) {
|
|
6
|
+
return a.id === b.id && a.generation === b.generation && a.db === b.db;
|
|
7
|
+
}
|
|
8
|
+
/** Reject noncanonical Base64 rather than silently changing byte identity. */
|
|
9
|
+
function sameProposal(a, b) {
|
|
10
|
+
return a.id === b.id && a.expiresAt === b.expiresAt && sameTarget(a.target, b.target)
|
|
11
|
+
&& a.host === b.host && a.port === b.port && a.type === b.type && a.keyTtl === b.keyTtl && a.fieldTtl === b.fieldTtl
|
|
12
|
+
&& a.keyBase64 === b.keyBase64 && a.fieldBase64 === b.fieldBase64
|
|
13
|
+
&& a.beforeBase64 === b.beforeBase64 && a.afterBase64 === b.afterBase64;
|
|
14
|
+
}
|
|
15
|
+
function copy(proposal) {
|
|
16
|
+
return { ...proposal, target: { ...proposal.target } };
|
|
17
|
+
}
|
|
18
|
+
/** Host-only, per-session write switch and short-lived one-shot proposal. */
|
|
19
|
+
export class RedisWriteProposals {
|
|
20
|
+
enabled = new Map();
|
|
21
|
+
turns = new Map();
|
|
22
|
+
entries = new Map();
|
|
23
|
+
timer;
|
|
24
|
+
disposed = false;
|
|
25
|
+
/** Called only from a user-controlled Host/client entry, never a model tool. */
|
|
26
|
+
enable(sessionId, target) {
|
|
27
|
+
if (this.disposed || sessionId.length === 0 || sessionId.length > 256)
|
|
28
|
+
throw new Error('Redis 写入会话不可用。');
|
|
29
|
+
if (!this.enabled.has(sessionId) && this.enabled.size >= MAX_SESSIONS)
|
|
30
|
+
throw new Error('Redis 写入会话已达到上限。');
|
|
31
|
+
this.entries.delete(sessionId);
|
|
32
|
+
this.turns.delete(sessionId);
|
|
33
|
+
this.enabled.set(sessionId, { ...target });
|
|
34
|
+
this.schedule();
|
|
35
|
+
}
|
|
36
|
+
disable(sessionId) {
|
|
37
|
+
this.enabled.delete(sessionId);
|
|
38
|
+
this.turns.delete(sessionId);
|
|
39
|
+
this.entries.delete(sessionId);
|
|
40
|
+
this.schedule();
|
|
41
|
+
}
|
|
42
|
+
/** Only a trusted Host lifecycle event may establish the current turn. */
|
|
43
|
+
beginTurn(sessionId, turnId) {
|
|
44
|
+
if (!this.enabled.has(sessionId) || turnId.length === 0 || turnId.length > 256)
|
|
45
|
+
throw new Error('Redis 写入轮次不可用。');
|
|
46
|
+
const previous = this.turns.get(sessionId);
|
|
47
|
+
if (previous?.id === turnId)
|
|
48
|
+
return previous.token;
|
|
49
|
+
const token = Symbol('redis-write-turn');
|
|
50
|
+
this.turns.set(sessionId, { id: turnId, token });
|
|
51
|
+
this.entries.delete(sessionId);
|
|
52
|
+
this.schedule();
|
|
53
|
+
return token;
|
|
54
|
+
}
|
|
55
|
+
/** A late end event for an older turn must not remove a newer proposal. */
|
|
56
|
+
endTurn(sessionId, turnId) {
|
|
57
|
+
if (this.turns.get(sessionId)?.id !== turnId)
|
|
58
|
+
return;
|
|
59
|
+
this.turns.delete(sessionId);
|
|
60
|
+
this.entries.delete(sessionId);
|
|
61
|
+
this.schedule();
|
|
62
|
+
}
|
|
63
|
+
isEnabled(sessionId, target) {
|
|
64
|
+
const enabled = this.enabled.get(sessionId);
|
|
65
|
+
return !this.disposed && enabled !== undefined && sameTarget(enabled, target);
|
|
66
|
+
}
|
|
67
|
+
/** The token was captured before asynchronous preflight and cannot survive re-enabling. */
|
|
68
|
+
isCurrent(sessionId, turnId, token, target) {
|
|
69
|
+
const turn = this.turns.get(sessionId);
|
|
70
|
+
return this.isEnabled(sessionId, target) && turn?.id === turnId && turn.token === token;
|
|
71
|
+
}
|
|
72
|
+
/** Issuing another proposal invalidates the previous one in this session. */
|
|
73
|
+
issue(sessionId, turnId, token, draft) {
|
|
74
|
+
if (!this.isEnabled(sessionId, draft.target))
|
|
75
|
+
throw new Error('Redis 写入未启用或连接已变化。');
|
|
76
|
+
const turn = this.turns.get(sessionId);
|
|
77
|
+
if (turn?.id !== turnId || turn.token !== token)
|
|
78
|
+
throw new Error('Redis 写入轮次已结束或变化。');
|
|
79
|
+
if (turnId.length === 0 || turnId.length > 256 || draft.host.length === 0 || draft.host.length > 1024 || !Number.isInteger(draft.port) || draft.port < 1 || draft.port > 65535
|
|
80
|
+
|| draft.keyTtl !== 'none' || decodeWriteBytes(draft.keyBase64) === null || decodeWriteBytes(draft.beforeBase64) === null || decodeWriteBytes(draft.afterBase64) === null
|
|
81
|
+
|| (draft.type === 'string' ? draft.fieldBase64 !== null || draft.fieldTtl !== null : draft.type !== 'hash' || draft.fieldTtl !== 'none' || draft.fieldBase64 === null || decodeWriteBytes(draft.fieldBase64) === null)) {
|
|
82
|
+
throw new Error('Redis 写入提案无效或超过限制。');
|
|
83
|
+
}
|
|
84
|
+
const proposal = { ...draft, target: { ...draft.target }, id: randomUUID(), expiresAt: new Date(Date.now() + LIFETIME_MS).toISOString() };
|
|
85
|
+
this.entries.set(sessionId, { proposal, turnId, token, deadline: performance.now() + LIFETIME_MS });
|
|
86
|
+
this.schedule();
|
|
87
|
+
return copy(proposal);
|
|
88
|
+
}
|
|
89
|
+
/** Consume synchronously after the DSH policy permits this exact call. */
|
|
90
|
+
consume(sessionId, turnId, requested) {
|
|
91
|
+
const entry = this.entries.get(sessionId);
|
|
92
|
+
if (entry === undefined || entry.deadline <= performance.now()) {
|
|
93
|
+
this.entries.delete(sessionId);
|
|
94
|
+
this.schedule();
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const turn = this.turns.get(sessionId);
|
|
98
|
+
if (entry.turnId !== turnId || turn?.id !== turnId || turn.token !== entry.token || !this.isEnabled(sessionId, entry.proposal.target) || !sameProposal(entry.proposal, requested))
|
|
99
|
+
return null;
|
|
100
|
+
this.entries.delete(sessionId);
|
|
101
|
+
this.schedule();
|
|
102
|
+
return { proposal: copy(entry.proposal), isCurrent: () => performance.now() < entry.deadline && this.isCurrent(sessionId, turnId, entry.token, entry.proposal.target) };
|
|
103
|
+
}
|
|
104
|
+
schedule() {
|
|
105
|
+
if (this.timer !== undefined)
|
|
106
|
+
clearTimeout(this.timer);
|
|
107
|
+
this.timer = undefined;
|
|
108
|
+
let earliest = Infinity;
|
|
109
|
+
for (const entry of this.entries.values())
|
|
110
|
+
earliest = Math.min(earliest, entry.deadline);
|
|
111
|
+
if (earliest === Infinity)
|
|
112
|
+
return;
|
|
113
|
+
this.timer = setTimeout(() => {
|
|
114
|
+
this.timer = undefined;
|
|
115
|
+
const now = performance.now();
|
|
116
|
+
for (const [session, entry] of this.entries)
|
|
117
|
+
if (entry.deadline <= now)
|
|
118
|
+
this.entries.delete(session);
|
|
119
|
+
this.schedule();
|
|
120
|
+
}, Math.max(0, earliest - performance.now()));
|
|
121
|
+
this.timer.unref();
|
|
122
|
+
}
|
|
123
|
+
dispose() {
|
|
124
|
+
this.disposed = true;
|
|
125
|
+
this.enabled.clear();
|
|
126
|
+
this.turns.clear();
|
|
127
|
+
this.entries.clear();
|
|
128
|
+
if (this.timer !== undefined)
|
|
129
|
+
clearTimeout(this.timer);
|
|
130
|
+
this.timer = undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ZSetReadRequest, type ZSetReadResult } from '../domain.js';
|
|
2
|
+
import type { RedisConnections } from './connection-service.js';
|
|
3
|
+
/** Fixed ascending ZRANGE WITHSCORES or one unordered ZSCAN, never a write path. */
|
|
4
|
+
export declare class RedisZSets {
|
|
5
|
+
private readonly connections;
|
|
6
|
+
private readonly retryCancelled;
|
|
7
|
+
private readonly scans;
|
|
8
|
+
constructor(connections: RedisConnections, retryCancelled?: boolean);
|
|
9
|
+
disposeSession(sessionId: string): void;
|
|
10
|
+
dispose(): void;
|
|
11
|
+
read(sessionId: string, request: ZSetReadRequest, signal: AbortSignal): Promise<ZSetReadResult>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { MAX_SCAN_KEYS, MAX_SCAN_JSON_BYTES } from "../domain.js";
|
|
2
|
+
import { RedisFrameError } from "./resp.js";
|
|
3
|
+
import { RedisReadContext } from "./read-context.js";
|
|
4
|
+
import { RedisScanCursors, scanReply } from "./scan-cursors.js";
|
|
5
|
+
import { zsetEntries } from "./zset-values.js";
|
|
6
|
+
const MAX_INTEGER = (1n << 63n) - 1n;
|
|
7
|
+
/** Fixed ascending ZRANGE WITHSCORES or one unordered ZSCAN, never a write path. */
|
|
8
|
+
export class RedisZSets {
|
|
9
|
+
connections;
|
|
10
|
+
retryCancelled;
|
|
11
|
+
scans = new RedisScanCursors();
|
|
12
|
+
constructor(connections, retryCancelled = false) {
|
|
13
|
+
this.connections = connections;
|
|
14
|
+
this.retryCancelled = retryCancelled;
|
|
15
|
+
}
|
|
16
|
+
disposeSession(sessionId) { this.scans.disposeSession(sessionId); }
|
|
17
|
+
dispose() { this.scans.dispose(); }
|
|
18
|
+
async read(sessionId, request, signal) {
|
|
19
|
+
let scan;
|
|
20
|
+
const context = new RedisReadContext(this.connections, sessionId, request.target, signal, () => { if (scan !== undefined)
|
|
21
|
+
this.scans.validate(sessionId, scan); });
|
|
22
|
+
const result = (state, error = null, metadata = null) => ({ state, error, metadata, mode: request.mode, entries: [], offset: request.mode === 'range' ? request.offset : null, nextOffset: null, cursor: null, sampledAt: new Date().toISOString() });
|
|
23
|
+
try {
|
|
24
|
+
signal.throwIfAborted();
|
|
25
|
+
this.connections.assertTarget(sessionId, request.target);
|
|
26
|
+
if (request.mode === 'scan')
|
|
27
|
+
scan = this.scans.begin(sessionId, JSON.stringify([request.target.id, request.target.generation, request.target.db, request.keyBase64, request.pattern]), request.cursor);
|
|
28
|
+
else
|
|
29
|
+
this.scans.disposeSession(sessionId);
|
|
30
|
+
const key = Buffer.from(request.keyBase64, 'base64'), firstType = await context.type(key);
|
|
31
|
+
if (firstType === 'none')
|
|
32
|
+
return result('missing');
|
|
33
|
+
const firstTtl = await context.ttl(key);
|
|
34
|
+
if (firstTtl === -2n)
|
|
35
|
+
return result('missing');
|
|
36
|
+
if (firstType !== 'zset')
|
|
37
|
+
return result('unsupported', null, { type: firstType, ttlMs: String(firstTtl), length: null });
|
|
38
|
+
const length = await context.integer('ZCARD', [key]);
|
|
39
|
+
let values, cursor = '0';
|
|
40
|
+
if (request.mode === 'range') {
|
|
41
|
+
const wantedEnd = BigInt(request.offset) + BigInt(request.count) - 1n;
|
|
42
|
+
const reply = await context.command('ZRANGE', [key, request.offset, String(wantedEnd > MAX_INTEGER ? MAX_INTEGER : wantedEnd), 'WITHSCORES']);
|
|
43
|
+
if (!Array.isArray(reply))
|
|
44
|
+
throw new RedisFrameError('protocol');
|
|
45
|
+
values = reply;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
if (scan === undefined)
|
|
49
|
+
throw new RedisFrameError('protocol');
|
|
50
|
+
const page = scanReply(await context.command('ZSCAN', [key, scan.cursor, 'MATCH', request.pattern, 'COUNT', '100']));
|
|
51
|
+
values = page.values;
|
|
52
|
+
cursor = page.cursor;
|
|
53
|
+
}
|
|
54
|
+
const rows = zsetEntries(values, request.mode === 'range' ? request.offset : null, request.mode === 'range' ? request.count : MAX_SCAN_KEYS);
|
|
55
|
+
const secondType = await context.type(key);
|
|
56
|
+
if (secondType === 'none')
|
|
57
|
+
return result('missing');
|
|
58
|
+
if (secondType !== 'zset')
|
|
59
|
+
return result('changed');
|
|
60
|
+
const secondLength = await context.integer('ZCARD', [key]), ttl = await context.ttl(key);
|
|
61
|
+
if (ttl === -2n)
|
|
62
|
+
return result('missing');
|
|
63
|
+
if (length !== secondLength || BigInt(rows.length) > length)
|
|
64
|
+
return result('changed');
|
|
65
|
+
let nextOffset = null;
|
|
66
|
+
if (request.mode === 'range') {
|
|
67
|
+
const available = length > BigInt(request.offset) ? length - BigInt(request.offset) : 0n;
|
|
68
|
+
if (BigInt(rows.length) !== (available < BigInt(request.count) ? available : BigInt(request.count)))
|
|
69
|
+
return result('changed');
|
|
70
|
+
const next = BigInt(request.offset) + BigInt(rows.length);
|
|
71
|
+
nextOffset = rows.length > 0 && next < length ? String(next) : null;
|
|
72
|
+
}
|
|
73
|
+
context.signal.throwIfAborted();
|
|
74
|
+
const page = { ...result('page', null, { type: 'zset', ttlMs: String(ttl), length: String(secondLength) }), entries: rows, nextOffset };
|
|
75
|
+
if (Buffer.byteLength(JSON.stringify(page)) + 36 > MAX_SCAN_JSON_BYTES)
|
|
76
|
+
throw new RedisFrameError('oversize');
|
|
77
|
+
if (scan !== undefined) {
|
|
78
|
+
const token = this.scans.advance(sessionId, scan, cursor);
|
|
79
|
+
scan = undefined;
|
|
80
|
+
return { ...page, cursor: token };
|
|
81
|
+
}
|
|
82
|
+
return page;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
const code = context.error(error);
|
|
86
|
+
// Only AI ZSCAN retries an explicitly cancelled, still-current cursor.
|
|
87
|
+
if (this.retryCancelled && signal.aborted && request.mode === 'scan' && scan !== undefined) {
|
|
88
|
+
try {
|
|
89
|
+
this.connections.assertTarget(sessionId, request.target);
|
|
90
|
+
if (this.scans.restore(sessionId, scan, request.cursor))
|
|
91
|
+
scan = undefined;
|
|
92
|
+
}
|
|
93
|
+
catch { /* target changed */ }
|
|
94
|
+
}
|
|
95
|
+
return code === 'type-changed' ? result('changed') : result('failed', code);
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
if (scan !== undefined)
|
|
99
|
+
this.scans.discard(sessionId, scan);
|
|
100
|
+
context.dispose();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ZSetEntry } from '../domain.js';
|
|
2
|
+
import { type RedisReply } from './resp.js';
|
|
3
|
+
/** Shared complete member/score decoding for ZSet pages and explicit GEO ranges.
|
|
4
|
+
* Scan duplicates update their score; rank ranges must contain unique members.
|
|
5
|
+
*/
|
|
6
|
+
export declare function zsetEntries(values: readonly RedisReply[], offset: string | null, limit: number): readonly ZSetEntry[];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MAX_KEY_BYTES } from "../domain.js";
|
|
2
|
+
import { isRedisScore } from "../redis-score.js";
|
|
3
|
+
import { byteIdentity } from "./byte-identity.js";
|
|
4
|
+
import { RedisFrameError } from "./resp.js";
|
|
5
|
+
/** Shared complete member/score decoding for ZSet pages and explicit GEO ranges.
|
|
6
|
+
* Scan duplicates update their score; rank ranges must contain unique members.
|
|
7
|
+
*/
|
|
8
|
+
export function zsetEntries(values, offset, limit) {
|
|
9
|
+
if (values.length % 2 !== 0)
|
|
10
|
+
throw new RedisFrameError('protocol');
|
|
11
|
+
if (values.length / 2 > limit)
|
|
12
|
+
throw new RedisFrameError(offset === null ? 'oversize' : 'protocol');
|
|
13
|
+
const rows = new Map();
|
|
14
|
+
for (let index = 0; index < values.length; index += 2) {
|
|
15
|
+
const raw = values[index], rawScore = values[index + 1];
|
|
16
|
+
if (!Buffer.isBuffer(raw) || !Buffer.isBuffer(rawScore))
|
|
17
|
+
throw new RedisFrameError('protocol');
|
|
18
|
+
if (raw.length > MAX_KEY_BYTES)
|
|
19
|
+
throw new RedisFrameError('oversize');
|
|
20
|
+
const member = byteIdentity(raw), score = rawScore.toString('utf8');
|
|
21
|
+
if (!isRedisScore(score) || (offset !== null && rows.has(member.base64)))
|
|
22
|
+
throw new RedisFrameError('protocol');
|
|
23
|
+
rows.set(member.base64, { member, score, rank: offset === null ? null : String(BigInt(offset) + BigInt(index / 2)) });
|
|
24
|
+
}
|
|
25
|
+
return [...rows.values()];
|
|
26
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Loader entry. Browser modules are discovered separately by DSH. */
|
|
2
|
+
export declare const name = "@whiteplusms/dsh-redis";
|
|
3
|
+
export declare const inject: string[];
|
|
4
|
+
export { apply } from './runtime.js';
|
|
5
|
+
export { RedisRemoteService } from './remote-service.js';
|
|
6
|
+
export type * from './domain.js';
|