@pgxsinkit/pgwasm
Version:
564 lines (537 loc) • 15.4 kB
JavaScript
// @bun
import {
__export
} from "./chunk-kb3rsr7n.js";
// packages/pgwasm/src/protocol/wire/messages.ts
var exports_messages = {};
__export(exports_messages, {
AuthenticationCleartextPassword: () => AuthenticationCleartextPassword,
AuthenticationMD5Password: () => AuthenticationMD5Password,
AuthenticationOk: () => AuthenticationOk,
AuthenticationSASL: () => AuthenticationSASL,
AuthenticationSASLContinue: () => AuthenticationSASLContinue,
AuthenticationSASLFinal: () => AuthenticationSASLFinal,
BackendKeyDataMessage: () => BackendKeyDataMessage,
CommandCompleteMessage: () => CommandCompleteMessage,
CopyDataMessage: () => CopyDataMessage,
CopyResponse: () => CopyResponse,
DataRowMessage: () => DataRowMessage,
DatabaseError: () => DatabaseError2,
Field: () => Field,
NoticeMessage: () => NoticeMessage,
NotificationResponseMessage: () => NotificationResponseMessage,
ParameterDescriptionMessage: () => ParameterDescriptionMessage,
ParameterStatusMessage: () => ParameterStatusMessage,
ReadyForQueryMessage: () => ReadyForQueryMessage,
RowDescriptionMessage: () => RowDescriptionMessage,
bindComplete: () => bindComplete,
closeComplete: () => closeComplete,
copyDone: () => copyDone,
emptyQuery: () => emptyQuery,
noData: () => noData,
parseComplete: () => parseComplete,
portalSuspended: () => portalSuspended,
replicationStart: () => replicationStart
});
var parseComplete = { name: "parseComplete", length: 5 };
var bindComplete = { name: "bindComplete", length: 5 };
var closeComplete = { name: "closeComplete", length: 5 };
var noData = { name: "noData", length: 5 };
var portalSuspended = { name: "portalSuspended", length: 5 };
var replicationStart = { name: "replicationStart", length: 4 };
var emptyQuery = { name: "emptyQuery", length: 4 };
var copyDone = { name: "copyDone", length: 4 };
class AuthenticationOk {
name = "authenticationOk";
length;
constructor(length) {
this.length = length;
}
}
class AuthenticationCleartextPassword {
name = "authenticationCleartextPassword";
length;
constructor(length) {
this.length = length;
}
}
class AuthenticationMD5Password {
name = "authenticationMD5Password";
length;
salt;
constructor(length, salt) {
this.length = length;
this.salt = salt;
}
}
class AuthenticationSASL {
name = "authenticationSASL";
length;
mechanisms;
constructor(length, mechanisms) {
this.length = length;
this.mechanisms = mechanisms;
}
}
class AuthenticationSASLContinue {
name = "authenticationSASLContinue";
length;
data;
constructor(length, data) {
this.length = length;
this.data = data;
}
}
class AuthenticationSASLFinal {
name = "authenticationSASLFinal";
length;
data;
constructor(length, data) {
this.length = length;
this.data = data;
}
}
class DatabaseError2 extends Error {
severity;
code;
detail;
hint;
position;
internalPosition;
internalQuery;
where;
schema;
table;
column;
dataType;
constraint;
file;
line;
routine;
length;
name;
constructor(message, length, name) {
super(message);
this.length = length;
this.name = name;
}
}
class CopyDataMessage {
name = "copyData";
length;
chunk;
constructor(length, chunk) {
this.length = length;
this.chunk = chunk;
}
}
class CopyResponse {
length;
name;
binary;
columnTypes;
constructor(length, name, binary, columnCount) {
this.length = length;
this.name = name;
this.binary = binary;
this.columnTypes = Array.from({ length: columnCount }, () => 0);
}
}
class Field {
name;
tableID;
columnID;
dataTypeID;
dataTypeSize;
dataTypeModifier;
format;
constructor(name, tableID, columnID, dataTypeID, dataTypeSize, dataTypeModifier, format) {
this.name = name;
this.tableID = tableID;
this.columnID = columnID;
this.dataTypeID = dataTypeID;
this.dataTypeSize = dataTypeSize;
this.dataTypeModifier = dataTypeModifier;
this.format = format;
}
}
class RowDescriptionMessage {
name = "rowDescription";
length;
fieldCount;
fields;
constructor(length, fields) {
this.length = length;
this.fieldCount = fields.length;
this.fields = fields;
}
}
class ParameterDescriptionMessage {
name = "parameterDescription";
length;
parameterCount;
dataTypeIDs;
constructor(length, dataTypeIDs) {
this.length = length;
this.parameterCount = dataTypeIDs.length;
this.dataTypeIDs = dataTypeIDs;
}
}
class ParameterStatusMessage {
name = "parameterStatus";
length;
parameterName;
parameterValue;
constructor(length, parameterName, parameterValue) {
this.length = length;
this.parameterName = parameterName;
this.parameterValue = parameterValue;
}
}
class BackendKeyDataMessage {
name = "backendKeyData";
length;
processID;
secretKey;
constructor(length, processID, secretKey) {
this.length = length;
this.processID = processID;
this.secretKey = secretKey;
}
}
class NotificationResponseMessage {
name = "notification";
length;
processId;
channel;
payload;
constructor(length, processId, channel, payload) {
this.length = length;
this.processId = processId;
this.channel = channel;
this.payload = payload;
}
}
class ReadyForQueryMessage {
name = "readyForQuery";
length;
status;
constructor(length, status) {
this.length = length;
this.status = status;
}
}
class CommandCompleteMessage {
name = "commandComplete";
length;
text;
constructor(length, text) {
this.length = length;
this.text = text;
}
}
class DataRowMessage {
name = "dataRow";
length;
fieldCount;
fields;
constructor(length, fields) {
this.length = length;
this.fields = fields;
this.fieldCount = fields.length;
}
}
class NoticeMessage {
name = "notice";
length;
message;
severity;
code;
detail;
hint;
position;
internalPosition;
internalQuery;
where;
schema;
table;
column;
dataType;
constraint;
file;
line;
routine;
constructor(length, message) {
this.length = length;
this.message = message;
}
}
// packages/pgwasm/src/protocol/wire/string-utils.ts
function byteLengthUtf8(str) {
let byteLength = str.length;
for (let i = str.length - 1;i >= 0; i--) {
const code = str.charCodeAt(i);
if (code > 127 && code <= 2047)
byteLength++;
else if (code > 2047 && code <= 65535)
byteLength += 2;
if (code >= 56320 && code <= 57343)
i--;
}
return byteLength;
}
// packages/pgwasm/src/protocol/wire/buffer-writer.ts
class Writer {
#size;
#bufferView;
#offset = 5;
#encoder = new TextEncoder;
#headerPosition = 0;
constructor(size = 256) {
this.#size = size;
this.#bufferView = this.#allocateBuffer(size);
}
#allocateBuffer(size) {
return new DataView(new ArrayBuffer(size));
}
#ensure(size) {
const remaining = this.#bufferView.byteLength - this.#offset;
if (remaining < size) {
const oldBuffer = this.#bufferView.buffer;
const newSize = oldBuffer.byteLength + (oldBuffer.byteLength >> 1) + size;
this.#bufferView = this.#allocateBuffer(newSize);
new Uint8Array(this.#bufferView.buffer).set(new Uint8Array(oldBuffer));
}
}
addInt32(num) {
this.#ensure(4);
this.#bufferView.setInt32(this.#offset, num, false);
this.#offset += 4;
return this;
}
addInt16(num) {
this.#ensure(2);
this.#bufferView.setInt16(this.#offset, num, false);
this.#offset += 2;
return this;
}
addCString(string) {
if (string) {
this.addString(string);
}
this.#ensure(1);
this.#bufferView.setUint8(this.#offset, 0);
this.#offset++;
return this;
}
addString(string = "") {
const length = byteLengthUtf8(string);
this.#ensure(length);
this.#encoder.encodeInto(string, new Uint8Array(this.#bufferView.buffer, this.#offset));
this.#offset += length;
return this;
}
add(otherBuffer) {
const bytes = ArrayBuffer.isView(otherBuffer) ? new Uint8Array(otherBuffer.buffer, otherBuffer.byteOffset, otherBuffer.byteLength) : new Uint8Array(otherBuffer);
this.#ensure(bytes.byteLength);
new Uint8Array(this.#bufferView.buffer).set(bytes, this.#offset);
this.#offset += bytes.byteLength;
return this;
}
#join(code) {
if (code) {
this.#bufferView.setUint8(this.#headerPosition, code);
const length = this.#offset - (this.#headerPosition + 1);
this.#bufferView.setInt32(this.#headerPosition + 1, length, false);
}
return this.#bufferView.buffer.slice(code ? 0 : 5, this.#offset);
}
flush(code) {
const result = this.#join(code);
this.#offset = 5;
this.#bufferView = this.#allocateBuffer(this.#size);
return new Uint8Array(result);
}
}
// packages/pgwasm/src/protocol/wire/serializer.ts
var code = {
startup: 112,
query: 81,
parse: 80,
bind: 66,
execute: 69,
flush: 72,
sync: 83,
end: 88,
close: 67,
describe: 68,
copyFromChunk: 100,
copyDone: 99,
copyFail: 102
};
var writer = new Writer;
function startup(opts) {
writer.addInt16(3).addInt16(0);
for (const [key, value] of Object.entries(opts)) {
writer.addCString(key).addCString(value);
}
writer.addCString("client_encoding").addCString("UTF8");
const bodyBuffer = writer.addCString("").flush();
const length = bodyBuffer.byteLength + 4;
return new Writer().addInt32(length).add(bodyBuffer).flush();
}
function requestSsl() {
const bufferView = new DataView(new ArrayBuffer(8));
bufferView.setInt32(0, 8, false);
bufferView.setInt32(4, 80877103, false);
return new Uint8Array(bufferView.buffer);
}
function password(value) {
return writer.addCString(value).flush(code.startup);
}
function sendSASLInitialResponseMessage(mechanism, initialResponse) {
writer.addCString(mechanism).addInt32(byteLengthUtf8(initialResponse)).addString(initialResponse);
return writer.flush(code.startup);
}
function sendSCRAMClientFinalMessage(additionalData) {
return writer.addString(additionalData).flush(code.startup);
}
function query(text) {
return writer.addCString(text).flush(code.query);
}
var emptyValueArray = [];
function parse(opts) {
const name = opts.name ?? "";
if (name.length > 63) {
throw new RangeError(`Postgres statement names are at most 63 characters: "${name}" (${name.length})`);
}
const buffer = writer.addCString(name).addCString(opts.text).addInt16(opts.types?.length ?? 0);
opts.types?.forEach((type) => buffer.addInt32(type));
return writer.flush(code.parse);
}
var paramWriter = new Writer;
var ParamType = { STRING: 0, BINARY: 1 };
function writeValues(values, valueMapper) {
for (let i = 0;i < values.length; i++) {
const value = values[i] ?? null;
const mappedVal = valueMapper ? valueMapper(value, i) : value;
if (mappedVal === null) {
writer.addInt16(ParamType.STRING);
paramWriter.addInt32(-1);
} else if (mappedVal instanceof ArrayBuffer || ArrayBuffer.isView(mappedVal)) {
const byteLength = mappedVal.byteLength;
writer.addInt16(ParamType.BINARY);
paramWriter.addInt32(byteLength);
paramWriter.add(mappedVal);
} else {
writer.addInt16(ParamType.STRING);
paramWriter.addInt32(byteLengthUtf8(mappedVal));
paramWriter.addString(mappedVal);
}
}
}
function bind(config = {}) {
const portal = config.portal ?? "";
const statement = config.statement ?? "";
const binary = config.binary ?? false;
const values = config.values ?? emptyValueArray;
const len = values.length;
writer.addCString(portal).addCString(statement);
writer.addInt16(len);
writeValues(values, config.valueMapper);
writer.addInt16(len);
writer.add(paramWriter.flush());
writer.addInt16(binary ? ParamType.BINARY : ParamType.STRING);
return writer.flush(code.bind);
}
var emptyExecute = new Uint8Array([code.execute, 0, 0, 0, 9, 0, 0, 0, 0, 0]);
function execute(config) {
if (!config || !config.portal && !config.rows) {
return emptyExecute;
}
const portal = config.portal ?? "";
const rows = config.rows ?? 0;
const portalLength = byteLengthUtf8(portal);
const len = 4 + portalLength + 1 + 4;
const bufferView = new DataView(new ArrayBuffer(1 + len));
bufferView.setUint8(0, code.execute);
bufferView.setInt32(1, len, false);
new TextEncoder().encodeInto(portal, new Uint8Array(bufferView.buffer, 5));
bufferView.setUint8(portalLength + 5, 0);
bufferView.setUint32(bufferView.byteLength - 4, rows, false);
return new Uint8Array(bufferView.buffer);
}
function cancel(processID, secretKey) {
const bufferView = new DataView(new ArrayBuffer(16));
bufferView.setInt32(0, 16, false);
bufferView.setInt16(4, 1234, false);
bufferView.setInt16(6, 5678, false);
bufferView.setInt32(8, processID, false);
bufferView.setInt32(12, secretKey, false);
return new Uint8Array(bufferView.buffer);
}
function cstringMessage(messageCode, string) {
return new Writer().addCString(string).flush(messageCode);
}
var emptyDescribePortal = writer.addCString("P").flush(code.describe);
var emptyDescribeStatement = writer.addCString("S").flush(code.describe);
function describe(msg) {
if (msg.name)
return cstringMessage(code.describe, `${msg.type}${msg.name}`);
return msg.type === "P" ? emptyDescribePortal : emptyDescribeStatement;
}
function close(msg) {
return cstringMessage(code.close, `${msg.type}${msg.name ?? ""}`);
}
function copyData(chunk) {
return writer.add(chunk).flush(code.copyFromChunk);
}
function copyFail(message) {
return cstringMessage(code.copyFail, message);
}
var codeOnlyBuffer = (messageCode) => new Uint8Array([messageCode, 0, 0, 0, 4]);
var flushBuffer = codeOnlyBuffer(code.flush);
var syncBuffer = codeOnlyBuffer(code.sync);
var endBuffer = codeOnlyBuffer(code.end);
var copyDoneBuffer = codeOnlyBuffer(code.copyDone);
var serialize2 = {
startup,
password,
requestSsl,
sendSASLInitialResponseMessage,
sendSCRAMClientFinalMessage,
query,
parse,
bind,
execute,
describe,
close,
flush: () => flushBuffer,
sync: () => syncBuffer,
end: () => endBuffer,
copyData,
copyDone: () => copyDoneBuffer,
copyFail,
cancel
};
// packages/pgwasm/src/core/internals.ts
var registry = new WeakMap;
function registerProtocolAccess(pg, access) {
registry.set(pg, access);
}
function protocolAccessOf(pg) {
const access = registry.get(pg);
if (access === undefined) {
throw new TypeError("Not a pgwasm instance (created by createPgwasm).");
}
return access;
}
var strictSyncs = new WeakMap;
function registerStrictSync(pg, strictSync) {
strictSyncs.set(pg, strictSync);
}
function strictSyncOf(pg) {
return strictSyncs.get(pg);
}
export { parseComplete, bindComplete, closeComplete, noData, portalSuspended, replicationStart, emptyQuery, copyDone, AuthenticationOk, AuthenticationCleartextPassword, AuthenticationMD5Password, AuthenticationSASL, AuthenticationSASLContinue, AuthenticationSASLFinal, DatabaseError2, CopyDataMessage, CopyResponse, Field, RowDescriptionMessage, ParameterDescriptionMessage, ParameterStatusMessage, BackendKeyDataMessage, NotificationResponseMessage, ReadyForQueryMessage, CommandCompleteMessage, DataRowMessage, NoticeMessage, exports_messages, serialize2, registerProtocolAccess, protocolAccessOf, registerStrictSync, strictSyncOf };
//# debugId=1B7DEC0546AD5FAB64756E2164756E21