diff --git a/dist/index.d.ts b/dist/index.d.ts index fcfeeb080e5a685e87e492e07c80f83ce87b33d5..045047e8c253c74004f01f928d161d3ffe163afc 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,4 +1,11 @@ import type { Options } from './types.js'; -declare const updater: ({ name, version, ttl }: Options) => Promise; +declare const updater: ({ + authInfo, + logCallback, + name, + registryUrl, + version, + ttl +}: Options) => Promise; export default updater; export type { Options }; diff --git a/dist/index.js b/dist/index.js index ac3994c9526fa2c751f3bfe7c05006bfe77c50b3..19a43bd23a6b78c81b6dbf87afc150323a954493 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,11 +3,21 @@ import Store from './store.js'; import Utils from './utils.js'; /* MAIN */ //TODO: Account for non-latest releases -const updater = async ({ name, version, ttl = 0 }) => { +const updater = async (options) => { + const { + authInfo, + logCallback, + name, + registryUrl, + version, + ttl = 0, + } = { __proto__: null, ...options }; const record = Store.get(name); const timestamp = Date.now(); const isFresh = !record || (timestamp - record.timestampFetch) >= ttl; - const latest = isFresh ? await Utils.getLatestVersion(name).catch(Utils.noop) : record?.version; + const latest = isFresh + ? await Utils.getLatestVersion(name, { authInfo, registryUrl }).catch(Utils.noop) + : record?.version; if (!latest) return false; if (isFresh) { @@ -18,7 +28,10 @@ const updater = async ({ name, version, ttl = 0 }) => { return false; } if (isFresh) { - Utils.notify(name, version, latest); + const logger = logCallback + ? () => logCallback(name, version, latest) + : () => console.log(`\n\nšŸ“¦ Update available for ${name}: ${version} → ${latest}`); + Utils.notify(logger); } return true; }; diff --git a/dist/types.d.ts b/dist/types.d.ts index 984202ceb64c20d0f4d9c463d32310755b83959c..1a5bd0366130a1d9ba3bf360632132905b725698 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -1,11 +1,31 @@ +type AuthInfo = { + type: string; + token: string; +}; type Options = { + authInfo?: AuthInfo | undefined; + logCallback?: ((name: string, version: string, latest: string) => void) | undefined; name: string; + registryUrl?: string | undefined; version: string; - ttl?: number; + ttl?: number | undefined; }; type StoreRecord = { timestampFetch: number; timestampNotification: number; version: string; }; -export type { Options, StoreRecord }; +type UtilsFetchOptions = { + authInfo?: AuthInfo | undefined; +}; +type UtilsGetLatestVersionOptions = { + authInfo?: AuthInfo | undefined; + registryUrl?: string | undefined; +}; +export type { + AuthInfo, + Options, + StoreRecord, + UtilsFetchOptions, + UtilsGetLatestVersionOptions +}; diff --git a/dist/utils.d.ts b/dist/utils.d.ts index 05ec4d0d4fc6a17f6c80b36cc03cbfe8008585cb..ce9fd88382078dc9c8dc7547a455dddc5057fbe6 100644 --- a/dist/utils.d.ts +++ b/dist/utils.d.ts @@ -1,13 +1,17 @@ +import { UtilsFetchOptions, UtilsGetLatestVersionOptions } from './types'; declare const Utils: { - fetch: (url: string) => Promise<{ + fetch: (url: string, options?: UtilsFetchOptions | undefined) => Promise<{ version?: string; }>; getExitSignal: () => AbortSignal; - getLatestVersion: (name: string) => Promise; + getLatestVersion: ( + name: string, + options?: UtilsGetLatestVersionOptions | undefined + ) => Promise; isNumber: (value: unknown) => value is number; isString: (value: unknown) => value is string; isUpdateAvailable: (current: string, latest: string) => boolean; noop: () => undefined; - notify: (name: string, version: string, latest: string) => void; + notify: (logger: () => void) => void; }; export default Utils; diff --git a/dist/utils.js b/dist/utils.js index d16d8622706ac21b91879479100e164ddaa47201..7df5618c1718f4bb10de9f8942c5a198d4e84853 100644 --- a/dist/utils.js +++ b/dist/utils.js @@ -1,25 +1,37 @@ /* IMPORT */ import colors from 'tiny-colors'; -import whenExit from 'when-exit'; +import signalExit from '@socketsecurity/registry/external/signal-exit'; import compare from './compare.js'; /* MAIN */ const Utils = { /* API */ - fetch: async (url) => { + fetch: async (url, options = {}) => { + const { authInfo } = { __proto__: null, ...options }; + const headers = new Headers({ + 'Accept': 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*' + }); + if (authInfo) { + headers.set('Authorization', `${authInfo.type} ${authInfo.token}`); + } const signal = Utils.getExitSignal(); - const request = await fetch(url, { signal }); + const request = await fetch(url, { headers, signal }); const json = await request.json(); return json; }, getExitSignal: () => { const aborter = new AbortController(); - whenExit(() => aborter.abort()); + signalExit.onExit(() => aborter.abort()); return aborter.signal; }, - getLatestVersion: async (name) => { - const latestUrl = `https://registry.npmjs.org/${name}/latest`; - const latest = await Utils.fetch(latestUrl); - return latest.version; + getLatestVersion: async (name, options = {}) => { + const { + authInfo, + registryUrl = 'https://registry.npmjs.org/', + } = { __proto__: null, ...options }; + const maybeSlash = registryUrl.endsWith('/') ? '' : '/'; + const latestUrl = `${registryUrl}${maybeSlash}${name}/latest`; + const json = await Utils.fetch(latestUrl, { authInfo }); + return json.version; }, isNumber: (value) => { return typeof value === 'number'; @@ -33,11 +45,10 @@ const Utils = { noop: () => { return; }, - notify: (name, version, latest) => { + notify: (logger) => { if (!globalThis.process?.stdout?.isTTY) return; // Probably piping stdout - const log = () => console.log(`\n\nšŸ“¦ Update available for ${colors.cyan(name)}: ${colors.gray(version)} → ${colors.green(latest)}`); - whenExit(log); + signalExit.onExit(logger); } }; /* EXPORT */ diff --git a/package.json b/package.json index 7ea4a2f03b0b479dc76b8b9f65d7573e2b6753b6..c9313e05868bfca8204d5221c24384733655fcad 100755 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "dependencies": { "ionstore": "^1.0.1", "tiny-colors": "^2.2.2", - "when-exit": "^2.1.4" + "@socketsecurity/registry": "^1" }, "devDependencies": { "fava": "^0.3.4",