@open-wa/session-sync 5.2.0 → 5.3.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/index.cjs +204 -8
- package/dist/index.d.cts +23 -3
- package/dist/index.d.cts.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,8 @@ let path = require("path");
|
|
|
31
31
|
path = __toESM(path);
|
|
32
32
|
let events = require("events");
|
|
33
33
|
let stream = require("stream");
|
|
34
|
+
let pico_s3 = require("pico-s3");
|
|
35
|
+
let node_crypto = require("node:crypto");
|
|
34
36
|
//#region src/local-compression.ts
|
|
35
37
|
const zstd$1 = require("simple-zstd");
|
|
36
38
|
var LocalSessionCompression = class extends events.EventEmitter {
|
|
@@ -115,20 +117,214 @@ var LocalSessionCompression = class extends events.EventEmitter {
|
|
|
115
117
|
}
|
|
116
118
|
};
|
|
117
119
|
//#endregion
|
|
120
|
+
//#region src/s3-options.ts
|
|
121
|
+
/** Use the same PicoS3 provider vocabulary for archives and portable objects. */
|
|
122
|
+
function toS3Options(config) {
|
|
123
|
+
const endpoint = config.endpoint || config.url;
|
|
124
|
+
let host = endpoint || config.host;
|
|
125
|
+
let provider = (endpoint ? "MINIO" : config.provider || (host ? "MINIO" : "AWS")).toUpperCase();
|
|
126
|
+
if (provider === "R2_ALT") provider = "MINIO";
|
|
127
|
+
if (!Object.values(pico_s3.CLOUD_PROVIDERS).includes(provider) || !config.bucket || !config.accessKeyId || !config.secretAccessKey) throw new Error("SESSION_S3_CONFIG_INVALID");
|
|
128
|
+
if (host) {
|
|
129
|
+
let parsed;
|
|
130
|
+
try {
|
|
131
|
+
parsed = new URL(host.includes("://") ? host : `https://${host}`);
|
|
132
|
+
} catch {
|
|
133
|
+
throw new Error("SESSION_S3_ENDPOINT_INVALID");
|
|
134
|
+
}
|
|
135
|
+
if (!["https:", "http:"].includes(parsed.protocol) || parsed.username || parsed.password || parsed.search || parsed.hash || parsed.pathname !== "/") throw new Error("SESSION_S3_ENDPOINT_INVALID");
|
|
136
|
+
host = parsed.origin;
|
|
137
|
+
}
|
|
138
|
+
if ([
|
|
139
|
+
"MINIO",
|
|
140
|
+
"R2",
|
|
141
|
+
"R2_ALT"
|
|
142
|
+
].includes(provider) && !host) throw new Error("SESSION_S3_ENDPOINT_REQUIRED");
|
|
143
|
+
return {
|
|
144
|
+
provider,
|
|
145
|
+
bucket: config.bucket,
|
|
146
|
+
region: config.region || (config.provider?.toUpperCase().startsWith("R2") ? "auto" : "us-east-1"),
|
|
147
|
+
accessKeyId: config.accessKeyId,
|
|
148
|
+
secretAccessKey: config.secretAccessKey,
|
|
149
|
+
host,
|
|
150
|
+
headers: config.headers
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/session-file.ts
|
|
155
|
+
var SessionStoreError = class extends Error {
|
|
156
|
+
constructor(message, status = 503) {
|
|
157
|
+
super(message);
|
|
158
|
+
this.status = status;
|
|
159
|
+
this.name = "OpenWAError";
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const limit = 67108864;
|
|
163
|
+
const logoutPrefix = "OPENWA_SESSION_LOGGED_OUT:";
|
|
164
|
+
const keyPart = (value) => {
|
|
165
|
+
if (!value || value === "." || value === ".." || /[\x00-\x1f\x7f\\]/.test(value)) throw new SessionStoreError("SESSION_S3_KEY_INVALID", 400);
|
|
166
|
+
return encodeURIComponent(value).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
167
|
+
};
|
|
168
|
+
/** PicoS3 transport, with a non-expiring conditional lock and versioned object replacement. */
|
|
169
|
+
async function acquireSessionFile(config, filename) {
|
|
170
|
+
try {
|
|
171
|
+
return await acquire(config, filename);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
throw error instanceof SessionStoreError ? error : new SessionStoreError("SESSION_S3_ACQUIRE_FAILED");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
async function acquire(config, filename) {
|
|
177
|
+
const options = toS3Options(config);
|
|
178
|
+
const directory = (config.directory ?? "_sessionData").replace(/^\/+|\/+$/g, "");
|
|
179
|
+
const encodedDirectory = directory ? directory.split("/").map(keyPart).join("/") : void 0;
|
|
180
|
+
const encodedFilename = filename.split("/").map(keyPart).join("/");
|
|
181
|
+
const extraHeaders = {};
|
|
182
|
+
for (const [key, value] of Object.entries(config.headers ?? {})) {
|
|
183
|
+
if (!/^x-amz-(server-side-encryption(?:-.+)?|security-token|expected-bucket-owner|request-payer|storage-class)$/i.test(key) || typeof value !== "string" || /[\r\n]/.test(value)) throw new SessionStoreError("SESSION_S3_HEADER_INVALID", 400);
|
|
184
|
+
extraHeaders[key.toLowerCase()] = value;
|
|
185
|
+
}
|
|
186
|
+
if (config.sessionToken) extraHeaders["x-amz-security-token"] = config.sessionToken;
|
|
187
|
+
const readHeaders = Object.fromEntries(Object.entries(extraHeaders).filter(([key]) => key.startsWith("x-amz-server-side-encryption-customer-") || [
|
|
188
|
+
"x-amz-security-token",
|
|
189
|
+
"x-amz-expected-bucket-owner",
|
|
190
|
+
"x-amz-request-payer"
|
|
191
|
+
].includes(key)));
|
|
192
|
+
const request = async (name, method, body, etag) => {
|
|
193
|
+
const object = {
|
|
194
|
+
...options,
|
|
195
|
+
filename: name,
|
|
196
|
+
directory: encodedDirectory
|
|
197
|
+
};
|
|
198
|
+
const url = new URL((0, pico_s3.getProviderConfig)(options.provider).url(object));
|
|
199
|
+
if (options.provider === "AWS") url.hostname = `${options.bucket}.s3.${options.region}.amazonaws.com`;
|
|
200
|
+
const data = body === void 0 ? void 0 : Buffer.from(body, "utf8");
|
|
201
|
+
try {
|
|
202
|
+
const response = await (0, pico_s3.s3Request)(object, {
|
|
203
|
+
method,
|
|
204
|
+
url: url.toString(),
|
|
205
|
+
host: url.host,
|
|
206
|
+
path: url.pathname,
|
|
207
|
+
data,
|
|
208
|
+
responseType: "arraybuffer",
|
|
209
|
+
timeout: 3e4,
|
|
210
|
+
maxRedirects: 0,
|
|
211
|
+
maxContentLength: limit,
|
|
212
|
+
maxBodyLength: limit,
|
|
213
|
+
validateStatus: () => true,
|
|
214
|
+
headers: {
|
|
215
|
+
...method === "PUT" ? extraHeaders : readHeaders,
|
|
216
|
+
"x-amz-content-sha256": (0, node_crypto.createHash)("sha256").update(data ?? "").digest("hex"),
|
|
217
|
+
...data ? {
|
|
218
|
+
"Content-Type": body.startsWith("{") ? "application/json" : "text/plain",
|
|
219
|
+
"Content-Length": String(data.length)
|
|
220
|
+
} : {},
|
|
221
|
+
...etag === null ? { "If-None-Match": "*" } : etag ? { "If-Match": etag } : {}
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
if (method === "GET" && response.status === 404) return null;
|
|
225
|
+
if (response.status === 409 || response.status === 412) throw new SessionStoreError("SESSION_S3_CONFLICT", 409);
|
|
226
|
+
if (response.status !== 200) throw new SessionStoreError("SESSION_S3_REQUEST_FAILED");
|
|
227
|
+
const version = response.headers.etag;
|
|
228
|
+
if (typeof version !== "string" || !/^"[^"\r\n]+"$/.test(version)) throw new SessionStoreError("SESSION_S3_ETAG_REQUIRED");
|
|
229
|
+
return {
|
|
230
|
+
etag: version,
|
|
231
|
+
body: method === "GET" ? Buffer.from(response.data).toString("utf8") : body
|
|
232
|
+
};
|
|
233
|
+
} catch (error) {
|
|
234
|
+
throw error instanceof SessionStoreError ? error : new SessionStoreError("SESSION_S3_REQUEST_FAILED");
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const lockName = `${encodedFilename}.lock`;
|
|
238
|
+
const owner = (0, node_crypto.randomUUID)();
|
|
239
|
+
const previous = await request(lockName, "GET");
|
|
240
|
+
if (previous) {
|
|
241
|
+
let value;
|
|
242
|
+
try {
|
|
243
|
+
value = JSON.parse(previous.body);
|
|
244
|
+
} catch {
|
|
245
|
+
throw new SessionStoreError("SESSION_S3_LOCK_INVALID");
|
|
246
|
+
}
|
|
247
|
+
if (!value || value.owner !== null || typeof value.nonce !== "string") throw new SessionStoreError("SESSION_S3_LOCKED", 409);
|
|
248
|
+
}
|
|
249
|
+
const lock = await request(lockName, "PUT", JSON.stringify({
|
|
250
|
+
owner,
|
|
251
|
+
nonce: (0, node_crypto.randomUUID)()
|
|
252
|
+
}), previous?.etag ?? null);
|
|
253
|
+
if (!lock) throw new SessionStoreError("SESSION_S3_LOCK_FAILED");
|
|
254
|
+
let closed = false;
|
|
255
|
+
let busy = false;
|
|
256
|
+
let uncertain = false;
|
|
257
|
+
let releaseResult;
|
|
258
|
+
let current;
|
|
259
|
+
try {
|
|
260
|
+
current = await request(encodedFilename, "GET");
|
|
261
|
+
} catch (error) {
|
|
262
|
+
try {
|
|
263
|
+
await request(lockName, "PUT", JSON.stringify({
|
|
264
|
+
owner: null,
|
|
265
|
+
nonce: (0, node_crypto.randomUUID)()
|
|
266
|
+
}), lock.etag);
|
|
267
|
+
} catch {}
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
const snapshot = () => ({
|
|
271
|
+
exists: current !== null,
|
|
272
|
+
payload: current === null || current.body === "LOGGED OUT" || current.body.startsWith(logoutPrefix) ? null : current.body
|
|
273
|
+
});
|
|
274
|
+
const available = () => {
|
|
275
|
+
if (closed || uncertain) throw new SessionStoreError("SESSION_S3_CLOSED");
|
|
276
|
+
if (busy) throw new SessionStoreError("SESSION_S3_BUSY", 409);
|
|
277
|
+
};
|
|
278
|
+
const replace = async (body) => {
|
|
279
|
+
available();
|
|
280
|
+
if (typeof body !== "string" || Buffer.byteLength(body) > limit) throw new SessionStoreError("SESSION_S3_PAYLOAD_INVALID", 400);
|
|
281
|
+
busy = true;
|
|
282
|
+
try {
|
|
283
|
+
current = await request(encodedFilename, "PUT", body, current?.etag ?? null);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
uncertain = true;
|
|
286
|
+
throw error;
|
|
287
|
+
} finally {
|
|
288
|
+
busy = false;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
return {
|
|
292
|
+
read: async () => {
|
|
293
|
+
available();
|
|
294
|
+
return snapshot();
|
|
295
|
+
},
|
|
296
|
+
write: replace,
|
|
297
|
+
remove: () => replace(`${logoutPrefix}${(0, node_crypto.randomUUID)()}`),
|
|
298
|
+
release: () => {
|
|
299
|
+
if (releaseResult) return releaseResult;
|
|
300
|
+
try {
|
|
301
|
+
available();
|
|
302
|
+
} catch (error) {
|
|
303
|
+
return Promise.reject(error);
|
|
304
|
+
}
|
|
305
|
+
closed = true;
|
|
306
|
+
releaseResult = request(lockName, "PUT", JSON.stringify({
|
|
307
|
+
owner: null,
|
|
308
|
+
nonce: (0, node_crypto.randomUUID)()
|
|
309
|
+
}), lock.etag).then(() => void 0, () => {
|
|
310
|
+
throw new SessionStoreError("SESSION_S3_RELEASE_FAILED");
|
|
311
|
+
});
|
|
312
|
+
return releaseResult;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
//#endregion
|
|
118
317
|
//#region src/s3-sync.ts
|
|
119
318
|
const PicoS3 = require("pico-s3");
|
|
120
319
|
const zstd = require("simple-zstd");
|
|
121
320
|
var S3SyncManager = class {
|
|
122
321
|
constructor(config) {
|
|
123
322
|
this.config = config;
|
|
124
|
-
this.p3 = new PicoS3.PicoS3(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
endpoint: config.endpoint || config.host || config.url,
|
|
130
|
-
host: config.host
|
|
131
|
-
});
|
|
323
|
+
this.p3 = new PicoS3.PicoS3(toS3Options(config));
|
|
324
|
+
}
|
|
325
|
+
/** Opaque JSON persistence; uses the existing PicoS3 transport without profile compression. */
|
|
326
|
+
acquireSessionFile(filename) {
|
|
327
|
+
return acquireSessionFile(this.config, filename);
|
|
132
328
|
}
|
|
133
329
|
async backupSession(sessionZstPath, remoteFilename) {
|
|
134
330
|
const filename = remoteFilename || path.default.basename(sessionZstPath);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from "events";
|
|
2
|
+
import "pico-s3";
|
|
2
3
|
//#region src/local-compression.d.ts
|
|
3
4
|
interface CompressionOptions {
|
|
4
5
|
sessionPath: string;
|
|
@@ -19,20 +20,39 @@ export declare class LocalSessionCompression extends EventEmitter {
|
|
|
19
20
|
compress(): Promise<void>;
|
|
20
21
|
}
|
|
21
22
|
//#endregion
|
|
22
|
-
//#region src/s3-
|
|
23
|
+
//#region src/s3-options.d.ts
|
|
23
24
|
interface S3Config {
|
|
24
25
|
bucket: string;
|
|
25
|
-
region
|
|
26
|
+
region?: string;
|
|
26
27
|
accessKeyId: string;
|
|
27
28
|
secretAccessKey: string;
|
|
29
|
+
provider?: string;
|
|
28
30
|
endpoint?: string;
|
|
29
31
|
host?: string;
|
|
30
32
|
url?: string;
|
|
33
|
+
directory?: string;
|
|
34
|
+
headers?: Record<string, string>;
|
|
35
|
+
sessionToken?: string;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/session-file.d.ts
|
|
39
|
+
interface SessionFileSnapshot {
|
|
40
|
+
exists: boolean;
|
|
41
|
+
payload: string | null;
|
|
31
42
|
}
|
|
43
|
+
interface SessionFileLease {
|
|
44
|
+
read(): Promise<SessionFileSnapshot>;
|
|
45
|
+
write(payload: string): Promise<void>;
|
|
46
|
+
remove(): Promise<void>;
|
|
47
|
+
release(): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/s3-sync.d.ts
|
|
32
51
|
export declare class S3SyncManager {
|
|
33
52
|
private p3;
|
|
34
53
|
private config;
|
|
35
54
|
constructor(config: S3Config);
|
|
55
|
+
acquireSessionFile(filename: string): Promise<SessionFileLease>;
|
|
36
56
|
backupSession(sessionZstPath: string, remoteFilename?: string): Promise<string>;
|
|
37
57
|
restoreSession(filename: string, targetPath: string): Promise<boolean>;
|
|
38
58
|
getDownloadUrl(filename: string, _expiresIn?: number): Promise<string>;
|
|
@@ -40,5 +60,5 @@ export declare class S3SyncManager {
|
|
|
40
60
|
deleteSession(filename: string): Promise<boolean>;
|
|
41
61
|
}
|
|
42
62
|
//#endregion
|
|
43
|
-
export type { CompressionOptions, S3Config };
|
|
63
|
+
export type { CompressionOptions, S3Config, SessionFileLease, SessionFileSnapshot };
|
|
44
64
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/local-compression.ts","../src/s3-sync.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/local-compression.ts","../src/s3-options.ts","../src/session-file.ts","../src/s3-sync.ts"],"mappings":";;;UAOiB;EACb;EACA;EACA;EACA;;qBAGS,gCAAgC;UACjC;UACA;UACA;UACA;UACA;EAER,YAAY,QAAQ;EAMb;EAqBA;UAWC;EAoBK,YAAQ;;;;UC7ER;EACb;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,UAAU;EACV;;;;UCTa;EAAsB;EAAiB;;UACvC;EACb,QAAQ,QAAQ;EAChB,MAAM,kBAAkB;EACxB,UAAU;EACV,WAAW;;;;qBCCF;UACD;UACA;EAER,YAAY,QAAQ;EAMb,mBAAmB,mBAAmB,QAAQ;EAIxC,cAAc,wBAAwB,0BAA0B;EAoBhE,eAAe,kBAAkB,qBAAqB;EA2CtD,eAAe,kBAAkB,sBAA0B;EAU3D,cAAc,mBAAmB;EAWjC,cAAc,mBAAmB"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-wa/session-sync",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"main": "dist/index.cjs",
|
|
5
5
|
"types": "dist/index.d.cts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@open-wa/schema": "5.
|
|
7
|
+
"@open-wa/schema": "5.3.0",
|
|
8
8
|
"chokidar": "^5.0.0",
|
|
9
9
|
"pico-s3": "^2.11.0",
|
|
10
10
|
"simple-zstd": "^2.1.0",
|