You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add unsafe flag for straight serialisation
* Update index.js
Fixed indentation
* Added tests and updated docs
* Fixed formatting and bumped version
* Formatting (fix last space too)
* Update package.json
* Remove venv
* Catch typo...
... and clarifying that options is an object,
* Fixed more spelling errors
I caught a few old ones too :)
Copy file name to clipboardExpand all lines: README.md
+17-4
Original file line number
Diff line number
Diff line change
@@ -11,9 +11,12 @@ Serialize JavaScript to a _superset_ of JSON that includes regular expressions,
11
11
12
12
The code in this package began its life as an internal module to [express-state][]. To expand its usefulness, it now lives as `serialize-javascript` — an independent package on npm.
13
13
14
-
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps** or **dates**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client.
14
+
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps** or **dates**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client. But this module is also great for communicating between node processes.
15
+
16
+
The string returned from this package's single export function is literal JavaScript which can be saved to a `.js` file, or be embedded into an HTML document by making the content of a `<script>` element.
17
+
18
+
> **HTML characters and JavaScript line terminators are escaped automatically.**
15
19
16
-
The string returned from this package's single export function is literal JavaScript which can be saved to a `.js` file, or be embedded into an HTML document by making the content of a `<script>` element. **HTML charaters and JavaScript line terminators are escaped automatically.**
17
20
18
21
## Installation
19
22
@@ -67,9 +70,11 @@ The above will produce the following string, HTML-escaped output which is safe t
67
70
'{"haxorXSS":"\\u003C\\u002Fscript\\u003E"}'
68
71
```
69
72
73
+
> You can pass an optional `unsafe` argument to `serialize()` for straight serialization.
74
+
70
75
### Options
71
76
72
-
The `serialize()` function accepts `options` as its second argument. There are two options, both default to being`undefined`:
77
+
The `serialize()` function accepts an `options`object as its second argument. All options are being defaulted to`undefined`:
73
78
74
79
#### `options.space`
75
80
@@ -89,9 +94,17 @@ This option is a signal to `serialize()` that the object being serialized does n
89
94
serialize(obj, {isJSON:true});
90
95
```
91
96
97
+
#### `options.unsafe`
98
+
99
+
This option is to signal `serialize()` that we want to do a straight conversion, without the XSS protection. This options needs to be explicitly set to `true`. HTML characters and JavaScript line terminators will not be escaped. You will have to roll your own.
100
+
101
+
```js
102
+
serialize(obj, {unsafe:true});
103
+
```
104
+
92
105
## Deserializing
93
106
94
-
For some use cases you might also need to deserialize the string. This is explicitely not part of this module. However, you can easily write it yourself:
107
+
For some use cases you might also need to deserialize the string. This is explicitly not part of this module. However, you can easily write it yourself:
0 commit comments