Skip to content

Commit 52a840f

Browse files
committed
fix: debug memory leak
See: debug-js/debug#678
1 parent 4566238 commit 52a840f

File tree

10 files changed

+54
-38
lines changed

10 files changed

+54
-38
lines changed

server/graphql/v1/utils.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import debug from 'debug';
1+
import debugLib from 'debug';
22
import { graphql } from 'graphql';
33
import { loaders } from '../loaders';
44
import schema from './schema';
55

6+
const debug = debugLib('graphql');
7+
68
export const makeRequest = (remoteUser, query) => {
79
return {
810
remoteUser,
@@ -22,9 +24,9 @@ export const graphqlQuery = async (query, variables, remoteUser) => {
2224
};
2325

2426
if (process.env.DEBUG && process.env.DEBUG.match(/graphql/)) {
25-
debug('graphql')('query', query);
26-
debug('graphql')('variables', variables);
27-
debug('graphql')('context', remoteUser);
27+
debug('query', query);
28+
debug('variables', variables);
29+
debug('context', remoteUser);
2830
}
2931

3032
return prepare().then(() =>

server/lib/backyourstack/dispatcher.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import status from '../../constants/order_status';
1010
import activities from '../../constants/activities';
1111
import * as paymentsLib from '../payments';
1212

13+
const debug = debugLib('dispatch_prepaid_subscription');
14+
1315
export function needsDispatching(nextDispatchDate) {
1416
const needs = moment(nextDispatchDate).isSameOrBefore();
1517
return needs;
@@ -61,7 +63,6 @@ async function createPaymentMethod(originalCreditTransaction) {
6163
}
6264

6365
export async function dispatchFunds(order) {
64-
const debug = debugLib('dispatch_prepaid_subscription');
6566
// Amount shareable amongst dependencies
6667
const transaction = await models.Transaction.findOne({
6768
where: { OrderId: order.id, type: 'CREDIT' },

server/lib/email.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import whiteListDomains from './whiteListDomains';
1515
import { md5 } from './utils';
1616

1717
const debug = debugLib('email');
18+
const debugData = debugLib('data');
19+
const debugText = debugLib('text');
20+
const debugHtml = debugLib('html');
1821

1922
export const getMailer = () => {
2023
if (config.maildev.client) {
@@ -48,7 +51,7 @@ const render = (template, data) => {
4851

4952
// When in development mode, we log the data used to compile the template
5053
// (useful to get login token without sending an email)
51-
debugLib('data')(`Rendering ${template} with data`, data);
54+
debugData(`Rendering ${template} with data`, data);
5255

5356
return { text, html };
5457
};
@@ -185,8 +188,8 @@ const sendMessage = (recipients, subject, html, options = {}) => {
185188
});
186189
} else {
187190
debug('>>> mailer not configured');
188-
debugLib('text')(options.text);
189-
debugLib('html')(html);
191+
debugText(options.text);
192+
debugHtml(html);
190193
return Promise.resolve();
191194
}
192195
};

server/lib/express.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ export default function(app) {
3333
app.use(loadersMiddleware);
3434

3535
if (process.env.DEBUG && process.env.DEBUG.match(/response/)) {
36+
const debugResponse = debug('response');
3637
app.use((req, res, next) => {
3738
const temp = res.end;
3839
res.end = function(str) {
3940
try {
4041
const obj = JSON.parse(str);
41-
debug('response')(JSON.stringify(obj, null, ' '));
42+
debugResponse(JSON.stringify(obj, null, ' '));
4243
} catch (e) {
43-
debug('response', str);
44+
debugResponse(str);
4445
}
4546
temp.apply(this, arguments);
4647
};

server/lib/notifications.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { sanitizeActivity, enrichActivity } from './webhooks';
1515
import { PayoutMethodTypes } from '../models/PayoutMethod';
1616

1717
const debug = debugLib('notification');
18+
const debugActivityData = debugLib('activity.data');
1819

1920
export default async (Sequelize, activity) => {
2021
// publish everything to our private channel
@@ -186,7 +187,7 @@ async function notifyMembersOfCollective(CollectiveId, activity, options) {
186187

187188
async function notifyByEmail(activity) {
188189
debug('notifyByEmail', activity.type);
189-
debugLib('activity.data')('activity.data', activity.data);
190+
debugActivityData('activity.data', activity.data);
190191
switch (activity.type) {
191192
case activityType.TICKET_CONFIRMED:
192193
notifyUserId(activity.data.UserId, activity);

server/middleware/security/auth.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import config from 'config';
2-
import debug from 'debug';
2+
import debugLib from 'debug';
33

44
import errors from '../../lib/errors';
55
import { authenticateUser } from './authentication';
@@ -8,6 +8,8 @@ import models from '../../models';
88

99
const { Unauthorized } = errors;
1010

11+
const debug = debugLib('auth');
12+
1113
/**
1214
* Check Client App
1315
*
@@ -21,7 +23,7 @@ export async function checkClientApp(req, res, next) {
2123
where: { type: 'apiKey', apiKey },
2224
});
2325
if (app) {
24-
debug('auth')('Valid Client App (apiKey)');
26+
debug('Valid Client App (apiKey)');
2527
req.clientApp = app;
2628
const collectiveId = app.CollectiveId;
2729
if (collectiveId) {
@@ -35,7 +37,7 @@ export async function checkClientApp(req, res, next) {
3537
}
3638
next();
3739
} else {
38-
debug('auth')(`Invalid Client App (apiKey: ${apiKey}).`);
40+
debug(`Invalid Client App (apiKey: ${apiKey}).`);
3941
next(new Unauthorized(`Invalid Api Key: ${apiKey}.`));
4042
}
4143
} else if (clientId) {
@@ -44,16 +46,16 @@ export async function checkClientApp(req, res, next) {
4446
where: { clientId },
4547
});
4648
if (app) {
47-
debug('auth')('Valid Client App');
49+
debug('Valid Client App');
4850
req.clientApp = app;
4951
next();
5052
} else {
51-
debug('auth')(`Invalid Client App (clientId: ${clientId}).`);
53+
debug(`Invalid Client App (clientId: ${clientId}).`);
5254
next(new Unauthorized(`Invalid Client Id: ${clientId}.`));
5355
}
5456
} else {
5557
next();
56-
debug('auth')('No Client App');
58+
debug('No Client App');
5759
}
5860
}
5961

@@ -94,16 +96,16 @@ export function authorizeClientApp(req, res, next) {
9496

9597
const apiKey = req.get('Api-Key') || req.query.apiKey || req.query.api_key || req.body.api_key;
9698
if (req.clientApp) {
97-
debug('auth')('Valid Client App');
99+
debug('Valid Client App');
98100
next();
99101
} else if (apiKey === config.keys.opencollective.apiKey) {
100-
debug('auth')(`Valid API key: ${apiKey}`);
102+
debug(`Valid API key: ${apiKey}`);
101103
next();
102104
} else if (apiKey) {
103-
debug('auth')(`Invalid API key: ${apiKey}`);
105+
debug(`Invalid API key: ${apiKey}`);
104106
next(new Unauthorized(`Invalid API key: ${apiKey}`));
105107
} else {
106-
debug('auth')('Missing API key or Client Id');
108+
debug('Missing API key or Client Id');
107109
next();
108110
}
109111
}

server/middleware/security/authentication.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import debug from 'debug';
1+
import debugLib from 'debug';
22
import config from 'config';
33
import jwt from 'jsonwebtoken';
44
import passport from 'passport';
@@ -20,6 +20,7 @@ const { User } = models;
2020
const { BadRequest, CustomError } = errors;
2121

2222
const { jwtSecret } = config.keys.opencollective;
23+
const debug = debugLib('auth');
2324

2425
/**
2526
* Middleware related to authentication.
@@ -128,7 +129,7 @@ export const _authenticateUserByJwt = async (req, res, next) => {
128129

129130
req.remoteUser = user;
130131

131-
debug('auth')('logged in user', req.remoteUser.id, 'roles:', req.remoteUser.rolesByCollectiveId);
132+
debug('logged in user', req.remoteUser.id, 'roles:', req.remoteUser.rolesByCollectiveId);
132133
next();
133134
};
134135

@@ -147,14 +148,14 @@ export function authenticateUser(req, res, next) {
147148
parseJwtNoExpiryCheck(req, res, e => {
148149
// If a token was submitted but is invalid, we continue without authenticating the user
149150
if (e) {
150-
debug('auth')('>>> checkJwtExpiry invalid error', e);
151+
debug('>>> checkJwtExpiry invalid error', e);
151152
return next();
152153
}
153154

154155
checkJwtExpiry(req, res, e => {
155156
// If a token was submitted and is expired, we continue without authenticating the user
156157
if (e) {
157-
debug('auth')('>>> checkJwtExpiry expiry error', e);
158+
debug('>>> checkJwtExpiry expiry error', e);
158159
return next();
159160
}
160161
_authenticateUserByJwt(req, res, next);

server/models/PaymentMethod.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @module models/PaymentMethod */
22

3-
import libdebug from 'debug';
3+
import debugLib from 'debug';
44
import Promise from 'bluebird';
55
import { get, intersection } from 'lodash';
66
import { Op } from 'sequelize';
@@ -17,7 +17,7 @@ import { isTestToken } from '../lib/stripe';
1717

1818
import { maxInteger } from '../constants/math';
1919

20-
const debug = libdebug('PaymentMethod');
20+
const debug = debugLib('PaymentMethod');
2121

2222
export default function(Sequelize, DataTypes) {
2323
const { models } = Sequelize;

server/models/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pg from 'pg';
22
import Sequelize from 'sequelize';
33
import config from 'config';
4-
import debug from 'debug';
4+
import debugLib from 'debug';
55

66
import logger from '../lib/logger';
77
import { getDBConf } from '../lib/db';
@@ -11,6 +11,7 @@ import { getDBConf } from '../lib/db';
1111
pg.defaults.parseInt8 = true;
1212

1313
const dbConfig = getDBConf('database');
14+
const debug = debug('psql');
1415

1516
/**
1617
* Database connection.
@@ -26,12 +27,12 @@ if (config.database.options.logging) {
2627
if (process.env.NODE_ENV === 'production') {
2728
config.database.options.logging = (query, executionTime) => {
2829
if (executionTime > 50) {
29-
debug('psql')(query.replace(/(\n|\t| +)/g, ' ').slice(0, 100), '|', executionTime, 'ms');
30+
debug(query.replace(/(\n|\t| +)/g, ' ').slice(0, 100), '|', executionTime, 'ms');
3031
}
3132
};
3233
} else {
3334
config.database.options.logging = (query, executionTime) => {
34-
debug('psql')(
35+
debug(
3536
'\n-------------------- <query> --------------------\n',
3637
query,
3738
`\n-------------------- </query executionTime="${executionTime}"> --------------------\n`,

server/routes.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,25 @@ export default app => {
9494
});
9595
app.use('/graphql', rateLimiter);
9696
}
97-
9897
if (process.env.DEBUG) {
98+
const debugOperation = debug('operation');
99+
const debugParams = debug('params');
100+
const debugHeaders = debug('headers');
101+
const debugCurl = debug('curl');
102+
99103
app.use('*', (req, res, next) => {
100104
const body = sanitizeForLogs(req.body || {});
101-
debug('operation')(body.operationName, JSON.stringify(body.variables, null));
105+
debugOperation(body.operationName, JSON.stringify(body.variables, null));
102106
if (body.query) {
103107
const query = body.query;
104-
debug('params')(query);
108+
debugParams(query);
105109
delete body.query;
106110
}
107-
debug('params')('req.query', req.query);
108-
debug('params')('req.body', JSON.stringify(body, null, ' '));
109-
debug('params')('req.params', req.params);
110-
debug('headers')('req.headers', req.headers);
111-
debug('curl')('curl', curlify(req, req.body));
111+
debugParams('req.query', req.query);
112+
debugParams('req.body', JSON.stringify(body, null, ' '));
113+
debugParams('req.params', req.params);
114+
debugHeaders('req.headers', req.headers);
115+
debugCurl('curl', curlify(req, req.body));
112116
next();
113117
});
114118
}

0 commit comments

Comments
 (0)