{"id":32977159,"url":"https://github.com/bbarr/medium","last_synced_at":"2026-02-01T18:35:22.954Z","repository":{"id":1208108,"uuid":"37478270","full_name":"bbarr/medium","owner":"bbarr","description":"A functional CSP library using ES7 async/await keywords","archived":false,"fork":false,"pushed_at":"2024-06-16T14:07:43.000Z","size":451,"stargazers_count":55,"open_issues_count":2,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-04T19:54:40.017Z","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/bbarr.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":"2015-06-15T16:51:48.000Z","updated_at":"2025-02-07T18:03:35.000Z","dependencies_parsed_at":"2022-08-06T10:15:43.847Z","dependency_job_id":null,"html_url":"https://github.com/bbarr/medium","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bbarr/medium","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbarr%2Fmedium","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbarr%2Fmedium/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbarr%2Fmedium/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbarr%2Fmedium/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bbarr","download_url":"https://codeload.github.com/bbarr/medium/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbarr%2Fmedium/sbom","scorecard":{"id":227730,"data":{"date":"2025-08-11","repo":{"name":"github.com/bbarr/medium","commit":"881e31e5a02b1171d79cd12a50a08ecdec09ee16"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.2,"checks":[{"name":"Code-Review","score":0,"reason":"Found 2/23 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":"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":"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":"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":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","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":"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":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/test.yml:1","Info: no jobLevel write permissions found"],"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":"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":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/bbarr/medium/test.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/bbarr/medium/test.yml/master?enable=pin","Warn: npmCommand not pinned by hash: .github/workflows/test.yml:21","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 npmCommand dependencies pinned"],"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":"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":"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":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"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"}},{"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":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 9 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":4,"reason":"6 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2","Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-17T04:08:51.981Z","repository_id":1208108,"created_at":"2025-08-17T04:08:51.982Z","updated_at":"2025-08-17T04:08:51.982Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28985818,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T18:17:03.387Z","status":"ssl_error","status_checked_at":"2026-02-01T18:16:57.287Z","response_time":56,"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":"2025-11-13T06:00:32.662Z","updated_at":"2026-02-01T18:35:22.949Z","avatar_url":"https://github.com/bbarr.png","language":"JavaScript","readme":"# Medium\nCSP-style channel library using ES7 async/await keywords.\n\n#### Installation\n\n```javascript\nnpm install medium\n```\n\n#### First, the requisite naive ping/pong example (ported from [Go](https://talks.golang.org/2013/advconc.slide#6))\n```javascript\n\nconst { chan, put, close, take, sleep, CLOSED } = require('medium')\n\nconst player = async (name, table) =\u003e {\n  while (true) {\n\n    const ball = await take(table)\n    if (ball === CLOSED) break\n\n    ball.hits++\n    console.log(`${name} ${ball.hits}`)\n    await sleep(100)\n    put(table, ball)\n  }\n}\n\nconst start = async () =\u003e {\n\n  const table = chan()\n\n  player('ping', table)\n  player('pong', table)\n\n  put(table, { hits: 0 })\n  await sleep(1000)\n\n  close(table)\n}\n\nstart()\n\n```\n\n#### Channel interactions in a nutshell\n\nChannels are queues, you can ```put``` things onto them and ```take``` things off, in a first-in-first-out way. Channels can be closed, after which, they will not receive or deliver values. ```put``` and ```take``` are both asynchronous actions, and return promises. ```put``` promises simply resolve to ```true``` if it was able to successfully add its value to the channel, or ```false``` if the channel is closed. ```take``` promises resolve either to whatever was next in the channel queue, or to the constant ```CLOSED``` if the channel is closed. For example:\n\n```javascript\nconst ch1 = chan()\nput(ch1, 1)\ntake(ch1).then(console.log)\n// LOGS: 1\n\ntake(ch1).then(console.log)\nput(ch1, 2)\n// LOGS: 2\n\n// Notice how it doesn't matter what order the take and put occur in. This is the secret sauce of coordinating asynchronous activites.\n\ntake(ch1).then(console.log)\nclose(ch1)\n// LOGS: CLOSED\n\nput(ch1, 3).then(console.log)\n// LOGS: false\n```\n\nThe strategy with which a channel handles an excess of ```put```s is implemented as a ```buffer```. The default channel does not allow for any buffered values, so if you ```put``` without a waiting ```take``` for the value, it will not resolve the ```put``` until a corresponding ```take``` is added. For example:\n\n#### No buffer\n```javascript\nconst ch1 = chan()\nput(ch1, 1).then(() =\u003e console.log('put 1'))\nput(ch1, 2).then(() =\u003e console.log('put 2'))\ntake(ch1)\n// LOGS: 'put 1'\ntake(ch1)\n// LOGS: 'put 2'\n```\n\nAn example of a different buffer would be a \"fixed\" buffer, which has N slots for ```put``` values to wait for a ```take```. For example:\n\n#### Fixed buffer\n```javascript\nconst ch = chan()\nconst fixedCh = chan(buffers.fixed(2)) // or shortcut with chan(2)\n\nput(ch, 1).then(console.log)\n// LOGS NOTHING\n\nput(fixedCh, 1).then(() =\u003e console.log('put 1'))\n// LOGS: put 1\nput(fixedCh, 2).then(() =\u003e console.log('put 2'))\n// LOGS: put 2\nput(fixedCh, 3).then(() =\u003e console.log('put 3'))\n// LOGS NOTHING\n\ntake(fixedCh).then(console.log)\n// LOGS: 1\n// LOGS: put 3\n\n```\n\nThe other included buffers are, \"dropping\", which allows N puts, then begins \"dropping\" them, causing the put to resolve successfully but the value is not added to the channel, and \"sliding\", which allows N puts, then begins shifting the buffer, dropping the oldest buffered ```put``` value and adding the newest to the other end.\n\n#### Dropping buffer\n```javascript\nconst ch = chan(buffers.dropping(2))\nput(ch, 1)\nput(ch, 2)\nput(ch, 3) // this is dropped\ntake(ch).then(console.log)\n// LOGS: 1\ntake(ch).then(console.log)\n// LOGS: 2\ntake(ch).then(console.log)\n// LOGS NOTHING\nput(ch, 3)\n// LOGS: 3\n```\n\n#### Sliding buffer\n```javascript\nconst ch = chan(buffers.sliding(2))\nput(ch, 1)\nput(ch, 2)\nput(ch, 3) // this causes the put of 1 to be dropped\ntake(ch).then(console.log)\n// LOGS: 2\ntake(ch).then(console.log)\n// LOGS: 3\n\n```\n\n#### Building something larger\n\nThings get much more interesting though when we use async/await to better coordinate our channels.\n\n```javascript\nimport { chan, put, take, sleep, go } from 'medium'\n\nconst numbers = chan()\nconst oddNumbers = chan()\n\ngo(async () =\u003e {\n  while (true) {\n    console.log('an odd number: ', await take(oddNumbers))\n  }\n})\n\ngo(async () =\u003e {\n  while (true) {\n    let n = await numbers // awaiting a channel is an implied \"take\"\n    if (n % 2 === 1)\n      await put(oddNumbers, n)\n  }\n})\n\ngo(async () =\u003e {\n  while (true) {\n    let randomNum = Math.floor(Math.random() * 100)\n    await put(numbers, randomNum)\n    await sleep(1000)\n  }\n})\n\n```\n\nSo we have a number being generated every second, and put onto the ```numbers``` channel. This is consumed and tested for \"oddness\", and if it passes, then it is put onto the ```oddNumbers``` channel where it is simply console.log'ed.\n\nWhat if we want to keep track of the percent odd vs. even? We can put a bit of local state in the process that checks for oddness. However, mutating state sucks, so, we use the function ```repeat``` to both act as a ```while``` loop and manage state immutably!\n\n```javascript\nimport { chan, put, take, sleep, go, repeat } from 'medium'\n\nconst numbers = chan()\nconst oddNumbers = chan()\nconst stats = chan()\n\ngo(async () =\u003e {\n  while (true) {\n    console.log('an odd number: ', await oddNumbers)\n  }\n})\n\ngo(async () =\u003e {\n  while (true) {\n    console.log('Stats: ', await stats)\n  }\n})\n\ngo(async () =\u003e {\n  repeat(async ({ total, odds }) =\u003e {\n    put(stats, `${odds / total * 100}% odd numbers`)\n    \n    const n = await numbers\n    if (n % 2) {\n      put(oddNumbers, n)\n      return { total: total + 1, odds: odds + 1 }\n    } else {\n      return { total: total + 1, odds }\n    }\n    \n  }, { total: 0, odds: 0 })\n})\n\ngo(async () =\u003e {\n  while (true) {\n    let randomNum = Math.floor(Math.random() * 100)\n    await put(numbers, randomNum)\n    await sleep(1000)\n  }\n})\n```\n\nAnd now we see that, indeed, our universe isn't broken and over time our cumalitive chance of an odd number closes in on 50%.\n\nWe can even take our ```repeat``` function one step further, and use ```repeatTake```, since that is exactly what we are doing.\n\n```javascript\ngo(async () =\u003e {\n  repeatTake(numbers, async (n, { total, odds }) =\u003e {\n    put(stats, `${odds / total * 100}% odd numbers`)\n    \n    if (n % 2) {\n      put(oddNumbers, n)\n      return { total: total + 1, odds: odds + 1 }\n    } else {\n      return { total: total + 1, odds }\n    }\n    \n  }, { total: 0, odds: 0 })\n})\n```\n\nSo we just change the signature a bit, and our local \"repeat\" state is passed as the second argument instead of the first. \n\nMore documentation is coming, but the core functionality is ~160LOC, so it should \njust take a single cup of coffee to read through. I wanted to be sure that the API was built \ndeliberately, and not just a port from some previous effort.\n\n## API \n\n### chan(numOrBuffer=null) -\u003e Chan\nCreates a channel. All arguments are optional.\n**numOfBuffer** - Any number or buffer. A number is a shortcut for ```buffers.fixed(number)```.\n\n### put(ch, val) -\u003e Promise -\u003e true|false\nPuts a value onto a channel. Returned promise resolves to true if successful, or false if the channel is closed.\n\n### take(ch) -\u003e Promise -\u003e takenValue|CLOSED\nTakes a value from a channel. Returned Promise resolves to taken value or CLOSED constant if the channel is closed.\n\n### go(async function) -\u003e promiseFromInvokedAsyncFunction\nImmediately invokes (and returns) given function.\n\n### sleep(ms) -\u003e Promise\nCreates a promise that will resolve successfully after ```ms``` milliseconds.\n\n### CLOSED\nA constant, which all takes on a closed channel receive instead of a value.\n\n### close(ch) -\u003e undefined\nCloses a channel. This causes:\n- all puts and pending puts to resolve to false\n- all takes and pending takes to resolve to the CLOSED constant\n\n### clone(ch) -\u003e Chan\nMakes a new channel, same as the old channel.\n\n### any(...ports) -\u003e Promise -\u003e [theResolvedValue, theSourceChannelOrPromise]\nLike ```alts``` in Clojure's ```core-async```.\n\n```ports``` can be a channel to take from, a promise to resolve, or an array \nto put data onto a channel, like ```[ theChannel, valueToPut ]```.\n\nIf none of them have a pending value, it will resolve with whichever channel receives a value next.\nIf one of the channels has a pending value already, it will simply resolve to that.\nIf more than one channel has a pending value, it selects one in a non-deterministic fashion.\n\nAlways resolves with a double of ```[ theResolvedValue, theSourceChannel ]```.\n\nAll non-winning actions will be canceled so that their data does not go missing.\n\n### repeat(async function, seed=null) -\u003e undefined\nThis functions like a while loop, except you can track state using its return value. Return false to end. Return a value other than false, and it will be available as the argument to your callback async function. Pass in a ```seed``` value as the second argument to repeat.\n\n### repeatTake(ch, async function, seed=null) -\u003e undefined\nThis is just like ```repeat``` above, except that before it repeats, it waits for a successful ```take``` on the given channel. Then it passes this taken value in as the first argument, with any local state being passed as the second argument.\n\nSee the ping/pong example above to see this in action.\n\n### merge(...chs)\nCreates a new channel that will receive all puts to the received channels.\n\n## buffers\n### buffers.unbuffered()\nNo buffer space. The default choice for when first argument to ```chan``` is falsy.\n### buffers.fixed(num)\nBuffer has space of ```num```. Any extra ```put```s are parked.\n### buffers.sliding(num)\nBuffer simply slides across pending puts as a window of ```num``` width. So, oldest puts are dropped as new ones are added.\n### buffers.dropping(num)\nBuffer drops, and resolves, any extra puts beyond ```num```.\n","funding_links":[],"categories":["Libraries"],"sub_categories":["[Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbarr%2Fmedium","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbarr%2Fmedium","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbarr%2Fmedium/lists"}