forked from atjn/usable-gun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfs.js
168 lines (166 loc) · 4.58 KB
/
rfs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import mathRandomPlugin from './../usableLib/mathRandomPlugin.js'
import nodeFsImport from 'node:fs'
import gunPlugin from '../gun.js'
let __usable_isActivated = false
const __usable_module = {}
/**
*
* @param __usable_environment
* @param __usable_MODULE
*/
export default function (__usable_environment) {
if (__usable_isActivated) return __usable_module.exports
__usable_isActivated = true
const __usable_globalThis = new Proxy(
'window' in globalThis ? window : globalThis,
{
get (target, property) {
if (['window', 'globalThis', 'global'].includes(property)) {
return __usable_globalThis
} else if (__usable_environment.library[property] !== undefined) {
return __usable_environment.library[property]
} else if ('window' in globalThis) {
return window[property]
} else {
return globalThis[property]
}
},
set (object, property, value) {
__usable_environment.library[property] = value
return true
}
}
)
const __usable_window =
__usable_environment.environmentHint !== 'browser'
? undefined
: new Proxy('window' in globalThis ? window : globalThis, {
get (target, property) {
if (['window', 'globalThis', 'global'].includes(property)) {
return __usable_window
} else if (__usable_environment.library[property] !== undefined) {
return __usable_environment.library[property]
} else if ('window' in globalThis) {
return window[property]
} else {
return undefined
}
},
set (object, property, value) {
__usable_environment.library[property] = value
return true
}
})
/* BEGIN WRAPPED GUN CODE */
mathRandomPlugin(__usable_environment)
/**
*
* @param opt
*/
function Store (opt) {
opt = opt || {}
opt.log = opt.log || __usable_globalThis.debug.log
opt.file = String(opt.file || 'radata')
const fs = nodeFsImport
const store = () => {}
if (Store[opt.file]) {
__usable_globalThis.debug.log(
'Warning: reusing same fs store and options as 1st.'
)
return Store[opt.file]
}
Store[opt.file] = store
const puts = {}
// TODO!!! ADD ZLIB INFLATE / DEFLATE COMPRESSION!
store.put = (file, data, cb) => {
const random = __usable_globalThis.mathRandom().toString(36).slice(-3)
puts[file] = {
id: random,
data
}
const tmp = `${opt.file}-${file}-${random}.tmp`
fs.writeFile(tmp, data, (err) => {
if (err) {
if (random === (puts[file] || '').id) {
delete puts[file]
}
return cb(err)
}
move(tmp, `${opt.file}/${file}`, (err, ok) => {
if (random === (puts[file] || '').id) {
delete puts[file]
}
cb(err, ok || !err)
})
})
}
store.get = (file, cb) => {
let tmp // this took 3s+?
if ((tmp = puts[file])) {
cb(undefined, tmp.data)
return
}
fs.readFile(`${opt.file}/${file}`, (err, data) => {
if (err) {
if ((err.code || '').toUpperCase() === 'ENOENT') {
return cb()
}
opt.log('ERROR:', err)
}
cb(err, data)
})
}
if (!fs.existsSync(opt.file)) {
fs.mkdirSync(opt.file)
}
/**
*
* @param oldPath
* @param newPath
* @param cb
*/
function move (oldPath, newPath, cb) {
fs.rename(oldPath, newPath, (err) => {
if (err) {
if (err.code === 'EXDEV') {
const readStream = fs.createReadStream(oldPath)
const writeStream = fs.createWriteStream(newPath)
readStream.on('error', cb)
writeStream.on('error', cb)
readStream.on('close', () => {
fs.unlink(oldPath, cb)
})
readStream.pipe(writeStream)
} else {
cb(err)
}
} else {
cb()
}
})
}
store.list = (cb) => {
const dir = fs.readdirSync(opt.file)
dir.forEach((file) => {
cb(file)
})
cb()
}
return store
}
const Gun =
typeof __usable_window !== 'undefined'
? __usable_window.Gun
: gunPlugin(__usable_environment)
Gun.on('create', function (root) {
this.to.next(root)
const opt = root.opt
if (opt.rfs === false) {
return
}
opt.store = opt.store || (!Gun.window && Store(opt))
})
__usable_module.exports = Store
__usable_environment.exports.lib.rfs = __usable_module.exports
return __usable_module.exports
}