@nostrify/policies 0.36.13 → 0.36.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +32 -32
- package/.turbo/turbo-typecheck.log +6 -0
- package/CHANGELOG.md +18 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -3
- package/.turbo/turbo-setup.log +0 -22
- package/.turbo/turbo-test.log +0 -53
- package/dist/AntiDuplicationPolicy.ts +0 -82
- package/dist/AnyPolicy.ts +0 -24
- package/dist/AuthorPolicy.ts +0 -29
- package/dist/DomainPolicy.ts +0 -96
- package/dist/FiltersPolicy.ts +0 -30
- package/dist/HashtagPolicy.ts +0 -28
- package/dist/HellthreadPolicy.ts +0 -30
- package/dist/InvertPolicy.ts +0 -23
- package/dist/KeywordPolicy.ts +0 -28
- package/dist/NoOpPolicy.ts +0 -9
- package/dist/OpenAIPolicy.ts +0 -116
- package/dist/PipePolicy.ts +0 -39
- package/dist/PowPolicy.ts +0 -44
- package/dist/PubkeyBanPolicy.ts +0 -27
- package/dist/ReadOnlyPolicy.ts +0 -9
- package/dist/RegexPolicy.ts +0 -25
- package/dist/ReplyBotPolicy.ts +0 -62
- package/dist/SizePolicy.ts +0 -39
- package/dist/WhitelistPolicy.ts +0 -36
- package/dist/WoTPolicy.ts +0 -64
- package/dist/mod.ts +0 -20
package/dist/PubkeyBanPolicy.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Ban events from individual pubkeys.
|
|
5
|
-
*
|
|
6
|
-
* ```ts
|
|
7
|
-
* // Ban a specific pubkey.
|
|
8
|
-
* new PubkeyBanPolicy(['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']);
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
export class PubkeyBanPolicy implements NPolicy {
|
|
12
|
-
private pubkeys: Iterable<string>;
|
|
13
|
-
constructor(pubkeys: Iterable<string>) {
|
|
14
|
-
this.pubkeys = pubkeys;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// deno-lint-ignore require-await
|
|
18
|
-
async call({ id, pubkey }: NostrEvent): Promise<NostrRelayOK> {
|
|
19
|
-
for (const p of this.pubkeys) {
|
|
20
|
-
if (p === pubkey) {
|
|
21
|
-
return ['OK', id, false, 'blocked: pubkey is banned'];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return ['OK', id, true, ''];
|
|
26
|
-
}
|
|
27
|
-
}
|
package/dist/ReadOnlyPolicy.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/types';
|
|
2
|
-
|
|
3
|
-
/** This policy rejects all messages. */
|
|
4
|
-
export class ReadOnlyPolicy implements NPolicy {
|
|
5
|
-
// deno-lint-ignore require-await
|
|
6
|
-
async call(event: NostrEvent): Promise<NostrRelayOK> {
|
|
7
|
-
return ['OK', event.id, false, 'blocked: the relay is read-only'];
|
|
8
|
-
}
|
|
9
|
-
}
|
package/dist/RegexPolicy.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Reject events whose content matches the regex.
|
|
5
|
-
*
|
|
6
|
-
* ```ts
|
|
7
|
-
* // Ban events matching a regex.
|
|
8
|
-
* new RegexPolicy(/(🟠|🔥|😳)ChtaGPT/i);
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
export class RegexPolicy implements NPolicy {
|
|
12
|
-
private regex: RegExp;
|
|
13
|
-
constructor(regex: RegExp) {
|
|
14
|
-
this.regex = regex;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// deno-lint-ignore require-await
|
|
18
|
-
async call({ id, content }: NostrEvent): Promise<NostrRelayOK> {
|
|
19
|
-
if (this.regex.test(content)) {
|
|
20
|
-
return ['OK', id, false, 'blocked: text matches a banned expression'];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return ['OK', id, true, ''];
|
|
24
|
-
}
|
|
25
|
-
}
|
package/dist/ReplyBotPolicy.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, NostrRelayOK, NPolicy, NStore } from '@nostrify/types';
|
|
2
|
-
|
|
3
|
-
/** Options for the ReplyBotPolicy. */
|
|
4
|
-
export interface ReplyBotPolicyOpts {
|
|
5
|
-
/** The store to use for fetching events. */
|
|
6
|
-
store: NStore;
|
|
7
|
-
/** The minimum time in seconds between two posts. */
|
|
8
|
-
threshold?: number;
|
|
9
|
-
/** The kinds of events to apply the policy to. */
|
|
10
|
-
kinds?: number[];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** Block events that reply too quickly to another event. */
|
|
14
|
-
export class ReplyBotPolicy implements NPolicy {
|
|
15
|
-
private opts: ReplyBotPolicyOpts;
|
|
16
|
-
constructor(opts: ReplyBotPolicyOpts) {
|
|
17
|
-
this.opts = opts;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async call(event: NostrEvent, signal?: AbortSignal): Promise<NostrRelayOK> {
|
|
21
|
-
const { store, threshold = 1, kinds = [1] } = this.opts;
|
|
22
|
-
|
|
23
|
-
if (kinds.includes(event.kind)) {
|
|
24
|
-
const [, replyToId] = ReplyBotPolicy.findReplyTag(event.tags) ?? [];
|
|
25
|
-
|
|
26
|
-
if (replyToId) {
|
|
27
|
-
const [prevEvent] = await store.query([{ ids: [replyToId] }], { signal });
|
|
28
|
-
|
|
29
|
-
if (prevEvent) {
|
|
30
|
-
const diff = event.created_at - prevEvent.created_at;
|
|
31
|
-
const pTag = prevEvent.tags.find(([name, value]: string[]) => name === 'p' && value === event.pubkey);
|
|
32
|
-
|
|
33
|
-
if (diff <= threshold && !pTag) {
|
|
34
|
-
return ['OK', event.id, false, 'rate-limited: replied too quickly'];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return ['OK', event.id, true, ''];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Tag is a NIP-10 root tag. */
|
|
44
|
-
private static isRootTag(tag: string[]): tag is ['e', string, string, 'root', ...string[]] {
|
|
45
|
-
return tag[0] === 'e' && tag[3] === 'root';
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Tag is a NIP-10 reply tag. */
|
|
49
|
-
private static isReplyTag(tag: string[]): tag is ['e', string, string, 'reply', ...string[]] {
|
|
50
|
-
return tag[0] === 'e' && tag[3] === 'reply';
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Tag is an "e" tag without a NIP-10 marker. */
|
|
54
|
-
private static isLegacyReplyTag(tag: string[]): tag is ['e', string, string] {
|
|
55
|
-
return tag[0] === 'e' && !tag[3];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Get the "e" tag for the event being replied to, first according to the NIPs then falling back to the legacy way. */
|
|
59
|
-
private static findReplyTag(tags: string[][]): ['e', ...string[]] | undefined {
|
|
60
|
-
return tags.find(this.isReplyTag) || tags.find(this.isRootTag) || tags.findLast(this.isLegacyReplyTag);
|
|
61
|
-
}
|
|
62
|
-
}
|
package/dist/SizePolicy.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/types';
|
|
2
|
-
|
|
3
|
-
/** Policy options for `SizePolicy`. */
|
|
4
|
-
interface SizePolicyOpts {
|
|
5
|
-
/** Maximum size of the message content in bytes. Default: 8192 (8KB) */
|
|
6
|
-
maxBytes?: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Reject events larger than a specified size in bytes.
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* // Reject events larger than the default size (8KB) .
|
|
14
|
-
* new SizePolicy();
|
|
15
|
-
* // Reject events larger than a custom size (15KB).
|
|
16
|
-
* new SizePolicy({ maxBytes: 15 * 1024 });
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export class SizePolicy implements NPolicy {
|
|
20
|
-
private opts: SizePolicyOpts;
|
|
21
|
-
|
|
22
|
-
constructor(opts: SizePolicyOpts = {}) {
|
|
23
|
-
this.opts = opts;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// deno-lint-ignore require-await
|
|
27
|
-
async call(event: NostrEvent): Promise<NostrRelayOK> {
|
|
28
|
-
const { maxBytes = 8 * 1024 } = this.opts;
|
|
29
|
-
|
|
30
|
-
const json = JSON.stringify(event);
|
|
31
|
-
const size = new TextEncoder().encode(json).length;
|
|
32
|
-
|
|
33
|
-
if (size > maxBytes) {
|
|
34
|
-
return ['OK', event.id, false, `blocked: event is too large`];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return ['OK', event.id, true, ''];
|
|
38
|
-
}
|
|
39
|
-
}
|
package/dist/WhitelistPolicy.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, NostrRelayInfo, NostrRelayOK, NPolicy } from '@nostrify/types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Allows only the listed pubkeys to post. All other events are rejected.
|
|
5
|
-
*
|
|
6
|
-
* ```ts
|
|
7
|
-
* // Only the given pubkey may post.
|
|
8
|
-
* new WhitelistPolicy(['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']);
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
export class WhitelistPolicy implements NPolicy {
|
|
12
|
-
private pubkeys: Iterable<string>;
|
|
13
|
-
|
|
14
|
-
constructor(pubkeys: Iterable<string>) {
|
|
15
|
-
this.pubkeys = pubkeys;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// deno-lint-ignore require-await
|
|
19
|
-
async call({ id, pubkey }: NostrEvent): Promise<NostrRelayOK> {
|
|
20
|
-
for (const p of this.pubkeys) {
|
|
21
|
-
if (p === pubkey) {
|
|
22
|
-
return ['OK', id, true, ''];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return ['OK', id, false, 'blocked: only certain pubkeys are allowed to post'];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
get info(): NostrRelayInfo {
|
|
30
|
-
return {
|
|
31
|
-
limitation: {
|
|
32
|
-
restricted_writes: true,
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
}
|
package/dist/WoTPolicy.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, NostrRelayOK, NPolicy, NStore } from '@nostrify/types';
|
|
2
|
-
|
|
3
|
-
/** Options for the `WoTPolicy`. */
|
|
4
|
-
interface WoTPolicyOpts {
|
|
5
|
-
/** Store to get kind 3 follow lists from. */
|
|
6
|
-
store: NStore;
|
|
7
|
-
/** Initial set of trusted pubkeys to query follow lists for. */
|
|
8
|
-
pubkeys: Iterable<string>;
|
|
9
|
-
/**
|
|
10
|
-
* How many levels of follow lists to query.
|
|
11
|
-
* `0` will just whitelist the given `pubkeys` without checking their follow lists.
|
|
12
|
-
* `1` will query their follows,
|
|
13
|
-
* `2` will query their follows follows, etc.
|
|
14
|
-
*/
|
|
15
|
-
depth: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** Whitelist pubkeys the given user follows, people those users follow, etc. up to `depth`. */
|
|
19
|
-
export class WoTPolicy implements NPolicy {
|
|
20
|
-
private pubkeys: Promise<Set<string>> | undefined;
|
|
21
|
-
private opts: WoTPolicyOpts;
|
|
22
|
-
|
|
23
|
-
constructor(opts: WoTPolicyOpts) {
|
|
24
|
-
this.opts = opts;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async call(event: NostrEvent): Promise<NostrRelayOK> {
|
|
28
|
-
this.pubkeys ??= this.getPubkeys();
|
|
29
|
-
const pubkeys = await this.pubkeys;
|
|
30
|
-
|
|
31
|
-
if (pubkeys.has(event.pubkey)) {
|
|
32
|
-
return ['OK', event.id, true, ''];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return ['OK', event.id, false, 'blocked: only certain pubkeys are allowed to post'];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Retrieve the complete set of pubkeys to whitelist. */
|
|
39
|
-
private async getPubkeys(): Promise<Set<string>> {
|
|
40
|
-
const { store, depth } = this.opts;
|
|
41
|
-
|
|
42
|
-
const pubkeys = new Set<string>([...this.opts.pubkeys]);
|
|
43
|
-
const authors = new Set(pubkeys);
|
|
44
|
-
|
|
45
|
-
for (let i = 0; i < depth; i++) {
|
|
46
|
-
const events = await store.query([{ kinds: [3], authors: [...authors] }]);
|
|
47
|
-
|
|
48
|
-
authors.clear();
|
|
49
|
-
|
|
50
|
-
for (const event of events) {
|
|
51
|
-
for (const [name, value] of event.tags) {
|
|
52
|
-
if (name === 'p') {
|
|
53
|
-
if (!pubkeys.has(value)) { // Avoid infinite loops.
|
|
54
|
-
authors.add(value);
|
|
55
|
-
}
|
|
56
|
-
pubkeys.add(value);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return pubkeys;
|
|
63
|
-
}
|
|
64
|
-
}
|
package/dist/mod.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export { AntiDuplicationPolicy } from './AntiDuplicationPolicy.ts';
|
|
2
|
-
export { AnyPolicy } from './AnyPolicy.ts';
|
|
3
|
-
export { AuthorPolicy } from './AuthorPolicy.ts';
|
|
4
|
-
export { DomainPolicy } from './DomainPolicy.ts';
|
|
5
|
-
export { FiltersPolicy } from './FiltersPolicy.ts';
|
|
6
|
-
export { HashtagPolicy } from './HashtagPolicy.ts';
|
|
7
|
-
export { HellthreadPolicy } from './HellthreadPolicy.ts';
|
|
8
|
-
export { InvertPolicy } from './InvertPolicy.ts';
|
|
9
|
-
export { KeywordPolicy } from './KeywordPolicy.ts';
|
|
10
|
-
export { NoOpPolicy } from './NoOpPolicy.ts';
|
|
11
|
-
export { OpenAIPolicy } from './OpenAIPolicy.ts';
|
|
12
|
-
export { PipePolicy } from './PipePolicy.ts';
|
|
13
|
-
export { PowPolicy } from './PowPolicy.ts';
|
|
14
|
-
export { PubkeyBanPolicy } from './PubkeyBanPolicy.ts';
|
|
15
|
-
export { ReadOnlyPolicy } from './ReadOnlyPolicy.ts';
|
|
16
|
-
export { RegexPolicy } from './RegexPolicy.ts';
|
|
17
|
-
export { ReplyBotPolicy } from './ReplyBotPolicy.ts';
|
|
18
|
-
export { SizePolicy } from './SizePolicy.ts';
|
|
19
|
-
export { WhitelistPolicy } from './WhitelistPolicy.ts';
|
|
20
|
-
export { WoTPolicy } from './WoTPolicy.ts';
|