Skip to content

Commit

Permalink
resolve circular includes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhiggs committed Sep 11, 2024
2 parents b6b4753 + d335f5f commit f7a0d9f
Show file tree
Hide file tree
Showing 67 changed files with 1,327 additions and 722 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Publish Docker image

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
submodules: true

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build Docker image
run: |
repo_name=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
docker build -t ghcr.io/${repo_name}/dvb-i-tools:latest .
- name: Push Docker image to GitHub Container Registry
run: |
repo_name=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
docker push ghcr.io/${repo_name}/dvb-i-tools:latest
8 changes: 7 additions & 1 deletion DVB-I_definitions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* DVB-I_defintions.js
*
* Defintions made in DVB A177 Bluebooks
*/
import { tva, tvaEA, tvaEC, TVA_CSmetadata } from "./TVA_definitions.js";

const DVB_metadata = "urn:dvb:metadata";
const DVB_CSmetadata = `${DVB_metadata}:cs`,
FVC_CSmetadata = "urn:fvc:metadata:cs";

import { tva, tvaEA, tvaEC, TVA_CSmetadata } from "./TVA_definitions.js";

const PaginationPrefix = `${FVC_CSmetadata}:HowRelatedCS:2015-12:pagination`,
NowNextCRIDPrefix = "crid://dvb.org/metadata/schedules/now-next";
Expand Down
7 changes: 3 additions & 4 deletions DVB_definitions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/**
* Some definitions from other (non DVB-I) specifications
*
* DVB_definitions.js
*
* Some definitions from other (non DVB-I) specifications
*
* references
* (DVB-SI) ETSI EN 300 468 - https://www.etsi.org/deliver/etsi_en/300400_300499/300468/
* (DVB-S) ETSI EN 300 421 - https://www.etsi.org/deliver/etsi_en/300400_300499/300421/
* (DVB-S2) ETSI EN 303 307-1 - https://www.etsi.org/deliver/etsi_en/302300_302399/30230701/
* (DVB-S2x) ETSI EN 303 307-2 - https://www.etsi.org/deliver/etsi_en/302300_302399/30230702/
*/


