Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix: improve consistency of assetdb between local and s3 when a range is provided

Mohamed Bassem546a89b0f55e
diff --git a/packages/e2e_tests/tests/assetdb/interface-compliance.test.ts b/packages/e2e_tests/tests/assetdb/interface-compliance.test.ts
index d5288c7a..66d227d5 100644
--- a/packages/e2e_tests/tests/assetdb/interface-compliance.test.ts
+++ b/packages/e2e_tests/tests/assetdb/interface-compliance.test.ts
@@ -187,6 +187,152 @@ describe.each([
const streamedContent = await streamToBuffer(stream);
expect(streamedContent.toString()).toBe("56789a");
});
+
+ it("should support start-only range requests in streams", async () => {
+ const content = Buffer.from("0123456789abcdef");
+ const testData = createTestAssetData({ content });
+
+ await context.store.saveAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ asset: testData.content,
+ metadata: testData.metadata,
+ });
+
+ const stream = await context.store.createAssetReadStream({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ start: 10,
+ });
+
+ const streamedContent = await streamToBuffer(stream);
+ expect(streamedContent.toString()).toBe("abcdef");
+ });
+
+ it("should support end-only range requests in streams", async () => {
+ const content = Buffer.from("0123456789abcdef");
+ const testData = createTestAssetData({ content });
+
+ await context.store.saveAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ asset: testData.content,
+ metadata: testData.metadata,
+ });
+
+ const stream = await context.store.createAssetReadStream({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ end: 5,
+ });
+
+ const streamedContent = await streamToBuffer(stream);
+ expect(streamedContent.toString()).toBe("012345");
+ });
+ });
+
+ describe("Range Read Operations", () => {
+ it("should support range requests in readAsset", async () => {
+ const content = Buffer.from("0123456789abcdef");
+ const testData = createTestAssetData({ content });
+
+ await context.store.saveAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ asset: testData.content,
+ metadata: testData.metadata,
+ });
+
+ const { asset, metadata } = await context.store.readAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ start: 5,
+ end: 10,
+ });
+
+ expect(asset.toString()).toBe("56789a");
+ expect(metadata).toEqual(testData.metadata);
+ });
+
+ it("should support start-only range requests in readAsset", async () => {
+ const content = Buffer.from("0123456789abcdef");
+ const testData = createTestAssetData({ content });
+
+ await context.store.saveAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ asset: testData.content,
+ metadata: testData.metadata,
+ });
+
+ const { asset } = await context.store.readAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ start: 10,
+ });
+
+ expect(asset.toString()).toBe("abcdef");
+ });
+
+ it("should support end-only range requests in readAsset", async () => {
+ const content = Buffer.from("0123456789abcdef");
+ const testData = createTestAssetData({ content });
+
+ await context.store.saveAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ asset: testData.content,
+ metadata: testData.metadata,
+ });
+
+ const { asset } = await context.store.readAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ end: 5,
+ });
+
+ expect(asset.toString()).toBe("012345");
+ });
+
+ it("should handle end larger than file size in readAsset", async () => {
+ const content = Buffer.from("0123456789abcdef");
+ const testData = createTestAssetData({ content });
+
+ await context.store.saveAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ asset: testData.content,
+ metadata: testData.metadata,
+ });
+
+ const { asset } = await context.store.readAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ start: 10,
+ end: 1000,
+ });
+
+ expect(asset.toString()).toBe("abcdef");
+ });
+
+ it("should return full content when no range is specified in readAsset", async () => {
+ const content = Buffer.from("0123456789abcdef");
+ const testData = createTestAssetData({ content });
+
+ await context.store.saveAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ asset: testData.content,
+ metadata: testData.metadata,
+ });
+
+ const { asset } = await context.store.readAsset({
+ userId: testData.userId,
+ assetId: testData.assetId,
+ });
+
+ expect(asset.toString()).toBe("0123456789abcdef");
+ });
});
describe("Asset Types Support", () => {
diff --git a/packages/shared/assetdb.ts b/packages/shared/assetdb.ts
index 2e22faf7..8f98d0fd 100644
--- a/packages/shared/assetdb.ts
+++ b/packages/shared/assetdb.ts
@@ -102,6 +102,8 @@ export interface AssetStore {
readAsset(params: {
userId: string;
assetId: string;
+ start?: number;
+ end?: number;
}): Promise<{ asset: Buffer; metadata: AssetMetadata }>;
createAssetReadStream(params: {
@@ -203,11 +205,47 @@ class LocalFileSystemAssetStore implements AssetStore {
await fs.promises.rm(assetPath);
}
- async readAsset({ userId, assetId }: { userId: string; assetId: string }) {
+ async readAsset({
+ userId,
+ assetId,
+ start,
+ end,
+ }: {
+ userId: string;
+ assetId: string;
+ start?: number;
+ end?: number;
+ }) {
const assetDir = this.getAssetDir(userId, assetId);
+ const readAssetFile =
+ start !== undefined || end !== undefined
+ ? (async () => {
+ const fd = await fs.promises.open(
+ path.join(assetDir, "asset.bin"),
+ "r",
+ );
+ try {
+ const stat = await fd.stat();
+ const offset = start ?? 0;
+ const effectiveEnd = Math.min(
+ end !== undefined ? end + 1 : stat.size,
+ stat.size,
+ );
+ const length = effectiveEnd - offset;
+ const buffer = Buffer.alloc(length);
+ const { bytesRead } = await fd.read(buffer, 0, length, offset);
+ return bytesRead < length
+ ? buffer.subarray(0, bytesRead)
+ : buffer;
+ } finally {
+ await fd.close();
+ }
+ })()
+ : fs.promises.readFile(path.join(assetDir, "asset.bin"));
+
const [asset, metadataStr] = await Promise.all([
- fs.promises.readFile(path.join(assetDir, "asset.bin")),
+ readAssetFile,
fs.promises.readFile(path.join(assetDir, "metadata.json"), {
encoding: "utf8",
}),
@@ -396,11 +434,27 @@ class S3AssetStore implements AssetStore {
await fs.promises.rm(assetPath);
}
- async readAsset({ userId, assetId }: { userId: string; assetId: string }) {
+ async readAsset({
+ userId,
+ assetId,
+ start,
+ end,
+ }: {
+ userId: string;
+ assetId: string;
+ start?: number;
+ end?: number;
+ }) {
+ const range =
+ start !== undefined || end !== undefined
+ ? `bytes=${start ?? 0}-${end ?? ""}`
+ : undefined;
+
const response = await this.s3Client.send(
new GetObjectCommand({
Bucket: this.bucketName,
Key: this.getAssetKey(userId, assetId),
+ Range: range,
}),
);
@@ -426,8 +480,8 @@ class S3AssetStore implements AssetStore {
end?: number;
}) {
const range =
- start !== undefined && end !== undefined
- ? `bytes=${start}-${end}`
+ start !== undefined || end !== undefined
+ ? `bytes=${start ?? 0}-${end ?? ""}`
: undefined;
const command = new GetObjectCommand({
@@ -682,11 +736,15 @@ export async function saveAssetFromFile({
export async function readAsset({
userId,
assetId,
+ start,
+ end,
}: {
userId: string;
assetId: string;
+ start?: number;
+ end?: number;
}) {
- return defaultAssetStore.readAsset({ userId, assetId });
+ return defaultAssetStore.readAsset({ userId, assetId, start, end });
}
export async function createAssetReadStream({