Skip to content

Commit 5ccdc84

Browse files
authored
Add debug mode (#680)
Adds a debug mode for the SDK that prints every requests and responses. To activate in Node.js: export DEBUG=kuzzle-sdk To activate in the Browser: add the following search param in the URL ?debugKuzzleSdk
1 parent 46d4893 commit 5ccdc84

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
code: false
3+
type: page
4+
title: Debugging
5+
description: How to debug the SDK in your application
6+
order: 600
7+
---
8+
9+
# Debugging
10+
11+
The SDK internal behavior can be observed for debugging purposes.
12+
13+
## Events listening
14+
15+
The SDK emits internal events, the complete list is available here: [Events](/sdk/js/7/essentials/events).
16+
17+
You can listen to every event and print payload contents with the following snippet:
18+
19+
```js
20+
for (const event of kuzzle.events) {
21+
kuzzle.on(event, (...args) => console.log(event, ...args));
22+
}
23+
```
24+
25+
## Print Request and Response
26+
27+
<SinceBadge version="auto-version"/>
28+
29+
You can print every request sent to Kuzzle and every response sent back to the SDK by activating the debug mode.
30+
31+
In Node.js, the `DEBUG` environment variable should contain the `kuzzle-sdk` string.
32+
33+
```bash
34+
export DEBUG=kuzzle-sdk
35+
36+
# Run your program
37+
```
38+
39+
In the Browser, you need to add the `debugKuzzleSdk` search param in the URL.
40+
41+
```
42+
http://my-application.by/devices?debugKuzzleSdk
43+
```

src/Kuzzle.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MemoryStorageController } from './controllers/MemoryStorage';
1414
import { Deprecation } from './utils/Deprecation';
1515
import { uuidv4 } from './utils/uuidv4';
1616
import { proxify } from './utils/proxify';
17+
import { debug } from './utils/debug';
1718
import { JSONObject } from './types';
1819
import { RequestPayload } from './types/RequestPayload';
1920
import { ResponsePayload } from './types/ResponsePayload';
@@ -823,7 +824,9 @@ export class Kuzzle extends KuzzleEventEmitter {
823824
if (this._queuing) {
824825
if (queuable) {
825826
this._cleanQueue();
826-
this.emit('offlineQueuePush', {request});
827+
828+
this.emit('offlineQueuePush', { request });
829+
827830
return new Promise((resolve, reject) => {
828831
this.offlineQueue.push({
829832
resolve,
@@ -844,7 +847,11 @@ Discarded request: ${JSON.stringify(request)}`));
844847
requestTimeout,
845848
request,
846849
options
847-
).then((response: ResponsePayload) => this.deprecationHandler.logDeprecation(response));
850+
).then((response: ResponsePayload) => {
851+
debug('RESPONSE', response);
852+
853+
return this.deprecationHandler.logDeprecation(response);
854+
});
848855
}
849856

850857
/**
@@ -1044,6 +1051,8 @@ Discarded request: ${JSON.stringify(request)}`));
10441051
* @returns Resolved request or a TimedOutError
10451052
*/
10461053
private _timeoutRequest(delay: number, request: RequestPayload, options: JSONObject = {}) {
1054+
debug('REQUEST', request);
1055+
10471056
// No timeout
10481057
if (delay === -1) {
10491058
return this.protocol.query(request, options);

src/controllers/Auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class AuthController extends BaseController {
4949
* Do not add the token for the checkToken route, to avoid getting a token error when
5050
* a developer simply wishes to verify their token
5151
*/
52-
authenticateRequest (request: any) {
52+
authenticateRequest (request: RequestPayload) {
5353
if (this.kuzzle.cookieAuthentication) {
5454
return;
5555
}
@@ -216,7 +216,7 @@ export class AuthController extends BaseController {
216216
token = this.authenticationToken.encodedJwt;
217217
}
218218
}
219-
219+
220220
return this.query({
221221
action: 'checkToken',
222222
body: { token },

src/utils/Deprecation.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import { ResponsePayload } from '../types/ResponsePayload';
22

33
export class Deprecation {
4-
5-
private _deprecationWarning: boolean;
4+
private deprecationWarning: boolean;
65

76
constructor (deprecationWarning: boolean) {
8-
this._deprecationWarning =
7+
this.deprecationWarning =
98
process.env.NODE_ENV !== 'production' ? deprecationWarning : false;
109
}
1110

1211
/**
1312
* Warn the developer that he is using a deprecated action (disabled if NODE_ENV=production)
14-
*
13+
*
1514
* @param response Result of a query to the API
16-
*
15+
*
1716
* @returns Same as response param, just like a middleware
1817
*/
1918
logDeprecation (response: ResponsePayload) {
20-
if ( this._deprecationWarning
19+
if ( this.deprecationWarning
2120
&& response.deprecations
2221
&& response.deprecations.length
2322
) {
2423
for (const deprecation of response.deprecations) {
2524
// eslint-disable-next-line no-console
26-
console.warn(deprecation.message);
25+
console.warn(deprecation.message);
2726
}
2827
}
2928
return response;

src/utils/debug.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function shouldDebug () {
2+
if (typeof window === 'undefined') {
3+
const debugString = process.env.DEBUG || '';
4+
5+
return debugString.includes('kuzzle-sdk');
6+
}
7+
8+
const url = new URL(window.location);
9+
10+
return url.searchParams.get('debugKuzzleSdk') !== null;
11+
}
12+
13+
/**
14+
* Print debug only if activated
15+
*
16+
* In Node.js, you can set the `DEBUG=kuzzle-sdk` env variable
17+
* In a browser, you can add the `?debugKuzzleSdk` in the URL
18+
*/
19+
function debug (message, obj) {
20+
if (! shouldDebug()) {
21+
return ;
22+
}
23+
24+
// eslint-disable-next-line no-console
25+
console.log(message);
26+
27+
if (obj) {
28+
// eslint-disable-next-line no-console
29+
console.log(JSON.stringify(obj));
30+
}
31+
}
32+
33+
module.exports = { debug };

0 commit comments

Comments
 (0)