Skip to content

Commit 45fa27e

Browse files
authored
[pg-protocol] use literals instead of const enum (#2490)
Co-authored-by: Emily Marigold Klassen <[email protected]>
1 parent 69af1cc commit 45fa27e

File tree

2 files changed

+56
-59
lines changed

2 files changed

+56
-59
lines changed

packages/pg-protocol/src/messages.ts

+45-46
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,75 @@
11
export type Mode = 'text' | 'binary'
22

3-
export const enum MessageName {
4-
parseComplete = 'parseComplete',
5-
bindComplete = 'bindComplete',
6-
closeComplete = 'closeComplete',
7-
noData = 'noData',
8-
portalSuspended = 'portalSuspended',
9-
replicationStart = 'replicationStart',
10-
emptyQuery = 'emptyQuery',
11-
copyDone = 'copyDone',
12-
copyData = 'copyData',
13-
rowDescription = 'rowDescription',
14-
parameterStatus = 'parameterStatus',
15-
backendKeyData = 'backendKeyData',
16-
notification = 'notification',
17-
readyForQuery = 'readyForQuery',
18-
commandComplete = 'commandComplete',
19-
dataRow = 'dataRow',
20-
copyInResponse = 'copyInResponse',
21-
copyOutResponse = 'copyOutResponse',
22-
authenticationOk = 'authenticationOk',
23-
authenticationMD5Password = 'authenticationMD5Password',
24-
authenticationCleartextPassword = 'authenticationCleartextPassword',
25-
authenticationSASL = 'authenticationSASL',
26-
authenticationSASLContinue = 'authenticationSASLContinue',
27-
authenticationSASLFinal = 'authenticationSASLFinal',
28-
error = 'error',
29-
notice = 'notice',
30-
}
3+
export type MessageName =
4+
| 'parseComplete'
5+
| 'bindComplete'
6+
| 'closeComplete'
7+
| 'noData'
8+
| 'portalSuspended'
9+
| 'replicationStart'
10+
| 'emptyQuery'
11+
| 'copyDone'
12+
| 'copyData'
13+
| 'rowDescription'
14+
| 'parameterStatus'
15+
| 'backendKeyData'
16+
| 'notification'
17+
| 'readyForQuery'
18+
| 'commandComplete'
19+
| 'dataRow'
20+
| 'copyInResponse'
21+
| 'copyOutResponse'
22+
| 'authenticationOk'
23+
| 'authenticationMD5Password'
24+
| 'authenticationCleartextPassword'
25+
| 'authenticationSASL'
26+
| 'authenticationSASLContinue'
27+
| 'authenticationSASLFinal'
28+
| 'error'
29+
| 'notice'
3130

3231
export interface BackendMessage {
3332
name: MessageName
3433
length: number
3534
}
3635

3736
export const parseComplete: BackendMessage = {
38-
name: MessageName.parseComplete,
37+
name: 'parseComplete',
3938
length: 5,
4039
}
4140

4241
export const bindComplete: BackendMessage = {
43-
name: MessageName.bindComplete,
42+
name: 'bindComplete',
4443
length: 5,
4544
}
4645

4746
export const closeComplete: BackendMessage = {
48-
name: MessageName.closeComplete,
47+
name: 'closeComplete',
4948
length: 5,
5049
}
5150

5251
export const noData: BackendMessage = {
53-
name: MessageName.noData,
52+
name: 'noData',
5453
length: 5,
5554
}
5655

5756
export const portalSuspended: BackendMessage = {
58-
name: MessageName.portalSuspended,
57+
name: 'portalSuspended',
5958
length: 5,
6059
}
6160

6261
export const replicationStart: BackendMessage = {
63-
name: MessageName.replicationStart,
62+
name: 'replicationStart',
6463
length: 4,
6564
}
6665

6766
export const emptyQuery: BackendMessage = {
68-
name: MessageName.emptyQuery,
67+
name: 'emptyQuery',
6968
length: 4,
7069
}
7170

7271
export const copyDone: BackendMessage = {
73-
name: MessageName.copyDone,
72+
name: 'copyDone',
7473
length: 4,
7574
}
7675

@@ -117,7 +116,7 @@ export class DatabaseError extends Error implements NoticeOrError {
117116
}
118117

119118
export class CopyDataMessage {
120-
public readonly name = MessageName.copyData
119+
public readonly name = 'copyData'
121120
constructor(public readonly length: number, public readonly chunk: Buffer) {}
122121
}
123122

@@ -146,15 +145,15 @@ export class Field {
146145
}
147146

148147
export class RowDescriptionMessage {
149-
public readonly name: MessageName = MessageName.rowDescription
148+
public readonly name: MessageName = 'rowDescription'
150149
public readonly fields: Field[]
151150
constructor(public readonly length: number, public readonly fieldCount: number) {
152151
this.fields = new Array(this.fieldCount)
153152
}
154153
}
155154

156155
export class ParameterStatusMessage {
157-
public readonly name: MessageName = MessageName.parameterStatus
156+
public readonly name: MessageName = 'parameterStatus'
158157
constructor(
159158
public readonly length: number,
160159
public readonly parameterName: string,
@@ -163,17 +162,17 @@ export class ParameterStatusMessage {
163162
}
164163

165164
export class AuthenticationMD5Password implements BackendMessage {
166-
public readonly name: MessageName = MessageName.authenticationMD5Password
165+
public readonly name: MessageName = 'authenticationMD5Password'
167166
constructor(public readonly length: number, public readonly salt: Buffer) {}
168167
}
169168

170169
export class BackendKeyDataMessage {
171-
public readonly name: MessageName = MessageName.backendKeyData
170+
public readonly name: MessageName = 'backendKeyData'
172171
constructor(public readonly length: number, public readonly processID: number, public readonly secretKey: number) {}
173172
}
174173

175174
export class NotificationResponseMessage {
176-
public readonly name: MessageName = MessageName.notification
175+
public readonly name: MessageName = 'notification'
177176
constructor(
178177
public readonly length: number,
179178
public readonly processId: number,
@@ -183,26 +182,26 @@ export class NotificationResponseMessage {
183182
}
184183

185184
export class ReadyForQueryMessage {
186-
public readonly name: MessageName = MessageName.readyForQuery
185+
public readonly name: MessageName = 'readyForQuery'
187186
constructor(public readonly length: number, public readonly status: string) {}
188187
}
189188

190189
export class CommandCompleteMessage {
191-
public readonly name: MessageName = MessageName.commandComplete
190+
public readonly name: MessageName = 'commandComplete'
192191
constructor(public readonly length: number, public readonly text: string) {}
193192
}
194193

195194
export class DataRowMessage {
196195
public readonly fieldCount: number
197-
public readonly name: MessageName = MessageName.dataRow
196+
public readonly name: MessageName = 'dataRow'
198197
constructor(public length: number, public fields: any[]) {
199198
this.fieldCount = fields.length
200199
}
201200
}
202201

203202
export class NoticeMessage implements BackendMessage, NoticeOrError {
204203
constructor(public readonly length: number, public readonly message: string | undefined) {}
205-
public readonly name = MessageName.notice
204+
public readonly name = 'notice'
206205
public severity: string | undefined
207206
public code: string | undefined
208207
public detail: string | undefined

packages/pg-protocol/src/parser.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ export class Parser {
183183
case MessageCodes.BackendKeyData:
184184
return this.parseBackendKeyData(offset, length, bytes)
185185
case MessageCodes.ErrorMessage:
186-
return this.parseErrorMessage(offset, length, bytes, MessageName.error)
186+
return this.parseErrorMessage(offset, length, bytes, 'error')
187187
case MessageCodes.NoticeMessage:
188-
return this.parseErrorMessage(offset, length, bytes, MessageName.notice)
188+
return this.parseErrorMessage(offset, length, bytes, 'notice')
189189
case MessageCodes.RowDescriptionMessage:
190190
return this.parseRowDescriptionMessage(offset, length, bytes)
191191
case MessageCodes.CopyIn:
@@ -217,11 +217,11 @@ export class Parser {
217217
}
218218

219219
private parseCopyInMessage(offset: number, length: number, bytes: Buffer) {
220-
return this.parseCopyMessage(offset, length, bytes, MessageName.copyInResponse)
220+
return this.parseCopyMessage(offset, length, bytes, 'copyInResponse')
221221
}
222222

223223
private parseCopyOutMessage(offset: number, length: number, bytes: Buffer) {
224-
return this.parseCopyMessage(offset, length, bytes, MessageName.copyOutResponse)
224+
return this.parseCopyMessage(offset, length, bytes, 'copyOutResponse')
225225
}
226226

227227
private parseCopyMessage(offset: number, length: number, bytes: Buffer, messageName: MessageName) {
@@ -295,7 +295,7 @@ export class Parser {
295295
const code = this.reader.int32()
296296
// TODO(bmc): maybe better types here
297297
const message: BackendMessage & any = {
298-
name: MessageName.authenticationOk,
298+
name: 'authenticationOk',
299299
length,
300300
}
301301

@@ -304,18 +304,18 @@ export class Parser {
304304
break
305305
case 3: // AuthenticationCleartextPassword
306306
if (message.length === 8) {
307-
message.name = MessageName.authenticationCleartextPassword
307+
message.name = 'authenticationCleartextPassword'
308308
}
309309
break
310310
case 5: // AuthenticationMD5Password
311311
if (message.length === 12) {
312-
message.name = MessageName.authenticationMD5Password
312+
message.name = 'authenticationMD5Password'
313313
const salt = this.reader.bytes(4)
314314
return new AuthenticationMD5Password(length, salt)
315315
}
316316
break
317317
case 10: // AuthenticationSASL
318-
message.name = MessageName.authenticationSASL
318+
message.name = 'authenticationSASL'
319319
message.mechanisms = []
320320
let mechanism: string
321321
do {
@@ -327,11 +327,11 @@ export class Parser {
327327
} while (mechanism)
328328
break
329329
case 11: // AuthenticationSASLContinue
330-
message.name = MessageName.authenticationSASLContinue
330+
message.name = 'authenticationSASLContinue'
331331
message.data = this.reader.string(length - 8)
332332
break
333333
case 12: // AuthenticationSASLFinal
334-
message.name = MessageName.authenticationSASLFinal
334+
message.name = 'authenticationSASLFinal'
335335
message.data = this.reader.string(length - 8)
336336
break
337337
default:
@@ -352,9 +352,7 @@ export class Parser {
352352
const messageValue = fields.M
353353

354354
const message =
355-
name === MessageName.notice
356-
? new NoticeMessage(length, messageValue)
357-
: new DatabaseError(messageValue, length, name)
355+
name === 'notice' ? new NoticeMessage(length, messageValue) : new DatabaseError(messageValue, length, name)
358356

359357
message.severity = fields.S
360358
message.code = fields.C

0 commit comments

Comments
 (0)