@datastax/astra-db-ts
Version:
Data API TypeScript client
91 lines (90 loc) • 3.15 kB
JavaScript
;
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseClientEvent = exports.PropagationState = void 0;
var PropagationState;
(function (PropagationState) {
PropagationState[PropagationState["Continue"] = 0] = "Continue";
PropagationState[PropagationState["Stop"] = 1] = "Stop";
PropagationState[PropagationState["StopImmediate"] = 2] = "StopImmediate";
})(PropagationState || (exports.PropagationState = PropagationState = {}));
class BaseClientEvent {
static setDefaultFormatter(formatter) {
BaseClientEvent._defaultFormatter = formatter;
}
constructor(name, requestId, extra) {
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "timestamp", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "requestId", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "extraLogInfo", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.name = name;
this.requestId = requestId;
this.extraLogInfo = (extra && Object.keys(extra).length > 0) ? extra : undefined;
this.timestamp = new Date();
Object.defineProperty(this, '_propagationState', {
value: PropagationState.Continue,
enumerable: false,
writable: true,
});
}
format(formatter = BaseClientEvent._defaultFormatter) {
return formatter(this, this.getMessagePrefix() + ' ' + this.getMessage());
}
formatVerbose() {
const clone = { ...this, timestamp: formatTimestampSimple(this.timestamp) };
this._modifyEventForFormatVerbose?.(clone);
return JSON.stringify(clone, null, 2);
}
stopPropagation() {
this._propagationState = PropagationState.Stop;
}
stopImmediatePropagation() {
this._propagationState = PropagationState.StopImmediate;
}
trimDuplicateFields() {
return this;
}
}
exports.BaseClientEvent = BaseClientEvent;
Object.defineProperty(BaseClientEvent, "_defaultFormatter", {
enumerable: true,
configurable: true,
writable: true,
value: defaultFormatFn
});
function defaultFormatFn(event, fullMessage) {
return `${formatTimestampSimple(event.timestamp)} [${event.requestId.slice(0, 8)}] [${event.name}]: ${fullMessage}`;
}
function formatTimestampSimple(date) {
return date.toLocaleString('en-CA', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZoneName: 'short',
}).replace(',', '');
}