Skip to content

Commit 387156f

Browse files
committed
db-tabulator: rename js preprocess to postprocess for clarity
1 parent d274868 commit 387156f

6 files changed

Lines changed: 37 additions & 37 deletions

File tree

db-tabulator/app.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {NS_CATEGORY, NS_FILE, NS_MAIN} from "../namespaces";
66
import {formatSummary} from "../reports/commons";
77
import {MetadataStore} from "./MetadataStore";
88
import {HybridMetadataStore} from "./HybridMetadataStore";
9-
import {applyJsPreprocessing, processQueriesExternally} from "./preprocess";
9+
import {applyJsPostProcessing, processQueriesExternally} from "./postprocess";
1010
import {EventEmitter} from "events";
1111

1212
export const BOT_NAME = 'SDZeroBot';
@@ -43,7 +43,7 @@ export function getQueriesFromText(text: string, title: string): Query[] {
4343
return [];
4444
}
4545
return templates.map((template, idx) =>
46-
new Query(template, title, idx + 1, !!template.getValue('preprocess_js')?.trim()));
46+
new Query(template, title, idx + 1, !!template.getValue('postprocess_js')?.trim()));
4747
}
4848

4949
export async function processQueries(allQueries: Record<string, Query[]>, notifier?: EventEmitter) {
@@ -134,7 +134,7 @@ export class Query extends EventEmitter {
134134
/** Internal tracking: for edit summary */
135135
endNotFound = false;
136136

137-
/** Internal tracking: for queries with JS preprocessing enabled */
137+
/** Internal tracking: for queries with JS postprocessing enabled */
138138
needsExternalRun = false;
139139
needsForceKill = false;
140140

@@ -397,12 +397,12 @@ export class Query extends EventEmitter {
397397
return String(value);
398398
});
399399
}
400-
if (this.getTemplateValue('preprocess_js')) {
401-
const jsCode = stripOuterNowikis(this.getTemplateValue('preprocess_js'));
400+
if (this.getTemplateValue('postprocess_js')) {
401+
const jsCode = stripOuterNowikis(this.getTemplateValue('postprocess_js'));
402402
try {
403-
result = await applyJsPreprocessing(result, jsCode, this);
403+
result = await applyJsPostProcessing(result, jsCode, this);
404404
} catch (e) {
405-
log(`[E] Error in applyJsPreprocessing`);
405+
log(`[E] Error in applyJsPostProcessing`);
406406
log(e);
407407
}
408408
}

db-tabulator/database-report.hbs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
// From Query class:
5353
'query-executing': data => `Query (<code>${shorten(data.args[0], 80)}</code>) submitted to database.`,
5454
'query-executed': data => `Query finished running in ${data.args[0]} seconds.`,
55-
'preprocessing': _ => `Started JS preprocessing on query result.`,
55+
'postprocessing': _ => `Started JS postprocessing on query result.`,
5656
'js-logging': data => `Logging output: <pre>${safeStringify(data.args[0])}</pre>`,
57-
'js-no-array': _ => error(`JS preprocess() must return an array. `) + 'Saving result without preprocessing.',
58-
'js-invalid-return': _ => error(`JS preprocess() returned a value which is not transferable. `) +
59-
'Saving result without preprocessing.',
60-
'js-failed': data => error(`JS preprocessing failed. `) + `Error: ${data.args[0]}. Saving result without preprocessing.`,
57+
'js-no-array': _ => error(`JS postprocess() must return an array. `) + 'Saving result without postprocessing.',
58+
'js-invalid-return': _ => error(`JS postprocess() returned a value which is not transferable. `) +
59+
'Saving result without postprocessing.',
60+
'js-failed': data => error(`JS postprocessing failed. `) + `Error: ${data.args[0]}. Saving result without postprocessing.`,
6161
'process-timed-out': _ => error(`Child process timed out`),
62-
'preprocessing-complete': data => `Finished JS preprocessing on query result in ${data.args[0]} seconds.`,
62+
'postprocessing-complete': data => `Finished JS postprocessing on query result in ${data.args[0]} seconds.`,
6363
'catastrophic-error': _ => error(`Your custom JS code was force-terminated due to excessive memory or time usage.`),
6464
'saving': data => `Saving ${link(data.args[0])}.`,
6565
'end-not-found': _ => `[WARNING]: No {` + `{database report end}} template was found. Overwriting rest of the page.`,

db-tabulator/external-update.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {metadataStore, fetchQueriesForPage, processQueriesForPage} from "./app";
33

44
/**
55
* Entry point invoked in a child Node.js process for queries
6-
* with custom JS preprocessing enabled.
6+
* with custom JS postprocessing enabled.
77
*/
88
(async function () {
99

db-tabulator/isolate.vm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-unused-vars */
2-
/* global __mwApiGet, __rawReq, __dbQueryResult, preprocess */
2+
/* global __mwApiGet, __rawReq, __dbQueryResult, postprocess */
33
(async function() {
44
const bot = {
55
async request(url) {
@@ -16,5 +16,5 @@
1616

1717
"${JS_CODE}";
1818

19-
return JSON.stringify(await preprocess(JSON.parse(__dbQueryResult)));
19+
return JSON.stringify(await postprocess(JSON.parse(__dbQueryResult)));
2020
})
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const apiClient = new Mwn({
7575
});
7676
apiClient.setRequestOptions({ timeout: 10000 });
7777

78-
const preprocessCodeTemplate = fs.readFileSync(__dirname + '/isolate.vm.js')
78+
const postprocessCodeTemplate = fs.readFileSync(__dirname + '/isolate.vm.js')
7979
.toString()
8080
.replace(/^\/\*.*?\*\/$/m, ''); // remove linter comments /* ... */
8181

@@ -130,9 +130,9 @@ async function makeSandboxedHttpRequest(url: string) {
130130
}
131131
}
132132

133-
export async function applyJsPreprocessing(rows: Record<string, string>[], jsCode: string, query: Query): Promise<Record<string, any>[]> {
134-
log(`[+] Applying JS preprocessing for ${query}`);
135-
query.emit('preprocessing');
133+
export async function applyJsPostProcessing(rows: Record<string, string>[], jsCode: string, query: Query): Promise<Record<string, any>[]> {
134+
log(`[+] Applying JS postprocessing for ${query}`);
135+
query.emit('postprocessing');
136136
let startTime = process.hrtime.bigint();
137137

138138
// Import dynamically as this has native dependencies
@@ -172,10 +172,10 @@ export async function applyJsPreprocessing(rows: Record<string, string>[], jsCod
172172

173173
let result = rows;
174174

175-
let doPreprocessing = async () => {
175+
let doPostProcessing = async () => {
176176
try {
177-
// jsCode is expected to declare function preprocess(rows) {...}
178-
let fullCode = preprocessCodeTemplate.replace('"${JS_CODE}"', jsCode);
177+
// jsCode is expected to declare function postprocess(rows) {...}
178+
let fullCode = postprocessCodeTemplate.replace('"${JS_CODE}"', jsCode);
179179
let wrapped = await context.eval(fullCode, {
180180
reference: true,
181181
timeout: softTimeout
@@ -190,29 +190,29 @@ export async function applyJsPreprocessing(rows: Record<string, string>[], jsCod
190190
if (Array.isArray(userCodeResultParsed)) {
191191
result = userCodeResultParsed;
192192
} else {
193-
log(`[E] JS preprocessing for ${query} returned a non-array: ${userCodeResult.slice(0, 100)} ... Ignoring.`);
194-
query.warnings.push(`JS preprocessing didn't return an array of rows, will be ignored`);
193+
log(`[E] JS postprocessing for ${query} returned a non-array: ${userCodeResult.slice(0, 100)} ... Ignoring.`);
194+
query.warnings.push(`JS postprocessing didn't return an array of rows, will be ignored`);
195195
query.emit('js-no-array');
196196
}
197197
} else {
198-
log(`[E] JS preprocessing for ${query} has an invalid return value: ${userCodeResult}. Ignoring.`);
199-
query.warnings.push(`JS preprocessing must have a transferable return value`);
198+
log(`[E] JS postprocessing for ${query} has an invalid return value: ${userCodeResult}. Ignoring.`);
199+
query.warnings.push(`JS postprocessing must have a transferable return value`);
200200
query.emit('js-invalid-return');
201201
}
202202
} catch (e) { // Shouldn't occur as we are the ones doing the JSON.stringify
203-
log(`[E] JS preprocessing for ${query} returned a non-JSON: ${userCodeResult.slice(0, 100)}. Ignoring.`);
203+
log(`[E] JS postprocessing for ${query} returned a non-JSON: ${userCodeResult.slice(0, 100)}. Ignoring.`);
204204
}
205205
} catch (e) {
206-
log(`[E] JS preprocessing for ${query} failed: ${e.toString()}`);
206+
log(`[E] JS postprocessing for ${query} failed: ${e.toString()}`);
207207
log(e);
208-
query.warnings.push(`JS preprocessing failed: ${e.toString()}`);
208+
query.warnings.push(`JS postprocessing failed: ${e.toString()}`);
209209
query.emit('js-failed', e.toString());
210210
}
211211
}
212212

213213
await timedPromise(
214214
hardTimeout,
215-
doPreprocessing(),
215+
doPostProcessing(),
216216
() => {
217217
// In case isolated-vm timeout doesn't work
218218
log(`[E] Past ${hardTimeout/1000} second timeout, force-disposing isolate`);
@@ -222,8 +222,8 @@ export async function applyJsPreprocessing(rows: Record<string, string>[], jsCod
222222

223223
let endTime = process.hrtime.bigint();
224224
let timeTaken = (Number(endTime - startTime) / 1e9).toFixed(3);
225-
log(`[+] JS preprocessing for ${query} took ${timeTaken} seconds, cpuTime: ${isolate.cpuTime}, wallTime: ${isolate.wallTime}.`);
226-
query.emit('preprocessing-complete', timeTaken);
225+
log(`[+] JS postprocessing for ${query} took ${timeTaken} seconds, cpuTime: ${isolate.cpuTime}, wallTime: ${isolate.wallTime}.`);
226+
query.emit('postprocessing-complete', timeTaken);
227227

228228
return result;
229229
}

db-tabulator/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import assert = require("assert");
44
import {NoMetadataStore} from "./NoMetadataStore";
55
import {Template} from "../../mwn/build/wikitext";
66
import {MwnDate} from "../../mwn";
7-
import {applyJsPreprocessing} from "./preprocess";
7+
import {applyJsPostProcessing} from "./postprocess";
88

99
describe('db-tabulator', () => {
1010

@@ -28,10 +28,10 @@ describe('db-tabulator', () => {
2828
assert.strictEqual(isUpdateDue(new bot.date().subtract(40, 'hour'), 2), true);
2929
});
3030

31-
it('applyJsPreprocessing', async () => {
32-
console.log(await applyJsPreprocessing(
31+
it('applyJsPostProcessing', async () => {
32+
console.log(await applyJsPostProcessing(
3333
[{id: '1', name: 'Main Page'}, {id: '2', name: "Talk:Main Page"}],
34-
`function preprocess(rows) {
34+
`function postprocess(rows) {
3535
rows.forEach(row => {
3636
row.id = parseInt(row.id) + 100;
3737
})

0 commit comments

Comments
 (0)