Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .internal file handling from the setup repo #16

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "./build/index.js",
"types": "./build/index.d.ts",
"scripts": {
"compile": "tsc",
"compile": "tsc --watch",
"test": "jest --config jest.config.js",
"lint": "eslint src --ext .ts --fix",
"build": "npm run lint && npm run compile && npm run build:browser && npm run build:docs && npm run build:docs.json && npm run test",
Expand Down Expand Up @@ -38,12 +38,14 @@
},
"homepage": "https://github.com/AzurAPI/azurapi-js#readme",
"dependencies": {
"fuse.js": "^6.4.6"
"fuse.js": "^6.4.6",
"node-fetch": "^2.6.7"
},
"devDependencies": {
"@augu/eslint-config": "^2.2.0",
"@types/jest": "^26.0.14",
"@types/node": "^14.14.45",
"@types/node-fetch": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"eslint": "^8.19.0",
Expand Down
22 changes: 19 additions & 3 deletions src/core/CacheUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Functions relating to updating the cache
* @packageDocumentation
*/
import { baseFolder, data, datatype, local } from './Data';
import { baseFolder, datatype, local, getDataUrl } from './Data';
import fs from 'fs';
import { check, fetch } from './UpdateChecker';
import { check } from './UpdateChecker';
import { AzurAPI } from './Client';
import fetch from 'node-fetch';

export default class Updater {
public cron?: NodeJS.Timeout;
Expand All @@ -29,7 +30,22 @@ export default class Updater {
this.client.emit('updateAvalible', updates);
return Promise.all(
updates.map(async (type) => {
const raw = JSON.parse(await fetch(data[type])) ?? [];
let raw: any = [];
try {
let response = await fetch(getDataUrl(type));
if (response.status === 404) {
response = await fetch(getDataUrl(type, { internal: true }));
}
raw = await response.json();

if (raw?.length === undefined) {
raw = Object.values(raw);
}
} catch (e) {
console.log(`Error while fetching data for ${type} (${getDataUrl(type)})`);
console.log(e);
return;
}

if (type === 'voicelines') {
/**
Expand Down
34 changes: 21 additions & 13 deletions src/core/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
*/
import { join } from 'path';

export const data = {
version: 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/dist/version.json',
ships: 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/dist/ships.json',
equipments:
'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/dist/equipments.json',
chapters:
'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/dist/chapters.min.json',
voicelines: 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/voice_lines.json',
barrages: 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/dist/barrage.json',
shipList: 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/dist/ship-list.json',
idMap: 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/dist/id-map.json',
const baseDataUrl = 'https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master';

export type datatype = 'ships' | 'equipments' | 'voicelines' | 'chapters' | 'barrages';

type UrlType = datatype | 'version' | 'shipList' | 'idMap';

export const getDataUrl = (type: UrlType, options?: { internal?: boolean }) => {
const internalString = options?.internal ? '.internal' : '';

const urls: Record<UrlType, string> = {
version: `${baseDataUrl}/dist/version${internalString}.json`,
ships: `${baseDataUrl}/dist/ships${internalString}.json`,
equipments: `${baseDataUrl}/dist/equipments${internalString}.json`,
chapters: `${baseDataUrl}/dist/chapters${internalString}.min.json`,
voicelines: `${baseDataUrl}/voice_lines${internalString}.json`,
barrages: `${baseDataUrl}/dist/barrage${internalString}.json`,
shipList: `${baseDataUrl}/dist/ship-list${internalString}.json`,
idMap: `${baseDataUrl}/dist/id-map${internalString}.json`,
};

return urls[type];
};

export const local = {
Expand All @@ -27,9 +37,7 @@ export const local = {
shipList: join(__dirname, '..', '.azurlane', 'ship-list.json'),
idMap: join(__dirname, '..', '.azurlane', 'id-map.json'),
};
export type datatype = 'ships' | 'equipments' | 'voicelines' | 'chapters' | 'barrages';
export let baseFolder = join(__dirname, '..', '.azurlane');
export let versionInfo = join(__dirname, '..', '.azurlane', 'version.json');

Object.freeze(local);
Object.freeze(data);
23 changes: 4 additions & 19 deletions src/core/UpdateChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Check for updates and functions relating to updates
* @packageDocumentation
*/
import { data, datatype, versionInfo } from './Data';
import { datatype, versionInfo, getDataUrl } from './Data';
import fs from 'fs';
import https from 'https';
import fetch from 'node-fetch';

const supported = ['ships', 'equipments', 'chapters', 'barrages', 'voicelines'];

Expand All @@ -18,7 +18,8 @@ let remote;
export async function check(): Promise<datatype[]> {
if (!version && fs.existsSync(versionInfo))
version = JSON.parse(fs.readFileSync(versionInfo).toString());
remote = JSON.parse(await fetch(data.version));
const response = await fetch(getDataUrl('version'));
remote = await response.json();
let needUpdate: datatype[] = [];
for (let type of supported)
if (
Expand All @@ -38,19 +39,3 @@ export async function check(): Promise<datatype[]> {
export function save() {
if (remote) fs.writeFileSync(versionInfo, JSON.stringify((version = remote)));
}

/**
* Fetch data
* @param url URL
*/
export function fetch(url: string): Promise<string> {
return new Promise((resolve, reject) =>
https
.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => (data += chunk));
resp.on('end', () => resolve(data));
})
.on('error', (err) => reject(err))
);
}