v0.2.0
This commit is contained in:
5
server/src/object-store/index.ts
Normal file
5
server/src/object-store/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export default abstract class ObjectStore {
|
||||
public async initialize() {}
|
||||
public abstract get(key: string): Promise<string | null>;
|
||||
public abstract put(key: string, value: string, contentType: string): Promise<void>;
|
||||
}
|
||||
43
server/src/object-store/s3.ts
Normal file
43
server/src/object-store/s3.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
S3,
|
||||
PutObjectCommand,
|
||||
GetObjectCommand,
|
||||
} from "@aws-sdk/client-s3";
|
||||
import type {Readable} from 'stream';
|
||||
import ObjectStore from "./index";
|
||||
|
||||
const bucket = process.env.S3_BUCKET;
|
||||
|
||||
const s3 = new S3({
|
||||
region: process.env.DEFAULT_S3_REGION,
|
||||
});
|
||||
|
||||
export default class S3ObjectStore extends ObjectStore {
|
||||
public async get(key: string) {
|
||||
const params = {
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
};
|
||||
const data = await s3.send(new GetObjectCommand(params));
|
||||
return await readStream(data.Body as Readable);
|
||||
}
|
||||
|
||||
public async put(key: string, value: string, contentType: string) {
|
||||
const params = {
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
Body: value,
|
||||
ContentType: contentType,
|
||||
StorageClass: "INTELLIGENT_TIERING",
|
||||
};
|
||||
await s3.send(new PutObjectCommand(params));
|
||||
}
|
||||
}
|
||||
|
||||
async function readStream(stream: Readable) {
|
||||
const chunks: any[] = [];
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return Buffer.concat(chunks).toString('utf8');
|
||||
}
|
||||
48
server/src/object-store/sqlite.ts
Normal file
48
server/src/object-store/sqlite.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { verbose } from "sqlite3";
|
||||
import ObjectStore from "./index";
|
||||
|
||||
const sqlite3 = verbose();
|
||||
|
||||
const db = new sqlite3.Database('./data/object-store.sqlite');
|
||||
|
||||
export interface StoredObject {
|
||||
key: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export default class SQLiteObjectStore extends ObjectStore {
|
||||
public async initialize() {
|
||||
db.serialize(() => {
|
||||
db.run(`CREATE TABLE IF NOT EXISTS objects (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT
|
||||
)`);
|
||||
});
|
||||
}
|
||||
|
||||
public async get(key: string): Promise<string | null> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.get(`SELECT * FROM objects WHERE key = ?`, [key], (err, row) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(row?.value ?? null);
|
||||
console.log(`[object-store:sqlite] retrieved object ${key}`)
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async put(key: string, value: string, contentType: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.run(`INSERT OR REPLACE INTO objects (key, value) VALUES (?, ?)`, [key, value], (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
console.log(`[object-store:sqlite] stored object ${key}`)
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user