@5minds/processcube_app_sdk 5.1.0-feature-cd163e-looetttn → 5.1.0-feature-22cf74-loogdr08

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@5minds/processcube_app_sdk",
3
- "version": "5.1.0-feature-cd163e-looetttn",
3
+ "version": "5.1.0-feature-22cf74-loogdr08",
4
4
  "description": "The SDK for ProcessCube Apps",
5
5
  "type": "module",
6
6
  "main": "common/index.cjs",
package/server/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var Y=Object.create;var T=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var Q=(e,t)=>{for(var s in t)T(e,s,{get:t[s],enumerable:!0})},N=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of V(t))!Z.call(e,r)&&r!==s&&T(e,r,{get:()=>t[r],enumerable:!(n=j(t,r))||n.enumerable});return e};var z=(e,t,s)=>(s=e!=null?Y(J(e)):{},N(t||!e||!e.__esModule?T(s,"default",{value:e,enumerable:!0}):s,e)),ee=e=>N(T({},"__esModule",{value:!0}),e);var Ne={};Q(Ne,{finishUserTask:()=>Ce,finishUserTaskAndGetNext:()=>fe,getActiveProcessInstances:()=>ue,getAssignedUserTasksByIdentity:()=>ge,getEngineClient:()=>ce,getIdentity:()=>de,getReservedUserTasksByIdentity:()=>we,getUserTasks:()=>v,getWaitingUserTaskByFlowNodeInstanceId:()=>B,getWaitingUserTasks:()=>ke,getWaitingUserTasksByCorrelationId:()=>Te,getWaitingUserTasksByFlowNodeId:()=>Ie,getWaitingUserTasksByProcessInstanceId:()=>me,navigateToUrl:()=>Ee,startProcess:()=>xe,waitForUserTask:()=>pe,withApplicationSdk:()=>ye});module.exports=ee(Ne);var at=require("only-server");var _=require("@5minds/processcube_engine_sdk"),P=require("@5minds/processcube_engine_client"),f=require("node:path"),U=require("esbuild"),m=require("node:fs"),R=require("openid-client"),F=z(require("jwt-decode"),1),A=require("chokidar");var S=require("@5minds/processcube_engine_client"),y=process.env.PROCESSCUBE_ENGINE_URL??"http://localhost:10560",o=new S.EngineClient(y);var te={token:"ZHVtbXlfdG9rZW4=",userId:"dummy_token"},se=.85,re="external_task.ts",k=new _.Logger("processcube_app_sdk:external_task_adapter"),w=process.env.PROCESSCUBE_AUTHORITY_URL!==void 0;async function D(e){let t,s=[e,(0,f.join)(process.cwd(),"app"),(0,f.join)(process.cwd(),"src","app")];for(let r of s)if(r&&(0,m.existsSync)(r)){t=r;break}if(!t)throw new Error("Could not find external tasks directory");let n=await M(t);for(let r of n){let a=await ne(r);if(!a)continue;let l=(0,f.join)(r,a),d=(0,f.relative)(t,r).replace(/^\.\/+|\([^)]+\)|^\/*|\/*$/g,"").replace(/[\/]{2,}/g,"/"),i=await h(l,d);(0,A.watch)(l).on("all",async(c,I)=>{c==="change"&&(i.dispose(),i.stop(),k.info(`Restarting external task worker ${i.workerId} for topic ${d}`,{reason:"code changes detected",workerId:i.workerId,topic:d}),i=await h(l,d,i.workerId))})}}async function h(e,t,s){let n=await oe(e),r=w?await b():null,a={identity:await L(r),...n?.config};s&&(a={...a,workerId:s});let l=n.default,d=new P.ExternalTaskWorker(y,t,l,a);return await E(r,d),d.onWorkerError((i,c,I)=>{k.error(`Intercepted "${i}"-type error: ${c.message}`,{err:c,type:i,externalTask:I})}),d.start(),k.info(`Started external task worker ${d.workerId} for topic ${t}`),d}async function ne(e){let s=(await m.promises.readdir(e)).filter(n=>n===re);if(s.length===0)return null;if(s.length>1)throw new Error(`Found more than one external task file in directory '${e}'`);return s[0]}async function b(){if(!w)throw new Error("No authority is configured");let e=await R.Issuer.discover(process.env.PROCESSCUBE_AUTHORITY_URL);return await new e.Client({client_id:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_ID,client_secret:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_SECRET}).grant({grant_type:"client_credentials",scope:"engine_etw"})}async function L(e){if(!w||e===null)return te;let t=e.access_token,s=(0,F.default)(t);return{token:e.access_token,userId:s.sub}}async function E(e,t,s=5){try{if(!w||e===null)return;let r=await ae(e)*se*1e3;setTimeout(async()=>{let a=await b(),l=await L(a);t.identity=l,await E(a,t)},r)}catch(n){if(s===0)throw n;k.error(`Could not refresh identity for external task worker ${t.workerId}`,{err:n,workerId:t.workerId,retryCount:s});let r=2*1e3;setTimeout(async()=>await E(e,t,s-1),r)}}async function oe(e){let t=await(0,U.build)({entryPoints:[e],write:!1,bundle:!0,platform:"node",target:"node18",format:"cjs"}),s=t.outputFiles[0].text,n=ie(s,e);if(t.errors.length>0)throw k.error(`Could not transpile file at '${e}'`,{errors:t.errors}),new Error(`Could not transpile file at '${e}'`);return t.warnings.length>0&&k.warn(`Transpiled file at '${e}' with warnings`,{warnings:t.warnings}),n}async function M(e){let t=await m.promises.readdir(e,{withFileTypes:!0}),s=await Promise.all(t.map(async n=>{let r=(0,f.join)(e.toString(),n.name);return n.isDirectory()?[r,...await M(r)]:[]}));return Array.prototype.concat(...s)}async function ae(e){let t=e.expires_in;if(!t&&e.expires_at&&(t=Math.floor(e.expires_at-Date.now()/1e3)),t===void 0)throw new Error("Could not determine the time until the access token for external task workers expires");return t}function ie(e,t){try{var s=module.constructor,n=new s;return n._compile(e,t),n.exports}catch(r){throw k.error("Could not require module from string",{err:r}),r}}function ce(){return o}var p=require("next/headers"),W=require("next/dist/compiled/@edge-runtime/cookies"),x=require("next-auth/jwt"),le=process.env.NEXTAUTH_URL?.startsWith("https://"),O=`${le?"__Secure-":""}next-auth.session-token`;async function de(){let e=await(0,x.getToken)({req:{cookies:(0,p.cookies)(),headers:(0,p.headers)()}});if(e?.expiresAt&&Date.now()>=e.expiresAt*1e3){let s=await fetch(`${process.env.NEXTAUTH_URL}/api/auth/session`,{method:"GET",headers:{"Content-Type":"application/json",cookie:(0,p.cookies)().toString()}}),n=new W.ResponseCookies(s.headers),r=n.get(O);if(r){let a=!1;try{(0,p.cookies)().set(O,r.value,{...r})}catch{a=!0}if(e=await(0,x.getToken)({req:{cookies:a?n:(0,p.cookies)(),headers:(0,p.headers)()}}),e?.error)throw e.error}}if(!e?.accessToken||!e?.sub)throw new Error("AccessToken or Sub could not be determined!");return{token:e?.accessToken,userId:e?.sub}}var q=require("@5minds/processcube_engine_client");async function ue(e){return await o.processInstances.query({...e?.query,state:q.DataModels.ProcessInstances.ProcessInstanceState.running},e?.options)}var u=require("@5minds/processcube_engine_client");async function pe(e={},t){let{processInstanceId:s,flowNodeId:n}=e;return new Promise(async(r,a)=>{let l=await o.userTasks.onUserTaskWaiting(async c=>{let I=c.flowNodeInstanceId===void 0,G=s!==void 0&&c.processInstanceId!==s,H=n!==void 0&&c.flowNodeId!==n,X=s!==void 0&&n!==void 0&&c.processInstanceId!==s&&c.flowNodeId!==n;if(I||G||H||X)return;let C=await B(c.flowNodeInstanceId,{identity:t});return o.notification.removeSubscription(l,t),C===null?a(new Error(`UserTask with instance ID "${c.flowNodeInstanceId}" does not exist.`)):r(C)},{identity:t}),i=(await v({processInstanceId:s,flowNodeId:n,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:t})).userTasks[0];i&&(o.notification.removeSubscription(l,t),r(i))})}async function fe(e,t,s,n){await o.userTasks.finishUserTask(e,t,n);let r=await o.userTasks.query({flowNodeId:s,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:n});return r.userTasks.length===0?null:r.userTasks[0]}async function v(...e){return await o.userTasks.query(...e)}async function ke(e){return await o.userTasks.query({state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},e)}async function me(e,t){return await o.userTasks.query({processInstanceId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function Ie(e,t){return await o.userTasks.query({flowNodeId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function B(e,t){let s=await o.userTasks.query({flowNodeInstanceId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{...t,limit:1});return s.userTasks.length===0?null:s.userTasks[0]}async function Te(e,t){return await o.userTasks.query({correlationId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function we(e,t){let s=await o.userTasks.query({state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),n=s.userTasks.filter(r=>r.actualOwnerId===e.userId);return s.userTasks=n,s}async function ge(e,t){let s=await o.userTasks.query({state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),n=s.userTasks.filter(r=>r.assignedUserIds?.includes(e.userId));return s.userTasks=n,s}var g=require("next/dist/shared/lib/constants");function ye(e={}){let{applicationSdk:t,...s}=e;return async(n,r)=>{let a=n===g.PHASE_DEVELOPMENT_SERVER||n===g.PHASE_PRODUCTION_SERVER;return t?.useExternalTasks&&a&&await D(t?.customExternalTasksDirPath),s}}var $=require("next/cache"),K=require("next/navigation");function Ee(e){(0,$.revalidatePath)(e),(0,K.redirect)(e)}async function xe(...e){return await o.processModels.startProcessInstance(...e)}async function Ce(...e){await o.userTasks.finishUserTask(...e)}0&&(module.exports={finishUserTask,finishUserTaskAndGetNext,getActiveProcessInstances,getAssignedUserTasksByIdentity,getEngineClient,getIdentity,getReservedUserTasksByIdentity,getUserTasks,getWaitingUserTaskByFlowNodeInstanceId,getWaitingUserTasks,getWaitingUserTasksByCorrelationId,getWaitingUserTasksByFlowNodeId,getWaitingUserTasksByProcessInstanceId,navigateToUrl,startProcess,waitForUserTask,withApplicationSdk});
1
+ "use strict";var Y=Object.create;var I=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var Q=(e,t)=>{for(var s in t)I(e,s,{get:t[s],enumerable:!0})},N=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of V(t))!Z.call(e,r)&&r!==s&&I(e,r,{get:()=>t[r],enumerable:!(n=j(t,r))||n.enumerable});return e};var z=(e,t,s)=>(s=e!=null?Y(J(e)):{},N(t||!e||!e.__esModule?I(s,"default",{value:e,enumerable:!0}):s,e)),ee=e=>N(I({},"__esModule",{value:!0}),e);var Ne={};Q(Ne,{finishUserTask:()=>Ce,finishUserTaskAndGetNext:()=>fe,getActiveProcessInstances:()=>ue,getAssignedUserTasksByIdentity:()=>ge,getEngineClient:()=>ce,getIdentity:()=>le,getReservedUserTasksByIdentity:()=>Te,getUserTasks:()=>v,getWaitingUserTaskByFlowNodeInstanceId:()=>B,getWaitingUserTasks:()=>ke,getWaitingUserTasksByCorrelationId:()=>we,getWaitingUserTasksByFlowNodeId:()=>Ie,getWaitingUserTasksByProcessInstanceId:()=>me,navigateToUrl:()=>Ee,startProcess:()=>xe,waitForUserTask:()=>pe,withApplicationSdk:()=>ye});module.exports=ee(Ne);var at=require("only-server");var _=require("@5minds/processcube_engine_sdk"),P=require("@5minds/processcube_engine_client"),k=require("node:path"),U=require("esbuild"),m=require("node:fs"),R=require("openid-client"),F=z(require("jwt-decode"),1),A=require("chokidar");var S=require("@5minds/processcube_engine_client"),y=process.env.PROCESSCUBE_ENGINE_URL??"http://localhost:10560",o=new S.EngineClient(y);var te={token:"ZHVtbXlfdG9rZW4=",userId:"dummy_token"},se=.85,re="external_task.ts",p=new _.Logger("processcube_app_sdk:external_task_adapter"),w=process.env.PROCESSCUBE_AUTHORITY_URL!==void 0;async function D(e){let t,s=[e,(0,k.join)(process.cwd(),"app"),(0,k.join)(process.cwd(),"src","app")];for(let r of s)if(r&&(0,m.existsSync)(r)){t=r;break}if(!t)throw new Error("Could not find external tasks directory");let n=await M(t);for(let r of n){let a=await ne(r);if(!a)continue;let l=(0,k.join)(r,a),d=(0,k.relative)(t,r).replace(/^\.\/+|\([^)]+\)|^\/*|\/*$/g,"").replace(/[\/]{2,}/g,"/"),i=await h(l,d);(0,A.watch)(l).on("change",async c=>{i.dispose(),i.stop(),p.info(`Restarting external task worker ${i.workerId} for topic ${d}`,{reason:"code changes detected",workerId:i.workerId,topic:d}),i=await h(l,d,i.workerId)}).on("unlink",async c=>{i.dispose(),i.stop(),p.info(`Stopping external task worker ${i.workerId} for topic ${d}`,{reason:`ETW ${c} was removed`,workerId:i.workerId,topic:d})}).on("error",c=>p.info(`Watcher error: ${c}`))}}async function h(e,t,s){let n=await oe(e),r=w?await b():null,a={identity:await L(r),...n?.config};s&&(a={...a,workerId:s});let l=n.default,d=new P.ExternalTaskWorker(y,t,l,a);return await E(r,d),d.onWorkerError((i,c,g)=>{p.error(`Intercepted "${i}"-type error: ${c.message}`,{err:c,type:i,externalTask:g})}),d.start(),p.info(`Started external task worker ${d.workerId} for topic ${t}`),d}async function ne(e){let s=(await m.promises.readdir(e)).filter(n=>n===re);if(s.length===0)return null;if(s.length>1)throw new Error(`Found more than one external task file in directory '${e}'`);return s[0]}async function b(){if(!w)throw new Error("No authority is configured");let e=await R.Issuer.discover(process.env.PROCESSCUBE_AUTHORITY_URL);return await new e.Client({client_id:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_ID,client_secret:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_SECRET}).grant({grant_type:"client_credentials",scope:"engine_etw"})}async function L(e){if(!w||e===null)return te;let t=e.access_token,s=(0,F.default)(t);return{token:e.access_token,userId:s.sub}}async function E(e,t,s=5){try{if(!w||e===null)return;let r=await ae(e)*se*1e3;setTimeout(async()=>{let a=await b(),l=await L(a);t.identity=l,await E(a,t)},r)}catch(n){if(s===0)throw n;p.error(`Could not refresh identity for external task worker ${t.workerId}`,{err:n,workerId:t.workerId,retryCount:s});let r=2*1e3;setTimeout(async()=>await E(e,t,s-1),r)}}async function oe(e){let t=await(0,U.build)({entryPoints:[e],write:!1,bundle:!0,platform:"node",target:"node18",format:"cjs"}),s=t.outputFiles[0].text,n=ie(s,e);if(t.errors.length>0)throw p.error(`Could not transpile file at '${e}'`,{errors:t.errors}),new Error(`Could not transpile file at '${e}'`);return t.warnings.length>0&&p.warn(`Transpiled file at '${e}' with warnings`,{warnings:t.warnings}),n}async function M(e){let t=await m.promises.readdir(e,{withFileTypes:!0}),s=await Promise.all(t.map(async n=>{let r=(0,k.join)(e.toString(),n.name);return n.isDirectory()?[r,...await M(r)]:[]}));return Array.prototype.concat(...s)}async function ae(e){let t=e.expires_in;if(!t&&e.expires_at&&(t=Math.floor(e.expires_at-Date.now()/1e3)),t===void 0)throw new Error("Could not determine the time until the access token for external task workers expires");return t}function ie(e,t){try{var s=module.constructor,n=new s;return n._compile(e,t),n.exports}catch(r){throw p.error("Could not require module from string",{err:r}),r}}function ce(){return o}var f=require("next/headers"),W=require("next/dist/compiled/@edge-runtime/cookies"),x=require("next-auth/jwt"),de=process.env.NEXTAUTH_URL?.startsWith("https://"),O=`${de?"__Secure-":""}next-auth.session-token`;async function le(){let e=await(0,x.getToken)({req:{cookies:(0,f.cookies)(),headers:(0,f.headers)()}});if(e?.expiresAt&&Date.now()>=e.expiresAt*1e3){let s=await fetch(`${process.env.NEXTAUTH_URL}/api/auth/session`,{method:"GET",headers:{"Content-Type":"application/json",cookie:(0,f.cookies)().toString()}}),n=new W.ResponseCookies(s.headers),r=n.get(O);if(r){let a=!1;try{(0,f.cookies)().set(O,r.value,{...r})}catch{a=!0}if(e=await(0,x.getToken)({req:{cookies:a?n:(0,f.cookies)(),headers:(0,f.headers)()}}),e?.error)throw e.error}}if(!e?.accessToken||!e?.sub)throw new Error("AccessToken or Sub could not be determined!");return{token:e?.accessToken,userId:e?.sub}}var q=require("@5minds/processcube_engine_client");async function ue(e){return await o.processInstances.query({...e?.query,state:q.DataModels.ProcessInstances.ProcessInstanceState.running},e?.options)}var u=require("@5minds/processcube_engine_client");async function pe(e={},t){let{processInstanceId:s,flowNodeId:n}=e;return new Promise(async(r,a)=>{let l=await o.userTasks.onUserTaskWaiting(async c=>{let g=c.flowNodeInstanceId===void 0,G=s!==void 0&&c.processInstanceId!==s,H=n!==void 0&&c.flowNodeId!==n,X=s!==void 0&&n!==void 0&&c.processInstanceId!==s&&c.flowNodeId!==n;if(g||G||H||X)return;let C=await B(c.flowNodeInstanceId,{identity:t});return o.notification.removeSubscription(l,t),C===null?a(new Error(`UserTask with instance ID "${c.flowNodeInstanceId}" does not exist.`)):r(C)},{identity:t}),i=(await v({processInstanceId:s,flowNodeId:n,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:t})).userTasks[0];i&&(o.notification.removeSubscription(l,t),r(i))})}async function fe(e,t,s,n){await o.userTasks.finishUserTask(e,t,n);let r=await o.userTasks.query({flowNodeId:s,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:n});return r.userTasks.length===0?null:r.userTasks[0]}async function v(...e){return await o.userTasks.query(...e)}async function ke(e){return await o.userTasks.query({state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},e)}async function me(e,t){return await o.userTasks.query({processInstanceId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function Ie(e,t){return await o.userTasks.query({flowNodeId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function B(e,t){let s=await o.userTasks.query({flowNodeInstanceId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{...t,limit:1});return s.userTasks.length===0?null:s.userTasks[0]}async function we(e,t){return await o.userTasks.query({correlationId:e,state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function Te(e,t){let s=await o.userTasks.query({state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),n=s.userTasks.filter(r=>r.actualOwnerId===e.userId);return s.userTasks=n,s}async function ge(e,t){let s=await o.userTasks.query({state:u.DataModels.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),n=s.userTasks.filter(r=>r.assignedUserIds?.includes(e.userId));return s.userTasks=n,s}var T=require("next/dist/shared/lib/constants");function ye(e={}){let{applicationSdk:t,...s}=e;return async(n,r)=>{let a=n===T.PHASE_DEVELOPMENT_SERVER||n===T.PHASE_PRODUCTION_SERVER;return t?.useExternalTasks&&a&&await D(t?.customExternalTasksDirPath),s}}var $=require("next/cache"),K=require("next/navigation");function Ee(e){(0,$.revalidatePath)(e),(0,K.redirect)(e)}async function xe(...e){return await o.processModels.startProcessInstance(...e)}async function Ce(...e){await o.userTasks.finishUserTask(...e)}0&&(module.exports={finishUserTask,finishUserTaskAndGetNext,getActiveProcessInstances,getAssignedUserTasksByIdentity,getEngineClient,getIdentity,getReservedUserTasksByIdentity,getUserTasks,getWaitingUserTaskByFlowNodeInstanceId,getWaitingUserTasks,getWaitingUserTasksByCorrelationId,getWaitingUserTasksByFlowNodeId,getWaitingUserTasksByProcessInstanceId,navigateToUrl,startProcess,waitForUserTask,withApplicationSdk});
package/server/index.mjs CHANGED
@@ -1 +1 @@
1
- import"only-server";import{Logger as D}from"@5minds/processcube_engine_sdk";import{ExternalTaskWorker as b}from"@5minds/processcube_engine_client";import{join as k,relative as L}from"node:path";import{build as M}from"esbuild";import{promises as E,existsSync as O}from"node:fs";import{Issuer as W}from"openid-client";import q from"jwt-decode";import{watch as v}from"chokidar";import{EngineClient as A}from"@5minds/processcube_engine_client";var T=process.env.PROCESSCUBE_ENGINE_URL??"http://localhost:10560",o=new A(T);var B={token:"ZHVtbXlfdG9rZW4=",userId:"dummy_token"},$=.85,K="external_task.ts",p=new D("processcube_app_sdk:external_task_adapter"),m=process.env.PROCESSCUBE_AUTHORITY_URL!==void 0;async function x(e){let t,s=[e,k(process.cwd(),"app"),k(process.cwd(),"src","app")];for(let n of s)if(n&&O(n)){t=n;break}if(!t)throw new Error("Could not find external tasks directory");let r=await S(t);for(let n of r){let a=await G(n);if(!a)continue;let l=k(n,a),d=L(t,n).replace(/^\.\/+|\([^)]+\)|^\/*|\/*$/g,"").replace(/[\/]{2,}/g,"/"),i=await y(l,d);v(l).on("all",async(c,f)=>{c==="change"&&(i.dispose(),i.stop(),p.info(`Restarting external task worker ${i.workerId} for topic ${d}`,{reason:"code changes detected",workerId:i.workerId,topic:d}),i=await y(l,d,i.workerId))})}}async function y(e,t,s){let r=await H(e),n=m?await C():null,a={identity:await N(n),...r?.config};s&&(a={...a,workerId:s});let l=r.default,d=new b(T,t,l,a);return await w(n,d),d.onWorkerError((i,c,f)=>{p.error(`Intercepted "${i}"-type error: ${c.message}`,{err:c,type:i,externalTask:f})}),d.start(),p.info(`Started external task worker ${d.workerId} for topic ${t}`),d}async function G(e){let s=(await E.readdir(e)).filter(r=>r===K);if(s.length===0)return null;if(s.length>1)throw new Error(`Found more than one external task file in directory '${e}'`);return s[0]}async function C(){if(!m)throw new Error("No authority is configured");let e=await W.discover(process.env.PROCESSCUBE_AUTHORITY_URL);return await new e.Client({client_id:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_ID,client_secret:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_SECRET}).grant({grant_type:"client_credentials",scope:"engine_etw"})}async function N(e){if(!m||e===null)return B;let t=e.access_token,s=q(t);return{token:e.access_token,userId:s.sub}}async function w(e,t,s=5){try{if(!m||e===null)return;let n=await X(e)*$*1e3;setTimeout(async()=>{let a=await C(),l=await N(a);t.identity=l,await w(a,t)},n)}catch(r){if(s===0)throw r;p.error(`Could not refresh identity for external task worker ${t.workerId}`,{err:r,workerId:t.workerId,retryCount:s});let n=2*1e3;setTimeout(async()=>await w(e,t,s-1),n)}}async function H(e){let t=await M({entryPoints:[e],write:!1,bundle:!0,platform:"node",target:"node18",format:"cjs"}),s=t.outputFiles[0].text,r=Y(s,e);if(t.errors.length>0)throw p.error(`Could not transpile file at '${e}'`,{errors:t.errors}),new Error(`Could not transpile file at '${e}'`);return t.warnings.length>0&&p.warn(`Transpiled file at '${e}' with warnings`,{warnings:t.warnings}),r}async function S(e){let t=await E.readdir(e,{withFileTypes:!0}),s=await Promise.all(t.map(async r=>{let n=k(e.toString(),r.name);return r.isDirectory()?[n,...await S(n)]:[]}));return Array.prototype.concat(...s)}async function X(e){let t=e.expires_in;if(!t&&e.expires_at&&(t=Math.floor(e.expires_at-Date.now()/1e3)),t===void 0)throw new Error("Could not determine the time until the access token for external task workers expires");return t}function Y(e,t){try{var s=module.constructor,r=new s;return r._compile(e,t),r.exports}catch(n){throw p.error("Could not require module from string",{err:n}),n}}function ye(){return o}import{cookies as I,headers as h}from"next/headers";import{ResponseCookies as j}from"next/dist/compiled/@edge-runtime/cookies";import{getToken as _}from"next-auth/jwt";var V=process.env.NEXTAUTH_URL?.startsWith("https://"),P=`${V?"__Secure-":""}next-auth.session-token`;async function Se(){let e=await _({req:{cookies:I(),headers:h()}});if(e?.expiresAt&&Date.now()>=e.expiresAt*1e3){let s=await fetch(`${process.env.NEXTAUTH_URL}/api/auth/session`,{method:"GET",headers:{"Content-Type":"application/json",cookie:I().toString()}}),r=new j(s.headers),n=r.get(P);if(n){let a=!1;try{I().set(P,n.value,{...n})}catch{a=!0}if(e=await _({req:{cookies:a?r:I(),headers:h()}}),e?.error)throw e.error}}if(!e?.accessToken||!e?.sub)throw new Error("AccessToken or Sub could not be determined!");return{token:e?.accessToken,userId:e?.sub}}import{DataModels as J}from"@5minds/processcube_engine_client";async function Ue(e){return await o.processInstances.query({...e?.query,state:J.ProcessInstances.ProcessInstanceState.running},e?.options)}import{DataModels as u}from"@5minds/processcube_engine_client";async function De(e={},t){let{processInstanceId:s,flowNodeId:r}=e;return new Promise(async(n,a)=>{let l=await o.userTasks.onUserTaskWaiting(async c=>{let f=c.flowNodeInstanceId===void 0,U=s!==void 0&&c.processInstanceId!==s,R=r!==void 0&&c.flowNodeId!==r,F=s!==void 0&&r!==void 0&&c.processInstanceId!==s&&c.flowNodeId!==r;if(f||U||R||F)return;let g=await Q(c.flowNodeInstanceId,{identity:t});return o.notification.removeSubscription(l,t),g===null?a(new Error(`UserTask with instance ID "${c.flowNodeInstanceId}" does not exist.`)):n(g)},{identity:t}),i=(await Z({processInstanceId:s,flowNodeId:r,state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:t})).userTasks[0];i&&(o.notification.removeSubscription(l,t),n(i))})}async function be(e,t,s,r){await o.userTasks.finishUserTask(e,t,r);let n=await o.userTasks.query({flowNodeId:s,state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:r});return n.userTasks.length===0?null:n.userTasks[0]}async function Z(...e){return await o.userTasks.query(...e)}async function Le(e){return await o.userTasks.query({state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},e)}async function Me(e,t){return await o.userTasks.query({processInstanceId:e,state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function Oe(e,t){return await o.userTasks.query({flowNodeId:e,state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function Q(e,t){let s=await o.userTasks.query({flowNodeInstanceId:e,state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},{...t,limit:1});return s.userTasks.length===0?null:s.userTasks[0]}async function We(e,t){return await o.userTasks.query({correlationId:e,state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function qe(e,t){let s=await o.userTasks.query({state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),r=s.userTasks.filter(n=>n.actualOwnerId===e.userId);return s.userTasks=r,s}async function ve(e,t){let s=await o.userTasks.query({state:u.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),r=s.userTasks.filter(n=>n.assignedUserIds?.includes(e.userId));return s.userTasks=r,s}import{PHASE_DEVELOPMENT_SERVER as z,PHASE_PRODUCTION_SERVER as ee}from"next/dist/shared/lib/constants";function Ze(e={}){let{applicationSdk:t,...s}=e;return async(r,n)=>{let a=r===z||r===ee;return t?.useExternalTasks&&a&&await x(t?.customExternalTasksDirPath),s}}import{revalidatePath as te}from"next/cache";import{redirect as se}from"next/navigation";function rt(e){te(e),se(e)}async function at(...e){return await o.processModels.startProcessInstance(...e)}async function lt(...e){await o.userTasks.finishUserTask(...e)}export{lt as finishUserTask,be as finishUserTaskAndGetNext,Ue as getActiveProcessInstances,ve as getAssignedUserTasksByIdentity,ye as getEngineClient,Se as getIdentity,qe as getReservedUserTasksByIdentity,Z as getUserTasks,Q as getWaitingUserTaskByFlowNodeInstanceId,Le as getWaitingUserTasks,We as getWaitingUserTasksByCorrelationId,Oe as getWaitingUserTasksByFlowNodeId,Me as getWaitingUserTasksByProcessInstanceId,rt as navigateToUrl,at as startProcess,De as waitForUserTask,Ze as withApplicationSdk};
1
+ import"only-server";import{Logger as D}from"@5minds/processcube_engine_sdk";import{ExternalTaskWorker as b}from"@5minds/processcube_engine_client";import{join as f,relative as L}from"node:path";import{build as M}from"esbuild";import{promises as E,existsSync as O}from"node:fs";import{Issuer as W}from"openid-client";import q from"jwt-decode";import{watch as v}from"chokidar";import{EngineClient as A}from"@5minds/processcube_engine_client";var w=process.env.PROCESSCUBE_ENGINE_URL??"http://localhost:10560",o=new A(w);var B={token:"ZHVtbXlfdG9rZW4=",userId:"dummy_token"},$=.85,K="external_task.ts",u=new D("processcube_app_sdk:external_task_adapter"),k=process.env.PROCESSCUBE_AUTHORITY_URL!==void 0;async function x(e){let t,s=[e,f(process.cwd(),"app"),f(process.cwd(),"src","app")];for(let n of s)if(n&&O(n)){t=n;break}if(!t)throw new Error("Could not find external tasks directory");let r=await S(t);for(let n of r){let a=await G(n);if(!a)continue;let l=f(n,a),d=L(t,n).replace(/^\.\/+|\([^)]+\)|^\/*|\/*$/g,"").replace(/[\/]{2,}/g,"/"),i=await y(l,d);v(l).on("change",async c=>{i.dispose(),i.stop(),u.info(`Restarting external task worker ${i.workerId} for topic ${d}`,{reason:"code changes detected",workerId:i.workerId,topic:d}),i=await y(l,d,i.workerId)}).on("unlink",async c=>{i.dispose(),i.stop(),u.info(`Stopping external task worker ${i.workerId} for topic ${d}`,{reason:`ETW ${c} was removed`,workerId:i.workerId,topic:d})}).on("error",c=>u.info(`Watcher error: ${c}`))}}async function y(e,t,s){let r=await H(e),n=k?await C():null,a={identity:await N(n),...r?.config};s&&(a={...a,workerId:s});let l=r.default,d=new b(w,t,l,a);return await T(n,d),d.onWorkerError((i,c,I)=>{u.error(`Intercepted "${i}"-type error: ${c.message}`,{err:c,type:i,externalTask:I})}),d.start(),u.info(`Started external task worker ${d.workerId} for topic ${t}`),d}async function G(e){let s=(await E.readdir(e)).filter(r=>r===K);if(s.length===0)return null;if(s.length>1)throw new Error(`Found more than one external task file in directory '${e}'`);return s[0]}async function C(){if(!k)throw new Error("No authority is configured");let e=await W.discover(process.env.PROCESSCUBE_AUTHORITY_URL);return await new e.Client({client_id:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_ID,client_secret:process.env.PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_SECRET}).grant({grant_type:"client_credentials",scope:"engine_etw"})}async function N(e){if(!k||e===null)return B;let t=e.access_token,s=q(t);return{token:e.access_token,userId:s.sub}}async function T(e,t,s=5){try{if(!k||e===null)return;let n=await X(e)*$*1e3;setTimeout(async()=>{let a=await C(),l=await N(a);t.identity=l,await T(a,t)},n)}catch(r){if(s===0)throw r;u.error(`Could not refresh identity for external task worker ${t.workerId}`,{err:r,workerId:t.workerId,retryCount:s});let n=2*1e3;setTimeout(async()=>await T(e,t,s-1),n)}}async function H(e){let t=await M({entryPoints:[e],write:!1,bundle:!0,platform:"node",target:"node18",format:"cjs"}),s=t.outputFiles[0].text,r=Y(s,e);if(t.errors.length>0)throw u.error(`Could not transpile file at '${e}'`,{errors:t.errors}),new Error(`Could not transpile file at '${e}'`);return t.warnings.length>0&&u.warn(`Transpiled file at '${e}' with warnings`,{warnings:t.warnings}),r}async function S(e){let t=await E.readdir(e,{withFileTypes:!0}),s=await Promise.all(t.map(async r=>{let n=f(e.toString(),r.name);return r.isDirectory()?[n,...await S(n)]:[]}));return Array.prototype.concat(...s)}async function X(e){let t=e.expires_in;if(!t&&e.expires_at&&(t=Math.floor(e.expires_at-Date.now()/1e3)),t===void 0)throw new Error("Could not determine the time until the access token for external task workers expires");return t}function Y(e,t){try{var s=module.constructor,r=new s;return r._compile(e,t),r.exports}catch(n){throw u.error("Could not require module from string",{err:n}),n}}function ye(){return o}import{cookies as m,headers as h}from"next/headers";import{ResponseCookies as j}from"next/dist/compiled/@edge-runtime/cookies";import{getToken as _}from"next-auth/jwt";var V=process.env.NEXTAUTH_URL?.startsWith("https://"),P=`${V?"__Secure-":""}next-auth.session-token`;async function Se(){let e=await _({req:{cookies:m(),headers:h()}});if(e?.expiresAt&&Date.now()>=e.expiresAt*1e3){let s=await fetch(`${process.env.NEXTAUTH_URL}/api/auth/session`,{method:"GET",headers:{"Content-Type":"application/json",cookie:m().toString()}}),r=new j(s.headers),n=r.get(P);if(n){let a=!1;try{m().set(P,n.value,{...n})}catch{a=!0}if(e=await _({req:{cookies:a?r:m(),headers:h()}}),e?.error)throw e.error}}if(!e?.accessToken||!e?.sub)throw new Error("AccessToken or Sub could not be determined!");return{token:e?.accessToken,userId:e?.sub}}import{DataModels as J}from"@5minds/processcube_engine_client";async function Ue(e){return await o.processInstances.query({...e?.query,state:J.ProcessInstances.ProcessInstanceState.running},e?.options)}import{DataModels as p}from"@5minds/processcube_engine_client";async function De(e={},t){let{processInstanceId:s,flowNodeId:r}=e;return new Promise(async(n,a)=>{let l=await o.userTasks.onUserTaskWaiting(async c=>{let I=c.flowNodeInstanceId===void 0,U=s!==void 0&&c.processInstanceId!==s,R=r!==void 0&&c.flowNodeId!==r,F=s!==void 0&&r!==void 0&&c.processInstanceId!==s&&c.flowNodeId!==r;if(I||U||R||F)return;let g=await Q(c.flowNodeInstanceId,{identity:t});return o.notification.removeSubscription(l,t),g===null?a(new Error(`UserTask with instance ID "${c.flowNodeInstanceId}" does not exist.`)):n(g)},{identity:t}),i=(await Z({processInstanceId:s,flowNodeId:r,state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:t})).userTasks[0];i&&(o.notification.removeSubscription(l,t),n(i))})}async function be(e,t,s,r){await o.userTasks.finishUserTask(e,t,r);let n=await o.userTasks.query({flowNodeId:s,state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:r});return n.userTasks.length===0?null:n.userTasks[0]}async function Z(...e){return await o.userTasks.query(...e)}async function Le(e){return await o.userTasks.query({state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},e)}async function Me(e,t){return await o.userTasks.query({processInstanceId:e,state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function Oe(e,t){return await o.userTasks.query({flowNodeId:e,state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function Q(e,t){let s=await o.userTasks.query({flowNodeInstanceId:e,state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},{...t,limit:1});return s.userTasks.length===0?null:s.userTasks[0]}async function We(e,t){return await o.userTasks.query({correlationId:e,state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},t)}async function qe(e,t){let s=await o.userTasks.query({state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),r=s.userTasks.filter(n=>n.actualOwnerId===e.userId);return s.userTasks=r,s}async function ve(e,t){let s=await o.userTasks.query({state:p.FlowNodeInstances.FlowNodeInstanceState.suspended},{identity:e,...t}),r=s.userTasks.filter(n=>n.assignedUserIds?.includes(e.userId));return s.userTasks=r,s}import{PHASE_DEVELOPMENT_SERVER as z,PHASE_PRODUCTION_SERVER as ee}from"next/dist/shared/lib/constants";function Ze(e={}){let{applicationSdk:t,...s}=e;return async(r,n)=>{let a=r===z||r===ee;return t?.useExternalTasks&&a&&await x(t?.customExternalTasksDirPath),s}}import{revalidatePath as te}from"next/cache";import{redirect as se}from"next/navigation";function rt(e){te(e),se(e)}async function at(...e){return await o.processModels.startProcessInstance(...e)}async function dt(...e){await o.userTasks.finishUserTask(...e)}export{dt as finishUserTask,be as finishUserTaskAndGetNext,Ue as getActiveProcessInstances,ve as getAssignedUserTasksByIdentity,ye as getEngineClient,Se as getIdentity,qe as getReservedUserTasksByIdentity,Z as getUserTasks,Q as getWaitingUserTaskByFlowNodeInstanceId,Le as getWaitingUserTasks,We as getWaitingUserTasksByCorrelationId,Oe as getWaitingUserTasksByFlowNodeId,Me as getWaitingUserTasksByProcessInstanceId,rt as navigateToUrl,at as startProcess,De as waitForUserTask,Ze as withApplicationSdk};