@n1k1t/mock-server
Version:
The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations
93 lines • 4.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemProvider = exports.SystemHistoryStorage = exports.SystemContainersStorage = void 0;
const expectations_1 = require("../../../expectations");
const containers_1 = require("../containers");
const history_1 = require("../history");
const context_1 = require("../context");
const models_1 = require("../../models");
const model_1 = require("./model");
class SystemContainersStorage extends containers_1.ContainersStorage {
/** Creates containers using provided dump */
restore(containers) {
containers.forEach((backup) => {
const container = this.register(containers_1.Container.build(backup));
this.entities.delete(container.key);
this.entities.set(`${backup.group}:${backup.key}`, container);
});
return this;
}
}
exports.SystemContainersStorage = SystemContainersStorage;
class SystemHistoryStorage extends history_1.HistoryStorage {
/** Injects and registers history items from plain */
restore(list) {
const fake = model_1.Provider.build({ group: this.configuration.group });
list.forEach((history) => this.register(history_1.History.build({
id: history.id,
group: history.group,
status: history.status,
timestamp: history.timestamp,
meta: history.meta,
...(history.expectation && {
expectation: expectations_1.Expectation.build(history.expectation)
}),
snapshot: context_1.RequestContextSnapshot.build({
transport: history.snapshot.transport,
flags: history.snapshot.flags,
cache: history.snapshot.cache,
state: history.snapshot.state,
error: history.snapshot.error,
incoming: Object.assign(history.snapshot.incoming, { raw: {} }),
outgoing: Object.assign(history.snapshot.outgoing, { raw: {} }),
messages: history.snapshot.messages.map((message) => models_1.RequestMessage.build(message)),
seed: history.snapshot.seed,
storage: fake.storages.containers,
...(history.snapshot.forwarded && {
forwarded: {
schema: history.snapshot.forwarded.schema,
messages: history.snapshot.forwarded.messages?.map((message) => models_1.RequestMessage.build(message)),
incoming: Object.assign(history.snapshot.forwarded.incoming, { raw: {} }),
...(history.snapshot.forwarded.outgoing && {
outgoing: Object.assign(history.snapshot.forwarded.outgoing, { raw: {} }),
}),
},
}),
})
})));
return this;
}
}
exports.SystemHistoryStorage = SystemHistoryStorage;
class SystemProvider extends model_1.Provider {
constructor() {
super(...arguments);
this.storages = {
expectations: new expectations_1.ExpectationsStorage({ group: this.group }),
containers: new SystemContainersStorage({ group: this.group }),
history: new SystemHistoryStorage({ group: this.group, limit: Infinity }),
};
}
/** Distributes persistent history and containers to target provider */
distribute(provider) {
for (const history of this.storages.history.values()) {
if (history.group !== provider.group) {
continue;
}
provider.storages.history.register(history);
this.storages.history.delete(history.id);
}
for (const container of this.storages.containers.values()) {
if (container.group !== provider.group) {
continue;
}
provider.storages.containers.register(container);
this.storages.containers.delete(container.key);
}
}
static build(provided) {
return new SystemProvider(provided);
}
}
exports.SystemProvider = SystemProvider;
//# sourceMappingURL=system.js.map