-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.mjs
77 lines (63 loc) · 1.89 KB
/
index.mjs
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
import express from 'express'
import bodyparser from 'body-parser'
// Import the package
import NDK from "@nostr-dev-kit/ndk";
const app = express()
app.use(bodyparser.json())
const port = process.env.PORT || 3000
const defaultRelays = [
'wss://nostr.bitcoiner.social',
'wss://nostr-pub.wellorder.net',
'wss://nostr.mom',
'wss://nos.lol',
'wss://relay.mostr.pub',
'wss://relay.damus.io'
]
var ndk = null
app.get('/e/:idHex', async (req, res) => {
if (ndk == null) {
connect()
}
let event = await ndk.fetchEvent(req.params.idHex);
if (event) {
if (event.kind == 1065) {
// media content
let eventData = await ndk.fetchEvent(event.tagValue("e"));
if (eventData && eventData != null) {
var img = Buffer.from(eventData.content, 'base64')
res.setHeader('content-type', event.tagValue("m"))
res.send(img)
} else {
res.send("Data Event Not Found")
}
} else if (event.kind == 5393) {
res.setHeader('content-type', "text/css")
res.send(event.content)
} else if (event.kind == 5392) {
res.setHeader('content-type', "text/html")
res.send(event.content)
} else if (event.kind == 5394) {
res.setHeader('content-type', "text/javascript")
res.send(event.content)
}
} else {
res.send("Event Not Found")
}
})
app.listen(port, () => {
console.log("Listening to port" + port)
})
var myTimeout = null
function connect() {
if (ndk != null) return
if (myTimeout) {
clearTimeout(myTimeout)
}
ndk = new NDK({ explicitRelayUrls: defaultRelays });
ndk.connect().catch((e) => {
ndk = null
console.error("Disconnecting", e);
myTimeout = setTimeout(() => { connect() }, 10_000)
});
}
connect()