使用 new filehandler 时获取 filehandler 不是构造函数,但不能作为静态类工作

问题描述 投票:0回答:1
使用

filehandler

 时,获取 
new filehandler
 不是构造函数,但不能用作静态类

用途:

demos.filehandler.mjs

 文件:

import { default as filehandler } from "../index.js"; const FileHandler = filehandler.FileHandlerStatic; const fs = new FileHandler(); (async () => { let tst = await FileHandler.readFileStreaming("./demos/profile.jpeg", "binary"); tst = tst; console.log("read: \n\n", tst); let written = await FileHandler.writeFileStreaming("./demos/profile.jpeg", tst, "binary"); console.log("written: ", written); })();

index.fs

 文件转换为 
index.js
 使用 
npx tsc
 使用目标编译 
es5
:

同时以下

demos.filehandler.js

 文件实现有效:

const filehandler = require("../index").default; const FileHandler = filehandler.FileHandlerStatic; const fs = new FileHandler(); (async () => { let tst = await fs.readFileStreaming("./demos/profile.jpeg", "binary"); console.log("read: \n\n", tst); let written = await fs.writeFileStreaming("./demos/profile.jpeg", tst, "binary"); console.log("written: ", written); })();

export class FileHandlerStatic {

  readFileStreaming(filePath: string | URL, options?: BufferEncoding | any | undefined): Promise<any> {
    return new Promise((resolve, reject) => {
      let readStream = fs.createReadStream(filePath, options || {});
      let chunks: any[] = [];
      // readerStream.setEncoding(encoding || { encoding: 'UTF8' });
      readStream.on('error', (err: any) => { reject(err); });
      readStream.on('data', (chunk: any) => { chunks.push(chunk); });
      readStream.on('end', () => {
        Buffer.isBuffer(chunks[0]) ? resolve(Buffer.concat(chunks)) : resolve(chunks.join(""));
      });
    });
  }

  writeFileStreaming(filePath: string | URL, iterableData: any[], options?: BufferEncoding | any | undefined): Promise<any> {
    return new Promise(async (resolve, reject) => {
      try {
        const finished = util.promisify(stream.finished); // (A)
        const writable = fs.createWriteStream(filePath, options || "utf8");
        for await (const chunk of iterableData) {
          // Handle backpressure
          if (!writable.write(chunk)) { await once(writable, 'drain'); }
        }
        writable.end();
        await finished(writable);
        resolve(true);
      } catch (e) {
        reject(e);
      }
    });
  }
}
出现错误

file:///C:/Users/ganes/OneDrive/Documents/projects/filehandler/demos/demos.filehandler.jpeg.static.mjs.mjs:23 const fs = new FileHandler(); ^ TypeError: FileHandler is not a constructor at file:///C:/Users/ganes/OneDrive/Documents/projects/filehandler/demos/demos.filehandler.jpeg.static.mjs.mjs:23:12 at ModuleJob.run (node:internal/modules/esm/module_job:218:25) at async ModuleLoader.import (node:internal/modules/esm/loader:323:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:120:12) Node.js v21.6.1
    
node.js typescript streaming tsc node.js-fs
1个回答
0
投票
应该是:

import * as filehandler from "../index.js"; const FileHandler = filehandler.FileHandlerStatic;
或者

import { FileHandlerStatic as FileHandler } from "../index.js";
    
© www.soinside.com 2019 - 2024. All rights reserved.