-
-
Notifications
You must be signed in to change notification settings - Fork 38
feat: dynamic pp calculator switching #635
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
base: master
Are you sure you want to change the base?
Changes from all commits
7900adb
bfaf09e
2d89fe0
da85477
15b87f9
033a5bb
54dc9ec
fbaa474
b13786f
9f09339
20c7ef3
9d8b6d6
8c43271
39aa24f
9c846e3
154fb0f
1b0f0c8
2c989fb
bbfe5f7
b2e3799
c778d10
c7f7ebc
b917a6d
6446cb7
8328ae6
73e68d7
523b469
efe7137
a30094a
281518f
caf322d
7a0a27a
22ffb96
45fbc80
83a3467
ed760a4
bf5f265
88722fd
53eeb8b
f974b46
43067ef
3132a7c
676ef60
24e12be
0e9be69
1b5cd77
6e114f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "@tosu/pp-module-loader", | ||
| "version": "0.1.0", | ||
| "type": "module", | ||
| "description": "Load and manage pp calculators", | ||
| "main": "src/index.ts", | ||
| "scripts": {}, | ||
| "dependencies": { | ||
| "@tosu/common": "workspace:*", | ||
| "@tosuapp/lazer-calculator-prebuilt": "0.5.0-20260715-main.0", | ||
| "semver": "catalog:", | ||
| "tar": "^7.5.16" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^24.10.4", | ||
| "@types/semver": "^7.7.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { downloadFile, getCachePath, verifyDownload } from '@tosu/common'; | ||
| import * as fs from 'node:fs/promises'; | ||
| import * as path from 'node:path'; | ||
| import * as tar from 'tar'; | ||
|
|
||
| import { getPrebuiltPackageName } from './package'; | ||
| import type { NpmPackageDist } from './registry/types'; | ||
|
|
||
| export async function downloadCalculator( | ||
| version: string, | ||
| fetchDist: () => Promise<NpmPackageDist> | NpmPackageDist | ||
| ): Promise<string> { | ||
| const packageName = getPrebuiltPackageName(); | ||
|
|
||
| const calculatorRootPath = getCalculatorPath(); | ||
| const folderPath = path.join( | ||
| calculatorRootPath, | ||
| `${packageName}-${version}` | ||
| ); | ||
| const archivePath = path.join( | ||
| calculatorRootPath, | ||
| `${packageName}-${version}.tgz` | ||
| ); | ||
|
|
||
| if (await fs.stat(folderPath).catch(() => false)) { | ||
| return folderPath; | ||
| } | ||
|
|
||
| const dist = await fetchDist(); | ||
| await fs.mkdir(calculatorRootPath, { recursive: true }); | ||
| try { | ||
| await downloadFile(dist.tarball, archivePath); | ||
|
|
||
| if (!(await verifyDownload(`sha1:${dist.shasum}`, archivePath))) { | ||
| await fs.rm(archivePath); | ||
| throw new Error('Download verification failed.'); | ||
| } | ||
|
|
||
| await fs.mkdir(folderPath, { recursive: true }); | ||
| await tar.x({ | ||
| file: archivePath, | ||
| cwd: folderPath, | ||
| strip: 1 | ||
| }); | ||
|
Comment on lines
+40
to
+44
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yesterday i had to unpack import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import path from 'path';
async function extractTar(file_path: string, destination: string, args: string[]): Promise<string | Error> {
try {
const tgz_path = path.resolve(file_path);
const dest_path = path.resolve(destination);
const result = await promisify(execFile)('tar', ['-xzf', `${tgz_path}`, '-C', `${dest_path}`, ...args]);
if (result.stderr) return new Error(result.stderr);
return result.stdout;
} catch (error: any) {
return error.code === 'ENOENT'
? new Error('The "tar" command is not found in your system PATH. Please ensure it is installed.')
: error;
};
};example of usage: const extract = await extractTar(file_path, cache_directory, ['--strip-components=2']);
if (extract.status == 'error') {
console.error(`extracting failed`, extract);
process.exit(1);
};
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea if it's good idea to rely on os command
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } catch (err) { | ||
| await fs.rm(folderPath, { recursive: true, force: true }); | ||
|
|
||
| throw err; | ||
| } finally { | ||
| await fs.rm(archivePath, { force: true }); | ||
| } | ||
|
|
||
| return folderPath; | ||
| } | ||
|
|
||
| function getCalculatorPath(): string { | ||
| return path.join(getCachePath(), 'calculators'); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { wLogger } from '@tosu/common'; | ||
| import * as internalCalculator from '@tosuapp/lazer-calculator-prebuilt'; | ||
| import EventEmitter from 'node:events'; | ||
| import { resolve } from 'node:path'; | ||
|
|
||
| import { downloadCalculator } from './downloader'; | ||
| import { isCompatibleVersion } from './package'; | ||
| import { onlinePpRegistry } from './registry'; | ||
|
|
||
| export type LazerCalculator = | ||
| typeof import('@tosuapp/lazer-calculator-prebuilt'); | ||
|
|
||
| export type PpModule = | ||
| | { type: 'internal' } | ||
| | { type: 'dist-tag'; tag: string } | ||
| | { type: 'release'; version: string } | ||
| | { type: 'local'; path: string }; | ||
|
|
||
| let currentCalculator = internalCalculator; | ||
|
|
||
| const ppModuleManager = { | ||
| events: new EventEmitter<{ changed: [] }>(), | ||
|
|
||
| get current(): LazerCalculator { | ||
| return currentCalculator; | ||
| }, | ||
|
|
||
| async load(module: PpModule): Promise<void> { | ||
| try { | ||
| currentCalculator = await resolveCalculator(module); | ||
| wLogger.info('Successfully loaded external pp calculator.'); | ||
| } catch (err) { | ||
| wLogger.error( | ||
| 'Failed to load external pp calculator. Falling back to internal pp calculator.' | ||
| ); | ||
| wLogger.debug('Failed to load pp calculator:', err); | ||
| currentCalculator = internalCalculator; | ||
| } | ||
|
|
||
| this.events.emit('changed'); | ||
| } | ||
| }; | ||
|
|
||
| async function resolveCalculator(module: PpModule): Promise<LazerCalculator> { | ||
| switch (module.type) { | ||
| case 'internal': { | ||
| wLogger.info('Using internal pp calculator'); | ||
| return internalCalculator; | ||
| } | ||
|
|
||
| case 'dist-tag': { | ||
| wLogger.info( | ||
| `Loading external pp calculator. channel: ${module.tag}` | ||
| ); | ||
|
|
||
| const pkg = await onlinePpRegistry.fetch(module.tag); | ||
| if (!isCompatibleVersion(pkg.version)) { | ||
| const msg = `Incompatible pp calculator. channel: ${module.tag}`; | ||
| wLogger.error(msg); | ||
|
|
||
| throw new Error(msg); | ||
| } | ||
|
|
||
| return loadCalculator( | ||
| await downloadCalculator(pkg.version, () => pkg.dist) | ||
| ); | ||
| } | ||
|
|
||
| case 'release': { | ||
| wLogger.info( | ||
| `Loading external pp calculator. version: ${module.version}` | ||
| ); | ||
|
|
||
| if (!isCompatibleVersion(module.version)) { | ||
| const msg = `Incompatible pp calculator. version: ${module.version}`; | ||
| wLogger.error(msg); | ||
|
|
||
| throw new Error(msg); | ||
| } | ||
|
|
||
| return loadCalculator( | ||
| await downloadCalculator(module.version, async () => { | ||
| const pkg = await onlinePpRegistry.fetch(module.version); | ||
| return pkg.dist; | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| case 'local': { | ||
| wLogger.info(`Loading local pp calculator. path: ${module.path}`); | ||
|
|
||
| return loadCalculator(module.path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function loadCalculator(path: string): LazerCalculator { | ||
| return require(resolve(process.cwd(), path)) as LazerCalculator; | ||
| } | ||
|
|
||
| export { ppModuleManager, onlinePpRegistry }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import * as semver from 'semver'; | ||
|
|
||
| import { dependencies } from '../package.json'; | ||
|
|
||
| const CALCULATOR_VERSION = new semver.SemVer( | ||
| dependencies['@tosuapp/lazer-calculator-prebuilt'] | ||
| ); | ||
|
|
||
| const PLATFORM_KEY = `${process.platform}-${process.arch}`; | ||
| const VERSION_RANGE = `^${CALCULATOR_VERSION.version}`; | ||
|
|
||
| export function getFullPrebuiltPackageName() { | ||
| return `@tosuapp/${getPrebuiltPackageName()}`; | ||
| } | ||
|
|
||
| export function isCompatibleVersion(version: string): boolean { | ||
| return semver.satisfies(version, VERSION_RANGE, { | ||
| includePrerelease: true | ||
| }); | ||
| } | ||
|
|
||
| export function getPrebuiltPackageName() { | ||
| switch (PLATFORM_KEY) { | ||
| case 'win32-x64': | ||
| return 'lazer-calculator-win32-x64'; | ||
|
|
||
| case 'linux-x64': | ||
| return 'lazer-calculator-linux-x64'; | ||
|
|
||
| default: | ||
| throw new Error(`Unsupported platform: ${PLATFORM_KEY}`); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { PpModule } from '..'; | ||
| import { getFullPrebuiltPackageName, isCompatibleVersion } from '../package'; | ||
| import type { NpmPackage, NpmPackageVersion } from './types'; | ||
|
|
||
| export const onlinePpRegistry = { | ||
| async fetchAll(): Promise<PpModule[]> { | ||
| const pkg = await npmFetch<NpmPackage>(getFullPrebuiltPackageName()); | ||
|
|
||
| const list: PpModule[] = []; | ||
|
|
||
| for (const version in pkg.versions) { | ||
| if (!isCompatibleVersion(version)) continue; | ||
|
|
||
| list.push({ type: 'release', version }); | ||
| } | ||
|
|
||
| for (const tag in pkg['dist-tags']) { | ||
| const version = pkg['dist-tags'][tag]; | ||
| if (!isCompatibleVersion(version)) continue; | ||
|
|
||
| list.push({ type: 'dist-tag', tag }); | ||
| } | ||
|
|
||
| return list; | ||
| }, | ||
|
|
||
| fetch(specifier: string): Promise<NpmPackageVersion> { | ||
| return npmFetch<NpmPackageVersion>( | ||
| `${getFullPrebuiltPackageName()}/${specifier}` | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| async function npmFetch<T>(path: string): Promise<T> { | ||
| const res = await fetch(new URL(path, 'https://registry.npmjs.org/')); | ||
| const json = await res.json(); | ||
|
|
||
| return json as T; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| export interface NpmPackage { | ||
| 'dist-tags': Record<string, string>; | ||
| versions: Record<string, NpmPackageVersion>; | ||
| } | ||
|
|
||
| export interface NpmPackageVersion { | ||
| name: string; | ||
| version: string; | ||
| dist: NpmPackageDist; | ||
| } | ||
|
|
||
| export interface NpmPackageDist { | ||
| shasum: string; | ||
| tarball: string; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "include": [ | ||
| "./src", | ||
| "types.ts" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type * from '@tosuapp/lazer-calculator-prebuilt'; |
Uh oh!
There was an error while loading. Please reload this page.