cno-bedrock 0.0.4 → 0.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.
- package/CHANGELOG.md +38 -0
- package/package.json +116 -116
- package/scripts/generate-docs.js +0 -0
- package/src/cno-assert.js +222 -15
- package/src/cno-assert.test.js +331 -0
- package/src/cno-base64.js +230 -0
- package/src/cno-base64.test.js +262 -0
- package/src/cno-bytes.js +387 -0
- package/src/cno-bytes.test.js +222 -0
- package/src/cno-constants.js +121 -0
- package/src/cno-files.js +2135 -0
- package/src/cno-files.test.js +939 -0
- package/src/cno-math.js +407 -0
- package/src/cno-math.test.js +249 -0
- package/src/cno-noop.js +6 -2
- package/src/cno-options.js +73 -4
- package/src/cno-options.test.js +65 -0
- package/src/cno-sha2-256.js +831 -0
- package/src/cno-sha2-256.test.js +697 -0
- package/src/cno-test-matrix.js +709 -0
- package/src/cno-test-matrix.test.js +199 -0
- package/src/cno-timed-cache-map.js +103 -0
- package/src/cno-timed-cache-map.test.js +113 -0
- package/src/cno-tree.js +365 -0
- package/src/cno-tree.test.js +147 -0
- package/src/cno-utility.js +545 -2
- package/src/cno-utility.test.js +276 -0
- package/src/cno-vector.js +1053 -0
- package/src/cno-vector.test.js +523 -0
- package/src/lib.js +5 -138
- package/src/lib.test.js +19 -7
- package/src/stream-test.js +116 -0
- package/test/big-lorem.txt +29 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-base64.test.js](src/cno-base64.test.js)
|
|
4
|
+
> Primary tests for `cno-base64`
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2024 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import DefaultExport from './cno-base64.js';
|
|
34
|
+
import * as NamespaceExport from './cno-base64.js';
|
|
35
|
+
import { getBase64FromBuffer, getBufferFromBase64 } from './cno-base64.js';
|
|
36
|
+
import TestNS from './cno-test-matrix.js';
|
|
37
|
+
//## Standard
|
|
38
|
+
//## External
|
|
39
|
+
//# Constants
|
|
40
|
+
const FILENAME = 'cno-base64.test.js';
|
|
41
|
+
const LOREM_BUFFER = Buffer.from( "Lorem ipsum dolor sit amet biam. ~~~\u00ff\u00ff" );
|
|
42
|
+
//## Errors
|
|
43
|
+
|
|
44
|
+
//# Global Variables
|
|
45
|
+
/**## Functions*/
|
|
46
|
+
|
|
47
|
+
TestNS.test( 'getBase64FromBuffer:throws', async function( t ){
|
|
48
|
+
t.diagnostic( t.name );
|
|
49
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
50
|
+
tm.addFunction( { name: 'defaultExport', func: DefaultExport.getBase64FromBuffer } );
|
|
51
|
+
tm.addFunction( { name: 'namespaceExport', func: NamespaceExport.getBase64FromBuffer } );
|
|
52
|
+
tm.addFunction( { name: 'namedExport', func: getBase64FromBuffer } );
|
|
53
|
+
var conditions = {
|
|
54
|
+
input_buffer_type: {
|
|
55
|
+
args: [
|
|
56
|
+
true
|
|
57
|
+
],
|
|
58
|
+
expected: {
|
|
59
|
+
instanceOf: TypeError,
|
|
60
|
+
code: 'ERR_INVALID_ARG_TYPE'
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
input_buffer_null: {
|
|
64
|
+
args: [
|
|
65
|
+
null
|
|
66
|
+
],
|
|
67
|
+
expected: {
|
|
68
|
+
instanceOf: TypeError,
|
|
69
|
+
code: 'ERR_INVALID_ARG_TYPE'
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
input_options_type: {
|
|
73
|
+
args: [
|
|
74
|
+
Buffer.from( [ 0 ] ),
|
|
75
|
+
true
|
|
76
|
+
],
|
|
77
|
+
expected: {
|
|
78
|
+
instanceOf: TypeError,
|
|
79
|
+
code: 'ERR_INVALID_ARG_TYPE'
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
tm.addOldConditions( conditions );
|
|
84
|
+
await tm.run();
|
|
85
|
+
} );
|
|
86
|
+
TestNS.test( 'getBase64FromBuffer:returns', async function( t ){
|
|
87
|
+
t.diagnostic( t.name );
|
|
88
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
89
|
+
tm.addFunction( { name: 'defaultExport', func: DefaultExport.getBase64FromBuffer } );
|
|
90
|
+
tm.addFunction( { name: 'namespaceExport', func: NamespaceExport.getBase64FromBuffer } );
|
|
91
|
+
tm.addFunction( { name: 'namedExport', func: getBase64FromBuffer } );
|
|
92
|
+
var conditions = {
|
|
93
|
+
input_buffer_default: {
|
|
94
|
+
args: [
|
|
95
|
+
LOREM_BUFFER
|
|
96
|
+
],
|
|
97
|
+
expected: 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5-w7_Dvw'
|
|
98
|
+
},
|
|
99
|
+
input_options_noop: {
|
|
100
|
+
args: [
|
|
101
|
+
LOREM_BUFFER,
|
|
102
|
+
{
|
|
103
|
+
noop: true
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
expected: ''
|
|
107
|
+
},
|
|
108
|
+
input_options_noDefaults: {
|
|
109
|
+
args: [
|
|
110
|
+
LOREM_BUFFER,
|
|
111
|
+
{
|
|
112
|
+
noDefaults: true
|
|
113
|
+
}
|
|
114
|
+
],
|
|
115
|
+
expected: 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5+w7/Dvw'
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var url_safe = false;
|
|
119
|
+
var padding = false;
|
|
120
|
+
var prefix = false;
|
|
121
|
+
var expected = '';
|
|
122
|
+
for( var i = 1; i <= 2**3; i++ ){
|
|
123
|
+
url_safe = !url_safe;
|
|
124
|
+
if( ( i % 2 ) === 0 ){
|
|
125
|
+
padding = !padding;
|
|
126
|
+
}
|
|
127
|
+
if( ( i % 4 ) === 0 ){
|
|
128
|
+
prefix = !prefix;
|
|
129
|
+
}
|
|
130
|
+
if( url_safe ){
|
|
131
|
+
expected = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5-w7_Dvw';
|
|
132
|
+
} else{
|
|
133
|
+
expected = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5+w7/Dvw';
|
|
134
|
+
}
|
|
135
|
+
if( padding ){
|
|
136
|
+
expected += '==';
|
|
137
|
+
}
|
|
138
|
+
if( prefix ){
|
|
139
|
+
if( url_safe && padding ){
|
|
140
|
+
expected = 'U'+expected;
|
|
141
|
+
} else if( url_safe ){
|
|
142
|
+
expected = 'u'+expected;
|
|
143
|
+
} else if( padding ){
|
|
144
|
+
expected = 'M'+expected;
|
|
145
|
+
} else{
|
|
146
|
+
expected = 'm'+expected;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
conditions['option_set_'+i] = {
|
|
150
|
+
args: [
|
|
151
|
+
LOREM_BUFFER,
|
|
152
|
+
{
|
|
153
|
+
urlSafe: url_safe,
|
|
154
|
+
padding: padding,
|
|
155
|
+
prefix: prefix
|
|
156
|
+
}
|
|
157
|
+
],
|
|
158
|
+
expected: expected
|
|
159
|
+
};
|
|
160
|
+
} //for i
|
|
161
|
+
tm.addOldConditions( conditions );
|
|
162
|
+
await tm.run();
|
|
163
|
+
} );
|
|
164
|
+
TestNS.test( 'getBufferFromBase64:throws', async function( t ){
|
|
165
|
+
t.diagnostic( t.name );
|
|
166
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
167
|
+
tm.addFunction( { name: 'defaultExport', func: DefaultExport.getBufferFromBase64 } );
|
|
168
|
+
tm.addFunction( { name: 'namespaceExport', func: NamespaceExport.getBufferFromBase64 } );
|
|
169
|
+
tm.addFunction( { name: 'namedExport', func: getBufferFromBase64 } );
|
|
170
|
+
var conditions = {
|
|
171
|
+
input_string_type: {
|
|
172
|
+
args: [
|
|
173
|
+
true
|
|
174
|
+
],
|
|
175
|
+
expected: {
|
|
176
|
+
instanceOf: TypeError,
|
|
177
|
+
code: 'ERR_INVALID_ARG_TYPE'
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
input_string_empty: {
|
|
181
|
+
args: [
|
|
182
|
+
''
|
|
183
|
+
],
|
|
184
|
+
expected: {
|
|
185
|
+
instanceOf: Error,
|
|
186
|
+
code: 'ERR_INVALID_ARG_VALUE'
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
input_options_type: {
|
|
190
|
+
args: [
|
|
191
|
+
'Unimportant.',
|
|
192
|
+
true
|
|
193
|
+
],
|
|
194
|
+
expected: {
|
|
195
|
+
instanceOf: TypeError,
|
|
196
|
+
code: 'ERR_INVALID_ARG_TYPE'
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
tm.addOldConditions( conditions );
|
|
201
|
+
await tm.run();
|
|
202
|
+
} );
|
|
203
|
+
TestNS.test( 'getBufferFromBase64:returns', async function( t ){
|
|
204
|
+
t.diagnostic( t.name );
|
|
205
|
+
var tm = new TestNS.TestMatrix( { testContext: t } );
|
|
206
|
+
tm.addFunction( { name: 'defaultExport', func: DefaultExport.getBufferFromBase64 } );
|
|
207
|
+
tm.addFunction( { name: 'namespaceExport', func: NamespaceExport.getBufferFromBase64 } );
|
|
208
|
+
tm.addFunction( { name: 'namedExport', func: getBufferFromBase64 } );
|
|
209
|
+
var conditions = {
|
|
210
|
+
input_string_default: {
|
|
211
|
+
args: [
|
|
212
|
+
'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5-w7_Dvw'
|
|
213
|
+
],
|
|
214
|
+
expected: LOREM_BUFFER
|
|
215
|
+
},
|
|
216
|
+
input_options_noOptions: {
|
|
217
|
+
args: [
|
|
218
|
+
'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5-w7_Dvw',
|
|
219
|
+
{
|
|
220
|
+
noop: true,
|
|
221
|
+
noDefaults: true
|
|
222
|
+
}
|
|
223
|
+
],
|
|
224
|
+
expected: null
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var url_safe = false;
|
|
228
|
+
var prefix = false;
|
|
229
|
+
var input_string = '';
|
|
230
|
+
for( var i = 1; i <= 2**2; i++ ){
|
|
231
|
+
url_safe = !url_safe;
|
|
232
|
+
if( ( i % 2 ) === 0 ){
|
|
233
|
+
prefix = !prefix;
|
|
234
|
+
}
|
|
235
|
+
if( url_safe ){
|
|
236
|
+
input_string = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5-w7_Dvw';
|
|
237
|
+
} else{
|
|
238
|
+
input_string = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQgYmlhbS4gfn5+w7/Dvw';
|
|
239
|
+
}
|
|
240
|
+
if( prefix ){
|
|
241
|
+
if( url_safe ){
|
|
242
|
+
input_string = 'u'+input_string;
|
|
243
|
+
} else{
|
|
244
|
+
input_string = 'm'+input_string;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
conditions['option_set_'+i] = {
|
|
248
|
+
args: [
|
|
249
|
+
input_string,
|
|
250
|
+
{
|
|
251
|
+
prefix: prefix
|
|
252
|
+
}
|
|
253
|
+
],
|
|
254
|
+
expected: LOREM_BUFFER
|
|
255
|
+
};
|
|
256
|
+
} //for i
|
|
257
|
+
tm.addOldConditions( conditions );
|
|
258
|
+
await tm.run();
|
|
259
|
+
} );
|
|
260
|
+
|
|
261
|
+
// cno-base64.test.js EOF
|
|
262
|
+
|
package/src/cno-bytes.js
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-bytes.js](src/cno-bytes.js)
|
|
4
|
+
> `cno-bytes`: micropackage for byte string, AOB and bitwise manipulations.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2024 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import deriveOptions from './cno-options.js';
|
|
34
|
+
import Base64 from './cno-base64.js';
|
|
35
|
+
import SHA256 from './cno-sha2-256.js';
|
|
36
|
+
//## Standard
|
|
37
|
+
//## External
|
|
38
|
+
//# Constants
|
|
39
|
+
const FILENAME = 'cno-bytes.js';
|
|
40
|
+
//## Errors
|
|
41
|
+
|
|
42
|
+
//# Global Variables
|
|
43
|
+
/**## Functions*/
|
|
44
|
+
/**
|
|
45
|
+
### getBase16FromBuffer
|
|
46
|
+
> Returns a Base16 string with optional URL-safe alphabet, padding, and MultiBase prefix.
|
|
47
|
+
|
|
48
|
+
#### Parametres
|
|
49
|
+
| name | type | description |
|
|
50
|
+
| --- | --- | --- |
|
|
51
|
+
| input_buffer | Buffer | The Node Buffer to convert to Base16. |
|
|
52
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
53
|
+
|
|
54
|
+
##### `options` Properties
|
|
55
|
+
| name | type | default | description |
|
|
56
|
+
| noop | boolean | false | Skip primary functionality. |
|
|
57
|
+
| noDefaults | boolean | false | Don't apply static default options. |
|
|
58
|
+
| noDynamic | boolean | false | Don't apply dynamic default options. |
|
|
59
|
+
| uppercase | boolean | true | Whether to encode the string using uppercase letters A-F. |
|
|
60
|
+
| space | boolean | false | Whether to add a space between each byte (every two digits). |
|
|
61
|
+
| prefix | boolean | false | True to prefix the returned string with the respective [MultiBase](https://github.com/multiformats/multibase) code. |
|
|
62
|
+
|
|
63
|
+
#### Returns
|
|
64
|
+
| type | description |
|
|
65
|
+
| --- | --- |
|
|
66
|
+
| string | The Base16-encoded string. |
|
|
67
|
+
|
|
68
|
+
#### Throws
|
|
69
|
+
| code | type | condition |
|
|
70
|
+
| --- | --- | --- |
|
|
71
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
72
|
+
|
|
73
|
+
#### History
|
|
74
|
+
| version | change |
|
|
75
|
+
| --- | --- |
|
|
76
|
+
| 0.0.1 | WIP |
|
|
77
|
+
*/
|
|
78
|
+
function getBase16FromBuffer( input_buffer, input_options = {} ){
|
|
79
|
+
const FUNCTION_NAME = 'getBase16FromBuffer';
|
|
80
|
+
const DEFAULT_OPTIONS = {
|
|
81
|
+
uppercase: true,
|
|
82
|
+
space: false,
|
|
83
|
+
prefix: false,
|
|
84
|
+
};//Variables
|
|
85
|
+
var arguments_array = Array.from(arguments);
|
|
86
|
+
var _return = '';
|
|
87
|
+
var return_error = null;
|
|
88
|
+
var options = {};
|
|
89
|
+
var base16_string = '';
|
|
90
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${arguments_array}`});
|
|
91
|
+
//Parametre checks
|
|
92
|
+
if( Buffer.isBuffer(input_buffer) === false ){
|
|
93
|
+
return_error = new TypeError('Param "input_buffer" is not of type Buffer.');
|
|
94
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
95
|
+
throw return_error;
|
|
96
|
+
}
|
|
97
|
+
if( typeof(input_options) !== 'object' ){
|
|
98
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
99
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
100
|
+
throw return_error;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Options
|
|
104
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
105
|
+
if( options.noop !== true ){
|
|
106
|
+
// Function
|
|
107
|
+
base16_string = input_buffer.toString( 'hex' );
|
|
108
|
+
if( options.uppercase === true ){
|
|
109
|
+
base16_string = base16_string.toUpperCase();
|
|
110
|
+
}
|
|
111
|
+
if( options.space === true ){
|
|
112
|
+
base16_string = base16_string.replaceAll( /([0-9A-Fa-f]{2})/g, "$1 " ).trimEnd();
|
|
113
|
+
}
|
|
114
|
+
if( options.prefix === true ){
|
|
115
|
+
if( options.uppercase === true ){
|
|
116
|
+
base16_string = 'F' + base16_string;
|
|
117
|
+
} else{
|
|
118
|
+
base16_string = 'f' + base16_string;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
_return = base16_string;
|
|
122
|
+
} // noop
|
|
123
|
+
//Return
|
|
124
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
125
|
+
return _return;
|
|
126
|
+
} // getBase16FromBuffer
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
### getBufferFromBase16
|
|
130
|
+
> Returns a Node Buffer for the given Base16-encoded string with automatic MultiBase prefix removal.
|
|
131
|
+
|
|
132
|
+
#### Parametres
|
|
133
|
+
| name | type | description |
|
|
134
|
+
| --- | --- | --- |
|
|
135
|
+
| input_string | object | The Base16-encoded string. |
|
|
136
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
137
|
+
|
|
138
|
+
##### `options` Properties
|
|
139
|
+
| name | type | default | description |
|
|
140
|
+
| space | boolean | false | When `true`, remove spaces from the input string before conversion. |
|
|
141
|
+
| prefix | boolean | false | Set to `true`, to remove a MultiBase prefix from the input string. |
|
|
142
|
+
|
|
143
|
+
#### Returns
|
|
144
|
+
| type | description |
|
|
145
|
+
| --- | --- |
|
|
146
|
+
| Buffer | A Node buffer of the data encoded in the given Base16 string. |
|
|
147
|
+
|
|
148
|
+
#### Throws
|
|
149
|
+
| code | type | condition |
|
|
150
|
+
| --- | --- | --- |
|
|
151
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
152
|
+
|
|
153
|
+
#### History
|
|
154
|
+
| version | change |
|
|
155
|
+
| --- | --- |
|
|
156
|
+
| 0.0.1 | WIP |
|
|
157
|
+
*/
|
|
158
|
+
function getBufferFromBase16( input_string, input_options = {} ){
|
|
159
|
+
const FUNCTION_NAME = 'getBufferFromBase16';
|
|
160
|
+
const DEFAULT_OPTIONS = {
|
|
161
|
+
space: false,
|
|
162
|
+
prefix: false, // Set to `true`, to remove a MultiBase prefix from the input string.
|
|
163
|
+
};
|
|
164
|
+
//Variables
|
|
165
|
+
var arguments_array = Array.from(arguments);
|
|
166
|
+
var _return = null;
|
|
167
|
+
var return_error = null;
|
|
168
|
+
var options = {};
|
|
169
|
+
var base16_string = '';
|
|
170
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${arguments_array}`});
|
|
171
|
+
//Parametre checks
|
|
172
|
+
if( typeof(input_string) !== 'string' ){
|
|
173
|
+
return_error = new TypeError('Param "input_string" is not of type object.');
|
|
174
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
175
|
+
throw return_error;
|
|
176
|
+
} else if( input_string == '' ){
|
|
177
|
+
return_error = new Error(`Param "input_string" is an empty string`);
|
|
178
|
+
return_error.code = 'ERR_INVALID_ARG_VALUE';
|
|
179
|
+
throw return_error;
|
|
180
|
+
}
|
|
181
|
+
if( typeof(input_options) !== 'object' ){
|
|
182
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
183
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
184
|
+
throw return_error;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Options
|
|
188
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
189
|
+
if( options.noop !== true ){
|
|
190
|
+
// Function
|
|
191
|
+
if( options.space === true ){
|
|
192
|
+
base16_string = input_string.replaceAll( ' ', '' );
|
|
193
|
+
} else{
|
|
194
|
+
base16_string = input_string;
|
|
195
|
+
}
|
|
196
|
+
if( options.prefix === true ){
|
|
197
|
+
base16_string = base16_string.slice( 1 );
|
|
198
|
+
}
|
|
199
|
+
try{
|
|
200
|
+
_return = Buffer.from( base16_string, 'hex' );
|
|
201
|
+
} catch(error) /* c8 ignore start */ {
|
|
202
|
+
return_error = new Error(`Buffer.from threw an error: ${error}`);
|
|
203
|
+
throw return_error;
|
|
204
|
+
} /* c8 ignore stop */
|
|
205
|
+
} // noop
|
|
206
|
+
//Return
|
|
207
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
208
|
+
return _return;
|
|
209
|
+
} // getBufferFromBase16
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
### deriveAOBStringFromSamplesArray
|
|
213
|
+
> Derives a single [array-of-bytes](https://en.wikipedia.org/wiki/Array_of_bytes)-style pattern string from an array of equal-length sample strings by comparing each character position across all samples: positions where every sample agrees keep that character, while positions where any sample differs are replaced with `'x'` \(the conventional AOB wildcard\).
|
|
214
|
+
|
|
215
|
+
#### Parametres
|
|
216
|
+
| name | type | description |
|
|
217
|
+
| --- | --- | --- |
|
|
218
|
+
| samples_array | Array | An array of strings to compare, position by position; only the length of the first sample is considered. |
|
|
219
|
+
|
|
220
|
+
#### Returns
|
|
221
|
+
| type | description |
|
|
222
|
+
| --- | --- |
|
|
223
|
+
| string | The derived pattern string, as long as `samples_array[0]`. |
|
|
224
|
+
|
|
225
|
+
#### Throws
|
|
226
|
+
| code | type | condition |
|
|
227
|
+
| --- | --- | --- |
|
|
228
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if `samples_array` is `null`, `undefined`, or not an `Array`. |
|
|
229
|
+
|
|
230
|
+
#### History
|
|
231
|
+
| version | change |
|
|
232
|
+
| --- | --- |
|
|
233
|
+
| 0.1.1 | Fixed the `for( const letter = 0; ... )` loop bug and added documentation. |
|
|
234
|
+
| 0.0.1 | WIP |
|
|
235
|
+
*/
|
|
236
|
+
function deriveAOBStringFromSamplesArray( samples_array ){
|
|
237
|
+
const FUNCTION_NAME = 'deriveAOBStringFromSamplesArray';
|
|
238
|
+
var _return = null;
|
|
239
|
+
var return_error = null;
|
|
240
|
+
// Variables
|
|
241
|
+
var difference_counter = 0;
|
|
242
|
+
var aob_string = '';
|
|
243
|
+
// Parametre checks
|
|
244
|
+
if( samples_array == null || Array.isArray(samples_array) === false ){
|
|
245
|
+
return_error = new TypeError('Error: param "samples_array" is either null or not an Array.');
|
|
246
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
247
|
+
throw return_error;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Function
|
|
251
|
+
for( let letter = 0; letter < samples_array[0].length; letter++ ){
|
|
252
|
+
difference_counter = 0;
|
|
253
|
+
for( let sample_index = 1; sample_index < samples_array.length; sample_index++ ){
|
|
254
|
+
if( samples_array[0][letter] !== samples_array[sample_index][letter] ){
|
|
255
|
+
difference_counter++;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if( difference_counter === 0 ){
|
|
259
|
+
aob_string += samples_array[0][letter];
|
|
260
|
+
} else{
|
|
261
|
+
aob_string += 'x';
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// Return
|
|
265
|
+
_return = aob_string;
|
|
266
|
+
return _return;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
### AOB
|
|
270
|
+
> Smart constructor for a string-like object for representing AOB strings.
|
|
271
|
+
|
|
272
|
+
#### Parametres
|
|
273
|
+
| name | type | description |
|
|
274
|
+
| --- | --- | --- |
|
|
275
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
276
|
+
|
|
277
|
+
##### `options` Properties
|
|
278
|
+
| name | type | default | description |
|
|
279
|
+
| --- | --- | --- | --- |
|
|
280
|
+
|
|
281
|
+
#### Returns
|
|
282
|
+
| type | description |
|
|
283
|
+
| --- | --- |
|
|
284
|
+
| AOB | A string-like object for representing AOBs. |
|
|
285
|
+
|
|
286
|
+
#### Throws
|
|
287
|
+
| code | type | condition |
|
|
288
|
+
| --- | --- | --- |
|
|
289
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
290
|
+
|
|
291
|
+
#### History
|
|
292
|
+
| version | change |
|
|
293
|
+
| --- | --- |
|
|
294
|
+
| 0.0.1 | WIP |
|
|
295
|
+
*/
|
|
296
|
+
function AOB( input_options = {} ){
|
|
297
|
+
if( !new.target ){
|
|
298
|
+
return new AOB( input_options );
|
|
299
|
+
}
|
|
300
|
+
// Constants
|
|
301
|
+
const FUNCTION_NAME = 'AOB';
|
|
302
|
+
const DEFAULT_OPTIONS = {
|
|
303
|
+
};// Variables
|
|
304
|
+
var _return = this;
|
|
305
|
+
var return_error = null;
|
|
306
|
+
// Public Properties
|
|
307
|
+
Object.defineProperties( this, {
|
|
308
|
+
name: {
|
|
309
|
+
value: '',
|
|
310
|
+
writable: true,
|
|
311
|
+
enumerable: true
|
|
312
|
+
},
|
|
313
|
+
string: {
|
|
314
|
+
value: '',
|
|
315
|
+
enumerable: true,
|
|
316
|
+
writable: true
|
|
317
|
+
},
|
|
318
|
+
inputLength: {
|
|
319
|
+
value: 0,
|
|
320
|
+
writable: true,
|
|
321
|
+
enumerable: true
|
|
322
|
+
},
|
|
323
|
+
outputLength: {
|
|
324
|
+
value: 0,
|
|
325
|
+
writable: true,
|
|
326
|
+
enumerable: true
|
|
327
|
+
},
|
|
328
|
+
samplesArray: {
|
|
329
|
+
value: [],
|
|
330
|
+
writable: true,
|
|
331
|
+
enumerable: true
|
|
332
|
+
}
|
|
333
|
+
} );
|
|
334
|
+
// Parametre checks
|
|
335
|
+
if( typeof(input_options) !== 'object' ){
|
|
336
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
337
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
338
|
+
throw return_error;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Options
|
|
342
|
+
var options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
343
|
+
// Function
|
|
344
|
+
// Return
|
|
345
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
346
|
+
return _return;
|
|
347
|
+
} // AOB
|
|
348
|
+
|
|
349
|
+
export * from './cno-base64.js';
|
|
350
|
+
export * from './cno-sha2-256.js';
|
|
351
|
+
|
|
352
|
+
const NAMESPACE = {};
|
|
353
|
+
Object.defineProperties( NAMESPACE, {
|
|
354
|
+
getBase16FromBuffer: {
|
|
355
|
+
value: getBase16FromBuffer,
|
|
356
|
+
enumerable: true
|
|
357
|
+
},
|
|
358
|
+
getBufferFromBase16: {
|
|
359
|
+
value: getBufferFromBase16,
|
|
360
|
+
enumerable: true
|
|
361
|
+
},
|
|
362
|
+
deriveAOBStringFromSamplesArray: {
|
|
363
|
+
value: deriveAOBStringFromSamplesArray,
|
|
364
|
+
enumerable: true
|
|
365
|
+
},
|
|
366
|
+
AOB: {
|
|
367
|
+
value: AOB,
|
|
368
|
+
enumerable: true
|
|
369
|
+
},
|
|
370
|
+
base64: {
|
|
371
|
+
value: Base64,
|
|
372
|
+
enumerable: true
|
|
373
|
+
},
|
|
374
|
+
sha256: {
|
|
375
|
+
value: SHA256,
|
|
376
|
+
enumerable: true
|
|
377
|
+
}
|
|
378
|
+
} );
|
|
379
|
+
|
|
380
|
+
export{ NAMESPACE as default, getBase16FromBuffer, getBufferFromBase16, deriveAOBStringFromSamplesArray, AOB, Base64 as base64, SHA256 as sha256 };
|
|
381
|
+
|
|
382
|
+
if( process.argv.length > 2 ){
|
|
383
|
+
var buf = getBufferFromBase16( process.argv[2] );
|
|
384
|
+
console.log( buf.toString() );
|
|
385
|
+
}
|
|
386
|
+
// cno-bytes.js EOF
|
|
387
|
+
|