Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
package-lock.json
22 changes: 10 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const b4a = require('b4a')

module.exports = codecs

codecs.ascii = createString('ascii')
Expand All @@ -12,15 +14,11 @@ codecs.binary = {
name: 'binary',
encode: function encodeBinary (obj) {
return typeof obj === 'string'
? Buffer.from(obj, 'utf-8')
: Buffer.isBuffer(obj)
? obj
: Buffer.from(obj.buffer, obj.byteOffset, obj.byteLength)
? b4a.from(obj, 'utf-8')
: b4a.toBuffer(obj)
},
decode: function decodeBinary (buf) {
return Buffer.isBuffer(buf)
? buf
: Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
return b4a.toBuffer(buf)
}
}

Expand Down Expand Up @@ -49,16 +47,16 @@ function createJSON (newline) {
name: newline ? 'ndjson' : 'json',
encode: newline ? encodeNDJSON : encodeJSON,
decode: function decodeJSON (buf) {
return JSON.parse(buf.toString())
return JSON.parse(b4a.toString(buf))
}
}

function encodeJSON (val) {
return Buffer.from(JSON.stringify(val))
return b4a.from(JSON.stringify(val))
}

function encodeNDJSON (val) {
return Buffer.from(JSON.stringify(val) + '\n')
return b4a.from(JSON.stringify(val) + '\n')
}
}

Expand All @@ -67,10 +65,10 @@ function createString (type) {
name: type,
encode: function encodeString (val) {
if (typeof val !== 'string') val = val.toString()
return Buffer.from(val, type)
return b4a.from(val, type)
},
decode: function decodeString (buf) {
return buf.toString(type)
return b4a.toString(buf, type)
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "2.2.0",
"description": "Create an binary encoder/decoder for json, utf-8 or custom types",
"main": "index.js",
"dependencies": {},
"dependencies": {
"b4a": "^1.1.1"
},
"devDependencies": {
"standard": "^12.0.1",
"tape": "^4.10.1"
Expand Down