Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/S3Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ const xmlParser = new XMLParser({
jPath === "ListBucketResult.Contents" ||
jPath === "ListPartsResult.Part" ||
jPath === "DeleteResult.Deleted" ||
jPath === "DeleteResult.Error",
jPath === "DeleteResult.Error" ||
jPath === "CORSConfiguration.CORSRule" ||
jPath === "CORSConfiguration.CORSRule.AllowedMethod" ||
jPath === "CORSConfiguration.CORSRule.AllowedOrigin" ||
jPath === "CORSConfiguration.CORSRule.AllowedHeader" ||
jPath === "CORSConfiguration.CORSRule.ExposeHeader",
});
const xmlBuilder = new XMLBuilder({
attributeNamePrefix: "$",
Expand Down Expand Up @@ -1450,10 +1455,31 @@ export default class S3Client {
throw fromStatusCode(response.statusCode, "");
}

// const text = await response.body.text();
// console.log(text)
const text = await response.body.text();
const root = ensureParsedXml(text).CORSConfiguration ?? {};

const rulesArray = root.CORSRule ?? [];

throw new Error("Not implemented");
return {
rules: rulesArray
.filter(Boolean)
// biome-ignore lint/suspicious/noExplicitAny: parsing code
.map((r: any) => ({
allowedMethods: (r.AllowedMethod ?? []) as HttpMethod[],
allowedOrigins: (r.AllowedOrigin ?? []) as string[],
allowedHeaders: r.AllowedHeader
? (r.AllowedHeader as string[])
: undefined,
exposeHeaders: r.ExposeHeader
? (r.ExposeHeader as string[])
: undefined,
id: r.ID ?? undefined,
maxAgeSeconds:
typeof r.MaxAgeSeconds !== "undefined" && r.MaxAgeSeconds !== null
? Number(r.MaxAgeSeconds)
: undefined,
})),
};
}

/**
Expand Down
52 changes: 45 additions & 7 deletions src/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1296,11 +1296,9 @@ export function runTests(
test("put", async t => {
if (
implementation === "minio" ||
implementation === "garage" ||
implementation === "ceph" ||
implementation === "localstack" ||
implementation === "rustfs" ||
implementation === "s3mock" ||
implementation === "rustfs" ||
implementation === "backblaze" ||
implementation === "cloudflare"
) {
Expand All @@ -1312,8 +1310,6 @@ export function runTests(
return;
}

t.todo("Not yet implemented");

await client.putBucketCors([
{
allowedMethods: ["GET"],
Expand All @@ -1322,18 +1318,60 @@ export function runTests(
},
]);

const { rules } = await client.getBucketCors();
let { rules } = await client.getBucketCors();
expect(rules).toStrictEqual([
{
allowedMethods: ["GET"],
allowedOrigins: ["https://example.com"],
allowedHeaders: undefined,
allowedHeaders: ["*"],
exposeHeaders: undefined,
id: undefined,
maxAgeSeconds: undefined,
},
]);

await client.putBucketCors([
{
allowedMethods: ["GET"],
allowedOrigins: ["https://example.com"],
allowedHeaders: ["*"],
exposeHeaders: ["Content-Type"],
},
]);

rules = (await client.getBucketCors()).rules;
expect(rules).toStrictEqual([
{
allowedMethods: ["GET"],
allowedOrigins: ["https://example.com"],
allowedHeaders: ["*"],
exposeHeaders: ["Content-Type"],
id: undefined,
maxAgeSeconds: undefined,
},
]);

await client.putBucketCors([
{
allowedMethods: ["GET"],
allowedOrigins: ["https://example.com"],
allowedHeaders: ["*"],
exposeHeaders: ["Content-Type"],
id: "fancy-id",
},
]);

rules = (await client.getBucketCors()).rules;
expect(rules).toStrictEqual([
{
allowedMethods: ["GET"],
allowedOrigins: ["https://example.com"],
allowedHeaders: ["*"],
exposeHeaders: ["Content-Type"],
id: "fancy-id",
maxAgeSeconds: undefined,
},
]);
await client.deleteBucketCors();

expect(client.getBucketCors()).rejects.toThrow();
Expand Down