Skip to content

Commit cc60216

Browse files
committed
added linting; added Docker test stuff; added initial support for frequency counters
1 parent d6de5d9 commit cc60216

31 files changed

+3264
-239
lines changed

.eslintrc.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: [
5+
'./tsconfig.json',
6+
],
7+
tsconfigRootDir: __dirname,
8+
},
9+
plugins: [
10+
'@typescript-eslint',
11+
],
12+
extends: [
13+
'eslint:recommended',
14+
'plugin:@typescript-eslint/recommended',
15+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
16+
// Sets up both the plugin and eslint-config-prettier in one go.
17+
'plugin:prettier/recommended',
18+
],
19+
root: true,
20+
env: {
21+
node: true,
22+
jest: true,
23+
},
24+
ignorePatterns: ['.eslintrc.js'],
25+
rules: {
26+
// '@typescript-eslint/interface-name-prefix': 'off',
27+
// '@typescript-eslint/explicit-function-return-type': 'off',
28+
// '@typescript-eslint/explicit-module-boundary-types': 'off',
29+
// '@typescript-eslint/no-explicit-any': 'off',
30+
'prefer-const': ['error', {
31+
destructuring: 'all'
32+
}],
33+
},
34+
};

.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"endOfLine": "auto"
4+
}

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true
3+
}

Dockerfile

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# syntax=docker/dockerfile:experimental
2+
FROM node:16-alpine as builder_base
3+
4+
ARG NPM_TOKEN
5+
6+
RUN apk --no-cache add \
7+
#autoconf \
8+
#automake \
9+
#python \
10+
#make \
11+
#g++ \
12+
yarn
13+
14+
WORKDIR /app
15+
16+
COPY package.json yarn.lock ./
17+
18+
19+
FROM builder_base as builder_development
20+
21+
ENV NODE_ENV development
22+
23+
RUN \
24+
# This is for the yarn cache. We have to use different caches otherwise both
25+
# this and the production stage will use the same cache and clobber each
26+
# other if they are run in parallel.
27+
--mount=type=cache,id=builder_development_yarn,target=/usr/local/share/.cache/yarn \
28+
# This is to keep the node_modules dir around between builds otherwise yarn
29+
# has to do a fresh install + build whenever the lock file changes.
30+
--mount=type=cache,id=builder_development_node_modules,target=/app/node_modules \
31+
# Defaults to installing all (prod + dev) packages.
32+
yarn --no-progress && \
33+
# The node_modules dir ceases to exist after the build so unless it's copied
34+
# off it can't be copied into the below stages.
35+
cp -r /app/node_modules/ /app/node_modules.sav
36+
37+
38+
# This is used for both staging and production builds so we can't solely rely
39+
# on the NODE_ENV env var.
40+
FROM builder_base as builder_production
41+
42+
ENV NODE_ENV production
43+
44+
RUN \
45+
--mount=type=cache,id=builder_production_yarn,target=/usr/local/share/.cache/yarn \
46+
--mount=type=cache,id=builder_production_node_modules,target=/app/node_modules \
47+
yarn --prod --no-progress && \
48+
cp -r /app/node_modules/ /app/node_modules.sav
49+
50+
51+
FROM node:16-alpine as base
52+
53+
ARG UID=2001
54+
55+
ARG USER=ngapps
56+
57+
RUN adduser --disabled-password --uid ${UID} --shell /bin/ash ${USER}
58+
59+
ENV PATH /app/node_modules/.bin:$PATH
60+
61+
62+
FROM base as development
63+
64+
RUN mkdir /app && \
65+
chown ${USER}:${USER} /app
66+
67+
WORKDIR /app
68+
69+
ARG NPM_TOKEN
70+
71+
ENV DEBUG manager:*
72+
73+
ENV NODE_ENV development
74+
75+
ENV NPM_TOKEN=${NPM_TOKEN}
76+
77+
COPY --from=builder_development --chown=${USER}:${USER} \
78+
/app/node_modules.sav node_modules
79+
80+
USER ${USER}
81+
82+
CMD ["nodemon", "-r", "source-map-support/register", "--delay", "1", "-e", "js", "dist/demo.js"]
83+
84+
85+
FROM base as tsc_watch
86+
87+
ENV NODE_ENV development
88+
89+
WORKDIR /app
90+
91+
USER ${USER}
92+
93+
# Not using tsc's actual watch command here because it clears the screen.
94+
# CMD ["nodemon", "--watch", "/app/src/server", "--watch", "/app/src/test", "-e", "ts,js", "--exec", "tsc -p tsconfig.development.json"]
95+
CMD ["tsc", "-w", "--preserveWatchOutput", "-p", "tsconfig.json"]
96+
97+
98+
# FROM base as testwatch
99+
#
100+
# ENV NODE_ENV test
101+
#
102+
# RUN mkdir /app && \
103+
# chown ${USER}:${USER} /app
104+
#
105+
# WORKDIR /app
106+
#
107+
# COPY --from=builder_development --chown=${USER}:${USER} \
108+
# /app/node_modules.sav node_modules
109+
#
110+
# USER ${USER}
111+
#
112+
# CMD ["nodemon", "--exec", "tap", "src/**/*.test.js"]
113+
#
114+
#
115+
# FROM base as test
116+
#
117+
# ENV NODE_ENV test
118+
#
119+
# RUN mkdir /app && \
120+
# chown ${USER}:${USER} /app
121+
#
122+
# WORKDIR /app
123+
#
124+
# COPY --from=builder_development --chown=${USER}:${USER} \
125+
# /app/node_modules.sav node_modules
126+
#
127+
# USER ${USER}
128+
#
129+
# RUN \
130+
# --mount=type=bind,source=package.json,target=package.json \
131+
# --mount=type=bind,source=src,target=src \
132+
# npm test
133+
#
134+
#
135+
# FROM base as production
136+
#
137+
# EXPOSE 3000
138+
#
139+
# WORKDIR /app
140+
#
141+
# COPY --from=builder_production /app/node_modules.sav node_modules
142+
#
143+
# COPY src/ src/
144+
#
145+
# ARG NODE_ENV=production
146+
#
147+
# ENV NODE_ENV ${NODE_ENV}
148+
#
149+
# USER ${USER}
150+
#
151+
# CMD ["node", "src/server.js"]

