Sharding Utility for CI Services

2.1.0 · maintenance · verified Sun Apr 19

ShardyMcShardFace is a JavaScript/TypeScript utility library designed to efficiently shard arrays of items across multiple parallel processes, primarily for Continuous Integration (CI) environments. It leverages standard CI environment variables like `CI_NODE_INDEX` and `CI_NODE_TOTAL` (via the `ci-parallel-vars` dependency) to determine the current shard and total shard count, distributing items as evenly as possible. The current stable version is 2.1.0, released in November 2019. Since then, the project appears to be in a maintenance-only state, with no new releases in over five years. Key features include stable, random-seeded item distribution, graceful handling of scenarios where the item count is less than the total shard count, and full TypeScript support, ensuring type safety. It offers both a programmatic API for direct integration into JavaScript/TypeScript applications and a command-line interface for simple shell scripting. Its primary differentiator is its direct, opinionated integration with common CI parallelization parameters, simplifying the setup for distributed test suites or build steps.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically shard an array of items using `shardy-mc-shard-face`. It simulates CI environment variables and shows both basic usage and how to apply custom options like a stable seed.

import { shard as shardyMcShardFace } from 'shardy-mc-shard-face';

// Simulate CI environment variables for local testing
process.env.CI_NODE_INDEX = '0'; // Current shard index (0-based)
process.env.CI_NODE_TOTAL = '2'; // Total number of shards

const allItems = [
  'test_file_A.spec.ts',
  'test_file_B.spec.ts',
  'test_file_C.spec.ts',
  'test_file_D.spec.ts',
  'test_file_E.spec.ts',
  'test_file_F.spec.ts'
];

// Basic sharding, options are optional
const myShard = shardyMcShardFace(allItems);

console.log(`Running on CI node ${process.env.CI_NODE_INDEX} of ${process.env.CI_NODE_TOTAL}`);
console.log('My assigned shard:', myShard);

// Example with custom options and seed for stable distribution
const customShard = shardyMcShardFace(allItems, {
  throwOnEmpty: false, // Don't throw if shard is empty
  seed: 'my-stable-seed' // Ensures same items always go to same shard
});

console.log('My assigned shard with custom seed:', customShard);

view raw JSON →