z-astro-web-audio-stream
Version:
Astro integration for Web Audio Stream with separated download/storage optimization and automatic worklet deployment
73 lines • 3.5 kB
JavaScript
// Astro integration for Web Audio Stream
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { resolve, join } from 'path';
/**
* Astro integration for Web Audio Stream
*
* Automatically deploys the iOS Safari-safe audio worklet processor
* to your public directory during build.
*/
export default function webAudioStream(options = {}) {
const { workletPath = '/audio-worklet-processor.js', publicDir = 'public', autoDeploy = true, verbose = true } = options;
return {
name: '@web-audio-stream/astro',
hooks: {
'astro:config:setup': ({ config, logger }) => {
if (verbose) {
logger.info('🎵 Web Audio Stream integration loaded');
logger.info('🍎 iOS Safari audio fixes will be applied automatically');
}
// Ensure the worklet path is correctly configured
if (!workletPath.startsWith('/')) {
throw new Error('@web-audio-stream/astro: workletPath must start with "/"');
}
},
'astro:config:done': ({ config, logger }) => {
if (!autoDeploy) {
if (verbose) {
logger.info('Auto-deploy disabled. Run `npx web-audio-stream-cli deploy` manually.');
}
return;
}
try {
// Find the worklet file from z-web-audio-stream package
let workletSource;
// Try to find it in node_modules
const nodeModulesPath = resolve(config.root.pathname, 'node_modules/z-web-audio-stream/dist/audio-worklet-processor.js');
if (existsSync(nodeModulesPath)) {
workletSource = readFileSync(nodeModulesPath, 'utf8');
}
else {
throw new Error('Could not find z-web-audio-stream package. Please install it: npm install z-web-audio-stream');
}
// Determine output path
const publicPath = resolve(config.root.pathname, publicDir);
const outputPath = join(publicPath, workletPath.replace(/^\//, ''));
// Ensure public directory exists
if (!existsSync(publicPath)) {
mkdirSync(publicPath, { recursive: true });
if (verbose) {
logger.info(`Created public directory: ${publicPath}`);
}
}
// Write worklet file
writeFileSync(outputPath, workletSource);
if (verbose) {
logger.info(`✅ Audio worklet deployed to: ${outputPath}`);
logger.info(`🔧 Use in your app: setupWebAudio({ workletPath: "${workletPath}" })`);
}
}
catch (error) {
logger.error(`❌ Failed to deploy audio worklet: ${error instanceof Error ? error.message : String(error)}`);
throw error;
}
},
'astro:build:start': ({ logger }) => {
if (verbose) {
logger.info('🍎 Building with iOS Safari audio optimizations');
}
}
}
};
}
//# sourceMappingURL=index.js.map