File size: 2,064 Bytes
23a3b80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { propertyInjectorFactory } from 'civkit/property-injector';
import { KoaRPCRegistry } from 'civkit/civ-rpc/koa';
import { container, singleton } from 'tsyringe';
import { IntegrityEnvelope } from 'civkit/civ-rpc';
import bodyParser from '@koa/bodyparser';

import { GlobalLogger } from './logger';
import { TempFileManager } from './temp-file';
import { AsyncLocalContext } from './async-context';
import { BlackHoleDetector } from './blackhole-detector';
export { Context } from 'koa';

export const InjectProperty = propertyInjectorFactory(container);

@singleton()
export class RPCRegistry extends KoaRPCRegistry {

    title = 'Jina Reader API';
    container = container;
    logger = this.globalLogger.child({ service: this.constructor.name });
    static override envelope = IntegrityEnvelope;
    override _BODY_PARSER_LIMIT = '102mb';
    override _RESPONSE_STREAM_MODE = 'koa' as const;

    override koaMiddlewares = [
        this.__CORSAllowAllMiddleware.bind(this),
        bodyParser({
            encoding: 'utf-8',
            enableTypes: ['json', 'form'],
            jsonLimit: this._BODY_PARSER_LIMIT,
            xmlLimit: this._BODY_PARSER_LIMIT,
            formLimit: this._BODY_PARSER_LIMIT,
        }),
        this.__multiParse.bind(this),
        this.__binaryParse.bind(this),
    ];

    constructor(
        protected globalLogger: GlobalLogger,
        protected ctxMgr: AsyncLocalContext,
        protected tempFileManager: TempFileManager,
        protected blackHoleDetector: BlackHoleDetector,
    ) {
        super(...arguments);

        this.on('run', () => this.blackHoleDetector.incomingRequest());
        this.on('ran', () => this.blackHoleDetector.doneWithRequest());
        this.on('fail', () => this.blackHoleDetector.doneWithRequest());
    }

    override async init() {
        await this.dependencyReady();
        this.emit('ready');
    }

}

const instance = container.resolve(RPCRegistry);
export default instance;
export const { Method, RPCMethod, RPCReflect, Param, Ctx, } = instance.decorators();