Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeffomatic/funcster
node.js function serialization/deserialization
https://github.com/jeffomatic/funcster
Last synced: 15 days ago
JSON representation
node.js function serialization/deserialization
- Host: GitHub
- URL: https://github.com/jeffomatic/funcster
- Owner: jeffomatic
- License: mit
- Created: 2013-03-26T06:38:02.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2019-06-20T05:00:48.000Z (over 5 years ago)
- Last Synced: 2024-09-20T01:06:28.727Z (about 2 months ago)
- Language: CoffeeScript
- Size: 43.9 KB
- Stars: 9
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# funcster
This library contains utilities for serializing and deserializing functions. It provides recursive traversal to discover both serialized and unserialized functions nested within objects and arrays. This is particularly useful for embedding functions into JSON objects.
## Security warning
This package performs the equivalent of `eval`, and thus should only be used to deserialize functions delivered from trusted sources. Do not use any of the deserialization functions on strings fron untrusted sources.
## Installation
npm install funcster
## Function reference
### serialize(function, [marker])
```js
serialize(function() { return "Hello world!" });
// -> { __js_function: 'function() { return "Hello world!" }' }
```----
### deepSerialize(root, [marker])
```js
lib = {
moduleA: {
functions: {
helloWorld: function() { return "Hello world!" }
}
},
moduleB: {
functions: {
goodbyeWorld: function() { return "Goodbye world!" }
}
},
};funcster.deepSerialize(lib);
// -> {
// moduleA: {
// functions: {
// helloWorld: { __js_function: 'function() { return "Hello world!" }' }
// }
// },
// moduleB: {
// functions: {
// goodbyeWorld: { __js_function: 'function() { return "Goodbye world!" }' }
// }
// },
// }
```----
### deepDeserialize(root, [marker, [moduleOpts]])
#### Security warning
`deepDeserialize` performs code evaluation on strings, and is susceptible to arbitrary code injection. Please make sure that `root` comes from a trusted source before using it.
#### Example
```js
serializedLib = {
moduleA: {
functions: {
helloWorld: { __js_function: 'function() { return "Hello world!" }' }
}
},
moduleB: {
functions: {
goodbyeWorld: { __js_function: 'function() { return "Goodbye world!" }' }
}
},
};deserializedLib = funcster.deepDeserialize(serializedLib);
deserializedLib.moduleA.functions.helloWorld(); // -> Hello world!
deserializedLib.moduleB.functions.goodbyeWorld(); // -> Hello world!
```Available options:
#### globals (object)
This option injects objects from the host context into the function evaluation context. The key is the name of the object inside the function evaluation context, and the value is the object in the host context.
```js
deserializedLib = funcster.deepDeserialize(serializedLib, {
globals: { foo: true }
});
```#### requires (object)
This option injects `require`-able modules into the function evaluation context. These modules will be **re-required** in the host context, generating distinct module objects to those that might already exist. This is a safer method of granting serialized functions access to common libraries.
```js
deserializedLib = funcster.deepDeserialize(serializedLib, {
requires: { _: 'underscore' }
});
```