0.1.0 • Published 6 years ago

bt-storage v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

安装

npm install bt-storage

示例

import BetterStorage from "bt-storage";

const stg = new BetterStorage({
  storage: localStorage,         // localStorage or sessionStorage
  prefix: 'BIU',
  expires: 0,
  secret: false,
  AES: {
    secret: '30D17839695CB24C',    // AES secret key
    iv: '8BCAB84D2148034C'         // AES IV
  },
})

stg.set('test', 'some value', {
  expires: 1000, // 1秒后过期
  secret: true,  // 加密存储
})

参数说明

instanceConfig:

属性说明类型是否必填默认值
storagestorage类型,为localStorage或sessionStorageStorage--
prefix存储的key的前缀string--
expires存储持续时间,为0则不过期number0
secret存储时是否加密booleanfalse
AES.secret加密存储时的AES secret key,为8n位的16进制字符string30D17839695CB24C
AES.iv加密存储时的AES IV,为8n位的16进制字符string8BCAB84D2148034C

setConfig:

属性说明类型是否必填默认值
expires存储持续时间,为0则不过期number取自instance config
secret存储时是否加密boolean取自instance config

storageValue:

属性说明类型
expires过期的时间戳number
secret是否被加密boolean
value被存储的数据any
signature签名,用于读取时校验数据是否被更改string

API

创建实例

constructor(options: instanceConfig): storageInstance

const stg = new BetterStorage({
  storage: localStorage,
  prefix: 'BIU',
  expires: 0,
  secret: false,
  AES: {
    secret: '01234567',
    iv: '0123456789abcdef'
  },
})

set(key: string, value: any, config: setConfig): void

向storage中存数据

stg.set('hello', 'world')

/ 存储结果 '@BIU/hello' { "secret":false, "value":"world", "expires":0, "signature":"8E162389DE1AB8B27CB8D41C17AC94AD08721FC7" } /

stg.set('hello', 'world', { secret: true, expires: 2000, })

/ 存储结果 '@BIU/hello' { "secret":true, "value":"e1FcAN/aPkJR/dCAWIl10Q==", "expires":1573025822648, "signature":"1A867EFC666516B7B0E7CC4552EFA969106A907F" } /

### get(key: string): any
> 从storage中取数据,未取到时返回null
```javascript
const result = stg.get('hello') // "world"

remove(key: string): void

从storage中移除相应数据

stg.remove('hello')

stg.get('hello') // null

### clear(): void
> 移除所有由该实例存入的数据
````javascript
localStorage.setItem('hi', 'hi from origin');
stg.set('hi', 'hi from stg');
stg.set('hello', 'hello from stg');

localStorage.getItem('hi') // "hi from origin"
stg.get('hi') // "hi from stg"
stg.get('hello') // "hello from stg"

stg.clear();
localStorage.getItem('hi') // "hi from origin"
stg.get('hi') // null
stg.get('hello') // null

readable(): void

允许调用Api get

unreadable(): void

禁止调用Api get

stg.get('hello') // "world"

stg.unreadable() stg.get('hello') // Error: this storage is unreadable for now!

### keys: string[]
```javascript
stg.set('a', 1)
stg.set('b', 1)
stg.set('c', 1)

console.log(stg.keys) // ["a", "b", "c"]

length: number

stg.set('a', 1)
stg.set('b', 1)
stg.set('c', 1)

console.log(stg.length) // 3

Author

👤 render