@bytehide/secrets
v1.0.7
Published
A manager for secrets
Downloads
514
Readme
ByteHide Secrets
ByteHide Secrets is a lightweight library to securely manage environment secrets and tokens for your applications.
Features
- Securely initialize the secrets management system.
- Retrieve and set secrets with ease.
- Compatible with multiple environments.
Installation
Install the ByteHide Secrets library using npm or yarn:
npm install @bytehide/secrets
# or
yarn add @bytehide/secrets
Usage
Initialization
Before using the library, initialize it with your project token and environment:
import { Secrets } from '@bytehide/secrets';
Secrets.unsecureInitialize('<your-project-token>', '<environment>');
Retrieving a Secret
Use the get
method to retrieve a secret. The method returns a promise:
async function fetchSecret() {
const secretKey = 'my-secret-key';
const secret = await Secrets.get(secretKey);
if (secret) {
console.log(`Retrieved secret: ${secret}`);
} else {
console.error('Failed to retrieve the secret.');
}
}
fetchSecret();
Setting a Secret
You can set a new secret using the set
method:
async function storeSecret() {
const secretKey = 'my-secret-key';
const secretValue = 'my-secret-value';
const result = await Secrets.set({[secretKey]: secretValue});
if (result) {
console.log('Secret successfully stored.');
} else {
console.error('Failed to store the secret.');
}
}
storeSecret();
Example: Simulating a Database Connection
Here's an example of using get
to simulate a database connection:
export async function setupDB() {
Secrets.unsecureInitialize('<your-project-token>', 'production');
let dbIsReady = false;
let secret = '';
while (!dbIsReady) {
secret = await Secrets.get('db-connection-string');
if (secret) {
dbIsReady = true;
}
}
const setDB = (db) => {
console.log(`Database "${db}" is initialized with secret: ${secret}`);
};
setDB('myDatabase');
}
Setting Up Environment Variables
Node.js Projects
- Create a
.env
file in the root directory of your project. - Add your environment variables in the following format:
BYTEHIDE_SECRETS_ENVIRONMENT=production BYTEHIDE_SECRETS_TOKEN=your-project-token
- Ensure you have
dotenv
installed to load these variables automatically:npm install dotenv
- Your application will now have access to these variables through
process.env
.
Browser Projects (e.g., Vite)
For browser-based projects, you'll need to inject the environment variables during the build process.
Webpack Example
Add the following configuration to your Webpack setup:
new webpack.DefinePlugin({
'window.env': JSON.stringify({
BYTEHIDE_SECRETS_ENVIRONMENT: 'production',
BYTEHIDE_SECRETS_TOKEN: 'your-project-token',
}),
});
Vite Example
Modify your vite.config.js
to define the environment variables:
export default defineConfig({
define: {
'window.env': {
BYTEHIDE_SECRETS_ENVIRONMENT: 'production',
BYTEHIDE_SECRETS_TOKEN: 'your-project-token',
},
},
});
API Reference
Secrets.unsecureInitialize(token: string, environment: string)
Initializes the secrets management system.
token
: Your project token.environment
: The environment (e.g.,development
,production
).
Secrets.get(key: string): Promise
Retrieves the value of a secret.
key
: The key of the secret to retrieve.
Returns a promise resolving to the secret value.
Secrets.setSecrets(payload: Record<string, string>): Promise
Sets or updates a secret.
payload
: An object containing key-value pairs of secrets.
Returns a promise resolving to true
if successful.
Contributing
Contributions are welcome! If you have any feature requests or find issues, feel free to open an issue or submit a pull request.
License
MIT License
Happy coding but keep it safe with @bytehide/secrets
! 🛡️