|
1
|
import Configstore from "configstore" |
|
2
|
import path from "path" |
|
3
|
|
|
4
|
import { InMemoryConfigStore } from "./util/in-memory-config" |
|
5
|
import isTruthy from "./util/is-truthy" |
|
6
|
import OutboxStore from "./util/outbox-store" |
|
7
|
|
|
8
|
class Store { |
|
9
|
constructor() { |
|
10
|
try { |
|
11
|
this.config_ = new Configstore(`medusa`, {}, { globalConfigPath: true }) |
|
12
|
} catch (e) { |
|
13
|
this.config_ = new InMemoryConfigStore() |
|
14
|
} |
|
15
|
|
|
16
|
const baseDir = path.dirname(this.config_.path) |
|
17
|
this.outbox_ = new OutboxStore(baseDir) |
|
18
|
|
|
19
|
this.disabled_ = isTruthy(process.env.MEDUSA_DISABLE_TELEMETRY) |
|
20
|
} |
|
21
|
|
|
22
|
getQueueSize() { |
|
23
|
return this.outbox_.getSize() |
|
24
|
} |
|
25
|
|
|
26
|
getQueueCount() { |
|
27
|
return this.outbox_.getCount() |
|
28
|
} |
|
29
|
|
|
30
|
addEvent(event) { |
|
31
|
if (this.disabled_) { |
|
32
|
return |
|
33
|
} |
|
34
|
|
|
35
|
const eventString = JSON.stringify(event) |
|
36
|
return this.outbox_.appendToBuffer(eventString + `\n`) |
|
37
|
} |
|
38
|
|
|
39
|
async flushEvents(handler) { |
|
40
|
return await this.outbox_.startFlushEvents(async (eventData) => { |
|
41
|
const events = eventData |
|
42
|
.split(`\n`) |
|
43
|
.filter((e) => e && e.length > 2) |
|
44
|
.map((e) => JSON.parse(e)) |
|
45
|
|
|
46
|
return await handler(events) |
|
47
|
}) |
|
48
|
} |
|
49
|
|
|
50
|
getConfig(path) { |
|
51
|
return this.config_.get(path) |
|
52
|
} |
|
53
|
|
|
54
|
setConfig(path, val) { |
|
55
|
return this.config_.set(path, val) |
|
56
|
} |
|
57
|
} |
|
58
|
|
|
59
|
export default Store |