balena-settings-storage
Advanced tools
Comparing version 5.0.2 to 6.0.0-6-x-3e2e311c531c730e588ebed1271c2ab9e3a3634c
@@ -1,9 +0,8 @@ | ||
interface StorageLike { | ||
_init?: () => void; | ||
clear(): void; | ||
getItem(key: string): string | null; | ||
setItem(key: string, data: string): void; | ||
removeItem(key: string): void; | ||
export interface StorageLike { | ||
clear(): PromiseLike<void> | void; | ||
getItem(key: string): PromiseLike<string | null> | string | null; | ||
setItem(key: string, data: string): PromiseLike<void> | void; | ||
removeItem(key: string): PromiseLike<void> | void; | ||
} | ||
declare let createStorage: (dataDirectory?: string) => StorageLike; | ||
export = createStorage; | ||
export { createStorage }; |
@@ -17,7 +17,9 @@ "use strict"; | ||
*/ | ||
var prefixed = function (key) { return "balena-" + key; }; | ||
var createVirtualStore = function () { | ||
var _store = {}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createStorage = void 0; | ||
const prefixed = (key) => `balena-${key}`; | ||
const createVirtualStore = () => { | ||
let _store = {}; | ||
return { | ||
getItem: function (key) { | ||
getItem(key) { | ||
if (_store.hasOwnProperty(key)) { | ||
@@ -30,9 +32,9 @@ return _store[key]; | ||
}, | ||
setItem: function (key, value) { | ||
setItem(key, value) { | ||
_store[key] = value; | ||
}, | ||
removeItem: function (key) { | ||
removeItem(key) { | ||
delete _store[key]; | ||
}, | ||
clear: function () { | ||
clear() { | ||
_store = {}; | ||
@@ -43,3 +45,3 @@ }, | ||
// Inspired by https://github.com/gsklee/ngStorage | ||
var isStorageSupported = function ($window, storageType) { | ||
const isStorageSupported = ($window, storageType) => { | ||
// Some installations of IE, for an unknown reason, throw "SCRIPT5: Error: Access is denied" | ||
@@ -50,3 +52,3 @@ // when accessing window.localStorage. This happens before you try to do anything with it. Catch | ||
// when "Block cookies": "Always block" is turned on | ||
var storage; | ||
let storage; | ||
try { | ||
@@ -58,7 +60,7 @@ storage = $window[storageType]; | ||
} | ||
var supported; | ||
let supported; | ||
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage and sessionStorage | ||
// is available, but trying to call .setItem throws an exception below: | ||
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." | ||
var key = "__" + Math.round(Math.random() * 1e7); | ||
const key = `__${Math.round(Math.random() * 1e7)}`; | ||
try { | ||
@@ -74,22 +76,23 @@ storage.setItem(key, key); | ||
}; | ||
var createStorage; | ||
let createStorage; | ||
exports.createStorage = createStorage; | ||
if (typeof window !== 'undefined') { | ||
if (isStorageSupported(window, 'localStorage')) { | ||
createStorage = function () { return ({ | ||
getItem: function (key) { | ||
exports.createStorage = createStorage = () => ({ | ||
getItem(key) { | ||
return localStorage.getItem(prefixed(key)); | ||
}, | ||
setItem: function (key, value) { | ||
setItem(key, value) { | ||
return localStorage.setItem(prefixed(key), value); | ||
}, | ||
removeItem: function (key) { | ||
removeItem(key) { | ||
return localStorage.removeItem(prefixed(key)); | ||
}, | ||
clear: function () { | ||
clear() { | ||
return localStorage.clear(); | ||
}, | ||
}); }; | ||
}); | ||
} | ||
else { | ||
createStorage = createVirtualStore; | ||
exports.createStorage = createStorage = createVirtualStore; | ||
} | ||
@@ -99,10 +102,14 @@ } | ||
// Fallback to filesystem based storage if not in the browser. | ||
// tslint:disable-next-line no-var-requires | ||
var LocalStorage_1 = require('node-localstorage').LocalStorage; | ||
createStorage = function (dataDirectory) { | ||
// Set infinite quota | ||
return new LocalStorage_1(dataDirectory, Infinity); | ||
const { NodeStorage, } = require('./node-storage'); | ||
const storageCache = Object.create(null); | ||
exports.createStorage = createStorage = (dataDirectory) => { | ||
if (dataDirectory == null) { | ||
throw new Error('dataDirectory must be specified in nodejs'); | ||
} | ||
if (!storageCache[dataDirectory]) { | ||
storageCache[dataDirectory] = new NodeStorage(dataDirectory); | ||
} | ||
return storageCache[dataDirectory]; | ||
}; | ||
} | ||
module.exports = createStorage; | ||
//# sourceMappingURL=local-storage.js.map |
@@ -17,7 +17,7 @@ "use strict"; | ||
*/ | ||
const tslib_1 = require("tslib"); | ||
/** | ||
* @module storage | ||
*/ | ||
var Promise = require("bluebird"); | ||
var getLocalStorage = require("./local-storage"); | ||
const local_storage_1 = require("./local-storage"); | ||
/** | ||
@@ -38,5 +38,4 @@ * @summary Get an instance of storage module | ||
*/ | ||
var getStorage = function (_a) { | ||
var dataDirectory = (_a === void 0 ? {} : _a).dataDirectory; | ||
var localStorage = getLocalStorage(dataDirectory); | ||
const getStorage = ({ dataDirectory, } = {}) => { | ||
const localStorage = local_storage_1.createStorage(dataDirectory); | ||
/** | ||
@@ -55,10 +54,8 @@ * @summary Set a value | ||
*/ | ||
var set = function (name, value) { | ||
return Promise.try(function () { | ||
if (typeof value !== 'string') { | ||
value = JSON.stringify(value); | ||
} | ||
return localStorage.setItem(name, value); | ||
}); | ||
}; | ||
const set = (name, value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
if (typeof value !== 'string') { | ||
value = JSON.stringify(value); | ||
} | ||
return localStorage.setItem(name, value); | ||
}); | ||
/** | ||
@@ -78,13 +75,5 @@ * @summary Get a value | ||
*/ | ||
var get = function (name) { | ||
return Promise.try(function () { | ||
// Run `node-localstorage` constructor to update | ||
// internal cache of saved files. | ||
// Without this, external changes to the data | ||
// directory (with `fs` for example) will not | ||
// be detected by `node-localstorage`. | ||
if (typeof localStorage._init === 'function') { | ||
localStorage._init(); | ||
} | ||
var result = localStorage.getItem(name); | ||
const get = (name) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
const result = yield localStorage.getItem(name); | ||
if (result == null) { | ||
@@ -103,4 +92,7 @@ return undefined; | ||
return result; | ||
}).catchReturn(undefined); | ||
}; | ||
} | ||
catch (_a) { | ||
return undefined; | ||
} | ||
}); | ||
/** | ||
@@ -123,3 +115,6 @@ * @summary Check if the value exists | ||
*/ | ||
var has = function (name) { return get(name).then(function (value) { return value != null; }); }; | ||
const has = (name) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
const value = yield get(name); | ||
return value != null; | ||
}); | ||
/** | ||
@@ -137,5 +132,3 @@ * @summary Remove a value | ||
*/ | ||
var remove = function (name) { | ||
return Promise.try(function () { return localStorage.removeItem(name); }); | ||
}; | ||
const remove = (name) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return localStorage.removeItem(name); }); | ||
/** | ||
@@ -152,6 +145,6 @@ * @summary Remove all values | ||
*/ | ||
var clear = function () { return Promise.try(function () { return localStorage.clear(); }); }; | ||
return { set: set, get: get, has: has, remove: remove, clear: clear }; | ||
const clear = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return localStorage.clear(); }); | ||
return { set, get, has, remove, clear }; | ||
}; | ||
module.exports = getStorage; | ||
//# sourceMappingURL=storage.js.map |
@@ -1,2 +0,1 @@ | ||
import * as Promise from 'bluebird'; | ||
export interface BalenaSettingsStorage { | ||
@@ -3,0 +2,0 @@ set: (name: string, value: any) => Promise<void>; |
@@ -7,2 +7,13 @@ # Change Log | ||
# v6.0.0 | ||
## (2020-07-02) | ||
* Enable strict type checking [Pagan Gazzard] | ||
* Update typescript [Pagan Gazzard] | ||
* Add engines property to package.json [Pagan Gazzard] | ||
* Update to @balena/lint 5.x [Pagan Gazzard] | ||
* Drop support for nodejs < 10 [Pagan Gazzard] | ||
* Switch to generating es2015 output [Pagan Gazzard] | ||
* Switch to native promises and use asynchronous fs access on nodejs [Pagan Gazzard] | ||
# v5.0.2 | ||
@@ -9,0 +20,0 @@ ## (2020-05-04) |
@@ -76,8 +76,7 @@ /* | ||
interface StorageLike { | ||
_init?: () => void; | ||
clear(): void; | ||
getItem(key: string): string | null; | ||
setItem(key: string, data: string): void; | ||
removeItem(key: string): void; | ||
export interface StorageLike { | ||
clear(): PromiseLike<void> | void; | ||
getItem(key: string): PromiseLike<string | null> | string | null; | ||
setItem(key: string, data: string): PromiseLike<void> | void; | ||
removeItem(key: string): PromiseLike<void> | void; | ||
} | ||
@@ -108,9 +107,18 @@ | ||
// Fallback to filesystem based storage if not in the browser. | ||
// tslint:disable-next-line no-var-requires | ||
const { LocalStorage } = require('node-localstorage'); | ||
createStorage = (dataDirectory: string) => | ||
// Set infinite quota | ||
new LocalStorage(dataDirectory, Infinity); | ||
const { | ||
NodeStorage, | ||
// tslint:disable-next-line no-var-requires | ||
} = require('./node-storage') as typeof import('./node-storage'); | ||
const storageCache = Object.create(null); | ||
createStorage = (dataDirectory?: string) => { | ||
if (dataDirectory == null) { | ||
throw new Error('dataDirectory must be specified in nodejs'); | ||
} | ||
if (!storageCache[dataDirectory]) { | ||
storageCache[dataDirectory] = new NodeStorage(dataDirectory); | ||
} | ||
return storageCache[dataDirectory]; | ||
}; | ||
} | ||
export = createStorage; | ||
export { createStorage }; |
@@ -21,4 +21,3 @@ /* | ||
import * as Promise from 'bluebird'; | ||
import getLocalStorage = require('./local-storage'); | ||
import { createStorage } from './local-storage'; | ||
import { BalenaSettingsStorage } from './types'; | ||
@@ -44,3 +43,3 @@ | ||
}: { dataDirectory?: string } = {}): BalenaSettingsStorage => { | ||
const localStorage = getLocalStorage(dataDirectory); | ||
const localStorage = createStorage(dataDirectory); | ||
@@ -60,9 +59,8 @@ /** | ||
*/ | ||
const set = (name: string, value: any) => | ||
Promise.try(() => { | ||
if (typeof value !== 'string') { | ||
value = JSON.stringify(value); | ||
} | ||
return localStorage.setItem(name, value); | ||
}); | ||
const set = async (name: string, value: any) => { | ||
if (typeof value !== 'string') { | ||
value = JSON.stringify(value); | ||
} | ||
return localStorage.setItem(name, value); | ||
}; | ||
@@ -83,15 +81,8 @@ /** | ||
*/ | ||
const get = (name: string): Promise<string | number | object | undefined> => | ||
Promise.try(() => { | ||
// Run `node-localstorage` constructor to update | ||
// internal cache of saved files. | ||
// Without this, external changes to the data | ||
// directory (with `fs` for example) will not | ||
// be detected by `node-localstorage`. | ||
if (typeof localStorage._init === 'function') { | ||
localStorage._init(); | ||
} | ||
const get = async ( | ||
name: string, | ||
): Promise<string | number | object | undefined> => { | ||
try { | ||
const result = await localStorage.getItem(name); | ||
const result = localStorage.getItem(name); | ||
if (result == null) { | ||
@@ -112,3 +103,6 @@ return undefined; | ||
return result; | ||
}).catchReturn(undefined); | ||
} catch { | ||
return undefined; | ||
} | ||
}; | ||
@@ -132,3 +126,6 @@ /** | ||
*/ | ||
const has = (name: string) => get(name).then(value => value != null); | ||
const has = async (name: string) => { | ||
const value = await get(name); | ||
return value != null; | ||
}; | ||
@@ -147,4 +144,3 @@ /** | ||
*/ | ||
const remove = (name: string) => | ||
Promise.try(() => localStorage.removeItem(name)); | ||
const remove = async (name: string) => localStorage.removeItem(name); | ||
@@ -162,3 +158,3 @@ /** | ||
*/ | ||
const clear = () => Promise.try(() => localStorage.clear()); | ||
const clear = async () => localStorage.clear(); | ||
@@ -165,0 +161,0 @@ return { set, get, has, remove, clear }; |
@@ -8,4 +8,2 @@ /* TODO: | ||
import * as Promise from 'bluebird'; | ||
export interface BalenaSettingsStorage { | ||
@@ -12,0 +10,0 @@ set: (name: string, value: any) => Promise<void>; |
{ | ||
"name": "balena-settings-storage", | ||
"version": "5.0.2", | ||
"version": "6.0.0-6-x-3e2e311c531c730e588ebed1271c2ab9e3a3634c", | ||
"description": "Balena settings storage utilities", | ||
@@ -35,3 +35,3 @@ "main": "build/storage.js", | ||
"devDependencies": { | ||
"@balena/lint": "^4.1.1", | ||
"@balena/lint": "^5.1.0", | ||
"@resin.io/types-mochainon": "^2.0.1", | ||
@@ -48,11 +48,10 @@ "@types/mocha": "^2.2.41", | ||
"ts-node": "^3.3.0", | ||
"typescript": "3.1.3" | ||
"typescript": "^3.9.6" | ||
}, | ||
"dependencies": { | ||
"@resin.io/types-node-localstorage": "^1.3.0", | ||
"@types/bluebird": "^3.5.8", | ||
"@types/node": "^8.0.19", | ||
"bluebird": "^3.3.4", | ||
"node-localstorage": "^1.3.0" | ||
"@types/node": "^10.17.26" | ||
}, | ||
"engines": { | ||
"node": ">=10.17.0" | ||
} | ||
} |
import * as BalenaSettingsClientModule from 'balena-settings-client'; | ||
import * as m from 'mochainon'; | ||
import getLocalStorage = require('../lib/local-storage'); | ||
import { createStorage } from '../lib/local-storage'; | ||
@@ -15,3 +15,3 @@ const IS_BROWSER = typeof window !== 'undefined'; | ||
const localStorage = getLocalStorage(dataDirectory); | ||
const localStorage = createStorage(dataDirectory); | ||
@@ -18,0 +18,0 @@ describe('Local Storage:', () => { |
@@ -6,3 +6,3 @@ import * as BalenaSettingsClientModule from 'balena-settings-client'; | ||
import getLocalStorage = require('../lib/local-storage'); | ||
import { createStorage } from '../lib/local-storage'; | ||
import getStorage = require('../lib/storage'); | ||
@@ -21,3 +21,3 @@ | ||
const localStorage = getLocalStorage(dataDirectory); | ||
const localStorage = createStorage(dataDirectory); | ||
const storage = getStorage({ dataDirectory }); | ||
@@ -28,9 +28,5 @@ | ||
describe('Storage:', () => { | ||
beforeEach(() => { | ||
storage.clear(); | ||
}); | ||
beforeEach(() => storage.clear()); | ||
after(() => { | ||
storage.clear(); | ||
}); | ||
after(() => storage.clear()); | ||
@@ -114,9 +110,10 @@ describe('given numbers', () => { | ||
describe('given getItem throws an error', () => { | ||
let getItemStub: any; | ||
beforeEach(() => { | ||
this.getItemStub = m.sinon.stub(localStorage, 'getItem'); | ||
this.getItemStub.throws(new Error('ENOENT')); | ||
getItemStub = m.sinon.stub(localStorage, 'getItem'); | ||
getItemStub.throws(new Error('ENOENT')); | ||
}); | ||
afterEach(() => { | ||
this.getItemStub.restore(); | ||
getItemStub.restore(); | ||
}); | ||
@@ -134,9 +131,10 @@ | ||
let fooPath: string; | ||
beforeEach(() => { | ||
this.path = path.join(dataDirectory!, 'foo'); | ||
fs.writeFileSync(this.path, 'hello world'); | ||
fooPath = path.join(dataDirectory!, 'foo'); | ||
fs.writeFileSync(fooPath, 'hello world'); | ||
}); | ||
afterEach(() => { | ||
fs.unlinkSync(this.path); | ||
fs.unlinkSync(fooPath); | ||
}); | ||
@@ -143,0 +141,0 @@ |
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"target": "es2015", | ||
"outDir": "build", | ||
@@ -13,4 +13,3 @@ "importHelpers": true, | ||
"sourceMap": true, | ||
"strictNullChecks": true, | ||
"lib": ["es5", "es6", "dom"], | ||
"strict": true, | ||
"typeRoots": [ | ||
@@ -17,0 +16,0 @@ "node_modules/@types", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55579
1
29
932
1
1
+ Added@types/node@10.17.60(transitive)
- Removed@types/bluebird@^3.5.8
- Removedbluebird@^3.3.4
- Removednode-localstorage@^1.3.0
- Removed@resin.io/types-node-localstorage@1.3.0(transitive)
- Removed@types/bluebird@3.5.42(transitive)
- Removed@types/node@8.10.66(transitive)
- Removedbluebird@3.7.2(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removednode-localstorage@1.3.1(transitive)
- Removedslide@1.1.6(transitive)
- Removedwrite-file-atomic@1.3.4(transitive)
Updated@types/node@^10.17.26