Move utility functions

This avoids importing most dependencies when not used.
This commit is contained in:
Samuel Elliott 2022-05-21 23:52:36 +01:00
parent 5f6004bd57
commit 967a063220
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
82 changed files with 458 additions and 398 deletions

View File

@ -12,8 +12,7 @@
"files": [
"dist",
"!dist/app",
"!dist/app-main-bundle.cjs",
"!dist/cli-bundle.js",
"!dist/bundle",
"bin",
"resources"
],
@ -82,14 +81,13 @@
"npmRebuild": false,
"files": [
"dist/app/bundle",
"dist/app-main-bundle.cjs",
"dist/cli-bundle.js",
"dist/bundle",
"!dist/app/package",
"resources"
],
"asar": false,
"extraMetadata": {
"main": "dist/app-main-bundle.cjs"
"main": "dist/bundle/app-main-bundle.cjs"
},
"directories": {
"output": "dist/app/package"

View File

@ -14,7 +14,7 @@ import json from '@rollup/plugin-json';
const cli = {
input: 'src/cli-entry.ts',
output: {
file: 'dist/cli-bundle.js',
file: 'dist/bundle/cli-bundle.js',
format: 'es',
inlineDynamicImports: true,
},
@ -48,7 +48,7 @@ const cli = {
const app = {
input: 'src/app/main/app-entry.cts',
output: {
file: 'dist/app-main-bundle.cjs',
file: 'dist/bundle/app-main-bundle.cjs',
format: 'cjs',
inlineDynamicImports: true,
},

View File

@ -1,7 +1,7 @@
import fetch from 'node-fetch';
import createDebug from 'debug';
import { ErrorResponse } from './util.js';
import { version } from '../util.js';
import { version } from '../util/product.js';
const debugS2s = createDebug('nxapi:api:s2s');
const debugFlapg = createDebug('nxapi:api:flapg');

View File

@ -1,7 +1,7 @@
import fetch from 'node-fetch';
import createDebug from 'debug';
import { ErrorResponse } from './util.js';
import { JwtPayload } from '../util.js';
import { JwtPayload } from '../util/jwt.js';
const debug = createDebug('nxapi:api:na');

View File

@ -3,7 +3,7 @@ import createDebug from 'debug';
import { ActiveEvent, Announcements, CurrentUser, Event, Friend, Presence, PresencePermissions, User, WebService, WebServiceToken, ZncStatus, ZncSuccessResponse } from './znc-types.js';
import { ErrorResponse } from './util.js';
import ZncApi from './znc.js';
import { version } from '../util.js';
import { version } from '../util/product.js';
import { NintendoAccountUser } from './na.js';
import { SavedToken } from '../common/auth/nso.js';

View File

@ -5,7 +5,7 @@ import { flapg, FlapgIid, genfc } from './f.js';
import { AccountLogin, AccountToken, Announcements, CurrentUser, CurrentUserPermissions, Event, Friends, GetActiveEventResult, PresencePermissions, User, WebServices, WebServiceToken, ZncResponse, ZncStatus } from './znc-types.js';
import { getNintendoAccountToken, getNintendoAccountUser, NintendoAccountUser } from './na.js';
import { ErrorResponse } from './util.js';
import { JwtPayload } from '../util.js';
import { JwtPayload } from '../util/jwt.js';
const debug = createDebug('nxapi:api:znc');

View File

@ -1,7 +1,7 @@
Electron app
---
The Electron app is bundled into ~4 files in `dist/app/bundle` using Rollup. The main process code is not bundled for development, but is when packaging the app at `dist/app-main-bundle.cjs` (with the command line executable at `dist/cli-bundle.js`).
The Electron app is bundled into ~4 files in `dist/app/bundle` using Rollup. The main process code is not bundled for development, but is when packaging the app at `dist/bundle/app-main-bundle.cjs` (with the command line executable at `dist/bundle/cli-bundle.js`).
[electron.ts](electron.ts) exports all Electron APIs used in the main process. This is because the `electron` module doesn't actually exist - Electron patches the `require` function, but not the module importer. Additionally Electron does not support using a standard JavaScript module as the app entrypoint, so [main/app-entry.cts](main/app-entry.cts) is used to import the actual app entrypoint after the `ready` event.

View File

@ -1,4 +1,4 @@
import { app, BrowserWindow, dialog, ipcMain, nativeImage, Notification } from '../electron.js';
import { app, BrowserWindow, dialog, ipcMain, nativeImage, Notification } from './electron.js';
import * as path from 'path';
import { EventEmitter } from 'events';
import createDebug from 'debug';
@ -6,7 +6,6 @@ import * as persist from 'node-persist';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import dotenvExpand from 'dotenv-expand';
import { dir, initStorage, LoopResult, paths } from '../../util.js';
import MenuApp from './menu.js';
import { WebServiceIpc } from './webservices.js';
import { createWindow, getWindowConfiguration } from './windows.js';
@ -16,6 +15,9 @@ import { ErrorResponse } from '../../api/util.js';
import { ZncDiscordPresence } from '../../common/presence.js';
import { NotificationManager } from '../../common/notify.js';
import { getToken } from '../../common/auth/nso.js';
import { dir } from '../../util/product.js';
import { initStorage, paths } from '../../util/storage.js';
import { LoopResult } from '../../util/loop.js';
const debug = createDebug('app:main');

View File

@ -1,4 +1,4 @@
import { app, dialog, Menu, Tray, nativeImage, MenuItem } from '../electron.js';
import { app, dialog, Menu, Tray, nativeImage, MenuItem } from './electron.js';
import createDebug from 'debug';
import { addNsoAccount, addPctlAccount } from './na-auth.js';
import { PresenceMonitorManager, Store } from './index.js';
@ -6,7 +6,7 @@ import { WebService } from '../../api/znc-types.js';
import openWebService from './webservices.js';
import { getToken, SavedToken } from '../../common/auth/nso.js';
import { SavedMoonToken } from '../../common/auth/moon.js';
import { dev } from '../../util.js';
import { dev } from '../../util/product.js';
const debug = createDebug('app:main:menu');

View File

@ -2,13 +2,13 @@ import * as crypto from 'crypto';
import createDebug from 'debug';
import * as persist from 'node-persist';
import fetch from 'node-fetch';
import { BrowserWindow, nativeImage, Notification, session, shell } from '../electron.js';
import { BrowserWindow, nativeImage, Notification, session, shell } from './electron.js';
import { getNintendoAccountSessionToken, NintendoAccountSessionToken } from '../../api/na.js';
import { Jwt } from '../../util.js';
import { ZNCA_CLIENT_ID } from '../../api/znc.js';
import { ZNMA_CLIENT_ID } from '../../api/moon.js';
import { getToken, SavedToken } from '../../common/auth/nso.js';
import { getPctlToken, SavedMoonToken } from '../../common/auth/moon.js';
import { Jwt } from '../../util/jwt.js';
const debug = createDebug('app:main:na-auth');

View File

@ -1,8 +1,8 @@
import * as path from 'path';
import createDebug from 'debug';
import { BrowserWindow, IpcMainInvokeEvent, session, shell, WebContents } from '../electron.js';
import { BrowserWindow, IpcMainInvokeEvent, session, shell, WebContents } from './electron.js';
import ZncApi from '../../api/znc.js';
import { dev } from '../../util.js';
import { dev } from '../../util/product.js';
import { WebService } from '../../api/znc-types.js';
import { bundlepath, Store } from './index.js';
import type { NativeShareRequest, NativeShareUrlRequest } from '../preload-webservice/znca-js-api.js';

View File

@ -1,7 +1,7 @@
import { app, BrowserWindow, dialog, ipcMain, nativeImage, Notification, WebContents } from '../electron.js';
import { BrowserWindow, WebContents } from './electron.js';
import * as path from 'path';
import { bundlepath } from './index.js';
import { dev } from '../../util.js';
import { dev } from '../../util/product.js';
import { WindowConfiguration, WindowType } from '../common/types.js';
const windows = new Map<WebContents, WindowConfiguration>();

View File

@ -3,9 +3,11 @@ import createDebug from 'debug';
import Yargs from 'yargs';
import dotenv from 'dotenv';
import dotenvExpand from 'dotenv-expand';
import { dev, paths, YargsArguments } from './util.js';
import * as commands from './cli/index.js';
import { checkUpdates } from './cli/update.js';
import { dev } from './util/product.js';
import { paths } from './util/storage.js';
import { YargsArguments } from './util/yargs.js';
const debug = createDebug('cli');

View File

@ -6,10 +6,12 @@ import frida, { Session } from 'frida';
import express from 'express';
import bodyParser from 'body-parser';
import type { Arguments as ParentArguments } from '../cli.js';
import { ArgumentsCamelCase, Argv, dir, getJwks, initStorage, YargsArguments } from '../util.js';
import { Jwt } from '../util.js';
import { NintendoAccountIdTokenJwtPayload } from '../api/na.js';
import { ZNCA_CLIENT_ID, ZncJwtPayload } from '../api/znc.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../util/yargs.js';
import { dir } from '../util/product.js';
import { initStorage } from '../util/storage.js';
import { getJwks, Jwt } from '../util/jwt.js';
const debug = createDebug('cli:android-znca-api-server-frida');
const debugApi = createDebug('cli:android-znca-api-server-frida:api');

View File

@ -2,7 +2,7 @@ import { createRequire } from 'module';
import { execFileSync } from 'child_process';
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../cli.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../util/yargs.js';
const debug = createDebug('cli:app');

View File

@ -1,6 +1,6 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../cli.js';
import { Argv, YargsArguments } from '../util.js';
import { Argv, YargsArguments } from '../util/yargs.js';
import * as commands from './nooklink/index.js';
const debug = createDebug('cli:nooklink');

View File

@ -3,7 +3,8 @@ import * as path from 'path';
import createDebug from 'debug';
import mkdirp from 'mkdirp';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:dump-newspapers');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken, getWebServiceToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:island');

View File

@ -1,7 +1,8 @@
import { promisify } from 'util';
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:keyboard');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:newspaper');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:newspapers');

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:post-reaction');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:reactions');

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getUserToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:user');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nooklink.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getWebServiceToken } from '../../common/auth/nooklink.js';
const debug = createDebug('cli:nooklink:users');

View File

@ -1,6 +1,6 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../cli.js';
import { Argv, YargsArguments } from '../util.js';
import { Argv, YargsArguments } from '../util/yargs.js';
import * as commands from './nso/index.js';
const debug = createDebug('cli:nso');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:nso:announcements');

View File

@ -2,7 +2,8 @@ import * as util from 'util';
import createDebug from 'debug';
import * as crypto from 'crypto';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
import { getNintendoAccountSessionToken } from '../../api/na.js';
import { ZNCA_CLIENT_ID } from '../../api/znc.js';

View File

@ -2,7 +2,9 @@ import createDebug from 'debug';
import Table from '../util/table.js';
import { PresenceState } from '../../api/znc-types.js';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, hrduration, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { hrduration } from '../../util/misc.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:nso:friends');

