@sphereon/ssi-sdk.resource-resolver 0.30.2-feature.SPRIND.93.oidf.support.247 → 0.30.2-feature.mdoc.funke2.366
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -3
- package/dist/agent/ResourceResolver.d.ts +2 -0
- package/dist/agent/ResourceResolver.d.ts.map +1 -1
- package/dist/agent/ResourceResolver.js +30 -7
- package/dist/agent/ResourceResolver.js.map +1 -1
- package/dist/types/IResourceResolver.d.ts +8 -1
- package/dist/types/IResourceResolver.d.ts.map +1 -1
- package/dist/utils/ResourceResolverUtils.d.ts.map +1 -1
- package/dist/utils/ResourceResolverUtils.js +3 -3
- package/dist/utils/ResourceResolverUtils.js.map +1 -1
- package/package.json +5 -4
- package/src/agent/ResourceResolver.ts +38 -41
- package/src/types/IResourceResolver.ts +10 -2
- package/src/utils/ResourceResolverUtils.ts +11 -16
package/README.md
CHANGED
@@ -21,7 +21,8 @@ A Veramo resource resolver plugin. This plugin has the option to cache resources
|
|
21
21
|
- resourceDefaultStoreId
|
22
22
|
- resourceDefaultNamespace
|
23
23
|
- resourceDefaultTtl
|
24
|
-
-
|
24
|
+
-
|
25
|
+
|
25
26
|
## Usage
|
26
27
|
|
27
28
|
```typescript
|
@@ -42,8 +43,8 @@ const dbConnection = createConnection({
|
|
42
43
|
const agent = createAgent<IResourceResolver>({
|
43
44
|
plugins: [
|
44
45
|
new ResourceResolver({
|
45
|
-
resourceStores: new KeyValueStore({
|
46
|
-
store: new KeyValueTypeORMStoreAdapter({ dbConnection })
|
46
|
+
resourceStores: new KeyValueStore({
|
47
|
+
store: new KeyValueTypeORMStoreAdapter({ dbConnection }),
|
47
48
|
}),
|
48
49
|
}),
|
49
50
|
],
|
@@ -9,10 +9,12 @@ export declare class ResourceResolver implements IAgentPlugin {
|
|
9
9
|
private readonly defaultStoreId;
|
10
10
|
private readonly defaultNamespace;
|
11
11
|
private readonly defaultTtl;
|
12
|
+
private readonly detectLocation;
|
12
13
|
private readonly _resourceStores;
|
13
14
|
constructor(options?: ResourceResolverOptions);
|
14
15
|
/** {@inheritDoc IResourceResolver.resourceResolve} */
|
15
16
|
private resourceResolve;
|
17
|
+
private retrieveLocation;
|
16
18
|
/** {@inheritDoc IResourceResolver.resourceClearAllResources} */
|
17
19
|
private resourceClearAllResources;
|
18
20
|
/** {@inheritDoc IResourceResolver.resourceDefaultStoreId} */
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ResourceResolver.d.ts","sourceRoot":"","sources":["../../src/agent/ResourceResolver.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"ResourceResolver.d.ts","sourceRoot":"","sources":["../../src/agent/ResourceResolver.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAI3C,OAAO,EAIL,iBAAiB,EAKjB,uBAAuB,EAIxB,MAAM,4BAA4B,CAAA;AAEnC;;GAEG;AACH,qBAAa,gBAAiB,YAAW,YAAY;IACnD,QAAQ,CAAC,MAAM,MAA2B;IAC1C,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAMlC;IAED,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAQ;IACvC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAQ;IACzC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAQ;IACnC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAuC;gBAE3D,OAAO,CAAC,EAAE,uBAAuB;IAwB7C,sDAAsD;YACxC,eAAe;YAkDf,gBAAgB;IAc9B,gEAAgE;YAClD,yBAAyB;IAOvC,6DAA6D;YAC/C,sBAAsB;IAIpC,+DAA+D;YACjD,wBAAwB;IAItC,yDAAyD;YAC3C,kBAAkB;YAIlB,WAAW;YAUX,eAAe;IAe7B,OAAO,CAAC,KAAK;IASb,OAAO,CAAC,UAAU;IAKlB,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,MAAM;CAIf"}
|
@@ -50,10 +50,11 @@ class ResourceResolver {
|
|
50
50
|
resourceDefaultNamespace: this.resourceDefaultNamespace.bind(this),
|
51
51
|
resourceDefaultTtl: this.resourceDefaultTtl.bind(this),
|
52
52
|
};
|
53
|
-
const { defaultStore, defaultNamespace, resourceStores, ttl } = options !== null && options !== void 0 ? options : {};
|
53
|
+
const { defaultStore, defaultNamespace, resourceStores, ttl, detectLocation } = options !== null && options !== void 0 ? options : {};
|
54
54
|
this.defaultStoreId = defaultStore !== null && defaultStore !== void 0 ? defaultStore : '_default';
|
55
55
|
this.defaultNamespace = defaultNamespace !== null && defaultNamespace !== void 0 ? defaultNamespace : 'resources';
|
56
56
|
this.defaultTtl = ttl !== null && ttl !== void 0 ? ttl : 3600;
|
57
|
+
this.detectLocation = detectLocation !== null && detectLocation !== void 0 ? detectLocation : false;
|
57
58
|
if (resourceStores && resourceStores instanceof Map) {
|
58
59
|
this._resourceStores = resourceStores;
|
59
60
|
}
|
@@ -64,7 +65,7 @@ class ResourceResolver {
|
|
64
65
|
this._resourceStores = new Map().set(this.defaultStoreId, new ssi_sdk_kv_store_temp_1.KeyValueStore({
|
65
66
|
namespace: this.defaultNamespace,
|
66
67
|
store: new Map(),
|
67
|
-
ttl: this.defaultTtl
|
68
|
+
ttl: this.defaultTtl,
|
68
69
|
}));
|
69
70
|
}
|
70
71
|
}
|
@@ -81,23 +82,28 @@ class ResourceResolver {
|
|
81
82
|
return new cross_fetch_1.Response(JSON.stringify({ error: 'Resource not found' }), {
|
82
83
|
status: 404,
|
83
84
|
statusText: 'Not Found',
|
84
|
-
headers: new cross_fetch_1.Headers({ 'Content-Type': 'application/json' })
|
85
|
+
headers: new cross_fetch_1.Headers({ 'Content-Type': 'application/json' }),
|
85
86
|
});
|
86
87
|
}
|
88
|
+
let location;
|
89
|
+
if (this.detectLocation) {
|
90
|
+
location = yield this.retrieveLocation(input, context);
|
91
|
+
}
|
87
92
|
const response = yield (0, cross_fetch_1.default)(input, init);
|
88
|
-
if (!(resolveOpts === null || resolveOpts === void 0 ? void 0 : resolveOpts.skipPersistence) &&
|
93
|
+
if (!(resolveOpts === null || resolveOpts === void 0 ? void 0 : resolveOpts.skipPersistence) && response.status >= 200 && response.status < 300) {
|
89
94
|
const serializedResponse = yield (0, ResourceResolverUtils_1.serializeResponse)(response);
|
90
95
|
const resource = {
|
96
|
+
location,
|
91
97
|
response: serializedResponse,
|
92
98
|
resourceType,
|
93
99
|
insertedAt: Date.now(),
|
94
|
-
partyCorrelationId
|
100
|
+
partyCorrelationId,
|
95
101
|
};
|
96
102
|
const cachedResource = yield this.persistResource({
|
97
103
|
resource,
|
98
104
|
resourceIdentifier,
|
99
105
|
namespace,
|
100
|
-
storeId
|
106
|
+
storeId,
|
101
107
|
});
|
102
108
|
if (!cachedResource.value) {
|
103
109
|
return Promise.reject(Error('Resource not present in persistence result'));
|
@@ -107,6 +113,23 @@ class ResourceResolver {
|
|
107
113
|
return response;
|
108
114
|
});
|
109
115
|
}
|
116
|
+
retrieveLocation(input, context) {
|
117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
118
|
+
let url;
|
119
|
+
if (input instanceof Request && input.url !== undefined && input.url !== null) {
|
120
|
+
url = new URL(input.url);
|
121
|
+
}
|
122
|
+
else if (input instanceof URL) {
|
123
|
+
url = input;
|
124
|
+
}
|
125
|
+
else {
|
126
|
+
throw Error(`input type is required to be RequestInfo | URL`);
|
127
|
+
}
|
128
|
+
return yield context.agent.anomalyDetectionLookupLocation({
|
129
|
+
ipOrHostname: url.hostname,
|
130
|
+
});
|
131
|
+
});
|
132
|
+
}
|
110
133
|
/** {@inheritDoc IResourceResolver.resourceClearAllResources} */
|
111
134
|
resourceClearAllResources(args, context) {
|
112
135
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -139,7 +162,7 @@ class ResourceResolver {
|
|
139
162
|
const { resourceIdentifier, storeId, namespace } = args;
|
140
163
|
return this.store({ stores: this._resourceStores, storeId }).getAsValueData(this.prefix({
|
141
164
|
namespace,
|
142
|
-
resourceIdentifier
|
165
|
+
resourceIdentifier,
|
143
166
|
}));
|
144
167
|
});
|
145
168
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ResourceResolver.js","sourceRoot":"","sources":["../../src/agent/ResourceResolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
1
|
+
{"version":3,"file":"ResourceResolver.js","sourceRoot":"","sources":["../../src/agent/ResourceResolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2EAA2G;AAE3G,2DAAsD;AACtD,oCAAiC;AACjC,0EAAmI;AAgBnI;;GAEG;AACH,MAAa,gBAAgB;IAgB3B,YAAY,OAAiC;QAfpC,WAAM,GAAG,cAAM,CAAC,iBAAiB,CAAA;QACjC,YAAO,GAAsB;YACpC,eAAe,EAAE,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC;YAChD,yBAAyB,EAAE,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC;YACpE,sBAAsB,EAAE,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC;YAC9D,wBAAwB,EAAE,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,IAAI,CAAC;YAClE,kBAAkB,EAAE,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC;SACvD,CAAA;QASC,MAAM,EAAE,YAAY,EAAE,gBAAgB,EAAE,cAAc,EAAE,GAAG,EAAE,cAAc,EAAE,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAA;QAE7F,IAAI,CAAC,cAAc,GAAG,YAAY,aAAZ,YAAY,cAAZ,YAAY,GAAI,UAAU,CAAA;QAChD,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,aAAhB,gBAAgB,cAAhB,gBAAgB,GAAI,WAAW,CAAA;QACvD,IAAI,CAAC,UAAU,GAAG,GAAG,aA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E,OAAO,EAAE,GAAG,IAAI,CAAA;QACxB,OAAO,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,IAAI,CAAC,cAAc,CAAA;IACvC,CAAC;IAEO,YAAY,CAAC,IAAsB;QACzC,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAA;QAC1B,OAAO,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,IAAI,CAAC,gBAAgB,CAAA;IAC3C,CAAC;IAEO,MAAM,CAAC,IAAgB;QAC7B,MAAM,EAAE,SAAS,EAAE,kBAAkB,EAAE,GAAG,IAAI,CAAA;QAC9C,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,EAAE,SAAS,EAAE,CAAC,IAAI,kBAAkB,EAAE,CAAA;IACpE,CAAC;CACF;AAhLD,4CAgLC"}
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { IKeyValueStore, ValueStoreType } from '@sphereon/ssi-sdk.kv-store-temp';
|
2
2
|
import { IAgentContext, IPluginMethodMap } from '@veramo/core';
|
3
|
+
import { IAnomalyDetection } from '@sphereon/ssi-sdk.anomaly-detection';
|
3
4
|
export interface IResourceResolver extends IPluginMethodMap {
|
4
5
|
resourceResolve(args: ResolveArgs, context: RequiredContext): Promise<Response>;
|
5
6
|
resourceClearAllResources(args: ClearAllResourcesArgs, context: RequiredContext): Promise<boolean>;
|
@@ -12,6 +13,7 @@ export type ResourceResolverOptions = {
|
|
12
13
|
defaultNamespace?: string;
|
13
14
|
resourceStores?: Map<string, IKeyValueStore<Resource>> | IKeyValueStore<Resource>;
|
14
15
|
ttl?: number;
|
16
|
+
detectLocation?: boolean;
|
15
17
|
};
|
16
18
|
export type ResolveArgs = {
|
17
19
|
input: RequestInfo | URL;
|
@@ -58,7 +60,12 @@ export type StoreArgs<T extends ValueStoreType> = {
|
|
58
60
|
stores: Map<string, IKeyValueStore<T>>;
|
59
61
|
storeId?: string;
|
60
62
|
};
|
63
|
+
export type Location = {
|
64
|
+
continent?: string;
|
65
|
+
country?: string;
|
66
|
+
};
|
61
67
|
export type Resource = {
|
68
|
+
location?: Location | null;
|
62
69
|
response: SerializedResponse;
|
63
70
|
resourceType: ResourceType;
|
64
71
|
insertedAt: number;
|
@@ -72,5 +79,5 @@ export type SerializedResponse = {
|
|
72
79
|
};
|
73
80
|
body: string;
|
74
81
|
};
|
75
|
-
export type RequiredContext = IAgentContext<
|
82
|
+
export type RequiredContext = IAgentContext<IAnomalyDetection>;
|
76
83
|
//# sourceMappingURL=IResourceResolver.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"IResourceResolver.d.ts","sourceRoot":"","sources":["../../src/types/IResourceResolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAA;AAChF,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;
|
1
|
+
{"version":3,"file":"IResourceResolver.d.ts","sourceRoot":"","sources":["../../src/types/IResourceResolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAA;AAChF,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAA;AAEvE,MAAM,WAAW,iBAAkB,SAAQ,gBAAgB;IACzD,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC/E,yBAAyB,CAAC,IAAI,EAAE,qBAAqB,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClG,sBAAsB,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACjE,wBAAwB,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACnE,kBAAkB,CAAC,OA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1 @@
|
|
1
|
-
{"version":3,"file":"ResourceResolverUtils.d.ts","sourceRoot":"","sources":["../../src/utils/ResourceResolverUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAEzF,eAAO,MAAM,qBAAqB,UAAW,OAAO,GAAG,MAAM,GAAG,GAAG,KAAG,MAUrE,CAAA;AAED,eAAO,MAAM,iBAAiB,aAAoB,QAAQ,KAAG,OAAO,CAAC,kBAAkB,CAUtF,CAAA;AAED,eAAO,MAAM,mBAAmB,SAAgB,kBAAkB,KAAG,OAAO,CAAC,QAAQ,
|
1
|
+
{"version":3,"file":"ResourceResolverUtils.d.ts","sourceRoot":"","sources":["../../src/utils/ResourceResolverUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAEzF,eAAO,MAAM,qBAAqB,UAAW,OAAO,GAAG,MAAM,GAAG,GAAG,KAAG,MAUrE,CAAA;AAED,eAAO,MAAM,iBAAiB,aAAoB,QAAQ,KAAG,OAAO,CAAC,kBAAkB,CAUtF,CAAA;AAED,eAAO,MAAM,mBAAmB,SAAgB,kBAAkB,KAAG,OAAO,CAAC,QAAQ,CAWpF,CAAA;AAGD,eAAO,MAAM,mBAAmB,mBAAoB,QAAQ,gBAAgB,cAAc,KAAG,OAE5F,CAAA"}
|
@@ -55,7 +55,7 @@ const serializeResponse = (response) => __awaiter(void 0, void 0, void 0, functi
|
|
55
55
|
status: response.status,
|
56
56
|
statusText: response.statusText,
|
57
57
|
headers: Object.fromEntries(response.headers.entries()),
|
58
|
-
body: base64Url
|
58
|
+
body: base64Url,
|
59
59
|
};
|
60
60
|
});
|
61
61
|
exports.serializeResponse = serializeResponse;
|
@@ -66,13 +66,13 @@ const deserializeResponse = (data) => __awaiter(void 0, void 0, void 0, function
|
|
66
66
|
return new cross_fetch_1.Response(arrayBuffer, {
|
67
67
|
status,
|
68
68
|
statusText,
|
69
|
-
headers: new cross_fetch_1.Headers(headers)
|
69
|
+
headers: new cross_fetch_1.Headers(headers),
|
70
70
|
});
|
71
71
|
});
|
72
72
|
exports.deserializeResponse = deserializeResponse;
|
73
73
|
// Check if the cache is still within the acceptable age
|
74
74
|
const isCacheWithinMaxAge = (cachedResource, resolveOpts) => {
|
75
|
-
return cachedResource && ((resolveOpts === null || resolveOpts === void 0 ? void 0 : resolveOpts.maxAgeMs) === undefined ||
|
75
|
+
return cachedResource && ((resolveOpts === null || resolveOpts === void 0 ? void 0 : resolveOpts.maxAgeMs) === undefined || Date.now() - cachedResource.insertedAt < resolveOpts.maxAgeMs);
|
76
76
|
};
|
77
77
|
exports.isCacheWithinMaxAge = isCacheWithinMaxAge;
|
78
78
|
//# sourceMappingURL=ResourceResolverUtils.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ResourceResolverUtils.js","sourceRoot":"","sources":["../../src/utils/ResourceResolverUtils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAwD;AACxD,iDAAkC;AAG3B,MAAM,qBAAqB,GAAG,CAAC,KAA6B,EAAU,EAAE;IAC7E,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,
|
1
|
+
{"version":3,"file":"ResourceResolverUtils.js","sourceRoot":"","sources":["../../src/utils/ResourceResolverUtils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAwD;AACxD,iDAAkC;AAG3B,MAAM,qBAAqB,GAAG,CAAC,KAA6B,EAAU,EAAE;IAC7E,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAA;IACd,CAAC;SAAM,IAAI,KAAK,YAAY,qBAAO,EAAE,CAAC;QACpC,OAAO,KAAK,CAAC,GAAG,CAAA;IAClB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AwD;AACjD,MAAM,mBAAmB,GAAG,CAAC,cAAwB,EAAE,WAA4B,EAAW,EAAE;IACrG,OAAO,cAAc,IAAI,CAAC,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,QAAQ,MAAK,SAAS,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,cAAc,CAAC,UAAU,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAA;AACjI,CAAC,CAAA;AAFY,QAAA,mBAAmB,uBAE/B"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@sphereon/ssi-sdk.resource-resolver",
|
3
|
-
"version": "0.30.2-feature.
|
3
|
+
"version": "0.30.2-feature.mdoc.funke2.366+ef5ec5ec",
|
4
4
|
"source": "src/index.ts",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -14,14 +14,15 @@
|
|
14
14
|
"build:clean": "tsc --build --clean && tsc --build"
|
15
15
|
},
|
16
16
|
"dependencies": {
|
17
|
-
"@sphereon/ssi-sdk.
|
17
|
+
"@sphereon/ssi-sdk.anomaly-detection": "0.30.2-feature.mdoc.funke2.366+ef5ec5ec",
|
18
|
+
"@sphereon/ssi-sdk.kv-store-temp": "0.30.2-feature.mdoc.funke2.366+ef5ec5ec",
|
18
19
|
"cross-fetch": "^3.1.8",
|
19
20
|
"debug": "^4.3.5",
|
20
21
|
"typeorm": "^0.3.20",
|
21
22
|
"uint8arrays": "^3.1.1"
|
22
23
|
},
|
23
24
|
"devDependencies": {
|
24
|
-
"@sphereon/ssi-sdk.agent-config": "0.30.2-feature.
|
25
|
+
"@sphereon/ssi-sdk.agent-config": "0.30.2-feature.mdoc.funke2.366+ef5ec5ec",
|
25
26
|
"@veramo/remote-client": "4.2.0",
|
26
27
|
"@veramo/remote-server": "4.2.0",
|
27
28
|
"nock": "^13.5.4",
|
@@ -48,5 +49,5 @@
|
|
48
49
|
"Resource Management",
|
49
50
|
"Caching"
|
50
51
|
],
|
51
|
-
"gitHead": "
|
52
|
+
"gitHead": "ef5ec5ec5f8c24bd38b5d4846f0ad27b582f5bc0"
|
52
53
|
}
|
@@ -1,18 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
IKeyValueStore,
|
3
|
-
IValueData,
|
4
|
-
KeyValueStore,
|
5
|
-
ValueStoreType
|
6
|
-
} from '@sphereon/ssi-sdk.kv-store-temp'
|
1
|
+
import { IKeyValueStore, IValueData, KeyValueStore, ValueStoreType } from '@sphereon/ssi-sdk.kv-store-temp'
|
7
2
|
import { IAgentPlugin } from '@veramo/core'
|
8
3
|
import fetch, { Response, Headers } from 'cross-fetch'
|
9
4
|
import { schema } from '../index'
|
10
|
-
import {
|
11
|
-
deserializeResponse,
|
12
|
-
getResourceIdentifier,
|
13
|
-
isCacheWithinMaxAge,
|
14
|
-
serializeResponse
|
15
|
-
} from '../utils/ResourceResolverUtils'
|
5
|
+
import { deserializeResponse, getResourceIdentifier, isCacheWithinMaxAge, serializeResponse } from '../utils/ResourceResolverUtils'
|
16
6
|
import {
|
17
7
|
ClearAllResourcesArgs,
|
18
8
|
ResolveArgs,
|
@@ -25,7 +15,7 @@ import {
|
|
25
15
|
ResourceResolverOptions,
|
26
16
|
StoreArgs,
|
27
17
|
StoreIdStrArgs,
|
28
|
-
Resource
|
18
|
+
Resource,
|
29
19
|
} from '../types/IResourceResolver'
|
30
20
|
|
31
21
|
/**
|
@@ -44,19 +34,16 @@ export class ResourceResolver implements IAgentPlugin {
|
|
44
34
|
private readonly defaultStoreId: string
|
45
35
|
private readonly defaultNamespace: string
|
46
36
|
private readonly defaultTtl: number
|
37
|
+
private readonly detectLocation: boolean
|
47
38
|
private readonly _resourceStores: Map<string, IKeyValueStore<Resource>>
|
48
39
|
|
49
40
|
constructor(options?: ResourceResolverOptions) {
|
50
|
-
const {
|
51
|
-
defaultStore,
|
52
|
-
defaultNamespace,
|
53
|
-
resourceStores,
|
54
|
-
ttl
|
55
|
-
} = options ?? {}
|
41
|
+
const { defaultStore, defaultNamespace, resourceStores, ttl, detectLocation } = options ?? {}
|
56
42
|
|
57
43
|
this.defaultStoreId = defaultStore ?? '_default'
|
58
44
|
this.defaultNamespace = defaultNamespace ?? 'resources'
|
59
45
|
this.defaultTtl = ttl ?? 3600
|
46
|
+
this.detectLocation = detectLocation ?? false
|
60
47
|
|
61
48
|
if (resourceStores && resourceStores instanceof Map) {
|
62
49
|
this._resourceStores = resourceStores
|
@@ -68,53 +55,51 @@ export class ResourceResolver implements IAgentPlugin {
|
|
68
55
|
new KeyValueStore({
|
69
56
|
namespace: this.defaultNamespace,
|
70
57
|
store: new Map<string, Resource>(),
|
71
|
-
ttl: this.defaultTtl
|
72
|
-
})
|
58
|
+
ttl: this.defaultTtl,
|
59
|
+
}),
|
73
60
|
)
|
74
61
|
}
|
75
62
|
}
|
76
63
|
|
77
64
|
/** {@inheritDoc IResourceResolver.resourceResolve} */
|
78
65
|
private async resourceResolve(args: ResolveArgs, context: RequiredContext): Promise<Response> {
|
79
|
-
const {
|
80
|
-
input,
|
81
|
-
init,
|
82
|
-
resourceType,
|
83
|
-
resolveOpts,
|
84
|
-
partyCorrelationId,
|
85
|
-
storeId,
|
86
|
-
namespace
|
87
|
-
} = args
|
66
|
+
const { input, init, resourceType, resolveOpts, partyCorrelationId, storeId, namespace } = args
|
88
67
|
|
89
68
|
const resourceIdentifier = getResourceIdentifier(input)
|
90
69
|
|
91
70
|
const cachedResource = await this.getResource({ resourceIdentifier, storeId, namespace })
|
92
71
|
if (cachedResource.value && isCacheWithinMaxAge(cachedResource.value, resolveOpts)) {
|
93
|
-
return deserializeResponse(cachedResource.value.response)
|
72
|
+
return deserializeResponse(cachedResource.value.response)
|
94
73
|
}
|
95
74
|
|
96
75
|
if (resolveOpts?.onlyCache) {
|
97
76
|
return new Response(JSON.stringify({ error: 'Resource not found' }), {
|
98
77
|
status: 404,
|
99
78
|
statusText: 'Not Found',
|
100
|
-
headers: new Headers({ 'Content-Type': 'application/json' })
|
101
|
-
})
|
79
|
+
headers: new Headers({ 'Content-Type': 'application/json' }),
|
80
|
+
})
|
81
|
+
}
|
82
|
+
|
83
|
+
let location
|
84
|
+
if (this.detectLocation) {
|
85
|
+
location = await this.retrieveLocation(input, context)
|
102
86
|
}
|
103
87
|
|
104
88
|
const response = await fetch(input, init)
|
105
|
-
if (!resolveOpts?.skipPersistence &&
|
106
|
-
const serializedResponse = await serializeResponse(response)
|
107
|
-
const resource = {
|
89
|
+
if (!resolveOpts?.skipPersistence && response.status >= 200 && response.status < 300) {
|
90
|
+
const serializedResponse = await serializeResponse(response)
|
91
|
+
const resource: Resource = {
|
92
|
+
location,
|
108
93
|
response: serializedResponse,
|
109
94
|
resourceType,
|
110
95
|
insertedAt: Date.now(),
|
111
|
-
partyCorrelationId
|
96
|
+
partyCorrelationId,
|
112
97
|
}
|
113
98
|
const cachedResource = await this.persistResource({
|
114
99
|
resource,
|
115
100
|
resourceIdentifier,
|
116
101
|
namespace,
|
117
|
-
storeId
|
102
|
+
storeId,
|
118
103
|
})
|
119
104
|
|
120
105
|
if (!cachedResource.value) {
|
@@ -127,6 +112,20 @@ export class ResourceResolver implements IAgentPlugin {
|
|
127
112
|
return response
|
128
113
|
}
|
129
114
|
|
115
|
+
private async retrieveLocation(input: RequestInfo | URL, context: RequiredContext) {
|
116
|
+
let url: URL
|
117
|
+
if (input instanceof Request && input.url !== undefined && input.url !== null) {
|
118
|
+
url = new URL(input.url)
|
119
|
+
} else if (input instanceof URL) {
|
120
|
+
url = input
|
121
|
+
} else {
|
122
|
+
throw Error(`input type is required to be RequestInfo | URL`)
|
123
|
+
}
|
124
|
+
return await context.agent.anomalyDetectionLookupLocation({
|
125
|
+
ipOrHostname: url.hostname,
|
126
|
+
})
|
127
|
+
}
|
128
|
+
|
130
129
|
/** {@inheritDoc IResourceResolver.resourceClearAllResources} */
|
131
130
|
private async resourceClearAllResources(args: ClearAllResourcesArgs, context: RequiredContext): Promise<boolean> {
|
132
131
|
const { storeId } = args
|
@@ -155,7 +154,7 @@ export class ResourceResolver implements IAgentPlugin {
|
|
155
154
|
return this.store({ stores: this._resourceStores, storeId }).getAsValueData(
|
156
155
|
this.prefix({
|
157
156
|
namespace,
|
158
|
-
resourceIdentifier
|
157
|
+
resourceIdentifier,
|
159
158
|
}),
|
160
159
|
)
|
161
160
|
}
|
@@ -198,6 +197,4 @@ export class ResourceResolver implements IAgentPlugin {
|
|
198
197
|
const { namespace, resourceIdentifier } = args
|
199
198
|
return `${this.namespaceStr({ namespace })}:${resourceIdentifier}`
|
200
199
|
}
|
201
|
-
|
202
200
|
}
|
203
|
-
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { IKeyValueStore, ValueStoreType } from '@sphereon/ssi-sdk.kv-store-temp'
|
2
2
|
import { IAgentContext, IPluginMethodMap } from '@veramo/core'
|
3
|
+
import { IAnomalyDetection } from '@sphereon/ssi-sdk.anomaly-detection'
|
3
4
|
|
4
5
|
export interface IResourceResolver extends IPluginMethodMap {
|
5
6
|
resourceResolve(args: ResolveArgs, context: RequiredContext): Promise<Response>
|
@@ -14,6 +15,7 @@ export type ResourceResolverOptions = {
|
|
14
15
|
defaultNamespace?: string
|
15
16
|
resourceStores?: Map<string, IKeyValueStore<Resource>> | IKeyValueStore<Resource>
|
16
17
|
ttl?: number
|
18
|
+
detectLocation?: boolean
|
17
19
|
}
|
18
20
|
|
19
21
|
export type ResolveArgs = {
|
@@ -62,7 +64,7 @@ export type NamespaceStrArgs = {
|
|
62
64
|
}
|
63
65
|
|
64
66
|
export type PrefixArgs = {
|
65
|
-
namespace?: string
|
67
|
+
namespace?: string
|
66
68
|
resourceIdentifier: string
|
67
69
|
}
|
68
70
|
|
@@ -71,7 +73,13 @@ export type StoreArgs<T extends ValueStoreType> = {
|
|
71
73
|
storeId?: string
|
72
74
|
}
|
73
75
|
|
76
|
+
export type Location = {
|
77
|
+
continent?: string
|
78
|
+
country?: string
|
79
|
+
}
|
80
|
+
|
74
81
|
export type Resource = {
|
82
|
+
location?: Location | null
|
75
83
|
response: SerializedResponse
|
76
84
|
resourceType: ResourceType
|
77
85
|
insertedAt: number
|
@@ -85,4 +93,4 @@ export type SerializedResponse = {
|
|
85
93
|
body: string
|
86
94
|
}
|
87
95
|
|
88
|
-
export type RequiredContext = IAgentContext<
|
96
|
+
export type RequiredContext = IAgentContext<IAnomalyDetection>
|
@@ -4,47 +4,42 @@ import { ResolveOptions, Resource, SerializedResponse } from '../types/IResource
|
|
4
4
|
|
5
5
|
export const getResourceIdentifier = (input: Request | string | URL): string => {
|
6
6
|
if (typeof input === 'string') {
|
7
|
-
return input
|
7
|
+
return input
|
8
8
|
} else if (input instanceof Request) {
|
9
|
-
return input.url
|
9
|
+
return input.url
|
10
10
|
} else if (input instanceof URL) {
|
11
|
-
return input.toString()
|
11
|
+
return input.toString()
|
12
12
|
}
|
13
13
|
|
14
|
-
throw new Error('Invalid input type. Expected Request, string, or URL.')
|
14
|
+
throw new Error('Invalid input type. Expected Request, string, or URL.')
|
15
15
|
}
|
16
16
|
|
17
17
|
export const serializeResponse = async (response: Response): Promise<SerializedResponse> => {
|
18
|
-
const arrayBuffer = await response.arrayBuffer()
|
19
|
-
const base64Url = u8a.toString(new Uint8Array(arrayBuffer), 'base64url')
|
18
|
+
const arrayBuffer = await response.arrayBuffer()
|
19
|
+
const base64Url = u8a.toString(new Uint8Array(arrayBuffer), 'base64url')
|
20
20
|
|
21
21
|
return {
|
22
22
|
status: response.status,
|
23
23
|
statusText: response.statusText,
|
24
24
|
headers: Object.fromEntries(response.headers.entries()),
|
25
|
-
body: base64Url
|
25
|
+
body: base64Url,
|
26
26
|
}
|
27
27
|
}
|
28
28
|
|
29
29
|
export const deserializeResponse = async (data: SerializedResponse): Promise<Response> => {
|
30
|
-
const {
|
31
|
-
status,
|
32
|
-
statusText,
|
33
|
-
headers,
|
34
|
-
body
|
35
|
-
} = data
|
30
|
+
const { status, statusText, headers, body } = data
|
36
31
|
|
37
32
|
const uint8Array = u8a.fromString(body, 'base64url')
|
38
|
-
const arrayBuffer = uint8Array.buffer.slice(uint8Array.byteOffset, uint8Array.byteOffset + uint8Array.byteLength)
|
33
|
+
const arrayBuffer = uint8Array.buffer.slice(uint8Array.byteOffset, uint8Array.byteOffset + uint8Array.byteLength)
|
39
34
|
|
40
35
|
return new Response(arrayBuffer, {
|
41
36
|
status,
|
42
37
|
statusText,
|
43
|
-
headers: new Headers(headers)
|
38
|
+
headers: new Headers(headers),
|
44
39
|
})
|
45
40
|
}
|
46
41
|
|
47
42
|
// Check if the cache is still within the acceptable age
|
48
43
|
export const isCacheWithinMaxAge = (cachedResource: Resource, resolveOpts?: ResolveOptions): boolean => {
|
49
|
-
return cachedResource && (resolveOpts?.maxAgeMs === undefined ||
|
44
|
+
return cachedResource && (resolveOpts?.maxAgeMs === undefined || Date.now() - cachedResource.insertedAt < resolveOpts.maxAgeMs)
|
50
45
|
}
|