The AdaptPacketContext class represents MUFL packets within TypeScript code.

Hierarchy

Table of contents

Accessors

Methods

Accessors

lifetime

get lifetime(): undefined | AdaptObjectLifetime

Returns

undefined | AdaptObjectLifetime

Inherited from

AdaptObject.lifetime

Defined in

common/dist/memory_management/adapt_object_lifetime.d.ts:54

set lifetime(lt): void

Parameters

Name Type
lt undefined | AdaptObjectLifetime

Returns

void

Inherited from

AdaptObject.lifetime

Defined in

common/dist/memory_management/adapt_object_lifetime.d.ts:55

Methods

Attach

Attach(lt?): AdaptPacketContext

Attach an object to the given lifetime object.

When a lifetime object has already been defined, first detach the object from the previous lifetime object, then attach it to the new one.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const lifetime1 = new AdaptObjectLifetime();
     const value = new AdaptValue(lifetime, 5);
     value.Attach(lifetime1);
     lifetime.Finalize(); // value is not destroyed by this call
     lifetime1.Finalize(); // value is destroyed
})

Parameters

Name Type Description
lt? AdaptObjectLifetime Lifetime to attach object to

Returns

AdaptPacketContext

Inherited from

AdaptObject.Attach

Defined in

common/dist/memory_management/adapt_object_lifetime.d.ts:76


Clone

Clone(lifetime?): AdaptPacketContext

Create a deep copy of AdaptPacketContext.

Throws

An error if ADAPT is not initialized (see Initialize or InitializeAsync)

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const path = "this/is/path/to/a/packet.muflp";
     const packet = AdaptPacketContext.LoadFromFile(lifetime, path);
     const packet_cloned = packet.Clone(undefined);
     lifetime.Finalize();

     // packet_cloned is still accessible because it was cloned and attached to the `undefined` lifetime object.

     packet_cloned.Destroy();  
   
})

Example

Missing clone

AdaptEnvironment.InitializeAsync().then(() => {
     const path = "this/is/path/to/a/packet.muflp";
     const packet = AdaptPacketContext.LoadFromFile(undefined, path);
     const packetCopyRef = packet;
     packet.Destroy();

     // packetCopyRef is an invalid packet because the object it references within C++ code has been destroyed.
})

Parameters

Name Type
lifetime? AdaptObjectLifetime

Returns

AdaptPacketContext

A cloned AdaptPacketContext

Overrides

AdaptObject.Clone

Defined in

adapt_js/src/adapt.ts:1031


CreateDictionary

CreateDictionary(): AdaptValue

Create an empty dictionary.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet: AdaptPacketContext; // assume it has been created correctly
     const dict = packet.CreateDictionary(); 

     lifetime.Finalize();
})

Returns

AdaptValue

An empty dictionary with the lifetime of the packet

Defined in

adapt_js/src/adapt.ts:1098


Destroy

Destroy(): void

Destroy the C++ object. After calling this method, the object reference is no longer valid and, thus, can’t be used.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const value = new AdaptValue(lifetime, 5);
     const anotherValue = value;
     const valueCloned = value.Clone(undefined);
     value.Destroy();
     // Both `value` and `anotherValue` can't be used because they are pointing to the same C++ object that has been deleted.
     // However, `valueCloned` is valid because it points to another (cloned) C++ object.
     valueCloned.Destroy();
     lifetime.Finalize();
})

Returns

void

Inherited from

AdaptObject.Destroy

Defined in

common/dist/memory_management/adapt_object_lifetime.d.ts:34


Detach

Detach(): AdaptPacketContext

Detach the value from the lifetime object.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const value = new AdaptValue(lifetime, 5);
     value.Detach(); // value must be destroyed manually now
     lifetime.Finalize(); // value is not destroyed by this call
     value.Destroy();
})

Returns

AdaptPacketContext

Inherited from

AdaptObject.Detach

Defined in

common/dist/memory_management/adapt_object_lifetime.d.ts:92


ExecuteFunction

ExecuteFunction(func_name, args?): AdaptValue

Execute a function in the packet. Note: calling functions in secure packets is not allowed.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet = AdaptEnvironment.EmptyPacket(lifetime, false); // not secure packet
     const platform_type_id = packet.ExecuteFunction("_get_platform_type_id"); // call without args (NIL argument is passed automatically)
     
     const strlen = packet.ExecuteFunction("_strlen", "hello").GetNumber(); // 5
     // the following call is equivalent
     const strlen1 = packet.ExecuteFunction("_strlen", ["hello"]).GetNumber(); // 5 

     const substring = packet.ExecuteFunction("_substr", ["hello", 1, 2]).GetString(); // "el"

     lifetime.Finalize();
})

