{"id":13454488,"url":"https://github.com/mafintosh/peek-stream","last_synced_at":"2025-04-06T07:14:16.354Z","repository":{"id":18936640,"uuid":"22156258","full_name":"mafintosh/peek-stream","owner":"mafintosh","description":"Transform stream that lets you peek the first line before deciding how to parse it","archived":false,"fork":false,"pushed_at":"2023-03-31T11:29:03.000Z","size":9,"stargazers_count":57,"open_issues_count":3,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T06:06:31.206Z","etag":null,"topics":[],"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/mafintosh.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-07-23T15:17:38.000Z","updated_at":"2025-01-13T01:29:04.000Z","dependencies_parsed_at":"2024-01-02T20:55:02.334Z","dependency_job_id":null,"html_url":"https://github.com/mafintosh/peek-stream","commit_stats":{"total_commits":19,"total_committers":2,"mean_commits":9.5,"dds":"0.052631578947368474","last_synced_commit":"93ee7f3ef52de411293be0e6b37edcd541b9d756"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fpeek-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fpeek-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fpeek-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fpeek-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mafintosh","download_url":"https://codeload.github.com/mafintosh/peek-stream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445681,"owners_count":20939961,"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":[],"created_at":"2024-07-31T08:00:54.611Z","updated_at":"2025-04-06T07:14:16.329Z","avatar_url":"https://github.com/mafintosh.png","language":"JavaScript","funding_links":[],"categories":["Packages","Repository","JavaScript","包","目录","Streams","Modules"],"sub_categories":["Streams","文件流","流处理","流"],"readme":"# peek-stream\n\nTransform stream that lets you peek the first line before deciding how to parse it\n\n```\nnpm install peek-stream\n```\n\n[![build status](http://img.shields.io/travis/mafintosh/peek-stream.svg?style=flat)](http://travis-ci.org/mafintosh/peek-stream)\n![dat](http://img.shields.io/badge/Development%20sponsored%20by-dat-green.svg?style=flat)\n\n## Usage\n\n``` js\nvar peek = require('peek-stream')\nvar ldjson = require('ldjson-stream')\nvar csv = require('csv-parser')\n\nvar isCSV = function(data) {\n  return data.toString().indexOf(',') \u003e -1\n}\n\nvar isJSON = function(data) {\n  try {\n    JSON.parse(data)\n    return true\n  } catch (err) {\n    return false\n  }\n}\n\n// call parser to create a new parser\nvar parser = function() {\n  return peek(function(data, swap) {\n    // maybe it is JSON?\n    if (isJSON(data)) return swap(null, ldjson())\n\n    // maybe it is CSV?\n    if (isCSV(data)) return swap(null, csv())\n\n    // we do not know - bail\n    swap(new Error('No parser available'))\n  })\n}\n```\n\nThe above parser will be able to parse both line delimited JSON and CSV\n\n``` js\nvar parse = parser()\n\nparse.write('{\"hello\":\"world\"}\\n{\"hello\":\"another\"}\\n')\nparse.on('data', function(data) {\n  console.log(data) // prints {hello:'world'} and {hello:'another'}\n})\n```\n\nOr\n\n``` js\nvar parse = parser()\n\nparse.write('test,header\\nvalue-1,value-2\\n')\nparse.on('data', function(data) {\n  console.log(data) // prints {test:'value-1', header:'value-2'}\n})\n```\n\nPer default `data` is the first line (or the first `65535` bytes if no newline is found).\nTo change the max buffer pass an options map to the constructor\n\n``` js\nvar parse = peek({\n  maxBuffer: 10000\n}, function(data, swap) {\n  ...\n})\n```\n\nIf you want to emit an error if no newline is found set `strict: true` as well.\n\n\n## License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fpeek-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmafintosh%2Fpeek-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fpeek-stream/lists"}