Skip to content

Commit 1cad637

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

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

lib/node/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class File extends Node {
1515
* Returns a clone of the file
1616
*/
1717
clone(): File {
18-
return new File(this.data)
18+
return new File(structuredClone(this._data), this._knownDavService)
1919
}
2020

2121
}

lib/node/folder.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
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'
65
import { FileType } from './fileType'
76
import { Node } from './node'
87

98
export class Folder extends Node {
109

11-
constructor(data: NodeData) {
10+
constructor(...[data, davService]: ConstructorParameters<typeof Node>) {
1211
// enforcing mimes
1312
super({
1413
...data,
1514
mime: 'httpd/unix-directory',
16-
})
15+
}, davService)
1716
}
1817

1918
get type(): FileType.Folder {
@@ -32,7 +31,7 @@ export class Folder extends Node {
3231
* Returns a clone of the folder
3332
*/
3433
clone(): Folder {
35-
return new Folder(this.data)
34+
return new Folder(structuredClone(this._data), this._knownDavService)
3635
}
3736

3837
}

lib/node/node.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { encodePath } from '@nextcloud/paths'
77

88
import { Permission } from '../permissions'
99
import { FileType } from './fileType'
10-
import { Attribute, isDavResource, NodeData, validateData } from './nodeData'
10+
import { Attribute, fixDates, fixRegExp, isDavResource, NodeData, validateData } from './nodeData'
1111
import logger from '../utils/logger'
1212

1313
export enum NodeStatus {
@@ -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,13 +61,17 @@ 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
}
6568

69+
// Fix primitive types if needed
70+
fixDates(data)
71+
davService = fixRegExp(davService || this._knownDavService)
72+
6673
// Validate data
67-
validateData(data, davService || this._knownDavService)
74+
validateData(data, davService)
6875

6976
this._data = {
7077
// TODO: Remove with next major release, this is just for compatibility
@@ -346,13 +353,6 @@ export abstract class Node {
346353
this._data.status = status
347354
}
348355

349-
/**
350-
* Get the node data
351-
*/
352-
get data(): NodeData {
353-
return structuredClone(this._data)
354-
}
355-
356356
/**
357357
* Move the node to a new destination
358358
*
@@ -424,7 +424,17 @@ export abstract class Node {
424424
/**
425425
* Returns a clone of the node
426426
*/
427-
abstract clone(): Node
427+
clone(): this {
428+
// @ts-expect-error -- this class is abstract and cannot be instantiated directly but all its children can
429+
return new this.constructor(structuredClone(this._data), this._knownDavService)
430+
}
431+
432+
/**
433+
* JSON representation of the node
434+
*/
435+
toJSON(): string {
436+
return JSON.stringify([structuredClone(this._data), this._knownDavService.toString()])
437+
}
428438

429439
}
430440

0 commit comments

Comments
 (0)