@antti5/ship-tool-common 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ Ship Tool common utils and library code
2
+
3
+ Note: Other code for the project are currently in private repositories.
@@ -0,0 +1,11 @@
1
+ #!/bin/bash
2
+
3
+ curl -w "\n\n\
4
+ time_namelookup: %{time_namelookup} s\n\
5
+ time_connect: %{time_connect} s\n\
6
+ time_appconnect: %{time_appconnect} s\n\
7
+ time_pretransfer: %{time_pretransfer} s\n\
8
+ time_redirect: %{time_redirect} s\n\
9
+ time_starttransfer: %{time_starttransfer} s\n\
10
+ ---------------------------------\n\
11
+ time_total: %{time_total} s\n" -o /dev/null $@
package/bin/jpretty ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env node
2
+
3
+ /* Read UTF8-formatted JSON from stdin and print it out prettified */
4
+
5
+ import fs from 'fs';
6
+ import zlib from 'zlib';
7
+
8
+ const print = data => {
9
+ console.log(JSON.stringify(JSON.parse(data), null, 3));
10
+ };
11
+
12
+ if (process.argv.length > 2)
13
+ for (const filename of process.argv.slice(2))
14
+ print(filename.endsWith('.gz') ?
15
+ zlib.gunzipSync(fs.readFileSync(filename)) :
16
+ fs.readFileSync(filename)
17
+ );
18
+ else {
19
+ const chunks = [];
20
+ for await (const chunk of process.stdin)
21
+ chunks.push(chunk);
22
+
23
+ let data = Buffer.concat(chunks);
24
+ if (data[0] === 0x1f && data[1] === 0x8b)
25
+ data = zlib.gunzipSync(data);
26
+ print(data);
27
+ }
package/bin/repot ADDED
@@ -0,0 +1,69 @@
1
+ #!/bin/bash
2
+
3
+ RED='\033[0;31m'
4
+ GRAY='\033[1;30m'
5
+ RESET='\033[0m'
6
+
7
+ banner() {
8
+ echo
9
+ echo " $1 $2 " | tr '[:print:]' '='
10
+ echo -n " $1"
11
+ echo -en $GRAY
12
+ echo -n " $2"
13
+ echo -e $RESET
14
+ echo " $1 $2 " | tr '[:print:]' '='
15
+ echo
16
+ }
17
+
18
+ REPOS_DIRECTORY=$HOME
19
+
20
+ REPOS=""
21
+ for repo in ship-tool/common ship-tool/frontend ship-tool/backend ship-tool/data; do
22
+ if [ -d $REPOS_DIRECTORY/$repo ]; then
23
+ REPOS="$REPOS $repo"
24
+ else
25
+ echo -e "\nWarning: Repository \"$repo\" is not cloned?\n"
26
+ sleep 2
27
+ fi
28
+ done
29
+
30
+ # Store current working directory
31
+
32
+ PWD=`pwd`
33
+
34
+ # List local source files
35
+
36
+ FILES_INCLUDE='js$\|jsx$\|py$\|css$'
37
+ FILES_EXCLUDE='^build.*/'
38
+
39
+ files=''
40
+
41
+ cd $REPOS_DIRECTORY
42
+ for repo in $REPOS; do
43
+ cd $repo
44
+ for file in `git ls-files | grep $FILES_INCLUDE | grep -v $FILES_EXCLUDE`; do
45
+ files="$files$repo/$file "
46
+ done
47
+ cd $REPOS_DIRECTORY
48
+ done
49
+
50
+ wc -l $files
51
+
52
+ # Update remote refs for repos and check if there are incoming or outgoing changes
53
+
54
+ for repo in $REPOS; do
55
+ cd $REPOS_DIRECTORY/$repo
56
+ name=`jgrep -v name package.json`
57
+ version=`jgrep -v ^version package.json`
58
+ banner $name $version
59
+ git remote update origin
60
+ git status
61
+ # Print out warnings if dependencies are not up to date
62
+ echo -en $RED
63
+ npm ls > /dev/null
64
+ echo -en $RESET
65
+ done
66
+
67
+ # Restore working directory
68
+
69
+ cd $PWD
package/bin/tag ADDED
@@ -0,0 +1,24 @@
1
+ #!/bin/bash
2
+
3
+ WD=`pwd`
4
+ REGEX='^v0\.[0-9]+\.[0-9]+\.?[0-9]?$'
5
+
6
+ if [[ $# < 1 ]]; then
7
+ echo "Usage: tag <version>"
8
+ exit 1
9
+ fi
10
+
11
+ if [[ ! $1 =~ $REGEX ]]; then
12
+ echo "Error: Tag must match regular expression $REGEX"
13
+ exit 1
14
+ fi
15
+
16
+ cd ~/ship-tool
17
+ for d in frontend backend data; do
18
+ cd $d
19
+ git tag $1
20
+ git push --tags
21
+ cd ..
22
+ done
23
+
24
+ cd $WD
@@ -0,0 +1,44 @@
1
+ import { defineConfig } from 'eslint/config';
2
+ import js from '@eslint/js';
3
+ import globals from 'globals';
4
+
5
+ export default defineConfig([{
6
+ files: ['**/*.{js,mjs,cjs}'],
7
+ plugins: { js },
8
+ extends: ['js/recommended'],
9
+ languageOptions: {
10
+ globals: {
11
+ ...globals.browser, // window object is used in Util
12
+ ...globals.node
13
+ },
14
+ ecmaVersion: 2022,
15
+ sourceType: 'module'
16
+ },
17
+ rules: {
18
+ indent: ['error', 3, {
19
+ ignoreComments: true,
20
+ SwitchCase: 1
21
+ }],
22
+ 'linebreak-style': ['error', 'unix'],
23
+ quotes: ['error', 'single'],
24
+ semi: ['error', 'always'],
25
+ eqeqeq: 'error',
26
+ 'no-trailing-spaces': ['warn', {
27
+ ignoreComments: true
28
+ }],
29
+ 'arrow-spacing': ['error', {
30
+ before: true,
31
+ after: true
32
+ }],
33
+ 'no-unused-vars': ['warn', {
34
+ varsIgnorePattern: '^_',
35
+ argsIgnorePattern: '^_',
36
+ caughtErrorsIgnorePattern: '^_'
37
+ }],
38
+ 'object-curly-spacing': ['error', 'always'],
39
+ 'no-constant-condition': ['warn', {
40
+ checkLoops: false
41
+ }],
42
+ },
43
+ }]);
44
+
@@ -0,0 +1,120 @@
1
+ class Semaphore {
2
+
3
+ static TPS_MAX = 10_000;
4
+
5
+ #value = 0;
6
+ #max;
7
+ #queue = [];
8
+
9
+ #waiter = {};
10
+ #waiterQueue = {};
11
+
12
+ #measureTps;
13
+ #timestamps = [];
14
+
15
+ constructor(max = 1, { measureTps = false } = {}) {
16
+ this.#max = max;
17
+ this.#measureTps = measureTps;
18
+ }
19
+
20
+ acquire({ jobClass = null } = {}) {
21
+ if (this.#value < this.#max) {
22
+ this.#value++;
23
+ return new Promise(resolve => resolve());
24
+ } else
25
+ return new Promise((resolve, reject) =>
26
+ this.#queue.push({ jobClass, resolve, reject })
27
+ );
28
+ }
29
+
30
+ release() {
31
+ this.#value--;
32
+
33
+ if (this.#measureTps) {
34
+ this.#timestamps.push(Date.now());
35
+ if (this.#timestamps.length > Semaphore.TPS_MAX)
36
+ this.#timestamps.shift();
37
+ }
38
+
39
+ if (this.#queue.length > 0 && this.#value < this.#max) {
40
+ this.#value++;
41
+ this.#queue.shift().resolve();
42
+
43
+ if (this.#waiterQueue[this.#queue.length]?.length > 0)
44
+ for (const { resolve } of this.#waiterQueue[this.#queue.length])
45
+ resolve();
46
+ }
47
+
48
+ else if (this.#waiter[this.#value]?.length > 0)
49
+ for (const { resolve } of this.#waiter[this.#value])
50
+ resolve();
51
+ }
52
+
53
+ purge() {
54
+ for (const { reject } of this.#queue)
55
+ reject('Semaphore was purged');
56
+
57
+ for (const { reject } of [...Object.values(this.#waiter), ...Object.values(this.#waiterQueue)].flat())
58
+ reject('Semaphore was purged');
59
+
60
+ this.#queue = [];
61
+ this.#value = 0;
62
+ }
63
+
64
+ raiseJobClass(jobClass) {
65
+ this.#queue = [
66
+ ...this.#queue.filter(job => job.jobClass === jobClass),
67
+ ...this.#queue.filter(job => !job.jobClass || job.jobClass !== jobClass)
68
+ ];
69
+ }
70
+
71
+ getQueueLength() {
72
+ return this.#queue.length;
73
+ }
74
+
75
+ getTps(count = Semaphore.TPS_MAX) {
76
+ const slice = this.#timestamps.slice(-count);
77
+ return slice.length > 1 ?
78
+ (slice.length - 1) / (slice.at(-1) - slice.at(0)) * 1000 :
79
+ 0;
80
+ }
81
+
82
+ getMax() {
83
+ return this.#max;
84
+ }
85
+
86
+ setMax(max) {
87
+ this.#max = max;
88
+ }
89
+
90
+ async wait(value = 0) {
91
+ if (this.#value > value) {
92
+ return new Promise((resolve, reject) => {
93
+ this.#waiter[value] ??= [];
94
+ this.#waiter[value].push({ resolve, reject });
95
+ });
96
+ }
97
+ }
98
+
99
+ async waitQueue(length = 0) {
100
+ while (this.#queue.length > length)
101
+ return new Promise((resolve, reject) => {
102
+ this.#waiterQueue[length] ??= [];
103
+ this.#waiterQueue[length].push({ resolve, reject });
104
+ });
105
+ }
106
+
107
+ async run(func, { jobClass = null } = {}) {
108
+ try {
109
+ await this.acquire({ jobClass });
110
+ return await func();
111
+ }
112
+
113
+ finally {
114
+ this.release();
115
+ }
116
+ }
117
+ }
118
+
119
+
120
+ export default Semaphore;