Skip to content

Commit fca9b76

Browse files
authored
Revert "171 feat support non json content types in write attachmentsread attachments (#172)" (#174)
This reverts commit a71b7aa.
1 parent 66088af commit fca9b76

3 files changed

Lines changed: 8 additions & 59 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
### Added
11-
12-
- Support non-JSON content types in `writeAttachments` and `readAttachments`, [PR-172](https://github.com/reductstore/reduct-js/pull/172)
13-
1410
### Security
1511

1612
- Restrict GitHub Actions `GITHUB_TOKEN` default permissions to `contents: read` in CI workflow and remove unnecessary OIDC write access.

src/Bucket.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// @ts-ignore`
2-
import { Buffer } from "buffer";
32
import { BucketSettings } from "./messages/BucketSettings";
43
import { BucketInfo } from "./messages/BucketInfo";
54
import { EntryInfo } from "./messages/EntryInfo";
@@ -589,20 +588,20 @@ export class Bucket {
589588
async writeAttachments(
590589
entry: string,
591590
attachments: Record<string, unknown>,
592-
contentType?: string,
593591
): Promise<void> {
594-
const ct = contentType ?? "application/json";
595-
const isJson = isJsonContentType(ct);
596592
const batch = this.beginWriteRecordBatch();
597593
const entryName = `${entry}/$meta`;
598594
const baseTs = BigInt(Date.now()) * 1000n;
599595
let offset = 0n;
600596

601597
for (const [key, content] of Object.entries(attachments)) {
602-
const data = isJson
603-
? JSON.stringify(content)
604-
: Buffer.from(content as string, "base64");
605-
batch.add(entryName, baseTs + offset, data, ct, { key });
598+
batch.add(
599+
entryName,
600+
baseTs + offset,
601+
JSON.stringify(content),
602+
"application/json",
603+
{ key },
604+
);
606605
offset += 1n;
607606
}
608607

@@ -625,12 +624,7 @@ export class Bucket {
625624
}
626625

627626
const key = record.labels["key"].toString();
628-
if (isJsonContentType(record.contentType ?? "")) {
629-
attachments[key] = JSON.parse(await record.readAsString());
630-
} else {
631-
const buf = await record.read();
632-
attachments[key] = buf.toString("base64");
633-
}
627+
attachments[key] = JSON.parse(await record.readAsString());
634628
}
635629

636630
return attachments;
@@ -674,10 +668,3 @@ export class Bucket {
674668
await batch.send();
675669
}
676670
}
677-
678-
function isJsonContentType(contentType: string): boolean {
679-
const ct = contentType.split(";")[0].trim().toLowerCase();
680-
return (
681-
ct === "application/json" || ct === "text/json" || ct.endsWith("+json")
682-
);
683-
}

test/Bucket.test.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -864,40 +864,6 @@ describe("Bucket", () => {
864864
});
865865
},
866866
);
867-
868-
it_api("1.19", true)(
869-
"should write and read non-JSON attachments",
870-
async () => {
871-
const bucket: Bucket = await client.getBucket("bucket");
872-
const rawBytes = Buffer.from([0xde, 0xad, 0xbe, 0xef]);
873-
const encoded = rawBytes.toString("base64");
874-
875-
await bucket.writeAttachments(
876-
"entry-1",
877-
{ "binary-data": encoded },
878-
"application/octet-stream",
879-
);
880-
881-
const attachments = await bucket.readAttachments("entry-1");
882-
expect(attachments["binary-data"]).toEqual(encoded);
883-
},
884-
);
885-
886-
it_api("1.19", true)(
887-
"should write and read JSON attachments with default content type",
888-
async () => {
889-
const bucket: Bucket = await client.getBucket("bucket");
890-
await bucket.writeAttachments("entry-1", {
891-
"meta-1": { enabled: true, values: [1, 2, 3] },
892-
"meta-2": { name: "test" },
893-
});
894-
895-
await expect(bucket.readAttachments("entry-1")).resolves.toEqual({
896-
"meta-1": { enabled: true, values: [1, 2, 3] },
897-
"meta-2": { name: "test" },
898-
});
899-
},
900-
);
901867
});
902868

903869
describe("rename", () => {

0 commit comments

Comments
 (0)