View File

@ -6,7 +6,8 @@ import { v4 as uuidgen } from 'uuid';
import { Announcement, CurrentUser, Friend, GetActiveEventResult, Presence, WebService } from '../../api/znc-types.js';
import ZncApi from '../../api/znc.js';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken, SavedToken } from '../../common/auth/nso.js';
import { NotificationManager, ZncNotifications } from '../../common/notify.js';

View File

@ -3,7 +3,8 @@ import createDebug from 'debug';
import persist from 'node-persist';
import notifier from 'node-notifier';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
import { EmbeddedSplatNet2Monitor, NotificationManager, ZncNotifications } from '../../common/notify.js';

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import { PresencePermissions } from '../../api/znc-types.js';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:nso:permissions');

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
import { DiscordPresencePlayTime } from '../../discord/util.js';
import { handleEnableSplatNet2Monitoring } from './notify.js';

View File

@ -1,7 +1,8 @@
import * as util from 'util';
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:nso:token');

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:nso:user');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:nso:webservices');

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../nso.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:nso:webservicetoken');

View File

@ -2,9 +2,10 @@ import createDebug from 'debug';
import fetch from 'node-fetch';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../nso.js';
import { Argv, initStorage } from '../../util.js';
import { getToken } from '../../common/auth/nso.js';
import { AuthPolicy, AuthToken } from './http-server.js';
import { Argv } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
const debug = createDebug('cli:nso:znc-proxy-tokens');

