@dgac/nmb2b-client
Version:
EUROCONTROL Network Manager B2B SOAP client
170 lines (167 loc) • 5.76 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/security.ts
var security_exports = {};
__export(security_exports, {
clearCache: () => clearCache,
fromEnv: () => fromEnv,
isValidSecurity: () => isValidSecurity,
prepareSecurity: () => prepareSecurity
});
module.exports = __toCommonJS(security_exports);
var import_invariant = __toESM(require("invariant"), 1);
// src/utils/debug.ts
var import_debug = __toESM(require("debug"), 1);
var PREFIX = "@dgac/nmb2b-client";
var debug = (0, import_debug.default)(PREFIX);
function log(ns) {
if (!ns) {
return debug;
}
return debug.extend(ns);
}
var debug_default = log;
// src/security.ts
var import_soap = require("soap");
var import_fs = __toESM(require("fs"), 1);
var debug2 = debug_default("security");
function isValidSecurity(obj) {
(0, import_invariant.default)(!!obj && typeof obj === "object", "Must be an object");
if ("apiKeyId" in obj) {
(0, import_invariant.default)(
!!obj.apiKeyId && typeof obj.apiKeyId === "string" && obj.apiKeyId.length > 0,
"security.apiKeyId must be a string with a length > 0"
);
(0, import_invariant.default)(
"apiSecretKey" in obj && typeof obj.apiSecretKey === "string" && obj.apiSecretKey.length > 0,
"security.apiSecretKey must be defined when using security.apiKeyId"
);
return true;
}
(0, import_invariant.default)(
"pfx" in obj && Buffer.isBuffer(obj.pfx) || "cert" in obj && Buffer.isBuffer(obj.cert),
"security.pfx or security.cert must be buffers"
);
if ("cert" in obj && obj.cert) {
(0, import_invariant.default)(
"key" in obj && obj.key && Buffer.isBuffer(obj.key),
"security.key must be a buffer if security.pem is defined"
);
}
return true;
}
function prepareSecurity(config) {
const { security } = config;
if ("apiKeyId" in security) {
const { apiKeyId, apiSecretKey } = security;
debug2("Using ApiGateway security");
return new import_soap.BasicAuthSecurity(apiKeyId, apiSecretKey);
} else if ("pfx" in security) {
const { pfx, passphrase } = security;
debug2("Using PFX certificates");
return new import_soap.ClientSSLSecurityPFX(pfx, passphrase);
} else if ("cert" in security) {
debug2("Using PEM certificates");
const { key, cert, passphrase } = security;
return new import_soap.ClientSSLSecurity(
key,
cert,
void 0,
passphrase ? { passphrase } : null
);
}
throw new Error("Invalid security object");
}
var envSecurity;
function fromEnv() {
if (envSecurity) {
return envSecurity;
}
const { B2B_CERT, B2B_API_KEY_ID, B2B_API_SECRET_KEY } = process.env;
if (!B2B_CERT && !B2B_API_KEY_ID) {
throw new Error(
"Please define a B2B_CERT or a B2B_API_KEY_ID environment variable"
);
}
if (B2B_API_KEY_ID) {
if (!B2B_API_SECRET_KEY) {
throw new Error(
`When using B2B_API_KEY_ID, a B2B_API_SECRET_KEY must be defined`
);
}
return {
apiKeyId: B2B_API_KEY_ID,
apiSecretKey: B2B_API_SECRET_KEY
};
}
if (!B2B_CERT) {
throw new Error("Should never happen");
}
if (!import_fs.default.existsSync(B2B_CERT)) {
throw new Error(`${B2B_CERT} is not a valid certificate file`);
}
const pfxOrPem = import_fs.default.readFileSync(B2B_CERT);
if (!process.env.B2B_CERT_FORMAT || process.env.B2B_CERT_FORMAT === "pfx") {
envSecurity = {
pfx: pfxOrPem,
passphrase: process.env.B2B_CERT_PASSPHRASE ?? ""
};
return envSecurity;
} else if (process.env.B2B_CERT_FORMAT === "pem") {
if (!process.env.B2B_CERT_KEY || !import_fs.default.existsSync(process.env.B2B_CERT_KEY)) {
throw new Error(
"Please define a valid B2B_CERT_KEY environment variable"
);
}
envSecurity = {
cert: pfxOrPem,
key: import_fs.default.readFileSync(process.env.B2B_CERT_KEY)
};
if (process.env.B2B_CERT_PASSPHRASE) {
envSecurity = {
...envSecurity,
passphrase: process.env.B2B_CERT_PASSPHRASE
};
return envSecurity;
}
return envSecurity;
}
throw new Error("Unsupported B2B_CERT_FORMAT, must be pfx or pem");
}
function clearCache() {
envSecurity = void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
clearCache,
fromEnv,
isValidSecurity,
prepareSecurity
});
//# sourceMappingURL=security.cjs.map