UNPKG

slavery-js

Version:

A simple clustering app that allows you to scale an application on multiple thread, containers or machines

1 lines 4.31 kB
{"version":3,"sources":["../../src/app/entry.ts"],"sourcesContent":["import PeerDiscoveryServer from './peerDiscovery/index.js';\nimport makeProxyObject from './makeProxyObject.js';\nimport Service, { SlaveMethods, Options as ServiceOptions } from '../service/index.js';\nimport { isSlaveMethods, isMasterCallback } from './typeGuards.js';\n//import type { \n\ntype CallableFunction = (...args: any[]) => any;\n\ntype MasterCallback = Function | CallableFunction;\n\ntype EntryOptions = {\n host: string;\n port: number;\n}\n\nconst entry = (entryOptions: EntryOptions) => {\n // this function is use to set up the options for the servies\n let options = entryOptions;\n // make a proxy object will take of xreating each service\n let proxyObject = makeProxyObject(handleProxyCall(options));\n // make the peer discovery server\n let peerDiscoveryServer = new PeerDiscoveryServer({\n host: options.host,\n port: options.port,\n });\n peerDiscoveryServer.start();\n // return the proxy object\n return proxyObject; // <--- the proxy obj will return itself in perpituity\n}\n\n\nconst handleProxyCall = (globalOptions: EntryOptions) => (\n // this are all of the possible input to service now the question is who to know \n // which one wa passed\n method: string,\n param1: MasterCallback | SlaveMethods,\n param2?: SlaveMethods | ServiceOptions, \n param3?: ServiceOptions) => {\n // first we check which function where passed\n const { mastercallback, slaveMethods, options } = paramertesDiscermination(param1, param2, param3);\n if(mastercallback === undefined) throw new Error('Master callback is undefined');\n // get the service name and the functions\n const service_name = method;\n // get the port and the host\n const port = globalOptions.port;\n const host = globalOptions.host;\n // make a new service\n let service = new Service({\n service_name,\n peerDiscoveryAddress: { host, port },\n mastercallback: mastercallback as CallableFunction,\n slaveMethods,\n options,\n })\n service.start()\n }\n\n\nconst paramertesDiscermination = (param1: MasterCallback | SlaveMethods, param2?: SlaveMethods | ServiceOptions, param3?: ServiceOptions) => {\n let mastercallback, slaveMethods, options;\n // check if the first paramet is either a MasterCallback or SlaveMethods\n if( isMasterCallback(param1) ) {\n mastercallback = param1;\n // check what the second paramter is\n if( isSlaveMethods(param2) ) {\n slaveMethods = param2;\n options = param3 || {};\n }\n } else if( isSlaveMethods(param1) ) {\n mastercallback = () => {};\n slaveMethods = param1;\n // check what the second paramter is\n options = param2 || {};\n } else { \n throw new Error('Invalid first parameter. Must be either a funcition or an object');\n }\n return { \n mastercallback,\n slaveMethods,\n options\n }\n}\n\n\n\n\nexport default entry;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAAgC;AAChC,6BAA4B;AAC5B,qBAAiE;AACjE,wBAAiD;AAYjD,MAAM,QAAQ,CAAC,iBAA+B;AAE1C,MAAI,UAAU;AAEd,MAAI,kBAAc,uBAAAA,SAAgB,gBAAgB,OAAO,CAAC;AAE1D,MAAI,sBAAsB,IAAI,qBAAAC,QAAoB;AAAA,IAC9C,MAAM,QAAQ;AAAA,IACd,MAAM,QAAQ;AAAA,EAClB,CAAC;AACD,sBAAoB,MAAM;AAE1B,SAAO;AACX;AAGA,MAAM,kBAAkB,CAAC,kBAAgC,CAGrD,QACA,QACA,QACA,WAA4B;AAExB,QAAM,EAAE,gBAAgB,cAAc,QAAQ,IAAI,yBAAyB,QAAQ,QAAQ,MAAM;AACjG,MAAG,mBAAmB,OAAW,OAAM,IAAI,MAAM,8BAA8B;AAE/E,QAAM,eAAe;AAErB,QAAM,OAAO,cAAc;AAC3B,QAAM,OAAO,cAAc;AAE3B,MAAI,UAAU,IAAI,eAAAC,QAAQ;AAAA,IACtB;AAAA,IACA,sBAAsB,EAAE,MAAM,KAAK;AAAA,IACnC;AAAA,IACA;AAAA,IACA;AAAA,EACJ,CAAC;AACD,UAAQ,MAAM;AAClB;AAGJ,MAAM,2BAA2B,CAAC,QAAuC,QAAwC,WAA4B;AACzI,MAAI,gBAAgB,cAAc;AAElC,UAAI,oCAAiB,MAAM,GAAI;AAC3B,qBAAiB;AAEjB,YAAI,kCAAe,MAAM,GAAI;AACzB,qBAAe;AACf,gBAAU,UAAU,CAAC;AAAA,IACzB;AAAA,E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