triangle-utils 1.0.3 → 1.0.5
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/dynamodb.js +27 -27
- package/email.js +20 -0
- package/index.js +26 -6
- package/misc.js +98 -0
- package/package.json +1 -1
package/dynamodb.js
CHANGED
|
@@ -54,7 +54,7 @@ async function paginate(request, f) {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
export async function dynamodb_scan(
|
|
57
|
+
export async function dynamodb_scan(clients, table, attribute_names = [], filters = {}) {
|
|
58
58
|
const request = Object.fromEntries(Object.entries({
|
|
59
59
|
TableName : table,
|
|
60
60
|
ExpressionAttributeNames : Object.fromEntries((Object.keys(filters).concat(attribute_names)).map(attribute_name => ["#" + attribute_name, attribute_name])),
|
|
@@ -62,11 +62,11 @@ export async function dynamodb_scan(dynamodb, table, attribute_names = [], filte
|
|
|
62
62
|
FilterExpression : Object.keys(filters).map(attribute_name => "#" + attribute_name + " = :" + attribute_name).join(","),
|
|
63
63
|
ProjectionExpression : attribute_names.map(attribute_name => "#" + attribute_name).join(", ")
|
|
64
64
|
}).filter(([field, value]) => value !== undefined && Object.keys(value).length !== 0))
|
|
65
|
-
return await paginate(request, (request) => dynamodb.scan(request))
|
|
65
|
+
return await paginate(request, (request) => clients.dynamodb.scan(request))
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
export async function dynamodb_get(
|
|
69
|
-
const item = await dynamodb.getItem({
|
|
68
|
+
export async function dynamodb_get(clients, table, key, consistent=false) {
|
|
69
|
+
const item = await clients.dynamodb.getItem({
|
|
70
70
|
ConsistentRead : consistent,
|
|
71
71
|
TableName : table,
|
|
72
72
|
Key : convert_input(key).M
|
|
@@ -78,7 +78,7 @@ export async function dynamodb_get(dynamodb, table, key, consistent=false) {
|
|
|
78
78
|
return convert_output({ M : item })
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
export async function dynamodb_get_max(
|
|
81
|
+
export async function dynamodb_get_max(clients, table, primary_key) {
|
|
82
82
|
if (Object.keys(primary_key).length !== 1) {
|
|
83
83
|
return undefined
|
|
84
84
|
}
|
|
@@ -94,7 +94,7 @@ export async function dynamodb_get_max(dynamodb, table, primary_key) {
|
|
|
94
94
|
Limit : 1,
|
|
95
95
|
ScanIndexForward : false
|
|
96
96
|
}
|
|
97
|
-
const items = await dynamodb.query(request)
|
|
97
|
+
const items = await clients.dynamodb.query(request)
|
|
98
98
|
.then(response => response.Items)
|
|
99
99
|
if (items[0] === undefined) {
|
|
100
100
|
return undefined
|
|
@@ -104,7 +104,7 @@ export async function dynamodb_get_max(dynamodb, table, primary_key) {
|
|
|
104
104
|
|
|
105
105
|
|
|
106
106
|
|
|
107
|
-
export async function dynamodb_query(
|
|
107
|
+
export async function dynamodb_query(clients, table, primary_key, reverse=false) {
|
|
108
108
|
if (Object.keys(primary_key).length !== 1) {
|
|
109
109
|
return undefined
|
|
110
110
|
}
|
|
@@ -119,10 +119,10 @@ export async function dynamodb_query(dynamodb, table, primary_key, reverse=false
|
|
|
119
119
|
KeyConditionExpression: "#a = :a",
|
|
120
120
|
ScanIndexForward : !reverse
|
|
121
121
|
}
|
|
122
|
-
return await paginate(request, (request) => dynamodb.query(request))
|
|
122
|
+
return await paginate(request, (request) => clients.dynamodb.query(request))
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
export async function dynamodb_query_prefix(
|
|
125
|
+
export async function dynamodb_query_prefix(clients, table, primary_key, secondary_key_prefix, reverse=false) {
|
|
126
126
|
if (Object.keys(primary_key).length !== 1 || Object.keys(secondary_key_prefix).length !== 1) {
|
|
127
127
|
return undefined
|
|
128
128
|
}
|
|
@@ -139,10 +139,10 @@ export async function dynamodb_query_prefix(dynamodb, table, primary_key, second
|
|
|
139
139
|
KeyConditionExpression: "#a = :a AND begins_with(#b, :b)",
|
|
140
140
|
ScanIndexForward : !reverse
|
|
141
141
|
}
|
|
142
|
-
return await paginate(request, (request) => dynamodb.query(request))
|
|
142
|
+
return await paginate(request, (request) => clients.dynamodb.query(request))
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
export async function dynamodb_query_range(
|
|
145
|
+
export async function dynamodb_query_range(clients, table, primary_key, secondary_key_range, reverse=false) {
|
|
146
146
|
if (Object.keys(primary_key).length !== 1 || Object.keys(secondary_key_range).length !== 1 || Object.values(secondary_key_range)[0].length !== 2) {
|
|
147
147
|
return undefined
|
|
148
148
|
}
|
|
@@ -160,11 +160,11 @@ export async function dynamodb_query_range(dynamodb, table, primary_key, seconda
|
|
|
160
160
|
KeyConditionExpression: "#a = :a AND (#b BETWEEN :b1 AND :b2)",
|
|
161
161
|
ScanIndexForward : !reverse
|
|
162
162
|
}
|
|
163
|
-
return await paginate(request, (request) => dynamodb.query(request))
|
|
163
|
+
return await paginate(request, (request) => clients.dynamodb.query(request))
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
-
export async function dynamodb_set(
|
|
167
|
-
return await dynamodb.updateItem({
|
|
166
|
+
export async function dynamodb_set(clients, table, key, attributes) {
|
|
167
|
+
return await clients.dynamodb.updateItem({
|
|
168
168
|
TableName : table,
|
|
169
169
|
Key : convert_input(key).M,
|
|
170
170
|
UpdateExpression: "set " + Object.keys(attributes)
|
|
@@ -185,8 +185,8 @@ export async function dynamodb_set(dynamodb, table, key, attributes) {
|
|
|
185
185
|
})
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
export async function dynamodb_append(
|
|
189
|
-
return await dynamodb.updateItem({
|
|
188
|
+
export async function dynamodb_append(clients, table, key, attributes) {
|
|
189
|
+
return await clients.dynamodb.updateItem({
|
|
190
190
|
TableName : table,
|
|
191
191
|
Key : convert_input(key).M,
|
|
192
192
|
UpdateExpression: "set " + Object.keys(attributes)
|
|
@@ -203,7 +203,7 @@ export async function dynamodb_append(dynamodb, table, key, attributes) {
|
|
|
203
203
|
})
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
export async function dynamodb_add(
|
|
206
|
+
export async function dynamodb_add(clients, table, key, attributes) {
|
|
207
207
|
const item = await get(table, key, true)
|
|
208
208
|
const new_attributes = {}
|
|
209
209
|
for (const [attribute, values] of Object.entries(attributes)) {
|
|
@@ -221,8 +221,8 @@ export async function dynamodb_add(dynamodb, table, key, attributes) {
|
|
|
221
221
|
return await append(table, key, attributes)
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
-
export async function dynamodb_remove(
|
|
225
|
-
return await dynamodb.updateItem({
|
|
224
|
+
export async function dynamodb_remove(clients, table, key, attributes) {
|
|
225
|
+
return await clients.dynamodb.updateItem({
|
|
226
226
|
TableName : table,
|
|
227
227
|
Key : convert_input(key).M,
|
|
228
228
|
UpdateExpression: "remove " + attributes
|
|
@@ -233,26 +233,26 @@ export async function dynamodb_remove(dynamodb, table, key, attributes) {
|
|
|
233
233
|
})
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
export async function dynamodb_create(
|
|
236
|
+
export async function dynamodb_create(clients, table, key, attributes = {}) {
|
|
237
237
|
const item = await get(table, key)
|
|
238
238
|
if (item !== undefined) {
|
|
239
239
|
return undefined
|
|
240
240
|
}
|
|
241
|
-
return await dynamodb.putItem({
|
|
241
|
+
return await clients.dynamodb.putItem({
|
|
242
242
|
TableName : table,
|
|
243
243
|
Item : { ...convert_input(key).M, ...convert_input(attributes).M }
|
|
244
244
|
})
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
-
export async function dynamodb_delete(
|
|
248
|
-
return await dynamodb.deleteItem({
|
|
247
|
+
export async function dynamodb_delete(clients, table, key) {
|
|
248
|
+
return await clients.dynamodb.deleteItem({
|
|
249
249
|
TableName: table,
|
|
250
250
|
Key: Object.fromEntries(Object.keys(key).map(key_name => [key_name, convert_input(key[key_name])]))
|
|
251
251
|
})
|
|
252
252
|
}
|
|
253
253
|
|
|
254
|
-
export async function dynamodb_duplicate_attribute(
|
|
255
|
-
const table_key_names = await dynamodb.describeTable({
|
|
254
|
+
export async function dynamodb_duplicate_attribute(clients, table, attribute_name, new_attribute_name) {
|
|
255
|
+
const table_key_names = await clients.dynamodb.describeTable({
|
|
256
256
|
TableName : table
|
|
257
257
|
}).then(metadata => metadata.Table.KeySchema.map(key => key.AttributeName))
|
|
258
258
|
const items = await scan(table, table_key_names.concat([attribute_name, new_attribute_name]))
|
|
@@ -269,8 +269,8 @@ export async function dynamodb_duplicate_attribute(dynamodb, table, attribute_na
|
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
-
export async function dynamodb_remove_attribute(
|
|
273
|
-
const table_key_names = await dynamodb.describeTable({
|
|
272
|
+
export async function dynamodb_remove_attribute(clients, table, attribute_name) {
|
|
273
|
+
const table_key_names = await clients.dynamodb.describeTable({
|
|
274
274
|
TableName : table
|
|
275
275
|
}).then(metadata => metadata.Table.KeySchema.map(key => key.AttributeName))
|
|
276
276
|
const items = await scan(table)
|
package/email.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export async function send_email(recipient, subject, text) {
|
|
2
|
+
for (let i = 0; i < 3; i++) {
|
|
3
|
+
try {
|
|
4
|
+
await transporter.sendMail({
|
|
5
|
+
from: config.alerts_email,
|
|
6
|
+
to: recipient,
|
|
7
|
+
subject: subject,
|
|
8
|
+
text: text
|
|
9
|
+
})
|
|
10
|
+
return
|
|
11
|
+
} catch (error) {
|
|
12
|
+
console.log("EMAIL ERROR", error)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function admin_alert(text) {
|
|
18
|
+
console.log("ADMIN ALERT:", text)
|
|
19
|
+
await send_email(config.google_email, "ADMIN ALERT", text)
|
|
20
|
+
}
|
package/index.js
CHANGED
|
@@ -1,12 +1,32 @@
|
|
|
1
|
-
import * as utils_dynamodb from "./dynamodb.js"
|
|
2
1
|
|
|
2
|
+
import nodemailer from "nodemailer"
|
|
3
3
|
import { DynamoDB } from "@aws-sdk/client-dynamodb"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import * as utils_dynamodb from "./dynamodb.js"
|
|
6
|
+
import * as utils_misc from "./misc.js"
|
|
7
|
+
|
|
8
|
+
export default function get_utils(config) {
|
|
9
|
+
const clients = {}
|
|
10
|
+
if (config.region !== undefined) {
|
|
11
|
+
clients.dynamodb = new DynamoDB({ region : config.region })
|
|
12
|
+
}
|
|
13
|
+
if (config.google_email !== undefined && config.google_app_password) {
|
|
14
|
+
clients.transporter = nodemailer.createTransport({
|
|
15
|
+
service: "Gmail",
|
|
16
|
+
host: "smtp.gmail.com",
|
|
17
|
+
port: 465,
|
|
18
|
+
secure: true,
|
|
19
|
+
auth: {
|
|
20
|
+
user: config.google_email,
|
|
21
|
+
pass: config.google_app_password,
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
}
|
|
6
25
|
const utils = {}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
26
|
+
for (const utils_list of [utils_misc, utils_dynamodb]) {
|
|
27
|
+
for (const [util_name, util] of Object.entries(utils_list)) {
|
|
28
|
+
utils[util_name] = async (...args) => await util(clients, ...args)
|
|
29
|
+
}
|
|
10
30
|
}
|
|
11
31
|
return utils
|
|
12
|
-
}
|
|
32
|
+
}
|
package/misc.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import config from "../config.js"
|
|
2
|
+
import election_ids from "./election_ids.js"
|
|
3
|
+
import nodemailer from "nodemailer"
|
|
4
|
+
import crypto from "crypto"
|
|
5
|
+
import * as dynamo from "./dynamodb.js"
|
|
6
|
+
|
|
7
|
+
export async function wait(client, duration) {
|
|
8
|
+
return new Promise(resolve => setTimeout(resolve, duration))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function get_current_time(client, milliseconds = false) {
|
|
12
|
+
if (milliseconds) {
|
|
13
|
+
return (new Date()).getTime()
|
|
14
|
+
}
|
|
15
|
+
return (new Date()).toISOString()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function send_email(recipient, subject, text) {
|
|
19
|
+
for (let i = 0; i < 3; i++) {
|
|
20
|
+
try {
|
|
21
|
+
await transporter.sendMail({
|
|
22
|
+
from: config.alerts_email,
|
|
23
|
+
to: recipient,
|
|
24
|
+
subject: subject,
|
|
25
|
+
text: text
|
|
26
|
+
})
|
|
27
|
+
return
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.log("EMAIL ERROR", error)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function admin_alert(text) {
|
|
35
|
+
console.log("ADMIN ALERT:", text)
|
|
36
|
+
await send_email(config.google_email, "ADMIN ALERT", text)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function safe_run(clients, f) {
|
|
40
|
+
try {
|
|
41
|
+
await f()
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error.stack !== undefined) {
|
|
44
|
+
await admin_alert(error.stack.toString())
|
|
45
|
+
} else {
|
|
46
|
+
await admin_alert(error.toString())
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function iterate(clients, inputs, f, concurrency = 1, print_indices = false) {
|
|
53
|
+
let index = 0
|
|
54
|
+
let iterators = []
|
|
55
|
+
for (let i = 0; i < concurrency; i++) {
|
|
56
|
+
iterators.push((async () => {
|
|
57
|
+
while (index < inputs.length) {
|
|
58
|
+
index += 1
|
|
59
|
+
if (print_indices) {
|
|
60
|
+
console.log(i + ":" + (index - 1) + "/" + inputs.length)
|
|
61
|
+
}
|
|
62
|
+
await safe_run(clients, () => f(inputs[index - 1]))
|
|
63
|
+
}
|
|
64
|
+
})())
|
|
65
|
+
}
|
|
66
|
+
await Promise.all(iterators)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const text_encoder = new TextEncoder()
|
|
70
|
+
|
|
71
|
+
export async function sha256(clients, input) {
|
|
72
|
+
const input_buffer = text_encoder.encode(input);
|
|
73
|
+
|
|
74
|
+
const hash_buffer = await crypto.subtle.digest("SHA-256", input_buffer)
|
|
75
|
+
|
|
76
|
+
const hash_array = Array.from(new Uint8Array(hash_buffer))
|
|
77
|
+
|
|
78
|
+
const hash = hash_array.map(b => b.toString(16).padStart(2, "0")).join("")
|
|
79
|
+
return hash;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function get_election_id(clients, year, office, state, district) {
|
|
83
|
+
if (year === undefined || office === undefined || state === undefined || office === "P") {
|
|
84
|
+
return undefined
|
|
85
|
+
}
|
|
86
|
+
if (office === "H" && (district === undefined || district.length !== 2)) {
|
|
87
|
+
return undefined
|
|
88
|
+
}
|
|
89
|
+
if (year.length !== 4 || office.length !== 1 || state.length !== 2) {
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
const office_name = { "H" : "house", "S" : "sen", "P" : "pres" }[office]
|
|
93
|
+
const election_id = year + "-" + office_name + "-" + state.toLowerCase() + (office === "H" ? ("-" + district.toLowerCase()) : "") + "-election"
|
|
94
|
+
if (!election_ids.has(election_id)) {
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
return election_id
|
|
98
|
+
}
|