Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

URI encode parts of the extension download URL #59

Merged
merged 2 commits into from
Aug 12, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions lib/registry_utils.js
Original file line number Diff line number Diff line change
@@ -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.
*
*
*/

/*
Expand Down Expand Up @@ -116,6 +116,22 @@ exports.authorInfo = function () {
return result;
};

/**
* URL encodes the extenion name and the version.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: extenion

*
* @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.
Expand All @@ -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).
Expand All @@ -142,6 +158,6 @@ exports.sortRegistry = function (registry, subkey) {
return getPublishTime((subkey && entry2[subkey]) || entry2) -
getPublishTime((subkey && entry1[subkey]) || entry1);
});

return sortedEntries;
};
};
2 changes: 1 addition & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});

Expand Down
47 changes: 47 additions & 0 deletions spec/registry_utils.spec.js
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is backwards... it should be expect(formattedURL).toBe("http://...") (expect takes the "actual" value)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and No. The output reads nicer when writing it this way. ;-) I'm going to change it to avoid confusion.


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);
});
});
});
2 changes: 1 addition & 1 deletion views/registryList.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<tr>
<td {{#if canAdmin}}class="info"{{/if}}></td>
<td>
<a href="{{../repositoryBaseURL}}/{{metadata.name}}/{{metadata.name}}-{{metadata.version}}.zip">
<a href="{{formatDownloadURL ../repositoryBaseURL metadata.name metadata.version}}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use 3 braces to not escape the already escaped URL again.

{{#if metadata.title}}{{metadata.title}}{{else}}{{metadata.name}}{{/if}}</a>
<span class="muted ext-version">{{metadata.version}}</span><br/>
<span class="muted ext-author">by {{{authorInfo}}}</span>
Expand Down