npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@onify/flow-extensions

v8.3.1

Published

Onify Flow extensions

Downloads

395

Readme

Onify Flow Extensions

Built latestCoverage Status

Api

  • extensions: Flow extensions
  • extendFn: extend function to pass to serializer

Examples

Bpmn engine example

import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { Engine } from 'bpmn-engine';
import { extensions } from '@onify/flow-extensions';
import { FlowScripts } from '@onify/flow-extensions/FlowScripts';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:camunda="http://camunda.org/schema/1.0/bpmn">
  <process id="theProcess" isExecutable="true">
    <serviceTask id="task1" camunda:expression="\${environment.services.serviceFn}" camunda:resultVariable="result" />
    <sequenceFlow id="to-task2" sourceRef="task1" targetRef="task2" />
    <scriptTask id="task2" camunda:resultVariable="out" scriptFormat="js">
      <script>
        next(null, myContextFn());
      </script>
    </scriptTask>
  </process>
</definitions>`;

const name = 'onify flow';
const engine = new Engine({
  name,
  source,
  moddleOptions: {
    camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
  },
  services: {
    serviceFn(scope, callback) {
      callback(null, { data: 1 });
    },
  },
  extensions: {
    onify: extensions,
  },
  scripts: new FlowScripts(name, './script-resources', {
    myContextFn() {
      return 2;
    },
  }),
});

engine.execute((err, instance) => {
  if (err) throw err;
  console.log(instance.name, instance.environment.output);
});

Extract scripts with extend function

import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import BpmnModdle from 'bpmn-moddle';
import * as Elements from 'bpmn-elements';
import { Serializer, TypeResolver } from 'moddle-context-serializer';
import { extendFn } from '@onify/flow-extensions';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `
<definitions id="Def_0" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  targetNamespace="http://bpmn.io/schema/bpmn">
  <process id="process-1" name="Onify Flow" isExecutable="true">
    <serviceTask id="task1">
      <extensionElements>
        <camunda:connector>
          <camunda:connectorId>onifyApiRequest</camunda:connectorId>
          <camunda:inputOutput>
            <camunda:inputParameter name="method">GET</camunda:inputParameter>
            <camunda:inputParameter name="url">/my/items/workspace-1</camunda:inputParameter>
            <camunda:outputParameter name="result">
              <camunda:script scriptFormat="js">
                next(null, {
                  id: content.id,
                  statuscode,
                });
              </camunda:script>
            </camunda:outputParameter>
          </camunda:inputOutput>
        </camunda:connector>
        <camunda:inputOutput>
          <camunda:outputParameter name="result">\${content.output.result.statuscode}</camunda:outputParameter>
        </camunda:inputOutput>
      </extensionElements>
    </serviceTask>
    <sequenceFlow id="to-task2" sourceRef="task1" targetRef="task2" />
    <scriptTask id="task2" camunda:resultVariable="out" scriptFormat="js">
      <script>
        next(null, 2);
      </script>
    </scriptTask>
  </process>
</definitions>`;

getScripts(source).then(console.log).catch(console.error);

async function getScripts(bpmnSource) {
  const moddle = await getModdleContext(bpmnSource, {
    camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
  });

  const serialized = Serializer(moddle, TypeResolver(Elements), extendFn);
  return serialized.elements.scripts;
}

function getModdleContext(source, options) {
  const bpmnModdle = new BpmnModdle(options);
  return bpmnModdle.fromXML(Buffer.isBuffer(source) ? source.toString() : source.trim());
}

Extract timers

import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import BpmnModdle from 'bpmn-moddle';
import * as Elements from 'bpmn-elements';
import { Serializer, TypeResolver } from 'moddle-context-serializer';
import { extendFn, OnifyTimerEventDefinition } from '@onify/flow-extensions';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `
<definitions id="Def_0" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  targetNamespace="http://bpmn.io/schema/bpmn">
  <process id="cycle-1" name="Onify start at time cycle" isExecutable="true" camunda:historyTimeToLive="PT180M">
    <startEvent id="start">
      <timerEventDefinition>
        <timeCycle xsi:type="tFormalExpression">0 1 * * *</timeCycle>
      </timerEventDefinition>
    </startEvent>
    <sequenceFlow id="to-task" sourceRef="start" targetRef="task" />
    <userTask id="task" />
    <boundaryEvent id="bound-timer" cancelActivity="false" attachedToRef="task">
      <timerEventDefinition>
        <timeDuration xsi:type="tFormalExpression">R3/PT1M</timeDuration>
      </timerEventDefinition>
    </boundaryEvent>
    <sequenceFlow id="to-wait" sourceRef="task" targetRef="wait" />
    <intermediateThrowEvent id="timer">
      <timerEventDefinition>
        <timeCycle xsi:type="tFormalExpression">\${environment.settings.postpone}</timeCycle>
      </timerEventDefinition>
    </intermediateThrowEvent>
    <sequenceFlow id="to-end" sourceRef="wait" targetRef="end" />
    <endEvent id="end" />
  </process>
