genshin-manager
Version:
<div align="center"> <p> <a href="https://www.npmjs.com/package/genshin-manager"><img src="https://img.shields.io/npm/v/genshin-manager.svg?maxAge=3600" alt="npm version" /></a> <a href="https://www.npmjs.com/package/genshin-manager"><img src="https:
180 lines (179 loc) • 8.09 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AudioAssets = void 0;
const fs_1 = __importDefault(require("fs"));
const fsPromises = __importStar(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const promises_1 = require("stream/promises");
const AudioNotFoundError_1 = require("../../errors/AudioNotFoundError");
const ReadableStreamWrapper_1 = require("../../utils/ReadableStreamWrapper");
/**
* Class for compiling information about audio
*/
class AudioAssets {
/**
* Classes for handling audios
* @param name Audio name
* @param cv Audio cv
* @param characterId Character ID
*/
constructor(name, cv, characterId) {
var _a;
this.name = name;
this.cv = cv;
this.characterId = characterId;
this.audioBaseURL =
(_a = Object.keys(AudioAssets.audioBaseURLByRegex).find((url) => AudioAssets.audioBaseURLByRegex[url].some((regex) => regex.test(name)))) !== null && _a !== void 0 ? _a : AudioAssets.defaultAudioBaseURL;
const cvPath = cv === undefined ? '' : `/${cv}`;
const characterIdPath = characterId === undefined ? '' : `/${characterId}`;
this.url =
name === ''
? ''
: `${this.audioBaseURL}${cvPath}${characterIdPath}/${name}.ogg`;
this.mihoyoURL =
name === ''
? ''
: `${AudioAssets.AUDIO_BASE_URL_MIHOYO}${cvPath}${characterIdPath}/${name}.ogg`;
}
/**
* Classes for handling audios
* @param option Client option
*/
static deploy(option) {
this.fetchOption = option.fetchOption;
this.audioBaseURLByRegex = option.audioBaseURLByRegex;
this.defaultAudioBaseURL = option.defaultAudioBaseURL;
this.autoCacheAudio = option.autoCacheAudio;
this.audioFolderPath = path_1.default.resolve(option.assetCacheFolderPath, 'Audios');
if (!fs_1.default.existsSync(this.audioFolderPath))
fs_1.default.mkdirSync(this.audioFolderPath);
}
/**
* Fetch audio buffer
* @returns Audio buffer
*/
fetchBuffer() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.url)
throw new AudioNotFoundError_1.AudioNotFoundError(this.name, this.url);
const cvPaths = [];
if (this.cv !== undefined)
cvPaths.push(this.cv);
if (this.characterId !== undefined)
cvPaths.push(`${this.characterId}`);
const audioCachePath = path_1.default.resolve(AudioAssets.audioFolderPath, ...cvPaths, `${this.name}.ogg`);
if (fs_1.default.existsSync(audioCachePath) && !this.isOGGCorrupted(audioCachePath)) {
return yield fsPromises.readFile(audioCachePath);
}
else {
const res = yield fetch(this.url, AudioAssets.fetchOption);
if (!res.ok || !res.body)
throw new AudioNotFoundError_1.AudioNotFoundError(this.name, this.url);
const arrayBuffer = yield res.arrayBuffer();
const data = Buffer.from(arrayBuffer);
if (AudioAssets.autoCacheAudio) {
fs_1.default.mkdirSync(path_1.default.dirname(audioCachePath), { recursive: true });
yield fsPromises.writeFile(audioCachePath, data, { flag: 'w' });
}
return data;
}
});
}
/**
* Fetch audio stream
* @param highWaterMark HighWaterMark
* @returns Audio stream
*/
fetchStream(highWaterMark) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.url)
throw new AudioNotFoundError_1.AudioNotFoundError(this.name, this.url);
const cvPaths = [];
if (this.cv !== undefined)
cvPaths.push(this.cv);
if (this.characterId !== undefined)
cvPaths.push(`${this.characterId}`);
const audioCachePath = path_1.default.resolve(AudioAssets.audioFolderPath, ...cvPaths, `${this.name}.ogg`);
if (fs_1.default.existsSync(audioCachePath) && !this.isOGGCorrupted(audioCachePath)) {
return fs_1.default.createReadStream(audioCachePath, {
highWaterMark: highWaterMark,
});
}
else {
const res = yield fetch(this.url, AudioAssets.fetchOption);
if (!res.ok || !res.body)
throw new AudioNotFoundError_1.AudioNotFoundError(this.name, this.url);
if (AudioAssets.autoCacheAudio) {
fs_1.default.mkdirSync(path_1.default.dirname(audioCachePath), { recursive: true });
const fsWriteStream = fs_1.default.createWriteStream(audioCachePath, {
highWaterMark: highWaterMark,
});
yield (0, promises_1.pipeline)(new ReadableStreamWrapper_1.ReadableStreamWrapper(res.body.getReader()), fsWriteStream);
}
return fs_1.default.createReadStream(audioCachePath, {
highWaterMark: highWaterMark,
});
}
});
}
/**
* Check if the OGG file is corrupted
* @warning This function is not perfect, it may not be able to detect all corrupted OGG files. because it only checks the last OggS header.
* @param filePath File path
* @returns is OGG file corrupted
*/
isOGGCorrupted(filePath) {
const data = fs_1.default.readFileSync(filePath); // There is no footer in the ogg file, so all the data has to be read in.
const lastIndex = data.lastIndexOf('OggS');
if (lastIndex !== -1) {
const headerType = data.readUInt8(lastIndex + 5);
if (headerType === 4)
return false;
else
return true;
}
else {
return true;
}
}
}
exports.AudioAssets = AudioAssets;
/**
* Audio base URL of mihoyo
*/
AudioAssets.AUDIO_BASE_URL_MIHOYO = 'https://upload-os-bbs.mihoyo.com/game_record/genshin';