Skip to content

Commit 80dc1af

Browse files
authored
Merge pull request #1495 from nextcloud-libraries/snowflake
feat(node): add support for string based snowflake ids
2 parents 983fe40 + 9ccd156 commit 80dc1af

3 files changed

Lines changed: 77 additions & 7 deletions

File tree

__tests__/files/node.spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,51 @@ describe('FileId attribute', () => {
7777
})
7878
})
7979

80+
describe('Id attribute', () => {
81+
test('Id undefined', () => {
82+
const file = new File({
83+
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
84+
root: '/files/emma',
85+
mime: 'image/jpeg',
86+
owner: 'emma',
87+
})
88+
expect(file.id).toBeUndefined()
89+
})
90+
91+
test('id definition', () => {
92+
const file = new File({
93+
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
94+
root: '/files/emma',
95+
mime: 'image/jpeg',
96+
owner: 'emma',
97+
id: '1234',
98+
})
99+
expect(file.id).toBe('1234')
100+
})
101+
102+
test('Legacy id definition', () => {
103+
const file = new File({
104+
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
105+
root: '/files/emma',
106+
mime: 'image/jpeg',
107+
owner: 'emma',
108+
id: 1234,
109+
})
110+
expect(file.id).toBe('1234')
111+
})
112+
113+
test('Legacy failed node id', () => {
114+
const file = new File({
115+
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
116+
root: '/files/emma',
117+
mime: 'image/jpeg',
118+
owner: 'emma',
119+
id: -1,
120+
})
121+
expect(file.id).toBeUndefined()
122+
})
123+
})
124+
80125
describe('Mime attribute', () => {
81126
test('Mime definition', () => {
82127
const file = new File({
@@ -304,7 +349,7 @@ describe('Sanity checks', () => {
304349
root: '/files/emma',
305350
mime: 'image/jpeg',
306351
owner: 'emma',
307-
id: '1234' as unknown as number,
352+
id: true as unknown as number,
308353
})).toThrowError('Invalid id type of value')
309354
})
310355

lib/node/node.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,29 @@ export abstract class Node {
287287
}
288288

289289
/**
290-
* Get the node id if defined.
291-
* There is no setter as the fileid is not meant to be changed
290+
* Get the nodes file id if defined.
291+
* There is no setter as the fileid is not meant to be changed.
292+
*
293+
* @deprecated Nextcloud is migrating to snowflake ids which are strings. Use the `id` attribute instead.
292294
*/
293295
get fileid(): number | undefined {
294-
return this._data?.id
296+
return typeof this._data?.id === 'number' ? this._data.id : undefined
297+
}
298+
299+
/**
300+
* Get the nodes id - if defined.
301+
*
302+
* Note: As Nextcloud is migrating to snowflake ids the id has to be a string,
303+
* due to limitations of the JavaScript number type (snowflake ids are 64bit JavaScript numbers can only accurately represent integers up to 53 bit).
304+
*/
305+
get id(): string | undefined {
306+
if (typeof this._data?.id === 'undefined'
307+
|| (typeof this._data.id === 'number' && this._data.id < 0)) {
308+
// legacy fileid < 0 means the node failed, this should be communicated via the `status` attribute
309+
// so in that case there is no id available
310+
return undefined
311+
}
312+
return String(this._data.id)
295313
}
296314

297315
/**

lib/node/nodeData.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ export interface NodeData {
3737
*/
3838
root: string
3939

40-
/** Unique ID */
41-
id?: number
40+
/**
41+
* Unique ID
42+
*
43+
* This is usually the file id from the backend where this would be a number.
44+
* For some type of nodes this is already a snowflake id and thus as string.
45+
*
46+
* Note: Passing a number is deprecated.
47+
*/
48+
id?: number | string
4249

4350
/** Last modified time */
4451
mtime?: Date
@@ -91,7 +98,7 @@ export function isDavResource(source: string, davService: RegExp): boolean {
9198
* @param davService Pattern to check if source is DAV ressource
9299
*/
93100
export function validateData(data: NodeData, davService: RegExp) {
94-
if (data.id && typeof data.id !== 'number') {
101+
if (data.id && typeof data.id !== 'number' && typeof data.id !== 'string') {
95102
throw new Error('Invalid id type of value')
96103
}
97104

0 commit comments

Comments
 (0)