@rebasepro/server-postgresql 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.umd.js CHANGED
@@ -3972,39 +3972,18 @@
3972
3972
  }
3973
3973
  return value;
3974
3974
  }
3975
- case "binary":
3976
- if (typeof value === "string") {
3977
- if (value.startsWith("data:application/octet-stream;base64,")) {
3978
- const base64Data = value.split(",")[1];
3979
- if (base64Data) {
3980
- return Buffer.from(base64Data, "base64");
3981
- }
3982
- }
3983
- }
3984
- if (Buffer.isBuffer(value)) {
3985
- return value;
3986
- }
3975
+ case "binary": {
3976
+ const decoded = tryDecodeBase64DataUrl(value);
3977
+ if (decoded) return decoded;
3978
+ if (Buffer.isBuffer(value)) return value;
3987
3979
  return value;
3980
+ }
3988
3981
  case "string":
3989
- if (typeof value === "string") {
3990
- if (value.startsWith("data:application/octet-stream;base64,")) {
3991
- const base64Data = value.split(",")[1];
3992
- if (base64Data) {
3993
- return Buffer.from(base64Data, "base64");
3994
- }
3995
- }
3996
- }
3997
- return value;
3998
- default:
3999
- if (typeof value === "string") {
4000
- if (value.startsWith("data:application/octet-stream;base64,")) {
4001
- const base64Data = value.split(",")[1];
4002
- if (base64Data) {
4003
- return Buffer.from(base64Data, "base64");
4004
- }
4005
- }
4006
- }
3982
+ default: {
3983
+ const decoded = tryDecodeBase64DataUrl(value);
3984
+ if (decoded) return decoded;
4007
3985
  return value;
3986
+ }
4008
3987
  }
4009
3988
  }
4010
3989
  async function parseDataFromServer(data, collection, db, registry) {
@@ -4124,21 +4103,36 @@
4124
4103
  }
4125
4104
  return result;
4126
4105
  }
