cno-bedrock 0.0.5 → 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.
@@ -0,0 +1,331 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ # [cno-assert.test.js](src/cno-assert.test.js)
4
+ > Tests for [`cno-assert`](./cno-assert.js).
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: [![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](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 AssertNS, {
34
+ assertTruthy,
35
+ assertNumber,
36
+ assertLessThan,
37
+ assertGreaterThan,
38
+ assertSameType,
39
+ assertNonstrictNotEqual,
40
+ assertIsEmpty,
41
+ assertIsNullOrUndefined,
42
+ assertIsNullOrEmpty,
43
+ assertStrictlyNotEqual,
44
+ assertInstanceof,
45
+ assertExpectedError,
46
+ assertObjectsQuantitativelyEqual,
47
+ assertNullOrFunction,
48
+ assertIsMap,
49
+ deepStrictEqual
50
+ } from './cno-assert.js';
51
+ import TestNS from './cno-test-matrix.js';
52
+ //## Standard
53
+ //## External
54
+ //# Constants
55
+ const FILENAME = 'cno-assert.test.js';
56
+ //## Errors
57
+
58
+ //# Global Variables
59
+ /**## Functions*/
60
+ TestNS.test( 'module-shape', async function( t ){
61
+ t.diagnostic( t.name );
62
+ // The default export is a namespace object that also spreads node's strict assert.
63
+ deepStrictEqual( typeof(AssertNS), 'object' );
64
+ deepStrictEqual( AssertNS.assertTruthy, assertTruthy );
65
+ deepStrictEqual( AssertNS.assertIsMap, assertIsMap );
66
+ // The repo-specific asserts plus the re-exported node asserts are all present.
67
+ for( const name of [ 'assertTruthy', 'assertNumber', 'assertLessThan', 'assertGreaterThan', 'assertSameType', 'assertNonstrictNotEqual', 'assertIsEmpty', 'assertIsNullOrUndefined', 'assertIsNullOrEmpty', 'assertStrictlyNotEqual', 'assertInstanceof', 'assertExpectedError', 'assertObjectsQuantitativelyEqual', 'assertNullOrFunction', 'assertIsMap' ] ){
68
+ t.diagnostic( `${t.name}:${name}` );
69
+ deepStrictEqual( typeof(AssertNS[name]), 'function' );
70
+ }
71
+ deepStrictEqual( typeof(AssertNS.deepStrictEqual), 'function' );
72
+ deepStrictEqual( typeof(AssertNS.ok), 'function' );
73
+ } );
74
+ TestNS.test( 'assertTruthy', async function( t ){
75
+ t.diagnostic( t.name );
76
+ AssertNS.assertIsNullOrUndefined( assertTruthy( true ) );
77
+ AssertNS.assertIsNullOrUndefined( assertTruthy( 1 ) );
78
+ AssertNS.assertIsNullOrUndefined( assertTruthy( 'non-empty' ) );
79
+ AssertNS.throws(
80
+ () => assertTruthy( false ),
81
+ ( error ) => {
82
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
83
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_TRUTHY' );
84
+ return true;
85
+ }
86
+ );
87
+ AssertNS.throws( () => assertTruthy( 0 ), { code: 'ERR_ASSERTION_TRUTHY' } );
88
+ AssertNS.throws( () => assertTruthy( null ), { code: 'ERR_ASSERTION_TRUTHY' } );
89
+ AssertNS.throws( () => assertTruthy( undefined ), { code: 'ERR_ASSERTION_TRUTHY' } );
90
+ } );
91
+ TestNS.test( 'assertNumber', async function( t ){
92
+ t.diagnostic( t.name );
93
+ AssertNS.assertIsNullOrUndefined( assertNumber( 0 ) );
94
+ AssertNS.assertIsNullOrUndefined( assertNumber( -1.5 ) );
95
+ AssertNS.assertIsNullOrUndefined( assertNumber( Number.NaN ) );
96
+ AssertNS.throws(
97
+ () => assertNumber( '0' ),
98
+ ( error ) => {
99
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
100
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_NUMBER' );
101
+ return true;
102
+ }
103
+ );
104
+ AssertNS.throws( () => assertNumber( null ), { code: 'ERR_ASSERTION_NUMBER' } );
105
+ AssertNS.throws( () => assertNumber( true ), { code: 'ERR_ASSERTION_NUMBER' } );
106
+ } );
107
+ TestNS.test( 'assertLessThan', async function( t ){
108
+ t.diagnostic( t.name );
109
+ AssertNS.assertIsNullOrUndefined( assertLessThan( 1, 2 ) );
110
+ AssertNS.assertIsNullOrUndefined( assertLessThan( -1, 0 ) );
111
+ // Non-number actual propagates assertNumber's error.
112
+ AssertNS.throws( () => assertLessThan( '1', 2 ), { code: 'ERR_ASSERTION_NUMBER' } );
113
+ // Non-number expected propagates assertNumber's error.
114
+ AssertNS.throws( () => assertLessThan( 1, '2' ), { code: 'ERR_ASSERTION_NUMBER' } );
115
+ AssertNS.throws(
116
+ () => assertLessThan( 2, 1 ),
117
+ ( error ) => {
118
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
119
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_LESSTHAN' );
120
+ return true;
121
+ }
122
+ );
123
+ AssertNS.throws( () => assertLessThan( 1, 1 ), { code: 'ERR_ASSERTION_LESSTHAN' } );
124
+ } );
125
+ TestNS.test( 'assertGreaterThan', async function( t ){
126
+ t.diagnostic( t.name );
127
+ AssertNS.assertIsNullOrUndefined( assertGreaterThan( 2, 1 ) );
128
+ AssertNS.assertIsNullOrUndefined( assertGreaterThan( 0, -1 ) );
129
+ AssertNS.throws( () => assertGreaterThan( '2', 1 ), { code: 'ERR_ASSERTION_NUMBER' } );
130
+ AssertNS.throws( () => assertGreaterThan( 2, '1' ), { code: 'ERR_ASSERTION_NUMBER' } );
131
+ AssertNS.throws(
132
+ () => assertGreaterThan( 1, 2 ),
133
+ ( error ) => {
134
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
135
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_GREATERTHAN' );
136
+ return true;
137
+ }
138
+ );
139
+ AssertNS.throws( () => assertGreaterThan( 1, 1 ), { code: 'ERR_ASSERTION_GREATERTHAN' } );
140
+ } );
141
+ TestNS.test( 'assertSameType', async function( t ){
142
+ t.diagnostic( t.name );
143
+ AssertNS.assertIsNullOrUndefined( assertSameType( 1, 2 ) );
144
+ AssertNS.assertIsNullOrUndefined( assertSameType( 'a', 'b' ) );
145
+ AssertNS.assertIsNullOrUndefined( assertSameType( null, null ) );
146
+ AssertNS.throws(
147
+ () => assertSameType( 1, '1' ),
148
+ ( error ) => {
149
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
150
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_SAMETYPE' );
151
+ return true;
152
+ }
153
+ );
154
+ AssertNS.throws( () => assertSameType( null, undefined ), { code: 'ERR_ASSERTION_SAMETYPE' } );
155
+ } );
156
+ TestNS.test( 'assertNonstrictNotEqual', async function( t ){
157
+ t.diagnostic( t.name );
158
+ AssertNS.assertIsNullOrUndefined( assertNonstrictNotEqual( 1, 2 ) );
159
+ AssertNS.assertIsNullOrUndefined( assertNonstrictNotEqual( 'a', 'b' ) );
160
+ AssertNS.throws(
161
+ () => assertNonstrictNotEqual( 1, '1' ),
162
+ ( error ) => {
163
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
164
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_NONSTRICTNOTEQUAL' );
165
+ return true;
166
+ }
167
+ );
168
+ AssertNS.throws( () => assertNonstrictNotEqual( 0, false ), { code: 'ERR_ASSERTION_NONSTRICTNOTEQUAL' } );
169
+ } );
170
+ TestNS.test( 'assertIsEmpty', async function( t ){
171
+ t.diagnostic( t.name );
172
+ AssertNS.assertIsNullOrUndefined( assertIsEmpty( '' ) );
173
+ AssertNS.assertIsNullOrUndefined( assertIsEmpty( [] ) );
174
+ AssertNS.assertIsNullOrUndefined( assertIsEmpty( {} ) );
175
+ AssertNS.assertIsNullOrUndefined( assertIsEmpty( null ) );
176
+ AssertNS.throws(
177
+ () => assertIsEmpty( 'non-empty' ),
178
+ ( error ) => {
179
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
180
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_ISEMPTY' );
181
+ return true;
182
+ }
183
+ );
184
+ AssertNS.throws( () => assertIsEmpty( [ 1 ] ), { code: 'ERR_ASSERTION_ISEMPTY' } );
185
+ } );
186
+ TestNS.test( 'assertIsNullOrUndefined', async function( t ){
187
+ t.diagnostic( t.name );
188
+ AssertNS.assertIsNullOrUndefined( assertIsNullOrUndefined( null ) );
189
+ AssertNS.assertIsNullOrUndefined( assertIsNullOrUndefined( undefined ) );
190
+ AssertNS.throws(
191
+ () => assertIsNullOrUndefined( 0 ),
192
+ ( error ) => {
193
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
194
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_ISNULLORUNDEFINED' );
195
+ return true;
196
+ }
197
+ );
198
+ AssertNS.throws( () => assertIsNullOrUndefined( '' ), { code: 'ERR_ASSERTION_ISNULLORUNDEFINED' } );
199
+ AssertNS.throws( () => assertIsNullOrUndefined( false ), { code: 'ERR_ASSERTION_ISNULLORUNDEFINED' } );
200
+ } );
201
+ TestNS.test( 'assertIsNullOrEmpty', async function( t ){
202
+ t.diagnostic( t.name );
203
+ // Empty strings, null, and undefined all pass.
204
+ AssertNS.assertIsNullOrUndefined( assertIsNullOrEmpty( '' ) );
205
+ AssertNS.assertIsNullOrUndefined( assertIsNullOrEmpty( null ) );
206
+ AssertNS.assertIsNullOrUndefined( assertIsNullOrEmpty( undefined ) );
207
+ // A non-empty string fails via assertIsEmpty; its message is the string-specific one.
208
+ AssertNS.throws(
209
+ () => assertIsNullOrEmpty( 'non-empty' ),
210
+ ( error ) => {
211
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
212
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_ISNULLOREMPTY' );
213
+ AssertNS.assertInstanceof( error.cause, AssertNS.AssertionError );
214
+ return true;
215
+ }
216
+ );
217
+ // A non-string, non-null value fails via assertIsNullOrUndefined.
218
+ AssertNS.throws( () => assertIsNullOrEmpty( 0 ), { code: 'ERR_ASSERTION_ISNULLOREMPTY' } );
219
+ AssertNS.throws( () => assertIsNullOrEmpty( false ), { code: 'ERR_ASSERTION_ISNULLOREMPTY' } );
220
+ } );
221
+ TestNS.test( 'assertStrictlyNotEqual', async function( t ){
222
+ t.diagnostic( t.name );
223
+ // Different types and different values pass.
224
+ AssertNS.assertIsNullOrUndefined( assertStrictlyNotEqual( 1, 2 ) );
225
+ AssertNS.assertIsNullOrUndefined( assertStrictlyNotEqual( 'a', 'b' ) );
226
+ // Same type but equal value -> error path (message name/value overriding).
227
+ AssertNS.throws(
228
+ () => assertStrictlyNotEqual( 1, 1, 'myName', 'myValue', 'number' ),
229
+ ( error ) => {
230
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
231
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_STRICTLYNOTEQUAL' );
232
+ return true;
233
+ }
234
+ );
235
+ // Default name/value/expectedType fallbacks.
236
+ AssertNS.throws( () => assertStrictlyNotEqual( 1, 1 ), { code: 'ERR_ASSERTION_STRICTLYNOTEQUAL' } );
237
+ // Different types -> type-mismatch error path (uses name/value/expectedType overrides).
238
+ AssertNS.throws(
239
+ () => assertStrictlyNotEqual( 1, '1', 'myName', 'myValue', 'string' ),
240
+ ( error ) => {
241
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
242
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_STRICTLYNOTEQUAL' );
243
+ return true;
244
+ }
245
+ );
246
+ // Different types with default fallbacks.
247
+ AssertNS.throws( () => assertStrictlyNotEqual( 1, '1' ), { code: 'ERR_ASSERTION_STRICTLYNOTEQUAL' } );
248
+ } );
249
+ TestNS.test( 'assertInstanceof', async function( t ){
250
+ t.diagnostic( t.name );
251
+ AssertNS.assertIsNullOrUndefined( assertInstanceof( new TypeError('x'), TypeError ) );
252
+ AssertNS.assertIsNullOrUndefined( assertInstanceof( [], Array ) );
253
+ AssertNS.throws(
254
+ () => assertInstanceof( {}, Array ),
255
+ ( error ) => {
256
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
257
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_INSTANCEOF' );
258
+ return true;
259
+ }
260
+ );
261
+ AssertNS.throws( () => assertInstanceof( 'x', TypeError ), { code: 'ERR_ASSERTION_INSTANCEOF' } );
262
+ } );
263
+ TestNS.test( 'assertExpectedError', async function( t ){
264
+ t.diagnostic( t.name );
265
+ var error = new TypeError('bad');
266
+ error.code = 'ERR_INVALID_ARG_TYPE';
267
+ // Matches: type and code both match.
268
+ AssertNS.assertIsNullOrUndefined( assertExpectedError( error, { constructor: TypeError, code: 'ERR_INVALID_ARG_TYPE' } ) );
269
+ // Code mismatch.
270
+ AssertNS.throws(
271
+ () => assertExpectedError( error, { constructor: TypeError, code: 'ERR_INVALID_ARG_VALUE' } ),
272
+ ( e ) => {
273
+ AssertNS.assertInstanceof( e, AssertNS.AssertionError );
274
+ AssertNS.deepStrictEqual( e.code, 'ERR_ASSERTION_EXPECTEDERROR' );
275
+ return true;
276
+ }
277
+ );
278
+ // Type mismatch (constructor path with constructor_string override).
279
+ AssertNS.throws(
280
+ () => assertExpectedError( error, { constructor: RangeError, code: 'ERR_INVALID_ARG_TYPE', constructor_string: 'RangeError' } ),
281
+ { code: 'ERR_ASSERTION_EXPECTEDERROR' }
282
+ );
283
+ // Type mismatch uses default constructor_string fallback.
284
+ AssertNS.throws(
285
+ () => assertExpectedError( error, { constructor: RangeError, code: 'ERR_INVALID_ARG_TYPE' } ),
286
+ { code: 'ERR_ASSERTION_EXPECTEDERROR' }
287
+ );
288
+ } );
289
+ TestNS.test( 'assertObjectsQuantitativelyEqual', async function( t ){
290
+ t.diagnostic( t.name );
291
+ AssertNS.assertIsNullOrUndefined( assertObjectsQuantitativelyEqual( { a: 1, b: [ 2, 3 ] }, { a: 1, b: [ 2, 3 ] } ) );
292
+ AssertNS.throws(
293
+ () => assertObjectsQuantitativelyEqual( { a: 1 }, { a: 2 } ),
294
+ ( error ) => {
295
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
296
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_OBJECTSQUANTITAIVELYEQUAL' );
297
+ return true;
298
+ }
299
+ );
300
+ AssertNS.throws( () => assertObjectsQuantitativelyEqual( { a: 1 }, { b: 1 } ), { code: 'ERR_ASSERTION_OBJECTSQUANTITAIVELYEQUAL' } );
301
+ } );
302
+ TestNS.test( 'assertNullOrFunction', async function( t ){
303
+ t.diagnostic( t.name );
304
+ AssertNS.assertIsNullOrUndefined( assertNullOrFunction( null ) );
305
+ AssertNS.assertIsNullOrUndefined( assertNullOrFunction( undefined ) );
306
+ AssertNS.assertIsNullOrUndefined( assertNullOrFunction( () => {} ) );
307
+ AssertNS.throws(
308
+ () => assertNullOrFunction( {} ),
309
+ ( error ) => {
310
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
311
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_NULLORFUNCTION' );
312
+ return true;
313
+ }
314
+ );
315
+ AssertNS.throws( () => assertNullOrFunction( 0 ), { code: 'ERR_ASSERTION_NULLORFUNCTION' } );
316
+ } );
317
+ TestNS.test( 'assertIsMap', async function( t ){
318
+ t.diagnostic( t.name );
319
+ AssertNS.assertIsNullOrUndefined( assertIsMap( new Map() ) );
320
+ AssertNS.throws(
321
+ () => assertIsMap( {} ),
322
+ ( error ) => {
323
+ AssertNS.assertInstanceof( error, AssertNS.AssertionError );
324
+ AssertNS.deepStrictEqual( error.code, 'ERR_ASSERTION_ISMAP' );
325
+ return true;
326
+ }
327
+ );
328
+ AssertNS.throws( () => assertIsMap( null ), { code: 'ERR_ASSERTION_ISMAP' } );
329
+ } );
330
+
331
+ // cno-assert.test.js EOF
@@ -0,0 +1,230 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ # [cno-base64.js](src/cno-base64.js)
4
+ > cno-base64: Micropackage: convert between a NodeJS Buffer and a base64-encoded string with optional URL-safe alphabet, padding, and MultiBase prefix.
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: [![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](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
+ //## Standard
34
+ //## External
35
+ //# Constants
36
+ const FILENAME = 'cno-base64.js';
37
+ //## Errors
38
+
39
+ //# Global Variables
40
+ /**## Functions*/
41
+ /**
42
+ ### getBase64FromBuffer
43
+ > Returns a Base64 string with optional URL-safe alphabet, padding, and MultiBase prefix.
44
+
45
+ #### Parametres
46
+ | name | type | description |
47
+ | --- | --- | --- |
48
+ | input_buffer | Buffer | The Node Buffer to convert to Base64. |
49
+ | input_options | object | Run-time options. \[default: {}\] |
50
+
51
+ ##### `options` Properties
52
+ | name | type | default | description |
53
+ | noop | boolean | false | Skip primary functionality. |
54
+ | noDefaults | boolean | false | Don't apply static default options. |
55
+ | noDynamic | boolean | false | Don't apply dynamic default options. |
56
+ | urlSafe | boolean | true | True to use the [URL-Safe variant of Base64](https://datatracker.ietf.org/doc/html/rfc4648.html#section-5). |
57
+ | padding | boolean | false | True to include '=' padding characters. |
58
+ | prefix | boolean | false | True to prefix the returned string with the respective [MultiBase](https://github.com/multiformats/multibase) code. |
59
+
60
+ #### Returns
61
+ | type | description |
62
+ | --- | --- |
63
+ | string | The Base64-encoded string. |
64
+
65
+ #### Throws
66
+ | code | type | condition |
67
+ | --- | --- | --- |
68
+ | 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
69
+
70
+ #### History
71
+ | version | change |
72
+ | --- | --- |
73
+ | 0.0.1 | WIP |
74
+ */
75
+ function getBase64FromBuffer( input_buffer, input_options = {} ){
76
+ const FUNCTION_NAME = 'getBase64FromBuffer';
77
+ const DEFAULT_OPTIONS = {
78
+ noop: false, // Skip primary functionality.
79
+ noDefaults: false, // Don't apply static default options.
80
+ noDynamic: false, // Don't apply dynamic default options.
81
+ urlSafe: true, // True to use the [URL-Safe variant of Base64](https://datatracker.ietf.org/doc/html/rfc4648.html#section-5).
82
+ padding: false, // True to include '=' padding characters.
83
+ prefix: false, // True to prefix the returned string with the respective [MultiBase](https://github.com/multiformats/multibase) code.
84
+ };//Variables
85
+ var arguments_array = Array.from(arguments);
86
+ var _return = '';
87
+ var return_error = null;
88
+ var options = {};
89
+ var base64_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
+ if( input_options.noDefaults !== true ){
105
+ options = Object.assign( options, DEFAULT_OPTIONS, input_options );
106
+ } else{
107
+ options = Object.assign( options, input_options );
108
+ }
109
+ if( options.noop !== true ){
110
+ // Function
111
+ if( options.urlSafe === true && options.padding === false ){
112
+ base64_string = input_buffer.toString('base64url'); //Node's base64url already removes padding.
113
+ if( options.prefix === true ){
114
+ base64_string = 'u' + base64_string;
115
+ }
116
+ } else{
117
+ base64_string = input_buffer.toString('base64');
118
+ if( options.urlSafe === true ){ //options.padding must be true.
119
+ base64_string = base64_string.replaceAll( '+', '-' ).replaceAll( '/', '_' );
120
+ if( options.prefix === true ){
121
+ base64_string = 'U' + base64_string;
122
+ }
123
+ } else if( options.padding !== true ){ //Not URL but still need to remove padding.
124
+ base64_string = base64_string.replaceAll( '=', '' );
125
+ if( options.prefix === true ){
126
+ base64_string = 'm' + base64_string;
127
+ }
128
+ } else if( options.prefix === true ){ //base64_string already has what we need so all that's left is to possibly prepend the multiformat code.
129
+ base64_string = 'M' + base64_string;
130
+ }
131
+ }
132
+ _return = base64_string;
133
+ } // noop
134
+ //Return
135
+ this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
136
+ return _return;
137
+ } // getBase64FromBuffer
138
+ /**
139
+ ### getBufferFromBase64
140
+ > Returns a Node Buffer for the given Base64-encoded string with automatic MultiBase prefix removal.
141
+
142
+ #### Parametres
143
+ | name | type | description |
144
+ | --- | --- | --- |
145
+ | input_string | object | The Base64-encoded string. |
146
+ | input_options | object | Run-time options. \[default: {}\] |
147
+
148
+ ##### `options` Properties
149
+ | name | type | default | description |
150
+ | noop | boolean | false | Skip primary functionality. |
151
+ | noDefaults | boolean | false | Don't apply static default options. |
152
+ | noDynamic | boolean | false | Don't apply dynamic default options. |
153
+ | prefix | boolean | false | Set to `True`, to remove a MultiBase prefix from the input string. |
154
+
155
+ #### Returns
156
+ | type | description |
157
+ | --- | --- |
158
+ | Buffer | A Node buffer of the data encoded in the given Base64 string. |
159
+
160
+ #### Throws
161
+ | code | type | condition |
162
+ | --- | --- | --- |
163
+ | 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
164
+
165
+ #### History
166
+ | version | change |
167
+ | --- | --- |
168
+ | 0.0.1 | WIP |
169
+ */
170
+ function getBufferFromBase64( input_string, input_options = {} ){
171
+ const FUNCTION_NAME = 'getBufferFromBase64';
172
+ const DEFAULT_OPTIONS = {
173
+ noop: false, // Skip primary functionality.
174
+ noDefaults: false, // Don't apply static default options.
175
+ noDynamic: false, // Don't apply dynamic default options.
176
+ prefix: false, // Set to `true`, to remove a MultiBase prefix from the input string.
177
+ };
178
+ //Variables
179
+ var arguments_array = Array.from(arguments);
180
+ var _return = null;
181
+ var return_error = null;
182
+ var options = {};
183
+ var base64_string = '';
184
+ this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${arguments_array}`});
185
+ //Parametre checks
186
+ if( typeof(input_string) !== 'string' ){
187
+ return_error = new TypeError('Param "input_string" is not of type object.');
188
+ return_error.code = 'ERR_INVALID_ARG_TYPE';
189
+ throw return_error;
190
+ } else if( input_string == '' ){
191
+ return_error = new Error(`Param "input_string" is an empty string`);
192
+ return_error.code = 'ERR_INVALID_ARG_VALUE';
193
+ throw return_error;
194
+ }
195
+ if( typeof(input_options) !== 'object' ){
196
+ return_error = new TypeError('Param "input_options" is not of type object.');
197
+ return_error.code = 'ERR_INVALID_ARG_TYPE';
198
+ throw return_error;
199
+ }
200
+
201
+ // Options
202
+ if( input_options.noDefaults !== true ){
203
+ options = Object.assign( options, DEFAULT_OPTIONS, input_options );
204
+ } else{
205
+ options = Object.assign( options, input_options );
206
+ }
207
+ if( options.noop !== true ){
208
+ // Function
209
+ if( options.prefix === true ){
210
+ base64_string = input_string.slice( 1 );
211
+ } else{
212
+ base64_string = input_string;
213
+ }
214
+ try{
215
+ _return = Buffer.from( base64_string, 'base64' );
216
+ } catch(error) /* c8 ignore start */ {
217
+ return_error = new Error(`Buffer.from threw an error: ${error}`);
218
+ throw return_error;
219
+ } /* c8 ignore stop */
220
+ } // noop
221
+ //Return
222
+ this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
223
+ return _return;
224
+ } // getBufferFromBase64
225
+
226
+ const NAMESPACE = { getBufferFromBase64: getBufferFromBase64, getBase64FromBuffer: getBase64FromBuffer };
227
+ export { NAMESPACE as default, getBufferFromBase64, getBase64FromBuffer };
228
+
229
+ // cno-base64.js EOF
230
+