hilink-nodejs-sms
Version:
Node.js library to send sms using Huawei modems (hilink).
53 lines (52 loc) • 3.09 kB
JavaScript
;
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 });
const child_process_1 = require("child_process");
const common_1 = require("../../../common");
const cUrlRequest = (url, body, options) => __awaiter(void 0, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
var _a;
const headersText = (options === null || options === void 0 ? void 0 : options.headers) !== undefined ? Object.keys(options.headers).map(hName => ` -H "${hName}: ${options.headers[hName]}" `).join('') : '';
const bodyText = body !== undefined ? ` -d "${body}" ` : '';
const interfaceText = (options === null || options === void 0 ? void 0 : options.networkInterface) !== undefined ? ` --interface ${options.networkInterface} ` : '';
const methodText = ((_a = options === null || options === void 0 ? void 0 : options.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === 'POST' ? ' -X POST ' : '';
child_process_1.exec('curl -i ' + headersText + interfaceText + bodyText + methodText + url, (error, stdout, stderr) => {
if (error) {
common_1.networkErrorHandler(error);
}
const responseRows = stdout.split('\n');
responseRows.shift(); // http status
const divider = responseRows.indexOf('\r');
if (divider === -1) {
reject('Parsing response error.');
}
const headers = responseRows.slice(0, divider);
const body = responseRows.slice(divider + 1, responseRows.length);
resolve({
headers: headers.reduce((result, phrase) => {
let [key, value] = phrase.split(':');
result[key.trim().toLowerCase()] = value.trim();
return result;
}, {}),
body: body.join('')
});
});
});
});
const cUrlConnector = (baseUrl, networkInterface) => ({
get: (url, headers) => __awaiter(void 0, void 0, void 0, function* () {
return yield cUrlRequest(baseUrl + url, undefined, { networkInterface, method: 'GET', headers });
}),
post: (url, body, headers) => __awaiter(void 0, void 0, void 0, function* () {
return yield cUrlRequest(baseUrl + url, body, { networkInterface, method: 'GET', headers });
})
});
exports.default = cUrlConnector;