@soga/encoder 0.1.14 → 0.2.1

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/encoder.d.ts CHANGED
@@ -11,6 +11,7 @@ export declare class Encoder {
11
11
  private maxThreads;
12
12
  private threads;
13
13
  params: EncoderParams;
14
+ private sdk;
14
15
  protected getValidThreads(threads: number): number;
15
16
  constructor(params: EncoderParams);
16
17
  setThreads(threads: number): Promise<void>;
@@ -20,4 +21,5 @@ export declare class Encoder {
20
21
  stopAll(change_db?: boolean): Promise<void>;
21
22
  private getThread;
22
23
  private run;
24
+ private getUid;
23
25
  }
package/dist/encoder.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Encoder=void 0;const typeorm_1=require("typeorm"),entities_1=require("@soga/entities"),types_1=require("@soga/types"),piscina_1=require("piscina"),single_encoder_1=require("@soga/single-encoder"),utils_1=require("@soga/utils"),piscina=new piscina_1.Piscina({filename:require.resolve("@soga/single-encoder")});class Encoder{onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;isRunning=!1;thread_count=3;maxThreads=10;threads=[];params;getValidThreads(e){return Math.min(Math.max(e??0,0),this.maxThreads)}constructor(e){this.params=e,this.fileRepository=e.dataSource.getRepository(entities_1.UploadFile),this.thread_count=this.getValidThreads(e.threads??this.thread_count),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this)),piscina.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:a,step:r}=s;i.progress[r].percent=a;const o=(0,utils_1.getProgressPercent)(i.progress);await this.fileRepository.update(i.id,{progress:i.progress,percent:o}),this.onProgress(i)}})),piscina.on("error",(async e=>{await(this.params.onError?.(null,new utils_1.SimpleError({code:12200,message:e.message,stack:e.stack})))}))}async setThreads(e){const t=this.getValidThreads(e);this.thread_count=t,await this.run()}async init(){await this.fileRepository.update({uid:this.params.uid,encode_status:types_1.EncodeStatus.PROCESS},{encode_status:types_1.EncodeStatus.NULL})}async start(){await this.run()}async stopFiles(e,t=!1){t&&await this.fileRepository.update({id:(0,typeorm_1.In)(e),is_paused:!1},{is_paused:!0});const s=this.threads.filter((t=>e.includes(t.id)));await Promise.all(s.map((e=>e.stop()))),await this.run()}async stopAll(e=!1){e&&await this.fileRepository.update({uid:this.params.uid},{is_paused:!0}),await Promise.all(this.threads.map((e=>e.stop())))}getThread(e){const t=new AbortController;return{id:e.id,uid:e.uid,start:async()=>{try{const s=await this.fileRepository.findOneBy({id:e.id});if(s.encode_status!==types_1.EncodeStatus.PROCESS)return;if(!s)return;const{id:i,uid:a,config:r,path:o,type:n,external_texts:d}=s,p=[];s.ali_host_id&&p.push(types_1.HostType.ALI),s.baidu_host_id&&p.push(types_1.HostType.BAIDU);const c=await this.params.getOutputRoot(s);await this.fileRepository.update(e.id,{output_root:c});const u={id:i,uid:a,keepPreview:r.file_keeps==types_1.RecordFileKeep.PREVIEW||r.file_keeps==types_1.RecordFileKeep.BOTH,keepSource:r.file_keeps==types_1.RecordFileKeep.SOURCE||r.file_keeps==types_1.RecordFileKeep.BOTH,filePath:o,type:n,hosts:p,outputRoot:c,texts:d,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug},h=await(0,single_encoder_1.prepare)(u);Object.keys(s.progress??{}).length||await this.fileRepository.update(e.id,{progress:h.data});const l=await piscina.run(u,{name:"encode",signal:t.signal});if(!l)throw new Error("upload chunk failed!");const{img:y,media:_,source:f,txt:g}=l;await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,img_data:y,media_data:_,source_data:f,txt_data:g}),await this.onSuccess(await this.fileRepository.findOneBy({id:i}))}catch(t){"AbortError"==t.name?await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.NULL}):"SimpleError"==t.name?await this.onError(await this.fileRepository.findOneBy({id:e.id}),t):await this.onError(await this.fileRepository.findOneBy({id:e.id}),new utils_1.SimpleError({code:12100,message:t.message,stack:t.stack}))}finally{this.threads=this.threads.filter((t=>t.id!==e.id)),await this.run()}},stop:async()=>{t.abort()}}}async run(){for(;this.isRunning;)await new Promise((e=>{setTimeout(e,100)}));this.isRunning=!0;const e=this.threads.length,t=this.thread_count;if(e<t){const s=t-e;for(let e=0;e<s;e++){const e=await this.fileRepository.findOne({where:{uid:this.params.uid,encode_status:types_1.EncodeStatus.NULL,is_folder:!1,is_paused:!1},order:{created_at:"ASC"}});if(!e)break;const t=this.getThread(e);this.threads.push(t),await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.PROCESS}),t.start()}}else if(e>t){const s=e-t,i=this.threads.slice(0,s);for(const e of i)await e.stop()}this.isRunning=!1}}exports.Encoder=Encoder;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Encoder=void 0;const typeorm_1=require("typeorm"),entities_1=require("@soga/entities"),types_1=require("@soga/types"),piscina_1=require("piscina"),single_encoder_1=require("@soga/single-encoder"),utils_1=require("@soga/utils"),sdk_1=require("@soga/sdk"),path_1=require("path"),piscina=new piscina_1.Piscina({filename:require.resolve("@soga/single-encoder")});class Encoder{onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;isRunning=!1;thread_count=3;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(entities_1.UploadFile),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=(0,sdk_1.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)),piscina.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:a,step:r}=s;i.progress[r].percent=a;const o=(0,utils_1.getProgressPercent)(i.progress);await this.fileRepository.update(i.id,{progress:i.progress,percent:o}),this.onProgress(i)}})),piscina.on("error",(async e=>{await(this.params.onError?.(null,new utils_1.SimpleError({code:12200,message:e.message,stack:e.stack})))}))}async setThreads(e){const t=this.getValidThreads(e);this.thread_count=t,await this.run()}async init(){const e=await this.getUid();await this.fileRepository.update({uid:e,encode_status:types_1.EncodeStatus.PROCESS},{encode_status:types_1.EncodeStatus.NULL})}async start(){await this.run()}async stopFiles(e,t=!1){t&&await this.fileRepository.update({id:(0,typeorm_1.In)(e)},{is_paused:!0});const s=this.threads.filter((t=>e.includes(t.id)));await Promise.all(s.map((e=>e.stop()))),await this.run()}async stopAll(e=!1){const t=await this.getUid();e&&await this.fileRepository.update({uid:t},{is_paused:!0}),await Promise.all(this.threads.map((e=>e.stop())))}getThread(e){const t=new AbortController;return{id:e.id,uid:e.uid,start:async()=>{try{const s=await this.fileRepository.findOneBy({id:e.id});if(s.encode_status!=types_1.EncodeStatus.PROCESS)return;if(!s)return;const{id:i,uid:a,config:r,inputs:o,type:n,external_texts:d}=s,p=[];s.ali_host_id&&p.push(types_1.HostType.ALI),s.baidu_host_id&&p.push(types_1.HostType.BAIDU);const{encode_root:c}=await this.sdk.getSystemConfig(),u=(0,path_1.resolve)(c,s.id.toString());if(await this.fileRepository.update(e.id,{output_root:u}),s.type!=types_1.RecordType.AFFIX){const c={id:i,uid:a,keepPreview:r.file_keeps==types_1.RecordFileKeep.PREVIEW||r.file_keeps==types_1.RecordFileKeep.BOTH,keepSource:r.file_keeps==types_1.RecordFileKeep.SOURCE||r.file_keeps==types_1.RecordFileKeep.BOTH,input:o[0],type:n,hosts:p,outputRoot:u,texts:d,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug};if(!Object.keys(s.progress??{}).length){const t=await(0,single_encoder_1.prepare)(c);await this.fileRepository.update(e.id,{progress:t.data})}const h=await piscina.run(c,{name:"encode",signal:t.signal});if(!h)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:h})}else{const a={id:i,inputs:s.inputs,outputRoot:u,hosts:p,debug:this.params.debug};if(!Object.keys(s.progress??{}).length){const t=await(0,single_encoder_1.prepareAffix)(a);await this.fileRepository.update(e.id,{progress:t})}const r=await piscina.run(a,{name:"encodeAffix",signal:t.signal});if(!r)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:{affix:r}})}await this.onSuccess(await this.fileRepository.findOneBy({id:i}))}catch(t){"AbortError"==t.name?await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.NULL}):"SimpleError"==t.name?await this.onError(await this.fileRepository.findOneBy({id:e.id}),t):await this.onError(await this.fileRepository.findOneBy({id:e.id}),new utils_1.SimpleError({code:12100,message:t.message,stack:t.stack}))}finally{this.threads=this.threads.filter((t=>t.id!==e.id)),await this.run()}},stop:async()=>{t.abort()}}}async run(){for(;this.isRunning;)await new Promise((e=>{setTimeout(e,100)}));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,encode_status:types_1.EncodeStatus.NULL,type:(0,typeorm_1.Not)(types_1.RecordType.FOLDER),is_paused:!1},order:{first_order:"ASC",second_order:"ASC"}});if(!t)break;const s=this.getThread(t);this.threads.push(s),await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.PROCESS}),s.start()}}else if(t>s){const e=t-s,i=this.threads.slice(0,e);for(const e of i)await e.stop()}this.isRunning=!1}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}}exports.Encoder=Encoder;
package/dist/main.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import { Encoder } from './encoder';
2
2
  import { EncoderParams } from './types';
