Skip to content

Commit 2e7ff56

Browse files
committed
fix(pencil): Fix sdk version extraction
1 parent e05f15f commit 2e7ff56

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/event-manager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import IEvent from './events/event';
22
import { SecureNativeOptions } from './types/securenative-options';
33
import { FetchOptions } from './types/fetch-options';
4-
import { promiseTimeout } from './utils/utils';
4+
import { promiseTimeout, getSDKVersion } from './utils/utils';
55
import { Logger } from './logger';
66

77
export default class EventManager {
@@ -10,12 +10,16 @@ export default class EventManager {
1010
private sendEnabled: Boolean = false;
1111
private timeoutId = null;
1212

13+
1314
constructor(private fetcher: any, private options: SecureNativeOptions) {
15+
1416
this.defaultFetchOptions = {
1517
url: options.apiUrl,
1618
options: {
1719
method: 'post',
1820
headers: {
21+
'SN-Version' : getSDKVersion(),
22+
'User-Agent' : 'SecureNative-node.js',
1923
Authorization: this.options.apiKey,
2024
},
2125
},

src/securenative.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export default class SecureNative {
3535

3636
public static init(options: SecureNativeOptions) {
3737
const defaultOptions = ConfigurationManager.getConfig();
38-
const config: SecureNativeOptions = { ...options, ...defaultOptions };
38+
const config: SecureNativeOptions = { ...defaultOptions, ...options };
3939

4040
const eventManager = new EventManager(fetch, config);
41-
if (options.autoSend) {
41+
if (config.autoSend) {
4242
eventManager.startEventsPersist();
4343
}
4444
SecureNative.initialize(eventManager, config);
@@ -48,7 +48,7 @@ export default class SecureNative {
4848
if (SecureNative.instance) {
4949
throw new Error('This SDK was already initialized');
5050
}
51-
51+
5252
const appPkg: Package = PackageManager.getPackage(join(process.cwd(), PACKAGE_FILE_NAME));
5353
// set default app name
5454
if (!options.appName) {

src/utils/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import { KeyValuePair } from '../types/key-value-pair';
66
import { Logger } from '../logger';
77
import { RequestContext } from '../types/request-context';
88
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http2';
9+
import { PackageManager } from '../package-manager';
10+
import { join } from 'path';
911

1012
const ALGORITHM = 'aes-256-cbc';
1113
const BLOCK_SIZE = 16;
1214
const AES_KEY_SIZE = 32;
1315
const ipHeaders = ['x-forwarded-for', 'x-client-ip', 'x-real-ip', 'x-forwarded', 'x-cluster-client-ip', 'forwarded-for', 'forwarded', 'via'];
16+
const PACKAGE_FILE_NAME = 'package.json';
1417

1518
const clientIpFromRequest = (req: any) => {
1619
if (!req) {
@@ -259,6 +262,11 @@ const isModuleExists = (path) => {
259262
}
260263
};
261264

265+
const getSDKVersion = () => {
266+
const agentPkg = PackageManager.getPackage(join(process.cwd(), '/node_modules/@securenative/agent/', PACKAGE_FILE_NAME));
267+
return agentPkg.version;
268+
};
269+
262270
export {
263271
clientIpFromRequest,
264272
remoteIpFromRequest,
@@ -280,4 +288,5 @@ export {
280288
isModuleExists,
281289
contextFromRequest,
282290
mergeRequestContexts,
291+
getSDKVersion,
283292
};

0 commit comments

Comments
 (0)