@hello.nrfcloud.com/proto-map 8.0.6 → 8.1.1
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/dist/senml/fromCBOR.js +179 -0
- package/dist/senml/fromCBOR.spec.js +53 -0
- package/export.js +1 -0
- package/index.d.ts +1 -0
- package/package.json +7 -7
- package/senml/fromCBOR.spec.ts +24 -0
- package/senml/fromCBOR.ts +36 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
function _array_like_to_array(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _array_with_holes(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
9
|
+
function _define_property(obj, key, value) {
|
|
10
|
+
if (key in obj) {
|
|
11
|
+
Object.defineProperty(obj, key, {
|
|
12
|
+
value: value,
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
obj[key] = value;
|
|
19
|
+
}
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
function _iterable_to_array_limit(arr, i) {
|
|
23
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
24
|
+
if (_i == null) return;
|
|
25
|
+
var _arr = [];
|
|
26
|
+
var _n = true;
|
|
27
|
+
var _d = false;
|
|
28
|
+
var _s, _e;
|
|
29
|
+
try {
|
|
30
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
31
|
+
_arr.push(_s.value);
|
|
32
|
+
if (i && _arr.length === i) break;
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
_d = true;
|
|
36
|
+
_e = err;
|
|
37
|
+
} finally{
|
|
38
|
+
try {
|
|
39
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
40
|
+
} finally{
|
|
41
|
+
if (_d) throw _e;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return _arr;
|
|
45
|
+
}
|
|
46
|
+
function _non_iterable_rest() {
|
|
47
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
48
|
+
}
|
|
49
|
+
function _object_spread(target) {
|
|
50
|
+
for(var i = 1; i < arguments.length; i++){
|
|
51
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
52
|
+
var ownKeys = Object.keys(source);
|
|
53
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
54
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
55
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
ownKeys.forEach(function(key) {
|
|
59
|
+
_define_property(target, key, source[key]);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return target;
|
|
63
|
+
}
|
|
64
|
+
function ownKeys(object, enumerableOnly) {
|
|
65
|
+
var keys = Object.keys(object);
|
|
66
|
+
if (Object.getOwnPropertySymbols) {
|
|
67
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
68
|
+
if (enumerableOnly) {
|
|
69
|
+
symbols = symbols.filter(function(sym) {
|
|
70
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
keys.push.apply(keys, symbols);
|
|
74
|
+
}
|
|
75
|
+
return keys;
|
|
76
|
+
}
|
|
77
|
+
function _object_spread_props(target, source) {
|
|
78
|
+
source = source != null ? source : {};
|
|
79
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
80
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
81
|
+
} else {
|
|
82
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
83
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return target;
|
|
87
|
+
}
|
|
88
|
+
function _sliced_to_array(arr, i) {
|
|
89
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
90
|
+
}
|
|
91
|
+
function _type_of(obj) {
|
|
92
|
+
"@swc/helpers - typeof";
|
|
93
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
94
|
+
}
|
|
95
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
96
|
+
if (!o) return;
|
|
97
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
98
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
99
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
100
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
101
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
102
|
+
}
|
|
103
|
+
var labelMap = new Map([
|
|
104
|
+
[
|
|
105
|
+
-1,
|
|
106
|
+
'bver'
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
-2,
|
|
110
|
+
'bn'
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
-3,
|
|
114
|
+
'bt'
|
|
115
|
+
],
|
|
116
|
+
[
|
|
117
|
+
-4,
|
|
118
|
+
'bu'
|
|
119
|
+
],
|
|
120
|
+
[
|
|
121
|
+
-5,
|
|
122
|
+
'bv'
|
|
123
|
+
],
|
|
124
|
+
[
|
|
125
|
+
-6,
|
|
126
|
+
'bs'
|
|
127
|
+
],
|
|
128
|
+
[
|
|
129
|
+
0,
|
|
130
|
+
'n'
|
|
131
|
+
],
|
|
132
|
+
[
|
|
133
|
+
1,
|
|
134
|
+
'u'
|
|
135
|
+
],
|
|
136
|
+
[
|
|
137
|
+
2,
|
|
138
|
+
'v'
|
|
139
|
+
],
|
|
140
|
+
[
|
|
141
|
+
3,
|
|
142
|
+
'vs'
|
|
143
|
+
],
|
|
144
|
+
[
|
|
145
|
+
4,
|
|
146
|
+
'vb'
|
|
147
|
+
],
|
|
148
|
+
[
|
|
149
|
+
5,
|
|
150
|
+
's'
|
|
151
|
+
],
|
|
152
|
+
[
|
|
153
|
+
6,
|
|
154
|
+
't'
|
|
155
|
+
],
|
|
156
|
+
[
|
|
157
|
+
7,
|
|
158
|
+
'ut'
|
|
159
|
+
],
|
|
160
|
+
[
|
|
161
|
+
8,
|
|
162
|
+
'vd'
|
|
163
|
+
]
|
|
164
|
+
]);
|
|
165
|
+
/**
|
|
166
|
+
* Convert SenML from CBOR notation to JSON notation.
|
|
167
|
+
*
|
|
168
|
+
* @see https://www.rfc-editor.org/rfc/rfc8428.html#section-6
|
|
169
|
+
*/ export var fromCBOR = function(records) {
|
|
170
|
+
return records.map(function(record) {
|
|
171
|
+
return Object.entries(record).reduce(function(json, param) {
|
|
172
|
+
var _param = _sliced_to_array(param, 2), k = _param[0], v = _param[1];
|
|
173
|
+
var label = labelMap.get(parseInt(k, 10));
|
|
174
|
+
if (label === undefined) return json;
|
|
175
|
+
if ((typeof v === "undefined" ? "undefined" : _type_of(v)) === 'bigint') v = Number(v);
|
|
176
|
+
return _object_spread_props(_object_spread({}, json), _define_property({}, label, v));
|
|
177
|
+
}, {});
|
|
178
|
+
});
|
|
179
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { fromCBOR } from './fromCBOR.js';
|
|
4
|
+
void describe('fromCBOR()', function() {
|
|
5
|
+
void it('should convert CBOR encoding to JSON encoding', function() {
|
|
6
|
+
return assert.deepEqual(fromCBOR([
|
|
7
|
+
{
|
|
8
|
+
'0': '0',
|
|
9
|
+
'2': 28.904369,
|
|
10
|
+
'-2': '14205/0/'
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
'0': '1',
|
|
14
|
+
'2': 17.66905
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
'0': '2',
|
|
18
|
+
'2': 100562.21875
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
'0': '10',
|
|
22
|
+
'2': 57
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
'0': '99',
|
|
26
|
+
'2': 1716475294528n
|
|
27
|
+
}
|
|
28
|
+
]), [
|
|
29
|
+
{
|
|
30
|
+
n: '0',
|
|
31
|
+
v: 28.904369,
|
|
32
|
+
bn: '14205/0/'
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
n: '1',
|
|
36
|
+
v: 17.66905
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
n: '2',
|
|
40
|
+
v: 100562.21875
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
n: '10',
|
|
44
|
+
v: 57
|
|
45
|
+
},
|
|
46
|
+
// Convert bigints
|
|
47
|
+
{
|
|
48
|
+
n: '99',
|
|
49
|
+
v: 1716475294528
|
|
50
|
+
}
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
});
|
package/export.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './dist/senml/SenMLSchema.js'
|
|
2
2
|
export * from './dist/senml/senMLtoLwM2M.js'
|
|
3
3
|
export * from './dist/senml/lwm2mToSenML.js'
|
|
4
|
+
export * from './dist/senml/fromCBOR.js'
|
|
4
5
|
export * from './dist/models/models.js'
|
|
5
6
|
export * from './dist/models/types.js'
|
|
6
7
|
export * from './dist/lwm2m/timestampResources.js'
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './senml/SenMLSchema.js'
|
|
2
2
|
export * from './senml/senMLtoLwM2M.js'
|
|
3
3
|
export * from './senml/lwm2mToSenML.js'
|
|
4
|
+
export * from './senml/fromCBOR.js'
|
|
4
5
|
export * from './models/models.js'
|
|
5
6
|
export * from './models/types.js'
|
|
6
7
|
export * from './lwm2m/timestampResources.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hello.nrfcloud.com/proto-map",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.1.1",
|
|
4
4
|
"description": "Documents the communication protocol between devices, the hello.nrfcloud.com/map backend and web application",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -39,22 +39,22 @@
|
|
|
39
39
|
"prepublishOnly": "./compile.sh"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@bifravst/eslint-config-typescript": "6.0.
|
|
42
|
+
"@bifravst/eslint-config-typescript": "6.0.26",
|
|
43
43
|
"@bifravst/prettier-config": "1.0.0",
|
|
44
44
|
"@commitlint/config-conventional": "19.2.2",
|
|
45
45
|
"@swc/cli": "0.3.12",
|
|
46
|
-
"@swc/core": "1.5.
|
|
47
|
-
"@types/node": "20.12.
|
|
46
|
+
"@swc/core": "1.5.7",
|
|
47
|
+
"@types/node": "20.12.12",
|
|
48
48
|
"@types/xml2js": "0.4.14",
|
|
49
49
|
"chalk": "5.3.0",
|
|
50
50
|
"globstar": "1.0.0",
|
|
51
51
|
"husky": "9.0.11",
|
|
52
|
-
"lint-staged": "15.2.
|
|
52
|
+
"lint-staged": "15.2.4",
|
|
53
53
|
"prettier-plugin-organize-imports": "3.2.4",
|
|
54
54
|
"remark": "15.0.1",
|
|
55
55
|
"remark-frontmatter": "5.0.0",
|
|
56
56
|
"tsmatchers": "5.0.2",
|
|
57
|
-
"tsx": "4.
|
|
57
|
+
"tsx": "4.11.0",
|
|
58
58
|
"xml2js": "0.6.2",
|
|
59
59
|
"yaml": "2.4.2"
|
|
60
60
|
},
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"lwm2m"
|
|
105
105
|
],
|
|
106
106
|
"peerDependencies": {
|
|
107
|
-
"@sinclair/typebox": "^0.32.
|
|
107
|
+
"@sinclair/typebox": "^0.32.31",
|
|
108
108
|
"ajv": "^8.13.0"
|
|
109
109
|
}
|
|
110
110
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
import { fromCBOR } from './fromCBOR.js'
|
|
4
|
+
|
|
5
|
+
void describe('fromCBOR()', () => {
|
|
6
|
+
void it('should convert CBOR encoding to JSON encoding', () =>
|
|
7
|
+
assert.deepEqual(
|
|
8
|
+
fromCBOR([
|
|
9
|
+
{ '0': '0', '2': 28.904369, '-2': '14205/0/' },
|
|
10
|
+
{ '0': '1', '2': 17.66905 },
|
|
11
|
+
{ '0': '2', '2': 100562.21875 },
|
|
12
|
+
{ '0': '10', '2': 57 },
|
|
13
|
+
{ '0': '99', '2': 1716475294528n },
|
|
14
|
+
]),
|
|
15
|
+
[
|
|
16
|
+
{ n: '0', v: 28.904369, bn: '14205/0/' },
|
|
17
|
+
{ n: '1', v: 17.66905 },
|
|
18
|
+
{ n: '2', v: 100562.21875 },
|
|
19
|
+
{ n: '10', v: 57 },
|
|
20
|
+
// Convert bigints
|
|
21
|
+
{ n: '99', v: 1716475294528 },
|
|
22
|
+
],
|
|
23
|
+
))
|
|
24
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const labelMap = new Map<number, string>([
|
|
2
|
+
[-1, 'bver'], // Base Version
|
|
3
|
+
[-2, 'bn'], // Base Name
|
|
4
|
+
[-3, 'bt'], // Base Time
|
|
5
|
+
[-4, 'bu'], // Base Unit
|
|
6
|
+
[-5, 'bv'], // Base Value
|
|
7
|
+
[-6, 'bs'], // Base Sum
|
|
8
|
+
[0, 'n'], // Name
|
|
9
|
+
[1, 'u'], // Unit
|
|
10
|
+
[2, 'v'], // Value
|
|
11
|
+
[3, 'vs'], // String Value
|
|
12
|
+
[4, 'vb'], // Boolean Value
|
|
13
|
+
[5, 's'], // Sum
|
|
14
|
+
[6, 't'], // Time
|
|
15
|
+
[7, 'ut'], // Update Time
|
|
16
|
+
[8, 'vd'], // Data Value
|
|
17
|
+
])
|
|
18
|
+
/**
|
|
19
|
+
* Convert SenML from CBOR notation to JSON notation.
|
|
20
|
+
*
|
|
21
|
+
* @see https://www.rfc-editor.org/rfc/rfc8428.html#section-6
|
|
22
|
+
*/
|
|
23
|
+
export const fromCBOR = (
|
|
24
|
+
records: Array<Record<number, any>>,
|
|
25
|
+
): Array<Record<string, unknown>> =>
|
|
26
|
+
records.map((record) =>
|
|
27
|
+
Object.entries(record).reduce((json, [k, v]) => {
|
|
28
|
+
const label = labelMap.get(parseInt(k, 10))
|
|
29
|
+
if (label === undefined) return json
|
|
30
|
+
if (typeof v === 'bigint') v = Number(v)
|
|
31
|
+
return {
|
|
32
|
+
...json,
|
|
33
|
+
[label]: v,
|
|
34
|
+
}
|
|
35
|
+
}, {}),
|
|
36
|
+
)
|