Skip to content

Commit

Permalink
refactor: move formatLocks out of adapter and into Method
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Aug 4, 2022
1 parent 92ae750 commit 869f31d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 56 deletions.
47 changes: 0 additions & 47 deletions src/FileSystemAdapter/Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,53 +361,6 @@ export default class Adapter implements AdapterInterface {
});
}

async formatLocks(locks: Lock[], baseUrl: URL) {
const xml = { activelock: [] as any[] };

if (locks != null) {
for (let lock of locks) {
const secondsLeft =
lock.timeout === Infinity
? Infinity
: (lock.date.getTime() + lock.timeout - new Date().getTime()) /
1000;

if (secondsLeft <= 0) {
continue;
}

xml.activelock.push({
locktype: {
write: {},
},
lockscope: {
[lock.scope]: {},
},
depth: {
_: `${lock.depth}`,
},
owner: lock.owner,
timeout:
secondsLeft === Infinity
? { _: 'Infinite' }
: { _: `Second-${secondsLeft}` },
locktoken: { href: { _: lock.token } },
lockroot: {
href: {
_: (await lock.resource.getCanonicalUrl(baseUrl)).pathname,
},
},
});
}
}

if (!xml.activelock.length) {
return {};
}

return xml;
}

getMethod(method: string): typeof Method {
// No additional methods to handle.
if (method === 'POST' || method === 'PATCH') {
Expand Down
2 changes: 1 addition & 1 deletion src/FileSystemAdapter/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import User from './User.js';

export default class Lock implements LockInterface {
resource: Resource;
user: User;
token: string = '';
date: Date = new Date();
timeout: number = 1000 * 60 * 60 * 24 * 2; // Default to two day timeout.
scope: 'exclusive' | 'shared' = 'exclusive';
depth: '0' | 'infinity' = '0';
provisional: boolean = false;
owner: any = {};
user: User;

constructor({ resource, user }: { resource: Resource; user: User }) {
this.resource = resource;
Expand Down
5 changes: 0 additions & 5 deletions src/Interfaces/Adapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ export interface Adapter {
*/
newCollection(url: URL, baseUrl: string): Promise<Resource>;

/**
* Format a list of locks into an object acceptable by xml2js.
*/
formatLocks(locks: Lock[], baseUrl: URL): Promise<any>;

/**
* Get a handler class for an additional method.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Interfaces/Lock.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Resource } from '../index.js';
import type { User } from './User.js';

export interface Lock {
/**
* The lock-root resource of this lock.
*/
resource: Resource;
/**
* A unique token representing this lock.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Methods/LOCK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class LOCK extends Method {
const currentLocks = await this.getLocks(request, resource);
const responseObj = {
prop: {
lockdiscovery: await this.adapter.formatLocks(
lockdiscovery: await this.formatLocks(
currentLocks.all,
this.getRequestBaseUrl(request)
),
Expand Down Expand Up @@ -456,7 +456,7 @@ export class LOCK extends Method {
const currentLocks = await this.getLocks(request, resource);
const responseObj = {
prop: {
lockdiscovery: await this.adapter.formatLocks(
lockdiscovery: await this.formatLocks(
currentLocks.all,
this.getRequestBaseUrl(request)
),
Expand Down
50 changes: 50 additions & 0 deletions src/Methods/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,4 +962,54 @@ export class Method {
}
return this.xmlBuilder.buildObject(obj);
}

/**
* Format a list of locks into an object acceptable by xml2js.
*/
async formatLocks(locks: Lock[], baseUrl: URL) {
const xml = { activelock: [] as any[] };

if (locks != null) {
for (let lock of locks) {
const secondsLeft =
lock.timeout === Infinity
? Infinity
: (lock.date.getTime() + lock.timeout - new Date().getTime()) /
1000;

if (secondsLeft <= 0) {
continue;
}

xml.activelock.push({
locktype: {
write: {},
},
lockscope: {
[lock.scope]: {},
},
depth: {
_: `${lock.depth}`,
},
owner: lock.owner,
timeout:
secondsLeft === Infinity
? { _: 'Infinite' }
: { _: `Second-${secondsLeft}` },
locktoken: { href: { _: lock.token } },
lockroot: {
href: {
_: (await lock.resource.getCanonicalUrl(baseUrl)).pathname,
},
},
});
}
}

if (!xml.activelock.length) {
return {};
}

return xml;
}
}
2 changes: 1 addition & 1 deletion src/Methods/PROPFIND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class PROPFIND extends Method {
(allprop || requestedProps.includes('lockdiscovery'))
) {
const currentLocks = await this.getLocks(request, resource);
propObj.lockdiscovery = await this.adapter.formatLocks(
propObj.lockdiscovery = await this.formatLocks(
currentLocks.all,
this.getRequestBaseUrl(request)
);
Expand Down

0 comments on commit 869f31d

Please sign in to comment.