WIP. A little custom music server
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: refactor flac.ts to use Effect.fn in some places

+66 -56
+66 -56
backend/src/flac.ts
··· 140 140 return { key: key.toUpperCase(), value }; 141 141 } 142 142 143 - function readHeader(file: Uint8Array, offset: number) { 144 - return Effect.gen(function* () { 145 - const header = yield* Schema.decode(FlacHeaderFromUint8Array)({ 146 - uint8Array: file, 147 - offset: offset, 148 - }); 143 + //function readHeader(file: Uint8Array, offset: number) { 144 + //return Effect.gen(function* () { 145 + //const header = yield* Schema.decode(FlacHeaderFromUint8Array)({ 146 + //uint8Array: file, 147 + //offset: offset, 148 + //}); 149 + 150 + //return header; 151 + //}).pipe( 152 + //Effect.withSpan("flac-readHeader"), 153 + //Effect.mapError( 154 + //(e) => 155 + //new FlacError({ 156 + //cause: e, 157 + //message: "Failed reading header", 158 + //}), 159 + //), 160 + //); 161 + //} 149 162 150 - return header; 163 + const readHeader = Effect.fn("flac-readHeader")(function* (file: Uint8Array, offset: number) { 164 + const result = yield* Schema.decode(FlacHeaderFromUint8Array)({ 165 + uint8Array: file, 166 + offset: offset, 151 167 }).pipe( 152 - Effect.withSpan("flac-readHeader"), 153 168 Effect.mapError( 154 169 (e) => 155 170 new FlacError({ ··· 158 173 }), 159 174 ), 160 175 ); 161 - } 162 176 163 - function readVorbisComment(file: Uint8Array, offset: number, length: number) { 164 - return Effect.gen(function* () { 165 - const slice = file.slice(offset, offset + length); 177 + return result; 178 + }); 166 179 167 - const vorbisComment = yield* Schema.decode(MetadataFromUint8Array)({ 168 - uint8Array: slice, 169 - offset, 170 - length, 171 - }); 180 + const readVorbisComment = Effect.fn("readVorbisComment")(function* (file: Uint8Array, offset: number, length: number) { 181 + const slice = file.slice(offset, offset + length); 172 182 173 - return vorbisComment; 183 + const vorbisComment = yield* Schema.decode(MetadataFromUint8Array)({ 184 + uint8Array: slice, 185 + offset, 186 + length, 174 187 }).pipe( 175 188 Effect.mapError( 176 189 (e) => ··· 179 192 message: "Failed to parse Vorbis Comment", 180 193 }), 181 194 ), 195 + ); 182 196 183 - Effect.withSpan("readVorbisComment"), 184 - ); 185 - } 197 + return vorbisComment; 198 + }); 186 199 187 200 // Public API 188 - export function isFlac(path: string) { 189 - return Effect.gen(function* () { 190 - const fs = yield* FileSystem.FileSystem; 191 - const file = yield* fs.readFile(path); 192 - const slice = file.slice(0, 4).toString(); 193 - return slice === "fLaC"; 194 - }).pipe(Effect.withSpan("isFlac")); 195 - } 196 201 197 - export function readMetadata(path: string) { 198 - return Effect.gen(function* () { 199 - const fs = yield* FileSystem.FileSystem; 202 + export const isFlac = Effect.fn("isFlac")(function* (path: string) { 203 + const fs = yield* FileSystem.FileSystem; 204 + const file = yield* fs.readFile(path); 205 + const slice = file.slice(0, 4).toString(); 206 + return slice === "fLaC"; 207 + }); 200 208 201 - const fileIsFlac = yield* isFlac(path); 202 - if (!fileIsFlac) { 203 - return yield* Effect.fail( 204 - new FlacError({ 205 - message: "The file you are trying to parse as FLAC is NOT FLAC", 206 - }), 207 - ); 208 - } 209 + export const readMetadata = Effect.fn("flac-readMetadata")(function* (path: string) { 210 + const fs = yield* FileSystem.FileSystem; 211 + 212 + const fileIsFlac = yield* isFlac(path); 213 + if (!fileIsFlac) { 214 + return yield* Effect.fail( 215 + new FlacError({ 216 + message: "The file you are trying to parse as FLAC is NOT FLAC", 217 + }), 218 + ); 219 + } 209 220 210 - const file = yield* fs.readFile(path); 221 + const file = yield* fs.readFile(path); 211 222 212 - let offset = 4; 213 - let header = yield* readHeader(file, offset); 223 + let offset = 4; 224 + let header = yield* readHeader(file, offset); 214 225 215 - while (!header.isLast && header.streamInfo !== VORBIS_STREAMINFO) { 216 - offset = offset + 4 + header.length; 217 - header = yield* readHeader(file, offset); 218 - } 226 + while (!header.isLast && header.streamInfo !== VORBIS_STREAMINFO) { 227 + offset = offset + 4 + header.length; 228 + header = yield* readHeader(file, offset); 229 + } 219 230 220 - offset += 4; 231 + offset += 4; 221 232 222 - const vorbisComment = yield* readVorbisComment(file, offset, header.length); 233 + const vorbisComment = yield* readVorbisComment(file, offset, header.length); 223 234 224 - const result = MetadataWithFilepathSchema.make({ 225 - ...vorbisComment, 226 - filePath: path, 227 - }); 235 + const result = MetadataWithFilepathSchema.make({ 236 + ...vorbisComment, 237 + filePath: path, 238 + }); 228 239 229 - return result; 230 - }).pipe(Effect.withSpan("flac-readMetadata")); 231 - } 240 + return result; 241 + });