|
|
@@ -19,6 +19,14 @@ class languages {
|
|
|
*/
|
|
|
#libs;
|
|
|
|
|
|
+ /**
|
|
|
+ * @var {bool}
|
|
|
+ * This store that directory is in the local file system, or remote
|
|
|
+ * server. When true, resources would be loaded by node:fs. When
|
|
|
+ * false, resources would be fetched.
|
|
|
+ */
|
|
|
+ #local;
|
|
|
+
|
|
|
/**
|
|
|
* This create new languages library. Next, languages could be added to
|
|
|
* the library by command, or by loading index file.
|
|
|
@@ -26,12 +34,19 @@ class languages {
|
|
|
* @throws {TypeError} - When parameters is not in correct format.
|
|
|
*
|
|
|
* @param {string} path - Path to phrasebooks on the server or filesystem.
|
|
|
+ * @param {bool} local - True when phrasebooks dirs would be loaded by
|
|
|
+ * node:fs module. False when would be fetch.
|
|
|
*/
|
|
|
- constructor(path) {
|
|
|
+ constructor(path, local = false) {
|
|
|
if (typeof(path) !== "string") {
|
|
|
throw new TypeError("Path to the phrasebooks must be string.");
|
|
|
}
|
|
|
|
|
|
+ if (typeof(local) !== "boolean") {
|
|
|
+ throw new TypeError("Local must be bool variable.");
|
|
|
+ }
|
|
|
+
|
|
|
+ this.#local = local;
|
|
|
this.#path = path;
|
|
|
this.#libs = new Map();
|
|
|
}
|
|
|
@@ -97,11 +112,9 @@ class languages {
|
|
|
throw new TypeError("Name of index file is not string.");
|
|
|
}
|
|
|
|
|
|
- const request = await fetch(this.#full_path(index));
|
|
|
- const response = await request.json();
|
|
|
-
|
|
|
- const result = new languages(this.#path);
|
|
|
-
|
|
|
+ const response = await this.#load_index(index);
|
|
|
+ const result = new languages(this.#path, this.#local);
|
|
|
+
|
|
|
Object.keys(response).forEach(name => {
|
|
|
if (typeof(name) !== "string") {
|
|
|
console.error("Name of the language must be string.");
|
|
|
@@ -114,6 +127,7 @@ class languages {
|
|
|
console.error("Name of phrasebook file must be string.");
|
|
|
console.error("Check languages index.");
|
|
|
console.error("Skipping it.");
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
result.add(name, response[name]);
|
|
|
@@ -122,6 +136,34 @@ class languages {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This load index object. That check, and when content must be loaded
|
|
|
+ * from local filesystem, it use node:fs, or when it must be fetched from
|
|
|
+ * remote, then use fetch API.
|
|
|
+ *
|
|
|
+ * @param {string} index - Name of the index file in library.
|
|
|
+ * @returns {object} - Loaded index file content.
|
|
|
+ */
|
|
|
+ async #load_index(index) {
|
|
|
+ const path = this.#full_path(index);
|
|
|
+
|
|
|
+ if (this.#local) {
|
|
|
+ let fs = null;
|
|
|
+ NODE: fs = require("node:fs/promises");
|
|
|
+
|
|
|
+ if (fs === null) {
|
|
|
+ throw new Error("Could not use ndoe:fs in browser.");
|
|
|
+ }
|
|
|
+
|
|
|
+ return JSON.parse(
|
|
|
+ await fs.readFile(path, { encoding: "utf-8" })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ const request = await fetch(path);
|
|
|
+ return await request.json();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* This check that language exists in languages library.
|
|
|
*
|
|
|
@@ -162,13 +204,17 @@ class languages {
|
|
|
}
|
|
|
|
|
|
if (!this.has(name)) {
|
|
|
- throw new RangeError("Not found language \"" + name + "\".");
|
|
|
+ DEBUG: throw new RangeError(
|
|
|
+ "Not found language \"" + name + "\"."
|
|
|
+ );
|
|
|
+
|
|
|
+ return new phrasebook(new Map());
|
|
|
}
|
|
|
|
|
|
const file = this.#libs.get(name);
|
|
|
const path = this.#full_path(file);
|
|
|
|
|
|
- return new loader(path).load();
|
|
|
+ return new loader(path, this.#local).load();
|
|
|
}
|
|
|
|
|
|
/**
|