Skip to content
Reference

Shared libraries

Versioned JavaScript that every workflow in a workspace can call as libs.NAME, pinned per step so an edit never changes a deployed workflow.

On this page

A shared library is JavaScript that the JavaScript steps of a workspace can call: a phone number formatter, a date helper, a payer code check. You write it once in Settings → Libraries, and a step calls it as libs.NAME.fn(). It is the MedHook equivalent of a Mirth code template library.

Writing a library

A library assigns what it shares to exports. Each export is a function or a constant that is plain JSON.

javascript
exports.normalizePhone = function (p) {
  var d = String(p || "").replace(/\D/g, "");
  return d.length === 10 ? "+1" + d : d;
};
exports.FACILITY_CODES = { N: "North", S: "South" };

A step that pins the library calls it:

javascript
return { phone: libs.Phone.normalizePhone(input.patient.phone) };

Saving checks the library before it stores anything. The code must compile on its own, finish within the sandbox's 5 second limit, assign at least one export, and export only functions and JSON constants. A library that fails the check is not saved, and the error says why.

Versions and pins

Every save of a library is a new version: v1, v2, and so on. A version never changes and is never deleted while the library exists.

A step pins the libraries it uses in its Libraries field, as name@version (Phone@2). To find it, open the workflow's Build tab, click the JavaScript step, and stay on Configure: Libraries is in the Script section, to the right of Transform and Code tables, above the code. Type the name and press Tab, then choose Apply and Save. Type the name alone to pin the newest version.

A JavaScript step's Configure tab: the Script section with the Transform, Code tables and Libraries fields above the code, and DemoLibrary@1 typed in Libraries

A step that calls a library it doesn't pin fails with "Library … is not pinned on this step", which says where to pin it. If the Libraries field says a library is listed on this step but not pinned, type that name in the field: only the field's pins are read when the step runs. The pin is part of the step, so it is in every saved version of the workflow: editing a library never changes what a deployed workflow runs. To use a new version, update the pin, test the workflow, and deploy it.

The deploy checklist reports pins:

CodeSeverityMeaning
LIBRARY_OUTDATEDWarning"Library update available (Phone v1 → v2)". The step keeps running v1; deploy is not blocked.
LIBRARY_MISSINGErrorThe pinned library or version does not exist. Deploy is blocked.

A step whose pinned version does not exist fails without a retry. A step can pin up to 10 libraries.

What a library can reach

A library runs inside the step's sandbox, after the step's helpers and before its code.

A library can useA library cannot use
hl7, date, lookup, console, env (read-only)require, fetch, XMLHttpRequest, process, files, timers
Its own functions and constantsThe step's input, vars, globals or responses
Another library's exports

Exports are frozen: neither the step nor another library can change them. A library that loops forever stops the step at the 5 second limit, like any other code in the step.

Deleting a library

Delete a library from Settings → Libraries. The engine refuses while any workflow pins it, in its current draft or in a saved version, and lists the workflows. Deleting removes every version. Each change writes an audit event: shared_library.created, shared_library.versioned or shared_library.deleted.

Imported from Mirth

The Mirth importer turns each code template library into a shared library at v1. The templates' code is kept, and each top-level function is exported. The JavaScript steps of the channels the library is enabled for pin v1 and get one line per function (var fmt = libs.Lib.fmt;), so the Mirth code that calls fmt() keeps working. The Mirth export removes those lines again.

A library that uses literal E4X syntax (<PID/>, m..OBX, .@id), or reads msg or tmp as globals, is not pinned: its finding says to rewrite it with AI, then save the result as a shared library. A function that reads the Mirth message tree a step passes it (m['PID']['PID.3']) is pinned and runs. Library code that the sandbox refuses is not created, and the import's warnings say which.

Imported from Rhapsody

The Rhapsody importer turns each shared JavaScript library that a route's JavaScript filter calls into a shared library at v1, once, however many routes call it. The library's functions are kept, and each top-level function is exported. A JavaScript filter that calls one of its functions becomes a JavaScript step that pins the library, gets one line per function (var hl7Date = libs.DemoHealthDates.hl7Date;), and runs the Rhapsody script as written:

  • input[0] is the step's message, with getText/setText (or text), getProperty/setProperty, and getField/setField on HL7 v2 paths such as PID-5.1.
  • output.append(message) passes the message on. Message properties are kept in the channel map for the later steps.
  • log.info, log.warn and log.error write to the step's log.

Each such step is marked for review: run it on a sample message before you deploy. A library the export names without its code, one with no top-level function, or one no route calls is not created, and the import's findings name it. Library code that the sandbox refuses is not created, and the import's warnings say which.

Reference