Skip to content

Commit 78a63d7

Browse files
authored
Revert "[pg-protocol] use literals instead of const enum (#2490)"
This reverts commit 45fa27e.
1 parent 45fa27e commit 78a63d7

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

packages/pg-protocol/src/messages.ts

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

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'
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+
}
3031

3132
export interface BackendMessage {
3233
name: MessageName
3334
length: number
3435
}
3536

3637
export const parseComplete: BackendMessage = {
37-
name: 'parseComplete',
38+
name: MessageName.parseComplete,
3839
length: 5,
3940
}
4041

4142
export const bindComplete: BackendMessage = {
42-
name: 'bindComplete',
43+
name: MessageName.bindComplete,
4344
length: 5,
4445
}
4546

4647
export const closeComplete: BackendMessage = {
47-
name: 'closeComplete',
48+
name: MessageName.closeComplete,
4849
length: 5,
4950
}
5051

5152
export const noData: BackendMessage = {
52-
name: 'noData',
53+
name: MessageName.noData,
5354
length: 5,
5455
}
5556

5657
export const portalSuspended: BackendMessage = {
57-
name: 'portalSuspended',
58+
name: MessageName.portalSuspended,
5859
length: 5,
5960
}
6061

6162
export const replicationStart: BackendMessage = {
62-
name: 'replicationStart',
63+
name: MessageName.replicationStart,
6364
length: 4,
6465
}
6566

6667
export const emptyQuery: BackendMessage = {
67-
name: 'emptyQuery',
68+
name: MessageName.emptyQuery,
6869
length: 4,
6970
}
7071

7172
export const copyDone: BackendMessage = {
72-
name: 'copyDone',
73+
name: MessageName.copyDone,
7374
length: 4,
7475
}
7576

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

118119
export class CopyDataMessage {
119-
public readonly name = 'copyData'
120+
public readonly name = MessageName.copyData
120121
constructor(public readonly length: number, public readonly chunk: Buffer) {}
121122
}
122123

@@ -145,15 +146,15 @@ export class Field {
145146
}
146147

147148
export class RowDescriptionMessage {
148-
public readonly name: MessageName = 'rowDescription'
149+
public readonly name: MessageName = MessageName.rowDescription
149150
public readonly fields: Field[]
150151
constructor(public readonly length: number, public readonly fieldCount: number) {
151152
this.fields = new Array(this.fieldCount)
152153
}
153154
}
154155

155156
export class ParameterStatusMessage {
156-
public readonly name: MessageName = 'parameterStatus'
157+
public readonly name: MessageName = MessageName.parameterStatus
157158
constructor(
158159
public readonly length: number,
159160
public readonly parameterName: string,
@@ -162,17 +163,17 @@ export class ParameterStatusMessage {
162163
}
163164

164165
export class AuthenticationMD5Password implements BackendMessage {
165-
public readonly name: MessageName = 'authenticationMD5Password'
166+
public readonly name: MessageName = MessageName.authenticationMD5Password
166167
constructor(public readonly length: number, public readonly salt: Buffer) {}
167168
}
168169

169170
export class BackendKeyDataMessage {
170-
public readonly name: MessageName = 'backendKeyData'
171+
public readonly name: MessageName = MessageName.backendKeyData
171172
constructor(public readonly length: number, public readonly processID: number, public readonly secretKey: number) {}
172173
}
173174

174175
export class NotificationResponseMessage {
175-
public readonly name: MessageName = 'notification'
176+
public readonly name: MessageName = MessageName.notification
176177
constructor(
177178
public readonly length: number,
178179
public readonly processId: number,
@@ -182,26 +183,26 @@ export class NotificationResponseMessage {
182183
}
183184

184185
export class ReadyForQueryMessage {
185-
public readonly name: MessageName = 'readyForQuery'
186+
public readonly name: MessageName = MessageName.readyForQuery
186187
constructor(public readonly length: number, public readonly status: string) {}
187188
}
188189

189190
export class CommandCompleteMessage {
190-
public readonly name: MessageName = 'commandComplete'
191+
public readonly name: MessageName = MessageName.commandComplete
191192
constructor(public readonly length: number, public readonly text: string) {}
192193
}
193194

194195
export class DataRowMessage {
195196
public readonly fieldCount: number
196-
public readonly name: MessageName = 'dataRow'
197+
public readonly name: MessageName = MessageName.dataRow
197198
constructor(public length: number, public fields: any[]) {
198199
this.fieldCount = fields.length
199200
}
200201
}
201202

202203
export class NoticeMessage implements BackendMessage, NoticeOrError {
203204
constructor(public readonly length: number, public readonly message: string | undefined) {}
204-
public readonly name = 'notice'
205+
public readonly name = MessageName.notice
205206
public severity: string | undefined
206207
public code: string | undefined
207208
public detail: string | undefined

packages/pg-protocol/src/parser.ts

+13-11
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, 'error')
186+
return this.parseErrorMessage(offset, length, bytes, MessageName.error)
187187
case MessageCodes.NoticeMessage:
188-
return this.parseErrorMessage(offset, length, bytes, 'notice')
188+
return this.parseErrorMessage(offset, length, bytes, MessageName.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, 'copyInResponse')
220+
return this.parseCopyMessage(offset, length, bytes, MessageName.copyInResponse)
221221
}
222222

223223
private parseCopyOutMessage(offset: number, length: number, bytes: Buffer) {
224-
return this.parseCopyMessage(offset, length, bytes, 'copyOutResponse')
224+
return this.parseCopyMessage(offset, length, bytes, MessageName.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: 'authenticationOk',
298+
name: MessageName.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 = 'authenticationCleartextPassword'
307+
message.name = MessageName.authenticationCleartextPassword
308308
}
309309
break
310310
case 5: // AuthenticationMD5Password
311311
if (message.length === 12) {
312-
message.name = 'authenticationMD5Password'
312+
message.name = MessageName.authenticationMD5Password
313313
const salt = this.reader.bytes(4)
314314
return new AuthenticationMD5Password(length, salt)
315315
}
316316
break
317317
case 10: // AuthenticationSASL
318-
message.name = 'authenticationSASL'
318+
message.name = MessageName.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 = 'authenticationSASLContinue'
330+
message.name = MessageName.authenticationSASLContinue
331331
message.data = this.reader.string(length - 8)
332332
break
333333
case 12: // AuthenticationSASLFinal
334-
message.name = 'authenticationSASLFinal'
334+
message.name = MessageName.authenticationSASLFinal
335335
message.data = this.reader.string(length - 8)
336336
break
337337
default:
@@ -352,7 +352,9 @@ export class Parser {
352352
const messageValue = fields.M
353353

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

357359
message.severity = fields.S
358360
message.code = fields.C

0 commit comments

Comments
 (0)