@aiam/ciba 0.9.0 → 0.9.1
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/package.json +1 -1
- package/token.mjs +29 -4
package/package.json
CHANGED
package/token.mjs
CHANGED
|
@@ -167,6 +167,31 @@ const meta = deviceDoc.getMap('meta');
|
|
|
167
167
|
if (meta.get('public_key') !== publicKey) meta.set('public_key', publicKey);
|
|
168
168
|
|
|
169
169
|
const resourcesMap = deviceDoc.getMap('resources');
|
|
170
|
+
const optionsMap = deviceDoc.getMap('options');
|
|
171
|
+
|
|
172
|
+
// Read server options — resolve short name or use as-is.
|
|
173
|
+
// options.resources = { webagents: { default: true, ... }, a2a: { ... }, ... }
|
|
174
|
+
const serverResources = optionsMap.get('resources') ?? {};
|
|
175
|
+
const resourceNames = Object.keys(serverResources);
|
|
176
|
+
|
|
177
|
+
// Resolve the requested resource:
|
|
178
|
+
// 1. If resource matches a short name directly → use it
|
|
179
|
+
// 2. If resource is a URN → use it as-is
|
|
180
|
+
// 3. If no resource → use default from options
|
|
181
|
+
let resolvedResource = resource;
|
|
182
|
+
if (!resource.includes(':') && !resourceNames.includes(resource)) {
|
|
183
|
+
// Try to find a match case-insensitively
|
|
184
|
+
const match = resourceNames.find(n => n.toLowerCase() === resource.toLowerCase());
|
|
185
|
+
if (match) resolvedResource = match;
|
|
186
|
+
}
|
|
187
|
+
if (!resource) {
|
|
188
|
+
// Use default
|
|
189
|
+
const def = resourceNames.find(n => serverResources[n]?.default);
|
|
190
|
+
resolvedResource = def ?? resource;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
log(`→ options: ${JSON.stringify(serverResources)}`);
|
|
194
|
+
log(`→ resolved: ${resolvedResource}`);
|
|
170
195
|
log(`→ resources: ${JSON.stringify([...resourcesMap.entries()])}`);
|
|
171
196
|
|
|
172
197
|
// Refresh mode: write request and exit immediately.
|
|
@@ -180,7 +205,7 @@ if (isRefresh) {
|
|
|
180
205
|
}
|
|
181
206
|
|
|
182
207
|
// tryGet immediately
|
|
183
|
-
const found = tryGet(resourcesMap, deviceDoc,
|
|
208
|
+
const found = tryGet(resourcesMap, deviceDoc, resolvedResource);
|
|
184
209
|
if (found) {
|
|
185
210
|
log('→ cache hit');
|
|
186
211
|
const token = decrypt(found);
|
|
@@ -192,8 +217,8 @@ if (found) {
|
|
|
192
217
|
// Cache miss — write request
|
|
193
218
|
const requests = deviceDoc.getMap('requests');
|
|
194
219
|
const newRid = randomBytes(8).toString('base64url');
|
|
195
|
-
log(`→ cache miss — writing request ${newRid}`);
|
|
196
|
-
requests.set(newRid, { resource, status: 'pending', created_at: new Date().toISOString() });
|
|
220
|
+
log(`→ cache miss — writing request ${newRid} resource=${resolvedResource}`);
|
|
221
|
+
requests.set(newRid, { resource: resolvedResource, status: 'pending', created_at: new Date().toISOString() });
|
|
197
222
|
|
|
198
223
|
// Wait for resources to update
|
|
199
224
|
log('→ waiting for token...');
|
|
@@ -205,7 +230,7 @@ const token = await new Promise((resolve, reject) => {
|
|
|
205
230
|
|
|
206
231
|
const observer = () => {
|
|
207
232
|
log(`→ resources updated: ${JSON.stringify([...resourcesMap.entries()])}`);
|
|
208
|
-
const found = tryGet(resourcesMap, deviceDoc,
|
|
233
|
+
const found = tryGet(resourcesMap, deviceDoc, resolvedResource);
|
|
209
234
|
if (found) {
|
|
210
235
|
clearTimeout(timer);
|
|
211
236
|
resourcesMap.unobserve(observer);
|