soundlink 2.11.0 → 2.13.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # soundlink
2
2
 
3
+ ## 2.13.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#50](https://github.com/Fan-ID/typescript-sdk/pull/50) [`2cefa2f`](https://github.com/Fan-ID/typescript-sdk/commit/2cefa2fa7f0b21557622dde3c19a4da852a19851) Thanks [@jotanarciso](https://github.com/jotanarciso)! - Add emulator pixel list, save, delete, and test-events. Keep `server_error` through the SDK default retries.
8
+
9
+ ## 2.12.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#48](https://github.com/Fan-ID/typescript-sdk/pull/48) [`a67f133`](https://github.com/Fan-ID/typescript-sdk/commit/a67f133502a50a8bfd7e1b79ce98c72f97021c0b) Thanks [@jotanarciso](https://github.com/jotanarciso)! - Fail the next emulator request on demand with timeout, 409, or 500.
14
+
3
15
  ## 2.11.0
4
16
 
5
17
  ### Minor Changes
@@ -14,7 +14,28 @@ function computeRefundGrossFromRawSpend(mediaSpendUsd, committedUsd, feeRate) {
14
14
  const grossEquivSpend = rate > 0 ? mediaSpendUsd / (1 - rate) : mediaSpendUsd;
15
15
  return Math.max(0, Number((committedUsd - grossEquivSpend).toFixed(2)));
16
16
  }
17
+
18
+ // src/constants/defaults.ts
19
+ var DEFAULT_MAX_RETRIES = 2;
20
+
21
+ // src/emulator/store.ts
17
22
  var ORG_EMULATOR_ID = "org_emulator";
23
+ var SERVER_ERROR_FAULT_RESPONSES = DEFAULT_MAX_RETRIES + 1;
24
+ function resolveFaultResponses(fault, maxRetries) {
25
+ if (fault !== "server_error") {
26
+ if (maxRetries !== void 0) {
27
+ return { ok: false, message: "maxRetries applies only to server_error." };
28
+ }
29
+ return { ok: true, responses: 1 };
30
+ }
31
+ if (maxRetries === void 0) {
32
+ return { ok: true, responses: SERVER_ERROR_FAULT_RESPONSES };
33
+ }
34
+ if (typeof maxRetries !== "number" || !Number.isInteger(maxRetries) || maxRetries < 0) {
35
+ return { ok: false, message: "maxRetries must be a non-negative integer." };
36
+ }
37
+ return { ok: true, responses: maxRetries + 1 };
38
+ }
18
39
  function nowIso() {
19
40
  return (/* @__PURE__ */ new Date()).toISOString();
20
41
  }
