Skip to content

Latest commit

 

History

History
105 lines (74 loc) · 3.35 KB

api.md

File metadata and controls

105 lines (74 loc) · 3.35 KB

@savannstm/marshal

Table of Contents

load(data, loadOptions?)

Parse a Ruby Marshal data to a JavaScript value.

  • data {string | Uint8Array | ArrayBuffer} The Marshal data.
  • loadOptions {Object} Parse options.

loadOptions.stringMode: "utf8" | "binary"

Force decode or not decode string values.

data = Marshal.dump(["foo", "foo".force_encoding("binary")])
load(data); // => ["foo", { __type: "bytes", data: [102, 111, 111] }]
load(data, { stringMode: "utf8" }); // => ["foo", "foo"]
load(data, { stringMode: "binary" }); // => [{ __type: "bytes", data: [102, 111, 111] }, { __type: "bytes", data: [102, 111, 111] }]

loadOptions.instanceVarPrefix: string

If set, changes "@" instance variables prefix to passed.

If you've loaded files with this option, also use it in dump.

load(data); // => [{ "__symbol__@instanceVar": 5 }]
load(data, { instanceVarPrefix: "" }); // => [{ "__symbol__instanceVar": 5 }]
load(data, { instanceVarPrefix: "_" }); // => [{ "__symbol___instanceVar": 5 }]

loadOptions.decodeKnown: { class }

Decode Ruby objects as same-class JavaScript objects.

class A end
data = Marshal.dump(A.new)
class A {}
load(data); // => RubyObject { class: Symbol(A) }
load(data, { decodeKnown: { A } }); // => A {}

dump(value, dumpOptions?)

Encode a JavaScript value into Ruby marshal data. Returns a Uint8Array. Note that the Uint8Array may not always be the same length as its underlying buffer. You should always check the byteOffset and byteLength when accessing the buffer.

  • value {unknown} The JavaScript value.
  • dumpOptions {Object} Encode options.

dumpOptions.instanceVarPrefix: string

Set it to the same value as in load().

dumpOptions.encodeKnown: { class }

Encode JavaScript objects into same-name Ruby objects.

class A {}
dump(new A()); // Error: Cannot dump type object of object [object Object]
dump(new A(), { encodeKnown: { A } }); // => ruby: #<A>

dumpOptions.encodeUnknown: (obj) => string

This is an alter to the error case of dumpOptions.encodeKnown. It should return a string indicating the Ruby class name to encode into. If you return null or empty string, it fallbacks to throw the error.

dump(new A(), { encodeUnknown: (a) => a.constructor?.name }); // => ruby: #<A>

dumpOptions.maxByteLength: number

If set, force maximum size of the output buffer to passed. Passed number should be bytes (!) number. Default value is 16000000 (16 MB).

dump(hugeStructure); // => RangeError: ArrayBuffer.prototype.resize: Invalid length parameter
dump(hugeStructure, { maxByteLength: 32000000 }); // => All good