Example

Secure packet
AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet = AdaptEnvironment.EmptyPacket(lifetime, true); // secure packet
     
     try {
         packet.ExecuteFunction("_strlen", "hello"); // calling a function in a secure packet is not allowed
     } catch (e) {} 

     lifetime.Finalize();
})

Parameters

Name Type Description
func_name string The name of the function to be called
args AdaptValueOrConvertible | AdaptValueOrConvertible[] The arguments of the function (can be either one argument or list of arguments)

Returns

AdaptValue

A function return value

Defined in

adapt_js/src/adapt.ts:1251


ExecuteTransaction

ExecuteTransaction(envelope, entropy_hex?, timestamp?): AdaptValue

Execute a transaction in the packet.

The envelope passed should be either user or external. A user envelope is defined as the type: ($name -> str, $targ -> any). The argument of the transaction is defined by the transaction in the MUFL code itself. An external envelope is created by the MUFL code using functions from the transaction.mm library. For details, refer to the code.

Example

Assume MUFL has a transaction named “::test::tr”

module test
{
     trn tr arg: int
     {
         _print "Transaction called. Argument is " arg "\n".
         return arg + 100.
     }
}

This transaction could be called from the TypeScript as follows:

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet: AdaptPacketContext; // assume packet has been created in a right way
     const arg = new AdaptValue(lifetime, 34);
     const envelope = packet.CreateDictionary().Mutate("name", "::test::tr").Mutate("targ", arg);
     const trn_result = packet.ExecuteTransaction(envelope, AdaptEnvironment.GetRandomBytes(lifetime, 256).Visualize().substring(2), AdaptEnvironment.SystemTime(lifetime));
     

     lifetime.Finalize(); 
})

Parameters

Name Type Description
envelope Convertible | AdaptValue -
entropy_hex? string A hex-encoded entropy without the ‘0x’ prefix
timestamp? Convertible | AdaptValue A timestamp

Returns

AdaptValue

The result of the transaction, which is the AdaptValue with the lifetime object of the packet.

Defined in

adapt_js/src/adapt.ts:1203


GetCodeID

GetCodeID(): AdaptValue

Get the code ID of the packet.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet = AdaptEnvironment.EmptyPacket(lifetime, true);
     const code_id = packet.GetCodeID();
     
     lifetime.Finalize();
})

Returns

AdaptValue

The code ID of the packet (the hash of the compiled MUFL code, which is the name of the corresponding .muflo file)

Defined in

adapt_js/src/adapt.ts:1315


GetContainerID

GetContainerID(): AdaptValue

Get the container ID of the MUFL packet.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet = AdaptEnvironment.EmptyPacket(lifetime, true);
     const container_id = packet.GetContainerID();
     
     lifetime.Finalize();
})

Returns

AdaptValue

The container ID of the packet

Defined in

adapt_js/src/adapt.ts:1295


GetHash

GetHash(): AdaptValue

Get the hash of the packet (sometimes referred to as the state ID of the packet).

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet = AdaptEnvironment.EmptyPacket(lifetime, true);
     const hash = packet.GetHash();
     
     lifetime.Finalize();
})

Returns

AdaptValue

The hash code of the packet

Defined in

adapt_js/src/adapt.ts:1274


NewBinaryFromBuffer

NewBinaryFromBuffer(buffer): AdaptValue

Create a MUFL value of domain BINARY from raw binary data.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet = AdaptEnvironment.EmptyPacket(lifetime, true);
     const value = new AdaptValue(lifetime, 45);
     const raw_data = value.Serialize(); // used just to obtain some binary data
     const value_bin = packet.NewBinaryFromBuffer(raw_data); // it doesn't deserealize the value. It just creates the binary value with the serialized content.

     lifetime.Finalize();
})

Parameters

Name Type Description
buffer Buffer Raw binary data

Returns

AdaptValue

An AdaptValue wrapping the MUFL value of domain BINARY

Defined in

adapt_js/src/adapt.ts:1157


NewBinaryFromHex

NewBinaryFromHex(hex): AdaptValue

