Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tech(config): set and get config from json #50

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"secret":"encrypt","algorithm":"AES"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

algorithm value not used?

Copy link
Collaborator Author

@shangan23 shangan23 Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. For now, created the config method and just used the secret. But this solution may not work. Since the config is based on a file on each config set the file will get overridden. So need to think of some alternate solution to maintain the config object instead of keeping the values in a file.

CONVERTED TO DRAFT PR

6 changes: 5 additions & 1 deletion src/decrypt.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const Url = require('url-parse')
const CryptoJS = require('crypto-js')
const fs = require('fs')
import { isUrl, isPatternUrl, isPattern, isPath } from '../util'

const configData = fs.readFileSync(__dirname + '/config.json', 'utf8')
const configObject = JSON.parse(configData)

const decrypt = (function () {
const secret = 'Secret Passphrase'
const secret = configObject['secret']
return {
url: function (givenUrl, options = {}) {
if (!givenUrl) throw new Error('url is missing')
Expand Down
14 changes: 13 additions & 1 deletion src/encrypt.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
const Url = require('url-parse')
const CryptoJS = require('crypto-js')
const fs = require('fs')
import { isUrl, isPatternUrl, isPatterObject, isPattern, isPath } from '../util'

const configData = fs.readFileSync(__dirname + '/config.json', 'utf8')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be template literal like below.

const configFile = 'config.json'
fs.readFileSync(`${__dirname}/${configFile}`, 'utf8')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be. Refer Comment

const configObject = JSON.parse(configData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.parse should be inside try / catch block. there is possibility of circular exception error. can be like below.

let moduleConfig = {}
try {
moduleConfig = JSON.parse(configData)
} catch (e) {
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will work on it.


const encrypt = (function () {
const secret = 'Secret Passphrase'
const secret = configObject.secret
return {
config: function (config = {}) {
if (Object.keys(config).length === 0)
throw new Error('options is missing')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move all error messages to errorMessages.js

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure will do that

for (const [key, value] of Object.entries(config)) {
configObject[key] = value
}
fs.writeFileSync(__dirname + '/config.json', JSON.stringify(configObject))
},
url: function (url, options = {}) {
if (!url) throw new Error('url is missing')

Expand Down