Skip to content

Commit

Permalink
wip: nostr workers
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Sep 20, 2023
1 parent 91249e6 commit 3bde7eb
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 2,014 deletions.
10 changes: 10 additions & 0 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
babel: function (config, env) {
console.log(config)
config.plugins.push(['@babel/plugin-transform-private-property-in-object']);
config.plugins.push(['@babel/plugin-transform-optional-chaining']);
config.plugins.push(['@babel/plugin-proposal-private-property-in-object']);
config.plugins.push(['@babel/preset-env']);
return config;
}
}
2,481 changes: 503 additions & 1,978 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"version": "7.3.3",
"license": "MIT",
"scripts": {
"start": "echo \"$CF_PAGES_COMMIT_SHA\" > public/commit_hash && react-scripts start",
"build": "echo \"$CF_PAGES_COMMIT_SHA\" > public/commit_hash && react-scripts build",
"test": "react-scripts test",
"start": "echo \"$CF_PAGES_COMMIT_SHA\" > public/commit_hash && react-app-rewired start",
"build": "echo \"$CF_PAGES_COMMIT_SHA\" > public/commit_hash && react-app-rewired build",
"test": "react-app-rewired test",
"prepare": "husky install",
"pre-commit": "lint-staged",
"analyze": "source-map-explorer 'build/static/js/*.js'",
Expand All @@ -35,6 +35,7 @@
"@terra-money/ledger-station-js": "^1.3.8",
"@terra-money/log-finder-ruleset": "^3.0.3",
"@terra-money/msg-reader": "^3.0.1",
"@terra-money/nostr-tools": "1.10.2",
"@terra-money/station-connector": "^1.0.5",
"@terra-money/terra-station-mobile": "^1.0.12",
"@terra-money/terra-utils": "^1.0.9",
Expand All @@ -56,8 +57,8 @@
"bech32": "^2.0.0",
"bignumber.js": "^9.0.2",
"buffer": "^6.0.3",
"comlink": "^4.4.1",
"classnames": "^2.3.2",
"comlink": "^4.4.1",
"crypto-js": "^4.1.1",
"date-fns": "^2.28.0",
"i18next": "^21.8.14",
Expand All @@ -74,13 +75,13 @@
"lodash.uniqby": "^4.7.0",
"lottie-react": "^2.3.1",
"moment": "^2.29.4",
"nostr-tools": "1.5.0",
"numeral": "^2.0.6",
"prettier": "^2.6.2",
"qrcode.react": "^3.1.0",
"qs": "^6.11.0",
"ramda": "^0.28.0",
"react": "^18.2.0",
"react-app-rewired": "^2.2.1",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.29.0",
Expand All @@ -99,6 +100,9 @@
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/plugin-transform-optional-chaining": "^7.22.15",
"@babel/plugin-transform-private-property-in-object": "^7.22.11",
"@babel/preset-env": "^7.22.20",
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@types/crypto-js": "^4.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/app/electron.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from "nostr-tools"
import { Event } from "@terra-money/nostr-tools"
import { RelayDict } from "types/nostr"
import Raven from "utils/nostr/raven"

Expand Down
2 changes: 1 addition & 1 deletion src/components/channel/message-view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { darken } from "@mui/material"
import Box from "@mui/material/Box"
import { useTheme } from "@mui/material/styles"
import Tooltip from "@mui/material/Tooltip"
import { nip19 } from "nostr-tools"
import { nip19 } from "@terra-money/nostr-tools"
import ProfileDialog from "components/channel/message-view/profile"
import useContentRenderer from "utils/hooks/use-render-content"
import Avatar from "components/channel/message-view/profile/avatar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ShortEmojiPicker = (props: {
if (frequent === null) {
// @ts-ignore
const promises = Object.values(
FrequentlyUsed.get({
FrequentlyUsed._get1({
maxFrequentRows: 2,
perLine: 3,
})
Expand Down
2 changes: 1 addition & 1 deletion src/components/channel/message-view/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Box from "@mui/material/Box"
import { useTheme } from "@mui/material/styles"
import Tooltip from "@mui/material/Tooltip"
import DialogContent from "@mui/material/DialogContent"
import { nip05, nip19 } from "nostr-tools"
import { nip05, nip19 } from "@terra-money/nostr-tools"
import Avatar from "components/channel/message-view/profile/avatar"
import { keysAtom } from "utils/nostr/atoms"
import { Profile } from "types/nostr/index"
Expand Down
2 changes: 1 addition & 1 deletion src/providers/raven.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useMemo, useState } from "react"
import uniq from "lodash.uniq"
import { nip04, nip19 } from "nostr-tools"
import { nip04, nip19 } from "@terra-money/nostr-tools"

import { initRaven, RavenEvents } from "utils/nostr/raven"
import {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/hooks/use-live-public-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const useLivePublicMessages = () => {
clean
.filter((c) => c.root === TERRA_CID)
.map((c) => ({ ...c, children: clean.filter((x) => x.root === c.id) })),
[clean, TERRA_CID]
[clean]
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/nostr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const SCROLL_DETECT_THRESHOLD = 5
export const MESSAGE_PER_PAGE = 30
export const ACCEPTABLE_LESS_PAGE_MESSAGES = 5
export const TERRA_CID =
"899c87b9677f1f220c8df29137f51c9432943b2a8a6a3981bdaa73309337b8ba"
"61e9000f661983b2ccc5b6665acd4aa7a42b5580985562312c6caae5d15811f3"

export function notEmpty<TValue>(
value: TValue | null | undefined
Expand Down
39 changes: 18 additions & 21 deletions src/utils/nostr/raven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
nip04,
signEvent,
SimplePool,
} from "nostr-tools"
} from "@terra-money/nostr-tools"
import * as Comlink from "comlink"
import {
PrivKey,
Expand All @@ -26,7 +26,7 @@ import {
} from "types/nostr"
import chunk from "lodash.chunk"
import uniq from "lodash.uniq"
import { BgRaven } from "./workers"
import { BgRaven } from "./worker"
import { DEFAULT_RELAYS, MESSAGE_PER_PAGE, TERRA_CID } from "utils/nostr"
import { notEmpty, isSha256 } from "."
import { TypedEventEmitter } from "providers/event-emitter"
Expand Down Expand Up @@ -71,25 +71,22 @@ type EventHandlerMap = {
}

class Raven extends TypedEventEmitter<RavenEvents, EventHandlerMap> {
private readonly worker: Worker
private bgRaven: Comlink.Remote<BgRaven>
worker: Worker
bgRaven: Comlink.Remote<BgRaven>
priv: PrivKey
pub: string

private readonly priv: PrivKey
private readonly pub: string

private readonly readRelays = Object.keys(DEFAULT_RELAYS).filter(
(r) => DEFAULT_RELAYS[r].read
)
private readonly writeRelays = Object.keys(DEFAULT_RELAYS).filter(
readRelays = Object.keys(DEFAULT_RELAYS).filter((r) => DEFAULT_RELAYS[r].read)
writeRelays = Object.keys(DEFAULT_RELAYS).filter(
(r) => DEFAULT_RELAYS[r].write
)

private eventQueue: Event[] = []
private eventQueueTimer: any
private eventQueueFlag = true
private eventQueueBuffer: Event[] = []
eventQueue: Event[] = []
eventQueueTimer: any
eventQueueFlag = true
eventQueueBuffer: Event[] = []

private nameCache: Record<string, number> = {}
nameCache: Record<string, number> = {}

listenerSub: string | null = null
messageListenerSub: string | null = null
Expand All @@ -102,7 +99,7 @@ class Raven extends TypedEventEmitter<RavenEvents, EventHandlerMap> {
// Raven is all about relay/pool management through websockets using timers.
// Browsers (chrome) slows down timer tasks when the window goes to inactive.
// That is why we use a web worker to read data from relays.
this.worker = new Worker(new URL("worker.ts", import.meta.url))
this.worker = new Worker("./worker.ts")
this.bgRaven = Comlink.wrap<BgRaven>(this.worker)
this.bgRaven.setup(this.readRelays).then()

Expand Down Expand Up @@ -212,11 +209,11 @@ class Raven extends TypedEventEmitter<RavenEvents, EventHandlerMap> {
public async fetchChannel(id: string): Promise<Channel | null> {
const filters: Filter[] = [
{
kinds: [Kind.ChannelCreation],
kinds: [40],
ids: [id],
},
{
kinds: [Kind.ChannelMetadata, Kind.EventDeletion],
kinds: [41, 5],
"#e": [id],
},
]
Expand Down Expand Up @@ -539,7 +536,7 @@ class Raven extends TypedEventEmitter<RavenEvents, EventHandlerMap> {
}
}

pub[0].on("ok", (r: string) => {
pub.on("ok", (r: string) => {
okRelays.push(r)
if (!resolved) {
resolve(event)
Expand All @@ -548,7 +545,7 @@ class Raven extends TypedEventEmitter<RavenEvents, EventHandlerMap> {
closePool()
})

pub[0].on("failed", (r: string) => {
pub.on("failed", (r: string) => {
failedRelays.push(r)
if (failedRelays.length === this.writeRelays.length) {
reject("Event couldn't be published on any relay!")
Expand Down
4 changes: 2 additions & 2 deletions src/utils/nostr/workers.ts → src/utils/nostr/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Comlink from "comlink"
import { Event, Filter, SimplePool, Sub } from "nostr-tools"
import { Event, Filter, SimplePool, Sub } from "@terra-money/nostr-tools"

export class BgRaven {
private seenOn: Record<string, string[]> = {}
Expand Down Expand Up @@ -65,7 +65,7 @@ export class BgRaven {
const pool = this.getPool()
const sub = pool.sub(this.relays, filters, { id: subId })

sub.on("event", (event) => {
sub.on("event", (event: Event) => {
this.seenOn[event.id] = pool.seenOn(event.id)
onEvent(event)
})
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2021",
"target": "es2020",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 3bde7eb

Please sign in to comment.