From b279e042e13e8011cef3f4c2b1808b468e04c4fc Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 25 Mar 2023 11:38:27 -0500 Subject: [PATCH] strfry can send multiple messages at once (!!) --- README.md | 38 +++++++++++++++++++------------------- entrypoint.example.ts | 18 +++++++++--------- src/io.ts | 11 ++++++----- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 4862492..837a119 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/entrypoint.example.ts b/entrypoint.example.ts index 875dd85..75a23f8 100644 --- a/entrypoint.example.ts +++ b/entrypoint.example.ts @@ -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); +} diff --git a/src/io.ts b/src/io.ts index d62aa8c..5303d3e 100644 --- a/src/io.ts +++ b/src/io.ts @@ -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 { - const { value } = await readLines(Deno.stdin).next(); - return JSON.parse(value); +async function* readStdin(): AsyncGenerator { + for await (const line of readLines(Deno.stdin)) { + yield JSON.parse(line); + } } /** Writes the output message to stdout. */