From c771087b8adb14981464016b78857d86c349c3d7 Mon Sep 17 00:00:00 2001 From: Ingo Richter Date: Fri, 8 Aug 2014 17:55:33 -0700 Subject: [PATCH 1/2] - URI encode parts of the extension download URL. --- lib/registry_utils.js | 48 ++++++++++++++++++++++++------------- lib/routes.js | 2 +- spec/registry_utils.spec.js | 47 ++++++++++++++++++++++++++++++++++++ views/registryList.html | 2 +- 4 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 spec/registry_utils.spec.js diff --git a/lib/registry_utils.js b/lib/registry_utils.js index ef6704c..974f2e5 100644 --- a/lib/registry_utils.js +++ b/lib/registry_utils.js @@ -1,24 +1,24 @@ /* * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. - * + * * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. - * + * */ /* @@ -116,6 +116,22 @@ exports.authorInfo = function () { return result; }; +/** + * URL encodes the extenion name and the version. + * + * @param {string} baseURL The registry base url + * @param {string} name The name of the extension + * @param {string} version The version of the extension + * + * @return {string} An URI to download the extension + */ +exports.formatDownloadURL = function(baseURL, name, version) { + var urlEncodedName = encodeURIComponent(name), + urlEncodedNameAndVersion = encodeURIComponent(name + "-" + version + ".zip"); + + return baseURL + "/" + urlEncodedName + "/" + urlEncodedNameAndVersion; +} + /** * Returns an array of current registry entries, sorted by the publish date of the latest version of each entry. * @param {object} registry The unsorted registry. @@ -128,10 +144,10 @@ exports.sortRegistry = function (registry, subkey) { if (entry.versions) { return new Date(entry.versions[entry.versions.length - 1].published).getTime(); } - + return Number.NEGATIVE_INFINITY; } - + var sortedEntries = []; // Sort the registry by last published date (newest first). @@ -142,6 +158,6 @@ exports.sortRegistry = function (registry, subkey) { return getPublishTime((subkey && entry2[subkey]) || entry2) - getPublishTime((subkey && entry1[subkey]) || entry1); }); - + return sortedEntries; -}; \ No newline at end of file +}; diff --git a/lib/routes.js b/lib/routes.js index f2f157f..ab6f7ef 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -144,7 +144,7 @@ function _toErrorMessageList(err) { hbs.registerPartial("registryList", fs.readFileSync(path.resolve(__dirname, "../views/registryList.html"), "utf8")); -["lastVersionDate", "formatUserId", "ownerLink", "authorInfo"].forEach(function (helper) { +["lastVersionDate", "formatUserId", "ownerLink", "authorInfo", "formatDownloadURL"].forEach(function (helper) { hbs.registerHelper(helper, registry_utils[helper]); }); diff --git a/spec/registry_utils.spec.js b/spec/registry_utils.spec.js new file mode 100644 index 0000000..9920488 --- /dev/null +++ b/spec/registry_utils.spec.js @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +/*jslint vars: true, plusplus: true, nomen: true, node: true, indent: 4, maxerr: 50 */ +/*global expect, describe, it, beforeEach, afterEach, createSpy, waitsFor, spyOn */ + +"use strict"; + +var registryUtils = require("../lib/registry_utils"); + +describe("Registry Utils", function () { + describe("Format Download URL", function () { + it("should return the formatted url", function () { + var formattedURL = registryUtils.formatDownloadURL("http://localhost:1234", "test-extension", "0.0.1"); + + expect("http://localhost:1234/test-extension/test-extension-0.0.1.zip").toBe(formattedURL); + }); + + it("should return the formatted url with proper url encoding", function () { + var formattedURL = registryUtils.formatDownloadURL("http://localhost:1234", "jasonsanjose.brackets-sass", "0.4.1+sha.fc425b5"); + expect("http://localhost:1234/jasonsanjose.brackets-sass/jasonsanjose.brackets-sass-0.4.1%2Bsha.fc425b5.zip").toBe(formattedURL); + + formattedURL = registryUtils.formatDownloadURL("http://localhost:1234", "test-extension", "0.0.1&<>abcdef"); + expect("http://localhost:1234/test-extension/test-extension-0.0.1%26%3C%3Eabcdef.zip").toBe(formattedURL); + }); + }); +}); diff --git a/views/registryList.html b/views/registryList.html index dca8649..78bdf6a 100644 --- a/views/registryList.html +++ b/views/registryList.html @@ -10,7 +10,7 @@ - + {{#if metadata.title}}{{metadata.title}}{{else}}{{metadata.name}}{{/if}} {{metadata.version}}
by {{{authorInfo}}} From 82a67d751aca24bb4e917e6e2c2c40a3d807e519 Mon Sep 17 00:00:00 2001 From: Ingo Richter Date: Mon, 11 Aug 2014 14:40:11 -0700 Subject: [PATCH 2/2] - some minor tweaks based on review feedback --- lib/registry_utils.js | 2 +- spec/registry_utils.spec.js | 6 +++--- views/registryList.html | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/registry_utils.js b/lib/registry_utils.js index 974f2e5..15a5e1a 100644 --- a/lib/registry_utils.js +++ b/lib/registry_utils.js @@ -117,7 +117,7 @@ exports.authorInfo = function () { }; /** - * URL encodes the extenion name and the version. + * URL encodes the extension name and the version. * * @param {string} baseURL The registry base url * @param {string} name The name of the extension diff --git a/spec/registry_utils.spec.js b/spec/registry_utils.spec.js index 9920488..e112826 100644 --- a/spec/registry_utils.spec.js +++ b/spec/registry_utils.spec.js @@ -33,15 +33,15 @@ describe("Registry Utils", function () { it("should return the formatted url", function () { var formattedURL = registryUtils.formatDownloadURL("http://localhost:1234", "test-extension", "0.0.1"); - expect("http://localhost:1234/test-extension/test-extension-0.0.1.zip").toBe(formattedURL); + expect(formattedURL).toBe("http://localhost:1234/test-extension/test-extension-0.0.1.zip"); }); it("should return the formatted url with proper url encoding", function () { var formattedURL = registryUtils.formatDownloadURL("http://localhost:1234", "jasonsanjose.brackets-sass", "0.4.1+sha.fc425b5"); - expect("http://localhost:1234/jasonsanjose.brackets-sass/jasonsanjose.brackets-sass-0.4.1%2Bsha.fc425b5.zip").toBe(formattedURL); + expect(formattedURL).toBe("http://localhost:1234/jasonsanjose.brackets-sass/jasonsanjose.brackets-sass-0.4.1%2Bsha.fc425b5.zip"); formattedURL = registryUtils.formatDownloadURL("http://localhost:1234", "test-extension", "0.0.1&<>abcdef"); - expect("http://localhost:1234/test-extension/test-extension-0.0.1%26%3C%3Eabcdef.zip").toBe(formattedURL); + expect(formattedURL).toBe("http://localhost:1234/test-extension/test-extension-0.0.1%26%3C%3Eabcdef.zip"); }); }); }); diff --git a/views/registryList.html b/views/registryList.html index 78bdf6a..03503ce 100644 --- a/views/registryList.html +++ b/views/registryList.html @@ -10,7 +10,7 @@ - + {{#if metadata.title}}{{metadata.title}}{{else}}{{metadata.name}}{{/if}} {{metadata.version}}
by {{{authorInfo}}}