@noy-db/s3 0.5.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 +21 -0
- package/README.md +41 -0
- package/dist/index.cjs +175 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +157 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 vLannaAi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @noy-db/s3
|
|
2
|
+
|
|
3
|
+
> AWS S3 adapter for [noy-db](https://github.com/vLannaAi/noy-db) — encrypted object storage with zero-knowledge cloud sync.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@noy-db/s3)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @noy-db/core @noy-db/s3 @aws-sdk/client-s3
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@aws-sdk/client-s3` is a peer dependency — install it in your app.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createNoydb } from '@noy-db/core'
|
|
19
|
+
import { s3 } from '@noy-db/s3'
|
|
20
|
+
import { S3Client } from '@aws-sdk/client-s3'
|
|
21
|
+
|
|
22
|
+
const client = new S3Client({ region: 'ap-southeast-1' })
|
|
23
|
+
|
|
24
|
+
const db = await createNoydb({
|
|
25
|
+
adapter: s3({ client, bucket: 'noydb-prod', prefix: 'tenant-a/' }),
|
|
26
|
+
userId: 'alice',
|
|
27
|
+
passphrase: process.env.NOYDB_PASSPHRASE!,
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Each record becomes an S3 object containing only a ciphertext envelope. S3 never sees plaintext — even with full bucket access, an attacker learns nothing without the user's passphrase.
|
|
32
|
+
|
|
33
|
+
Best suited for:
|
|
34
|
+
|
|
35
|
+
- Infrequent-access archival with strong privacy guarantees
|
|
36
|
+
- Cold storage of audit trails and backups
|
|
37
|
+
- Lower-cost alternative to DynamoDB for small teams
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
MIT © vLannaAi — see the [noy-db repo](https://github.com/vLannaAi/noy-db) for full documentation.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
s3: () => s3
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_core = require("@noy-db/core");
|
|
27
|
+
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
28
|
+
function s3(options) {
|
|
29
|
+
const { bucket, prefix = "" } = options;
|
|
30
|
+
const client = options.client ?? new import_client_s3.S3Client({
|
|
31
|
+
...options.region ? { region: options.region } : {},
|
|
32
|
+
...options.endpoint ? { endpoint: options.endpoint, forcePathStyle: true } : {}
|
|
33
|
+
});
|
|
34
|
+
function objectKey(compartment, collection, id) {
|
|
35
|
+
const parts = [compartment, collection, `${id}.json`];
|
|
36
|
+
return prefix ? `${prefix}/${parts.join("/")}` : parts.join("/");
|
|
37
|
+
}
|
|
38
|
+
function collPrefix(compartment, collection) {
|
|
39
|
+
const parts = [compartment, collection, ""];
|
|
40
|
+
return prefix ? `${prefix}/${parts.join("/")}` : parts.join("/");
|
|
41
|
+
}
|
|
42
|
+
function compPrefix(compartment) {
|
|
43
|
+
return prefix ? `${prefix}/${compartment}/` : `${compartment}/`;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
name: "s3",
|
|
47
|
+
async get(compartment, collection, id) {
|
|
48
|
+
try {
|
|
49
|
+
const result = await client.send(new import_client_s3.GetObjectCommand({
|
|
50
|
+
Bucket: bucket,
|
|
51
|
+
Key: objectKey(compartment, collection, id)
|
|
52
|
+
}));
|
|
53
|
+
if (!result.Body) return null;
|
|
54
|
+
const body = await result.Body.transformToString();
|
|
55
|
+
return JSON.parse(body);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
if (err instanceof Error && (err.name === "NoSuchKey" || err.name === "NotFound")) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
async put(compartment, collection, id, envelope, expectedVersion) {
|
|
64
|
+
if (expectedVersion !== void 0) {
|
|
65
|
+
const existing = await this.get(compartment, collection, id);
|
|
66
|
+
if (existing && existing._v !== expectedVersion) {
|
|
67
|
+
throw new import_core.ConflictError(existing._v, `Version conflict: expected ${expectedVersion}, found ${existing._v}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
await client.send(new import_client_s3.PutObjectCommand({
|
|
71
|
+
Bucket: bucket,
|
|
72
|
+
Key: objectKey(compartment, collection, id),
|
|
73
|
+
Body: JSON.stringify(envelope),
|
|
74
|
+
ContentType: "application/json"
|
|
75
|
+
}));
|
|
76
|
+
},
|
|
77
|
+
async delete(compartment, collection, id) {
|
|
78
|
+
await client.send(new import_client_s3.DeleteObjectCommand({
|
|
79
|
+
Bucket: bucket,
|
|
80
|
+
Key: objectKey(compartment, collection, id)
|
|
81
|
+
}));
|
|
82
|
+
},
|
|
83
|
+
async list(compartment, collection) {
|
|
84
|
+
const pfx = collPrefix(compartment, collection);
|
|
85
|
+
const result = await client.send(new import_client_s3.ListObjectsV2Command({
|
|
86
|
+
Bucket: bucket,
|
|
87
|
+
Prefix: pfx
|
|
88
|
+
}));
|
|
89
|
+
return (result.Contents ?? []).map((obj) => obj.Key ?? "").filter((k) => k.endsWith(".json")).map((k) => k.slice(pfx.length, -5));
|
|
90
|
+
},
|
|
91
|
+
async loadAll(compartment) {
|
|
92
|
+
const pfx = compPrefix(compartment);
|
|
93
|
+
const listResult = await client.send(new import_client_s3.ListObjectsV2Command({
|
|
94
|
+
Bucket: bucket,
|
|
95
|
+
Prefix: pfx
|
|
96
|
+
}));
|
|
97
|
+
const snapshot = {};
|
|
98
|
+
for (const obj of listResult.Contents ?? []) {
|
|
99
|
+
const key = obj.Key ?? "";
|
|
100
|
+
if (!key.endsWith(".json")) continue;
|
|
101
|
+
const relativePath = key.slice(pfx.length);
|
|
102
|
+
const parts = relativePath.split("/");
|
|
103
|
+
if (parts.length !== 2) continue;
|
|
104
|
+
const collection = parts[0];
|
|
105
|
+
const id = parts[1].slice(0, -5);
|
|
106
|
+
if (collection.startsWith("_")) continue;
|
|
107
|
+
const getResult = await client.send(new import_client_s3.GetObjectCommand({
|
|
108
|
+
Bucket: bucket,
|
|
109
|
+
Key: key
|
|
110
|
+
}));
|
|
111
|
+
if (!getResult.Body) continue;
|
|
112
|
+
const body = await getResult.Body.transformToString();
|
|
113
|
+
if (!snapshot[collection]) snapshot[collection] = {};
|
|
114
|
+
snapshot[collection][id] = JSON.parse(body);
|
|
115
|
+
}
|
|
116
|
+
return snapshot;
|
|
117
|
+
},
|
|
118
|
+
async saveAll(compartment, data) {
|
|
119
|
+
for (const [collection, records] of Object.entries(data)) {
|
|
120
|
+
for (const [id, envelope] of Object.entries(records)) {
|
|
121
|
+
await this.put(compartment, collection, id, envelope);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async ping() {
|
|
126
|
+
try {
|
|
127
|
+
await client.send(new import_client_s3.HeadBucketCommand({ Bucket: bucket }));
|
|
128
|
+
return true;
|
|
129
|
+
} catch {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
/**
|
|
134
|
+
* Paginate over a collection using S3's native `ContinuationToken`.
|
|
135
|
+
*
|
|
136
|
+
* Each page does:
|
|
137
|
+
* 1. ListObjectsV2 with MaxKeys = limit and the previous token
|
|
138
|
+
* 2. GetObject for every key on the page (in parallel)
|
|
139
|
+
*
|
|
140
|
+
* The 2-step pattern is necessary because S3 list responses don't
|
|
141
|
+
* include object bodies. For very large collections this is N+1 — but
|
|
142
|
+
* the parallel GETs amortize well, and consumers willing to pay for
|
|
143
|
+
* stronger pagination should use a different adapter (Dynamo).
|
|
144
|
+
*/
|
|
145
|
+
async listPage(compartment, collection, cursor, limit = 100) {
|
|
146
|
+
const pfx = collPrefix(compartment, collection);
|
|
147
|
+
const listResult = await client.send(new import_client_s3.ListObjectsV2Command({
|
|
148
|
+
Bucket: bucket,
|
|
149
|
+
Prefix: pfx,
|
|
150
|
+
MaxKeys: limit,
|
|
151
|
+
...cursor ? { ContinuationToken: cursor } : {}
|
|
152
|
+
}));
|
|
153
|
+
const keys = (listResult.Contents ?? []).map((obj) => obj.Key ?? "").filter((k) => k.endsWith(".json"));
|
|
154
|
+
const items = await Promise.all(keys.map(async (key) => {
|
|
155
|
+
const id = key.slice(pfx.length, -5);
|
|
156
|
+
const getResult = await client.send(new import_client_s3.GetObjectCommand({
|
|
157
|
+
Bucket: bucket,
|
|
158
|
+
Key: key
|
|
159
|
+
}));
|
|
160
|
+
if (!getResult.Body) return null;
|
|
161
|
+
const body = await getResult.Body.transformToString();
|
|
162
|
+
return { id, envelope: JSON.parse(body) };
|
|
163
|
+
}));
|
|
164
|
+
return {
|
|
165
|
+
items: items.filter((x) => x !== null),
|
|
166
|
+
nextCursor: listResult.IsTruncated && listResult.NextContinuationToken ? listResult.NextContinuationToken : null
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
172
|
+
0 && (module.exports = {
|
|
173
|
+
s3
|
|
174
|
+
});
|
|
175
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { NoydbAdapter, EncryptedEnvelope, CompartmentSnapshot } from '@noy-db/core'\nimport { ConflictError } from '@noy-db/core'\nimport {\n S3Client as AwsS3Client,\n GetObjectCommand,\n PutObjectCommand,\n DeleteObjectCommand,\n ListObjectsV2Command,\n HeadBucketCommand,\n} from '@aws-sdk/client-s3'\n\n/**\n * Minimal interface for an S3 client. Compatible with @aws-sdk/client-s3's\n * S3Client. Exposed so tests (and advanced consumers) can inject a mock or\n * a pre-configured client without going through the default constructor.\n */\nexport interface S3ClientLike {\n send(command: unknown): Promise<unknown>\n}\n\nexport interface S3Options {\n /** S3 bucket name. */\n bucket: string\n /** Key prefix within the bucket. Default: ''. */\n prefix?: string\n /** AWS region. Default: 'us-east-1'. */\n region?: string\n /** Custom endpoint (e.g., for MinIO or LocalStack). */\n endpoint?: string\n /**\n * Pre-built S3 client. If provided, the adapter uses this client\n * directly and ignores `region` / `endpoint`. Useful for tests and\n * for apps that want to share a client across adapters.\n */\n client?: S3ClientLike\n}\n\n/**\n * Create an S3 adapter.\n * Key scheme: `{prefix}/{compartment}/{collection}/{id}.json`\n */\nexport function s3(options: S3Options): NoydbAdapter {\n const { bucket, prefix = '' } = options\n\n // Use the injected client if provided (tests, advanced consumers).\n // The cast through `S3ClientLike` is safe because the AWS S3Client's\n // `send()` method matches the structural shape — we only call `send`\n // and inspect the documented response fields.\n const client = (options.client ?? new AwsS3Client({\n ...(options.region ? { region: options.region } : {}),\n ...(options.endpoint ? { endpoint: options.endpoint, forcePathStyle: true } : {}),\n })) as AwsS3Client\n\n function objectKey(compartment: string, collection: string, id: string): string {\n const parts = [compartment, collection, `${id}.json`]\n return prefix ? `${prefix}/${parts.join('/')}` : parts.join('/')\n }\n\n function collPrefix(compartment: string, collection: string): string {\n const parts = [compartment, collection, '']\n return prefix ? `${prefix}/${parts.join('/')}` : parts.join('/')\n }\n\n function compPrefix(compartment: string): string {\n return prefix ? `${prefix}/${compartment}/` : `${compartment}/`\n }\n\n return {\n name: 's3',\n\n async get(compartment, collection, id) {\n try {\n const result = await client.send(new GetObjectCommand({\n Bucket: bucket,\n Key: objectKey(compartment, collection, id),\n }))\n\n if (!result.Body) return null\n const body = await result.Body.transformToString()\n return JSON.parse(body) as EncryptedEnvelope\n } catch (err: unknown) {\n if (err instanceof Error && (err.name === 'NoSuchKey' || err.name === 'NotFound')) {\n return null\n }\n throw err\n }\n },\n\n async put(compartment, collection, id, envelope, expectedVersion) {\n if (expectedVersion !== undefined) {\n const existing = await this.get(compartment, collection, id)\n if (existing && existing._v !== expectedVersion) {\n throw new ConflictError(existing._v, `Version conflict: expected ${expectedVersion}, found ${existing._v}`)\n }\n }\n\n await client.send(new PutObjectCommand({\n Bucket: bucket,\n Key: objectKey(compartment, collection, id),\n Body: JSON.stringify(envelope),\n ContentType: 'application/json',\n }))\n },\n\n async delete(compartment, collection, id) {\n await client.send(new DeleteObjectCommand({\n Bucket: bucket,\n Key: objectKey(compartment, collection, id),\n }))\n },\n\n async list(compartment, collection) {\n const pfx = collPrefix(compartment, collection)\n const result = await client.send(new ListObjectsV2Command({\n Bucket: bucket,\n Prefix: pfx,\n }))\n\n return (result.Contents ?? [])\n .map(obj => obj.Key ?? '')\n .filter(k => k.endsWith('.json'))\n .map(k => k.slice(pfx.length, -5))\n },\n\n async loadAll(compartment) {\n const pfx = compPrefix(compartment)\n const listResult = await client.send(new ListObjectsV2Command({\n Bucket: bucket,\n Prefix: pfx,\n }))\n\n const snapshot: CompartmentSnapshot = {}\n\n for (const obj of listResult.Contents ?? []) {\n const key = obj.Key ?? ''\n if (!key.endsWith('.json')) continue\n\n const relativePath = key.slice(pfx.length)\n const parts = relativePath.split('/')\n if (parts.length !== 2) continue\n\n const collection = parts[0]!\n const id = parts[1]!.slice(0, -5)\n if (collection.startsWith('_')) continue\n\n const getResult = await client.send(new GetObjectCommand({\n Bucket: bucket,\n Key: key,\n }))\n\n if (!getResult.Body) continue\n const body = await getResult.Body.transformToString()\n\n if (!snapshot[collection]) snapshot[collection] = {}\n snapshot[collection][id] = JSON.parse(body) as EncryptedEnvelope\n }\n\n return snapshot\n },\n\n async saveAll(compartment, data) {\n for (const [collection, records] of Object.entries(data)) {\n for (const [id, envelope] of Object.entries(records)) {\n await this.put(compartment, collection, id, envelope)\n }\n }\n },\n\n async ping() {\n try {\n await client.send(new HeadBucketCommand({ Bucket: bucket }))\n return true\n } catch {\n return false\n }\n },\n\n /**\n * Paginate over a collection using S3's native `ContinuationToken`.\n *\n * Each page does:\n * 1. ListObjectsV2 with MaxKeys = limit and the previous token\n * 2. GetObject for every key on the page (in parallel)\n *\n * The 2-step pattern is necessary because S3 list responses don't\n * include object bodies. For very large collections this is N+1 — but\n * the parallel GETs amortize well, and consumers willing to pay for\n * stronger pagination should use a different adapter (Dynamo).\n */\n async listPage(compartment, collection, cursor, limit = 100) {\n const pfx = collPrefix(compartment, collection)\n const listResult = await client.send(new ListObjectsV2Command({\n Bucket: bucket,\n Prefix: pfx,\n MaxKeys: limit,\n ...(cursor ? { ContinuationToken: cursor } : {}),\n }))\n\n const keys = (listResult.Contents ?? [])\n .map(obj => obj.Key ?? '')\n .filter(k => k.endsWith('.json'))\n\n // Fetch every body in parallel — bounded by `limit` so we never\n // fan out beyond the page size.\n const items = await Promise.all(keys.map(async (key) => {\n const id = key.slice(pfx.length, -5)\n const getResult = await client.send(new GetObjectCommand({\n Bucket: bucket,\n Key: key,\n }))\n if (!getResult.Body) return null\n const body = await getResult.Body.transformToString()\n return { id, envelope: JSON.parse(body) as EncryptedEnvelope }\n }))\n\n return {\n items: items.filter((x): x is { id: string; envelope: EncryptedEnvelope } => x !== null),\n nextCursor: listResult.IsTruncated && listResult.NextContinuationToken\n ? listResult.NextContinuationToken\n : null,\n }\n },\n }\n}\n"],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AAA;AAAA,IAcA,MAAM,SAAS,aAAa,YAAY,QAAQ,QAAQ,KAAK;AAC3D,YAAM,MAAM,WAAW,aAAa,UAAU;AAC9C,YAAM,aAAa,MAAM,OAAO,KAAK,IAAI,sCAAqB;AAAA,QAC5D,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,SAAS;AAAA,QACT,GAAI,SAAS,EAAE,mBAAmB,OAAO,IAAI,CAAC;AAAA,MAChD,CAAC,CAAC;AAEF,YAAM,QAAQ,WAAW,YAAY,CAAC,GACnC,IAAI,SAAO,IAAI,OAAO,EAAE,EACxB,OAAO,OAAK,EAAE,SAAS,OAAO,CAAC;AAIlC,YAAM,QAAQ,MAAM,QAAQ,IAAI,KAAK,IAAI,OAAO,QAAQ;AACtD,cAAM,KAAK,IAAI,MAAM,IAAI,QAAQ,EAAE;AACnC,cAAM,YAAY,MAAM,OAAO,KAAK,IAAI,kCAAiB;AAAA,UACvD,QAAQ;AAAA,UACR,KAAK;AAAA,QACP,CAAC,CAAC;AACF,YAAI,CAAC,UAAU,KAAM,QAAO;AAC5B,cAAM,OAAO,MAAM,UAAU,KAAK,kBAAkB;AACpD,eAAO,EAAE,IAAI,UAAU,KAAK,MAAM,IAAI,EAAuB;AAAA,MAC/D,CAAC,CAAC;AAEF,aAAO;AAAA,QACL,OAAO,MAAM,OAAO,CAAC,MAAwD,MAAM,IAAI;AAAA,QACvF,YAAY,WAAW,eAAe,WAAW,wBAC7C,WAAW,wBACX;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;","names":["AwsS3Client"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { NoydbAdapter } from '@noy-db/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Minimal interface for an S3 client. Compatible with @aws-sdk/client-s3's
|
|
5
|
+
* S3Client. Exposed so tests (and advanced consumers) can inject a mock or
|
|
6
|
+
* a pre-configured client without going through the default constructor.
|
|
7
|
+
*/
|
|
8
|
+
interface S3ClientLike {
|
|
9
|
+
send(command: unknown): Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
interface S3Options {
|
|
12
|
+
/** S3 bucket name. */
|
|
13
|
+
bucket: string;
|
|
14
|
+
/** Key prefix within the bucket. Default: ''. */
|
|
15
|
+
prefix?: string;
|
|
16
|
+
/** AWS region. Default: 'us-east-1'. */
|
|
17
|
+
region?: string;
|
|
18
|
+
/** Custom endpoint (e.g., for MinIO or LocalStack). */
|
|
19
|
+
endpoint?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Pre-built S3 client. If provided, the adapter uses this client
|
|
22
|
+
* directly and ignores `region` / `endpoint`. Useful for tests and
|
|
23
|
+
* for apps that want to share a client across adapters.
|
|
24
|
+
*/
|
|
25
|
+
client?: S3ClientLike;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create an S3 adapter.
|
|
29
|
+
* Key scheme: `{prefix}/{compartment}/{collection}/{id}.json`
|
|
30
|
+
*/
|
|
31
|
+
declare function s3(options: S3Options): NoydbAdapter;
|
|
32
|
+
|
|
33
|
+
export { type S3ClientLike, type S3Options, s3 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { NoydbAdapter } from '@noy-db/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Minimal interface for an S3 client. Compatible with @aws-sdk/client-s3's
|
|
5
|
+
* S3Client. Exposed so tests (and advanced consumers) can inject a mock or
|
|
6
|
+
* a pre-configured client without going through the default constructor.
|
|
7
|
+
*/
|
|
8
|
+
interface S3ClientLike {
|
|
9
|
+
send(command: unknown): Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
interface S3Options {
|
|
12
|
+
/** S3 bucket name. */
|
|
13
|
+
bucket: string;
|
|
14
|
+
/** Key prefix within the bucket. Default: ''. */
|
|
15
|
+
prefix?: string;
|
|
16
|
+
/** AWS region. Default: 'us-east-1'. */
|
|
17
|
+
region?: string;
|
|
18
|
+
/** Custom endpoint (e.g., for MinIO or LocalStack). */
|
|
19
|
+
endpoint?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Pre-built S3 client. If provided, the adapter uses this client
|
|
22
|
+
* directly and ignores `region` / `endpoint`. Useful for tests and
|
|
23
|
+
* for apps that want to share a client across adapters.
|
|
24
|
+
*/
|
|
25
|
+
client?: S3ClientLike;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create an S3 adapter.
|
|
29
|
+
* Key scheme: `{prefix}/{compartment}/{collection}/{id}.json`
|
|
30
|
+
*/
|
|
31
|
+
declare function s3(options: S3Options): NoydbAdapter;
|
|
32
|
+
|
|
33
|
+
export { type S3ClientLike, type S3Options, s3 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { ConflictError } from "@noy-db/core";
|
|
3
|
+
import {
|
|
4
|
+
S3Client as AwsS3Client,
|
|
5
|
+
GetObjectCommand,
|
|
6
|
+
PutObjectCommand,
|
|
7
|
+
DeleteObjectCommand,
|
|
8
|
+
ListObjectsV2Command,
|
|
9
|
+
HeadBucketCommand
|
|
10
|
+
} from "@aws-sdk/client-s3";
|
|
11
|
+
function s3(options) {
|
|
12
|
+
const { bucket, prefix = "" } = options;
|
|
13
|
+
const client = options.client ?? new AwsS3Client({
|
|
14
|
+
...options.region ? { region: options.region } : {},
|
|
15
|
+
...options.endpoint ? { endpoint: options.endpoint, forcePathStyle: true } : {}
|
|
16
|
+
});
|
|
17
|
+
function objectKey(compartment, collection, id) {
|
|
18
|
+
const parts = [compartment, collection, `${id}.json`];
|
|
19
|
+
return prefix ? `${prefix}/${parts.join("/")}` : parts.join("/");
|
|
20
|
+
}
|
|
21
|
+
function collPrefix(compartment, collection) {
|
|
22
|
+
const parts = [compartment, collection, ""];
|
|
23
|
+
return prefix ? `${prefix}/${parts.join("/")}` : parts.join("/");
|
|
24
|
+
}
|
|
25
|
+
function compPrefix(compartment) {
|
|
26
|
+
return prefix ? `${prefix}/${compartment}/` : `${compartment}/`;
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
name: "s3",
|
|
30
|
+
async get(compartment, collection, id) {
|
|
31
|
+
try {
|
|
32
|
+
const result = await client.send(new GetObjectCommand({
|
|
33
|
+
Bucket: bucket,
|
|
34
|
+
Key: objectKey(compartment, collection, id)
|
|
35
|
+
}));
|
|
36
|
+
if (!result.Body) return null;
|
|
37
|
+
const body = await result.Body.transformToString();
|
|
38
|
+
return JSON.parse(body);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if (err instanceof Error && (err.name === "NoSuchKey" || err.name === "NotFound")) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
async put(compartment, collection, id, envelope, expectedVersion) {
|
|
47
|
+
if (expectedVersion !== void 0) {
|
|
48
|
+
const existing = await this.get(compartment, collection, id);
|
|
49
|
+
if (existing && existing._v !== expectedVersion) {
|
|
50
|
+
throw new ConflictError(existing._v, `Version conflict: expected ${expectedVersion}, found ${existing._v}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
await client.send(new PutObjectCommand({
|
|
54
|
+
Bucket: bucket,
|
|
55
|
+
Key: objectKey(compartment, collection, id),
|
|
56
|
+
Body: JSON.stringify(envelope),
|
|
57
|
+
ContentType: "application/json"
|
|
58
|
+
}));
|
|
59
|
+
},
|
|
60
|
+
async delete(compartment, collection, id) {
|
|
61
|
+
await client.send(new DeleteObjectCommand({
|
|
62
|
+
Bucket: bucket,
|
|
63
|
+
Key: objectKey(compartment, collection, id)
|
|
64
|
+
}));
|
|
65
|
+
},
|
|
66
|
+
async list(compartment, collection) {
|
|
67
|
+
const pfx = collPrefix(compartment, collection);
|
|
68
|
+
const result = await client.send(new ListObjectsV2Command({
|
|
69
|
+
Bucket: bucket,
|
|
70
|
+
Prefix: pfx
|
|
71
|
+
}));
|
|
72
|
+
return (result.Contents ?? []).map((obj) => obj.Key ?? "").filter((k) => k.endsWith(".json")).map((k) => k.slice(pfx.length, -5));
|
|
73
|
+
},
|
|
74
|
+
async loadAll(compartment) {
|
|
75
|
+
const pfx = compPrefix(compartment);
|
|
76
|
+
const listResult = await client.send(new ListObjectsV2Command({
|
|
77
|
+
Bucket: bucket,
|
|
78
|
+
Prefix: pfx
|
|
79
|
+
}));
|
|
80
|
+
const snapshot = {};
|
|
81
|
+
for (const obj of listResult.Contents ?? []) {
|
|
82
|
+
const key = obj.Key ?? "";
|
|
83
|
+
if (!key.endsWith(".json")) continue;
|
|
84
|
+
const relativePath = key.slice(pfx.length);
|
|
85
|
+
const parts = relativePath.split("/");
|
|
86
|
+
if (parts.length !== 2) continue;
|
|
87
|
+
const collection = parts[0];
|
|
88
|
+
const id = parts[1].slice(0, -5);
|
|
89
|
+
if (collection.startsWith("_")) continue;
|
|
90
|
+
const getResult = await client.send(new GetObjectCommand({
|
|
91
|
+
Bucket: bucket,
|
|
92
|
+
Key: key
|
|
93
|
+
}));
|
|
94
|
+
if (!getResult.Body) continue;
|
|
95
|
+
const body = await getResult.Body.transformToString();
|
|
96
|
+
if (!snapshot[collection]) snapshot[collection] = {};
|
|
97
|
+
snapshot[collection][id] = JSON.parse(body);
|
|
98
|
+
}
|
|
99
|
+
return snapshot;
|
|
100
|
+
},
|
|
101
|
+
async saveAll(compartment, data) {
|
|
102
|
+
for (const [collection, records] of Object.entries(data)) {
|
|
103
|
+
for (const [id, envelope] of Object.entries(records)) {
|
|
104
|
+
await this.put(compartment, collection, id, envelope);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
async ping() {
|
|
109
|
+
try {
|
|
110
|
+
await client.send(new HeadBucketCommand({ Bucket: bucket }));
|
|
111
|
+
return true;
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
/**
|
|
117
|
+
* Paginate over a collection using S3's native `ContinuationToken`.
|
|
118
|
+
*
|
|
119
|
+
* Each page does:
|
|
120
|
+
* 1. ListObjectsV2 with MaxKeys = limit and the previous token
|
|
121
|
+
* 2. GetObject for every key on the page (in parallel)
|
|
122
|
+
*
|
|
123
|
+
* The 2-step pattern is necessary because S3 list responses don't
|
|
124
|
+
* include object bodies. For very large collections this is N+1 — but
|
|
125
|
+
* the parallel GETs amortize well, and consumers willing to pay for
|
|
126
|
+
* stronger pagination should use a different adapter (Dynamo).
|
|
127
|
+
*/
|
|
128
|
+
async listPage(compartment, collection, cursor, limit = 100) {
|
|
129
|
+
const pfx = collPrefix(compartment, collection);
|
|
130
|
+
const listResult = await client.send(new ListObjectsV2Command({
|
|
131
|
+
Bucket: bucket,
|
|
132
|
+
Prefix: pfx,
|
|
133
|
+
MaxKeys: limit,
|
|
134
|
+
...cursor ? { ContinuationToken: cursor } : {}
|
|
135
|
+
}));
|
|
136
|
+
const keys = (listResult.Contents ?? []).map((obj) => obj.Key ?? "").filter((k) => k.endsWith(".json"));
|
|
137
|
+
const items = await Promise.all(keys.map(async (key) => {
|
|
138
|
+
const id = key.slice(pfx.length, -5);
|
|
139
|
+
const getResult = await client.send(new GetObjectCommand({
|
|
140
|
+
Bucket: bucket,
|
|
141
|
+
Key: key
|
|
142
|
+
}));
|
|
143
|
+
if (!getResult.Body) return null;
|
|
144
|
+
const body = await getResult.Body.transformToString();
|
|
145
|
+
return { id, envelope: JSON.parse(body) };
|
|
146
|
+
}));
|
|
147
|
+
return {
|
|
148
|
+
items: items.filter((x) => x !== null),
|
|
149
|
+
nextCursor: listResult.IsTruncated && listResult.NextContinuationToken ? listResult.NextContinuationToken : null
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
export {
|
|
155
|
+
s3
|
|
156
|
+
};
|
|
157
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { NoydbAdapter, EncryptedEnvelope, CompartmentSnapshot } from '@noy-db/core'\nimport { ConflictError } from '@noy-db/core'\nimport {\n S3Client as AwsS3Client,\n GetObjectCommand,\n PutObjectCommand,\n DeleteObjectCommand,\n ListObjectsV2Command,\n HeadBucketCommand,\n} from '@aws-sdk/client-s3'\n\n/**\n * Minimal interface for an S3 client. Compatible with @aws-sdk/client-s3's\n * S3Client. Exposed so tests (and advanced consumers) can inject a mock or\n * a pre-configured client without going through the default constructor.\n */\nexport interface S3ClientLike {\n send(command: unknown): Promise<unknown>\n}\n\nexport interface S3Options {\n /** S3 bucket name. */\n bucket: string\n /** Key prefix within the bucket. Default: ''. */\n prefix?: string\n /** AWS region. Default: 'us-east-1'. */\n region?: string\n /** Custom endpoint (e.g., for MinIO or LocalStack). */\n endpoint?: string\n /**\n * Pre-built S3 client. If provided, the adapter uses this client\n * directly and ignores `region` / `endpoint`. Useful for tests and\n * for apps that want to share a client across adapters.\n */\n client?: S3ClientLike\n}\n\n/**\n * Create an S3 adapter.\n * Key scheme: `{prefix}/{compartment}/{collection}/{id}.json`\n */\nexport function s3(options: S3Options): NoydbAdapter {\n const { bucket, prefix = '' } = options\n\n // Use the injected client if provided (tests, advanced consumers).\n // The cast through `S3ClientLike` is safe because the AWS S3Client's\n // `send()` method matches the structural shape — we only call `send`\n // and inspect the documented response fields.\n const client = (options.client ?? new AwsS3Client({\n ...(options.region ? { region: options.region } : {}),\n ...(options.endpoint ? { endpoint: options.endpoint, forcePathStyle: true } : {}),\n })) as AwsS3Client\n\n function objectKey(compartment: string, collection: string, id: string): string {\n const parts = [compartment, collection, `${id}.json`]\n return prefix ? `${prefix}/${parts.join('/')}` : parts.join('/')\n }\n\n function collPrefix(compartment: string, collection: string): string {\n const parts = [compartment, collection, '']\n return prefix ? `${prefix}/${parts.join('/')}` : parts.join('/')\n }\n\n function compPrefix(compartment: string): string {\n return prefix ? `${prefix}/${compartment}/` : `${compartment}/`\n }\n\n return {\n name: 's3',\n\n async get(compartment, collection, id) {\n try {\n const result = await client.send(new GetObjectCommand({\n Bucket: bucket,\n Key: objectKey(compartment, collection, id),\n }))\n\n if (!result.Body) return null\n const body = await result.Body.transformToString()\n return JSON.parse(body) as EncryptedEnvelope\n } catch (err: unknown) {\n if (err instanceof Error && (err.name === 'NoSuchKey' || err.name === 'NotFound')) {\n return null\n }\n throw err\n }\n },\n\n async put(compartment, collection, id, envelope, expectedVersion) {\n if (expectedVersion !== undefined) {\n const existing = await this.get(compartment, collection, id)\n if (existing && existing._v !== expectedVersion) {\n throw new ConflictError(existing._v, `Version conflict: expected ${expectedVersion}, found ${existing._v}`)\n }\n }\n\n await client.send(new PutObjectCommand({\n Bucket: bucket,\n Key: objectKey(compartment, collection, id),\n Body: JSON.stringify(envelope),\n ContentType: 'application/json',\n }))\n },\n\n async delete(compartment, collection, id) {\n await client.send(new DeleteObjectCommand({\n Bucket: bucket,\n Key: objectKey(compartment, collection, id),\n }))\n },\n\n async list(compartment, collection) {\n const pfx = collPrefix(compartment, collection)\n const result = await client.send(new ListObjectsV2Command({\n Bucket: bucket,\n Prefix: pfx,\n }))\n\n return (result.Contents ?? [])\n .map(obj => obj.Key ?? '')\n .filter(k => k.endsWith('.json'))\n .map(k => k.slice(pfx.length, -5))\n },\n\n async loadAll(compartment) {\n const pfx = compPrefix(compartment)\n const listResult = await client.send(new ListObjectsV2Command({\n Bucket: bucket,\n Prefix: pfx,\n }))\n\n const snapshot: CompartmentSnapshot = {}\n\n for (const obj of listResult.Contents ?? []) {\n const key = obj.Key ?? ''\n if (!key.endsWith('.json')) continue\n\n const relativePath = key.slice(pfx.length)\n const parts = relativePath.split('/')\n if (parts.length !== 2) continue\n\n const collection = parts[0]!\n const id = parts[1]!.slice(0, -5)\n if (collection.startsWith('_')) continue\n\n const getResult = await client.send(new GetObjectCommand({\n Bucket: bucket,\n Key: key,\n }))\n\n if (!getResult.Body) continue\n const body = await getResult.Body.transformToString()\n\n if (!snapshot[collection]) snapshot[collection] = {}\n snapshot[collection][id] = JSON.parse(body) as EncryptedEnvelope\n }\n\n return snapshot\n },\n\n async saveAll(compartment, data) {\n for (const [collection, records] of Object.entries(data)) {\n for (const [id, envelope] of Object.entries(records)) {\n await this.put(compartment, collection, id, envelope)\n }\n }\n },\n\n async ping() {\n try {\n await client.send(new HeadBucketCommand({ Bucket: bucket }))\n return true\n } catch {\n return false\n }\n },\n\n /**\n * Paginate over a collection using S3's native `ContinuationToken`.\n *\n * Each page does:\n * 1. ListObjectsV2 with MaxKeys = limit and the previous token\n * 2. GetObject for every key on the page (in parallel)\n *\n * The 2-step pattern is necessary because S3 list responses don't\n * include object bodies. For very large collections this is N+1 — but\n * the parallel GETs amortize well, and consumers willing to pay for\n * stronger pagination should use a different adapter (Dynamo).\n */\n async listPage(compartment, collection, cursor, limit = 100) {\n const pfx = collPrefix(compartment, collection)\n const listResult = await client.send(new ListObjectsV2Command({\n Bucket: bucket,\n Prefix: pfx,\n MaxKeys: limit,\n ...(cursor ? { ContinuationToken: cursor } : {}),\n }))\n\n const keys = (listResult.Contents ?? [])\n .map(obj => obj.Key ?? '')\n .filter(k => k.endsWith('.json'))\n\n // Fetch every body in parallel — bounded by `limit` so we never\n // fan out beyond the page size.\n const items = await Promise.all(keys.map(async (key) => {\n const id = key.slice(pfx.length, -5)\n const getResult = await client.send(new GetObjectCommand({\n Bucket: bucket,\n Key: key,\n }))\n if (!getResult.Body) return null\n const body = await getResult.Body.transformToString()\n return { id, envelope: JSON.parse(body) as EncryptedEnvelope }\n }))\n\n return {\n items: items.filter((x): x is { id: string; envelope: EncryptedEnvelope } => x !== null),\n nextCursor: listResult.IsTruncated && listResult.NextContinuationToken\n ? listResult.NextContinuationToken\n : null,\n }\n },\n }\n}\n"],"mappings":";AACA,SAAS,qBAAqB;AAC9B;AAAA,EACE,YAAY;AAAA,EACZ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAgCA,SAAS,GAAG,SAAkC;AACnD,QAAM,EAA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I,qBAAqB;AAAA,QAC5D,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,SAAS;AAAA,QACT,GAAI,SAAS,EAAE,mBAAmB,OAAO,IAAI,CAAC;AAAA,MAChD,CAAC,CAAC;AAEF,YAAM,QAAQ,WAAW,YAAY,CAAC,GACnC,IAAI,SAAO,IAAI,OAAO,EAAE,EACxB,OAAO,OAAK,EAAE,SAAS,OAAO,CAAC;AAIlC,YAAM,QAAQ,MAAM,QAAQ,IAAI,KAAK,IAAI,OAAO,QAAQ;AACtD,cAAM,KAAK,IAAI,MAAM,IAAI,QAAQ,EAAE;AACnC,cAAM,YAAY,MAAM,OAAO,KAAK,IAAI,iBAAiB;AAAA,UACvD,QAAQ;AAAA,UACR,KAAK;AAAA,QACP,CAAC,CAAC;AACF,YAAI,CAAC,UAAU,KAAM,QAAO;AAC5B,cAAM,OAAO,MAAM,UAAU,KAAK,kBAAkB;AACpD,eAAO,EAAE,IAAI,UAAU,KAAK,MAAM,IAAI,EAAuB;AAAA,MAC/D,CAAC,CAAC;AAEF,aAAO;AAAA,QACL,OAAO,MAAM,OAAO,CAAC,MAAwD,MAAM,IAAI;AAAA,QACvF,YAAY,WAAW,eAAe,WAAW,wBAC7C,WAAW,wBACX;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@noy-db/s3",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "AWS S3 adapter for noy-db — encrypted object storage with zero-knowledge cloud sync",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "vLannaAi <vicio@lanna.ai>",
|
|
7
|
+
"homepage": "https://github.com/vLannaAi/noy-db/tree/main/packages/s3#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/vLannaAi/noy-db.git",
|
|
11
|
+
"directory": "packages/s3"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/vLannaAi/noy-db/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/index.d.cts",
|
|
26
|
+
"default": "./dist/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.cjs",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@aws-sdk/client-s3": "^3.0.0",
|
|
43
|
+
"@noy-db/core": "^0.5.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@aws-sdk/client-s3": "^3.0.0",
|
|
47
|
+
"@noy-db/core": "0.5.0"
|
|
48
|
+
},
|
|
49
|
+
"keywords": [
|
|
50
|
+
"noy-db",
|
|
51
|
+
"adapter",
|
|
52
|
+
"s3",
|
|
53
|
+
"aws",
|
|
54
|
+
"object-storage",
|
|
55
|
+
"cloud",
|
|
56
|
+
"encryption",
|
|
57
|
+
"zero-knowledge"
|
|
58
|
+
],
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsup",
|
|
61
|
+
"test": "vitest run",
|
|
62
|
+
"lint": "eslint src/",
|
|
63
|
+
"typecheck": "tsc --noEmit"
|
|
64
|
+
}
|
|
65
|
+
}
|