How to use the bodec.isBinary function in bodec

To help you get started, we’ve selected a few bodec 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 creationix / wheaty / server.js View on Github external
}
      for (var path in recording.paths) {
        var actual = (yield repo.pathToEntry(root, path)).hash;
        var expected = recording.paths[path];
        if (actual !== expected) {
          console.log("change in " + path + " from " + expected + " to " + actual);
          break outer;
        }
      }
      return [304, recording.headers];
    }
  }
  if (result === false) result = (yield* execute(root, code, url));
  if (result) {
    var headers = result[1];
    if (bodec.isBinary(result[2])) {

      // Auto deflate text files if request accepts it.
      if (/\b(?:text|javascript)\b/.test(headers["Content-Type"]) &&
          /\bgzip\b/.test(req.headers["accept-encoding"])) {
        result[2] = yield function (callback) {
          zlib.gzip(result[2], callback);
        };
        headers["Content-Encoding"] = "gzip";
      }

      // Auto-add Content-Length header for response bodies.
      if (!headers["Content-Length"]) {
        headers["Content-Length"] = result[2].length;
      }
    }
  }
github es-git / es-git / test / test-zlib.js View on Github external
function testRoundTrip() {
    const deflated = deflate(bin);
    if (!bodec.isBinary(deflated)) {
      throw new Error("deflate output should be native binary");
    }
    const inflated = inflate(deflated);
    if (!bodec.isBinary(inflated)) {
      throw new Error("inflate output should be native binary");
    }
    if (bodec.toRaw(bin) !== bodec.toRaw(inflated)) {
      console.log([bin, inflated]);
      throw new Error("Problem with roundtrip");
    }
  },
  function testStream() {
github es-git / es-git / test / test-pack-codec.js View on Github external
items.forEach(item => {
      if (!bodec.isBinary(item.body)) {
        item.body = encoders[item.type](item.body);
        }
      write(item);
    });
    write();
github creationix / wheaty / server.js View on Github external
};

  function* load(path) {
    var meta = yield repo.pathToEntry(root, path);
    if (meta) {
      recording.paths[path] = meta.hash;
      meta.repo = repo;
    }
    return meta;
  }
  var result = yield* render(load, url);
  if (result) {
    if (typeof result[2] === "string") {
      result[2] = bodec.fromUnicode(result[2]);
    }
    if (!result[1].ETag && bodec.isBinary(result[2])) {
      result[1].ETag = '"' + sha1(result[2]) + '"';
    }
    recording.headers = result[1];
    records[url] = recording;
  }
  return result;
}
github es-git / es-git / ts / net / git-fetch-pack.ts View on Github external
function onNak(line) {
    if (line === undefined) return api.put();
    if (line === null) return socket.take(onNak);
    if (bodec.isBinary(line) || line.progress || line.error) {
      packChannel = makeChannel();
      progressChannel = makeChannel();
      errorChannel = makeChannel();
      api.put({
        pack: { take: packChannel.take },
        progress: { take: progressChannel.take },
        error: { take: errorChannel.take },
      });
      return onMore(null, line);
    }
    let match = line.match(/^shallow ([0-9a-f]{40})$/);
    if (match) {
      refs.shallows.push(match[1]);
      return socket.take(onNak);
    }
    match = line.match(/^ACK ([0-9a-f]{40})$/);
github digidem / react-mapfilter / app / js / lib / backbone-github.js View on Github external
function encodeBlob(blob) {
  if (typeof blob === "string") return {
    content: bodec.encodeUtf8(blob),
    encoding: "utf-8"
  };
  if (bodec.isBinary(blob)) return {
    content: bodec.toBase64(blob),
    encoding: "base64"
  };
  throw new TypeError("Invalid blob type, must be binary or string");
}
github creationix / js-github / mixins / github-db.js View on Github external
var toCreate = entries.filter(function (entry) {
      return bodec.isBinary(entry.content);
    });
github creationix / http-codec / http-codec.js View on Github external
return function (res) {
    if (res === undefined) return write(undefined);
    if (bodec.isBinary(res)) return write(res);
    var head = "HTTP/1.1 " + res.code + " " + STATUS_CODES[res.code] + "\r\n";
    res.headers.forEach(function (pair) {
      head += pair[0] + ": " + pair[1] + "\r\n";
    });
    head += "\r\n";
    write(bodec.fromUnicode(head));
  };
}
github creationix / wheaty / wheaty.js View on Github external
function* execute(url) {
    var recording = {
      paths: {},
      headers: {}
    };

    var result = yield* render(pathToEntryRecorded, url, runtimes);

    if (!result) return;

    if (typeof result[2] === "string") {
      result[2] = bodec.fromUnicode(result[2]);
    }
    if (!result[1].ETag && bodec.isBinary(result[2])) {
      result[1].ETag = '"' + sha1(result[2]) + '"';
    }
    recording.headers = result[1];
    records[url] = recording;

    return result;

    function* pathToEntryRecorded(path) {
      var meta = yield* pathToEntry(path);
      if (meta) recording.paths[path] = meta.hash;
      return meta;
    }
  }
};