diff --git a/common/src/http-auth-fetch.ts b/common/src/http-auth-fetch.ts index df9cdbfd8..64f9c5220 100644 --- a/common/src/http-auth-fetch.ts +++ b/common/src/http-auth-fetch.ts @@ -17,6 +17,8 @@ export interface AuthFetchOptions extends HttpFetchOptions { } function getAuth(options: AuthFetchOptions, method: string) { + if (!options.credential) + return; const { digest, basic } = options.credential; if (digest) { options.credential.count ||= 0; @@ -75,7 +77,7 @@ export async function authHttpFetch(options: AuthFetchOptions, init?: R ignoreStatusCode: true, }, init, StreamParser); - if (initialResponse.statusCode !== 401) { + if (initialResponse.statusCode !== 401 || !options.credential) { if (!options?.ignoreStatusCode) checkStatus(initialResponse.statusCode); return { diff --git a/plugins/snapshot/package-lock.json b/plugins/snapshot/package-lock.json index 3c4e24db9..b886c7333 100644 --- a/plugins/snapshot/package-lock.json +++ b/plugins/snapshot/package-lock.json @@ -8,9 +8,7 @@ "name": "@scrypted/snapshot", "version": "0.2.31", "dependencies": { - "@koush/axios-digest-auth": "^0.8.7", "@types/node": "^20.10.6", - "axios": "^0.21.4", "sharp": "^0.33.1", "whatwg-mimetype": "^4.0.0" }, @@ -30,10 +28,11 @@ "@scrypted/server": "file:../server", "http-auth-utils": "^3.0.2", "node-fetch-commonjs": "^3.1.1", - "typescript": "^4.4.3" + "typescript": "^5.3.3" }, "devDependencies": { - "@types/node": "^16.9.0" + "@types/node": "^20.10.8", + "ts-node": "^10.9.2" } }, "../../sdk": { @@ -514,15 +513,6 @@ "url": "https://opencollective.com/libvips" } }, - "node_modules/@koush/axios-digest-auth": { - "version": "0.8.7", - "resolved": "https://registry.npmjs.org/@koush/axios-digest-auth/-/axios-digest-auth-0.8.7.tgz", - "integrity": "sha512-sZepmWhDt4JUMB1ycX8k9SmDfHeCX+g+pGslrpLORHhEo2vLYFzTjAzL62NFmZO9uG4xmedDn4i0eJW5IK3//Q==", - "dependencies": { - "auth-header": "^1.0.0", - "axios": "^0.21.4" - } - }, "node_modules/@scrypted/common": { "resolved": "../../common", "link": true @@ -545,19 +535,6 @@ "integrity": "sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA==", "dev": true }, - "node_modules/auth-header": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/auth-header/-/auth-header-1.0.0.tgz", - "integrity": "sha512-CPPazq09YVDUNNVWo4oSPTQmtwIzHusZhQmahCKvIsk0/xH6U3QsMAv3sM+7+Q0B1K2KJ/Q38OND317uXs4NHA==" - }, - "node_modules/axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "dependencies": { - "follow-redirects": "^1.14.0" - } - }, "node_modules/color": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/color/-/color-4.2.3.tgz", @@ -603,25 +580,6 @@ "node": ">=8" } }, - "node_modules/follow-redirects": { - "version": "1.15.2", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", - "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, "node_modules/is-arrayish": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", @@ -875,24 +833,16 @@ "integrity": "sha512-XaM69X0n6kTEsp9tVYYLhXdg7Qj32vYJlAKRutxUsm1UlgQNx6BOhHwZPwukCGXBU2+tH87ip2eV1I/E8MQnZg==", "optional": true }, - "@koush/axios-digest-auth": { - "version": "0.8.7", - "resolved": "https://registry.npmjs.org/@koush/axios-digest-auth/-/axios-digest-auth-0.8.7.tgz", - "integrity": "sha512-sZepmWhDt4JUMB1ycX8k9SmDfHeCX+g+pGslrpLORHhEo2vLYFzTjAzL62NFmZO9uG4xmedDn4i0eJW5IK3//Q==", - "requires": { - "auth-header": "^1.0.0", - "axios": "^0.21.4" - } - }, "@scrypted/common": { "version": "file:../../common", "requires": { "@scrypted/sdk": "file:../sdk", "@scrypted/server": "file:../server", - "@types/node": "^16.9.0", + "@types/node": "^20.10.8", "http-auth-utils": "^3.0.2", "node-fetch-commonjs": "^3.1.1", - "typescript": "^4.4.3" + "ts-node": "^10.9.2", + "typescript": "^5.3.3" } }, "@scrypted/sdk": { @@ -933,19 +883,6 @@ "integrity": "sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA==", "dev": true }, - "auth-header": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/auth-header/-/auth-header-1.0.0.tgz", - "integrity": "sha512-CPPazq09YVDUNNVWo4oSPTQmtwIzHusZhQmahCKvIsk0/xH6U3QsMAv3sM+7+Q0B1K2KJ/Q38OND317uXs4NHA==" - }, - "axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "requires": { - "follow-redirects": "^1.14.0" - } - }, "color": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/color/-/color-4.2.3.tgz", @@ -982,11 +919,6 @@ "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz", "integrity": "sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==" }, - "follow-redirects": { - "version": "1.15.2", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", - "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==" - }, "is-arrayish": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", diff --git a/plugins/snapshot/package.json b/plugins/snapshot/package.json index 7c358c535..7bd1b6e2d 100644 --- a/plugins/snapshot/package.json +++ b/plugins/snapshot/package.json @@ -34,9 +34,7 @@ ] }, "dependencies": { - "@koush/axios-digest-auth": "^0.8.7", "@types/node": "^20.10.6", - "axios": "^0.21.4", "sharp": "^0.33.1", "whatwg-mimetype": "^4.0.0" }, diff --git a/plugins/snapshot/src/main.ts b/plugins/snapshot/src/main.ts index 85c1f41ba..98a879e11 100644 --- a/plugins/snapshot/src/main.ts +++ b/plugins/snapshot/src/main.ts @@ -1,18 +1,18 @@ -import AxiosDigestAuth from '@koush/axios-digest-auth'; import { AutoenableMixinProvider } from "@scrypted/common/src/autoenable-mixin-provider"; -import { createMapPromiseDebouncer, RefreshPromise, singletonPromise, TimeoutError } from "@scrypted/common/src/promise-utils"; +import { AuthFetchCredentialState, authHttpFetch } from '@scrypted/common/src/http-auth-fetch'; +import { RefreshPromise, TimeoutError, createMapPromiseDebouncer, singletonPromise } from "@scrypted/common/src/promise-utils"; import { SettingsMixinDeviceBase, SettingsMixinDeviceOptions } from "@scrypted/common/src/settings-mixin"; -import sdk, { BufferConverter, Camera, DeviceManifest, DeviceProvider, FFmpegInput, HttpRequest, HttpRequestHandler, HttpResponse, MediaObject, MediaObjectOptions, MixinProvider, RequestMediaStreamOptions, RequestPictureOptions, ResponsePictureOptions, ScryptedDevice, ScryptedDeviceType, ScryptedInterface, ScryptedMimeTypes, Setting, Settings, SettingValue, VideoCamera } from "@scrypted/sdk"; +import sdk, { BufferConverter, Camera, DeviceManifest, DeviceProvider, FFmpegInput, HttpRequest, HttpRequestHandler, HttpResponse, MediaObject, MediaObjectOptions, MixinProvider, RequestMediaStreamOptions, RequestPictureOptions, ResponsePictureOptions, ScryptedDevice, ScryptedDeviceType, ScryptedInterface, ScryptedMimeTypes, Setting, SettingValue, Settings, VideoCamera } from "@scrypted/sdk"; import { StorageSettings } from "@scrypted/sdk/storage-settings"; -import axios, { AxiosInstance } from "axios"; import https from 'https'; +import os from 'os'; import path from 'path'; import url from 'url'; +import { BufferParser } from "../../../server/src/http-fetch-helpers"; import { ffmpegFilterImage, ffmpegFilterImageBuffer } from './ffmpeg-image-filter'; import { ImageConverter, ImageConverterNativeId } from './image-converter'; import { ImageReader, ImageReaderNativeId, loadSharp, loadVipsImage } from './image-reader'; import { ImageWriter, ImageWriterNativeId } from './image-writer'; -import os from 'os'; const { mediaManager, systemManager } = sdk; if (os.cpus().find(cpu => cpu.model?.toLowerCase().includes('qemu'))) { @@ -217,30 +217,27 @@ class SnapshotMixin extends SettingsMixinDeviceBase implements Camera { password = settings?.find(setting => setting.key === 'password')?.value?.toString(); } - let axiosClient: AxiosDigestAuth | AxiosInstance; + let credential: AuthFetchCredentialState; if (username && password) { - axiosClient = new AxiosDigestAuth({ - username, + credential = { + username, password, - }); - } - else { - axiosClient = axios; + }; } try { - const response = await axiosClient.request({ + const response = await authHttpFetch({ httpsAgent, - method: "GET", - responseType: 'arraybuffer', url: this.storageSettings.values.snapshotUrl, + credential, + }, { timeout: 60000, headers: { 'Accept': 'image/*', } - }); + }, BufferParser); - return response.data; + return response.body; } catch (e) { return retryWithPrebuffer(e);