fix protocol error

This commit is contained in:
Ben Irvin 2023-08-02 10:01:49 +02:00
parent f5107c7236
commit f55ad19141

View File

@ -1,10 +1,15 @@
import { join } from 'path';
import https from 'https';
import http from 'http';
import { Duplex, PassThrough, Readable } from 'stream';
import { stat, createReadStream, ReadStream } from 'fs-extra';
import type { IAsset } from '../../../../types';
const httpMethod = (filepath) => {
return filepath?.startsWith('https') ? https : http;
};
function getFileStream(filepath: string, isLocal = false): PassThrough | ReadStream {
if (isLocal) {
return createReadStream(filepath);
@ -12,7 +17,7 @@ function getFileStream(filepath: string, isLocal = false): PassThrough | ReadStr
const readableStream = new PassThrough();
https
httpMethod(filepath)
.get(filepath, (res) => {
if (res.statusCode !== 200) {
readableStream.emit(
@ -36,7 +41,7 @@ function getFileStats(filepath: string, isLocal = false): Promise<{ size: number
return stat(filepath);
}
return new Promise((resolve, reject) => {
https
httpMethod(filepath)
.get(filepath, (res) => {
if (res.statusCode !== 200) {
reject(new Error(`Request failed with status code ${res.statusCode}`));