@signalk/freeboard-sk 2.19.0-beta.1 → 2.19.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.
@@ -1,132 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initFlags = void 0;
4
- const fs_1 = require("fs");
5
- const promises_1 = require("fs/promises");
6
- const mid_1 = require("./mid");
7
- let server;
8
- let pluginId;
9
- const FLAGS_API_PATH = '/signalk/v2/api/resources/flags';
10
- let IMG_BASE_PATH = '';
11
- const initFlags = (app, id) => {
12
- server = app;
13
- pluginId = id;
14
- initFs();
15
- initFlagsEndpoints();
16
- };
17
- exports.initFlags = initFlags;
18
- // check path to flag resources
19
- const initFs = async () => {
20
- const p = __dirname.split('/');
21
- const sp = p.slice(0, p.indexOf('plugin')).join('/');
22
- IMG_BASE_PATH = `${sp}/node_modules/flag-icons/flags`;
23
- try {
24
- // check path exists
25
- await (0, promises_1.access)(IMG_BASE_PATH, fs_1.constants.R_OK);
26
- }
27
- catch (error) {
28
- server.setPluginError(`Flags path NOT found!`);
29
- }
30
- };
31
- const initFlagsEndpoints = () => {
32
- server.debug(`** Registering Flag resources endpoint(s) **`);
33
- server.get(`${FLAGS_API_PATH}`, async (req, res) => {
34
- server.debug(`** ${req.method} ${req.path}`);
35
- try {
36
- const list = await listResponse();
37
- res.status(200).json({
38
- aspects: ['1x1', '4x3'],
39
- flags: list
40
- });
41
- }
42
- catch (e) {
43
- res.status(400).json({
44
- state: 'FAILED',
45
- statusCode: 400,
46
- message: e.message
47
- });
48
- }
49
- });
50
- server.get(`${FLAGS_API_PATH}/mid/:mid`, (req, res) => {
51
- server.debug(`** ${req.method} ${req.path}`);
52
- iconByMid(req.params.mid, req.params.aspect, res);
53
- });
54
- server.get(`${FLAGS_API_PATH}/:aspect/:icon`, (req, res) => {
55
- server.debug(`** ${req.method} ${req.path}`);
56
- iconResponse(req.params.icon, req.params.aspect, res);
57
- });
58
- };
59
- /**
60
- * Build list of flag ids
61
- * @param aspect Aspect ratio '1x1' or '4x3'
62
- * @returns array of flag ids
63
- */
64
- const listResponse = async (aspect = '1x1') => {
65
- const entries = await (0, promises_1.readdir)(`${IMG_BASE_PATH}/${aspect}`, {
66
- withFileTypes: true
67
- });
68
- return entries.map((entry) => {
69
- if (entry.isFile()) {
70
- return entry.name.split('.')[0];
71
- }
72
- });
73
- };
74
- /**
75
- * Send file response for the specified flag id
76
- * @param aspect Aspect ratio '1x1' or '4x3'
77
- * @returns svg file contents
78
- */
79
- const iconResponse = async (id, aspect = '1x1', res) => {
80
- const flag = `${IMG_BASE_PATH}/${aspect}/${id}.svg`;
81
- try {
82
- // check path exists
83
- await (0, promises_1.access)(flag, fs_1.constants.R_OK);
84
- res.sendFile(flag, (err) => {
85
- if (err) {
86
- res.status(400).json({
87
- state: 'FAILED',
88
- statusCode: 400,
89
- message: err.message
90
- });
91
- }
92
- });
93
- }
94
- catch (error) {
95
- res.status(400).json({
96
- state: 'FAILED',
97
- statusCode: 400,
98
- message: `Flag (${id}) NOT found!`
99
- });
100
- return;
101
- }
102
- };
103
- /**
104
- * Send file response for the supplied mid
105
- * @param aspect Aspect ratio '1x1' or '4x3'
106
- * @returns svg file contents
107
- */
108
- const iconByMid = async (mid, aspect = '4x3', res) => {
109
- const code = mid_1.MID[mid][0]?.toLowerCase();
110
- const flag = `${IMG_BASE_PATH}/${aspect}/${code}.svg`;
111
- try {
112
- // check path exists
113
- await (0, promises_1.access)(flag, fs_1.constants.R_OK);
114
- res.sendFile(flag, (err) => {
115
- if (err) {
116
- res.status(400).json({
117
- state: 'FAILED',
118
- statusCode: 400,
119
- message: err.message
120
- });
121
- }
122
- });
123
- }
124
- catch (error) {
125
- res.status(400).json({
126
- state: 'FAILED',
127
- statusCode: 400,
128
- message: `Flag (${mid}) NOT found!`
129
- });
130
- return;
131
- }
132
- };