export const dvb = {
a_termID: "termID",
a_uri: "uri",
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16
FROM node:20
COPY ./ /usr/src/app
WORKDIR /usr/src/app
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./selfsigned.key -out selfsigned.crt -subj "/C=US/ST=New Sweden/L=Stockholm/O=.../OU=.../CN=.../emailAddress=..."
Expand Down
102 changes: 64 additions & 38 deletions IANAlanguages.js → IANA_languages.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/**
* IANA_languages.js
*
* Load and check language identifiers
*/
import { readFile, readFileSync } from "fs";

import chalk from "chalk";
import { handleErrors } from "./fetch-err-handler.js";

import { readFile } from "fs";
import { datatypeIs } from "./phlib/phlib.js";

import { handleErrors } from "./fetch_err_handler.js";
import { isIn, isIni } from "./utils.js";
import { isHTTPURL } from "./pattern_checks.js";
import { datatypeIs } from "./phlib/phlib.js";
import fetchS from "sync-fetch";

export default class IANAlanguages {
#languagesList;
Expand All @@ -29,6 +36,7 @@ export default class IANAlanguages {
this.#redundantLanguagesList = [];
this.#languageRanges = [];
this.#signLanguagesList = [];
this.#languageFileDate = null;
}

count() {
Expand All @@ -38,9 +46,12 @@ export default class IANAlanguages {
stats(res) {
res.numLanguages = this.#languagesList.length;
res.numRedundantLanguages = this.#redundantLanguagesList.length;
let t = [];
this.#redundantLanguagesList.forEach((e) => t.push(`${e.tag}${e.preferred ? `~${e.preferred}` : ""}`));
res.RedundantLanguages = t.join(", ");
res.numLanguageRanges = this.#languageRanges.length;
res.numSignLanguages = this.#signLanguagesList.length;
if (this.#languageFileDate) res.#languageFileDate = this.#languageFileDate;
if (this.#languageFileDate) res.languageFileDate = this.#languageFileDate;
}

/**
Expand All @@ -58,15 +69,13 @@ export default class IANAlanguages {
* @return {boolean} true if the language subtag is a sign language
*/
function isSignLanguage(items) {
let isSign = false;
for (let i = 0; i < items.length; i++) if (items[i].startsWith("Description") && items[i].toLowerCase().includes("sign")) isSign = true;

return isSign;
for (let i = 0; i < items.length; i++) if (items[i].startsWith("Description") && items[i].toLowerCase().includes("sign")) return true;
return false;
}

let entries = languageData.split("%%");
const entries = languageData.split("%%");
entries.forEach((entry) => {
let items = entry.replace(/(\r|\t)/gm, "").split("\n");
const items = entry.replace(/(\r|\t)/gm, "").split("\n");

if (items[0].startsWith("File-Date")) {
let tl = items[0].split(":");
Expand Down Expand Up @@ -119,19 +128,24 @@ export default class IANAlanguages {
* @param {String} languagesFile the file name to load
* @param {boolean} purge erase the existing values before loading new
*/
#loadLanguagesFromFile(languagesFile, purge = false) {
#loadLanguagesFromFile(languagesFile, purge = false, async = true) {
console.log(chalk.yellow(`reading languages from ${languagesFile}`));
if (purge) this.empty();

readFile(
languagesFile,
{ encoding: "utf-8" },
function (err, data) {
if (!err) {
this.#processLanguageData(data);
} else console.log(chalk.red(`error loading languages ${err}`));
}.bind(this)
);
if (async) {
readFile(
languagesFile,
{ encoding: "utf-8" },
function (err, data) {
if (!err) {
this.#processLanguageData(data);
} else console.log(chalk.red(`error loading languages ${err}`));
}.bind(this)
);
} else {
let langs = readFileSync(languagesFile, { encoding: "utf-8" }).toString();
this.#processLanguageData(langs);
}
}

/**
Expand All @@ -140,25 +154,39 @@ export default class IANAlanguages {
* @param {String} languagesURL the URL to load
* @param {boolean} purge erase the existing values before loading new
*/
#loadLanguagesFromURL(languagesURL, purge = false) {
#loadLanguagesFromURL(languagesURL, purge = false, async = true) {
let isHTTPurl = isHTTPURL(languagesURL);
console.log(chalk.yellow(`${isHTTPurl ? "" : "--> NOT "}retrieving languages from ${languagesURL} using fetch()`));
if (!isHTTPurl) return;

if (purge) this.empty();
fetch(languagesURL)
.then(handleErrors)
.then((response) => response.text())
.then((responseText) => this.#processLanguageData(responseText))
.catch((error) => console.log(chalk.red(`error (${error}) retrieving ${languagesURL}`)));

if (async)
fetch(languagesURL)
.then(handleErrors)
.then((response) => response.text())
.then((responseText) => this.#processLanguageData(responseText))
.catch((error) => console.log(chalk.red(`error (${error}) retrieving ${languagesURL}`)));
else {
let resp = null;
try {
resp = fetchS(languagesURL);
} catch (error) {
console.log(chalk.red(error.message));
}
if (resp) {
if (resp.ok) this.#processLanguageData(resp.text);
else console.log(chalk.red(`error (${resp.error}) retrieving ${languagesURL}`));
}
}
}

loadLanguages(options) {
loadLanguages(options, async = true) {
if (!options) options = {};
if (!Object.prototype.hasOwnProperty.call(options, "purge")) options.purge = false;

if (options.file) this.#loadLanguagesFromFile(options.file, options.purge);
else if (options.url) this.#loadLanguagesFromURL(options.url, options.purge);
if (options.file) this.#loadLanguagesFromFile(options.file, options.purge, async);
else if (options.url) this.#loadLanguagesFromURL(options.url, options.purge, async);
}

/**
Expand All @@ -173,6 +201,13 @@ export default class IANAlanguages {
if (datatypeIs(value, "string")) {
if (this.#languageRanges.find((range) => range.start <= value && value <= range.end)) return { resp: this.languageKnown };

let found = this.#redundantLanguagesList.find((e) => e.tag.toLowerCase() == value.toLowerCase());
if (found) {
let res = { resp: this.languageRedundant };
if (found?.preferred) res.pref = found.preferred;
return res;
}

if (value.indexOf("-") != -1) {
let matches = true;
let parts = value.split("-");
Expand All @@ -184,15 +219,6 @@ export default class IANAlanguages {

if (isIni(this.#languagesList, value)) return { resp: this.languageKnown };

let lc = value.toLowerCase();
let found = this.#redundantLanguagesList.find((e) => e.tag.toLowerCase() == lc);
if (found) {
let res = { resp: this.languageRedundant };
if (found?.preferred) res.pref = found.preferred;
[];
return res;
}

return { resp: this.languageUnknown };
}
return { resp: this.languageInvalidType };
Expand Down
71 changes: 48 additions & 23 deletions ISOcountries.js → ISO_countries.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import chalk from "chalk";
import { handleErrors } from "./fetch-err-handler.js";
/**
* ISO_countries.js
*
* Load and check country codes
*/
import { readFile, readFileSync } from "fs";

import { readFile } from "fs";
import chalk from "chalk";
import fetchS from "sync-fetch";

import { handleErrors } from "./fetch_err_handler.js";
import { isHTTPURL } from "./pattern_checks.js";

/**
Expand Down Expand Up @@ -48,17 +54,22 @@ export default class ISOcountries {
* @param {String} countriesFile the file name to load
* @param {boolean} purge erase the existing values before loading new
*/
#loadCountriesFromFile(countriesFile, purge = false) {
#loadCountriesFromFile(countriesFile, purge = false, async = true) {
console.log(chalk.yellow(`reading countries from ${countriesFile}`));
if (purge) this.reset();
readFile(
countriesFile,
{ encoding: "utf-8" },
function (err, data) {
if (!err) this.#countriesList = loadCountryData(data);
else console.log(chalk.red(err.error));
}.bind(this)
);
if (async)
readFile(
countriesFile,
{ encoding: "utf-8" },
function (err, data) {
if (!err) this.#countriesList = loadCountryData(data);
else console.log(chalk.red(err.error));
}.bind(this)
);
else {
let langs = readFileSync(countriesFile, { encoding: "utf-8" } ).toString();
this.#countriesList = loadCountryData(langs);
}
}

/**
Expand All @@ -67,25 +78,39 @@ export default class ISOcountries {
* @param {String} countriesURL the URL to the file to load
* @param {boolean} purge erase the existing values before loading new
*/
#loadCountriesFromURL(countriesURL, purge = false) {
#loadCountriesFromURL(countriesURL, purge = false, async = true) {
let isHTTPurl = isHTTPURL(countriesURL);
console.log(chalk.yellow(`${isHTTPurl ? "" : "--> NOT "}retrieving countries from ${countriesURL} using fetch()`));
if (!isHTTPurl) return;

if (purge) this.reset();
fetch(countriesURL)
.then(handleErrors)
.then((response) => response.text())
.then((responseText) => (this.#countriesList = loadCountryData(responseText)))
.catch((error) => console.log(chalk.red(`error (${error}) retrieving ${countriesURL}`)));
if (async)
fetch(countriesURL)
.then(handleErrors)
.then((response) => response.text())
.then((responseText) => (this.#countriesList = loadCountryData(responseText)))
.catch((error) => console.log(chalk.red(`error (${error}) retrieving ${countriesURL}`)));
else {
let resp = null;
try {
resp = fetchS(countriesURL);
} catch (error) {
console.log(chalk.red(error.message));
}
if (resp) {
if (resp.ok)
this.#countriesList = loadCountryData(response.text)
else console.log(chalk.red(`error (${error}) retrieving ${languagesURL}`));
}
}
}

loadCountries(options) {
loadCountries(options, async=true) {
if (!options) options = {};
if (!options.purge) options.purge = true;

if (options.file) this.#loadCountriesFromFile(options.file, options.purge);
else if (options.url) this.#loadCountriesFromURL(options.url, options.purge);
if (options.file) this.#loadCountriesFromFile(options.file, options.purge, async);
else if (options.url) this.#loadCountriesFromURL(options.url, options.purge, async);
}

reset() {
Expand All @@ -104,8 +129,8 @@ export default class ISOcountries {
* @return {boolean} true if countryCode is known else false
*/
isISO3166code(countryCode, caseSensitive = true) {
let found = false,
countryCode_lc = countryCode.toLowerCase();
let found = false;
const countryCode_lc = countryCode.toLowerCase();

if (this.#use3CharCountries && countryCode.length == 3) {
if (caseSensitive ? this.#countriesList.find((elem) => elem.alpha3 == countryCode) : this.#countriesList.find((elem) => elem.alpha3.toLowerCase() == countryCode_lc))
Expand Down
10 changes: 8 additions & 2 deletions MIME_checks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* MIME_checks.js
*
* useful routines to check MIME types that could be used in DVB-I documents
*/

const JPEG_MIME = "image/jpeg",
PNG_MIME = "image/png",
WebP_MIME = "image/WebP"; // BUG2937 - https://bugzilla.dvb.org/show_bug.cgi?id=2937
Expand All @@ -11,15 +17,15 @@ const allowedImageTypes = [JPEG_MIME, PNG_MIME, WebP_MIME];
* @param {String} val the MIME type
* @return {boolean} true if the MIME type represents a JPEG image, otherwise false
*/
export var isJPEGmime = (val) => (val ? val == JPEG_MIME : false);
export let isJPEGmime = (val) => (val ? val == JPEG_MIME : false);

/**
* determines if the value is a valid PNG MIME type
*
* @param {String} val the MIME type
* @return {boolean} true if the MIME type represents a PNG image, otherwise false
*/
export var isPNGmime = (val) => (val ? val == PNG_MIME : false);
export let isPNGmime = (val) => (val ? val == PNG_MIME : false);

/** BUG2937 - https://bugzilla.dvb.org/show_bug.cgi?id=2937
* determines if the value is a valid WebP MIME type
Expand Down
Loading

0 comments on commit f7a0d9f

Please sign in to comment.