Move types into their own file, import them

This commit is contained in:
Alex Gleason 2023-03-24 13:19:35 -05:00
parent 7e503a3892
commit e3df8579a0
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
6 changed files with 34 additions and 119 deletions

View File

@ -3,33 +3,11 @@
import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts';
import { Keydb } from 'https://deno.land/x/keydb@1.0.0/sqlite.ts'; import { Keydb } from 'https://deno.land/x/keydb@1.0.0/sqlite.ts';
import type { InputMessage, OutputMessage } from '../types.ts';
const ANTI_DUPLICATION_TTL = Number(Deno.env.get('ANTI_DUPLICATION_TTL') || 60000); const ANTI_DUPLICATION_TTL = Number(Deno.env.get('ANTI_DUPLICATION_TTL') || 60000);
const ANTI_DUPLICATION_MIN_LENGTH = Number(Deno.env.get('ANTI_DUPLICATION_MIN_LENGTH') || 50); const ANTI_DUPLICATION_MIN_LENGTH = Number(Deno.env.get('ANTI_DUPLICATION_MIN_LENGTH') || 50);
interface InputMessage {
type: 'new' | 'lookback';
event: Event;
receivedAt: number;
sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync';
sourceInfo: string;
}
interface OutputMessage {
id: string;
action: 'accept' | 'reject' | 'shadowReject';
msg: string;
}
interface Event {
id: string;
sig: string;
kind: number;
tags: string[][];
pubkey: string;
content: string;
created_at: number;
}
/** https://stackoverflow.com/a/8831937 */ /** https://stackoverflow.com/a/8831937 */
function hashCode(str: string): number { function hashCode(str: string): number {
let hash = 0; let hash = 0;

View File

@ -1,32 +1,10 @@
#!/usr/bin/env -S deno run #!/usr/bin/env -S deno run
import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts';
import type { InputMessage, OutputMessage } from '../types.ts';
const HELLTHREAD_LIMIT = Number(Deno.env.get('HELLTHREAD_LIMIT') || 100); const HELLTHREAD_LIMIT = Number(Deno.env.get('HELLTHREAD_LIMIT') || 100);
interface InputMessage {
type: 'new' | 'lookback';
event: Event;
receivedAt: number;
sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync';
sourceInfo: string;
}
interface OutputMessage {
id: string;
action: 'accept' | 'reject' | 'shadowReject';
msg: string;
}
interface Event {
id: string;
sig: string;
kind: number;
tags: string[][];
pubkey: string;
content: string;
created_at: number;
}
function handleMessage(msg: InputMessage): OutputMessage { function handleMessage(msg: InputMessage): OutputMessage {
if (msg.event.kind === 1) { if (msg.event.kind === 1) {
const p = msg.event.tags.filter((tag) => tag[0] === 'p'); const p = msg.event.tags.filter((tag) => tag[0] === 'p');

View File

@ -1,29 +1,7 @@
#!/usr/bin/env -S deno run #!/usr/bin/env -S deno run
import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts';
interface InputMessage { import type { InputMessage, OutputMessage } from '../types.ts';
type: 'new' | 'lookback';
event: Event;
receivedAt: number;
sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync';
sourceInfo: string;
}
interface OutputMessage {
id: string;
action: 'accept' | 'reject' | 'shadowReject';
msg: string;
}
interface Event {
id: string;
sig: string;
kind: number;
tags: string[][];
pubkey: string;
content: string;
created_at: number;
}
function handleMessage(msg: InputMessage): OutputMessage { function handleMessage(msg: InputMessage): OutputMessage {
return { return {

View File

@ -3,35 +3,13 @@
import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts';
import { Keydb } from 'https://deno.land/x/keydb@1.0.0/sqlite.ts'; import { Keydb } from 'https://deno.land/x/keydb@1.0.0/sqlite.ts';
import type { InputMessage, OutputMessage } from '../types.ts';
const IP_WHITELIST = (Deno.env.get('IP_WHITELIST') || '').split(','); const IP_WHITELIST = (Deno.env.get('IP_WHITELIST') || '').split(',');
const RATE_LIMIT_INTERVAL = Number(Deno.env.get('RATE_LIMIT_INTERVAL') || 60000); const RATE_LIMIT_INTERVAL = Number(Deno.env.get('RATE_LIMIT_INTERVAL') || 60000);
const RATE_LIMIT_MAX = Number(Deno.env.get('RATE_LIMIT_MAX') || 10); const RATE_LIMIT_MAX = Number(Deno.env.get('RATE_LIMIT_MAX') || 10);
interface InputMessage {
type: 'new' | 'lookback';
event: Event;
receivedAt: number;
sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync';
sourceInfo: string;
}
interface OutputMessage {
id: string;
action: 'accept' | 'reject' | 'shadowReject';
msg: string;
}
interface Event {
id: string;
sig: string;
kind: number;
tags: string[][];
pubkey: string;
content: string;
created_at: number;
}
async function handleMessage(msg: InputMessage): Promise<OutputMessage> { async function handleMessage(msg: InputMessage): Promise<OutputMessage> {
if ((msg.sourceType === 'IP4' || msg.sourceType === 'IP6') && !IP_WHITELIST.includes(msg.sourceInfo)) { if ((msg.sourceType === 'IP4' || msg.sourceType === 'IP6') && !IP_WHITELIST.includes(msg.sourceInfo)) {
const db = new Keydb('sqlite:///tmp/strfry-rate-limit-policy.sqlite3'); const db = new Keydb('sqlite:///tmp/strfry-rate-limit-policy.sqlite3');

View File

@ -2,29 +2,7 @@
//bin/true; exec deno run -A "$0" "$@" //bin/true; exec deno run -A "$0" "$@"
import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts';
interface InputMessage { import type { InputMessage, OutputMessage } from '../types.ts';
type: 'new' | 'lookback';
event: Event;
receivedAt: number;
sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync';
sourceInfo: string;
}
interface OutputMessage {
id: string;
action: 'accept' | 'reject' | 'shadowReject';
msg: string;
}
interface Event {
id: string;
sig: string;
kind: number;
tags: string[][];
pubkey: string;
content: string;
created_at: number;
}
function handleMessage(msg: InputMessage): OutputMessage { function handleMessage(msg: InputMessage): OutputMessage {
return { return {

25
src/types.ts Normal file
View File

@ -0,0 +1,25 @@
interface InputMessage {
type: 'new' | 'lookback';
event: Event;
receivedAt: number;
sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync';
sourceInfo: string;
}
interface OutputMessage {
id: string;
action: 'accept' | 'reject' | 'shadowReject';
msg: string;
}
interface Event<K extends number = number> {
id: string;
sig: string;
kind: K;
tags: string[][];
pubkey: string;
content: string;
created_at: number;
}
export type { Event, InputMessage, OutputMessage };