Skip to content

Commit

Permalink
Merge pull request #209 from StarfilesFileSharing/alpha
Browse files Browse the repository at this point in the history
Alpha
  • Loading branch information
QuixThe2nd authored Nov 26, 2024
2 parents e98991a + 5f83417 commit cbe6fa7
Show file tree
Hide file tree
Showing 23 changed files with 297 additions and 289 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ build/

__sysdb__.sqlite
public/docs/
typedoc-theme/
typedoc-theme/

sandbox/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<h1 align="center">Hydrafiles</h1>
<p align="center">The (P2P) web privacy layer.</p>
<a href="https://github.com/StarfilesFileSharing/hydrafiles/releases">Quick Install</a>
<p align="center">
<a href="https://github.com/StarfilesFileSharing/hydrafiles/releases">Quick Install</a>
<br><br>
<img src="./public/favicon.ico">
<br><br>
Please submit ideas & feature requests as well as any problems you're facing as an issue.
Expand Down
14 changes: 9 additions & 5 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ console.log(
format: "esm",
platform: "browser",
sourcemap: true,
minify: true,
treeShaking: true,
keepNames: true,
minify: false, // TODO: Toggle for dev/prod
treeShaking: false, // TODO: Toggle for dev/prod
sourcesContent: true, // TODO: Toggle for dev/prod
metafile: true,
}),
await esbuild.build({
plugins: [...denoPlugins()],
Expand All @@ -22,10 +24,12 @@ console.log(
format: "esm",
platform: "browser",
sourcemap: true,
minify: true,
treeShaking: true,
external: ["https://esm.sh/[email protected]"],
keepNames: true,
minify: false, // TODO: Toggle for dev/prod
treeShaking: false, // TODO: Toggle for dev/prod
sourcesContent: true, // TODO: Toggle for dev/prod
metafile: true,
external: ["https://esm.sh/[email protected]"],
}),
);

Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@starfiles/hydrafiles",
"version": "0.10.7",
"version": "0.11",
"description": "The (P2P) web privacy layer.",
"main": "src/hydrafiles.ts",
"exports": {
Expand Down
32 changes: 23 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export interface Config {
httpsPort: number;

/**
* SSL Certificate Path.
* @default "./certs/ca/localhost/localhost.crt"
* SSL Certificate Path. Replace `../` with `./` if replacing the cert.
* @default "../certs/ca/localhost/localhost.crt"
*/
sslCertPath: string;

/**
* SSL Key Path.
* @default "./certs/ca/localhost/localhost.key"
* SSL Key Path. Replace `../` with `./` if replacing the cert.
* @default "../certs/ca/localhost/localhost.key"
*/
sslKeyPath: string;

Expand Down Expand Up @@ -176,15 +176,27 @@ export interface Config {
* @default ""
*/
deriveKey: string;

/**
* Base directory to save files to.
* @default "./"
*/
baseDir: `${string}/`;

/**
* Whether or not to listen for & serve requests via HTTP, WebRTC, WS. Disabling this breaks many P2P functionalities such as lowing data availability and lowering privacy. Static files (i.e. GUI and docs) are still served.
* @default true
*/
listen: boolean;
}

// DO NOT CHANGE DEFAULT CONFIG - Check documentation on how to set custom config.
const defaultConfig: Config = {
hostname: "0.0.0.0",
httpPort: 80,
httpsPort: 443,
sslCertPath: "./certs/ca/localhost/localhost.crt",
sslKeyPath: "./certs/ca/localhost/localhost.key",
sslCertPath: "../certs/ca/localhost/localhost.crt",
sslKeyPath: "../certs/ca/localhost/localhost.key",
publicHostname: "http://127.0.0.1:80",
maxCache: -1,
permaFiles: ["04aa07009174edc6f03224f003a435bcdc9033d2c52348f3a35fbb342ea82f6f"],
Expand All @@ -203,11 +215,13 @@ const defaultConfig: Config = {
logLevel: "normal",
summarySpeed: 300000,
backfill: true,
comparePeersSpeed: 3600000,
compareFilesSpeed: 300000,
announceSpeed: 30000,
comparePeersSpeed: 300000,
compareFilesSpeed: 600000,
announceSpeed: 60000,
dontUseFileSystemAPI: false,
deriveKey: "",
baseDir: "./",
listen: true,
};

/** @internal */
Expand Down
17 changes: 9 additions & 8 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Database as SQLite } from "jsr:@db/sqlite";
import type { indexedDB } from "https://deno.land/x/[email protected]/ponyfill.ts";
import { ErrorMissingRequiredProperty, ErrorNotFound, ErrorNotInitialised } from "./errors.ts";
import Hydrafiles, { type NonEmptyString } from "./hydrafiles.ts";
import Hydrafiles from "./hydrafiles.ts";
import type { NonEmptyString } from "./utils.ts";

export interface ModelType {
tableName: string;
Expand Down Expand Up @@ -66,7 +67,7 @@ export default class Database<T extends ModelType> {

if (typeof window === "undefined") {
const SQLite = (await import("jsr:@db/sqlite")).Database;
const db: DatabaseWrapperSQLite = { type: "SQLITE", db: new SQLite(`${model.tableName}.db`) };
const db: DatabaseWrapperSQLite = { type: "SQLITE", db: new SQLite(`${client.config.baseDir}${model.tableName}.db`) };
database.db = db;

const columns = Object.entries(model.columns)
Expand All @@ -83,11 +84,11 @@ export default class Database<T extends ModelType> {
Object.entries(model.columns).forEach(([name, def]) => addColumnIfNotExists(db.db, model.tableName, name, def.type));
} else {
const db = await new Promise<IDBDatabase>((resolve, reject) => {
console.log(`Startup: ${model.tableName}DB: Opening IndexedDB Connection`);
console.log(`Database: ${model.tableName}DB: Opening IndexedDB Connection`);
// @ts-expect-error:
const request = indexedDB.open(model.tableName, 2);
request.onupgradeneeded = (event): void => {
console.log(`Startup: ${model.tableName}DB: On Upgrade Needed`);
console.log(`Database: ${model.tableName}DB: On Upgrade Needed`);
// @ts-expect-error:
if (!event.target.result.objectStoreNames.contains(model.tableName)) {
// @ts-expect-error:
Expand All @@ -102,15 +103,15 @@ export default class Database<T extends ModelType> {
}
};
request.onsuccess = () => {
console.log(`Startup: ${model.tableName}DB: On Success`);
console.log(`Database: ${model.tableName}DB: On Success`);
resolve(request.result as unknown as IDBDatabase);
};
request.onerror = () => {
console.error(`Startup: ${model.tableName}DB error:`, request.error);
console.error(`Database: ${model.tableName}DB error:`, request.error);
reject(request.error);
};
request.onblocked = () => {
console.error(`Startup: ${model.tableName}DB: Blocked. Close other tabs with this site open.`);
console.error(`Database: ${model.tableName}DB: Blocked. Close other tabs with this site open.`);
};
});
database.db = { type: "INDEXEDDB", db: db };
Expand Down Expand Up @@ -361,7 +362,7 @@ export default class Database<T extends ModelType> {
} as Partial<DatabaseModal<T>>;

for (const [key, def] of Object.entries(this.model.columns)) {
if (!def.default && !def.isNullable && result[key as keyof DatabaseModal<T>] === undefined) return new ErrorMissingRequiredProperty(`Missing required property: ${key}`);
if (!def.default && !def.isNullable && result[key as keyof DatabaseModal<T>] === undefined) return new ErrorMissingRequiredProperty(key);
}

return result as DatabaseModal<T>;
Expand Down
33 changes: 33 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ export class ErrorNotFound extends Error {
readonly brand = Symbol();
}
export class ErrorMissingRequiredProperty extends Error {
constructor(msg?: string) {
super(msg);
console.error("ErrorMissingRequiredProperty", this.stack);
}
readonly brand = Symbol();
}
export class ErrorUnreachableCodeReached extends Error {
constructor() {
super("Error of type 'ErrorUnreachableCodeReached' thrown");
console.error("ErrorUnreachableCodeReached", this.stack);
}
readonly brand = Symbol();
}
export class ErrorNotInitialised extends Error {
constructor() {
super("Error of type 'ErrorNotInitialised' thrown");
console.error("ErrorNotInitialised", this.stack);
}
readonly brand = Symbol();
}
export class ErrorWrongDatabaseType extends Error {
constructor() {
super("Error of type 'ErrorWrongDatabaseType' thrown");
console.error("ErrorWrongDatabaseType", this.stack);
}
readonly brand = Symbol();
}
export class ErrorChecksumMismatch extends Error {
Expand All @@ -23,11 +39,28 @@ export class ErrorRequestFailed extends Error {
readonly brand = Symbol();
}
export class ErrorDownloadFailed extends Error {
constructor() {
super("Error of type 'ErrorDownloadFailed' thrown");
console.error("ErrorDownloadFailed", this.stack);
}
readonly brand = Symbol();
}
export class ErrorFailedToReadFile extends Error {
constructor(msg?: string) {
super(msg);
console.error("ErrorFailedToReadFile", this.stack);
}
readonly brand = Symbol();
}

export class ErrorInsufficientBalance extends Error {
constructor() {
super("Error of type 'ErrorInsufficientBalance' thrown");
console.error("ErrorInsufficientBalance", this.stack);
}
readonly brand = Symbol();
}

export class ErrorUnexpectedProtocol extends Error {
readonly brand = Symbol();
}
8 changes: 4 additions & 4 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class File implements FileAttributes {
const responses = Files._client.rpcClient.rtc.fetch(new URL(`http://localhost/download/${this.hash}`));
for (let i = 0; i < responses.length; i++) {
const response = await responses[i];
const fileContent = new Uint8Array(await response.arrayBuffer());
const fileContent = new Uint8Array(response.arrayBuffer());
console.log(`File: ${this.hash} Validating hash`);
const verifiedHash = await Utils.hashUint8Array(fileContent);
console.log(`File: ${this.hash} Done Validating hash`);
Expand Down Expand Up @@ -448,7 +448,7 @@ class Files {
backfillFiles = (): void => {
setTimeout(async () => {
while (true) {
console.log("Backfilling file");
console.log("Files: Finding file to backfill");
const keys = Array.from(this.filesHash.keys());
if (keys.length === 0) {
await delay(500);
Expand All @@ -467,7 +467,7 @@ class Files {

// TODO: Compare list between all peers and give score based on how similar they are. 100% = all exactly the same, 0% = no items in list were shared. The lower the score, the lower the propagation times, the lower the decentralisation
async updateFileList(onProgress?: (progress: number, total: number) => void): Promise<void> {
console.log(`Comparing file list`);
console.log(`Files: Comparing file list`);
let files: FileAttributes[] = [];
const responses = await Promise.all(await Files._client.rpcClient.fetch("http://localhost/files"));
for (let i = 0; i < responses.length; i++) {
Expand All @@ -476,7 +476,7 @@ class Files {
try {
files = files.concat(JSON.parse(response.text()) as FileAttributes[]);
} catch (e) {
if (Files._client.config.logLevel === "verbose") console.log(e);
if (!(e instanceof SyntaxError)) throw e;
}
}
}
Expand Down
Loading

0 comments on commit cbe6fa7

Please sign in to comment.