Skip to content

Commit

Permalink
refactor(core,cli): Remove unnecessary Node.js Buffer uses (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Jul 25, 2024
1 parent 1413806 commit 8957098
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/transforms/toktx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export const toktx = function (options: ETC1SOptions | UASTCOptions): Transform
const srcPath = join(batchDir.name, `${batchPrefix}_${textureIndex}.${srcExtension}`);
const dstPath = join(batchDir.name, `${batchPrefix}_${textureIndex}.ktx2`);

await fs.writeFile(srcPath, Buffer.from(srcImage));
await fs.writeFile(srcPath, srcImage);

const params = [
'create',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test('merge', async (t) => {
.setBuffer(bufB);
await io.write(inputB, documentB);

fs.writeFileSync(inputC, Buffer.from([1, 2, 3, 4, 5]));
fs.writeFileSync(inputC, new Uint8Array([1, 2, 3, 4, 5]));

await program
// https://github.com/mattallty/Caporal.js/issues/195
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/io/node-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class NodeIO extends PlatformIO {

/** @internal */
private async _writeGLB(uri: string, doc: Document): Promise<void> {
const buffer = Buffer.from(await this.writeBinary(doc));
const buffer = await this.writeBinary(doc);
await this._fs.writeFile(uri, buffer);
this.lastWriteBytes = buffer.byteLength;
}
Expand Down
10 changes: 2 additions & 8 deletions packages/core/src/utils/buffer-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,12 @@ export class BufferUtils {

/** Encodes text to a byte array. */
static encodeText(text: string): Uint8Array {
if (typeof TextEncoder !== 'undefined') {
return new TextEncoder().encode(text);
}
return Buffer.from(text);
return new TextEncoder().encode(text);
}

/** Decodes a byte array to text. */
static decodeText(array: Uint8Array): string {
if (typeof TextDecoder !== 'undefined') {
return new TextDecoder().decode(array);
}
return Buffer.from(array).toString('utf8');
return new TextDecoder().decode(array);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/utils/buffer-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('node.js', (t) => {
);
t.is(BufferUtils.decodeText(BufferUtils.encodeText('hey')), 'hey', 'encode/decode');

const buffer = Buffer.from([1, 2]);
const buffer = new Uint8Array([1, 2]);
t.is(BufferUtils.equals(buffer, buffer), true, 'equals strict');
t.is(BufferUtils.equals(buffer, Buffer.from([1])), false, 'equals by length');
t.is(BufferUtils.equals(buffer, new Uint8Array([1])), false, 'equals by length');
});

0 comments on commit 8957098

Please sign in to comment.