Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paged_results_control.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var assert = require('assert-plus');
  2. var util = require('util');
  3. var asn1 = require('asn1');
  4. var Control = require('./control');
  5. ///--- Globals
  6. var BerReader = asn1.BerReader;
  7. var BerWriter = asn1.BerWriter;
  8. ///--- API
  9. function PagedResultsControl(options) {
  10. assert.optionalObject(options);
  11. options = options || {};
  12. options.type = PagedResultsControl.OID;
  13. if (options.value) {
  14. if (Buffer.isBuffer(options.value)) {
  15. this.parse(options.value);
  16. } else if (typeof (options.value) === 'object') {
  17. this._value = options.value;
  18. } else {
  19. throw new TypeError('options.value must be a Buffer or Object');
  20. }
  21. options.value = null;
  22. }
  23. Control.call(this, options);
  24. }
  25. util.inherits(PagedResultsControl, Control);
  26. Object.defineProperties(PagedResultsControl.prototype, {
  27. value: {
  28. get: function () { return this._value || {}; },
  29. configurable: false
  30. }
  31. });
  32. PagedResultsControl.prototype.parse = function parse(buffer) {
  33. assert.ok(buffer);
  34. var ber = new BerReader(buffer);
  35. if (ber.readSequence()) {
  36. this._value = {};
  37. this._value.size = ber.readInt();
  38. this._value.cookie = ber.readString(asn1.Ber.OctetString, true);
  39. //readString returns '' instead of a zero-length buffer
  40. if (!this._value.cookie)
  41. this._value.cookie = new Buffer(0);
  42. return true;
  43. }
  44. return false;
  45. };
  46. PagedResultsControl.prototype._toBer = function (ber) {
  47. assert.ok(ber);
  48. if (!this._value)
  49. return;
  50. var writer = new BerWriter();
  51. writer.startSequence();
  52. writer.writeInt(this.value.size);
  53. if (this.value.cookie && this.value.cookie.length > 0) {
  54. writer.writeBuffer(this.value.cookie, asn1.Ber.OctetString);
  55. } else {
  56. writer.writeString(''); //writeBuffer rejects zero-length buffers
  57. }
  58. writer.endSequence();
  59. ber.writeBuffer(writer.buffer, 0x04);
  60. };
  61. PagedResultsControl.prototype._json = function (obj) {
  62. obj.controlValue = this.value;
  63. return obj;
  64. };
  65. PagedResultsControl.OID = '1.2.840.113556.1.4.319';
  66. ///--- Exports
  67. module.exports = PagedResultsControl;