smartlog-destination-receiver/ts/index.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-06-26 07:27:01 +00:00
import * as plugins from './smartlog-destination-receiver.plugins.js';
2018-11-11 13:29:26 +00:00
import {
2024-05-17 23:32:05 +00:00
type ILogDestination,
type ILogPackageAuthenticated,
type ILogPackage,
} from '@push.rocks/smartlog-interfaces';
2018-11-11 13:29:26 +00:00
export interface ISmartlogDestinationReceiverConstructorOptions {
passphrase: string;
receiverEndpoint: string;
}
export class SmartlogDestinationReceiver implements ILogDestination {
private options: ISmartlogDestinationReceiverConstructorOptions;
2022-06-26 07:27:01 +00:00
private webrequest = new plugins.webrequest.WebRequest();
2018-11-11 13:29:26 +00:00
constructor(optionsArg: ISmartlogDestinationReceiverConstructorOptions) {
this.options = optionsArg;
}
2022-06-26 09:05:53 +00:00
private errorCounter = 0;
2018-11-11 18:40:59 +00:00
public async handleLog(logPackageArg: ILogPackage) {
2022-06-26 07:27:01 +00:00
const response = await this.webrequest.postJson(this.options.receiverEndpoint, {
2022-07-29 01:01:21 +00:00
auth: plugins.smarthash.sha256FromStringSync(this.options.passphrase),
logPackage: logPackageArg,
2022-06-26 09:05:53 +00:00
}).catch(err => {
if (this.errorCounter % 100 === 0) {
console.error(`There seems to be an error with logging.`);
console.error(`Accumulated ${this.errorCounter} errors so far`)
}
this.errorCounter++;
2018-11-11 13:29:26 +00:00
});
2020-06-10 06:48:16 +00:00
return response.body;
2018-11-11 13:29:26 +00:00
}
}