Skip to content

Commit 44b01e3

Browse files
committed
fix(node): cloning
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 92a9363 commit 44b01e3

3 files changed

Lines changed: 20 additions & 28 deletions

File tree

lib/node/file.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5+
import type { NodeConstructorData } from './node'
6+
57
import { FileType } from './fileType'
68
import { Node } from './node'
79

810
export class File extends Node {
911

10-
get type(): FileType.File {
11-
return FileType.File
12+
public constructor(...[data, davService]: NodeConstructorData) {
13+
super(data, davService)
1214
}
1315

14-
/**
15-
* Returns a clone of the file
16-
*/
17-
clone(): File {
18-
return new File(this.data)
16+
get type(): FileType.File {
17+
return FileType.File
1918
}
2019

2120
}

lib/node/folder.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5-
import type { NodeData } from './nodeData'
5+
import type { NodeConstructorData } from './node'
6+
67
import { FileType } from './fileType'
78
import { Node } from './node'
89

910
export class Folder extends Node {
1011

11-
constructor(data: NodeData) {
12+
constructor(...[data, davService]: NodeConstructorData) {
1213
// enforcing mimes
1314
super({
1415
...data,
1516
mime: 'httpd/unix-directory',
16-
})
17+
}, davService)
1718
}
1819

1920
get type(): FileType.Folder {
@@ -28,13 +29,6 @@ export class Folder extends Node {
2829
return 'httpd/unix-directory'
2930
}
3031

31-
/**
32-
* Returns a clone of the folder
33-
*/
34-
clone(): Folder {
35-
return new Folder(this.data)
36-
}
37-
3832
}
3933

4034
/**

lib/node/node.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ export enum NodeStatus {
2121
LOCKED = 'locked',
2222
}
2323

24+
type NodeConstructorData = [NodeData, RegExp?]
25+
2426
export abstract class Node {
2527

26-
private _data: NodeData
2728
private _attributes: Attribute
28-
private _knownDavService = /(remote|public)\.php\/(web)?dav/i
29+
30+
protected _data: NodeData
31+
protected _knownDavService = /(remote|public)\.php\/(web)?dav/i
2932

3033
private readonlyAttributes = Object.entries(Object.getOwnPropertyDescriptors(Node.prototype))
3134
.filter(e => typeof e[1].get === 'function' && e[0] !== '__proto__')
@@ -58,7 +61,7 @@ export abstract class Node {
5861
},
5962
} as ProxyHandler<Attribute>
6063

61-
constructor(data: NodeData, davService?: RegExp) {
64+
protected constructor(...[data, davService]: NodeConstructorData) {
6265
if (!data.mime) {
6366
data.mime = 'application/octet-stream'
6467
}
@@ -346,13 +349,6 @@ export abstract class Node {
346349
this._data.status = status
347350
}
348351

349-
/**
350-
* Get the node data
351-
*/
352-
get data(): NodeData {
353-
return structuredClone(this._data)
354-
}
355-
356352
/**
357353
* Move the node to a new destination
358354
*
@@ -424,7 +420,10 @@ export abstract class Node {
424420
/**
425421
* Returns a clone of the node
426422
*/
427-
abstract clone(): Node
423+
clone(): this {
424+
// @ts-expect-error -- this class is abstract and cannot be instantiated directly but all its children can
425+
return new this.constructor(this.data, this._knownDavService)
426+
}
428427

429428
}
430429

0 commit comments

Comments
 (0)