-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor hostname helper function (#1735)
Related tiny-pilot/tinypilot-pro#1162 Resolves #771 This PR refactors the `futureLocation` function to `app/static/js/hostname.js` and renames it to `determineFutureOrigin`. This change is needed to allow the function to be reused by tiny-pilot/tinypilot-pro#1169 This is mostly a non-functional change with the following caveats: 1. Name change * Before `futureLocation` * After `determineFutureOrigin` 3. Docstring change * Before https://github.com/tiny-pilot/tinypilot/blob/34161c3c450f5bc3c99cffbd3cd0a778aaeed6dd/app/templates/custom-elements/change-hostname-dialog.html#L100-L107 * After https://github.com/tiny-pilot/tinypilot/blob/d889c24044a510a5ba91464ac229dcd3c08bd578/app/static/js/hostname.js#L1-L8 5. Added guard on `oldHostname` variable * Before https://github.com/tiny-pilot/tinypilot/blob/34161c3c450f5bc3c99cffbd3cd0a778aaeed6dd/app/templates/custom-elements/change-hostname-dialog.html#L111 * After https://github.com/tiny-pilot/tinypilot/blob/d889c24044a510a5ba91464ac229dcd3c08bd578/app/static/js/hostname.js#L16 7. Added tests * [app/static/js/hostname.test.js](https://github.com/tiny-pilot/tinypilot/blob/d889c24044a510a5ba91464ac229dcd3c08bd578/app/static/js/hostname.test.js) <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1735"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a>
- Loading branch information
1 parent
34161c3
commit dc9d8cc
Showing
3 changed files
with
137 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Determines the new base URL (i.e., the origin of the URL, without the | ||
* pathname or search parameters) of the TinyPilot device, given a new hostname. | ||
* | ||
* @example | ||
* // returns 'https://new-hostname.local' | ||
* determineFutureOrigin( | ||
* new URL('https://old-hostname.local/some-path/'), | ||
* 'old-hostname', | ||
* 'new-hostname' | ||
* ); | ||
* | ||
* @param {URL} currentLocation | ||
* @param {string} oldHostname | ||
* @param {string} newHostname | ||
* @returns {string} | ||
*/ | ||
export function determineFutureOrigin( | ||
currentLocation, | ||
oldHostname, | ||
newHostname | ||
) { | ||
const protocol = currentLocation.protocol + "//"; | ||
let fqdn = currentLocation.hostname; | ||
if (oldHostname && fqdn.startsWith(oldHostname + ".")) { | ||
// When the fqdn (fully qualified domain name) starts with the old | ||
// hostname followed by a dot, then we replace the old one by the | ||
// new one in order to preserve the domain part. | ||
// E.g.: "oldtinypilot.home.local" => "newtinypilot.home.local" | ||
fqdn = fqdn.replace(oldHostname, newHostname); | ||
} else { | ||
// Otherwise we just assume the new hostname to be a fully qualified | ||
// one. That might not be correct, but it’s the best possible guess. | ||
fqdn = newHostname; | ||
} | ||
const port = currentLocation.port === "" ? "" : `:${currentLocation.port}`; | ||
return protocol + fqdn + port; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { determineFutureOrigin } from "./hostname.js"; | ||
import { describe, it } from "mocha"; | ||
import assert from "assert"; | ||
|
||
describe("determineFutureOrigin", () => { | ||
it("returns origin by replacing old hostname with new hostname", () => { | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("https://old-tinypilot/"), | ||
"old-tinypilot", | ||
"new-tinypilot" | ||
), | ||
"https://new-tinypilot" | ||
); | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("https://old-tinypilot.local/"), | ||
"old-tinypilot", | ||
"new-tinypilot" | ||
), | ||
"https://new-tinypilot.local" | ||
); | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("https://old-tinypilot.domain.local/"), | ||
"old-tinypilot", | ||
"new-tinypilot" | ||
), | ||
"https://new-tinypilot.domain.local" | ||
); | ||
}); | ||
it("returns origin using only new hostname", () => { | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("https://old-tinypilot/"), | ||
undefined, | ||
"new-tinypilot" | ||
), | ||
"https://new-tinypilot" | ||
); | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("https://old-tinypilot.local/"), | ||
undefined, | ||
"new-tinypilot" | ||
), | ||
"https://new-tinypilot" | ||
); | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("https://old-tinypilot.domain.local/"), | ||
undefined, | ||
"new-tinypilot" | ||
), | ||
"https://new-tinypilot" | ||
); | ||
}); | ||
it("maintains port number", () => { | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("https://old-tinypilot:8080/"), | ||
"old-tinypilot", | ||
"new-tinypilot" | ||
), | ||
"https://new-tinypilot:8080" | ||
); | ||
}); | ||
it("maintains protocol", () => { | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("http://old-tinypilot/"), | ||
"old-tinypilot", | ||
"new-tinypilot" | ||
), | ||
"http://new-tinypilot" | ||
); | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("ftp://old-tinypilot/"), | ||
"old-tinypilot", | ||
"new-tinypilot" | ||
), | ||
"ftp://new-tinypilot" | ||
); | ||
}); | ||
it("strips pathname", () => { | ||
assert.strictEqual( | ||
determineFutureOrigin( | ||
new URL("http://old-tinypilot/some-path/"), | ||
"old-tinypilot", | ||
"new-tinypilot" | ||
), | ||
"http://new-tinypilot" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters