Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions scopes/scope/objects/models/source.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from 'chai';
import { deflateSync } from 'zlib';
import { BitObject } from '../objects';
import Source from './source';

describe('Source', () => {
it('should not keep the whole inflated buffer when the file is small', async () => {
const file = Source.from(Buffer.from('const a = 1;'));
const parsed = (await BitObject.parseObject(deflateSync(file.serialize()))) as Source;
expect(parsed.contents.toString()).to.equal('const a = 1;');
expect(parsed.contents.buffer.byteLength).to.be.below(16 * 1024);
});
});
3 changes: 3 additions & 0 deletions scopes/scope/objects/models/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default class Source extends BitObject {
}

static parse(contents: Buffer): Source {
// zlib returns small results as a slice of a bigger (16KB) buffer. unlike other objects, which are parsed and dropped,
// a Source keeps these bytes, often for long (e.g. as a component file), so it copies them instead of pinning 16KB.
if (contents.byteLength < contents.buffer.byteLength / 2) contents = Buffer.from(contents);
return new Source(contents);
}

Expand Down