Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ const REFLUX_HEADER = "reflux-middleware";
const REFLUX_VERSION = "1.0.0";

export class RefluxAPI {
private pluginStorage = localforage.createInstance({
#pluginStorage = localforage.createInstance({
name: 'Reflux',
storeName: 'plugins'
});
private statusStorage = localforage.createInstance({
#statusStorage = localforage.createInstance({
name: 'Reflux',
storeName: 'status'
});

private metadataStorageInstance?: LocalForage;
#metadataStorageInstance?: LocalForage;

constructor() {
try {
this.pluginStorage = localforage.createInstance({
this.#pluginStorage = localforage.createInstance({
name: 'Reflux',
storeName: 'plugins'
});
this.statusStorage = localforage.createInstance({
this.#statusStorage = localforage.createInstance({
name: 'Reflux',
storeName: 'status'
});
Expand All @@ -79,26 +79,26 @@ export class RefluxAPI {
}
}

private ensureStorages(): void {
if (!this.pluginStorage) {
this.pluginStorage = localforage.createInstance({ name: 'Reflux', storeName: 'plugins' });
#ensureStorages(): void {
if (!this.#pluginStorage) {
this.#pluginStorage = localforage.createInstance({ name: 'Reflux', storeName: 'plugins' });
}
if (!this.statusStorage) {
this.statusStorage = localforage.createInstance({ name: 'Reflux', storeName: 'status' });
if (!this.#statusStorage) {
this.#statusStorage = localforage.createInstance({ name: 'Reflux', storeName: 'status' });
}
}

async addPlugin(plugin: RefluxPlugin): Promise<void> {
try {
this.ensureStorages();
if (!this.pluginStorage) throw new Error('pluginStorage unavailable');
await this.pluginStorage.setItem(plugin.name, plugin.function);
this.#ensureStorages();
if (!this.#pluginStorage) throw new Error('pluginStorage unavailable');
await this.#pluginStorage.setItem(plugin.name, plugin.function);

const metadataStorage = localforage.createInstance({
name: 'Reflux',
storeName: 'pluginMetadata'
});

#
await metadataStorage.setItem(plugin.name, {
sites: plugin.sites,
name: plugin.name
Expand All @@ -113,19 +113,19 @@ export class RefluxAPI {

async removePlugin(name: string): Promise<void> {
try {
this.ensureStorages();
if (!this.pluginStorage) throw new Error('pluginStorage unavailable');
await this.pluginStorage.removeItem(name);
this.#ensureStorages();
if (!this.#pluginStorage) throw new Error('pluginStorage unavailable');
await this.#pluginStorage.removeItem(name);

const metadataStorage = localforage.createInstance({
name: 'Reflux',
storeName: 'pluginMetadata'
});
await metadataStorage.removeItem(name);

const enabledPlugins = await this.statusStorage.getItem<string[]>('enabled') || [];
const enabledPlugins = await this.#statusStorage.getItem<string[]>('enabled') || [];
const updatedEnabled = enabledPlugins.filter(id => id !== name);
await this.statusStorage.setItem('enabled', updatedEnabled);
await this.#statusStorage.setItem('enabled', updatedEnabled);

console.log(`Plugin ${name} removed successfully`);
} catch (error) {
Expand All @@ -136,13 +136,13 @@ export class RefluxAPI {

async enablePlugin(name: string): Promise<void> {
try {
this.ensureStorages();
if (!this.statusStorage) throw new Error('statusStorage unavailable');
const enabledPlugins = await this.statusStorage.getItem<string[]>('enabled') || [];
this.#ensureStorages();
if (!this.#statusStorage) throw new Error('statusStorage unavailable');
const enabledPlugins = await this.#statusStorage.getItem<string[]>('enabled') || [];

if (!enabledPlugins.includes(name)) {
enabledPlugins.push(name);
await this.statusStorage.setItem('enabled', enabledPlugins);
await this.#statusStorage.setItem('enabled', enabledPlugins);
}

console.log(`Plugin ${name} enabled successfully`);
Expand All @@ -154,11 +154,11 @@ export class RefluxAPI {

async disablePlugin(name: string): Promise<void> {
try {
this.ensureStorages();
if (!this.statusStorage) throw new Error('statusStorage unavailable');
const enabledPlugins = await this.statusStorage.getItem<string[]>('enabled') || [];
this.#ensureStorages();
if (!this.#statusStorage) throw new Error('statusStorage unavailable');
const enabledPlugins = await this.#statusStorage.getItem<string[]>('enabled') || [];
const updatedEnabled = enabledPlugins.filter(id => id !== name);
await this.statusStorage.setItem('enabled', updatedEnabled);
await this.#statusStorage.setItem('enabled', updatedEnabled);

console.log(`Plugin ${name} disabled successfully`);
} catch (error) {
Expand All @@ -169,10 +169,10 @@ export class RefluxAPI {

async listPlugins(): Promise<Array<{ name: string; sites: string[]; enabled: boolean; function: string }>> {
try {
this.ensureStorages();
if (!this.pluginStorage || !this.statusStorage) throw new Error('storages unavailable');
const pluginKeys = await this.pluginStorage.keys();
const enabledPlugins = await this.statusStorage.getItem<string[]>('enabled') || [];
this.#ensureStorages();
if (!this.#pluginStorage || !this.#statusStorage) throw new Error('storages unavailable');
const pluginKeys = await this.#pluginStorage.keys();
const enabledPlugins = await this.#statusStorage.getItem<string[]>('enabled') || [];
const metadataStorage = localforage.createInstance({
name: 'Reflux',
storeName: 'pluginMetadata'
Expand All @@ -181,7 +181,7 @@ export class RefluxAPI {
const plugins: Array<{ name: string; sites: string[]; enabled: boolean; function: string }> = [];

for (const key of pluginKeys) {
const pluginCode = await this.pluginStorage.getItem<string>(key);
const pluginCode = await this.#pluginStorage.getItem<string>(key);
const metadata = await metadataStorage.getItem<{sites: string[], name: string}>(key);

if (pluginCode) {
Expand All @@ -203,9 +203,9 @@ export class RefluxAPI {

async getEnabledPlugins(): Promise<string[]> {
try {
this.ensureStorages();
if (!this.statusStorage) return [];
return await this.statusStorage.getItem<string[]>('enabled') || [];
this.#ensureStorages();
if (!this.#statusStorage) return [];
return await this.#statusStorage.getItem<string[]>('enabled') || [];
} catch (error) {
console.error('Error getting enabled plugins:', error);
return [];
Expand Down
29 changes: 16 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ export type RefluxOptions = {
export default class RefluxTransport implements BareTransport {
ready = false;

private inner!: BareTransport;
private wrapped!: MiddlewareTransport;
#inner!: BareTransport;
#wrapped!: MiddlewareTransport;
#opts: RefluxOptions;

constructor(private opts: RefluxOptions) {}
constructor(opts: RefluxOptions) {
this.#opts = opts;
}

get middleware(): MiddlewareTransport {
return this.wrapped;
return this.#wrapped;
}

async init() {
Expand All @@ -30,22 +33,22 @@ export default class RefluxTransport implements BareTransport {
middleware = [],
controlPort,
...innerOptions
} = this.opts;
} = this.#opts;

console.debug('%cRF%c Initializing transport wrapper', 'background: #0066cc; color: white; padding: 2px 4px; border-radius: 2px; font-weight: bold', '');

try {
const mod = await import(transportPath);
const TransportClass = mod.default;

this.inner = new TransportClass(innerOptions);
this.#inner = new TransportClass(innerOptions);

if (typeof this.inner.init === 'function') {
await this.inner.init();
if (typeof this.#inner.init === 'function') {
await this.#inner.init();
}

this.wrapped = new MiddlewareTransport(this.inner);
await this.wrapped.init();
this.#wrapped = new MiddlewareTransport(this.#inner);
await this.#wrapped.init();

this.ready = true;
console.debug('%cRF%c Transport ready', 'background: #0066cc; color: white; padding: 2px 4px; border-radius: 2px; font-weight: bold', '');
Expand All @@ -56,7 +59,7 @@ export default class RefluxTransport implements BareTransport {
}

async meta() {
return this.wrapped.meta?.();
return this.#wrapped.meta?.();
}

async request(
Expand All @@ -66,7 +69,7 @@ export default class RefluxTransport implements BareTransport {
headers: BareHeaders,
signal?: AbortSignal
): Promise<TransferrableResponse> {
return this.wrapped.request(remote, method, body, headers, signal);
return this.#wrapped.request(remote, method, body, headers, signal);
}

connect(
Expand All @@ -78,7 +81,7 @@ export default class RefluxTransport implements BareTransport {
onclose: (code: number, reason: string) => void,
onerror: (error: string) => void
): [(data: Blob | ArrayBuffer | string) => void, (code: number, reason: string) => void] {
return this.wrapped.connect(
return this.#wrapped.connect(
url,
protocols,
requestHeaders,
Expand Down
Loading