🧑🏻‍🔧 Worker Streams

This commit is contained in:
2021-05-10 23:10:22 -07:00
parent 924453b22d
commit 325024afd1
7 changed files with 177 additions and 62 deletions

View File

@@ -1,18 +1,19 @@
import axios from "axios";
import fetch, { Response } from "node-fetch";
import {
IExtractComicBookCoverErrorResponse,
IExtractedComicBookCoverFile,
IExtractionOptions,
IFolderData,
} from "../../server/interfaces/folder.interface";
import { FS_API_BASE_URI } from "../constants/endpoints";
import { API_BASE_URI } from "../constants/endpoints";
export async function walkFolder(path: string): Promise<Array<IFolderData>> {
return axios
.request<Array<IFolderData>>({
url: FS_API_BASE_URI + "walkFolder",
url: API_BASE_URI + "walkFolder",
method: "POST",
params: {
data: {
basePathToWalk: path,
},
transformResponse: (r: string) => JSON.parse(r),
@@ -26,17 +27,16 @@ export async function walkFolder(path: string): Promise<Array<IFolderData>> {
export async function extractCoverFromComicBookArchive(
extractionOptions: IExtractionOptions,
walkedFolders: Array<IFolderData>,
): Promise<
| IExtractedComicBookCoverFile
| IExtractedComicBookCoverFile[]
| IExtractComicBookCoverErrorResponse
> {
return await axios.request({
url: FS_API_BASE_URI + "getComicCovers",
): Promise<Response> {
return await fetch(API_BASE_URI + "getComicCovers", {
method: "POST",
data: {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
extractionOptions,
walkedFolders,
},
}),
});
}

View File

@@ -1,3 +1,3 @@
export const COMICBOOKINFO_SERVICE_URI =
"http://localhost:6050/api/comicbookinfo/";
export const FS_API_BASE_URI = "http://localhost:8050/api/";
export const API_BASE_URI = "http://localhost:8050/api/";

View File

@@ -4,6 +4,8 @@ import {
} from "../actions/fileops.actions";
import { IExtractedComicBookCoverFile } from "../../server/interfaces/folder.interface";
const ndjsonStream = require("can-ndjson-stream");
import fetch from "node-fetch";
import { API_BASE_URI } from "../constants/endpoints";
export const greet = async (
path: string,
@@ -19,14 +21,23 @@ export const greet = async (
pageLimit: 25,
page: 1,
};
const extractionOptions = {
...targetOptions,
paginationOptions: pagingConfig,
};
const fileObjects = await walkFolder("./comics");
const fo = await extractCoverFromComicBookArchive(
{
...targetOptions,
paginationOptions: pagingConfig,
},
const fetchedResource = await extractCoverFromComicBookArchive(
extractionOptions,
fileObjects,
);
// return JSON.stringify(fo);
const reader = await ndjsonStream(fetchedResource.body).getReader();
reader.read().then(function process({ done, value }) {
if (done) {
console.log("done");
return;
}
return reader.read().then(process);
});
};