Skip to content

Commit

Permalink
server: fix plugin fork storage desync
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jun 25, 2024
1 parent 8b303e0 commit 6892b44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 18 additions & 3 deletions server/src/plugin/plugin-remote-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,30 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe

let postInstallSourceMapSupport: (scrypted: ScryptedStatic) => void;

const forks = new Set<PluginRemote>();

attachPluginRemote(peer, {
createMediaManager: async (sm, dm) => {
systemManager = sm;
deviceManager = dm
return new MediaManagerImpl(systemManager, dm);
},
onGetRemote: async (_api, _pluginId) => {
api = _api;
class PluginForkableAPI extends PluginAPIProxy {
[RpcPeer.PROPERTY_PROXY_ONEWAY_METHODS] = (_api as any)[RpcPeer.PROPERTY_PROXY_ONEWAY_METHODS];

setStorage(nativeId: string, storage: { [key: string]: any; }): Promise<void> {
const id = deviceManager.nativeIds.get(nativeId).id;
for (const r of forks) {
r.setNativeId(nativeId, id, storage);
}
return super.setStorage(nativeId, storage);
}
}

api = new PluginForkableAPI(_api);
peer.selfName = pluginId;
return api;
},
getPluginConsole,
getDeviceConsole,
Expand Down Expand Up @@ -298,7 +313,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
process.send(options, socket);
}

const forks = new Set<PluginRemote>();
const pluginRemoteAPI: PluginRemote = scrypted.pluginRemoteAPI;

scrypted.fork = () => {
const ntw = new NodeThreadWorker(mainFilename, pluginId, {
Expand All @@ -322,7 +337,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe

setStorage(nativeId: string, storage: { [key: string]: any; }): Promise<void> {
const id = deviceManager.nativeIds.get(nativeId).id;
(scrypted.pluginRemoteAPI as PluginRemote).setNativeId(nativeId, id, storage);
pluginRemoteAPI.setNativeId(nativeId, id, storage);
for (const r of forks) {
if (r === remote)
continue;
Expand Down
4 changes: 2 additions & 2 deletions server/src/plugin/plugin-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export interface PluginRemoteAttachOptions {
getPluginConsole?: () => Console;
getMixinConsole?: (id: string, nativeId?: ScryptedNativeId) => Console;
onLoadZip?: (scrypted: ScryptedStatic, params: any, packageJson: any, getZip: () => Promise<Buffer>, zipOptions: PluginRemoteLoadZipOptions) => Promise<any>;
onGetRemote?: (api: PluginAPI, pluginId: string) => Promise<void>;
onGetRemote?: (api: PluginAPI, pluginId: string) => Promise<PluginAPI>;
}

export function attachPluginRemote(peer: RpcPeer, options?: PluginRemoteAttachOptions): Promise<ScryptedStatic> {
Expand Down Expand Up @@ -496,7 +496,7 @@ export function attachPluginRemote(peer: RpcPeer, options?: PluginRemoteAttachOp
}
});

await options?.onGetRemote?.(api, pluginId);
api = await options?.onGetRemote?.(api, pluginId) || api;

const systemManager = new SystemManagerImpl();
const deviceManager = new DeviceManagerImpl(systemManager, getDeviceConsole, getMixinConsole);
Expand Down

0 comments on commit 6892b44

Please sign in to comment.