@byok-sdk/cloud-dataplane 0.10.1 → 0.11.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
@@ -808,6 +808,18 @@ function toRecord(row) {
808
808
  };
809
809
  }
810
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
+ var DEVICE_STATE_DELETES = [
812
+ "DELETE FROM device_presence WHERE tenant_id = $1 AND device_id = ANY($2::text[])",
813
+ "DELETE FROM auth_nonce WHERE tenant_id = $1 AND device_id = ANY($2::text[])",
814
+ "DELETE FROM inbound_dedup WHERE tenant_id = $1 AND device_id = ANY($2::text[])",
815
+ "DELETE FROM device_assertion_replay WHERE tenant_id = $1 AND device_id = ANY($2::text[])"
816
+ ];
817
+ async function deleteDeviceState(client, tenant, deviceIds) {
818
+ if (deviceIds.length === 0) return;
819
+ for (const statement of DEVICE_STATE_DELETES) {
820
+ await client.query(statement, [tenant, [...deviceIds]]);
821
+ }
822
+ }
811
823
  async function rollback(client) {
812
824
  try {
813
825
  await client.query("ROLLBACK");
@@ -826,11 +838,16 @@ var PostgresDeviceDirectory = class {
826
838
  try {
827
839
  await client.query("BEGIN");
828
840
  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]
841
+ const superseded = await client.query(
842
+ `DELETE FROM device
843
+ WHERE tenant_id = $1 AND product_id = $2 AND machine_id = $3 AND device_id <> $4
844
+ RETURNING device_id`,
845
+ [tenant, input.productId, input.machineId, input.deviceId]
846
+ );
847
+ await deleteDeviceState(
848
+ client,
849
+ tenant,
850
+ superseded.rows.map((row) => row.device_id)
834
851
  );
835
852
  }
836
853
  const result = await client.query(
@@ -877,11 +894,35 @@ var PostgresDeviceDirectory = class {
877
894
  const row = result.rows[0];
878
895
  return row === void 0 ? void 0 : toRecord(row);
879
896
  }
897
+ /**
898
+ * Revocation removes the registration and the device-scoped state that only
899
+ * existed to serve it, in one transaction: a half-applied revoke that left
900
+ * live presence or an unspent challenge nonce behind would be state for a
901
+ * device the directory can no longer name.
902
+ *
903
+ * A no-op for a device this tenant does not own: revoking what you cannot
904
+ * address deletes nothing and reports nothing back.
905
+ */
880
906
  async revoke(tenant, deviceId) {
881
- await this.#pool.query("UPDATE device SET revoked = true WHERE tenant_id = $1 AND device_id = $2", [
882
- tenant,
883
- deviceId
884
- ]);
907
+ const client = await this.#pool.connect();
908
+ try {
909
+ await client.query("BEGIN");
910
+ const removed = await client.query(
911
+ "DELETE FROM device WHERE tenant_id = $1 AND device_id = $2 RETURNING device_id",
912
+ [tenant, deviceId]
913
+ );
914
+ await deleteDeviceState(
915
+ client,
916
+ tenant,
917
+ removed.rows.map((row) => row.device_id)
918
+ );
919
+ await client.query("COMMIT");
920
+ } catch (cause) {
921
+ await rollback(client);
922
+ throw cause;
923
+ } finally {
924
+ client.release();
925
+ }
885
926
  }
886
927
  async recordCapabilities(tenant, input) {
887
928
  const result = await this.#pool.query(