Create a MUFL value of domain BINARY from the HEX string. Important! The ‘0x’ prefix of the HEX string must be omitted.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet: AdaptPacketContext = AdaptEnvironment.EmptyPacket(lifetime, true);
     const value = packet.NewBinaryFromHex("abcd");

     lifetime.Finalize();
})

Example

Invalid HEX format

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet: AdaptPacketContext = AdaptEnvironment.EmptyPacket(lifetime, true);
     try {
         const value = packet.NewBinaryFromHex("0xabcd"); // this value is invalid, '0x' prefix must be omitted
     } catch (e) {}

     lifetime.Finalize();
})

Parameters

Name Type Description
hex string A HEX string without ‘0x’ prefix

Returns

AdaptValue

An AdaptValue wrapping the MUFL value of domain BINARY

Defined in

adapt_js/src/adapt.ts:1134


ParseValue

ParseValue(value): AdaptValue

Create a new AdaptValue from a serialized MUFL value.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lilfetime = new AdaptObjectLifetime();
     const packet: AdaptPacketContext; // assume it has been created correctly
     const dict = packet.CreateDictionary().Mutate(1, "hello world");
     const serialized = dict.Serialize();
     const dict_copy = packet.ParseValue(serialized);

     lifetime.Finalize();
})

Parameters

Name Type Description
value Uint8Array A serialized MUFL value

Returns

AdaptValue

A parsed AdaptValue with the lifetime of the packet

Defined in

adapt_js/src/adapt.ts:1055


ParseValueFromJSON

ParseValueFromJSON(json): AdaptValue

Enable parsing the AdaptValue from JSON format.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet: AdaptPacketContext; // asuume it has been created correctly
     const value_json = '{"hello": 25, "world": false}'
     const value = packet.ParseValueFromJSON(value_json);
})

Parameters

Name Type Description
json string A JSON-formatted ADAPT value

Returns

AdaptValue

A parsed AdaptValue with the lifetime of the packet

Defined in

adapt_js/src/adapt.ts:1077


Serialize

Serialize(): Buffer

Serialize the packet.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const packet = AdaptEnvironment.EmptyPacket(lifetime, true);
     const serialized = packet.Serialize();
     const packet_copy = AdaptPacketContext.LoadFromContents(lifetime, serialized);     

     lifetime.Finalize();
})

Returns

Buffer

Raw binary data

Defined in

adapt_js/src/adapt.ts:1337


TransactionsList

TransactionsList(): string[]

Get a list of transactions available in the packet

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetiem();
     const packet = AdaptPacketContext.LoadFromFile(lifetime, "path/to/packet.muflp");
     const transactions = packet.TransactionList();
     console.log(transactions); // prints all transactions available in the packet

     lifetime.Finalize();

Returns

string[]

An array of transaction names

Defined in

adapt_js/src/adapt.ts:1357


LoadFromContents

Static LoadFromContents(lifetime, contents): AdaptPacketContext

Enable loading the packet from the raw packet binary data.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const path = "this/is/path/to/a/packet.muflp";
     const packet = AdaptPacketContext.LoadFromFile(lifetime, path);
     const packet_raw_data = packet.Serialize();
     const new_packet = AdaptPacketContext.LoadFromContents(lifetime, packet_raw_data);

     lifetime.Finalize();
})

Parameters

Name Type Description
lifetime undefined | AdaptObjectLifetime The lifetime object to attach the created packet to. If undefined is passed, the packet must be destroyed manually.
contents Buffer The raw packet binary data

Returns

AdaptPacketContext

A created packet

Defined in

adapt_js/src/adapt.ts:988


LoadFromFile

Static LoadFromFile(lifetime, path): AdaptPacketContext

Enable loading the packet from the .muflp file, which contains raw packet binary data.

Example

AdaptEnvironment.InitializeAsync().then(() => {
     const lifetime = new AdaptObjectLifetime();
     const path = "this/is/path/to/a/packet.muflp";
     const packet = AdaptPacketContext.LoadFromFile(lifetime, path);

     lifetime.Finalize();
})

Parameters

Name Type Description
lifetime undefined | AdaptObjectLifetime The lifetime object to attach the created packet to. If undefined is passed, the packet must be destroyed manually.
path string The path to the file containing the raw packet data

Returns

AdaptPacketContext

A created packet

Defined in

adapt_js/src/adapt.ts:961