@joystick.js/node-canary 0.0.0-canary.31 → 0.0.0-canary.310
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 +2 -2
- package/dist/action/class.js +21 -0
- package/dist/api/get.js +15 -13
- package/dist/api/set.js +15 -13
- package/dist/app/accounts/createMetadataTableColumns.js +12 -0
- package/dist/app/accounts/deleteUser.js +7 -0
- package/dist/app/accounts/generateSession.js +2 -4
- package/dist/app/accounts/getBrowserSafeUser.js +5 -2
- package/dist/app/accounts/hasLoginTokenExpired.js +1 -2
- package/dist/app/accounts/index.js +2 -0
- package/dist/app/accounts/login.js +6 -0
- package/dist/app/accounts/recoverPassword.js +3 -0
- package/dist/app/accounts/resetPassword.js +6 -0
- package/dist/app/accounts/signup.js +50 -7
- package/dist/app/accounts/verifyEmail.js +3 -0
- package/dist/app/cluster.js +26 -0
- package/dist/app/databases/generate_sql_from_object.js +45 -0
- package/dist/app/databases/mongodb/buildConnectionString.js +1 -1
- package/dist/app/databases/mongodb/createAccountsIndexes.js +18 -0
- package/dist/app/databases/mongodb/createSessionsIndexes.js +10 -0
- package/dist/app/databases/mongodb/index.js +1 -1
- package/dist/app/databases/mongodb/queries/accounts.js +8 -1
- package/dist/app/databases/mongodb/queries/queues.js +40 -26
- package/dist/app/databases/mongodb/queries/sessions.js +26 -0
- package/dist/app/databases/postgresql/createSessionsIndexes.js +10 -0
- package/dist/app/databases/postgresql/createSessionsTables.js +14 -0
- package/dist/app/databases/postgresql/handleCleanupQueues.js +35 -0
- package/dist/app/databases/postgresql/index.js +66 -4
- package/dist/app/databases/postgresql/queries/accounts.js +5 -2
- package/dist/app/databases/postgresql/queries/queues.js +69 -36
- package/dist/app/databases/postgresql/queries/sessions.js +43 -0
- package/dist/app/databases/queryMap.js +6 -2
- package/dist/app/databases/stringToSnakeCase.js +6 -0
- package/dist/app/getBrowserSafeRequest.js +3 -2
- package/dist/app/index.js +222 -75
- package/dist/app/initExpress.js +1 -1
- package/dist/app/middleware/csp.js +2 -2
- package/dist/app/middleware/getTranslations.js +64 -0
- package/dist/app/middleware/get_insecure_landing_page_html.js +71 -0
- package/dist/app/middleware/hmr/client.js +13 -9
- package/dist/app/middleware/index.js +6 -5
- package/dist/app/middleware/insecure.js +3 -4
- package/dist/app/middleware/render.js +11 -68
- package/dist/app/middleware/session.js +12 -11
- package/dist/app/queues/index.js +64 -28
- package/dist/app/registerGetters.js +5 -6
- package/dist/app/registerSetters.js +5 -6
- package/dist/app/runGetter.js +17 -5
- package/dist/app/runSessionQuery.js +15 -0
- package/dist/app/runSetter.js +17 -5
- package/dist/app/sanitizeAPIResponse.js +1 -1
- package/dist/app/validateSession.js +8 -3
- package/dist/app/validateUploaderOptions.js +3 -3
- package/dist/app/validateUploads.js +12 -1
- package/dist/email/send.js +7 -1
- package/dist/email/templates/reset-password.js +0 -1
- package/dist/fixture/index.js +40 -0
- package/dist/index.js +19 -0
- package/dist/lib/escapeKeyValuePair.js +13 -0
- package/dist/lib/formatAPIError.js +0 -1
- package/dist/lib/getBuildPath.js +1 -1
- package/dist/lib/getSSLCertificates.js +3 -3
- package/dist/lib/importFile.js +7 -0
- package/dist/lib/isValidJSONString.js +1 -1
- package/dist/lib/log.js +0 -3
- package/dist/lib/objectToSQLKeysString.js +1 -1
- package/dist/lib/objectToSQLValuesString.js +1 -1
- package/dist/lib/serializeQueryParameters.js +1 -1
- package/dist/lib/timestamps.js +47 -0
- package/dist/lib/wait.js +8 -0
- package/dist/push/logs/index.js +10 -3
- package/dist/settings/load.js +3 -5
- package/dist/ssr/compileCSS.js +4 -4
- package/dist/ssr/findComponentInTree.js +1 -1
- package/dist/ssr/getAPIForDataFunctions.js +35 -0
- package/dist/ssr/getDataFromComponent.js +15 -0
- package/dist/ssr/index.js +19 -45
- package/dist/ssr/replaceWhenTags.js +2 -3
- package/dist/ssr/setHeadTagsInHTML.js +3 -3
- package/dist/test/index.js +9 -0
- package/dist/test/trackFunctionCall.js +17 -0
- package/dist/validation/inputWithSchema/index.js +3 -3
- package/dist/validation/schema/index.js +5 -5
- package/dist/websockets/index.js +4 -0
- package/getSanitizedContext.js +43 -0
- package/package.json +2 -1
- package/dist/app/accounts/roles/index.test.js +0 -123
- package/dist/app/index.test.js +0 -575
- package/dist/app/middleware/sanitizeRequestParameters.js +0 -21
- package/dist/email/send.test.js +0 -37
- package/dist/validation/index.test.js +0 -463
|
@@ -1,40 +1,47 @@
|
|
|
1
|
-
import
|
|
1
|
+
import getTargetDatabaseProvider from "../../getTargetDatabaseProvider.js";
|
|
2
2
|
var queues_default = {
|
|
3
3
|
addJob: function(jobToAdd = {}) {
|
|
4
|
-
const db =
|
|
5
|
-
return db.insertOne(
|
|
4
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
5
|
+
return db.insertOne({
|
|
6
|
+
...jobToAdd,
|
|
7
|
+
attempts: 0
|
|
8
|
+
});
|
|
6
9
|
},
|
|
7
10
|
countJobs: function(status = "") {
|
|
8
|
-
const db =
|
|
11
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
9
12
|
return db.countDocuments({ status });
|
|
10
13
|
},
|
|
11
14
|
deleteJob: function(jobId = "") {
|
|
12
|
-
const db =
|
|
15
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
13
16
|
return db.deleteOne({ _id: jobId });
|
|
14
17
|
},
|
|
18
|
+
deleteIncompleteJobsForMachine: function() {
|
|
19
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
20
|
+
return db.deleteMany({ status: { $in: ["incomplete", "running"] }, lockedBy: this.machineId });
|
|
21
|
+
},
|
|
15
22
|
getJobs: function(query = {}) {
|
|
16
|
-
const db =
|
|
23
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
17
24
|
return db.find(query).toArray();
|
|
18
25
|
},
|
|
19
26
|
getNextJobToRun: async function() {
|
|
20
|
-
const db =
|
|
27
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
21
28
|
const nextJob = await db.findOneAndUpdate({
|
|
22
29
|
$or: [
|
|
23
30
|
{
|
|
24
31
|
status: "pending",
|
|
25
|
-
nextRunAt: { $lte:
|
|
32
|
+
nextRunAt: { $lte: new Date().toISOString() },
|
|
26
33
|
lockedBy: { $exists: false }
|
|
27
34
|
},
|
|
28
35
|
{
|
|
29
36
|
status: "pending",
|
|
30
|
-
nextRunAt: { $lte:
|
|
37
|
+
nextRunAt: { $lte: new Date().toISOString() },
|
|
31
38
|
lockedBy: null
|
|
32
39
|
}
|
|
33
40
|
]
|
|
34
41
|
}, {
|
|
35
42
|
$set: {
|
|
36
43
|
status: "running",
|
|
37
|
-
startedAt:
|
|
44
|
+
startedAt: new Date().toISOString(),
|
|
38
45
|
lockedBy: this.machineId
|
|
39
46
|
}
|
|
40
47
|
}, {
|
|
@@ -44,19 +51,34 @@ var queues_default = {
|
|
|
44
51
|
});
|
|
45
52
|
return nextJob?.value;
|
|
46
53
|
},
|
|
47
|
-
initializeDatabase: function() {
|
|
48
|
-
const db =
|
|
54
|
+
initializeDatabase: async function() {
|
|
55
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
56
|
+
const indexes = await this.db?.collection(`queue_${this.queue.name}`).indexes();
|
|
49
57
|
db.createIndex({ status: 1 });
|
|
50
58
|
db.createIndex({ status: 1, nextRunAt: 1 });
|
|
51
59
|
if (this.queue.options?.cleanup?.completedAfterSeconds) {
|
|
60
|
+
if (indexes?.find((index) => index?.name === "completedAt_1")) {
|
|
61
|
+
await db.dropIndex({ completedAt: 1 });
|
|
62
|
+
}
|
|
52
63
|
db.createIndex({ completedAt: 1 }, { expireAfterSeconds: this?.queue?.options?.cleanup?.completedAfterSeconds });
|
|
53
64
|
}
|
|
54
65
|
if (this.queue.options?.cleanup?.failedAfterSeconds) {
|
|
66
|
+
if (indexes?.find((index) => index?.name === "failedAt_1")) {
|
|
67
|
+
await db.dropIndex({ failedAt: 1 });
|
|
68
|
+
}
|
|
55
69
|
db.createIndex({ failedAt: 1 }, { expireAfterSeconds: this?.queue?.options?.cleanup?.failedAfterSeconds });
|
|
56
70
|
}
|
|
57
71
|
},
|
|
72
|
+
logAttempt: function(jobId = "") {
|
|
73
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
74
|
+
return db.updateOne({ _id: jobId }, {
|
|
75
|
+
$inc: {
|
|
76
|
+
attempts: 1
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
},
|
|
58
80
|
requeueJob: function(jobId = "", nextRunAt = null) {
|
|
59
|
-
const db =
|
|
81
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
60
82
|
return db.updateOne({ _id: jobId }, {
|
|
61
83
|
$set: {
|
|
62
84
|
status: "pending",
|
|
@@ -67,35 +89,27 @@ var queues_default = {
|
|
|
67
89
|
}
|
|
68
90
|
});
|
|
69
91
|
},
|
|
70
|
-
setJobsForMachineIncomplete: function() {
|
|
71
|
-
const db = process.databases._queues?.collection(`queue_${this.queue.name}`);
|
|
72
|
-
return db.updateMany({ status: "running", lockedBy: this.machineId }, {
|
|
73
|
-
$set: {
|
|
74
|
-
status: "incomplete"
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
},
|
|
78
92
|
setJobCompleted: function(jobId = "") {
|
|
79
|
-
const db =
|
|
93
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
80
94
|
return db.updateOne({ _id: jobId }, {
|
|
81
95
|
$set: {
|
|
82
96
|
status: "completed",
|
|
83
|
-
completedAt:
|
|
97
|
+
completedAt: new Date(new Date().toISOString())
|
|
84
98
|
}
|
|
85
99
|
});
|
|
86
100
|
},
|
|
87
101
|
setJobFailed: function(jobId = "", error = "") {
|
|
88
|
-
const db =
|
|
102
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
89
103
|
return db.updateOne({ _id: jobId }, {
|
|
90
104
|
$set: {
|
|
91
105
|
status: "failed",
|
|
92
|
-
failedAt:
|
|
106
|
+
failedAt: new Date(new Date().toISOString()),
|
|
93
107
|
error
|
|
94
108
|
}
|
|
95
109
|
});
|
|
96
110
|
},
|
|
97
111
|
setJobsForMachinePending: function() {
|
|
98
|
-
const db =
|
|
112
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
99
113
|
return db.updateMany({ status: { $in: ["pending", "running"] }, lockedBy: this.machineId }, {
|
|
100
114
|
$set: {
|
|
101
115
|
status: "pending"
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import generateId from "../../../../lib/generateId";
|
|
3
|
+
var sessions_default = {
|
|
4
|
+
create_session: async () => {
|
|
5
|
+
const session_id = generateId();
|
|
6
|
+
await process.databases._sessions?.collection("sessions").insertOne({
|
|
7
|
+
_id: session_id,
|
|
8
|
+
csrf: generateId(32),
|
|
9
|
+
createdAt: new Date().toISOString()
|
|
10
|
+
});
|
|
11
|
+
return session_id;
|
|
12
|
+
},
|
|
13
|
+
get_session: async (input = {}) => {
|
|
14
|
+
return process.databases._sessions?.collection("sessions").findOne({
|
|
15
|
+
_id: input?.session_id
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
delete_expired_sessions: async () => {
|
|
19
|
+
return process.databases._sessions?.collection("sessions").deleteMany({
|
|
20
|
+
createdAt: { $lte: dayjs().subtract(1, "hour").format() }
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
sessions_default as default
|
|
26
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const createIndex = async (indexName = "", tableName = "", tableColumns = []) => {
|
|
2
|
+
return process.databases._sessions?.query(`CREATE INDEX IF NOT EXISTS ${indexName} ON ${tableName}(${tableColumns.join(", ")})`);
|
|
3
|
+
};
|
|
4
|
+
var createSessionsIndexes_default = async () => {
|
|
5
|
+
await createIndex("session_by_id", "sessions", ["session_id"]);
|
|
6
|
+
await createIndex("session_created_at", "sessions", ["created_at"]);
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
createSessionsIndexes_default as default
|
|
10
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const createTable = async (table = "", tableColumns = []) => {
|
|
2
|
+
return process.databases._sessions?.query(`CREATE TABLE IF NOT EXISTS ${table} (${tableColumns.join(", ")})`);
|
|
3
|
+
};
|
|
4
|
+
var createSessionsTables_default = async () => {
|
|
5
|
+
await createTable("sessions", [
|
|
6
|
+
"id bigserial primary key",
|
|
7
|
+
"session_id text",
|
|
8
|
+
"csrf text",
|
|
9
|
+
"created_at text"
|
|
10
|
+
]);
|
|
11
|
+
};
|
|
12
|
+
export {
|
|
13
|
+
createSessionsTables_default as default
|
|
14
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const get_future_timestamp = (timestamp, seconds = 0) => {
|
|
2
|
+
const date = new Date(timestamp);
|
|
3
|
+
date.setSeconds(date.getSeconds() + seconds);
|
|
4
|
+
return date.toISOString();
|
|
5
|
+
};
|
|
6
|
+
var handleCleanupQueues_default = async ({
|
|
7
|
+
database = {},
|
|
8
|
+
table = "",
|
|
9
|
+
seconds = 0
|
|
10
|
+
}) => {
|
|
11
|
+
const jobs_with_status = await database.query(`
|
|
12
|
+
SELECT * FROM ${table} WHERE status = ANY($1)
|
|
13
|
+
`, [
|
|
14
|
+
["completed", "failed"]
|
|
15
|
+
]);
|
|
16
|
+
const jobs_to_cleanup = jobs_with_status?.filter((job = {}) => {
|
|
17
|
+
if (job?.status === "completed") {
|
|
18
|
+
const completed_at_with_seconds = get_future_timestamp(job?.completed_at, seconds);
|
|
19
|
+
return completed_at_with_seconds <= new Date().toISOString();
|
|
20
|
+
}
|
|
21
|
+
if (job?.status === "failed") {
|
|
22
|
+
const failed_at_with_seconds = get_future_timestamp(job?.failed_at, seconds);
|
|
23
|
+
return failed_at_with_seconds <= new Date().toISOString();
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
await database.query(`
|
|
28
|
+
DELETE FROM ${table} WHERE _id = ANY($1)
|
|
29
|
+
`, [
|
|
30
|
+
jobs_to_cleanup?.map(({ _id }) => _id)
|
|
31
|
+
]);
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
handleCleanupQueues_default as default
|
|
35
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import postgresql from "pg";
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import os from "os";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import generate_sql_from_object from "../generate_sql_from_object";
|
|
4
6
|
const { Pool } = postgresql;
|
|
5
7
|
var postgresql_default = async (settings = {}, databasePort = 2610) => {
|
|
6
8
|
const connection = settings?.connection || {
|
|
@@ -13,18 +15,78 @@ var postgresql_default = async (settings = {}, databasePort = 2610) => {
|
|
|
13
15
|
};
|
|
14
16
|
try {
|
|
15
17
|
const host = connection.hosts && connection.hosts[0];
|
|
16
|
-
const
|
|
18
|
+
const connection_config = {
|
|
17
19
|
user: connection?.username || "",
|
|
18
20
|
database: connection?.database,
|
|
19
21
|
password: connection?.password || "",
|
|
20
22
|
host: host?.hostname,
|
|
21
23
|
port: host?.port,
|
|
22
24
|
...settings?.options || {}
|
|
23
|
-
}
|
|
25
|
+
};
|
|
26
|
+
if (settings?.options?.ssl?.ca) {
|
|
27
|
+
connection_config.ssl = {
|
|
28
|
+
ca: fs.readFileSync(settings?.options?.ssl?.ca)
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const pool = new Pool(connection_config);
|
|
24
32
|
return {
|
|
25
33
|
pool,
|
|
26
|
-
query: (...
|
|
27
|
-
return pool.query(...
|
|
34
|
+
query: (...args2) => {
|
|
35
|
+
return pool.query(...args2).then((response) => {
|
|
36
|
+
return response?.rows || [];
|
|
37
|
+
}).catch((error) => {
|
|
38
|
+
console.log(chalk.redBright(`
|
|
39
|
+
Failed SQL Statement:
|
|
40
|
+
`));
|
|
41
|
+
console.log(args2[0]);
|
|
42
|
+
console.log(`
|
|
43
|
+
`);
|
|
44
|
+
console.log(chalk.redBright(`
|
|
45
|
+
Failed Values:
|
|
46
|
+
`));
|
|
47
|
+
console.log(args2[1]);
|
|
48
|
+
throw error;
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
select: (options = {}) => {
|
|
52
|
+
const select = generate_sql_from_object.select(options);
|
|
53
|
+
return pool.query(select.statement, select.where).then((response) => {
|
|
54
|
+
return response?.rows || [];
|
|
55
|
+
}).catch((error) => {
|
|
56
|
+
console.log(chalk.redBright(`
|
|
57
|
+
Failed SQL Statement:
|
|
58
|
+
`));
|
|
59
|
+
console.log(args[0]);
|
|
60
|
+
console.log(`
|
|
61
|
+
`);
|
|
62
|
+
console.log(chalk.redBright(`
|
|
63
|
+
Failed Values:
|
|
64
|
+
`));
|
|
65
|
+
console.log(args[1]);
|
|
66
|
+
throw error;
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
insert: (options = {}) => {
|
|
70
|
+
const insert = generate_sql_from_object.insert(options);
|
|
71
|
+
return pool.query(insert.statement, insert.values).then((response) => {
|
|
72
|
+
return response?.rows || [];
|
|
73
|
+
}).catch((error) => {
|
|
74
|
+
console.log(chalk.redBright(`
|
|
75
|
+
Failed SQL Statement:
|
|
76
|
+
`));
|
|
77
|
+
console.log(args[0]);
|
|
78
|
+
console.log(`
|
|
79
|
+
`);
|
|
80
|
+
console.log(chalk.redBright(`
|
|
81
|
+
Failed Values:
|
|
82
|
+
`));
|
|
83
|
+
console.log(args[1]);
|
|
84
|
+
throw error;
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
update: (options = {}) => {
|
|
88
|
+
const update = generate_sql_from_object.update(options);
|
|
89
|
+
return pool.query(update.statement, update.values).then((response) => {
|
|
28
90
|
return response?.rows || [];
|
|
29
91
|
}).catch((error) => {
|
|
30
92
|
console.log(chalk.redBright(`
|
|
@@ -21,7 +21,7 @@ var accounts_default = {
|
|
|
21
21
|
},
|
|
22
22
|
createUser: async (input = {}) => {
|
|
23
23
|
const userId = generateId();
|
|
24
|
-
const keys = ["user_id", ...Object.keys(input) || []]?.map((inputKey) => {
|
|
24
|
+
const keys = ["user_id", ...Object.keys(input || {}) || []]?.map((inputKey) => {
|
|
25
25
|
return camelPascalToSnake(inputKey);
|
|
26
26
|
})?.join(",");
|
|
27
27
|
const values = [userId, ...Object.values(input) || []];
|
|
@@ -43,8 +43,11 @@ var accounts_default = {
|
|
|
43
43
|
}
|
|
44
44
|
return null;
|
|
45
45
|
},
|
|
46
|
+
deleteUser: async (input = {}) => {
|
|
47
|
+
await process.databases._users?.query(`DELETE FROM users WHERE user_id = $1`, [input?.userId]);
|
|
48
|
+
},
|
|
46
49
|
deleteOldSessions: async (input = {}) => {
|
|
47
|
-
await process.databases._users?.query(`DELETE FROM users_sessions WHERE user_id = $1 AND token_expires_at::
|
|
50
|
+
await process.databases._users?.query(`DELETE FROM users_sessions WHERE user_id = $1 AND token_expires_at::timestamp < NOW()`, [input?.userId]);
|
|
48
51
|
},
|
|
49
52
|
addSession: async (input = {}) => {
|
|
50
53
|
await process.databases._users?.query(`INSERT INTO users_sessions(user_id, token, token_expires_at) VALUES ($1, $2, $3);`, [input?.userId, input?.session?.token, input?.session?.tokenExpiresAt]);
|
|
@@ -1,27 +1,30 @@
|
|
|
1
|
-
import
|
|
1
|
+
import cron from "node-cron";
|
|
2
|
+
import handleCleanupQueues from "../handleCleanupQueues";
|
|
2
3
|
var queues_default = {
|
|
3
4
|
addJob: function(jobToAdd = {}) {
|
|
4
|
-
const db =
|
|
5
|
+
const db = this?.db;
|
|
5
6
|
return db?.query(`
|
|
6
7
|
INSERT INTO queue_${this.queue.name} (
|
|
7
8
|
_id,
|
|
8
9
|
status,
|
|
9
10
|
job,
|
|
10
11
|
payload,
|
|
11
|
-
next_run_at
|
|
12
|
+
next_run_at,
|
|
13
|
+
attempts
|
|
12
14
|
) VALUES (
|
|
13
|
-
$1, $2, $3, $4, $5
|
|
15
|
+
$1, $2, $3, $4, $5, $6
|
|
14
16
|
)
|
|
15
17
|
`, [
|
|
16
18
|
jobToAdd?._id,
|
|
17
19
|
jobToAdd?.status,
|
|
18
20
|
jobToAdd?.job,
|
|
19
21
|
JSON.stringify(jobToAdd?.payload),
|
|
20
|
-
jobToAdd?.nextRunAt
|
|
22
|
+
jobToAdd?.nextRunAt,
|
|
23
|
+
0
|
|
21
24
|
]);
|
|
22
25
|
},
|
|
23
26
|
countJobs: async function(status = "") {
|
|
24
|
-
const db =
|
|
27
|
+
const db = this?.db;
|
|
25
28
|
const [jobs] = await db?.query(`
|
|
26
29
|
SELECT
|
|
27
30
|
count(*)
|
|
@@ -35,7 +38,7 @@ var queues_default = {
|
|
|
35
38
|
return Promise.resolve(jobs.count);
|
|
36
39
|
},
|
|
37
40
|
deleteJob: function(jobId = "") {
|
|
38
|
-
const db =
|
|
41
|
+
const db = this?.db;
|
|
39
42
|
return db?.query(`
|
|
40
43
|
DELETE FROM
|
|
41
44
|
queue_${this.queue.name}
|
|
@@ -45,8 +48,22 @@ var queues_default = {
|
|
|
45
48
|
jobId
|
|
46
49
|
]);
|
|
47
50
|
},
|
|
51
|
+
deleteIncompleteJobsForMachine: function() {
|
|
52
|
+
const db = this?.db;
|
|
53
|
+
return db?.query(`
|
|
54
|
+
DELETE FROM
|
|
55
|
+
queue_${this.queue.name}
|
|
56
|
+
WHERE
|
|
57
|
+
status = ANY($1)
|
|
58
|
+
AND
|
|
59
|
+
locked_by = $2
|
|
60
|
+
`, [
|
|
61
|
+
["incomplete", "running"],
|
|
62
|
+
this.machineId
|
|
63
|
+
]);
|
|
64
|
+
},
|
|
48
65
|
getJobs: function(query = {}) {
|
|
49
|
-
const db =
|
|
66
|
+
const db = this?.db;
|
|
50
67
|
return db?.query(`
|
|
51
68
|
SELECT * FROM
|
|
52
69
|
queue_${this.queue.name}
|
|
@@ -59,14 +76,14 @@ var queues_default = {
|
|
|
59
76
|
]);
|
|
60
77
|
},
|
|
61
78
|
getNextJobToRun: async function() {
|
|
62
|
-
const db =
|
|
79
|
+
const db = this?.db;
|
|
63
80
|
const [nextJob] = await db?.query(`
|
|
64
81
|
SELECT * FROM
|
|
65
82
|
queue_${this.queue.name}
|
|
66
83
|
WHERE
|
|
67
84
|
status = $1
|
|
68
85
|
AND
|
|
69
|
-
next_run_at::
|
|
86
|
+
next_run_at::timestamp <= NOW()
|
|
70
87
|
AND
|
|
71
88
|
locked_by IS NULL
|
|
72
89
|
ORDER BY
|
|
@@ -86,7 +103,7 @@ var queues_default = {
|
|
|
86
103
|
_id = $4
|
|
87
104
|
`, [
|
|
88
105
|
"running",
|
|
89
|
-
|
|
106
|
+
new Date().toISOString(),
|
|
90
107
|
this.machineId,
|
|
91
108
|
nextJob?._id
|
|
92
109
|
]);
|
|
@@ -97,7 +114,7 @@ var queues_default = {
|
|
|
97
114
|
} : {};
|
|
98
115
|
},
|
|
99
116
|
initializeDatabase: async function() {
|
|
100
|
-
const db =
|
|
117
|
+
const db = this?.db;
|
|
101
118
|
await db?.query(`
|
|
102
119
|
CREATE TABLE IF NOT EXISTS queue_${this.queue.name} (
|
|
103
120
|
_id text PRIMARY KEY,
|
|
@@ -109,51 +126,67 @@ var queues_default = {
|
|
|
109
126
|
started_at text,
|
|
110
127
|
completed_at text,
|
|
111
128
|
failed_at text,
|
|
112
|
-
error text
|
|
129
|
+
error text,
|
|
130
|
+
attempts smallint
|
|
113
131
|
)
|
|
114
132
|
`);
|
|
133
|
+
db?.query(`ALTER TABLE queue_${this.queue.name} ADD COLUMN IF NOT EXISTS attempts smallint`);
|
|
115
134
|
db?.query(`CREATE INDEX IF NOT EXISTS status_index ON queue_${this.queue.name} (status)`);
|
|
116
135
|
db?.query(`CREATE INDEX IF NOT EXISTS status_nextRunAt_index ON queue_${this.queue.name} (status, next_run_at)`);
|
|
117
136
|
db?.query(`CREATE INDEX IF NOT EXISTS completedAt_index ON queue_${this.queue.name} (completed_at)`);
|
|
118
137
|
db?.query(`CREATE INDEX IF NOT EXISTS failedAt_index ON queue_${this.queue.name} (failed_at)`);
|
|
138
|
+
if (this.queue.options?.cleanup?.completedAfterSeconds) {
|
|
139
|
+
cron.schedule("*/30 * * * * *", () => {
|
|
140
|
+
handleCleanupQueues({
|
|
141
|
+
database: db,
|
|
142
|
+
table: `queue_${this.queue.name}`,
|
|
143
|
+
seconds: this.queue.options?.cleanup?.completedAfterSeconds
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (this.queue.options?.cleanup?.failedAfterSeconds) {
|
|
148
|
+
cron.schedule("*/30 * * * * *", () => {
|
|
149
|
+
handleCleanupQueues({
|
|
150
|
+
database: db,
|
|
151
|
+
table: `queue_${this.queue.name}`,
|
|
152
|
+
seconds: this.queue.options?.cleanup?.failedAfterSeconds
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
119
156
|
},
|
|
120
|
-
|
|
121
|
-
const db =
|
|
157
|
+
logAttempt: function(jobId = "") {
|
|
158
|
+
const db = this?.db;
|
|
122
159
|
return db?.query(`
|
|
123
160
|
UPDATE
|
|
124
161
|
queue_${this.queue.name}
|
|
125
162
|
SET
|
|
126
|
-
|
|
127
|
-
next_run_at = $2,
|
|
128
|
-
locked_by = $3
|
|
163
|
+
attempts = attempts + 1
|
|
129
164
|
WHERE
|
|
130
|
-
_id = $
|
|
165
|
+
_id = $1
|
|
131
166
|
`, [
|
|
132
|
-
"pending",
|
|
133
|
-
nextRunAt,
|
|
134
|
-
null,
|
|
135
167
|
jobId
|
|
136
168
|
]);
|
|
137
169
|
},
|
|
138
|
-
|
|
139
|
-
const db =
|
|
170
|
+
requeueJob: function(jobId = "", nextRunAt = null) {
|
|
171
|
+
const db = this?.db;
|
|
140
172
|
return db?.query(`
|
|
141
173
|
UPDATE
|
|
142
174
|
queue_${this.queue.name}
|
|
143
175
|
SET
|
|
144
|
-
status = $1
|
|
145
|
-
|
|
146
|
-
status = $2
|
|
147
|
-
AND
|
|
176
|
+
status = $1,
|
|
177
|
+
next_run_at = $2,
|
|
148
178
|
locked_by = $3
|
|
179
|
+
WHERE
|
|
180
|
+
_id = $4
|
|
149
181
|
`, [
|
|
150
|
-
"
|
|
151
|
-
|
|
152
|
-
|
|
182
|
+
"pending",
|
|
183
|
+
nextRunAt,
|
|
184
|
+
null,
|
|
185
|
+
jobId
|
|
153
186
|
]);
|
|
154
187
|
},
|
|
155
188
|
setJobCompleted: function(jobId = "") {
|
|
156
|
-
const db =
|
|
189
|
+
const db = this?.db;
|
|
157
190
|
return db?.query(`
|
|
158
191
|
UPDATE
|
|
159
192
|
queue_${this.queue.name}
|
|
@@ -164,12 +197,12 @@ var queues_default = {
|
|
|
164
197
|
_id = $3
|
|
165
198
|
`, [
|
|
166
199
|
"completed",
|
|
167
|
-
|
|
200
|
+
new Date().toISOString(),
|
|
168
201
|
jobId
|
|
169
202
|
]);
|
|
170
203
|
},
|
|
171
204
|
setJobFailed: function(jobId = "", error = "") {
|
|
172
|
-
const db =
|
|
205
|
+
const db = this?.db;
|
|
173
206
|
return db?.query(`
|
|
174
207
|
UPDATE
|
|
175
208
|
queue_${this.queue.name}
|
|
@@ -181,13 +214,13 @@ var queues_default = {
|
|
|
181
214
|
_id = $4
|
|
182
215
|
`, [
|
|
183
216
|
"failed",
|
|
184
|
-
|
|
217
|
+
new Date().toISOString(),
|
|
185
218
|
error,
|
|
186
219
|
jobId
|
|
187
220
|
]);
|
|
188
221
|
},
|
|
189
222
|
setJobsForMachinePending: function() {
|
|
190
|
-
const db =
|
|
223
|
+
const db = this?.db;
|
|
191
224
|
return db?.query(`
|
|
192
225
|
UPDATE
|
|
193
226
|
queue_${this.queue.name}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import generateId from "../../../../lib/generateId";
|
|
2
|
+
var sessions_default = {
|
|
3
|
+
create_session: async (input = {}) => {
|
|
4
|
+
const session_id = generateId();
|
|
5
|
+
await process.databases._sessions?.query(`
|
|
6
|
+
INSERT INTO
|
|
7
|
+
sessions (
|
|
8
|
+
session_id,
|
|
9
|
+
csrf,
|
|
10
|
+
created_at
|
|
11
|
+
)
|
|
12
|
+
VALUES ($1, $2, $3)
|
|
13
|
+
`, [
|
|
14
|
+
session_id,
|
|
15
|
+
generateId(32),
|
|
16
|
+
new Date().toISOString()
|
|
17
|
+
]);
|
|
18
|
+
return session_id;
|
|
19
|
+
},
|
|
20
|
+
get_session: async (input = {}) => {
|
|
21
|
+
const [session] = await process.databases._sessions?.query(`
|
|
22
|
+
SELECT * FROM
|
|
23
|
+
sessions
|
|
24
|
+
WHERE
|
|
25
|
+
session_id = $1
|
|
26
|
+
`, [
|
|
27
|
+
input?.session_id
|
|
28
|
+
]);
|
|
29
|
+
return session;
|
|
30
|
+
},
|
|
31
|
+
delete_expired_sessions: async () => {
|
|
32
|
+
const date = new Date();
|
|
33
|
+
date.setHours(date.getHours() - 1);
|
|
34
|
+
return process.databases.postgresql.query(`
|
|
35
|
+
DELETE FROM sessions WHERE created_at::timestamp < $1
|
|
36
|
+
`, [
|
|
37
|
+
date.toISOString()
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
sessions_default as default
|
|
43
|
+
};
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import mongodbAccountsQueries from "./mongodb/queries/accounts";
|
|
2
2
|
import mongodbQueuesQueries from "./mongodb/queries/queues";
|
|
3
|
+
import mongodbSessionsQueries from "./mongodb/queries/sessions";
|
|
3
4
|
import postgresqlAccountsQueries from "./postgresql/queries/accounts";
|
|
4
5
|
import postgresqlQueuesQueries from "./postgresql/queries/queues";
|
|
6
|
+
import postgresqlSessionsQueries from "./postgresql/queries/sessions";
|
|
5
7
|
var queryMap_default = {
|
|
6
8
|
mongodb: {
|
|
7
9
|
accounts: mongodbAccountsQueries,
|
|
8
|
-
queues: mongodbQueuesQueries
|
|
10
|
+
queues: mongodbQueuesQueries,
|
|
11
|
+
sessions: mongodbSessionsQueries
|
|
9
12
|
},
|
|
10
13
|
postgresql: {
|
|
11
14
|
accounts: postgresqlAccountsQueries,
|
|
12
|
-
queues: postgresqlQueuesQueries
|
|
15
|
+
queues: postgresqlQueuesQueries,
|
|
16
|
+
sessions: postgresqlSessionsQueries
|
|
13
17
|
}
|
|
14
18
|
};
|
|
15
19
|
export {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import getBrowserSafeUser from "./accounts/getBrowserSafeUser";
|
|
2
|
+
import escapeKeyValuePair from "../lib/escapeKeyValuePair.js";
|
|
2
3
|
var getBrowserSafeRequest_default = (req = {}) => {
|
|
3
4
|
const browserSafeRequest = {};
|
|
4
|
-
browserSafeRequest.params = req.params;
|
|
5
|
-
browserSafeRequest.query = req.query;
|
|
5
|
+
browserSafeRequest.params = escapeKeyValuePair(req.params);
|
|
6
|
+
browserSafeRequest.query = escapeKeyValuePair(req.query);
|
|
6
7
|
browserSafeRequest.context = {
|
|
7
8
|
user: getBrowserSafeUser(req.context.user)
|
|
8
9
|
};
|