Skip to content

Commit ee15edd

Browse files
LaurensRietveldbhstahl
authored andcommitted
Replaced console.[log|info|warn|error] statements with debug statements
1 parent 7a9e2aa commit ee15edd

File tree

9 files changed

+71
-35
lines changed

9 files changed

+71
-35
lines changed

lib/Server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const OptionsHandler = require('./handlers/OptionsHandler');
1515
const PatchHandler = require('./handlers/PatchHandler');
1616
const PostHandler = require('./handlers/PostHandler');
1717
const RequestValidator = require('./validators/RequestValidator');
18-
1918
const EXPOSED_HEADERS = require('./constants').EXPOSED_HEADERS;
2019
const REQUEST_METHODS = require('./constants').REQUEST_METHODS;
2120
const TUS_RESUMABLE = require('./constants').TUS_RESUMABLE;
22-
21+
const Debug = require('debug');
22+
const log = Debug('tus-node-server');
2323
class TusServer extends EventEmitter {
2424

2525
constructor() {
@@ -108,7 +108,7 @@ class TusServer extends EventEmitter {
108108
* @return {ServerResponse}
109109
*/
110110
handle(req, res) {
111-
console.info(`[TusServer] handle: ${req.method} ${req.url}`);
111+
log(`[TusServer] handle: ${req.method} ${req.url}`);
112112

113113
// Allow overriding the HTTP method. The reason for this is
114114
// that some libraries/environments to not support PATCH and
@@ -149,7 +149,7 @@ class TusServer extends EventEmitter {
149149
}
150150

151151
if (RequestValidator.isInvalidHeader(header_name, req.headers[header_name])) {
152-
console.warn(`Invalid ${header_name} header: ${req.headers[header_name]}`);
152+
log(`Invalid ${header_name} header: ${req.headers[header_name]}`);
153153
invalid_headers.push(header_name);
154154
}
155155
}

lib/handlers/HeadHandler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const BaseHandler = require('./BaseHandler');
44
const ERRORS = require('../constants').ERRORS;
5-
5+
const Debug = require('debug');
6+
const log = Debug('tus-node-server:handlers:head');
67
class HeadHandler extends BaseHandler {
78
/**
89
* Send the bytes received for a given file.
@@ -49,7 +50,7 @@ class HeadHandler extends BaseHandler {
4950
return res.end();
5051
})
5152
.catch((error) => {
52-
console.warn('[HeadHandler]', error);
53+
log('[HeadHandler]', error);
5354
const status_code = error.status_code || ERRORS.UNKNOWN_ERROR.status_code;
5455
const body = error.body || `${ERRORS.UNKNOWN_ERROR.body}${error.message || ''}\n`;
5556
return super.send(res, status_code, {}, body);

lib/handlers/PatchHandler.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const BaseHandler = require('./BaseHandler');
44
const ERRORS = require('../constants').ERRORS;
5-
5+
const Debug = require('debug');
6+
const log = Debug('tus-node-server:handlers:patch');
67
class PatchHandler extends BaseHandler {
78
/**
89
* Write data to the DataStore and return the new offset.
@@ -35,7 +36,7 @@ class PatchHandler extends BaseHandler {
3536
.then((stats) => {
3637
if (stats.size !== offset) {
3738
// If the offsets do not match, the Server MUST respond with the 409 Conflict status without modifying the upload resource.
38-
console.warn(`[PatchHandler] send: Incorrect offset - ${offset} sent but file is ${stats.size}`);
39+
log(`[PatchHandler] send: Incorrect offset - ${offset} sent but file is ${stats.size}`);
3940
return Promise.reject(ERRORS.INVALID_OFFSET);
4041
}
4142

@@ -50,7 +51,7 @@ class PatchHandler extends BaseHandler {
5051
return super.send(res, 204, headers);
5152
})
5253
.catch((error) => {
53-
console.warn('[PatchHandler]', error);
54+
log('[PatchHandler]', error);
5455
const status_code = error.status_code || ERRORS.UNKNOWN_ERROR.status_code;
5556
const body = error.body || `${ERRORS.UNKNOWN_ERROR.body}${error.message || ''}\n`;
5657
return super.send(res, status_code, {}, body);

lib/handlers/PostHandler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
const BaseHandler = require('./BaseHandler');
44
const ERRORS = require('../constants').ERRORS;
55
const EVENT_ENDPOINT_CREATED = require('../constants').EVENT_ENDPOINT_CREATED;
6-
6+
const Debug = require('debug');
7+
const log = Debug('tus-node-server:handlers:post');
78
class PostHandler extends BaseHandler {
89
/**
910
* Create a file in the DataStore.
@@ -20,7 +21,7 @@ class PostHandler extends BaseHandler {
2021
return super.send(res, 201, { Location: url });
2122
})
2223
.catch((error) => {
23-
console.warn('[PostHandler]', error);
24+
log('[PostHandler]', error);
2425
const status_code = error.status_code || ERRORS.UNKNOWN_ERROR.status_code;
2526
const body = error.body || `${ERRORS.UNKNOWN_ERROR.body}${error.message || ''}\n`;
2627
return super.send(res, status_code, {}, body);

lib/stores/DataStore.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const File = require('../models/File');
1212
const EventEmitter = require('events');
1313
const ERRORS = require('../constants').ERRORS;
1414
const EVENTS = require('../constants').EVENTS;
15-
15+
const Debug = require('debug');
16+
const log = Debug('tus-node-server:stores');
1617
class DataStore extends EventEmitter {
1718
constructor(options) {
1819
super();
@@ -78,7 +79,7 @@ class DataStore extends EventEmitter {
7879
* @return {Promise}
7980
*/
8081
write(req) {
81-
console.log('[DataStore] write');
82+
log('[DataStore] write');
8283
return new Promise((resolve, reject) => {
8384
// Stub resolve for tests
8485
const offset = 0;

lib/stores/FileStore.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const IGNORED_MKDIR_ERROR = 'EEXIST';
1010
const FILE_DOESNT_EXIST = 'ENOENT';
1111
const ERRORS = require('../constants').ERRORS;
1212
const EVENTS = require('../constants').EVENTS;
13-
13+
const Debug = require('debug');
14+
const log = Debug('tus-node-server:stores:filestore');
1415

1516
/**
1617
* @fileOverview
@@ -63,23 +64,23 @@ class FileStore extends DataStore {
6364
file_id = this.generateFileName(req);
6465
}
6566
catch (generateError) {
66-
console.warn('[FileStore] create: check your namingFunction. Error', generateError);
67+
log('[FileStore] create: check your namingFunction. Error', generateError);
6768
return reject(ERRORS.FILE_WRITE_ERROR);
6869
}
6970

7071
const file = new File(file_id, upload_length, upload_defer_length, upload_metadata);
7172

7273
return fs.open(`${this.directory}/${file.id}`, 'w', (err, fd) => {
7374
if (err) {
74-
console.warn('[FileStore] create: Error', err);
75+
log('[FileStore] create: Error', err);
7576
return reject(err);
7677
}
7778

7879
this.configstore.set(file.id, file);
7980

8081
return fs.close(fd, (exception) => {
8182
if (exception) {
82-
console.warn('[FileStore] create: Error', exception);
83+
log('[FileStore] create: Error', exception);
8384
return reject(exception);
8485
}
8586

@@ -114,14 +115,14 @@ class FileStore extends DataStore {
114115
});
115116

116117
stream.on('error', (e) => {
117-
console.warn('[FileStore] write: Error', e);
118+
log('[FileStore] write: Error', e);
118119
reject(ERRORS.FILE_WRITE_ERROR);
119120
});
120121

121122
return req.pipe(stream).on('finish', () => {
122-
console.info(`[FileStore] write: ${new_offset} bytes written to ${path}`);
123+
log(`[FileStore] write: ${new_offset} bytes written to ${path}`);
123124
offset += new_offset;
124-
console.info(`[FileStore] write: File is now ${offset} bytes`);
125+
log(`[FileStore] write: File is now ${offset} bytes`);
125126

126127
const config = this.configstore.get(file_id);
127128
if (config && parseInt(config.upload_length, 10) === offset) {
@@ -144,12 +145,12 @@ class FileStore extends DataStore {
144145
const file_path = `${this.directory}/${file_id}`;
145146
fs.stat(file_path, (error, stats) => {
146147
if (error && error.code === FILE_DOESNT_EXIST && config) {
147-
console.warn(`[FileStore] getOffset: No file found at ${file_path} but db record exists`, config);
148+
log(`[FileStore] getOffset: No file found at ${file_path} but db record exists`, config);
148149
return reject(ERRORS.FILE_NO_LONGER_EXISTS);
149150
}
150151

151152
if (error && error.code === FILE_DOESNT_EXIST) {
152-
console.warn(`[FileStore] getOffset: No file found at ${file_path}`);
153+
log(`[FileStore] getOffset: No file found at ${file_path}`);
153154
return reject(ERRORS.FILE_NOT_FOUND);
154155
}
155156

@@ -158,7 +159,7 @@ class FileStore extends DataStore {
158159
}
159160

160161
if (stats.isDirectory()) {
161-
console.warn(`[FileStore] getOffset: ${file_path} is a directory`);
162+
log(`[FileStore] getOffset: ${file_path} is a directory`);
162163
return reject(ERRORS.FILE_NOT_FOUND);
163164
}
164165

lib/stores/GCSDataStore.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const TUS_RESUMABLE = require('../constants').TUS_RESUMABLE;
1111
const DEFAULT_CONFIG = {
1212
scopes: ['https://www.googleapis.com/auth/devstorage.full_control'],
1313
};
14-
14+
const Debug = require('debug');
15+
const log = Debug('tus-node-server:stores:gcsstore');
1516

1617
/**
1718
* @fileOverview
@@ -49,7 +50,7 @@ class GCSDataStore extends DataStore {
4950
const bucket = this.gcs.bucket(this.bucket_name);
5051
bucket.exists((error, exists) => {
5152
if (error) {
52-
console.warn(error);
53+
log(error);
5354
throw new Error(`[GCSDataStore] _getBucket: ${error.message}`);
5455
}
5556

@@ -85,7 +86,7 @@ class GCSDataStore extends DataStore {
8586
file_id = this.generateFileName(req);
8687
}
8788
catch (generateError) {
88-
console.warn('[FileStore] create: check your namingFunction. Error', generateError);
89+
log('[FileStore] create: check your namingFunction. Error', generateError);
8990
reject(ERRORS.FILE_WRITE_ERROR);
9091
return;
9192
}
@@ -154,7 +155,7 @@ class GCSDataStore extends DataStore {
154155
});
155156

156157
req.on('end', () => {
157-
console.log(`${new_offset} bytes written`);
158+
log(`${new_offset} bytes written`);
158159

159160
if (data.upload_length === new_offset) {
160161
this.emit(EVENTS.EVENT_UPLOAD_COMPLETE, { file });
@@ -164,7 +165,7 @@ class GCSDataStore extends DataStore {
164165
});
165166

166167
write_stream.on('error', (e) => {
167-
console.log(e);
168+
log(e);
168169
reject(ERRORS.FILE_WRITE_ERROR);
169170
});
170171

@@ -188,7 +189,7 @@ class GCSDataStore extends DataStore {
188189
}
189190

190191
if (error) {
191-
console.warn('[GCSDataStore] getFileMetadata', error);
192+
log('[GCSDataStore] getFileMetadata', error);
192193
return reject(error);
193194
}
194195

package-lock.json

Lines changed: 35 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@google-cloud/storage": "1.1.1",
5757
"configstore": "^3.1.1",
5858
"crypto-rand": "0.0.2",
59+
"debug": "^3.1.0",
5960
"google-auto-auth": "^0.8.1",
6061
"object-assign": "^4.1.1",
6162
"request": "^2.72.0"

0 commit comments

Comments
 (0)