</definitions>`;

getTimers(source).then(console.log).catch(console.error);

const dummyEventActivity = { broker: {}, environment: { Logger() {} } };

async function getTimers(bpmnSource) {
  const moddle = await getModdleContext(bpmnSource, {
    camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
  });

  const serialized = Serializer(moddle, TypeResolver(Elements), extendFn);

  for (const t of serialized.elements.timers) {
    const ed = new OnifyTimerEventDefinition(dummyEventActivity, t.timer);

    try {
      t.parsed = ed.parse(t.timer.timerType, t.timer.value);
    } catch {}
  }

  return serialized.elements.timers;
}

function getModdleContext(source, options) {
  const bpmnModdle = new BpmnModdle(options);
  return bpmnModdle.fromXML(Buffer.isBuffer(source) ? source.toString() : source.trim());
}

Extend sequence flow with properties and take listeners

import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { Engine } from 'bpmn-engine';
import * as Elements from 'bpmn-elements';
import { OnifySequenceFlow, extensions } from '@onify/flow-extensions';
import { FlowScripts } from '@onify/flow-extensions/FlowScripts';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `
<?xml version="1.0" encoding="UTF-8"?>
<definitions
  id="Def_0"
  xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
  targetNamespace="http://bpmn.io/schema/bpmn">
  <process id="Process_1kk79yr" isExecutable="true">
    <startEvent id="start" />
    <sequenceFlow id="to-script" sourceRef="start" targetRef="script">
      <extensionElements>
        <camunda:properties>
          <camunda:property name="source" value="\${content.id}" />
        </camunda:properties>
        <camunda:executionListener event="take">
          <camunda:script scriptFormat="js">environment.output.fields = listener.fields; next();</camunda:script>
          <camunda:field name="taken">
            <camunda:expression>\${true}</camunda:expression>
          </camunda:field>
          <camunda:field name="bar">
            <camunda:string>baz</camunda:string>
          </camunda:field>
        </camunda:executionListener>
      </extensionElements>
    </sequenceFlow>
    <sequenceFlow id="to-end" sourceRef="script" targetRef="end">
      <extensionElements>
        <camunda:properties>
          <camunda:property name="foo" value="bar" />
        </camunda:properties>
      </extensionElements>
    </sequenceFlow>
    <scriptTask id="script" name="script" scriptFormat="js">
      <script>next(null, { foo: environment.variables.required.input });</script>
    </scriptTask>
    <boundaryEvent id="catch-err" attachedToRef="script">
      <errorEventDefinition />
    </boundaryEvent>
    <endEvent id="end-err">
      <extensionElements>
        <camunda:executionListener event="start">
          <camunda:script scriptFormat="js">
            environment.output.failedBy = content.inbound[0].properties.error;
            if (next) next();
          </camunda:script>
        </camunda:executionListener>
      </extensionElements>
    </endEvent>
    <sequenceFlow id="to-end-err" sourceRef="catch-err" targetRef="end-err">
      <extensionElements>
        <camunda:properties>
          <camunda:property name="error" value="\${content.output}" />
        </camunda:properties>
      </extensionElements>
    </sequenceFlow>
    <endEvent id="end" />
  </process>
</definitions>`;

const engine = new Engine({
  name: 'sequence flow extension',
  source,
  moddleOptions: {
    camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
  },
  extensions: {
    onify: extensions,
  },
  scripts: new FlowScripts('sequence flow extension'),
  elements: {
    ...Elements,
    SequenceFlow: OnifySequenceFlow,
  },
  variables: {
    required: {
      input: true,
    },
  },
});

engine.execute((err, instance) => {
  if (err) throw err;
  console.log(instance.name, instance.environment.output);
});