Skip to content

Commit 3277ecf

Browse files
fix(sdk): Remove stray call to node:Buffer
Update assertion.ts fix(lib): Removes all refs to node:buffer I could find - removes the polyfill from the web-test-runner, to make sure we aren't accidentally including it (at least in things that covers) - updates all .spec and .test files, in turn - renames a bunch of things called `buffer` to something else so I can search for this easier Update cli.ts
1 parent f851c02 commit 3277ecf

23 files changed

+113
-222
lines changed

cli/package-lock.json

Lines changed: 1 addition & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/cli.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { CLIError, Level, log } from './logger.js';
2020
import { webcrypto } from 'crypto';
2121
import { attributeFQNsAsValues } from '@opentdf/client/nano';
22+
import { base64 } from '@opentdf/client/encodings';
2223

2324
type AuthToProcess = {
2425
auth?: string;
@@ -37,8 +38,9 @@ const bindingTypes = ['ecdsa', 'gmac'];
3738
const containerTypes = ['tdf3', 'nano', 'dataset', 'ztdf'];
3839

3940
const parseJwt = (jwt: string, field = 1) => {
40-
return JSON.parse(Buffer.from(jwt.split('.')[field], 'base64').toString());
41+
return JSON.parse(base64.decode(jwt.split('.')[field]));
4142
};
43+
4244
const parseJwtComplete = (jwt: string) => {
4345
return { header: parseJwt(jwt, 0), payload: parseJwt(jwt) };
4446
};
@@ -413,9 +415,9 @@ export const handleArgs = (args: string[]) => {
413415

414416
log('DEBUG', 'Handle output.');
415417
if (argv.output) {
416-
await writeFile(argv.output, Buffer.from(plaintext));
418+
await writeFile(argv.output, new Uint8Array(plaintext));
417419
} else {
418-
console.log(Buffer.from(plaintext).toString('utf8'));
420+
console.log(new TextDecoder().decode(plaintext));
419421
}
420422
}
421423
const lastRequest = authProvider.requestLog[authProvider.requestLog.length - 1];
@@ -503,9 +505,9 @@ export const handleArgs = (args: string[]) => {
503505

504506
log('DEBUG', `Handle cyphertext output ${JSON.stringify(cyphertext)}`);
505507
if (argv.output) {
506-
await writeFile(argv.output, Buffer.from(cyphertext));
508+
await writeFile(argv.output, new Uint8Array(cyphertext));
507509
} else {
508-
console.log(Buffer.from(cyphertext).toString('base64'));
510+
console.log(base64.encodeArrayBuffer(cyphertext));
509511
}
510512
}
511513
}

lib/package-lock.json

Lines changed: 1 addition & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
"axios-retry": "^3.9.0",
6565
"base64-js": "^1.5.1",
6666
"browser-fs-access": "^0.34.1",
67-
"buffer": "^6.0.3",
6867
"buffer-crc32": "^0.2.13",
6968
"dpop": "^1.2.0",
7069
"eventemitter3": "^5.0.1",

