first commit

This commit is contained in:
DanielRamirezGe
2020-01-14 20:43:08 -06:00
parent 3557894f7f
commit 5eecfbf6cd
26326 changed files with 2434803 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
2.0.0 / 2019-02-25
-------------------
- rewrite to fix edge cases
1.0.0 / 2015-06-18
------------------
- initial release
+53
View File
@@ -0,0 +1,53 @@
# buffer-json
```
npm install buffer-json
```
```js
const BJSON = require('buffer-json')
const str = BJSON.stringify({ buf: Buffer.from('hello') })
// => '{"buf":{"type":"Buffer","data":"base64:aGVsbG8="}}'
BJSON.parse(str)
// => { buf: <Buffer 68 65 6c 6c 6f> }
```
The [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) class in Node.js is used to represent binary data. JSON does not specify a way to encode binary data, so the Node.js implementation of `JSON.stringify` represents buffers as an object of shape `{ type: "Buffer", data: [<bytes as numbers>] }`. Unfortunately, `JSON.parse` does not turn this structure back into a `Buffer` object:
```
$ node
> JSON.parse(JSON.stringify({ buf: Buffer.from('hello world') }))
{ buf:
{ type: 'Buffer',
data: [ 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ] } }
```
`JSON.stringify` and `JSON.parse` accept arguments called [`replacer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter) and [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) respectively which allow customizing the parsing/encoding behavior. This module provides a replacer which encodes Buffer data as a base64-encoded string, and a reviver which turns JSON objects which contain buffer-like data (either as arrays of numbers or strings) into `Buffer` instances. All other types of values are parsed/encoded as normal.
## API
### `stringify(value[, space])`
Convenience wrapper for [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) with the `replacer` described below.
### `parse(text)`
Convenience wrapper for [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) with the `reviver` described below.
### `replacer(key, value)`
A [`replacer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter) implementation which turns every value that is a `Buffer` instance into an object of shape `{ type: 'Buffer', data: 'base64:<base64-encoded buffer content>' }`. Empty buffers are encoded as `{ type: 'Buffer', data: '' }`.
### `reviver(key, value)`
A [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) implementation which turns every object of shape `{ type: 'Buffer', data: <array of numbers or string> }` into a `Buffer` instance.
## Related modules
- [`buffer-json-encoding`](https://github.com/lachenmayer/buffer-json-encoding): an [`abstract-encoding`](https://github.com/mafintosh/abstract-encoding) compatible JSON encoder/decoder which uses this module.
## License
MIT
+60
View File
@@ -0,0 +1,60 @@
function stringify (value, space) {
return JSON.stringify(value, replacer, space)
}
function parse (text) {
return JSON.parse(text, reviver)
}
function replacer (key, value) {
if (isBufferLike(value)) {
if (isArray(value.data)) {
if (value.data.length > 0) {
value.data = 'base64:' + Buffer.from(value.data).toString('base64')
} else {
value.data = ''
}
}
}
return value
}
function reviver (key, value) {
if (isBufferLike(value)) {
if (isArray(value.data)) {
return Buffer.from(value.data)
} else if (isString(value.data)) {
if (value.data.startsWith('base64:')) {
return Buffer.from(value.data.slice('base64:'.length), 'base64')
}
// Assume that the string is UTF-8 encoded (or empty).
return Buffer.from(value.data)
}
}
return value
}
function isBufferLike (x) {
return (
isObject(x) && x.type === 'Buffer' && (isArray(x.data) || isString(x.data))
)
}
function isArray (x) {
return Array.isArray(x)
}
function isString (x) {
return typeof x === 'string'
}
function isObject (x) {
return typeof x === 'object' && x !== null
}
module.exports = {
stringify,
parse,
replacer,
reviver
}
+59
View File
@@ -0,0 +1,59 @@
{
"_from": "buffer-json@^2.0.0",
"_id": "buffer-json@2.0.0",
"_inBundle": false,
"_integrity": "sha512-+jjPFVqyfF1esi9fvfUs3NqM0pH1ziZ36VP4hmA/y/Ssfo/5w5xHKfTw9BwQjoJ1w/oVtpLomqwUHKdefGyuHw==",
"_location": "/buffer-json",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "buffer-json@^2.0.0",
"name": "buffer-json",
"escapedName": "buffer-json",
"rawSpec": "^2.0.0",
"saveSpec": null,
"fetchSpec": "^2.0.0"
},
"_requiredBy": [
"/cache-loader"
],
"_resolved": "https://registry.npmjs.org/buffer-json/-/buffer-json-2.0.0.tgz",
"_shasum": "f73e13b1e42f196fe2fd67d001c7d7107edd7c23",
"_spec": "buffer-json@^2.0.0",
"_where": "/home/george/citwa/red_de_investigacion_front/first/node_modules/cache-loader",
"author": {
"name": "JP Richardson"
},
"bugs": {
"url": "https://github.com/jprichardson/buffer-json/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "JSON.stringify & JSON.parse which can encode/decode buffers.",
"devDependencies": {
"standard": "^12.0.1",
"tape": "^4.10.1"
},
"homepage": "https://github.com/jprichardson/buffer-json#readme",
"keywords": [
"JSON",
"parse",
"stringify",
"buffer",
"reviver",
"replacer",
"base64"
],
"license": "MIT",
"main": "index.js",
"name": "buffer-json",
"repository": {
"type": "git",
"url": "git+https://github.com/jprichardson/buffer-json.git"
},
"scripts": {
"test": "standard --fix && node test.js"
},
"version": "2.0.0"
}