keichi / binary-parser

A blazing-fast declarative parser builder for binary data
MIT License
864 stars 134 forks source link

Works in deno! #182

Closed manzt closed 2 years ago

manzt commented 2 years ago

Since we merge context.ts into binary_parser.ts, the source code itself runs in deno :) Thanks for moving forward with these PRs. This library is very portable!

deno run --allow-net --no-check mod.ts
``` { files: [ { name: "a/", mode: 493, uid: 501, gid: 20, size: 0, mtime: 1515642944, chksum: 5047, typeflag: 5, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "a/h", mode: 420, uid: 501, gid: 20, size: 44, mtime: 1515642944, chksum: 5152, typeflag: 0, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "a/c/", mode: 493, uid: 501, gid: 20, size: 0, mtime: 1515642936, chksum: 5199, typeflag: 5, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "a/b/", mode: 493, uid: 501, gid: 20, size: 0, mtime: 1515642920, chksum: 5196, typeflag: 5, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "a/b/e", mode: 420, uid: 501, gid: 20, size: 11, mtime: 1515642920, chksum: 5293, typeflag: 0, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "a/c/g", mode: 420, uid: 501, gid: 20, size: 33, mtime: 1515642936, chksum: 5299, typeflag: 0, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "a/c/d/", mode: 493, uid: 501, gid: 20, size: 0, mtime: 1515642929, chksum: 5346, typeflag: 5, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "a/c/d/f", mode: 420, uid: 501, gid: 20, size: 22, mtime: 1515642929, chksum: 5448, typeflag: 0, linkname: "", magic: "ustar", version: 0, uname: "keichi", gname: "staff", devmajor: 0, devminor: 0, prefix: "" }, { name: "", mode: NaN, uid: NaN, gid: NaN, size: NaN, mtime: NaN, chksum: NaN, typeflag: NaN, linkname: "", magic: "", version: NaN, uname: "", gname: "", devmajor: NaN, devminor: NaN, prefix: "" } ] } ```
// mod.ts

import { Parser } from 'https://raw.githubusercontent.com/keichi/binary-parser/master/lib/binary_parser.ts';

const oct2int = function (s) {
  return parseInt(s, 8);
};

const tarHeader = new Parser()
  .string("name", { length: 100, stripNull: true })
  .string("mode", { length: 8, stripNull: true, formatter: oct2int })
  .string("uid", { length: 8, stripNull: true, formatter: oct2int })
  .string("gid", { length: 8, stripNull: true, formatter: oct2int })
  .string("size", { length: 12, stripNull: true, formatter: oct2int })
  .string("mtime", { length: 12, stripNull: true, formatter: oct2int })
  .string("chksum", { length: 8, stripNull: true, formatter: oct2int })
  .string("typeflag", { length: 1, stripNull: true, formatter: oct2int })
  .string("linkname", { length: 100, stripNull: true })
  .string("magic", { length: 6, stripNull: true })
  .string("version", { length: 2, stripNull: true, formatter: oct2int })
  .string("uname", { length: 32, stripNull: true })
  .string("gname", { length: 32, stripNull: true })
  .string("devmajor", { length: 8, stripNull: true, formatter: oct2int })
  .string("devminor", { length: 8, stripNull: true, formatter: oct2int })
  .string("prefix", { length: 155, stripNull: true })
  .seek(12);

const tarItem = new Parser()
  .nest({
    type: tarHeader,
  })
  .seek(function () {
    return Math.ceil(this.size / 512) * 512;
  });

const tarArchive = new Parser().array("files", {
  type: tarItem,
  readUntil: "eof",
});

fetch('https://github.com/keichi/binary-parser/raw/master/example/test.tar')
    .then(res => res.arrayBuffer())
    .then(buf => tarArchive.parse(new Uint8Array(buf)))
    .then(console.log);

Feel free to close, but I thought I'd share.

keichi commented 2 years ago

Nice! We should have unit tests to officially support Deno and web browsers.

We also need to fix type-related errors to make it work without the --no-check flag.