Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-native-storage
Advanced tools
A local storage wrapper for both react-native(AsyncStorage) and browser(localStorage). Support size controlling, auto expiring, remote data auto syncing and getting batch data in one query.
This is a local storage wrapper for both mobile React-Native apps (using AsyncStorage) and React browser-invoked web apps (using localStorage). ES6 syntax, promise for async load, fully tested with jest.
查看中文文档请点击README-CHN.md
npm install react-native-storage --save
You need to use webpack and babel to enable es6 modules for web development.
Add the following lines to your webpack config:
// ...
module: {
loaders: [
// ...
{
test: /\.js?$/,
include: [
// path.join(__dirname, 'your-own-js-files'),
// path.join(__dirname, 'node_modules/some-other-lib-that-needs-babel'),
path.join(__dirname, 'node_modules/react-native-storage')
],
loader: 'babel',
query: {
cacheDirectory: true,
presets: ['es2015', 'stage-1', 'react'],
plugins: ['transform-runtime']
}
}
]
}
You don't have to configure anything (but React Native version >= 0.13 is required).
import Storage from 'react-native-storage';
Do not use require('react-native-storage')
, which will cause an error in react native version >= 0.16.
import Storage from 'react-native-storage';
import { AsyncStorage } from 'react-native';
var storage = new Storage({
// maximum capacity, default 1000
size: 1000,
// Use AsyncStorage for RN apps, or window.localStorage for web apps.
// If storageBackend is not set, data will be lost after reload.
storageBackend: AsyncStorage, // for web: window.localStorage
// expire time, default: 1 day (1000 * 3600 * 24 milliseconds).
// can be null, which means never expire.
defaultExpires: 1000 * 3600 * 24,
// cache data in the memory. default is true.
enableCache: true,
// if data was not found in storage or expired data was found,
// the corresponding sync method will be invoked returning
// the latest data.
sync : {
// we'll talk about the details later.
}
})
// I suggest you have one (and only one) storage instance in global scope.
// for web
// window.storage = storage;
// for react native
// global.storage = storage;
// Save something with key only. (using only a keyname but no id)
// This key should be unique. This is for data frequently used.
// The key and value pair is permanently stored unless you remove it yourself.
storage.save({
key: 'loginState', // Note: Do not use underscore("_") in key!
data: {
from: 'some other site',
userid: 'some userid',
token: 'some token'
},
// if expires not specified, the defaultExpires will be applied instead.
// if set to null, then it will never expire.
expires: 1000 * 3600
});
// load
storage.load({
key: 'loginState',
// autoSync (default: true) means if data is not found or has expired,
// then invoke the corresponding sync method
autoSync: true,
// syncInBackground (default: true) means if data expired,
// return the outdated data first while invoking the sync method.
// If syncInBackground is set to false, and there is expired data,
// it will wait for the new data and return only after the sync completed.
// (This, of course, is slower)
syncInBackground: true,
// you can pass extra params to the sync method
// see sync example below
syncParams: {
extraFetchOptions: {
// blahblah
},
someFlag: true,
},
}).then(ret => {
// found data go to then()
console.log(ret.userid);
}).catch(err => {
// any exception including data not found
// goes to catch()
console.warn(err.message);
switch (err.name) {
case 'NotFoundError':
// TODO;
break;
case 'ExpiredError':
// TODO
break;
}
})
// --------------------------------------------------
// Save something with key and id.
// "key-id" data size cannot surpass the size parameter you pass in the constructor.
// By default the 1001st data will overwrite the 1st data item.
// If you then load the 1st data, a catch(NotFoundError) or sync will be invoked.
var userA = {
name: 'A',
age: 20,
tags: [
'geek',
'nerd',
'otaku'
]
};
storage.save({
key: 'user', // Note: Do not use underscore("_") in key!
id: '1001', // Note: Do not use underscore("_") in id!
data: userA,
expires: 1000 * 60
});
// load
storage.load({
key: 'user',
id: '1001'
}).then(ret => {
// found data goes to then()
console.log(ret.userid);
}).catch(err => {
// any exception including data not found
// goes to catch()
console.warn(err.message);
switch (err.name) {
case 'NotFoundError':
// TODO;
break;
case 'ExpiredError':
// TODO
break;
}
});
// --------------------------------------------------
// get all ids for "key-id" data under a key,
// note: does not include "key-only" information (which has no ids)
storage.getIdsForKey('user').then(ids => {
console.log(ids);
});
// get all the "key-id" data under a key
// !! important: this does not include "key-only" data
storage.getAllDataForKey('user').then(users => {
console.log(users);
});
// clear all "key-id" data under a key
// !! important: "key-only" data is not cleared by this function
storage.clearMapForKey('user');
// --------------------------------------------------
// remove a single record
storage.remove({
key: 'lastPage'
});
storage.remove({
key: 'user',
id: '1001'
});
// clear map and remove all "key-id" data
// !! important: "key-only" data is not cleared, and is left intact
storage.clearMap();
There are two ways to set the sync method. You can pass the sync method in the constructor's parameter, as a function in an object, or you can define it at any time as shown below:
storage.sync = {
// The name of the sync method must be the same as the data's key name
// And the passed params will be an all-in-one object.
// You can either use promise here, or a plain callback function with resolve/reject, like:
user(params){
let { id, resolve, reject, syncParams: { extraFetchOptions, someFlag } } = params;
fetch('user/', {
method: 'GET',
body: 'id=' + id,
...extraFetchOptions,
}).then(response => {
return response.json();
}).then(json => {
// console.log(json);
if(json && json.user){
storage.save({
key: 'user',
id,
data: json.user
});
if (someFlag) {
// do something for this extra param
}
// Call resolve() when succeed
resolve && resolve(json.user);
}
else{
// Call reject() when failed
reject && reject(new Error('data parse error'));
}
}).catch(err => {
console.warn(err);
reject && reject(err);
});
}
}
In the following example the sync method is called, when you invoke storage.load
:
storage.load({
key: 'user',
id: '1002'
}).then(...)
If there is no user 1002 currently in storage, then storage.sync.user will be invoked to fetch and return the remote data.
// Load batch data with an array of `storage.load` parameters.
// It will invoke each key's sync method,
// and when all are complete will return all the data in an ordered array.
// The sync methods behave according to the syncInBackground setting: (default true)
// When set to true (the default), if timed out will return the current value
// while when set to false, will wait till the sync method completes
storage.getBatchData([
{ key: 'loginState' },
{ key: 'checkPoint', syncInBackground: false },
{ key: 'balance' },
{ key: 'user', id: '1009' }
])
.then(results => {
results.forEach(result => {
console.log(result);
})
})
// Load batch data with one key and an array of ids.
storage.getBatchDataWithIds({
key: 'user',
ids: ['1001', '1002', '1003']
})
.then( ... )
There is an important difference between the way these two methods perform: getBatchData will invoke separate sync methods for each different key one after the other when the corresponding data is missing or not in sync. However, getBatchDataWithIds will collect a list of the missing data, pushing their ids to an array, and then pass the array to the single corresponding sync method once, reducing the number of requests, so you need to implement array query on the server side and handle the parameters of sync method properly. Note that the id parameter can be a single string or an array of strings.
rawData
is now deprecated, use "data" instead!defaultExpires
can be null
now, which means never expire.FAQs
A local storage wrapper for both react-native(AsyncStorage) and browser(localStorage).
We found that react-native-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.