@streamyx/api
Version:
Build extensions for Streamyx with Node.js
2 lines • 4.42 kB
JavaScript
;var e=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),t=e((exports,t)=>{let{readFileSync:n,writeFileSync:r}=require(`node:fs`),{join:i}=require(`node:path`),a=(e,t)=>{let a=n(e,`utf8`),o=t||i(process.cwd(),`package.json`),s=require(o),c=/module\.exports\s*=\s*(\w+)/g,l,u;for(;(l=c.exec(a))!==null;)u=l;if(!u)throw Error(`No module.exports found in source file`);let d=u[1],f=a.replace(/(module\.exports\s*=\s*(\w+))(?![^]*module\.exports\s*=\s*\w+)/,`${d}.package=${JSON.stringify(s)};module.exports = $2`);return r(e,f,`utf8`),{code:f,packageJson:s}};t.exports={injectPackageJson:a}}),n=e((exports,t)=>{let{createCipheriv:n,createDecipheriv:r,publicEncrypt:i,privateDecrypt:a,randomBytes:o}=require(`node:crypto`),{readFileSync:s,writeFileSync:c}=require(`node:fs`),l=Buffer.from(`STX`,`utf8`),u=(e,t,r)=>{let a=o(32),u=o(16),d=s(t),f=n(`aes-256-cbc`,a,u),p=Buffer.concat([f.update(d),f.final()]),m=i(e,a),h=Buffer.concat([l,u,m,p]);return r&&c(r,h),{aesIv:u,encryptedAesKey:m,encryptedCode:p}},d=(e,t,n)=>{let i=s(t),o=l.byteLength,u=i.subarray(0,o);if(!u.equals(l))throw Error(`Incorrect file format`);let d=o,f=d+16,p=f+512,m=i.subarray(d,f),h=i.subarray(f,p),g=i.subarray(p),_=a(e,h),v=r(`aes-256-cbc`,_,m),y=Buffer.concat([v.update(g),v.final()]),b=y.toString(`utf8`);return n&&c(n,b),b};t.exports={encrypt:u,decrypt:d}}),r=e((exports,t)=>{let{readFileSync:n,writeFileSync:r}=require(`node:fs`),i=require(`javascript-obfuscator`),a=(e,t)=>{let a=n(e,`utf8`);console.log(`Obfuscating...`),console.time(`Obfuscation complete in`);let o=i.obfuscate(a,{target:`node`,compact:!0,controlFlowFlattening:!0,controlFlowFlatteningThreshold:1,deadCodeInjection:!0,debugProtection:!0,identifierNamesGenerator:`hexadecimal`,numbersToExpressions:!0,selfDefending:!0,simplify:!0,splitStrings:!0,splitStringsChunkLength:5,stringArray:!0,stringArrayCallsTransform:!0,stringArrayCallsTransformThreshold:1,stringArrayEncoding:[`base64`],stringArrayIndexShift:!0,stringArrayRotate:!0,stringArrayShuffle:!0,stringArrayWrappersCount:2,stringArrayWrappersChainedCalls:!0,stringArrayWrappersType:`function`,stringArrayThreshold:1,transformObjectKeys:!0,unicodeEscapeSequence:!1});console.timeEnd(`Obfuscation complete in`);let s=o.getObfuscatedCode();return r(t||e,s),s};t.exports={obfuscate:a}});const{parseArgs:i}=require(`node:util`),{rmSync:a,readdirSync:o,readFileSync:s}=require(`node:fs`),c=require(`node:path`),{build:l}=require(`tsdown`),{injectPackageJson:u}=t(),{encrypt:d,decrypt:f}=n(),{obfuscate:p}=r(),m=i({allowPositionals:!0,allowNegative:!0,options:{input:{type:`string`,short:`i`,description:`Path to the input file`},key:{type:`string`,short:`k`,description:`Path to the key file (public for encrypt, private for decrypt)`},output:{type:`string`,short:`o`,description:`Path to the output file`},watch:{type:`boolean`,description:`Watch file changes`},minify:{type:`boolean`,description:`Minify output`},obfuscate:{type:`boolean`,description:`Obfuscate output`},encrypt:{type:`boolean`,description:`Encrypt extension source code (.js will be packed inside .stx)`},decrypt:{type:`boolean`,description:`Decrypt encrypted extension`}}}),h=()=>{let e=[process.cwd(),c.join(process.cwd(),`src`),c.join(process.cwd(),`lib`)];for(let t of e){let e=o(t),n=e.filter(e=>!e.endsWith(`.d.ts`)).find(e=>new Set([`.ts`,`.js`]).has(c.extname(e)));if(n)return c.join(t,n)}};(async()=>{let[e,...t]=m.positionals,n=m.values.input||t[0]||h();if(e===`build`||e===`pack`){let t=typeof m.values.minify==`boolean`?m.values.minify:!0;await l({entry:n,format:`cjs`,platform:`node`,target:`node20`,watch:m.values.watch,minify:t}),m.values.input=c.join(process.cwd(),`dist`,c.basename(n).replace(/\.ts$/,`.js`));let{packageJson:r}=u(m.values.input),i=typeof m.values.obfuscate==`boolean`,o=i?m.values.obfuscate:r.private;o&&p(m.values.input,m.values.input);let f=m.values.encrypt||e===`pack`;if(f){let e=s(m.values.key||c.join(__dirname,`../public.pem`)),t=m.values.output||m.values.input.replace(/\.js$/,`-${r.version}.stx`);d(e,m.values.input,t),a(m.values.input)}}else if(e===`decrypt`){let e=s(m.values.key||c.join(__dirname,`../private.pem`)),t=m.values.output||n.replace(/\.stx$/,`.js`);f(e,n,t),a(m.values.input)}else if(e===`dev`||e===`develop`||!e){let e=typeof m.values.minify==`boolean`?m.values.minify:!1;await l({entry:n,format:`cjs`,platform:`node`,target:`node20`,watch:!0,minify:e})}})();