@soga/encoder 0.0.14 → 0.1.0

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
@@ -1,5 +1,5 @@
1
1
  import { Repository } from 'typeorm';
2
- import { EncoderParams } from './types/main';
2
+ import { EncoderParams } from './types';
3
3
  import { UploadFile } from '@soga/entities';
4
4
  export declare class Encoder {
5
5
  protected onProgress: (file: UploadFile) => Promise<void>;
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"),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(t){return Math.min(Math.max(t??0,0),this.maxThreads)}constructor(t){this.params=t,this.fileRepository=t.dataSource.getRepository(entities_1.UploadFile),this.thread_count=this.getValidThreads(t.threads??this.thread_count),t.onProgress&&(this.onProgress=t.onProgress.bind(this)),t.onSuccess&&(this.onSuccess=t.onSuccess.bind(this)),t.onError&&(this.onError=t.onError.bind(this)),piscina.on("message",(async t=>{if("percent"==t.type){const{id:e,data:s}=t,i=await this.fileRepository.findOneBy({id:e});if(!i)return;const{percent:a,step:o}=s;i.progress[o].percent=a,await this.fileRepository.update(i.id,{progress:i.progress}),this.onProgress(i)}}))}async setThreads(t){const e=this.getValidThreads(t);this.thread_count=e,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(t,e=!1){e&&await this.fileRepository.update({id:(0,typeorm_1.In)(t),is_paused:!1},{is_paused:!0});const s=this.threads.filter((e=>t.includes(e.id)));await Promise.all(s.map((t=>t.stop()))),await this.run()}async stopAll(t=!1){t&&await this.fileRepository.update({uid:this.params.uid},{is_paused:!0}),await Promise.all(this.threads.map((t=>t.stop())))}getThread(t){const e=new AbortController;return{id:t.id,uid:t.uid,start:async()=>{try{const s=await this.fileRepository.findOneBy({id:t.id});if(s.encode_status!==types_1.EncodeStatus.PROCESS)return;if(!s)return;const{id:i,uid:a,config:o,path:r,type:n,ftype:d,external_texts:p}=s,c=[];s.ali_host_id&&c.push(types_1.HostType.ALI),s.baidu_host_id&&c.push(types_1.HostType.BAIDU);const h=await this.params.getOutputRoot(s);await this.fileRepository.update(t.id,{output_root:h});const u={id:i,uid:a,keepPreview:o.file_keeps==types_1.RecordFileKeep.PREVIEW||o.file_keeps==types_1.RecordFileKeep.BOTH,keepSource:o.file_keeps==types_1.RecordFileKeep.SOURCE||o.file_keeps==types_1.RecordFileKeep.BOTH,filePath:r,type:n,ftype:d,hosts:c,outputRoot:h,texts:p,ffmpegPath:this.params.ffmpegPath},l=await(0,single_encoder_1.prepare)(u);Object.keys(s.progress??{}).length||await this.fileRepository.update(t.id,{progress:l.data});const y=await piscina.run(u,{name:"encode",signal:e.signal});if(!y||!y.state)throw new Error("upload chunk failed!");const{img:_,media:f,source:g,txt:w}=y.data;await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.SUCCESS,img_data:_,media_data:f,source_data:g,txt_data:w}),await this.onSuccess(await this.fileRepository.findOneBy({id:i}))}catch(e){"AbortError"!==e.name?await this.onError(await this.fileRepository.findOneBy({id:t.id})):await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.NULL})}finally{this.threads=this.threads.filter((e=>e.id!==t.id)),await this.run()}},stop:async()=>{e.abort()}}}async run(){for(;this.isRunning;)await new Promise((t=>{setTimeout(t,100)}));this.isRunning=!0;const t=this.threads.length,e=this.thread_count;if(t<e){const s=e-t;for(let t=0;t<s;t++){const t=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(!t)break;const e=this.getThread(t);this.threads.push(e),await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.PROCESS}),e.start()}}else if(t>e){const s=t-e,i=this.threads.slice(0,s);for(const t of i)await t.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"),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(t){return Math.min(Math.max(t??0,0),this.maxThreads)}constructor(t){this.params=t,this.fileRepository=t.dataSource.getRepository(entities_1.UploadFile),this.thread_count=this.getValidThreads(t.threads??this.thread_count),t.onProgress&&(this.onProgress=t.onProgress.bind(this)),t.onSuccess&&(this.onSuccess=t.onSuccess.bind(this)),t.onError&&(this.onError=t.onError.bind(this)),piscina.on("message",(async t=>{if("percent"==t.type){const{id:e,data:s}=t,i=await this.fileRepository.findOneBy({id:e});if(!i)return;const{percent:a,step:r}=s;i.progress[r].percent=a,await this.fileRepository.update(i.id,{progress:i.progress}),this.onProgress(i)}})),piscina.on("error",(async t=>{await(this.params.onError?.(null,t))}))}async setThreads(t){const e=this.getValidThreads(t);this.thread_count=e,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(t,e=!1){e&&await this.fileRepository.update({id:(0,typeorm_1.In)(t),is_paused:!1},{is_paused:!0});const s=this.threads.filter((e=>t.includes(e.id)));await Promise.all(s.map((t=>t.stop()))),await this.run()}async stopAll(t=!1){t&&await this.fileRepository.update({uid:this.params.uid},{is_paused:!0}),await Promise.all(this.threads.map((t=>t.stop())))}getThread(t){const e=new AbortController;return{id:t.id,uid:t.uid,start:async()=>{try{const s=await this.fileRepository.findOneBy({id:t.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(t.id,{output_root:c});const h={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},u=await(0,single_encoder_1.prepare)(h);Object.keys(s.progress??{}).length||await this.fileRepository.update(t.id,{progress:u.data});const l=await piscina.run(h,{name:"encode",signal:e.signal});if(!l||!l.state)throw new Error("upload chunk failed!");const{img:y,media:_,source:f,txt:g}=l.data;await this.fileRepository.update(t.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(e){"AbortError"!==e.name?await this.onError(await this.fileRepository.findOneBy({id:t.id})):await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.NULL})}finally{this.threads=this.threads.filter((e=>e.id!==t.id)),await this.run()}},stop:async()=>{e.abort()}}}async run(){for(;this.isRunning;)await new Promise((t=>{setTimeout(t,100)}));this.isRunning=!0;const t=this.threads.length,e=this.thread_count;if(t<e){const s=e-t;for(let t=0;t<s;t++){const t=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(!t)break;const e=this.getThread(t);this.threads.push(e),await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.PROCESS}),e.start()}}else if(t>e){const s=t-e,i=this.threads.slice(0,s);for(const t of i)await t.stop()}this.isRunning=!1}}exports.Encoder=Encoder;
package/dist/main.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Encoder } from './encoder';
2
- import { EncoderParams } from './types/main';
3
- export declare const getEncoder: (params: EncoderParams) => Promise<Encoder>;
2
+ import { EncoderParams } from './types';
3
+ export declare const getEncoder: (params: EncoderParams, cleanOtherCache?: boolean) => Promise<Encoder>;
4
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=>{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 n={encoder:new encoder_1.Encoder(e),initing:!0};return instanceMap.set(e.uid,n),await n.encoder.init(),n.initing=!1,instanceMap.set(e.uid,n),n.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.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;
@@ -9,11 +9,5 @@ export type EncoderParams = {
9
9
  debug?: boolean;
10
10
  onProgress?: (file: UploadFile) => Promise<void>;
11
11
  onSuccess?: (file: UploadFile) => Promise<void>;
12
- onError?: (file: UploadFile) => Promise<void>;
13
- };
14
- export type ThreadType = {
15
- id: number;
16
- uid: number;
17
- start: () => Promise<void>;
18
- stop: () => Promise<void>;
12
+ onError?: (file: UploadFile | null, error?: Error) => Promise<void>;
19
13
  };
package/package.json CHANGED
@@ -1,57 +1,59 @@
1
- {
2
- "name": "@soga/encoder",
3
- "version": "0.0.14",
4
- "publishConfig": {
5
- "access": "public"
6
- },
7
- "description": "",
8
- "main": "dist/main.js",
9
- "types": "dist/main.d.ts",
10
- "files": [
11
- "dist"
12
- ],
13
- "scripts": {
14
- "build": "rimraf dist && tsc && ts-node ./scripts/minify",
15
- "minify": "ts-node ./scripts/minify",
16
- "demo_backup": "ts-node ./demo/demo.ts",
17
- "demo": "ts-node ./demo/demo.ts",
18
- "worker": "tsc && ts-node ./demo/worker.ts",
19
- "test": "jest",
20
- "dev": "ts-node ./src/main.ts",
21
- "lint": "eslint . --ext .ts",
22
- "prepublishOnly": "npm run build"
23
- },
24
- "devDependencies": {
25
- "@types/fs-extra": "^11.0.4",
26
- "@types/glob": "^8.1.0",
27
- "@types/jest": "^29.5.4",
28
- "@types/node": "^20.8.7",
29
- "@typescript-eslint/eslint-plugin": "^6.4.1",
30
- "@typescript-eslint/parser": "^6.4.1",
31
- "eslint": "^8.47.0",
32
- "eslint-config-prettier": "^9.0.0",
33
- "eslint-plugin-jest": "^27.2.3",
34
- "eslint-plugin-prettier": "^5.0.0",
35
- "glob": "^10.3.3",
36
- "jest": "^29.6.3",
37
- "piscina": "^4.8.0",
38
- "prettier": "^3.0.2",
39
- "terser": "^5.19.2",
40
- "ts-jest": "^29.1.1",
41
- "ts-node": "^10.9.1",
42
- "typescript": "^5.1.6"
43
- },
44
- "keywords": [],
45
- "author": "",
46
- "license": "ISC",
47
- "dependencies": {
48
- "@soga/single-encoder": "^0.0.4",
49
- "@soga/types": "^0.0.61",
50
- "fs-extra": "^11.2.0"
51
- },
52
- "peerDependencies": {
53
- "@soga/entities": "*",
54
- "typeorm": "*",
55
- "better-sqlite3": "*"
56
- }
57
- }
1
+ {
2
+ "name": "@soga/encoder",
3
+ "version": "0.1.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "",
8
+ "main": "dist/main.js",
9
+ "types": "dist/main.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "build": "rimraf dist && tsc && ts-node ./scripts/minify",
15
+ "minify": "ts-node ./scripts/minify",
16
+ "demo_backup": "ts-node ./demo/demo.ts",
17
+ "demo": "ts-node ./demo/demo.ts",
18
+ "worker": "tsc && ts-node ./demo/worker.ts",
19
+ "test": "jest",
20
+ "dev": "ts-node ./src/main.ts",
21
+ "lint": "eslint . --ext .ts",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "devDependencies": {
25
+ "@soga/single-encoder": "^0.1.0",
26
+ "@types/fs-extra": "^11.0.4",
27
+ "@types/glob": "^8.1.0",
28
+ "@types/jest": "^29.5.4",
29
+ "@types/node": "^20.8.7",
30
+ "@typescript-eslint/eslint-plugin": "^6.4.1",
31
+ "@typescript-eslint/parser": "^6.4.1",
32
+ "eslint": "^8.47.0",
33
+ "eslint-config-prettier": "^9.0.0",
34
+ "eslint-plugin-jest": "^27.2.3",
35
+ "eslint-plugin-prettier": "^5.0.0",
36
+ "glob": "^10.3.3",
37
+ "jest": "^29.6.3",
38
+ "piscina": "^4.8.0",
39
+ "prettier": "^3.0.2",
40
+ "rimraf": "^6.0.1",
41
+ "terser": "^5.19.2",
42
+ "ts-jest": "^29.1.1",
43
+ "ts-node": "^10.9.1",
44
+ "typescript": "^5.1.6"
45
+ },
46
+ "keywords": [],
47
+ "author": "",
48
+ "license": "ISC",
49
+ "dependencies": {
50
+ "fs-extra": "^11.2.0"
51
+ },
52
+ "peerDependencies": {
53
+ "@soga/entities": "file:../base-entities",
54
+ "@soga/types": "file:../base-types",
55
+ "better-sqlite3": "*",
56
+ "typeorm": "*"
57
+ },
58
+ "gitHead": "9c477d59857bc4b74ae6479898b38b1444744144"
59
+ }
File without changes