Skip to content

Commit

Permalink
[1.0] Replace using levelup for caching with lowdb to avoid native de…
Browse files Browse the repository at this point in the history
…pendency (#1142)

* Replace leveldb with lowdb to avoid native dependency

* Add missing slash
  • Loading branch information
KyleAMathews authored Jun 11, 2017
1 parent acbc22e commit d182f94
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@
"joi": "^9.1.1",
"json-loader": "^0.5.2",
"json5": "^0.5.0",
"level": "^1.6.0",
"loader-utils": "^0.2.16",
"lodash": "^4.17.4",
"lodash-id": "^0.14.0",
"lowdb": "^0.16.2",
"markdown-it": "^7.0.1",
"md5-file": "^3.1.1",
"mime": "^1.3.4",
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby/src/cache-dir/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const apiRunner = require(`./api-runner-browser`)
// Let the site/plugins run code very early.
apiRunner(`onClientEntry`)

import React from "react"
import ReactDOM from "react-dom"
import { AppContainer as HotContainer } from "react-hot-loader"
window.___emitter = require(`./emitter`)

// Let the site/plugins run code very early.
apiRunner(`onClientEntry`)

/**
* Service Workers are persistent by nature. They stick around,
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/cache-dir/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import ComponentRenderer from "./component-renderer"
import loader from "./loader"
loader.addPagesArray(pages)
loader.addDevRequires(syncRequires)

window.___loader = loader

const history = createHistory()
Expand Down
13 changes: 13 additions & 0 deletions packages/gatsby/src/utils/__tests__/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const cache = require(`../cache`)
cache.initCache()

describe(`site cache`, () => {
it(`can set and get cache items`, async () => {
await cache.set(`a key`, `value`)
await cache.set(`a boolean key`, true)
const value = await cache.get(`a key`)
const value2 = await cache.get(`a boolean key`)
expect(value).toBe(`value`)
expect(value2).toBe(true)
})
})
54 changes: 28 additions & 26 deletions packages/gatsby/src/utils/cache.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
let levelup
if (process.env.NODE_ENV !== `test`) {
levelup = require(`level`)
}
const Promise = require(`bluebird`)
const low = require(`lowdb`)
const fs = require(`fs-extra`)

let db
exports.initCache = () => {
fs.ensureDirSync(`${process.cwd()}/.cache/cache`)
let directory
if (process.env.NODE_ENV === `test`) {
db = {
get: () => false,
set: () => false,
}
directory = require(`os`).tmpdir()
} else {
db = levelup(`${process.cwd()}/.cache/cache`, {
keyEncoding: `json`,
valueEncoding: `json`,
})
directory = process.cwd() + `/.cache/cache`
}
db = low(`${directory}/site-cache.json`, {
storage: require("lowdb/lib/storages/file-async"),
format: {
serialize: obj => JSON.stringify(obj),
deserialize: str => JSON.parse(str),
},
})
db._.mixin(require("lodash-id"))

db.defaults({ keys: [] }).write()
}

exports.get = key =>
new Promise((resolve, reject) => {
db.get(key, (err, value) => {
if (err && !err.notFound) {
reject(err)
} else {
resolve(value)
}
})
let pair
try {
pair = db.get(`keys`).getById(key).value()
} catch (e) {
// ignore
}

if (pair) {
resolve(pair.value)
} else {
resolve()
}
})

exports.set = (key, value) =>
new Promise((resolve, reject) => {
db.put(key, value, err => {
if (err) {
reject(err)
} else {
resolve(`OK`)
}
})
db.get(`keys`).upsert({ id: key, value }).write()
resolve(`Ok`)
})

0 comments on commit d182f94

Please sign in to comment.