( store: BlobStore, partitions: OwnerPartitions, pluginId: string, )
| 76 | * executor to build the `blobs` field handed to each plugin's `storage` factory. |
| 77 | */ |
| 78 | export const pluginBlobStore = ( |
| 79 | store: BlobStore, |
| 80 | partitions: OwnerPartitions, |
| 81 | pluginId: string, |
| 82 | ): PluginBlobStore => { |
| 83 | const readNamespaces = (): readonly string[] => |
| 84 | (partitions.user == null ? [partitions.org] : [partitions.user, partitions.org]).map((p) => |
| 85 | nsFor(p, pluginId), |
| 86 | ); |
| 87 | |
| 88 | const partitionFor = (owner: Owner): Effect.Effect<string, StorageError> => { |
| 89 | if (owner === "org") return Effect.succeed(partitions.org); |
| 90 | if (partitions.user == null) { |
| 91 | return Effect.fail( |
| 92 | new StorageError({ |
| 93 | message: 'Blob write targets owner "user" but the executor has no subject.', |
| 94 | cause: undefined, |
| 95 | }), |
| 96 | ); |
| 97 | } |
| 98 | return Effect.succeed(partitions.user); |
| 99 | }; |
| 100 | |
| 101 | return { |
| 102 | get: (key) => |
| 103 | Effect.gen(function* () { |
| 104 | const namespaces = readNamespaces(); |
| 105 | const hits = yield* store.getMany(namespaces, key); |
| 106 | if (hits.size === 0) return null; |
| 107 | for (const ns of namespaces) { |
| 108 | const v = hits.get(ns); |
| 109 | if (v !== undefined) return v; |
| 110 | } |
| 111 | return null; |
| 112 | }), |
| 113 | put: (key, value, options) => |
| 114 | Effect.flatMap(partitionFor(options.owner), (partition) => |
| 115 | store.put(nsFor(partition, pluginId), key, value), |
| 116 | ), |
| 117 | delete: (key, options) => |
| 118 | Effect.flatMap(partitionFor(options.owner), (partition) => |
| 119 | store.delete(nsFor(partition, pluginId), key), |
| 120 | ), |
| 121 | has: (key) => store.getMany(readNamespaces(), key).pipe(Effect.map((hits) => hits.size > 0)), |
| 122 | }; |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * Minimal in-memory BlobStore — good for tests and trivial hosts. Real |
no test coverage detected