Skip to content

Commit

Permalink
update config and driver system
Browse files Browse the repository at this point in the history
  • Loading branch information
taisan11 committed Oct 19, 2024
1 parent 7557af0 commit c15c952
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: create data folder
run: "mkdir ./data"
- name: create config
run: 'echo -e "import deno_kv_adapder from \"../src/module/storage/deno_kv_adapder.ts\";\nimport { Config } from \"../src/module/config\";\n\nconst config:Config = {\n caps:{\n admin:{\n name:\"Admin\",\n pw:\"Admin\",\n fullname:\"Administrator\",\n description:\"Administrator\",\n }\n },\n preference:{\n site:{\n name:\"Och\",\n use:\"bun\",\n websocket:true,\n API:true,\n driver:\"unstorage\",\n UnstorageOptions:deno_kv_adapder({}),\n dbOptin:\"sqlite\",\n },\n }\n}\n\nexport default config" > ./data/system.config.ts'
run: 'echo -e "import deno_kv_adapder from \"../src/module/storage/deno_kv_adapder.ts\";\nimport { unstorage_driver } from \"../src/module/storage/unstrage-base\";\nimport { Config } from \"../src/module/config\";\n\nconst config:Config = {\n caps:{\n admin:{\n name:\"Admin\",\n pw:\"Admin\",\n fullname:\"Administrator\",\n description:\"Administrator\",\n }\n },\n preference:{\n site:{\n name:\"Och\",\n use:\"bun\",\n websocket:true,\n API:true,\n driver:\"unstorage_driver(deno_kv_adapder())\",\n },\n }\n}\n\nexport default config" > ./data/system.config.ts'

