io.ts 7.0 KB
Newer Older
P
Peter Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright 2020 Baidu Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

P
Peter Pan 已提交
17 18 19
/* eslint-disable no-console */

import crypto, {BinaryLike} from 'crypto';
20
import {promises as fs, writeFileSync} from 'fs';
P
Peter Pan 已提交
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 62 63 64 65 66

import path from 'path';
import querystring from 'querystring';

const apiUrl = '/api';

type Query = Record<string, string | number> | null;

interface WriteOptions {
    type?: 'json' | 'buffer';
}

interface MetaData {
    uri: string;
    query?: Record<string, string | string[]>;
    filename: string;
    headers: Record<string, string>;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
interface ResponseData<T = any> {
    status: number;
    msg?: string;
    data: T;
}

function isEmpty(obj: Record<string, unknown> | null | undefined) {
    if (obj == null) {
        return true;
    }
    return !Object.keys(obj).length;
}

export default class IO {
    public static readonly metaFileName = 'meta.json';
    public static readonly dataPath = 'data';
    public static readonly hashFunction = 'md4';

    protected readonly url: string;
    protected readonly dataDir: string;

    protected metadata: MetaData[] = [];

    constructor(url: string, dataDir: string) {
        this.url = url;
        this.dataDir = dataDir;
67 68 69 70 71

        writeFileSync(path.join(this.dataDir, IO.metaFileName), JSON.stringify(this.metadata), {
            encoding: 'utf-8',
            flag: 'w'
        });
P
Peter Pan 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }

    public static isSameUri(url1: Pick<MetaData, 'uri' | 'query'>, url2: Pick<MetaData, 'uri' | 'query'>) {
        if (url1.uri !== url2.uri) {
            return false;
        }
        if (!isEmpty(url2.query)) {
            if (isEmpty(url1.query)) {
                return false;
            }
            for (const [key, value] of Object.entries(url2.query)) {
                const existValue = url1.query[key];
                if (existValue !== value) {
                    if (Array.isArray(value) && Array.isArray(existValue)) {
                        const count = value.reduce<Record<string, number>>((m, v) => {
                            if (m[v] == null) {
                                m[v] = 1;
                            } else {
                                m[v]++;
                            }
                            return m;
                        }, {});
                        for (const i of existValue) {
                            if (count[i] == null) {
                                return false;
                            }
                            count[i]--;
                        }
                        return Object.values(count).every(c => c === 0);
                    }
                    return false;
                }
            }
            return true;
        } else {
            return isEmpty(url1.query);
        }
    }

    private generateFilename(content: BinaryLike) {
        const hash = crypto.createHash(IO.hashFunction);
        hash.update(content);
        return hash.digest('hex');
    }

    private addMeta(meta: MetaData) {
        const exist = this.metadata.find(data => IO.isSameUri(data, meta));
        if (!exist) {
            this.metadata.push(meta);
        }
    }

    protected async write(
        filePath: string,
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        content: Record<string, any> | Buffer,
        contentType: string,
        options?: WriteOptions | WriteOptions['type']
    ) {
P
Peter Pan 已提交
131
        const {default: mkdirp} = await import('mkdirp');
P
Peter Pan 已提交
132 133 134 135 136 137 138
        const type = 'string' === typeof options ? options : options?.type ?? 'json';

        const fileDir = path.join(this.dataDir, IO.dataPath, filePath);
        await mkdirp(fileDir);
        let fileContent: Buffer;
        let extname: string;
        if (type === 'buffer') {
P
Peter Pan 已提交
139
            const {default: mime} = await import('mime-types');
P
Peter Pan 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            extname = mime.extension(contentType) || '';
            if (extname) {
                extname = '.' + extname;
            }
            fileContent = content as Buffer;
        } else {
            extname = '.json';
            fileContent = Buffer.from(JSON.stringify(content), 'utf-8');
        }

        const filename = this.generateFilename(fileContent) + extname;
        await fs.writeFile(path.join(fileDir, filename), fileContent, {
            encoding: null,
            flag: 'w'
        });
        console.log(`write file ${path.join(filePath, filename)}`);
        return filename;
    }

159 160
    async fetch(uri: string, query?: Query) {
        const {default: fetch} = await import('node-fetch');
P
Peter Pan 已提交
161 162 163 164 165
        let url = this.url + apiUrl + uri;
        if (!isEmpty(query)) {
            url += '?' + querystring.stringify(query);
        }
        try {
166
            return await fetch(url);
P
Peter Pan 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        } catch (e) {
            console.error(e);
        }
    }

    protected async fetchAndWrite<T>(uri: string, query?: Query, options?: WriteOptions | WriteOptions['type']) {
        const type = 'string' === typeof options ? options : options?.type ?? 'json';

        const response = await this.fetch(uri, query);
        if (!response.ok) {
            throw new Error('not ok');
        }

        let content: ResponseData<T> | ArrayBuffer;
        if (type === 'buffer') {
            content = await response.buffer();
        } else {
            content = (await response.json()) as ResponseData<T>;
        }
        const filename = await this.write(uri, content, response.headers.get('content-type'), options);
        this.addMeta({
            uri,
            query: isEmpty(query) ? undefined : querystring.parse(querystring.stringify(query)),
            filename,
            headers: ['Content-Type', 'Content-Disposition'].reduce((m, t) => {
                m[t] = response.headers.get(t) || undefined;
                return m;
            }, {})
        });
        return content;
    }

    async save<T>(uri: string, query?: Query) {
        return ((await this.fetchAndWrite<T>(uri, query, 'json')) as ResponseData<T>).data;
    }

    async saveBinary(uri: string, query?: Query) {
        return (await this.fetchAndWrite(uri, query, 'buffer')) as Buffer;
    }

    async getData<T>(uri: string, query?: Query) {
        return ((await (await this.fetch(uri, query)).json()) as ResponseData<T>).data;
    }

    generateMeta() {
        return fs.writeFile(path.join(this.dataDir, IO.metaFileName), JSON.stringify(this.metadata), {
            encoding: 'utf-8',
            flag: 'w'
        });
    }

    sleep(time: number) {
        return new Promise(resolve => {
            setTimeout(resolve, time);
        });
    }
}