@sphereon/ssi-sdk.resource-resolver 0.30.2-feature.FIDES.integration.223
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/LICENSE +201 -0
- package/README.md +97 -0
- package/dist/agent/ResourceResolver.d.ts +31 -0
- package/dist/agent/ResourceResolver.d.ts.map +1 -0
- package/dist/agent/ResourceResolver.js +179 -0
- package/dist/agent/ResourceResolver.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/types/IResourceResolver.d.ts +76 -0
- package/dist/types/IResourceResolver.d.ts.map +1 -0
- package/dist/types/IResourceResolver.js +3 -0
- package/dist/types/IResourceResolver.js.map +1 -0
- package/dist/utils/ResourceResolverUtils.d.ts +6 -0
- package/dist/utils/ResourceResolverUtils.d.ts.map +1 -0
- package/dist/utils/ResourceResolverUtils.js +78 -0
- package/dist/utils/ResourceResolverUtils.js.map +1 -0
- package/package.json +52 -0
- package/plugin.schema.json +88 -0
- package/src/agent/ResourceResolver.ts +203 -0
- package/src/index.ts +7 -0
- package/src/types/IResourceResolver.ts +88 -0
- package/src/utils/ResourceResolverUtils.ts +50 -0
package/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [2024] [Sphereon International B.V.]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
<!--suppress HtmlDeprecatedAttribute -->
|
2
|
+
<h1 align="center">
|
3
|
+
<br>
|
4
|
+
<a href="https://www.sphereon.com"><img src="https://sphereon.com/content/themes/sphereon/assets/img/logo.svg" alt="Sphereon" width="400"></a>
|
5
|
+
<br>Resource Resolver (Typescript)
|
6
|
+
<br>
|
7
|
+
</h1>
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
**Warning: This package still is in very early development. Breaking changes without notice will happen at this point!**
|
12
|
+
|
13
|
+
---
|
14
|
+
|
15
|
+
A Veramo resource resolver plugin. This plugin has the option to cache resources and fetch them from a storage.
|
16
|
+
|
17
|
+
## Available functions
|
18
|
+
|
19
|
+
- resourceResolve
|
20
|
+
- resourceClearAllResources
|
21
|
+
- resourceDefaultStoreId
|
22
|
+
- resourceDefaultNamespace
|
23
|
+
- resourceDefaultTtl
|
24
|
+
-
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```typescript
|
28
|
+
import { IResourceResolver, ResourceResolver } from '@sphereon/ssi-sdk.resource-resolver'
|
29
|
+
import { KeyValueStoreEntity, kvStoreMigrations, KeyValueStore, KeyValueTypeORMStoreAdapter } from '@sphereon/ssi-sdk.kv-store-temp'
|
30
|
+
|
31
|
+
const dbConnection = createConnection({
|
32
|
+
type: 'react-native',
|
33
|
+
database: 'app.sqlite',
|
34
|
+
location: 'default',
|
35
|
+
logging: false,
|
36
|
+
synchronize: false,
|
37
|
+
migrationsRun: true,
|
38
|
+
migrations: kvStoreMigrations,
|
39
|
+
entities: [KeyValueStoreEntity],
|
40
|
+
})
|
41
|
+
|
42
|
+
const agent = createAgent<IResourceResolver>({
|
43
|
+
plugins: [
|
44
|
+
new ResourceResolver({
|
45
|
+
resourceStores: new KeyValueStore({
|
46
|
+
store: new KeyValueTypeORMStoreAdapter({ dbConnection })
|
47
|
+
}),
|
48
|
+
}),
|
49
|
+
],
|
50
|
+
})
|
51
|
+
```
|
52
|
+
|
53
|
+
### Resolve a resource:
|
54
|
+
|
55
|
+
```typescript
|
56
|
+
const response = await agent.resourceResolve({
|
57
|
+
input: 'https://example.com/example_resource.jpg',
|
58
|
+
resourceType: 'example_type',
|
59
|
+
})
|
60
|
+
```
|
61
|
+
|
62
|
+
### Clear all resources:
|
63
|
+
|
64
|
+
```typescript
|
65
|
+
const storeId = '3c0e6c59-fe47-433c-a3d2-ad18b9c83517'
|
66
|
+
const result = await agent.resourceClearAllResources({ storeId })
|
67
|
+
```
|
68
|
+
|
69
|
+
### Get default store id:
|
70
|
+
|
71
|
+
```typescript
|
72
|
+
const result = await agent.resourceDefaultStoreId()
|
73
|
+
```
|
74
|
+
|
75
|
+
### Get default namespace:
|
76
|
+
|
77
|
+
```typescript
|
78
|
+
const result = await agent.resourceDefaultNamespace()
|
79
|
+
```
|
80
|
+
|
81
|
+
### Get default ttl:
|
82
|
+
|
83
|
+
```typescript
|
84
|
+
const result = await agent.resourceDefaultTtl()
|
85
|
+
```
|
86
|
+
|
87
|
+
## Installation
|
88
|
+
|
89
|
+
```shell
|
90
|
+
yarn add @sphereon/ssi-sdk.resource-resolver
|
91
|
+
```
|
92
|
+
|
93
|
+
## Build
|
94
|
+
|
95
|
+
```shell
|
96
|
+
yarn build
|
97
|
+
```
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { IAgentPlugin } from '@veramo/core';
|
2
|
+
import { IResourceResolver, ResourceResolverOptions } from '../types/IResourceResolver';
|
3
|
+
/**
|
4
|
+
* {@inheritDoc IResourceResolver}
|
5
|
+
*/
|
6
|
+
export declare class ResourceResolver implements IAgentPlugin {
|
7
|
+
readonly schema: any;
|
8
|
+
readonly methods: IResourceResolver;
|
9
|
+
private readonly defaultStoreId;
|
10
|
+
private readonly defaultNamespace;
|
11
|
+
private readonly defaultTtl;
|
12
|
+
private readonly _resourceStores;
|
13
|
+
constructor(options?: ResourceResolverOptions);
|
14
|
+
/** {@inheritDoc IResourceResolver.resourceResolve} */
|
15
|
+
private resourceResolve;
|
16
|
+
/** {@inheritDoc IResourceResolver.resourceClearAllResources} */
|
17
|
+
private resourceClearAllResources;
|
18
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultStoreId} */
|
19
|
+
private resourceDefaultStoreId;
|
20
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultNamespace} */
|
21
|
+
private resourceDefaultNamespace;
|
22
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultTtl} */
|
23
|
+
private resourceDefaultTtl;
|
24
|
+
private getResource;
|
25
|
+
private persistResource;
|
26
|
+
private store;
|
27
|
+
private storeIdStr;
|
28
|
+
private namespaceStr;
|
29
|
+
private prefix;
|
30
|
+
}
|
31
|
+
//# sourceMappingURL=ResourceResolver.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ResourceResolver.d.ts","sourceRoot":"","sources":["../../src/agent/ResourceResolver.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAS3C,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,eAAe,CAAuC;gBAE3D,OAAO,CAAC,EAAE,uBAAuB;IA4B7C,sDAAsD;YACxC,eAAe;IAoD7B,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;CAKf"}
|
@@ -0,0 +1,179 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
32
|
+
});
|
33
|
+
};
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
35
|
+
exports.ResourceResolver = void 0;
|
36
|
+
const ssi_sdk_kv_store_temp_1 = require("@sphereon/ssi-sdk.kv-store-temp");
|
37
|
+
const cross_fetch_1 = __importStar(require("cross-fetch"));
|
38
|
+
const index_1 = require("../index");
|
39
|
+
const ResourceResolverUtils_1 = require("../utils/ResourceResolverUtils");
|
40
|
+
/**
|
41
|
+
* {@inheritDoc IResourceResolver}
|
42
|
+
*/
|
43
|
+
class ResourceResolver {
|
44
|
+
constructor(options) {
|
45
|
+
this.schema = index_1.schema.IResourceResolver;
|
46
|
+
this.methods = {
|
47
|
+
resourceResolve: this.resourceResolve.bind(this),
|
48
|
+
resourceClearAllResources: this.resourceClearAllResources.bind(this),
|
49
|
+
resourceDefaultStoreId: this.resourceDefaultStoreId.bind(this),
|
50
|
+
resourceDefaultNamespace: this.resourceDefaultNamespace.bind(this),
|
51
|
+
resourceDefaultTtl: this.resourceDefaultTtl.bind(this),
|
52
|
+
};
|
53
|
+
const { defaultStore, defaultNamespace, resourceStores, ttl } = options !== null && options !== void 0 ? options : {};
|
54
|
+
this.defaultStoreId = defaultStore !== null && defaultStore !== void 0 ? defaultStore : '_default';
|
55
|
+
this.defaultNamespace = defaultNamespace !== null && defaultNamespace !== void 0 ? defaultNamespace : 'resources';
|
56
|
+
this.defaultTtl = ttl !== null && ttl !== void 0 ? ttl : 3600;
|
57
|
+
if (resourceStores && resourceStores instanceof Map) {
|
58
|
+
this._resourceStores = resourceStores;
|
59
|
+
}
|
60
|
+
else if (resourceStores) {
|
61
|
+
this._resourceStores = new Map().set(this.defaultStoreId, resourceStores);
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
this._resourceStores = new Map().set(this.defaultStoreId, new ssi_sdk_kv_store_temp_1.KeyValueStore({
|
65
|
+
namespace: this.defaultNamespace,
|
66
|
+
store: new Map(),
|
67
|
+
ttl: this.defaultTtl
|
68
|
+
}));
|
69
|
+
}
|
70
|
+
}
|
71
|
+
/** {@inheritDoc IResourceResolver.resourceResolve} */
|
72
|
+
resourceResolve(args, context) {
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
74
|
+
const { input, init, resourceType, resolveOpts, partyCorrelationId, storeId, namespace } = args;
|
75
|
+
const resourceIdentifier = (0, ResourceResolverUtils_1.getResourceIdentifier)(input);
|
76
|
+
const cachedResource = yield this.getResource({ resourceIdentifier, storeId, namespace });
|
77
|
+
if (cachedResource.value && (0, ResourceResolverUtils_1.isCacheWithinMaxAge)(cachedResource.value, resolveOpts)) {
|
78
|
+
return (0, ResourceResolverUtils_1.deserializeResponse)(cachedResource.value.response);
|
79
|
+
}
|
80
|
+
if (resolveOpts === null || resolveOpts === void 0 ? void 0 : resolveOpts.onlyCache) {
|
81
|
+
return new cross_fetch_1.Response(JSON.stringify({ error: 'Resource not found' }), {
|
82
|
+
status: 404,
|
83
|
+
statusText: 'Not Found',
|
84
|
+
headers: new cross_fetch_1.Headers({ 'Content-Type': 'application/json' })
|
85
|
+
});
|
86
|
+
}
|
87
|
+
const response = yield (0, cross_fetch_1.default)(input, init);
|
88
|
+
if (!(resolveOpts === null || resolveOpts === void 0 ? void 0 : resolveOpts.skipPersistence) && (response.status >= 200 && response.status < 300)) {
|
89
|
+
const serializedResponse = yield (0, ResourceResolverUtils_1.serializeResponse)(response);
|
90
|
+
const resource = {
|
91
|
+
response: serializedResponse,
|
92
|
+
resourceType,
|
93
|
+
insertedAt: Date.now(),
|
94
|
+
partyCorrelationId
|
95
|
+
};
|
96
|
+
const cachedResource = yield this.persistResource({
|
97
|
+
resource,
|
98
|
+
resourceIdentifier,
|
99
|
+
namespace,
|
100
|
+
storeId
|
101
|
+
});
|
102
|
+
if (!cachedResource.value) {
|
103
|
+
return Promise.reject(Error('Resource not present in persistence result'));
|
104
|
+
}
|
105
|
+
return (0, ResourceResolverUtils_1.deserializeResponse)(cachedResource.value.response);
|
106
|
+
}
|
107
|
+
return response;
|
108
|
+
});
|
109
|
+
}
|
110
|
+
/** {@inheritDoc IResourceResolver.resourceClearAllResources} */
|
111
|
+
resourceClearAllResources(args, context) {
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
113
|
+
const { storeId } = args;
|
114
|
+
return yield this.store({ stores: this._resourceStores, storeId })
|
115
|
+
.clear()
|
116
|
+
.then(() => true);
|
117
|
+
});
|
118
|
+
}
|
119
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultStoreId} */
|
120
|
+
resourceDefaultStoreId(context) {
|
121
|
+
return __awaiter(this, void 0, void 0, function* () {
|
122
|
+
return this.defaultStoreId;
|
123
|
+
});
|
124
|
+
}
|
125
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultNamespace} */
|
126
|
+
resourceDefaultNamespace(context) {
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
128
|
+
return this.defaultNamespace;
|
129
|
+
});
|
130
|
+
}
|
131
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultTtl} */
|
132
|
+
resourceDefaultTtl(context) {
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
134
|
+
return this.defaultTtl;
|
135
|
+
});
|
136
|
+
}
|
137
|
+
getResource(args) {
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
139
|
+
const { resourceIdentifier, storeId, namespace } = args;
|
140
|
+
return this.store({ stores: this._resourceStores, storeId }).getAsValueData(this.prefix({
|
141
|
+
namespace,
|
142
|
+
resourceIdentifier
|
143
|
+
}));
|
144
|
+
});
|
145
|
+
}
|
146
|
+
persistResource(args) {
|
147
|
+
return __awaiter(this, void 0, void 0, function* () {
|
148
|
+
const { resource, resourceIdentifier, ttl } = args;
|
149
|
+
const namespace = this.namespaceStr(args);
|
150
|
+
const storeId = this.storeIdStr(args);
|
151
|
+
return yield this.store({ stores: this._resourceStores, storeId }).set(this.prefix({
|
152
|
+
namespace,
|
153
|
+
resourceIdentifier,
|
154
|
+
}), resource, ttl !== null && ttl !== void 0 ? ttl : this.defaultTtl);
|
155
|
+
});
|
156
|
+
}
|
157
|
+
store(args) {
|
158
|
+
const storeId = this.storeIdStr({ storeId: args.storeId });
|
159
|
+
const store = args.stores.get(storeId);
|
160
|
+
if (!store) {
|
161
|
+
throw Error(`Could not get resource store: ${storeId}`);
|
162
|
+
}
|
163
|
+
return store;
|
164
|
+
}
|
165
|
+
storeIdStr(args) {
|
166
|
+
const { storeId } = args;
|
167
|
+
return storeId !== null && storeId !== void 0 ? storeId : this.defaultStoreId;
|
168
|
+
}
|
169
|
+
namespaceStr(args) {
|
170
|
+
const { namespace } = args;
|
171
|
+
return namespace !== null && namespace !== void 0 ? namespace : this.defaultNamespace;
|
172
|
+
}
|
173
|
+
prefix(args) {
|
174
|
+
const { namespace, resourceIdentifier } = args;
|
175
|
+
return `${this.namespaceStr({ namespace })}:${resourceIdentifier}`;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
exports.ResourceResolver = ResourceResolver;
|
179
|
+
//# sourceMappingURL=ResourceResolver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AC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACtC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,KAAK,CAAC,iCAAiC,OAAO,EAAE,CAAC,CAAA;QACzD,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAEO,UAAU,CAAC,IAAoB;QACrC,MAAM,EAAE,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;CAEF;AAxKD,4CAwKC"}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,QAAA,MAAM,MAAM,KAAmC,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,CAAA;AACjB,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAC3D,cAAc,2BAA2B,CAAA"}
|
package/dist/index.js
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
exports.ResourceResolver = exports.schema = void 0;
|
18
|
+
/**
|
19
|
+
* @public
|
20
|
+
*/
|
21
|
+
const schema = require('../plugin.schema.json');
|
22
|
+
exports.schema = schema;
|
23
|
+
var ResourceResolver_1 = require("./agent/ResourceResolver");
|
24
|
+
Object.defineProperty(exports, "ResourceResolver", { enumerable: true, get: function () { return ResourceResolver_1.ResourceResolver; } });
|
25
|
+
__exportStar(require("./types/IResourceResolver"), exports);
|
26
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA;;GAEG;AACH,MAAM,MAAM,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;AACtC,wBAAM;AACf,6DAA2D;AAAlD,oHAAA,gBAAgB,OAAA;AACzB,4DAAyC"}
|
@@ -0,0 +1,76 @@
|
|
1
|
+
import { IKeyValueStore, ValueStoreType } from '@sphereon/ssi-sdk.kv-store-temp';
|
2
|
+
import { IAgentContext, IPluginMethodMap } from '@veramo/core';
|
3
|
+
export interface IResourceResolver extends IPluginMethodMap {
|
4
|
+
resourceResolve(args: ResolveArgs, context: RequiredContext): Promise<Response>;
|
5
|
+
resourceClearAllResources(args: ClearAllResourcesArgs, context: RequiredContext): Promise<boolean>;
|
6
|
+
resourceDefaultStoreId(context: RequiredContext): Promise<string>;
|
7
|
+
resourceDefaultNamespace(context: RequiredContext): Promise<string>;
|
8
|
+
resourceDefaultTtl(context: RequiredContext): Promise<number>;
|
9
|
+
}
|
10
|
+
export type ResourceResolverOptions = {
|
11
|
+
defaultStore?: string;
|
12
|
+
defaultNamespace?: string;
|
13
|
+
resourceStores?: Map<string, IKeyValueStore<Resource>> | IKeyValueStore<Resource>;
|
14
|
+
ttl?: number;
|
15
|
+
};
|
16
|
+
export type ResolveArgs = {
|
17
|
+
input: RequestInfo | URL;
|
18
|
+
init?: RequestInit;
|
19
|
+
resourceType: ResourceType;
|
20
|
+
partyCorrelationId?: string;
|
21
|
+
storeId?: string;
|
22
|
+
namespace?: string;
|
23
|
+
resolveOpts?: ResolveOptions;
|
24
|
+
};
|
25
|
+
export type ResolveOptions = {
|
26
|
+
ttl?: number;
|
27
|
+
maxAgeMs?: number;
|
28
|
+
onlyCache?: boolean;
|
29
|
+
skipPersistence?: boolean;
|
30
|
+
};
|
31
|
+
export type ResourceType = 'credential_branding_image' | 'issuer_branding_image' | 'oid4vci_metadata' | string;
|
32
|
+
export type ClearAllResourcesArgs = {
|
33
|
+
storeId?: string;
|
34
|
+
};
|
35
|
+
export type PersistResourceArgs = {
|
36
|
+
resource: Resource;
|
37
|
+
resourceIdentifier: string;
|
38
|
+
ttl?: number;
|
39
|
+
storeId?: string;
|
40
|
+
namespace?: string;
|
41
|
+
};
|
42
|
+
export type GetResourceArgs = {
|
43
|
+
resourceIdentifier: string;
|
44
|
+
storeId?: string;
|
45
|
+
namespace?: string;
|
46
|
+
};
|
47
|
+
export type StoreIdStrArgs = {
|
48
|
+
storeId?: string;
|
49
|
+
};
|
50
|
+
export type NamespaceStrArgs = {
|
51
|
+
namespace?: string;
|
52
|
+
};
|
53
|
+
export type PrefixArgs = {
|
54
|
+
namespace?: string;
|
55
|
+
resourceIdentifier: string;
|
56
|
+
};
|
57
|
+
export type StoreArgs<T extends ValueStoreType> = {
|
58
|
+
stores: Map<string, IKeyValueStore<T>>;
|
59
|
+
storeId?: string;
|
60
|
+
};
|
61
|
+
export type Resource = {
|
62
|
+
response: SerializedResponse;
|
63
|
+
resourceType: ResourceType;
|
64
|
+
insertedAt: number;
|
65
|
+
partyCorrelationId?: string;
|
66
|
+
};
|
67
|
+
export type SerializedResponse = {
|
68
|
+
status: number;
|
69
|
+
statusText: string;
|
70
|
+
headers: {
|
71
|
+
[x: string]: string;
|
72
|
+
};
|
73
|
+
body: string;
|
74
|
+
};
|
75
|
+
export type RequiredContext = IAgentContext<never>;
|
76
|
+
//# sourceMappingURL=IResourceResolver.d.ts.map
|
@@ -0,0 +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;AAE9D,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,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CAC9D;AAED,MAAM,MAAM,uBAAuB,GAAG;IACpC,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,cAAc,CAAC,EAAE,GAAG,C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AED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,kBAAkB,EAAE,MAAM,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,cAAc,IAAI;IAChD,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,CAAA;IACtC,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,EAAE,kBAAkB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;IAC1B,UAAU,EAAE,MAAM,CAAA;IAClB,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE;QAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;IAChC,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED,MAAM,MAAM,eAAe,GAAG,aAAa,CAAC,KAAK,CAAC,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"IResourceResolver.js","sourceRoot":"","sources":["../../src/types/IResourceResolver.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import { ResolveOptions, Resource, SerializedResponse } from '../types/IResourceResolver';
|
2
|
+
export declare const getResourceIdentifier: (input: Request | string | URL) => string;
|
3
|
+
export declare const serializeResponse: (response: Response) => Promise<SerializedResponse>;
|
4
|
+
export declare const deserializeResponse: (data: SerializedResponse) => Promise<Response>;
|
5
|
+
export declare const isCacheWithinMaxAge: (cachedResource: Resource, resolveOpts?: ResolveOptions) => boolean;
|
6
|
+
//# sourceMappingURL=ResourceResolverUtils.d.ts.map
|
@@ -0,0 +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,CAgBpF,CAAA;AAGD,eAAO,MAAM,mBAAmB,mBAAoB,QAAQ,gBAAgB,cAAc,KAAG,OAE5F,CAAA"}
|
@@ -0,0 +1,78 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
32
|
+
});
|
33
|
+
};
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
35
|
+
exports.isCacheWithinMaxAge = exports.deserializeResponse = exports.serializeResponse = exports.getResourceIdentifier = void 0;
|
36
|
+
const cross_fetch_1 = require("cross-fetch");
|
37
|
+
const u8a = __importStar(require("uint8arrays"));
|
38
|
+
const getResourceIdentifier = (input) => {
|
39
|
+
if (typeof input === 'string') {
|
40
|
+
return input;
|
41
|
+
}
|
42
|
+
else if (input instanceof cross_fetch_1.Request) {
|
43
|
+
return input.url;
|
44
|
+
}
|
45
|
+
else if (input instanceof URL) {
|
46
|
+
return input.toString();
|
47
|
+
}
|
48
|
+
throw new Error('Invalid input type. Expected Request, string, or URL.');
|
49
|
+
};
|
50
|
+
exports.getResourceIdentifier = getResourceIdentifier;
|
51
|
+
const serializeResponse = (response) => __awaiter(void 0, void 0, void 0, function* () {
|
52
|
+
const arrayBuffer = yield response.arrayBuffer();
|
53
|
+
const base64Url = u8a.toString(new Uint8Array(arrayBuffer), 'base64url');
|
54
|
+
return {
|
55
|
+
status: response.status,
|
56
|
+
statusText: response.statusText,
|
57
|
+
headers: Object.fromEntries(response.headers.entries()),
|
58
|
+
body: base64Url
|
59
|
+
};
|
60
|
+
});
|
61
|
+
exports.serializeResponse = serializeResponse;
|
62
|
+
const deserializeResponse = (data) => __awaiter(void 0, void 0, void 0, function* () {
|
63
|
+
const { status, statusText, headers, body } = data;
|
64
|
+
const uint8Array = u8a.fromString(body, 'base64url');
|
65
|
+
const arrayBuffer = uint8Array.buffer.slice(uint8Array.byteOffset, uint8Array.byteOffset + uint8Array.byteLength);
|
66
|
+
return new cross_fetch_1.Response(arrayBuffer, {
|
67
|
+
status,
|
68
|
+
statusText,
|
69
|
+
headers: new cross_fetch_1.Headers(headers)
|
70
|
+
});
|
71
|
+
});
|
72
|
+
exports.deserializeResponse = deserializeResponse;
|
73
|
+
// Check if the cache is still within the acceptable age
|
74
|
+
const isCacheWithinMaxAge = (cachedResource, resolveOpts) => {
|
75
|
+
return cachedResource && ((resolveOpts === null || resolveOpts === void 0 ? void 0 : resolveOpts.maxAgeMs) === undefined || (Date.now() - cachedResource.insertedAt < resolveOpts.maxAgeMs));
|
76
|
+
};
|
77
|
+
exports.isCacheWithinMaxAge = isCacheWithinMaxAge;
|
78
|
+
//# sourceMappingURL=ResourceResolverUtils.js.map
|
@@ -0,0 +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,CAAC;IACf,CAAC;SAAM,IAAI,KAAK,YAAY,qBAAO,EAAE,CAAC;QACpC,OAAO,KAAK,CAAC,GAAG,CAAC;IACnB,CAAC;SAAM,IAAI,KAAK,YAAY,GAAG,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC1B,CAAC;IAED,MAAM,IAAI,KAAK,CA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AW,CAAE,QAAQ,MAAK,SAAS,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,cAAc,CAAC,UAAU,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAA;AACnI,CAAC,CAAA;AAFY,QAAA,mBAAmB,uBAE/B"}
|
package/package.json
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
{
|
2
|
+
"name": "@sphereon/ssi-sdk.resource-resolver",
|
3
|
+
"version": "0.30.2-feature.FIDES.integration.223+f1d96b7c",
|
4
|
+
"source": "src/index.ts",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
|
+
"veramo": {
|
8
|
+
"pluginInterfaces": {
|
9
|
+
"IResourceResolverManager": "./src/types/IResourceResolverManager.ts"
|
10
|
+
}
|
11
|
+
},
|
12
|
+
"scripts": {
|
13
|
+
"build": "tsc",
|
14
|
+
"build:clean": "tsc --build --clean && tsc --build"
|
15
|
+
},
|
16
|
+
"dependencies": {
|
17
|
+
"@sphereon/ssi-sdk.kv-store-temp": "0.30.2-feature.FIDES.integration.223+f1d96b7c",
|
18
|
+
"cross-fetch": "^3.1.8",
|
19
|
+
"debug": "^4.3.5",
|
20
|
+
"typeorm": "^0.3.20",
|
21
|
+
"uint8arrays": "^3.1.1"
|
22
|
+
},
|
23
|
+
"devDependencies": {
|
24
|
+
"@sphereon/ssi-sdk.agent-config": "0.30.2-feature.FIDES.integration.223+f1d96b7c",
|
25
|
+
"@veramo/remote-client": "4.2.0",
|
26
|
+
"@veramo/remote-server": "4.2.0",
|
27
|
+
"nock": "^13.5.4",
|
28
|
+
"timekeeper": "^2.3.1"
|
29
|
+
},
|
30
|
+
"files": [
|
31
|
+
"dist/**/*",
|
32
|
+
"src/**/*",
|
33
|
+
"README.md",
|
34
|
+
"plugin.schema.json",
|
35
|
+
"LICENSE"
|
36
|
+
],
|
37
|
+
"private": false,
|
38
|
+
"publishConfig": {
|
39
|
+
"access": "public"
|
40
|
+
},
|
41
|
+
"repository": "git@github.com:Sphereon-Opensource/SSI-SDK.git",
|
42
|
+
"author": "Sphereon <dev@sphereon.com>",
|
43
|
+
"license": "Apache-2.0",
|
44
|
+
"keywords": [
|
45
|
+
"Sphereon",
|
46
|
+
"SSI",
|
47
|
+
"Veramo",
|
48
|
+
"Resource Management",
|
49
|
+
"Caching"
|
50
|
+
],
|
51
|
+
"gitHead": "f1d96b7c86d7cfe23140fb25b5ad0e432b0c562b"
|
52
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
{
|
2
|
+
"IResourceResolver": {
|
3
|
+
"components": {
|
4
|
+
"schemas": {
|
5
|
+
"ResourceResolverOptions": {
|
6
|
+
"type": "object",
|
7
|
+
"properties": {}
|
8
|
+
},
|
9
|
+
"ResolveArgs": {
|
10
|
+
"type": "object",
|
11
|
+
"properties": {}
|
12
|
+
},
|
13
|
+
"ResolveOptions": {
|
14
|
+
"type": "object",
|
15
|
+
"properties": {}
|
16
|
+
},
|
17
|
+
"ResourceType": {
|
18
|
+
"type": "object",
|
19
|
+
"properties": {}
|
20
|
+
},
|
21
|
+
"ClearAllResourcesArgs": {
|
22
|
+
"type": "object",
|
23
|
+
"properties": {}
|
24
|
+
},
|
25
|
+
"PersistResourceArgs": {
|
26
|
+
"type": "object",
|
27
|
+
"properties": {}
|
28
|
+
},
|
29
|
+
"GetResourceArgs": {
|
30
|
+
"type": "object",
|
31
|
+
"properties": {}
|
32
|
+
},
|
33
|
+
"StoreIdStrArgs": {
|
34
|
+
"type": "object",
|
35
|
+
"properties": {}
|
36
|
+
},
|
37
|
+
"NamespaceStrArgs": {
|
38
|
+
"type": "object",
|
39
|
+
"properties": {}
|
40
|
+
},
|
41
|
+
"PrefixArgs": {
|
42
|
+
"type": "object",
|
43
|
+
"properties": {}
|
44
|
+
},
|
45
|
+
"StoreArgs": {
|
46
|
+
"type": "object",
|
47
|
+
"properties": {}
|
48
|
+
},
|
49
|
+
"Resource": {
|
50
|
+
"type": "object",
|
51
|
+
"properties": {}
|
52
|
+
},
|
53
|
+
"SerializedResponse": {
|
54
|
+
"type": "object",
|
55
|
+
"properties": {}
|
56
|
+
}
|
57
|
+
},
|
58
|
+
"methods": {
|
59
|
+
"resourceResolve": {
|
60
|
+
"description": "",
|
61
|
+
"arguments": {
|
62
|
+
"$ref": "#/components/schemas/ResolveArgs"
|
63
|
+
},
|
64
|
+
"returnType": {}
|
65
|
+
},
|
66
|
+
"resourceClearAllResources": {
|
67
|
+
"description": "",
|
68
|
+
"arguments": {
|
69
|
+
"$ref": "#/components/schemas/ClearAllResourcesArgs"
|
70
|
+
},
|
71
|
+
"returnType": {}
|
72
|
+
},
|
73
|
+
"resourceDefaultStoreId": {
|
74
|
+
"description": "",
|
75
|
+
"returnType": {}
|
76
|
+
},
|
77
|
+
"resourceDefaultNamespace": {
|
78
|
+
"description": "",
|
79
|
+
"returnType": {}
|
80
|
+
},
|
81
|
+
"resourceDefaultTtl": {
|
82
|
+
"description": "",
|
83
|
+
"returnType": {}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
@@ -0,0 +1,203 @@
|
|
1
|
+
import {
|
2
|
+
IKeyValueStore,
|
3
|
+
IValueData,
|
4
|
+
KeyValueStore,
|
5
|
+
ValueStoreType
|
6
|
+
} from '@sphereon/ssi-sdk.kv-store-temp'
|
7
|
+
import { IAgentPlugin } from '@veramo/core'
|
8
|
+
import fetch, { Response, Headers } from 'cross-fetch'
|
9
|
+
import { schema } from '../index'
|
10
|
+
import {
|
11
|
+
deserializeResponse,
|
12
|
+
getResourceIdentifier,
|
13
|
+
isCacheWithinMaxAge,
|
14
|
+
serializeResponse
|
15
|
+
} from '../utils/ResourceResolverUtils'
|
16
|
+
import {
|
17
|
+
ClearAllResourcesArgs,
|
18
|
+
ResolveArgs,
|
19
|
+
GetResourceArgs,
|
20
|
+
IResourceResolver,
|
21
|
+
NamespaceStrArgs,
|
22
|
+
PersistResourceArgs,
|
23
|
+
PrefixArgs,
|
24
|
+
RequiredContext,
|
25
|
+
ResourceResolverOptions,
|
26
|
+
StoreArgs,
|
27
|
+
StoreIdStrArgs,
|
28
|
+
Resource
|
29
|
+
} from '../types/IResourceResolver'
|
30
|
+
|
31
|
+
/**
|
32
|
+
* {@inheritDoc IResourceResolver}
|
33
|
+
*/
|
34
|
+
export class ResourceResolver implements IAgentPlugin {
|
35
|
+
readonly schema = schema.IResourceResolver
|
36
|
+
readonly methods: IResourceResolver = {
|
37
|
+
resourceResolve: this.resourceResolve.bind(this),
|
38
|
+
resourceClearAllResources: this.resourceClearAllResources.bind(this),
|
39
|
+
resourceDefaultStoreId: this.resourceDefaultStoreId.bind(this),
|
40
|
+
resourceDefaultNamespace: this.resourceDefaultNamespace.bind(this),
|
41
|
+
resourceDefaultTtl: this.resourceDefaultTtl.bind(this),
|
42
|
+
}
|
43
|
+
|
44
|
+
private readonly defaultStoreId: string
|
45
|
+
private readonly defaultNamespace: string
|
46
|
+
private readonly defaultTtl: number
|
47
|
+
private readonly _resourceStores: Map<string, IKeyValueStore<Resource>>
|
48
|
+
|
49
|
+
constructor(options?: ResourceResolverOptions) {
|
50
|
+
const {
|
51
|
+
defaultStore,
|
52
|
+
defaultNamespace,
|
53
|
+
resourceStores,
|
54
|
+
ttl
|
55
|
+
} = options ?? {}
|
56
|
+
|
57
|
+
this.defaultStoreId = defaultStore ?? '_default'
|
58
|
+
this.defaultNamespace = defaultNamespace ?? 'resources'
|
59
|
+
this.defaultTtl = ttl ?? 3600
|
60
|
+
|
61
|
+
if (resourceStores && resourceStores instanceof Map) {
|
62
|
+
this._resourceStores = resourceStores
|
63
|
+
} else if (resourceStores) {
|
64
|
+
this._resourceStores = new Map().set(this.defaultStoreId, resourceStores)
|
65
|
+
} else {
|
66
|
+
this._resourceStores = new Map().set(
|
67
|
+
this.defaultStoreId,
|
68
|
+
new KeyValueStore({
|
69
|
+
namespace: this.defaultNamespace,
|
70
|
+
store: new Map<string, Resource>(),
|
71
|
+
ttl: this.defaultTtl
|
72
|
+
})
|
73
|
+
)
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
/** {@inheritDoc IResourceResolver.resourceResolve} */
|
78
|
+
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
|
88
|
+
|
89
|
+
const resourceIdentifier = getResourceIdentifier(input)
|
90
|
+
|
91
|
+
const cachedResource = await this.getResource({ resourceIdentifier, storeId, namespace })
|
92
|
+
if (cachedResource.value && isCacheWithinMaxAge(cachedResource.value, resolveOpts)) {
|
93
|
+
return deserializeResponse(cachedResource.value.response);
|
94
|
+
}
|
95
|
+
|
96
|
+
if (resolveOpts?.onlyCache) {
|
97
|
+
return new Response(JSON.stringify({ error: 'Resource not found' }), {
|
98
|
+
status: 404,
|
99
|
+
statusText: 'Not Found',
|
100
|
+
headers: new Headers({ 'Content-Type': 'application/json' })
|
101
|
+
});
|
102
|
+
}
|
103
|
+
|
104
|
+
const response = await fetch(input, init)
|
105
|
+
if (!resolveOpts?.skipPersistence && (response.status >= 200 && response.status < 300)) {
|
106
|
+
const serializedResponse = await serializeResponse(response);
|
107
|
+
const resource = {
|
108
|
+
response: serializedResponse,
|
109
|
+
resourceType,
|
110
|
+
insertedAt: Date.now(),
|
111
|
+
partyCorrelationId
|
112
|
+
}
|
113
|
+
const cachedResource = await this.persistResource({
|
114
|
+
resource,
|
115
|
+
resourceIdentifier,
|
116
|
+
namespace,
|
117
|
+
storeId
|
118
|
+
})
|
119
|
+
|
120
|
+
if (!cachedResource.value) {
|
121
|
+
return Promise.reject(Error('Resource not present in persistence result'))
|
122
|
+
}
|
123
|
+
|
124
|
+
return deserializeResponse(cachedResource.value.response)
|
125
|
+
}
|
126
|
+
|
127
|
+
return response
|
128
|
+
}
|
129
|
+
|
130
|
+
/** {@inheritDoc IResourceResolver.resourceClearAllResources} */
|
131
|
+
private async resourceClearAllResources(args: ClearAllResourcesArgs, context: RequiredContext): Promise<boolean> {
|
132
|
+
const { storeId } = args
|
133
|
+
return await this.store({ stores: this._resourceStores, storeId })
|
134
|
+
.clear()
|
135
|
+
.then(() => true)
|
136
|
+
}
|
137
|
+
|
138
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultStoreId} */
|
139
|
+
private async resourceDefaultStoreId(context: RequiredContext): Promise<string> {
|
140
|
+
return this.defaultStoreId
|
141
|
+
}
|
142
|
+
|
143
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultNamespace} */
|
144
|
+
private async resourceDefaultNamespace(context: RequiredContext): Promise<string> {
|
145
|
+
return this.defaultNamespace
|
146
|
+
}
|
147
|
+
|
148
|
+
/** {@inheritDoc IResourceResolver.resourceDefaultTtl} */
|
149
|
+
private async resourceDefaultTtl(context: RequiredContext): Promise<number> {
|
150
|
+
return this.defaultTtl
|
151
|
+
}
|
152
|
+
|
153
|
+
private async getResource(args: GetResourceArgs): Promise<IValueData<Resource>> {
|
154
|
+
const { resourceIdentifier, storeId, namespace } = args
|
155
|
+
return this.store({ stores: this._resourceStores, storeId }).getAsValueData(
|
156
|
+
this.prefix({
|
157
|
+
namespace,
|
158
|
+
resourceIdentifier
|
159
|
+
}),
|
160
|
+
)
|
161
|
+
}
|
162
|
+
|
163
|
+
private async persistResource(args: PersistResourceArgs): Promise<IValueData<Resource>> {
|
164
|
+
const { resource, resourceIdentifier, ttl } = args
|
165
|
+
const namespace = this.namespaceStr(args)
|
166
|
+
const storeId = this.storeIdStr(args)
|
167
|
+
|
168
|
+
return await this.store({ stores: this._resourceStores, storeId }).set(
|
169
|
+
this.prefix({
|
170
|
+
namespace,
|
171
|
+
resourceIdentifier,
|
172
|
+
}),
|
173
|
+
resource,
|
174
|
+
ttl ?? this.defaultTtl,
|
175
|
+
)
|
176
|
+
}
|
177
|
+
|
178
|
+
private store<T extends ValueStoreType>(args: StoreArgs<T>): IKeyValueStore<T> {
|
179
|
+
const storeId = this.storeIdStr({ storeId: args.storeId })
|
180
|
+
const store = args.stores.get(storeId)
|
181
|
+
if (!store) {
|
182
|
+
throw Error(`Could not get resource store: ${storeId}`)
|
183
|
+
}
|
184
|
+
return store
|
185
|
+
}
|
186
|
+
|
187
|
+
private storeIdStr(args: StoreIdStrArgs): string {
|
188
|
+
const { storeId } = args
|
189
|
+
return storeId ?? this.defaultStoreId
|
190
|
+
}
|
191
|
+
|
192
|
+
private namespaceStr(args: NamespaceStrArgs): string {
|
193
|
+
const { namespace } = args
|
194
|
+
return namespace ?? this.defaultNamespace
|
195
|
+
}
|
196
|
+
|
197
|
+
private prefix(args: PrefixArgs): string {
|
198
|
+
const { namespace, resourceIdentifier } = args
|
199
|
+
return `${this.namespaceStr({ namespace })}:${resourceIdentifier}`
|
200
|
+
}
|
201
|
+
|
202
|
+
}
|
203
|
+
|
package/src/index.ts
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
import { IKeyValueStore, ValueStoreType } from '@sphereon/ssi-sdk.kv-store-temp'
|
2
|
+
import { IAgentContext, IPluginMethodMap } from '@veramo/core'
|
3
|
+
|
4
|
+
export interface IResourceResolver extends IPluginMethodMap {
|
5
|
+
resourceResolve(args: ResolveArgs, context: RequiredContext): Promise<Response>
|
6
|
+
resourceClearAllResources(args: ClearAllResourcesArgs, context: RequiredContext): Promise<boolean>
|
7
|
+
resourceDefaultStoreId(context: RequiredContext): Promise<string>
|
8
|
+
resourceDefaultNamespace(context: RequiredContext): Promise<string>
|
9
|
+
resourceDefaultTtl(context: RequiredContext): Promise<number>
|
10
|
+
}
|
11
|
+
|
12
|
+
export type ResourceResolverOptions = {
|
13
|
+
defaultStore?: string
|
14
|
+
defaultNamespace?: string
|
15
|
+
resourceStores?: Map<string, IKeyValueStore<Resource>> | IKeyValueStore<Resource>
|
16
|
+
ttl?: number
|
17
|
+
}
|
18
|
+
|
19
|
+
export type ResolveArgs = {
|
20
|
+
input: RequestInfo | URL
|
21
|
+
init?: RequestInit
|
22
|
+
resourceType: ResourceType
|
23
|
+
partyCorrelationId?: string
|
24
|
+
storeId?: string
|
25
|
+
namespace?: string
|
26
|
+
resolveOpts?: ResolveOptions
|
27
|
+
}
|
28
|
+
|
29
|
+
export type ResolveOptions = {
|
30
|
+
ttl?: number
|
31
|
+
maxAgeMs?: number
|
32
|
+
onlyCache?: boolean
|
33
|
+
skipPersistence?: boolean
|
34
|
+
}
|
35
|
+
|
36
|
+
export type ResourceType = 'credential_branding_image' | 'issuer_branding_image' | 'oid4vci_metadata' | string
|
37
|
+
|
38
|
+
export type ClearAllResourcesArgs = {
|
39
|
+
storeId?: string
|
40
|
+
}
|
41
|
+
|
42
|
+
export type PersistResourceArgs = {
|
43
|
+
resource: Resource
|
44
|
+
resourceIdentifier: string
|
45
|
+
ttl?: number
|
46
|
+
storeId?: string
|
47
|
+
namespace?: string
|
48
|
+
}
|
49
|
+
|
50
|
+
export type GetResourceArgs = {
|
51
|
+
resourceIdentifier: string
|
52
|
+
storeId?: string
|
53
|
+
namespace?: string
|
54
|
+
}
|
55
|
+
|
56
|
+
export type StoreIdStrArgs = {
|
57
|
+
storeId?: string
|
58
|
+
}
|
59
|
+
|
60
|
+
export type NamespaceStrArgs = {
|
61
|
+
namespace?: string
|
62
|
+
}
|
63
|
+
|
64
|
+
export type PrefixArgs = {
|
65
|
+
namespace?: string;
|
66
|
+
resourceIdentifier: string
|
67
|
+
}
|
68
|
+
|
69
|
+
export type StoreArgs<T extends ValueStoreType> = {
|
70
|
+
stores: Map<string, IKeyValueStore<T>>
|
71
|
+
storeId?: string
|
72
|
+
}
|
73
|
+
|
74
|
+
export type Resource = {
|
75
|
+
response: SerializedResponse
|
76
|
+
resourceType: ResourceType
|
77
|
+
insertedAt: number
|
78
|
+
partyCorrelationId?: string
|
79
|
+
}
|
80
|
+
|
81
|
+
export type SerializedResponse = {
|
82
|
+
status: number
|
83
|
+
statusText: string
|
84
|
+
headers: { [x: string]: string }
|
85
|
+
body: string
|
86
|
+
}
|
87
|
+
|
88
|
+
export type RequiredContext = IAgentContext<never>
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import { Headers, Response, Request } from 'cross-fetch'
|
2
|
+
import * as u8a from 'uint8arrays'
|
3
|
+
import { ResolveOptions, Resource, SerializedResponse } from '../types/IResourceResolver'
|
4
|
+
|
5
|
+
export const getResourceIdentifier = (input: Request | string | URL): string => {
|
6
|
+
if (typeof input === 'string') {
|
7
|
+
return input;
|
8
|
+
} else if (input instanceof Request) {
|
9
|
+
return input.url;
|
10
|
+
} else if (input instanceof URL) {
|
11
|
+
return input.toString();
|
12
|
+
}
|
13
|
+
|
14
|
+
throw new Error('Invalid input type. Expected Request, string, or URL.');
|
15
|
+
}
|
16
|
+
|
17
|
+
export const serializeResponse = async (response: Response): Promise<SerializedResponse> => {
|
18
|
+
const arrayBuffer = await response.arrayBuffer();
|
19
|
+
const base64Url = u8a.toString(new Uint8Array(arrayBuffer), 'base64url');
|
20
|
+
|
21
|
+
return {
|
22
|
+
status: response.status,
|
23
|
+
statusText: response.statusText,
|
24
|
+
headers: Object.fromEntries(response.headers.entries()),
|
25
|
+
body: base64Url
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
export const deserializeResponse = async (data: SerializedResponse): Promise<Response> => {
|
30
|
+
const {
|
31
|
+
status,
|
32
|
+
statusText,
|
33
|
+
headers,
|
34
|
+
body
|
35
|
+
} = data
|
36
|
+
|
37
|
+
const uint8Array = u8a.fromString(body, 'base64url')
|
38
|
+
const arrayBuffer = uint8Array.buffer.slice(uint8Array.byteOffset, uint8Array.byteOffset + uint8Array.byteLength);
|
39
|
+
|
40
|
+
return new Response(arrayBuffer, {
|
41
|
+
status,
|
42
|
+
statusText,
|
43
|
+
headers: new Headers(headers)
|
44
|
+
})
|
45
|
+
}
|
46
|
+
|
47
|
+
// Check if the cache is still within the acceptable age
|
48
|
+
export const isCacheWithinMaxAge = (cachedResource: Resource, resolveOpts?: ResolveOptions): boolean => {
|
49
|
+
return cachedResource && (resolveOpts?.maxAgeMs === undefined || (Date.now() - cachedResource.insertedAt < resolveOpts.maxAgeMs))
|
50
|
+
}
|