- name: Build step
run: "npx jiti ./script/build.deno.ts"
Expand Down
6 changes: 2 additions & 4 deletions src/module/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ export type Config = {
use?: RuntimeName|"other",
websocket: boolean;
API:boolean;
driver:"unstorage"|"db"
UnstorageOptions?:Driver
drizzle:ReturnType<typeof drizzle>;
driver:driver
plugins?: Plugin[];
};
limit?: {
Expand Down Expand Up @@ -72,8 +70,8 @@ export type Config = {

import configa from "../../data/system.config";
import { RuntimeName, runtimeInfo } from "std-env";
import { Driver } from "unstorage";
import { Plugin } from "./plugin";
import { driver } from "./storage";
export function config():Config{
configa.preference.site.use = runtimeInfo?.name ||"other"|| configa.preference.site.use;
if (configa.preference.limit) {
Expand Down
74 changes: 19 additions & 55 deletions src/module/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { config } from "./config";
import { DeleteOldSubject_db, getdat_db, getSubject_db, getSubjecttxt_db, getThread_db, NewThread_db, postThread_db } from "./storage/drizzle";
import { addSubject_file, DeleteOldSubject_file, getSubject_file, NewThread_file, postThread_file, getThread_file,getSubjecttxt_file,getdat_file, init_file } from "./storage/unstrage-base";

export type Subject = {
date: Date;
Expand Down Expand Up @@ -42,6 +40,17 @@ export type postReturn = {
sc:boolean,
redirect:string
}
export type driver = {
addSubject: (BBSKEY:string,postnum: number, title: string,id: string) => Promise<void>;
DeleteOldSubject: (BBSKEY: string) => Promise<void>;
getSubjecttxt: (BBSKEY: string) => Promise<string>;
getSubject: (BBSKEY: string) => Promise<getSubjectReturn>;
NewThread: (BBSKEY: string, { name, mail, message, date, title, id }: NewThreadParams) => Promise<postReturn>;
postThread: (BBSKEY: string, { name, mail, message, date, id }: PostThreadParams) => Promise<postReturn>;
getThread: (BBSKEY: string, id: string) => Promise<getThreadReturn>;
getdat: (BBSKEY: string, idextension: string) => Promise<string>;
init: () => Promise<string>;
}
const driver = config().preference.site.driver;
// export async function addSubject(BBSKEY:string,date: string, title: string,id: string): Promise<void>{
// if (driver === "unstorage") {
Expand All @@ -50,76 +59,31 @@ const driver = config().preference.site.driver;
// }

export async function DeleteOldSubject(BBSKEY:string,): Promise<void>{
if (driver === "unstorage") {
return await DeleteOldSubject_file(BBSKEY)
}
if (driver === "db") {
return await DeleteOldSubject_db(BBSKEY)
}
return
return driver.DeleteOldSubject(BBSKEY)
}
export async function getSubjecttxt(BBSKEY:string,): Promise<string>{
if (driver === "unstorage") {
return await getSubjecttxt_file(BBSKEY)
}
if (driver === "db") {
return await getSubjecttxt_db(BBSKEY)
}
return ""
return driver.getSubjecttxt(BBSKEY)
}
export async function getSubject(BBSKEY:string,): Promise<getSubjectReturn>{
if (driver === "unstorage") {
return await getSubject_file(BBSKEY)
}
if (driver === "db") {
return await getSubject_db(BBSKEY)
}
return {has:false,data:{}}
return driver.getSubject(BBSKEY)
}
export async function NewThread(BBSKEY:string,{ name, mail, message, date, title, id }: NewThreadParams):Promise<postReturn> {
if (driver === "unstorage") {
return await NewThread_file(BBSKEY,{ name, mail, message, date, title, id })
}
if (driver === "db") {
return await NewThread_db(BBSKEY,{ name, mail, message, date, title, id })
}
return {sc:false,redirect:"/test"}
return driver.NewThread(BBSKEY,{ name, mail, message, date, title, id })
}
export async function postThread(BBSKEY:string,{ name, mail, message, date, id }: PostThreadParams):Promise<postReturn> {
if (driver === "unstorage") {
return await postThread_file(BBSKEY,{ name, mail, message, date, id })
}
if (driver === "db") {
return await postThread_db(BBSKEY,{ name, mail, message, date, id })
}
return {sc:false,redirect:"/test"}
return driver.postThread(BBSKEY,{ name, mail, message, date, id })
}

export async function getThread(BBSKEY:string,id: string):Promise<getThreadReturn> {
if (driver === "unstorage") {
return await getThread_file(BBSKEY,id)
}
if (driver === "db") {
return await getThread_db(BBSKEY,id)
}
return {data:{"title":"",post:[]},has:false}
return driver.getThread(BBSKEY,id)
}
export async function getdat(BBSKEY:string,idextension: string):Promise<string> {
if (driver === "unstorage") {
return await getdat_file(BBSKEY,idextension)
}
if (driver === "db") {
return await getdat_db(BBSKEY,idextension)
}
return ""
return driver.getdat(BBSKEY,idextension)
}

/**
* @description 仮実装 init admin
*/
export async function init():Promise<string> {
if (driver === "unstorage") {
return await init_file()
}
return ""
return driver.init()
}
182 changes: 98 additions & 84 deletions src/module/storage/unstrage-base.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,106 @@
import { createStorage } from "unstorage";
import { config } from "../config";
import { subjectpaser,datpaser } from "../pase";
import { NewThreadParams,PostThreadParams, getSubjectReturn, getThreadReturn, postReturn } from "../storage";
import { NewThreadParams,PostThreadParams, driver, getSubjectReturn, getThreadReturn, postReturn } from "../storage";
import { Driver } from "unstorage";

const drives = {driver:config().preference.site.UnstorageOptions}

export async function addSubject_file(BBSKEY:string,postnum: number, title: string,id: string):Promise<void> {
const storage = createStorage(drives);
const SUBJECT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
await storage.setItem(`${BBSKEY}/SUBJECT.TXT`, `${id}.dat<>${title} (${postnum})\n${SUBJECT}`);
}
export async function postNumEdit(BBSKEY:string,id: string):Promise<void> {
const storage = createStorage(drives);
const SUBJECT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
const lines = String(SUBJECT).split('\n');
const newLines = lines.map((line) => {
if (line.startsWith(`${id}.dat`)) {
const [id, title] = line.split('<>');
const [postnum] = title.match(/\((\d+)\)/) || ['0'];
//(1) -> 1
const newNum = postnum.replace(/\((\d+)\)/, '$1');
//title (1) -> title (2)
const newTitle = title.replace(/\((\d+)\)/, `(${Number(newNum) + 1})`);
return `${id}<>${newTitle}`;
export function unstorage_driver(UnstorageOptions:Driver):driver{
const drives = {"driver":UnstorageOptions}
async function addSubject_file(BBSKEY:string,postnum: number, title: string,id: string):Promise<void> {
const storage = createStorage(drives);
const SUBJECT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
await storage.setItem(`${BBSKEY}/SUBJECT.TXT`, `${id}.dat<>${title} (${postnum})\n${SUBJECT}`);
}
async function postNumEdit(BBSKEY:string,id: string):Promise<void> {
const storage = createStorage(drives);
const SUBJECT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
const lines = String(SUBJECT).split('\n');
const newLines = lines.map((line) => {
if (line.startsWith(`${id}.dat`)) {
const [id, title] = line.split('<>');
const [postnum] = title.match(/\((\d+)\)/) || ['0'];
//(1) -> 1
const newNum = postnum.replace(/\((\d+)\)/, '$1');
//title (1) -> title (2)
const newTitle = title.replace(/\((\d+)\)/, `(${Number(newNum) + 1})`);
return `${id}<>${newTitle}`;
}
return line;
});
const newSubject = newLines.join('\n');
await storage.setItem(`${BBSKEY}/SUBJECT.TXT`, newSubject);
}
async function DeleteOldSubject_file(BBSKEY:string,):Promise<void> {
const storage = createStorage(drives);
const SUBJECT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
const lines = String(SUBJECT).split('\n');
const newLines = lines.slice(0, config()!.preference!.limit!.MaxSubject); // Keep the first 10 lines
const newSubject = newLines.join('\n');
await storage.setItem(`${BBSKEY}/SUBJECT.TXT`, newSubject);
}
async function getSubjecttxt_file(BBSKEY:string,):Promise<string> {
const storage = createStorage(drives);
const SUBTXT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
return String(SUBTXT);
}
async function getSubject_file(BBSKEY:string,):Promise<getSubjectReturn> {
const storage = createStorage(drives);
const HASSUB = await storage.hasItem(`${BBSKEY}/SUBJECT.TXT`);
if (!HASSUB) {
return {'data':{'a':["a","a"]},'has':HASSUB};
}
return line;
});
const newSubject = newLines.join('\n');
await storage.setItem(`${BBSKEY}/SUBJECT.TXT`, newSubject);
}
export async function DeleteOldSubject_file(BBSKEY:string,):Promise<void> {
const storage = createStorage(drives);
const SUBJECT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
const lines = String(SUBJECT).split('\n');
const newLines = lines.slice(0, config()!.preference!.limit!.MaxSubject); // Keep the first 10 lines
const newSubject = newLines.join('\n');
await storage.setItem(`${BBSKEY}/SUBJECT.TXT`, newSubject);
}
export async function getSubjecttxt_file(BBSKEY:string,):Promise<string> {
const storage = createStorage(drives);
const SUBTXT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
return String(SUBTXT);
}
export async function getSubject_file(BBSKEY:string,):Promise<getSubjectReturn> {
const storage = createStorage(drives);
const HASSUB = await storage.hasItem(`${BBSKEY}/SUBJECT.TXT`);
if (!HASSUB) {
return {'data':{'a':["a","a"]},'has':HASSUB};
const SUBTXT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
return {'data':subjectpaser(String(SUBTXT)),'has':HASSUB};
}
async function NewThread_file(BBSKEY:string,{ name, mail, message, date, title, id }: NewThreadParams):Promise<postReturn> {
const storage = createStorage(drives);
await storage.setItem(`${BBSKEY}/dat/${id}.dat`, `${name}<>${mail}<>${date}<>${message}<>${title}`);
await addSubject_file(BBSKEY,1, title,id);
return { 'sc': true, 'redirect': `${BBSKEY}/${id}` };
}

async function postThread_file(BBSKEY:string,{ name, mail, message, date, id }: PostThreadParams):Promise<postReturn> {
const storage = createStorage(drives);
const THDATTXT = await storage.getItem(`${BBSKEY}/dat/${id}.dat`);
await storage.setItem(`${BBSKEY}/dat/${id}.dat`, `${THDATTXT}\n${name}<>${mail}<>${date}<>${message}`);
await postNumEdit(BBSKEY,id);
return {'sc':true,'redirect':`${BBSKEY}/${id}`};
}

async function getThread_file(BBSKEY:string,id: string):Promise<getThreadReturn> {
const storage = createStorage(drives);
const dat = await storage.getItem(`${BBSKEY}/dat/${id}.dat`);
const hasdat= await storage.hasItem(`${BBSKEY}/dat/${id}.dat`);
return {'data':datpaser(String(dat)),has:hasdat};
}
async function getdat_file(BBSKEY:string,idextension: string):Promise<string> {
const storage = createStorage(drives);
const dat = await storage.getItem(`${BBSKEY}/dat/${idextension}.dat`);
return String(dat)
}

/**
* @description 仮実装 init admin
*/
async function init_file():Promise<string> {
const storage = createStorage(drives);
await storage.clear();
await storage.setItem("/test/SUBJECT.TXT", '1.dat<>初期スレ (1)');
console.log('TEST(subjectFile):',await storage.hasItem("/test/SUBJECT.TXT"))
await storage.setItem("/test/dat/1.dat", 'カワイイ名無しさん<><>2022/09/08(木) 17:40:07.67 ID:000000000<>こんにちは!!<>初期スレ');
console.log('TEST(datFile):',await storage.hasItem("/test/dat/1.dat"))
return String('init!!')
}
const SUBTXT = await storage.getItem(`${BBSKEY}/SUBJECT.TXT`);
return {'data':subjectpaser(String(SUBTXT)),'has':HASSUB};
}
export async function NewThread_file(BBSKEY:string,{ name, mail, message, date, title, id }: NewThreadParams):Promise<postReturn> {
const storage = createStorage(drives);
await storage.setItem(`${BBSKEY}/dat/${id}.dat`, `${name}<>${mail}<>${date}<>${message}<>${title}`);
await addSubject_file(BBSKEY,1, title,id);
return { 'sc': true, 'redirect': `${BBSKEY}/${id}` };
}

export async function postThread_file(BBSKEY:string,{ name, mail, message, date, id }: PostThreadParams):Promise<postReturn> {
const storage = createStorage(drives);
const THDATTXT = await storage.getItem(`${BBSKEY}/dat/${id}.dat`);
await storage.setItem(`${BBSKEY}/dat/${id}.dat`, `${THDATTXT}\n${name}<>${mail}<>${date}<>${message}`);
await postNumEdit(BBSKEY,id);
return {'sc':true,'redirect':`${BBSKEY}/${id}`};
}

export async function getThread_file(BBSKEY:string,id: string):Promise<getThreadReturn> {
const storage = createStorage(drives);
const dat = await storage.getItem(`${BBSKEY}/dat/${id}.dat`);
const hasdat= await storage.hasItem(`${BBSKEY}/dat/${id}.dat`);
return {'data':datpaser(String(dat)),has:hasdat};
}
export async function getdat_file(BBSKEY:string,idextension: string):Promise<string> {
const storage = createStorage(drives);
const dat = await storage.getItem(`${BBSKEY}/dat/${idextension}.dat`);
return String(dat)
}

/**
* @description 仮実装 init admin
*/
export async function init_file():Promise<string> {
const storage = createStorage(drives);
await storage.clear();
await storage.setItem("/test/SUBJECT.TXT", '1.dat<>初期スレ (1)');
console.log('TEST(subjectFile):',await storage.hasItem("/test/SUBJECT.TXT"))
await storage.setItem("/test/dat/1.dat", 'カワイイ名無しさん<><>2022/09/08(木) 17:40:07.67 ID:000000000<>こんにちは!!<>初期スレ');
console.log('TEST(datFile):',await storage.hasItem("/test/dat/1.dat"))
return String('init!!')
return {
addSubject: (BBSKEY:string,postnum: number, title: string,id: string) => addSubject_file(BBSKEY,postnum, title,id),
DeleteOldSubject: (BBSKEY: string) => DeleteOldSubject_file(BBSKEY),
getSubjecttxt: (BBSKEY: string) => getSubjecttxt_file(BBSKEY),
getSubject: (BBSKEY: string) => getSubject_file(BBSKEY),
NewThread: (BBSKEY: string, { name, mail, message, date, title, id }: NewThreadParams) => NewThread_file(BBSKEY,{ name, mail, message, date, title, id }),
postThread: (BBSKEY: string, { name, mail, message, date, id }: PostThreadParams) => postThread_file(BBSKEY,{ name, mail, message, date, id }),
getThread: (BBSKEY: string, id: string) => getThread_file(BBSKEY,id),
getdat: (BBSKEY: string, idextension: string) => getdat_file(BBSKEY,idextension),
init: () => init_file(),
}
}

0 comments on commit c15c952

Please sign in to comment.