3
3
  export declare const getEncoder: (params: EncoderParams, cleanOtherCache?: boolean) => Promise<Encoder>;
4
- export declare const getEncoderByUid: (uid: number) => Promise<Encoder>;
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getEncoderByUid=exports.getEncoder=void 0;const encoder_1=require("./encoder"),instanceMap=new Map,getEncoder=async(e,n=!0)=>{for(;instanceMap.get(e.uid)?.initing;)await new Promise((e=>setTimeout(e,100)));if(instanceMap.get(e.uid)?.encoder)return instanceMap.get(e.uid).encoder;const t={encoder:new encoder_1.Encoder(e),initing:!0};return instanceMap.set(e.uid,t),await t.encoder.init(),t.initing=!1,instanceMap.set(e.uid,t),n&&instanceMap.forEach(((n,t)=>{t!==e.uid&&instanceMap.delete(t)})),t.encoder};exports.getEncoder=getEncoder;const getEncoderByUid=async e=>{if(instanceMap.has(e)){for(;instanceMap.get(e)?.initing;)await new Promise((e=>setTimeout(e,100)));return instanceMap.get(e)?.encoder}return null};exports.getEncoderByUid=getEncoderByUid;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getEncoder=void 0;const sdk_1=require("@soga/sdk"),encoder_1=require("./encoder"),instanceMap=new Map,getEncoder=async(e,n=!0)=>{const t=(0,sdk_1.getSdk)(e.sdk_domain),r=(await t.getUserInfo({refresh:!1})).id;for(;instanceMap.get(r)?.initing;)await new Promise((e=>setTimeout(e,100)));if(instanceMap.get(r)?.encoder)return instanceMap.get(r).encoder;const i={encoder:new encoder_1.Encoder(e),initing:!0};return instanceMap.set(r,i),await i.encoder.init(),i.initing=!1,instanceMap.set(r,i),n&&instanceMap.forEach(((e,n)=>{n!==r&&instanceMap.delete(n)})),i.encoder};exports.getEncoder=getEncoder;
package/dist/types.d.ts CHANGED
@@ -1,10 +1,9 @@
1
- import { DataSource } from 'typeorm';
2
1
  import { UploadFile } from '@soga/entities';
