@joystick.js/node-canary 0.0.0-canary.447 → 0.0.0-canary.449

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/dist/app/index.js CHANGED
@@ -1 +1 @@
1
- import m from"./api/accounts/authenticated.js";import h from"./api/accounts/login.js";import g from"./api/accounts/logout.js";import f from"./api/accounts/recover_password.js";import d from"./api/accounts/reset_password.js";import l from"./api/accounts/signup.js";import b from"./api/accounts/user.js";import x from"./api/accounts/verify_email.js";import j from"./api/push/health.js";import w from"./api/test/accounts/delete.js";import y from"./api/test/accounts/signup.js";import q from"./api/test/bootstrap.js";import v from"./api/test/process.js";import O from"./api/test/queues.js";import k from"./databases/mongodb/create_indexes.js";import E from"./databases/postgresql/create_indexes.js";import R from"./databases/postgresql/create_tables.js";import D from"./generate_machine_id.js";import N from"./generate_process_id.js";import _ from"./databases/get_target_database_connection.js";import I from"./handle_process_errors.js";import P from"./settings/load.js";import S from"./queues/index.js";import J from"./register_app_options.js";import L from"./cron_jobs/register.js";import U from"./databases/register_database.js";import A from"./api/register_getters.js";import B from"./routes/register_route_from_function.js";import V from"./routes/register_route_from_object.js";import H from"./api/register_setters.js";import Q from"./uploaders/register.js";import T from"./websockets/register.js";import Y from"./start_express.js";import $ from"./start_node_as_cluster.js";import p from"../lib/types.js";import z from"./push_logs.js";const C=P();process.env.NODE_ENV!=="development"&&process.env.IS_PUSH_DEPLOYED&&z();class F{constructor(t={}){I(t?.events),J(this,t),this.generate_machine_id(),this.generate_process_id(),process.title=process.env.NODE_ENV==="test"?"joystick_test_app":"joystick_app",process.joystick={app_options:t}}async connect_databases(){const t=C?.config?.databases;for(let s=0;s<t?.length;s+=1){const e=t[s],r=parseInt(process.env.PORT,10)+10+s,a=t?.filter(n=>e?.provider===n?.provider)?.length>1;await U(e,r,a)}if(t?.length>0){const s=_("queues"),e=_("sessions"),r=_("users");process.databases._queues=s?.connection,process.databases._sessions=e?.connection,process.databases._users=r?.connection;const a=[s,e,r],n=a?.filter(i=>i?.provider==="mongodb")?.map(i=>i?.database_type);await k(n);const c=a?.filter(i=>i?.provider==="postgresql")?.map(i=>i?.database_type);await R(c),await E(c)}}async generate_machine_id(){this.joystick_machine_id=await D()}async generate_process_id(){this.joystick_process_id=await N()}on_after_start_server(t={}){process.on("message",s=>{typeof s=="string"&&JSON.parse(s)?.type==="BUILD_ERROR"&&(process.BUILD_ERROR=JSON.parse(s))}),console.log(`App running at: http://localhost:${t.port}`)}register_accounts(){this.express.app.get("/api/_accounts/authenticated",m),this.express.app.post("/api/_accounts/user",b),this.express.app.post("/api/_accounts/login",h),this.express.app.post("/api/_accounts/logout",g),this.express.app.post("/api/_accounts/recover-password",f),this.express.app.post("/api/_accounts/reset-password",d),this.express.app.post("/api/_accounts/signup",l),this.express.app.post("/api/_accounts/verify-email",x)}register_api(){const t=this?.options?.api?.getters,s=this?.options?.api?.setters,e=this?.options?.api?.options,r=this?.options?.api?.context;t&&p.is_object(t)&&Object.keys(t||{}).length>0&&A(this.express.app,Object.entries(t||{}),r,e),s&&p.is_object(s)&&Object.keys(s||{}).length>0&&H(this.express.app,Object.entries(s||{}),r,e)}register_cron_jobs(){L(this.options.cronJobs||this.options.cron_jobs)}register_fixtures(){p.is_function(this.options.fixtures)&&this.options.fixtures()}register_indexes(){p.is_function(this.options.indexes)&&this.options.indexes()}register_push(){this.express.app.get("/api/_push/health",j)}register_queues(){if(p.is_object(this.options.queues)){const t=Object.entries(this.options.queues||{});for(let s=0;s<t.length;s+=1){const[e,r]=t[s];process.queues={...process.queues||{},[e]:new S(e,r)}}}}register_routes(){const t=Object.entries(this?.options?.routes||{});for(let s=0;s<t?.length;s+=1){const[e,r]=t[s],a=p.is_object(r);p.is_function(r)&&B(this.express.app,e,r),a&&V(this.express.app,e,r)}}register_tests(){this.express.app.get("/api/_test/bootstrap",(t={},s={})=>q(t,s,this)),this.express.app.get("/api/_test/process",v),this.express.app.delete("/api/_test/accounts",w),this.express.app.post("/api/_test/accounts/signup",y),this.express.app.post("/api/_test/queues",(t={},s={})=>O(t,s,this))}register_uploaders(){Q(this.options.uploaders,this)}register_websockets(){T(this.options.websockets,this)}async start(){await this.connect_databases(),this.register_cron_jobs(),this.register_queues(),this.start_express(),this.register_tests(),this.register_accounts(),this.register_api(),this.register_routes(),this.register_websockets(),this.register_uploaders(),this.register_push(),this.register_fixtures(),this.register_indexes()}start_express(){this.express=Y(this.on_after_start_server,this)}}const u=async(o={})=>{const t=new F(o);return await t.start(o),t},G=(o={})=>new Promise(async t=>{if(o?.cluster)$(async()=>{const s=await u(o);return t(s.express)});else{const s=await u(o);return t(s.express)}});var Is=G;export{Is as default};
1
+ import m from"./api/accounts/authenticated.js";import h from"./api/accounts/login.js";import g from"./api/accounts/logout.js";import f from"./api/accounts/recover_password.js";import d from"./api/accounts/reset_password.js";import l from"./api/accounts/signup.js";import b from"./api/accounts/user.js";import x from"./api/accounts/verify_email.js";import j from"./api/push/health.js";import w from"./api/test/accounts/delete.js";import y from"./api/test/accounts/signup.js";import q from"./api/test/bootstrap.js";import v from"./api/test/process.js";import O from"./api/test/queues.js";import k from"./databases/mongodb/create_indexes.js";import E from"./databases/postgresql/create_indexes.js";import R from"./databases/postgresql/create_tables.js";import D from"./generate_machine_id.js";import N from"./generate_process_id.js";import _ from"./databases/get_target_database_connection.js";import I from"./handle_process_errors.js";import P from"./settings/load.js";import S from"./queues/index.js";import J from"./register_app_options.js";import L from"./cron_jobs/register.js";import U from"./databases/register_database.js";import A from"./api/register_getters.js";import B from"./routes/register_route_from_function.js";import V from"./routes/register_route_from_object.js";import H from"./api/register_setters.js";import Q from"./uploaders/register.js";import T from"./websockets/register.js";import Y from"./start_express.js";import $ from"./start_node_as_cluster.js";import p from"../lib/types.js";import z from"./push_logs.js";const C=P();process.env.NODE_ENV!=="development"&&process.env.IS_PUSH_DEPLOYED&&z();class F{constructor(t={}){I(t?.events),J(this,t),this.generate_machine_id(),this.generate_process_id(),process.title=process.env.NODE_ENV==="test"?"joystick_test_app":"joystick_app",process.joystick={app_options:t}}async connect_databases(){const t=C?.config?.databases;for(let s=0;s<t?.length;s+=1){const e=t[s],r=parseInt(process.env.PORT,10)+10+s,a=t?.filter(n=>e?.provider===n?.provider)?.length>1;await U(e,r,a)}if(t?.length>0){const s=_("queues"),e=_("sessions"),r=_("users");process.databases._queues=s?.connection,process.databases._sessions=e?.connection,process.databases._users=r?.connection;const a=[s,e,r],n=a?.filter(i=>i?.provider==="mongodb")?.map(i=>i?.database_type);await k(n);const c=a?.filter(i=>i?.provider==="postgresql")?.map(i=>i?.database_type);await R(c),await E(c)}}async generate_machine_id(){this.joystick_machine_id=await D()}async generate_process_id(){this.joystick_process_id=await N()}on_after_start_server(t={}){process.on("message",s=>{typeof s=="string"&&JSON.parse(s)?.type==="BUILD_ERROR"&&(process.BUILD_ERROR=JSON.parse(s))}),console.log(`App running at: http://localhost:${t.port}`)}register_accounts(){this.express.app.get("/api/_accounts/authenticated",m),this.express.app.post("/api/_accounts/user",b),this.express.app.post("/api/_accounts/login",h),this.express.app.post("/api/_accounts/logout",g),this.express.app.post("/api/_accounts/recover-password",f),this.express.app.post("/api/_accounts/reset-password",d),this.express.app.post("/api/_accounts/signup",l),this.express.app.post("/api/_accounts/verify-email",x)}register_api(){const t=this?.options?.api?.getters,s=this?.options?.api?.setters,e=this?.options?.api?.options,r=this?.options?.api?.context;t&&p.is_object(t)&&Object.keys(t||{}).length>0&&A(this.express.app,Object.entries(t||{}),r,e),s&&p.is_object(s)&&Object.keys(s||{}).length>0&&H(this.express.app,Object.entries(s||{}),r,e)}register_cron_jobs(){L(this.options.cronJobs||this.options.cron_jobs)}register_fixtures(){p.is_function(this.options.fixtures)&&this.options.fixtures()}register_indexes(){p.is_function(this.options.indexes)&&this.options.indexes()}register_push(){this.express.app.get("/api/_push/health",j)}register_queues(){if(p.is_object(this.options.queues)){const t=Object.entries(this.options.queues||{});for(let s=0;s<t.length;s+=1){const[e,r]=t[s];process.queues={...process.queues||{},[e]:new S(e,r)}}}}register_routes(){const t=Object.entries(this?.options?.routes||{});for(let s=0;s<t?.length;s+=1){const[e,r]=t[s],a=p.is_object(r);p.is_function(r)&&B(this.express.app,e,r),a&&V(this.express.app,e,r)}}register_tests(){this.express.app.get("/api/_test/bootstrap",(t={},s={})=>q(t,s,this)),this.express.app.get("/api/_test/process",v),this.express.app.delete("/api/_test/accounts",w),this.express.app.post("/api/_test/accounts/signup",y),this.express.app.post("/api/_test/queues",(t={},s={})=>O(t,s,this))}register_uploaders(){Q(this.options.uploaders,this)}register_websockets(){T(this.options.websockets,this)}async start(){await this.connect_databases(),this.register_cron_jobs(),this.register_queues(),this.start_express(),this.register_tests(),this.register_accounts(),this.register_push(),this.register_api(),this.register_routes(),this.register_websockets(),this.register_uploaders(),this.register_fixtures(),this.register_indexes()}start_express(){this.express=Y(this.on_after_start_server,this)}}const u=async(o={})=>{const t=new F(o);return await t.start(o),t},G=(o={})=>new Promise(async t=>{if(o?.cluster)$(async()=>{const s=await u(o);return t(s.express)});else{const s=await u(o);return t(s.express)}});var Is=G;export{Is as default};
@@ -1 +1 @@
1
- import d from"chalk";import h from"fs";import c from"os";import b from"../../lib/generate_id.js";import f from"../databases/get_target_database_connection.js";import l from"../databases/queries/map.js";import _ from"../../lib/timestamps.js";import i from"../../lib/types.js";import p from"../../test/track_function_call.js";class m{constructor(e="",t={}){this.init_database=this.init_database.bind(this),this.machine_id=h.readFileSync(`${c.homedir()}/.cheatcode/MACHINE_ID`,"utf-8")?.trim().replace(/\n/g,""),this.name=e,this.options={concurrent_jobs:1,...t},this.init_database(this?.options?.external,this?.options?.database?.provider)}async init_database(e=!1,t=null){const n=t||f("queues")?.provider,s=l[n]?.queues,o=this._get_database_connection();i.is_object(o)&&i.is_object(s)&&(this.db=Object.entries(s||{})?.reduce((a={},[r,u])=>(a[r]=u.bind({db:o,machine_id:this.machine_id,queue:{name:this.name,options:this.options}}),a),{_connection:o}),e||(await this.db.initialize_database(n),(this?.options?.runOnStartup||this?.options?.run_on_startup)&&this.run()))}_get_database_connection(){if(this?.options?.database){const{provider:e,name:t}=this?.options?.database,n=process.databases&&process.databases[e]&&process.databases[e][t];return n||console.warn(d.red(`Connection to database ${e}.${t} not found on process. Cannot start queue.`)),n||null}return process.databases._queues}async add(e={}){const t=(e?.next_run_at||e?.next_run_at)==="now"||!e?.next_run_at&&!e?.next_run_at?_.get_future_time():e?.next_run_at||e?.next_run_at,n={_id:b(16),status:"pending",environment:process.env.NODE_ENV,next_run_at:t,job:e?.job,payload:e?.payload},s=this?.options?.jobs&&this?.options?.jobs[e?.job];if(s&&(i.is_function(s?.preflight?.onBeforeAdd)||i.is_function(s?.preflight?.on_before_add))&&!await(s?.preflight?.onBeforeAdd||s?.preflight?.on_before_add)(n,this.db._connection,`queue_${this.name}`))return null;this.db.add_job(n)}async _check_if_okay_to_run_jobs(){return await this._get_number_of_jobs_running()<(this.options.concurrent_jobs||this.options.concurrent_jobs||1)}_get_number_of_jobs_running(){return this.db.count_jobs("running")}_handle_requeue_jobs_running_before_restart(){if(this.db)return!this.options.retryJobsRunningBeforeRestart&&!this.options.retry_jobs_running_before_restart?this.db.delete_incomplete_jobs_for_machine():this.db.set_jobs_for_machine_pending()}run(){this.db&&(process.env.NODE_ENV!=="test"&&console.log(`Starting ${this.name} queue...`),this._handle_requeue_jobs_running_before_restart().then(()=>{setInterval(async()=>{if(await this._check_if_okay_to_run_jobs()&&!process.env.HALT_QUEUES){const t=await this.db.get_next_job_to_run();this.handle_next_job(t)}},300)}))}async handle_next_job(e={}){const t=this.options.jobs[e?.job];if(e&&e?.job&&t&&i.is_function(t?.run))try{if((i.is_function(t?.preflight?.okayToRun)||i.is_function(t?.preflight?.okay_to_run))&&!await(t?.preflight?.okayToRun||t?.preflight?.okay_to_run)(e?.payload,e))return this._handle_requeue_job(e,_.get_future_time("seconds",t?.preflight?.requeueDelayInSeconds||t?.preflight?.requeue_delay_in_seconds||10));if((i.is_number(t?.maxAttempts)||i.is_number(t?.max_attempts))&&e?.attempts>=parseInt(t?.maxAttempts||t?.max_attempts,10))return(i.is_function(t?.onMaxAttemptsExhausted)||i.is_function(t?.on_max_attempts_exhausted))&&await(t.onMaxAttemptsExhausted||t.on_max_attempts_exhausted)(e),this._handle_delete_job(e?._id);await this._log_attempt(e?._id),p(`node.queues.${this?.name}.jobs.${e?.job}`,[e?.payload]),await t.run(e?.payload,{...e,queue:this,completed:()=>this._handle_job_completed(e?._id),failed:n=>this._handle_job_failed(e,t,n),delete:()=>this._handle_delete_job(e?._id),requeue:(n="")=>this._handle_requeue_job(e,n)})}catch(n){console.warn(n),this._handle_job_failed(e,t,n)}}_log_attempt(e=""){return this.db.log_attempt(e)}_handle_job_completed(e=""){return this.db.set_job_completed(e)}_handle_job_failed(e={},t={},n=""){return t?.requeueOnFailure||t?.requeue_on_failure?this._handle_requeue_job(e,_.get_future_time("seconds",10)):this.db.set_job_failed(e?._id,n)}_handle_delete_job(e=""){return this.db.delete_job(e)}_handle_requeue_job(e={},t=null){return this.db.requeue_job(e?._id,t||_.get_future_time())}list(e=""){const t={};return e&&(t.status=e),this.db.get_jobs(t)}}var $=m;export{$ as default};
1
+ import d from"chalk";import h from"fs";import c from"os";import b from"../../lib/generate_id.js";import f from"../databases/get_target_database_connection.js";import l from"../databases/queries/map.js";import _ from"../../lib/timestamps.js";import i from"../../lib/types.js";import p from"../../test/track_function_call.js";class m{constructor(e="",t={}){this.init_database=this.init_database.bind(this),this.machine_id=h.readFileSync(`${c.homedir()}/.cheatcode/MACHINE_ID`,"utf-8")?.trim().replace(/\n/g,""),this.name=e,this.options={concurrent_jobs:1,...t},this.init_database(this?.options?.external,this?.options?.database?.provider)}async init_database(e=!1,t=null){const n=t||f("queues")?.provider,s=l[n]?.queues,o=this._get_database_connection();i.is_object(o)&&i.is_object(s)&&(this.db=Object.entries(s||{})?.reduce((a={},[r,u])=>(a[r]=u.bind({db:o,machine_id:this.machine_id,queue:{name:this.name,options:this.options}}),a),{_connection:o}),e||(await this.db.initialize_database(n),(this?.options?.runOnStartup||this?.options?.run_on_startup)&&this.run()))}_get_database_connection(){if(this?.options?.database){const{provider:e,name:t}=this?.options?.database,n=process.databases&&process.databases[e]&&process.databases[e][t];return n||console.warn(d.red(`Connection to database ${e}.${t} not found on process. Cannot start queue.`)),n||null}return process.databases._queues}async add(e={}){const t=(e?.nextRunAt||e?.next_run_at)==="now"||!e?.nextRunAt&&!e?.next_run_at?_.get_future_time():e?.nextRunAt||e?.next_run_at,n={_id:b(16),status:"pending",environment:process.env.NODE_ENV,next_run_at:t,job:e?.job,payload:e?.payload},s=this?.options?.jobs&&this?.options?.jobs[e?.job];if(s&&(i.is_function(s?.preflight?.onBeforeAdd)||i.is_function(s?.preflight?.on_before_add))&&!await(s?.preflight?.onBeforeAdd||s?.preflight?.on_before_add)(n,this.db._connection,`queue_${this.name}`))return null;this.db.add_job(n)}async _check_if_okay_to_run_jobs(){return await this._get_number_of_jobs_running()<(this.options.concurrentJobs||this.options.concurrent_jobs||1)}_get_number_of_jobs_running(){return this.db.count_jobs("running")}_handle_requeue_jobs_running_before_restart(){if(this.db)return!this.options.retryJobsRunningBeforeRestart&&!this.options.retry_jobs_running_before_restart?this.db.delete_incomplete_jobs_for_machine():this.db.set_jobs_for_machine_pending()}run(){this.db&&(process.env.NODE_ENV!=="test"&&console.log(`Starting ${this.name} queue...`),this._handle_requeue_jobs_running_before_restart().then(()=>{setInterval(async()=>{if(await this._check_if_okay_to_run_jobs()&&!process.env.HALT_QUEUES){const t=await this.db.get_next_job_to_run();this.handle_next_job(t)}},300)}))}async handle_next_job(e={}){const t=this.options.jobs[e?.job];if(e&&e?.job&&t&&i.is_function(t?.run))try{if((i.is_function(t?.preflight?.okayToRun)||i.is_function(t?.preflight?.okay_to_run))&&!await(t?.preflight?.okayToRun||t?.preflight?.okay_to_run)(e?.payload,e))return this._handle_requeue_job(e,_.get_future_time("seconds",t?.preflight?.requeueDelayInSeconds||t?.preflight?.requeue_delay_in_seconds||10));if((i.is_number(t?.maxAttempts)||i.is_number(t?.max_attempts))&&e?.attempts>=parseInt(t?.maxAttempts||t?.max_attempts,10))return(i.is_function(t?.onMaxAttemptsExhausted)||i.is_function(t?.on_max_attempts_exhausted))&&await(t.onMaxAttemptsExhausted||t.on_max_attempts_exhausted)(e),this._handle_delete_job(e?._id);await this._log_attempt(e?._id),p(`node.queues.${this?.name}.jobs.${e?.job}`,[e?.payload]),await t.run(e?.payload,{...e,queue:this,completed:()=>this._handle_job_completed(e?._id),failed:n=>this._handle_job_failed(e,t,n),delete:()=>this._handle_delete_job(e?._id),requeue:(n="")=>this._handle_requeue_job(e,n)})}catch(n){console.warn(n),this._handle_job_failed(e,t,n)}}_log_attempt(e=""){return this.db.log_attempt(e)}_handle_job_completed(e=""){return this.db.set_job_completed(e)}_handle_job_failed(e={},t={},n=""){return t?.requeueOnFailure||t?.requeue_on_failure?this._handle_requeue_job(e,_.get_future_time("seconds",10)):this.db.set_job_failed(e?._id,n)}_handle_delete_job(e=""){return this.db.delete_job(e)}_handle_requeue_job(e={},t=null){return this.db.requeue_job(e?._id,t||_.get_future_time())}list(e=""){const t={};return e&&(t.status=e),this.db.get_jobs(t)}}var R=m;export{R as default};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@joystick.js/node-canary",
3
3
  "type": "module",
