{"id":15668801,"url":"https://github.com/yoshuawuyts/nanostack","last_synced_at":"2025-09-14T08:32:09.528Z","repository":{"id":57308018,"uuid":"83165136","full_name":"yoshuawuyts/nanostack","owner":"yoshuawuyts","description":"Small middleware stack library","archived":false,"fork":false,"pushed_at":"2017-03-02T15:19:07.000Z","size":26,"stargazers_count":40,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-03T12:32:47.664Z","etag":null,"topics":["data-structure","frontend","javascript","middleware","stack"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yoshuawuyts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-25T21:46:48.000Z","updated_at":"2023-07-18T11:21:38.000Z","dependencies_parsed_at":"2022-09-12T11:44:27.924Z","dependency_job_id":null,"html_url":"https://github.com/yoshuawuyts/nanostack","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fnanostack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fnanostack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fnanostack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshuawuyts%2Fnanostack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yoshuawuyts","download_url":"https://codeload.github.com/yoshuawuyts/nanostack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232962559,"owners_count":18603378,"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":["data-structure","frontend","javascript","middleware","stack"],"created_at":"2024-10-03T14:19:26.573Z","updated_at":"2025-01-08T01:44:14.336Z","avatar_url":"https://github.com/yoshuawuyts.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nanostack [![stability][0]][1]\n[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nSmall middleware stack library. Analogous to [co][co] but without relying on\nfancy language features. Weighs `~0.4kb` gzipped.\n\n## Usage\n```js\nvar nanostack = require('nanostack')\nvar stack = nanostack()\n\nstack.push(function timeElapsed (ctx, next) {\n  var start = Date.now()\n\n  next(null, function (err, ctx, next) {\n    if (err) return next(err)\n    var now = Date.now()\n    var elapsed = start - now\n    console.log('time elapsed: ' + elapsed + 'ms')\n    next()\n  })\n})\n\nvar ctx = {}\nstack.walk(ctx, function (err, data, next) {\n  if (err) throw err\n})\n```\n\n## How does this work?\nA stack is a \"last-in, first-out\" type structure. The last thing that's added\nis also the first thing that's taken off when you \"unwind the stack\".\n\nIn `nanostack` we push middleware onto the stack. This means that middleware is\nfirst executed _upwards_ (e.g. next function in sequence) until `next()` is\ncalled without a callback. When that happens the stack starts to unwind, and\nmiddleware is executed _downwards_. You can think of the execution order like\nthis:\n\n```txt\n  Nanostack\n1.          7.  Middleware 1\n==============\n2.          6.  Middleware 2\n==============\n3.          5.  Middleware 3\n==============\n      4.        Middleware 4\n```\n```txt\nSequence: middleware 1, middleware 2, middleware 3\n          middleware 4, middleware 3, middleware 2\n          middleware 1\n```\nA keyd thing to note here is that any part of middleware can cause the stack to\nunwind. This is done by not passing a callback into the `next()` function. This\nis for example useful to handle create generic error handlers for the whole\nstack of middleware.\n\n## API\n### `stack = nanostack`\nCreate a new `nanostack` instance.\n\n### `stack.push(cb(ctx, next))`\nPush a new handler onto the middleware stack.\n\n### `stack.walk(ctx, next)`\nCall the functions on the middleware stack. Takes an initial context object and\na callback.\n\n### `next([err], [value], [handler])`\nCall the next function in the stack. If `handler` is not passed (e.g. last\nargument is not a function) the stack unwinds, calling all previous `handler`\nfunctions in reverse order (e.g. as a stack).\n\n## FAQ\n### Why did you write this?\nI realized that most frontend and backend plugin systems / middleware is often\nbest expressed as a stack that can execute some code at the start, handing off\ncontrol to a function up the stack and then execute some more code when it\nregains control (before handing control off again down the stack).\n\n`co` figured this out a while ago, but relying on newer language features\nprevented it from being generally applicable (for me). This package takes the\nsame ideas and allows them to run in more environments.\n\n### When shouldn't I use this?\nThis package is probably best left for frameworks to consume / when doing more\ncomplex-ish architecture things. Generally the last handler on the stack would\nbe a message bus / router that enables multiple handlers to resolve. If you're\nbuilding something simple that might be all you need actually. Or if you want\nto get fancy you might want to consider using a package that consumes this one\nand exposes a neato pattern on top.\n\n## Similar Packages\n- [tj/co][co]\n\n## License\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/nanostack.svg?style=flat-square\n[3]: https://npmjs.org/package/nanostack\n[4]: https://img.shields.io/travis/yoshuawuyts/nanostack/master.svg?style=flat-square\n[5]: https://travis-ci.org/yoshuawuyts/nanostack\n[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/nanostack/master.svg?style=flat-square\n[7]: https://codecov.io/github/yoshuawuyts/nanostack\n[8]: http://img.shields.io/npm/dm/nanostack.svg?style=flat-square\n[9]: https://npmjs.org/package/nanostack\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n[co]: https://github.com/tj/co\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshuawuyts%2Fnanostack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyoshuawuyts%2Fnanostack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshuawuyts%2Fnanostack/lists"}