Skip to content

Commit

Permalink
fix(types): Vendor in TextEncoderCommon type (#5221)
Browse files Browse the repository at this point in the history
`TextEncoderCommon` is a type from `lib.dom.d.ts`. We don't want to
require users to set DOM types, so we vendor it in. It's a relatively
simple type, so we don't have to worry about potential conflicts or user
issues.
  • Loading branch information
AbhiPrasad authored Jun 7, 2022
1 parent fb1c284 commit fb267e0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type { Severity, SeverityLevel } from './severity';
export type { Span, SpanContext } from './span';
export type { StackFrame } from './stackframe';
export type { Stacktrace, StackParser, StackLineParser, StackLineParserFn } from './stacktrace';
export type { TextEncoderInternal } from './textencoder';
export type {
CustomSamplingContext,
Measurements,
Expand Down
16 changes: 16 additions & 0 deletions packages/types/src/textencoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Vendored type from TS 3.8 `typescript/lib/lib.dom.d.ts`.
*
* Type is vendored in so that users don't have to opt-in to DOM types.
*/
export interface TextEncoderCommon {
/**
* Returns "utf-8".
*/
readonly encoding: string;
}

// Combination of global TextEncoder and Node require('util').TextEncoder
export interface TextEncoderInternal extends TextEncoderCommon {
encode(input?: string): Uint8Array;
}
6 changes: 1 addition & 5 deletions packages/types/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EventDropReason } from './clientreport';
import { DataCategory } from './datacategory';
import { Envelope } from './envelope';
import { TextEncoderInternal } from './textencoder';

export type TransportRequest = {
body: string | Uint8Array;
Expand All @@ -15,11 +16,6 @@ export type TransportMakeRequestResponse = {
};
};

// Combination of global TextEncoder and Node require('util').TextEncoder
interface TextEncoderInternal extends TextEncoderCommon {
encode(input?: string): Uint8Array;
}

export interface InternalBaseTransportOptions {
bufferSize?: number;
recordDroppedEvent: (reason: EventDropReason, dataCategory: DataCategory) => void;
Expand Down
15 changes: 9 additions & 6 deletions packages/utils/src/envelope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Attachment, AttachmentItem, DataCategory, Envelope, EnvelopeItem, EnvelopeItemType } from '@sentry/types';
import {
Attachment,
AttachmentItem,
DataCategory,
Envelope,
EnvelopeItem,
EnvelopeItemType,
TextEncoderInternal,
} from '@sentry/types';

import { dropUndefinedKeys } from './object';

Expand Down Expand Up @@ -36,11 +44,6 @@ export function forEachEnvelopeItem<E extends Envelope>(
});
}

// Combination of global TextEncoder and Node require('util').TextEncoder
interface TextEncoderInternal extends TextEncoderCommon {
encode(input?: string): Uint8Array;
}

function encodeUTF8(input: string, textEncoder?: TextEncoderInternal): Uint8Array {
const utf8 = textEncoder || new TextEncoder();
return utf8.encode(input);
Expand Down

0 comments on commit fb267e0

Please sign in to comment.