{"id":15408925,"url":"https://github.com/sec-ant/torrefy","last_synced_at":"2025-04-19T03:12:00.259Z","repository":{"id":136828157,"uuid":"518481234","full_name":"Sec-ant/torrefy","owner":"Sec-ant","description":"An ESM package that uses Web Streams API to create v1, v2 or hybrid torrents in your web browser","archived":false,"fork":false,"pushed_at":"2023-01-28T16:51:14.000Z","size":288,"stargazers_count":11,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T23:05:37.288Z","etag":null,"topics":["bdecode","bencode","bittorrent","esm","infohash","metainfo","torrent","webstream","webtorrent"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/torrefy","language":"TypeScript","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/Sec-ant.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-27T13:59:46.000Z","updated_at":"2024-10-21T08:33:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"a739bb73-ed8f-47be-ac6e-04b6b1734960","html_url":"https://github.com/Sec-ant/torrefy","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/Sec-ant%2Ftorrefy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftorrefy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftorrefy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftorrefy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sec-ant","download_url":"https://codeload.github.com/Sec-ant/torrefy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249598153,"owners_count":21297463,"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":["bdecode","bencode","bittorrent","esm","infohash","metainfo","torrent","webstream","webtorrent"],"created_at":"2024-10-01T16:36:00.025Z","updated_at":"2025-04-19T03:12:00.229Z","avatar_url":"https://github.com/Sec-ant.png","language":"TypeScript","readme":"\u003cdiv align=\"center\"\u003e\n\u003cimg width=\"200\" src=\"https://user-images.githubusercontent.com/10386119/202842623-06e8ca3f-5761-41ed-9a8a-3a617b4e33a5.svg\"\u003e\n  \u003ch1\u003etorrefy\u003c/h1\u003e\n  \u003cp\u003e\n    \u003cimg src=\"https://img.shields.io/github/languages/top/Sec-ant/torrefy\" alt=\"GitHub top language\"\u003e \u003ca href=\"https://www.npmjs.com/package/torrefy\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/torrefy\" alt=\"npm version\"\u003e\u003c/a\u003e \u003ca href=\"https://www.npmjs.com/package/torrefy\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/torrefy\" alt=\"npm downloads\"\u003e\u003c/a\u003e \u003ca href=\"https://www.jsdelivr.com/package/npm/torrefy\"\u003e\u003cimg src=\"https://data.jsdelivr.com/v1/package/npm/torrefy/badge?style=rounded\" alt=\"\"\u003e\u003c/a\u003e \u003cimg src=\"https://img.shields.io/github/search/Sec-ant/torrefy/goto\" alt=\"GitHub search hit counter\"\u003e \u003ca href=\"https://openbase.com/js/torrefy?utm_source=embedded\u0026amp;utm_medium=badge\u0026amp;utm_campaign=rate-badge\"\u003e\u003cimg src=\"https://badges.openbase.com/js/rating/torrefy.svg?token=UY9uJPeXa2wpaK3OZLFien356kfd00deRlZejfs6B6g=\" alt=\"Rate this package\"\u003e\u003c/a\u003e\n  \u003c/p\u003e\n  \u003cp\u003e\n    An \u003ca href=\"https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules\"\u003eESM\u003c/a\u003e package that uses \u003ca href=\"https://developer.mozilla.org/docs/Web/API/Streams_API\"\u003eWeb Streams API\u003c/a\u003e to create v1, v2 or hybrid torrents in your web browser.\n  \u003c/p\u003e\n  \u003cp\u003e\n    🏗This package is under active development.🏗\n  \u003c/p\u003e\n\u003c/div\u003e\n\n## Install\n\n```bash\nnpm i torrefy # or yarn add torrefy\n```\n\n## Basic usage\n\n```ts\nimport { create, encode, decode } from \"torrefy\";\n\n// create a test file\nconst testFile = new File(\n  [\"Hello world. This is the test file content.\"],\n  \"testfile.txt\"\n);\n\n// calculate (hash) the meta info of the test file\nconst metaInfo = await create([testFile]);\n\n// bencode meta info into a readable stream\nconst torrentStream = encode(metaInfo);\n\n// tee the readable stream into two readable streams\nconst [torrentStream1, torrentStream2] = torrentStream.tee();\n\n// consume the first readable stream as an array buffer\nconst torrentBinary = await new Response(torrentStream1).arrayBuffer();\n\n// decode the second readable stream into meta info\nconst decodedMetaInfo = await decode(torrentStream2);\n```\n\n## Features\n\n### Supports Creating V1, V2 or Hybrid Torrents\n\nThis package supports creating [v1](http://bittorrent.org/beps/bep_0003.html), [v2](https://www.bittorrent.org/beps/bep_0052.html) ([introduction blog](https://blog.libtorrent.org/2020/09/bittorrent-v2/)) or [hybrid](https://www.bittorrent.org/beps/bep_0052.html#upgrade-path) ([introduction blog](https://blog.libtorrent.org/2020/09/bittorrent-v2/#:~:text=for%20backwards%20compatibility.-,backwards%20compatibility,-All%20new%20features)) torrents.\n\n### Covers Various Web File APIs\n\nThis package can handle input files or directories acquired from [File API](https://developer.mozilla.org/docs/Web/API/File), [File and Directory Entries API](https://developer.mozilla.org/docs/Web/API/File_and_Directory_Entries_API) or [File System Access API](https://developer.mozilla.org/docs/Web/API/File_System_Access_API).\n\n### Supports Comprehensive Options\n\nTBD\n\n### Supports Handling Progress\n\nTBD\n\n### Exposes Stream-Based APIs\n\nThe `create` function consumes an [iterable](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) of input files as [`ReadableStream`](https://developer.mozilla.org/docs/Web/API/ReadableStream)s with options and populates a `MetaInfo` object. This function internally uses several [`TransformStream`](https://developer.mozilla.org/docs/Web/API/TransformStream)s to chop the files into pieces and hash them.\n\nThe `encode` function consumes any bcodec friendly entity (e.g. `MetaInfo` object) and [bencode](http://bittorrent.org/beps/bep_0003.html#bencoding)s it into a `ReadableStream`.\n\nThe `decode` function consumes any bcodec friendly `ReadableStream` (e.g. torrent `ReadableStream`) and bdecodes it into the corresponding entity. This function internally uses a `TransformStream` called `Tokenizer` to tokenize the input `ReadableStream` and then calls `parse` function to parse the `Tokens`.\n\nAll `TransformStream`s used in this package are also exported.\n\n### Supports a Comprehensive Set of Bcodec Friendly Javascript Types\n\nBcodec friendly Javascript types includes (for the time being):\n\n| Bcodec Type \\ Javascript Type |                                                           `Strict`                                                            |                                                                        `Loose`                                                                        |\n| :---------------------------: | :---------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------: |\n|         `ByteString`          |                                [`string`](https://developer.mozilla.org/docs/Glossary/String)                                 |                   `string` [`ArrayBuffer`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)                    |\n|           `Integer`           | [`number`](https://developer.mozilla.org/docs/Glossary/Number) [`bigint`](https://developer.mozilla.org/docs/Glossary/BigInt) |                                  `number` `bigint` [`boolean`](https://developer.mozilla.org/docs/Glossary/Boolean)                                   |\n|            `List`             |                [`Strict[]`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)                 |                                                                       `Loose[]`                                                                       |\n|         `Dictionary`          |        [`{[key: string]: Strict}`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)         | ` {[key: string]: Loose}` \u003cbr/\u003e [`Map\u003cstring \\| ArrayBuffer, Loose\u003e`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map) |\n|            ignored            |                                                               -                                                               |                                                                  `undefined` `null`                                                                   |\n\n`encode` function supports all `Loose` type inputs and `decode` function always returns `Strict` type results.\n\n### Supports Hooks in Bencoding\n\nYou can register encoder hooks when using the `encode` function. A common use case is extracting the bencoded `info` dictionary and calculating the [`infohash`](http://bittorrent.org/beps/bep_0052.html#infohash). (This package doesn't provide an out-of-box function to calculate `infohash` for now)\n\nTo use encoder hooks, you will have to install the peer dependency [`@sec-ant/trie-map`](https://www.npmjs.com/package/@sec-ant/trie-map), which acts as an encoder hook system and allows you to register encoder hooks with iterable paths as keys in. Refer to its [README](https://github.com/Sec-ant/trie-map) to learn more about the package.\n\nThis package provides several helper functions to help you register hooks in a hook system and consume their results as you please: `useUint8ArrayStreamHook`, `useArrayBufferPromiseHook`, `useTextPromiseHook`. You can also define your own functions to register and use hooks.\n\nHere is probably how you should use this feature:\n\n```ts\nimport { encode, EncoderHookSystem, useArrayBufferPromiseHook } from \"torrefy\";\nimport { TrieMap } from \"@sec-ant/trie-map\";\n\n// create a dummy object to encode\nconst dummyObject = {\n  a: \"b\",\n  c: 1,\n  info: {\n    foo: \"bar\",\n  },\n  s: [\"t\"],\n};\n\n// initialize an encoder hook system\nconst hookSystem: EncoderHookSystem = new TrieMap();\n\n// register an encoder hook under dummyObject.info path in the hook system\n// and consume the result as an array buffer promise\nconst infoArrayBufferPromise = useArrayBufferPromiseHook([\"info\"], hookSystem);\n\n// pass the hook system as an input argument to the encode function\nconst bencodedReadableStream = encode(dummyObject, hookSystem);\n\n// consume the result of the hook\nconst infoArrayBuffer = await infoArrayBufferPromise; // =\u003e ArrayBuffer(12)\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsec-ant%2Ftorrefy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsec-ant%2Ftorrefy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsec-ant%2Ftorrefy/lists"}