signalk-polar-performance-plugin 1.4.0 → 2.1.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.
@@ -0,0 +1,42 @@
1
+ 'use strict'
2
+
3
+ function withRoutePolicies(router) {
4
+ if (router.routePolicies instanceof Map) return router
5
+
6
+ const policies = new Map()
7
+ const methods = ['get', 'post', 'put', 'patch', 'delete']
8
+ const originalMethods = new Map()
9
+ for (const method of methods) {
10
+ if (typeof router[method] !== 'function') continue
11
+ const original = router[method]
12
+ originalMethods.set(method, original)
13
+ router[method] = (route, ...handlers) => {
14
+ policies.set(`${method.toUpperCase()} ${route}`, 'admin')
15
+ return original.call(router, route, ...handlers)
16
+ }
17
+ }
18
+
19
+ router.access = (level) => {
20
+ if (level !== 'readonly' && level !== 'readwrite') {
21
+ throw new Error(`Unsupported route access level: ${level}`)
22
+ }
23
+ const scopedRouter = {}
24
+ for (const [method, original] of originalMethods) {
25
+ scopedRouter[method] = (route, ...handlers) => {
26
+ policies.set(`${method.toUpperCase()} ${route}`, level)
27
+ return original.call(router, route, ...handlers)
28
+ }
29
+ }
30
+ return scopedRouter
31
+ }
32
+ router.routePolicies = policies
33
+ return router
34
+ }
35
+
36
+ function wrapPluginRoutes(plugin) {
37
+ const registerWithRouter = plugin.registerWithRouter
38
+ plugin.registerWithRouter = (router) => registerWithRouter(withRoutePolicies(router))
39
+ return plugin
40
+ }
41
+
42
+ module.exports = { withRoutePolicies, wrapPluginRoutes }
package/docs/import.png DELETED
Binary file
package/docs/polar.png DELETED
Binary file
Binary file
@@ -1,133 +0,0 @@
1
- 'use strict'
2
-
3
- const fs = require('fs')
4
- const path = require('path')
5
- const { PolarTable } = require('./PolarTable')
6
-
7
- /**
8
- * PolarFileStore — manages stored polar resources in the plugin data directory.
9
- *
10
- * All file names are bare names (no path, no extension).
11
- * Files are stored as canonical <name>.json resources in the configured data directory.
12
- */
13
- class PolarFileStore {
14
- /**
15
- * @param {string} dataDir - Absolute path to the directory where polar files are stored.
16
- */
17
- constructor(dataDir) {
18
- this.dataDir = dataDir
19
- if (!fs.existsSync(dataDir)) {
20
- fs.mkdirSync(dataDir, { recursive: true })
21
- }
22
- }
23
-
24
- /** Resolves a bare name to its full JSON file path. */
25
- _jsonPath(name) {
26
- const safe = path.basename(name)
27
- return path.join(this.dataDir, `${safe}.json`)
28
- }
29
-
30
- /** Checks whether a canonical polar resource exists. */
31
- exists(name) {
32
- return fs.existsSync(this._jsonPath(name))
33
- }
34
-
35
- /**
36
- * List all canonical polar resources in the data directory.
37
- * @returns {string[]} Array of bare names, sorted alphabetically.
38
- */
39
- list() {
40
- return fs.readdirSync(this.dataDir)
41
- .filter(fileName => fileName.endsWith('.json'))
42
- .map(fileName => fileName.slice(0, -5))
43
- .sort()
44
- }
45
-
46
- /**
47
- * Read the raw JSON content of a stored canonical polar resource.
48
- * @param {string} name - Bare name of the polar.
49
- * @returns {string} Raw file content.
50
- * @throws {Error} If the file does not exist.
51
- */
52
- read(name) {
53
- const jp = this._jsonPath(name)
54
- if (!fs.existsSync(jp)) throw new Error(`Polar not found: ${name}`)
55
- return fs.readFileSync(jp, 'utf8')
56
- }
57
-
58
- /**
59
- * Load a canonical polar resource and return a populated PolarTable.
60
- * @param {string} name - Bare name of the polar.
61
- * @returns {PolarTable}
62
- * @throws {Error} If the file does not exist or is not a canonical resource.
63
- */
64
- load(name) {
65
- const stored = this.readObject(name)
66
- if (stored.kind !== 'polarTable') {
67
- throw new Error(`Stored polar is not a canonical polarTable resource: ${name}`)
68
- }
69
- return new PolarTable().loadFromCanonical(stored)
70
- }
71
-
72
- /** Reads and parses a stored JSON polar resource. */
73
- readObject(name) {
74
- const jp = this._jsonPath(name)
75
- if (!fs.existsSync(jp)) throw new Error(`Polar not found: ${name}`)
76
- return JSON.parse(fs.readFileSync(jp, 'utf8'))
77
- }
78
-
79
- /**
80
- * Read metadata for a polar.
81
- * Reads directly from the stored canonical resource.
82
- */
83
- readMeta(name) {
84
- try {
85
- const stored = this.readObject(name)
86
- return {
87
- ...(stored.name ? { boatName: stored.name } : {}),
88
- ...(stored.boatType ? { boatType: stored.boatType } : {}),
89
- ...(stored.sailnumber ? { sailnumber: stored.sailnumber } : {}),
90
- ...(Number.isInteger(stored.year) ? { year: stored.year } : {}),
91
- ...(stored.source ? { source: stored.source } : {}),
92
- ...(stored.notes ? { notes: stored.notes } : {}),
93
- }
94
- } catch (_) { return {} }
95
- }
96
-
97
- /**
98
- * List all polar files with their stored metadata.
99
- * @returns {{ name: string, boatName?: string, boatType?: string, sailnumber?: string }[]}
100
- */
101
- listWithMeta() {
102
- return this.list().map(name => ({ name, ...this.readMeta(name) }))
103
- }
104
-
105
- /** Save a canonical polar resource as JSON. */
106
- saveCanonical(name, resource) {
107
- const stored = { ...resource, id: name, kind: 'polarTable' }
108
- fs.writeFileSync(this._jsonPath(name), JSON.stringify(stored, null, 2), 'utf8')
109
- return name
110
- }
111
-
112
- /**
113
- * Copy a stored canonical polar resource to a new name.
114
- */
115
- copy(srcName, dstName) {
116
- const srcJson = this._jsonPath(srcName)
117
- if (!fs.existsSync(srcJson)) throw new Error(`Polar not found: ${srcName}`)
118
- fs.copyFileSync(srcJson, this._jsonPath(dstName))
119
- }
120
-
121
- /**
122
- * Delete a stored canonical polar resource.
123
- * @param {string} name - Bare name of the polar to delete.
124
- * @throws {Error} If the file does not exist.
125
- */
126
- delete(name) {
127
- const jp = this._jsonPath(name)
128
- if (!fs.existsSync(jp)) throw new Error(`Polar not found: ${name}`)
129
- fs.unlinkSync(jp)
130
- }
131
- }
132
-
133
- module.exports = PolarFileStore