Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error if config.storeName isn't a String #948

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 44 additions & 36 deletions src/localforage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ const DefaultConfig = {
};

function callWhenReady(localForageInstance, libraryMethod) {
localForageInstance[libraryMethod] = function() {
localForageInstance[libraryMethod] = function () {
const _args = arguments;
return localForageInstance.ready().then(function() {
return localForageInstance.ready().then(function () {
return localForageInstance[libraryMethod].apply(
localForageInstance,
_args
Expand Down Expand Up @@ -107,7 +107,7 @@ class LocalForage {
this._dbInfo = null;

this._wrapLibraryMethodsWithReady();
this.setDriver(this._config.driver).catch(() => {});
this.setDriver(this._config.driver).catch(() => { });
}

// Set any config values for localForage; can be called anytime before
Expand All @@ -118,50 +118,58 @@ class LocalForage {
// If the options argument is an object, we use it to set values.
// Otherwise, we return either a specified config value or all
// config values.
if (typeof options === 'object') {
// If localforage is ready and fully initialized, we can't set
// any new configuration values. Instead, we return an error.
if (this._ready) {
return new Error(
"Can't call config() after localforage " + 'has been used.'
);
}

for (let i in options) {
if (i === 'storeName') {
options[i] = options[i].replace(/\W/g, '_');
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this try/catch block here is helpful to add, as it will prevent unexpected errors from throwing.

if (typeof options === 'object') {
// If localforage is ready and fully initialized, we can't set
// any new configuration values. Instead, we return an error.
if (this._ready) {
return new Error(
"Can't call config() after localforage " + 'has been used.'
);
}

if (i === 'version' && typeof options[i] !== 'number') {
return new Error('Database version must be a number.');
for (let i in options) {
if (i === 'storeName' && typeof options[i] !== 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading this code it seems like this if check will fail (calling the else block below) for any option key that isn't storeName. You'll want to nest the check for (typeof options[i] !== 'string') inside this if statement or this will break other things.

options[i] = options[i].replace(/\W/g, '_');
} else {
return new Error('storeName must be a string');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a helpful touch, and I guess it isn't a breaking change since .replace on anything that isn't a String would fail anyway (since it isn't a function).

}

if (i === 'version' && typeof options[i] !== 'number') {
return new Error('Database version must be a number.');
}

this._config[i] = options[i];
}

this._config[i] = options[i];
}
// after all config options are set and
// the driver option is used, try setting it
if ('driver' in options && options.driver) {
return this.setDriver(this._config.driver);
}

// after all config options are set and
// the driver option is used, try setting it
if ('driver' in options && options.driver) {
return this.setDriver(this._config.driver);
return true;
} else if (typeof options === 'string') {
return this._config[options];
} else {
return this._config;
}

return true;
} else if (typeof options === 'string') {
return this._config[options];
} else {
return this._config;
} catch (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't catch any error here; instead this should throw as it did previously.

return new Error(err);
}
}



// Used to define a custom driver, shared across all instances of
// localForage.
defineDriver(driverObject, callback, errorCallback) {
const promise = new Promise(function(resolve, reject) {
const promise = new Promise(function (resolve, reject) {
try {
const driverName = driverObject._driver;
const complianceError = new Error(
'Custom driver not compliant; see ' +
'https://mozilla.github.io/localForage/#definedriver'
'https://mozilla.github.io/localForage/#definedriver'
);

// A driver name should be defined and not overlap with the
Expand Down Expand Up @@ -190,9 +198,9 @@ class LocalForage {
}
}

const configureMissingMethods = function() {
const methodNotImplementedFactory = function(methodName) {
return function() {
const configureMissingMethods = function () {
const methodNotImplementedFactory = function (methodName) {
return function () {
const error = new Error(
`Method ${methodName} is not implemented by the current driver`
);
Expand Down Expand Up @@ -223,7 +231,7 @@ class LocalForage {

configureMissingMethods();

const setDriverSupport = function(support) {
const setDriverSupport = function (support) {
if (DefinedDrivers[driverName]) {
console.info(
`Redefining LocalForage driver: ${driverName}`
Expand Down Expand Up @@ -314,7 +322,7 @@ class LocalForage {
}

function initDriver(supportedDrivers) {
return function() {
return function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why these code changes were made, but they're failing the linter; could you please revert these style/whitespace changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this due to correcting the indentation by some extension. I'll revert it back.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think test cases in the test folder needed to be more organized in categories for manging.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with the changes and also added a test case in test/test.config.js regarding this issue fixing test. Can you check it again

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get any update from you, sir.

let currentDriverIndex = 0;

function driverPromiseLoop() {
Expand Down