Skip to content

Commit

Permalink
Merge pull request #66 from badgateway/kill-bytesequence
Browse files Browse the repository at this point in the history
Use ArrayBuffer instead of ByteSequence
  • Loading branch information
evert authored Oct 2, 2024
2 parents b3ab63d + 8166148 commit 1b182f5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 61 deletions.
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ChangeLog
=========

2.0.0 (????-??-??)
------------------

* #66: We now convert from/to ArrayBuffer instead of a custom ByteSequence
object. This is a breaking change.


2.0.0-alpha.1 (2024-02-23)
--------------------------

Expand Down
7 changes: 3 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import {
BareItem,
Parameters,
InnerList,
ByteSequence
} from './types.js';

import { Token } from './token.js';

import { isAscii } from './util.js';
import { isAscii, base64ToArrayBuffer } from './util.js';
import { DisplayString } from './displaystring.js';

export function parseDictionary(input: string): Dictionary {
Expand Down Expand Up @@ -367,7 +366,7 @@ export default class Parser {

}

private parseByteSequence(): ByteSequence {
private parseByteSequence(): ArrayBuffer {

this.expectChar(':');
this.pos++;
Expand All @@ -382,7 +381,7 @@ export default class Parser {
throw new ParseError(this.pos, 'ByteSequence does not contain a valid base64 string');
}

return new ByteSequence(b64Content);
return base64ToArrayBuffer(b64Content);

}

Expand Down
9 changes: 4 additions & 5 deletions src/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
BareItem,
ByteSequence,
Dictionary,
DictionaryObject,
InnerList,
Expand All @@ -11,7 +10,7 @@ import {

import { Token } from './token.js';

import { isAscii, isInnerList, isValidKeyStr } from './util.js';
import { isAscii, isInnerList, isValidKeyStr, arrayBufferToBase64 } from './util.js';
import { DisplayString } from './displaystring.js';

export class SerializeError extends Error {}
Expand Down Expand Up @@ -100,7 +99,7 @@ export function serializeBareItem(input: BareItem): string {
if (input instanceof Token) {
return serializeToken(input);
}
if (input instanceof ByteSequence) {
if (input instanceof ArrayBuffer) {
return serializeByteSequence(input);
}
if (input instanceof DisplayString) {
Expand Down Expand Up @@ -162,8 +161,8 @@ export function serializeBoolean(input: boolean): string {
return input ? '?1' : '?0';
}

export function serializeByteSequence(input: ByteSequence): string {
return `:${input.toBase64()}:`;
export function serializeByteSequence(input: ArrayBuffer): string {
return `:${arrayBufferToBase64(input)}:`;
}

export function serializeToken(input: Token): string {
Expand Down
19 changes: 1 addition & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,6 @@ export type Dictionary = Map<string, Item|InnerList>;
*/
export type DictionaryObject = Record<string, BareItem|Item|InnerList>;

export class ByteSequence {

base64Value: string;
constructor(base64Value: string) {

this.base64Value = base64Value;

}

toBase64(): string {

return this.base64Value;

}

}

export type BareItem = number | string | Token | ByteSequence | Date | boolean | DisplayString;
export type BareItem = number | string | Token | ArrayBuffer | Date | boolean | DisplayString;

export type Item = [BareItem, Parameters];
33 changes: 30 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Item, InnerList, BareItem, ByteSequence } from './types';
import { Item, InnerList } from './types';

const asciiRe = /^[\x20-\x7E]*$/;
const tokenRe = /^[a-zA-Z*][:/!#$%&'*+\-.^_`|~A-Za-z0-9]*$/;
Expand Down Expand Up @@ -29,9 +29,36 @@ export function isInnerList(input: Item | InnerList): input is InnerList {

}

export function arrayBufferToBase64(ab: ArrayBuffer): string {

export function isByteSequence(input: BareItem): input is ByteSequence {
// Create a Uint8Array to read the ArrayBuffer as bytes
const bytes = new Uint8Array(ab);
let binary = '';

return typeof input === 'object' && 'base64Value' in input;
// Convert each byte to a character
for (const byte of bytes) {
binary += String.fromCharCode(byte);
}

// Encode the binary string as Base64
return btoa(binary);
}

export function base64ToArrayBuffer(b64: string): ArrayBuffer {

// Decode the base64 string into a binary string
const binaryString = atob(b64);

// Create a new ArrayBuffer with the same length as the binary string
const len = binaryString.length;
const bytes = new Uint8Array(len);

// Convert each character to its corresponding byte
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}

// Return the ArrayBuffer
return bytes.buffer;

}
7 changes: 3 additions & 4 deletions test/httpwg-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ParseError,

Token,
ByteSequence,
DisplayString,

} from '../dist/index.js';
Expand Down Expand Up @@ -304,10 +303,10 @@ function packTestValue(input) {
value: input.toString()
}
}
if (input instanceof ByteSequence) {
if (input instanceof ArrayBuffer) {
return {
__type: 'binary',
value: base32Encode(Buffer.from(input.toBase64(), 'base64'), 'RFC4648')
value: base32Encode(input, 'RFC4648')
}
}
if (input instanceof Date) {
Expand Down Expand Up @@ -357,7 +356,7 @@ function unpackTestValue(input) {
case 'token' :
return new Token(input.value);
case 'binary':
return new ByteSequence(Buffer.from(base32Decode(input.value, 'RFC4648')).toString('base64'));
return new base32Decode(input.value, 'RFC4648');
case 'date' :
return new Date(input.value * 1000);
case 'displaystring' :
Expand Down
27 changes: 0 additions & 27 deletions test/util.js

This file was deleted.

0 comments on commit 1b182f5

Please sign in to comment.