4
- "version": "0.0.0-canary.447",
4
+ "version": "0.0.0-canary.449",
5
5
  "description": "The Node.js framework for Joystick.",
6
6
  "main": "./dist/index.js",
7
7
  "scripts": {
package/src/app/index.js CHANGED
@@ -216,11 +216,11 @@ class App {
216
216
  this.start_express();
217
217
  this.register_tests();
218
218
  this.register_accounts();
219
+ this.register_push();
219
220
  this.register_api();
220
221
  this.register_routes();
221
222
  this.register_websockets();
222
223
  this.register_uploaders();
223
- this.register_push();
224
224
  this.register_fixtures();
225
225
  this.register_indexes();
226
226
  }
@@ -81,9 +81,9 @@ class Queue {
81
81
  async add(options = {}) {
82
82
  // NOTE: If no next_run_at specified, run the job ASAP.
83
83
  const next_run_at =
84
- (options?.next_run_at || options?.next_run_at) === "now" || (!options?.next_run_at && !options?.next_run_at)
84
+ (options?.nextRunAt || options?.next_run_at) === "now" || (!options?.nextRunAt && !options?.next_run_at)
85
85
  ? timestamps.get_future_time()
86
- : (options?.next_run_at || options?.next_run_at);
86
+ : (options?.nextRunAt || options?.next_run_at);
87
87
 
88
88
  const job_to_add = {
89
89
  _id: generate_id(16),
@@ -123,7 +123,7 @@ class Queue {
123
123
 
124
124
  async _check_if_okay_to_run_jobs() {
125
125
  const jobs_running = await this._get_number_of_jobs_running();
126
- return jobs_running < (this.options.concurrent_jobs || this.options.concurrent_jobs || 1);
126
+ return jobs_running < (this.options.concurrentJobs || this.options.concurrent_jobs || 1);
127
127
  }
128
128
 
129
129
  _get_number_of_jobs_running() {