{"id":16880545,"url":"https://github.com/squaremo/node-spb","last_synced_at":"2025-04-11T20:11:55.958Z","repository":{"id":2057033,"uuid":"2995156","full_name":"squaremo/node-spb","owner":"squaremo","description":"Size-prefixed blob streams for Node.JS - OLD EXPERIMENT","archived":false,"fork":false,"pushed_at":"2011-12-22T13:53:52.000Z","size":137,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T20:11:51.211Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/squaremo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-12-16T14:31:34.000Z","updated_at":"2022-10-23T14:30:32.000Z","dependencies_parsed_at":"2022-07-18T08:30:41.148Z","dependency_job_id":null,"html_url":"https://github.com/squaremo/node-spb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squaremo%2Fnode-spb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squaremo%2Fnode-spb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squaremo%2Fnode-spb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squaremo%2Fnode-spb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/squaremo","download_url":"https://codeload.github.com/squaremo/node-spb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248473129,"owners_count":21109628,"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-10-13T15:59:09.747Z","updated_at":"2025-04-11T20:11:55.930Z","avatar_url":"https://github.com/squaremo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Size-prefixed blob streams\n\nIt is often useful to treat a continuous byte stream as a series of\ndiscrete messages. This library gives you a simple but effective way\nto do this, by prefacing each message with the size of the message. In\nother words, it saves you writing a bit of fiddly parsing code if you\njust want to deal with whole things (e.g., JSON objects) at a time.\n\nYou can wrap streams (e.g., `net.Socket`) or things that look like\n`net.Server`. If you wrap a stream, then it treats incoming data as\nsize-prefix encoded and hands you each message (without the size\nprefix) as a `'data'` event; if you write a string or buffer, it\ntreats it as a message and encode it with a size prefix. If you wrap a\nserver, it provides wrapped streams in the `'connection'` event.\n\n## Trivial example\n\n    \u003e var netserver = require('net').createServer();\n    \u003e var msgserver = require('spb').server(netserver);\n    \u003e msgserver.on('connection', function(connection) {\n    ... connection.on('data', function(message) {\n    ...... // do something with the whole message\n    ...... });\n    ... });\n    \u003e netserver.listen(5975);\n\n## Reference\n\n### **`spb.stream(underlying)`**\n\nWraps a [Stream](http://nodejs.org/docs/latest/api/streams.html).\n\nThe returned stream shall parse bytes from the underlying stream and\nemit `'data'` events consisting of whole messages (without the length\nprefix). Buffers or strings written to the returned stream shall be\nencoded with a size-prefix and written to the underlying stream.\n\nThe wrapped stream relays events (`'drain'`, `'error'`, `'end'`,\n`'pipe'`) from the underlying stream. Aside from `'data'` it is\nequivalent to give a callback to either the underlying stream or the\nwrapped stream.\n\nLikewise, the methods `pause`, `resume`, `destroy`, and `destroySoon`\nfor the wrapped stream simply invoke the same on the underlying\nstream. It is not in general safe to invoke either `write` or\n`setEncoding` on the underlying stream, as these interfere with the\nsize prefixing. `setEncoding` on the wrapped stream does the 'right\nthing', in other words, it results in the messages emitted being\nstrings.\n\n### **`spb.server(underlying)`**\n\nWraps a server, providing connections that are wrapped streams.\n\nThe underlying server must look like a `net.Server`. Specifically, it\nmust emit the `'listening'`, `'close'` and `'connection'` events, and\nhave a `close` method. It is safe to invoke methods and to supply\ncallbacks for the above events on the underlying server; only\n`'connection'` is not equivalent.\n\n## A note on pipes\n\nPiping a spb stream to a byte stream will simply strip out size\nprefixes, effectively concatenating the messages into undelimited\nbytes. Piping a byte stream to a spb stream will encode as discrete\nmessages the (probably arbitrarily delimited) packets read on the byte\nstream.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsquaremo%2Fnode-spb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsquaremo%2Fnode-spb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsquaremo%2Fnode-spb/lists"}