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,10 +34,9 @@ import {
rateLimitPolicy, rateLimitPolicy,
readStdin, readStdin,
writeStdout, 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, [ const result = await pipeline(msg, [
[hellthreadPolicy, { limit: 100 }], [hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }], [antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
@ -45,6 +44,8 @@ const result = await pipeline(msg, [
]); ]);
writeStdout(result); writeStdout(result);
}
``` ```
Finally, edit `strfry.conf` and enable the policy: Finally, edit `strfry.conf` and enable the policy:
@ -110,15 +111,14 @@ Once you're done, you can either upload the file somewhere online or directly to
```diff ```diff
--- a/strfry-policy.ts --- a/strfry-policy.ts
+++ b/strfry-policy.ts +++ b/strfry-policy.ts
@@ -9,6 +9,7 @@ import { @@ -8,12 +8,14 @@ import {
readStdin, readStdin,
writeStdout, 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'; +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, [
@@ -17,6 +18,7 @@ const result = await pipeline(msg, [
[hellthreadPolicy, { limit: 100 }], [hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }], [antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }], [rateLimitPolicy, { whitelist: ['127.0.0.1'] }],

View File

@ -10,8 +10,7 @@ import {
writeStdout, writeStdout,
} from './mod.ts'; } from './mod.ts';
const msg = await readStdin(); for await (const msg of readStdin()) {
const result = await pipeline(msg, [ const result = await pipeline(msg, [
noopPolicy, noopPolicy,
[hellthreadPolicy, { limit: 100 }], [hellthreadPolicy, { limit: 100 }],
@ -20,3 +19,4 @@ const result = await pipeline(msg, [
]); ]);
writeStdout(result); writeStdout(result);
}

View File

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