Skip to content

Commit

Permalink
Reword titleName to title, and remove DirEntry.name() and .getReadabl…
Browse files Browse the repository at this point in the history
…eName().

To help understanding the code.
  • Loading branch information
mossroy committed May 28, 2017
1 parent e521f9d commit 25cda11
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 105 deletions.
36 changes: 18 additions & 18 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,39 +99,39 @@ function fetchEventListener(event) {
console.log('Asking app.js for a content', event.request.url);
event.respondWith(new Promise(function(resolve, reject) {
var nameSpace;
var titleName;
var titleNameWithNameSpace;
var title;
var titleWithNameSpace;
var contentType;
if (regexpContentUrlWithoutNamespace.test(event.request.url)) {
// When the request URL is in the same folder,
// it means it's a link to an article (namespace A)
var regexpResult = regexpContentUrlWithoutNamespace.exec(event.request.url);
nameSpace = 'A';
titleName = regexpResult[1];
title = regexpResult[1];
} else {
var regexpResult = regexpContentUrlWithNamespace.exec(event.request.url);
nameSpace = regexpResult[1];
titleName = regexpResult[2];
title = regexpResult[2];
}

// The namespace defines the type of content. See http://www.openzim.org/wiki/ZIM_file_format#Namespaces
// TODO : read the contentType from the ZIM file instead of hard-coding it here
if (nameSpace === 'A') {
console.log("It's an article : " + titleName);
console.log("It's an article : " + title);
contentType = 'text/html';
}
else if (nameSpace === 'I' || nameSpace === 'J') {
console.log("It's an image : " + titleName);
if (regexpJPEG.test(titleName)) {
console.log("It's an image : " + title);
if (regexpJPEG.test(title)) {
contentType = 'image/jpeg';
}
else if (regexpPNG.test(titleName)) {
else if (regexpPNG.test(title)) {
contentType = 'image/png';
}
}
else if (nameSpace === '-') {
console.log("It's a layout dependency : " + titleName);
if (regexpJS.test(titleName)) {
console.log("It's a layout dependency : " + title);
if (regexpJS.test(title)) {
contentType = 'text/javascript';
var responseInit = {
status: 200,
Expand All @@ -147,21 +147,21 @@ function fetchEventListener(event) {
resolve(httpResponse);
return;
}
else if (regexpCSS.test(titleName)) {
else if (regexpCSS.test(title)) {
contentType = 'text/css';
}
}

// We need to remove the potential parameters in the URL
titleName = removeUrlParameters(decodeURIComponent(titleName));
title = removeUrlParameters(decodeURIComponent(title));

titleNameWithNameSpace = nameSpace + '/' + titleName;
titleWithNameSpace = nameSpace + '/' + title;

// Let's instanciate a new messageChannel, to allow app.s to give us the content
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
if (event.data.action === 'giveContent') {
console.log('content message received for ' + titleNameWithNameSpace, event.data);
console.log('content message received for ' + titleWithNameSpace, event.data);
var responseInit = {
status: 200,
statusText: 'OK',
Expand All @@ -172,16 +172,16 @@ function fetchEventListener(event) {

var httpResponse = new Response(event.data.content, responseInit);

console.log('ServiceWorker responding to the HTTP request for ' + titleNameWithNameSpace + ' (size=' + event.data.content.length + ' octets)' , httpResponse);
console.log('ServiceWorker responding to the HTTP request for ' + titleWithNameSpace + ' (size=' + event.data.content.length + ' octets)' , httpResponse);
resolve(httpResponse);
}
else {
console.log('Invalid message received from app.js for ' + titleNameWithNameSpace, event.data);
console.log('Invalid message received from app.js for ' + titleWithNameSpace, event.data);
reject(event.data);
}
};
console.log('Eventlistener added to listen for an answer to ' + titleNameWithNameSpace);
outgoingMessagePort.postMessage({'action': 'askForContent', 'titleName': titleNameWithNameSpace}, [messageChannel.port2]);
console.log('Eventlistener added to listen for an answer to ' + titleWithNameSpace);
outgoingMessagePort.postMessage({'action': 'askForContent', 'title': titleWithNameSpace}, [messageChannel.port2]);
console.log('Message sent to app.js through outgoingMessagePort');
}));
}
Expand Down
40 changes: 20 additions & 20 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
QUnit.test("article '(The Night Time Is) The Right Time' correctly redirects to 'Night Time Is the Right Time'", function(assert) {
var done = assert.async();
assert.expect(6);
localZimArchive.getDirEntryByTitleName("A/(The_Night_Time_Is)_The_Right_Time.html").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("A/(The_Night_Time_Is)_The_Right_Time.html").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.name(), "(The Night Time Is) The Right Time", "Correct redirect title name.");
assert.equal(dirEntry.title, "(The Night Time Is) The Right Time", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.name(), "Night Time Is the Right Time", "Correct redirected title name.");
assert.equal(dirEntry.title, "Night Time Is the Right Time", "Correct redirected title name.");
done();
});
} else {
Expand All @@ -191,15 +191,15 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
QUnit.test("article 'Raelettes' correctly redirects to 'The Raelettes'", function(assert) {
var done = assert.async();
assert.expect(6);
localZimArchive.getDirEntryByTitleName("A/Raelettes.html").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("A/Raelettes.html").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.name(), "Raelettes", "Correct redirect title name.");
assert.equal(dirEntry.title, "Raelettes", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.name(), "The Raelettes", "Correct redirected title name.");
assert.equal(dirEntry.title, "The Raelettes", "Correct redirected title name.");
done();
});
} else {
Expand All @@ -210,15 +210,15 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
QUnit.test("article 'Bein Green' correctly redirects to 'Bein' Green", function(assert) {
var done = assert.async();
assert.expect(6);
localZimArchive.getDirEntryByTitleName("A/Bein_Green.html").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("A/Bein_Green.html").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.name(), "Bein Green", "Correct redirect title name.");
assert.equal(dirEntry.title, "Bein Green", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.name(), "Bein' Green", "Correct redirected title name.");
assert.equal(dirEntry.title, "Bein' Green", "Correct redirected title name.");
done();
});
} else {
Expand All @@ -229,15 +229,15 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
QUnit.test("article 'America, the Beautiful' correctly redirects to 'America the Beautiful'", function(assert) {
var done = assert.async();
assert.expect(6);
localZimArchive.getDirEntryByTitleName("A/America,_the_Beautiful.html").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("A/America,_the_Beautiful.html").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.ok(dirEntry.isRedirect(), "DirEntry is a redirect.");
assert.equal(dirEntry.name(), "America, the Beautiful", "Correct redirect title name.");
assert.equal(dirEntry.title, "America, the Beautiful", "Correct redirect title name.");
localZimArchive.resolveRedirect(dirEntry, function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
assert.ok(!dirEntry.isRedirect(), "DirEntry is not a redirect.");
assert.equal(dirEntry.name(), "America the Beautiful", "Correct redirected title name.");
assert.equal(dirEntry.title, "America the Beautiful", "Correct redirected title name.");
done();
});
} else {
Expand All @@ -248,7 +248,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
QUnit.test("Image 'm/RayCharles_AManAndHisSoul.jpg' can be loaded", function(assert) {
var done = assert.async();
assert.expect(4);
localZimArchive.getDirEntryByTitleName("I/m/RayCharles_AManAndHisSoul.jpg").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("I/m/RayCharles_AManAndHisSoul.jpg").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.equal(dirEntry.namespace +"/"+ dirEntry.url, "I/m/RayCharles_AManAndHisSoul.jpg", "URL is correct.");
Expand All @@ -268,7 +268,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
var done = assert.async();

assert.expect(4);
localZimArchive.getDirEntryByTitleName("-/s/style.css").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("-/s/style.css").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.equal(dirEntry.namespace +"/"+ dirEntry.url, "-/s/style.css", "URL is correct.");
Expand All @@ -287,7 +287,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
QUnit.test("Javascript '-/j/local.js' can be loaded", function(assert) {
var done = assert.async();
assert.expect(4);
localZimArchive.getDirEntryByTitleName("-/j/local.js").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("-/j/local.js").then(function(dirEntry) {
assert.ok(dirEntry !== null, "DirEntry found");
if (dirEntry !== null) {
assert.equal(dirEntry.namespace +"/"+ dirEntry.url, "-/j/local.js", "URL is correct.");
Expand All @@ -307,12 +307,12 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
QUnit.test("Split article 'A/Ray_Charles.html' can be loaded", function(assert) {
var done = assert.async();
assert.expect(6);
localZimArchive.getDirEntryByTitleName("A/Ray_Charles.html").then(function(dirEntry) {
localZimArchive.getDirEntryByTitle("A/Ray_Charles.html").then(function(dirEntry) {
assert.ok(dirEntry !== null, "Title found");
if (dirEntry !== null) {
assert.equal(dirEntry.namespace +"/"+ dirEntry.url, "A/Ray_Charles.html", "URL is correct.");
localZimArchive.readArticle(dirEntry, function(titleName, data) {
assert.equal(titleName, "Ray Charles", "Title is correct.");
localZimArchive.readArticle(dirEntry, function(title, data) {
assert.equal(title, "Ray Charles", "Title is correct.");
assert.equal(data.length, 157186, "Data length is correct.");
assert.equal(data.indexOf("the only true genius in show business"), 5535, "Specific substring at beginning found.");
assert.equal(data.indexOf("Random Access Memories"), 154107, "Specific substring at end found.");
Expand All @@ -330,7 +330,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
assert.expect(2);
var callbackRandomArticleFound = function(dirEntry) {
assert.ok(dirEntry !== null, "One DirEntry should be found");
assert.ok(dirEntry.name() !== null, "The random DirEntry should have a name" );
assert.ok(dirEntry.title !== null, "The random DirEntry should have a title" );

done();
};
Expand All @@ -341,7 +341,7 @@ define(['jquery', 'zimArchive', 'zimDirEntry', 'util', 'utf8'],
assert.expect(2);
var callbackMainPageArticleFound = function(dirEntry) {
assert.ok(dirEntry !== null, "Main DirEntry should be found");
assert.equal(dirEntry.name(), "Summary", "The main DirEntry should be called Summary" );
assert.equal(dirEntry.title, "Summary", "The main DirEntry should be called Summary" );

done();
};
Expand Down
Loading

0 comments on commit 25cda11

Please sign in to comment.