From 0963cdb5c4f0f885012cd41dfb667e1c6e61d098 Mon Sep 17 00:00:00 2001 From: monoid Date: Thu, 28 Apr 2022 13:40:16 +0900 Subject: [PATCH] fix: use zip.async --- src/content/comic.ts | 2 +- src/route/comic.ts | 6 +++--- src/util/zipwrap.ts | 45 +++++++++++++++----------------------------- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/content/comic.ts b/src/content/comic.ts index 74e3772..003f666 100644 --- a/src/content/comic.ts +++ b/src/content/comic.ts @@ -26,7 +26,7 @@ export class ComicReferrer extends createDefaultClass("comic"){ async initDesc():Promise{ if(this.desc !== undefined) return; const zip = await readZip(this.path); - const entries = zip.entries(); + const entries = await zip.entries(); this.pagenum = Object.keys(entries).filter(x=>ImageExt.includes(extname(x))).length; const entry = entries["desc.json"]; if(entry === undefined){ diff --git a/src/route/comic.ts b/src/route/comic.ts index bb03f1e..071e7f8 100644 --- a/src/route/comic.ts +++ b/src/route/comic.ts @@ -3,17 +3,17 @@ import { createReadableStreamFromZip, entriesByNaturalOrder, readZip, + ZipAsync, } from "../util/zipwrap"; import { since_last_modified } from "./util"; import { ContentContext } from "./context"; import Router from "koa-router"; -import StreamZip from "node-stream-zip"; /** * zip stream cache. */ -let ZipStreamCache: { [path: string]: [StreamZip, number] } = {}; +let ZipStreamCache: { [path: string]: [ZipAsync, number] } = {}; async function acquireZip(path: string) { if (ZipStreamCache[path] === undefined) { @@ -48,7 +48,7 @@ async function renderZipImage(ctx: Context, path: string, page: number) { const image_ext = ["gif", "png", "jpeg", "bmp", "webp", "jpg"]; //console.log(`opened ${page}`); let zip = await acquireZip(path); - const entries = entriesByNaturalOrder(zip).filter((x) => { + const entries = (await entriesByNaturalOrder(zip)).filter((x) => { const ext = x.name.split(".").pop(); return ext !== undefined && image_ext.includes(ext); }); diff --git a/src/util/zipwrap.ts b/src/util/zipwrap.ts index 04db8ea..0457f12 100644 --- a/src/util/zipwrap.ts +++ b/src/util/zipwrap.ts @@ -1,41 +1,26 @@ -import StreamZip, { ZipEntry } from 'node-stream-zip'; +import { ZipEntry } from 'node-stream-zip'; + import {orderBy} from 'natural-orderby'; import { ReadStream } from 'fs'; +import StreamZip from 'node-stream-zip'; -export async function readZip(path : string):Promise{ - return new Promise((resolve,reject)=>{ - let zip = new StreamZip({ - file:path, - storeEntries: true - }); - zip.on('error',(err)=>{ - console.error(`read zip file ${path}`); - reject(err); - }); - zip.on('ready',()=>{ - resolve(zip); - }); - } - ); +export type ZipAsync = InstanceType; +export async function readZip(path : string): Promise{ + return new StreamZip.async({ + file:path, + storeEntries: true + }); } -export function entriesByNaturalOrder(zip: StreamZip){ - const entries = zip.entries(); +export async function entriesByNaturalOrder(zip: ZipAsync){ + const entries = await zip.entries(); const ret = orderBy(Object.values(entries),v=>v.name); return ret; } -export async function createReadableStreamFromZip(zip:StreamZip,entry: ZipEntry):Promise{ - return new Promise((resolve,reject)=>{ - zip.stream(entry,(err, stream)=>{ - if(stream !== undefined){ - resolve(stream); - } - else{ - reject(err); - } - });} - ); + +export async function createReadableStreamFromZip(zip:ZipAsync,entry: ZipEntry):Promise{ + return await zip.stream(entry); } -export async function readAllFromZip(zip:StreamZip,entry: ZipEntry):Promise{ +export async function readAllFromZip(zip:ZipAsync,entry: ZipEntry):Promise{ const stream = await createReadableStreamFromZip(zip,entry); const chunks:Uint8Array[] = []; return new Promise((resolve,reject)=>{