Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FS async implementation #19

Merged
merged 1 commit into from
Oct 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 101 additions & 77 deletions src/core/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,32 @@ module.exports = class FileStore {
constructor(options) {
let self = this
self.path = options.path

if (!Fs.existsSync(self.path)) {
Fs.ensureDirSync(self.path)
}

let cacheFiles = Fs.readdirSync(self.path)
self.cache = {}
cacheFiles.forEach(function(file) {
file = file.replace('.json', '')

self.cache[file] = true
Fs.exists(self.path, (isExists) => {
if (!isExists) {
Fs.ensureDir(self.path, (err) => {
if (err) throw new Error(`ensureDir error ${err}`)

readDir(self)
})
} else {
readDir(self)
}
})

function readDir(self) {
Fs.readdir(self.path, (err, cacheFiles) => {
if (err) throw new Error(`readDir error ${err}`)

self.cache = {}
cacheFiles.forEach(function(file) {
file = file.replace('.json', '')

self.cache[file] = true
})
})
}
}

/**
Expand All @@ -37,34 +51,38 @@ module.exports = class FileStore {
key = sanitize(key)

let val = null,
data = null,
cacheFile = path.join(this.path, key + '.json')

if (Fs.existsSync(cacheFile)) {
data = Fs.readFileSync(cacheFile)
data = JSON.parse(data)
} else {
return fn(null, null)
}

if (!this.cache[key]) {
return fn(null, null)
}

if (!data) return fn(null, data)
if (data.expire < Date.now()) {
this.remove(key)
return fn(null, null)
}

try {
val = JSON.parse(data.value)
} catch (e) {
return fn(e)
}

process.nextTick(function tick() {
fn(null, val)
Fs.exists(cacheFile, (isExists) => {
if (isExists) {
Fs.readFile(cacheFile, (err, data) => {
if (err) return fn(err)

data = JSON.parse(data)

if (!this.cache[key]) {
return fn(null, null)
}

if (!data) return fn(null, data)
if (data.expire < Date.now()) {
this.remove(key)
return fn(null, null)
}

try {
val = JSON.parse(data.value)
} catch (e) {
return fn(e)
}

process.nextTick(function tick() {
fn(null, val)
})
})
} else {
return fn(null, null)
}
})
}

Expand Down Expand Up @@ -94,9 +112,9 @@ module.exports = class FileStore {
}

let cacheFile = path.join(this.path, key + '.json')
Fs.writeFileSync(cacheFile, JSON.stringify(data, null, 4))
Fs.writeFile(cacheFile, JSON.stringify(data, null, 4), (err) => {
if (err) return fn(err)

process.nextTick(() => {
this.cache[key] = true
fn(null, val)
})
Expand All @@ -112,21 +130,25 @@ module.exports = class FileStore {
key = sanitize(key)
let cacheFile = path.join(this.path, key + '.json')

if (!Fs.existsSync(cacheFile)) {
delete this.cache[key]
return fn()
}
Fs.exists(cacheFile, (isExists) => {
if (!isExists) {
delete this.cache[key]
return fn()
}

try {
Fs.removeSync(cacheFile)
} catch (e) {
return fn(e)
}
try {
Fs.remove(cacheFile, (err) => {
if (err) return fn(err)

process.nextTick(() => {
delete this.cache[key]
process.nextTick(() => {
delete this.cache[key]

fn(null)
fn(null)
})
})
} catch (err) {
return fn(err)
}
})
}

Expand All @@ -137,16 +159,14 @@ module.exports = class FileStore {
* @api public
*/
clear(fn = noop) {
try {
Fs.removeSync(this.path)
Fs.mkdirSync(this.path)
} catch (e) {
return fn(e)
}
Fs.remove(this.path, (err) => {
if (err) return fn(err)

process.nextTick(() => {
this.cache = {}
fn(null)
Fs.mkdir(this.path, () => {
if (err) return fn(err)
this.cache = {}
fn(null)
})
})
}

Expand All @@ -159,16 +179,16 @@ module.exports = class FileStore {
entries = [],
cache = self.cache

Object.keys(cache).forEach(function (entry) {
const keys = Object.keys(cache)

keys.forEach((entry, index) => {
self.get(entry, function (err, data) {
if (err) return fn(err)

entries.push({ key: entry, value: data })
})
})

process.nextTick(function () {
fn(null, entries)
if (index === keys.length - 1) fn(null, entries)
})
})
}

Expand All @@ -184,24 +204,28 @@ module.exports = class FileStore {
clearedKeys = []

try {
let files = Fs.readdirSync(storagePath)
Fs.readdir(storagePath, (err, files) => {
if (err) return fn(err)

files.forEach(file => {
if (file.match(pattern)) {
Fs.removeSync(path.join(storagePath, file))
clearedKeys.push(file.split('.json')[0])
}
files.forEach(file => {
if (file.match(pattern)) {
Fs.remove(path.join(storagePath, file), (err) => {
if (err) return fn(err)

clearedKeys.push(file.split('.json')[0])
process.nextTick(function tick() {
clearedKeys.forEach(key => {
self.cache[key] = null
})

fn(null)
})
})
}
})
})
} catch (e) {
return fn(e)
}

process.nextTick(function tick() {
clearedKeys.forEach(key => {
self.cache[key] = null
})

fn(null)
})
}
}