lib/src/nanotdf/models/Header.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,28 +232,28 @@ export default class Header {
232232
/**
233233
* Copy the contents of the header to buffer
234234
*/
235-
copyToBuffer(buffer: Uint8Array): void {
236-
if (this.length > buffer.length) {
235+
copyToBuffer(target: Uint8Array): void {
236+
if (this.length > target.length) {
237237
throw new InvalidFileError('invalid buffer size to copy tdf header');
238238
}
239239

240240
let offset = 0;
241241

242242
// Write Magic number and version
243-
buffer.set(this.magicNumberVersion, 0);
243+
target.set(this.magicNumberVersion, 0);
244244
offset += this.magicNumberVersion.length;
245245

246246
// Write kas resource locator
247247
const kasResourceLocatorBuf = this.kas.toBuffer();
248-
buffer.set(kasResourceLocatorBuf, offset);
248+
target.set(kasResourceLocatorBuf, offset);
249249
offset += kasResourceLocatorBuf.length;
250250

251251
// Write ECC & Binding Mode
252252
const ecdsaBinding = this.useECDSABinding ? 1 : 0;
253253
const eccBingingMode = (ecdsaBinding << 7) | this.ephemeralCurveName;
254254
const eccBingingModeAsByte = new Uint8Array(1);
255255
eccBingingModeAsByte[0] = eccBingingMode;
256-
buffer.set(eccBingingModeAsByte, offset);
256+
target.set(eccBingingModeAsByte, offset);
257257
offset += eccBingingModeAsByte.length;
258258

259259
// Write symmetric & payload config
@@ -262,16 +262,16 @@ export default class Header {
262262
(isSignatureEnable << 7) | this.signatureCurveName | this.symmetricCipher;
263263
const symmetricPayloadConfigAsByte = new Uint8Array(1);
264264
symmetricPayloadConfigAsByte[0] = symmetricPayloadConfig;
265-
buffer.set(symmetricPayloadConfigAsByte, offset);
265+
target.set(symmetricPayloadConfigAsByte, offset);
266266
offset += symmetricPayloadConfigAsByte.length;
267267

268268
// Write the policy
269269
const policyBuffer = this.policy.toBuffer();
270-
buffer.set(policyBuffer, offset);
270+
target.set(policyBuffer, offset);
271271
offset += policyBuffer.length;
272272

273273
// Write the ephemeral public key
274-
buffer.set(this.ephemeralPublicKey, offset);
274+
target.set(this.ephemeralPublicKey, offset);
275275
}
276276

277277
/**
@@ -304,8 +304,8 @@ export default class Header {
304304
*/
305305
toBuffer(): ArrayBuffer {
306306
const arrayBuffer = new ArrayBuffer(this.length);
307-
const buffer = new Uint8Array(arrayBuffer);
308-
this.copyToBuffer(buffer);
307+
const target = new Uint8Array(arrayBuffer);
308+
this.copyToBuffer(target);
309309
return arrayBuffer;
310310
}
311311

lib/src/nanotdf/models/Payload.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ export default class Payload {
167167
/**
168168
* Copy the contents of the signature to buffer
169169
*/
170-
copyToBuffer(buffer: Uint8Array): void {
171-
if (this.length > buffer.length) {
170+
copyToBuffer(target: Uint8Array): void {
171+
if (this.length > target.length) {
172172
throw new Error('internal: invalid buffer size to copy payload');
173173
}
174174

@@ -188,9 +188,9 @@ export default class Payload {
188188
payloadSizeAsBg[1] = lengthAsUint24[1];
189189
payloadSizeAsBg[2] = lengthAsUint24[0];
190190

191-
buffer.set(payloadSizeAsBg, 0);
192-
buffer.set(this.iv, payloadSizeAsBg.length);
193-
buffer.set(this.ciphertext, payloadSizeAsBg.length + this.iv.length);
194-
buffer.set(this.authTag, payloadSizeAsBg.length + this.iv.length + this.ciphertext.length);
191+
target.set(payloadSizeAsBg, 0);
192+
target.set(this.iv, payloadSizeAsBg.length);
193+
target.set(this.ciphertext, payloadSizeAsBg.length + this.iv.length);
194+
target.set(this.authTag, payloadSizeAsBg.length + this.iv.length + this.ciphertext.length);
195195
}
196196
}

lib/src/nanotdf/models/Policy/EmbeddedPolicy.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ class EmbeddedPolicy extends AbstractPolicy implements EmbeddedPolicyInterface {
6969
* Return the content of the policy
7070
*/
7171
override toBuffer(): Uint8Array {
72-
const buffer = new Uint8Array(this.getLength());
72+
const target = new Uint8Array(this.getLength());
7373

7474
if (this.content.length > EmbeddedPolicy.MAX_POLICY_SIZE) {
7575
throw new ConfigurationError("TDF Policy can't be more that 2^16");
7676
}
7777

78-
buffer.set([this.type], 0);
78+
target.set([this.type], 0);
7979

8080
// Write the policy length, assuming the host system is little endian
8181
// TODO: There should be better way to convert to big endian
@@ -86,15 +86,15 @@ class EmbeddedPolicy extends AbstractPolicy implements EmbeddedPolicyInterface {
8686
const policyContentSizeAsBg = new Uint8Array(2);
8787
policyContentSizeAsBg[0] = temp[1];
8888
policyContentSizeAsBg[1] = temp[0];
89-
buffer.set(policyContentSizeAsBg, 1);
89+
target.set(policyContentSizeAsBg, 1);
9090

9191
// Write the policy content
92-
buffer.set(this.content, policyContentSizeAsBg.length + 1);
92+
target.set(this.content, policyContentSizeAsBg.length + 1);
9393

9494
// Write the binding.
95-
buffer.set(this.binding, this.content.length + policyContentSizeAsBg.length + 1);
95+
target.set(this.binding, this.content.length + policyContentSizeAsBg.length + 1);
9696

97-
return buffer;
97+
return target;
9898
}
9999
}
100100

lib/src/nanotdf/models/Policy/RemotePolicy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ class RemotePolicy extends AbstractPolicy implements RemotePolicyInterface {
5656
* Return the content of the policy
5757
*/
5858
override toBuffer(): Uint8Array {
59-
const buffer = new Uint8Array(this.getLength());
59+
const target = new Uint8Array(this.getLength());
6060

61-
buffer.set([PolicyTypeEnum.Remote], 0);
61+
target.set([PolicyTypeEnum.Remote], 0);
6262

6363
// Write the remote policy location
6464
const resourceLocatorAsBuf = this.remotePolicy.toBuffer();
65-
buffer.set(resourceLocatorAsBuf, 1);
65+
target.set(resourceLocatorAsBuf, 1);
6666

6767
// Write the binding.
68-
buffer.set(this.binding, resourceLocatorAsBuf.length + 1);
68+
target.set(this.binding, resourceLocatorAsBuf.length + 1);
6969

70-
return buffer;
70+
return target;
7171
}
7272
}
7373

lib/src/nanotdf/models/ResourceLocator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export default class ResourceLocator {
178178
* Return the contents of the Resource Locator in buffer
179179
*/
180180
toBuffer(): Uint8Array {
181-
const buffer = new Uint8Array(ResourceLocator.BODY_OFFSET + this.body.length + this.idType);
181+
const target = new Uint8Array(ResourceLocator.BODY_OFFSET + this.body.length + this.idType);
182182
let idTypeNibble = 0;
183183
switch (this.idType) {
184184
case ResourceLocatorIdentifierEnum.TwoBytes:
@@ -191,13 +191,13 @@ export default class ResourceLocator {
191191
idTypeNibble = ResourceLocator.IDENTIFIER_32_BYTE;
192192
break;
193193
}
194-
buffer.set([this.protocol | idTypeNibble], ResourceLocator.PROTOCOL_OFFSET);
195-
buffer.set([this.lengthOfBody], ResourceLocator.LENGTH_OFFSET);
196-
buffer.set(new TextEncoder().encode(this.body), ResourceLocator.BODY_OFFSET);
194+
target.set([this.protocol | idTypeNibble], ResourceLocator.PROTOCOL_OFFSET);
195+
target.set([this.lengthOfBody], ResourceLocator.LENGTH_OFFSET);
196+
target.set(new TextEncoder().encode(this.body), ResourceLocator.BODY_OFFSET);
197197
if (this.id) {
198-
buffer.set(new TextEncoder().encode(this.id), ResourceLocator.BODY_OFFSET + this.body.length);
198+
target.set(new TextEncoder().encode(this.id), ResourceLocator.BODY_OFFSET + this.body.length);
199199
}
200-
return buffer;
200+
return target;
201201
}
202202

203203
/**

lib/src/nanotdf/models/Signature.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ export default class Signature {
7474
/**
7575
* Copy the contents of the signature to buffer
7676
*/
77-
copyToBuffer(buffer: Uint8Array): void {
78-
if (this.length > buffer.length) {
77+
copyToBuffer(target: Uint8Array): void {
78+
if (this.length > target.length) {
7979
throw new ConfigurationError('Invalid buffer size to copy signature');
8080
}
8181

82-
buffer.set(this.publicKey, 0);
83-
buffer.set(this.signature, this.publicKey.length);
82+
target.set(this.publicKey, 0);
83+
target.set(this.signature, this.publicKey.length);
8484
}
8585
}

0 commit comments

Comments
 (0)