Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit 79dd51e

Browse files
committed
chore(): handle un-common start_url
1 parent 5b9f0d9 commit 79dd51e

File tree

2 files changed

+63
-58
lines changed

2 files changed

+63
-58
lines changed

lib/manifestTools/manifestValidator.js

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict';
22

33
var path = require('path'),
4-
fs = require('fs'),
5-
Q = require('q'),
6-
url = require('url');
4+
fs = require('fs'),
5+
Q = require('q'),
6+
url = require('url');
77

88
var constants = require('../constants'),
9-
log = require('../log'),
10-
platformTools = require('../platformTools'),
11-
utils = require('../utils');
9+
log = require('../log'),
10+
platformTools = require('../platformTools'),
11+
utils = require('../utils');
1212

1313
var toStringFunction = Object.prototype.toString;
1414

1515
// load the rule(s) from a folder or file
1616
function loadValidationRules(fileOrDir, platforms, callback) {
1717

18-
var stat = Q.nfbind(fs.stat);
18+
var stat = Q.nfbind(fs.stat);
1919

2020
// list contents of the validation rules folder
2121
return Q.nfcall(fs.readdir, fileOrDir).then(function (files) {
@@ -26,13 +26,13 @@ function loadValidationRules(fileOrDir, platforms, callback) {
2626
if (info.isDirectory()) {
2727
// ignore any directory that doesn't match one of the requested platforms
2828
if (platforms.indexOf(file) < 0) {
29-
return Q.resolve();
29+
return Q.resolve();
3030
}
31-
31+
3232
// process the rules in the platform folder
3333
return loadValidationRules(filePath, []);
3434
}
35-
35+
3636
// load the rules defined in the file
3737
var rulePath = path.join(fileOrDir, file);
3838
try {
@@ -44,40 +44,40 @@ function loadValidationRules(fileOrDir, platforms, callback) {
4444
}
4545
});
4646
}))
47-
.then (function (results) {
48-
// verify the results and consolidate the loaded rules
49-
return results.reduce(function (validationRules, result) {
50-
if (result.state === 'fulfilled') {
51-
if (result.value) {
52-
if (Array.isArray(result.value)) {
53-
validationRules.push.apply(validationRules, result.value);
47+
.then(function (results) {
48+
// verify the results and consolidate the loaded rules
49+
return results.reduce(function (validationRules, result) {
50+
if (result.state === 'fulfilled') {
51+
if (result.value) {
52+
if (Array.isArray(result.value)) {
53+
validationRules.push.apply(validationRules, result.value);
54+
}
55+
else {
56+
validationRules.push(result.value);
57+
}
5458
}
55-
else {
56-
validationRules.push(result.value);
57-
}
58-
}
59-
}
60-
else {
61-
log.error(result.reason.getMessage());
62-
}
63-
64-
return validationRules;
65-
}, []);
66-
});
67-
})
68-
.catch (function (err) {
69-
if (err.code !== 'ENOTDIR') {
70-
throw err;
71-
}
72-
73-
// fileOrDir is a file
74-
var rules = require(fileOrDir);
75-
return Array.isArray(rules) ? rules : [rules];
76-
})
77-
.catch(function (err) {
78-
return Q.reject(new Error('Failed to read validation rules from the specified location: \'' + fileOrDir + '\'. ' + err.message + '.'));
59+
}
60+
else {
61+
log.error(result.reason.getMessage());
62+
}
63+
64+
return validationRules;
65+
}, []);
66+
});
7967
})
80-
.nodeify(callback);
68+
.catch(function (err) {
69+
if (err.code !== 'ENOTDIR') {
70+
throw err;
71+
}
72+
73+
// fileOrDir is a file
74+
var rules = require(fileOrDir);
75+
return Array.isArray(rules) ? rules : [rules];
76+
})
77+
.catch(function (err) {
78+
return Q.reject(new Error('Failed to read validation rules from the specified location: \'' + fileOrDir + '\'. ' + err.message + '.'));
79+
})
80+
.nodeify(callback);
8181
}
8282

