forked from cmdruid/nostr-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnostrTerm.js
executable file
·125 lines (103 loc) · 2.98 KB
/
nostrTerm.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
#!/usr/bin/env node
const os = require('os')
const pty = require('node-pty')
const path = require('path')
const dotenv = require('dotenv')
const { writeFile } = require('node:fs/promises')
const { parseArgs } = require('node:util')
const NostrEmitter = require('@cmdcode/nostr-emitter')
// Setup our utility libraries.
const ec = new TextEncoder()
const utils = NostrEmitter.utils
// Define our dotenv config.
const configSchema = { override: true }
// Define our argument parser interface.
const optSchema = {
'silent' : { type: 'boolean', short: 's' },
'verbose' : { type: 'boolean', short: 'v' },
'config' : { type: 'string', short: 'c' },
'output' : { type: 'string', short: 'o' }
}
// Parse our arguments.
const { values: opt, positionals: arg } = parseArgs({
options: optSchema,
args: process.argv.slice(2),
allowPositionals: true
})
// If a config path is specified, add to dotenv config.
if (opt.config) {
configSchema.path = path.resolve(opt.config)
}
// Apply the dotenv configuration.
const config = dotenv.config(configSchema)
if (opt.verbose) console.log('Startup config:', opt, arg, config)
// Define our connection parameters.
let relayUrl = arg[0] || config.RELAY_URL || 'wss://nostr-relay.wlvs.space'
secret = arg[1] || config.SECRET_KEY || utils.getRandomString()
// Initialize our emitter object.
const emitter = new NostrEmitter({ silent: opt.silent })
// Setup our shell process.
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash'
const ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 120,
rows: 40,
cwd: process.env.HOME,
env: process.env
})
// Initialize our buffers.
let hasBuffer = false
let sendBuffer = false
emitter.on('init', () => {
// On init, send clear command.
ptyProcess.write('clear\r')
})
emitter.on('buff', (data) => {
// If data sent via buffer,
// set hasBuffer flag.
const keys = ec.encode(data)
if (keys.at(-1) === 9) {
// If there's a trailing tab,
// send a return buffer.
sendBuffer = true
} else {
sendBuffer = false
}
hasBuffer = true
if (opt.verbose) console.log('buff:', ec.encode(data))
ptyProcess.write(data)
})
emitter.on('data', (data) => {
if (opt.verbose) console.log('data', ec.encode(data))
ptyProcess.write(data)
})
// Define our main connection function.
async function main() {
await emitter.connect(relayUrl, secret)
ptyProcess.onData((data) => {
if (hasBuffer) {
// If buffer flag is set,
// skip returning chunk.
hasBuffer = false
} else {
// Return chunk.
if (sendBuffer) {
sendBuffer = false
emitter.emit('buff', data)
} else {
emitter.emit('data', data)
}
}
sendBuffer = false
})
}
// Output our connection details.
const sharelink = utils.encodeShareLink(secret, relayUrl)
if (typeof opt.output === 'string') {
const data = ec.encode(sharelink)
writeFile(opt.output, data, { mode: 0644 })
} else {
console.log(sharelink)
}
// Start main.
main()