Skip to content

Commit fe79549

Browse files
committed
Add now option to delegate().
1 parent 961c8b3 commit fe79549

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# @digitalbazaar/ezcap Changelog
22

3+
## 4.2.0 - 2025-mm-dd
4+
5+
### Added
6+
- Allow `now` to be passed to `delegate()` to make testing easier.
7+
38
## 4.1.0 - 2024-01-01
49

510
### Changed

lib/ZcapClient.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright (c) 2021-2023 Digital Bazaar, Inc. All rights reserved.
2+
* Copyright (c) 2021-2025 Digital Bazaar, Inc. All rights reserved.
33
*/
44
import {
55
CapabilityDelegation,
@@ -132,13 +132,15 @@ export class ZcapClient {
132132
* @param {string|Array} [options.allowedActions] - Optional list of allowed
133133
* actions or string specifying allowed delegated action. Default: [] -
134134
* delegate all actions.
135+
* @param {number} [options.now] - Optional timestamp for the current time;
136+
* useful for testing.
135137
*
136138
* @returns {Promise<object>} - A promise that resolves to a delegated
137139
* capability.
138140
*/
139141
async delegate({
140142
capability, controller, invocationTarget, expires,
141-
allowedActions
143+
allowedActions, now = Date.now()
142144
} = {}) {
143145
if(!(typeof controller === 'string' && controller.includes(':'))) {
144146
throw new Error(
@@ -161,7 +163,7 @@ export class ZcapClient {
161163

162164
if(expires === undefined) {
163165
// default expiration is 5 minutes in the future
164-
expires = new Date(Date.now() + 5 * 60 * 1000);
166+
expires = new Date(now + 5 * 60 * 1000);
165167
} else if(expires instanceof Date || typeof expires === 'string') {
166168
// ensure expires is a valid date
167169
const time = Date.parse(expires);
@@ -242,7 +244,7 @@ export class ZcapClient {
242244

243245
// ensure delegation date will not be at least after parent delegation date
244246
const [parentProof] = _getDelegationProofs({capability});
245-
let date = new Date();
247+
let date = new Date(now);
246248
if(parentProof) {
247249
const parentDelegationDate = new Date(parentProof.created);
248250
if(date < parentDelegationDate) {

0 commit comments

Comments
 (0)