@@ -6,9 +6,9 @@ const URI = HyperSwitch.URI;
66const HTTPError = HyperSwitch . HTTPError ;
77
88const uuidv1 = require ( 'uuid/v1' ) ;
9- const uuidUtils = require ( '../lib /uuidUtils' ) ;
9+ const uuidUtils = require ( './uuidUtils' ) ;
1010
11- const mwUtil = require ( '../lib /mwUtil' ) ;
11+ const mwUtil = require ( './mwUtil' ) ;
1212
1313// Temporary work-around for Parsoid issue
1414// https://phabricator.wikimedia.org/T93715
@@ -104,12 +104,15 @@ class ParsoidService {
104104
105105 _initOpts ( opts = { } ) {
106106 this . options = opts ;
107- this . parsoidHost = opts . parsoidHost ;
107+ this . parsoidUri = opts . host || opts . parsoidHost ;
108108 this . options . stash_ratelimit = opts . stash_ratelimit || 5 ;
109- this . options . grace_ttl = opts . grace_ttl || 86400 ;
110109 this . _blacklist = compileReRenderBlacklist ( opts . rerenderBlacklist ) ;
111- if ( ! opts . parsoidHost ) {
112- throw new Error ( 'Parsoid module: the option parsoidHost must be provided!' ) ;
110+ if ( ! this . parsoidUri ) {
111+ throw new Error ( 'Parsoid module: the option host must be provided!' ) ;
112+ }
113+ // remove the trailing slash, if any
114+ if ( this . parsoidUri . slice ( - 1 ) === '/' ) {
115+ this . parsoidUri = this . parsoidUri . slice ( 0 , - 1 ) ;
113116 }
114117 }
115118
@@ -142,34 +145,6 @@ class ParsoidService {
142145 }
143146 }
144147
145- /**
146- * Get the URI of a bucket for the latest Parsoid content.
147- * @param {string } domain the domain name.
148- * @param {string } title the article title.
149- * @return {HyperSwitch.URI }
150- */
151- getLatestBucketURI ( domain , title ) {
152- return new URI ( [
153- domain , 'sys' , 'key_value' , 'parsoid' , title
154- ] ) ;
155- }
156-
157- /**
158- * Get the URI of a bucket for stashing Parsoid content. Used both for stashing
159- * original HTML/Data-Parsoid for normal edits as well as for stashing transforms.
160- *
161- * @param {string } domain the domain name.
162- * @param {string } title the article title.
163- * @param {number } revision the revision of the article.
164- * @param {string } tid the TID of the content.
165- * @return {HyperSwitch.URI }
166- */
167- getStashBucketURI ( domain , title , revision , tid ) {
168- return new URI ( [
169- domain , 'sys' , 'key_value' , 'parsoid-stash' , `${ title } :${ revision } :${ tid } `
170- ] ) ;
171- }
172-
173148 /**
174149 * Get full content from the stash bucket.
175150 * @param {HyperSwitch } hyper the hyper object to route requests
@@ -183,7 +158,7 @@ class ParsoidService {
183158 */
184159 _getStashedContent ( hyper , domain , title , revision , tid ) {
185160 return hyper . get ( {
186- uri : this . getStashBucketURI ( domain , title , revision , tid )
161+ uri : this . _getStashBucketURI ( domain , title , revision , tid )
187162 } )
188163 . then ( ( res ) => {
189164 res = res . body ;
@@ -198,7 +173,7 @@ class ParsoidService {
198173 const htmlResponse = parsoidResp . body . html ;
199174 const etag = mwUtil . parseETag ( parsoidResp . headers . etag ) ;
200175 return hyper . put ( {
201- uri : this . getStashBucketURI ( rp . domain , rp . title , etag . rev , etag . tid ) ,
176+ uri : this . _getStashBucketURI ( rp . domain , rp . title , etag . rev , etag . tid ) ,
202177 // Note. The headers we are storing here are for the whole pagebundle response.
203178 // The individual components of the pagebundle contain their own headers that
204179 // which are used to generate actual responses.
@@ -230,7 +205,7 @@ class ParsoidService {
230205 saveParsoidResultToLatest ( hyper , domain , title , parsoidResp ) {
231206 const dataParsoidResponse = parsoidResp . body [ 'data-parsoid' ] ;
232207 const htmlResponse = parsoidResp . body . html ;
233- return hyper . get ( { uri : this . getLatestBucketURI ( domain , title ) } )
208+ return hyper . get ( { uri : this . _getLatestBucketURI ( domain , title ) } )
234209 . then ( ( existingRes ) => {
235210 // TODO: This is a race condition and we're doing a write after read
236211 // in a distributed concurrent environment. For revisions this should
@@ -244,7 +219,7 @@ class ParsoidService {
244219 return existingRes ;
245220 } )
246221 . catch ( { status : 404 } , { status : 412 } , ( ) => hyper . put ( {
247- uri : this . getLatestBucketURI ( domain , title ) ,
222+ uri : this . _getLatestBucketURI ( domain , title ) ,
248223 // Note. The headers we are storing here are for the whole pagebundle response.
249224 // The individual components of the pagebundle contain their own headers that
250225 // which are used to generate actual responses.
@@ -270,7 +245,7 @@ class ParsoidService {
270245 const etag = mwUtil . makeETag ( rp . revision , tid , 'stash' ) ;
271246 const wtType = req . original && req . original . headers [ 'content-type' ] || 'text/plain' ;
272247 return transformPromise . then ( ( original ) => hyper . put ( {
273- uri : this . getStashBucketURI ( rp . domain , rp . title , rp . revision , tid ) ,
248+ uri : this . _getStashBucketURI ( rp . domain , rp . title , rp . revision , tid ) ,
274249 headers : {
275250 'x-store-etag' : etag ,
276251 'content-type' : 'application/octet-stream' ,
@@ -310,9 +285,9 @@ class ParsoidService {
310285 */
311286 _getContentWithFallback ( hyper , domain , title , revision , tid ) {
312287 if ( ! revision && ! tid ) {
313- return hyper . get ( { uri : this . getLatestBucketURI ( domain , title ) } ) ;
288+ return hyper . get ( { uri : this . _getLatestBucketURI ( domain , title ) } ) ;
314289 } else if ( ! tid ) {
315- return hyper . get ( { uri : this . getLatestBucketURI ( domain , title ) } )
290+ return hyper . get ( { uri : this . _getLatestBucketURI ( domain , title ) } )
316291 . then ( ( res ) => {
317292 const resEtag = mwUtil . parseETag ( res . headers . etag ) ;
318293 if ( revision !== resEtag . rev ) {
@@ -322,10 +297,10 @@ class ParsoidService {
322297 } ) ;
323298 } else {
324299 return hyper . get ( {
325- uri : this . getStashBucketURI ( domain , title , revision , tid )
300+ uri : this . _getStashBucketURI ( domain , title , revision , tid )
326301 } )
327302 . catch ( { status : 404 } , ( ) =>
328- hyper . get ( { uri : this . getLatestBucketURI ( domain , title ) } )
303+ hyper . get ( { uri : this . _getLatestBucketURI ( domain , title ) } )
329304 . then ( ( res ) => {
330305 const resEtag = mwUtil . parseETag ( res . headers . etag ) ;
331306 if ( revision !== resEtag . rev || tid !== resEtag . tid ) {
@@ -339,12 +314,10 @@ class ParsoidService {
339314
340315 _getPageBundleFromParsoid ( hyper , req ) {
341316 const rp = req . params ;
342- const parsoidURI = `${ this . parsoidHost } /${ rp . domain } /v3/page/pagebundle/` +
343- `${ encodeURIComponent ( rp . title ) } /${ rp . revision } ` ;
344- return hyper . get ( {
345- uri : new URI ( parsoidURI ) ,
346- headers : req . headers
347- } ) ;
317+ return hyper . get ( this . _getParsoidReq (
318+ req ,
319+ `page/pagebundle/${ encodeURIComponent ( rp . title ) } /${ rp . revision } `
320+ ) ) ;
348321 }
349322
350323 /**
@@ -654,18 +627,18 @@ class ParsoidService {
654627 parsoidExtraPath = `/${ parsoidExtraPath } ` ;
655628 }
656629
657- const parsoidReq = {
658- uri : ` ${ this . parsoidHost } / ${ rp . domain } /v3/transform/` +
659- ` ${ parsoidFrom } /to/${ parsoidTo } ${ parsoidExtraPath } `,
660- headers : {
630+ const parsoidReq = this . _getParsoidReq (
631+ req ,
632+ `transform/ ${ parsoidFrom } /to/${ parsoidTo } ${ parsoidExtraPath } `,
633+ {
661634 'content-type' : 'application/json' ,
662635 'user-agent' : req [ 'user-agent' ] ,
663636 'content-language' : req . headers [ 'content-language' ] ,
664637 accept : req . headers . accept ,
665638 'accept-language' : req . headers [ 'accept-language' ]
666639 } ,
667- body : req . body
668- } ;
640+ req . body
641+ ) ;
669642
670643 const transformPromise = hyper . post ( parsoidReq ) ;
671644 if ( req . body . stash && from === 'wikitext' && to === 'html' ) {
@@ -677,12 +650,11 @@ class ParsoidService {
677650
678651 getLintErrors ( hyper , req ) {
679652 const rp = req . params ;
680- let path = `${ this . parsoidHost } /${ rp . domain } /v3/transform/` +
681- `wikitext/to/lint/${ encodeURIComponent ( rp . title ) } ` ;
653+ let path = `transform/wikitext/to/lint/${ encodeURIComponent ( rp . title ) } ` ;
682654 if ( rp . revision ) {
683655 path += `/${ rp . revision } ` ;
684656 }
685- return hyper . post ( { uri : path } ) ;
657+ return hyper . post ( this . _getParsoidReq ( req , path , { } ) ) ;
686658 }
687659
688660 makeTransform ( from , to ) {
0 commit comments