@@ -12,7 +12,6 @@ import { Mimes } from 'vs/base/common/mime';
1212import { isWeb } from 'vs/base/common/platform' ;
1313import { ConfigurationSyncStore } from 'vs/base/common/product' ;
1414import { joinPath , relativePath } from 'vs/base/common/resources' ;
15- import { join } from 'vs/base/common/path' ;
1615import { isObject , isString } from 'vs/base/common/types' ;
1716import { URI } from 'vs/base/common/uri' ;
1817import { generateUuid } from 'vs/base/common/uuid' ;
@@ -231,12 +230,60 @@ export class UserDataSyncStoreClient extends Disposable {
231230 }
232231 }
233232
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 [ ] > {
235282 if ( ! this . userDataSyncStoreUrl ) {
236283 throw new Error ( 'No settings sync store url configured.' ) ;
237284 }
238285
239- const uri = joinPath ( this . userDataSyncStoreUrl , 'resource' , path ) ;
286+ const uri = this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) ;
240287 const headers : IHeaders = { } ;
241288
242289 const context = await this . request ( uri . toString ( ) , { type : 'GET' , headers } , [ ] , CancellationToken . None ) ;
@@ -245,12 +292,12 @@ export class UserDataSyncStoreClient extends Disposable {
245292 return result . map ( ( { url, created } ) => ( { ref : relativePath ( uri , uri . with ( { path : url } ) ) ! , created : created * 1000 /* Server returns in seconds */ } ) ) ;
246293 }
247294
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 > {
249296 if ( ! this . userDataSyncStoreUrl ) {
250297 throw new Error ( 'No settings sync store url configured.' ) ;
251298 }
252299
253- const url = joinPath ( this . userDataSyncStoreUrl , 'resource' , path , ref ) . toString ( ) ;
300+ const url = joinPath ( this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) , ref ) . toString ( ) ;
254301 headers = { ...headers } ;
255302 headers [ 'Cache-Control' ] = 'no-cache' ;
256303
@@ -259,23 +306,34 @@ export class UserDataSyncStoreClient extends Disposable {
259306 return content ;
260307 }
261308
262- async deleteResource ( path : string , ref : string | null ) : Promise < void > {
309+ async deleteResource ( resource : ServerResource , ref : string | null , collection ?: string ) : Promise < void > {
263310 if ( ! this . userDataSyncStoreUrl ) {
264311 throw new Error ( 'No settings sync store url configured.' ) ;
265312 }
266313
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 ( ) ;
268315 const headers : IHeaders = { } ;
269316
270317 await this . request ( url , { type : 'DELETE' , headers } , [ ] , CancellationToken . None ) ;
271318 }
272319
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 > {
274332 if ( ! this . userDataSyncStoreUrl ) {
275333 throw new Error ( 'No settings sync store url configured.' ) ;
276334 }
277335
278- const url = joinPath ( this . userDataSyncStoreUrl , 'resource' , path , 'latest' ) . toString ( ) ;
336+ const url = joinPath ( this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) , 'latest' ) . toString ( ) ;
279337 headers = { ...headers } ;
280338 // Disable caching as they are cached by synchronisers
281339 headers [ 'Cache-Control' ] = 'no-cache' ;
@@ -307,12 +365,12 @@ export class UserDataSyncStoreClient extends Disposable {
307365 return userData ;
308366 }
309367
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 > {
311369 if ( ! this . userDataSyncStoreUrl ) {
312370 throw new Error ( 'No settings sync store url configured.' ) ;
313371 }
314372
315- const url = joinPath ( this . userDataSyncStoreUrl , 'resource' , path ) . toString ( ) ;
373+ const url = this . getResourceUrl ( this . userDataSyncStoreUrl , collection , resource ) . toString ( ) ;
316374 headers = { ...headers } ;
317375 headers [ 'Content-Type' ] = Mimes . text ;
318376 if ( ref ) {
@@ -328,6 +386,8 @@ export class UserDataSyncStoreClient extends Disposable {
328386 return newRef ;
329387 }
330388
389+ // #endregion
390+
331391 async manifest ( oldValue : IUserDataManifest | null , headers : IHeaders = { } ) : Promise < IUserDataManifest | null > {
332392 if ( ! this . userDataSyncStoreUrl ) {
333393 throw new Error ( 'No settings sync store url configured.' ) ;
@@ -388,15 +448,17 @@ export class UserDataSyncStoreClient extends Disposable {
388448 throw new Error ( 'No settings sync store url configured.' ) ;
389449 }
390450
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 ( ) ;
395453
396454 // clear cached session.
397455 this . clearSession ( ) ;
398456 }
399457
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+
400462 private clearSession ( ) : void {
401463 this . storageService . remove ( USER_SESSION_ID_KEY , StorageScope . APPLICATION ) ;
402464 this . storageService . remove ( MACHINE_SESSION_ID_KEY , StorageScope . APPLICATION ) ;
@@ -551,32 +613,6 @@ export class UserDataSyncStoreService extends UserDataSyncStoreClient implements
551613 this . _register ( userDataSyncStoreManagementService . onDidChangeUserDataSyncStore ( ( ) => this . updateUserDataSyncStoreUrl ( userDataSyncStoreManagementService . userDataSyncStore ?. url ) ) ) ;
552614 }
553615
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- }
580616}
581617
582618export class RequestsSession {
0 commit comments