Skip to content

Commit

Permalink
feat: types (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgnomon authored Nov 1, 2023
1 parent 37c9e3e commit b07d89e
Show file tree
Hide file tree
Showing 9 changed files with 804 additions and 76 deletions.
13 changes: 5 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"env": {
"es6": true,
"es2024": true,
"node": true,
"commonjs": true,
"mocha": true
Expand All @@ -10,19 +10,16 @@
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["import", "mocha", "eslint-comments"],
"plugins": ["import", "mocha", "eslint-comments", "jsdoc", "unicorn"],
"extends": [
"eslint:recommended",
"plugin:eslint-comments/recommended",
"plugin:mocha/recommended"
"plugin:mocha/recommended",
"plugin:jsdoc/recommended-error",
"plugin:unicorn/recommended"
],
"globals": { "console": true },
"rules": {
"eslint-comments/disable-enable-pair": [
"error",
{ "allowWholeFile": true }
],
"eslint-comments/no-unused-disable": "error",
"import/order": [
"error",
{ "newlines-between": "always", "alphabetize": { "order": "asc" } }
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
npm ci
npm run fmt:check
npm run lint
npm run build
npm run test
- run: npm publish
env:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
npm ci
npm run fmt:check
npm run lint
npm run build
npm run test
./clapp migrate down
- run: ./clapp deinit -v
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/node_modules
/coverage
/types

# misc
.DS_Store
Expand Down
117 changes: 67 additions & 50 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ import util from "node:util";
import Redis from "ioredis";
import mysql from "mysql";

/**
* @typedef {object} MEMConfig
* @property {string} MEM_SHARD_1_INTERNAL_HOST - The internal host for MEM shard 1.
* @property {string} MEM_SHARD_2_INTERNAL_HOST - The internal host for MEM shard 2.
* @property {string} MEM_SHARD_3_INTERNAL_HOST - The internal host for MEM shard 3.
* @property {number} MEM_INTERNAL_PORT - The internal port for MEM.
*/

/**
* @param {number} shard -
* @returns {{host:string, port:number}} - endpoint
*/
function resolveEndpoint(shard) {
let portNum = 3306 - 1 + shard;
let portNumber = 3306 - 1 + shard;
if (process.env[`DB_SHARD_${shard}_INTERNAL_PORT`]) {
portNum = parseInt(process.env[`DB_SHARD_${shard}_INTERNAL_PORT`] || "0");
portNumber = Number.parseInt(
process.env[`DB_SHARD_${shard}_INTERNAL_PORT`] || "0"
);
}
return {
host: process.env[`DB_SHARD_${shard}_INTERNAL_HOST`] || "127.0.0.1",
port: portNum,
port: portNumber,
};
}

/**
* createPool.
*
* @param {number} shard
* @returns {Promise<mysql.Pool>}
* @param {number} shard - shard number
* @returns {Promise<mysql.Pool>} - mysql pool
*/
function createPool(shard) {
return new Promise((resolve, reject) => {
Expand All @@ -31,29 +43,32 @@ function createPool(shard) {
database: `myproject_${shard}`,
});
resolve(pool);
} catch (e) {
reject(e);
} catch (error) {
reject(error);
}
});
}

/**
* @typedef {import('ioredis').Cluster} RedisCluster
*/
export class DB {
#pool;
constructor(shardNum = 1) {
this.#pool = createPool(shardNum);

constructor(shardNumber = 1) {
this.#pool = createPool(shardNumber);
}

/**
* query.
* @param {string} sql
* @returns {Promise<[unknown, mysql.FieldInfo[]|undefined]>}
* @param {string} sql - sql
* @returns {Promise<[unknown, mysql.FieldInfo[]|undefined]>} - [results, fields]
*/
async query(sql) {
const pool = await this.#pool;
return new Promise((resolve, reject) => {
pool.query(sql, (err, results, fields) => {
if (err) {
reject(err);
pool.query(sql, (error, results, fields) => {
if (error) {
reject(error);
return;
}
resolve([results, fields]);
Expand All @@ -62,52 +77,53 @@ export class DB {
}

/**
* close.
* @returns {Promise<void>}
*/
async close() {
const pool = await this.#pool;
return new Promise((resolve) => {
pool.end((err) => {
if (err) {
console.error(err);
pool.end((error) => {
if (error) {
console.error(error);
return;
}
resolve(undefined);
resolve();
});
});
}
}

export class MEM {
#conf;
constructor(conf) {
if (!conf) {
conf = {
/**
* @param {MEMConfig} [config] - configuration
*/
constructor(config) {
if (!config) {
config = {
MEM_SHARD_1_INTERNAL_HOST: "clapp-mem-shard-1",
MEM_SHARD_2_INTERNAL_HOST: "clapp-mem-shard-2",
MEM_SHARD_3_INTERNAL_HOST: "clapp-mem-shard-3",
MEM_INTERNAL_PORT: 6373,
};
}
if (!conf.MEM_INTERNAL_PORT) {
if (!config.MEM_INTERNAL_PORT) {
throw new Error("MEM_INTERNAL_PORT is not set");
}
if (!conf.MEM_SHARD_1_INTERNAL_HOST) {
if (!config.MEM_SHARD_1_INTERNAL_HOST) {
throw new Error("MEM_SHARD_1_INTERNAL_HOST is not set");
}
if (!conf.MEM_SHARD_2_INTERNAL_HOST) {
if (!config.MEM_SHARD_2_INTERNAL_HOST) {
throw new Error("MEM_SHARD_2_INTERNAL_HOST is not set");
}
if (!conf.MEM_SHARD_3_INTERNAL_HOST) {
if (!config.MEM_SHARD_3_INTERNAL_HOST) {
throw new Error("MEM_SHARD_3_INTERNAL_HOST is not set");
}
this.#conf = conf;
this.#conf = config;
}

/**
* connect.
* @returns {Promise<Redis.Cluster>}
* @returns {Promise<RedisCluster>} - redisio cluster
*/
async connect() {
const ports = [
Expand All @@ -123,14 +139,15 @@ export class MEM {
const { stdout: getnetResult } = await execP(
"docker run --rm --network mynet ghcr.io/clusterlean/ark:main getent hosts clapp-mem-shard-1 clapp-mem-shard-2 clapp-mem-shard-3"
);
const hostMap = getnetResult
const ipHosts = getnetResult
.split(/\r?\n/)
.filter((line) => line.length > 0)
.map((line) => line.split(/\s+/))
.reduce((acc, [ip, host]) => {
acc[host] = ip;
return acc;
}, {});
.map((line) => line.split(/\s+/));

let hostMap = {};
for (const [ip, host] of ipHosts) {
hostMap[host] = ip;
}

config.natMap = {
[`${hostMap[this.#conf.MEM_SHARD_1_INTERNAL_HOST]}:${
Expand All @@ -157,20 +174,20 @@ export class MEM {
const cluster = new Redis.Cluster(
[
{
port: !process.env.REDIS_NATMAP_DISABLED
? ports[0]
: this.#conf.MEM_INTERNAL_PORT,
host: !process.env.REDIS_NATMAP_DISABLED
? dockerHost
: this.#conf.MEM_SHARD_1_INTERNAL_HOST,
port: process.env.REDIS_NATMAP_DISABLED
? this.#conf.MEM_INTERNAL_PORT
: ports[0],
host: process.env.REDIS_NATMAP_DISABLED
? this.#conf.MEM_SHARD_1_INTERNAL_HOST
: dockerHost,
},
{
port: !process.env.REDIS_NATMAP_DISABLED
? ports[1]
: this.#conf.MEM_INTERNAL_PORT,
host: !process.env.REDIS_NATMAP_DISABLED
? dockerHost
: this.#conf.MEM_SHARD_2_INTERNAL_HOST,
port: process.env.REDIS_NATMAP_DISABLED
? this.#conf.MEM_INTERNAL_PORT
: ports[1],
host: process.env.REDIS_NATMAP_DISABLED
? this.#conf.MEM_SHARD_2_INTERNAL_HOST
: dockerHost,
},
],
config
Expand Down
Loading

0 comments on commit b07d89e

Please sign in to comment.