The tmp package with promises support and disposers.
93
Build a secure credential manager that stores sensitive user credentials in temporary files with proper security controls.
Credential Storage: Create a function storeCredentials(username, password, apiKey) that:
Credential Retrieval: Create a function retrieveCredentials(filePath) that:
Secure Processing: Create a function processCredentialsSecurely(username, password, apiKey, processFn) that:
processFn with the file pathprocessFn throws an errorImplement the following test cases in credential-manager.test.js:
// @test: Verify secure file permissions are set
const { storeCredentials } = require('./credential-manager');
const fs = require('fs');
async function testSecurePermissions() {
const { path, cleanup } = await storeCredentials('user1', 'pass123', 'key-abc');
const stats = fs.statSync(path);
const mode = stats.mode & 0o777;
console.assert(mode === 0o600, 'Expected file permissions to be 0600 (owner-only)');
await cleanup();
console.log('Test passed: Secure permissions verified');
}
testSecurePermissions().catch(console.error);// @test: Verify automatic cleanup after secure processing
const { processCredentialsSecurely } = require('./credential-manager');
const fs = require('fs');
async function testAutoCleanup() {
let tempPath;
await processCredentialsSecurely('user1', 'pass123', 'key-abc', (path) => {
tempPath = path;
console.assert(fs.existsSync(path), 'File should exist during processing');
});
console.assert(!fs.existsSync(tempPath), 'File should be cleaned up after processing');
console.log('Test passed: Automatic cleanup verified');
}
testAutoCleanup().catch(console.error);// @test: Verify cleanup happens even on error
const { processCredentialsSecurely } = require('./credential-manager');
const fs = require('fs');
async function testCleanupOnError() {
let tempPath;
try {
await processCredentialsSecurely('user1', 'pass123', 'key-abc', (path) => {
tempPath = path;
throw new Error('Simulated processing error');
});
} catch (err) {
// Expected error
}
console.assert(!fs.existsSync(tempPath), 'File should be cleaned up even after error');
console.log('Test passed: Cleanup on error verified');
}
testCleanupOnError().catch(console.error);credential-manager.js with the three required functionsProvides promise-based temporary file creation with automatic cleanup support.
Install with Tessl CLI
npx tessl i tessl/npm-tmp-promisedocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10