8383
function runValidationRules(w3cManifestInfo, rules, callback) {
@@ -135,7 +135,7 @@ function applyValidationRules(w3cManifestInfo, platformModules, platforms) {
135135

136136
return Q.allSettled(platformTasks);
137137
}
138-
138+
139139
// Only run the "All Platform" validations for W3C manifests
140140
if (w3cManifestInfo.format === constants.BASE_MANIFEST_FORMAT) {
141141
return validateAllPlatforms()
@@ -160,12 +160,12 @@ function validateManifest(w3cManifestInfo, platforms, callback) {
160160
return platformTools.loadPlatforms(platforms).then(function (platformModules) {
161161
return applyValidationRules(w3cManifestInfo, platformModules, platforms);
162162
})
163-
.nodeify(callback);
163+
.nodeify(callback);
164164
}
165165

166166
function imageValidation(manifestContent, description, platform, level, requiredIconSizes, callback) {
167167
var icons = manifestContent.icons;
168-
168+
169169
var result = {
170170
description: description,
171171
platform: platform,
@@ -208,7 +208,7 @@ function imageValidation(manifestContent, description, platform, level, required
208208

209209
function imageGroupValidation(manifestContent, description, platform, validIconSizes, callback) {
210210
var icons = manifestContent.icons;
211-
211+
212212
var result = {
213213
description: description,
214214
platform: platform,
@@ -239,38 +239,43 @@ function validateAndNormalizeStartUrl(siteUrl, manifestInfo, callback) {
239239
if (manifestInfo.format !== constants.BASE_MANIFEST_FORMAT) {
240240
return callback(new Error('The manifest found is not a W3C manifest.'), manifestInfo);
241241
}
242-
242+
243243
if (manifestInfo.content.start_url) {
244244
if (!utils.isURL(manifestInfo.content.start_url)) {
245245
return callback(new Error('The manifest\'s start_url member is not a valid URL: \'' + manifestInfo.content.start_url + '\''), manifestInfo);
246246
}
247247
} else {
248248
manifestInfo.content.start_url = '/';
249249
}
250-
250+
251251
if (siteUrl) {
252252
if (!utils.isURL(siteUrl)) {
253253
return callback(new Error('The site URL is not a valid URL: \'' + siteUrl + '\''), manifestInfo);
254254
}
255-
255+
256256
var parsedSiteUrl = url.parse(siteUrl);
257257
var parsedManifestStartUrl = url.parse(manifestInfo.content.start_url);
258258
if (parsedManifestStartUrl.hostname && parsedSiteUrl.hostname !== parsedManifestStartUrl.hostname) {
259-
// issue #88 - bis
259+
// issue #88 - bis
260260
var subDomainOfManifestStartUrlSplitted = parsedManifestStartUrl.hostname.split('.');
261261
var lengthSubDomain = subDomainOfManifestStartUrlSplitted.length;
262262
var subDomainOfManifestStartUrl = null;
263-
if(lengthSubDomain >= 2){
264-
subDomainOfManifestStartUrl =
265-
subDomainOfManifestStartUrlSplitted[lengthSubDomain - 2] + '.' + subDomainOfManifestStartUrlSplitted[lengthSubDomain - 1];
263+
if (lengthSubDomain >= 2) {
264+
subDomainOfManifestStartUrl =
265+
subDomainOfManifestStartUrlSplitted[lengthSubDomain - 2] + '.' + subDomainOfManifestStartUrlSplitted[lengthSubDomain - 1];
266266
}
267-
if(!subDomainOfManifestStartUrl || !utils.isURL(subDomainOfManifestStartUrl) || parsedSiteUrl.hostname.toLowerCase() !== subDomainOfManifestStartUrl.toLowerCase()){
267+
if (!subDomainOfManifestStartUrl || !utils.isURL(subDomainOfManifestStartUrl) || parsedSiteUrl.hostname.toLowerCase() !== subDomainOfManifestStartUrl.toLowerCase()) {
268268
return callback(new Error('The domain of the hosted site (' + parsedSiteUrl.hostname + ') does not match the domain of the manifest\'s start_url member (' + parsedManifestStartUrl.hostname + ')'), manifestInfo);
269269
}
270270
}
271-
272-
manifestInfo.content.start_url = url.resolve(siteUrl, manifestInfo.content.start_url);
273-
271+
272+
if (parsedManifestStartUrl.search !== null) {
273+
manifestInfo.content.start_url = siteUrl.split('?')[0];
274+
}
275+
else {
276+
manifestInfo.content.start_url = url.resolve(siteUrl, manifestInfo.content.start_url);
277+
}
278+
274279
manifestInfo.default = { short_name: utils.getDefaultShortName(siteUrl) };
275280
}
276281

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)