Git Product home page Git Product logo

saw-rpc's Introduction

Hi there 👋

Hello, my name is urain39!

I am a developer who like to writing programs in Python, JavaScript, and C-Like Languages :)

saw-rpc's People

Contributors

urain39 avatar

Watchers

 avatar  avatar  avatar

saw-rpc's Issues

部分类型优化

export type ARIA2OptionsWithout<T extends keyof ARIA2Options, U extends ARIA2Optional<ARIA2Options>> = {

改成:

export type ARIA2OptionsWithout<T extends keyof ARIA2Options, U extends ARIA2Optional<ARIA2Options>> = {
    [key in Exclude<keyof U, T>]: U[key];
};

关于异常的处理

目前我们的实现里是将异常当做是参数直接传给resolve的,这样或许不太直观。所以我打算用reject回调将异常变为async function可以catch的异常。

export type ARIA2Result<T> = ArgumentsType<JSONRPCHandler> extends [any, JSONRPCError, ...infer R] ? [T, JSONRPCError, ...R] : any[];

Aria2 watchdog实现

import { ARIA2, ARIA2Event } from './lib/saw-rpc/src/index';
import { promises as fs } from 'fs';
import * as path from 'path';
import { w3cwebsocket as WebSocket } from 'websocket';
import { rpcPath, secret } from './config.json';


// @ts-ignore
global.WebSocket = WebSocket;


const UNDEFINED = void 22;
const aria2 = new ARIA2(rpcPath, secret);


// 检查当前执行状态
(async function () {
    try {
        await aria2.tellActive(['gid']);
    } catch (e) {
        console.log('Wrong rpcPath or secret!');
        process.exit(1);
    }
})();

// 绑定事件处理回调
async function onDownloadComplete(ev: ARIA2Event) {
    const status = await aria2.tellStatus(ev.gid, ['dir', 'files']);

    const dir = status.dir;
    const files = status.files;
    if (dir !== UNDEFINED && files !== UNDEFINED) {
        const titleName = ARIA2.getTitleName(files[0], dir);
        const controlFile = path.join(dir, titleName + '.aria2');

        try {
            console.log(`aria2-watchdog: removing control file of completed '${titleName}'...`);
            await fs.unlink(controlFile);
        } catch (e) {
            console.error(e);
        }
    }
}

aria2.onNotify('aria2.onBtDownloadComplete', onDownloadComplete);
aria2.onNotify('aria2.onDownloadComplete', onDownloadComplete);

aria2.onNotify('aria2.onDownloadError', async function (ev: ARIA2Event) {
    const status = await aria2.tellStatus(ev.gid, ['dir', 'files']);

    const dir = status.dir;
    const files = status.files;
    if (dir !== UNDEFINED && files !== UNDEFINED) {
        const titleName = ARIA2.getTitleName(files[0], dir);
        const filePath = path.join(dir, titleName);
        const controlFile = filePath + '.aria2';

        try {
            console.log(`aria2-watchdog: removing fault files of '${titleName}'...`);
            await fs.rm(filePath, { 'force': true, 'recursive': true });
            await fs.unlink(controlFile);
        } catch (e) {
            console.error(e);
        }
    }
});

关于响应数据的坑

目前Aria2返回的数据中的类型都是字符串,这一点让人很头疼。我在想如果可以的话,我想写一个简单的预解析器,遍历整个返回的对象,将数据类型转换为标准类型。

getTitleName的bug

当将dir设置为/xxxx/(以文件夹分隔符结尾)时,我们获取的文件名就变成了''。这个问题应该是 Aria2 的造成的,它返回的数据里就有以连续的文件夹分隔符隔开的路径,如:F:/path/to//file

解决方法:我们只需要将源码中的三元表达式改写成无论如何都加上1,跳过分隔符就行了。

关于JSONRPC的设计与实现

/**
 * 关于JSONRPC的实现
 * 
 * 下面列出了一些WS与XHR最主要的区别:
 *
 * 前者可以多次请求和响应,但求后不能直接添加对应的回调
 * 。所以应该在请求后将请求id和处理回调放到等待列表里,
 * 当接收数据的回调被调用时,应该根据接收数据的id去查找
 * 是否有对应的数据处理回调。其是“有问不一定答”类型。
 *
 * 后者实现上因为每次都需要重新初始化对象,所以回调直接绑定
 * 在初始化后的对象上即可。换个角度来说,XHR模式下的id其实
 * 没有什么用,因为他本身就是“有问必答”类型的。
 *
 * 不过如果我们需要统计当前请求数量的话,那么这个id还是有点用的。
 */

关于发送前的预处理

差点忘了,我们内部对数据进行了预处理,但是发送时还应该是以字符串的形式发送的。不过这里的数据预处理比之前做的要简单很多,直接一次全局遍历,都不带判断的那种,将所有的数据转换为字符串就可以了。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.