@directctrl/fixturelibrary
Version:
Utility library making it easy to work with the open-fixture-library.
104 lines • 4.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFixtureGithubBlob = exports.fetchOflFixtureDirectory = exports.TruncatedDataError = exports.githubRawFixtureRequest = exports.fetchLatestSupportedCommit = exports.request = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const package_json_1 = require("../package.json");
async function request(url) {
return (0, node_fetch_1.default)(url).then(async (data) => {
if (data.ok) {
const jdata = await data.json();
if (jdata && typeof jdata === 'object') {
return jdata;
}
return undefined;
}
console.error(`${data.status} - ${data.statusText}`);
return undefined;
}).catch((err) => console.error(err));
}
exports.request = request;
async function githubRepositoryRequest(endpoint) {
const url = `https://api.github.com/repos/OpenLightingProject/open-fixture-library/${endpoint}`;
return request(url);
}
function extractTagVersion(tag) {
return tag.name.split('-')[1];
}
let latestCommit;
async function fetchLatestSupportedCommit(forceUpdate = false) {
if (latestCommit && !forceUpdate)
return latestCommit;
const tagReq = await githubRepositoryRequest('tags');
if (!tagReq)
return undefined;
const version = extractTagVersion(tagReq[0]);
if (version === package_json_1.openFixtureLibraryVersion) {
// If its equal to the latest supported version, we just fetch the latest commit
const latestCommitReq = await githubRepositoryRequest('commits/master');
latestCommit = latestCommitReq.sha;
}
else {
// Otherwise we just return the sha of the supported version tag.
// We cannot use a more recent one since commits can have multiple parents. (Backtracing)
latestCommit = tagReq[0].commit.sha;
}
return latestCommit;
}
exports.fetchLatestSupportedCommit = fetchLatestSupportedCommit;
async function githubRawFixtureRequest(path) {
const url = `https://raw.githubusercontent.com/OpenLightingProject/open-fixture-library/${await fetchLatestSupportedCommit()}/fixtures/${path}`;
return request(url);
}
exports.githubRawFixtureRequest = githubRawFixtureRequest;
class TruncatedDataError extends Error {
constructor(message) {
super(message);
this.name = 'TruncatedDataError';
}
}
exports.TruncatedDataError = TruncatedDataError;
/**
* @internal
* Fetching and filtering a list of all fixtures of ofl.
* @returns List of all fixtures in the Open Fixture Library
* @throws TruncatedDataError if the list is to long
*/
async function fetchOflFixtureDirectory() {
// At first we get the tree sha for the fixtures directory in the latest supported commit
const latComm = await fetchLatestSupportedCommit();
if (!latComm)
return undefined;
const dirReq = await githubRepositoryRequest(`git/trees/${latComm}`);
// the Fixture Directory
const fixturesDirSha = dirReq.tree.filter((e) => (e.path.startsWith('fixtures') && e.type === 'tree'))[0].sha;
// Now we can continue with the sub-tree
const fixtReq = await githubRepositoryRequest(`git/trees/${fixturesDirSha}?recursive=1`);
// Before we continue we need to check if the data got truncated
if (fixtReq.truncated)
throw new TruncatedDataError('The Open Fixture Library got to big. Please resort to specific File downloading!');
const fixtures = [];
fixtReq.tree.forEach((e) => {
// Filtering out directories or commits from the tree
if (e.type !== 'blob')
return;
fixtures.push({ path: e.path, sha: e.sha, url: e.url });
});
return fixtures;
}
exports.fetchOflFixtureDirectory = fetchOflFixtureDirectory;
/**
* Fetching a Github blob of a Fixture definition
* @param sha sha of the blob
* @returns sha and parsed fixture definition
*/
async function getFixtureGithubBlob(sha) {
const resp = await githubRepositoryRequest(`git/blobs/${sha}`);
const buff = Buffer.from(resp.content, 'base64');
const parsed = JSON.parse(buff.toString());
return { sha: resp.sha, content: parsed };
}
exports.getFixtureGithubBlob = getFixtureGithubBlob;
//# sourceMappingURL=webhandler.js.map