ee-core 1.5.1 → 1.5.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ const stringify = require('./_stringify')
2
+
3
+ class Base {
4
+ constructor(
5
+ source,
6
+ { defaultValue = {}, serialize = stringify, deserialize = JSON.parse } = {}
7
+ ) {
8
+ this.source = source
9
+ this.defaultValue = defaultValue
10
+ this.serialize = serialize
11
+ this.deserialize = deserialize
12
+ }
13
+ }
14
+
15
+ module.exports = Base
@@ -0,0 +1,41 @@
1
+ // Not using async/await on purpose to avoid adding regenerator-runtime
2
+ // to lowdb dependencies
3
+ const fs = require('graceful-fs')
4
+ const pify = require('pify')
5
+ const steno = require('steno')
6
+ const Base = require('./Base')
7
+
8
+ const readFile = pify(fs.readFile)
9
+ const writeFile = pify(steno.writeFile)
10
+
11
+ class FileAsync extends Base {
12
+ read() {
13
+ // fs.exists is deprecated but not fs.existsSync
14
+ if (fs.existsSync(this.source)) {
15
+ // Read database
16
+ return readFile(this.source, 'utf-8')
17
+ .then(data => {
18
+ // Handle blank file
19
+ const trimmed = data.trim()
20
+ return trimmed ? this.deserialize(trimmed) : this.defaultValue
21
+ })
22
+ .catch(e => {
23
+ if (e instanceof SyntaxError) {
24
+ e.message = `Malformed JSON in file: ${this.source}\n${e.message}`
25
+ }
26
+ throw e
27
+ })
28
+ } else {
29
+ // Initialize
30
+ return writeFile(this.source, this.serialize(this.defaultValue)).then(
31
+ () => this.defaultValue
32
+ )
33
+ }
34
+ }
35
+
36
+ write(data) {
37
+ return writeFile(this.source, this.serialize(data))
38
+ }
39
+ }
40
+
41
+ module.exports = FileAsync
@@ -0,0 +1,39 @@
1
+ const fs = require('graceful-fs')
2
+ const Base = require('./Base')
3
+ const fileSystem = require('fs')
4
+
5
+ //const readFile = fs.readFileSync
6
+ const writeFile = fs.writeFileSync
7
+
8
+ // Same code as in FileAsync, minus `await`
9
+ class FileSync extends Base {
10
+ read() {
11
+ // fs.exists is deprecated but not fs.existsSync
12
+ if (fs.existsSync(this.source)) {
13
+ // Read database
14
+ try {
15
+ // 使用 fileSystem的readFileSync
16
+ //const data = readFile(this.source, 'utf-8').trim()
17
+ const data = fileSystem.readFileSync(this.source, {encoding: 'utf-8'})
18
+
19
+ // Handle blank file
20
+ return data ? this.deserialize(data) : this.defaultValue
21
+ } catch (e) {
22
+ if (e instanceof SyntaxError) {
23
+ e.message = `Malformed JSON in file: ${this.source}\n${e.message}`
24
+ }
25
+ throw e
26
+ }
27
+ } else {
28
+ // Initialize
29
+ writeFile(this.source, this.serialize(this.defaultValue))
30
+ return this.defaultValue
31
+ }
32
+ }
33
+
34
+ write(data) {
35
+ return writeFile(this.source, this.serialize(data))
36
+ }
37
+ }
38
+
39
+ module.exports = FileSync
@@ -0,0 +1,20 @@
1
+ /* global localStorage */
2
+ const Base = require('./Base')
3
+
4
+ class LocalStorage extends Base {
5
+ read() {
6
+ const data = localStorage.getItem(this.source)
7
+ if (data) {
8
+ return this.deserialize(data)
9
+ } else {
10
+ localStorage.setItem(this.source, this.serialize(this.defaultValue))
11
+ return this.defaultValue
12
+ }
13
+ }
14
+
15
+ write(data) {
16
+ localStorage.setItem(this.source, this.serialize(data))
17
+ }
18
+ }
19
+
20
+ module.exports = LocalStorage
@@ -0,0 +1,8 @@
1
+ const Base = require('./Base')
2
+
3
+ module.exports = class Memory extends Base {
4
+ read() {
5
+ return this.defaultValue
6
+ }
7
+ write() {}
8
+ }
@@ -0,0 +1,4 @@
1
+ // Pretty stringify
2
+ module.exports = function stringify(obj) {
3
+ return JSON.stringify(obj, null, 2)
4
+ }
@@ -0,0 +1,33 @@
1
+ const isPromise = require('./is-promise')
2
+
3
+ const init = (db, key, adapter) => {
4
+ db.read = () => {
5
+ const r = adapter.read()
6
+
7
+ return isPromise(r) ? r.then(db.plant) : db.plant(r)
8
+ }
9
+
10
+ db.write = (value = db.getState()) => {
11
+ const w = adapter.write(db.getState())
12
+
13
+ return isPromise(w) ? w.then(() => value) : value
14
+ }
15
+
16
+ db.plant = state => {
17
+ db[key] = state
18
+ return db
19
+ }
20
+
21
+ db.getState = () => db[key]
22
+
23
+ db.setState = state => {
24
+ db.plant(state)
25
+ return db
26
+ }
27
+
28
+ return db.read()
29
+ }
30
+
31
+ module.exports = {
32
+ init
33
+ }
@@ -0,0 +1,23 @@
1
+ const flow = require('lodash/flow')
2
+ const get = require('lodash/get')
3
+ const set = require('lodash/set')
4
+ const common = require('./common')
5
+
6
+ module.exports = function(adapter) {
7
+ function db(path, defaultValue) {
8
+ function getValue(funcs) {
9
+ const result = get(db.getState(), path, defaultValue)
10
+ return flow(funcs)(result)
11
+ }
12
+
13
+ getValue.write = (...funcs) => {
14
+ const result = getValue(...funcs)
15
+ set(db.getState(), path, result)
16
+ return db.write()
17
+ }
18
+
19
+ return getValue
20
+ }
21
+
22
+ return common.init(db, '__state__', adapter)
23
+ }
@@ -0,0 +1,6 @@
1
+ module.exports = isPromise;
2
+ module.exports.default = isPromise;
3
+
4
+ function isPromise(obj) {
5
+ return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
6
+ }
@@ -0,0 +1,46 @@
1
+ const lodash = require('lodash')
2
+ const isPromise = require('is-promise')
3
+
4
+ module.exports = function(adapter) {
5
+ if (typeof adapter !== 'object') {
6
+ throw new Error(
7
+ 'An adapter must be provided, see https://github.com/typicode/lowdb/#usage'
8
+ )
9
+ }
10
+
11
+ // Create a fresh copy of lodash
12
+ const _ = lodash.runInContext()
13
+ const db = _.chain({})
14
+
15
+ // Add write function to lodash
16
+ // Calls save before returning result
17
+ _.prototype.write = _.wrap(_.prototype.value, function(func) {
18
+ const funcRes = func.apply(this)
19
+ return db.write(funcRes)
20
+ })
21
+
22
+ function plant(state) {
23
+ db.__wrapped__ = state
24
+ return db
25
+ }
26
+
27
+ // Lowdb API
28
+ // Expose _ for mixins
29
+ db._ = _
30
+
31
+ db.read = () => {
32
+ const r = adapter.read()
33
+ return isPromise(r) ? r.then(plant) : plant(r)
34
+ }
35
+
36
+ db.write = returnValue => {
37
+ const w = adapter.write(db.getState())
38
+ return isPromise(w) ? w.then(() => returnValue) : returnValue
39
+ }
40
+
41
+ db.getState = () => db.__wrapped__
42
+
43
+ db.setState = state => plant(state)
44
+
45
+ return db.read()
46
+ }
@@ -0,0 +1,5 @@
1
+ const common = require('./common')
2
+
3
+ module.exports = function(adapter) {
4
+ return common.init({}, '__state__', adapter)
5
+ }
@@ -3,8 +3,8 @@
3
3
  const assert = require('assert');
