-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.js
More file actions
662 lines (615 loc) · 18.9 KB
/
Copy pathrunner.js
File metadata and controls
662 lines (615 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import { handler } from "./index.js";
import path from "path";
import fs from "fs/promises";
import { csvToJson, attachHeader } from "./scripts/shapeData/index.js";
import logger from "simple-logs-sai-node";
import { performance } from "perf_hooks";
const HEADERS = {
Origin: "www.meetsai.ca",
Referer: "www.meetsai.ca",
"X-Forwarded-For": "124.0.0.1",
Authorization: "Bearer " + process.env.AWS_API_GATEWAY_BEARER,
};
const LOG_DIR = process.env.LOG_DIR || "runner_logs";
const FAIL_DIR = process.env.FAIL_DIR || "failedUploads";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const RUN_FAILURE_LOG_TS = new Date().toISOString().replace(/[:.]/g, "-");
let failureLogState = {
path: null,
entryCount: 0,
promise: Promise.resolve(),
initialized: false,
};
function parseMaybeJson(value) {
if (typeof value !== "string") return value;
try {
return JSON.parse(value);
} catch {
return value;
}
}
function stripLeadingBom(value) {
if (typeof value !== "string") return value;
return value.replace(/^\uFEFF+/, "");
}
function sanitizeBomDeep(value) {
if (Array.isArray(value)) {
return value.map((item) => sanitizeBomDeep(item));
}
if (!value || typeof value !== "object") {
return value;
}
const proto = Object.getPrototypeOf(value);
if (proto !== Object.prototype && proto !== null) {
return value;
}
const output = {};
for (const [rawKey, rawVal] of Object.entries(value)) {
const key = stripLeadingBom(rawKey);
output[key] = sanitizeBomDeep(rawVal);
}
return output;
}
function normalizePayload(input, headersDefault) {
let headers;
let body;
const addReferer = (h) => {
if (!h) return h;
const origin = h.Origin || h.origin;
if (origin && h.Referer === undefined && h.referer === undefined) {
h.Referer = origin;
}
return h;
};
const parseHeaders = (h) => {
const parsed = parseMaybeJson(h);
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
? parsed
: undefined;
};
const parseBody = (b) => {
const parsed = parseMaybeJson(b);
return parsed ?? b;
};
if (input && typeof input === "object" && !Array.isArray(input)) {
if ("payload" in input) {
const parsedPayload = parseMaybeJson(input.payload);
if (parsedPayload && typeof parsedPayload === "object" && !Array.isArray(parsedPayload)) {
headers = parseHeaders(parsedPayload.headers);
if (parsedPayload.body !== undefined) {
body = parseBody(parsedPayload.body);
} else {
body = parsedPayload;
}
} else {
body = parseBody(parsedPayload);
}
} else if ("body" in input) {
body = parseBody(input.body);
headers = parseHeaders(input.headers);
} else {
body = input;
}
} else {
body = parseBody(input);
}
if (body === undefined || body === null) {
body = {};
}
return {
headers: sanitizeBomDeep(addReferer(headers ?? headersDefault)),
body: sanitizeBomDeep(body),
};
}
function applySubmissionSource(body, force) {
if (!force) return false;
if (!body || typeof body !== "object") return false;
if (!body._meta || typeof body._meta !== "object") {
body._meta = {};
}
body._meta.submission_source = "cli-runner";
return true;
}
function applyCommsConsent(body, force) {
if (!force) return false;
if (!body || typeof body !== "object") return false;
body.comms_consent = true;
return true;
}
function unwrapBody(body) {
if (!body || typeof body !== "object") return { body, changed: false, detail: null };
let changed = false;
let detail = null;
const maybeParse = (val) => {
if (typeof val === "string") {
try {
return JSON.parse(val);
} catch {
return val;
}
}
return val;
};
if (body.body) {
const inner = maybeParse(body.body);
if (inner && typeof inner === "object") {
body = { ...body, ...inner };
delete body.body;
changed = true;
detail = "body.body";
}
} else if (body.value || body.values) {
const innerVal = maybeParse(body.value ?? body.values);
if (innerVal && typeof innerVal === "object") {
body = { ...body, ...innerVal };
delete body.value;
delete body.values;
changed = true;
detail = body.value ? "body.value" : "body.values";
}
}
return { body, changed, detail };
}
let logDirReady = false;
let failDirReady = false;
async function ensureLogDir() {
if (logDirReady) return true;
try {
await fs.mkdir(LOG_DIR, { recursive: true });
logDirReady = true;
return true;
} catch (err) {
console.warn(`Could not create log directory ${LOG_DIR}:`, err?.message || err);
logDirReady = false;
return false;
}
}
async function ensureFailDir() {
if (failDirReady) return true;
try {
await fs.mkdir(FAIL_DIR, { recursive: true });
failDirReady = true;
return true;
} catch (err) {
console.warn(`Could not create failed-upload directory ${FAIL_DIR}:`, err?.message || err);
failDirReady = false;
return false;
}
}
async function ensureFailureLogStream() {
if (failureLogState.initialized) return true;
if (!(await ensureLogDir())) return false;
failureLogState.path = path.join(LOG_DIR, `${RUN_FAILURE_LOG_TS}_row_failures.json`);
try {
await fs.writeFile(failureLogState.path, "[\n", "utf8");
failureLogState.initialized = true;
return true;
} catch (err) {
console.warn(
`Could not initialize runner failure log ${failureLogState.path}:`,
err?.message || err
);
failureLogState.initialized = false;
return false;
}
}
function appendFailureLogEntry(entry) {
failureLogState.promise = failureLogState.promise
.then(async () => {
if (!(await ensureFailureLogStream())) return;
const prefix = failureLogState.entryCount > 0 ? ",\n" : "";
const entryText = JSON.stringify(entry, null, 2);
await fs.appendFile(failureLogState.path, prefix + entryText, "utf8");
failureLogState.entryCount += 1;
const requestId = entry?._runner_failure?.request_backup_id;
const requestIdStr = requestId ? ` request_backup_id=${requestId}` : "";
console.log(
`runner_logs failure entry ${failureLogState.entryCount} appended to ${failureLogState.path}${requestIdStr}`
);
})
.catch((err) => {
console.warn("Could not append runner failure entry:", err?.message || err);
});
return failureLogState.promise;
}
async function finalizeFailureLogStream() {
await failureLogState.promise;
if (!failureLogState.initialized) return;
try {
await fs.appendFile(failureLogState.path, "\n]\n", "utf8");
} catch (err) {
console.warn(
`Could not finalize runner failure log ${failureLogState.path}:`,
err?.message || err
);
}
if (failureLogState.entryCount > 0) {
console.log(
`${failureLogState.entryCount} runner_logs failures saved to ${failureLogState.path}`
);
}
failureLogState.initialized = false;
}
function serializeResponse(res) {
if (!res) return res;
if (typeof res === "object") {
// Handle fetch Response
if (typeof res.status === "number" && typeof res.statusText === "string") {
return {
status: res.status,
statusText: res.statusText,
statusCode: res.statusCode ?? res.status,
};
}
if (res.statusCode) return res;
}
return res;
}
async function runner(payload) {
if (process.env.PIPELINE == "gateway") {
console.log("gateway");
try {
const response = await fetch(process.env.AWS_API_GATEWAY_ENDPOINT, {
method: "POST",
headers: payload.headers,
body: JSON.stringify(payload.body),
});
let data;
try {
data = await response.clone().json();
} catch {
try {
data = await response.clone().text();
} catch {
data = null;
}
}
console.log(
"response",
response.status,
response.status < 300 ? "" : JSON.stringify(data, null, 2)
);
const headers = {};
try {
response.headers.forEach((v, k) => {
headers[k] = v;
});
} catch {
// ignore
}
return { statusCode: response.status, body: data, headers };
} catch (error) {
console.log(error);
console.log(JSON.stringify(payload));
}
} else if (process.env.PIPELINE == "local") {
console.log("local");
return await handler(payload);
}
}
async function logEvent(data, response) {
if (!response?.statusCode || response.statusCode <= 300) return null;
logger.log(response.statusCode);
logger.log("logEvent", data, response);
const failure = {
...data,
_runner_failure: {
statusCode: response.statusCode,
response: serializeResponse(response),
request_backup_id: response.body?.request_backup_id,
},
};
return failure;
}
async function runOverArray(dataArray, callback, options = {}) {
const {
forceSubmissionSource,
forceCommsConsent,
unwrapBodies,
dryRun,
logPayload,
failures,
concurrency = 1,
stats,
} = options;
if (process.env.SLOW === "true") {
console.log("Estimated execution time: ", 2 * dataArray.length);
}
let nextIndex = 0;
const processRow = async (dataIndex) => {
const item = dataArray[dataIndex];
if (Array.isArray(item)) {
console.log("File", dataIndex);
await runOverArray(item, callback, options);
return;
}
const reqStart = performance.now();
const recordRowDuration = () => {
const reqDuration = performance.now() - reqStart;
if (stats) {
stats.totalRowDuration += reqDuration;
stats.rowsProcessed += 1;
}
console.log(`row ${dataIndex}: duration ${reqDuration.toFixed(2)}ms`);
};
const normalized = normalizePayload(item, HEADERS);
if (unwrapBodies) {
const { body, changed, detail } = unwrapBody(normalized.body);
normalized.body = body;
if (changed) {
console.log(`unwrapped nested ${detail} for payload`, dataIndex);
}
}
if (applySubmissionSource(normalized.body, forceSubmissionSource)) {
console.log("submission_source set to cli-runner for payload", dataIndex);
}
if (applyCommsConsent(normalized.body, forceCommsConsent)) {
console.log("comms_consent set to true for payload", dataIndex);
}
const event = normalized.headers ? normalized : attachHeader(normalized, HEADERS);
const summary = event.body?.email || event.body?.phone || event.body?.firstname || event.body?.id || "";
console.log(`row ${dataIndex}: ${dryRun ? "dry-run" : "sending"} ${summary}`);
if (dryRun) {
console.log("[dry-run] event:", JSON.stringify(event, null, 2));
recordRowDuration();
} else {
if (logPayload) {
console.log("[payload] event:", JSON.stringify(event, null, 2));
}
try {
const response = await callback(event);
const statusCode = response?.statusCode ?? response?.status;
console.log(`row ${dataIndex}: status ${statusCode}`);
const failure = await logEvent(event, response);
if (failure && failures) {
failures.push(failure);
appendFailureLogEntry(failure);
}
recordRowDuration();
} catch (error) {
console.error(error);
const failure = await logEvent(event, {
statusCode: 500,
body: "{message: failure on runner.js side}",
});
if (failure && failures) {
failures.push(failure);
appendFailureLogEntry(failure);
}
recordRowDuration();
}
}
if (process.env.SLOW === "true") {
console.log("waiting...");
await sleep(1500);
console.log("...starting");
}
};
const worker = async () => {
while (nextIndex < dataArray.length) {
const currentIndex = nextIndex++;
await processRow(currentIndex);
}
};
const poolSize = Math.max(1, Math.floor(concurrency));
const workers = Array.from({ length: Math.min(poolSize, dataArray.length || 1) }, () => worker());
await Promise.all(workers);
}
async function parseDir(pathLike) {
try {
const stats = await fs.stat(pathLike);
if (stats.isFile()) {
const ext = path.extname(pathLike).toLowerCase();
if (ext === ".csv") return await csvToJson(pathLike);
if (ext === ".json") {
const content = await fs.readFile(pathLike, "utf8");
return [JSON.parse(content)];
}
return [];
}
// if it's a directory
const files = await fs.readdir(pathLike);
let allData = [];
for (const file of files) {
const filePath = path.join(pathLike, file);
const fileData = await parseDir(filePath); // recursive
allData.push(...fileData);
}
return allData;
} catch (err) {
console.error("Error:", err);
return [];
}
}
async function main() {
const start = performance.now();
if (!process.env.PIPELINE) {
process.env.PIPELINE = "local";
}
const argv = process.argv.slice(2);
let forceSubmissionSource = true;
let forceCommsConsent = false;
let unwrapBodies = false;
let dryRun = false;
let logPayload = false;
let concurrency = 1;
const inputPaths = [];
const failures = [];
const stats = { totalRowDuration: 0, rowsProcessed: 0 };
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg.startsWith("--")) {
const [flag, inlineVal] = arg.slice(2).split("=");
const nextVal = argv[i + 1] && !argv[i + 1].startsWith("-") ? argv[i + 1] : undefined;
const getVal = () => {
if (inlineVal !== undefined && inlineVal !== "") return inlineVal;
if (nextVal !== undefined) {
i += 1;
return nextVal;
}
return undefined;
};
switch (flag) {
case "local":
process.env.PIPELINE = "local";
break;
case "gateway":
process.env.PIPELINE = "gateway";
break;
case "slow":
process.env.SLOW = "true";
break;
case "keep-source_submission":
forceSubmissionSource = false;
break;
case "force-comms-consent":
forceCommsConsent = true;
break;
case "unwrap-body":
unwrapBodies = true;
break;
case "dry-run":
dryRun = true;
break;
case "log-payload":
logPayload = true;
break;
case "concurrency": {
const val = getVal();
const parsed = Number.parseInt(val, 10);
if (Number.isFinite(parsed) && parsed >= 1) {
concurrency = parsed;
} else {
console.warn(`Ignoring invalid concurrency value: ${val}`);
}
break;
}
default:
break;
}
} else if (arg.startsWith("-")) {
if (arg.startsWith("-p")) {
const valPart = arg.slice(2);
let val = valPart;
if (!val) {
const nextVal = argv[i + 1] && !argv[i + 1].startsWith("-") ? argv[i + 1] : undefined;
if (nextVal !== undefined) {
val = nextVal;
i += 1;
}
}
const parsed = Number.parseInt(val, 10);
if (Number.isFinite(parsed) && parsed >= 1) {
concurrency = parsed;
} else {
console.warn(`Ignoring invalid concurrency value: ${val}`);
}
} else {
const flags = arg.slice(1);
for (const flag of flags) {
switch (flag) {
case "l":
process.env.PIPELINE = "local";
break;
case "g":
process.env.PIPELINE = "gateway";
break;
case "s":
process.env.SLOW = "true";
break;
case "k":
forceSubmissionSource = false;
break;
case "c":
forceCommsConsent = true;
break;
case "u":
unwrapBodies = true;
break;
case "d":
dryRun = true;
break;
case "v":
logPayload = true;
break;
default:
break;
}
}
}
} else {
inputPaths.push(arg);
}
}
await ensureLogDir();
await ensureFailDir();
if (forceSubmissionSource) {
console.log(
'submission_source override is ON (cli-runner). Use --keep-source_submission / -k to keep existing values.'
);
} else {
console.log(
'submission_source override is OFF (flag --keep-source_submission / -k). Existing values will be preserved.'
);
}
if (forceCommsConsent) {
console.log(
'comms_consent forcing is ON (--force-comms-consent / -c). Payload body will set comms_consent=true.'
);
} else {
console.log('comms_consent forcing is OFF by default. Use --force-comms-consent / -c to enable.');
}
if (concurrency > 1) {
console.log(`concurrency is ${concurrency} (--concurrency / -p). Up to ${concurrency} rows in flight.`);
}
if (unwrapBodies) {
console.log('unwrap-body is ON (--unwrap-body / -u). Nested body/body.value/body.values will be merged.');
} else {
console.log('unwrap-body is OFF by default. Use --unwrap-body / -u to enable nested body fixes.');
}
if (dryRun) {
console.log('dry-run is ON (--dry-run / -d). Payloads will be logged but not sent.');
}
if (logPayload) {
console.log('log-payload is ON (--log-payload / -v). Payloads will be logged during send.');
}
for (const pathArg of inputPaths) {
const dataArray = await parseDir(pathArg);
await runOverArray(dataArray, runner, {
forceSubmissionSource,
forceCommsConsent,
unwrapBodies,
dryRun,
logPayload,
failures,
concurrency,
stats,
});
}
if (failures.length) {
const ts = new Date().toISOString().replace(/[:.]/g, "-");
const filename = `${ts}_failures.json`;
const filePath = path.join(FAIL_DIR, filename);
try {
await fs.writeFile(filePath, JSON.stringify(failures, null, 2), "utf8");
console.log(`${failures.length} failed uploads saved to ${filePath}`);
console.log(`Re-run failed items with: node runner.js ${filePath}`);
} catch (err) {
console.warn(`Could not write failed uploads file ${filePath}:`, err?.message || err);
}
}
await finalizeFailureLogStream();
const scriptDuration = performance.now() - start;
const summedRowDuration = stats.totalRowDuration;
const rowsProcessed = stats.rowsProcessed;
console.log("duration", `${scriptDuration.toFixed(2)}ms`);
console.log("summed row duration", `${summedRowDuration.toFixed(2)}ms`);
if (rowsProcessed > 0) {
console.log("avg row duration (sum/rows)", `${(summedRowDuration / rowsProcessed).toFixed(2)}ms`);
console.log("script duration per row (script/rows)", `${(scriptDuration / rowsProcessed).toFixed(2)}ms`);
} else {
console.log("avg row duration (sum/rows)", "n/a");
console.log("script duration per row (script/rows)", "n/a");
}
}
main();