@c9up/echo 0.2.3 → 0.2.4
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/dist/CacheManager.d.ts +2 -0
- package/dist/CacheManager.d.ts.map +1 -1
- package/dist/CacheManager.js +92 -32
- package/dist/CacheManager.js.map +1 -1
- package/dist/EchoProvider.d.ts.map +1 -1
- package/dist/EchoProvider.js +32 -3
- package/dist/EchoProvider.js.map +1 -1
- package/dist/StoreManager.d.ts +28 -9
- package/dist/StoreManager.d.ts.map +1 -1
- package/dist/StoreManager.js +42 -18
- package/dist/StoreManager.js.map +1 -1
- package/dist/bytes.d.ts +13 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +35 -0
- package/dist/bytes.js.map +1 -0
- package/dist/claim.d.ts +15 -0
- package/dist/claim.d.ts.map +1 -0
- package/dist/claim.js +10 -0
- package/dist/claim.js.map +1 -0
- package/dist/configure.d.ts +1 -0
- package/dist/configure.d.ts.map +1 -1
- package/dist/configure.js +3 -0
- package/dist/configure.js.map +1 -1
- package/dist/console/cacheClear.d.ts +13 -0
- package/dist/console/cacheClear.d.ts.map +1 -0
- package/dist/console/cacheClear.js +69 -0
- package/dist/console/cacheClear.js.map +1 -0
- package/dist/console/cacheDelete.d.ts +11 -0
- package/dist/console/cacheDelete.d.ts.map +1 -0
- package/dist/console/cacheDelete.js +42 -0
- package/dist/console/cacheDelete.js.map +1 -0
- package/dist/console/cachePrune.d.ts +9 -0
- package/dist/console/cachePrune.d.ts.map +1 -0
- package/dist/console/cachePrune.js +35 -0
- package/dist/console/cachePrune.js.map +1 -0
- package/dist/console/contract.d.ts +64 -0
- package/dist/console/contract.d.ts.map +1 -0
- package/dist/console/contract.js +32 -0
- package/dist/console/contract.js.map +1 -0
- package/dist/console/index.d.ts +25 -0
- package/dist/console/index.d.ts.map +1 -0
- package/dist/console/index.js +38 -0
- package/dist/console/index.js.map +1 -0
- package/dist/console/store.d.ts +14 -0
- package/dist/console/store.d.ts.map +1 -0
- package/dist/console/store.js +35 -0
- package/dist/console/store.js.map +1 -0
- package/dist/drivers/MemoryDriver.d.ts +36 -1
- package/dist/drivers/MemoryDriver.d.ts.map +1 -1
- package/dist/drivers/MemoryDriver.js +157 -62
- package/dist/drivers/MemoryDriver.js.map +1 -1
- package/dist/drivers/RedisDriver.d.ts +13 -8
- package/dist/drivers/RedisDriver.d.ts.map +1 -1
- package/dist/drivers/RedisDriver.js +171 -94
- package/dist/drivers/RedisDriver.js.map +1 -1
- package/dist/drivers/TieredDriver.d.ts +42 -0
- package/dist/drivers/TieredDriver.d.ts.map +1 -1
- package/dist/drivers/TieredDriver.js +114 -9
- package/dist/drivers/TieredDriver.js.map +1 -1
- package/dist/duration.d.ts +13 -3
- package/dist/duration.d.ts.map +1 -1
- package/dist/duration.js +22 -7
- package/dist/duration.js.map +1 -1
- package/dist/errors.d.ts +60 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +81 -6
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/quasar.d.ts +1 -1
- package/dist/quasar.d.ts.map +1 -1
- package/dist/quasar.js +27 -6
- package/dist/quasar.js.map +1 -1
- package/dist/services/main.d.ts +1 -1
- package/dist/services/main.d.ts.map +1 -1
- package/dist/services/main.js +22 -5
- package/dist/services/main.js.map +1 -1
- package/dist/types.d.ts +12 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.d.ts +26 -3
- package/dist/vendor/quasarConnection.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.js +44 -23
- package/dist/vendor/quasarConnection.js.map +1 -1
- package/package.json +5 -1
- package/src/CacheManager.ts +117 -41
- package/src/EchoProvider.ts +45 -4
- package/src/StoreManager.ts +86 -31
- package/src/bytes.ts +38 -0
- package/src/claim.ts +22 -0
- package/src/configure.ts +4 -0
- package/src/console/cacheClear.ts +96 -0
- package/src/console/cacheDelete.ts +65 -0
- package/src/console/cachePrune.ts +57 -0
- package/src/console/contract.ts +93 -0
- package/src/console/index.ts +61 -0
- package/src/console/store.ts +47 -0
- package/src/drivers/MemoryDriver.ts +205 -60
- package/src/drivers/RedisDriver.ts +205 -101
- package/src/drivers/TieredDriver.ts +169 -9
- package/src/duration.ts +28 -7
- package/src/errors.ts +103 -6
- package/src/index.ts +17 -3
- package/src/quasar.ts +44 -8
- package/src/services/main.ts +26 -8
- package/src/types.ts +13 -2
- package/src/vendor/quasarConnection.ts +84 -22
package/src/types.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { Duration } from "./duration.js";
|
|
7
|
-
import type { FactoryError } from "./errors.js";
|
|
7
|
+
import type { FactoryError, LockTimeoutError } from "./errors.js";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* A driver-level entry. `stale` is `true` when the value is past its logical
|
|
@@ -20,6 +20,13 @@ export interface CacheEntry<T = unknown> {
|
|
|
20
20
|
* promotes an entry between tiers instead of resetting it to immortal.
|
|
21
21
|
*/
|
|
22
22
|
expiresAt?: number;
|
|
23
|
+
/**
|
|
24
|
+
* The tags the entry was written with, when the driver keeps them. A
|
|
25
|
+
* composing driver needs them to promote an entry between tiers: a
|
|
26
|
+
* promoted copy written without its tags was invisible to `deleteByTag`
|
|
27
|
+
* on the very instance that promoted it.
|
|
28
|
+
*/
|
|
29
|
+
tags?: string[];
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
/** Options for the grace-aware {@link CacheDriver.setEntry}. */
|
|
@@ -144,7 +151,11 @@ export interface GetOrSetOptions<T> {
|
|
|
144
151
|
timeout?: Duration;
|
|
145
152
|
/** Hard timeout: reject with {@link TimeoutError} if the factory exceeds it (even without a stale fallback). */
|
|
146
153
|
hardTimeout?: Duration;
|
|
147
|
-
/**
|
|
154
|
+
/**
|
|
155
|
+
* Max time to wait on another caller's in-flight factory for the same key
|
|
156
|
+
* (single-flight lock). Past it the stale value is returned when there is
|
|
157
|
+
* one, and a {@link LockTimeoutError} is thrown when there is not.
|
|
158
|
+
*/
|
|
148
159
|
lockTimeout?: Duration;
|
|
149
160
|
tags?: string[];
|
|
150
161
|
/** Observe factory failures (foreground and background). */
|
|
@@ -31,23 +31,62 @@ interface ConnectionSource {
|
|
|
31
31
|
* `"connection" in manager` is FALSE while reading it returns the function —
|
|
32
32
|
* gating on `in` rejected the real manager. And a module namespace under a
|
|
33
33
|
* test double RAISES for an export it does not have, rather than answering
|
|
34
|
-
* undefined, so a bare read fails on the mock.
|
|
35
|
-
*
|
|
34
|
+
* undefined, so a bare read fails on the mock.
|
|
35
|
+
*
|
|
36
|
+
* So the read is caught, but what it raised is kept: on quasar's accessor a
|
|
37
|
+
* throw is not an absence, it is the accessor saying its provider has not
|
|
38
|
+
* booted, and dropping that turned it into a wrong diagnosis.
|
|
36
39
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
type MemberRead = { value: unknown } | { threw: unknown };
|
|
41
|
+
|
|
42
|
+
function readMember(value: unknown, name: string): MemberRead {
|
|
43
|
+
if (typeof value !== "object" || value === null) return { value: undefined };
|
|
39
44
|
try {
|
|
40
|
-
return Reflect.get(value, name);
|
|
41
|
-
} catch {
|
|
42
|
-
return
|
|
45
|
+
return { value: Reflect.get(value, name) };
|
|
46
|
+
} catch (threw) {
|
|
47
|
+
return { threw };
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
function memberValue(read: MemberRead): unknown {
|
|
52
|
+
return "value" in read ? read.value : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
46
55
|
function isConnectionSource(value: unknown): value is ConnectionSource {
|
|
47
|
-
return typeof readMember(value, "connection") === "function";
|
|
56
|
+
return typeof memberValue(readMember(value, "connection")) === "function";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The commands among `commands` that `value` does not carry as functions.
|
|
61
|
+
*
|
|
62
|
+
* Reads go through the same catch as above, so a command whose read throws
|
|
63
|
+
* counts as missing.
|
|
64
|
+
*/
|
|
65
|
+
export function missingCommands(
|
|
66
|
+
value: unknown,
|
|
67
|
+
commands: readonly string[],
|
|
68
|
+
): string[] {
|
|
69
|
+
return commands.filter(
|
|
70
|
+
(command) => typeof memberValue(readMember(value, command)) !== "function",
|
|
71
|
+
);
|
|
48
72
|
}
|
|
49
73
|
|
|
50
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Whether `value` carries every one of `commands` as a function.
|
|
76
|
+
*
|
|
77
|
+
* The building block of the guard a caller passes as `isConnection`, over the
|
|
78
|
+
* same list it passes as `required` — which is what names the gap when the
|
|
79
|
+
* guard says no.
|
|
80
|
+
*/
|
|
81
|
+
export function hasCommands(
|
|
82
|
+
value: unknown,
|
|
83
|
+
commands: readonly string[],
|
|
84
|
+
): value is object {
|
|
85
|
+
if (typeof value !== "object" || value === null) return false;
|
|
86
|
+
return missingCommands(value, commands).length === 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface QuasarConnectionRequest<T> {
|
|
51
90
|
/** The package asking. It prefixes every message this can raise. */
|
|
52
91
|
pkg: string;
|
|
53
92
|
/**
|
|
@@ -65,8 +104,16 @@ export interface QuasarConnectionRequest {
|
|
|
65
104
|
) => Error;
|
|
66
105
|
/** Connection name, or `undefined` for quasar's default. */
|
|
67
106
|
name?: string;
|
|
68
|
-
/** The commands the caller will actually issue. */
|
|
107
|
+
/** The commands the caller will actually issue, named in the message. */
|
|
69
108
|
required: readonly string[];
|
|
109
|
+
/**
|
|
110
|
+
* The caller's check that the connection is the client it needs.
|
|
111
|
+
*
|
|
112
|
+
* Written by the caller over the commands it issues (`hasCommands` with
|
|
113
|
+
* `required` does it), because only the caller knows its client type. The
|
|
114
|
+
* connection is handed over only once this has said yes.
|
|
115
|
+
*/
|
|
116
|
+
isConnection: (value: unknown) => value is T;
|
|
70
117
|
/** What the caller does with them, for the message that names a gap. */
|
|
71
118
|
what: string;
|
|
72
119
|
/**
|
|
@@ -86,9 +133,10 @@ export interface QuasarConnectionRequest {
|
|
|
86
133
|
* command, far from the line that asked for it.
|
|
87
134
|
*/
|
|
88
135
|
export async function quasarConnection<T>(
|
|
89
|
-
request: QuasarConnectionRequest
|
|
136
|
+
request: QuasarConnectionRequest<T>,
|
|
90
137
|
): Promise<T> {
|
|
91
|
-
const { pkg, name, required, what, raise, alternative } =
|
|
138
|
+
const { pkg, name, required, what, raise, alternative, isConnection } =
|
|
139
|
+
request;
|
|
92
140
|
const specifier = "@c9up/quasar/services/main";
|
|
93
141
|
let loaded: unknown;
|
|
94
142
|
try {
|
|
@@ -102,9 +150,22 @@ export async function quasarConnection<T>(
|
|
|
102
150
|
);
|
|
103
151
|
}
|
|
104
152
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
153
|
+
// The namespace itself may be the manager (a test double); a throw there is
|
|
154
|
+
// the mock refusing an export it lacks, not an answer.
|
|
155
|
+
const direct = memberValue(readMember(loaded, "connection"));
|
|
156
|
+
const manager =
|
|
157
|
+
typeof direct === "function"
|
|
158
|
+
? loaded
|
|
159
|
+
: memberValue(readMember(Object(loaded), "default"));
|
|
160
|
+
const read = readMember(manager, "connection");
|
|
161
|
+
if ("threw" in read) {
|
|
162
|
+
throw raise(
|
|
163
|
+
"quasar-missing",
|
|
164
|
+
`[${pkg}] ${what} asks for the quasar connection "${name ?? "default"}", but @c9up/quasar's manager is not available yet: ` +
|
|
165
|
+
"its provider has not booted, or is not registered in this application's providers.",
|
|
166
|
+
read.threw,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
108
169
|
if (!isConnectionSource(manager)) {
|
|
109
170
|
throw raise(
|
|
110
171
|
"quasar-missing",
|
|
@@ -113,17 +174,18 @@ export async function quasarConnection<T>(
|
|
|
113
174
|
}
|
|
114
175
|
|
|
115
176
|
const connection = manager.connection(name);
|
|
116
|
-
const missing = required
|
|
117
|
-
(command) => typeof readMember(connection, command) !== "function",
|
|
118
|
-
);
|
|
177
|
+
const missing = missingCommands(connection, required);
|
|
119
178
|
if (missing.length > 0) {
|
|
120
179
|
throw raise(
|
|
121
180
|
"incomplete-connection",
|
|
122
181
|
`[${pkg}] the quasar connection${name ? ` '${name}'` : ""} is missing ${missing.join(", ")}, which ${what} issues`,
|
|
123
182
|
);
|
|
124
183
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
184
|
+
if (!isConnection(connection)) {
|
|
185
|
+
throw raise(
|
|
186
|
+
"incomplete-connection",
|
|
187
|
+
`[${pkg}] the quasar connection${name ? ` '${name}'` : ""} is not the client ${what} needs`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
return connection;
|
|
129
191
|
}
|