This commit is contained in:
Cogent Apps
2023-03-14 11:00:40 +00:00
parent 4a5e8c9e16
commit 645b66b988
104 changed files with 11064 additions and 1565 deletions

View 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>;
}

View 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');
}

View 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();
}
});
});
}
}