docker-compose.yml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
version: "3.9"
2+
services:
3+
proxy:
4+
build:
5+
args:
6+
- USER
7+
context: ./test-proxy
8+
hostname: test-proxy
9+
init: true
10+
ports:
11+
# Non-ssl port
12+
- 8100:8100
13+
- 8101:8101
14+
- 8102:8102
15+
- 8103:8103
16+
volumes:
17+
- /home/${USER}/.cache/yarn:/home/${USER}/.cache/yarn
18+
- ${PWD}/../haproxy-2.5.0/haproxy:/usr/local/sbin/haproxy
19+
- ${PWD}/test-proxy/etc:/usr/local/etc/haproxy
20+
- ${PWD}/test-proxy/package.json:/app/package.json
21+
- ${PWD}/test-proxy/yarn.lock:/app/yarn.lock
22+
23+
demo:
24+
build:
25+
args:
26+
- NODE_ENV
27+
- NPM_TOKEN
28+
- UID
29+
- USER
30+
context: .
31+
target: development
32+
init: true
33+
#ports:
34+
# - 8200:8200
35+
volumes:
36+
- ${PWD}/test-web/dot-npmrc:/app/.npmrc
37+
# Use the host's yarn cache otherwise each time you run yarn inside a
38+
# fresh container, it'll download all packages to the cache inside the
39+
# container. $USER is set by the shell and exported explicitly so it'll
40+
# just work here.
41+
- /home/${USER}/.cache/yarn:/home/${USER}/.cache/yarn
42+
- ${PWD}/dist:/app/dist
43+
- ${PWD}/package.json:/app/package.json
44+
- ${PWD}/yarn.lock:/app/yarn.lock
45+
46+
tsc_watch:
47+
build:
48+
args:
49+
- UID
50+
- USER
51+
context: .
52+
target: tsc_watch
53+
init: true
54+
volumes:
55+
- ${PWD}:/app
56+
57+
# web:
58+
# image: nginx
59+
# ports:
60+
# - 8300:80
61+
# #init: true
62+
# volumes:
63+
# - ${PWD}/web:/usr/share/nginx/html:ro

