4
4
var isPro = require ( '../libs/debug' ) . isPro ;
5
5
var isDev = require ( '../libs/debug' ) . isDev ;
6
6
var isDbg = require ( '../libs/debug' ) . isDbg ;
7
+ var statusError = require ( '../libs/debug' ) . statusError ;
7
8
8
9
//
9
10
@@ -1181,11 +1182,21 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1181
1182
var libraries = [ ] ;
1182
1183
1183
1184
1184
- if ( ! aMeta || process . env . READ_ONLY_SCRIPT_STORAGE === 'true' ) {
1185
- aCallback ( null ) ;
1185
+ if ( process . env . READ_ONLY_SCRIPT_STORAGE === 'true' ) {
1186
+ aCallback ( new statusError ( {
1187
+ message : 'Read only script storage. Please try again later.' ,
1188
+ code : 501 // Not Implemented
1189
+ } ) , null ) ;
1186
1190
return ;
1187
1191
}
1188
1192
1193
+ if ( ! aMeta ) {
1194
+ aCallback ( new statusError ( {
1195
+ message : 'Metadata block(s) missing.' ,
1196
+ code : 400
1197
+ } ) , null ) ;
1198
+ return ;
1199
+ }
1189
1200
1190
1201
// `@name` validations
1191
1202
if ( ! isLibrary ) {
@@ -1196,7 +1207,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1196
1207
1197
1208
// Can't install a script without a @name (maybe replace with random value)
1198
1209
if ( ! name ) {
1199
- aCallback ( null ) ;
1210
+ aCallback ( new statusError ( {
1211
+ message : '`@name` missing.' ,
1212
+ code : 400
1213
+ } ) , null ) ;
1200
1214
return ;
1201
1215
}
1202
1216
@@ -1207,15 +1221,21 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1207
1221
}
1208
1222
} ) ;
1209
1223
1210
- // Can't install a script without a cleaned @name (maybe replace with random value)
1224
+ // Check for non-localized presence
1211
1225
if ( ! scriptName ) {
1212
- aCallback ( null ) ;
1226
+ aCallback ( new statusError ( {
1227
+ message : '`@name` non-localized missing.' ,
1228
+ code : 400
1229
+ } ) , null ) ;
1213
1230
return ;
1214
1231
}
1215
1232
1216
1233
// Can't install a script name ending in a reserved extension
1217
1234
if ( / \. (?: m i n | u s e r | u s e r \. j s | m e t a ) $ / . test ( scriptName ) ) {
1218
- aCallback ( null ) ;
1235
+ aCallback ( new statusError ( {
1236
+ message : '`@name` ends in a reserved extension.' ,
1237
+ code : 400
1238
+ } ) , null ) ;
1219
1239
return ;
1220
1240
}
1221
1241
@@ -1226,7 +1246,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1226
1246
1227
1247
if ( isLibrary && ! isEqualKeyset ( slaveKeyset , masterKeyset ) ) {
1228
1248
// Keysets do not match exactly... reject
1229
- aCallback ( null ) ;
1249
+ aCallback ( new statusError ( {
1250
+ message : '`@name` must match in UserScript and UserLibrary metadata blocks.' ,
1251
+ code : 400
1252
+ } ) , null ) ;
1230
1253
return ;
1231
1254
}
1232
1255
@@ -1237,7 +1260,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1237
1260
1238
1261
if ( isLibrary && ! isEqualKeyset ( slaveKeyset , masterKeyset ) ) {
1239
1262
// Keysets do not match exactly... reject
1240
- aCallback ( null ) ;
1263
+ aCallback ( new statusError ( {
1264
+ message : '`@description` must match in UserScript and UserLibrary metadata blocks.' ,
1265
+ code : 400
1266
+ } ) , null ) ;
1241
1267
return ;
1242
1268
}
1243
1269
@@ -1248,7 +1274,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1248
1274
1249
1275
if ( isLibrary && ! isEqualKeyset ( slaveKeyset , masterKeyset ) ) {
1250
1276
// Keysets do not match exactly... reject
1251
- aCallback ( null ) ;
1277
+ aCallback ( new statusError ( {
1278
+ message : '`@copyright` must match in UserScript and UserLibrary metadata blocks.' ,
1279
+ code : 400
1280
+ } ) , null ) ;
1252
1281
return ;
1253
1282
}
1254
1283
@@ -1259,7 +1288,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1259
1288
1260
1289
if ( isLibrary && ! isEqualKeyset ( slaveKeyset , masterKeyset ) ) {
1261
1290
// Keysets do not match exactly... reject
1262
- aCallback ( null ) ;
1291
+ aCallback ( new statusError ( {
1292
+ message : '`@license` must match in UserScript and UserLibrary metadata blocks.' ,
1293
+ code : 400
1294
+ } ) , null ) ;
1263
1295
return ;
1264
1296
}
1265
1297
@@ -1273,37 +1305,52 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1273
1305
thatSPDX = userKeyset [ userKeyset . length - 1 ] . split ( '; ' ) [ 0 ] . replace ( / \+ $ / , '' ) ;
1274
1306
if ( SPDXOSI . indexOf ( thatSPDX ) === - 1 ) {
1275
1307
// No valid OSI primary e.g. last key... reject
1276
- aCallback ( null ) ;
1308
+ aCallback ( new statusError ( {
1309
+ message : '`@license` is not OSI primary and compatible in the metadata block(s).' ,
1310
+ code : 400
1311
+ } ) , null ) ;
1277
1312
return ;
1278
1313
}
1279
1314
1280
1315
for ( i = 0 ; userKey = userKeyset [ i ++ ] ; ) {
1281
1316
thisKeyComponents = userKey . split ( '; ' ) ;
1282
1317
if ( thisKeyComponents . length > 2 ) {
1283
1318
// Too many parts... reject
1284
- aCallback ( null ) ;
1319
+ aCallback ( new statusError ( {
1320
+ message : '`@license` has too many parts in the metadata block(s).' ,
1321
+ code : 400
1322
+ } ) , null ) ;
1285
1323
return ;
1286
1324
}
1287
1325
1288
1326
if ( thisKeyComponents . length === 2 ) {
1289
1327
if ( ! isFQUrl ( thisKeyComponents [ 1 ] ) ) {
1290
1328
1291
1329
// Not a web url... reject
1292
- aCallback ( null ) ;
1330
+ aCallback ( new statusError ( {
1331
+ message : '`@license` type component not a web url in the metadata block(s).' ,
1332
+ code : 400
1333
+ } ) , null ) ;
1293
1334
return ;
1294
1335
}
1295
1336
}
1296
1337
1297
1338
thatSPDX = thisKeyComponents [ 0 ] . replace ( / \+ $ / , '' ) ;
1298
1339
if ( SPDX . indexOf ( thatSPDX ) === - 1 || blockSPDX . indexOf ( thatSPDX ) > - 1 ) {
1299
1340
// Absent SPDX short code or blocked SPDX... reject
1300
- aCallback ( null ) ;
1341
+ aCallback ( new statusError ( {
1342
+ message : '`@license` has an incompatible SPDX in the metadata block(s).' ,
1343
+ code : 400
1344
+ } ) , null ) ;
1301
1345
return ;
1302
1346
}
1303
1347
}
1304
1348
} else {
1305
1349
// No licensing... reject
1306
- aCallback ( null ) ;
1350
+ aCallback ( new statusError ( {
1351
+ message : '`@license` is absent in the metadata block(s).' ,
1352
+ code : 400
1353
+ } ) , null ) ;
1307
1354
return ;
1308
1355
}
1309
1356
@@ -1314,7 +1361,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1314
1361
1315
1362
if ( isLibrary && ! isEqualKeyset ( slaveKeyset , masterKeyset ) ) {
1316
1363
// Keysets do not match exactly... reject
1317
- aCallback ( null ) ;
1364
+ aCallback ( new statusError ( {
1365
+ message : '`@version` must match in UserScript and UserLibrary metadata blocks.' ,
1366
+ code : 400
1367
+ } ) , null ) ;
1318
1368
return ;
1319
1369
}
1320
1370
@@ -1325,7 +1375,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1325
1375
if ( ! isFQUrl ( supportURL , true ) ) {
1326
1376
1327
1377
// Not a web url... reject
1328
- aCallback ( null ) ;
1378
+ aCallback ( new statusError ( {
1379
+ message : '`@supportURL` not a web url in the UserScript metadata block.' ,
1380
+ code : 400
1381
+ } ) , null ) ;
1329
1382
return ;
1330
1383
}
1331
1384
}
@@ -1338,7 +1391,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1338
1391
if ( ! isFQUrl ( homepageURL ) ) {
1339
1392
1340
1393
// Not a web url... reject
1341
- aCallback ( null ) ;
1394
+ aCallback ( new statusError ( {
1395
+ message : '`@homepageURL` not a web url' ,
1396
+ code : 400
1397
+ } ) , null ) ;
1342
1398
return ;
1343
1399
}
1344
1400
}
@@ -1351,7 +1407,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1351
1407
if ( ! isFQUrl ( updateURL ) ) {
1352
1408
1353
1409
// Not a web url... reject
1354
- aCallback ( null ) ;
1410
+ aCallback ( new statusError ( {
1411
+ message : '`@updateURL` not a web url' ,
1412
+ code : 400
1413
+ } ) , null ) ;
1355
1414
return ;
1356
1415
}
1357
1416
}
@@ -1363,7 +1422,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1363
1422
if ( ! isFQUrl ( downloadURL ) ) {
1364
1423
1365
1424
// Not a web url... reject
1366
- aCallback ( null ) ;
1425
+ aCallback ( new statusError ( {
1426
+ message : '`@downloadURL` not a web url' ,
1427
+ code : 400
1428
+ } ) , null ) ;
1367
1429
return ;
1368
1430
}
1369
1431
@@ -1373,7 +1435,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1373
1435
if ( rAnyLocalHost . test ( downloadURL . host ) &&
1374
1436
! / ^ \/ (?: i n s t a l l | s r c \/ s c r i p t s ) \/ / . test ( downloadURL . pathname ) )
1375
1437
{
1376
- aCallback ( null ) ;
1438
+ aCallback ( new statusError ( {
1439
+ message : '`@downloadURL` not .user.js' ,
1440
+ code : 400
1441
+ } ) , null ) ;
1377
1442
return ;
1378
1443
}
1379
1444
@@ -1382,7 +1447,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1382
1447
/ ^ \/ (?: i n s t a l l | s r c \/ s c r i p t s ) \/ / . test ( downloadURL . pathname ) &&
1383
1448
/ \. m e t a \. j s $ / . test ( downloadURL . pathname ) )
1384
1449
{
1385
- aCallback ( null ) ;
1450
+ aCallback ( new statusError ( {
1451
+ message : '`@downloadURL` not .user.js' ,
1452
+ code : 400
1453
+ } ) , null ) ;
1386
1454
return ;
1387
1455
}
1388
1456
}
@@ -1401,7 +1469,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1401
1469
}
1402
1470
1403
1471
if ( missingExcludeAll ) {
1404
- aCallback ( null ) ;
1472
+ aCallback ( new statusError ( {
1473
+ message : 'UserScript Metadata Block missing `@exclude *`.' ,
1474
+ code : 400
1475
+ } ) , null ) ;
1405
1476
return ;
1406
1477
}
1407
1478
}
@@ -1449,8 +1520,17 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1449
1520
var script = null ;
1450
1521
var s3 = null ;
1451
1522
1452
- if ( aRemoved || ( ! aScript && ( aUpdate || collaboration ) ) ) {
1453
- aCallback ( null ) ;
1523
+ if ( aRemoved ) {
1524
+ aCallback ( new statusError ( {
1525
+ message : 'Script removed permanently.' ,
1526
+ code : 403
1527
+ } ) , null ) ;
1528
+ return ;
1529
+ } else if ( ( ! aScript && ( aUpdate || collaboration ) ) ) {
1530
+ aCallback ( new statusError ( {
1531
+ message : 'Collaboration restricted.' ,
1532
+ code : 403
1533
+ } ) , null ) ;
1454
1534
return ;
1455
1535
} else if ( ! aScript ) {
1456
1536
// New script
@@ -1485,7 +1565,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1485
1565
JSON . stringify ( findMeta ( script . meta , 'OpenUserJS.collaborator.value' ) ) !==
1486
1566
JSON . stringify ( collaborators ) ) ) {
1487
1567
1488
- aCallback ( null ) ;
1568
+ aCallback ( new statusError ( {
1569
+ message : 'Forbidden with collaboration' ,
1570
+ code : 403
1571
+ } ) , null ) ;
1489
1572
return ;
1490
1573
}
1491
1574
aScript . meta = aMeta ;
@@ -1524,7 +1607,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1524
1607
aErr . stack
1525
1608
) ;
1526
1609
1527
- aCallback ( null ) ;
1610
+ aCallback ( new statusError ( {
1611
+ message : 'Remote storage write error' ,
1612
+ code : 502
1613
+ } ) , null ) ;
1528
1614
return ;
1529
1615
}
1530
1616
@@ -1538,7 +1624,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1538
1624
installName + '\n' +
1539
1625
JSON . stringify ( aErr , null , ' ' )
1540
1626
) ;
1541
- aCallback ( null ) ;
1627
+ aCallback ( new statusError ( {
1628
+ message : 'Database write error' ,
1629
+ code : 502
1630
+ } ) , null ) ;
1542
1631
return ;
1543
1632
}
1544
1633
@@ -1554,7 +1643,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1554
1643
userDoc . name + ' was NOT role elevated from User to Author with err of:\n' +
1555
1644
JSON . stringify ( aErr , null , ' ' )
1556
1645
) ;
1557
- aCallback ( aScript ) ;
1646
+ aCallback ( new statusError ( {
1647
+ message : 'Database find error' ,
1648
+ code : 502
1649
+ } ) , aScript ) ;
1558
1650
return ;
1559
1651
}
1560
1652
@@ -1567,7 +1659,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1567
1659
) ;
1568
1660
// fallthrough
1569
1661
}
1570
- aCallback ( aScript ) ;
1662
+ aCallback ( null , aScript ) ;
1571
1663
} ) ;
1572
1664
} ) ;
1573
1665
} else {
@@ -1580,18 +1672,21 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
1580
1672
) ;
1581
1673
// fallthrough
1582
1674
}
1583
- aCallback ( aScript ) ;
1675
+ aCallback ( null , aScript ) ;
1584
1676
} ) ;
1585
1677
}
1586
1678
} else {
1587
- aCallback ( aScript ) ;
1679
+ aCallback ( null , aScript ) ;
1588
1680
}
1589
1681
} ) ;
1590
1682
} ) ;
1591
1683
} else {
1592
1684
// NOTE: This shouldn't happen
1593
1685
console . warn ( 'S3 `new AWS.S3()` critical error' ) ;
1594
- aCallback ( null ) ;
1686
+ aCallback ( new statusError ( {
1687
+ message : 'Storage critical error' ,
1688
+ code : 500
1689
+ } ) , null ) ;
1595
1690
return ;
1596
1691
}
1597
1692
} ) ;
0 commit comments