@cosmicdrift/kumiko-bundled-features 0.410.0 → 0.412.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/dist/agent-tools/agent-doc-lint.d.ts +9 -0
- package/dist/agent-tools/agent-doc-lint.js +42 -5
- package/dist/agent-tools/agent-manifest.d.ts +0 -1
- package/dist/agent-tools/agent-manifest.js +9 -99
- package/dist/agent-tools/api-docs/anchors.d.ts +14 -0
- package/dist/agent-tools/api-docs/anchors.js +49 -0
- package/dist/agent-tools/api-docs/check.d.ts +7 -0
- package/dist/agent-tools/api-docs/check.js +15 -0
- package/dist/agent-tools/api-docs/collect-events.d.ts +10 -0
- package/dist/agent-tools/api-docs/collect-events.js +90 -0
- package/dist/agent-tools/api-docs/collect.d.ts +15 -0
- package/dist/agent-tools/api-docs/collect.js +108 -0
- package/dist/agent-tools/api-docs/curl.d.ts +5 -0
- package/dist/agent-tools/api-docs/curl.js +24 -0
- package/dist/agent-tools/api-docs/error-table.d.ts +9 -0
- package/dist/agent-tools/api-docs/error-table.js +31 -0
- package/dist/agent-tools/api-docs/examples.d.ts +16 -0
- package/dist/agent-tools/api-docs/examples.js +25 -0
- package/dist/agent-tools/api-docs/generate.d.ts +19 -0
- package/dist/agent-tools/api-docs/generate.js +104 -0
- package/dist/agent-tools/api-docs/index-json.d.ts +15 -0
- package/dist/agent-tools/api-docs/index-json.js +6 -0
- package/dist/agent-tools/api-docs/index.d.ts +9 -0
- package/dist/agent-tools/api-docs/index.js +6 -0
- package/dist/agent-tools/api-docs/list-parameters.d.ts +61 -0
- package/dist/agent-tools/api-docs/list-parameters.js +28 -0
- package/dist/agent-tools/api-docs/markdown.d.ts +29 -0
- package/dist/agent-tools/api-docs/markdown.js +48 -0
- package/dist/agent-tools/api-docs/menu-path.d.ts +8 -0
- package/dist/agent-tools/api-docs/menu-path.js +60 -0
- package/dist/agent-tools/api-docs/model.d.ts +76 -0
- package/dist/agent-tools/api-docs/model.js +1 -0
- package/dist/agent-tools/api-docs/problems.d.ts +32 -0
- package/dist/agent-tools/api-docs/problems.js +28 -0
- package/dist/agent-tools/api-docs/render-events.d.ts +12 -0
- package/dist/agent-tools/api-docs/render-events.js +55 -0
- package/dist/agent-tools/api-docs/render-openapi.d.ts +10 -0
- package/dist/agent-tools/api-docs/render-openapi.js +170 -0
- package/dist/agent-tools/api-docs/render-overview.d.ts +11 -0
- package/dist/agent-tools/api-docs/render-overview.js +60 -0
- package/dist/agent-tools/api-docs/render-reference.d.ts +14 -0
- package/dist/agent-tools/api-docs/render-reference.js +50 -0
- package/dist/agent-tools/api-docs/render-tokens.d.ts +12 -0
- package/dist/agent-tools/api-docs/render-tokens.js +101 -0
- package/dist/agent-tools/api-docs/schema-fields.d.ts +16 -0
- package/dist/agent-tools/api-docs/schema-fields.js +141 -0
- package/dist/agent-tools/api-docs/section-ids.d.ts +25 -0
- package/dist/agent-tools/api-docs/section-ids.js +26 -0
- package/dist/agent-tools/api-docs/system-only.d.ts +5 -0
- package/dist/agent-tools/api-docs/system-only.js +7 -0
- package/dist/agent-tools/api-docs/texts/de.d.ts +123 -0
- package/dist/agent-tools/api-docs/texts/de.js +123 -0
- package/dist/agent-tools/api-docs/texts/en.d.ts +123 -0
- package/dist/agent-tools/api-docs/texts/en.js +123 -0
- package/dist/agent-tools/api-docs/texts/es.d.ts +123 -0
- package/dist/agent-tools/api-docs/texts/es.js +123 -0
- package/dist/agent-tools/api-docs/texts/fr.d.ts +123 -0
- package/dist/agent-tools/api-docs/texts/fr.js +123 -0
- package/dist/agent-tools/api-docs/texts/index.d.ts +14 -0
- package/dist/agent-tools/api-docs/texts/index.js +55 -0
- package/dist/agent-tools/api-docs/texts/it.d.ts +123 -0
- package/dist/agent-tools/api-docs/texts/it.js +123 -0
- package/dist/agent-tools/api-docs/texts/types.d.ts +18 -0
- package/dist/agent-tools/api-docs/texts/types.js +104 -0
- package/dist/agent-tools/api-docs/ui-writes.d.ts +12 -0
- package/dist/agent-tools/api-docs/ui-writes.js +73 -0
- package/dist/agent-tools/handler-schema.d.ts +5 -0
- package/dist/agent-tools/handler-schema.js +37 -0
- package/dist/agent-tools/index.d.ts +2 -1
- package/dist/agent-tools/index.js +2 -1
- package/dist/agent-tools/labels.d.ts +26 -0
- package/dist/agent-tools/labels.js +131 -0
- package/dist/auth-email-password/seeding.d.ts +17 -2
- package/dist/auth-email-password/seeding.js +68 -6
- package/dist/auth-email-password/web/i18n.js +1 -0
- package/dist/auth-email-password/web/signup-screen.d.ts +1 -1
- package/dist/auth-email-password/web/signup-screen.js +20 -2
- package/dist/auth-email-password/web/static-account.d.ts +40 -0
- package/dist/auth-email-password/web/static-account.js +94 -0
- package/dist/auth-email-password/web/static-client.d.ts +38 -0
- package/dist/auth-email-password/web/static-client.js +169 -0
- package/dist/foundation-shared/demo-admin-env.d.ts +4 -0
- package/dist/foundation-shared/demo-admin-env.js +7 -0
- package/dist/foundation-shared/index.d.ts +1 -1
- package/dist/foundation-shared/index.js +1 -1
- package/dist/inbound-mail-foundation/entities.d.ts +84 -0
- package/dist/inbound-mail-foundation/entities.js +96 -71
- package/dist/inbound-mail-foundation/feature.js +2 -2
- package/dist/inbound-provider-imap/imap-client.d.ts +1 -1
- package/dist/inbound-provider-imap/imap-client.js +3 -1
- package/dist/personal-access-tokens/feature.d.ts +1 -0
- package/dist/personal-access-tokens/feature.js +4 -2
- package/dist/personal-access-tokens/handlers/create.write.js +2 -2
- package/dist/personal-access-tokens/i18n.js +5 -1
- package/dist/personal-access-tokens/index.d.ts +4 -2
- package/dist/personal-access-tokens/index.js +29 -2
- package/dist/personal-access-tokens/scope-events-check.d.ts +3 -0
- package/dist/personal-access-tokens/scope-events-check.js +18 -0
- package/dist/personal-access-tokens/screens.js +8 -2
- package/dist/personal-access-tokens/web/constants.d.ts +1 -0
- package/dist/personal-access-tokens/web/constants.js +1 -0
- package/dist/personal-access-tokens/web/scopes.d.ts +2 -0
- package/dist/personal-access-tokens/web/scopes.js +12 -0
- package/dist/presence/feature.d.ts +3 -0
- package/dist/presence/feature.js +20 -0
- package/dist/presence/handlers/heartbeat.write.d.ts +12 -0
- package/dist/presence/handlers/heartbeat.write.js +31 -0
- package/dist/presence/handlers/leave.write.d.ts +8 -0
- package/dist/presence/handlers/leave.write.js +33 -0
- package/dist/presence/handlers/list.query.d.ts +7 -0
- package/dist/presence/handlers/list.query.js +28 -0
- package/dist/presence/index.d.ts +3 -0
- package/dist/presence/index.js +2 -0
- package/dist/presence/options.d.ts +17 -0
- package/dist/presence/options.js +13 -0
- package/dist/presence/payloads.d.ts +18 -0
- package/dist/presence/payloads.js +18 -0
- package/dist/presence/room.d.ts +13 -0
- package/dist/presence/room.js +35 -0
- package/dist/presence/store.d.ts +32 -0
- package/dist/presence/store.js +140 -0
- package/dist/sessions/feature.js +2 -2
- package/dist/sessions/handlers/last-activity-probe.route.d.ts +2 -0
- package/dist/sessions/handlers/last-activity-probe.route.js +14 -0
- package/dist/tenant-inactivity/check-inactive-tenants.js +12 -6
- package/dist/tenant-inactivity/constants.d.ts +1 -1
- package/dist/tenant-inactivity/constants.js +3 -1
- package/dist/tenant-inactivity/handlers/inactivity-state.query.js +2 -1
- package/dist/tenant-inactivity/handlers/keep-account.write.js +2 -1
- package/dist/tenant-inactivity/options.d.ts +1 -0
- package/dist/tenant-inactivity/remote-activity.d.ts +3 -1
- package/dist/tenant-inactivity/remote-activity.js +4 -1
- package/dist/tenant-inactivity/static-keep-account.d.ts +6 -0
- package/dist/tenant-inactivity/static-keep-account.js +15 -0
- package/dist/tenant-lifecycle/constants.d.ts +1 -0
- package/dist/tenant-lifecycle/constants.js +1 -0
- package/dist/tenant-lifecycle/handlers/cancel-destruction.write.js +2 -2
- package/dist/tenant-lifecycle/handlers/request-destruction.write.js +2 -1
- package/dist/tenant-lifecycle/index.d.ts +1 -1
- package/dist/tenant-lifecycle/index.js +1 -1
- package/dist/waitlist/static-submit.d.ts +15 -0
- package/dist/waitlist/static-submit.js +24 -0
- package/package.json +59 -11
- package/src/agent-tools/changes.json +24 -0
- package/src/auth-email-password/changes.json +24 -0
- package/src/inbound-mail-foundation/changes.json +6 -0
- package/src/inbound-provider-imap/changes.json +6 -0
- package/src/personal-access-tokens/changes.json +6 -0
- package/src/presence/changes.json +8 -0
- package/src/tenant-inactivity/changes.json +12 -0
- package/src/waitlist/changes.json +6 -0
|
@@ -6,8 +6,35 @@ export { PAT_REVOKED_AGGREGATE_TYPE, PAT_REVOKED_EVENT_QN, PAT_REVOKED_EVENT_SHO
|
|
|
6
6
|
export { createPatResolver } from "./resolver.js";
|
|
7
7
|
export { revokeAllPatTokensForUser } from "./revoke-for-user.js";
|
|
8
8
|
export { apiTokenEntity, apiTokenTable } from "./schema/api-token.js";
|
|
9
|
-
export { PAT_DEFAULT_RATE_LIMIT, PAT_FEATURE, PatHandlers, PatQueries } from "./web/constants.js";
|
|
10
|
-
export { expandScopes } from "./web/scopes.js";
|
|
9
|
+
export { PAT_DEFAULT_EXPIRES_IN_DAYS, PAT_DEFAULT_RATE_LIMIT, PAT_FEATURE, PAT_MAX_EXPIRES_IN_DAYS, PAT_SCREEN_ID, PatHandlers, PatQueries, } from "./web/constants.js";
|
|
10
|
+
export { expandScopeEvents, expandScopes } from "./web/scopes.js";
|
|
11
|
+
function isStringArray(value) {
|
|
12
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
13
|
+
}
|
|
14
|
+
function isPatScopeDef(value) {
|
|
15
|
+
if (!value || typeof value !== "object")
|
|
16
|
+
return false;
|
|
17
|
+
if (!("label" in value) || typeof value.label !== "string")
|
|
18
|
+
return false;
|
|
19
|
+
if (!("read" in value) || !isStringArray(value.read))
|
|
20
|
+
return false;
|
|
21
|
+
if ("write" in value && value.write !== undefined && !isStringArray(value.write))
|
|
22
|
+
return false;
|
|
23
|
+
return !("events" in value) || value.events === undefined || isStringArray(value.events);
|
|
24
|
+
}
|
|
25
|
+
function isPatScopeConfig(value) {
|
|
26
|
+
return !!value && typeof value === "object" && Object.values(value).every(isPatScopeDef);
|
|
27
|
+
}
|
|
28
|
+
// Reads the scope catalog off a mounted PAT feature's exports; undefined for any other feature.
|
|
29
|
+
export function patScopesFromFeature(feature) {
|
|
30
|
+
const exports = feature.exports;
|
|
31
|
+
if (exports && typeof exports === "object" && "scopes" in exports) {
|
|
32
|
+
const { scopes } = exports;
|
|
33
|
+
if (isPatScopeConfig(scopes))
|
|
34
|
+
return scopes;
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
11
38
|
// Reads the per-token rate-limit config off a mounted PAT feature's exports.
|
|
12
39
|
// Falls back to the default when absent. run-prod-app uses this to build the
|
|
13
40
|
// limiter from the same declaration the feature was given.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createWebhookContractResolver, } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { PAT_FEATURE } from "./web/constants.js";
|
|
3
|
+
function entityNameOf(qualifiedName) {
|
|
4
|
+
const dot = qualifiedName.lastIndexOf(".");
|
|
5
|
+
return dot > 0 ? qualifiedName.slice(0, dot) : qualifiedName;
|
|
6
|
+
}
|
|
7
|
+
export function validateScopeEventContracts(scopes, features) {
|
|
8
|
+
const resolver = createWebhookContractResolver(features);
|
|
9
|
+
for (const [domain, def] of Object.entries(scopes)) {
|
|
10
|
+
for (const qn of def.events ?? []) {
|
|
11
|
+
if (resolver.get(qn))
|
|
12
|
+
continue;
|
|
13
|
+
throw new Error(`[${PAT_FEATURE}] scope "${domain}" lists event "${qn}" without a public contract. ` +
|
|
14
|
+
"Declare it with r.defineWebhookEvent(...), or set webhookEvents: true on the entity to release its <entity>.created/.updated/.deleted events " +
|
|
15
|
+
`(entity "${entityNameOf(qn)}" has no such opt-in); internal r.defineEvent events are never released.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { i18nKey, MAX_LIST_LIMIT, } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
2
|
import { ACTION_FORM_ENTITY } from "@cosmicdrift/kumiko-framework/ui-types";
|
|
3
3
|
import { fieldOptionLabelKeyPrefix } from "@cosmicdrift/kumiko-renderer/headless";
|
|
4
|
-
import { PAT_FEATURE, PAT_MINT_SCREEN_ID, PAT_SCREEN_ID, PatHandlers, PatQueries, } from "./web/constants.js";
|
|
4
|
+
import { PAT_DEFAULT_EXPIRES_IN_DAYS, PAT_FEATURE, PAT_MAX_EXPIRES_IN_DAYS, PAT_MINT_SCREEN_ID, PAT_SCREEN_ID, PatHandlers, PatQueries, } from "./web/constants.js";
|
|
5
5
|
const PAT_STATUS_OPTION_KEY_PREFIX = "pat.list.status.";
|
|
6
6
|
// Same keys the mint form's `scopes` multiSelect options resolve through
|
|
7
7
|
// (see patScopeOptionTranslations), so list and form show identical labels.
|
|
@@ -114,7 +114,13 @@ export function createPatMintScreen(scopes) {
|
|
|
114
114
|
display: "checkboxes",
|
|
115
115
|
options: patGrantOptions(scopes),
|
|
116
116
|
},
|
|
117
|
-
expiresInDays: {
|
|
117
|
+
expiresInDays: {
|
|
118
|
+
type: "number",
|
|
119
|
+
integer: true,
|
|
120
|
+
min: 1,
|
|
121
|
+
max: PAT_MAX_EXPIRES_IN_DAYS,
|
|
122
|
+
default: PAT_DEFAULT_EXPIRES_IN_DAYS,
|
|
123
|
+
},
|
|
118
124
|
currentPassword: { type: "text", required: true, format: "password", sensitive: true },
|
|
119
125
|
mfaCode: { type: "text" },
|
|
120
126
|
},
|
|
@@ -5,6 +5,7 @@ export declare const PatErrors: {
|
|
|
5
5
|
readonly reauthRequired: "reauth_required";
|
|
6
6
|
};
|
|
7
7
|
export declare const PAT_DEFAULT_EXPIRES_IN_DAYS = 90;
|
|
8
|
+
export declare const PAT_MAX_EXPIRES_IN_DAYS = 3650;
|
|
8
9
|
export declare const PAT_SCREEN_ID = "api-tokens";
|
|
9
10
|
export declare const PAT_MINT_SCREEN_ID = "api-token-create";
|
|
10
11
|
export declare const PatHandlers: {
|
|
@@ -13,6 +13,7 @@ export const PatErrors = {
|
|
|
13
13
|
// default is a standing-credential risk; the existing 3650-day cap still
|
|
14
14
|
// lets callers who genuinely want a long-lived token pass it explicitly.
|
|
15
15
|
export const PAT_DEFAULT_EXPIRES_IN_DAYS = 90;
|
|
16
|
+
export const PAT_MAX_EXPIRES_IN_DAYS = 3650;
|
|
16
17
|
// Declarative projectionList screen id — the caller's own tokens, revoke as a
|
|
17
18
|
// row action. The app places it via r.nav.
|
|
18
19
|
export const PAT_SCREEN_ID = "api-tokens";
|
|
@@ -2,6 +2,7 @@ export type PatScopeDef = {
|
|
|
2
2
|
readonly label: string;
|
|
3
3
|
readonly read: readonly string[];
|
|
4
4
|
readonly write?: readonly string[];
|
|
5
|
+
readonly events?: readonly string[];
|
|
5
6
|
};
|
|
6
7
|
export type PatScopeConfig = Readonly<Record<string, PatScopeDef>>;
|
|
7
8
|
export type PatLevel = "read" | "write";
|
|
@@ -9,4 +10,5 @@ export declare function parseGrant(grant: string): {
|
|
|
9
10
|
domain: string;
|
|
10
11
|
level: string;
|
|
11
12
|
} | null;
|
|
13
|
+
export declare function expandScopeEvents(config: PatScopeConfig, granted: readonly string[]): string[];
|
|
12
14
|
export declare function expandScopes(config: PatScopeConfig, granted: readonly string[]): string[];
|
|
@@ -6,6 +6,18 @@ export function parseGrant(grant) {
|
|
|
6
6
|
return null;
|
|
7
7
|
return { domain: grant.slice(0, idx), level: grant.slice(idx + 1) };
|
|
8
8
|
}
|
|
9
|
+
// Union of the webhook events of every granted domain; read level already suffices to subscribe.
|
|
10
|
+
export function expandScopeEvents(config, granted) {
|
|
11
|
+
const out = new Set();
|
|
12
|
+
for (const grant of granted) {
|
|
13
|
+
const parsed = parseGrant(grant);
|
|
14
|
+
if (!parsed)
|
|
15
|
+
continue;
|
|
16
|
+
for (const qn of config[parsed.domain]?.events ?? [])
|
|
17
|
+
out.add(qn);
|
|
18
|
+
}
|
|
19
|
+
return [...out];
|
|
20
|
+
}
|
|
9
21
|
// Expand granted "<domain>:<level>" entries into the union of QN globs: read
|
|
10
22
|
// always grants the read QNs; write additionally grants the write QNs. Unknown
|
|
11
23
|
// domains contribute nothing (fail-closed — a scope dropped from config after a
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineFeature } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { PRESENCE_FEATURE } from "@cosmicdrift/kumiko-types/presence-types";
|
|
3
|
+
import { createHeartbeatHandler } from "./handlers/heartbeat.write.js";
|
|
4
|
+
import { createLeaveHandler } from "./handlers/leave.write.js";
|
|
5
|
+
import { createListQuery } from "./handlers/list.query.js";
|
|
6
|
+
import { resolvePresenceSettings } from "./options.js";
|
|
7
|
+
import { createMemoryPresenceStore } from "./store.js";
|
|
8
|
+
export function createPresenceFeature(options = {}) {
|
|
9
|
+
const settings = resolvePresenceSettings(options);
|
|
10
|
+
// Without Redis the room lives in this process only, which is right for a single instance.
|
|
11
|
+
const runtime = { settings, memoryStore: createMemoryPresenceStore() };
|
|
12
|
+
const access = { roles: settings.roles };
|
|
13
|
+
return defineFeature(PRESENCE_FEATURE, (r) => {
|
|
14
|
+
r.describe('Who else is on this record right now: a browser sends a heartbeat every 15 seconds for one row of an allow-listed entity and gets the other viewers and editors back, with a live signal to them on a change. State is ephemeral (Redis hash per tenant, entity and row, in-memory without Redis), keyed by the session tenant, expires after ttlSeconds without a heartbeat and holds user ids only; display names are looked up per response. A caller sees presence only for rows visible to them. Mount with createPresenceFeature({ entities: ["task"] }).');
|
|
15
|
+
r.uiHints({ displayLabel: "Presence", category: "communication", recommended: false });
|
|
16
|
+
r.writeHandler(createHeartbeatHandler(runtime, access));
|
|
17
|
+
r.writeHandler(createLeaveHandler(runtime, access));
|
|
18
|
+
r.queryHandler(createListQuery(runtime, access));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { type PresenceHeartbeatResult } from "@cosmicdrift/kumiko-types/presence-types";
|
|
3
|
+
import { type PresenceRuntime } from "../room.ts";
|
|
4
|
+
export declare function createHeartbeatHandler(runtime: PresenceRuntime, access: AccessRule): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"heartbeat", import("zod").ZodObject<{
|
|
5
|
+
entity: import("zod").ZodString;
|
|
6
|
+
id: import("zod").ZodString;
|
|
7
|
+
activity: import("zod").ZodEnum<{
|
|
8
|
+
editing: "editing";
|
|
9
|
+
viewing: "viewing";
|
|
10
|
+
}>;
|
|
11
|
+
focus: import("zod").ZodOptional<import("zod").ZodString>;
|
|
12
|
+
}, import("zod/v4/core").$strip>, PresenceHeartbeatResult, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { NotFoundError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import { PRESENCE_CHANGED_EVENT, } from "@cosmicdrift/kumiko-types/presence-types";
|
|
4
|
+
import { HeartbeatSchema } from "../payloads.js";
|
|
5
|
+
import { canUseRoom, othersOf, roomKey, storeFor, toEntries, } from "../room.js";
|
|
6
|
+
export function createHeartbeatHandler(runtime, access) {
|
|
7
|
+
return defineWriteHandler({
|
|
8
|
+
name: "heartbeat",
|
|
9
|
+
schema: HeartbeatSchema,
|
|
10
|
+
access,
|
|
11
|
+
agent: { expose: false },
|
|
12
|
+
// Presence lives in Redis or memory, never Postgres: no transaction per 15 s heartbeat.
|
|
13
|
+
transaction: "perCommand",
|
|
14
|
+
description: "Announces that the caller is looking at or editing one row and answers with the other people currently on it; the browser sends it every 15 seconds while a record is open.",
|
|
15
|
+
handler: async (event, ctx) => {
|
|
16
|
+
const { entity, id, activity, focus } = event.payload;
|
|
17
|
+
if (!(await canUseRoom(runtime, ctx, entity, id))) {
|
|
18
|
+
return writeFailure(new NotFoundError(entity, id));
|
|
19
|
+
}
|
|
20
|
+
const store = storeFor(runtime, ctx);
|
|
21
|
+
const key = roomKey(ctx, entity, id);
|
|
22
|
+
const nowMs = ctx.tz.now().epochMilliseconds;
|
|
23
|
+
const { changed } = await store.touch(key, event.user.id, { activity, ...(focus !== undefined && { focus }) }, nowMs, runtime.settings.ttlMs);
|
|
24
|
+
const others = othersOf(await store.list(key, nowMs, runtime.settings.ttlMs), event.user.id);
|
|
25
|
+
if (changed && others.length > 0) {
|
|
26
|
+
await ctx.services.userLiveSignals?.push(others.map((record) => record.userId), { type: PRESENCE_CHANGED_EVENT, entity, id });
|
|
27
|
+
}
|
|
28
|
+
return { isSuccess: true, data: { others: await toEntries(ctx, others) } };
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { type PresenceRuntime } from "../room.ts";
|
|
3
|
+
export declare function createLeaveHandler(runtime: PresenceRuntime, access: AccessRule): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"leave", import("zod").ZodObject<{
|
|
4
|
+
entity: import("zod").ZodString;
|
|
5
|
+
id: import("zod").ZodString;
|
|
6
|
+
}, import("zod/v4/core").$strip>, {
|
|
7
|
+
readonly left: boolean;
|
|
8
|
+
}, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { NotFoundError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import { PRESENCE_CHANGED_EVENT } from "@cosmicdrift/kumiko-types/presence-types";
|
|
4
|
+
import { LeaveSchema } from "../payloads.js";
|
|
5
|
+
import { canUseRoom, othersOf, roomKey, storeFor } from "../room.js";
|
|
6
|
+
export function createLeaveHandler(runtime, access) {
|
|
7
|
+
return defineWriteHandler({
|
|
8
|
+
name: "leave",
|
|
9
|
+
schema: LeaveSchema,
|
|
10
|
+
access,
|
|
11
|
+
agent: { expose: false },
|
|
12
|
+
// Presence lives in Redis or memory, never Postgres: no transaction per 15 s heartbeat.
|
|
13
|
+
transaction: "perCommand",
|
|
14
|
+
description: "Removes the caller from one row's presence list right away, so others do not wait for the entry to expire; the browser sends it when a record is closed.",
|
|
15
|
+
handler: async (event, ctx) => {
|
|
16
|
+
const { entity, id } = event.payload;
|
|
17
|
+
if (!(await canUseRoom(runtime, ctx, entity, id))) {
|
|
18
|
+
return writeFailure(new NotFoundError(entity, id));
|
|
19
|
+
}
|
|
20
|
+
const store = storeFor(runtime, ctx);
|
|
21
|
+
const key = roomKey(ctx, entity, id);
|
|
22
|
+
const { existed } = await store.remove(key, event.user.id);
|
|
23
|
+
if (existed) {
|
|
24
|
+
const nowMs = ctx.tz.now().epochMilliseconds;
|
|
25
|
+
const others = othersOf(await store.list(key, nowMs, runtime.settings.ttlMs), event.user.id);
|
|
26
|
+
if (others.length > 0) {
|
|
27
|
+
await ctx.services.userLiveSignals?.push(others.map((record) => record.userId), { type: PRESENCE_CHANGED_EVENT, entity, id });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { isSuccess: true, data: { left: existed } };
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { PresenceListResult } from "@cosmicdrift/kumiko-types/presence-types";
|
|
3
|
+
import { type PresenceRuntime } from "../room.ts";
|
|
4
|
+
export declare function createListQuery(runtime: PresenceRuntime, access: AccessRule): import("@cosmicdrift/kumiko-types/define-handler").QueryHandlerDefinition<"list", import("zod").ZodObject<{
|
|
5
|
+
entity: import("zod").ZodString;
|
|
6
|
+
ids: import("zod").ZodArray<import("zod").ZodString>;
|
|
7
|
+
}, import("zod/v4/core").$strip>, PresenceListResult, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineQueryHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { ListSchema } from "../payloads.js";
|
|
3
|
+
import { canUseRoom, othersOf, roomKey, storeFor, toEntries, } from "../room.js";
|
|
4
|
+
export function createListQuery(runtime, access) {
|
|
5
|
+
return defineQueryHandler({
|
|
6
|
+
name: "list",
|
|
7
|
+
schema: ListSchema,
|
|
8
|
+
access,
|
|
9
|
+
agent: { expose: false },
|
|
10
|
+
description: "Lists the other people currently on each of up to 100 rows of one entity, for badges in a list; rows the caller cannot see are left out without a hint.",
|
|
11
|
+
handler: async (event, ctx) => {
|
|
12
|
+
const { entity, ids } = event.payload;
|
|
13
|
+
if (!runtime.settings.entities.has(entity))
|
|
14
|
+
return { byId: {} };
|
|
15
|
+
const store = storeFor(runtime, ctx);
|
|
16
|
+
const nowMs = ctx.tz.now().epochMilliseconds;
|
|
17
|
+
const byId = {};
|
|
18
|
+
for (const id of new Set(ids)) {
|
|
19
|
+
const others = othersOf(await store.list(roomKey(ctx, entity, id), nowMs, runtime.settings.ttlMs), event.user.id);
|
|
20
|
+
// Visibility is checked only where someone is present, so an empty row costs no lookup.
|
|
21
|
+
if (others.length === 0 || !(await canUseRoom(runtime, ctx, entity, id)))
|
|
22
|
+
continue;
|
|
23
|
+
byId[id] = await toEntries(ctx, others);
|
|
24
|
+
}
|
|
25
|
+
return { byId };
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const PRESENCE_DEFAULT_ROLES: readonly string[];
|
|
2
|
+
export declare const PRESENCE_DEFAULT_TTL_SECONDS = 45;
|
|
3
|
+
export type PresenceOptions = {
|
|
4
|
+
/** Entities whose rows may carry presence. Default none: a heartbeat for an entity that is not
|
|
5
|
+
* listed is rejected. */
|
|
6
|
+
readonly entities?: readonly string[];
|
|
7
|
+
/** Roles allowed to send heartbeats and read presence. Default TenantAdmin and TenantMember. */
|
|
8
|
+
readonly roles?: readonly string[];
|
|
9
|
+
/** Seconds a user stays listed without a heartbeat. Default 45 (three missed 15 s beats). */
|
|
10
|
+
readonly ttlSeconds?: number;
|
|
11
|
+
};
|
|
12
|
+
export type PresenceSettings = {
|
|
13
|
+
readonly entities: ReadonlySet<string>;
|
|
14
|
+
readonly roles: readonly string[];
|
|
15
|
+
readonly ttlMs: number;
|
|
16
|
+
};
|
|
17
|
+
export declare function resolvePresenceSettings(options: PresenceOptions): PresenceSettings;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const PRESENCE_DEFAULT_ROLES = ["TenantAdmin", "TenantMember"];
|
|
2
|
+
export const PRESENCE_DEFAULT_TTL_SECONDS = 45;
|
|
3
|
+
export function resolvePresenceSettings(options) {
|
|
4
|
+
const ttlSeconds = options.ttlSeconds ?? PRESENCE_DEFAULT_TTL_SECONDS;
|
|
5
|
+
if (!Number.isFinite(ttlSeconds) || ttlSeconds <= 0) {
|
|
6
|
+
throw new Error(`presence: ttlSeconds must be a positive number, got ${ttlSeconds}`);
|
|
7
|
+
}
|
|
8
|
+
return {
|
|
9
|
+
entities: new Set(options.entities ?? []),
|
|
10
|
+
roles: options.roles ?? PRESENCE_DEFAULT_ROLES,
|
|
11
|
+
ttlMs: ttlSeconds * 1000,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
export declare const HeartbeatSchema: z.ZodObject<{
|
|
3
|
+
entity: z.ZodString;
|
|
4
|
+
id: z.ZodString;
|
|
5
|
+
activity: z.ZodEnum<{
|
|
6
|
+
editing: "editing";
|
|
7
|
+
viewing: "viewing";
|
|
8
|
+
}>;
|
|
9
|
+
focus: z.ZodOptional<z.ZodString>;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
export declare const LeaveSchema: z.ZodObject<{
|
|
12
|
+
entity: z.ZodString;
|
|
13
|
+
id: z.ZodString;
|
|
14
|
+
}, z.core.$strip>;
|
|
15
|
+
export declare const ListSchema: z.ZodObject<{
|
|
16
|
+
entity: z.ZodString;
|
|
17
|
+
ids: z.ZodArray<z.ZodString>;
|
|
18
|
+
}, z.core.$strip>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PRESENCE_FOCUS_MAX_LENGTH, PRESENCE_LIST_MAX_IDS, } from "@cosmicdrift/kumiko-types/presence-types";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
const ENTITY_NAME_MAX = 64;
|
|
4
|
+
const ROW_ID_MAX = 64;
|
|
5
|
+
const FOCUS_PATTERN = /^[\w:.-]+$/;
|
|
6
|
+
const entity = z.string().min(1).max(ENTITY_NAME_MAX);
|
|
7
|
+
const rowId = z.string().min(1).max(ROW_ID_MAX);
|
|
8
|
+
export const HeartbeatSchema = z.object({
|
|
9
|
+
entity,
|
|
10
|
+
id: rowId,
|
|
11
|
+
activity: z.enum(["viewing", "editing"]),
|
|
12
|
+
focus: z.string().max(PRESENCE_FOCUS_MAX_LENGTH).regex(FOCUS_PATTERN).optional(),
|
|
13
|
+
});
|
|
14
|
+
export const LeaveSchema = z.object({ entity, id: rowId });
|
|
15
|
+
export const ListSchema = z.object({
|
|
16
|
+
entity,
|
|
17
|
+
ids: z.array(rowId).min(1).max(PRESENCE_LIST_MAX_IDS),
|
|
18
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HandlerContext } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { PresenceEntry } from "@cosmicdrift/kumiko-types/presence-types";
|
|
3
|
+
import type { PresenceSettings } from "./options.ts";
|
|
4
|
+
import { type PresenceRecord, type PresenceStore } from "./store.ts";
|
|
5
|
+
export type PresenceRuntime = {
|
|
6
|
+
readonly settings: PresenceSettings;
|
|
7
|
+
readonly memoryStore: PresenceStore;
|
|
8
|
+
};
|
|
9
|
+
export declare function storeFor(runtime: PresenceRuntime, ctx: HandlerContext): PresenceStore;
|
|
10
|
+
export declare function roomKey(ctx: HandlerContext, entity: string, id: string): string;
|
|
11
|
+
export declare function canUseRoom(runtime: PresenceRuntime, ctx: HandlerContext, entity: string, id: string): Promise<boolean>;
|
|
12
|
+
export declare function othersOf(records: readonly PresenceRecord[], userId: string): readonly PresenceRecord[];
|
|
13
|
+
export declare function toEntries(ctx: HandlerContext, records: readonly PresenceRecord[]): Promise<readonly PresenceEntry[]>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { parentRowIsVisible } from "../shared/index.js";
|
|
2
|
+
import { resolveUserDisplayNames, USER_FEATURE } from "../user/index.js";
|
|
3
|
+
import { createRedisPresenceStore, presenceKey, } from "./store.js";
|
|
4
|
+
export function storeFor(runtime, ctx) {
|
|
5
|
+
const redis = ctx.services.redis;
|
|
6
|
+
return redis ? createRedisPresenceStore(redis, ctx.obs.log) : runtime.memoryStore;
|
|
7
|
+
}
|
|
8
|
+
export function roomKey(ctx, entity, id) {
|
|
9
|
+
return presenceKey(ctx.user.tenantId, entity, id);
|
|
10
|
+
}
|
|
11
|
+
// Not-allowed and not-visible are indistinguishable to the caller, so the endpoint is no
|
|
12
|
+
// existence oracle for rows or entity names.
|
|
13
|
+
export async function canUseRoom(runtime, ctx, entity, id) {
|
|
14
|
+
if (!runtime.settings.entities.has(entity))
|
|
15
|
+
return false;
|
|
16
|
+
return parentRowIsVisible(ctx.registry, entity, id, ctx.user, ctx.db);
|
|
17
|
+
}
|
|
18
|
+
export function othersOf(records, userId) {
|
|
19
|
+
return records.filter((record) => record.userId !== userId);
|
|
20
|
+
}
|
|
21
|
+
// Names are looked up per response and never stored, so an erased user leaves no name behind.
|
|
22
|
+
export async function toEntries(ctx, records) {
|
|
23
|
+
if (records.length === 0)
|
|
24
|
+
return [];
|
|
25
|
+
const names = ctx.registry.getFeature(USER_FEATURE)
|
|
26
|
+
? await resolveUserDisplayNames(ctx.db, records.map((record) => record.userId))
|
|
27
|
+
: new Map();
|
|
28
|
+
return records.map((record) => ({
|
|
29
|
+
userId: record.userId,
|
|
30
|
+
displayName: names.get(record.userId) ?? record.userId,
|
|
31
|
+
activity: record.activity,
|
|
32
|
+
...(record.focus !== undefined && { focus: record.focus }),
|
|
33
|
+
since: Temporal.Instant.fromEpochMilliseconds(record.since).toString(),
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Logger } from "@cosmicdrift/kumiko-framework/logging";
|
|
2
|
+
import type { PresenceActivity } from "@cosmicdrift/kumiko-types/presence-types";
|
|
3
|
+
import type { Redis } from "ioredis";
|
|
4
|
+
export type PresenceRecord = {
|
|
5
|
+
readonly userId: string;
|
|
6
|
+
readonly activity: PresenceActivity;
|
|
7
|
+
readonly focus?: string;
|
|
8
|
+
readonly since: number;
|
|
9
|
+
readonly seenAt: number;
|
|
10
|
+
};
|
|
11
|
+
export type PresenceState = {
|
|
12
|
+
readonly activity: PresenceActivity;
|
|
13
|
+
readonly focus?: string;
|
|
14
|
+
};
|
|
15
|
+
export type PresenceStore = {
|
|
16
|
+
/** `changed` is true for a new entry or a changed activity/focus, false for a plain keep-alive. */
|
|
17
|
+
touch(key: string, userId: string, state: PresenceState, nowMs: number, ttlMs: number): Promise<{
|
|
18
|
+
readonly changed: boolean;
|
|
19
|
+
}>;
|
|
20
|
+
remove(key: string, userId: string): Promise<{
|
|
21
|
+
readonly existed: boolean;
|
|
22
|
+
}>;
|
|
23
|
+
/** Live entries only; expired ones are dropped on read. */
|
|
24
|
+
list(key: string, nowMs: number, ttlMs: number): Promise<readonly PresenceRecord[]>;
|
|
25
|
+
};
|
|
26
|
+
export declare function presenceKey(tenantId: string, entity: string, id: string): string;
|
|
27
|
+
export type MemoryPresenceStore = PresenceStore & {
|
|
28
|
+
/** Rooms currently held, expired or not; lets tests observe the sweep. */
|
|
29
|
+
roomCount(): number;
|
|
30
|
+
};
|
|
31
|
+
export declare function createMemoryPresenceStore(): MemoryPresenceStore;
|
|
32
|
+
export declare function createRedisPresenceStore(redis: Redis, log: Logger): PresenceStore;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
export function presenceKey(tenantId, entity, id) {
|
|
3
|
+
return `presence:${tenantId}:${entity}:${id}`;
|
|
4
|
+
}
|
|
5
|
+
function isExpired(record, nowMs, ttlMs) {
|
|
6
|
+
return record.seenAt + ttlMs < nowMs;
|
|
7
|
+
}
|
|
8
|
+
function sameState(record, state) {
|
|
9
|
+
return record.activity === state.activity && record.focus === state.focus;
|
|
10
|
+
}
|
|
11
|
+
function nextRecord(userId, state, previous, nowMs) {
|
|
12
|
+
const unchanged = previous !== undefined && sameState(previous, state);
|
|
13
|
+
return {
|
|
14
|
+
userId,
|
|
15
|
+
activity: state.activity,
|
|
16
|
+
...(state.focus !== undefined && { focus: state.focus }),
|
|
17
|
+
since: unchanged ? previous.since : nowMs,
|
|
18
|
+
seenAt: nowMs,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function createMemoryPresenceStore() {
|
|
22
|
+
const rooms = new Map();
|
|
23
|
+
let lastSweepMs = 0;
|
|
24
|
+
// Rooms whose last user left without a leave are never read again, so reads alone would keep
|
|
25
|
+
// them forever; a sweep per ttl on write bounds the map.
|
|
26
|
+
function sweepExpired(nowMs, ttlMs) {
|
|
27
|
+
// skip: the last sweep is still within one ttl
|
|
28
|
+
if (nowMs - lastSweepMs < ttlMs)
|
|
29
|
+
return;
|
|
30
|
+
lastSweepMs = nowMs;
|
|
31
|
+
for (const [key, room] of rooms) {
|
|
32
|
+
for (const [userId, record] of room) {
|
|
33
|
+
if (isExpired(record, nowMs, ttlMs))
|
|
34
|
+
room.delete(userId);
|
|
35
|
+
}
|
|
36
|
+
if (room.size === 0)
|
|
37
|
+
rooms.delete(key);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
roomCount: () => rooms.size,
|
|
42
|
+
async touch(key, userId, state, nowMs, ttlMs) {
|
|
43
|
+
sweepExpired(nowMs, ttlMs);
|
|
44
|
+
const room = rooms.get(key) ?? new Map();
|
|
45
|
+
rooms.set(key, room);
|
|
46
|
+
const existing = room.get(userId);
|
|
47
|
+
const previous = existing && !isExpired(existing, nowMs, ttlMs) ? existing : undefined;
|
|
48
|
+
room.set(userId, nextRecord(userId, state, previous, nowMs));
|
|
49
|
+
return { changed: previous === undefined || !sameState(previous, state) };
|
|
50
|
+
},
|
|
51
|
+
async remove(key, userId) {
|
|
52
|
+
const room = rooms.get(key);
|
|
53
|
+
const existed = room?.delete(userId) ?? false;
|
|
54
|
+
if (room?.size === 0)
|
|
55
|
+
rooms.delete(key);
|
|
56
|
+
return { existed };
|
|
57
|
+
},
|
|
58
|
+
async list(key, nowMs, ttlMs) {
|
|
59
|
+
const room = rooms.get(key);
|
|
60
|
+
if (!room)
|
|
61
|
+
return [];
|
|
62
|
+
for (const [userId, record] of room) {
|
|
63
|
+
if (isExpired(record, nowMs, ttlMs))
|
|
64
|
+
room.delete(userId);
|
|
65
|
+
}
|
|
66
|
+
if (room.size === 0)
|
|
67
|
+
rooms.delete(key);
|
|
68
|
+
return [...room.values()];
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const KEY_TTL_FACTOR = 2;
|
|
73
|
+
const storedRecordSchema = z.object({
|
|
74
|
+
activity: z.enum(["viewing", "editing"]),
|
|
75
|
+
focus: z.string().optional(),
|
|
76
|
+
since: z.number(),
|
|
77
|
+
seenAt: z.number(),
|
|
78
|
+
});
|
|
79
|
+
// Stored value carries no display name: Redis and the live frames hold ids only.
|
|
80
|
+
function parseRecord(userId, raw) {
|
|
81
|
+
try {
|
|
82
|
+
const parsed = storedRecordSchema.safeParse(JSON.parse(raw));
|
|
83
|
+
if (!parsed.success)
|
|
84
|
+
return undefined;
|
|
85
|
+
const { activity, focus, since, seenAt } = parsed.data;
|
|
86
|
+
return { userId, activity, ...(focus !== undefined && { focus }), since, seenAt };
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export function createRedisPresenceStore(redis, log) {
|
|
93
|
+
async function guarded(fallback, run) {
|
|
94
|
+
if (redis.status !== "ready") {
|
|
95
|
+
log.warn("presence store skipped: redis connection not ready", { status: redis.status });
|
|
96
|
+
return fallback;
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
return await run();
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
log.warn("presence store command failed", { err });
|
|
103
|
+
return fallback;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function read(key, userId) {
|
|
107
|
+
const raw = await redis.hget(key, userId);
|
|
108
|
+
return raw === null ? undefined : parseRecord(userId, raw);
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
touch: (key, userId, state, nowMs, ttlMs) => guarded({ changed: false }, async () => {
|
|
112
|
+
const existing = await read(key, userId);
|
|
113
|
+
const previous = existing && !isExpired(existing, nowMs, ttlMs) ? existing : undefined;
|
|
114
|
+
const record = nextRecord(userId, state, previous, nowMs);
|
|
115
|
+
const { userId: _omit, ...stored } = record;
|
|
116
|
+
await redis
|
|
117
|
+
.multi()
|
|
118
|
+
.hset(key, userId, JSON.stringify(stored))
|
|
119
|
+
.expire(key, Math.ceil((ttlMs * KEY_TTL_FACTOR) / 1000))
|
|
120
|
+
.exec();
|
|
121
|
+
return { changed: previous === undefined || !sameState(previous, state) };
|
|
122
|
+
}),
|
|
123
|
+
remove: (key, userId) => guarded({ existed: false }, async () => ({ existed: (await redis.hdel(key, userId)) > 0 })),
|
|
124
|
+
list: (key, nowMs, ttlMs) => guarded([], async () => {
|
|
125
|
+
const all = await redis.hgetall(key);
|
|
126
|
+
const live = [];
|
|
127
|
+
const expired = [];
|
|
128
|
+
for (const [userId, raw] of Object.entries(all)) {
|
|
129
|
+
const record = parseRecord(userId, raw);
|
|
130
|
+
if (record && !isExpired(record, nowMs, ttlMs))
|
|
131
|
+
live.push(record);
|
|
132
|
+
else
|
|
133
|
+
expired.push(userId);
|
|
134
|
+
}
|
|
135
|
+
if (expired.length > 0)
|
|
136
|
+
await redis.hdel(key, ...expired);
|
|
137
|
+
return live;
|
|
138
|
+
}),
|
|
139
|
+
};
|
|
140
|
+
}
|
package/dist/sessions/feature.js
CHANGED
|
@@ -5,7 +5,7 @@ import { createLateBoundSlot, readFeatureExportFn } from "../shared/index.js";
|
|
|
5
5
|
import { USER_SESSION_ENTITY_NAME } from "./constants.js";
|
|
6
6
|
import { cleanupJob } from "./handlers/cleanup.job.js";
|
|
7
7
|
import { createDetailQuery } from "./handlers/detail.query.js";
|
|
8
|
-
import { registerLastActivityProbe } from "./handlers/last-activity-probe.route.js";
|
|
8
|
+
import { readProbeSecretFromEnv, registerLastActivityProbe, } from "./handlers/last-activity-probe.route.js";
|
|
9
9
|
import { createListQuery } from "./handlers/list.query.js";
|
|
10
10
|
import { mineQuery } from "./handlers/mine.query.js";
|
|
11
11
|
import { revokeWrite } from "./handlers/revoke.write.js";
|
|
@@ -120,7 +120,7 @@ export function createSessionsFeature(options) {
|
|
|
120
120
|
escapeHatch: { reason: CLEANUP_ESCAPE_HATCH_REASON },
|
|
121
121
|
handler: (payload, ctx) => cleanupJob(payload, ctx, ctx.db.unsafeRaw()),
|
|
122
122
|
});
|
|
123
|
-
const probeSecret = options?.lastActivityProbeSecret;
|
|
123
|
+
const probeSecret = options?.lastActivityProbeSecret ?? readProbeSecretFromEnv();
|
|
124
124
|
if (probeSecret !== undefined)
|
|
125
125
|
registerLastActivityProbe(r, probeSecret);
|
|
126
126
|
// Cross-feature entity hook on "user". `r.entityHook` (NOT `r.hook`) is
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { type FeatureRegistrar } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
export declare const LAST_ACTIVITY_PROBE_SECRET_ENV = "SESSIONS_LAST_ACTIVITY_PROBE_SECRET";
|
|
3
|
+
export declare function readProbeSecretFromEnv(env?: Readonly<Record<string, string | undefined>> | undefined): string | undefined;
|
|
2
4
|
export declare function registerLastActivityProbe(r: FeatureRegistrar, secret: string): void;
|