@cn-shell/aws-utils
Version:
A Cloud Native extension for AWS
225 lines (224 loc) • 7.53 kB
JavaScript
;
var __createBinding =
(this && this.__createBinding) ||
(Object.create
? function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, {
enumerable: true,
get: function() {
return m[k];
},
});
}
: 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(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (k !== "default" && Object.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault =
(this && this.__importDefault) ||
function(mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Base = void 0;
// imports here
const cn_shell_1 = __importDefault(require("cn-shell"));
const fs = __importStar(require("fs"));
// Playback version consts here
const END_OF_FILE = "";
const SQS_RCV_PLAYBACK_VER_1 = "SQS-RCV-V1";
const SQS_SND_PLAYBACK_VER_1 = "SQS-SND-V1";
const SNS_PLAYBACK_VER_1 = "SNS-V1";
const FHS_PLAYBACK_VER_1 = "FH-V1";
const SQS_RCV_CURRENT_PLAYBACK_VER = SQS_RCV_PLAYBACK_VER_1;
const SQS_SND_CURRENT_PLAYBACK_VER = SQS_SND_PLAYBACK_VER_1;
const SNS_CURRENT_PLAYBACK_VER = SNS_PLAYBACK_VER_1;
const FHS_CURRENT_PLAYBACK_VER = FHS_PLAYBACK_VER_1;
// Enums here
var RecordTypes;
(function(RecordTypes) {
RecordTypes[(RecordTypes["SNS"] = 0)] = "SNS";
RecordTypes[(RecordTypes["SQS_SENDER"] = 1)] = "SQS_SENDER";
RecordTypes[(RecordTypes["SQS_RECEIVER"] = 2)] = "SQS_RECEIVER";
RecordTypes[(RecordTypes["FHS"] = 3)] = "FHS";
})(RecordTypes || (RecordTypes = {}));
// Class AwsBase here
class Base extends cn_shell_1.default {
// Constructor here
constructor(name, opts) {
super(name);
this._region = opts.region;
this.info("Region: %s", this._region);
this._playbackFile = "";
}
// Getters here
static get RecordTypes() {
return RecordTypes;
}
// Public/private methods here
writePlayback(msg, type) {
if (this._playbackFile === "") {
return;
}
switch (type) {
case RecordTypes.SNS:
// Check the current playback we should generate
switch (SNS_CURRENT_PLAYBACK_VER) {
case SNS_PLAYBACK_VER_1:
this.writePbFld(SNS_PLAYBACK_VER_1, "|");
this.writePbFld(this.name, "|");
this.writePbFld(Date.now().toString(), "|");
this.writePbFld(msg, "\n");
break;
}
break;
case RecordTypes.SQS_SENDER:
// Check the current playback we should generate
switch (SQS_SND_CURRENT_PLAYBACK_VER) {
case SQS_SND_PLAYBACK_VER_1:
this.writePbFld(SQS_SND_PLAYBACK_VER_1, "|");
this.writePbFld(this.name, "|");
this.writePbFld(Date.now().toString(), "|");
this.writePbFld(msg, "\n");
break;
}
break;
case RecordTypes.SQS_RECEIVER:
// Check the current playback we should generate
switch (SQS_RCV_CURRENT_PLAYBACK_VER) {
case SQS_RCV_PLAYBACK_VER_1:
this.writePbFld(SQS_RCV_PLAYBACK_VER_1, "|");
this.writePbFld(this.name, "|");
this.writePbFld(Date.now().toString(), "|");
this.writePbFld(msg, "\n");
break;
}
break;
case RecordTypes.FHS:
// Check the current playback we should generate
switch (FHS_CURRENT_PLAYBACK_VER) {
case FHS_PLAYBACK_VER_1:
this.writePbFld(FHS_PLAYBACK_VER_1, "|");
this.writePbFld(this.name, "|");
this.writePbFld(Date.now().toString(), "|");
this.writePbFld(msg, "\n");
break;
}
break;
}
}
writePbFld(fld, delimiter) {
// The 1st byte indicates, in ASCII, how many bytes make up the len
// The following bytes, in ASCII, are the size
// e.g. is len is 120 bytes the encoding is "3120"
let fldSize = fld.length.toString();
let field = `${fldSize.length}${fldSize}${fld}${delimiter}`;
fs.appendFileSync(this._playbackFile, field);
}
startRecording(playbackFile) {
if (playbackFile === "") {
this.error("startRecording: Must specify a playback file");
}
this.info("Starting to record to playback file: %s", playbackFile);
this._playbackFile = playbackFile;
}
stopRecording() {
this.info("Stopping recording to playback file: %s", this._playbackFile);
this._playbackFile = "";
}
// Static methods here
static replayPlayback(fd, line) {
// The first field is ALWAYS the playback version
let version = Base.getPbFld("version", fd, line);
switch (version) {
case SQS_SND_PLAYBACK_VER_1:
return {
type: RecordTypes.SQS_SENDER,
name: Base.getPbFld("name", fd, line),
ts: parseInt(Base.getPbFld("ts", fd, line), 10),
msg: Base.getPbFld("msg", fd, line),
};
break;
case SQS_RCV_PLAYBACK_VER_1:
return {
type: RecordTypes.SQS_RECEIVER,
name: Base.getPbFld("name", fd, line),
ts: parseInt(Base.getPbFld("ts", fd, line), 10),
msg: Base.getPbFld("msg", fd, line),
};
case SNS_PLAYBACK_VER_1:
return {
type: RecordTypes.SNS,
name: Base.getPbFld("name", fd, line),
ts: parseInt(Base.getPbFld("ts", fd, line), 10),
msg: Base.getPbFld("msg", fd, line),
};
case FHS_PLAYBACK_VER_1:
return {
type: RecordTypes.FHS,
name: Base.getPbFld("name", fd, line),
ts: parseInt(Base.getPbFld("ts", fd, line), 10),
msg: Base.getPbFld("msg", fd, line),
};
case END_OF_FILE:
return null;
default:
throw new Error(
`startPlayback: Playback line ${line}: Unknown version`,
);
}
}
static getPbFld(name, fd, line) {
// The 1st byte represents the number of byte that make up the len
let sizeLenBuf = new Buffer(1);
let bytes = fs.readSync(fd, sizeLenBuf, 0, 1, null);
if (bytes !== 1) {
// Can not read any more bytse that means: End of file
return END_OF_FILE;
}
let sizeLen = parseInt(sizeLenBuf.toString(), 10);
// The next msgSizeLen number of bytes contains the size of the msg
let sizeBuf = new Buffer(sizeLen);
bytes = fs.readSync(fd, sizeBuf, 0, sizeLen, null);
if (bytes !== sizeLen) {
throw new Error(
`getPbFld: Playback line ${line}: Can not read msgSize of ${name}`,
);
}
let size = parseInt(sizeBuf.toString(), 10);
// Get the fld
let fldBuf = new Buffer(size);
bytes = fs.readSync(fd, fldBuf, 0, size, null);
if (bytes !== size) {
throw new Error(
`getPbFld: Playback line ${line}: Can not read field of ${name}`,
);
}
// All flds have a delimiter of 1 byte - read but ignore it
bytes = fs.readSync(fd, sizeLenBuf, 0, 1, null);
return fldBuf.toString();
}
}
exports.Base = Base;