• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / boxcli / 12977187857

26 Jan 2025 06:18PM UTC coverage: 85.226% (-1.5%) from 86.684%
12977187857

Pull #551

github

web-flow
Merge a07095182 into 2793efbad
Pull Request #551: feat!: BoxCLI major version 4.0.0

1213 of 1603 branches covered (75.67%)

Branch coverage included in aggregate %.

38 of 106 new or added lines in 2 files covered. (35.85%)

2 existing lines in 2 files now uncovered.

4319 of 4888 relevant lines covered (88.36%)

395.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.15
/src/token-cache.js
1
'use strict';
2

3
/* eslint-disable promise/prefer-await-to-callbacks,promise/catch-or-return,promise/prefer-await-to-then,promise/no-callback-in-promise */
4

5
const os = require('os');
9✔
6
const path = require('path');
9✔
7
const BoxCLIError = require('./cli-error');
9✔
8
const utils = require('./util');
9✔
9

10
/**
11
 * Cache interface used by the Node SDK to cache tokens to disk in the user's home directory
12
 */
13
class CLITokenCache {
14

15
        /**
16
         * @constructor
17
         * @param {string} environmentName The name of the active CLI environment
18
         */
19
        constructor(environmentName) {
20

21
                this.filePath = path.join(os.homedir(), '.box', `${environmentName}_token_cache.json`);
×
22
        }
23

24
        /**
25
         * Read tokens from disk
26
         * @param {Function} callback The callback to pass resulting token info to
27
         * @returns {void}
28
         */
29
        read(callback) {
30

31
                utils.readFileAsync(this.filePath, 'utf8')
×
32
                        .then(json => JSON.parse(json))
×
33
                // If file is not present or not valid JSON, treat that as empty (but available) cache
34
                        .catch(() => ({}))
×
35
                        .then(tokenInfo => callback(null, tokenInfo));
×
36
        }
37

38
        /**
39
         * Write tokens to disk
40
         * @param {Object} tokenInfo The token object to write
41
         * @param {Function} callback The callback to pass results to
42
         * @returns {void}
43
         */
44
        write(tokenInfo, callback) {
45

46
                let output = JSON.stringify(tokenInfo, null, 4);
×
47
                utils.writeFileAsync(this.filePath, output, 'utf8')
×
48
                // Pass success or error to the callback
49
                        .then(callback)
50
                        .catch(err => callback(new BoxCLIError('Failed to write to token cache', err)));
×
51
        }
52

53
        /**
54
         * Delete the cache file from disk
55
         * @param {Function} callback The callback to pass results to
56
         * @returns {void}
57
         */
58
        clear(callback) {
UNCOV
59
                utils.unlinkAsync(this.filePath)
×
60
                // Pass success or error to the callback
61
                        .then(callback)
62
                        .catch(err => callback(new BoxCLIError('Failed to delete token cache', err)));
×
63
        }
64

65
        /**
66
         * Write the token to disk, complatible with TS SDK
67
         * @param {AccessToken} token The token to write
68
         * @returns {Promise<undefined>} A promise resolving to undefined
69
         */
70
        store(token) {
71
                // eslint-disable-next-line promise/avoid-new
NEW
72
                return new Promise((resolve, reject) => {
×
NEW
73
                        const accquiredAtMS = (new Date()).getTime();
×
NEW
74
                        const tokenInfo = {
×
75
                                accessToken: token.accessToken,
76
                                accessTokenTTLMS: token.expiresIn * 1000,
77
                                refreshToken: token.refreshToken,
78
                                acquiredAtMS: accquiredAtMS
79
                        };
NEW
80
                        this.write(tokenInfo, err => {
×
NEW
81
                                if (err) {
×
NEW
82
                                        reject(err);
×
83
                                } else {
NEW
84
                                        resolve();
×
85
                                }
86
                        });
87
                });
88
        }
89

90
        /**
91
         * Read the token from disk, compatible with TS SDK
92
         * @returns {Promise<undefined | AccessToken>} A promise resolving to the token
93
         */
94
        get() {
95
                // eslint-disable-next-line promise/avoid-new
NEW
96
                return new Promise((resolve, reject) => {
×
NEW
97
                        this.read((err, tokenInfo) => {
×
NEW
98
                                if (err) {
×
NEW
99
                                        reject(err);
×
100
                                } else {
NEW
101
                                        resolve(tokenInfo.accessToken ? tokenInfo : undefined);
×
102
                                }
103
                        });
104
                });
105
        }
106
}
107

108
module.exports = CLITokenCache;
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc