@@ -203,58 +203,81 @@ exports.getSource = function (aReq, aCallback) {
203
203
} ) ;
204
204
} ;
205
205
206
- exports . sendScript = function ( aReq , aRes , aNext ) {
207
- if ( aReq . params . type === 'libs' ) {
208
- aReq . params . isLib = true ;
209
- }
206
+ exports . keyScript = function ( aReq , aRes , aNext ) {
207
+ let pathname = aReq . _parsedUrl . pathname ;
208
+ let isLib = / ^ \/ s r c \/ l i b s \/ / . test ( pathname ) ;
209
+
210
+ let installName = pathname . replace ( / ^ \/ (?: i n s t a l l | s r c \/ (?: s c r i p t s | l i b s ) ) \/ / , '' ) ;
210
211
211
- let accept = aReq . headers . accept ;
212
+ let rUserJS = / \. u s e r \. j s $ / ;
213
+ let rMetaJS = / \. m e t a \. j s $ / ;
214
+ let rJS = / \. j s $ / ;
215
+
216
+ let accept = aReq . headers . accept || '*/*' ;
212
217
let accepts = null ;
218
+
213
219
let hasAcceptUserScriptMeta = false ;
214
220
let hasAcceptNotAcceptable = false ;
215
- let url = URL . parse ( aReq . url ) ;
216
221
217
- let isLib = aReq . params . isLib || / ^ \/ l i b s \/ / . test ( url . pathname ) ;
218
- let rUserJS = / \. u s e r \. j s $ / ;
219
- let rMetaJS = / \. m e t a \. j s $ / ;
222
+ let parts = installName . split ( '/' ) ;
223
+ let userName = parts [ 0 ] . toLowerCase ( ) ;
224
+ let scriptName = parts [ 1 ] ;
220
225
221
226
if ( ! isLib ) {
222
- if ( accept ) {
223
- accepts = accept . split ( ',' ) ;
227
+ accepts = accept . split ( ',' ) ;
228
+
229
+ if ( rUserJS . test ( scriptName ) ) {
224
230
accepts . forEach ( function ( aElement , aIndex , aArray ) {
225
231
let acceptItem = aElement . trim ( ) ;
226
232
227
- if ( / ^ t e x t \/ x \- u s e r s c r i p t \- m e t a / . test ( acceptItem ) ) { // TODO: toggle `\-meta` in re
228
- hasAcceptUserScriptMeta = true ;
229
- }
230
-
231
- // Find 406 (not acceptables)
233
+ // Find not acceptables
232
234
if ( / ^ i m a g e \/ / . test ( acceptItem ) ) {
233
235
hasAcceptNotAcceptable = true ;
234
236
}
235
237
238
+ // Find acceptables
239
+ if ( / ^ t e x t \/ x \- u s e r s c r i p t \- m e t a / . test ( acceptItem ) ) {
240
+ hasAcceptUserScriptMeta = true ;
241
+ }
236
242
} ) ;
237
- }
238
243
239
- // Test for 406 (not acceptables)
240
- if ( hasAcceptNotAcceptable && rUserJS . test ( url . pathname ) ) {
241
- aRes . status ( 406 ) . send ( ) ;
242
- return ;
243
- }
244
+ // Test acceptables
245
+ if ( hasAcceptNotAcceptable ) {
246
+ aRes . status ( 406 ) . send ( ) ;
247
+ return ;
248
+ }
244
249
245
- if ( hasAcceptUserScriptMeta && rUserJS . test ( url . pathname ) ||
246
- rMetaJS . test ( url . pathname ) ) {
247
- //
248
- exports . sendMeta ( aReq , aRes , aNext ) ;
250
+ if ( hasAcceptUserScriptMeta ) {
251
+ exports . sendMeta ( aReq , aRes , aNext ) ;
252
+ return ;
253
+ }
254
+
255
+ aNext ( userName + '/' + scriptName . replace ( / ( \. m i n ) ? \. u s e r \. j s / , '.user.js' ) ) ;
249
256
return ;
257
+
258
+ } else if ( rMetaJS . test ( scriptName ) ) {
259
+ if ( ! / \. m i n \. m e t a \. j s $ / . test ( scriptName ) ) {
260
+ exports . sendMeta ( aReq , aRes , aNext ) ;
261
+ return ;
262
+ }
250
263
}
251
- } else {
252
- if ( rMetaJS . test ( url . pathname ) ) {
253
- aNext ( ) ;
264
+ } else if ( rJS . test ( scriptName ) ) {
265
+ aNext ( userName + '/' + scriptName . replace ( / ( \. m i n ) ? \. j s / , '.js' ) ) ;
254
266
return ;
255
- }
256
267
}
257
268
269
+ // No matches so return a bad request
270
+ aRes . status ( 400 ) . send ( ) ;
271
+ }
272
+
273
+ exports . sendScript = function ( aReq , aRes , aNext ) {
274
+ if ( aReq . params . type === 'libs' ) {
275
+ aReq . params . isLib = true ;
276
+ }
277
+
278
+ let pathname = aReq . _parsedUrl . pathname ;
279
+ let isLib = aReq . params . isLib || / ^ \/ s r c \/ l i b s \/ / . test ( pathname ) ;
280
+
258
281
exports . getSource ( aReq , function ( aScript , aStream ) {
259
282
let chunks = [ ] ;
260
283
let updateURL = null ;
@@ -581,7 +604,6 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
581
604
var name = null ;
582
605
var thisName = null ;
583
606
var scriptName = null ;
584
- var updateURL = null ;
585
607
var author = null ;
586
608
var collaborators = null ;
587
609
var installName = aUser . name + '/' ;
0 commit comments