@@ -85,6 +106,9 @@ function createEmulatorStore(options) {
85
106
  let updatedAt = nowIso();
86
107
  const campaigns = /* @__PURE__ */ new Map();
87
108
  const idempotency = /* @__PURE__ */ new Map();
109
+ const pixels = /* @__PURE__ */ new Map();
110
+ let pendingFault = null;
111
+ let faultRemaining = 0;
88
112
  const store = {
89
113
  apiKey: options.apiKey,
90
114
  feeRate: options.feeRate,
@@ -95,6 +119,9 @@ function createEmulatorStore(options) {
95
119
  updatedAt = nowIso();
96
120
  campaigns.clear();
97
121
  idempotency.clear();
122
+ pixels.clear();
123
+ pendingFault = null;
124
+ faultRemaining = 0;
98
125
  },
99
126
  getWallet() {
100
127
  return {
@@ -176,6 +203,52 @@ function createEmulatorStore(options) {
176
203
  deleteIdempotency(key) {
177
204
  idempotency.delete(key);
178
205
  },
206
+ armFault(fault, responses) {
207
+ if (pendingFault) {
208
+ return false;
209
+ }
210
+ pendingFault = fault;
211
+ faultRemaining = responses ?? (fault === "server_error" ? SERVER_ERROR_FAULT_RESPONSES : 1);
212
+ return true;
213
+ },
214
+ takeFault() {
215
+ if (!pendingFault || faultRemaining <= 0) {
216
+ return null;
217
+ }
218
+ const fault = pendingFault;
219
+ faultRemaining -= 1;
220
+ if (faultRemaining === 0) {
221
+ pendingFault = null;
222
+ }
223
+ return fault;
224
+ },
225
+ listPixels() {
226
+ return [...pixels.values()];
227
+ },
228
+ getPixel(pixelId) {
229
+ return pixels.get(pixelId);
230
+ },
231
+ findPixelByAccessToken(accessToken) {
232
+ for (const pixel of pixels.values()) {
233
+ if (pixel.accessToken === accessToken) {
234
+ return pixel;
235
+ }
236
+ }
237
+ return void 0;
238
+ },
239
+ savePixel(pixel) {
240
+ if (pixel.isDefault) {
241
+ for (const existing of pixels.values()) {
242
+ if (existing.id !== pixel.id) {
243
+ existing.isDefault = false;
244
+ }
245
+ }
246
+ }
247
+ pixels.set(pixel.id, pixel);
248
+ },
249
+ deletePixel(pixelId) {
250
+ pixels.delete(pixelId);
251
+ },
179
252
  toCampaignDetail(campaign) {
180
253
  return {
181
254
  campaignId: campaign.campaignId,
@@ -543,6 +616,20 @@ function dispatch(store, request) {
543
616
  if (authError) {
544
617
  return authError;
545
618
  }
619
+ const fault = store.takeFault();
620
+ if (fault === "timeout") {
621
+ return { status: 408, json: null, hang: true };
622
+ }
623
+ if (fault === "conflict") {
624
+ return failure(
625
+ 409,
626
+ "idempotency_key_conflict",
627
+ "This Idempotency-Key was already used with a different request body."
628
+ );
629
+ }
630
+ if (fault === "server_error") {
631
+ return failure(500, "internal_error", "Emulator fault.");
632
+ }
546
633
  if (method === "GET" && pathname === "/v1/ping") {
547
634
  return success(200, { status: "ok" });
548
635
  }
@@ -560,6 +647,10 @@ function dispatch(store, request) {
560
647
  ]
561
648
  });
562
649
  }
650
+ const pixelResponse = dispatchPixel(store, method, pathname, request.body);
651
+ if (pixelResponse) {
652
+ return pixelResponse;
653
+ }
563
654
  if (method === "GET" && pathname === "/v1/campaigns") {
564
655
  const items = store.listCampaigns().map((campaign) => {
565
656
  const detail = store.toCampaignDetail(campaign);
@@ -718,7 +809,167 @@ function handleStopCampaign(store, campaignId, request) {
718
809
  });
719
810
  return success(200, responseBody);
720
811
  }
812
+ function toPublicPixel(pixel) {
813
+ return {
814
+ id: pixel.id,
815
+ platform: "meta",
816
+ isDefault: pixel.isDefault,
817
+ status: "ok",
818
+ lastErrorCode: null,
819
+ errorSince: null
820
+ };
821
+ }
822
+ function decodePixelId(raw) {
823
+ try {
824
+ return decodeURIComponent(raw).trim();
825
+ } catch {
826
+ return raw.trim();
827
+ }
828
+ }
829
+ function parsePixelBody(body) {
830
+ if (body === null || typeof body !== "object" || Array.isArray(body)) {
831
+ return { ok: false, message: "Request body must be a JSON object." };
832
+ }
833
+ const record = body;
834
+ const extra = Object.keys(record).filter(
835
+ (key) => key !== "platform" && key !== "accessToken" && key !== "isDefault"
836
+ );
837
+ const unknownKey = extra[0];
838
+ if (unknownKey) {
839
+ return {
840
+ ok: false,
841
+ message: `Invalid request body at body: Unrecognized key(s) in object: '${unknownKey}'.`
842
+ };
843
+ }
844
+ if (!("platform" in record)) {
845
+ return { ok: false, message: "Invalid request body at platform: Required." };
846
+ }
847
+ if (record.platform !== "meta") {
848
+ return {
849
+ ok: false,
850
+ message: 'Invalid request body at platform: Invalid literal value, expected "meta".'
851
+ };
852
+ }
853
+ let accessToken;
854
+ if ("accessToken" in record) {
855
+ if (typeof record.accessToken !== "string") {
856
+ return {
857
+ ok: false,
858
+ message: `Invalid request body at accessToken: Expected string, received ${typeof record.accessToken}.`
859
+ };
860
+ }
861
+ accessToken = record.accessToken.trim();
862
+ if (!accessToken) {
863
+ return {
864
+ ok: false,
865
+ message: "Invalid request body at accessToken: String must contain at least 1 character(s)."
866
+ };
867
+ }
868
+ }
869
+ let isDefault;
870
+ if ("isDefault" in record) {
871
+ if (typeof record.isDefault !== "boolean") {
872
+ return {
873
+ ok: false,
874
+ message: `Invalid request body at isDefault: Expected boolean, received ${typeof record.isDefault}.`
875
+ };
876
+ }
877
+ isDefault = record.isDefault;
878
+ }
879
+ if (accessToken === void 0 && isDefault === void 0) {
880
+ return {
881
+ ok: false,
882
+ message: "Invalid request body at accessToken: accessToken or isDefault is required."
883
+ };
884
+ }
885
+ return { ok: true, accessToken, isDefault };
886
+ }
887
+ function dispatchPixel(store, method, pathname, body) {
888
+ if (method === "GET" && pathname === "/v1/pixels") {
889
+ return success(200, {
890
+ pixels: store.listPixels().map(toPublicPixel)
891
+ });
892
+ }
893
+ const testPixelId = matchPathGroup(pathname, /^\/v1\/pixels\/([^/]+)\/test-events$/);
894
+ if (testPixelId !== void 0) {
895
+ if (method !== "POST") {
896
+ return failure(404, "not_found", "Not found.");
897
+ }
898
+ const pixelId2 = decodePixelId(testPixelId);
899
+ const pixel = pixelId2 ? store.getPixel(pixelId2) : void 0;
900
+ if (!pixel) {
901
+ return failure(404, "not_found", "Pixel not found.");
902
+ }
903
+ return success(200, { status: "sent", pixelId: pixel.id });
904
+ }
905
+ const rawPixelId = matchPathGroup(pathname, /^\/v1\/pixels\/([^/]+)$/);
906
+ if (rawPixelId === void 0) {
907
+ return null;
908
+ }
909
+ const pixelId = decodePixelId(rawPixelId);
910
+ if (!pixelId) {
911
+ return failure(400, "invalid_request", "pixelId is required.");
912
+ }
913
+ if (method === "PUT") {
914
+ const parsed = parsePixelBody(body);
915
+ if (!parsed.ok) {
916
+ return failure(400, "invalid_request", parsed.message);
917
+ }
918
+ const existing = store.getPixel(pixelId);
919
+ if (!existing && parsed.accessToken === void 0) {
920
+ return failure(404, "not_found", "Pixel not found.");
921
+ }
922
+ if (parsed.accessToken !== void 0) {
923
+ const owner = store.findPixelByAccessToken(parsed.accessToken);
924
+ if (owner && owner.id !== pixelId) {
925
+ return failure(
926
+ 400,
927
+ "invalid_request",
928
+ "Access token is already in use with another pixel ID"
929
+ );
930
+ }
931
+ }
932
+ const saved = {
933
+ id: pixelId,
934
+ accessToken: parsed.accessToken ?? existing?.accessToken ?? "",
935
+ isDefault: parsed.isDefault ?? existing?.isDefault ?? false
936
+ };
937
+ store.savePixel(saved);
938
+ return success(200, toPublicPixel(saved));
939
+ }
940
+ if (method === "DELETE") {
941
+ if (!store.getPixel(pixelId)) {
942
+ return failure(404, "not_found", "Pixel not found.");
943
+ }
944
+ store.deletePixel(pixelId);
945
+ return { status: 204, json: null };
946
+ }
947
+ return failure(404, "not_found", "Not found.");
948
+ }
949
+ function isFault(value) {
950
+ return value === "timeout" || value === "conflict" || value === "server_error";
951
+ }
721
952
  function dispatchHarness(store, method, pathname, body) {
953
+ if (method === "POST" && pathname === "/__emulator/fault") {
954
+ const record = body && typeof body === "object" ? body : void 0;
955
+ const fault = record?.fault;
956
+ if (!isFault(fault)) {
957
+ return failure(
958
+ 400,
959
+ "invalid_request",
960
+ "fault must be timeout | conflict | server_error."
961
+ );
962
+ }
963
+ const maxRetries = record?.maxRetries;
964
+ const resolved = resolveFaultResponses(fault, maxRetries);
965
+ if (!resolved.ok) {
966
+ return failure(400, "invalid_request", resolved.message);
967
+ }
968
+ if (!store.armFault(fault, resolved.responses)) {
969
+ return failure(400, "invalid_request", "A fault is already armed.");
970
+ }
971
+ return success(200, { fault });
972
+ }
722
973
  if (method === "POST" && pathname === "/__emulator/reset") {
723
974
  store.reset();
724
975
  return success(200, { ok: true });
@@ -843,6 +1094,19 @@ function listen(store, options = {}) {
843
1094
  return;
844
1095
  }
845
1096
  const response = dispatch(store, { method, path, headers, body });
1097
+ if (response.status === 204) {
1098
+ res.writeHead(204);
1099
+ res.end();
1100
+ return;
1101
+ }
1102
+ if (response.hang) {
1103
+ req.on("close", () => {
1104
+ if (!res.writableEnded) {
1105
+ res.end();
1106
+ }
1107
+ });
1108
+ return;
1109
+ }
846
1110
  res.writeHead(response.status, {
847
1111
  "Content-Type": "application/json"
848
1112
  });
@@ -915,6 +1179,15 @@ function createApiEmulator(options = {}) {
915
1179
  setWallet(balance) {
916
1180
  return store.setWalletBalance(balance);
917
1181
  },
1182
+ failNext(fault, maxRetries) {
1183
+ const resolved2 = resolveFaultResponses(fault, maxRetries);
1184
+ if (!resolved2.ok) {
1185
+ throw new Error(resolved2.message);
1186
+ }
1187
+ if (!store.armFault(fault, resolved2.responses)) {
1188
+ throw new Error("A fault is already armed.");
1189
+ }
1190
+ },
918
1191
  transition(campaignId, action, mediaSpendUsd) {
919
1192
  return applyTransition(store, campaignId, action, mediaSpendUsd);
920
1193
  },