4127
- function parsePropertyFromServer(value, property, collection, propertyKey) {
4128
- if (value === null || value === void 0) {
4129
- return value;
4106
+ function tryDecodeBase64DataUrl(value) {
4107
+ if (typeof value !== "string") return null;
4108
+ if (!value.startsWith("data:application/octet-stream;base64,")) return null;
4109
+ const base64Data = value.split(",")[1];
4110
+ return base64Data ? Buffer.from(base64Data, "base64") : null;
4111
+ }
4112
+ function tryResolveBuffer(value) {
4113
+ if (Buffer.isBuffer(value)) return value;
4114
+ if (typeof value === "object" && value !== null) {
4115
+ const rawVal = value;
4116
+ if (rawVal.type === "Buffer" && Array.isArray(rawVal.data)) {
4117
+ return Buffer.from(rawVal.data);
4118
+ }
4130
4119
  }
4120
+ return null;
4121
+ }
4122
+ function bufferToStringOrBase64(buf) {
4123
+ for (let i = 0; i < buf.length; i++) {
4124
+ const b = buf[i];
4125
+ if ((b < 32 || b > 126) && b !== 9 && b !== 10 && b !== 13) {
4126
+ return `data:application/octet-stream;base64,${buf.toString("base64")}`;
4127
+ }
4128
+ }
4129
+ return buf.toString("utf8");
4130
+ }
4131
+ function parsePropertyFromServer(value, property, collection, propertyKey) {
4132
+ if (value === null || value === void 0) return value;
4131
4133
  switch (property.type) {
4132
4134
  case "binary": {
4133
- let buf = null;
4134
- if (Buffer.isBuffer(value)) {
4135
- buf = value;
4136
- } else if (typeof value === "object" && value !== null) {
4137
- const rawVal = value;
4138
- if (rawVal.type === "Buffer" && Array.isArray(rawVal.data)) {
4139
- buf = Buffer.from(rawVal.data);
4140
- }
4141
- }
4135
+ const buf = tryResolveBuffer(value);
4142
4136
  if (buf) {
4143
4137
  return `data:application/octet-stream;base64,${buf.toString("base64")}`;
4144
4138
  }
@@ -4146,28 +4140,9 @@
4146
4140
  }
4147
4141
  case "string": {
4148
4142
  if (typeof value === "string") return value;
4149
- let isBuffer = false;
4150
- let buf = null;
4151
- if (Buffer.isBuffer(value)) {
4152
- isBuffer = true;
4153
- buf = value;
4154
- } else if (typeof value === "object" && value !== null) {
4155
- const rawVal = value;
4156
- if (rawVal.type === "Buffer" && Array.isArray(rawVal.data)) {
4157
- isBuffer = true;
4158
- buf = Buffer.from(rawVal.data);
4159
- }
4160
- }
4161
- if (isBuffer && buf) {
4162
- let isPrintable = true;
4163
- for (let i = 0; i < buf.length; i++) {
4164
- const b = buf[i];
4165
- if ((b < 32 || b > 126) && b !== 9 && b !== 10 && b !== 13) {
4166
- isPrintable = false;
4167
- break;
4168
- }
4169
- }
4170
- return isPrintable ? buf.toString("utf8") : `data:application/octet-stream;base64,${buf.toString("base64")}`;
4143
+ const buf = tryResolveBuffer(value);
4144
+ if (buf) {
4145
+ return bufferToStringOrBase64(buf);
4171
4146
  }
4172
4147
  if (typeof value === "object" && value !== null) {
4173
4148
  try {
@@ -4281,28 +4256,9 @@
4281
4256
  return null;
4282
4257
  }
4283
4258
  default: {
4284
- let isBuffer = false;
4285
- let buf = null;
4286
- if (Buffer.isBuffer(value)) {
4287
- isBuffer = true;
4288
- buf = value;
4289
- } else if (typeof value === "object" && value !== null) {
4290
- const rawVal = value;
4291
- if (rawVal.type === "Buffer" && Array.isArray(rawVal.data)) {
4292
- isBuffer = true;
4293
- buf = Buffer.from(rawVal.data);
4294
- }
4295
- }
4296
- if (isBuffer && buf) {
4297
- let isPrintable = true;
4298
- for (let i = 0; i < buf.length; i++) {
4299
- const b = buf[i];
4300
- if ((b < 32 || b > 126) && b !== 9 && b !== 10 && b !== 13) {
4301
- isPrintable = false;
4302
- break;
4303
- }
4304
- }
4305
- return isPrintable ? buf.toString("utf8") : `data:application/octet-stream;base64,${buf.toString("base64")}`;
4259
+ const buf = tryResolveBuffer(value);
4260
+ if (buf) {
4261
+ return bufferToStringOrBase64(buf);
4306
4262
  }
4307
4263
  return value;
4308
4264
  }
@@ -6495,12 +6451,12 @@
6495
6451
  */
6496
6452
  extractCauseMessage(error) {
6497
6453
  if (!error || typeof error !== "object") return null;
6498
- const err = error;
6499
- if (err.cause && typeof err.cause === "object") {
6500
- const deeper = this.extractCauseMessage(err.cause);
6454
+ if (!(error instanceof Error)) return null;
6455
+ if (error.cause && typeof error.cause === "object") {
6456
+ const deeper = this.extractCauseMessage(error.cause);
6501
6457
  if (deeper) return deeper;
6502
- if (err.cause instanceof Error && err.cause.message) {
6503
- return err.cause.message;
6458
+ if (error.cause instanceof Error && error.cause.message) {
6459
+ return error.cause.message;
6504
6460
  }
6505
6461
  }
6506
6462
  return null;
@@ -6521,12 +6477,17 @@
6521
6477
  */
6522
6478
  extractPgError(error) {
6523
6479
  if (!error || typeof error !== "object") return null;
6524
- const err = error;
6525
- if (err.code && /^[0-9A-Z]{5}$/.test(err.code)) {
6526
- return err;
6480
+ if (!(error instanceof Error)) {
6481
+ if ("cause" in error && error.cause && typeof error.cause === "object") {
6482
+ return this.extractPgError(error.cause);
6483
+ }
6484
+ return null;
6485
+ }
6486
+ if ("code" in error && typeof error.code === "string" && /^[0-9A-Z]{5}$/.test(error.code)) {
6487
+ return error;
6527
6488
  }
6528
- if (err.cause && typeof err.cause === "object") {
6529
- return this.extractPgError(err.cause);
6489
+ if (error.cause && typeof error.cause === "object") {
6490
+ return this.extractPgError(error.cause);
6530
6491
  }
6531
6492
  return null;
6532
6493
  }
@@ -7641,7 +7602,7 @@
7641
7602
  return this.withTransaction((delegate) => delegate.deleteEntity(props));
7642
7603
  }
7643
7604
  async deleteAll(path2) {
7644
- return this.delegate.deleteAll(path2);
7605
+ return this.withTransaction((delegate) => delegate.deleteAll(path2));
7645
7606
  }
7646
7607
  async checkUniqueField(path2, name, value, entityId, collection) {
7647
7608
  return this.withTransaction((delegate) => delegate.checkUniqueField(path2, name, value, entityId, collection));
@@ -7720,6 +7681,7 @@
7720
7681
  }
7721
7682
  await Promise.all(promises);
7722
7683
  this.pools.clear();
7684
+ this.drizzleInstances.clear();
7723
7685
  }
7724
7686
  }
7725
7687
  function createAuthSchema(usersSchemaName = "rebase") {
@@ -9613,20 +9575,19 @@ ${tableRelations.join(",\n")}
9613
9575
  }
9614
9576
  }
9615
9577
  const PostgresRealtimeProvider = RealtimeService;
9616
- const clientSessions = /* @__PURE__ */ new Map();
9617
9578
  const WS_RATE_LIMIT = 2e3;
9618
9579
  const WS_RATE_WINDOW_MS = 6e4;
9619
9580
  const ADMIN_ONLY_TYPES = /* @__PURE__ */ new Set(["EXECUTE_SQL", "FETCH_DATABASES", "FETCH_ROLES", "FETCH_UNMAPPED_TABLES", "FETCH_TABLE_METADATA", "FETCH_CURRENT_DATABASE", "CREATE_BRANCH", "DELETE_BRANCH", "LIST_BRANCHES"]);
9620
9581
  function extractErrorMessage(error) {
9621
9582
  if (!error) return "Unknown error";
9622
- if (typeof error === "object") {
9623
- const err = error;
9624
- if (err.cause) {
9625
- return extractErrorMessage(err.cause);
9626
- }
9627
- if (typeof err.message === "string") {
9628
- return err.message;
9583
+ if (error instanceof Error) {
9584
+ if ("cause" in error && error.cause) {
9585
+ return extractErrorMessage(error.cause);
9629
9586
  }
9587
+ return error.message;
9588
+ }
9589
+ if (typeof error === "object" && "message" in error && typeof error.message === "string") {
9590
+ return error.message;
9630
9591
  }
9631
9592
  return String(error);
9632
9593
  }
@@ -9634,14 +9595,10 @@ ${tableRelations.join(",\n")}
9634
9595
  if (!session?.user) return false;
9635
9596
  if (session.user.isAdmin) return true;
9636
9597
  if (!session.user.roles) return false;
9637
- return session.user.roles.some((r) => {
9638
- if (typeof r === "string") return r === "admin";
9639
- if (r && typeof r === "object" && "isAdmin" in r) return r.isAdmin;
9640
- if (r && typeof r === "object" && "id" in r) return r.id === "admin";
9641
- return false;
9642
- });
9598
+ return session.user.roles.some((r) => r === "admin");
9643
9599
  }
9644
9600
  function createPostgresWebSocket(server, realtimeService, driver, authConfig, authAdapter) {
9601
+ const clientSessions = /* @__PURE__ */ new Map();
9645
9602
  const isProduction = process.env.NODE_ENV === "production";
9646
9603
  const wsDebug = (...args) => {
9647
9604
  if (!isProduction) console.debug(...args);
@@ -9780,13 +9737,23 @@ ${tableRelations.join(",\n")}
9780
9737
  }
9781
9738
  const getScopedDelegate = async () => {
9782
9739
  const session = clientSessions.get(clientId);
9783
- if ("withAuth" in driver && typeof driver.withAuth === "function") {
9740
+ if (typeof driver.withAuth === "function") {
9784
9741
  try {
9785
9742
  const userForAuth = session?.user ? {
9786
9743
  uid: session.user.userId,
9744
+ displayName: null,
9745
+ email: null,
9746
+ photoURL: null,
9747
+ providerId: "websocket",
9748
+ isAnonymous: false,
9787
9749
  roles: session.user.roles ?? []
9788
9750
  } : {
9789
9751
  uid: "anon",
9752
+ displayName: null,
9753
+ email: null,
9754
+ photoURL: null,
9755
+ providerId: "websocket",
9756
+ isAnonymous: true,
9790
9757
  roles: ["anon"]
9791
9758
  };
9792
9759
  return await driver.withAuth(userForAuth);