trm-core
Version:
TRM (Transport Request Manager) Core
213 lines (212 loc) • 10.5 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;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrmArtifact = void 0;
const logger_1 = require("../logger");
const manifest_1 = require("../manifest");
const AdmZip = __importStar(require("adm-zip"));
const node_r3trans_1 = require("node-r3trans");
const DIST_FOLDER = 'dist';
class TrmArtifact {
constructor(binary, _distFolder, _manifest) {
this.binary = binary;
this._distFolder = _distFolder;
this._manifest = _manifest;
this._zip = new AdmZip.default(binary);
}
getManifest() {
if (this._manifest === undefined) {
const zipEntries = this._zip.getEntries();
const manifestEntry = zipEntries.find(o => o.entryName.trim().toLowerCase() === 'manifest.json');
const sapEntriesEntry = zipEntries.find(o => o.entryName.trim().toLowerCase() === 'sap_entries.json');
if (!manifestEntry) {
this._manifest = null;
}
else {
var jsonManifest = JSON.parse(manifestEntry.getData().toString());
if (!jsonManifest.sapEntries) {
jsonManifest.sapEntries = {};
}
if (sapEntriesEntry) {
const sapEntries = JSON.parse(sapEntriesEntry.getData().toString());
jsonManifest.sapEntries = Object.assign(Object.assign({}, jsonManifest.sapEntries), sapEntries);
}
const trmManifest = manifest_1.Manifest.normalize(jsonManifest, false);
this._manifest = new manifest_1.Manifest(trmManifest);
}
}
return this._manifest;
}
replaceManifest(oManifest) {
const manifestBuffer = Buffer.from(JSON.stringify(oManifest.get(false), null, 2), 'utf8');
this._zip.updateFile('manifest.json', manifestBuffer);
}
getDistFolder() {
var _a;
if (!this._distFolder) {
this._distFolder = (_a = this.getManifest()) === null || _a === void 0 ? void 0 : _a.get().distFolder;
}
return this._distFolder;
}
getTransportBinaries(r3transOption) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (!this._binaries) {
const distFolder = this.getDistFolder();
if (!distFolder) {
throw new Error(`Couldn't locate dist folder.`);
}
const zipEntries = this._zip.getEntries();
const aTransportEntries = zipEntries.filter(o => (new RegExp(`^${distFolder}(/|\\\\)`)).test(o.entryName.trim().toLowerCase()));
var aResult = [];
const r3trans = new node_r3trans_1.R3trans(r3transOption);
for (const entry of aTransportEntries) {
try {
const type = entry.comment;
const oPackedTransport = new AdmZip.default(entry.getData());
const aPackedTransportEntries = oPackedTransport.getEntries();
const oHeader = (_a = aPackedTransportEntries.find(o => o.comment === 'header')) === null || _a === void 0 ? void 0 : _a.getData();
const oData = (_b = aPackedTransportEntries.find(o => o.comment === 'data')) === null || _b === void 0 ? void 0 : _b.getData();
if (oHeader && oData) {
const trkorr = yield r3trans.getTransportTrkorr(oData);
aResult.push({
trkorr,
type: type,
binaries: {
header: oHeader,
data: oData
}
});
}
}
catch (e) { }
}
;
this._binaries = aResult;
}
return this._binaries || [];
});
}
getContent(r3transConfig) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._content) {
this._content = {};
try {
const transportBinaries = yield this.getTransportBinaries();
const r3trans = new node_r3trans_1.R3trans(r3transConfig);
for (const transportBinary of transportBinaries) {
const tableEntries = yield r3trans.getTableEntries(transportBinary.binaries.data);
if (!this._content[transportBinary.type]) {
this._content[transportBinary.type] = {
trkorr: transportBinary.trkorr,
content: {}
};
}
Object.keys(tableEntries).forEach(table => {
if (!this._content[transportBinary.type].content[table]) {
this._content[transportBinary.type].content[table] = [];
}
this._content[transportBinary.type].content[table] = this._content[transportBinary.type].content[table].concat(tableEntries[table]);
});
}
}
catch (e) {
delete this._content;
throw e;
}
}
return this._content || {};
});
}
static create(transports_1, manifest_2) {
return __awaiter(this, arguments, void 0, function* (transports, manifest, distFolder = DIST_FOLDER) {
logger_1.Logger.log(`Generating artifact with transports ${JSON.stringify(transports.map(o => o.trkorr))}`, true);
const artifact = new AdmZip.default();
logger_1.Logger.log(`Adding ZIP comment`, true);
artifact.addZipComment(`TRM Package`);
var binaries = [];
var packedTransports = [];
for (const transport of transports) {
logger_1.Logger.log(`Downloading transport ${transport.trmIdentifier}`, true);
const trBinary = yield transport.download();
binaries.push({
trkorr: transport.trkorr,
type: transport.trmIdentifier,
binaries: trBinary.binaries,
filenames: trBinary.filenames
});
}
for (const bin of binaries) {
const packedTransport = new AdmZip.default();
logger_1.Logger.log(`Packing header and data in single file`, true);
packedTransport.addZipComment(`Transport request: ${bin.trkorr}\nContent type: ${bin.type || 'Unknown'}`);
packedTransport.addFile(bin.filenames.header, bin.binaries.header, "header");
packedTransport.addFile(bin.filenames.data, bin.binaries.data, "data");
packedTransports.push({
filename: bin.trkorr,
binary: packedTransport.toBuffer(),
comment: bin.type ? bin.type : ''
});
}
for (const file of packedTransports) {
logger_1.Logger.log(`Adding packed transport ${file.comment} to artifact`, true);
artifact.addFile(`${distFolder}/${file.filename}`, file.binary, file.comment);
}
manifest.setDistFolder(distFolder);
var oManifest = manifest.get(false);
var oSapEntries = oManifest.sapEntries;
delete oManifest.sapEntries;
const manifestBuffer = Buffer.from(JSON.stringify(oManifest, null, 2), 'utf8');
logger_1.Logger.log(`Adding manifest.json`, true);
artifact.addFile(`manifest.json`, manifestBuffer, `manifest`);
if (oSapEntries && Object.keys(oSapEntries).length > 0) {
const sapEntriesBuffer = Buffer.from(JSON.stringify(oSapEntries, null, 2), 'utf8');
logger_1.Logger.log(`Adding sap_entries.json`, true);
artifact.addFile(`sap_entries.json`, sapEntriesBuffer, `sap_entries`);
}
return new TrmArtifact(artifact.toBuffer(), distFolder, manifest);
});
}
}
exports.TrmArtifact = TrmArtifact;