[Implement] Naive Buffer.from, and friends by jtenner · Pull Request #6 · AssemblyScript/node

Implements Buffer.from<T>(value: T) and a few other convenience functions.

There are lots of obvious problems with not having the function overloads, but there are different paths we can take.

For instance, because there are no optional parameters, we must assume that the default encoding for strings is UTF8.

// utf-16 has a work around in AssemblyScript
let buff = Buffer.from(String.UTF16.encode(value));

// using Buffer.fromString(value, "utf16le");

Things to note about this function:

  • String[] objects do something complicated: string -> f64 -> u8 to remain compatible with node.
> Buffer.from(["3", "257.3", "15", "Infinity", "NaN"])
<Buffer 03 01 0F 00 00>
  • String objects need to be converted to UTF8
  • Buffer objects share their view of the same ArrayBuffer
  • ArrayBuffer objects are simply attached to a new Buffer
  • ArrayBufferView objects need to convert their values to u8.