Add pipeline, scope out entrypoint.example.ts

This commit is contained in:
Alex Gleason 2023-03-24 16:08:36 -05:00
parent 8e41f41002
commit b5fe0c5e67
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 47 additions and 5 deletions

View File

@ -1,4 +1,22 @@
#!/bin/sh #!/bin/sh
//bin/true; exec deno run -A "$0" "$@" //bin/true; exec deno run -A "$0" "$@"
import {
antiDuplicationPolicy,
hellthreadPolicy,
noopPolicy,
pipeline,
rateLimitPolicy,
readStdin,
writeStdout,
} from './mod.ts';
// TODO const msg = await readStdin();
const result = await pipeline(msg, [
noopPolicy,
hellthreadPolicy,
antiDuplicationPolicy,
rateLimitPolicy,
]);
writeStdout(result);

3
mod.ts
View File

@ -3,3 +3,6 @@ export { default as hellthreadPolicy } from './src/policies/hellthread-policy.ts
export { default as noopPolicy } from './src/policies/noop-policy.ts'; export { default as noopPolicy } from './src/policies/noop-policy.ts';
export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts'; export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts';
export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts'; export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts';
export { readStdin, writeStdout } from './src/io.ts';
export { default as pipeline } from './src/pipeline.ts';

View File

@ -1,6 +1,6 @@
import { readLines } from './deps.ts'; import { readLines } from './deps.ts';
import type { InputMessage } from './types.ts'; import type { InputMessage, OutputMessage } from './types.ts';
/** /**
* Get the first line from stdin. * Get the first line from stdin.
@ -11,4 +11,9 @@ async function readStdin(): Promise<InputMessage> {
return JSON.parse(value); return JSON.parse(value);
} }
export { readStdin }; /** Writes the output message to stdout. */
function writeStdout(msg: OutputMessage): void {
console.log(JSON.stringify(msg));
}
export { readStdin, writeStdout };

View File

@ -1,3 +1,19 @@
import { readStdin } from './stdin.ts'; import { InputMessage, OutputMessage, Policy } from './types.ts';
console.log(await readStdin()); /** Processes messages through multiple policies, bailing early on rejection. */
async function pipeline(msg: InputMessage, policies: Policy[]): Promise<OutputMessage> {
for (const policy of policies) {
const result = await policy(msg);
if (result.action !== 'accept') {
return result;
}
}
return {
id: msg.event.id,
action: 'accept',
msg: '',
};
}
export default pipeline;