README
@hyrious/marshal
Ruby marshal for the browser and Node.js.
Install
npm i @hyrious/marshal
Usage
import { dump, load } from '@hyrious/marshal'
dump(null) // ArrayBuffer { 04 08 30 }
load(Uint8Array.of(4, 8, 0x30).buffer) // null
Ruby ↔ JavaScript
ruby | javascript |
---|---|
nil |
null |
"string" |
"string" (utf-8 only, no instance variables) |
:symbol |
Symbol.for('symbol') (same above) |
123456 (Integer) |
123456 (number) |
123.456 (Float) |
123.456 (number) |
/cat/mixn |
/cat/im (regexp, see comments below) |
[] |
[] (no instance variables) |
{} |
RubyHash { pairs: [] } (no instance variables) |
Object.new |
RubyObject { className: Symbol(Object) } |
S = Struct.new; S.new |
RubyStruct { className: Symbol(S), pairs: [] } |
Object |
RubyClass { name: 'Object' } |
Math |
RubyModule { name: 'Math' } |
Note about RegExp:
JavaScript RegExp is different from Ruby Regexp. Only these flags are preserved:
flag | meaning |
---|---|
i |
ignore case |
m |
multi-line |
FAQ
How to pass a Buffer
in Node.js ?
buffer.buffer
.
How to get a binary string from ArrayBuffer (and reverse back) ?
You can use TextEncoder/TextDecoder. But please no, you won't expect a USVString at the most of the time. All string/symbol in ruby will be converted to js string automatically.
Do you support Stream API ?
Because the stream api in the browser and in node.js is not similar at all, it should be separated into 2 files and add exports
respectively. I'm not in a hurry to implement this feature. If you really need this please make a pr/issue to let me know.
Do you support BigInt ?
Currently not, but JavaScript's number
is big enough to store up to 2^53 - 1
.
Do you support ...?
Feel free to open an issue or submit a pr to improve this library.
Reference
- Marshal Format (official doc)
- node-marshal
- A little/another/final dip into Ruby's Marshal format
Todo
License
MIT @ hyrious