Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 16x 16x 16x 16x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 9x 7x 7x 9x 9x 16x 16x 16x 16x 16x 7x 7x 7x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 5x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x | import {type IMeta} from '@feasibleone/blong';
import {handler} from './handlerGroup.ts';
type KnexQb = any;
/**
* Create/merge API bundles plus plain capabilities/roles.
*
* Wire: `gateway.bundle.merge` — a bundle wraps an `access.role` whose
* capabilities/actions are the bundle's authorized scopes. Also accepts
* generic `capability` (name → action list) and `role` (name → capability
* list) maps so seeds can wire management capabilities onto roles (e.g. a
* Developer role), keeping authorization uniform in the jwt plugin.
*
* Resource creation goes through the shared `core.resource.ensure` helper and
* graph edges through the shared `core.triple.merge` helper (P3), so the seed
* no longer hand-rolls `core_triple` inserts or `access_pathRefresh()`.
*/
export default handler(({lib: {splitNames, uuidBuf}}) => ({
async gatewayBundleMerge(
params: {
/** capabilityName → comma-separated action names. */
capability?: Record<string, string>;
/** roleName → comma-separated capability names. */
role?: Record<string, string>;
bundle?: Record<
string,
{
roleBit?: number;
/** Capability name to wrap. */
capability?: string;
/** Comma-separated action names granted by the capability. */
actions?: string;
baseMonthlyCredits?: number;
rateLimit?: number;
rateWindowSec?: number;
isActive?: boolean;
description?: string;
}
>;
},
$meta: IMeta,
): Promise<{success: boolean}> {
const qb: KnexQb = this.config?.context?.queryBuilder;
if (!qb) throw new Error('Database not available');
const triples: Array<{
subjectId: string;
predicateName: string;
objectId: string;
}> = [];
// A merge is long enough that a reader of the diagram cannot tell where it got to, so
// the phases announce themselves (PRD R26). A point is *announced*, not logged: it rides
// the next record this scope emits, which is what lets a handler with no logger of its own
// report its progress. In production `$meta.checkpoint` is not attached at all, so this
// call costs nothing there.
$meta.checkpoint?.('merge-started', {
capabilities: Object.keys(params.capability ?? {}).length,
roles: Object.keys(params.role ?? {}).length,
bundles: Object.keys(params.bundle ?? {}).length,
});
// 1. Capabilities + their actions
let actionsMerged = 0;
if (params.capability) {
for (const [capabilityName, actionList] of Object.entries(params.capability)) {
const {resourceId: capabilityId} = (await super.coreResourceEnsure(
{
name: capabilityName,
typeAlias: 'access.capability',
table: 'access_capability',
extraColumns: {description: `${capabilityName} capability`},
keyName: 'capabilityId',
},
$meta,
)) as {resourceId: string};
for (const actionName of splitNames(actionList)) {
const {resourceId: actionId} = (await super.coreResourceEnsure(
{
name: actionName,
typeAlias: 'access.action',
table: 'access_action',
extraColumns: {description: `${actionName} action`},
keyName: 'actionId',
},
$meta,
)) as {resourceId: string};
triples.push({
subjectId: capabilityId,
predicateName: 'hasAction',
objectId: actionId,
});
actionsMerged += 1;
}
}
$meta.checkpoint?.('capabilities-merged', {
capabilities: Object.keys(params.capability).length,
actions: actionsMerged,
});
}
// 2. Roles + their capabilities
if (params.role) {
for (const [roleName, capabilityList] of Object.entries(params.role)) {
const {role: ensuredRole} = (await super.accessRoleEnsure(
{role: {roleName}},
$meta,
)) as {role: {roleId: string}};
for (const capabilityName of splitNames(capabilityList)) {
const {resourceId: capabilityId} = (await super.coreResourceEnsure(
{
name: capabilityName,
typeAlias: 'access.capability',
table: 'access_capability',
extraColumns: {description: `${capabilityName} capability`},
keyName: 'capabilityId',
},
$meta,
)) as {resourceId: string};
triples.push({
subjectId: ensuredRole.roleId,
predicateName: 'hasCapability',
objectId: capabilityId,
});
}
}
$meta.checkpoint?.('roles-merged', {
roles: Object.keys(params.role).length,
});
}
// 3. Bundles (role-wrapped capabilities with rate/credit metadata)
if (params.bundle) {
for (const [bundleName, bundleDef] of Object.entries(params.bundle)) {
// 1. Capability + actions
const capabilityName = bundleDef.capability ?? bundleName;
const actionNames = splitNames(bundleDef.actions ?? '');
for (const actionName of actionNames) {
const {resourceId: actionId} = (await super.coreResourceEnsure(
{
name: actionName,
typeAlias: 'access.action',
table: 'access_action',
extraColumns: {description: `${actionName} action`},
keyName: 'actionId',
},
$meta,
)) as {resourceId: string};
const {resourceId: capabilityId} = (await super.coreResourceEnsure(
{
name: capabilityName,
typeAlias: 'access.capability',
table: 'access_capability',
extraColumns: {description: `${capabilityName} capability`},
keyName: 'capabilityId',
},
$meta,
)) as {resourceId: string};
triples.push({
subjectId: capabilityId,
predicateName: 'hasAction',
objectId: actionId,
});
}
// 2. Role (the bundle) + hasCapability edges. A declared bit is honoured;
// otherwise the framework allocates one. That choice is a branch rather than an
// `if` (PRD R11/R26): which layout a merge chose is exactly what a diagram of it
// should show, and each branch announces the layout it took, so the alternative
// that was not taken stays visible beside the call that was made.
// `access.role.ensure` comes from `access`, a library realm this process
// carries itself, so `super` reaches it in the same process.
const ensureBundleRole = (roleBit?: number) =>
super.accessRoleEnsure(
{
role: {
roleName: bundleName,
description: `${bundleName} bundle role`,
...(roleBit === undefined ? {} : {roleBit}),
},
},
$meta,
) as Promise<{role: {roleId: string}}>;
const decidedRole = await $meta.decide?.(
'role-bit',
{
bundleName,
declared: bundleDef.roleBit ?? null,
},
[
{
name: 'declared',
when: values => values.declared !== null,
run: () => {
$meta.checkpoint?.('role-bit-declared', {
roleBit: bundleDef.roleBit,
});
return ensureBundleRole(bundleDef.roleBit);
},
},
{
name: 'allocated',
when: () => true,
run: () => {
$meta.checkpoint?.('role-bit-allocated', {bundleName});
return ensureBundleRole();
},
},
],
);
// A `$meta` that no dispatch built carries no branch helper, and the default
// layout then is the one the `allocated` branch states.
const {role: bundleRole} = decidedRole ?? (await ensureBundleRole());
const roleId = bundleRole.roleId;
const {resourceId: capabilityId} = (await super.coreResourceEnsure(
{
name: capabilityName,
typeAlias: 'access.capability',
table: 'access_capability',
extraColumns: {description: `${capabilityName} capability`},
keyName: 'capabilityId',
},
$meta,
)) as {resourceId: string};
triples.push({
subjectId: roleId,
predicateName: 'hasCapability',
objectId: capabilityId,
});
// 3. Bundle metadata row
await qb('gateway_bundle')
.insert({
bundleId: uuidBuf(roleId),
roleId: uuidBuf(roleId),
isActive: bundleDef.isActive ?? 1,
baseMonthlyCredits: bundleDef.baseMonthlyCredits ?? 0,
rateLimit: bundleDef.rateLimit ?? 0,
rateWindowSec: bundleDef.rateWindowSec ?? 60,
description: bundleDef.description ?? null,
})
.onConflict(['bundleId'])
.merge({
isActive: bundleDef.isActive ?? 1,
baseMonthlyCredits: bundleDef.baseMonthlyCredits ?? 0,
rateLimit: bundleDef.rateLimit ?? 0,
rateWindowSec: bundleDef.rateWindowSec ?? 60,
description: bundleDef.description ?? null,
});
}
$meta.checkpoint?.('bundles-merged', {
bundles: Object.keys(params.bundle).length,
});
}
await super.coreTripleMerge({triples, refreshPath: true}, $meta);
// The last point of the sequence, and the one that lands on whatever the scope emits
// next — the response, when the dispatch records one. A point with no record after it is
// dropped with its scope, which is why every other point here is announced *before* the
// call that follows it.
$meta.checkpoint?.('graph-merged', {triples: triples.length});
return {success: true};
},
}));
|