View File

@ -1,6 +1,6 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../cli.js';
import { Argv, YargsArguments } from '../util.js';
import { Argv, YargsArguments } from '../util/yargs.js';
import * as commands from './pctl/index.js';
const debug = createDebug('cli:pctl');

View File

@ -2,7 +2,8 @@ import * as util from 'util';
import * as crypto from 'crypto';
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getPctlToken } from '../../common/auth/moon.js';
import { getNintendoAccountSessionToken } from '../../api/na.js';
import { ZNMA_CLIENT_ID } from '../../api/moon.js';

View File

@ -1,7 +1,9 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, hrduration, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { hrduration } from '../../util/misc.js';
import { getPctlToken } from '../../common/auth/moon.js';
const debug = createDebug('cli:pctl:daily-summaries');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getPctlToken } from '../../common/auth/moon.js';
const debug = createDebug('cli:pctl:devices');

View File

@ -3,7 +3,8 @@ import * as fs from 'fs/promises';
import createDebug from 'debug';
import mkdirp from 'mkdirp';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getPctlToken } from '../../common/auth/moon.js';
import { DailySummaryResult } from '../../api/moon-types.js';
import MoonApi from '../../api/moon.js';

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getPctlToken } from '../../common/auth/moon.js';
const debug = createDebug('cli:pctl:monthly-summaries');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getPctlToken } from '../../common/auth/moon.js';
const debug = createDebug('cli:pctl:monthly-summary');

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getPctlToken } from '../../common/auth/moon.js';
const debug = createDebug('cli:pctl:settings');

View File

@ -1,7 +1,8 @@
import * as util from 'util';
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getPctlToken } from '../../common/auth/moon.js';
const debug = createDebug('cli:pctl:token');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../pctl.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { getPctlToken } from '../../common/auth/moon.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
const debug = createDebug('cli:pctl:user');

View File

@ -1,6 +1,6 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../cli.js';
import { Argv, YargsArguments } from '../util.js';
import { Argv, YargsArguments } from '../util/yargs.js';
import * as commands from './splatnet2/index.js';
const debug = createDebug('cli:splatnet2');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
const debug = createDebug('cli:splatnet2:battles');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
const debug = createDebug('cli:splatnet2:challenges');

View File

@ -3,7 +3,8 @@ import * as fs from 'fs/promises';
import createDebug from 'debug';
import mkdirp from 'mkdirp';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
import { dumpChallenges, dumpHeroRecords, dumpProfileImage, dumpRecords } from '../../common/splatnet2/dump-records.js';

View File

@ -2,7 +2,8 @@ import * as path from 'path';
import createDebug from 'debug';
import mkdirp from 'mkdirp';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
import { dumpCoopResults, dumpResults } from '../../common/splatnet2/dump-results.js';

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
const debug = createDebug('cli:splatnet2:hero');

View File

@ -1,7 +1,8 @@
import * as path from 'path';
import createDebug from 'debug';
import { getIksmToken } from '../../common/auth/splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { Arguments as ParentArguments } from '../splatnet2.js';
import { SplatNet2RecordsMonitor } from '../../common/splatnet2/monitor.js';

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
const debug = createDebug('cli:splatnet2:schedule');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
const debug = createDebug('cli:splatnet2:stages');

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
const debug = createDebug('cli:splatnet2:user');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from '../util/table.js';
import type { Arguments as ParentArguments } from '../splatnet2.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getIksmToken } from '../../common/auth/splatnet2.js';
const debug = createDebug('cli:splatnet2:weapons');

