@@ -12,7 +12,6 @@ import { Mimes } from 'vs/base/common/mime';
12
12
import { isWeb } from 'vs/base/common/platform' ;
13
13
import { ConfigurationSyncStore } from 'vs/base/common/product' ;
14
14
import { joinPath , relativePath } from 'vs/base/common/resources' ;
15
- import { join } from 'vs/base/common/path' ;
16
15
import { isObject , isString } from 'vs/base/common/types' ;
17
16
import { URI } from 'vs/base/common/uri' ;
18
17
import { generateUuid } from 'vs/base/common/uuid' ;
@@ -231,12 +230,60 @@ export class UserDataSyncStoreClient extends Disposable {
231
230
}
232
231
}
233
232
234
- async getAllResourceRefs ( path : string ) : Promise < IResourceRefHandle [ ] > {
233
+ // #region Collection
234
+
235
+ async getAllCollections ( headers : IHeaders = { } ) : Promise < string [ ] > {
236
+ if ( ! this . userDataSyncStoreUrl ) {
237
+ throw new Error ( 'No settings sync store url configured.' ) ;
238
+ }
239
+
240
+ const url = joinPath ( this . userDataSyncStoreUrl , 'collection' ) . toString ( ) ;
241
+ headers = { ...headers } ;
242
+ headers [ 'Content-Type' ] = 'application/json' ;
243
+
244
+ const context = await this . request ( url , { type : 'GET' , headers } , [ ] , CancellationToken . None ) ;
245
+
246
+ return ( await asJson < string [ ] > ( context ) ) || [ ] ;
247
+ }
248
+
249
+ async createCollection ( headers : IHeaders = { } ) : Promise < string > {
250
+ if ( ! this . userDataSyncStoreUrl ) {
251
+ throw new Error ( 'No settings sync store url configured.' ) ;
252
+ }
253
+
254
+ const url = joinPath ( this . userDataSyncStoreUrl , 'collection' ) . toString ( ) ;
255
+ headers = { ...headers } ;
256
+ headers [ 'Content-Type' ] = Mimes . text ;
257
+
258
+ const context = await this . request ( url , { type : 'POST' , headers } , [ ] , CancellationToken . None ) ;
259
+ const collectionId = await asTextOrError ( context ) ;
260
+ if ( ! collectionId ) {
261
+ throw new UserDataSyncStoreError ( 'Server did not return the collection id' , url , UserDataSyncErrorCode . NoCollection , context . res . statusCode , context . res . headers [ HEADER_OPERATION_ID ] ) ;
262
+ }
263
+ return collectionId ;
264
+ }
265
+
266
+ async deleteCollection ( collection ?: string , headers : IHeaders = { } ) : Promise < void > {
267
+ if ( ! this . userDataSyncStoreUrl ) {
268
+ throw new Error ( 'No settings sync store url configured.' ) ;
269
+ }
270
+
271
+ const url = collection ? joinPath ( this . userDataSyncStoreUrl , 'collection' , collection ) . toString ( ) : joinPath ( this . userDataSyncStoreUrl , 'collection' ) . toString ( ) ;
272
+ headers = { ...headers } ;
273
+
274
+ await this . request ( url , { type : 'DELETE' , headers } , [ ] , CancellationToken . None ) ;
275
+ }
276
+
277
+ // #endregion
278
+
279
+ // #region Resource
280
+
281
+ async getAllResourceRefs ( resource : ServerResource , collection ?: string ) : Promise < IResourceRefHandle [ ] > {
235
282
if ( ! this . userDataSyncStoreUrl ) {
236
283
throw new Error ( 'No settings sync store url configured.' ) ;
237
284
}
238
285
239
- const uri = joinPath ( this . userDataSyncStoreUrl , 'resource' , path ) ;
286
+ const uri = this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) ;
240
287
const headers : IHeaders = { } ;
241
288
242
289
const context = await this . request ( uri . toString ( ) , { type : 'GET' , headers } , [ ] , CancellationToken . None ) ;
@@ -245,12 +292,12 @@ export class UserDataSyncStoreClient extends Disposable {
245
292
return result . map ( ( { url, created } ) => ( { ref : relativePath ( uri , uri . with ( { path : url } ) ) ! , created : created * 1000 /* Server returns in seconds */ } ) ) ;
246
293
}
247
294
248
- async resolveResourceContent ( path : string , ref : string , headers : IHeaders = { } ) : Promise < string | null > {
295
+ async resolveResourceContent ( resource : ServerResource , ref : string , collection ? : string , headers : IHeaders = { } ) : Promise < string | null > {
249
296
if ( ! this . userDataSyncStoreUrl ) {
250
297
throw new Error ( 'No settings sync store url configured.' ) ;
251
298
}
252
299
253
- const url = joinPath ( this . userDataSyncStoreUrl , 'resource' , path , ref ) . toString ( ) ;
300
+ const url = joinPath ( this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) , ref ) . toString ( ) ;
254
301
headers = { ...headers } ;
255
302
headers [ 'Cache-Control' ] = 'no-cache' ;
256
303
@@ -259,23 +306,34 @@ export class UserDataSyncStoreClient extends Disposable {
259
306
return content ;
260
307
}
261
308
262
- async deleteResource ( path : string , ref : string | null ) : Promise < void > {
309
+ async deleteResource ( resource : ServerResource , ref : string | null , collection ?: string ) : Promise < void > {
263
310
if ( ! this . userDataSyncStoreUrl ) {
264
311
throw new Error ( 'No settings sync store url configured.' ) ;
265
312
}
266
313
267
- const url = ref !== null ? joinPath ( this . userDataSyncStoreUrl , 'resource' , path , ref ) . toString ( ) : joinPath ( this . userDataSyncStoreUrl , 'resource' , path ) . toString ( ) ;
314
+ const url = ref !== null ? joinPath ( this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) , ref ) . toString ( ) : this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) . toString ( ) ;
268
315
const headers : IHeaders = { } ;
269
316
270
317
await this . request ( url , { type : 'DELETE' , headers } , [ ] , CancellationToken . None ) ;
271
318
}
272
319
273
- async readResource ( path : string , oldValue : IUserData | null , headers : IHeaders = { } ) : Promise < IUserData > {
320
+ async deleteResources ( ) : Promise < void > {
321
+ if ( ! this . userDataSyncStoreUrl ) {
322
+ throw new Error ( 'No settings sync store url configured.' ) ;
323
+ }
324
+
325
+ const url = joinPath ( this . userDataSyncStoreUrl , 'resource' ) . toString ( ) ;
326
+ const headers : IHeaders = { 'Content-Type' : Mimes . text } ;
327
+
328
+ await this . request ( url , { type : 'DELETE' , headers } , [ ] , CancellationToken . None ) ;
329
+ }
330
+
331
+ async readResource ( resource : ServerResource , oldValue : IUserData | null , collection ?: string , headers : IHeaders = { } ) : Promise < IUserData > {
274
332
if ( ! this . userDataSyncStoreUrl ) {
275
333
throw new Error ( 'No settings sync store url configured.' ) ;
276
334
}
277
335
278
- const url = joinPath ( this . userDataSyncStoreUrl , 'resource' , path , 'latest' ) . toString ( ) ;
336
+ const url = joinPath ( this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) , 'latest' ) . toString ( ) ;
279
337
headers = { ...headers } ;
280
338
// Disable caching as they are cached by synchronisers
281
339
headers [ 'Cache-Control' ] = 'no-cache' ;
@@ -307,12 +365,12 @@ export class UserDataSyncStoreClient extends Disposable {
307
365
return userData ;
308
366
}
309
367
310
- async writeResource ( path : string , data : string , ref : string | null , headers : IHeaders = { } ) : Promise < string > {
368
+ async writeResource ( resource : ServerResource , data : string , ref : string | null , collection ?: string , headers : IHeaders = { } ) : Promise < string > {
311
369
if ( ! this . userDataSyncStoreUrl ) {
312
370
throw new Error ( 'No settings sync store url configured.' ) ;
313
371
}
314
372
315
- const url = joinPath ( this . userDataSyncStoreUrl , 'resource' , path ) . toString ( ) ;
373
+ const url = this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) . toString ( ) ;
316
374
headers = { ...headers } ;
317
375
headers [ 'Content-Type' ] = Mimes . text ;
318
376
if ( ref ) {
@@ -328,6 +386,8 @@ export class UserDataSyncStoreClient extends Disposable {
328
386
return newRef ;
329
387
}
330
388
389
+ // #endregion
390
+
331
391
async manifest ( oldValue : IUserDataManifest | null , headers : IHeaders = { } ) : Promise < IUserDataManifest | null > {
332
392
if ( ! this . userDataSyncStoreUrl ) {
333
393
throw new Error ( 'No settings sync store url configured.' ) ;
@@ -388,15 +448,17 @@ export class UserDataSyncStoreClient extends Disposable {
388
448
throw new Error ( 'No settings sync store url configured.' ) ;
389
449
}
390
450
391
- const url = joinPath ( this . userDataSyncStoreUrl , 'resource' ) . toString ( ) ;
392
- const headers : IHeaders = { 'Content-Type' : Mimes . text } ;
393
-
394
- await this . request ( url , { type : 'DELETE' , headers } , [ ] , CancellationToken . None ) ;
451
+ await this . deleteResources ( ) ;
452
+ await this . deleteCollection ( ) ;
395
453
396
454
// clear cached session.
397
455
this . clearSession ( ) ;
398
456
}
399
457
458
+ private getResourceUrl ( userDataSyncStoreUrl : URI , collection : string | undefined , resource : ServerResource ) : URI {
459
+ return collection ? joinPath ( userDataSyncStoreUrl , 'collection' , collection , 'resource' , resource ) : joinPath ( userDataSyncStoreUrl , 'resource' , resource ) ;
460
+ }
461
+
400
462
private clearSession ( ) : void {
401
463
this . storageService . remove ( USER_SESSION_ID_KEY , StorageScope . APPLICATION ) ;
402
464
this . storageService . remove ( MACHINE_SESSION_ID_KEY , StorageScope . APPLICATION ) ;
@@ -551,32 +613,6 @@ export class UserDataSyncStoreService extends UserDataSyncStoreClient implements
551
613
this . _register ( userDataSyncStoreManagementService . onDidChangeUserDataSyncStore ( ( ) => this . updateUserDataSyncStoreUrl ( userDataSyncStoreManagementService . userDataSyncStore ?. url ) ) ) ;
552
614
}
553
615
554
- getAllRefs ( resource : ServerResource , profile ?: string ) : Promise < IResourceRefHandle [ ] > {
555
- return this . getAllResourceRefs ( profile ? this . getProfileResource ( resource , profile ) : resource ) ;
556
- }
557
-
558
- read ( resource : ServerResource , oldValue : IUserData | null , profile ?: string , headers ?: IHeaders ) : Promise < IUserData > {
559
- return this . readResource ( profile ? this . getProfileResource ( resource , profile ) : resource , oldValue , headers ) ;
560
- }
561
-
562
- write ( resource : ServerResource , content : string , ref : string | null , profile ?: string , headers ?: IHeaders ) : Promise < string > {
563
- return this . writeResource ( profile ? this . getProfileResource ( resource , profile ) : resource , content , ref , headers ) ;
564
- }
565
-
566
- delete ( resource : ServerResource , ref : string | null , profile ?: string ) : Promise < void > {
567
- return this . deleteResource ( profile ? this . getProfileResource ( resource , profile ) : resource , ref ) ;
568
- }
569
-
570
- resolveContent ( resource : ServerResource , ref : string , profile ?: string , headers ?: IHeaders ) : Promise < string | null > {
571
- return this . resolveResourceContent ( profile ? this . getProfileResource ( resource , profile ) : resource , ref , headers ) ;
572
- }
573
-
574
- private getProfileResource ( resource : ServerResource , profile : string ) : string {
575
- if ( resource === 'profiles' ) {
576
- throw new Error ( `Invalid Resource Argument: ${ resource } ` ) ;
577
- }
578
- return join ( 'profiles' , profile , resource ) ;
579
- }
580
616
}
581
617
582
618
export class RequestsSession {
0 commit comments