Nondeterministic Polynomial Munchies
Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

amf0-ts

1.3.9 • Public • Published

AMF0-TS

npm version

Fast AMF0 library written in pure Typescript with 0 dependencies, supporting TS, Node and the browser.

Installation

npm i amf0-ts

Type compatibility table

Type Serialize Deserialize Reason for exclusion
Null ✔️ ✔️ -
Undefined ✔️ ✔️ -
Boolean ✔️ ✔️ -
Number ✔️ ✔️ -
String ✔️ ✔️ -
Long string ✔️ ✔️ -
Object ✔️ ✔️ -
Object end ✔️ ✔️ -
Reference ✔️ ✔️ -
ECMA array ✔️ ✔️ -
Date ✔️ ✔️ -
Map ✔️ ✔️ -
Set ✔️ ✔️ -
Unsupported ✔️ ✔️ -
Typed object ✔️ ✔️ -
XML Document Who uses XML?
Strict array No remoting support
AVMPlus AMF0 only

Extra compatibility table

Name Supported
Class alias ✔️
Dynamic property writer ✔️

Usage

Below you'll find simple usage examples.

Basic types

Working with basic types is very self explanatory.

Typescript

import { AMF0, ECMAArray } from 'amf0-ts';
 
const obj: object = { id: 1 };
const ref: object = { ref1: obj, ref2: obj };
const mixed: ECMAArray = Object.assign([], [1,2,3], { 'Key': 'Value' });
const arr: ECMAArray = [1,2,3];
 
AMF0.parse(AMF0.stringify(obj));
AMF0.parse(AMF0.stringify(ref));
AMF0.parse(AMF0.stringify(mixed));
AMF0.parse(AMF0.stringify(arr));

Node

const { AMF0 } = require('amf0-ts');
 
const obj = { id: 1 };
const ref = { ref1: obj, ref2: obj };
const mixed = Object.assign([], [1,2,3], { 'Key': 'Value' })
const arr = [1,2,3];
 
AMF0.parse(AMF0.stringify(obj));
AMF0.parse(AMF0.stringify(ref));
AMF0.parse(AMF0.stringify(mixed));
AMF0.parse(AMF0.stringify(arr));

Browser

<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
 
<script>
  const obj = { id: 1 };
  const ref = { ref1: obj, ref2: obj };
  const mixed = Object.assign([], [1,2,3], { 'Key': 'Value' })
  const arr = [1,2,3];
 
  AMF0.parse(AMF0.stringify(obj));
  AMF0.parse(AMF0.stringify(ref));
  AMF0.parse(AMF0.stringify(mixed));
  AMF0.parse(AMF0.stringify(arr));
</script> 

Changes

This library has a few changes:

  • Movieclip has been changed to Map
  • Recordset has been changed to Set

Typescript

import { AMF0 } from 'amf0-ts';
 
const map: Map<string, any> = new Map([[1, 'Value']]);
const set: Set<any> = new Set([1, 'A', 2, 'B', 3, 'C']);
 
AMF0.parse(AMF0.stringify(map));
AMF0.parse(AMF0.stringify(set));

Node

const { AMF0 } = require('amf0-ts');
 
const map = new Map([['Key', 'Value']]);
const set = new Set([1, 'A', 2, 'B', 3, 'C']);
 
AMF0.parse(AMF0.stringify(map));
AMF0.parse(AMF0.stringify(set));

Browser

<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
 
<script>
  const map = new Map([['Key', 'Value']]);
  const set = new Set([1, 'A', 2, 'B', 3, 'C']);
 
  AMF0.parse(AMF0.stringify(map));
  AMF0.parse(AMF0.stringify(set));
</script> 

Typed class

This library also supports typed classes.

Typescript

import { AMF0 } from 'amf0-ts';
 
class Person {
  public name:string;
  public age:number;
 
  constructor(name:string, age:number) {
    this.name = name;
    this.age = age;
  }
}
 
AMF0.registerClassAlias('com.person', Person);
AMF0.isRegisteredClassAlias(Person); // Or 'com.person'
 
const person: Person = new Person('Daan', 18);
 
AMF0.parse(AMF0.stringify(person));
AMF0.deregisterClassAlias(Person); // Or 'com.person'

Node

const { AMF0 } = require('amf0-ts');
 
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}
 
AMF0.registerClassAlias('com.person', Person);
AMF0.isRegisteredClassAlias(Person); // Or 'com.person'
 
const person = new Person('Daan', 18);
 
AMF0.parse(AMF0.stringify(person));
AMF0.deregisterClassAlias(Person); // Or 'com.person'

Browser

<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
 
<script>
  class Person {
    constructor(name, age) {
      this.name = name;
      this.age = age;
    }
  }
 
  AMF0.registerClassAlias('com.person', Person);
  AMF0.isRegisteredClassAlias(Person); // Or 'com.person'
 
  const person = new Person('Daan', 18);
 
  AMF0.parse(AMF0.stringify(person));
  AMF0.deregisterClassAlias(Person); // Or 'com.person'
</script> 

Dynamic property writer

This library also supports Dynamic property writers.

Typescript

import { AMF0, IDynamicPropertyWriter, IDynamicPropertyOutput } from 'amf0-ts';
 
class HideSensitive implements IDynamicPropertyWriter {
  constructor() { }
 
  public writeDynamicProperties(obj: object, output: IDynamicPropertyOutput): void {
    for (const prop in obj) {
      if (prop.toLowerCase() !== 'password') {
        output.writeDynamicProperty(prop, obj[prop]);
      }
    }
  }
}
 
AMF0.setDynamicPropertyWriter(new HideSensitive());
AMF0.hasDynamicPropertyWriter(); // True
 
AMF0.parse(AMF0.stringify({ id: 1, username: 'Zaseth', password: '123123' }));
 
AMF0.setDynamicPropertyWriter(null); // Reset
AMF0.hasDynamicPropertyWriter(); // False

Node

const { AMF0 } = require('amf0-ts');
 
class HideSensitive {
  constructor() { }
 
  writeDynamicProperties(obj, output) {
    for (const prop in obj) {
      if (prop.toLowerCase() !== 'password') {
        output.writeDynamicProperty(prop, obj[prop]);
      }
    }
  }
}
 
AMF0.setDynamicPropertyWriter(new HideSensitive());
AMF0.hasDynamicPropertyWriter(); // True
 
AMF0.parse(AMF0.stringify({ id: 1, username: 'Zaseth', password: '123123' }));
 
AMF0.setDynamicPropertyWriter(null); // Reset
AMF0.hasDynamicPropertyWriter(); // False

Browser

<script src="https://cdn.jsdelivr.net/npm/amf0-ts@latest/browser/AMF0.js"></script>
 
<script>
  class HideSensitive {
    constructor() { }
 
    writeDynamicProperties(obj, output) {
      for (const prop in obj) {
        if (prop.toLowerCase() !== 'password') {
          output.writeDynamicProperty(prop, obj[prop]);
        }
      }
    }
  }
 
  AMF0.setDynamicPropertyWriter(new HideSensitive());
  AMF0.hasDynamicPropertyWriter(); // True
 
  AMF0.parse(AMF0.stringify({ id: 1, username: 'Zaseth', password: '123123' }));
 
  AMF0.setDynamicPropertyWriter(null); // Reset
  AMF0.hasDynamicPropertyWriter(); // False
</script> 

Install

npm i amf0-ts

DownloadsWeekly Downloads

16

Version

1.3.9

License

MIT

Unpacked Size

117 kB

Total Files

37

Last publish

Collaborators

  • avatar