vestauth 0.1.13 → 0.1.18
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 +11 -0
- package/LICENSE +28 -0
- package/package.json +22 -6
- package/src/cli/actions/challenge.js +23 -0
- package/src/cli/actions/hash.js +26 -0
- package/src/cli/actions/keypair.js +24 -0
- package/src/cli/actions/sign.js +26 -0
- package/src/cli/actions/verify.js +28 -0
- package/src/cli/vestauth.js +101 -0
- package/src/lib/helpers/colorDepth.js +17 -0
- package/src/lib/helpers/errors.js +18 -0
- package/src/lib/helpers/getCommanderVersion.js +10 -0
- package/src/lib/helpers/hash.js +2 -2
- package/src/lib/helpers/packageJson.js +3 -0
- package/src/lib/helpers/sign.js +2 -7
- package/src/lib/helpers/truncate.js +10 -0
- package/src/shared/colors.js +54 -0
- package/src/shared/logger.js +145 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
[Unreleased](https://github.com/dotenvx/dotenvx-ops/compare/v0.1.0...main)
|
|
6
|
+
|
|
7
|
+
## [0.1.3](https://github.com/dotenvx/dotenvx-ops/compare/v0.1.0...v0.1.1) (2026-01-17)
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
* TBD
|
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Scott Motte
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vestauth",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.18",
|
|
4
|
+
"description": "auth for agents",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vestauth"
|
|
7
7
|
],
|
|
@@ -16,6 +16,16 @@
|
|
|
16
16
|
],
|
|
17
17
|
"license": "BSD-3-Clause",
|
|
18
18
|
"main": "src/lib/main.js",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"require": "./src/lib/main.js",
|
|
22
|
+
"default": "./src/lib/main.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"bin": {
|
|
27
|
+
"vestauth": "./src/cli/vestauth.js"
|
|
28
|
+
},
|
|
19
29
|
"author": "@motdotla",
|
|
20
30
|
"type": "commonjs",
|
|
21
31
|
"scripts": {
|
|
@@ -23,17 +33,23 @@
|
|
|
23
33
|
"standard:fix": "standard --fix",
|
|
24
34
|
"test": "tap run --allow-empty-coverage --disable-coverage --timeout=60000",
|
|
25
35
|
"test-coverage": "tap run --show-full-coverage --timeout=60000",
|
|
26
|
-
"
|
|
27
|
-
"prerelease": "npm test && npm run testshell",
|
|
36
|
+
"prerelease": "npm test",
|
|
28
37
|
"release": "standard-version"
|
|
29
38
|
},
|
|
30
39
|
"dependencies": {
|
|
31
40
|
"@noble/hashes": "^1.8.0",
|
|
32
|
-
"@noble/secp256k1": "^
|
|
41
|
+
"@noble/secp256k1": "^1.7.2",
|
|
42
|
+
"commander": "^11.1.0",
|
|
33
43
|
"eciesjs": "^0.4.16"
|
|
34
44
|
},
|
|
35
45
|
"devDependencies": {
|
|
46
|
+
"@yao-pkg/pkg": "^5.14.2",
|
|
47
|
+
"capture-console": "^1.0.2",
|
|
48
|
+
"esbuild": "^0.25.8",
|
|
49
|
+
"proxyquire": "^2.1.3",
|
|
50
|
+
"sinon": "^14.0.1",
|
|
36
51
|
"standard": "^17.1.0",
|
|
37
|
-
"standard-version": "^9.5.0"
|
|
52
|
+
"standard-version": "^9.5.0",
|
|
53
|
+
"tap": "^21.0.1"
|
|
38
54
|
}
|
|
39
55
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const { logger } = require('./../../shared/logger')
|
|
2
|
+
|
|
3
|
+
const main = require('./../../lib/main')
|
|
4
|
+
|
|
5
|
+
function challenge () {
|
|
6
|
+
const options = this.opts()
|
|
7
|
+
logger.debug(`options: ${JSON.stringify(options)}`)
|
|
8
|
+
|
|
9
|
+
const chal = main.challenge()
|
|
10
|
+
|
|
11
|
+
const output = {
|
|
12
|
+
callenge: chal
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let space = 0
|
|
16
|
+
if (options.prettyPrint) {
|
|
17
|
+
space = 2
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
console.log(JSON.stringify(output, null, space))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = challenge
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { logger } = require('./../../shared/logger')
|
|
2
|
+
|
|
3
|
+
const main = require('./../../lib/main')
|
|
4
|
+
|
|
5
|
+
function hash (message) {
|
|
6
|
+
logger.debug(`message: ${message}`)
|
|
7
|
+
|
|
8
|
+
const options = this.opts()
|
|
9
|
+
logger.debug(`options: ${JSON.stringify(options)}`)
|
|
10
|
+
|
|
11
|
+
const hashMessage = main.hash(message)
|
|
12
|
+
const hashBase64Url = Buffer.from(hashMessage).toString('base64url')
|
|
13
|
+
|
|
14
|
+
const output = {
|
|
15
|
+
hash: hashBase64Url
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let space = 0
|
|
19
|
+
if (options.prettyPrint) {
|
|
20
|
+
space = 2
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log(JSON.stringify(output, null, space))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = hash
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const { logger } = require('./../../shared/logger')
|
|
2
|
+
|
|
3
|
+
const main = require('./../../lib/main')
|
|
4
|
+
|
|
5
|
+
function keypair (existingPrivateKey) {
|
|
6
|
+
const options = this.opts()
|
|
7
|
+
logger.debug(`options: ${JSON.stringify(options)}`)
|
|
8
|
+
|
|
9
|
+
const kp = main.keypair(existingPrivateKey)
|
|
10
|
+
|
|
11
|
+
const output = {
|
|
12
|
+
public_key: kp.publicKey,
|
|
13
|
+
private_key: kp.privateKey
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let space = 0
|
|
17
|
+
if (options.prettyPrint) {
|
|
18
|
+
space = 2
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
console.log(JSON.stringify(output, null, space))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = keypair
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { logger } = require('./../../shared/logger')
|
|
2
|
+
|
|
3
|
+
const main = require('./../../lib/main')
|
|
4
|
+
|
|
5
|
+
async function sign (challenge, privateKeyHex) {
|
|
6
|
+
logger.debug(`challenge: ${challenge}`)
|
|
7
|
+
logger.debug(`privateKey: ${privateKeyHex}`)
|
|
8
|
+
|
|
9
|
+
const options = this.opts()
|
|
10
|
+
logger.debug(`options: ${JSON.stringify(options)}`)
|
|
11
|
+
|
|
12
|
+
const signature = await main.sign(challenge, privateKeyHex)
|
|
13
|
+
|
|
14
|
+
const output = {
|
|
15
|
+
signature
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let space = 0
|
|
19
|
+
if (options.prettyPrint) {
|
|
20
|
+
space = 2
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log(JSON.stringify(output, null, space))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = sign
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const { logger } = require('./../../shared/logger')
|
|
2
|
+
|
|
3
|
+
const main = require('./../../lib/main')
|
|
4
|
+
|
|
5
|
+
async function verify (challenge, signatureBase64, publicKeyHex) {
|
|
6
|
+
logger.debug(`challenge: ${challenge}`)
|
|
7
|
+
logger.debug(`signature: ${signatureBase64}`)
|
|
8
|
+
logger.debug(`publicKey: ${publicKeyHex}`)
|
|
9
|
+
|
|
10
|
+
const options = this.opts()
|
|
11
|
+
logger.debug(`options: ${JSON.stringify(options)}`)
|
|
12
|
+
|
|
13
|
+
const success = main.verify(challenge, signatureBase64, publicKeyHex)
|
|
14
|
+
|
|
15
|
+
const output = {
|
|
16
|
+
success,
|
|
17
|
+
public_key: publicKeyHex
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let space = 0
|
|
21
|
+
if (options.prettyPrint) {
|
|
22
|
+
space = 2
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
console.log(JSON.stringify(output, null, space))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = verify
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/* c8 ignore start */
|
|
4
|
+
const { Command } = require('commander')
|
|
5
|
+
const program = new Command()
|
|
6
|
+
|
|
7
|
+
const { setLogLevel, logger } = require('../shared/logger')
|
|
8
|
+
const packageJson = require('./../lib/helpers/packageJson')
|
|
9
|
+
const Errors = require('./../lib/helpers/errors')
|
|
10
|
+
const getCommanderVersion = require('./../lib/helpers/getCommanderVersion')
|
|
11
|
+
|
|
12
|
+
// surface hoisting problems
|
|
13
|
+
const commanderVersion = getCommanderVersion()
|
|
14
|
+
if (commanderVersion && parseInt(commanderVersion.split('.')[0], 10) >= 12) {
|
|
15
|
+
const message = `vestauth depends on commander@11.x.x but you are attempting to hoist commander@${commanderVersion}`
|
|
16
|
+
const error = new Errors({ message }).dangerousDependencyHoist()
|
|
17
|
+
logger.error(error.message)
|
|
18
|
+
if (error.help) logger.error(error.help)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// global log levels
|
|
22
|
+
program
|
|
23
|
+
.usage('vestauth')
|
|
24
|
+
.option('-l, --log-level <level>', 'set log level', 'info')
|
|
25
|
+
.option('-q, --quiet', 'sets log level to error')
|
|
26
|
+
.option('-v, --verbose', 'sets log level to verbose')
|
|
27
|
+
.option('-d, --debug', 'sets log level to debug')
|
|
28
|
+
.hook('preAction', (thisCommand, actionCommand) => {
|
|
29
|
+
const options = thisCommand.opts()
|
|
30
|
+
|
|
31
|
+
setLogLevel(options)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
// cli
|
|
35
|
+
program
|
|
36
|
+
.name('vestauth')
|
|
37
|
+
.description(packageJson.description)
|
|
38
|
+
.version(packageJson.version)
|
|
39
|
+
.allowUnknownOption()
|
|
40
|
+
|
|
41
|
+
// vestauth challenge
|
|
42
|
+
const challengeAction = require('./actions/challenge')
|
|
43
|
+
program.command('challenge')
|
|
44
|
+
.description('generate challenge')
|
|
45
|
+
.option('-pp, --pretty-print', 'pretty print output')
|
|
46
|
+
.action(challengeAction)
|
|
47
|
+
|
|
48
|
+
// vestauth hash [message]
|
|
49
|
+
const hashAction = require('./actions/hash')
|
|
50
|
+
program.command('hash')
|
|
51
|
+
.description('hash message')
|
|
52
|
+
.argument('<message>', 'message string')
|
|
53
|
+
.option('-pp, --pretty-print', 'pretty print output')
|
|
54
|
+
.action(hashAction)
|
|
55
|
+
|
|
56
|
+
// vestauth keypair
|
|
57
|
+
const keypairAction = require('./actions/keypair')
|
|
58
|
+
program.command('keypair')
|
|
59
|
+
.description('generate public/private keypair')
|
|
60
|
+
.argument('[private_key]', 'pre-existing private key')
|
|
61
|
+
.option('-pp, --pretty-print', 'pretty print output')
|
|
62
|
+
.action(keypairAction)
|
|
63
|
+
|
|
64
|
+
// vestauth sign
|
|
65
|
+
const signAction = require('./actions/sign')
|
|
66
|
+
program.command('sign')
|
|
67
|
+
.description('sign challenge')
|
|
68
|
+
.argument('<challenge>', 'challenge (base64url)')
|
|
69
|
+
.argument('<privateKey>', 'private key (hex)')
|
|
70
|
+
.option('-pp, --pretty-print', 'pretty print output')
|
|
71
|
+
.action(signAction)
|
|
72
|
+
|
|
73
|
+
// vestauth verify
|
|
74
|
+
const verifyAction = require('./actions/verify')
|
|
75
|
+
program.command('verify')
|
|
76
|
+
.description('verify signature')
|
|
77
|
+
.argument('<challenge>', 'challenge (base64url)')
|
|
78
|
+
.argument('<signature>', 'signature (base64url)')
|
|
79
|
+
.argument('<publicKey>', 'public key (hex)')
|
|
80
|
+
.option('-pp, --pretty-print', 'pretty print output')
|
|
81
|
+
.action(verifyAction)
|
|
82
|
+
|
|
83
|
+
// vestauth help
|
|
84
|
+
program.command('help [command]')
|
|
85
|
+
.description('display help for command')
|
|
86
|
+
.action((command) => {
|
|
87
|
+
if (command) {
|
|
88
|
+
const subCommand = program.commands.find(c => c.name() === command)
|
|
89
|
+
if (subCommand) {
|
|
90
|
+
subCommand.outputHelp()
|
|
91
|
+
} else {
|
|
92
|
+
program.outputHelp()
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
program.outputHelp()
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
/* c8 ignore stop */
|
|
100
|
+
|
|
101
|
+
program.parse(process.argv)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const { WriteStream } = require('tty')
|
|
2
|
+
|
|
3
|
+
const getColorDepth = () => {
|
|
4
|
+
try {
|
|
5
|
+
return WriteStream.prototype.getColorDepth()
|
|
6
|
+
} catch (error) {
|
|
7
|
+
const term = process.env.TERM
|
|
8
|
+
|
|
9
|
+
if (term && (term.includes('256color') || term.includes('xterm'))) {
|
|
10
|
+
return 8 // 256 colors
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return 4
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { getColorDepth }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
class Errors {
|
|
2
|
+
constructor (options = {}) {
|
|
3
|
+
this.message = options.message
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
dangerousDependencyHoist () {
|
|
7
|
+
const code = 'DANGEROUS_DEPENDENCY_HOIST'
|
|
8
|
+
const message = `[${code}] your environment has hoisted an incompatible version of a vestauth dependency: ${this.message}`
|
|
9
|
+
const help = `[${code}] https://github.com/vestauth/vestauth`
|
|
10
|
+
|
|
11
|
+
const e = new Error(message)
|
|
12
|
+
e.code = code
|
|
13
|
+
e.help = help
|
|
14
|
+
return e
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = Errors
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
|
|
4
|
+
function getCommanderVersion () {
|
|
5
|
+
const commanderMain = require.resolve('commander')
|
|
6
|
+
const pkgPath = path.join(commanderMain, '..', 'package.json')
|
|
7
|
+
return JSON.parse(fs.readFileSync(pkgPath, 'utf8')).version
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
module.exports = getCommanderVersion
|
package/src/lib/helpers/hash.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const { sha256 } = require('@noble/hashes/sha2.js')
|
|
2
2
|
const { utf8ToBytes } = require('@noble/hashes/utils.js')
|
|
3
3
|
|
|
4
|
-
function hash (
|
|
5
|
-
return sha256(utf8ToBytes(
|
|
4
|
+
function hash (message = '') {
|
|
5
|
+
return sha256(utf8ToBytes(message))
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
module.exports = hash
|
package/src/lib/helpers/sign.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
// import { sign, verify, getPublicKey } from '@noble/secp256k1'
|
|
2
1
|
const secp = require('@noble/secp256k1')
|
|
3
|
-
const { sha256 } = require('@noble/hashes/sha2.js')
|
|
4
|
-
const { hmac } = require('@noble/hashes/hmac.js')
|
|
5
|
-
secp.hashes.sha256 = sha256
|
|
6
|
-
secp.hashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg)
|
|
7
2
|
const hash = require('./hash')
|
|
8
3
|
|
|
9
|
-
function sign (challenge, privateKeyHex) {
|
|
4
|
+
async function sign (challenge, privateKeyHex) {
|
|
10
5
|
const hashChallenge = hash(challenge)
|
|
11
6
|
const privateKeyBytes = Buffer.from(privateKeyHex, 'hex')
|
|
12
|
-
const signature = secp.sign(hashChallenge, privateKeyBytes)
|
|
7
|
+
const signature = await secp.sign(hashChallenge, privateKeyBytes)
|
|
13
8
|
|
|
14
9
|
return Buffer.from(signature).toString('base64url') // base64 returned
|
|
15
10
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const depth = require('../lib/helpers/colorDepth')
|
|
2
|
+
|
|
3
|
+
const colors16 = new Map([
|
|
4
|
+
['blue', 34],
|
|
5
|
+
['gray', 37],
|
|
6
|
+
['green', 32],
|
|
7
|
+
['olive', 33],
|
|
8
|
+
['orangered', 31], // mapped to red
|
|
9
|
+
['plum', 35], // mapped to magenta
|
|
10
|
+
['red', 31],
|
|
11
|
+
['electricblue', 36],
|
|
12
|
+
['dodgerblue', 36]
|
|
13
|
+
])
|
|
14
|
+
|
|
15
|
+
const colors256 = new Map([
|
|
16
|
+
['blue', 21],
|
|
17
|
+
['gray', 244],
|
|
18
|
+
['green', 34],
|
|
19
|
+
['olive', 142],
|
|
20
|
+
['orangered', 202],
|
|
21
|
+
['plum', 182],
|
|
22
|
+
['red', 196],
|
|
23
|
+
['electricblue', 45],
|
|
24
|
+
['dodgerblue', 33]
|
|
25
|
+
])
|
|
26
|
+
|
|
27
|
+
function getColor (color) {
|
|
28
|
+
const colorDepth = depth.getColorDepth()
|
|
29
|
+
if (!colors256.has(color)) {
|
|
30
|
+
throw new Error(`Invalid color ${color}`)
|
|
31
|
+
}
|
|
32
|
+
if (colorDepth >= 8) {
|
|
33
|
+
const code = colors256.get(color)
|
|
34
|
+
return (message) => `\x1b[38;5;${code}m${message}\x1b[39m`
|
|
35
|
+
}
|
|
36
|
+
if (colorDepth >= 4) {
|
|
37
|
+
const code = colors16.get(color)
|
|
38
|
+
return (message) => `\x1b[${code}m${message}\x1b[39m`
|
|
39
|
+
}
|
|
40
|
+
return (message) => message
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function bold (message) {
|
|
44
|
+
if (depth.getColorDepth() >= 4) {
|
|
45
|
+
return `\x1b[1m${message}\x1b[22m`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return message
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = {
|
|
52
|
+
getColor,
|
|
53
|
+
bold
|
|
54
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const packageJson = require('../lib/helpers/packageJson')
|
|
2
|
+
const { getColor, bold } = require('./colors')
|
|
3
|
+
|
|
4
|
+
const levels = {
|
|
5
|
+
error: 0,
|
|
6
|
+
warn: 1,
|
|
7
|
+
success: 2,
|
|
8
|
+
successv: 2,
|
|
9
|
+
info: 2,
|
|
10
|
+
help: 2,
|
|
11
|
+
verbose: 4,
|
|
12
|
+
debug: 5,
|
|
13
|
+
silly: 6
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const error = (m) => bold(getColor('red')(m))
|
|
17
|
+
const warn = getColor('orangered')
|
|
18
|
+
const success = getColor('green')
|
|
19
|
+
const successv = getColor('olive') // yellow-ish tint that 'looks' like dotenv
|
|
20
|
+
const help = getColor('dodgerblue')
|
|
21
|
+
const verbose = getColor('plum')
|
|
22
|
+
const debug = getColor('plum')
|
|
23
|
+
|
|
24
|
+
let currentLevel = levels.info // default log level
|
|
25
|
+
let currentName = 'vestauth' // default logger name
|
|
26
|
+
let currentVersion = packageJson.version // default logger version
|
|
27
|
+
|
|
28
|
+
function stderr (level, message) {
|
|
29
|
+
const formattedMessage = formatMessage(level, message)
|
|
30
|
+
console.error(formattedMessage)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function stdout (level, message) {
|
|
34
|
+
if (levels[level] === undefined) {
|
|
35
|
+
throw new Error(`MISSING_LOG_LEVEL: '${level}'. implement in logger.`)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (levels[level] <= currentLevel) {
|
|
39
|
+
const formattedMessage = formatMessage(level, message)
|
|
40
|
+
console.log(formattedMessage)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function formatMessage (level, message) {
|
|
45
|
+
const formattedMessage = typeof message === 'object' ? JSON.stringify(message) : message
|
|
46
|
+
|
|
47
|
+
switch (level.toLowerCase()) {
|
|
48
|
+
// errors
|
|
49
|
+
case 'error':
|
|
50
|
+
return error(formattedMessage)
|
|
51
|
+
// warns
|
|
52
|
+
case 'warn':
|
|
53
|
+
return warn(formattedMessage)
|
|
54
|
+
// successes
|
|
55
|
+
case 'success':
|
|
56
|
+
return success(formattedMessage)
|
|
57
|
+
case 'successv': // success with 'version'
|
|
58
|
+
return successv(`[${currentName}@${currentVersion}] ${formattedMessage}`)
|
|
59
|
+
// info
|
|
60
|
+
case 'info':
|
|
61
|
+
return formattedMessage
|
|
62
|
+
// help
|
|
63
|
+
case 'help':
|
|
64
|
+
return help(formattedMessage)
|
|
65
|
+
// verbose
|
|
66
|
+
case 'verbose':
|
|
67
|
+
return verbose(formattedMessage)
|
|
68
|
+
// debug
|
|
69
|
+
case 'debug':
|
|
70
|
+
return debug(formattedMessage)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const logger = {
|
|
75
|
+
// track level
|
|
76
|
+
level: 'info',
|
|
77
|
+
|
|
78
|
+
// errors
|
|
79
|
+
error: (msg) => stderr('error', msg),
|
|
80
|
+
// warns
|
|
81
|
+
warn: (msg) => stdout('warn', msg),
|
|
82
|
+
// success
|
|
83
|
+
success: (msg) => stdout('success', msg),
|
|
84
|
+
successv: (msg) => stdout('successv', msg),
|
|
85
|
+
// info
|
|
86
|
+
info: (msg) => stdout('info', msg),
|
|
87
|
+
// help
|
|
88
|
+
help: (msg) => stdout('help', msg),
|
|
89
|
+
// verbose
|
|
90
|
+
verbose: (msg) => stdout('verbose', msg),
|
|
91
|
+
// debug
|
|
92
|
+
debug: (msg) => stdout('debug', msg),
|
|
93
|
+
setLevel: (level) => {
|
|
94
|
+
if (levels[level] !== undefined) {
|
|
95
|
+
currentLevel = levels[level]
|
|
96
|
+
logger.level = level
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
setName: (name) => {
|
|
100
|
+
currentName = name
|
|
101
|
+
logger.name = name
|
|
102
|
+
},
|
|
103
|
+
setVersion: (version) => {
|
|
104
|
+
currentVersion = version
|
|
105
|
+
logger.version = version
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function setLogLevel (options) {
|
|
110
|
+
const logLevel = options.debug
|
|
111
|
+
? 'debug'
|
|
112
|
+
: options.verbose
|
|
113
|
+
? 'verbose'
|
|
114
|
+
: options.quiet
|
|
115
|
+
? 'error'
|
|
116
|
+
: options.logLevel
|
|
117
|
+
|
|
118
|
+
if (!logLevel) return
|
|
119
|
+
logger.setLevel(logLevel)
|
|
120
|
+
// Only log which level it's setting if it's not set to quiet mode
|
|
121
|
+
if (!options.quiet || (options.quiet && logLevel !== 'error')) {
|
|
122
|
+
logger.debug(`Setting log level to ${logLevel}`)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function setLogName (options) {
|
|
127
|
+
const logName = options.logName
|
|
128
|
+
if (!logName) return
|
|
129
|
+
logger.setName(logName)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function setLogVersion (options) {
|
|
133
|
+
const logVersion = options.logVersion
|
|
134
|
+
if (!logVersion) return
|
|
135
|
+
logger.setVersion(logVersion)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
module.exports = {
|
|
139
|
+
logger,
|
|
140
|
+
getColor,
|
|
141
|
+
setLogLevel,
|
|
142
|
+
setLogName,
|
|
143
|
+
setLogVersion,
|
|
144
|
+
levels
|
|
145
|
+
}
|