@@ -7,6 +7,7 @@ import Gitlab from 'gitlab';
77
88import { UserDataGroups } from './authcache' ;
99import { AuthCache , UserData } from './authcache' ;
10+ import { GitlabCache } from "./gitlabcache" ;
1011
1112export type VerdaccioGitlabAccessLevel = '$guest' | '$reporter' | '$developer' | '$maintainer' | '$owner' ;
1213
@@ -42,13 +43,16 @@ export default class VerdaccioGitLab implements IPluginAuth<VerdaccioGitlabConfi
4243 private options : PluginOptions < VerdaccioGitlabConfig > ;
4344 private config : VerdaccioGitlabConfig ;
4445 private authCache ?: AuthCache ;
46+ private gitlabCache : GitlabCache ;
4547 private logger : Logger ;
4648 private publishLevel : VerdaccioGitlabAccessLevel ;
4749
4850 public constructor ( config : VerdaccioGitlabConfig , options : PluginOptions < VerdaccioGitlabConfig > ) {
4951 this . logger = options . logger ;
5052 this . config = config ;
5153 this . options = options ;
54+ this . gitlabCache = new GitlabCache ( this . logger , this . config . authCache ?. ttl ) ;
55+
5256 this . logger . info ( `[gitlab] url: ${ this . config . url } ` ) ;
5357
5458 if ( ( this . config . authCache || { } ) . enabled === false ) {
@@ -89,7 +93,19 @@ export default class VerdaccioGitLab implements IPluginAuth<VerdaccioGitlabConfi
8993 token : password ,
9094 } ) ;
9195
92- GitlabAPI . Users . current ( )
96+ // Check if we already have a stored promise
97+ let promise = this . gitlabCache . getPromise ( user , password , 'user' ) ;
98+ if ( ! promise ) {
99+ this . logger . trace ( `[gitlab] querying gitlab user: ${ user } ` ) ;
100+
101+ promise = GitlabAPI . Users . current ( ) as Promise < any > ;
102+
103+ this . gitlabCache . storePromise ( user , password , 'user' , promise ) ;
104+ } else {
105+ this . logger . trace ( `[gitlab] using cached promise for user: ${ user } ` ) ;
106+ }
107+
108+ promise
93109 . then ( response => {
94110 if ( user . toLowerCase ( ) !== response . username . toLowerCase ( ) ) {
95111 return cb ( getUnauthorized ( 'wrong gitlab username' ) ) ;
@@ -102,15 +118,31 @@ export default class VerdaccioGitLab implements IPluginAuth<VerdaccioGitlabConfi
102118 // - for publish, the logged in user id and all the groups they can reach as configured with access level `$auth.gitlab.publish`
103119 const gitlabPublishQueryParams = { min_access_level : publishLevelId } ;
104120
105- this . logger . trace ( '[gitlab] querying gitlab user groups with params:' , gitlabPublishQueryParams . toString ( ) ) ;
121+ let groupsPromise = this . gitlabCache . getPromise ( user , password , 'groups' ) ;
122+ if ( ! groupsPromise ) {
123+ this . logger . trace ( '[gitlab] querying gitlab user groups with params:' , gitlabPublishQueryParams . toString ( ) ) ;
124+
125+ groupsPromise = GitlabAPI . Groups . all ( gitlabPublishQueryParams ) . then ( groups => {
126+ return groups . filter ( group => group . path === group . full_path ) . map ( group => group . path ) ;
127+ } ) ;
128+
129+ this . gitlabCache . storePromise ( user , password , 'groups' , groupsPromise ) ;
130+ } else {
131+ this . logger . trace ( '[gitlab] using cached promise for user groups with params:' , gitlabPublishQueryParams . toString ( ) ) ;
132+ }
106133
107- const groupsPromise = GitlabAPI . Groups . all ( gitlabPublishQueryParams ) . then ( groups => {
108- return groups . filter ( group => group . path === group . full_path ) . map ( group => group . path ) ;
109- } ) ;
134+ let projectsPromise = this . gitlabCache . getPromise ( user , password , 'projects' ) ;
135+ if ( ! projectsPromise ) {
136+ this . logger . trace ( '[gitlab] querying gitlab user projects with params:' , gitlabPublishQueryParams . toString ( ) ) ;
110137
111- const projectsPromise = GitlabAPI . Projects . all ( gitlabPublishQueryParams ) . then ( projects => {
112- return projects . map ( project => project . path_with_namespace ) ;
113- } ) ;
138+ projectsPromise = GitlabAPI . Projects . all ( gitlabPublishQueryParams ) . then ( projects => {
139+ return projects . map ( project => project . path_with_namespace ) ;
140+ } ) ;
141+
142+ this . gitlabCache . storePromise ( user , password , 'projects' , projectsPromise ) ;
143+ } else {
144+ this . logger . trace ( '[gitlab] using cached promise for user projects with params:' , gitlabPublishQueryParams . toString ( ) ) ;
145+ }
114146
115147 Promise . all ( [ groupsPromise , projectsPromise ] )
116148 . then ( ( [ groups , projectGroups ] ) => {
0 commit comments