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. 33
      striker-ui-api/src/types/AccessModule.d.ts

@ -4,6 +4,7 @@ import { readFileSync } from 'fs';
import { SERVER_PATHS } from './consts';
import { formatSql } from './formatSql';
import { isObject } from './isObject';
import { date, stderr as sherr, stdout as shout } from './shell';
const asyncAnvilAccessModule = (
@ -190,13 +191,31 @@ const dbSubRefreshTimestamp = () => {
return result;
};
const awrite = (script: string, options?: AsyncAnvilAccessModuleOptions) => {
const awrite = (
script: string,
{ onClose: initOnClose, ...restOptions }: AsyncDatabaseWriteOptions = {},
) => {
shout(formatSql(script));
return asyncAnvilAccessModule(
['--query', script, '--mode', 'write'],
options,
);
const onClose: AsyncAnvilAccessModuleCloseHandler = (args, ...rest) => {
const { stdout } = args;
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) => {

@ -10,21 +10,10 @@ import {
timestamp,
} from './lib/accessModule';
import { getSessionSecret } from './lib/getSessionSecret';
import { isObject } from './lib/isObject';
import { stderr, stdout, stdoutVar, uuidgen } from './lib/shell';
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 {
constructor(options = {}) {
super(options);
@ -38,9 +27,7 @@ export class SessionStore extends BaseSessionStore {
try {
awrite(`DELETE FROM sessions WHERE session_uuid = '${sid}';`, {
onClose({ stdout: s1 }) {
const wcode = getWriteCode(isObject(s1).obj);
onClose({ wcode }) {
if (wcode !== 0) {
stderr(
`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}',
modified_date = '${modifiedDate}';`,
{
onClose: ({ stdout: s1 }) => {
const wcode = getWriteCode(isObject(s1).obj);
onClose: ({ wcode }) => {
if (wcode !== 0) {
stderr(
`SQL script failed during set session ${sid}; code: ${wcode}`,
@ -181,9 +166,7 @@ export class SessionStore extends BaseSessionStore {
awrite(
`UPDATE sessions SET modified_date = '${timestamp()}' WHERE session_uuid = '${sid}';`,
{
onClose: ({ stdout: s1 }) => {
const wcode = getWriteCode(isObject(s1).obj);
onClose: ({ wcode }) => {
if (wcode !== 0) {
stderr(
`SQL script failed during touch session ${sid}; code: ${wcode}`,

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

Loading…
Cancel
Save