Library/Std moved to top level, now bundled in same way as plugs

This commit is contained in:
Zef Hemel
2025-03-24 17:08:04 +01:00
parent 771c1606ed
commit 0bc5cb7356
20 changed files with 20 additions and 153 deletions
+1
View File
@@ -0,0 +1 @@
Read DEVELOPMENT.md carefully.
+2 -7
View File
@@ -25,15 +25,10 @@
- Comments: Focus on "why" not "what", especially for complex logic
- Prefer const over let when variable won't be reassigned
- In Lua: use camelCase for variables and functions
- Tests end with .test.ts
## Architecture Overview
### Server and Client Architecture
- **ServerSystem**: Manages server-side operations, file system, and plugin management
- **ClientSystem**: Handles client UI, editor interactions, and supports both online and offline-synced modes
- **Space**: Core abstraction for document storage, reading/writing pages and managing metadata
- **SpacePrimitives**: Storage backend interface supporting multiple implementations (disk, HTTP, S3)
### Plugin System (Plugs)
- **PlugOS**: Extension framework with System, Plug, Sandbox, and Syscalls components
- **Hooks**: Extension points (commands, events, slash commands, widgets)
@@ -56,6 +51,7 @@
## Directory Structure
### Top-level Directories
- `/Library`: Standard library shipped with SilverBullet
- `/cmd`: Command-line interfaces and entry points
- `/common`: Shared core functionality for both server and client
- `/lib`: Utility libraries and internal modules
@@ -68,7 +64,6 @@
### Key Areas
- `/common/spaces`: Space implementation with different backend primitives
- `/common/space_lua`: Lua scripting implementation with parsers and runtime
- `/common/template`: Template language implementation
- `/lib/plugos`: Plugin system core functionality
- `/plugs`: Plugs for core functionality (editor, indexing, etc) distributed with the system
- `/web`: Client implementation
+2 -1
View File
@@ -15,7 +15,8 @@ if (import.meta.main) {
`./plugs/${name}/${name}.plug.yaml`
);
const targetDir = path.join("dist_plug_bundle", "_plug");
const plugBundlePath = "dist_plug_bundle";
const targetDir = path.join(plugBundlePath, "_plug");
Deno.mkdirSync(targetDir, { recursive: true });
Deno.mkdirSync("dist", { recursive: true });
+12 -1
View File
@@ -16,7 +16,12 @@ export async function bundleAll(
await buildCopyBundleAssets();
let timer;
if (watch) {
const watcher = Deno.watchFs(["web", "common", "dist_plug_bundle"]);
const watcher = Deno.watchFs([
"web",
"common",
"dist_plug_bundle/_plug",
"Library",
]);
for await (const _event of watcher) {
if (timer) {
clearTimeout(timer);
@@ -76,6 +81,11 @@ async function buildCopyBundleAssets() {
await Deno.mkdir("dist_client_bundle", { recursive: true });
await Deno.mkdir("dist_plug_bundle", { recursive: true });
// Copy Library files
await copy("Library", "dist_plug_bundle/Library", {
overwrite: true,
});
await bundleFolder(
"dist_plug_bundle",
"dist/plug_asset_bundle.json",
@@ -121,6 +131,7 @@ async function buildCopyBundleAssets() {
await Deno.writeTextFile("dist_client_bundle/service_worker.js", swCode);
await copyAssets("dist_client_bundle/.client");
await bundleFolder("dist_client_bundle", "dist/client_asset_bundle.json");
console.log("Built!");
+1 -33
View File
@@ -1,37 +1,5 @@
name: core
assets:
- "Library/Std/*"
functions:
init:
path: ./std.ts:init
env: server
events:
- system:ready
readFile:
path: ./std.ts:readFile
pageNamespace:
pattern: "^Library/Std/.+"
operation: readFile
writeFile:
path: ./std.ts:writeFile
pageNamespace:
pattern: "^Library/Std/.+"
operation: writeFile
deleteFile:
path: ./std.ts:deleteFile
pageNamespace:
pattern: "^Library/Std/.+"
operation: deleteFile
getFileMeta:
path: ./std.ts:getFileMeta
pageNamespace:
pattern: "^Library/Std/.+"
operation: getFileMeta
listFiles:
path: ./std.ts:listFiles
pageNamespace:
pattern: "^Library/Std/.+"
operation: listFiles
config:
# Built-in schemas
schema.tag:
-107
View File
@@ -1,107 +0,0 @@
import {
asset,
codeWidget,
datastore,
editor,
mq,
system,
} from "@silverbulletmd/silverbullet/syscalls";
import type { FileMeta } from "@silverbulletmd/silverbullet/types";
import { sleep } from "$lib/async.ts";
export async function listFiles(): Promise<FileMeta[]> {
return await asset.listFiles("core");
}
export async function readFile(
name: string,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
const text = await asset.readAsset("core", name, "utf8");
return {
data: new TextEncoder().encode(text),
meta: await asset.getFileMeta("core", name),
};
}
export function writeFile(): Promise<FileMeta> {
throw new Error("Writing std files not supported");
}
export function deleteFile(): Promise<void> {
throw new Error("Deleting std files not supported");
}
export function getFileMeta(name: string): Promise<FileMeta> {
return asset.getFileMeta("core", name);
}
const stdLibCacheKey = ["stdLibCache"];
type StdLibCache = Record<string, number>; // page name -> last modified time
/**
* Ensure indexing all Library/Std files
* @param initialSync set to true when this is triggered after the initial sync
* @returns
*/
export async function init(initialSync = false) {
// Check if in read-only mode
if (await system.getMode() === "ro") {
return;
}
console.log("Doing stdlib init");
let stdLibCache: StdLibCache | undefined = await datastore.get(
stdLibCacheKey,
);
if (!stdLibCache || initialSync) {
stdLibCache = {};
}
// Iterate over the current file listing, check if any new files have been added, removed or modified
const newListing = await listFiles();
let anythingChanged = false;
// First check for files that were removed
for (const cachedFile of Object.keys(stdLibCache)) {
if (!newListing.find((f) => f.name === cachedFile)) {
anythingChanged = true;
console.log(`Clearing index for removed file ${cachedFile}`);
await system.invokeFunction("index.clearDSIndex", cachedFile);
delete stdLibCache[cachedFile];
}
}
// Then check for new/modified files
for (const file of newListing) {
const lastModified = file.lastModified;
// Check if file is new or modified compared to cache
if (!stdLibCache[file.name] || stdLibCache[file.name] !== lastModified) {
anythingChanged = true;
await system.invokeFunction("index.clearDSIndex", file.name);
await mq.send("indexQueue", file.name);
stdLibCache[file.name] = lastModified;
}
}
// Save updated cache
await datastore.set(stdLibCacheKey, stdLibCache);
if (anythingChanged) {
console.log("Waiting for index queue to be processed");
let queueProcessed = false;
while (!queueProcessed) {
const stats = await mq.getQueueStats("indexQueue");
if (stats.queued === 0 && stats.processing === 0) {
queueProcessed = true;
}
await sleep(100);
}
if (!initialSync) {
// If this is not a
console.log("Index queue processed, reloading system");
await editor.reloadConfigAndCommands();
await codeWidget.refreshAll();
}
}
}
+2 -3
View File
@@ -184,8 +184,7 @@ export class Client {
this.eventHook,
(path) => { // isSyncCandidate
// Exclude all plug space primitives paths
return !this.plugSpaceRemotePrimitives.isLikelyHandled(path) &&
!path.startsWith("Library/Std/");
return !this.plugSpaceRemotePrimitives.isLikelyHandled(path);
},
);
@@ -511,7 +510,7 @@ export class Client {
),
this.eventHook,
// Don't trigger events for these paths, they're handled in a custom way
["Library/Std/"],
// ["Library/Std/"],
);
this.spaceKV = spaceKvPrimitives;
-1
View File
@@ -318,7 +318,6 @@ export class ClientSystem {
// await this.client.space.fetchPageList();
this.indexOngoing = true;
await this.system.invokeFunction("index.reindexSpace", []);
await this.system.invokeFunction("core.init", [true]); // initialSync = true
console.info("Full space index complete.");
await this.markFullSpaceIndexComplete(this.ds);
this.indexOngoing = false;