@soga/encoder 1.3.7 → 1.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +1 -1
- package/dist/main.mjs +1 -1
- package/package.json +9 -9
package/dist/main.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.create;var e,t=Object.defineProperty,
|
|
1
|
+
Object.create;var e,t=Object.defineProperty,s=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,r=(Object.getPrototypeOf,Object.prototype.hasOwnProperty),a=(e,a,o,n)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let d of i(a))r.call(e,d)||d===o||t(e,d,{get:()=>a[d],enumerable:!(n=s(a,d))||n.enumerable});return e},o={};((e,s)=>{for(var i in s)t(e,i,{get:s[i],enumerable:!0})})(o,{Encoder:()=>_,getEncoder:()=>m}),module.exports=(e=o,a(t({},"__esModule",{value:!0}),e));var n=require("@soga/sdk"),d=require("typeorm"),p=require("@soga/entities"),c=require("@soga/types"),u=require("piscina"),h=require("@soga/file-encoder"),l=require("@soga/utils"),g=require("@soga/sdk"),f=require("path"),y=require("worker_threads"),w=require("@soga/error"),_=class{piscina;onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;runningRepository;isRunning=!1;thread_count=2;maxThreads=10;threads=[];params;sdk;getValidThreads(e){return Math.min(Math.max(e??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:t}=e;this.fileRepository=t.getRepository(p.UploadFile),this.runningRepository=t.getRepository(p.UploadRunning),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=(0,g.getSdk)(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this));const s=new u.Piscina({filename:require.resolve("@soga/file-encoder")});this.piscina=s}async setThreads(e){const t=this.getValidThreads(e);this.thread_count=t,await this.start()}async init(){const e=await this.getUid();await this.fileRepository.update({uid:e,upload_status:c.UploadStatus.ENCODE_PROCESS},{upload_status:c.UploadStatus.NULL})}async start(){for(;this.isRunning;)await new Promise(e=>{setTimeout(e,100)});try{this.isRunning=!0;const e=await this.getUid(),t=this.threads.length,s=this.thread_count;if(t<s){const i=s-t;for(let t=0;t<i;t++){const t=await this.fileRepository.findOne({where:{uid:e,upload_status:c.UploadStatus.NULL,type:(0,d.Not)(c.RecordType.FOLDER),is_paused:!1},order:{created_at:"ASC"}});if(!t)break;const s=this.getThread(t);this.threads.push(s),await this.fileRepository.update(t.id,{upload_status:c.UploadStatus.ENCODE_PROCESS}),s.start().catch(e=>{})}}else if(t>s){const e=t-s,i=this.threads.slice(0,e);for(const e of i)await e.stop()}}finally{this.isRunning=!1}}async stopFiles(e){const t=this.threads.filter(t=>e.includes(t.id));await Promise.all(t.map(e=>e.stop())).catch(e=>{})}async stopAll(){await Promise.all(this.threads.map(e=>e.stop()))}getThread(e){const t=new AbortController,{port1:s,port2:i}=new y.MessageChannel;return{id:e.id,uid:e.uid,start:async()=>{i.on("message",async e=>{if("percent"==e.type){const{id:t,data:s}=e,i=await this.fileRepository.findOneBy({id:t});if(!i)return;const{percent:r,step:a}=s;i.progress[a].percent=r;const o=(0,l.getProgressPercent)(i.progress);await this.fileRepository.update(i.id,{progress:i.progress,percent:o}),await this.onProgress(i)}});try{const i=await this.fileRepository.findOneBy({id:e.id});if(!i)return;if(i.upload_status!=c.UploadStatus.ENCODE_PROCESS)return;await this.runningRepository.save(this.runningRepository.create({type:p.UploadRunningType.ENCODE,uid:e.uid,root_id:e.root_id,file_id:e.id}));const{id:r,uid:a,config:o,input:n,type:d,ftype:u,external_texts:l,is_attachment:g,is_shop_space:y}=i,w=[];i.ali_host_id&&w.push(c.HostType.ALI),i.baidu_host_id&&w.push(c.HostType.BAIDU);const{encode_root:_}=await this.sdk.getSystemConfig(),R=(0,f.resolve)(_,i.is_attachment?"attachment":"file",i.id.toString());await this.fileRepository.update(e.id,{output_root:R});let m=!0,E=!0;g||(m=o.file_keeps==c.RecordFileKeep.PREVIEW||o.file_keeps==c.RecordFileKeep.BOTH,E=o.file_keeps==c.RecordFileKeep.SOURCE||o.file_keeps==c.RecordFileKeep.BOTH);const O={id:r,uid:a,is_shop_space:y,keepPreview:m,keepSource:E,input:n,type:d,ftype:u,hosts:w,outputRoot:R,subtitles:l,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug??!1,port:s};if(!Object.keys(i.progress??{}).length){const t=await(0,h.getPrepare)(O);await this.fileRepository.update(e.id,{progress:t.prepareResult})}const S=await this.piscina.run(O,{name:"encode",signal:t.signal,transferList:[s]});if(!S)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{upload_status:c.UploadStatus.ENCODE_SUCCESS,encoded:S}),await this.onSuccess(await this.fileRepository.findOneBy({id:r}))}catch(t){if("AbortError"==t.name)await this.fileRepository.update(e.id,{upload_status:c.UploadStatus.NULL});else{const s=(0,w.buildDError)(t,{message:"Error occurred during encoding",detail:`Error occurred during encoding: ${e.input.filepath}`});try{await this.fileRepository.update(e.id,{upload_status:c.UploadStatus.ENCODE_ERROR,error:{type:"encode",message:s.message,stack:s.stack,details:s.details,cause:s.cause}}),await this.onError(await this.fileRepository.findOneBy({id:e.id}),t)}catch(e){throw e}}}finally{this.threads=this.threads.filter(t=>t.id!==e.id),i.close(),await this.runningRepository.delete({type:p.UploadRunningType.ENCODE,file_id:e.id}),await this.start()}},stop:async()=>{i.close(),t.abort(),this.threads=this.threads.filter(t=>t.id==e.id)}}}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}},R=new Map,m=async e=>{const t=(0,n.getSdk)(e.sdk_domain),s=(await t.getUserInfo({refresh:!1})).id;for(;R.get(s)?.initing;)await new Promise(e=>setTimeout(e,100));if(R.get(s)?.encoder)return R.get(s).encoder;const i={encoder:new _(e),initing:!0};return R.set(s,i),await i.encoder.init(),i.initing=!1,R.set(s,i),i.encoder};
|
package/dist/main.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t=(t=>"undefined"!=typeof require?require:"undefined"!=typeof Proxy?new Proxy(t,{get:(t,e)=>("undefined"!=typeof require?require:t)[e]}):t)(function(t){if("undefined"!=typeof require)return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});import{getSdk as e}from"@soga/sdk";import{Not as i}from"typeorm";import{UploadFile as s,UploadRunning as r,UploadRunningType as o}from"@soga/entities";import{UploadStatus as a,HostType as n,RecordFileKeep as d,RecordType as p}from"@soga/types";import{Piscina as h}from"piscina";import{getPrepare as c}from"@soga/file-encoder";import{getProgressPercent as u}from"@soga/utils";import{getSdk as f}from"@soga/sdk";import{resolve as l}from"path";import{MessageChannel as g}from"worker_threads";import{buildDError as y}from"@soga/error";var m=class{piscina;onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;runningRepository;isRunning=!1;thread_count=2;maxThreads=10;threads=[];params;sdk;getValidThreads(t){return Math.min(Math.max(t??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:i}=e;this.fileRepository=i.getRepository(s),this.runningRepository=i.getRepository(r),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=f(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this));const o=new h({filename:t.resolve("@soga/file-encoder")});this.piscina=o}async setThreads(t){const e=this.getValidThreads(t);this.thread_count=e,await this.start()}async init(){const t=await this.getUid();await this.fileRepository.update({uid:t,upload_status:a.ENCODE_PROCESS},{upload_status:a.NULL})}async start(){for(;this.isRunning;)await new Promise(t=>{setTimeout(t,100)});try{this.isRunning=!0;const t=await this.getUid(),e=this.threads.length,s=this.thread_count;if(e<s){const r=s-e;for(let e=0;e<r;e++){const e=await this.fileRepository.findOne({where:{uid:t,upload_status:a.NULL,type:i(p.FOLDER),is_paused:!1},order:{created_at:"ASC"}});if(!e)break;const s=this.getThread(e);this.threads.push(s),await this.fileRepository.update(e.id,{upload_status:a.ENCODE_PROCESS}),s.start().catch(t=>{})}}else if(e>s){const t=e-s,i=this.threads.slice(0,t);for(const t of i)await t.stop()}}finally{this.isRunning=!1}}async stopFiles(t){const e=this.threads.filter(e=>t.includes(e.id));await Promise.all(e.map(t=>t.stop())).catch(t=>{})}async stopAll(){await Promise.all(this.threads.map(t=>t.stop()))}getThread(t){const e=new AbortController,{port1:i,port2:s}=new g;return{id:t.id,uid:t.uid,start:async()=>{s.on("message",async t=>{if("percent"==t.type){const{id:e,data:i}=t,s=await this.fileRepository.findOneBy({id:e});if(!s)return;const{percent:r,step:o}=i;s.progress[o].percent=r;const a=u(s.progress);await this.fileRepository.update(s.id,{progress:s.progress,percent:a}),await this.onProgress(s)}});try{const s=await this.fileRepository.findOneBy({id:t.id});if(!s)return;if(s.upload_status!=a.ENCODE_PROCESS)return;await this.runningRepository.save(this.runningRepository.create({type:o.ENCODE,uid:t.uid,root_id:t.root_id,file_id:t.id}));const{id:r,uid:p,config:h,input:u,type:f,ftype:g,external_texts:y,is_attachment:m}=s,
|
|
1
|
+
var t=(t=>"undefined"!=typeof require?require:"undefined"!=typeof Proxy?new Proxy(t,{get:(t,e)=>("undefined"!=typeof require?require:t)[e]}):t)(function(t){if("undefined"!=typeof require)return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});import{getSdk as e}from"@soga/sdk";import{Not as i}from"typeorm";import{UploadFile as s,UploadRunning as r,UploadRunningType as o}from"@soga/entities";import{UploadStatus as a,HostType as n,RecordFileKeep as d,RecordType as p}from"@soga/types";import{Piscina as h}from"piscina";import{getPrepare as c}from"@soga/file-encoder";import{getProgressPercent as u}from"@soga/utils";import{getSdk as f}from"@soga/sdk";import{resolve as l}from"path";import{MessageChannel as g}from"worker_threads";import{buildDError as y}from"@soga/error";var m=class{piscina;onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;runningRepository;isRunning=!1;thread_count=2;maxThreads=10;threads=[];params;sdk;getValidThreads(t){return Math.min(Math.max(t??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:i}=e;this.fileRepository=i.getRepository(s),this.runningRepository=i.getRepository(r),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=f(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this));const o=new h({filename:t.resolve("@soga/file-encoder")});this.piscina=o}async setThreads(t){const e=this.getValidThreads(t);this.thread_count=e,await this.start()}async init(){const t=await this.getUid();await this.fileRepository.update({uid:t,upload_status:a.ENCODE_PROCESS},{upload_status:a.NULL})}async start(){for(;this.isRunning;)await new Promise(t=>{setTimeout(t,100)});try{this.isRunning=!0;const t=await this.getUid(),e=this.threads.length,s=this.thread_count;if(e<s){const r=s-e;for(let e=0;e<r;e++){const e=await this.fileRepository.findOne({where:{uid:t,upload_status:a.NULL,type:i(p.FOLDER),is_paused:!1},order:{created_at:"ASC"}});if(!e)break;const s=this.getThread(e);this.threads.push(s),await this.fileRepository.update(e.id,{upload_status:a.ENCODE_PROCESS}),s.start().catch(t=>{})}}else if(e>s){const t=e-s,i=this.threads.slice(0,t);for(const t of i)await t.stop()}}finally{this.isRunning=!1}}async stopFiles(t){const e=this.threads.filter(e=>t.includes(e.id));await Promise.all(e.map(t=>t.stop())).catch(t=>{})}async stopAll(){await Promise.all(this.threads.map(t=>t.stop()))}getThread(t){const e=new AbortController,{port1:i,port2:s}=new g;return{id:t.id,uid:t.uid,start:async()=>{s.on("message",async t=>{if("percent"==t.type){const{id:e,data:i}=t,s=await this.fileRepository.findOneBy({id:e});if(!s)return;const{percent:r,step:o}=i;s.progress[o].percent=r;const a=u(s.progress);await this.fileRepository.update(s.id,{progress:s.progress,percent:a}),await this.onProgress(s)}});try{const s=await this.fileRepository.findOneBy({id:t.id});if(!s)return;if(s.upload_status!=a.ENCODE_PROCESS)return;await this.runningRepository.save(this.runningRepository.create({type:o.ENCODE,uid:t.uid,root_id:t.root_id,file_id:t.id}));const{id:r,uid:p,config:h,input:u,type:f,ftype:g,external_texts:y,is_attachment:m,is_shop_space:w}=s,_=[];s.ali_host_id&&_.push(n.ALI),s.baidu_host_id&&_.push(n.BAIDU);const{encode_root:R}=await this.sdk.getSystemConfig(),E=l(R,s.is_attachment?"attachment":"file",s.id.toString());await this.fileRepository.update(t.id,{output_root:E});let k=!0,O=!0;m||(k=h.file_keeps==d.PREVIEW||h.file_keeps==d.BOTH,O=h.file_keeps==d.SOURCE||h.file_keeps==d.BOTH);const S={id:r,uid:p,is_shop_space:w,keepPreview:k,keepSource:O,input:u,type:f,ftype:g,hosts:_,outputRoot:E,subtitles:y,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug??!1,port:i};if(!Object.keys(s.progress??{}).length){const e=await c(S);await this.fileRepository.update(t.id,{progress:e.prepareResult})}const P=await this.piscina.run(S,{name:"encode",signal:e.signal,transferList:[i]});if(!P)throw new Error("upload chunk failed!");await this.fileRepository.update(t.id,{upload_status:a.ENCODE_SUCCESS,encoded:P}),await this.onSuccess(await this.fileRepository.findOneBy({id:r}))}catch(e){if("AbortError"==e.name)await this.fileRepository.update(t.id,{upload_status:a.NULL});else{const i=y(e,{message:"Error occurred during encoding",detail:`Error occurred during encoding: ${t.input.filepath}`});try{await this.fileRepository.update(t.id,{upload_status:a.ENCODE_ERROR,error:{type:"encode",message:i.message,stack:i.stack,details:i.details,cause:i.cause}}),await this.onError(await this.fileRepository.findOneBy({id:t.id}),e)}catch(t){throw t}}}finally{this.threads=this.threads.filter(e=>e.id!==t.id),s.close(),await this.runningRepository.delete({type:o.ENCODE,file_id:t.id}),await this.start()}},stop:async()=>{s.close(),e.abort(),this.threads=this.threads.filter(e=>e.id==t.id)}}}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}},w=new Map,_=async t=>{const i=e(t.sdk_domain),s=(await i.getUserInfo({refresh:!1})).id;for(;w.get(s)?.initing;)await new Promise(t=>setTimeout(t,100));if(w.get(s)?.encoder)return w.get(s).encoder;const r={encoder:new m(t),initing:!0};return w.set(s,r),await r.encoder.init(),r.initing=!1,w.set(s,r),r.encoder};export{m as Encoder,_ as getEncoder};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soga/encoder",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.12",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -17,13 +17,13 @@
|
|
|
17
17
|
"fs-extra": "^11.3.2",
|
|
18
18
|
"piscina": "^5.0.0",
|
|
19
19
|
"typeorm": "^0.3.24",
|
|
20
|
-
"@soga/entities": "1.3.
|
|
21
|
-
"@soga/sdk": "1.3.
|
|
22
|
-
"@soga/file-encoder": "1.3.
|
|
23
|
-
"@soga/
|
|
24
|
-
"@soga/
|
|
25
|
-
"@soga/types": "1.3.
|
|
26
|
-
"@soga/
|
|
20
|
+
"@soga/entities": "1.3.12",
|
|
21
|
+
"@soga/sdk": "1.3.12",
|
|
22
|
+
"@soga/file-encoder": "1.3.12",
|
|
23
|
+
"@soga/error": "1.3.12",
|
|
24
|
+
"@soga/utils": "1.3.12",
|
|
25
|
+
"@soga/node-types": "1.3.12",
|
|
26
|
+
"@soga/types": "1.3.12"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^24.5.2",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"terser": "^5.43.1",
|
|
32
32
|
"tsup": "^8.5.0",
|
|
33
33
|
"typescript": "^5.8.3",
|
|
34
|
-
"@soga/typescript-config": "1.3.
|
|
34
|
+
"@soga/typescript-config": "1.3.12"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "rimraf dist && tsup src/main.ts --format cjs,esm --dts --minify terser"
|