@mainsoft/agent-cms 0.7.0 → 0.10.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/CHANGELOG.md +60 -0
- package/README.md +291 -32
- package/TODO.md +72 -26
- package/UPGRADE-0.10.md +36 -0
- package/UPGRADE-0.8.md +117 -0
- package/dist/agent.cjs +39 -17
- package/dist/agent.d.cts +2 -2
- package/dist/agent.d.ts +2 -2
- package/dist/agent.js +9 -7
- package/dist/bin.js +4528 -355
- package/dist/{chunk-OQ55GEF3.js → chunk-3OPKTBEI.js} +1 -1
- package/dist/chunk-6PB4ZCHH.js +328 -0
- package/dist/chunk-7SCYDG2G.js +2943 -0
- package/dist/chunk-7WJIIJCI.js +544 -0
- package/dist/chunk-7Y6TU2UA.js +150 -0
- package/dist/{chunk-EXV6JNLY.js → chunk-AKCXMT3X.js} +15 -22
- package/dist/{chunk-QC2M4NZY.js → chunk-BFVBHIHV.js} +72 -1
- package/dist/chunk-DSJETZT4.js +493 -0
- package/dist/{chunk-IW3D4I7Q.js → chunk-IU2SMWAM.js} +246 -4
- package/dist/{chunk-VICZ76TR.js → chunk-KAO3Z3CF.js} +43 -2
- package/dist/{chunk-CRDW6Q72.js → chunk-MA6VKJ3F.js} +87 -25
- package/dist/{chunk-6K5ZTGHQ.js → chunk-PEQY4N73.js} +16 -0
- package/dist/{chunk-DOADAH4L.js → chunk-PGV2QPIL.js} +40 -0
- package/dist/{chunk-Y63XQS2J.js → chunk-TOZQYJFG.js} +1 -1
- package/dist/cli.cjs +4528 -355
- package/dist/cli.d.cts +19 -3
- package/dist/cli.d.ts +19 -3
- package/dist/cli.js +206 -16
- package/dist/client.cjs +72 -1
- package/dist/client.d.cts +308 -6
- package/dist/client.d.ts +308 -6
- package/dist/client.js +1 -1
- package/dist/cms-C_vkAxYV.d.cts +1537 -0
- package/dist/cms-pqJyowu1.d.ts +1537 -0
- package/dist/config.d.cts +7 -1
- package/dist/config.d.ts +7 -1
- package/dist/drizzle-repo-CviH3EX7.d.cts +146 -0
- package/dist/drizzle-repo-DWLD0rWe.d.ts +146 -0
- package/dist/errors.cjs +82 -1
- package/dist/errors.d.cts +43 -3
- package/dist/errors.d.ts +43 -3
- package/dist/errors.js +2 -2
- package/dist/frontmatter.d.cts +5 -4
- package/dist/frontmatter.d.ts +5 -4
- package/dist/http.cjs +640 -94
- package/dist/http.d.cts +51 -2
- package/dist/http.d.ts +51 -2
- package/dist/http.js +203 -13
- package/dist/index.cjs +3759 -575
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +226 -30
- package/dist/markdown.cjs +89 -25
- package/dist/markdown.d.cts +57 -3
- package/dist/markdown.d.ts +57 -3
- package/dist/markdown.js +5 -1
- package/dist/mcp.cjs +314 -37
- package/dist/mcp.d.cts +3 -2
- package/dist/mcp.d.ts +3 -2
- package/dist/mcp.js +12 -9
- package/dist/messages/en.cjs +40 -0
- package/dist/messages/en.js +1 -1
- package/dist/messages/ko.cjs +40 -0
- package/dist/messages/ko.js +40 -0
- package/dist/registry/cms-audit.json +1 -1
- package/dist/registry/cms-doc-editor.json +15 -4
- package/dist/registry/cms-doc-list.json +2 -1
- package/dist/registry/cms-files.json +29 -0
- package/dist/registry/cms-lib.json +3 -3
- package/dist/registry/cms-media-picker.json +29 -0
- package/dist/registry/cms-media.json +28 -0
- package/dist/registry/cms-preview-comments.json +24 -0
- package/dist/registry/cms-releases.json +28 -0
- package/dist/registry/cms-shell.json +2 -2
- package/dist/registry/cms-trash.json +24 -0
- package/dist/registry/cms-versions.json +2 -1
- package/dist/registry/registry.json +163 -3
- package/dist/storage-s3.cjs +168 -0
- package/dist/storage-s3.d.cts +53 -0
- package/dist/storage-s3.d.ts +53 -0
- package/dist/storage-s3.js +143 -0
- package/dist/storage.cjs +373 -0
- package/dist/storage.d.cts +183 -0
- package/dist/storage.d.ts +183 -0
- package/dist/storage.js +49 -0
- package/dist/tables.cjs +251 -3
- package/dist/tables.d.cts +4965 -681
- package/dist/tables.d.ts +4965 -681
- package/dist/tables.js +14 -2
- package/dist/tools.cjs +331 -20
- package/dist/tools.d.cts +54 -7
- package/dist/tools.d.ts +54 -7
- package/dist/tools.js +16 -7
- package/dist/types.cjs +99 -1
- package/dist/types.d.cts +205 -5
- package/dist/types.d.ts +205 -5
- package/dist/types.js +5 -3
- package/package.json +32 -22
- package/dist/ai.cjs +0 -555
- package/dist/ai.d.cts +0 -48
- package/dist/ai.d.ts +0 -48
- package/dist/ai.js +0 -73
- package/dist/chunk-52NUUZC7.js +0 -1446
- package/dist/chunk-IGL2V6US.js +0 -324
- package/dist/chunk-QQV6VLYJ.js +0 -204
- package/dist/cms-BPp01z-8.d.ts +0 -411
- package/dist/cms-D6D89-qZ.d.cts +0 -411
- package/dist/drizzle-repo-B1rt7r4j.d.ts +0 -90
- package/dist/drizzle-repo-Cn-kL2FK.d.cts +0 -90
- package/dist/repo-BWI2Pkrv.d.ts +0 -162
- package/dist/repo-CeDxUxO6.d.cts +0 -162
|
@@ -0,0 +1,168 @@
|
|
|
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/storage-s3.ts
|
|
21
|
+
var storage_s3_exports = {};
|
|
22
|
+
__export(storage_s3_exports, {
|
|
23
|
+
S3Error: () => S3Error,
|
|
24
|
+
s3CorsRule: () => s3CorsRule,
|
|
25
|
+
s3Storage: () => s3Storage
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(storage_s3_exports);
|
|
28
|
+
var import_aws4fetch = require("aws4fetch");
|
|
29
|
+
var S3Error = class extends Error {
|
|
30
|
+
constructor(status, code, message, op) {
|
|
31
|
+
super(`S3 ${op} failed (${status} ${code}): ${message}`);
|
|
32
|
+
this.status = status;
|
|
33
|
+
this.code = code;
|
|
34
|
+
this.op = op;
|
|
35
|
+
this.name = "S3Error";
|
|
36
|
+
}
|
|
37
|
+
status;
|
|
38
|
+
code;
|
|
39
|
+
op;
|
|
40
|
+
};
|
|
41
|
+
var unescapeXml = (s) => s.replace(/"/g, '"').replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n))).replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(parseInt(n, 16))).replace(/&/g, "&");
|
|
42
|
+
var escapeXml = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
43
|
+
var tag = (xml, name) => {
|
|
44
|
+
const m = new RegExp(`<${name}>([\\s\\S]*?)</${name}>`).exec(xml);
|
|
45
|
+
return m ? unescapeXml(m[1]) : null;
|
|
46
|
+
};
|
|
47
|
+
var blocks = (xml, name) => [...xml.matchAll(new RegExp(`<${name}>([\\s\\S]*?)</${name}>`, "g"))].map((m) => m[1]);
|
|
48
|
+
function s3Storage(o) {
|
|
49
|
+
const region = o.region ?? "us-east-1";
|
|
50
|
+
const pathStyle = o.forcePathStyle ?? !!o.endpoint;
|
|
51
|
+
const endpoint = (o.endpoint ?? `https://s3.${region}.amazonaws.com`).replace(/\/+$/, "");
|
|
52
|
+
const root = pathStyle ? `${endpoint}/${o.bucket}` : endpoint.replace(/^(https?:\/\/)/, `$1${o.bucket}.`);
|
|
53
|
+
const prefix = o.prefix ? `${o.prefix.replace(/^\/+|\/+$/g, "")}/` : "";
|
|
54
|
+
const client = new import_aws4fetch.AwsClient({ accessKeyId: o.accessKeyId, secretAccessKey: o.secretAccessKey, sessionToken: o.sessionToken, service: "s3", region, retries: 2 });
|
|
55
|
+
const doFetch = o.fetch ?? fetch;
|
|
56
|
+
const full = (key) => prefix + key;
|
|
57
|
+
const enc = (key) => full(key).split("/").map(encodeURIComponent).join("/");
|
|
58
|
+
const urlOf = (key, query = {}) => {
|
|
59
|
+
const u = new URL(`${root}/${enc(key)}`);
|
|
60
|
+
for (const [k, v] of Object.entries(query)) if (v !== void 0) u.searchParams.set(k, String(v));
|
|
61
|
+
return u;
|
|
62
|
+
};
|
|
63
|
+
const putHeaders = (p) => {
|
|
64
|
+
const h = { "content-type": p.mime };
|
|
65
|
+
if (p.disposition) h["content-disposition"] = p.disposition;
|
|
66
|
+
if (p.cacheControl) h["cache-control"] = p.cacheControl;
|
|
67
|
+
return h;
|
|
68
|
+
};
|
|
69
|
+
async function call(op, url, init = {}, okStatus = []) {
|
|
70
|
+
const res = await doFetch(await client.sign(url.toString(), init));
|
|
71
|
+
if (res.ok || okStatus.includes(res.status)) return res;
|
|
72
|
+
const text = await res.text().catch(() => "");
|
|
73
|
+
throw new S3Error(res.status, tag(text, "Code") ?? String(res.status), tag(text, "Message") ?? res.statusText, op);
|
|
74
|
+
}
|
|
75
|
+
async function presign(method, url, expires, headers = {}) {
|
|
76
|
+
url.searchParams.set("X-Amz-Expires", String(Math.max(1, Math.min(expires, 604800))));
|
|
77
|
+
const signed = await client.sign(url.toString(), { method, headers, aws: { signQuery: true, allHeaders: true } });
|
|
78
|
+
return signed.url;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
options: o,
|
|
82
|
+
async presignPut(key, p) {
|
|
83
|
+
const headers = putHeaders(p);
|
|
84
|
+
return { url: await presign("PUT", urlOf(key), p.expires, headers), headers };
|
|
85
|
+
},
|
|
86
|
+
async put(key, body, p) {
|
|
87
|
+
await call("put", urlOf(key), { method: "PUT", headers: putHeaders(p), body });
|
|
88
|
+
},
|
|
89
|
+
async createMultipart(key, p) {
|
|
90
|
+
const res = await call("createMultipart", urlOf(key, { uploads: "" }), { method: "POST", headers: putHeaders(p) });
|
|
91
|
+
const id = tag(await res.text(), "UploadId");
|
|
92
|
+
if (!id) throw new S3Error(res.status, "NoUploadId", "the storage did not return an upload id", "createMultipart");
|
|
93
|
+
return id;
|
|
94
|
+
},
|
|
95
|
+
presignPart(key, uploadId, partNumber, expires) {
|
|
96
|
+
return presign("PUT", urlOf(key, { partNumber, uploadId }), expires);
|
|
97
|
+
},
|
|
98
|
+
async listParts(key, uploadId) {
|
|
99
|
+
const out = [];
|
|
100
|
+
let marker;
|
|
101
|
+
for (let page = 0; page < 20; page++) {
|
|
102
|
+
const xml = await (await call("listParts", urlOf(key, { uploadId, "max-parts": 1e3, "part-number-marker": marker }))).text();
|
|
103
|
+
for (const b of blocks(xml, "Part")) out.push({ partNumber: Number(tag(b, "PartNumber")), etag: tag(b, "ETag") ?? "", size: Number(tag(b, "Size") ?? 0) });
|
|
104
|
+
if (tag(xml, "IsTruncated") !== "true") break;
|
|
105
|
+
marker = tag(xml, "NextPartNumberMarker") ?? void 0;
|
|
106
|
+
if (!marker) break;
|
|
107
|
+
}
|
|
108
|
+
return out.sort((a, b) => a.partNumber - b.partNumber);
|
|
109
|
+
},
|
|
110
|
+
async completeMultipart(key, uploadId, parts) {
|
|
111
|
+
const body = `<CompleteMultipartUpload>${parts.map((p) => `<Part><PartNumber>${p.partNumber}</PartNumber><ETag>${escapeXml(p.etag)}</ETag></Part>`).join("")}</CompleteMultipartUpload>`;
|
|
112
|
+
const res = await call("completeMultipart", urlOf(key, { uploadId }), { method: "POST", headers: { "content-type": "application/xml" }, body });
|
|
113
|
+
const text = await res.text();
|
|
114
|
+
if (/<Error>/.test(text)) throw new S3Error(res.status, tag(text, "Code") ?? "Error", tag(text, "Message") ?? "", "completeMultipart");
|
|
115
|
+
},
|
|
116
|
+
async abortMultipart(key, uploadId) {
|
|
117
|
+
await call("abortMultipart", urlOf(key, { uploadId }), { method: "DELETE" }, [404]);
|
|
118
|
+
},
|
|
119
|
+
async head(key) {
|
|
120
|
+
const res = await call("head", urlOf(key), { method: "HEAD" }, [404]);
|
|
121
|
+
if (res.status === 404) return null;
|
|
122
|
+
return { key, size: Number(res.headers.get("content-length") ?? 0), mime: res.headers.get("content-type"), etag: res.headers.get("etag"), lastModified: res.headers.get("last-modified") ? new Date(res.headers.get("last-modified")).toISOString() : null };
|
|
123
|
+
},
|
|
124
|
+
async readStart(key, bytes) {
|
|
125
|
+
const res = await call("read", urlOf(key), { headers: { range: `bytes=0-${Math.max(0, bytes - 1)}` } }, [404, 416]);
|
|
126
|
+
if (res.status === 404) return null;
|
|
127
|
+
if (res.status === 416) return new Uint8Array();
|
|
128
|
+
return new Uint8Array(await res.arrayBuffer()).slice(0, bytes);
|
|
129
|
+
},
|
|
130
|
+
presignGet(key, g) {
|
|
131
|
+
const q = {};
|
|
132
|
+
if (g.download) q["response-content-disposition"] = `attachment; filename*=UTF-8''${encodeURIComponent(g.download)}`;
|
|
133
|
+
return presign("GET", urlOf(key, q), g.expires);
|
|
134
|
+
},
|
|
135
|
+
publicUrl(key) {
|
|
136
|
+
return o.publicUrl ? `${o.publicUrl.replace(/\/+$/, "")}/${enc(key)}` : null;
|
|
137
|
+
},
|
|
138
|
+
async copy(from, to) {
|
|
139
|
+
const res = await call("copy", urlOf(to), { method: "PUT", headers: { "x-amz-copy-source": `/${o.bucket}/${enc(from)}` } });
|
|
140
|
+
const text = await res.text();
|
|
141
|
+
if (/<Error>/.test(text)) throw new S3Error(res.status, tag(text, "Code") ?? "Error", tag(text, "Message") ?? "", "copy");
|
|
142
|
+
},
|
|
143
|
+
async delete(key) {
|
|
144
|
+
await call("delete", urlOf(key), { method: "DELETE" }, [404]);
|
|
145
|
+
},
|
|
146
|
+
async list(l = {}) {
|
|
147
|
+
const u = new URL(`${root}/`);
|
|
148
|
+
const q = { "list-type": 2, prefix: prefix + (l.prefix ?? ""), delimiter: l.delimiter, "continuation-token": l.cursor ?? void 0, "max-keys": l.limit ?? 1e3 };
|
|
149
|
+
for (const [k, v] of Object.entries(q)) if (v !== void 0 && v !== "") u.searchParams.set(k, String(v));
|
|
150
|
+
const xml = await (await call("list", u)).text();
|
|
151
|
+
const strip = (k) => k.startsWith(prefix) ? k.slice(prefix.length) : k;
|
|
152
|
+
return {
|
|
153
|
+
objects: blocks(xml, "Contents").map((b) => ({ key: strip(tag(b, "Key") ?? ""), size: Number(tag(b, "Size") ?? 0), mime: null, etag: tag(b, "ETag"), lastModified: tag(b, "LastModified") })),
|
|
154
|
+
prefixes: blocks(xml, "CommonPrefixes").map((b) => strip(tag(b, "Prefix") ?? "")),
|
|
155
|
+
cursor: tag(xml, "IsTruncated") === "true" ? tag(xml, "NextContinuationToken") : null
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function s3CorsRule(origins) {
|
|
161
|
+
return [{ AllowedOrigins: [...origins], AllowedMethods: ["GET", "HEAD", "PUT"], AllowedHeaders: ["*"], ExposeHeaders: ["ETag"], MaxAgeSeconds: 3600 }];
|
|
162
|
+
}
|
|
163
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
164
|
+
0 && (module.exports = {
|
|
165
|
+
S3Error,
|
|
166
|
+
s3CorsRule,
|
|
167
|
+
s3Storage
|
|
168
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { StorageAdapter } from './storage.cjs';
|
|
2
|
+
import './types.cjs';
|
|
3
|
+
import 'zod';
|
|
4
|
+
import './errors.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* S3-compatible storage (AWS S3, Cloudflare R2, MinIO and others) as a
|
|
8
|
+
* `StorageAdapter`. Requests are signed with `aws4fetch` (no AWS SDK), so it
|
|
9
|
+
* runs anywhere `fetch` and Web Crypto do.
|
|
10
|
+
*
|
|
11
|
+
* s3Storage({ bucket, region, accessKeyId, secretAccessKey }) // AWS
|
|
12
|
+
* s3Storage({ bucket, region: "auto", endpoint: "https://<id>.r2.cloudflarestorage.com", …, publicUrl }) // R2
|
|
13
|
+
*
|
|
14
|
+
* The bucket needs a CORS rule that lets your site PUT to it; `agent-cms
|
|
15
|
+
* storage check` prints the rule and tests it.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
interface S3StorageOptions {
|
|
19
|
+
bucket: string;
|
|
20
|
+
/** `auto` for R2. Default `us-east-1`. */
|
|
21
|
+
region?: string;
|
|
22
|
+
/** Leave out for AWS. For others: the S3 API address, without the bucket. */
|
|
23
|
+
endpoint?: string;
|
|
24
|
+
accessKeyId: string;
|
|
25
|
+
secretAccessKey: string;
|
|
26
|
+
sessionToken?: string;
|
|
27
|
+
/** `https://host/bucket/key` instead of `https://bucket.host/key`. Default: true with `endpoint`, false on AWS. */
|
|
28
|
+
forcePathStyle?: boolean;
|
|
29
|
+
/** Where the bucket's objects are public (a CDN or the bucket's public address), without a trailing slash. */
|
|
30
|
+
publicUrl?: string | null;
|
|
31
|
+
/** Put every key under this folder of the bucket. */
|
|
32
|
+
prefix?: string;
|
|
33
|
+
fetch?: typeof fetch;
|
|
34
|
+
}
|
|
35
|
+
declare class S3Error extends Error {
|
|
36
|
+
readonly status: number;
|
|
37
|
+
readonly code: string;
|
|
38
|
+
readonly op: string;
|
|
39
|
+
constructor(status: number, code: string, message: string, op: string);
|
|
40
|
+
}
|
|
41
|
+
declare function s3Storage(o: S3StorageOptions): StorageAdapter & {
|
|
42
|
+
readonly options: Readonly<S3StorageOptions>;
|
|
43
|
+
};
|
|
44
|
+
/** The CORS rule a bucket needs so browsers on `origins` can upload to it. */
|
|
45
|
+
declare function s3CorsRule(origins: readonly string[]): {
|
|
46
|
+
AllowedOrigins: string[];
|
|
47
|
+
AllowedMethods: string[];
|
|
48
|
+
AllowedHeaders: string[];
|
|
49
|
+
ExposeHeaders: string[];
|
|
50
|
+
MaxAgeSeconds: number;
|
|
51
|
+
}[];
|
|
52
|
+
|
|
53
|
+
export { S3Error, type S3StorageOptions, s3CorsRule, s3Storage };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { StorageAdapter } from './storage.js';
|
|
2
|
+
import './types.js';
|
|
3
|
+
import 'zod';
|
|
4
|
+
import './errors.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* S3-compatible storage (AWS S3, Cloudflare R2, MinIO and others) as a
|
|
8
|
+
* `StorageAdapter`. Requests are signed with `aws4fetch` (no AWS SDK), so it
|
|
9
|
+
* runs anywhere `fetch` and Web Crypto do.
|
|
10
|
+
*
|
|
11
|
+
* s3Storage({ bucket, region, accessKeyId, secretAccessKey }) // AWS
|
|
12
|
+
* s3Storage({ bucket, region: "auto", endpoint: "https://<id>.r2.cloudflarestorage.com", …, publicUrl }) // R2
|
|
13
|
+
*
|
|
14
|
+
* The bucket needs a CORS rule that lets your site PUT to it; `agent-cms
|
|
15
|
+
* storage check` prints the rule and tests it.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
interface S3StorageOptions {
|
|
19
|
+
bucket: string;
|
|
20
|
+
/** `auto` for R2. Default `us-east-1`. */
|
|
21
|
+
region?: string;
|
|
22
|
+
/** Leave out for AWS. For others: the S3 API address, without the bucket. */
|
|
23
|
+
endpoint?: string;
|
|
24
|
+
accessKeyId: string;
|
|
25
|
+
secretAccessKey: string;
|
|
26
|
+
sessionToken?: string;
|
|
27
|
+
/** `https://host/bucket/key` instead of `https://bucket.host/key`. Default: true with `endpoint`, false on AWS. */
|
|
28
|
+
forcePathStyle?: boolean;
|
|
29
|
+
/** Where the bucket's objects are public (a CDN or the bucket's public address), without a trailing slash. */
|
|
30
|
+
publicUrl?: string | null;
|
|
31
|
+
/** Put every key under this folder of the bucket. */
|
|
32
|
+
prefix?: string;
|
|
33
|
+
fetch?: typeof fetch;
|
|
34
|
+
}
|
|
35
|
+
declare class S3Error extends Error {
|
|
36
|
+
readonly status: number;
|
|
37
|
+
readonly code: string;
|
|
38
|
+
readonly op: string;
|
|
39
|
+
constructor(status: number, code: string, message: string, op: string);
|
|
40
|
+
}
|
|
41
|
+
declare function s3Storage(o: S3StorageOptions): StorageAdapter & {
|
|
42
|
+
readonly options: Readonly<S3StorageOptions>;
|
|
43
|
+
};
|
|
44
|
+
/** The CORS rule a bucket needs so browsers on `origins` can upload to it. */
|
|
45
|
+
declare function s3CorsRule(origins: readonly string[]): {
|
|
46
|
+
AllowedOrigins: string[];
|
|
47
|
+
AllowedMethods: string[];
|
|
48
|
+
AllowedHeaders: string[];
|
|
49
|
+
ExposeHeaders: string[];
|
|
50
|
+
MaxAgeSeconds: number;
|
|
51
|
+
}[];
|
|
52
|
+
|
|
53
|
+
export { S3Error, type S3StorageOptions, s3CorsRule, s3Storage };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import "./chunk-2NTK7H4W.js";
|
|
2
|
+
|
|
3
|
+
// src/storage-s3.ts
|
|
4
|
+
import { AwsClient } from "aws4fetch";
|
|
5
|
+
var S3Error = class extends Error {
|
|
6
|
+
constructor(status, code, message, op) {
|
|
7
|
+
super(`S3 ${op} failed (${status} ${code}): ${message}`);
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.op = op;
|
|
11
|
+
this.name = "S3Error";
|
|
12
|
+
}
|
|
13
|
+
status;
|
|
14
|
+
code;
|
|
15
|
+
op;
|
|
16
|
+
};
|
|
17
|
+
var unescapeXml = (s) => s.replace(/"/g, '"').replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n))).replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(parseInt(n, 16))).replace(/&/g, "&");
|
|
18
|
+
var escapeXml = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
19
|
+
var tag = (xml, name) => {
|
|
20
|
+
const m = new RegExp(`<${name}>([\\s\\S]*?)</${name}>`).exec(xml);
|
|
21
|
+
return m ? unescapeXml(m[1]) : null;
|
|
22
|
+
};
|
|
23
|
+
var blocks = (xml, name) => [...xml.matchAll(new RegExp(`<${name}>([\\s\\S]*?)</${name}>`, "g"))].map((m) => m[1]);
|
|
24
|
+
function s3Storage(o) {
|
|
25
|
+
const region = o.region ?? "us-east-1";
|
|
26
|
+
const pathStyle = o.forcePathStyle ?? !!o.endpoint;
|
|
27
|
+
const endpoint = (o.endpoint ?? `https://s3.${region}.amazonaws.com`).replace(/\/+$/, "");
|
|
28
|
+
const root = pathStyle ? `${endpoint}/${o.bucket}` : endpoint.replace(/^(https?:\/\/)/, `$1${o.bucket}.`);
|
|
29
|
+
const prefix = o.prefix ? `${o.prefix.replace(/^\/+|\/+$/g, "")}/` : "";
|
|
30
|
+
const client = new AwsClient({ accessKeyId: o.accessKeyId, secretAccessKey: o.secretAccessKey, sessionToken: o.sessionToken, service: "s3", region, retries: 2 });
|
|
31
|
+
const doFetch = o.fetch ?? fetch;
|
|
32
|
+
const full = (key) => prefix + key;
|
|
33
|
+
const enc = (key) => full(key).split("/").map(encodeURIComponent).join("/");
|
|
34
|
+
const urlOf = (key, query = {}) => {
|
|
35
|
+
const u = new URL(`${root}/${enc(key)}`);
|
|
36
|
+
for (const [k, v] of Object.entries(query)) if (v !== void 0) u.searchParams.set(k, String(v));
|
|
37
|
+
return u;
|
|
38
|
+
};
|
|
39
|
+
const putHeaders = (p) => {
|
|
40
|
+
const h = { "content-type": p.mime };
|
|
41
|
+
if (p.disposition) h["content-disposition"] = p.disposition;
|
|
42
|
+
if (p.cacheControl) h["cache-control"] = p.cacheControl;
|
|
43
|
+
return h;
|
|
44
|
+
};
|
|
45
|
+
async function call(op, url, init = {}, okStatus = []) {
|
|
46
|
+
const res = await doFetch(await client.sign(url.toString(), init));
|
|
47
|
+
if (res.ok || okStatus.includes(res.status)) return res;
|
|
48
|
+
const text = await res.text().catch(() => "");
|
|
49
|
+
throw new S3Error(res.status, tag(text, "Code") ?? String(res.status), tag(text, "Message") ?? res.statusText, op);
|
|
50
|
+
}
|
|
51
|
+
async function presign(method, url, expires, headers = {}) {
|
|
52
|
+
url.searchParams.set("X-Amz-Expires", String(Math.max(1, Math.min(expires, 604800))));
|
|
53
|
+
const signed = await client.sign(url.toString(), { method, headers, aws: { signQuery: true, allHeaders: true } });
|
|
54
|
+
return signed.url;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
options: o,
|
|
58
|
+
async presignPut(key, p) {
|
|
59
|
+
const headers = putHeaders(p);
|
|
60
|
+
return { url: await presign("PUT", urlOf(key), p.expires, headers), headers };
|
|
61
|
+
},
|
|
62
|
+
async put(key, body, p) {
|
|
63
|
+
await call("put", urlOf(key), { method: "PUT", headers: putHeaders(p), body });
|
|
64
|
+
},
|
|
65
|
+
async createMultipart(key, p) {
|
|
66
|
+
const res = await call("createMultipart", urlOf(key, { uploads: "" }), { method: "POST", headers: putHeaders(p) });
|
|
67
|
+
const id = tag(await res.text(), "UploadId");
|
|
68
|
+
if (!id) throw new S3Error(res.status, "NoUploadId", "the storage did not return an upload id", "createMultipart");
|
|
69
|
+
return id;
|
|
70
|
+
},
|
|
71
|
+
presignPart(key, uploadId, partNumber, expires) {
|
|
72
|
+
return presign("PUT", urlOf(key, { partNumber, uploadId }), expires);
|
|
73
|
+
},
|
|
74
|
+
async listParts(key, uploadId) {
|
|
75
|
+
const out = [];
|
|
76
|
+
let marker;
|
|
77
|
+
for (let page = 0; page < 20; page++) {
|
|
78
|
+
const xml = await (await call("listParts", urlOf(key, { uploadId, "max-parts": 1e3, "part-number-marker": marker }))).text();
|
|
79
|
+
for (const b of blocks(xml, "Part")) out.push({ partNumber: Number(tag(b, "PartNumber")), etag: tag(b, "ETag") ?? "", size: Number(tag(b, "Size") ?? 0) });
|
|
80
|
+
if (tag(xml, "IsTruncated") !== "true") break;
|
|
81
|
+
marker = tag(xml, "NextPartNumberMarker") ?? void 0;
|
|
82
|
+
if (!marker) break;
|
|
83
|
+
}
|
|
84
|
+
return out.sort((a, b) => a.partNumber - b.partNumber);
|
|
85
|
+
},
|
|
86
|
+
async completeMultipart(key, uploadId, parts) {
|
|
87
|
+
const body = `<CompleteMultipartUpload>${parts.map((p) => `<Part><PartNumber>${p.partNumber}</PartNumber><ETag>${escapeXml(p.etag)}</ETag></Part>`).join("")}</CompleteMultipartUpload>`;
|
|
88
|
+
const res = await call("completeMultipart", urlOf(key, { uploadId }), { method: "POST", headers: { "content-type": "application/xml" }, body });
|
|
89
|
+
const text = await res.text();
|
|
90
|
+
if (/<Error>/.test(text)) throw new S3Error(res.status, tag(text, "Code") ?? "Error", tag(text, "Message") ?? "", "completeMultipart");
|
|
91
|
+
},
|
|
92
|
+
async abortMultipart(key, uploadId) {
|
|
93
|
+
await call("abortMultipart", urlOf(key, { uploadId }), { method: "DELETE" }, [404]);
|
|
94
|
+
},
|
|
95
|
+
async head(key) {
|
|
96
|
+
const res = await call("head", urlOf(key), { method: "HEAD" }, [404]);
|
|
97
|
+
if (res.status === 404) return null;
|
|
98
|
+
return { key, size: Number(res.headers.get("content-length") ?? 0), mime: res.headers.get("content-type"), etag: res.headers.get("etag"), lastModified: res.headers.get("last-modified") ? new Date(res.headers.get("last-modified")).toISOString() : null };
|
|
99
|
+
},
|
|
100
|
+
async readStart(key, bytes) {
|
|
101
|
+
const res = await call("read", urlOf(key), { headers: { range: `bytes=0-${Math.max(0, bytes - 1)}` } }, [404, 416]);
|
|
102
|
+
if (res.status === 404) return null;
|
|
103
|
+
if (res.status === 416) return new Uint8Array();
|
|
104
|
+
return new Uint8Array(await res.arrayBuffer()).slice(0, bytes);
|
|
105
|
+
},
|
|
106
|
+
presignGet(key, g) {
|
|
107
|
+
const q = {};
|
|
108
|
+
if (g.download) q["response-content-disposition"] = `attachment; filename*=UTF-8''${encodeURIComponent(g.download)}`;
|
|
109
|
+
return presign("GET", urlOf(key, q), g.expires);
|
|
110
|
+
},
|
|
111
|
+
publicUrl(key) {
|
|
112
|
+
return o.publicUrl ? `${o.publicUrl.replace(/\/+$/, "")}/${enc(key)}` : null;
|
|
113
|
+
},
|
|
114
|
+
async copy(from, to) {
|
|
115
|
+
const res = await call("copy", urlOf(to), { method: "PUT", headers: { "x-amz-copy-source": `/${o.bucket}/${enc(from)}` } });
|
|
116
|
+
const text = await res.text();
|
|
117
|
+
if (/<Error>/.test(text)) throw new S3Error(res.status, tag(text, "Code") ?? "Error", tag(text, "Message") ?? "", "copy");
|
|
118
|
+
},
|
|
119
|
+
async delete(key) {
|
|
120
|
+
await call("delete", urlOf(key), { method: "DELETE" }, [404]);
|
|
121
|
+
},
|
|
122
|
+
async list(l = {}) {
|
|
123
|
+
const u = new URL(`${root}/`);
|
|
124
|
+
const q = { "list-type": 2, prefix: prefix + (l.prefix ?? ""), delimiter: l.delimiter, "continuation-token": l.cursor ?? void 0, "max-keys": l.limit ?? 1e3 };
|
|
125
|
+
for (const [k, v] of Object.entries(q)) if (v !== void 0 && v !== "") u.searchParams.set(k, String(v));
|
|
126
|
+
const xml = await (await call("list", u)).text();
|
|
127
|
+
const strip = (k) => k.startsWith(prefix) ? k.slice(prefix.length) : k;
|
|
128
|
+
return {
|
|
129
|
+
objects: blocks(xml, "Contents").map((b) => ({ key: strip(tag(b, "Key") ?? ""), size: Number(tag(b, "Size") ?? 0), mime: null, etag: tag(b, "ETag"), lastModified: tag(b, "LastModified") })),
|
|
130
|
+
prefixes: blocks(xml, "CommonPrefixes").map((b) => strip(tag(b, "Prefix") ?? "")),
|
|
131
|
+
cursor: tag(xml, "IsTruncated") === "true" ? tag(xml, "NextContinuationToken") : null
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function s3CorsRule(origins) {
|
|
137
|
+
return [{ AllowedOrigins: [...origins], AllowedMethods: ["GET", "HEAD", "PUT"], AllowedHeaders: ["*"], ExposeHeaders: ["ETag"], MaxAgeSeconds: 3600 }];
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
S3Error,
|
|
141
|
+
s3CorsRule,
|
|
142
|
+
s3Storage
|
|
143
|
+
};
|