~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
84 lines · 2.2 KB Raw History
/**
 * Reading a blob as a Git LFS pointer.
 *
 * A pointer stands where a large file would be. Telling one from the file
 * itself is what stops a page from printing 132 bytes of text as a PNG.
 */

import { describe, expect, it } from 'vitest';
import { lfsPointer } from '../src/lfs.js';

const OID = 'a'.repeat(64);

/** @param {string} text */
const encode = (text) => new TextEncoder().encode(text);

/** @param {string[]} lines */
const pointer = (lines) => encode(lines.join('\n'));

describe('lfsPointer', () => {
  it('reads the file a pointer stands for', () => {
    expect(
      lfsPointer(
        pointer([
          'version https://git-lfs.github.com/spec/v1',
          `oid sha256:${OID}`,
          'size 412345',
          '',
        ]),
      ),
    ).toEqual({ oid: OID, size: 412345 });
  });

  it('refuses a file that carries its own content', () => {
    // The first bytes of a PNG, which are not text at all.
    expect(lfsPointer(new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d]))).toBe(
      null,
    );
    expect(lfsPointer(encode('# A README\n'))).toBe(null);
  });

  it('refuses a pointer missing the lines that name the file', () => {
    expect(
      lfsPointer(pointer(['version https://git-lfs.github.com/spec/v1', ''])),
    ).toBe(null);
    expect(
      lfsPointer(
        pointer([
          'version https://git-lfs.github.com/spec/v1',
          `oid sha256:${OID}`,
          '',
        ]),
      ),
    ).toBe(null);
  });

  it('refuses an oid that is not a sha256', () => {
    expect(
      lfsPointer(
        pointer([
          'version https://git-lfs.github.com/spec/v1',
          'oid sha256:short',
          'size 1',
          '',
        ]),
      ),
    ).toBe(null);
  });

  it('refuses a file too long to be a pointer', () => {
    const padded = pointer([
      'version https://git-lfs.github.com/spec/v1',
      `oid sha256:${OID}`,
      'size 1',
      `# ${'x'.repeat(1024)}`,
    ]);
    expect(lfsPointer(padded)).toBe(null);
  });

  it('refuses nothing at all', () => {
    expect(lfsPointer(null)).toBe(null);
    expect(lfsPointer(undefined)).toBe(null);
    expect(lfsPointer(new Uint8Array(0))).toBe(null);
  });
});