Skip to content

Commit

Permalink
Rename dataMiddleware to stdinMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed Feb 26, 2025
1 parent 7ff9a70 commit baee6b4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
6 changes: 3 additions & 3 deletions crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ export function decrypt(data, nk, src64, fc, scf, aad, mic) {
]);
}

import { dataMiddleware } from './utils.js';
import { stdinMiddleware } from './utils.js';
export const commands = [
{
command: 'encrypt [data]',
desc: 'Encrypt Packet',
builder: yargs => dataMiddleware(yargs
builder: yargs => stdinMiddleware(yargs
.option('network-key', {
alias: 'nk',
desc: 'Network Key (i.e. temp. Link Key)',
Expand Down Expand Up @@ -200,7 +200,7 @@ export const commands = [
{
command: 'decrypt [data]',
desc: 'Decrypt Packet',
builder: yargs => dataMiddleware(yargs
builder: yargs => stdinMiddleware(yargs
.option('network-key', {
alias: 'nk',
desc: 'Network Key (i.e. temp. Link Key)',
Expand Down
4 changes: 2 additions & 2 deletions format.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export function eui(eui, sep = `${euiSep}`, reverse) {
((index !== eui.length - 1) ? sep : ''), '');
}

import { moduleHandler, dataMiddleware } from './utils.js';
import { moduleHandler, stdinMiddleware } from './utils.js';
export const command = {
command: 'format <type> [data]',
desc: 'Format ICs / EUIs / ...',
builder: yargs => dataMiddleware(yargs
builder: yargs => stdinMiddleware(yargs
.positional('type', {
desc: 'Type',
type: 'string',
Expand Down
4 changes: 2 additions & 2 deletions hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ export function key(data, input = 0x0) {
return mmo(hashIn);
}

import { moduleHandler, dataMiddleware } from './utils.js';
import { moduleHandler, stdinMiddleware } from './utils.js';
export const command = {
command: 'hash [type] [data]',
desc: 'Hash / Checksum Calculation',
builder: yargs => dataMiddleware(yargs
builder: yargs => stdinMiddleware(yargs
.positional('type', {
desc: 'Type of hash / checksum to calculate',
type: 'string',
Expand Down
4 changes: 2 additions & 2 deletions ic.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export function link(ic) {
return mmo(checksum(ic));
}

import { moduleHandler, dataMiddleware } from './utils.js';
import { moduleHandler, stdinMiddleware } from './utils.js';
export const command = {
command: 'ic <action> [install-code]',
desc: 'Install Code Utilities',
builder: yargs => dataMiddleware(yargs
builder: yargs => stdinMiddleware(yargs
.positional('action', {
desc: 'Action to perform',
type: 'string',
Expand Down
39 changes: 20 additions & 19 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,23 +1083,24 @@ export function parse(data, type = 'zep') {

export default parse;

import { dataMiddleware, jsonStringify } from './utils.js';
import { type } from 'node:os';
export const command = {
command: 'parse [data]',
desc: 'Packet Binary Parser',
builder: yargs => dataMiddleware(yargs
.option('type', {
desc: 'Type of packet to parse',
type: 'string',
choices: Object.keys(parsers),
default: 'zep'
}), { desc: 'Data to parse' })
.example('$0 parse 4558020113fffe0029d84f48995f78359c000a91aa000000000000000000000502003ffecb', 'Parse the given data as a ZigBee Encapsulation Protocol (ZEP) packet')
.example('echo -n 4558020113fffe0029d84f48995f78359c000a91aa000000000000000000000502003ffecb | $0 parse', 'Parse the given data from stdin as a ZigBee Encapsulation Protocol (ZEP) packet')
.version(false)
.help(),
handler: argv => {
console.log(`${parse(argv.data, argv.type)}`);
import { stdinMiddleware, jsonStringify } from './utils.js';
export const commands = [
{
command: 'parse [data]',
desc: 'Packet Binary Parser',
builder: yargs => stdinMiddleware(yargs
.option('type', {
desc: 'Type of packet to parse',
type: 'string',
choices: Object.keys(parsers),
default: 'zep'
}), { desc: 'Data to parse' })
.example('$0 parse 4558020113fffe0029d84f48995f78359c000a91aa000000000000000000000502003ffecb', 'Parse the given data as a ZigBee Encapsulation Protocol (ZEP) packet')
.example('echo -n 4558020113fffe0029d84f48995f78359c000a91aa000000000000000000000502003ffecb | $0 parse', 'Parse the given data from stdin as a ZigBee Encapsulation Protocol (ZEP) packet')
.version(false)
.help(),
handler: argv => {
console.log(`${parse(argv.data, argv.type)}`);
}
}
};
];
4 changes: 2 additions & 2 deletions type.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,11 @@ export default function type(packet, type = 'zep') {
return types[type](packet);
}

import { dataMiddleware } from './utils.js';
import { stdinMiddleware } from './utils.js';
export const command = {
command: 'type [data]',
desc: 'Determine Packet Type',
builder: yargs => dataMiddleware(yargs
builder: yargs => stdinMiddleware(yargs
.option('type', {
desc: 'Type of packet to determine the type for',
type: 'string',
Expand Down
30 changes: 21 additions & 9 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,30 @@ export function moduleHandler(module, name = () => 'default', args = 'data', for
}

/**
* Create a yargs middleware for handling data input
* Create a yargs middleware for handling input also from stdin
*
* @param {object} yargs the yargs object to extend
* @param {string} [key='data'] the key to use for the data
* @param {object} [opt={ optional: false }] the options to use
* @param {object} [options={ optional: false }] the options to use
* @param {function} [convert=fromHex] the conversion function to use
* @returns {object} the yargs object for chaining
*/
export function dataMiddleware(yargs, key = 'data', opt = { optional: false }) {
export function stdinMiddleware(yargs, key = 'data', options = { optional: false }, convert = fromHex) {
if (typeof key !== 'string') {
opt = key;
convert = options;
options = key;
key = 'data';
}
if (typeof options === 'function') {
convert = options;
options = { optional: false };
}
if (convert !== 'function') {
convert = fromHex;
}

return yargs
.positional(key, Object.assign({}, opt, {
.positional(key, Object.assign({}, options, {
type: 'string'
}))
.middleware(async argv => {
Expand All @@ -184,17 +193,20 @@ export function dataMiddleware(yargs, key = 'data', opt = { optional: false }) {
}

if (argv[key]) {
argv[key] = Buffer.from(argv[key], 'hex');
for (const alias of ((typeof opt?.alias === 'string' ? [opt.alias] : opt?.alias) || [])) {
if (typeof convert === 'function') {
argv[key] = convert(argv[key]);
}

for (const alias of ((typeof options?.alias === 'string' ? [options.alias] : options?.alias) || [])) {
argv[alias] = argv[key];
}
}
}, true)
.check((argv, options) => {
if (argv.help) {
return true;
} else if (!argv[key] && !opt?.optional) {
throw new TypeError(`Missing positional data argument [${key}] nor anything was piped to stdin`);
} else if (!argv[key] && !options?.optional) {
throw new TypeError(`Missing positional argument [${key}] nor anything was piped to stdin`);
}

return true;
Expand Down

0 comments on commit baee6b4

Please sign in to comment.