UNPKG

@jplorg/jpl

Version:
101 lines (93 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyProgramDefaults = applyProgramDefaults; exports.default = void 0; var _applyDefaults = _interopRequireDefault(require("../applyDefaults")); var _library = require("../library"); var _runtime = _interopRequireWildcard(require("../runtime")); var _ops = _interopRequireDefault(require("./ops")); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const defaultOptions = {}; function applyProgramDefaults(options = {}, defaults = {}) { return (0, _applyDefaults.default)(options, defaults); } function validateDefinition(programDefinition) { const version = programDefinition?.version; const [, major, minor] = typeof version === 'string' ? /^(\d+)\.(\d+)?$/g.exec(version) ?? [] : []; if (!major || !minor || !Array.isArray(programDefinition.instructions)) { throw new _library.JPLFatalError('invalid program definition'); } if (+major !== _library.DEFINITION_VERSION_MAJOR || +minor > _library.DEFINITION_VERSION_MINOR) { throw new _library.JPLFatalError(`unsupported program definition v${major}.${minor} - this version of JPL only supports v${_library.DEFINITION_VERSION_MAJOR} (up to v${_library.DEFINITION_VERSION})`); } } /** JPL program */ class JPLProgram { constructor(programDefinition, options) { validateDefinition(programDefinition); this._options = applyProgramDefaults(options?.program, defaultOptions); this._runtimeOptions = options?.runtime; this._definition = programDefinition; this._ops = _ops.default; } /** Return the program's options */ get options() { return this._options; } /** * Return the program's definition. * The definition can be serialized as JSON to be reused in other JPL implementations. */ get definition() { return this._definition; } /** Return the program's OPs */ get ops() { return this._ops; } /** * Run the program with the provided inputs and runtime options. * The program throws a JPLExecutionError (see `JPLExecutionError.is`) for runtime failures. * Other errors may be thrown when execution fails. */ run = async (inputs, options) => { const runtime = new _runtime.default(this, { runtime: (0, _runtime.applyRuntimeDefaults)(options?.runtime, this._runtimeOptions) }); const normalizedInputs = runtime.normalizeValues(inputs, 'program inputs'); const outputs = await runtime.execute(normalizedInputs); return runtime.stripJSON(outputs); }; /** * Return a new program with the provided definition's instructions prepended to the program. */ prepend = programDefinition => { validateDefinition(programDefinition); const mergedDefinition = { version: _library.DEFINITION_VERSION, instructions: [...programDefinition.instructions, ...this.definition.instructions] }; return new JPLProgram(mergedDefinition, { program: this._options, runtime: this._runtimeOptions }); }; /** * Return a new program with the provided definition's instructions appended to the program. */ append = programDefinition => { validateDefinition(programDefinition); const mergedDefinition = { version: _library.DEFINITION_VERSION, instructions: [...this.definition.instructions, ...programDefinition.instructions] }; return new JPLProgram(mergedDefinition, { program: this._options, runtime: this._runtimeOptions }); }; } var _default = exports.default = JPLProgram;