Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const {boxFactory} = require('bakeryjs');
const {promisify} = require('util');
const timeout = promisify(setTimeout);
// boxFactory creates a component,
// it must be the default export of the module
module.exports = boxFactory(
// Component's metadata
{
provides: ['msg'], // What kind of data this component provides?
requires: [], // What kind of data this component requires to work?
emits: ['msg'], // This component can emit multiple messages at once, which must be reflected in this property
aggregates: false, // Whether the component can aggregate multiple messages
},
// Logic of the component, can be async function which emits data over time
async function(serviceProvider, value, emit) {
// Each message is an object with key corresponding to 'emits' property in metadata
emit([{msg: 'Hello world!'}]);
await timeout(230);
// Component can emit multiple messages at once
emit([{msg: '¡Hola mundo!'}, {msg: 'Ahoj světe!'}]);
await timeout(150);
emit([{msg: 'Hallo Welt!'}, {msg: 'Bonjour monde!'}]);
const {boxFactory} = require('bakeryjs');
// Processor component which calculates number of words in a message
module.exports = boxFactory(
{
provides: ['words'],
requires: ['msg'],
emits: [],
aggregates: false,
},
// serviceParamsProvider may contain some shared modules, like logger
// second parameter is an object with properties corresponding to 'requires'
function(serviceProvider, {msg}) {
return {words: msg.split(/\W+/).length};
}
);
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
module.exports = boxFactory(
{
provides: ['words'],
requires: ['msg'],
emits: [],
aggregates: false,
},
function(serviceProvider: ServiceProvider, value: MessageData) {
return {words: (value.msg as string).split(/\W+/).length};
}
);
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
module.exports = boxFactory(
{
provides: ['words'],
requires: ['msg'],
aggregates: false,
batch: {
maxSize: 3,
timeoutSeconds: 0.3,
},
},
async function(serviceProvider: ServiceProvider, values: MessageData[]) {
return values.map((value) =>
Object.create(null, {
words: {value: (value.msg as string).split(/\W+/).length},
})
);
}
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
module.exports = boxFactory(
{
provides: ['msg'],
requires: [],
emits: ['msg'],
aggregates: false,
},
async function(
serviceProvider: ServiceProvider,
value: MessageData,
emit: (chunk: MessageData[], priority?: number) => void
) {
return new Promise((resolve) => {
emit([{msg: 'Hello World!'}, {msg: 'Yellow World!'}]);
setTimeout(
() => emit([{msg: 'Hallo Welt!'}, {msg: 'Salut Monde!'}]),
150
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
module.exports = boxFactory(
{
provides: ['words'],
requires: ['msg'],
aggregates: false,
batch: {
maxSize: 3,
timeoutSeconds: 0.1,
},
},
async function(serviceProvider: ServiceProvider, values: MessageData[]) {
const vals = values.map((value) =>
Object.create(null, {
words: {value: (value.msg as string).split(/\W+/).length},
})
);
await new Promise((resolve) => setTimeout(resolve, 1200));
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
module.exports = boxFactory(
{
provides: ['msg'],
requires: [],
emits: ['msg'],
aggregates: false,
},
async function(
serviceProvider: ServiceProvider,
value: MessageData,
emit: (chunk: MessageData[], priority?: number) => void
) {
emit([{msg: 'Hello World!'}]);
return;
}
);
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
module.exports = boxFactory(
{
provides: ['checksum'],
requires: ['words', 'punct'],
emits: [],
aggregates: false,
parameters: {
title: 'Parameter of the box -- positive number',
type: 'number',
minimum: 0,
},
},
function(serviceProvider: ServiceProvider, value: MessageData) {
return {
checksum:
Math.sqrt(serviceProvider.parameters || 2) * value.words +
value.punct,
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
const Print = boxFactory(
{
requires: ['jobId', 'raw'],
provides: [],
emits: [],
aggregates: false,
},
function processValue(
services: ServiceProvider,
input: MessageData,
neverEmit: (chunk: MessageData[], priority?: number) => void
): MessageData {
services.get('logger').log({printBox: JSON.stringify(input)});
return {};
}
);
export default Print;
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';
const Tick = boxFactory(
{
requires: ['jobId'],
provides: ['raw'],
emits: ['tick'],
aggregates: false,
},
function processValue(
serviceProvider: ServiceProvider,
value: MessageData,
emitCallback: (chunk: MessageData[], priority?: number) => void
): Promise {
let i = 0;
return new Promise((resolve: (result?: any) => void): void => {
const id = setInterval((): void => {
if (i >= 3) {
clearInterval(id);