mindee
Version:
Mindee Client Library for Node.js
144 lines (143 loc) • 5.34 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalResponse = void 0;
const crypto = __importStar(require("crypto"));
const fs = __importStar(require("node:fs/promises"));
const errors_1 = require("../errors");
const buffer_1 = require("buffer");
/**
* Local response loaded from a file.
* Note: Has to be initialized through init() before use.
*/
class LocalResponse {
/**
* Creates an instance of LocalResponse.
*/
constructor(inputFile) {
this.initialized = false;
this.file = buffer_1.Buffer.alloc(0);
this.inputHandle = inputFile;
}
async init() {
/**
* @param inputFile - The input file, which can be a Buffer, string, or PathLike.
*/
if (buffer_1.Buffer.isBuffer(this.inputHandle)) {
this.file = this.inputHandle;
}
else if (typeof this.inputHandle === "string") {
let fileContents;
try {
await fs.access(this.inputHandle);
fileContents = await fs.readFile(this.inputHandle, { encoding: "utf-8" });
}
catch {
fileContents = this.inputHandle;
}
this.file = buffer_1.Buffer.from(fileContents.replace(/\r/g, "").replace(/\n/g, ""), "utf-8");
}
else {
throw new errors_1.MindeeError("Incompatible type for input.");
}
this.initialized = true;
}
/**
* Returns the dictionary representation of the file.
* @returns A JSON-like object.
*/
async asDict() {
if (!this.initialized) {
await this.init();
}
try {
const content = this.file.toString("utf-8");
return JSON.parse(content);
}
catch {
throw new errors_1.MindeeError("File is not a valid dictionary.");
}
}
/**
* Returns the HMAC signature of the local response, from the secret key provided.
* @param secretKey - Secret key, either a string or a byte/byte array.
* @returns The HMAC signature of the local response.
*/
getHmacSignature(secretKey) {
if (!this.initialized) {
throw new Error("The `init()` method must be called before calling `getHmacSignature()`.");
}
const algorithm = "sha256";
try {
const hmac = crypto.createHmac(algorithm, secretKey);
hmac.update(this.file);
return hmac.digest("hex");
}
catch {
throw new errors_1.MindeeError("Could not get HMAC signature from payload.");
}
}
/**
* Checks if the HMAC signature of the local response is valid.
* @param secretKey - Secret key, either a string or a byte/byte array.
* @param signature - The signature to be compared with.
* @returns True if the HMAC signature is valid.
*/
isValidHmacSignature(secretKey, signature) {
if (!this.initialized) {
throw new Error("The `init()` method must be called before calling `isValidHmacSignature()`.");
}
return signature === this.getHmacSignature(secretKey);
}
/**
* Deserialize the loaded local response into the requested CommonResponse-derived class.
*
* Typically used when dealing with V2 webhook callbacks.
*
* @typeParam ResponseT - A class that extends `CommonResponse`.
* @param responseClass - The constructor of the class into which the payload should be deserialized.
* @returns An instance of `responseClass` populated with the file content.
* @throws MindeeError If the provided class cannot be instantiated.
*/
async deserializeResponse(responseClass) {
try {
return new responseClass(await this.asDict());
}
catch {
throw new errors_1.MindeeError("Invalid response provided.");
}
}
}
exports.LocalResponse = LocalResponse;