Skip to content

Commit d433f0a

Browse files
author
Martii
committed
Some misc STYLEGUIDE.md conformance in .../scriptStorage.js
* Misplaced `var` declarations ... closer to current ES5.x implementation for some others just misplaced * Reordered on first use for all vars... not as critical but good for debugging * Changed `s3` early initialization... shouldn't need to be **and if it did** it should always be `NOTE:`d with a comment * Some newlines for white space usage... easier reading/debugging * Any `require` statements all at top... `require` functions become cached and always stay loaded after first use... so really shouldn't be attempting to optimize these in our code... only perceived benefit is startup pm time but is loaded elsewhere so no real benefit. * Some notation of `WARNING:` statement on incomplete error handling * White-space trim that my editor didn't catch on a previous commit even though it is on all the time everywhere. :\ * `WARNING:` note added for resaving script model object for no apparent reason... again should be notated. * Max line length of 100 conformance Applies with OpenUserJS#285, OpenUserJS#486, OpenUserJS#390, and OpenUserJS#19 ... perhaps some more... very minimal change in project behavior here which is why it is isolated.
1 parent 50ad0a3 commit d433f0a

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

controllers/scriptStorage.js

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ var AWS = require('aws-sdk');
1010

1111
var Script = require('../models/script').Script;
1212
var User = require('../models/user').User;
13+
var RepoManager = require('../libs/repoManager');
1314

1415
var cleanFilename = require('../libs/helpers').cleanFilename;
1516
var findDeadorAlive = require('../libs/remove').findDeadorAlive;
1617
var userRoles = require('../models/userRoles.json');
1718

1819
var bucketName = 'OpenUserJS.org';
1920