View File

@ -3,7 +3,8 @@ import * as fs from 'fs/promises';
import fetch from 'node-fetch';
import createDebug from 'debug';
import mkdirp from 'mkdirp';
import { dir, paths, version } from '../util.js';
import { dir, version } from '../util/product.js';
import { paths } from '../util/storage.js';
const debug = createDebug('cli:update');

View File

@ -1,7 +1,8 @@
import createDebug from 'debug';
import Table from './util/table.js';
import type { Arguments as ParentArguments } from '../cli.js';
import { Argv, initStorage } from '../util.js';
import { Argv } from '../util/yargs.js';
import { initStorage } from '../util/storage.js';
import { SavedToken } from '../common/auth/nso.js';
import { SavedMoonToken } from '../common/auth/moon.js';

View File

@ -1,6 +1,7 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../cli.js';
import { Argv, dev, YargsArguments } from '../util.js';
import { Argv, YargsArguments } from '../util/yargs.js';
import { dev } from '../util/product.js';
import * as commands from './util/index.js';
const debug = createDebug('cli:util');

View File

@ -2,7 +2,7 @@ import * as crypto from 'crypto';
import { Buffer } from 'buffer';
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../util.js';
import { Argv } from '../../util.js';
import { Argv } from '../../util/yargs.js';
const debug = createDebug('cli:util:captureid');

View File

