fs#readSync TypeScript Examples

The following examples show how to use fs#readSync. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: dbtProjectLog.ts    From vscode-dbt-power-user with MIT License 6 votes vote down vote up
private readLogFileFromLastPosition(event: ProjectConfigChangedEvent): void {
    const { projectRoot } = event;
    if (this.outputChannel) {
      let fileHandle;
      try {
        fileHandle = openSync(
          path.join(
            projectRoot.fsPath,
            DBTProjectLog.LOG_PATH,
            DBTProjectLog.LOG_FILE
          ),
          "r"
        );
        const chunkSize = 1024 * 1024;
        const buffer = Buffer.alloc(chunkSize);
        while (true) {
          const bytesRead = readSync(
            fileHandle,
            buffer,
            0,
            buffer.length,
            this.logPosition
          );
          if (!bytesRead) {
            break;
          }
          this.logPosition += bytesRead;
          this.outputChannel.appendLine(buffer.toString("utf8", 0, bytesRead));
        }
      } catch (error) {
        console.log("Could not read log file", error);
      } finally {
        if (fileHandle) {
          closeSync(fileHandle);
        }
      }
    }
  }
Example #2
Source File: conformance_ts.ts    From protobuf-ts with Apache License 2.0 6 votes vote down vote up
// Utility function to read a buffer of N bytes.
function readBuffer(bytes: number): Buffer | 'EOF' {
    let buf = Buffer.alloc(bytes);
    let read = 0;
    try {
        read = readSync(0, buf, 0, bytes, null);
    } catch (e) {
        throw "Error reading from stdin." + e;
    }
    if (read !== bytes) {
        if (read === 0) {
            return 'EOF';
        }
        throw "premature EOF on stdin.";
    }
    return buf;
}
Example #3
Source File: load-flash.ts    From rp2040js with MIT License 6 votes vote down vote up
export function loadMicropythonFlashImage(filename: string, rp2040: RP2040) {
  const file = openSync(filename, 'r');
  const buffer = new Uint8Array(MICROPYTHON_FS_BLOCKSIZE);
  let flashAddress = MICROPYTHON_FS_FLASH_START;
  while (readSync(file, buffer) === buffer.length) {
    rp2040.flash.set(buffer, flashAddress);
    flashAddress += buffer.length;
  }
  closeSync(file);
}
Example #4
Source File: load-flash.ts    From rp2040js with MIT License 6 votes vote down vote up
export function loadUF2(filename: string, rp2040: RP2040) {
  const file = openSync(filename, 'r');
  const buffer = new Uint8Array(512);
  while (readSync(file, buffer) === buffer.length) {
    const block = decodeBlock(buffer);
    const { flashAddress, payload } = block;
    rp2040.flash.set(payload, flashAddress - FLASH_START_ADDRESS);
  }
  closeSync(file);
}