@platformatic/sqlite-js-stream-s3 0.0.0-stage → 0.1.4
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 +202 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/leaser.d.ts +30 -0
- package/dist/leaser.d.ts.map +1 -0
- package/dist/leaser.js +176 -0
- package/dist/replica-client.d.ts +70 -0
- package/dist/replica-client.d.ts.map +1 -0
- package/dist/replica-client.js +316 -0
- package/package.json +47 -3
- package/src/index.ts +23 -0
- package/src/leaser.ts +202 -0
- package/src/replica-client.ts +414 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @platformatic/sqlite-js-stream-s3 — Amazon S3 upload strategy.
|
|
3
|
+
*
|
|
4
|
+
* Importing this module registers the `s3` scheme with the core's replica URL
|
|
5
|
+
* factory, so `newReplicaClientFromURL('s3://bucket/prefix')` resolves after
|
|
6
|
+
* this addon is loaded.
|
|
7
|
+
*/
|
|
8
|
+
export { S3ReplicaClient, newReplicaClientFromURL, ReplicaClientType, type S3ClientLike, type S3ReplicaClientOptions, } from './replica-client.ts';
|
|
9
|
+
export { S3Leaser } from './leaser.ts';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,iBAAiB,EACjB,KAAK,YAAY,EACjB,KAAK,sBAAsB,GAC5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @platformatic/sqlite-js-stream-s3 — Amazon S3 upload strategy.
|
|
3
|
+
*
|
|
4
|
+
* Importing this module registers the `s3` scheme with the core's replica URL
|
|
5
|
+
* factory, so `newReplicaClientFromURL('s3://bucket/prefix')` resolves after
|
|
6
|
+
* this addon is loaded.
|
|
7
|
+
*/
|
|
8
|
+
export { S3ReplicaClient, newReplicaClientFromURL, ReplicaClientType, } from "./replica-client.js";
|
|
9
|
+
export { S3Leaser } from "./leaser.js";
|
|
10
|
+
import { registerReplicaClientFactory } from '@platformatic/sqlite-js-stream';
|
|
11
|
+
import { newReplicaClientFromURL } from "./replica-client.js";
|
|
12
|
+
// Register the 's3' URL scheme when the addon is imported.
|
|
13
|
+
registerReplicaClientFactory('s3', newReplicaClientFromURL);
|
package/dist/leaser.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S3 Leaser — distributed lease backed by an S3 `lock.json` object.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors litestream's `s3/leaser.go`. Acquire uses `If-None-Match: *` when no
|
|
5
|
+
* lease exists (and `If-Match: <etag>` when replacing an expired lease), so two
|
|
6
|
+
* racing acquirers cannot both win; one gets `412 PreconditionFailed`.
|
|
7
|
+
*/
|
|
8
|
+
import { Lease, type Leaser } from '@platformatic/sqlite-js-stream';
|
|
9
|
+
import type { S3ClientLike } from './replica-client.ts';
|
|
10
|
+
/** S3Leaser acquires/renews/releases a lease object in an S3 bucket. */
|
|
11
|
+
export declare class S3Leaser implements Leaser {
|
|
12
|
+
readonly bucket: string;
|
|
13
|
+
readonly prefix: string;
|
|
14
|
+
readonly key: string;
|
|
15
|
+
private client;
|
|
16
|
+
private owner;
|
|
17
|
+
private ttlMs;
|
|
18
|
+
constructor(opts: {
|
|
19
|
+
bucket: string;
|
|
20
|
+
prefix?: string;
|
|
21
|
+
client: S3ClientLike;
|
|
22
|
+
owner?: string;
|
|
23
|
+
ttlMs?: number;
|
|
24
|
+
});
|
|
25
|
+
acquireLease(): Promise<Lease>;
|
|
26
|
+
renewLease(lease: Lease): Promise<Lease>;
|
|
27
|
+
releaseLease(lease: Lease): Promise<void>;
|
|
28
|
+
private readLease;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=leaser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leaser.d.ts","sourceRoot":"","sources":["../src/leaser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,OAAO,EAIL,KAAK,EAEL,KAAK,MAAM,EACZ,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAwDxD,wEAAwE;AACxE,qBAAa,QAAS,YAAW,MAAM;IACrC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,KAAK,CAAS;gBAEV,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,YAAY,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE;IAUrG,YAAY,IAAI,OAAO,CAAC,KAAK,CAAC;IAuC9B,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IA2BxC,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;YAiBjC,SAAS;CAoBxB"}
|
package/dist/leaser.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S3 Leaser — distributed lease backed by an S3 `lock.json` object.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors litestream's `s3/leaser.go`. Acquire uses `If-None-Match: *` when no
|
|
5
|
+
* lease exists (and `If-Match: <etag>` when replacing an expired lease), so two
|
|
6
|
+
* racing acquirers cannot both win; one gets `412 PreconditionFailed`.
|
|
7
|
+
*/
|
|
8
|
+
import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand, } from '@aws-sdk/client-s3';
|
|
9
|
+
import { DefaultLeasePath, DefaultLeaseTTL, ErrLeaseNotHeld, Lease, LeaseExistsError, } from '@platformatic/sqlite-js-stream';
|
|
10
|
+
/** Reads all bytes from an S3 body (stream or Buffer). */
|
|
11
|
+
async function readBody(body) {
|
|
12
|
+
// AWS SDK v3 returns a mix; normalize to a Uint8Array.
|
|
13
|
+
if (body instanceof Uint8Array)
|
|
14
|
+
return body;
|
|
15
|
+
if (typeof body?.on === 'function') {
|
|
16
|
+
// Node stream.
|
|
17
|
+
const chunks = [];
|
|
18
|
+
for await (const chunk of body) {
|
|
19
|
+
chunks.push(typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk);
|
|
20
|
+
}
|
|
21
|
+
let total = 0;
|
|
22
|
+
for (const c of chunks)
|
|
23
|
+
total += c.length;
|
|
24
|
+
const out = new Uint8Array(total);
|
|
25
|
+
let o = 0;
|
|
26
|
+
for (const c of chunks) {
|
|
27
|
+
out.set(c, o);
|
|
28
|
+
o += c.length;
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
// Web ReadableStream or Buffer.
|
|
33
|
+
if (typeof body?.getReader === 'function') {
|
|
34
|
+
const reader = body.getReader();
|
|
35
|
+
const chunks = [];
|
|
36
|
+
for (;;) {
|
|
37
|
+
const { done, value } = await reader.read();
|
|
38
|
+
if (done)
|
|
39
|
+
break;
|
|
40
|
+
chunks.push(value);
|
|
41
|
+
}
|
|
42
|
+
let total = 0;
|
|
43
|
+
for (const c of chunks)
|
|
44
|
+
total += c.length;
|
|
45
|
+
const out = new Uint8Array(total);
|
|
46
|
+
let o = 0;
|
|
47
|
+
for (const c of chunks) {
|
|
48
|
+
out.set(c, o);
|
|
49
|
+
o += c.length;
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
return new Uint8Array(body);
|
|
54
|
+
}
|
|
55
|
+
function isPreconditionFailed(err) {
|
|
56
|
+
const code = err?.$metadata?.httpStatusCode;
|
|
57
|
+
const name = err?.name ?? '';
|
|
58
|
+
return code === 412 || name === 'PreconditionFailed';
|
|
59
|
+
}
|
|
60
|
+
function isNotFound(err) {
|
|
61
|
+
const code = err?.$metadata?.httpStatusCode;
|
|
62
|
+
const name = err?.name ?? '';
|
|
63
|
+
return code === 404 || name === 'NoSuchKey' || name === 'NotFound';
|
|
64
|
+
}
|
|
65
|
+
/** S3Leaser acquires/renews/releases a lease object in an S3 bucket. */
|
|
66
|
+
export class S3Leaser {
|
|
67
|
+
bucket;
|
|
68
|
+
prefix;
|
|
69
|
+
key;
|
|
70
|
+
client;
|
|
71
|
+
owner;
|
|
72
|
+
ttlMs;
|
|
73
|
+
constructor(opts) {
|
|
74
|
+
if (!opts.bucket)
|
|
75
|
+
throw new Error('S3 leaser: bucket is required');
|
|
76
|
+
this.bucket = opts.bucket;
|
|
77
|
+
this.prefix = opts.prefix?.replace(/^\/+/, '').replace(/\/+$/, '') ?? '';
|
|
78
|
+
this.key = this.prefix ? `${this.prefix}/${DefaultLeasePath}` : DefaultLeasePath;
|
|
79
|
+
this.client = opts.client;
|
|
80
|
+
this.owner = opts.owner ?? `${process.pid}`;
|
|
81
|
+
this.ttlMs = opts.ttlMs ?? DefaultLeaseTTL;
|
|
82
|
+
}
|
|
83
|
+
async acquireLease() {
|
|
84
|
+
const existing = await this.readLease();
|
|
85
|
+
if (existing !== null && !existing.isExpired()) {
|
|
86
|
+
throw new LeaseExistsError(existing.owner, existing.expiresAt);
|
|
87
|
+
}
|
|
88
|
+
let generation = 1;
|
|
89
|
+
if (existing !== null)
|
|
90
|
+
generation = existing.generation + 1;
|
|
91
|
+
const lease = new Lease(generation, Date.now() + this.ttlMs, this.owner);
|
|
92
|
+
const data = new TextEncoder().encode(JSON.stringify({
|
|
93
|
+
generation: lease.generation,
|
|
94
|
+
expires_at: new Date(lease.expiresAt).toISOString(),
|
|
95
|
+
owner: lease.owner,
|
|
96
|
+
}));
|
|
97
|
+
try {
|
|
98
|
+
await this.client.send(new PutObjectCommand({
|
|
99
|
+
Bucket: this.bucket,
|
|
100
|
+
Key: this.key,
|
|
101
|
+
Body: data,
|
|
102
|
+
ContentType: 'application/json',
|
|
103
|
+
...(existing === null ? { IfNoneMatch: '*' } : { IfMatch: existing.etag ?? '' }),
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
if (isPreconditionFailed(err)) {
|
|
108
|
+
const current = await this.readLease();
|
|
109
|
+
if (current !== null)
|
|
110
|
+
throw new LeaseExistsError(current.owner, current.expiresAt);
|
|
111
|
+
}
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
return (await this.readLease()) ?? lease;
|
|
115
|
+
}
|
|
116
|
+
async renewLease(lease) {
|
|
117
|
+
if (lease.etag == null)
|
|
118
|
+
throw new ErrLeaseNotHeld();
|
|
119
|
+
const renewed = new Lease(lease.generation, Date.now() + this.ttlMs, lease.owner);
|
|
120
|
+
const data = new TextEncoder().encode(JSON.stringify({
|
|
121
|
+
generation: renewed.generation,
|
|
122
|
+
expires_at: new Date(renewed.expiresAt).toISOString(),
|
|
123
|
+
owner: renewed.owner,
|
|
124
|
+
}));
|
|
125
|
+
try {
|
|
126
|
+
await this.client.send(new PutObjectCommand({
|
|
127
|
+
Bucket: this.bucket,
|
|
128
|
+
Key: this.key,
|
|
129
|
+
Body: data,
|
|
130
|
+
ContentType: 'application/json',
|
|
131
|
+
IfMatch: lease.etag,
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
if (isPreconditionFailed(err))
|
|
136
|
+
throw new ErrLeaseNotHeld();
|
|
137
|
+
throw err;
|
|
138
|
+
}
|
|
139
|
+
return (await this.readLease()) ?? renewed;
|
|
140
|
+
}
|
|
141
|
+
async releaseLease(lease) {
|
|
142
|
+
if (lease.etag == null)
|
|
143
|
+
throw new ErrLeaseNotHeld();
|
|
144
|
+
try {
|
|
145
|
+
await this.client.send(new DeleteObjectCommand({
|
|
146
|
+
Bucket: this.bucket,
|
|
147
|
+
Key: this.key,
|
|
148
|
+
IfMatch: lease.etag,
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
if (isNotFound(err))
|
|
153
|
+
return; // already released
|
|
154
|
+
if (isPreconditionFailed(err))
|
|
155
|
+
throw new ErrLeaseNotHeld();
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async readLease() {
|
|
160
|
+
try {
|
|
161
|
+
const res = await this.client.send(new GetObjectCommand({ Bucket: this.bucket, Key: this.key }));
|
|
162
|
+
const data = await readBody(res.Body);
|
|
163
|
+
const parsed = JSON.parse(new TextDecoder().decode(data));
|
|
164
|
+
const expiresAt = Date.parse(parsed.expires_at ?? '');
|
|
165
|
+
if (Number.isNaN(expiresAt))
|
|
166
|
+
throw new Error('invalid lease file: bad expires_at');
|
|
167
|
+
const etag = typeof res.ETag === 'string' ? res.ETag : null;
|
|
168
|
+
return new Lease(parsed.generation ?? 1, expiresAt, parsed.owner ?? '', etag);
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
if (isNotFound(err))
|
|
172
|
+
return null;
|
|
173
|
+
throw err;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S3 replica client — replicates LTX files to an Amazon S3 bucket.
|
|
3
|
+
*
|
|
4
|
+
* Object layout mirrors the file backend:
|
|
5
|
+
* `{prefix}ltx/{level}/{minTXID}-{maxTXID}.ltx`
|
|
6
|
+
*
|
|
7
|
+
* The implementation talks to S3 through the AWS SDK v3 {@link S3Client}. A
|
|
8
|
+
* minimal `send`-shaped client can be injected (e.g. an in-memory fake in
|
|
9
|
+
* tests) so the upload strategy is testable without network access.
|
|
10
|
+
*
|
|
11
|
+
* Creation timestamps are preserved on upload as the `ltx-timestamp` object
|
|
12
|
+
* metadata; listing falls back to the object's `LastModified`.
|
|
13
|
+
*/
|
|
14
|
+
import { Readable } from 'node:stream';
|
|
15
|
+
import { type FileInfo, type Logger, type LTXFilesOptions, type OpenLTXFileOptions, type ReplicaClient, type WriteLTXFileOptions } from '@platformatic/sqlite-js-stream';
|
|
16
|
+
/** The strategy identifier reported by {@link S3ReplicaClient.type}. */
|
|
17
|
+
export declare const ReplicaClientType = "s3";
|
|
18
|
+
/**
|
|
19
|
+
* Minimal shape of a client used to send commands. The AWS SDK v3 `S3Client`
|
|
20
|
+
* satisfies this; tests inject an in-memory fake instead of hitting the network.
|
|
21
|
+
*/
|
|
22
|
+
export interface S3ClientLike {
|
|
23
|
+
send(command: any): Promise<any>;
|
|
24
|
+
}
|
|
25
|
+
/** Configuration for {@link S3ReplicaClient}. */
|
|
26
|
+
export interface S3ReplicaClientOptions {
|
|
27
|
+
/** S3 bucket name. Required. */
|
|
28
|
+
bucket: string;
|
|
29
|
+
/** Object key prefix (e.g. `backup`). Trailing slash is added as needed. */
|
|
30
|
+
prefix?: string;
|
|
31
|
+
/** AWS region. Defaults to the AWS SDK region resolution chain. */
|
|
32
|
+
region?: string;
|
|
33
|
+
/** Custom endpoint URL (e.g. for MinIO / LocalStack). */
|
|
34
|
+
endpoint?: string;
|
|
35
|
+
accessKeyId?: string;
|
|
36
|
+
secretAccessKey?: string;
|
|
37
|
+
sessionToken?: string;
|
|
38
|
+
/** Inject a preconfigured client. Mostly for tests. */
|
|
39
|
+
client?: S3ClientLike;
|
|
40
|
+
}
|
|
41
|
+
/** S3ReplicaClient writes LTX files to an Amazon S3 bucket. */
|
|
42
|
+
export declare class S3ReplicaClient implements ReplicaClient {
|
|
43
|
+
readonly bucket: string;
|
|
44
|
+
readonly prefix: string;
|
|
45
|
+
private client;
|
|
46
|
+
private logger;
|
|
47
|
+
constructor(opts: S3ReplicaClientOptions);
|
|
48
|
+
type(): string;
|
|
49
|
+
/** Best-effort connectivity check; real I/O errors surface on first use. */
|
|
50
|
+
init(): Promise<void>;
|
|
51
|
+
setLogger(logger: Logger): void;
|
|
52
|
+
/** Returns the object key prefix (empty string if none). */
|
|
53
|
+
prefixKey(): string;
|
|
54
|
+
/** Returns `{prefix}ltx/{level}/`. */
|
|
55
|
+
private levelPrefix;
|
|
56
|
+
/** Returns the object key for a level and TXID range. */
|
|
57
|
+
private fileKey;
|
|
58
|
+
ltxFiles(opts: LTXFilesOptions): AsyncIterable<FileInfo>;
|
|
59
|
+
openLtxFile(opts: OpenLTXFileOptions): Promise<Readable>;
|
|
60
|
+
writeLtxFile(opts: WriteLTXFileOptions, body: AsyncIterable<Uint8Array | string>): Promise<FileInfo>;
|
|
61
|
+
deleteLtxFiles(infos: FileInfo[]): Promise<void>;
|
|
62
|
+
deleteAll(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Builds an S3ReplicaClient from parsed replica URL parts. `host` is the bucket,
|
|
66
|
+
* `urlPath` is the prefix, `query` may carry `region`/`endpoint`, and `userinfo`
|
|
67
|
+
* carries `accessKeyId:secretAccessKey`.
|
|
68
|
+
*/
|
|
69
|
+
export declare function newReplicaClientFromURL(_scheme: string, host: string, urlPath: string, query: URLSearchParams | null, userinfo: string | null): ReplicaClient;
|
|
70
|
+
//# sourceMappingURL=replica-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replica-client.d.ts","sourceRoot":"","sources":["../src/replica-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAgBvC,OAAO,EAOL,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACzB,MAAM,gCAAgC,CAAC;AAExC,wEAAwE;AACxE,eAAO,MAAM,iBAAiB,OAAO,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;CAClC;AAED,iDAAiD;AACjD,MAAM,WAAW,sBAAsB;IACrC,gCAAgC;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,4EAA4E;IAC5E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,yDAAyD;IACzD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uDAAuD;IACvD,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB;AAuBD,+DAA+D;AAC/D,qBAAa,eAAgB,YAAW,aAAa;IACnD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,MAAM,CAAsB;gBAExB,IAAI,EAAE,sBAAsB;IAqBxC,IAAI,IAAI,MAAM;IAId,4EAA4E;IACtE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ3B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAI/B,4DAA4D;IAC5D,SAAS,IAAI,MAAM;IAInB,sCAAsC;IACtC,OAAO,CAAC,WAAW;IAInB,yDAAyD;IACzD,OAAO,CAAC,OAAO;IAIR,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,aAAa,CAAC,QAAQ,CAAC;IAkCzD,WAAW,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAwCxD,YAAY,CAAC,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,aAAa,CAAC,UAAU,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC;IA6HpG,cAAc,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAchD,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;CAoBjC;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,eAAe,GAAG,IAAI,EAC7B,QAAQ,EAAE,MAAM,GAAG,IAAI,GACtB,aAAa,CAaf"}
|