| 850 | } |
| 851 | |
| 852 | export const sunoApi = async (cookie?: string) => { |
| 853 | const resolvedCookie = cookie && cookie.includes('__client') ? cookie : process.env.SUNO_COOKIE; // Check for bad `Cookie` header (It's too expensive to actually parse the cookies *here*) |
| 854 | if (!resolvedCookie) { |
| 855 | logger.info('No cookie provided! Aborting...\nPlease provide a cookie either in the .env file or in the Cookie header of your request.') |
| 856 | throw new Error('Please provide a cookie either in the .env file or in the Cookie header of your request.'); |
| 857 | } |
| 858 | |
| 859 | // Check if the instance for this cookie already exists in the cache |
| 860 | const cachedInstance = cache.get(resolvedCookie); |
| 861 | if (cachedInstance) |
| 862 | return cachedInstance; |
| 863 | |
| 864 | // If not, create a new instance and initialize it |
| 865 | const instance = await new SunoApi(resolvedCookie).init(); |
| 866 | // Cache the initialized instance |
| 867 | cache.set(resolvedCookie, instance); |
| 868 | |
| 869 | return instance; |
| 870 | }; |