4
4
  const fs = require('fs');
5
5
  const path = require('path');
6
- const lowdb = require('lowdb');
7
- const FileSync = require('lowdb/adapters/FileSync');
6
+ const lowdb = require('./lowdb/main');
7
+ const FileSync = require('./lowdb/adapters/FileSync');
8
8
  const _ = require('lodash');
9
9
  const constant = require('../constant');
10
10
  const utilsCommon = require('../../utils/common');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "1.5.1",
3
+ "version": "1.5.2-beta.1",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,8 +25,10 @@
25
25
  "fs-extra": "^10.0.0",
26
26
  "get-port": "^5.1.1",
27
27
  "globby": "^10.0.0",
28
+ "graceful-fs": "^4.1.3",
28
29
  "humanize-ms": "^1.2.1",
29
30
  "is-type-of": "^1.2.1",
31
+ "javascript-obfuscator": "^4.0.0",
30
32
  "koa": "^2.13.4",
31
33
  "koa-body": "^5.0.0",
32
34
  "koa-convert": "^2.0.0",
@@ -35,9 +37,10 @@
35
37
  "lodash": "^4.17.21",
36
38
  "lowdb": "^1.0.0",
37
39
  "path-to-regexp": "^6.2.0",
40
+ "pify": "^6.1.0",
38
41
  "socket.io": "^4.4.1",
39
42
  "socket.io-client": "^4.4.1",
40
- "javascript-obfuscator": "^4.0.0",
43
+ "steno": "^0.4.1",
41
44
  "urllib": "^2.38.0",
42
45
  "utility": "^1.17.0"
43
46
  },