Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 30f85ac

Browse files
committed
Fix sprintf for undefined format strings
Since we remove all the ERROR_MESSAGES and LOG_MESSAGES format strings, the sprintf format function blows up when the format string is undefined. This proxy allows us to return a dummy format string regardless which "getter" property is accessed on those message objects. The dummy format string returned uses three placeholders; which should cover nearly all (if not all) possible invocations of the sprintf function (having a placeholder for up to 3 arguments). This way the sprintf function will basically operate as a "to-string" on all the arguments provided.
1 parent 5d59db0 commit 30f85ac

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

packages/optimizely-sdk/ext/enums.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
var jsSdkUtils = require('@optimizely/js-sdk-utils')
1+
const NOTIFICATION_TYPES = require('@optimizely/js-sdk-utils').NOTIFICATION_TYPES
22

33
const JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk'
44
const NODE_CLIENT_ENGINE = 'node-sdk'
55
const REACT_CLIENT_ENGINE = 'react-sdk'
66
const REACT_NATIVE_CLIENT_ENGINE = 'react-native-sdk'
77
const REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk'
88

9-
// The normal utils.sprintf accepts the format string as first param.
10-
// That string will be undefined for the strings stubbed by this file (ie,
11-
// ERROR_MESSAGES and LOG_MESSAGES, etc). So we override the sprintf function
12-
// to just splat out the args instead of attempting to invoke .replace on it.
13-
jsSdkUtils.sprintf = (...args) => args.toString()
9+
const allEmptyString = new Proxy({}, { get: () => '%s; %s; %s' });
1410

1511
module.exports = {
1612
LOG_LEVEL: {
@@ -21,9 +17,9 @@ module.exports = {
2117
ERROR: 4
2218
},
2319

24-
ERROR_MESSAGES: {},
20+
ERROR_MESSAGES: allEmptyString,
2521

26-
LOG_MESSAGES: {},
22+
LOG_MESSAGES: allEmptyString,
2723

2824
RESERVED_EVENT_KEYWORDS: {
2925
REVENUE: 'revenue',
@@ -52,7 +48,7 @@ module.exports = {
5248
REACT_NATIVE_JS_CLIENT_ENGINE,
5349
],
5450

55-
NOTIFICATION_TYPES: jsSdkUtils.NOTIFICATION_TYPES,
51+
NOTIFICATION_TYPES,
5652

5753
DECISION_NOTIFICATION_TYPES: {
5854
AB_TEST: 'ab-test',

0 commit comments

Comments
 (0)