Comparing version 0.12.0 to 0.13.0
@@ -19,2 +19,3 @@ "use strict"; | ||
const cafe_utility_1 = require("cafe-utility"); | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const koa_1 = __importDefault(require("koa")); | ||
@@ -27,2 +28,8 @@ const logger = cafe_utility_1.Logger.create('[Bee]'); | ||
.withOption({ | ||
key: 'purge', | ||
type: 'boolean', | ||
alias: 'p', | ||
description: 'Purge stamps every minute' | ||
}) | ||
.withOption({ | ||
key: 'expire', | ||
@@ -60,2 +67,8 @@ type: 'boolean', | ||
.withOption({ | ||
key: 'detect-overpurchase', | ||
type: 'boolean', | ||
description: 'Detect postage overpurchase', | ||
alias: 'd' | ||
}) | ||
.withOption({ | ||
key: 'chaos', | ||
@@ -70,3 +83,3 @@ type: 'number', | ||
.withOption({ | ||
key: 'delay', | ||
key: 'throttling', | ||
type: 'number', | ||
@@ -76,3 +89,3 @@ description: 'Maximum delay in milliseconds', | ||
default: 0, | ||
alias: 'd' | ||
alias: 't' | ||
}) | ||
@@ -96,11 +109,30 @@ .withFn((context) => __awaiter(this, void 0, void 0, function* () { | ||
} | ||
let purchaseCounter = 0; | ||
const stamps = parserContext.options['initial-stamp'] ? [createStamp('200500', 22)] : []; | ||
if (parserContext.options['purge']) { | ||
setInterval(() => { | ||
logger.info('♻️ Purging stamps ♻️'); | ||
purchaseCounter = 0; | ||
cafe_utility_1.Arrays.empty(stamps); | ||
}, cafe_utility_1.Dates.seconds(90)); | ||
} | ||
const app = new koa_1.default(); | ||
app.use((context, next) => __awaiter(this, void 0, void 0, function* () { | ||
logger.info(`${context.request.method} ${context.request.url}`); | ||
if (context.request.method === 'GET') { | ||
logger.info(`${chalk_1.default.bgWhite.black(` ${context.request.method} `)} ${context.request.url}`); | ||
} | ||
else if (context.request.method === 'POST') { | ||
logger.info(`${chalk_1.default.bgGreen.black(` ${context.request.method} `)} ${chalk_1.default.green(context.request.url)}`); | ||
} | ||
else if (context.request.method === 'PATCH') { | ||
logger.info(`${chalk_1.default.bgBlue.black(` ${context.request.method} `)} ${chalk_1.default.blue(context.request.url)}`); | ||
} | ||
else { | ||
logger.info(`${context.request.method} ${context.request.url}`); | ||
} | ||
yield next(); | ||
})); | ||
if (parserContext.options.delay) { | ||
if (parserContext.options.throttling) { | ||
app.use((_, next) => __awaiter(this, void 0, void 0, function* () { | ||
yield cafe_utility_1.System.sleepMillis(cafe_utility_1.Random.inclusiveInt(0, cafe_utility_1.Types.asNumber(parserContext.options.delay))); | ||
yield cafe_utility_1.System.sleepMillis(cafe_utility_1.Random.inclusiveInt(0, cafe_utility_1.Types.asNumber(parserContext.options.throttling) / 2)); | ||
yield next(); | ||
@@ -147,2 +179,6 @@ })); | ||
router.post('/stamps/:amount/:depth', (context) => __awaiter(this, void 0, void 0, function* () { | ||
if (parserContext.options['detect-overpurchase'] && (stamps.length > 0 || purchaseCounter > 0)) { | ||
logger.warn('⚠️ Detected postage overpurchase ⚠️'); | ||
} | ||
purchaseCounter++; | ||
if (parserContext.options['stuck-stamp']) { | ||
@@ -152,3 +188,3 @@ yield cafe_utility_1.System.sleepMillis(cafe_utility_1.Dates.hours(100)); | ||
if (!parserContext.options['instant-stamp']) { | ||
yield cafe_utility_1.System.sleepMillis(cafe_utility_1.Dates.seconds(30)); | ||
yield cafe_utility_1.System.sleepMillis(cafe_utility_1.Dates.seconds(20)); | ||
} | ||
@@ -160,2 +196,8 @@ const stamp = createStamp(context.params.amount, context.params.depth); | ||
app.use(router.routes()); | ||
if (parserContext.options.throttling) { | ||
app.use((_, next) => __awaiter(this, void 0, void 0, function* () { | ||
yield cafe_utility_1.System.sleepMillis(cafe_utility_1.Random.inclusiveInt(0, cafe_utility_1.Types.asNumber(parserContext.options.throttling) / 2)); | ||
yield next(); | ||
})); | ||
} | ||
app.listen(1633); | ||
@@ -162,0 +204,0 @@ app.listen(1635); |
@@ -65,7 +65,6 @@ "use strict"; | ||
.withOption({ | ||
key: 'reupload-interval-ms', | ||
type: 'number', | ||
alias: 'i', | ||
description: 'Reupload interval in milliseconds', | ||
default: cafe_utility_1.Dates.minutes(2) | ||
key: 'quick', | ||
type: 'boolean', | ||
alias: 'q', | ||
description: 'Postage check is very frequent' | ||
}) | ||
@@ -138,3 +137,3 @@ .withOption({ | ||
if (reupload) { | ||
env.REUPLOAD_PERIOD = cafe_utility_1.Types.asNumber(context.options['reupload-interval-ms']).toString(); | ||
env.REUPLOAD_PERIOD = cafe_utility_1.Dates.minutes(2).toString(); | ||
} | ||
@@ -144,2 +143,8 @@ if (context.options['debug']) { | ||
} | ||
if (context.options['quick']) { | ||
env.POSTAGE_REFRESH_PERIOD = cafe_utility_1.Dates.seconds(2).toString(); | ||
if (reupload) { | ||
env.REUPLOAD_PERIOD = cafe_utility_1.Dates.seconds(2).toString(); | ||
} | ||
} | ||
console.log('Starting with env:', env); | ||
@@ -146,0 +151,0 @@ cafe_utility_1.System.runProcess('npm', ['start'], { cwd: location, env }, x => process.stdout.write(x.toString()), x => process.stderr.write(x.toString())); |
{ | ||
"name": "cafe-tui", | ||
"version": "0.12.0", | ||
"version": "0.13.0", | ||
"description": "", | ||
@@ -31,2 +31,3 @@ "main": "dist/index.js", | ||
"cafe-utility": "4.14", | ||
"chalk": "^4.1.2", | ||
"inquirer": "^8", | ||
@@ -33,0 +34,0 @@ "js-yaml": "^4.1.0", |
import Router from '@koa/router' | ||
import { CafeFnContext, Command, Parser } from 'cafe-args' | ||
import { Dates, Logger, Numbers, Random, Strings, System, Types } from 'cafe-utility' | ||
import { Arrays, Dates, Logger, Numbers, Random, Strings, System, Types } from 'cafe-utility' | ||
import chalk from 'chalk' | ||
import Koa from 'koa' | ||
@@ -15,2 +16,8 @@ import { Stamp } from '../stamp' | ||
.withOption({ | ||
key: 'purge', | ||
type: 'boolean', | ||
alias: 'p', | ||
description: 'Purge stamps every minute' | ||
}) | ||
.withOption({ | ||
key: 'expire', | ||
@@ -48,2 +55,8 @@ type: 'boolean', | ||
.withOption({ | ||
key: 'detect-overpurchase', | ||
type: 'boolean', | ||
description: 'Detect postage overpurchase', | ||
alias: 'd' | ||
}) | ||
.withOption({ | ||
key: 'chaos', | ||
@@ -58,3 +71,3 @@ type: 'number', | ||
.withOption({ | ||
key: 'delay', | ||
key: 'throttling', | ||
type: 'number', | ||
@@ -64,3 +77,3 @@ description: 'Maximum delay in milliseconds', | ||
default: 0, | ||
alias: 'd' | ||
alias: 't' | ||
}) | ||
@@ -86,12 +99,29 @@ .withFn(async context => { | ||
let purchaseCounter = 0 | ||
const stamps: Stamp[] = parserContext.options['initial-stamp'] ? [createStamp('200500', 22)] : [] | ||
if (parserContext.options['purge']) { | ||
setInterval(() => { | ||
logger.info('♻️ Purging stamps ♻️') | ||
purchaseCounter = 0 | ||
Arrays.empty(stamps) | ||
}, Dates.seconds(90)) | ||
} | ||
const app = new Koa() | ||
app.use(async (context, next) => { | ||
logger.info(`${context.request.method} ${context.request.url}`) | ||
if (context.request.method === 'GET') { | ||
logger.info(`${chalk.bgWhite.black(` ${context.request.method} `)} ${context.request.url}`) | ||
} else if (context.request.method === 'POST') { | ||
logger.info(`${chalk.bgGreen.black(` ${context.request.method} `)} ${chalk.green(context.request.url)}`) | ||
} else if (context.request.method === 'PATCH') { | ||
logger.info(`${chalk.bgBlue.black(` ${context.request.method} `)} ${chalk.blue(context.request.url)}`) | ||
} else { | ||
logger.info(`${context.request.method} ${context.request.url}`) | ||
} | ||
await next() | ||
}) | ||
if (parserContext.options.delay) { | ||
if (parserContext.options.throttling) { | ||
app.use(async (_, next) => { | ||
await System.sleepMillis(Random.inclusiveInt(0, Types.asNumber(parserContext.options.delay))) | ||
await System.sleepMillis(Random.inclusiveInt(0, Types.asNumber(parserContext.options.throttling) / 2)) | ||
await next() | ||
@@ -137,2 +167,6 @@ }) | ||
router.post('/stamps/:amount/:depth', async (context: Koa.Context) => { | ||
if (parserContext.options['detect-overpurchase'] && (stamps.length > 0 || purchaseCounter > 0)) { | ||
logger.warn('⚠️ Detected postage overpurchase ⚠️') | ||
} | ||
purchaseCounter++ | ||
if (parserContext.options['stuck-stamp']) { | ||
@@ -142,3 +176,3 @@ await System.sleepMillis(Dates.hours(100)) | ||
if (!parserContext.options['instant-stamp']) { | ||
await System.sleepMillis(Dates.seconds(30)) | ||
await System.sleepMillis(Dates.seconds(20)) | ||
} | ||
@@ -150,2 +184,8 @@ const stamp = createStamp(context.params.amount, context.params.depth) | ||
app.use(router.routes()) | ||
if (parserContext.options.throttling) { | ||
app.use(async (_, next) => { | ||
await System.sleepMillis(Random.inclusiveInt(0, Types.asNumber(parserContext.options.throttling) / 2)) | ||
await next() | ||
}) | ||
} | ||
app.listen(1633) | ||
@@ -152,0 +192,0 @@ app.listen(1635) |
@@ -55,7 +55,6 @@ import { Command, Parser } from 'cafe-args' | ||
.withOption({ | ||
key: 'reupload-interval-ms', | ||
type: 'number', | ||
alias: 'i', | ||
description: 'Reupload interval in milliseconds', | ||
default: Dates.minutes(2) | ||
key: 'quick', | ||
type: 'boolean', | ||
alias: 'q', | ||
description: 'Postage check is very frequent' | ||
}) | ||
@@ -128,3 +127,3 @@ .withOption({ | ||
if (reupload) { | ||
env.REUPLOAD_PERIOD = Types.asNumber(context.options['reupload-interval-ms']).toString() | ||
env.REUPLOAD_PERIOD = Dates.minutes(2).toString() | ||
} | ||
@@ -134,2 +133,8 @@ if (context.options['debug']) { | ||
} | ||
if (context.options['quick']) { | ||
env.POSTAGE_REFRESH_PERIOD = Dates.seconds(2).toString() | ||
if (reupload) { | ||
env.REUPLOAD_PERIOD = Dates.seconds(2).toString() | ||
} | ||
} | ||
console.log('Starting with env:', env) | ||
@@ -136,0 +141,0 @@ System.runProcess( |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58591
1403
8
+ Addedchalk@^4.1.2