This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
330 lines (299 loc) · 12.6 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict'
// npm
const Dat = require('dat-node')
const dlr = require('dat-link-resolve')
const ram = require('random-access-memory')
const glob = require('glob')
const pump = require('pump')
const stdout = require('stdout')
const expandTilde = require('expand-tilde')
const mime = require('mime')
const TerminalRenderer = require('marked-terminal')
const DownmarkStream = require('downmark-stream')
const html2ansi = require('html-to-ansi')
const { mkfifo } = require('mkfifo')
// core
const repl = require('repl')
const resolvePath = require('path').resolve
const fs = require('fs')
const util = require('util')
const startsWith = (line, x) => !x.indexOf(line)
const openDat = (key) => new Promise((resolve, reject) => {
let tooBad
const doG = (dat, stats, g) => {
const st = stats.get()
if (!st.files) { return stats.once('update', doG.bind(null, dat, stats)) }
clearTimeout(tooBad)
resolve(dat)
}
dlr(key, true, (errDlr, resp) => {
if (errDlr) { return reject(errDlr) }
Dat(ram, { sparse: true, key: resp.key }, (err, dat) => {
if (err) { return reject(err) }
dat.joinNetwork()
const stats = dat.trackStats()
dat.shell = resp
stats.once('update', doG.bind(null, dat, stats))
dat.network.once('listening', () => {
tooBad = setTimeout(() => {
dat.leaveNetwork() // preferable to dat.close() in this specific case
reject(new Error('Timeout.'))
}, 60000)
})
})
})
})
const notFound = (cmd) => Object.assign(new Error(`Command not found.`), { cmd })
const writer = (str) => {
if (typeof str === 'string') { return str + '\n' }
if (typeof str === 'object' && str.length !== undefined) { return str.join('\n') + '\n' }
if (typeof str === 'object') { return util.inspect(str, { colors: true }) }
return str + '\n'
}
class DatRepl {
constructor (opts) {
Object.assign(this, opts)
if (opts.datKey) { this._datKeyProvided = opts.datKey }
const asyncs = ['ls', 'cat', 'cp', 'file', 'view', 'ln']
this._commands = {
'.help': {},
help: (args) => {
const lines = ['Available commands:']
let r
let str
for (r in this._commands) {
str = r
if (this._commands[r].help) { str += `: ${this._commands[r].help}` }
lines.push(str)
}
return lines
},
ln: (args) => new Promise((resolve, reject) => {
if (!this.datKey || !this._dat || !this._dat.archive) { return reject(new Error('Dat not ready.')) }
if (!args || args.length !== 2) { return reject(new Error('ln requires two file arguments.')) }
const fn = expandTilde(args[1])
mkfifo(fn, parseInt(644, 8), (err) => {
if (err && err.code !== 'EEXIST') { return reject(err) }
const inFile = this._dat.archive.createReadStream(resolvePath(this.cwd, args[0]))
const outFile = fs.createWriteStream(fn)
console.log(`${fn} is waiting to be read...`)
pump(inFile, outFile, (err) => {
fs.unlink(fn, (e2) => {
if (err) { return reject(err) }
if (e2) { return reject(e2) }
resolve()
})
})
})
}),
cp: (args) => new Promise((resolve, reject) => {
if (!this.datKey || !this._dat || !this._dat.archive) { return reject(new Error('Dat not ready.')) }
if (!args || args.length !== 2) { return reject(new Error('cp requires two file arguments.')) }
const inFile = this._dat.archive.createReadStream(resolvePath(this.cwd, args[0]))
const outFile = fs.createWriteStream(expandTilde(args[1]))
pump(inFile, outFile, (err) => err ? reject(err) : resolve())
}),
cat: (args) => new Promise((resolve, reject) => {
if (!this.datKey || !this._dat || !this._dat.archive) { return reject(new Error('Dat not ready.')) }
if (!args || !args[0]) { return reject(new Error('cat requires a file argument.')) }
pump(this._dat.archive.createReadStream(resolvePath(this.cwd, args[0])), stdout(), (err) => err ? reject(err) : resolve())
}),
ls: (args) => new Promise((resolve, reject) => {
if (!this.datKey || !this._dat || !this._dat.archive) { return reject(new Error('Dat not ready.')) }
const cwd = (args && args[0] && resolvePath(this.cwd, args[0])) || this.cwd
glob('*', { mark: true, cwd, fs: this._dat.archive }, (err, files) => err ? reject(err) : resolve(files.filter(Boolean)))
}),
file: (args) => new Promise((resolve, reject) => {
if (!this.datKey || !this._dat || !this._dat.archive) { return reject(new Error('Dat not ready.')) }
if (!args || !args[0]) { return reject(new Error('file requires a file argument.')) }
const type = mime.getType(args[0])
if (type) { return resolve(type) }
reject(new Error('No mimetype detected.'))
}),
view: (args) => new Promise((resolve, reject) => {
if (!this.datKey || !this._dat || !this._dat.archive) { return reject(new Error('Dat not ready.')) }
if (!args || !args[0]) { return reject(new Error('view requires a file argument.')) }
const type = mime.getType(args[0])
switch (type) {
case null:
return reject(new Error('No mimetype detected.'))
case 'application/json':
return this._dat.archive.readFile(resolvePath(this.cwd, args[0]), 'utf-8', (err, x) => {
if (err) { return reject(err) }
try {
resolve(JSON.parse(x))
} catch (e) {
reject(e)
}
})
case 'text/markdown':
return pump(this._dat.archive.createReadStream(resolvePath(this.cwd, args[0])), DownmarkStream({ renderer: new TerminalRenderer() }), stdout(), (err) => {
if (err) { return reject(err) }
resolve()
})
case 'text/plain':
return pump(this._dat.archive.createReadStream(resolvePath(this.cwd, args[0])), stdout(), (err) => {
if (err) { return reject(err) }
resolve()
})
case 'text/html':
return this._dat.archive.readFile(resolvePath(this.cwd, args[0]), 'utf-8', (err, x) => {
if (err) { return reject(err) }
const html = x
.replace(/<!doctype.+?>/i, '')
.replace(/<\/p>/ig, '</p>\n')
.replace(/<\/div>/ig, '</div>\n')
resolve(html2ansi(html))
})
}
const types = type.split('/')
if (types[0] === 'text') {
return pump(this._dat.archive.createReadStream(resolvePath(this.cwd, args[0])), stdout(), (err) => {
if (err) { return reject(err) }
resolve()
})
}
reject(new Error('Unsupported file type.'))
}),
sl: (args) => 'Choo! Choo!',
cd: (args) => {
if (args && args[0] === '-') {
if (!this._previousCwd) { return 'No previous working directory.' }
[this._previousCwd, this.cwd] = [this.cwd, this._previousCwd] // swap
} else {
this._previousCwd = this.cwd
this.cwd = args && args[0]
}
this._replServer.setPrompt(this._makePrompt())
},
pwd: (args) => this.cwd,
dat: (args) => {
if (args && args[0]) { this.datKey = (args[0] === '--close' || args[0] === '-c') ? false : args[0] }
return this.datKey
},
state: (args) => {
const lines = [...this._commands.version(), '', `cwd: ${this.cwd}`]
if (this._previousCwd) { lines.push(`previousCwd: ${this._previousCwd}`) }
if (this._datKeyProvided) { lines.push(`datKeyProvided: ${this._datKeyProvided}`) }
if (this.datKey) { lines.push(`datKey: ${this.datKey}`) }
if (this._version) { lines.push(`version: ${this._version}`) }
if (this._latestVersion) { lines.push(`latest version: ${this._latestVersion}`) }
return lines
},
version: (args) => [`${this.pkg.name} v${this.pkg.version}`, `${this.pkg.description}`],
quit: (args) => process.exit((args && parseInt(args[0], 10)) || 0),
exit: (args) => process.exit((args && parseInt(args[0], 10)) || 0)
}
this._commands['.help'].help = 'Internal repl commands.'
this._commands.help.help = 'List of commands and their descriptions.'
this._commands.ls.help = 'List files.'
this._commands.file.help = 'Detect mimetype.'
this._commands.ln.help = 'Pseudo symbolic link (mkfifo).'
this._commands.cat.help = 'View a file (concatenate).'
this._commands.view.help = 'Generic view command (text, markdown, html, etc.).'
this._commands.cp.help = 'Copy a file from remote dat to local filesystem.'
this._commands.cd.help = 'Change directory.'
this._commands.sl.help = 'Train yourself to avoid typos.'
this._commands.pwd.help = 'Output current working directory.'
this._commands.dat.help = 'dat -c to close; dat <KEY> to open; dat to output current key.'
this._commands.state.help = 'Output current state.'
this._commands.version.help = 'Current dat-shell version.'
this._commands.exit.help = 'Exit dat-shell (or CTRL-D).'
this._commands.quit.help = 'Exit dat-shell (or CTRL-D).'
const completer = (line) => [
[
...Object.keys(this._commands),
...Object.keys(this._replServer.commands).map((x) => `.${x}`)
]
.filter(startsWith.bind(this, line))
.sort(),
line
]
/*
const completer = (line, cb) => cb(null, [Object.keys(this._commands).filter((x) => !x.indexOf(line)), line])
cb(new Error('Kaboom'))
* Triggers the following
* sDEBUG: tab completion error %j
* (node:19857) [DEP0028] DeprecationWarning: util.debug is deprecated. Use console.error instead.
* Probably fixed in 9.0.0 (check it out)
*/
const startOptions = {
writer,
completer,
prompt: this._makePrompt(),
ignoreUndefined: true,
eval: (str, context, filename, cb) => {
const callback = (err, ok) => {
this.title = `${this._datKeyProvided} (${this.datKey}) ${this._version} ${this.cwd}`
cb(err, ok)
}
str = str.trim()
const parts = str.split(' ') // FIXME do it as bash (quotes, etc.)
const cmd = parts[0]
if (!str) { return callback() }
// FIXME make all this._commands async
if (asyncs.indexOf(cmd) !== -1) {
return this._commands[cmd](parts.slice(1))
.then((X) => callback(null, X))
.catch(callback)
}
if (this._commands[cmd]) { return callback(null, this._commands[cmd](parts.slice(1))) }
callback(notFound(str))
}
}
console.log(this._commands.state().join('\n'))
console.log(this._commands.help().join('\n'))
this._replServer = repl.start(startOptions)
// console.log('replServer:', Object.keys(this._replServer))
}
_makePrompt () { return `dat-shell ${this.cwd} $ ` }
set title (str) {
if (!this._replServer) { return }
this._replServer.outputStream.write(`\u001b]0;${str}\u0007`)
}
get replServer () { return this._replServer }
get cwd () { return this._cwd || '/' }
set cwd (d) {
if (!this._dat) { return }
const lastCwd = this.cwd
this._cwd = `${resolvePath(this.cwd, d || '/')}`
if (this._cwd.slice(-1) !== '/') { this._cwd += '/' }
this._commands.ls()
.then(() => this._replServer.setPrompt(this._makePrompt()))
.catch(() => { this.cwd = lastCwd })
}
get datKey () { return this._datKey }
set datKey (key) {
if (key) {
if (this._datKey !== key) {
if (this._datKey && this._dat && this._dat.close) { this._dat.close() }
openDat(key)
.then((dat) => {
this._dat = dat
this._latestVersion = dat.archive.version
this._datKey = dat.key.toString('hex')
this.cwd = (dat.shell.path) || '/'
if (dat.shell.version) {
this._version = dat.shell.version
if (dat.shell.version !== dat.archive.version) {
const na = dat.archive.checkout(dat.shell.version)
if (na) {
this._dat.archive = na
} else {
this._version = dat.archive.version
}
}
} else {
this._version = dat.archive.version
}
if (this._datKey !== key) { this._datKeyProvided = key }
})
}
} else if (this._datKey && this._dat && this._dat.close) {
this._dat.close()
this._datKey = undefined
this._datKeyProvided = undefined
}
}
}
module.exports = DatRepl