81 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-01-03 14:51:07 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Auth-Server-Helper which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, January 2021
*/
2021-01-06 22:43:03 +01:00
/* eslint-disable no-console */
2020-12-30 17:21:56 +01:00
import http from 'http';
2021-01-06 22:43:03 +01:00
import ks from '../lib/KeyStore';
2020-12-30 17:21:56 +01:00
2021-01-01 14:14:19 +01:00
export class Response extends http.IncomingMessage {
2020-12-30 17:21:56 +01:00
body?: string;
}
2021-01-01 14:14:19 +01:00
export function get (
2021-01-03 14:51:07 +01:00
// eslint-disable-next-line default-param-last
headers: http.OutgoingHttpHeaders = {},
body?: string
2020-12-30 17:21:56 +01:00
): Promise<Response> {
return new Promise ((resolve) => {
2021-01-03 14:51:07 +01:00
const req = http.request ('http://localhost:3000', {
headers,
method: typeof body === 'string' ? 'POST' : 'GET'
}, (res: Response) => {
let data = '';
2020-12-30 17:21:56 +01:00
res.on ('data', (d) => {
2021-01-03 14:51:07 +01:00
data += d;
2020-12-30 17:21:56 +01:00
});
res.on ('end', () => {
2021-01-03 14:51:07 +01:00
res.body = data;
2020-12-30 17:21:56 +01:00
resolve (res);
});
});
2021-01-03 14:51:07 +01:00
if (typeof body === 'string')
req.write (body);
req.end ();
2020-12-30 17:21:56 +01:00
});
}
2021-01-01 14:14:19 +01:00
export function modify_signature (signature: string): string {
const dec = signature.split ('.');
dec[1] = '';
return dec.join ('.');
}
2021-01-06 22:43:03 +01:00
/* eslint-disable dot-notation */
export function assert_keystore_state (): void {
2021-01-07 15:43:54 +01:00
const set = ks['_keys'];
const keys = Object.keys (set);
if (keys.length !== 0) {
const has_sign = keys.filter (
(v) => typeof set[v].private_key !== 'undefined'
).length;
2021-01-06 22:43:03 +01:00
console.warn ('keystore gc not running!');
2021-01-07 15:43:54 +01:00
console.warn (`${keys.length} keys with ${has_sign} signature keys left`);
2021-01-06 22:43:03 +01:00
ks['_keys'] = {};
}
}
/* eslint-enable dot-notation */
2021-01-07 15:43:54 +01:00
export function clock_setup ():void {
assert_keystore_state ();
const date = (new Date);
2021-01-08 13:30:53 +01:00
date.setHours (0, 0, 2, 0);
2021-01-06 22:43:03 +01:00
jasmine.clock ()
2021-01-07 15:43:54 +01:00
.install ();
jasmine.clock ()
.mockDate (date);
}
export function clock_finalize ():void {
2021-01-06 22:43:03 +01:00
jasmine.clock ()
.tick (30 * 24 * 60 * 60 * 1000);
2021-01-07 15:43:54 +01:00
// eslint-disable-next-line dot-notation
ks['garbage_collect'] ();
jasmine.clock ()
.uninstall ();
2021-01-06 22:43:03 +01:00
}