{"id":22220840,"url":"https://github.com/nokia/ts-serialize-closures","last_synced_at":"2025-04-05T23:05:12.876Z","repository":{"id":57157478,"uuid":"195021958","full_name":"nokia/ts-serialize-closures","owner":"nokia","description":"TypeScript Serialization module","archived":false,"fork":false,"pushed_at":"2025-03-21T15:10:21.000Z","size":222,"stargazers_count":45,"open_issues_count":3,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-29T22:04:06.049Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nokia.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":"2019-07-03T09:27:29.000Z","updated_at":"2025-02-20T11:45:38.000Z","dependencies_parsed_at":"2024-06-19T04:01:24.405Z","dependency_job_id":"946ee7ad-c306-4eb0-8316-341a3261e866","html_url":"https://github.com/nokia/ts-serialize-closures","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nokia%2Fts-serialize-closures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nokia%2Fts-serialize-closures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nokia%2Fts-serialize-closures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nokia%2Fts-serialize-closures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nokia","download_url":"https://codeload.github.com/nokia/ts-serialize-closures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411226,"owners_count":20934653,"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-12-02T23:10:23.899Z","updated_at":"2025-04-05T23:05:12.852Z","avatar_url":"https://github.com/nokia.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\n[![npm version](https://badge.fury.io/js/serialize-closures.svg)](https://badge.fury.io/js/serialize-closures)\n[![npm version](https://badge.fury.io/js/ts-closure-transform.svg)](https://badge.fury.io/js/ts-closure-transform)\n[![Build Status](https://travis-ci.org/nokia/ts-serialize-closures.svg?branch=master)](https://travis-ci.org/nokia/ts-serialize-closures)\n\n# ts-serialize-closures\n\nSerialize your TypeScript functions!\n\n`ts-serialize-closures` can serialize and deserialize arbitrary TypeScript/JavaScript object graphs. That includes:\n\n  * **functions, which may have captured data,**\n  * `Date` and `RegExp` objects,\n  * cyclic graphs,\n  * prototypes,\n  * references to built-in objects,\n  * etc.\n\nThe idea is that `serialize` creates a self-contained snapshot of all program state relevant to the object being serialized. The `deserialize` function decodes that snapshot back to a JavaScript object graph.\n\nThis tool might be useful in a number of scenarios:\n\n  * Exchanging functions between different processes. That's often a useful tool for building distributed systems.\n\n  * Lightweight remote post-mortem debugging: have failing processes create a neat little snapshot of their current state and send yourself that snapshot for analysis.\n\n## Usage\n\nThe serializer (`serialize-closures`) requires a preprocessing step (`ts-closure-transform`). Therefore, the typical usage of this library is to configure webpack to automatically transform the source code using a hook in the TypeScript compiler (`tsc`). Take the following steps to set up a stand-alone example:\n\n  1. Prepare project and install dev-dependencies\n```bash\nmkdir example \u0026\u0026 cd example\nnpm init\nnpm install --save-dev ts-closure-transform serialize-closures webpack webpack-cli typescript ts-loader util\n```\n\n  2.1 Configure `tsconfig.json`:\n```json\n{\n    \"compileOnSave\": true,\n    \"compilerOptions\": {\n        \"target\": \"ES5\",\n        \"module\": \"commonjs\",\n        \"declaration\": true,\n        \"moduleResolution\": \"node\",\n        \"stripInternal\": true,\n        \"jsx\": \"react\",\n        \"outDir\": \"dist\"\n    },\n    \"include\": [\n        \"src/**/*\"\n    ],\n    \"exclude\": [\n        \"node_modules\",\n        \"dist\"\n    ]\n}\n```\n\n  2.2 Configure `webpack.config.js`:\n```javascript\nconst tsClosureTransform = require('ts-closure-transform');\nconst path = require('path');\nmodule.exports = {\n  entry: {\n    example: './src/example.ts',\n  },\n  mode: 'development',\n  module: {\n    rules: [\n      {\n        test: /.tsx?$/,\n        loader: 'ts-loader', // or 'awesome-typescript-loader'\n        options: {\n          getCustomTransformers: () =\u003e ({\n            before: [tsClosureTransform.beforeTransform()],\n            after: [tsClosureTransform.afterTransform()]\n          })\n        }\n      }\n    ]\n  },\n  resolve: {\n    extensions: [ '.tsx', '.ts', '.js' ],\n    fallback: {\n      \"util\": require.resolve(\"util/\"),\n    },\n  },\n  output: {\n    path: path.join(__dirname, 'dist'),\n    filename: '[name].bundle.js',\n  }\n}\n```\n\n  3. Write code `src/example.ts` to serialize and deserialize arbitrary functions:\n```typescript\nimport { serialize, deserialize } from 'serialize-closures';\n// Just about anything can be serialized by calling `serialize`.\nlet capturedVariable = 5;\nlet serialized = serialize(() =\u003e capturedVariable);\n\n// Serialized representations can be stringified and parsed.\nlet text = JSON.stringify(serialized);\nlet parsed = JSON.parse(text);\n\n// Serialized representations can be deserialized by calling `deserialize`.\nconsole.log(deserialize(serialized)()); // Prints '5'.\nconsole.log(deserialize(parsed)());     // Prints '5'.\n```\n\n  4. Compile with webpack and run the sample\n```bash\nnpx webpack\nnode dist/example.bundle.js\n```\n\n## Components\n\nThe serializer consists of two components.\n\n  1. `ts-closure-transform`: a transformation to inject in the TypeScript compiler's pass pipeline. This transformation will rewrite all function definitions to include a special `__closure` property. The serializer uses that `__closure` property to figure out which variables are captured by the function.\n\n      How you inject this transform depends on the webpack loader you're using. For `ts-loader` and `awesome-typescript-loader`, you can do the following:\n\n      ```typescript\n      import { beforeTransform, afterTransform } from 'ts-closure-transform';\n      // ...\n      loader: 'ts-loader',\n      options: {\n        getCustomTransformers: () =\u003e ({\n          before: [beforeTransform()],\n          after: [afterTransform()]\n        })\n      }\n      // ...\n      ```\n\n      Note that `ts-closure-transform` is strictly a dev dependency: there's no need to package it with your application.\n\n  2. `serialize-closures`: a runtime library that defines the `serialize` and `deserialize` functions. These should work for any object graph as long as all source code has first been processed by `ts-closure-transform`.\n\n\n## Limitations\n\n`ts-serialize-closures` works fairly well for modest object graphs, but it does have a number of limitations you should be aware of:\n\n  * Variable-capturing functions defined in files that have not been transformed by `ts-closure-transform` cannot be deserialized correctly.\n\n  * Serializing class definitions works, but only if they are first lowered to function definitions by the TypeScript compiler, i.e., the target is ES5 or lower.\n\n  * Functions can only be serialized and deserialized *once.* There is no support for serializing a deserialized function.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnokia%2Fts-serialize-closures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnokia%2Fts-serialize-closures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnokia%2Fts-serialize-closures/lists"}