pepr 0.32.7 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +2 -1
- package/dist/cli/banner.d.ts +2 -0
- package/dist/cli/banner.d.ts.map +1 -0
- package/dist/cli/build.d.ts +19 -0
- package/dist/cli/build.d.ts.map +1 -0
- package/dist/cli/deploy.d.ts +3 -0
- package/dist/cli/deploy.d.ts.map +1 -0
- package/dist/cli/dev.d.ts +3 -0
- package/dist/cli/dev.d.ts.map +1 -0
- package/dist/cli/format.d.ts +9 -0
- package/dist/cli/format.d.ts.map +1 -0
- package/dist/cli/init/index.d.ts +3 -0
- package/dist/cli/init/index.d.ts.map +1 -0
- package/dist/cli/init/templates.d.ts +196 -0
- package/dist/cli/init/templates.d.ts.map +1 -0
- package/dist/cli/init/utils.d.ts +21 -0
- package/dist/cli/init/utils.d.ts.map +1 -0
- package/dist/cli/init/utils.test.d.ts +2 -0
- package/dist/cli/init/utils.test.d.ts.map +1 -0
- package/dist/cli/init/walkthrough.d.ts +8 -0
- package/dist/cli/init/walkthrough.d.ts.map +1 -0
- package/dist/cli/init/walkthrough.test.d.ts +2 -0
- package/dist/cli/init/walkthrough.test.d.ts.map +1 -0
- package/dist/cli/kfc.d.ts +3 -0
- package/dist/cli/kfc.d.ts.map +1 -0
- package/dist/cli/monitor.d.ts +3 -0
- package/dist/cli/monitor.d.ts.map +1 -0
- package/dist/cli/root.d.ts +5 -0
- package/dist/cli/root.d.ts.map +1 -0
- package/dist/cli/update.d.ts +3 -0
- package/dist/cli/update.d.ts.map +1 -0
- package/dist/cli/uuid.d.ts +3 -0
- package/dist/cli/uuid.d.ts.map +1 -0
- package/dist/cli.js +66 -25
- package/dist/controller.js +1 -1
- package/dist/fixtures/loader.d.ts +5 -0
- package/dist/fixtures/loader.d.ts.map +1 -0
- package/dist/lib/assets/helm.d.ts +1 -0
- package/dist/lib/assets/helm.d.ts.map +1 -1
- package/dist/lib/assets/helm.test.d.ts +2 -0
- package/dist/lib/assets/helm.test.d.ts.map +1 -0
- package/dist/lib/assets/index.d.ts.map +1 -1
- package/dist/lib/assets/pods.d.ts +3 -0
- package/dist/lib/assets/pods.d.ts.map +1 -1
- package/dist/lib/assets/pods.test.d.ts +2 -0
- package/dist/lib/assets/pods.test.d.ts.map +1 -0
- package/dist/lib/assets/yaml.d.ts.map +1 -1
- package/dist/lib/controller/store.d.ts.map +1 -1
- package/dist/lib/errors.test.d.ts +2 -0
- package/dist/lib/errors.test.d.ts.map +1 -0
- package/dist/lib/filter.test.d.ts +2 -0
- package/dist/lib/filter.test.d.ts.map +1 -0
- package/dist/lib/helpers.test.d.ts +2 -0
- package/dist/lib/helpers.test.d.ts.map +1 -0
- package/dist/lib/included-files.test.d.ts +2 -0
- package/dist/lib/included-files.test.d.ts.map +1 -0
- package/dist/lib/logger.test.d.ts +2 -0
- package/dist/lib/logger.test.d.ts.map +1 -0
- package/dist/lib/metrics.d.ts +18 -0
- package/dist/lib/metrics.d.ts.map +1 -1
- package/dist/lib/metrics.test.d.ts +2 -0
- package/dist/lib/metrics.test.d.ts.map +1 -0
- package/dist/lib/module.test.d.ts +2 -0
- package/dist/lib/module.test.d.ts.map +1 -0
- package/dist/lib/mutate-request.test.d.ts +2 -0
- package/dist/lib/mutate-request.test.d.ts.map +1 -0
- package/dist/lib/queue.test.d.ts +2 -0
- package/dist/lib/queue.test.d.ts.map +1 -0
- package/dist/lib/schedule.test.d.ts +15 -0
- package/dist/lib/schedule.test.d.ts.map +1 -0
- package/dist/lib/storage.d.ts +2 -0
- package/dist/lib/storage.d.ts.map +1 -1
- package/dist/lib/storage.test.d.ts +2 -0
- package/dist/lib/storage.test.d.ts.map +1 -0
- package/dist/lib/tls.test.d.ts +2 -0
- package/dist/lib/tls.test.d.ts.map +1 -0
- package/dist/lib/utils.test.d.ts +2 -0
- package/dist/lib/utils.test.d.ts.map +1 -0
- package/dist/lib/validate-request.test.d.ts +2 -0
- package/dist/lib/validate-request.test.d.ts.map +1 -0
- package/dist/lib/watch-processor.d.ts.map +1 -1
- package/dist/lib/watch-processor.test.d.ts +2 -0
- package/dist/lib/watch-processor.test.d.ts.map +1 -0
- package/dist/lib.js +142 -20
- package/dist/lib.js.map +3 -3
- package/dist/sdk/sdk.test.d.ts +2 -0
- package/dist/sdk/sdk.test.d.ts.map +1 -0
- package/package.json +26 -17
- package/src/cli/banner.ts +63 -0
- package/src/cli/build.ts +370 -0
- package/src/cli/deploy.ts +105 -0
- package/src/cli/dev.ts +118 -0
- package/src/cli/format.ts +83 -0
- package/src/cli/init/index.ts +99 -0
- package/src/cli/init/templates.ts +124 -0
- package/src/cli/init/utils.test.ts +28 -0
- package/src/cli/init/utils.ts +55 -0
- package/src/cli/init/walkthrough.test.ts +21 -0
- package/src/cli/init/walkthrough.ts +96 -0
- package/src/cli/kfc.ts +45 -0
- package/src/cli/monitor.ts +101 -0
- package/src/cli/root.ts +12 -0
- package/src/cli/update.ts +95 -0
- package/src/cli/uuid.ts +44 -0
- package/src/fixtures/data/create-pod.json +271 -0
- package/src/fixtures/data/delete-pod.json +271 -0
- package/src/fixtures/loader.ts +18 -0
- package/src/lib/.prettierrc +14 -0
- package/src/lib/assets/helm.test.ts +64 -0
- package/src/lib/assets/helm.ts +35 -0
- package/src/lib/assets/index.ts +5 -1
- package/src/lib/assets/pods.test.ts +553 -0
- package/src/lib/assets/pods.ts +14 -6
- package/src/lib/assets/yaml.ts +15 -18
- package/src/lib/controller/index.ts +2 -2
- package/src/lib/controller/store.ts +88 -2
- package/src/lib/errors.test.ts +85 -0
- package/src/lib/filter.test.ts +384 -0
- package/src/lib/helpers.test.ts +1192 -0
- package/src/lib/included-files.test.ts +22 -0
- package/src/lib/logger.test.ts +18 -0
- package/src/lib/metrics.test.ts +132 -0
- package/src/lib/metrics.ts +68 -6
- package/src/lib/module.test.ts +126 -0
- package/src/lib/mutate-request.test.ts +188 -0
- package/src/lib/queue.test.ts +58 -0
- package/src/lib/schedule.test.ts +217 -0
- package/src/lib/storage.test.ts +203 -0
- package/src/lib/storage.ts +22 -9
- package/src/lib/tls.test.ts +18 -0
- package/src/lib/utils.test.ts +69 -0
- package/src/lib/validate-request.test.ts +124 -0
- package/src/lib/watch-processor.test.ts +322 -0
- package/src/lib/watch-processor.ts +20 -4
- package/src/sdk/sdk.test.ts +243 -0
- package/src/templates/.eslintrc.json +6 -0
- package/.prettierignore +0 -1
- package/CODE_OF_CONDUCT.md +0 -133
- package/SECURITY.md +0 -18
- package/SUPPORT.md +0 -16
- package/codecov.yaml +0 -19
- package/commitlint.config.js +0 -1
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
+
|
|
4
|
+
import { beforeEach, describe, expect, it, jest, afterEach } from "@jest/globals";
|
|
5
|
+
import { OnSchedule, Schedule } from "./schedule";
|
|
6
|
+
import { Unsubscribe } from "./storage";
|
|
7
|
+
|
|
8
|
+
export class MockStorage {
|
|
9
|
+
private storage: Record<string, string> = {};
|
|
10
|
+
subscription: string;
|
|
11
|
+
constructor() {
|
|
12
|
+
this.subscription = "";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getItem(key: string): string | null {
|
|
16
|
+
return this.storage[key] || null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
setItem(key: string, value: string): void {
|
|
20
|
+
this.storage[key] = value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
setItemAndWait(key: string, value: string): Promise<void> {
|
|
24
|
+
return new Promise(resolve => {
|
|
25
|
+
this.storage[key] = value;
|
|
26
|
+
resolve();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
removeItem(key: string): void {
|
|
31
|
+
delete this.storage[key];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
removeItemAndWait(key: string): Promise<void> {
|
|
35
|
+
return new Promise(resolve => {
|
|
36
|
+
delete this.storage[key];
|
|
37
|
+
resolve();
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
clear(): void {
|
|
42
|
+
this.storage = {};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
subscribe(): Unsubscribe {
|
|
46
|
+
// Expected 'this' to be used by class method 'subscribe'
|
|
47
|
+
this.subscription = "";
|
|
48
|
+
return true as unknown as Unsubscribe;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
onReady(): void {
|
|
52
|
+
// Expected 'this' to be used by class method 'onReady'
|
|
53
|
+
this.subscription = "";
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe("OnSchedule", () => {
|
|
59
|
+
const mockSchedule: Schedule = {
|
|
60
|
+
name: "test",
|
|
61
|
+
every: 1,
|
|
62
|
+
unit: "minutes",
|
|
63
|
+
run: jest.fn(),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
afterEach(() => {
|
|
67
|
+
jest.clearAllMocks();
|
|
68
|
+
jest.useRealTimers();
|
|
69
|
+
jest.clearAllTimers();
|
|
70
|
+
jest.resetModules();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
beforeEach(() => {
|
|
74
|
+
jest.useFakeTimers();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should create an instance of OnSchedule", () => {
|
|
78
|
+
const onSchedule = new OnSchedule(mockSchedule);
|
|
79
|
+
onSchedule.setStore(new MockStorage());
|
|
80
|
+
expect(onSchedule).toBeInstanceOf(OnSchedule);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should startInterval, run, and start", () => {
|
|
84
|
+
const onSchedule = new OnSchedule(mockSchedule);
|
|
85
|
+
onSchedule.setStore(new MockStorage());
|
|
86
|
+
onSchedule.completions = 0;
|
|
87
|
+
|
|
88
|
+
onSchedule.start();
|
|
89
|
+
|
|
90
|
+
onSchedule.startTime = new Date(new Date().getTime() + 100000);
|
|
91
|
+
|
|
92
|
+
onSchedule.setupInterval();
|
|
93
|
+
jest.advanceTimersByTime(100000);
|
|
94
|
+
|
|
95
|
+
const secondSchedule = new OnSchedule(mockSchedule);
|
|
96
|
+
onSchedule.setStore(new MockStorage());
|
|
97
|
+
secondSchedule.completions = 9;
|
|
98
|
+
secondSchedule.duration = 1;
|
|
99
|
+
secondSchedule.start();
|
|
100
|
+
jest.advanceTimersByTime(100000);
|
|
101
|
+
expect(secondSchedule.completions).toBe(0);
|
|
102
|
+
jest.runOnlyPendingTimers();
|
|
103
|
+
jest.clearAllTimers();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("should stop, removeItem, and removeItem", () => {
|
|
107
|
+
const onSchedule = new OnSchedule(mockSchedule);
|
|
108
|
+
onSchedule.setStore(new MockStorage());
|
|
109
|
+
const removeItemSpy = jest.spyOn(onSchedule.store as MockStorage, "removeItem");
|
|
110
|
+
|
|
111
|
+
onSchedule.startInterval();
|
|
112
|
+
onSchedule.stop();
|
|
113
|
+
|
|
114
|
+
expect(onSchedule.intervalId).toBeNull();
|
|
115
|
+
expect(removeItemSpy).toHaveBeenCalled();
|
|
116
|
+
|
|
117
|
+
onSchedule.intervalId = 9 as unknown as NodeJS.Timeout;
|
|
118
|
+
onSchedule.stop();
|
|
119
|
+
expect(onSchedule.intervalId).toBeNull();
|
|
120
|
+
jest.runOnlyPendingTimers();
|
|
121
|
+
jest.clearAllTimers();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("should getDuration", () => {
|
|
125
|
+
// test second
|
|
126
|
+
mockSchedule.every = 10;
|
|
127
|
+
mockSchedule.unit = "seconds";
|
|
128
|
+
const onSchedule = new OnSchedule(mockSchedule);
|
|
129
|
+
onSchedule.getDuration();
|
|
130
|
+
expect(onSchedule.duration).toBe(10000);
|
|
131
|
+
|
|
132
|
+
// test second error
|
|
133
|
+
mockSchedule.every = 8;
|
|
134
|
+
try {
|
|
135
|
+
onSchedule.getDuration();
|
|
136
|
+
} catch (e) {
|
|
137
|
+
expect(e).toEqual(new Error("10 Seconds in the smallest interval allowed"));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// test minute(s)
|
|
141
|
+
onSchedule.unit = "minutes";
|
|
142
|
+
onSchedule.getDuration();
|
|
143
|
+
expect(onSchedule.duration).toBe(600000);
|
|
144
|
+
onSchedule.unit = "minute";
|
|
145
|
+
onSchedule.getDuration();
|
|
146
|
+
expect(onSchedule.duration).toBe(600000);
|
|
147
|
+
|
|
148
|
+
// test hour(s)
|
|
149
|
+
onSchedule.unit = "hours";
|
|
150
|
+
onSchedule.getDuration();
|
|
151
|
+
expect(onSchedule.duration).toBe(36000000);
|
|
152
|
+
onSchedule.unit = "hour";
|
|
153
|
+
onSchedule.getDuration();
|
|
154
|
+
expect(onSchedule.duration).toBe(36000000);
|
|
155
|
+
|
|
156
|
+
// test invalid unit
|
|
157
|
+
onSchedule.unit = "second";
|
|
158
|
+
try {
|
|
159
|
+
onSchedule.getDuration();
|
|
160
|
+
} catch (e) {
|
|
161
|
+
expect(e).toEqual(new Error("Invalid time unit"));
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("should setupInterval", () => {
|
|
166
|
+
// startTime and lastTimestamp should set startTime to undefined
|
|
167
|
+
mockSchedule.startTime = new Date();
|
|
168
|
+
mockSchedule.unit = "seconds";
|
|
169
|
+
mockSchedule.every = 10;
|
|
170
|
+
const onSchedule = new OnSchedule(mockSchedule);
|
|
171
|
+
onSchedule.setStore(new MockStorage());
|
|
172
|
+
onSchedule.lastTimestamp = new Date();
|
|
173
|
+
onSchedule.setupInterval();
|
|
174
|
+
expect(onSchedule.startTime).toBeUndefined();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should call setItem during saveToStore", () => {
|
|
178
|
+
const onSchedule = new OnSchedule(mockSchedule);
|
|
179
|
+
onSchedule.setStore(new MockStorage());
|
|
180
|
+
|
|
181
|
+
const setItemSpy = jest.spyOn(onSchedule.store as MockStorage, "setItem");
|
|
182
|
+
onSchedule.saveToStore();
|
|
183
|
+
expect(setItemSpy).toHaveBeenCalledTimes(1);
|
|
184
|
+
});
|
|
185
|
+
it("checkStore retrieves values from the store", () => {
|
|
186
|
+
mockSchedule.run = () => {};
|
|
187
|
+
const onSchedule = new OnSchedule(mockSchedule);
|
|
188
|
+
onSchedule.setStore(new MockStorage());
|
|
189
|
+
const getItemSpy = jest.spyOn(onSchedule.store as MockStorage, "getItem");
|
|
190
|
+
const startTime = "doesn't";
|
|
191
|
+
const lastTimestamp = "matter";
|
|
192
|
+
getItemSpy.mockReturnValue(
|
|
193
|
+
JSON.stringify({
|
|
194
|
+
completions: 1,
|
|
195
|
+
startTime,
|
|
196
|
+
lastTimestamp,
|
|
197
|
+
}),
|
|
198
|
+
);
|
|
199
|
+
onSchedule.checkStore();
|
|
200
|
+
|
|
201
|
+
expect(getItemSpy).toHaveBeenCalledTimes(1);
|
|
202
|
+
|
|
203
|
+
// // Verify that the values were correctly retrieved and assigned
|
|
204
|
+
expect(onSchedule.completions).toBe(1);
|
|
205
|
+
expect(onSchedule.startTime).toEqual(startTime);
|
|
206
|
+
expect(onSchedule.lastTimestamp).toEqual(lastTimestamp);
|
|
207
|
+
|
|
208
|
+
getItemSpy.mockReturnValue(
|
|
209
|
+
JSON.stringify({
|
|
210
|
+
lastTimestamp: undefined,
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
onSchedule.checkStore();
|
|
214
|
+
|
|
215
|
+
expect(onSchedule.lastTimestamp).toEqual(undefined);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
+
|
|
4
|
+
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|
5
|
+
import { DataStore, Storage, v2StoreKey, stripV2Prefix } from "./storage";
|
|
6
|
+
import fc from "fast-check";
|
|
7
|
+
|
|
8
|
+
describe("stripV2Prefix", () => {
|
|
9
|
+
it("should remove the v2 prefix", () => {
|
|
10
|
+
const keys = ["v2-key1", "v2-key2", "v2-key3", "v2-key4", "v2-key5"];
|
|
11
|
+
const results = ["key1", "key2", "key3", "key4", "key5"];
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < keys.length; i++) {
|
|
14
|
+
const result = stripV2Prefix(keys[i]);
|
|
15
|
+
expect(result).toEqual(results[i]);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
describe("v2StoreKey", () => {
|
|
20
|
+
it("should prefix the key with v2", () => {
|
|
21
|
+
const keys = ["key1", "key2", "key3", "key4", "key5"];
|
|
22
|
+
const results = ["v2-key1", "v2-key2", "v2-key3", "v2-key4", "v2-key5"];
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < keys.length; i++) {
|
|
25
|
+
const result = v2StoreKey(keys[i]);
|
|
26
|
+
expect(result).toEqual(results[i]);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe("Storage with fuzzing and property-based tests", () => {
|
|
31
|
+
let storage: Storage;
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
storage = new Storage();
|
|
35
|
+
storage.registerSender(jest.fn());
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should correctly set and retrieve items", () => {
|
|
39
|
+
fc.assert(
|
|
40
|
+
fc.property(fc.string(), fc.string(), (key, value) => {
|
|
41
|
+
storage.setItem(key, value);
|
|
42
|
+
const mockData: DataStore = { [v2StoreKey(key)]: value };
|
|
43
|
+
storage.receive(mockData);
|
|
44
|
+
if (value === "") {
|
|
45
|
+
expect(storage.getItem(key)).toBeNull();
|
|
46
|
+
} else {
|
|
47
|
+
expect(storage.getItem(key)).toEqual(value);
|
|
48
|
+
}
|
|
49
|
+
}),
|
|
50
|
+
{ numRuns: 100 },
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should return null for non-existing items", () => {
|
|
55
|
+
fc.assert(
|
|
56
|
+
fc.property(fc.string(), key => {
|
|
57
|
+
expect(storage.getItem(key)).toBeNull();
|
|
58
|
+
}),
|
|
59
|
+
{ numRuns: 100 },
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should correctly remove items", () => {
|
|
64
|
+
fc.assert(
|
|
65
|
+
fc.property(fc.string(), fc.string(), (key, value) => {
|
|
66
|
+
storage.setItem(key, value);
|
|
67
|
+
storage.removeItem(key);
|
|
68
|
+
expect(storage.getItem(key)).toBeNull();
|
|
69
|
+
}),
|
|
70
|
+
{ numRuns: 100 },
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should ensure all set items are v2-coded internally", () => {
|
|
75
|
+
fc.assert(
|
|
76
|
+
fc.property(fc.string(), fc.string(), (key, value) => {
|
|
77
|
+
storage.setItem(key, value);
|
|
78
|
+
const mockData: DataStore = { [v2StoreKey(key)]: value };
|
|
79
|
+
storage.receive(mockData);
|
|
80
|
+
if (value === "") {
|
|
81
|
+
expect(storage.getItem(key)).toBeNull();
|
|
82
|
+
} else {
|
|
83
|
+
expect(storage.getItem(key)).toEqual(value);
|
|
84
|
+
}
|
|
85
|
+
}),
|
|
86
|
+
{ numRuns: 100 },
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
describe("Storage", () => {
|
|
91
|
+
let storage: Storage;
|
|
92
|
+
|
|
93
|
+
beforeEach(() => {
|
|
94
|
+
storage = new Storage();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should set an item", () => {
|
|
98
|
+
const mockSender = jest.fn();
|
|
99
|
+
storage.registerSender(mockSender);
|
|
100
|
+
const key = "key1";
|
|
101
|
+
storage.setItem(key, "value1");
|
|
102
|
+
|
|
103
|
+
expect(mockSender).toHaveBeenCalledWith("add", [v2StoreKey(key)], "value1");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("should set an item and wait", () => {
|
|
107
|
+
const mockSender = jest.fn();
|
|
108
|
+
storage.registerSender(mockSender);
|
|
109
|
+
jest.useFakeTimers();
|
|
110
|
+
const key = "key1";
|
|
111
|
+
|
|
112
|
+
// asserting on sender invocation rather than Promise so no need to wait
|
|
113
|
+
void storage.setItemAndWait(key, "value1");
|
|
114
|
+
|
|
115
|
+
expect(mockSender).toHaveBeenCalledWith("add", [v2StoreKey(key)], "value1");
|
|
116
|
+
jest.useRealTimers();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("should remove an item and wait", () => {
|
|
120
|
+
const mockSender = jest.fn();
|
|
121
|
+
storage.registerSender(mockSender);
|
|
122
|
+
jest.useFakeTimers();
|
|
123
|
+
|
|
124
|
+
const key = "key1";
|
|
125
|
+
// asserting on sender invocation rather than Promise so no need to wait
|
|
126
|
+
void storage.removeItemAndWait(key);
|
|
127
|
+
|
|
128
|
+
expect(mockSender).toHaveBeenCalledWith("remove", [v2StoreKey(key)], undefined);
|
|
129
|
+
jest.useRealTimers();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should remove an item", () => {
|
|
133
|
+
const mockSender = jest.fn();
|
|
134
|
+
storage.registerSender(mockSender);
|
|
135
|
+
const key = "key1";
|
|
136
|
+
storage.removeItem(key);
|
|
137
|
+
|
|
138
|
+
expect(mockSender).toHaveBeenCalledWith("remove", [v2StoreKey(key)], undefined);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("should clear all items", () => {
|
|
142
|
+
const mockSender = jest.fn();
|
|
143
|
+
storage.registerSender(mockSender);
|
|
144
|
+
|
|
145
|
+
storage.receive({ key1: "value1", key2: "value2" });
|
|
146
|
+
storage.clear();
|
|
147
|
+
|
|
148
|
+
expect(mockSender).toHaveBeenCalledWith("remove", ["key1", "key2"], undefined);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("should get an item", () => {
|
|
152
|
+
const keys = ["key1", "!", "!", "pepr", "https://google.com", "sftp://here:22", "!"];
|
|
153
|
+
const results = ["value1", null, "!", "was-here", "3f7dd007-568f-4f4a-bbac-2e6bfff93860", "your-machine", " "];
|
|
154
|
+
|
|
155
|
+
keys.map((key, i) => {
|
|
156
|
+
const mockData: DataStore = { [v2StoreKey(key)]: results[i]! };
|
|
157
|
+
|
|
158
|
+
storage.receive(mockData);
|
|
159
|
+
const value = storage.getItem(keys[i]);
|
|
160
|
+
expect(value).toEqual(results[i]);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("should return null for non-existing item", () => {
|
|
165
|
+
const value = storage.getItem("key1");
|
|
166
|
+
|
|
167
|
+
expect(value).toBeNull();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("should subscribe and unsubscribe", () => {
|
|
171
|
+
const mockSubscriber = jest.fn();
|
|
172
|
+
const unsubscribe = storage.subscribe(mockSubscriber);
|
|
173
|
+
|
|
174
|
+
storage.receive({ key1: "value1" });
|
|
175
|
+
|
|
176
|
+
expect(mockSubscriber).toHaveBeenCalledWith({ key1: "value1" });
|
|
177
|
+
|
|
178
|
+
unsubscribe();
|
|
179
|
+
|
|
180
|
+
storage.receive({ key2: "value2" });
|
|
181
|
+
|
|
182
|
+
// Should not be called again after unsubscribe
|
|
183
|
+
expect(mockSubscriber).toHaveBeenCalledTimes(1);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("should call onReady handlers", () => {
|
|
187
|
+
const mockReadyHandler = jest.fn();
|
|
188
|
+
|
|
189
|
+
storage.onReady(mockReadyHandler);
|
|
190
|
+
storage.receive({ key1: "value1" });
|
|
191
|
+
|
|
192
|
+
expect(mockReadyHandler).toHaveBeenCalledWith({ key1: "value1" });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("should handle null data in receive method", () => {
|
|
196
|
+
const mockSubscriber = jest.fn();
|
|
197
|
+
storage.subscribe(mockSubscriber);
|
|
198
|
+
|
|
199
|
+
storage.receive(null as unknown as DataStore);
|
|
200
|
+
|
|
201
|
+
expect(mockSubscriber).toHaveBeenCalledWith({});
|
|
202
|
+
});
|
|
203
|
+
});
|
package/src/lib/storage.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { clone } from "ramda";
|
|
5
5
|
import Log from "./logger";
|
|
6
|
-
|
|
6
|
+
import pointer from "json-pointer";
|
|
7
7
|
export type DataOp = "add" | "remove";
|
|
8
8
|
export type DataStore = Record<string, string>;
|
|
9
9
|
export type DataSender = (op: DataOp, keys: string[], value?: string) => void;
|
|
@@ -11,6 +11,15 @@ export type DataReceiver = (data: DataStore) => void;
|
|
|
11
11
|
export type Unsubscribe = () => void;
|
|
12
12
|
|
|
13
13
|
const MAX_WAIT_TIME = 15000;
|
|
14
|
+
const STORE_VERSION_PREFIX = "v2";
|
|
15
|
+
|
|
16
|
+
export function v2StoreKey(key: string) {
|
|
17
|
+
return `${STORE_VERSION_PREFIX}-${pointer.escape(key)}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function stripV2Prefix(key: string) {
|
|
21
|
+
return key.replace(/^v2-/, "");
|
|
22
|
+
}
|
|
14
23
|
export interface PeprStore {
|
|
15
24
|
/**
|
|
16
25
|
* Returns the current value associated with the given key, or null if the given key does not exist.
|
|
@@ -60,6 +69,7 @@ export interface PeprStore {
|
|
|
60
69
|
*
|
|
61
70
|
* The API is similar to the [Storage API](https://developer.mozilla.org/docs/Web/API/Storage)
|
|
62
71
|
*/
|
|
72
|
+
|
|
63
73
|
export class Storage implements PeprStore {
|
|
64
74
|
#store: DataStore = {};
|
|
65
75
|
#send!: DataSender;
|
|
@@ -85,8 +95,11 @@ export class Storage implements PeprStore {
|
|
|
85
95
|
};
|
|
86
96
|
|
|
87
97
|
getItem = (key: string) => {
|
|
88
|
-
|
|
89
|
-
|
|
98
|
+
const result = this.#store[v2StoreKey(key)] || null;
|
|
99
|
+
if (result !== null && typeof result !== "function" && typeof result !== "object") {
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
90
103
|
};
|
|
91
104
|
|
|
92
105
|
clear = () => {
|
|
@@ -94,11 +107,11 @@ export class Storage implements PeprStore {
|
|
|
94
107
|
};
|
|
95
108
|
|
|
96
109
|
removeItem = (key: string) => {
|
|
97
|
-
this.#dispatchUpdate("remove", [key]);
|
|
110
|
+
this.#dispatchUpdate("remove", [v2StoreKey(key)]);
|
|
98
111
|
};
|
|
99
112
|
|
|
100
113
|
setItem = (key: string, value: string) => {
|
|
101
|
-
this.#dispatchUpdate("add", [key], value);
|
|
114
|
+
this.#dispatchUpdate("add", [v2StoreKey(key)], value);
|
|
102
115
|
};
|
|
103
116
|
|
|
104
117
|
/**
|
|
@@ -110,10 +123,10 @@ export class Storage implements PeprStore {
|
|
|
110
123
|
* @returns
|
|
111
124
|
*/
|
|
112
125
|
setItemAndWait = (key: string, value: string) => {
|
|
113
|
-
this.#dispatchUpdate("add", [key], value);
|
|
126
|
+
this.#dispatchUpdate("add", [v2StoreKey(key)], value);
|
|
114
127
|
return new Promise<void>((resolve, reject) => {
|
|
115
128
|
const unsubscribe = this.subscribe(data => {
|
|
116
|
-
if (data[key] === value) {
|
|
129
|
+
if (data[`${v2StoreKey(key)}`] === value) {
|
|
117
130
|
unsubscribe();
|
|
118
131
|
resolve();
|
|
119
132
|
}
|
|
@@ -135,10 +148,10 @@ export class Storage implements PeprStore {
|
|
|
135
148
|
* @returns
|
|
136
149
|
*/
|
|
137
150
|
removeItemAndWait = (key: string) => {
|
|
138
|
-
this.#dispatchUpdate("remove", [key]);
|
|
151
|
+
this.#dispatchUpdate("remove", [v2StoreKey(key)]);
|
|
139
152
|
return new Promise<void>((resolve, reject) => {
|
|
140
153
|
const unsubscribe = this.subscribe(data => {
|
|
141
|
-
if (!Object.hasOwn(data, key)) {
|
|
154
|
+
if (!Object.hasOwn(data, `${v2StoreKey(key)}`)) {
|
|
142
155
|
unsubscribe();
|
|
143
156
|
resolve();
|
|
144
157
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
+
|
|
4
|
+
import { expect, test, describe } from "@jest/globals";
|
|
5
|
+
import { genTLS } from "./tls";
|
|
6
|
+
|
|
7
|
+
describe("tls", () => {
|
|
8
|
+
test("genTLS should generate a valid TLSOut object", () => {
|
|
9
|
+
const tls = genTLS("test");
|
|
10
|
+
expect(tls).toHaveProperty("ca");
|
|
11
|
+
expect(tls).toHaveProperty("crt");
|
|
12
|
+
expect(tls).toHaveProperty("key");
|
|
13
|
+
expect(tls).toHaveProperty("pem");
|
|
14
|
+
expect(tls.pem).toHaveProperty("ca");
|
|
15
|
+
expect(tls.pem).toHaveProperty("crt");
|
|
16
|
+
expect(tls.pem).toHaveProperty("key");
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
+
|
|
4
|
+
import { expect, test, describe } from "@jest/globals";
|
|
5
|
+
import { convertToBase64Map, convertFromBase64Map, base64Decode, base64Encode } from "./utils";
|
|
6
|
+
|
|
7
|
+
describe("utils", () => {
|
|
8
|
+
test("convertToBase64Map should encode all ascii values and skip listed values in skip array", () => {
|
|
9
|
+
const obj = {
|
|
10
|
+
data: {
|
|
11
|
+
test1: "test1",
|
|
12
|
+
test2: "test2",
|
|
13
|
+
test3: "test3",
|
|
14
|
+
test4: "test4",
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
const skip = ["test2", "test4"];
|
|
18
|
+
convertToBase64Map(obj, skip);
|
|
19
|
+
expect(obj.data["test1"]).toBe(base64Encode("test1"));
|
|
20
|
+
expect(obj.data["test2"]).toBe("test2");
|
|
21
|
+
expect(obj.data["test3"]).toBe(base64Encode("test3"));
|
|
22
|
+
expect(obj.data["test4"]).toBe("test4");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("convertFromBase64Map should decode all ascii values and skip values in skip array", () => {
|
|
26
|
+
const obj = {
|
|
27
|
+
data: {
|
|
28
|
+
test1: base64Encode("test1"),
|
|
29
|
+
test2: "test2",
|
|
30
|
+
test3: base64Encode("test3"),
|
|
31
|
+
test4: "test4",
|
|
32
|
+
test5: undefined as unknown as string,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const skip = convertFromBase64Map(obj);
|
|
36
|
+
expect(obj.data["test1"]).toBe("test1");
|
|
37
|
+
expect(obj.data["test2"]).toBe("test2");
|
|
38
|
+
expect(obj.data["test3"]).toBe("test3");
|
|
39
|
+
expect(obj.data["test4"]).toBe("test4");
|
|
40
|
+
expect(skip).toEqual(["test2", "test4"]);
|
|
41
|
+
expect(obj.data["test5"]).toBe("");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("base64Decode should decode a base64 string", () => {
|
|
45
|
+
const data = "dGVzdDE=";
|
|
46
|
+
expect(base64Decode(data)).toBe("test1");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("base64Encode should encode a string to base64", () => {
|
|
50
|
+
const data = "test1";
|
|
51
|
+
expect(base64Encode(data)).toBe("dGVzdDE=");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("convertToBase64Map empty object", () => {
|
|
55
|
+
const obj = {};
|
|
56
|
+
const objOut = { data: {} };
|
|
57
|
+
convertToBase64Map(obj, []);
|
|
58
|
+
expect(obj).toStrictEqual(objOut);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("convertFromBase64Map empty object", () => {
|
|
62
|
+
const obj = {};
|
|
63
|
+
const objOut = { data: {} };
|
|
64
|
+
const skip = convertFromBase64Map(obj);
|
|
65
|
+
expect(obj).toStrictEqual(objOut);
|
|
66
|
+
expect(skip).toEqual([]);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
// Path: src/lib/utils.ts
|