time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

stdio.js (1157B)


      1 const aliases = ['stdin', 'stdout', 'stderr'];
      2 
      3 const hasAlias = options => aliases.some(alias => options[alias] !== undefined);
      4 
      5 export const normalizeStdio = options => {
      6 	if (!options) {
      7 		return;
      8 	}
      9 
     10 	const {stdio} = options;
     11 
     12 	if (stdio === undefined) {
     13 		return aliases.map(alias => options[alias]);
     14 	}
     15 
     16 	if (hasAlias(options)) {
     17 		throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`);
     18 	}
     19 
     20 	if (typeof stdio === 'string') {
     21 		return stdio;
     22 	}
     23 
     24 	if (!Array.isArray(stdio)) {
     25 		throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);
     26 	}
     27 
     28 	const length = Math.max(stdio.length, aliases.length);
     29 	return Array.from({length}, (value, index) => stdio[index]);
     30 };
     31 
     32 // `ipc` is pushed unless it is already present
     33 export const normalizeStdioNode = options => {
     34 	const stdio = normalizeStdio(options);
     35 
     36 	if (stdio === 'ipc') {
     37 		return 'ipc';
     38 	}
     39 
     40 	if (stdio === undefined || typeof stdio === 'string') {
     41 		return [stdio, stdio, stdio, 'ipc'];
     42 	}
     43 
     44 	if (stdio.includes('ipc')) {
     45 		return stdio;
     46 	}
     47 
     48 	return [...stdio, 'ipc'];
     49 };