Skip to content

Commit

Permalink
chore(gatsby): Remove fallback for v8 serialize (#16958)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and wardpeet committed Aug 22, 2019
1 parent 58ed3ca commit 27d3efc
Showing 1 changed file with 3 additions and 40 deletions.
43 changes: 3 additions & 40 deletions packages/gatsby/src/redux/persist.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
const v8 = require(`v8`)
const fs = require(`fs-extra`)
const stringify = require(`json-stringify-safe`)

const objectToMap = obj => {
const map = new Map()
Object.keys(obj).forEach(key => {
map.set(key, obj[key])
})
return map
}
const file = `${process.cwd()}/.cache/redux.state`

const mapToObject = map => {
const obj = {}
for (let [key, value] of map) {
obj[key] = value
}
return obj
}
const readFromCache = () => v8.deserialize(fs.readFileSync(file))

const jsonStringify = contents => {
contents.staticQueryComponents = mapToObject(contents.staticQueryComponents)
contents.components = mapToObject(contents.components)
contents.nodes = contents.nodes ? mapToObject(contents.nodes) : {}
return stringify(contents, null, 2)
}

const jsonParse = buffer => {
const parsed = JSON.parse(buffer.toString(`utf8`))
parsed.staticQueryComponents = objectToMap(parsed.staticQueryComponents)
parsed.components = objectToMap(parsed.components)
parsed.nodes = objectToMap(parsed.nodes || {})
return parsed
}

const useV8 = Boolean(v8.serialize)
const [serialize, deserialize, file] = useV8
? [v8.serialize, v8.deserialize, `${process.cwd()}/.cache/redux.state`]
: [jsonStringify, jsonParse, `${process.cwd()}/.cache/redux-state.json`]

const readFromCache = () => deserialize(fs.readFileSync(file))

const writeToCache = contents => {
fs.writeFileSync(file, serialize(contents))
}
const writeToCache = contents => fs.writeFileSync(file, v8.serialize(contents))

module.exports = { readFromCache, writeToCache }

0 comments on commit 27d3efc

Please sign in to comment.