mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 08:38:50 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f7e3f8473 | ||
|
|
536dbcbffe | ||
|
|
ed52d2a8d4 | ||
|
|
faf8e62120 | ||
|
|
dc489bf387 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ node_modules
|
|||||||
dist
|
dist
|
||||||
yarn.lock
|
yarn.lock
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
nostr.js
|
||||||
|
|||||||
17
README.md
17
README.md
@@ -70,3 +70,20 @@ pool.addRelay('<url>')
|
|||||||
All functions expect bytearrays as hex strings and output bytearrays as hex strings.
|
All functions expect bytearrays as hex strings and output bytearrays as hex strings.
|
||||||
|
|
||||||
For other utils please read the source (for now).
|
For other utils please read the source (for now).
|
||||||
|
|
||||||
|
### Using from the browser (if you don't want to use a bundler)
|
||||||
|
|
||||||
|
You can import nostr-tools as an ES module. Just add a script tag like this:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<script type="module">
|
||||||
|
import {generatePrivateKey} from 'https://unpkg.com/nostr-tools/nostr.js'
|
||||||
|
console.log(generatePrivateKey())
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
And import whatever function you would import from `"nostr-tools"` in a bundler.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Public domain.
|
||||||
|
|||||||
25
build.js
Executable file
25
build.js
Executable file
@@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const esbuild = require('esbuild')
|
||||||
|
const alias = require('esbuild-plugin-alias')
|
||||||
|
const nodeGlobals = require('@esbuild-plugins/node-globals-polyfill').default
|
||||||
|
|
||||||
|
const buildOptions = {
|
||||||
|
entryPoints: ['index.js'],
|
||||||
|
outfile: 'nostr.js',
|
||||||
|
bundle: true,
|
||||||
|
format: 'esm',
|
||||||
|
plugins: [
|
||||||
|
alias({
|
||||||
|
stream: require.resolve('readable-stream')
|
||||||
|
}),
|
||||||
|
nodeGlobals({buffer: true})
|
||||||
|
],
|
||||||
|
define: {
|
||||||
|
window: 'self',
|
||||||
|
global: 'self'
|
||||||
|
},
|
||||||
|
loader: {'.js': 'jsx'}
|
||||||
|
}
|
||||||
|
|
||||||
|
esbuild.build(buildOptions).then(() => console.log('build success.'))
|
||||||
12
package.json
12
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "0.23.0",
|
"version": "0.23.4",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -30,7 +30,15 @@
|
|||||||
"client"
|
"client"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
||||||
|
"esbuild": "^0.14.38",
|
||||||
|
"esbuild-plugin-alias": "^0.2.1",
|
||||||
"eslint": "^8.5.0",
|
"eslint": "^8.5.0",
|
||||||
"eslint-plugin-babel": "^5.3.1"
|
"eslint-plugin-babel": "^5.3.1",
|
||||||
|
"events": "^3.3.0",
|
||||||
|
"readable-stream": "^3.6.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"prepublish": "node build.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
pool.js
16
pool.js
@@ -26,16 +26,15 @@ export function relayPool() {
|
|||||||
|
|
||||||
const activeSubscriptions = {}
|
const activeSubscriptions = {}
|
||||||
|
|
||||||
const sub = (
|
const sub = ({cb, filter, beforeSend}, id) => {
|
||||||
{cb, filter, beforeSend},
|
if (!id) id = Math.random().toString().slice(2)
|
||||||
id = Math.random().toString().slice(2)
|
|
||||||
) => {
|
|
||||||
const subControllers = Object.fromEntries(
|
const subControllers = Object.fromEntries(
|
||||||
Object.values(relays)
|
Object.values(relays)
|
||||||
.filter(({policy}) => policy.read)
|
.filter(({policy}) => policy.read)
|
||||||
.map(({relay}) => [
|
.map(({relay}) => [
|
||||||
relay.url,
|
relay.url,
|
||||||
relay.sub({filter, cb: event => cb(event, relay.url), beforeSend}, id)
|
relay.sub({cb: event => cb(event, relay.url), filter, beforeSend}, id)
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,12 +53,15 @@ export function relayPool() {
|
|||||||
}) => {
|
}) => {
|
||||||
Object.entries(subControllers).map(([relayURL, sub]) => [
|
Object.entries(subControllers).map(([relayURL, sub]) => [
|
||||||
relayURL,
|
relayURL,
|
||||||
sub.sub({cb, filter, beforeSend}, id)
|
sub.sub({cb: event => cb(event, relayURL), filter, beforeSend}, id)
|
||||||
])
|
])
|
||||||
return activeSubscriptions[id]
|
return activeSubscriptions[id]
|
||||||
}
|
}
|
||||||
const addRelay = relay => {
|
const addRelay = relay => {
|
||||||
subControllers[relay.url] = relay.sub({cb, filter}, id)
|
subControllers[relay.url] = relay.sub(
|
||||||
|
{cb: event => cb(event, relay.url), filter, beforeSend},
|
||||||
|
id
|
||||||
|
)
|
||||||
return activeSubscriptions[id]
|
return activeSubscriptions[id]
|
||||||
}
|
}
|
||||||
const removeRelay = relayURL => {
|
const removeRelay = relayURL => {
|
||||||
|
|||||||
Reference in New Issue
Block a user