How to use the capnp-ts.Message function in capnp-ts

To help you get started, we’ve selected a few capnp-ts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jfrux / workbench / app / messages / event-capnp.js View on Github external
function Event (buf) {
  // force usage of new
  if (!(this instanceof Event)) {
    return new Event(buf);
  }
  this.msg = new capnp.Message(buf, false);
  this.event = this.msg.getRoot(CapnpEvent);

  this.toJSON = partial(toJSON, this.event);
}
github jdiaz5513 / capnp-ts / packages / js-examples / addressbook.js View on Github external
return readToEndOfStream(readStream).then(function(data) {
    const message = new capnp.Message(data);
    const addressBook = message.getRoot(AddressBook);
    addressBook.getPeople().forEach(function(person) {
      console.log(person.getName() + ': ' + person.getEmail());
      person.getPhones().forEach(function(phone) {
        let phoneTypeString = Person_PhoneNumber_Type[phone.getType()];
        console.log('  ' + phoneTypeString + ' phone: ' + phone.getNumber());
      });
      const employment = person.getEmployment();
      switch (employment.which()) {
        case Person_Employment.UNEMPLOYED:
          console.log('  unemployed');
          break;
        case Person_Employment.EMPLOYER:
          console.log('  employer: ' + employment.getEmployer());
          break;
        case Person_Employment.SCHOOL:
github jdiaz5513 / capnp-ts / packages / js-examples / addressbook.js View on Github external
function writeAddressBook(writeStream) {
  const message = new capnp.Message();
  const addressBook = message.initRoot(AddressBook);
  const people = addressBook.initPeople(2);

  const alice = people.get(0);
  alice.setId(123);
  alice.setName("Alice");
  alice.setEmail("alice@example.com");
  const alicePhones = alice.initPhones(1);
  alicePhones.get(0).setNumber("555-1212");
  alicePhones.get(0).setType(Person_PhoneNumber_Type.MOBILE);
  alice.getEmployment().setSchool("MIT");

  const bob = people.get(1);
  bob.setId(456);
  bob.setName("Bob");
  bob.setEmail("bob@example.com");
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / ast-creators.ts View on Github external
break;

    case s.Value.STRUCT:
      p = value.getStruct();

      break;

    case s.Value.INTERFACE:
    default:
      throw new Error(
        format(E.GEN_SERIALIZE_UNKNOWN_VALUE, s.Value_Which[value.which()])
      );
  }

  const m = new capnp.Message();
  m.setRoot(p);

  const buf = new Uint8Array(m.toPackedArrayBuffer());
  const bytes = new Array(buf.byteLength);

  for (let i = 0; i < buf.byteLength; i++) {
    bytes[i] = ts.createNumericLiteral(`0x${pad(buf[i].toString(16), 2)}`);
  }

  return ts.createCall(ts.createPropertyAccess(CAPNP, "readRawPointer"), __, [
    ts.createPropertyAccess(
      ts.createNew(ts.createIdentifier("Uint8Array"), __, [
        ts.createArrayLiteral(bytes, false)
      ]),
      "buffer"
    )
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / index.ts View on Github external
}).then(() => {
      const reqBuffer = new Buffer(
        chunks.reduce((l, chunk) => l + chunk.byteLength, 0)
      );

      let i = 0;

      chunks.forEach(chunk => {
        chunk.copy(reqBuffer, i);

        i += chunk.byteLength;
      });

      trace("reqBuffer (length: %d)", reqBuffer.length, reqBuffer);

      const message = new capnp.Message(reqBuffer, false);

      trace("message: %s", message.dump());

      const req = message.getRoot(s.CodeGeneratorRequest);

      trace("%s", req);

      const ctx = loadRequest(req);

      writeTsFiles(ctx);

      return ctx;
    });
  });