Skip to content

Commit 91fab47

Browse files
authored
Add sizeBytes to document metadata (#517)
* Add sizeBytes to document metadata * use literal size and simpler multi-byte char for test
1 parent a060e05 commit 91fab47

5 files changed

Lines changed: 24 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ tools/*
1515
/tmp-tst
1616
/jscpd
1717
/tmp
18+
/.idea/

src/document/Document.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TextDocument, Position, Range, DocumentUri } from 'vscode-languageserve
22
import { TopLevelSection } from '../context/CloudFormationEnums';
33
import { DefaultSettings } from '../settings/Settings';
44
import { LoggerFactory } from '../telemetry/LoggerFactory';
5+
import { byteSize } from '../utils/String';
56
import { DocumentMetadata } from './DocumentProtocol';
67
import { detectDocumentType, uriToPath } from './DocumentUtils';
78
import { parseValidYaml } from './YamlParser';
@@ -170,6 +171,7 @@ export class Document {
170171
languageId: this.languageId,
171172
version: this.version,
172173
lineCount: this.textDocument.lineCount,
174+
sizeBytes: byteSize(this.textDocument.getText()),
173175
};
174176
}
175177

src/document/DocumentManager.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { ScopedTelemetry } from '../telemetry/ScopedTelemetry';
77
import { Telemetry } from '../telemetry/TelemetryDecorator';
88
import { Closeable } from '../utils/Closeable';
99
import { Delayer } from '../utils/Delayer';
10-
import { byteSize } from '../utils/String';
1110
import { CloudFormationFileType, Document, DocumentType } from './Document';
1211
import { DocumentMetadata } from './DocumentProtocol';
1312

@@ -170,7 +169,7 @@ export class DocumentManager implements SettingsConfigurable, Closeable {
170169
private emitDocSizeMetrics() {
171170
for (const doc of this.documentMap.values()) {
172171
if (doc.isTemplate()) {
173-
this.telemetry.histogram('documents.template.size.bytes', byteSize(doc.contents()), { unit: 'By' });
172+
this.telemetry.histogram('documents.template.size.bytes', doc.metadata().sizeBytes, { unit: 'By' });
174173
}
175174
}
176175
}

src/document/DocumentProtocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type DocumentMetadata = {
1010
languageId: string;
1111
version: number;
1212
lineCount: number;
13+
sizeBytes: number;
1314
};
1415

1516
export type DocumentPreview = {

tst/unit/document/Document.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,4 +459,23 @@ describe('Document', () => {
459459
expect(doc.getTemplateSizeCategory()).toBe('xlarge');
460460
});
461461
});
462+
463+
describe('metadata', () => {
464+
it('should include sizeBytes matching content byte length', () => {
465+
const content = 'Resources:\n Bucket:\n Type: AWS::S3::Bucket';
466+
const textDocument = TextDocument.create('file:///test.yaml', 'yaml', 1, content);
467+
const doc = new Document(textDocument);
468+
469+
expect(doc.metadata().sizeBytes).toBe(46);
470+
});
471+
472+
it('should compute sizeBytes using byte length not character count', () => {
473+
// 'é' is 1 character but 2 bytes in UTF-8
474+
const content = 'é'.repeat(10);
475+
const textDocument = TextDocument.create('file:///unicode.yaml', 'yaml', 1, content);
476+
const doc = new Document(textDocument);
477+
478+
expect(doc.metadata().sizeBytes).toBe(20);
479+
});
480+
});
462481
});

0 commit comments

Comments
 (0)