A music player that connects to your cloud/distributed storage.
at v4 56 lines 1.3 kB view raw
1import { createClient } from "~/components/input/s3/common.js"; 2import { ostiary, rpc } from "~/common/worker.js"; 3 4import { OBJECT_PREFIX } from "./constants.js"; 5 6/** 7 * @import {S3OutputWorkerActions} from "./types.d.ts" 8 */ 9 10//////////////////////////////////////////// 11// ACTIONS 12//////////////////////////////////////////// 13 14/** 15 * @type {S3OutputWorkerActions["get"]} 16 */ 17export async function get({ bucket, name }) { 18 const client = createClient(bucket); 19 const path = bucket.path.replace(/(^\/+|\/+$)/g, ""); 20 const key = path 21 ? `${path}/${OBJECT_PREFIX}${name}` 22 : `${OBJECT_PREFIX}${name}`; 23 24 try { 25 const response = await client.getObject(key); 26 const buffer = await response.arrayBuffer(); 27 return new Uint8Array(buffer); 28 } catch (err) { 29 // Object doesn't exist yet, return undefined 30 return undefined; 31 } 32} 33 34/** 35 * @type {S3OutputWorkerActions["put"]} 36 */ 37export async function put({ bucket, data, name }) { 38 const client = createClient(bucket); 39 const path = bucket.path.replace(/(^\/+|\/+$)/g, ""); 40 const key = path 41 ? `${path}/${OBJECT_PREFIX}${name}` 42 : `${OBJECT_PREFIX}${name}`; 43 44 await client.putObject(key, data); 45} 46 47//////////////////////////////////////////// 48// ⚡️ 49//////////////////////////////////////////// 50 51ostiary((context) => { 52 rpc(context, { 53 get, 54 put, 55 }); 56});