bun_plugins 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -0
- package/dist/PluginManager.d.ts +62 -0
- package/dist/PluginManager.d.ts.map +1 -0
- package/dist/PluginManager.js +557 -0
- package/dist/PluginManager.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/managers/ContextFactory.d.ts +6 -0
- package/dist/managers/ContextFactory.d.ts.map +1 -0
- package/dist/managers/ContextFactory.js +109 -0
- package/dist/managers/ContextFactory.js.map +1 -0
- package/dist/managers/DependencyManager.d.ts +10 -0
- package/dist/managers/DependencyManager.d.ts.map +1 -0
- package/dist/managers/DependencyManager.js +94 -0
- package/dist/managers/DependencyManager.js.map +1 -0
- package/dist/managers/HooksManager.d.ts +21 -0
- package/dist/managers/HooksManager.d.ts.map +1 -0
- package/dist/managers/HooksManager.js +117 -0
- package/dist/managers/HooksManager.js.map +1 -0
- package/dist/managers/ResourceManager.d.ts +16 -0
- package/dist/managers/ResourceManager.d.ts.map +1 -0
- package/dist/managers/ResourceManager.js +50 -0
- package/dist/managers/ResourceManager.js.map +1 -0
- package/dist/storage/JsonPluginStorage.d.ts +14 -0
- package/dist/storage/JsonPluginStorage.d.ts.map +1 -0
- package/dist/storage/JsonPluginStorage.js +63 -0
- package/dist/storage/JsonPluginStorage.js.map +1 -0
- package/dist/types.d.ts +167 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +56 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/errorParser.d.ts +3 -0
- package/dist/utils/errorParser.d.ts.map +1 -0
- package/dist/utils/errorParser.js +23 -0
- package/dist/utils/errorParser.js.map +1 -0
- package/dist/utils/pluginValidator.d.ts +47 -0
- package/dist/utils/pluginValidator.d.ts.map +1 -0
- package/dist/utils/pluginValidator.js +76 -0
- package/dist/utils/pluginValidator.js.map +1 -0
- package/dist/utils/security.d.ts +3 -0
- package/dist/utils/security.d.ts.map +1 -0
- package/dist/utils/security.js +26 -0
- package/dist/utils/security.js.map +1 -0
- package/dist/worker/WorkerRunner.d.ts +2 -0
- package/dist/worker/WorkerRunner.d.ts.map +1 -0
- package/dist/worker/WorkerRunner.js +254 -0
- package/dist/worker/WorkerRunner.js.map +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Bun Plugins
|
|
2
|
+
|
|
3
|
+
A powerful, secure, and isolated plugin system for Bun applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Isolated Workers**: Plugins run in their own Bun Workers, providing security and crashing isolation.
|
|
8
|
+
- **Permission System**: Fine-grained control over network, file system, and environment access.
|
|
9
|
+
- **Hook System**: Intercept and modify resource loading with `onResolve` and `onLoad` hooks (compatible with Bun's plugin API).
|
|
10
|
+
- **Type Safe**: Written in TypeScript with full type definitions and Zod validation.
|
|
11
|
+
- **Communication RPC**: Seamless communication between the host and plugins using a proxy-based RPC.
|
|
12
|
+
- **Automatic Cleanup**: Resources (timers, workers, event listeners) are automatically cleaned up when a plugin is unloaded.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
bun add bun_plugins
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### 1. Define a Plugin
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
// plugins/MyPlugin.ts
|
|
26
|
+
import type { IPlugin, PluginContext } from "bun_plugins";
|
|
27
|
+
|
|
28
|
+
export class MyPlugin implements IPlugin {
|
|
29
|
+
name = "my-plugin";
|
|
30
|
+
version = "1.0.0";
|
|
31
|
+
|
|
32
|
+
async onLoad(context: PluginContext) {
|
|
33
|
+
context.log.info("Plugin loaded!");
|
|
34
|
+
|
|
35
|
+
context.on("hello", (name) => {
|
|
36
|
+
console.log(`Hello, ${name}!`);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. Use the Plugin Manager
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { PluginManager } from "bun_plugins";
|
|
46
|
+
import { join } from "node:path";
|
|
47
|
+
|
|
48
|
+
const manager = new PluginManager();
|
|
49
|
+
|
|
50
|
+
// Register a plugin class instance
|
|
51
|
+
await manager.register(new MyPlugin());
|
|
52
|
+
|
|
53
|
+
// Or load an isolated plugin from a file
|
|
54
|
+
await manager.registerIsolated(
|
|
55
|
+
join(process.cwd(), "plugins", "OtherPlugin.ts"),
|
|
56
|
+
"OtherPlugin"
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Emit events to plugins
|
|
60
|
+
manager.emit("hello", "World");
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Security
|
|
64
|
+
|
|
65
|
+
Plugins run in a restricted environment. By default, they have no permissions. You can grant them in the plugin metadata:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
export class SecurePlugin implements IPlugin {
|
|
69
|
+
name = "secure-plugin";
|
|
70
|
+
permissions = ["network"];
|
|
71
|
+
allowedDomains = ["api.example.com"];
|
|
72
|
+
|
|
73
|
+
async onLoad(context: PluginContext) {
|
|
74
|
+
const data = await context.network.fetch("https://api.example.com/data");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
MIT
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import { type IPlugin, type AppEvents, type BunWorkerOptions as WorkerOptions, type OnResolveArgs, type OnLoadArgs } from "./types";
|
|
3
|
+
import { HooksManager } from "./managers/HooksManager";
|
|
4
|
+
export declare class PluginManager extends EventEmitter {
|
|
5
|
+
private plugins;
|
|
6
|
+
private availablePlugins;
|
|
7
|
+
private resources;
|
|
8
|
+
private dependencyManager;
|
|
9
|
+
hooksManager: HooksManager;
|
|
10
|
+
private storageRoot;
|
|
11
|
+
private hostVersion;
|
|
12
|
+
private pluginLoadTimeout;
|
|
13
|
+
private workerFactory;
|
|
14
|
+
private workerRunnerPath;
|
|
15
|
+
constructor(storageRoot?: string, options?: {
|
|
16
|
+
pluginLoadTimeout?: number;
|
|
17
|
+
workerFactory?: (url: string | URL, options?: WorkerOptions) => Worker;
|
|
18
|
+
workerRunnerPath?: string;
|
|
19
|
+
});
|
|
20
|
+
getWorkerFactory(): (url: string | URL, options?: WorkerOptions) => Worker;
|
|
21
|
+
register(plugin: IPlugin): Promise<void>;
|
|
22
|
+
registerIsolated(pluginPath: string, pluginName: string): Promise<void>;
|
|
23
|
+
unregister(pluginName: string): Promise<void>;
|
|
24
|
+
getPlugin(name: string): IPlugin | undefined;
|
|
25
|
+
listPlugins(): string[];
|
|
26
|
+
emit<K extends keyof AppEvents>(eventName: K, payload: AppEvents[K]): boolean;
|
|
27
|
+
emit(eventName: string | symbol, ...args: any[]): boolean;
|
|
28
|
+
on<K extends keyof AppEvents>(eventName: K, listener: (payload: AppEvents[K]) => void): this;
|
|
29
|
+
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
30
|
+
loadPluginsFromDirectory(directoryPath?: string): Promise<void>;
|
|
31
|
+
disablePlugin(name: string): Promise<void>;
|
|
32
|
+
enablePlugin(name: string): Promise<void>;
|
|
33
|
+
reloadPlugin(name: string): Promise<void>;
|
|
34
|
+
private updatePluginState;
|
|
35
|
+
runOnResolve(args: OnResolveArgs): Promise<{
|
|
36
|
+
path: string;
|
|
37
|
+
namespace?: string;
|
|
38
|
+
} | null>;
|
|
39
|
+
runOnLoad(args: OnLoadArgs): Promise<{
|
|
40
|
+
contents?: string;
|
|
41
|
+
loader?: string;
|
|
42
|
+
} | null>;
|
|
43
|
+
toBunPlugin(): Bun.BunPlugin;
|
|
44
|
+
private hotReloadTimer;
|
|
45
|
+
enableHotReload(pluginDir: string): void;
|
|
46
|
+
getMetrics(): {
|
|
47
|
+
totalPlugins: number;
|
|
48
|
+
activePlugins: string[];
|
|
49
|
+
resources: {
|
|
50
|
+
totalWorkers: number;
|
|
51
|
+
totalTimers: number;
|
|
52
|
+
totalListeners: number;
|
|
53
|
+
pluginCount: number;
|
|
54
|
+
};
|
|
55
|
+
hooks: {
|
|
56
|
+
onResolve: number;
|
|
57
|
+
onLoad: number;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
getPluginStatus(): Record<string, any>;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=PluginManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PluginManager.d.ts","sourceRoot":"","sources":["../src/PluginManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAI3C,OAAO,EACH,KAAK,OAAO,EAEZ,KAAK,SAAS,EACd,KAAK,gBAAgB,IAAI,aAAa,EACtC,KAAK,aAAa,EAClB,KAAK,UAAU,EAKlB,MAAM,SAAS,CAAC;AAMjB,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAKvD,qBAAa,aAAc,SAAQ,YAAY;IAC7C,OAAO,CAAC,OAAO,CAAmC;IAClD,OAAO,CAAC,gBAAgB,CAAmC;IAG3D,OAAO,CAAC,SAAS,CAAkB;IACnC,OAAO,CAAC,iBAAiB,CAAoB;IACtC,YAAY,EAAE,YAAY,CAAC;IAElC,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,aAAa,CAAyD;IAC9E,OAAO,CAAC,gBAAgB,CAAS;gBACrB,WAAW,GAAE,MAAuC,EAAE,OAAO,CAAC,EAAE;QAC1E,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,aAAa,CA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