Skip to content

Commit 16328f0

Browse files
committed
chore: now the sync-log store show be available everywhere in fireproof
1 parent 654493f commit 16328f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1481
-992
lines changed

cloud/backend/base/ws-sockets.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sleep } from "@fireproof/core-runtime";
1+
import { consumeStream, sleep } from "@fireproof/core-runtime";
22
import * as ps from "@fireproof/core-types-protocols-cloud";
33
import { Msger } from "@fireproof/core-protocols-cloud";
44
import { testSuperThis } from "@fireproof/cloud-base";
@@ -38,17 +38,6 @@ describe("test multiple connections", () => {
3838
// await hserv.close();
3939
});
4040

41-
function consumeStream(stream: ReadableStream<ps.MsgWithError<ps.MsgWithConn>>, cb: (msg: ps.MsgBase) => void): void {
42-
const reader = stream.getReader();
43-
async function readNext() {
44-
const { done, value } = await reader.read();
45-
if (done) return;
46-
cb(value);
47-
readNext();
48-
}
49-
readNext();
50-
}
51-
5241
it("could open multiple connections", async () => {
5342
const id = sthis.nextId(4).str;
5443
const conns = await Promise.all(

core/blockstore/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@ipld/dag-cbor": "^9.2.5",
5353
"@ipld/dag-json": "^10.2.5",
5454
"@web3-storage/pail": "^0.6.2",
55+
"dexie": "^4.2.0",
5556
"multiformats": "^13.4.1",
5657
"p-map": "^7.0.3",
5758
"p-retry": "^7.0.0"

core/blockstore/register-store-protocol.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { BuildURI, Lazy, ResolveOnce, runtimeFn, URI } from "@adviser/cement";
1+
import { BuildURI, ResolveOnce, runtimeFn, URI } from "@adviser/cement";
22
import { SuperThis, PARAM } from "@fireproof/core-types-base";
3-
import { SerdeGateway, Gateway } from "@fireproof/core-types-blockstore";
4-
import { MemoryGateway } from "@fireproof/core-gateways-memory";
5-
import { FileGateway, FILESTORE_VERSION, sysFileSystemFactory } from "@fireproof/core-gateways-file";
3+
import { SerdeGateway, Gateway, FPIndexedDB } from "@fireproof/core-types-blockstore";
4+
import { memFPIndexedDB, MemoryGateway } from "@fireproof/core-gateways-memory";
5+
import { FileGateway, FILESTORE_VERSION, sysFileFPIndexedDB, sysFileSystemFactory } from "@fireproof/core-gateways-file";
66
import { DefSerdeGateway, INDEXEDDB_VERSION } from "@fireproof/core-gateways-base";
77
import { CloudGateway } from "@fireproof/core-gateways-cloud";
8-
import { FPSyncProtocol } from "../types/protocols/sync/index.js";
98

109
export interface SerdeGatewayFactoryItem {
1110
readonly protocol: string;
1211
readonly isDefault?: boolean;
1312
defaultURI(sthis: SuperThis): URI;
1413
serdegateway(sthis: SuperThis): Promise<SerdeGateway>;
15-
fpsync(sthis: SuperThis, uri: URI): Promise<FPSyncProtocol<unknown>>;
14+
fpIndexedDB(sthis: SuperThis, uri: URI): Promise<FPIndexedDB>;
1615
}
1716

1817
class OneSerdeGatewayFactoryItem implements SerdeGatewayFactoryItem {
@@ -45,7 +44,9 @@ class OneSerdeGatewayFactoryItem implements SerdeGatewayFactoryItem {
4544
return this.once.once(() => this.item.serdegateway(sthis));
4645
}
4746

48-
fpsync = Lazy((sthis: SuperThis, uri: URI) => this.item.fpsync(sthis, uri));
47+
async fpIndexedDB(sthis: SuperThis, uri: URI): Promise<FPIndexedDB> {
48+
return this.item.fpIndexedDB(sthis, uri);
49+
}
4950
}
5051

5152
const storeFactory = new Map<string, OneSerdeGatewayFactoryItem>();
@@ -76,11 +77,10 @@ export interface SerdeOrGatewayFactoryItem {
7677
readonly serdegateway?: (sthis: SuperThis) => Promise<SerdeGateway>;
7778
readonly gateway?: (sthis: SuperThis) => Promise<Gateway>;
7879

79-
readonly fpsync: (sthis: SuperThis, uri: URI) => Promise<FPSyncProtocol<unknown>>;
80+
readonly fpIndexedDB: (sthis: SuperThis, uri: URI) => Promise<FPIndexedDB>;
8081
}
8182

8283
export function registerStoreProtocol(item: SerdeOrGatewayFactoryItem): () => void {
83-
console.log("registerStoreProtocol", item.protocol);
8484
let protocol = item.protocol;
8585
if (!protocol.endsWith(":")) {
8686
protocol += ":";
@@ -151,10 +151,8 @@ if (runtimeFn().isNodeIsh || runtimeFn().isDeno) {
151151
gateway: async (sthis) => {
152152
return new FileGateway(sthis, await sysFileSystemFactory(defaultURI(sthis)));
153153
},
154-
fpsync: async (_sthis, _uri) => {
155-
throw new Error("fpsync for file: Not implemented");
156-
// const { fileFPSync } = await import("@fireproof/core-gateways-file");
157-
// return fileFPSync(sthis, uri) as Promise<FPSyncProtocol<unknown>>;
154+
fpIndexedDB: async (sthis, uri) => {
155+
return sysFileFPIndexedDB(sthis, uri);
158156
},
159157
});
160158
}
@@ -174,9 +172,9 @@ if (runtimeFn().isBrowser) {
174172
const { GatewayImpl } = await import("@fireproof/core-gateways-indexeddb");
175173
return new GatewayImpl();
176174
},
177-
fpsync: async (sthis, uri) => {
178-
const { indexedDBFPSync } = await import("@fireproof/core-gateways-indexeddb");
179-
return indexedDBFPSync(sthis, uri) as Promise<FPSyncProtocol<unknown>>;
175+
fpIndexedDB: async (sthis, uri) => {
176+
const { indexeddbFPIndexedDB } = await import("@fireproof/core-gateways-indexeddb");
177+
return indexeddbFPIndexedDB(sthis, uri);
180178
},
181179
});
182180
}
@@ -191,10 +189,9 @@ registerStoreProtocol({
191189
gateway: async (sthis) => {
192190
return new MemoryGateway(sthis, memory);
193191
},
194-
fpsync: () => {
195-
throw new Error("fpsync for memory: Not implemented");
196-
// memoryFPSync as (sthis: SuperThis, uri: URI) => Promise<FPSyncProtocol<unknown>>,
197-
}
192+
fpIndexedDB: (sthis, uri) => {
193+
return memFPIndexedDB(sthis, uri, memory);
194+
},
198195
});
199196

200197
//const onceRegisterFireproofCloudStoreProtocol = new KeyedResolvOnce<() => void>();
@@ -209,7 +206,7 @@ registerStoreProtocol({
209206
serdegateway: async (sthis: SuperThis) => {
210207
return new CloudGateway(sthis);
211208
},
212-
fpsync: async () => {
209+
fpIndexedDB: async () => {
213210
throw new Error("fpsync for fpcloud: Not implemented");
214211
},
215212
});

core/gateways/file-deno/deno-filesystem.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,75 @@
11
/// <reference types="deno" />
2-
import type { FPStats, SysFileSystem } from "@fireproof/core-types-base";
3-
import { to_uint8 } from "@adviser/cement";
2+
import type { FPSql, FPSqlCmd, FPSqlConn, FPSqlResult, FPStats, SysFileSystem } from "@fireproof/core-types-base";
3+
import { exception2Result, to_uint8 } from "@adviser/cement";
4+
import type { DatabaseSync } from "node:sqlite";
5+
6+
class NodeSqlite implements FPSql {
7+
readonly path: string;
8+
readonly db: DatabaseSync;
9+
readonly fs: SysFileSystem;
10+
11+
constructor(db: DatabaseSync, fs: SysFileSystem, path: string) {
12+
this.db = db;
13+
this.path = path;
14+
this.fs = fs;
15+
}
16+
17+
async batch(sqlCmds: FPSqlCmd[]): Promise<FPSqlResult[]> {
18+
const ret: FPSqlResult[] = [];
19+
const withStmt = sqlCmds.map((cmd) => ({
20+
...cmd,
21+
argss: cmd.argss ?? [[]],
22+
stmt: this.db.prepare(cmd.sql),
23+
}));
24+
for (const cmd of withStmt) {
25+
for (const args of cmd.argss) {
26+
const r = exception2Result(() => cmd.stmt.all(...args));
27+
switch (true) {
28+
case r.isOk():
29+
ret.push({ rows: r.Ok().map((i) => Object.values(i)) });
30+
break;
31+
case r.isErr():
32+
ret.push({ error: r.Err() });
33+
break;
34+
}
35+
}
36+
}
37+
return Promise.resolve(ret);
38+
}
39+
40+
async transaction<T>(fn: (tx: FPSql) => Promise<T>): Promise<T> {
41+
return fn(this);
42+
}
43+
44+
close(): Promise<void> {
45+
this.db.close();
46+
return Promise.resolve();
47+
}
48+
destroy(): Promise<void> {
49+
return this.fs.rm(this.path);
50+
}
51+
}
52+
53+
class NodeSqliteConn implements FPSqlConn {
54+
readonly fs: SysFileSystem;
55+
readonly #dbs: (path: string) => DatabaseSync;
56+
57+
static async create(fs: SysFileSystem): Promise<NodeSqliteConn> {
58+
const rNodeSql = await exception2Result(() => import("node:sqlite"));
59+
if (rNodeSql.isErr()) {
60+
throw new Error("Need node:sqlite in node 22 and deno.");
61+
}
62+
const { DatabaseSync } = rNodeSql.Ok();
63+
return new NodeSqliteConn(fs, (path: string) => new DatabaseSync(path));
64+
}
65+
private constructor(fs: SysFileSystem, dbs: (path: string) => DatabaseSync) {
66+
this.fs = fs;
67+
this.#dbs = dbs;
68+
}
69+
async open(path: string): Promise<FPSql> {
70+
return new NodeSqlite(this.#dbs(path), this.fs, path);
71+
}
72+
}
473

574
export class DenoFileSystem implements SysFileSystem {
675
fs?: {
@@ -14,6 +83,10 @@ export class DenoFileSystem implements SysFileSystem {
1483
writeFile: typeof Deno.writeFile;
1584
};
1685

86+
sqlite(): Promise<FPSqlConn> {
87+
return this.start().then((fs) => NodeSqliteConn.create(fs));
88+
}
89+
1790
async start(): Promise<SysFileSystem> {
1891
this.fs = Deno as unknown as DenoFileSystem["fs"];
1992
return this;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../../../tsconfig.json",
33
"compilerOptions": {
4-
"types": ["deno"],
4+
"types": ["deno", "node"],
55
"outDir": "./dist"
66
}
77
}

core/gateways/file-node/node-filesystem.ts

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,77 @@
11
import type { PathLike, MakeDirectoryOptions, Stats, ObjectEncodingOptions } from "node:fs";
22
import type { mkdir, readdir, rm, copyFile, readFile, stat, unlink, writeFile } from "node:fs/promises";
33
import { toArrayBuffer } from "./to-array-buffer.js";
4-
import type { SysFileSystem } from "@fireproof/core-types-base";
4+
import type { FPSqlCmd, FPSqlConn, FPSql, FPSqlResult, SysFileSystem } from "@fireproof/core-types-base";
5+
import { exception2Result } from "@adviser/cement";
6+
import type { DatabaseSync } from "node:sqlite";
7+
8+
class NodeSqlite implements FPSql {
9+
readonly path: string;
10+
readonly db: DatabaseSync;
11+
readonly fs: SysFileSystem;
12+
13+
constructor(db: DatabaseSync, fs: SysFileSystem, path: string) {
14+
this.db = db;
15+
this.path = path;
16+
this.fs = fs;
17+
}
18+
19+
async batch(sqlCmds: FPSqlCmd[]): Promise<FPSqlResult[]> {
20+
const ret: FPSqlResult[] = [];
21+
const withStmt = sqlCmds.map((cmd) => ({
22+
...cmd,
23+
argss: cmd.argss ?? [[]],
24+
stmt: this.db.prepare(cmd.sql),
25+
}));
26+
for (const cmd of withStmt) {
27+
for (const args of cmd.argss) {
28+
const r = exception2Result(() => cmd.stmt.all(...args));
29+
switch (true) {
30+
case r.isOk():
31+
ret.push({ rows: r.Ok().map((i) => Object.values(i)) });
32+
break;
33+
case r.isErr():
34+
ret.push({ error: r.Err() });
35+
break;
36+
}
37+
}
38+
}
39+
return Promise.resolve(ret);
40+
}
41+
42+
async transaction<T>(fn: (tx: FPSql) => Promise<T>): Promise<T> {
43+
return fn(this);
44+
}
45+
46+
close(): Promise<void> {
47+
this.db.close();
48+
return Promise.resolve();
49+
}
50+
destroy(): Promise<void> {
51+
return this.fs.rm(this.path);
52+
}
53+
}
54+
55+
class NodeSqliteConn implements FPSqlConn {
56+
readonly fs: SysFileSystem;
57+
readonly #dbs: (path: string) => DatabaseSync;
58+
59+
static async create(fs: SysFileSystem): Promise<NodeSqliteConn> {
60+
const rNodeSql = await exception2Result(() => import("node:sqlite"));
61+
if (rNodeSql.isErr()) {
62+
throw new Error("Need node:sqlite in node 22 and deno.");
63+
}
64+
const { DatabaseSync } = rNodeSql.Ok();
65+
return new NodeSqliteConn(fs, (path: string) => new DatabaseSync(path));
66+
}
67+
private constructor(fs: SysFileSystem, dbs: (path: string) => DatabaseSync) {
68+
this.fs = fs;
69+
this.#dbs = dbs;
70+
}
71+
async open(path: string): Promise<FPSql> {
72+
return new NodeSqlite(this.#dbs(path), this.fs, path);
73+
}
74+
}
575

676
export class NodeFileSystem implements SysFileSystem {
777
fs?: {
@@ -15,6 +85,10 @@ export class NodeFileSystem implements SysFileSystem {
1585
writeFile: typeof writeFile;
1686
};
1787

88+
sqlite(): Promise<FPSqlConn> {
89+
return this.start().then((fs) => NodeSqliteConn.create(fs));
90+
}
91+
1892
async start(): Promise<SysFileSystem> {
1993
this.fs = await import("node:fs/promises");
2094
return this;
@@ -45,38 +119,3 @@ export class NodeFileSystem implements SysFileSystem {
45119
return this.fs?.writeFile(path, data);
46120
}
47121
}
48-
49-
// import { type NodeMap, join } from "../../sys-container.js";
50-
// import type { ObjectEncodingOptions, PathLike } from "fs";
51-
// import * as fs from "fs/promises";
52-
// import * as path from "path";
53-
// import * as os from "os";
54-
// import * as url from "url";
55-
// import { toArrayBuffer } from "./utils.js";
56-
57-
// export async function createNodeSysContainer(): Promise<NodeMap> {
58-
// // const nodePath = "node:path";
59-
// // const nodeOS = "node:os";
60-
// // const nodeURL = "node:url";
61-
// // const nodeFS = "node:fs";
62-
// // const fs = (await import("node:fs")).promises;
63-
// // const assert = "assert";
64-
// // const path = await import("node:path");
65-
// return {
66-
// state: "node",
67-
// ...path,
68-
// // ...(await import("node:os")),
69-
// // ...(await import("node:url")),
70-
// ...os,
71-
// ...url,
72-
// ...fs,
73-
// join,
74-
// stat: fs.stat as NodeMap["stat"],
75-
// readdir: fs.readdir as NodeMap["readdir"],
76-
// readfile: async (path: PathLike, options?: ObjectEncodingOptions): Promise<Uint8Array> => {
77-
// const rs = await fs.readFile(path, options);
78-
// return toArrayBuffer(rs);
79-
// },
80-
// writefile: fs.writeFile as NodeMap["writefile"],
81-
// };
82-
// }

core/gateways/file/fp-sync.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

core/gateways/file/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ export * from "./key-bag-file.js";
22
export * from "./sys-file-system-factory.js";
33
export * from "./version.js";
44
export * from "./gateway-impl.js";
5-
export * from "./fp-sync.js";

core/gateways/file/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@
4747
"@fireproof/core-gateways-file-node": "workspace:0.0.0",
4848
"@fireproof/core-runtime": "workspace:0.0.0",
4949
"@fireproof/core-types-base": "workspace:0.0.0",
50-
"@fireproof/core-types-protocols-sync": "workspace:0.0.0",
5150
"@fireproof/core-types-blockstore": "workspace:0.0.0",
52-
"@fireproof/vendor": "workspace:0.0.0",
53-
"classic-level": "^3.0.0"
51+
"@fireproof/vendor": "workspace:0.0.0"
5452
}
5553
}

0 commit comments

Comments
 (0)