fix(striker-ui-api): include write code in async database write close handler args

main
Tsu-ba-me 2 years ago
parent 5c7ddbfb5e
commit d9e38d87e2
  1. 29
      striker-ui-api/src/lib/accessModule.ts
  2. 23
      striker-ui-api/src/session.ts
  3. 27
      striker-ui-api/src/types/AccessModule.d.ts

@ -4,6 +4,7 @@ import { readFileSync } from 'fs';
import { SERVER_PATHS } from './consts'; import { SERVER_PATHS } from './consts';
import { formatSql } from './formatSql'; import { formatSql } from './formatSql';
import { isObject } from './isObject';
import { date, stderr as sherr, stdout as shout } from './shell'; import { date, stderr as sherr, stdout as shout } from './shell';
const asyncAnvilAccessModule = ( const asyncAnvilAccessModule = (
@ -190,13 +191,31 @@ const dbSubRefreshTimestamp = () => {
return result; return result;
}; };
const awrite = (script: string, options?: AsyncAnvilAccessModuleOptions) => { const awrite = (
script: string,
{ onClose: initOnClose, ...restOptions }: AsyncDatabaseWriteOptions = {},
) => {
shout(formatSql(script)); shout(formatSql(script));
return asyncAnvilAccessModule( const onClose: AsyncAnvilAccessModuleCloseHandler = (args, ...rest) => {
['--query', script, '--mode', 'write'], const { stdout } = args;
options, const { obj: output } = isObject(stdout);
);
let wcode: number | null = null;
if ('write_code' in output) {
({ write_code: wcode } = output as { write_code: number });
shout(`Async write completed with write_code=${wcode}`);
}
initOnClose?.call(null, { wcode, ...args }, ...rest);
};
return asyncAnvilAccessModule(['--query', script, '--mode', 'write'], {
onClose,
...restOptions,
});
}; };
const dbWrite = (script: string, options?: SpawnSyncOptions) => { const dbWrite = (script: string, options?: SpawnSyncOptions) => {

@ -10,21 +10,10 @@ import {
timestamp, timestamp,
} from './lib/accessModule'; } from './lib/accessModule';
import { getSessionSecret } from './lib/getSessionSecret'; import { getSessionSecret } from './lib/getSessionSecret';
import { isObject } from './lib/isObject';
import { stderr, stdout, stdoutVar, uuidgen } from './lib/shell'; import { stderr, stdout, stdoutVar, uuidgen } from './lib/shell';
const DEFAULT_COOKIE_ORIGINAL_MAX_AGE = 3600000; const DEFAULT_COOKIE_ORIGINAL_MAX_AGE = 3600000;
const getWriteCode = (obj: object) => {
let result: number | undefined;
if ('write_code' in obj) {
({ write_code: result } = obj as { write_code: number });
}
return result;
};
export class SessionStore extends BaseSessionStore { export class SessionStore extends BaseSessionStore {
constructor(options = {}) { constructor(options = {}) {
super(options); super(options);
@ -38,9 +27,7 @@ export class SessionStore extends BaseSessionStore {
try { try {
awrite(`DELETE FROM sessions WHERE session_uuid = '${sid}';`, { awrite(`DELETE FROM sessions WHERE session_uuid = '${sid}';`, {
onClose({ stdout: s1 }) { onClose({ wcode }) {
const wcode = getWriteCode(isObject(s1).obj);
if (wcode !== 0) { if (wcode !== 0) {
stderr( stderr(
`SQL script failed during destroy session ${sid}; code: ${wcode}`, `SQL script failed during destroy session ${sid}; code: ${wcode}`,
@ -147,9 +134,7 @@ export class SessionStore extends BaseSessionStore {
DO UPDATE SET session_host_uuid = '${localHostUuid}', DO UPDATE SET session_host_uuid = '${localHostUuid}',
modified_date = '${modifiedDate}';`, modified_date = '${modifiedDate}';`,
{ {
onClose: ({ stdout: s1 }) => { onClose: ({ wcode }) => {
const wcode = getWriteCode(isObject(s1).obj);
if (wcode !== 0) { if (wcode !== 0) {
stderr( stderr(
`SQL script failed during set session ${sid}; code: ${wcode}`, `SQL script failed during set session ${sid}; code: ${wcode}`,
@ -181,9 +166,7 @@ export class SessionStore extends BaseSessionStore {
awrite( awrite(
`UPDATE sessions SET modified_date = '${timestamp()}' WHERE session_uuid = '${sid}';`, `UPDATE sessions SET modified_date = '${timestamp()}' WHERE session_uuid = '${sid}';`,
{ {
onClose: ({ stdout: s1 }) => { onClose: ({ wcode }) => {
const wcode = getWriteCode(isObject(s1).obj);
if (wcode !== 0) { if (wcode !== 0) {
stderr( stderr(
`SQL script failed during touch session ${sid}; code: ${wcode}`, `SQL script failed during touch session ${sid}; code: ${wcode}`,

@ -1,9 +1,30 @@
type AsyncAnvilAccessModuleOptions = import('child_process').SpawnOptions & { type AsyncAnvilAccessModuleCloseArgs = {
onClose?: (args: {
ecode: number | null; ecode: number | null;
signal: NodeJS.Signals | null; signal: NodeJS.Signals | null;
stderr: string; stderr: string;
stdout: unknown; stdout: unknown;
}) => void; };
type AsyncDatabaseWriteCloseArgs = AsyncAnvilAccessModuleCloseArgs & {
wcode: number | null;
};
type AsyncAnvilAccessModuleCloseHandler = (
args: AsyncAnvilAccessModuleCloseArgs,
) => void;
type AsyncDatabaseWriteCloseHandler = (
args: AsyncDatabaseWriteCloseArgs,
) => void;
type AsyncAnvilAccessModuleOptions = import('child_process').SpawnOptions & {
onClose?: AsyncAnvilAccessModuleCloseHandler;
onError?: (err: Error) => void; onError?: (err: Error) => void;
}; };
type AsyncDatabaseWriteOptions = Omit<
AsyncAnvilAccessModuleOptions,
'onClose'
> & {
onClose?: AsyncDatabaseWriteCloseHandler;
};

Loading…
Cancel
Save