Skip to content

Commit 1b182f5

Browse files
authored
Merge pull request #66 from badgateway/kill-bytesequence
Use ArrayBuffer instead of ByteSequence
2 parents b3ab63d + 8166148 commit 1b182f5

File tree

7 files changed

+48
-61
lines changed

7 files changed

+48
-61
lines changed

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
ChangeLog
22
=========
33

4+
2.0.0 (????-??-??)
5+
------------------
6+
7+
* #66: We now convert from/to ArrayBuffer instead of a custom ByteSequence
8+
object. This is a breaking change.
9+
10+
411
2.0.0-alpha.1 (2024-02-23)
512
--------------------------
613

src/parser.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import {
55
BareItem,
66
Parameters,
77
InnerList,
8-
ByteSequence
98
} from './types.js';
109

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

13-
import { isAscii } from './util.js';
12+
import { isAscii, base64ToArrayBuffer } from './util.js';
1413
import { DisplayString } from './displaystring.js';
1514

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

368367
}
369368

370-
private parseByteSequence(): ByteSequence {
369+
private parseByteSequence(): ArrayBuffer {
371370

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

385-
return new ByteSequence(b64Content);
384+
return base64ToArrayBuffer(b64Content);
386385

387386
}
388387

src/serializer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
BareItem,
3-
ByteSequence,
43
Dictionary,
54
DictionaryObject,
65
InnerList,
@@ -11,7 +10,7 @@ import {
1110

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

14-
import { isAscii, isInnerList, isValidKeyStr } from './util.js';
13+
import { isAscii, isInnerList, isValidKeyStr, arrayBufferToBase64 } from './util.js';
1514
import { DisplayString } from './displaystring.js';
1615

1716
export class SerializeError extends Error {}
@@ -100,7 +99,7 @@ export function serializeBareItem(input: BareItem): string {
10099
if (input instanceof Token) {
101100
return serializeToken(input);
102101
}
103-
if (input instanceof ByteSequence) {
102+
if (input instanceof ArrayBuffer) {
104103
return serializeByteSequence(input);
105104
}
106105
if (input instanceof DisplayString) {
@@ -162,8 +161,8 @@ export function serializeBoolean(input: boolean): string {
162161
return input ? '?1' : '?0';
163162
}
164163

165-
export function serializeByteSequence(input: ByteSequence): string {
166-
return `:${input.toBase64()}:`;
164+
export function serializeByteSequence(input: ArrayBuffer): string {
165+
return `:${arrayBufferToBase64(input)}:`;
167166
}
168167

169168
export function serializeToken(input: Token): string {

src/types.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,6 @@ export type Dictionary = Map<string, Item|InnerList>;
3939
*/
4040
export type DictionaryObject = Record<string, BareItem|Item|InnerList>;
4141

42-
export class ByteSequence {
43-
44-
base64Value: string;
45-
constructor(base64Value: string) {
46-
47-
this.base64Value = base64Value;
48-
49-
}
50-
51-
toBase64(): string {
52-
53-
return this.base64Value;
54-
55-
}
56-
57-
}
58-
59-
export type BareItem = number | string | Token | ByteSequence | Date | boolean | DisplayString;
42+
export type BareItem = number | string | Token | ArrayBuffer | Date | boolean | DisplayString;
6043

6144
export type Item = [BareItem, Parameters];

src/util.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Item, InnerList, BareItem, ByteSequence } from './types';
1+
import { Item, InnerList } from './types';
22

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

3030
}
3131

32+
export function arrayBufferToBase64(ab: ArrayBuffer): string {
3233

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

35-
return typeof input === 'object' && 'base64Value' in input;
38+
// Convert each byte to a character
39+
for (const byte of bytes) {
40+
binary += String.fromCharCode(byte);
41+
}
42+
43+
// Encode the binary string as Base64
44+
return btoa(binary);
45+
}
46+
47+
export function base64ToArrayBuffer(b64: string): ArrayBuffer {
48+
49+
// Decode the base64 string into a binary string
50+
const binaryString = atob(b64);
51+
52+
// Create a new ArrayBuffer with the same length as the binary string
53+
const len = binaryString.length;
54+
const bytes = new Uint8Array(len);
55+
56+
// Convert each character to its corresponding byte
57+
for (let i = 0; i < len; i++) {
58+
bytes[i] = binaryString.charCodeAt(i);
59+
}
60+
61+
// Return the ArrayBuffer
62+
return bytes.buffer;
3663

3764
}

test/httpwg-tests.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
ParseError,
1313

1414
Token,
15-
ByteSequence,
1615
DisplayString,
1716

1817
} from '../dist/index.js';
@@ -304,10 +303,10 @@ function packTestValue(input) {
304303
value: input.toString()
305304
}
306305
}
307-
if (input instanceof ByteSequence) {
306+
if (input instanceof ArrayBuffer) {
308307
return {
309308
__type: 'binary',
310-
value: base32Encode(Buffer.from(input.toBase64(), 'base64'), 'RFC4648')
309+
value: base32Encode(input, 'RFC4648')
311310
}
312311
}
313312
if (input instanceof Date) {
@@ -357,7 +356,7 @@ function unpackTestValue(input) {
357356
case 'token' :
358357
return new Token(input.value);
359358
case 'binary':
360-
return new ByteSequence(Buffer.from(base32Decode(input.value, 'RFC4648')).toString('base64'));
359+
return new base32Decode(input.value, 'RFC4648');
361360
case 'date' :
362361
return new Date(input.value * 1000);
363362
case 'displaystring' :

test/util.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)