ethstorage-sdk
Version:
eip-4844 blobs upload sdk
97 lines (95 loc) • 3.53 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/node/file.ts
var file_exports = {};
__export(file_exports, {
NodeFile: () => NodeFile,
assertArgument: () => assertArgument
});
module.exports = __toCommonJS(file_exports);
var import_fs = __toESM(require("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 {
isNodeJs;
filePath;
type;
size;
start;
end;
constructor(filePath, start = 0, end = 0, type = "") {
this.isNodeJs = true;
this.filePath = filePath;
this.type = type;
assertArgument(import_fs.default.existsSync(filePath), "invalid file path", "file", filePath);
const stat = import_fs.default.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 = import_fs.default.openSync(this.filePath, "r");
import_fs.default.readSync(fd, uint8Array, 0, length, start);
import_fs.default.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 import_fs.default.createReadStream(this.filePath, { start, end });
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
NodeFile,
assertArgument
});