ethstorage-sdk
Version:
eip-4844 blobs upload sdk
55 lines • 1.76 kB
JavaScript
// src/node/file.ts
import fs from "fs";
function assertArgument(check, message, name, value) {
if (!check) {
const error = new Error(message);
error.code = "INVALID_ARGUMENT";
error.argument = name;
error.value = value;
throw error;
}
}
var NodeFile = class _NodeFile {
constructor(filePath, start = 0, end = 0, type = "") {
this.isNodeJs = true;
this.filePath = filePath;
this.type = type;
assertArgument(fs.existsSync(filePath), "invalid file path", "file", filePath);
const stat = fs.statSync(filePath);
this.start = Math.min(start, stat.size - 1);
this.end = end == 0 ? stat.size : Math.min(end, stat.size);
this.size = this.end - this.start;
assertArgument(this.size > 0, "invalid file size", "file", this.size);
}
slice(start, end) {
const newStart = this.start + start;
const newEnd = newStart + (end - start);
assertArgument(newStart < newEnd && newEnd <= this.end, "invalid slice range", "file", { start, end });
return new _NodeFile(this.filePath, newStart, newEnd, this.type);
}
async arrayBuffer() {
const start = this.start;
const end = this.end;
const length = end - start;
const arrayBuffer = new ArrayBuffer(length);
const uint8Array = new Uint8Array(arrayBuffer);
const fd = fs.openSync(this.filePath, "r");
fs.readSync(fd, uint8Array, 0, length, start);
fs.closeSync(fd);
return arrayBuffer;
}
async text() {
const buffer = await this.arrayBuffer();
return new TextDecoder().decode(buffer);
}
stream() {
const start = this.start;
const end = this.end;
return fs.createReadStream(this.filePath, { start, end });
}
};
export {
NodeFile,
assertArgument
};
//# sourceMappingURL=file.mjs.map