@byok-sdk/cloud-dataplane 0.9.1 → 0.10.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.js CHANGED
@@ -803,10 +803,17 @@ function toRecord(row) {
803
803
  proofKeyId: row.proof_key_id,
804
804
  proofKeyEpoch: row.proof_key_epoch,
805
805
  revoked: row.revoked,
806
+ ...row.machine_id == null ? {} : { machineId: row.machine_id },
806
807
  ...row.capabilities == null ? {} : { capabilities: Object.freeze([...row.capabilities]) }
807
808
  };
808
809
  }
809
- var SELECT_COLUMNS = "tenant_id, device_id, product_id, device_name, device_public_key, proof_key_id, proof_key_epoch, revoked, capabilities";
810
+ var SELECT_COLUMNS = "tenant_id, device_id, product_id, device_name, device_public_key, proof_key_id, proof_key_epoch, revoked, machine_id, capabilities";
811
+ async function rollback(client) {
812
+ try {
813
+ await client.query("ROLLBACK");
814
+ } catch {
815
+ }
816
+ }
810
817
  var PostgresDeviceDirectory = class {
811
818
  #pool;
812
819
  #clock;
@@ -815,32 +822,52 @@ var PostgresDeviceDirectory = class {
815
822
  this.#clock = clock;
816
823
  }
817
824
  async register(tenant, input) {
818
- const result = await this.#pool.query(
819
- `INSERT INTO device (
820
- tenant_id, device_id, product_id, device_name, device_public_key,
821
- proof_key_id, proof_key_epoch, revoked
822
- )
823
- VALUES ($1, $2, $3, $4, $5, $6, $7, false)
824
- ON CONFLICT (tenant_id, device_id) DO UPDATE
825
- SET product_id = EXCLUDED.product_id,
826
- device_name = EXCLUDED.device_name,
827
- device_public_key = EXCLUDED.device_public_key,
828
- proof_key_id = EXCLUDED.proof_key_id,
829
- proof_key_epoch = EXCLUDED.proof_key_epoch,
830
- revoked = false,
831
- capabilities = NULL
832
- RETURNING ${SELECT_COLUMNS}`,
833
- [
834
- tenant,
835
- input.deviceId,
836
- input.productId,
837
- input.deviceName,
838
- input.devicePublicKey,
839
- input.proofKeyId,
840
- input.proofKeyEpoch
841
- ]
842
- );
843
- return toRecord(result.rows[0]);
825
+ const client = await this.#pool.connect();
826
+ try {
827
+ await client.query("BEGIN");
828
+ if (input.machineId !== void 0) {
829
+ await client.query(
830
+ `UPDATE device
831
+ SET revoked = true
832
+ WHERE tenant_id = $1 AND product_id = $2 AND machine_id = $3 AND NOT revoked`,
833
+ [tenant, input.productId, input.machineId]
834
+ );
835
+ }
836
+ const result = await client.query(
837
+ `INSERT INTO device (
838
+ tenant_id, device_id, product_id, device_name, device_public_key,
839
+ proof_key_id, proof_key_epoch, revoked, machine_id
840
+ )
841
+ VALUES ($1, $2, $3, $4, $5, $6, $7, false, $8)
842
+ ON CONFLICT (tenant_id, device_id) DO UPDATE
843
+ SET product_id = EXCLUDED.product_id,
844
+ device_name = EXCLUDED.device_name,
845
+ device_public_key = EXCLUDED.device_public_key,
846
+ proof_key_id = EXCLUDED.proof_key_id,
847
+ proof_key_epoch = EXCLUDED.proof_key_epoch,
848
+ revoked = false,
849
+ machine_id = EXCLUDED.machine_id,
850
+ capabilities = NULL
851
+ RETURNING ${SELECT_COLUMNS}`,
852
+ [
853
+ tenant,
854
+ input.deviceId,
855
+ input.productId,
856
+ input.deviceName,
857
+ input.devicePublicKey,
858
+ input.proofKeyId,
859
+ input.proofKeyEpoch,
860
+ input.machineId ?? null
861
+ ]
862
+ );
863
+ await client.query("COMMIT");
864
+ return toRecord(result.rows[0]);
865
+ } catch (cause) {
866
+ await rollback(client);
867
+ throw cause;
868
+ } finally {
869
+ client.release();
870
+ }
844
871
  }
845
872
  async get(tenant, deviceId) {
846
873
  const result = await this.#pool.query(
@@ -2083,7 +2110,7 @@ var PostgresAgentMemoryProjectionStore = class {
2083
2110
  }
2084
2111
  });
2085
2112
  } catch (cause) {
2086
- await rollback(client);
2113
+ await rollback2(client);
2087
2114
  throw cause;
2088
2115
  } finally {
2089
2116
  client.release();
@@ -2120,7 +2147,7 @@ var PostgresAgentMemoryProjectionStore = class {
2120
2147
  await client.query("COMMIT");
2121
2148
  return AgentMemoryProjectionEraseResultSchema.parse({ nextWriterEpoch });
2122
2149
  } catch (cause) {
2123
- await rollback(client);
2150
+ await rollback2(client);
2124
2151
  throw cause;
2125
2152
  } finally {
2126
2153
  client.release();
@@ -2257,7 +2284,7 @@ function toBase64(bytes) {
2257
2284
  function toBase64Url(bytes) {
2258
2285
  return toBase64(bytes).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/u, "");
2259
2286
  }
2260
- async function rollback(client) {
2287
+ async function rollback2(client) {
2261
2288
  try {
2262
2289
  await client.query("ROLLBACK");
2263
2290
  } catch {