{"id":15592058,"url":"https://github.com/derhuerst/simulate-network-conditions","last_synced_at":"2025-04-15T05:02:41.293Z","repository":{"id":57361609,"uuid":"266656653","full_name":"derhuerst/simulate-network-conditions","owner":"derhuerst","description":"Simulate lossy or high-latency network conditions.","archived":false,"fork":false,"pushed_at":"2020-05-30T22:56:08.000Z","size":17,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T05:02:14.313Z","etag":null,"topics":["latency","network","nodejs","packet-loss"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/derhuerst.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-25T01:31:52.000Z","updated_at":"2022-08-08T23:01:17.000Z","dependencies_parsed_at":"2022-09-26T16:40:59.017Z","dependency_job_id":null,"html_url":"https://github.com/derhuerst/simulate-network-conditions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fsimulate-network-conditions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fsimulate-network-conditions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fsimulate-network-conditions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fsimulate-network-conditions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derhuerst","download_url":"https://codeload.github.com/derhuerst/simulate-network-conditions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249010198,"owners_count":21197797,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["latency","network","nodejs","packet-loss"],"created_at":"2024-10-02T23:53:52.776Z","updated_at":"2025-04-15T05:02:41.276Z","avatar_url":"https://github.com/derhuerst.png","language":"JavaScript","funding_links":["https://github.com/sponsors/derhuerst"],"categories":[],"sub_categories":[],"readme":"# simulate-network-conditions\n\n**Simulate [lossy](https://en.wikipedia.org/wiki/Packet_loss) or high-latency network conditions.** Like the [throttling option in the Chrome/Chromium DevTools](https://developers.google.com/web/tools/chrome-devtools/network#throttle).\n\n[![npm version](https://img.shields.io/npm/v/simulate-network-conditions.svg)](https://www.npmjs.com/package/simulate-network-conditions)\n[![build status](https://api.travis-ci.org/derhuerst/simulate-network-conditions.svg?branch=master)](https://travis-ci.org/derhuerst/simulate-network-conditions)\n![ISC-licensed](https://img.shields.io/github/license/derhuerst/simulate-network-conditions.svg)\n![minimum Node.js version](https://img.shields.io/node/v/simulate-network-conditions.svg)\n[![chat with me on Gitter](https://img.shields.io/badge/chat%20with%20me-on%20gitter-512e92.svg)](https://gitter.im/derhuerst)\n[![support me via GitHub Sponsors](https://img.shields.io/badge/support%20me-donate-fa7664.svg)](https://github.com/sponsors/derhuerst)\n\n\n## Installation\n\n```shell\nnpm install simulate-network-conditions\n```\n\n\n## Usage\n\nLet's simulate an unreliable transport protocol (like UDP, unlike TCP) over a bad 2G connection:\n\n```js\nconst {pipeline} = require('stream')\nconst simulate = require('simulate-network-conditions')\n\nconst signalStrenth = t =\u003e t % 20_000 / 20_000\n\nconst lossy = simulate.lossByTime((t, i) =\u003e {\n\tif (signalStrenth(t) \u003c= .3) {\n\t\t// packet loss from now on, for 3s\n\t\treturn {from: t, length: 3000}\n\t}\n\treturn null // no packet loss\n})\n\nconst slow = simulate.latency(() =\u003e {\n\tconst s = signalStrenth(Date.now())\n\tconst jitter = Date.now() % 100 / 100\n\treturn Math.floor((6 + jitter - s) * 50)\n})\n\n// A -\u003e 2G network -\u003e B\npipeline(peerA, simulate(lossy), simulate(slow), peerB)\n// B -\u003e 2G network -\u003e A\npipeline(peerB, simulate(lossy), simulate(slow), peerA)\n```\n\n`simulate()` returns a [transform stream](https://nodejs.org/docs/latest-v10.x/api/stream.html#stream_class_stream_transform) in [object mode](https://nodejs.org/docs/latest-v10.x/api/stream.html#stream_object_mode).\n\nThe stream only represents *one-way* of network traffic. **If you want two-way network traffic as usually required, use *two* `emulate()` streams and [`duplexer3`](https://www.npmjs.com/package/duplexer3)**:\n\n```js\nconst duplexer = require('duplexer3')\n\nconst aToB = simulate([ /* … */ ])\nconst bToA = simulate([ /* … */ ])\n\n// A end of the connection\nconst aEnd = duplexer3(aToB, bToA)\n// B end of the connection\nconst bEnd = duplexer3(bToA, aToB)\n```\n\n### packet processors\n\nA\n\n```js\n// every packet delayed by exactly .2s\nsimulate.constantLatency(200)\n\n// jitter: packets randomly delayed by .1-.25s (breaks order!)\nsimulate.latency(() =\u003e 100 + Math.ceil(Math.random() * 150))\n\n// 2% lost (randomly)\nsimulate.constantLoss(.02)\n\n// 3 packets lost, every 20 packets\nsimulate.lossByIdx((idx) =\u003e ({from: idx + 17, length: 3}))\nsimulate.lossByIdx((idx, lossPeriodIdx) =\u003e {\n\treturn {from: lossPeriodIdx * 20, length: 3}\n})\n\n// 500ms loss every 3s\nsimulate.lossByTime(() =\u003e ({from: Date.now() + 3_000, length: 500}))\n```\n\n\n## Related\n\nThe great [Mininet](http://mininet.org/overview/) [allows you to specify bandwidth, latency \u0026 loss](https://github.com/mininet/mininet/blob/dfb297901fd4bf0ad0cad317973180392dafa277/examples/simpleperf.py#L33-L36), and probably works in a much more accurate way. You can use the [`mininet` npm package](https://github.com/mafintosh/mininet) from Node.js.\n\n\n## Contributing\n\nIf you have a question or need support using `simulate-network-conditions`, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use [the issues page](https://github.com/derhuerst/simulate-network-conditions/issues).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhuerst%2Fsimulate-network-conditions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderhuerst%2Fsimulate-network-conditions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhuerst%2Fsimulate-network-conditions/lists"}