2
+ import { DataSource } from 'typeorm';
3
3
  export type EncoderParams = {
4
- dataSource: DataSource;
5
4
  ffmpegPath: string;
6
- uid: number;
7
- getOutputRoot: (file: UploadFile) => Promise<string>;
5
+ sdk_domain: string;
6
+ dataSource: DataSource;
8
7
  threads?: number;
9
8
  debug?: boolean;
10
9
  onProgress?: (file: UploadFile) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soga/encoder",
3
- "version": "0.1.14",
3
+ "version": "0.2.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -34,7 +34,6 @@
34
34
  "eslint-plugin-prettier": "^5.0.0",
35
35
  "glob": "^10.3.3",
36
36
  "jest": "^29.6.3",
37
- "piscina": "^4.8.0",
38
37
  "prettier": "^3.0.2",
39
38
  "rimraf": "^6.0.1",
40
39
  "terser": "^5.19.2",
@@ -46,14 +45,16 @@
46
45
  "author": "",
47
46
  "license": "ISC",
48
47
  "dependencies": {
49
- "@soga/entities": "^0.1.14",
50
- "@soga/single-encoder": "^0.1.13",
51
- "@soga/types": "^0.1.13",
52
- "fs-extra": "^11.2.0"
48
+ "@soga/entities": "^0.2.1",
49
+ "@soga/sdk": "^0.2.1",
50
+ "@soga/single-encoder": "^0.2.1",
51
+ "@soga/types": "^0.2.1",
52
+ "fs-extra": "^11.2.0",
53
+ "piscina": "^4.8.0"
53
54
  },
54
55
  "peerDependencies": {
55
56
  "better-sqlite3": "*",
56
57
  "typeorm": "*"
57
58
  },
58
- "gitHead": "99f8fe90e0f34b6a645e6b2c6682071f74c9a0dc"
59
+ "gitHead": "92e93cbd89d863f10407c93b88db5ec5029ed426"
59
60
  }