-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2511 from calebccff/postmarketos
plugins: initial postmarketOS support
- Loading branch information
Showing
7 changed files
with
483 additions
and
1 deletion.
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
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
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
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,107 @@ | ||
"use strict"; | ||
|
||
/* | ||
* Copyright (C) 2020-2021 UBports Foundation <[email protected]> | ||
* Copyright (c) 2022 Caleb Connolly <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
const axios = require("axios"); | ||
|
||
/** @module postmarketOS */ | ||
|
||
const baseURL = "https://images.postmarketos.org"; | ||
|
||
const api = axios.create({ baseURL, timeout: 15000 }); | ||
|
||
/** | ||
* get interfaces from api | ||
* @param {String} device device codename | ||
* @returns {Promise<Array<String>>} interfaces | ||
* @throws {Error} message "unsupported" if 404 not found | ||
*/ | ||
const getInterfaces = device => | ||
api | ||
.get("/bpo/index.json") | ||
.then(({ data }) => { | ||
const devices = data.releases.find(c => c.name === "edge").devices; | ||
const interfaces = devices | ||
.find(d => d.name.includes(device)) | ||
.interfaces.map(i => i.name); | ||
return interfaces.map(i => { | ||
if (i === "phosh") return { value: i, label: "Phosh" }; | ||
if (i === "plasma-mobile") return { value: i, label: "Plasma Mobile" }; | ||
if (i === "sxmo-de-sway") return { value: i, label: "SXMO Sway" }; | ||
return { value: i, label: i }; | ||
}); | ||
}) | ||
.catch(error => { | ||
if (error?.response?.status === 404) throw new Error("404"); | ||
throw error; | ||
}); | ||
|
||
/** | ||
* get images from api | ||
* @param {String} release release | ||
* @param {String} ui user interface | ||
* @param {String} device device codename | ||
* @returns {Promise<Array<Object>>} images array | ||
* @throws {Error} message "no network" if request failed | ||
*/ | ||
const getImages = (release, ui, device) => | ||
api | ||
.get("/bpo/index.json") | ||
.then(({ data }) => { | ||
const rel = data.releases.find(c => c.name === release); | ||
const dev = rel.devices.find(d => d.name.includes(device)); | ||
const images = dev.interfaces.find(i => i.name === ui).images; | ||
// The first two are the latest rootfs and boot image | ||
const ts_latest = images[0].timestamp; | ||
return images | ||
.filter(i => i.timestamp === ts_latest) | ||
.map(i => ({ | ||
url: i.url, | ||
checksum: { | ||
sum: i.sha256, | ||
algorithm: "sha256" | ||
} | ||
})); | ||
}) | ||
.catch(error => { | ||
if (error?.response?.status === 404) throw new Error("404"); | ||
throw error; | ||
}); | ||
|
||
/** | ||
* get releases from api | ||
* @param {String} device device codename | ||
* @returns {Promise<Array<String>>} releases | ||
* @throws {Error} message "unsupported" if 404 not found | ||
*/ | ||
const getReleases = device => | ||
api | ||
.get("/bpo/index.json") | ||
.then(({ data }) => { | ||
const releases = data.releases; | ||
return releases | ||
.filter(release => release.devices.find(d => d.name.includes(device))) | ||
.map(release => release.name); | ||
}) | ||
.catch(error => { | ||
if (error?.response?.status === 404) throw new Error("404"); | ||
throw error; | ||
}); | ||
|
||
module.exports = { getInterfaces, getImages, getReleases }; |
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,132 @@ | ||
const axios = require("axios"); | ||
jest.mock("axios"); | ||
axios.create.mockReturnValue(axios); | ||
const api = require("./api.js"); | ||
|
||
const MOCK_DATA = { | ||
releases: [ | ||
{ | ||
name: "edge", | ||
devices: [ | ||
{ | ||
name: "somedevice", | ||
interfaces: [ | ||
{ name: "phosh" }, | ||
{ | ||
name: "plasma-mobile", | ||
images: [ | ||
{ | ||
timestamp: 0, | ||
url: "someurl", | ||
sha256: "sha256-first" | ||
}, | ||
{ | ||
timestamp: 0, | ||
url: "someurl2", | ||
sha256: "sha256-other" | ||
} | ||
] | ||
}, | ||
{ name: "sxmo-de-sway" }, | ||
{ name: "other" } | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}; | ||
|
||
describe("postmarketos api", () => { | ||
beforeEach(() => { | ||
axios.get.mockResolvedValueOnce({ | ||
data: MOCK_DATA | ||
}); | ||
}); | ||
|
||
describe("getInterfaces()", () => { | ||
it("should resolve interfaces", async () => { | ||
const result = await api.getInterfaces("somedevice"); | ||
expect(result).toContainEqual({ | ||
value: "phosh", | ||
label: "Phosh" | ||
}); | ||
expect(result).toContainEqual({ | ||
value: "plasma-mobile", | ||
label: "Plasma Mobile" | ||
}); | ||
expect(result).toContainEqual({ | ||
value: "sxmo-de-sway", | ||
label: "SXMO Sway" | ||
}); | ||
expect(result).toContainEqual({ | ||
value: "other", | ||
label: "other" | ||
}); | ||
}); | ||
|
||
it("should reject on errors", async () => { | ||
axios.get.mockReset(); | ||
axios.get.mockRejectedValueOnce({ | ||
response: { | ||
status: 404 | ||
} | ||
}); | ||
|
||
const test = () => api.getInterfaces("nonexistent"); | ||
await expect(test).rejects.toThrow("404"); | ||
|
||
axios.get.mockRejectedValueOnce(new Error("other")); | ||
await expect(test).rejects.toThrow("other"); | ||
}); | ||
}); | ||
|
||
describe("getImages()", () => { | ||
it("should resolve images", async () => { | ||
const result = await api.getImages("edge", "plasma-mobile", "somedevice"); | ||
expect(result).toContainEqual({ | ||
url: "someurl", | ||
checksum: { | ||
sum: "sha256-first", | ||
algorithm: "sha256" | ||
} | ||
}); | ||
}); | ||
|
||
it("should throw on 404", async () => { | ||
axios.get.mockReset(); | ||
axios.get.mockRejectedValueOnce({ | ||
response: { | ||
status: 404 | ||
} | ||
}); | ||
|
||
const test = () => api.getImages("non", "existent", "stuff"); | ||
await expect(test).rejects.toThrow("404"); | ||
|
||
axios.get.mockRejectedValueOnce(new Error("other")); | ||
await expect(test).rejects.toThrow("other"); | ||
}); | ||
}); | ||
|
||
describe("getReleases()", () => { | ||
it("should resolve releases", async () => { | ||
const result = await api.getReleases("somedevice"); | ||
expect(result).toEqual(["edge"]); | ||
}); | ||
|
||
it("should throw on 404", async () => { | ||
axios.get.mockReset(); | ||
axios.get.mockRejectedValueOnce({ | ||
response: { | ||
status: 404 | ||
} | ||
}); | ||
|
||
const test = () => api.getReleases("nonexistent"); | ||
await expect(test).rejects.toThrow("404"); | ||
|
||
axios.get.mockRejectedValueOnce(new Error("other")); | ||
await expect(test).rejects.toThrow("other"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.