@platformatic/sqlite-js-stream-s3 0.2.1 → 0.4.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/util.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Helpers shared by the S3 replica client and leaser: body reading and the
3
+ * classification of S3 errors (404, 412, 409).
4
+ */
5
+ /** Reads all bytes from an S3 body (stream or Buffer). */
6
+ export declare function readBody(body: any): Promise<Uint8Array>;
7
+ export declare function isPreconditionFailed(err: unknown): boolean;
8
+ export declare function isNotFound(err: unknown): boolean;
9
+ /**
10
+ * A concurrent conditional write to the same key (`409 ConditionalRequestConflict`).
11
+ * S3 asks the caller to retry the request.
12
+ */
13
+ export declare function isConditionalConflict(err: unknown): boolean;
14
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,0DAA0D;AAC1D,wBAAsB,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAuC7D;AAED,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAI1D;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAIhD;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAI3D"}
package/dist/util.js ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Helpers shared by the S3 replica client and leaser: body reading and the
3
+ * classification of S3 errors (404, 412, 409).
4
+ */
5
+ /** Reads all bytes from an S3 body (stream or Buffer). */
6
+ export async function readBody(body) {
7
+ // AWS SDK v3 returns a mix; normalize to a Uint8Array.
8
+ if (body instanceof Uint8Array)
9
+ return body;
10
+ if (typeof body?.on === 'function') {
11
+ // Node stream.
12
+ const chunks = [];
13
+ for await (const chunk of body) {
14
+ chunks.push(typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk);
15
+ }
16
+ let total = 0;
17
+ for (const c of chunks)
18
+ total += c.length;
19
+ const out = new Uint8Array(total);
20
+ let o = 0;
21
+ for (const c of chunks) {
22
+ out.set(c, o);
23
+ o += c.length;
24
+ }
25
+ return out;
26
+ }
27
+ // Web ReadableStream or Buffer.
28
+ if (typeof body?.getReader === 'function') {
29
+ const reader = body.getReader();
30
+ const chunks = [];
31
+ for (;;) {
32
+ const { done, value } = await reader.read();
33
+ if (done)
34
+ break;
35
+ chunks.push(value);
36
+ }
37
+ let total = 0;
38
+ for (const c of chunks)
39
+ total += c.length;
40
+ const out = new Uint8Array(total);
41
+ let o = 0;
42
+ for (const c of chunks) {
43
+ out.set(c, o);
44
+ o += c.length;
45
+ }
46
+ return out;
47
+ }
48
+ return new Uint8Array(body);
49
+ }
50
+ export function isPreconditionFailed(err) {
51
+ const code = err?.$metadata?.httpStatusCode;
52
+ const name = err?.name ?? '';
53
+ return code === 412 || name === 'PreconditionFailed';
54
+ }
55
+ export function isNotFound(err) {
56
+ const code = err?.$metadata?.httpStatusCode;
57
+ const name = err?.name ?? '';
58
+ return code === 404 || name === 'NoSuchKey' || name === 'NotFound';
59
+ }
60
+ /**
61
+ * A concurrent conditional write to the same key (`409 ConditionalRequestConflict`).
62
+ * S3 asks the caller to retry the request.
63
+ */
64
+ export function isConditionalConflict(err) {
65
+ const code = err?.$metadata?.httpStatusCode;
66
+ const name = err?.name ?? '';
67
+ return code === 409 || name === 'ConditionalRequestConflict';
68
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sqlite-js-stream-s3",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Amazon S3 upload strategy (replica client) for @platformatic/sqlite-js-stream",
5
5
  "type": "module",
6
6
  "engines": {
@@ -37,10 +37,10 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@aws-sdk/client-s3": "^3.1135.0",
40
- "@platformatic/sqlite-js-stream": "0.2.1"
40
+ "@platformatic/sqlite-js-stream": "0.4.0"
41
41
  },
42
42
  "devDependencies": {
43
- "@types/node": "^24.0.0",
43
+ "@types/node": "^26.0.0",
44
44
  "typescript": "^5.7.0"
45
45
  },
46
46
  "scripts": {
package/src/index.ts CHANGED
@@ -9,8 +9,14 @@
9
9
  export {
10
10
  S3ReplicaClient,
11
11
  newReplicaClientFromURL,
12
+ isFenced,
13
+ GenerationMetadataKey,
14
+ GenerationObjectName,
12
15
  ReplicaClientType,
16
+ type GenerationFence,
17
+ type GenerationState,
13
18
  type S3ClientLike,
19
+ type S3DeletePrefixOptions,
14
20
  type S3ReplicaClientOptions,
15
21
  } from './replica-client.ts';
16
22
 
package/src/leaser.ts CHANGED
@@ -22,60 +22,7 @@ import {
22
22
  } from '@platformatic/sqlite-js-stream';
23
23
 
24
24
  import type { S3ClientLike } from './replica-client.ts';
25
-
26
- /** Reads all bytes from an S3 body (stream or Buffer). */
27
- async function readBody(body: any): Promise<Uint8Array> {
28
- // AWS SDK v3 returns a mix; normalize to a Uint8Array.
29
- if (body instanceof Uint8Array) return body;
30
- if (typeof body?.on === 'function') {
31
- // Node stream.
32
- const chunks: Uint8Array[] = [];
33
- for await (const chunk of body as AsyncIterable<Uint8Array | string>) {
34
- chunks.push(typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk);
35
- }
36
- let total = 0;
37
- for (const c of chunks) total += c.length;
38
- const out = new Uint8Array(total);
39
- let o = 0;
40
- for (const c of chunks) {
41
- out.set(c, o);
42
- o += c.length;
43
- }
44
- return out;
45
- }
46
- // Web ReadableStream or Buffer.
47
- if (typeof body?.getReader === 'function') {
48
- const reader = (body as ReadableStream<Uint8Array>).getReader();
49
- const chunks: Uint8Array[] = [];
50
- for (;;) {
51
- const { done, value } = await reader.read();
52
- if (done) break;
53
- chunks.push(value);
54
- }
55
- let total = 0;
56
- for (const c of chunks) total += c.length;
57
- const out = new Uint8Array(total);
58
- let o = 0;
59
- for (const c of chunks) {
60
- out.set(c, o);
61
- o += c.length;
62
- }
63
- return out;
64
- }
65
- return new Uint8Array(body as Uint8Array);
66
- }
67
-
68
- function isPreconditionFailed(err: unknown): boolean {
69
- const code = (err as { $metadata?: { httpStatusCode?: number } })?.$metadata?.httpStatusCode;
70
- const name = (err as { name?: string })?.name ?? '';
71
- return code === 412 || name === 'PreconditionFailed';
72
- }
73
-
74
- function isNotFound(err: unknown): boolean {
75
- const code = (err as { $metadata?: { httpStatusCode?: number } })?.$metadata?.httpStatusCode;
76
- const name = (err as { name?: string })?.name ?? '';
77
- return code === 404 || name === 'NoSuchKey' || name === 'NotFound';
78
- }
25
+ import { isNotFound, isPreconditionFailed, readBody } from './util.ts';
79
26
 
80
27
  /** S3Leaser acquires/renews/releases a lease object in an S3 bucket. */
81
28
  export class S3Leaser implements Leaser {