@wopr-network/platform-core 1.18.0 → 1.20.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/dist/api/routes/admin-audit-helper.d.ts +1 -1
- package/dist/billing/crypto/btc/__tests__/address-gen.test.d.ts +1 -0
- package/dist/billing/crypto/btc/__tests__/address-gen.test.js +44 -0
- package/dist/billing/crypto/btc/__tests__/config.test.d.ts +1 -0
- package/dist/billing/crypto/btc/__tests__/config.test.js +24 -0
- package/dist/billing/crypto/btc/__tests__/settler.test.d.ts +1 -0
- package/dist/billing/crypto/btc/__tests__/settler.test.js +92 -0
- package/dist/billing/crypto/btc/address-gen.d.ts +8 -0
- package/dist/billing/crypto/btc/address-gen.js +34 -0
- package/dist/billing/crypto/btc/checkout.d.ts +21 -0
- package/dist/billing/crypto/btc/checkout.js +42 -0
- package/dist/billing/crypto/btc/config.d.ts +12 -0
- package/dist/billing/crypto/btc/config.js +28 -0
- package/dist/billing/crypto/btc/index.d.ts +9 -0
- package/dist/billing/crypto/btc/index.js +5 -0
- package/dist/billing/crypto/btc/settler.d.ts +23 -0
- package/dist/billing/crypto/btc/settler.js +55 -0
- package/dist/billing/crypto/btc/types.d.ts +23 -0
- package/dist/billing/crypto/btc/types.js +1 -0
- package/dist/billing/crypto/btc/watcher.d.ts +28 -0
- package/dist/billing/crypto/btc/watcher.js +83 -0
- package/dist/billing/crypto/charge-store.d.ts +3 -3
- package/dist/billing/crypto/evm/__tests__/config.test.js +42 -2
- package/dist/billing/crypto/evm/__tests__/watcher.test.js +31 -17
- package/dist/billing/crypto/evm/checkout.js +4 -2
- package/dist/billing/crypto/evm/config.js +73 -0
- package/dist/billing/crypto/evm/types.d.ts +1 -1
- package/dist/billing/crypto/evm/watcher.js +2 -0
- package/dist/billing/crypto/index.d.ts +2 -1
- package/dist/billing/crypto/index.js +1 -0
- package/dist/db/schema/crypto.js +1 -1
- package/dist/fleet/__tests__/rollout-strategy.test.d.ts +1 -0
- package/dist/fleet/__tests__/rollout-strategy.test.js +157 -0
- package/dist/fleet/__tests__/volume-snapshot-manager.test.d.ts +1 -0
- package/dist/fleet/__tests__/volume-snapshot-manager.test.js +171 -0
- package/dist/fleet/index.d.ts +2 -0
- package/dist/fleet/index.js +2 -0
- package/dist/fleet/rollout-strategy.d.ts +52 -0
- package/dist/fleet/rollout-strategy.js +91 -0
- package/dist/fleet/volume-snapshot-manager.d.ts +35 -0
- package/dist/fleet/volume-snapshot-manager.js +185 -0
- package/package.json +3 -1
- package/src/api/routes/admin-audit-helper.ts +1 -1
- package/src/billing/crypto/btc/__tests__/address-gen.test.ts +53 -0
- package/src/billing/crypto/btc/__tests__/config.test.ts +28 -0
- package/src/billing/crypto/btc/__tests__/settler.test.ts +103 -0
- package/src/billing/crypto/btc/address-gen.ts +41 -0
- package/src/billing/crypto/btc/checkout.ts +61 -0
- package/src/billing/crypto/btc/config.ts +33 -0
- package/src/billing/crypto/btc/index.ts +9 -0
- package/src/billing/crypto/btc/settler.ts +74 -0
- package/src/billing/crypto/btc/types.ts +25 -0
- package/src/billing/crypto/btc/watcher.ts +115 -0
- package/src/billing/crypto/charge-store.ts +3 -3
- package/src/billing/crypto/evm/__tests__/config.test.ts +51 -2
- package/src/billing/crypto/evm/__tests__/watcher.test.ts +34 -17
- package/src/billing/crypto/evm/checkout.ts +4 -2
- package/src/billing/crypto/evm/config.ts +73 -0
- package/src/billing/crypto/evm/types.ts +1 -1
- package/src/billing/crypto/evm/watcher.ts +2 -0
- package/src/billing/crypto/index.ts +2 -1
- package/src/db/schema/crypto.ts +1 -1
- package/src/fleet/__tests__/rollout-strategy.test.ts +192 -0
- package/src/fleet/__tests__/volume-snapshot-manager.test.ts +218 -0
- package/src/fleet/index.ts +2 -0
- package/src/fleet/rollout-strategy.ts +128 -0
- package/src/fleet/volume-snapshot-manager.ts +213 -0
- package/src/marketplace/volume-installer.test.ts +8 -2
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { mkdir, readdir, rm, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type Docker from "dockerode";
|
|
4
|
+
import { logger } from "../config/logger.js";
|
|
5
|
+
|
|
6
|
+
export interface VolumeSnapshot {
|
|
7
|
+
id: string;
|
|
8
|
+
volumeName: string;
|
|
9
|
+
archivePath: string;
|
|
10
|
+
createdAt: Date;
|
|
11
|
+
sizeBytes: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const ALPINE_IMAGE = "alpine:latest";
|
|
15
|
+
|
|
16
|
+
/** Strict validation for snapshot IDs — prevents path traversal and shell injection. */
|
|
17
|
+
const SNAPSHOT_ID_RE = /^[A-Za-z0-9._-]+$/;
|
|
18
|
+
|
|
19
|
+
function validateSnapshotId(snapshotId: string): void {
|
|
20
|
+
if (!SNAPSHOT_ID_RE.test(snapshotId)) {
|
|
21
|
+
throw new Error(`Invalid snapshot ID: ${snapshotId}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Snapshots and restores Docker named volumes using temporary alpine containers.
|
|
27
|
+
* Used for nuclear rollback during fleet updates — if a container update fails,
|
|
28
|
+
* we roll back both the image AND the data volumes.
|
|
29
|
+
*/
|
|
30
|
+
export class VolumeSnapshotManager {
|
|
31
|
+
private readonly docker: Docker;
|
|
32
|
+
private readonly backupDir: string;
|
|
33
|
+
|
|
34
|
+
constructor(docker: Docker, backupDir = "/data/fleet/snapshots") {
|
|
35
|
+
this.docker = docker;
|
|
36
|
+
this.backupDir = backupDir;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Create a snapshot of a Docker named volume */
|
|
40
|
+
async snapshot(volumeName: string): Promise<VolumeSnapshot> {
|
|
41
|
+
await mkdir(this.backupDir, { recursive: true });
|
|
42
|
+
|
|
43
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
44
|
+
const id = `${volumeName}-${timestamp}`;
|
|
45
|
+
const archivePath = join(this.backupDir, `${id}.tar`);
|
|
46
|
+
|
|
47
|
+
const container = await this.docker.createContainer({
|
|
48
|
+
Image: ALPINE_IMAGE,
|
|
49
|
+
Cmd: ["tar", "cf", `/backup/${id}.tar`, "-C", "/source", "."],
|
|
50
|
+
HostConfig: {
|
|
51
|
+
Binds: [`${volumeName}:/source:ro`, `${this.backupDir}:/backup`],
|
|
52
|
+
AutoRemove: true,
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
await container.start();
|
|
58
|
+
const result = await container.wait();
|
|
59
|
+
if (result.StatusCode !== 0) {
|
|
60
|
+
throw new Error(`Snapshot container exited with code ${result.StatusCode}`);
|
|
61
|
+
}
|
|
62
|
+
} catch (err) {
|
|
63
|
+
// AutoRemove handles cleanup, but if start failed the container may still exist
|
|
64
|
+
try {
|
|
65
|
+
await container.remove({ force: true });
|
|
66
|
+
} catch {
|
|
67
|
+
// already removed by AutoRemove
|
|
68
|
+
}
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const info = await stat(archivePath);
|
|
73
|
+
|
|
74
|
+
const snapshot: VolumeSnapshot = {
|
|
75
|
+
id,
|
|
76
|
+
volumeName,
|
|
77
|
+
archivePath,
|
|
78
|
+
createdAt: new Date(),
|
|
79
|
+
sizeBytes: info.size,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
logger.info(`Volume snapshot created: ${id} (${info.size} bytes)`);
|
|
83
|
+
return snapshot;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Restore a volume from a snapshot */
|
|
87
|
+
async restore(snapshotId: string): Promise<void> {
|
|
88
|
+
validateSnapshotId(snapshotId);
|
|
89
|
+
const archivePath = join(this.backupDir, `${snapshotId}.tar`);
|
|
90
|
+
|
|
91
|
+
// Verify archive exists
|
|
92
|
+
await stat(archivePath);
|
|
93
|
+
|
|
94
|
+
// Extract volume name from snapshot ID (everything before the last ISO timestamp)
|
|
95
|
+
const volumeName = this.extractVolumeName(snapshotId);
|
|
96
|
+
|
|
97
|
+
const container = await this.docker.createContainer({
|
|
98
|
+
Image: ALPINE_IMAGE,
|
|
99
|
+
Cmd: ["sh", "-c", `cd /target && rm -rf ./* ./.??* && tar xf /backup/${snapshotId}.tar -C /target`],
|
|
100
|
+
HostConfig: {
|
|
101
|
+
Binds: [`${volumeName}:/target`, `${this.backupDir}:/backup:ro`],
|
|
102
|
+
AutoRemove: true,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
await container.start();
|
|
108
|
+
const result = await container.wait();
|
|
109
|
+
if (result.StatusCode !== 0) {
|
|
110
|
+
throw new Error(`Restore container exited with code ${result.StatusCode}`);
|
|
111
|
+
}
|
|
112
|
+
} catch (err) {
|
|
113
|
+
try {
|
|
114
|
+
await container.remove({ force: true });
|
|
115
|
+
} catch {
|
|
116
|
+
// already removed by AutoRemove
|
|
117
|
+
}
|
|
118
|
+
throw err;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
logger.info(`Volume restored from snapshot: ${snapshotId}`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** List all snapshots for a volume */
|
|
125
|
+
async list(volumeName: string): Promise<VolumeSnapshot[]> {
|
|
126
|
+
let files: string[];
|
|
127
|
+
try {
|
|
128
|
+
files = await readdir(this.backupDir);
|
|
129
|
+
} catch {
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const prefix = `${volumeName}-`;
|
|
134
|
+
const matching = files.filter((f) => f.startsWith(prefix) && f.endsWith(".tar"));
|
|
135
|
+
|
|
136
|
+
const snapshots: VolumeSnapshot[] = [];
|
|
137
|
+
for (const file of matching) {
|
|
138
|
+
const id = file.replace(/\.tar$/, "");
|
|
139
|
+
const archivePath = join(this.backupDir, file);
|
|
140
|
+
try {
|
|
141
|
+
const info = await stat(archivePath);
|
|
142
|
+
snapshots.push({
|
|
143
|
+
id,
|
|
144
|
+
volumeName,
|
|
145
|
+
archivePath,
|
|
146
|
+
createdAt: info.mtime,
|
|
147
|
+
sizeBytes: info.size,
|
|
148
|
+
});
|
|
149
|
+
} catch {
|
|
150
|
+
// File disappeared between readdir and stat — skip
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Sort newest first
|
|
155
|
+
snapshots.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
156
|
+
return snapshots;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Delete a snapshot archive */
|
|
160
|
+
async delete(snapshotId: string): Promise<void> {
|
|
161
|
+
validateSnapshotId(snapshotId);
|
|
162
|
+
const archivePath = join(this.backupDir, `${snapshotId}.tar`);
|
|
163
|
+
await rm(archivePath, { force: true });
|
|
164
|
+
logger.info(`Volume snapshot deleted: ${snapshotId}`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Delete all snapshots older than maxAge ms */
|
|
168
|
+
async cleanup(maxAgeMs: number): Promise<number> {
|
|
169
|
+
let files: string[];
|
|
170
|
+
try {
|
|
171
|
+
files = await readdir(this.backupDir);
|
|
172
|
+
} catch {
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const cutoff = Date.now() - maxAgeMs;
|
|
177
|
+
let deleted = 0;
|
|
178
|
+
|
|
179
|
+
for (const file of files) {
|
|
180
|
+
if (!file.endsWith(".tar")) continue;
|
|
181
|
+
const archivePath = join(this.backupDir, file);
|
|
182
|
+
try {
|
|
183
|
+
const info = await stat(archivePath);
|
|
184
|
+
if (info.mtime.getTime() < cutoff) {
|
|
185
|
+
await rm(archivePath, { force: true });
|
|
186
|
+
deleted++;
|
|
187
|
+
}
|
|
188
|
+
} catch {
|
|
189
|
+
// File disappeared — skip
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (deleted > 0) {
|
|
194
|
+
logger.info(`Volume snapshot cleanup: removed ${deleted} old snapshots`);
|
|
195
|
+
}
|
|
196
|
+
return deleted;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Extract volume name from snapshot ID.
|
|
201
|
+
* Snapshot IDs are `${volumeName}-${ISO timestamp with colons/dots replaced}`.
|
|
202
|
+
* ISO timestamps start with 4 digits (year), so we find the last occurrence
|
|
203
|
+
* of `-YYYY` pattern to split.
|
|
204
|
+
*/
|
|
205
|
+
private extractVolumeName(snapshotId: string): string {
|
|
206
|
+
// Match the timestamp part: -YYYY-MM-DDTHH-MM-SS-MMMZ
|
|
207
|
+
const match = snapshotId.match(/^(.+)-\d{4}-\d{2}-\d{2}T/);
|
|
208
|
+
if (!match) {
|
|
209
|
+
throw new Error(`Cannot extract volume name from snapshot ID: ${snapshotId}`);
|
|
210
|
+
}
|
|
211
|
+
return match[1];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -238,7 +238,10 @@ describe("rollbackPluginOnVolume", () => {
|
|
|
238
238
|
});
|
|
239
239
|
|
|
240
240
|
describe("npm package validation", () => {
|
|
241
|
-
const validExec = vi.fn(
|
|
241
|
+
const validExec = vi.fn(
|
|
242
|
+
(_cmd: unknown, _args: unknown, _opts: unknown, cb: (err: unknown, stdout: string, stderr: string) => void) =>
|
|
243
|
+
cb(null, "", ""),
|
|
244
|
+
);
|
|
242
245
|
|
|
243
246
|
beforeEach(() => {
|
|
244
247
|
validExec.mockClear();
|
|
@@ -350,7 +353,10 @@ describe("npm package validation", () => {
|
|
|
350
353
|
});
|
|
351
354
|
|
|
352
355
|
it("passes -- separator before package name to prevent flag injection", async () => {
|
|
353
|
-
const execFn = vi.fn(
|
|
356
|
+
const execFn = vi.fn(
|
|
357
|
+
(_cmd: unknown, _args: unknown, _opts: unknown, cb: (err: unknown, stdout: string, stderr: string) => void) =>
|
|
358
|
+
cb(null, "", ""),
|
|
359
|
+
);
|
|
354
360
|
await installPluginToVolume({
|
|
355
361
|
pluginId: "p1",
|
|
356
362
|
npmPackage: "lodash",
|