| 28 | |
| 29 | /** A {@link Commit} without its signature, signer and timestamp */ |
| 30 | export class CommitBuilder implements CommitBuilderI { |
| 31 | subject: string; |
| 32 | set: Record<string, JSONValue>; |
| 33 | remove: string[]; |
| 34 | destroy?: boolean; |
| 35 | |
| 36 | /** Removes any query parameters from the Subject */ |
| 37 | constructor(subject: string) { |
| 38 | this.subject = removeQueryParamsFromURL(subject); |
| 39 | this.set = {}; |
| 40 | this.remove = []; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Signs the commit using the privateKey of the Agent, and returns a full |
| 45 | * Commit which is ready to be sent to an Atomic-Server `/commit` endpoint. |
| 46 | */ |
| 47 | async sign(privateKey: string, agentSubject: string): Promise<Commit> { |
| 48 | const commit = await signAt( |
| 49 | this.clone(), |
| 50 | agentSubject, |
| 51 | privateKey, |
| 52 | getTimestampNow(), |
| 53 | ); |
| 54 | return commit; |
| 55 | } |
| 56 | |
| 57 | /** Returns true if the CommitBuilder has non-empty changes (set, remove, destroy) */ |
| 58 | hasUnsavedChanges(): boolean { |
| 59 | return ( |
| 60 | Object.keys(this.set).length > 0 || this.destroy || this.remove.length > 0 |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a clone of the CommitBuilder. This is required, because I want to |
| 66 | * prevent any adjustments to the CommitBuilder while signing, as this could |
| 67 | * cause race conditions with wrong signatures |
| 68 | */ |
| 69 | // Warning: I'm not sure whether this actually solves the issue. Might be a good idea to remove this. |
| 70 | clone(): CommitBuilder { |
| 71 | const cm = new CommitBuilder(this.subject); |
| 72 | cm.set = this.set; |
| 73 | cm.destroy = this.destroy; |
| 74 | cm.remove = this.remove; |
| 75 | return cm; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** A {@link Commit} without its signature, but with a signer and timestamp */ |
| 80 | interface CommitPreSigned extends CommitBuilderI { |
nothing calls this directly
no outgoing calls
no test coverage detected