{"id":19621310,"url":"https://github.com/commenthol/connect-chain-if","last_synced_at":"2026-05-13T01:05:22.149Z","repository":{"id":17961832,"uuid":"20951772","full_name":"commenthol/connect-chain-if","owner":"commenthol","description":"Compose a new middleware from a chain of middlewares to be called in series","archived":false,"fork":false,"pushed_at":"2022-11-12T11:46:57.000Z","size":33,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-20T17:48:31.599Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/commenthol.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":"2014-06-18T06:29:29.000Z","updated_at":"2022-11-22T13:40:51.000Z","dependencies_parsed_at":"2023-01-13T19:35:50.793Z","dependency_job_id":null,"html_url":"https://github.com/commenthol/connect-chain-if","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/commenthol/connect-chain-if","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fconnect-chain-if","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fconnect-chain-if/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fconnect-chain-if/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fconnect-chain-if/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/commenthol","download_url":"https://codeload.github.com/commenthol/connect-chain-if/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fconnect-chain-if/sbom","scorecard":{"id":300804,"data":{"date":"2025-08-11","repo":{"name":"github.com/commenthol/connect-chain-if","commit":"784809c8363887a65e629b5753089a668bfa1a82"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Code-Review","score":0,"reason":"Found 0/10 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-17T20:34:52.331Z","repository_id":17961832,"created_at":"2025-08-17T20:34:52.331Z","updated_at":"2025-08-17T20:34:52.331Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32963180,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-12T23:30:32.555Z","status":"ssl_error","status_checked_at":"2026-05-12T23:30:18.191Z","response_time":102,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-11-11T11:22:19.223Z","updated_at":"2026-05-13T01:05:22.108Z","avatar_url":"https://github.com/commenthol.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Connect-Chain-If\n\n\"connect-chain-if\" is a middleware that composes a new middleware of type `function(req, res, next)` from a chain of middlewares to be called in series.\nThe new middleware is fully [connect][connect] compliant.\n\nAn error passed to `next` or thrown in a middleware will short circuit the chain and be\npassed to the next error middleware of type `function(err, req, res, next)` in the connect middleware chain.\n\nYou can use \"connect-chain-if\" to build up new middlewares from existing connect-style-middlewares using the conditional expressions `chain.if()` and `chain.switch()`.\n\nTo allow breaking up long lasting middleware chains, all middlewares within a chain are processed via the node event loop using `process.nextTick` instead of being processed synchroniously. Use `chain.nextTick(false);` to revert to the synchronous behaviour.\n\n## Usage\n\n### Building new middlewares\n\n```javascript\nvar\n  express = require('express'),\n  chain = require('connect-chain-if');\n\nvar\n  app = express(),\n  middleware1 = function(req, res, next) {\n    res.body = req.url + '\\n';\n    next \u0026\u0026 next();\n  },\n  middleware2 = function(req, res, next) {\n    res.writeHead(200);\n    res.write(res.body || '');\n    res.end();\n    next \u0026\u0026 next();\n  };\n\nvar newMiddleware = chain([ middleware1, middleware2 ]);\n// or to maintain backward compatibility\nnewMiddleware = chain(middleware1, middleware2);\n\n// then use it in express/ connect\napp.use(newMiddleware);\n\napp.listen(3000);\n```\n\n### Building middlewares with error traps\n\n```javascript\nvar\n  chain = require('connect-chain-if');\n\nvar\n  middleware1 = function(req, res, next) {\n    // ... something bad happened\n    console.log('middleware1');\n    next \u0026\u0026 next(new Error('error'));\n  },\n  middleware2 = function(req, res, next) {\n    console.log('middleware2');\n    next \u0026\u0026 next();\n  },\n  middleware3 = function(req, res, next) {\n    console.log('middleware3');\n    next \u0026\u0026 next();\n  },\n  middlewareErr = function(err, req, res, next) { // note the \"4\" arguments\n    console.log('trapped error:', err.message);\n    next \u0026\u0026 next();\n  };\n    next \u0026\u0026 next();\n  };\n\nchain([\n  middleware1,\n  middleware2,\n  middlewareErr,\n  middleware3\n])({},{});\n```\n\nIn this example `middleware2` will be bypassed. Output is:\n\n```\nmiddleware1\ntrapped error: error\nmiddleware3\n```\n\n### Using connect middlewares without connect/ express\n\n```javascript\nvar\n  http = require('http'),\n  chain = require('connect-chain-if');\n\nvar\n  middleware1 = function(req, res, next) {\n    if (/^\\/[a-z]?$/.test(req.url)) {\n      res.body = req.url;\n      next \u0026\u0026 next();\n    }\n    else {\n      next \u0026\u0026 next(new Error('err'));\n    }\n  },\n  middleware2 = function(req, res, next) {\n    res.writeHead(200);\n    res.write(res.body || '');\n    res.end();\n  },\n  middlewareErr = function(err, req, res, next) {\n    res.writeHead(404);\n    res.end();\n  };\n\nhttp.createServer(chain(middleware1, middleware2, middlewareErr))\n  .listen(3000, 'localhost');\n```\n\n*NOTE:* Take care that in this case your very last middleware in the `chain` handles any error.\n\n```bash\ncurl -v localhost:3000/a\n#\u003e \u003c HTTP/1.1 200 OK\n#\u003e /a\ncurl -v localhost:3000/aaa\n#\u003e \u003c HTTP/1.1 404 Not Found\n```\n\n### Creating conditional middlewares\n\nYou can either use `chain.if` or `chain.switch` to define conditional middlewares.\n\n```javascript\nvar\n  chain = require('connect-chain-if');\n\nvar middleware = function(req, res, next) {\n  chain([\n    chain.if(\n      /*if */ /^\\/$/.test(req.url), [ // defines a new middleware chain here\n        function(req, res, next) {\n          res.body = 'homepage';\n          next \u0026\u0026 next();\n        },\n        function(req, res, next) {\n          res.statusCode = 200;\n          next \u0026\u0026 next();\n      }],\n      /*if */ /^\\/error500$/.test(req.url), function(req, res, next) {\n        res.body = 'weired';\n        res.statusCode = 500;\n        next \u0026\u0026 next();\n      },\n      /*else */ function(req, res, next) {\n        res.body = 'no idea';\n        res.statusCode = 404;\n        next \u0026\u0026 next();\n      }\n    ),\n    function (req, res, next) {\n      console.log(res.statusCode, res.body);\n      next \u0026\u0026 next();\n    }\n  ])(req, res, function (err) {\n    next \u0026\u0026 next(err);\n  });\n};\n\nmiddleware({ url: '/' }, {});\n//\u003e 200 'homepage'\nmiddleware({ url: '/error500'}, {});\n//\u003e 500 'weired'\nmiddleware({ url: '/something'}, {});\n//\u003e 404 'no idea'\n```\n\nUsing `chain.switch`:\n\n```javascript\nvar\n  chain = require('connect-chain-if');\n\nvar middleware = function(req, res, next) {\n  chain([\n    chain.switch(\n      /*switch */ req.url,\n      /*case */ '/', [ // defines a new middleware chain here\n        function(req, res, next) {\n          res.body = 'homepage';\n          next \u0026\u0026 next();\n        },\n        function(req, res, next) {\n          res.statusCode = 200;\n          next \u0026\u0026 next();\n      }],\n      /*case */ '/error500', function(req, res, next) {\n        res.body = 'weired';\n        res.statusCode = 500;\n        next \u0026\u0026 next();\n      },\n      /*default */ function(req, res, next) {\n        res.body = 'no idea';\n        res.statusCode = 404;\n        next \u0026\u0026 next();\n      }\n    ),\n    function (req, res, next) {\n      console.log(res.statusCode, res.body);\n      next \u0026\u0026 next();\n    }\n  ])(req, res, function (err) {\n    next \u0026\u0026 next(err);\n  });\n};\n\nmiddleware({ url: '/' }, {});\n//\u003e 200 'homepage'\nmiddleware({ url: '/error500'}, {});\n//\u003e 500 'weired'\nmiddleware({ url: '/something'}, {});\n//\u003e 404 'no idea'\n```\n\n## License\n\nSoftware is released under [MIT][license].\n\n[connect]: https://github.com/senchalabs/connect\n[license]: ./LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fconnect-chain-if","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommenthol%2Fconnect-chain-if","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fconnect-chain-if/lists"}