Compare commits

..

5 Commits

Author SHA1 Message Date
fiatjaf
1b31a27d89 ensure types are emitted when publishing. 2023-12-20 10:51:41 -03:00
fiatjaf
0cc3c02d84 fix fix yield. 2023-12-20 10:49:08 -03:00
Shusui MOYATANI
8625d45152 fix yield 2023-12-20 09:27:49 -03:00
fiatjaf
8f03116687 tweak readme. 2023-12-19 14:21:04 -03:00
fiatjaf
e6d1808fda update readme to mention fragment importing and nostr-wasm. 2023-12-19 14:14:40 -03:00
5 changed files with 49 additions and 5 deletions

View File

@@ -35,7 +35,7 @@ let event = finalizeEvent({
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: 'hello',
}, privateKey)
}, sk)
let isGood = verifyEvent(event)
```
@@ -202,6 +202,36 @@ assert(data.pubkey === pk)
assert(data.relays.length === 2)
```
## Import modes
### Using just the packages you want
Importing the entirety of `nostr-tools` may bloat your build, so you should probably import individual packages instead:
```js
import { generateSecretKey, finalizeEvent, verifyEvent } from 'nostr-tools/pure'
import { matchFilter } from 'nostr-tools/filter'
import { decode, nprofileEncode, neventEncode, npubEncode } from 'nostr-tools/nip19'
// and so on and so forth
```
### Using it with `nostr-wasm`
[`nostr-wasm`](https://github.com/fiatjaf/nostr-wasm) is a thin wrapper over [libsecp256k1](https://github.com/bitcoin-core/secp256k1) compiled to WASM just for hashing, signing and verifying Nostr events.
```js
import { setNostrWasm, generateSecretKey, finalizeEvent, verifyEvent } from 'nostr-tools/wasm'
import { initNostrWasm } from 'nostr-wasm'
// make sure this promise resolves before your app starts calling finalizeEvent or verifyEvent
initNostrWasm().then(setNostrWasm)
// or use 'nostr-wasm/gzipped' or even 'nostr-wasm/headless',
// see https://www.npmjs.com/package/nostr-wasm for options
```
This may be faster than the pure-JS [noble libraries](https://paulmillr.com/noble/) used by default and in `nostr-tools/pure`.
### Using from the browser (if you don't want to use a bundler)
```html
@@ -213,8 +243,7 @@ assert(data.relays.length === 2)
## Plumbing
1. Install [`just`](https://just.systems/)
2. `just -l`
To develop `nostr-tools`, install [`just`](https://just.systems/) and run `just -l` to see commands available.
## License

View File

@@ -8,6 +8,8 @@ const entryPoints = fs
file =>
file.endsWith('.ts') &&
file !== 'core.ts' &&
file !== 'test-helpers.ts' &&
file !== 'helpers.ts' &&
!file.endsWith('.test.ts') &&
fs.statSync(join(process.cwd(), file)).isFile(),
)

9
helpers.ts Normal file
View File

@@ -0,0 +1,9 @@
export async function yieldThread() {
return new Promise(resolve => {
const ch = new MessageChannel()
// @ts-ignore (typescript thinks this property should be called `addListener`, but in fact it's `addEventListener`)
ch.port1.addEventListener('message', resolve)
ch.port2.postMessage(0)
ch.port1.start()
})
}

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "2.0.0",
"version": "2.0.1",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",
@@ -187,5 +187,8 @@
"prettier": "^3.0.3",
"tsd": "^0.22.0",
"typescript": "^5.0.4"
},
"scripts": {
"prepublish": "just build && just emit-types"
}
}

View File

@@ -5,6 +5,7 @@ import { matchFilters, type Filter } from './filter.ts'
import { getHex64, getSubscriptionId } from './fakejson.ts'
import { Queue, normalizeURL } from './utils.ts'
import { nip42 } from './index.ts'
import { yieldThread } from './helpers.ts'
export function relayConnect(url: string) {
const relay = new Relay(url)
@@ -117,7 +118,7 @@ export class Relay {
if (false === this.handleNext()) {
break
}
await Promise.resolve()
await yieldThread()
}
this.queueRunning = false
}