21+
var DEV_AWS_URL = null;
22+
2023
if (isPro) {
21-
AWS.config.update({ region: 'us-east-1' });
24+
AWS.config.update({
25+
region: 'us-east-1'
26+
});
2227
} else {
2328
// You need to install (and ruby too): https://github.com/jubos/fake-s3
2429
// Then run the fakes3.sh script or: fakes3 -r fakeS3 -p 10001
25-
var DEV_AWS_URL = process.env.DEV_AWS_URL || 'http://localhost:10001';
30+
DEV_AWS_URL = process.env.DEV_AWS_URL || 'http://localhost:10001';
2631
AWS.config.update({
2732
accessKeyId: 'fakeId',
2833
secretAccessKey: 'fakeKey',
@@ -46,8 +51,8 @@ exports.caseInsensitive = caseInsensitive;
4651
function caseSensitive(aInstallName, aMoreThanInstallName) {
4752

4853
var rMatchExpression = aMoreThanInstallName ? /^(.*)\/(.*)\/(.*)\/(.*)$/ : /^(.*)\/(.*)$/;
49-
var username = '';
5054
var char = null;
55+
var username = '';
5156
var rExpression = null;
5257

5358
var matches = aInstallName.match(rMatchExpression);
@@ -101,12 +106,13 @@ function caseSensitive(aInstallName, aMoreThanInstallName) {
101106
exports.caseSensitive = caseSensitive;
102107

103108
exports.getSource = function (aReq, aCallback) {
104-
var s3 = new AWS.S3();
105109
var installName = getInstallName(aReq);
106-
107110
Script.findOne({ installName: caseSensitive(installName) },
108111
function (aErr, aScript) {
109112
var s3Object = null;
113+
var s3 = new AWS.S3();
114+
115+
// WARNING: Partial error handling at this stage
110116

111117
if (!aScript) {
112118
return aCallback(null);
@@ -156,7 +162,9 @@ exports.sendScript = function (aReq, aRes, aNext) {
156162
++aScript.installs;
157163
++aScript.installsSinceUpdate;
158164

159-
aScript.save(function (aErr, aScript) { });
165+
aScript.save(function (aErr, aScript) {
166+
// WARNING: Resaving the script... WHY?
167+
});
160168
});
161169
};
162170

@@ -195,7 +203,8 @@ exports.sendMeta = function (aReq, aRes, aNext) {
195203
data = meta[prefix][key];
196204
if (data instanceof Array) {
197205
data.forEach(function (aValue) {
198-
aRes.write('// @' + prefix + ':' + key + (aValue ? whitespace + aValue : '') + '\n');
206+
aRes.write('// @' + prefix + ':' + key + (aValue ? whitespace + aValue : '') +
207+
'\n');
199208
});
200209
}
201210
else {
@@ -240,15 +249,19 @@ exports.findMeta = findMeta;
240249
// Modified from Count Issues (http://userscripts.org/scripts/show/69307)
241250
// By Marti Martz (http://userscripts.org/users/37004)
242251
function parseMeta(aString, aNormalize) {
252+
var lines = {};
243253
var rLine = /\/\/ @(\S+)(?:\s+(.*))?/;
254+
var line = null;
255+
var header = null;
256+
244257
var headers = {};
258+
var lineMatches = null;
245259
var name = null;
246-
var prefix = null;
247-
var key = null;
248260
var value = null;
249-
var line = null;
250-
var lineMatches = null;
251-
var lines = {};
261+
var key = null;
262+
var prefix = null;
263+
var unique = null;
264+
var one = null;
252265
var uniques = {
253266
'description': true,
254267
'icon': true,
@@ -258,16 +271,15 @@ function parseMeta(aString, aNormalize) {
258271
'version': true,
259272
'oujs:author': true
260273
};
261-
var unique = null;
262-
var one = null;
263274
var matches = null;
264275

276+
265277
lines = aString.split(/[\r\n]+/).filter(function (aElement, aIndex, aArray) {
266278
return (aElement.match(rLine));
267279
});
268280

269281
for (line in lines) {
270-
var header = null;
282+
header = null;
271283

272284
lineMatches = lines[line].replace(/\s+$/, '').match(rLine);
273285
name = lineMatches[1];
@@ -343,9 +355,9 @@ exports.getMeta = function (aChunks, aCallback) {
343355
// parse the header. But strings are memory inefficient compared
344356
// to buffers so we only convert the least number of chunks to
345357
// get the user script header.
346-
var str = '';
347358
var i = 0;
348359
var header = null;
360+
var str = '';
349361

350362
for (; i < aChunks.length; ++i) {
351363
header = null;
@@ -361,21 +373,21 @@ exports.getMeta = function (aChunks, aCallback) {
361373
};
362374

363375
exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
364-
var s3 = new AWS.S3();
376+
var isLibrary = typeof aMeta === 'string';
365377
var name = null;
366378
var scriptName = null;
379+
var author = null;
380+
var collaborators = null;
367381
var installName = aUser.name + '/';
368-
var isLibrary = typeof aMeta === 'string';
369-
var libraries = [];
382+
var collaboration = false;
370383
var requires = null;
371384
var match = null;
372-
var collaboration = false;
373-
var collaborators = null;
374-
var author = null;
375385
var rLibrary = new RegExp(
376386
'^(?:(?:(?:https?:)?\/\/' +
377387
(isPro ? 'openuserjs\.org' : 'localhost:8080') +
378388
')?\/(?:libs\/src|src\/libs)\/)?(.*?)([^\/]*\.js)$', '');
389+
var libraries = [];
390+
379391

380392
if (!aMeta) {
381393
return aCallback(null);
@@ -468,7 +480,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
468480
if (!aScript.isLib) {
469481
if (collaboration &&
470482
(findMeta(script.meta, 'oujs.author') !== findMeta(aMeta, 'oujs.author') ||
471-
JSON.stringify(findMeta(script.meta, 'oujs.collaborator')) !=
483+
JSON.stringify(findMeta(script.meta, 'oujs.collaborator')) !=
472484
JSON.stringify(collaborators))) {
473485
return aCallback(null);
474486
}
@@ -482,8 +494,14 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
482494
}
483495

484496
aScript.save(function (aErr, aScript) {
497+
var s3 = new AWS.S3();
498+
499+
// WARNING: No error handling at this stage
500+
485501
s3.putObject({ Bucket: bucketName, Key: installName, Body: aBuf },
486502
function (aErr, aData) {
503+
var userDoc = null;
504+
487505
// Don't save a script if storing failed
488506
if (aErr) {
489507
console.error(aUser.name, '-', installName);
@@ -493,13 +511,15 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
493511
}
494512

495513
if (aUser.role === userRoles.length - 1) {
496-
var userDoc = aUser;
514+
userDoc = aUser;
497515
if (!userDoc.save) {
498516
// We're probably using req.session.user which may have gotten serialized.
499517
userDoc = new User(userDoc);
500518
}
501519
--userDoc.role;
502-
userDoc.save(function (aErr, aUser) { aCallback(aScript); });
520+
userDoc.save(function (aErr, aUser) {
521+
aCallback(aScript);
522+
});
503523
} else {
504524
aCallback(aScript);
505525
}
@@ -512,6 +532,9 @@ exports.deleteScript = function (aInstallName, aCallback) {
512532
Script.findOne({ installName: caseSensitive(aInstallName) },
513533
function (aErr, aScript) {
514534
var s3 = new AWS.S3();
535+
536+
// WARNING: No error handling at this stage
537+
515538
s3.deleteObject({ Bucket : bucketName, Key : aScript.installName},
516539
function (aErr) {
517540
if (!aErr) {
@@ -526,7 +549,6 @@ exports.deleteScript = function (aInstallName, aCallback) {
526549
// GitHub calls this on a push if a webhook is setup
527550
// This controller makes sure we have the latest version of a script
528551
exports.webhook = function (aReq, aRes) {
529-
var RepoManager = require('../libs/repoManager');
530552
var payload = null;
531553
var username = null;
532554
var reponame = null;
@@ -538,7 +560,7 @@ exports.webhook = function (aReq, aRes) {
538560
// Test for know GH webhook ips: https://api.github.com/meta
539561
if (!aReq.body.payload ||
540562
!/192\.30\.25[2-5]\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/
541-
.test(aReq.connection.remoteAddress)) {
563+
.test(aReq.connection.remoteAddress)) {
542564
return;
543565
}
544566

@@ -557,6 +579,10 @@ exports.webhook = function (aReq, aRes) {
557579

558580
// Find the user that corresponds the repo owner
559581
User.findOne({ ghUsername: username }, function (aErr, aUser) {
582+
var repoManager = null;
583+
584+
// WARNING: Partial error handling at this stage
585+
560586
if (!aUser) {
561587
return;
562588
}
@@ -571,7 +597,8 @@ exports.webhook = function (aReq, aRes) {
571597
});
572598

573599
// Update modified scripts
574-
var repoManager = RepoManager.getManager(null, aUser, repos);
575-
repoManager.loadScripts(function () { }, true);
600+
repoManager = RepoManager.getManager(null, aUser, repos);
601+
repoManager.loadScripts(function () {
602+
}, true);
576603
});
577604
};

0 commit comments

Comments
 (0)