basic api to get events information

This commit is contained in:
Vincent Liao
2022-03-21 23:05:38 +07:00
parent 1386008f5c
commit 0111fcd312
5 changed files with 104 additions and 5 deletions

View File

@@ -13,17 +13,20 @@
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "next", "@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next", "@sveltejs/kit": "next",
"autoprefixer": "^10.4.2",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-svelte3": "^3.2.1", "eslint-plugin-svelte3": "^3.2.1",
"postcss": "^8.4.5",
"postcss-load-config": "^3.1.1",
"prettier": "^2.5.1", "prettier": "^2.5.1",
"prettier-plugin-svelte": "^2.5.0", "prettier-plugin-svelte": "^2.5.0",
"svelte": "^3.44.0", "svelte": "^3.44.0",
"postcss": "^8.4.5",
"postcss-load-config": "^3.1.1",
"svelte-preprocess": "^4.10.1", "svelte-preprocess": "^4.10.1",
"autoprefixer": "^10.4.2",
"tailwindcss": "^3.0.12" "tailwindcss": "^3.0.12"
}, },
"type": "module" "type": "module",
"dependencies": {
"ws": "^8.5.0"
}
} }

20
src/routes/api/const.js Normal file
View File

@@ -0,0 +1,20 @@
export const unixTime = Math.floor(Date.now() / 1000);
export const unixTimeMinus24h = unixTime - 60 * 60 * 24;
export const unixTimeMinus1h = unixTime - 60 * 60;
// taken from github.com/fiatjaf/nostr-relay-registry
export const relays = [
"wss://nostr-pub.wellorder.net",
"wss://relayer.fiatjaf.com",
"wss://nostr.rocks",
"wss://rsslay.fiatjaf.com",
"wss://freedom-relay.herokuapp.com/ws",
"wss://nostr-relay.freeberty.net",
"wss://nostr.bitcoiner.social",
"wss://nostr-relay.wlvs.space",
"wss://nostr.onsats.org",
"wss://nostr-relay.untethr.me",
"wss://nostr-verified.wellorder.net",
"wss://nostr.drss.io",
"wss://nostr.unknown.place",
];

66
src/routes/api/data.js Normal file
View File

@@ -0,0 +1,66 @@
import WebSocket from 'ws';
const ws = new WebSocket('wss://nostr-pub.wellorder.net');
const unixTime = Math.floor(Date.now() / 1000);
const unixTimeMinus24h = unixTime - 60 * 60 * 24;
const unixTimeMinus1h = unixTime - 60 * 60;
const relays = [
"wss://nostr-pub.wellorder.net",
"wss://relayer.fiatjaf.com",
"wss://nostr.rocks",
"wss://rsslay.fiatjaf.com",
"wss://freedom-relay.herokuapp.com/ws",
"wss://nostr-relay.freeberty.net",
"wss://nostr.bitcoiner.social",
"wss://nostr-relay.wlvs.space",
"wss://nostr.onsats.org",
"wss://nostr-relay.untethr.me",
"wss://nostr-verified.wellorder.net",
"wss://nostr.drss.io",
"wss://nostr.unknown.place",
];
export async function get() {
let events = [];
// index of array represents "kind"
// last index is kind "others"
let kindArr = [0, 0, 0, 0, 0, 0];
relays.forEach(relay => {
const ws = new WebSocket(relay);
ws.on('open', function open() {
ws.send(JSON.stringify(["REQ", "foobar", { since: unixTimeMinus24h }]));
});
ws.on('message', function message(data) {
if (data.toString() != 'PING') {
const payload = JSON.parse(data.toString());
// ["EVENT", <sub name>, event]
const event = payload[2];
events.push(event);
// count kinds
// also use filter() to do this
if (event.kind == "0") kindArr[0]++;
else if (event.kind == "1") kindArr[1]++;
else if (event.kind == "2") kindArr[2]++;
else if (event.kind == "3") kindArr[3]++;
else if (event.kind == "4") kindArr[4]++;
else kindArr[5]++;
}
});
});
// wait the events to collect first
await new Promise(r => setTimeout(r, 2000));
const latestEvents = events.reverse().slice(0, 20);
const events1h = events.filter(event => event.created_at > unixTimeMinus1h)
return { body: { hello: 'world', count24h: events.length, count1h: events1h.length, kinds: kindArr, events: latestEvents } }
}

View File

@@ -1,4 +1,9 @@
<script>
</script>
<h1>Welcome to SvelteKit</h1> <h1>Welcome to SvelteKit</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p> <p>
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation
</p>
<p class="underline">hello from branch</p> <p class="underline">hello from branch</p>

View File

@@ -1616,6 +1616,11 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
ws@^8.5.0:
version "8.5.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.5.0.tgz#bfb4be96600757fe5382de12c670dab984a1ed4f"
integrity sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==
xtend@^4.0.2: xtend@^4.0.2:
version "4.0.2" version "4.0.2"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"