@ -4,7 +4,8 @@ import { getPresenceFromUrl } from '../../api/znc-proxy.js';
import { ActiveEvent, CurrentUser, Friend, Game, Presence, PresenceState } from '../../api/znc-types.js';
import type { Arguments as ParentArguments } from '../util.js';
import { DiscordPresenceContext, DiscordPresencePlayTime, getDiscordPresence, getInactiveDiscordPresence } from '../../discord/util.js';
import { ArgumentsCamelCase, Argv, initStorage, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { initStorage } from '../../util/storage.js';
import { getToken } from '../../common/auth/nso.js';
const debug = createDebug('cli:util:discord-activity');

View File

@ -2,7 +2,7 @@ import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../util.js';
import { DiscordRpcClient, getAllIpcSockets } from '../../discord/rpc.js';
import { defaultTitle } from '../../discord/titles.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
const debug = createDebug('cli:util:discord-rpc');
debug.enabled = true;

View File

@ -1,7 +1,7 @@
import createDebug from 'debug';
import fetch from 'node-fetch';
import type { Arguments as ParentArguments } from '../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util.js';
import { ArgumentsCamelCase, Argv, YargsArguments } from '../../util/yargs.js';
import { titles as unsorted_titles } from '../../discord/titles.js';
import { DiscordApplicationRpc, getDiscordApplicationRpc } from './discord-activity.js';
import { Title } from '../../discord/util.js';

View File

@ -1,6 +1,6 @@
import createDebug from 'debug';
import type { Arguments as ParentArguments } from '../util.js';
import { ArgumentsCamelCase } from '../../util.js';
import { ArgumentsCamelCase } from '../../util/yargs.js';
import * as publishers from '../../discord/titles/index.js';
const debug = createDebug('cli:util:validate-discord-titles');

View File

@ -2,7 +2,7 @@ import createDebug from 'debug';
import * as persist from 'node-persist';
import { ZNMA_CLIENT_ID } from '../../api/moon.js';
import { NintendoAccountSessionTokenJwtPayload, NintendoAccountToken, NintendoAccountUser } from '../../api/na.js';
import { Jwt } from '../../util.js';
import { Jwt } from '../../util/jwt.js';
import MoonApi from '../../api/moon.js';
const debug = createDebug('nxapi:auth:moon');

View File

@ -2,7 +2,7 @@ import createDebug from 'debug';
import * as persist from 'node-persist';
import { FlapgApiResponse } from '../../api/f.js';
import { NintendoAccountSessionTokenJwtPayload, NintendoAccountToken, NintendoAccountUser } from '../../api/na.js';
import { Jwt } from '../../util.js';
import { Jwt } from '../../util/jwt.js';
import { AccountLogin } from '../../api/znc-types.js';
import ZncApi, { ZNCA_CLIENT_ID } from '../../api/znc.js';
import ZncProxyApi from '../../api/znc-proxy.js';

View File

@ -1,12 +1,13 @@
import createDebug from 'debug';
import persist from 'node-persist';
import { getTitleIdFromEcUrl, hrduration, Loop, LoopResult } from '../util.js';
import ZncApi from '../api/znc.js';
import { ActiveEvent, Announcements, CurrentUser, Friend, Game, Presence, PresenceState, WebServices, ZncErrorResponse } from '../api/znc-types.js';
import ZncProxyApi from '../api/znc-proxy.js';
import { ErrorResponse } from '../api/util.js';
import { SavedToken } from './auth/nso.js';
import { SplatNet2RecordsMonitor } from './splatnet2/monitor.js';
import Loop, { LoopResult } from '../util/loop.js';
import { getTitleIdFromEcUrl, hrduration } from '../util/misc.js';
const debug = createDebug('nxapi:nso:notify');
const debugFriends = createDebug('nxapi:nso:notify:friends');

View File

@ -2,10 +2,10 @@ import createDebug from 'debug';
import { DiscordRpcClient, findDiscordRpcClient } from '../discord/rpc.js';
import { DiscordPresencePlayTime, DiscordPresenceContext, getDiscordPresence, getInactiveDiscordPresence } from '../discord/util.js';
import { ZncNotifications } from './notify.js';
import { LoopResult } from '../util.js';
import { getPresenceFromUrl } from '../api/znc-proxy.js';
import { ActiveEvent, CurrentUser, Friend, Presence, PresenceState, ZncErrorResponse } from '../api/znc-types.js';
import { ErrorResponse } from '../api/util.js';
import { LoopResult } from '../util/loop.js';
const debug = createDebug('nxapi:nso:presence');
const debugProxy = createDebug('nxapi:nso:presence:proxy');

View File

@ -5,11 +5,11 @@ import persist from 'node-persist';
import mkdirp from 'mkdirp';
import SplatNet2Api from '../../api/splatnet2.js';
import { renewIksmToken } from '../auth/splatnet2.js';
import { Loop, LoopResult } from '../../util.js';
import { Records, Stages, WebServiceError } from '../../api/splatnet2-types.js';
import { dumpCoopResults, dumpResults } from './dump-results.js';
import { dumpProfileImage, dumpRecords } from './dump-records.js';
import { ErrorResponse } from '../../api/util.js';
import Loop, { LoopResult } from '../../util/loop.js';
const debug = createDebug('nxapi:splatnet2:monitor');

View File

@ -1,7 +1,8 @@
import DiscordRPC from 'discord-rpc';
import { ActiveEvent, CurrentUser, Friend, Game, PresenceState } from '../api/znc-types.js';
import { defaultTitle, titles } from './titles.js';
import { dev, getTitleIdFromEcUrl, git, hrduration, version } from '../util.js';
import { dev, git, version } from '../util/product.js';
import { getTitleIdFromEcUrl, hrduration } from '../util/misc.js';
import { ZncDiscordPresence } from '../common/presence.js';
const product = 'nxapi ' + version +

View File

@ -21,5 +21,5 @@ export {
} from './api/nooklink.js';
export * as nooklink from './api/nooklink-types.js';
export { getTitleIdFromEcUrl } from './util.js';
export { getTitleIdFromEcUrl } from './util/misc.js';
export { ErrorResponse } from './api/util.js';

View File

@ -1,313 +0,0 @@
import * as path from 'path';
import * as fs from 'fs';
import * as child_process from 'child_process';
import * as crypto from 'crypto';
import * as yargs from 'yargs';
import createDebug from 'debug';
import persist from 'node-persist';
import getPaths from 'env-paths';
import fetch from 'node-fetch';
const debug = createDebug('nxapi:util');
export const paths = getPaths('nxapi');
//
// Package/version info
//
export const dir = path.resolve(decodeURI(import.meta.url.substr(process.platform === 'win32' ? 8 : 7)), '..', '..');
export const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
export const version = pkg.version;
export const git = (() => {
try {
fs.statSync(path.join(dir, '.git'));
} catch (err) {
return null;
}
const options: child_process.ExecSyncOptions = {cwd: dir};
const revision = child_process.execSync('git rev-parse HEAD', options).toString().trim();
const branch = child_process.execSync('git rev-parse --abbrev-ref HEAD', options).toString().trim();
const changed_files = child_process.execSync('git diff --name-only HEAD', options).toString().trim();
return {
revision,
branch: branch && branch !== 'HEAD' ? branch : null,
changed_files: changed_files.length ? changed_files.split('\n') : [],
};
})();
export const dev = !!git || process.env.NODE_ENV === 'development';
//
// Yargs types
//
export type YargsArguments<T extends yargs.Argv> = T extends yargs.Argv<infer R> ? R : any;
export type Argv<T = {}> = yargs.Argv<T>;
// export type ArgumentsCamelCase<T = {}> = yargstypes.ArgumentsCamelCase<T>;
/** Convert literal string types like 'foo-bar' to 'FooBar' */
type PascalCase<S extends string> = string extends S ?
string : S extends `${infer T}-${infer U}` ?
`${Capitalize<T>}${PascalCase<U>}` : Capitalize<S>;
/** Convert literal string types like 'foo-bar' to 'fooBar' */
type CamelCase<S extends string> = string extends S ?
string : S extends `${infer T}-${infer U}` ?
`${T}${PascalCase<U>}` : S;
/** Convert literal string types like 'foo-bar' to 'fooBar', allowing all `PropertyKey` types */
type CamelCaseKey<K extends PropertyKey> = K extends string ? Exclude<CamelCase<K>, ''> : K;
/** Arguments type, with camelcased keys */
export type ArgumentsCamelCase<T = {}> = { [key in keyof T as key | CamelCaseKey<key>]: T[key] } & {
/** Non-option arguments */
_: Array<string | number>;
/** The script name or node command */
$0: string;
/** All remaining options */
[argName: string]: unknown;
};
//
// Other
//
export async function initStorage(dir: string) {
const storage = persist.create({
dir: path.join(dir, 'persist'),
stringify: data => JSON.stringify(data, null, 4) + '\n',
});
await storage.init();
return storage;
}
export function getTitleIdFromEcUrl(url: string) {
const match = url.match(/^https:\/\/ec\.nintendo\.com\/apps\/([0-9a-f]{16})\//);
return match?.[1] ?? null;
}
export function hrduration(duration: number, short = false) {
const hours = Math.floor(duration / 60);
const minutes = duration - (hours * 60);
const hour_str = short ? 'hr' : 'hour';
const minute_str = short ? 'min' : 'minute';
if (hours >= 1) {
return hours + ' ' + hour_str + (hours === 1 ? '' : 's') +
(minutes ? ', ' + minutes + ' ' + minute_str + (minutes === 1 ? '' : 's') : '');
} else {
return minutes + ' ' + minute_str + (minutes === 1 ? '' : 's');
}
}
export abstract class Loop {
update_interval = 60;
init(): void | Promise<LoopResult | void> {}
abstract update(): void | Promise<LoopResult | void>;
protected async loopRun(init = false): Promise<LoopResult> {
try {
const result = init ? await this.init() : await this.update();
return result ?? (init ? LoopResult.OK_SKIP_INTERVAL : LoopResult.OK);
} catch (err) {
return this.handleError(err as any);
}
}
async handleError(err: Error): Promise<LoopResult> {
throw err;
}
private is_loop_active = 0;
async loop(init = false) {
try {
this.is_loop_active++;
const result = await this.loopRun(init);
if (result === LoopResult.OK) {
if (this.skip_interval_once) {
this.skip_interval_once = false;
} else {
await new Promise(rs => setTimeout(this.timeout_resolve = rs, this.update_interval * 1000));
}
}
} finally {
this.is_loop_active--;
this.skip_interval_once = false;
this.timeout_resolve = null;
}
}
private skip_interval_once = false;
private timeout_resolve: ((value: void) => void) | null = null;
skipIntervalInCurrentLoop() {
debug('Skip update interval', this.is_loop_active);
if (!this.is_loop_active) return;
this.skip_interval_once = true;
this.timeout_resolve?.call(null);
}
}
const LoopRunOk = Symbol('LoopRunOk');
const LoopRunOkSkipInterval = Symbol('LoopRunOkSkipInterval');
export enum LoopResult {
OK = LoopRunOk as any,
OK_SKIP_INTERVAL = LoopRunOkSkipInterval as any,
}
//
// JSON Web Tokens
//
export interface JwtHeader {
typ?: 'JWT';
alg: JwtAlgorithm;
/** Key ID */
kid?: string;
/** JSON Web Key Set URL */
jku?: string;
}
export enum JwtAlgorithm {
RS256 = 'RS256',
}
export interface JwtPayload {
/** Audience */
aud: string;
/** Expiration timestamp (seconds) */
exp: number;
/** Issue timestamp (seconds) */
iat: number;
/** Issuer */
iss: string;
/** Token ID */
jti: string;
/** Subject */
sub: string | number;
/** Token type */
typ: string;
}
type JwtVerifier = (data: Buffer, signature: Buffer, key: string) => boolean;
export class Jwt<T = JwtPayload, H extends JwtHeader = JwtHeader> {
constructor(
readonly header: H,
readonly payload: T
) {}
static decode<T = JwtPayload, H extends JwtHeader = JwtHeader>(token: string) {
const [header_str, payload_str, signature_str] = token.split('.', 3);
const header = JSON.parse(Buffer.from(header_str, 'base64url').toString());
const payload = JSON.parse(Buffer.from(payload_str, 'base64url').toString());
const signature = Buffer.from(signature_str, 'base64url');
if ('typ' in header && header.typ !== 'JWT') {
throw new Error('Invalid JWT');
}
const jwt = new this<T, H>(header, payload);
return [jwt, signature] as const;
}
verify(signature: Buffer, key: string, verifier?: JwtVerifier) {
const header_str = Buffer.from(JSON.stringify(this.header)).toString('base64url');
const payload_str = Buffer.from(JSON.stringify(this.payload)).toString('base64url');
const sign_data = header_str + '.' + payload_str;
if (!verifier) {
if (!(this.header.alg in Jwt.verifiers) || !Jwt.verifiers[this.header.alg]) {
throw new Error('Unknown algorithm');
}
verifier = Jwt.verifiers[this.header.alg];
}
return verifier.call(null, Buffer.from(sign_data), signature, key);
}
static verifiers: Record<JwtAlgorithm, JwtVerifier> = {
[JwtAlgorithm.RS256]: (data, signature, key) => {
const verify = crypto.createVerify('RSA-SHA256');
verify.end(data);
return verify.verify(key, signature);
},
};
}
//
// JSON Web Key Sets
//
// Used for verifying JSON Web Tokens
//
export interface Jwks {
keys: Jwk[];
}
export interface Jwk {
/** Key type */
kty: string;
use?: JwkUse | string;
key_ops?: JwkKeyOperation | string;
alg?: JwtAlgorithm | string;
/** Key ID */
kid?: string;
x5u?: string[];
x5c?: string[];
x5t?: string;
'x5t#S256'?: string;
}
export enum JwkUse {
SIGNATURE = 'sig',
ENCRYPTION = 'enc',
}
export enum JwkKeyOperation {
SIGN = 'sign',
VERIFY = 'verify',
ENCRYPT = 'encrypt',
DECRYPT = 'decrypt',
WRAP_KEY = 'wrapKey',
UNWRAP_KEY = 'unwrapKey',
DERIVE_KEY = 'deriveKey',
DERIVE_BITS = 'deriveBits',
}
interface SavedJwks {
jwks: Jwks;
expires_at: number;
}
export async function getJwks(url: string, storage?: persist.LocalStorage) {
const cached_keyset: SavedJwks | undefined = await storage?.getItem('Jwks.' + url);
if (!cached_keyset || cached_keyset.expires_at <= Date.now()) {
debug('Downloading JSON Web Key Set from %s', url);
const response = await fetch(url);
const jwks = await response.json() as Jwks;
const cached_keyset: SavedJwks = {
jwks,
expires_at: Date.now() + (1 * 60 * 60 * 1000), // 1 hour
};
await storage?.setItem('Jwks.' + url, cached_keyset);
return jwks;
}
return cached_keyset.jwks;
}

152
src/util/jwt.ts Normal file
View File

@ -0,0 +1,152 @@
import * as crypto from 'crypto';
import createDebug from 'debug';
import persist from 'node-persist';
import fetch from 'node-fetch';
const debug = createDebug('nxapi:util:jwt');
//
// JSON Web Tokens
//
export interface JwtHeader {
typ?: 'JWT';
alg: JwtAlgorithm;
/** Key ID */
kid?: string;
/** JSON Web Key Set URL */
jku?: string;
}
export enum JwtAlgorithm {
RS256 = 'RS256',
}
export interface JwtPayload {
/** Audience */
aud: string;
/** Expiration timestamp (seconds) */
exp: number;
/** Issue timestamp (seconds) */
iat: number;
/** Issuer */
iss: string;
/** Token ID */
jti: string;
/** Subject */
sub: string | number;
/** Token type */
typ: string;
}
type JwtVerifier = (data: Buffer, signature: Buffer, key: string) => boolean;
export class Jwt<T = JwtPayload, H extends JwtHeader = JwtHeader> {
constructor(
readonly header: H,
readonly payload: T
) {}
static decode<T = JwtPayload, H extends JwtHeader = JwtHeader>(token: string) {
const [header_str, payload_str, signature_str] = token.split('.', 3);
const header = JSON.parse(Buffer.from(header_str, 'base64url').toString());
const payload = JSON.parse(Buffer.from(payload_str, 'base64url').toString());
const signature = Buffer.from(signature_str, 'base64url');
if ('typ' in header && header.typ !== 'JWT') {
throw new Error('Invalid JWT');
}
const jwt = new this<T, H>(header, payload);
return [jwt, signature] as const;
}
verify(signature: Buffer, key: string, verifier?: JwtVerifier) {
const header_str = Buffer.from(JSON.stringify(this.header)).toString('base64url');
const payload_str = Buffer.from(JSON.stringify(this.payload)).toString('base64url');
const sign_data = header_str + '.' + payload_str;
if (!verifier) {
if (!(this.header.alg in Jwt.verifiers) || !Jwt.verifiers[this.header.alg]) {
throw new Error('Unknown algorithm');
}
verifier = Jwt.verifiers[this.header.alg];
}
return verifier.call(null, Buffer.from(sign_data), signature, key);
}
static verifiers: Record<JwtAlgorithm, JwtVerifier> = {
[JwtAlgorithm.RS256]: (data, signature, key) => {
const verify = crypto.createVerify('RSA-SHA256');
verify.end(data);
return verify.verify(key, signature);
},
};
}
//
// JSON Web Key Sets
//
// Used for verifying JSON Web Tokens
//
export interface Jwks {
keys: Jwk[];
}
export interface Jwk {
/** Key type */
kty: string;
use?: JwkUse | string;
key_ops?: JwkKeyOperation | string;
alg?: JwtAlgorithm | string;
/** Key ID */
kid?: string;
x5u?: string[];
x5c?: string[];
x5t?: string;
'x5t#S256'?: string;
}
export enum JwkUse {
SIGNATURE = 'sig',
ENCRYPTION = 'enc',
}
export enum JwkKeyOperation {
SIGN = 'sign',
VERIFY = 'verify',
ENCRYPT = 'encrypt',
DECRYPT = 'decrypt',
WRAP_KEY = 'wrapKey',
UNWRAP_KEY = 'unwrapKey',
DERIVE_KEY = 'deriveKey',
DERIVE_BITS = 'deriveBits',
}
interface SavedJwks {
jwks: Jwks;
expires_at: number;
}
export async function getJwks(url: string, storage?: persist.LocalStorage) {
const cached_keyset: SavedJwks | undefined = await storage?.getItem('Jwks.' + url);
if (!cached_keyset || cached_keyset.expires_at <= Date.now()) {
debug('Downloading JSON Web Key Set from %s', url);
const response = await fetch(url);
const jwks = await response.json() as Jwks;
const cached_keyset: SavedJwks = {
jwks,
expires_at: Date.now() + (1 * 60 * 60 * 1000), // 1 hour
};
await storage?.setItem('Jwks.' + url, cached_keyset);
return jwks;
}
return cached_keyset.jwks;
}

66
src/util/loop.ts Normal file
View File

@ -0,0 +1,66 @@
import createDebug from 'debug';
const debug = createDebug('nxapi:util:loop');
export default abstract class Loop {
update_interval = 60;
init(): void | Promise<LoopResult | void> {}
abstract update(): void | Promise<LoopResult | void>;
protected async loopRun(init = false): Promise<LoopResult> {
try {
const result = init ? await this.init() : await this.update();
return result ?? (init ? LoopResult.OK_SKIP_INTERVAL : LoopResult.OK);
} catch (err) {
return this.handleError(err as any);
}
}
async handleError(err: Error): Promise<LoopResult> {
throw err;
}
private is_loop_active = 0;
async loop(init = false) {
try {
this.is_loop_active++;
const result = await this.loopRun(init);
if (result === LoopResult.OK) {
if (this.skip_interval_once) {
this.skip_interval_once = false;
} else {
await new Promise(rs => setTimeout(this.timeout_resolve = rs, this.update_interval * 1000));
}
}
} finally {
this.is_loop_active--;
this.skip_interval_once = false;
this.timeout_resolve = null;
}
}
private skip_interval_once = false;
private timeout_resolve: ((value: void) => void) | null = null;
skipIntervalInCurrentLoop() {
debug('Skip update interval', this.is_loop_active);
if (!this.is_loop_active) return;
this.skip_interval_once = true;
this.timeout_resolve?.call(null);
}
}
const LoopRunOk = Symbol('LoopRunOk');
const LoopRunOkSkipInterval = Symbol('LoopRunOkSkipInterval');
export enum LoopResult {
OK = LoopRunOk as any,
OK_SKIP_INTERVAL = LoopRunOkSkipInterval as any,
}

20
src/util/misc.ts Normal file
View File

@ -0,0 +1,20 @@
export function getTitleIdFromEcUrl(url: string) {
const match = url.match(/^https:\/\/ec\.nintendo\.com\/apps\/([0-9a-f]{16})\//);
return match?.[1] ?? null;
}
export function hrduration(duration: number, short = false) {
const hours = Math.floor(duration / 60);
const minutes = duration - (hours * 60);
const hour_str = short ? 'hr' : 'hour';
const minute_str = short ? 'min' : 'minute';
if (hours >= 1) {
return hours + ' ' + hour_str + (hours === 1 ? '' : 's') +
(minutes ? ', ' + minutes + ' ' + minute_str + (minutes === 1 ? '' : 's') : '');
} else {
return minutes + ' ' + minute_str + (minutes === 1 ? '' : 's');
}
}

34
src/util/product.ts Normal file
View File

@ -0,0 +1,34 @@
import * as path from 'path';
import { fileURLToPath } from 'url';
import * as fs from 'fs';
import * as child_process from 'child_process';
import createDebug from 'debug';
const debug = createDebug('nxapi:util:product');
//
// Package/version info
//
export const dir = path.resolve(fileURLToPath(import.meta.url), '..', '..', '..');
export const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8'));
export const version = pkg.version;
export const git = (() => {
try {
fs.statSync(path.join(dir, '.git'));
} catch (err) {
return null;
}
const options: child_process.ExecSyncOptions = {cwd: dir};
const revision = child_process.execSync('git rev-parse HEAD', options).toString().trim();
const branch = child_process.execSync('git rev-parse --abbrev-ref HEAD', options).toString().trim();
const changed_files = child_process.execSync('git diff --name-only HEAD', options).toString().trim();
return {
revision,
branch: branch && branch !== 'HEAD' ? branch : null,
changed_files: changed_files.length ? changed_files.split('\n') : [],
};
})();
export const dev = !!git || process.env.NODE_ENV === 'development';

17
src/util/storage.ts Normal file
View File

@ -0,0 +1,17 @@
import * as path from 'path';
import createDebug from 'debug';
import persist from 'node-persist';
import getPaths from 'env-paths';
const debug = createDebug('nxapi:util:storage');
export const paths = getPaths('nxapi');
export async function initStorage(dir: string) {
const storage = persist.create({
dir: path.join(dir, 'persist'),
stringify: data => JSON.stringify(data, null, 4) + '\n',
});
await storage.init();
return storage;
}

32
src/util/yargs.ts Normal file
View File

@ -0,0 +1,32 @@
import * as yargs from 'yargs';
//
// Yargs types
//
export type YargsArguments<T extends yargs.Argv> = T extends yargs.Argv<infer R> ? R : any;
export type Argv<T = {}> = yargs.Argv<T>;
// export type ArgumentsCamelCase<T = {}> = yargstypes.ArgumentsCamelCase<T>;
/** Convert literal string types like 'foo-bar' to 'FooBar' */
type PascalCase<S extends string> = string extends S ?
string : S extends `${infer T}-${infer U}` ?
`${Capitalize<T>}${PascalCase<U>}` : Capitalize<S>;
/** Convert literal string types like 'foo-bar' to 'fooBar' */
type CamelCase<S extends string> = string extends S ?
string : S extends `${infer T}-${infer U}` ?
`${T}${PascalCase<U>}` : S;
/** Convert literal string types like 'foo-bar' to 'fooBar', allowing all `PropertyKey` types */
type CamelCaseKey<K extends PropertyKey> = K extends string ? Exclude<CamelCase<K>, ''> : K;
/** Arguments type, with camelcased keys */
export type ArgumentsCamelCase<T = {}> = { [key in keyof T as key | CamelCaseKey<key>]: T[key] } & {
/** Non-option arguments */
_: Array<string | number>;
/** The script name or node command */
$0: string;
/** All remaining options */
[argName: string]: unknown;
};