Skip to content

Commit

Permalink
fix fsAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianMairinger committed Jan 28, 2024
1 parent e87a56b commit 1e0ed27
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 70 deletions.
32 changes: 19 additions & 13 deletions app/src/fsToSimpleUniDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function fsToSimpleUniDB(filePath: FilePath): Promise<SimpleUniDB>
}
else {
await fs.mkdir(path.dirname(filePath), { recursive: true })
await fs.writeFile(filePath, encode(undefined))
}


Expand All @@ -35,7 +36,10 @@ export async function fsToSimpleUniDB(filePath: FilePath): Promise<SimpleUniDB>
}


let writeHappened = false
// todo this should return cancelAblePromise, so that transaction can be canceled
async function updateOne(diff: { [key: string]: undefined | unknown }) {
writeHappened = true
if (!currentlyInTransaction) throw new Error("not in transaction")
tmpData = parseDataDiff(await findOne(), diff)
}
Expand Down Expand Up @@ -65,22 +69,24 @@ export async function fsToSimpleUniDB(filePath: FilePath): Promise<SimpleUniDB>
tmpData = initTmpData
currentlyInTransaction = false
})
const retProm = (fRes as CancelAblePromise).then(async () => {
try {
await fs.writeFile(filePath, encode(tmpData), { signal: abortController.signal })
initTmpData = tmpData = undefined
currentlyInTransaction = tmpDataSet = false
}
catch(e) {
if (e.name !== "AbortError") {
abort()
throw e
const retProm = (fRes as CancelAblePromise).then(async (res) => {
if (writeHappened) {
try {
await fs.writeFile(filePath, encode(await tmpData), { signal: abortController.signal })
initTmpData = tmpData = undefined
currentlyInTransaction = tmpDataSet = writeHappened = false
}
catch(e) {
if (e.name !== "AbortError") {
abort()
throw e
}
}
}


}, () => {
return res
}, (reason) => {
abort()
throw reason
}, "cancel" in fRes ? async (reason) => {
abortController.abort(reason)
abort()
Expand Down
3 changes: 2 additions & 1 deletion app/src/indexedDBToUniDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ export function indexedDBToUniDB({db, collectionName}: IDBPCollection): UniDB<ID
currentlyInTransaction = false
collectionInTransaction.transaction.abort()
})
return (fRes as CancelAblePromise).then(async () => {
return (fRes as CancelAblePromise).then(async (res) => {
await transaction.done
currentlyInTransaction = false
return res
}, (e) => {
abortTransaction()
}, "cancel" in fRes ? async (reason) => {
Expand Down
4 changes: 2 additions & 2 deletions app/src/josmReflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export function josmReflection(reflectionAdapter: PrimaryTransmissionAdapter | S
adapter.send(storedData)
}

if (db[instanceTypeSym] === "Data" && (typeof storedData === "object" || storedData !== null)) new Error("Reflection is object be should be primitive based on default.")
if (db[instanceTypeSym] === "DataBase" && !(typeof storedData === "object" || storedData !== null)) new Error("Reflection is primitive be should be object based on default.")
if (db[instanceTypeSym] === "Data" && (typeof storedData === "object" || storedData !== null)) throw new Error("Reflection is object be should be primitive based on default.")
if (db[instanceTypeSym] === "DataBase" && !(typeof storedData === "object" || storedData !== null)) throw new Error("Reflection is primitive be should be object based on default.")


p.res(pVal = {db, adapter})
Expand Down
12 changes: 6 additions & 6 deletions app/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ export const defaultTransactionOptions = {access: "readonly", skipAble: false, s


export type UniDB<ID = unknown> = {
findOne(id?: ID): Promise<unknown>
insertOne(doc: object): Promise<ID>
updateOne(diff: { [key: string]: undefined | unknown }, id?: ID): Promise<void>
findOne(id?: ID): Promise<unknown> | CancelAblePromise
insertOne(doc: object): Promise<ID> | CancelAblePromise
updateOne(diff: { [key: string]: undefined | unknown }, id?: ID): Promise<void> | CancelAblePromise
transaction<T, R extends Promise<T> | CancelAblePromise<T, string, Promise<void> | undefined>>(f: () => R, options?: typeof defaultTransactionOptions): R
rootId: ID
}

export type SimpleUniDB = {
findOne(id?: never): Promise<unknown>
// insertOne(doc: object): Promise<never>
updateOne(diff: { [key: string]: undefined | unknown }, id?: never): Promise<void>
findOne(id?: never): Promise<unknown> | CancelAblePromise
// insertOne(doc: object): Promise<never> | CancelAblePromise
updateOne(diff: { [key: string]: undefined | unknown }, id?: never): Promise<void> | CancelAblePromise
transaction<T, R extends Promise<T> | CancelAblePromise<T, string, Promise<void> | undefined>>(f: () => R, options?: typeof defaultTransactionOptions): R
}
3 changes: 2 additions & 1 deletion app/src/mongoToUniDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ export async function mongoDBToUniDB({ client, collection }: {client: MongoClien
currentlyInTransaction = false
await session.abortTransaction()
})
return (fRes as CancelAblePromise).then(async () => {
return (fRes as CancelAblePromise).then(async (res) => {
await commitWithRetry(session)
currentlyInTransaction = false
return res
}, async () => {
await abortTransaction()
}, "cancel" in fRes ? async (reason) => {
Expand Down
29 changes: 22 additions & 7 deletions app/src/staticUniDBReflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,45 @@ import { SimpleUniDB, parseDataDiff } from "./lib";
import { isAdapterSym } from "./fullyConnectedAdapter"
import clone, { mergeKeysDeep } from "circ-clone"
import { makeJosmReflection } from "./josmReflection";
import { CancelAblePromise } from "more-proms";



export async function simpleUniDBToAdapter(db: SimpleUniDB) {

let tempDiffStorage: object | undefined
let isTempDiffStorageSet: boolean = false

return {
msg() {
msg() {
return db.transaction(async () => {
// In case this returns a cancelAble Promise, we wrap it here, so that it cannot be canceled
return await db.findOne()
}).then((storedData) => parseDataDiff(storedData, tempDiffStorage))
}).then((storedData) =>
isTempDiffStorageSet ? parseDataDiff(storedData, tempDiffStorage) : storedData
)
},
async send(_dataDiff: any) {
const dataDiff = clone(_dataDiff)
db.transaction(async () => {

function onCancel() {
isTempDiffStorageSet = true
tempDiffStorage = mergeKeysDeep(tempDiffStorage, dataDiff)
}

db.transaction(() => {
const diff = mergeKeysDeep(tempDiffStorage, dataDiff)
isTempDiffStorageSet = false
tempDiffStorage = undefined
await db.updateOne(diff as any)
// This may return a cancelAble Promise.
const prom = db.updateOne(diff as any)
return !("cancel" in (prom as CancelAblePromise)) ? prom : (prom as CancelAblePromise).then(undefined, undefined, async (reason) => {
const res = await (prom as CancelAblePromise<any, any, any>).cancel(reason)
onCancel()
return res
})
}, {
skipAble: () => {
tempDiffStorage = mergeKeysDeep(tempDiffStorage, dataDiff)
},
skipAble: onCancel,
access: "readwrite",
skipPrevIfPossible: true
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"ws": "^8.15.1"
},
"dependencies": {
"circ-clone": "^2.7.10",
"circ-clone": "^2.7.11",
"circ-json": "^1.0.4",
"circ-msgpack": "^1.0.0",
"colorful-cli-logger": "^1.0.2",
Expand Down
9 changes: 7 additions & 2 deletions repl/src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import wsUrlify from "normalize-ws-url-protocol"
import clone from "circ-clone"
import { decode, encode } from "circ-msgpack"
import fs from "fs"
import delay from "tiny-delay"

declare const window: any

Expand All @@ -18,12 +19,16 @@ declare const window: any

const p = new Promise<void>((res, rej) => {
setTimeout(() => {
res()
console.log("1")
res(delay(1000).then(() => {
console.log("2")

}))
}, 1000)
})

p.then(() => {
return Promise.reject()
// return Promise.reject()
console.log("then")
}, () => {
console.log("catch")
Expand Down
109 changes: 72 additions & 37 deletions repl/src/replServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,105 @@ import {
websocketJosmAdapterClient,
restrictAdapter,
reflectionToSaniTemplate,
dataBaseToAdapter
dataBaseToAdapter,
josmStaticFsReflection
} from "./../../app/src/josmAdapter"
import { DataBase } from "josm"
import delay from "tiny-delay"
// import { josmFsReflection } from "../../app/src/fsReflection"
import { josmMongoReflection } from "../../app/src/mongoReflection"
import sani, { AND } from "sanitize-against"
import cloneKeys from "circ-clone"




setupServer("test").then(async ({app, db: mongo}) => {

const db = new DataBase({
whooop: 1,
secs: 1,
pw: "PRIVATE"

console.log("start")

const db = await josmStaticFsReflection("test123", {
lel: 2,
lol: "lol"
})

// josmFsReflection("lelol", db)
josmMongoReflection(mongo.collection("testDB"), db)
console.log(cloneKeys(db()))

// db({lol: "WHOO2"})






console.log(cloneKeys(db()))

console.log("end")




db((full, diff) => {
console.log("diff", diff)
})

// setInterval(() => {
// db.secs.set(db.secs.get() + 1)
// }, 1000)

app.ws("/con1", async (ws) => {
const userRights = {
read: {
secs: true,
whooop: true
},
write: {
secs: true
}
}

const userReadRequest = {
secs: true,
whooop: true
}

const readRestrictionsAsReflection = [userRights.read, userReadRequest]

const readRestrictionsAsSaniTemplate = readRestrictionsAsReflection.map(reflectionToSaniTemplate) as any[]
// @ts-ignore
const saniF = sani(new AND(...readRestrictionsAsSaniTemplate))
const restrictedDBAdapter = restrictAdapter(dataBaseToAdapter(db), {
read: saniF,
write: sani(reflectionToSaniTemplate(userRights.write)) as any
})



websocketJosmAdapterServer(ws, restrictedDBAdapter, true, false)

// const db = new DataBase({
// whooop: 1,
// secs: 1,
// pw: "PRIVATE"
// })

// // josmFsReflection("lelol", db)
// josmMongoReflection(mongo.collection("testDB"), db)


// db((full, diff) => {
// console.log("diff", diff)
// })

// // setInterval(() => {
// // db.secs.set(db.secs.get() + 1)
// // }, 1000)

// app.ws("/con1", async (ws) => {
// const userRights = {
// read: {
// secs: true,
// whooop: true
// },
// write: {
// secs: true
// }
// }

// const userReadRequest = {
// secs: true,
// whooop: true
// }

// const readRestrictionsAsReflection = [userRights.read, userReadRequest]

// const readRestrictionsAsSaniTemplate = readRestrictionsAsReflection.map(reflectionToSaniTemplate) as any[]
// // @ts-ignore
// const saniF = sani(new AND(...readRestrictionsAsSaniTemplate))
// const restrictedDBAdapter = restrictAdapter(dataBaseToAdapter(db), {
// read: saniF,
// write: sani(reflectionToSaniTemplate(userRights.write)) as any
// })



// websocketJosmAdapterServer(ws, restrictedDBAdapter, true, false)



})
// })


})
1 change: 1 addition & 0 deletions test123
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��lel�lol�WHOO2

0 comments on commit 1e0ed27

Please sign in to comment.