strfry can send multiple messages at once (!!)

This commit is contained in:
Alex Gleason 2023-03-25 11:38:27 -05:00
parent 8c8c969486
commit b279e042e1
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 34 additions and 33 deletions

View File

@ -34,17 +34,18 @@ import {
rateLimitPolicy,
readStdin,
writeStdout,
} from 'https://gitlab.com/soapbox-pub/strfry-policies/-/raw/develop/mod.ts';
} from './mod.ts';
const msg = await readStdin();
for await (const msg of readStdin()) {
const result = await pipeline(msg, [
[hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
]);
const result = await pipeline(msg, [
[hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
]);
writeStdout(result);
}
writeStdout(result);
```
Finally, edit `strfry.conf` and enable the policy:
@ -110,22 +111,21 @@ Once you're done, you can either upload the file somewhere online or directly to
```diff
--- a/strfry-policy.ts
+++ b/strfry-policy.ts
@@ -9,6 +9,7 @@ import {
@@ -8,12 +8,14 @@ import {
readStdin,
writeStdout,
} from 'https://gitlab.com/soapbox-pub/strfry-policies/-/raw/develop/mod.ts';
} from './mod.ts';
+import { americanPolicy } from 'https://gist.githubusercontent.com/alexgleason/5c2d084434fa0875397f44da198f4352/raw/3d3ce71c7ed9cef726f17c3a102c378b81760a45/american-policy.ts';
const msg = await readStdin();
for await (const msg of readStdin()) {
const result = await pipeline(msg, [
[hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
+ americanPolicy,
]);
@@ -17,6 +18,7 @@ const result = await pipeline(msg, [
[hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
+ americanPolicy,
]);
writeStdout(result);
writeStdout(result);
```
### Policy options

View File

@ -10,13 +10,13 @@ import {
writeStdout,
} from './mod.ts';
const msg = await readStdin();
for await (const msg of readStdin()) {
const result = await pipeline(msg, [
noopPolicy,
[hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
]);
const result = await pipeline(msg, [
noopPolicy,
[hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
]);
writeStdout(result);
writeStdout(result);
}

View File

@ -3,12 +3,13 @@ import { readLines } from './deps.ts';
import type { InputMessage, OutputMessage } from './types.ts';
/**
* Get the first line from stdin.
* Can only be read ONCE, or else it returns undefined.
* Parse strfy messages from stdin.
* strfry may batch multiple messages at once.
*/
async function readStdin(): Promise<InputMessage> {
const { value } = await readLines(Deno.stdin).next();
return JSON.parse(value);
async function* readStdin(): AsyncGenerator<InputMessage> {
for await (const line of readLines(Deno.stdin)) {
yield JSON.parse(line);
}
}
/** Writes the output message to stdout. */