├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # random-access-latency 2 | 3 | A [random-access-storage](https://github.com/random-access-storage/random-access-storage) instance that wraps another one and adds latency 4 | 5 | ```sh 6 | npm install random-access-latency 7 | ``` 8 | 9 | ## Usage 10 | 11 | ```js 12 | const ral = require('random-access-latency') 13 | const ram = require('random-access-memory') 14 | 15 | // use ral to wrap another storage instance and add latency 16 | 17 | // add between 50 and 100ms latency to each operation 18 | const storage = ral([50, 100], ram()) 19 | 20 | // should have latency 21 | storage.write(42, Buffer.from('hi'), function () { 22 | storage.read(42, 2, console.log) 23 | }) 24 | ``` 25 | 26 | ## API 27 | 28 | #### `storage = ral(latency, otherStorage)` 29 | 30 | Wrap another store in random-access-storage instance that adds latency to all operations. 31 | If `latency` is an array a random latency is picked between the first and second number in the array. 32 | 33 | ## License 34 | 35 | MIT 36 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const ral = require('random-access-latency') 2 | const ram = require('random-access-memory') 3 | 4 | // use ral to wrap another storage instance and add latency 5 | 6 | const storage = ral([50, 100], ram()) // add between 50 and 100ms latency to each operation 7 | 8 | // should have latency 9 | storage.write(42, Buffer.from('hi'), function () { 10 | storage.read(42, 2, console.log) 11 | }) 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const ras = require('random-access-storage') 2 | 3 | module.exports = function (latency, other) { 4 | if (!Array.isArray(latency)) latency = [latency, latency] 5 | 6 | const delta = latency[1] - latency[0] 7 | 8 | return ras({ 9 | read: run(0), 10 | write: run(1), 11 | del: run(2), 12 | stat: run(3) 13 | }) 14 | 15 | function run (type) { 16 | return function (req) { 17 | const timeout = Math.round(Math.random() * delta + latency[0]) 18 | setTimeout(() => fwd(type, req), timeout) 19 | } 20 | } 21 | 22 | function fwd (type, req) { 23 | const cb = req.callback.bind(req) 24 | 25 | switch (type) { 26 | case 0: return other.read(req.offset, req.size, cb) 27 | case 1: return other.write(req.offset, req.data, cb) 28 | case 2: return other.del(req.offset, req.size, cb) 29 | case 3: return other.stat(cb) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-access-latency", 3 | "version": "1.0.0", 4 | "description": "A random-access-storage instance that wraps another one and adds latency", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard" 8 | }, 9 | "dependencies": { 10 | "random-access-storage": "^1.1.1" 11 | }, 12 | "devDependencies": { 13 | "standard": "^11.0.1" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/random-access-storage/random-access-latency.git" 18 | }, 19 | "author": "Mathias Buus (@mafintosh)", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/random-access-storage/random-access-latency/issues" 23 | }, 24 | "homepage": "https://github.com/random-access-storage/random-access-latency" 25 | } 26 | --------------------------------------------------------------------------------