A music player that connects to your cloud/distributed storage. diffuse.sh
Something went wrong. Try again.
2.4 kB · 100 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import * as URI from "fast-uri";import { ostiary, rpc, workerProxy } from "~/common/worker.js";
/** * @import {UploadActions} from "@specs/components/upload/types.d.ts" * @import {ActionsWithTunnel, ProxiedActions} from "~/common/worker.d.ts" * @import {Actions} from "@specs/components/configurator/upload/types.d.ts" */
////////////////////////////////////////////// ACTIONS////////////////////////////////////////////
/** * @type {ActionsWithTunnel<Actions>['consult']} */export async function consult({ data, ports }) { const fileUriOrScheme = data; const scheme = fileUriOrScheme.includes(":") ? URI.parse(fileUriOrScheme).scheme || fileUriOrScheme : fileUriOrScheme;
const upload = grabUploader(scheme, ports); if (!upload) { return { supported: false, reason: "Unsupported scheme" }; }
return await upload.consult(fileUriOrScheme);}
/** * @type {ActionsWithTunnel<Actions>['createSource']} */export async function createSource({ data, ports }) { const upload = grabUploader(data.scheme, ports); if (!upload) { throw new Error(`Unsupported scheme: ${data.scheme}`); }
return await upload.createSource(data);}
/** * @type {ActionsWithTunnel<Actions>['delete']} */export async function deleteFn({ data, ports }) { const uri = data; const scheme = uri.split(":", 1)[0]; const upload = grabUploader(scheme, ports); if (!upload) { throw new Error(`Unsupported scheme: ${scheme}`); }
await upload.delete(uri);}
/** * @type {ActionsWithTunnel<Actions>['upload']} */export async function upload({ data, ports }) { const scheme = data.uri.split(":", 1)[0]; const upload = grabUploader(scheme, ports); if (!upload) { throw new Error(`Unsupported scheme: ${scheme}`); }
return await upload.upload(data);}
////////////////////////////////////////////// ⚡️////////////////////////////////////////////
ostiary((context) => { rpc(context, { consult, upload, delete: deleteFn, createSource, });});
////////////////////////////////////////////// 🛠️////////////////////////////////////////////
/** * @param {string} scheme * @param {Record<string, MessagePort>} ports * @returns {ProxiedActions<UploadActions> | null} */function grabUploader(scheme, ports) { const port = ports[scheme]; if (!port) return null;
return workerProxy(() => { port.start(); return port; });}