package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@
88
"license": "LGPL-3.0-or-later",
99
"private": true,
1010
"devDependencies": {
11-
"@types/debug": "^4.1.5",
12-
"@types/node": "^12.12.53",
13-
"typescript": "^4.0.5"
11+
"@types/debug": "^4.1.7",
12+
"@types/node": "^17.0.4",
13+
"@typescript-eslint/eslint-plugin": "^5.8.0",
14+
"@typescript-eslint/parser": "^5.8.0",
15+
"eslint": "^8.5.0",
16+
"eslint-config-prettier": "^8.3.0",
17+
"eslint-plugin-prettier": "^4.0.0",
18+
"nodemon": "^2.0.15",
19+
"prettier": "^2.5.1",
20+
"source-map-support": "^0.5.21",
21+
"typescript": "^4.5.4"
1422
},
1523
"dependencies": {
16-
"debug": "^4.3.1"
24+
"debug": "^4.3.3"
1725
}
1826
}

src/demo.ts

+37-22
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,61 @@
1717
* SPDX-License-Identifier: LGPL-3.0-or-later
1818
*/
1919

20-
import net from "net";
20+
import net from 'net';
21+
import d from 'debug';
2122
import {
2223
PeerConnection,
2324
PeerDirection,
2425
TableDefinition,
2526
EntryUpdate,
2627
SynchronizationType,
27-
} from "./haproxy/peers";
28-
import { DataType } from "./haproxy/peers/wire-types";
28+
} from './haproxy/peers';
29+
import { DataType } from './haproxy/peers/wire-types';
30+
31+
const debug = d('manager:demo');
2932

3033
function reconnect() {
31-
console.log("connecting");
32-
const socket = net.connect(20000, "127.0.0.1");
34+
debug('connecting');
35+
36+
const socket = net.connect(8102, 'test-proxy');
37+
3338
const conn = new PeerConnection(socket, {
34-
myName: "tracker",
35-
peerName: "haproxy.example.com",
39+
myName: 'tracker',
40+
peerName: 'test-proxy',
3641
direction: PeerDirection.OUT,
3742
});
38-
socket.on("close", () => setTimeout(reconnect, 1500));
39-
socket.on("error", (e) => {
40-
console.log(e);
43+
44+
socket.on('close', () => setTimeout(reconnect, 500));
45+
46+
socket.on('error', (e) => {
47+
debug(e);
4148
});
4249

43-
conn.on("tableDefinition", (def: TableDefinition) => {
44-
console.log(`Received table defition ${def.name}:`, def);
50+
conn.on('tableDefinition', (def: TableDefinition) => {
51+
debug(`Received table defition ${def.name}:`, def);
4552
});
46-
conn.on("entryUpdate", (update: EntryUpdate, def: TableDefinition) => {
47-
console.log(
48-
`Received entry update in table ${def.name} for key '${update.key.key}':`,
49-
new Map(Array.from(update.values.entries()).map(([k, v]) => {
50-
return [DataType[k], v];
51-
}))
53+
54+
conn.on('entryUpdate', (update: EntryUpdate, def: TableDefinition) => {
55+
debug(
56+
`Received entry update in table ${def.name} for key '${
57+
update.key.key as string
58+
}':`,
59+
new Map(
60+
Array.from(update.values.entries()).map(([k, v]) => {
61+
return [DataType[k], v];
62+
})
63+
)
5264
);
5365
});
54-
conn.on("synchronizationStarted", () => {
55-
console.log(`Started sync`);
66+
67+
conn.on('synchronizationStarted', () => {
68+
debug(`Started sync`);
5669
});
57-
conn.on("synchronizationFinished", (type: SynchronizationType) => {
58-
console.log(`Finished sync ${type}`);
70+
71+
conn.on('synchronizationFinished', (type: SynchronizationType) => {
72+
debug(`Finished sync ${type}`);
5973
});
74+
6075
conn.start(true);
6176
}
6277
reconnect();

0 commit comments

Comments
 (0)