UNPKG

@zowe/zos-files-for-zowe-sdk

Version:

Zowe SDK to interact with files and data sets on z/OS

195 lines 10.3 kB
"use strict"; /* * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * * Copyright Contributors to the Zowe Project. * */ 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.Utilities = void 0; const imperative_1 = require("@zowe/imperative"); const Tag_1 = require("./doc/Tag"); const ZosFiles_messages_1 = require("../../constants/ZosFiles.messages"); const ZosFiles_constants_1 = require("../../constants/ZosFiles.constants"); const core_for_zowe_sdk_1 = require("@zowe/core-for-zowe-sdk"); const path = require("path"); class Utilities { /** * Retrieve various details from USS file functions * * This function uses a JSON payload to retrieve information via zosmf utilities function * * @param {AbstractSession} session - z/OSMF connection info * @param {string} USSFileName - contains the file name * @param {JSON} payload - contains the options to be sent * * @returns {Promise<Buffer>} Promise that resolves to json information * * @throws {ImperativeError} */ static putUSSPayload(session, USSFileName, payload, responseTimeout) { return __awaiter(this, void 0, void 0, function* () { imperative_1.ImperativeExpect.toNotBeNullOrUndefined(USSFileName, ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message); imperative_1.ImperativeExpect.toNotBeEqual(USSFileName, "", ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message); imperative_1.ImperativeExpect.toNotBeNullOrUndefined(payload, ZosFiles_messages_1.ZosFilesMessages.missingPayload.message); imperative_1.ImperativeExpect.toNotBeEqual(payload, {}, ZosFiles_messages_1.ZosFilesMessages.missingPayload.message); // Get a proper destination for the file to be downloaded // If the "file" is not provided, we create a folder structure similar to the uss file structure const endpoint = imperative_1.EncodeUri.encUriPathForUss(session, ZosFiles_constants_1.ZosFilesConstants.RESOURCE + ZosFiles_constants_1.ZosFilesConstants.RES_USS_FILES + "/" + USSFileName); const reqHeaders = [ imperative_1.Headers.APPLICATION_JSON, { [imperative_1.Headers.CONTENT_LENGTH]: JSON.stringify(payload).length.toString() }, core_for_zowe_sdk_1.ZosmfHeaders.ACCEPT_ENCODING ]; if (responseTimeout != null && responseTimeout >= Utilities.minimumTimeout) { reqHeaders.push({ [core_for_zowe_sdk_1.ZosmfHeaders.X_IBM_RESPONSE_TIMEOUT]: responseTimeout.toString() }); } const response = yield core_for_zowe_sdk_1.ZosmfRestClient.putExpectBuffer(session, endpoint, reqHeaders, payload); return response; }); } /** * Changes the tag attributes associate with a file using function chtag * * This function uses a JSON payload to set the information via zosmf utilities function * * @param {AbstractSession} session - z/OSMF connection info * @param {Tag} type - enum of chtag type of Test, Binary or Mixed * @param {string} codeset - optional string describing codeset e.g. IBM-1047 * * @returns {Promise<IZosFilesResponse>} Promise that resolves to response object * * @throws {ImperativeError} */ static chtag(session, ussFileName, type, codeset, responseTimeout) { return __awaiter(this, void 0, void 0, function* () { imperative_1.ImperativeExpect.toNotBeNullOrUndefined(ussFileName, ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message); if (type === Tag_1.Tag.BINARY) { imperative_1.ImperativeExpect.toBeEqual(codeset, undefined, "A codeset cannot be specified for a binary file."); } const payload = { request: "chtag", action: "set", type: type.valueOf() }; if (codeset) { payload.codeset = codeset; } if (responseTimeout != null && responseTimeout >= Utilities.minimumTimeout) { yield Utilities.putUSSPayload(session, ussFileName, payload, responseTimeout); } else { yield Utilities.putUSSPayload(session, ussFileName, payload); } return { success: true, commandResponse: "File tagged successfully." }; }); } /** * Based upon the files chtag value. Identify how the tagging should be interpretted when transferring the contents * For example an EBCDIC file would always be converted from it's EBCDIC value to the ASCII * * An ASCII file value or binary should not be converted. * * The default value if the tag is not set or in an invalid state correctly is to convert. * * @param {AbstractSession} session - z/OSMF connection info * @param {string} USSFileName - contains the file name * * @returns {Promise<boolean>} Promise that resolves to true if the file is binary or ASCII text or false if file * should likely be converted to text. Default is false which aligns with the zosmf default behavior converting * * @throws {ImperativeError} */ static isFileTagBinOrAscii(session, USSFileName, responseTimeout) { return __awaiter(this, void 0, void 0, function* () { const payload = { request: "chtag", action: "list" }; let response; if (responseTimeout != null && responseTimeout >= Utilities.minimumTimeout) { response = yield Utilities.putUSSPayload(session, USSFileName, payload, responseTimeout); } else { response = yield Utilities.putUSSPayload(session, USSFileName, payload); } const jsonObj = JSON.parse(response.toString()); if (Object.prototype.hasOwnProperty.call(jsonObj, "stdout")) { const stdout = jsonObj.stdout[0]; // Tests if binary tag set return stdout.indexOf("b ") > -1 || stdout.indexOf("UTF-") > -1 || stdout.indexOf("ISO8859-") > -1 || stdout.indexOf("IBM-850") > -1; } return false; }); } /** * Check the tags on an existing USS file and update binary and encoding * properties on the options object. * @param session z/OSMF connection info * @param USSFileName Path to USS file * @param options Options for downloading a USS file */ static applyTaggedEncoding(session, USSFileName, options, responseTimeout) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const payload = { request: "chtag", action: "list" }; let response; if (responseTimeout != null && responseTimeout >= Utilities.minimumTimeout) { response = yield Utilities.putUSSPayload(session, USSFileName, payload, responseTimeout); } else { response = yield Utilities.putUSSPayload(session, USSFileName, payload); } const jsonObj = JSON.parse(response.toString()); if (Object.prototype.hasOwnProperty.call(jsonObj, "stdout")) { const columns = jsonObj.stdout[0].trim().split(/\s+/); // Tests if binary tag set if (columns[0] === "b" || ((_a = columns[1]) === null || _a === void 0 ? void 0 : _a.startsWith("ISO8859-")) || ((_b = columns[1]) === null || _b === void 0 ? void 0 : _b.startsWith("UCS-")) || ((_c = columns[1]) === null || _c === void 0 ? void 0 : _c.startsWith("UTF-"))) { options.binary = true; } else if ((_d = columns[1]) === null || _d === void 0 ? void 0 : _d.startsWith("IBM-")) { options.encoding = columns[1]; } } }); } /** * Re-name USS file or directory * * @param {AbstractSession} session - z/OSMF connection info * @param {string} USSFilePath - contains the current filepath * @param {string} newFilePath - contains the new filepath * * @returns {Promise<Buffer>} Promise that resolves to json information * * @throws {ImperativeError} */ static renameUSSFile(session, USSFilePath, newFilePath, responseTimeout) { return __awaiter(this, void 0, void 0, function* () { imperative_1.ImperativeExpect.toNotBeNullOrUndefined(newFilePath, ZosFiles_messages_1.ZosFilesMessages.missingUSSFileName.message); const oldFilePath = USSFilePath.charAt(0) === "/" ? USSFilePath : "/" + USSFilePath; const payload = { request: "move", from: path.posix.normalize(oldFilePath) }; let response; if (responseTimeout != null && responseTimeout >= Utilities.minimumTimeout) { response = yield Utilities.putUSSPayload(session, newFilePath, payload, responseTimeout); } else { response = yield Utilities.putUSSPayload(session, newFilePath, payload); } return response; }); } } exports.Utilities = Utilities; Utilities.minimumTimeout = 5; //# sourceMappingURL=Utilities.js.map