soundlink 2.12.0 → 2.14.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 +12 -0
- package/README.md +5 -1
- package/dist/cjs/api-emulator.cjs +230 -7
- package/dist/cjs/api-emulator.cjs.map +1 -1
- package/dist/cjs/index.cjs +8 -6
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/api-emulator.d.ts +5 -5
- package/dist/esm/api-emulator.js +226 -9
- package/dist/esm/api-emulator.js.map +1 -1
- package/dist/esm/{api-C7t2QZ2d.d.ts → api-iOibHCWR.d.ts} +23 -2
- package/dist/esm/chunk-RQQ64IWI.js +14 -0
- package/dist/esm/chunk-RQQ64IWI.js.map +1 -0
- package/dist/esm/index.d.ts +9 -9
- package/dist/esm/index.js +11 -17
- package/dist/esm/index.js.map +1 -1
- package/docs/api-emulator.md +4 -3
- package/openapi/soundlink-public-api-v1.yaml +601 -29
- package/package.json +1 -1
package/dist/esm/api-emulator.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { DEFAULT_MAX_RETRIES } from './chunk-RQQ64IWI.js';
|
|
1
2
|
import { randomUUID, createHash } from 'crypto';
|
|
2
3
|
import { createServer } from 'http';
|
|
3
4
|
|
|
4
|
-
// src/emulator/dispatch.ts
|
|
5
|
-
|
|
6
5
|
// src/emulator/money.ts
|
|
7
6
|
function computeCommittedUsd(dailyBudget, durationDays) {
|
|
8
7
|
return Math.round(dailyBudget * durationDays * 100) / 100;
|
|
@@ -13,6 +12,22 @@ function computeRefundGrossFromRawSpend(mediaSpendUsd, committedUsd, feeRate) {
|
|
|
13
12
|
return Math.max(0, Number((committedUsd - grossEquivSpend).toFixed(2)));
|
|
14
13
|
}
|
|
15
14
|
var ORG_EMULATOR_ID = "org_emulator";
|
|
15
|
+
var SERVER_ERROR_FAULT_RESPONSES = DEFAULT_MAX_RETRIES + 1;
|
|
16
|
+
function resolveFaultResponses(fault, maxRetries) {
|
|
17
|
+
if (fault !== "server_error") {
|
|
18
|
+
if (maxRetries !== void 0) {
|
|
19
|
+
return { ok: false, message: "maxRetries applies only to server_error." };
|
|
20
|
+
}
|
|
21
|
+
return { ok: true, responses: 1 };
|
|
22
|
+
}
|
|
23
|
+
if (maxRetries === void 0) {
|
|
24
|
+
return { ok: true, responses: SERVER_ERROR_FAULT_RESPONSES };
|
|
25
|
+
}
|
|
26
|
+
if (typeof maxRetries !== "number" || !Number.isInteger(maxRetries) || maxRetries < 0) {
|
|
27
|
+
return { ok: false, message: "maxRetries must be a non-negative integer." };
|
|
28
|
+
}
|
|
29
|
+
return { ok: true, responses: maxRetries + 1 };
|
|
30
|
+
}
|
|
16
31
|
function nowIso() {
|
|
17
32
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
18
33
|
}
|
|
@@ -83,7 +98,9 @@ function createEmulatorStore(options) {
|
|
|
83
98
|
let updatedAt = nowIso();
|
|
84
99
|
const campaigns = /* @__PURE__ */ new Map();
|
|
85
100
|
const idempotency = /* @__PURE__ */ new Map();
|
|
101
|
+
const pixels = /* @__PURE__ */ new Map();
|
|
86
102
|
let pendingFault = null;
|
|
103
|
+
let faultRemaining = 0;
|
|
87
104
|
const store = {
|
|
88
105
|
apiKey: options.apiKey,
|
|
89
106
|
feeRate: options.feeRate,
|
|
@@ -94,7 +111,9 @@ function createEmulatorStore(options) {
|
|
|
94
111
|
updatedAt = nowIso();
|
|
95
112
|
campaigns.clear();
|
|
96
113
|
idempotency.clear();
|
|
114
|
+
pixels.clear();
|
|
97
115
|
pendingFault = null;
|
|
116
|
+
faultRemaining = 0;
|
|
98
117
|
},
|
|
99
118
|
getWallet() {
|
|
100
119
|
return {
|
|
@@ -176,14 +195,52 @@ function createEmulatorStore(options) {
|
|
|
176
195
|
deleteIdempotency(key) {
|
|
177
196
|
idempotency.delete(key);
|
|
178
197
|
},
|
|
179
|
-
armFault(fault) {
|
|
198
|
+
armFault(fault, responses) {
|
|
199
|
+
if (pendingFault) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
180
202
|
pendingFault = fault;
|
|
203
|
+
faultRemaining = responses ?? (fault === "server_error" ? SERVER_ERROR_FAULT_RESPONSES : 1);
|
|
204
|
+
return true;
|
|
181
205
|
},
|
|
182
206
|
takeFault() {
|
|
207
|
+
if (!pendingFault || faultRemaining <= 0) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
183
210
|
const fault = pendingFault;
|
|
184
|
-
|
|
211
|
+
faultRemaining -= 1;
|
|
212
|
+
if (faultRemaining === 0) {
|
|
213
|
+
pendingFault = null;
|
|
214
|
+
}
|
|
185
215
|
return fault;
|
|
186
216
|
},
|
|
217
|
+
listPixels() {
|
|
218
|
+
return [...pixels.values()];
|
|
219
|
+
},
|
|
220
|
+
getPixel(pixelId) {
|
|
221
|
+
return pixels.get(pixelId);
|
|
222
|
+
},
|
|
223
|
+
findPixelByAccessToken(accessToken) {
|
|
224
|
+
for (const pixel of pixels.values()) {
|
|
225
|
+
if (pixel.accessToken === accessToken) {
|
|
226
|
+
return pixel;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return void 0;
|
|
230
|
+
},
|
|
231
|
+
savePixel(pixel) {
|
|
232
|
+
if (pixel.isDefault) {
|
|
233
|
+
for (const existing of pixels.values()) {
|
|
234
|
+
if (existing.id !== pixel.id) {
|
|
235
|
+
existing.isDefault = false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
pixels.set(pixel.id, pixel);
|
|
240
|
+
},
|
|
241
|
+
deletePixel(pixelId) {
|
|
242
|
+
pixels.delete(pixelId);
|
|
243
|
+
},
|
|
187
244
|
toCampaignDetail(campaign) {
|
|
188
245
|
return {
|
|
189
246
|
campaignId: campaign.campaignId,
|
|
@@ -553,7 +610,7 @@ function dispatch(store, request) {
|
|
|
553
610
|
}
|
|
554
611
|
const fault = store.takeFault();
|
|
555
612
|
if (fault === "timeout") {
|
|
556
|
-
return { status:
|
|
613
|
+
return { status: 408, json: null, hang: true };
|
|
557
614
|
}
|
|
558
615
|
if (fault === "conflict") {
|
|
559
616
|
return failure(
|
|
@@ -582,6 +639,10 @@ function dispatch(store, request) {
|
|
|
582
639
|
]
|
|
583
640
|
});
|
|
584
641
|
}
|
|
642
|
+
const pixelResponse = dispatchPixel(store, method, pathname, request.body);
|
|
643
|
+
if (pixelResponse) {
|
|
644
|
+
return pixelResponse;
|
|
645
|
+
}
|
|
585
646
|
if (method === "GET" && pathname === "/v1/campaigns") {
|
|
586
647
|
const items = store.listCampaigns().map((campaign) => {
|
|
587
648
|
const detail = store.toCampaignDetail(campaign);
|
|
@@ -740,12 +801,150 @@ function handleStopCampaign(store, campaignId, request) {
|
|
|
740
801
|
});
|
|
741
802
|
return success(200, responseBody);
|
|
742
803
|
}
|
|
804
|
+
function toPublicPixel(pixel) {
|
|
805
|
+
return {
|
|
806
|
+
id: pixel.id,
|
|
807
|
+
platform: "meta",
|
|
808
|
+
isDefault: pixel.isDefault,
|
|
809
|
+
status: "ok",
|
|
810
|
+
lastErrorCode: null,
|
|
811
|
+
errorSince: null
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
function decodePixelId(raw) {
|
|
815
|
+
try {
|
|
816
|
+
return decodeURIComponent(raw).trim();
|
|
817
|
+
} catch {
|
|
818
|
+
return raw.trim();
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
function parsePixelBody(body) {
|
|
822
|
+
if (body === null || typeof body !== "object" || Array.isArray(body)) {
|
|
823
|
+
return { ok: false, message: "Request body must be a JSON object." };
|
|
824
|
+
}
|
|
825
|
+
const record = body;
|
|
826
|
+
const extra = Object.keys(record).filter(
|
|
827
|
+
(key) => key !== "platform" && key !== "accessToken" && key !== "isDefault"
|
|
828
|
+
);
|
|
829
|
+
const unknownKey = extra[0];
|
|
830
|
+
if (unknownKey) {
|
|
831
|
+
return {
|
|
832
|
+
ok: false,
|
|
833
|
+
message: `Invalid request body at body: Unrecognized key(s) in object: '${unknownKey}'.`
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
if (!("platform" in record)) {
|
|
837
|
+
return { ok: false, message: "Invalid request body at platform: Required." };
|
|
838
|
+
}
|
|
839
|
+
if (record.platform !== "meta") {
|
|
840
|
+
return {
|
|
841
|
+
ok: false,
|
|
842
|
+
message: 'Invalid request body at platform: Invalid literal value, expected "meta".'
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
let accessToken;
|
|
846
|
+
if ("accessToken" in record) {
|
|
847
|
+
if (typeof record.accessToken !== "string") {
|
|
848
|
+
return {
|
|
849
|
+
ok: false,
|
|
850
|
+
message: `Invalid request body at accessToken: Expected string, received ${typeof record.accessToken}.`
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
accessToken = record.accessToken.trim();
|
|
854
|
+
if (!accessToken) {
|
|
855
|
+
return {
|
|
856
|
+
ok: false,
|
|
857
|
+
message: "Invalid request body at accessToken: String must contain at least 1 character(s)."
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
let isDefault;
|
|
862
|
+
if ("isDefault" in record) {
|
|
863
|
+
if (typeof record.isDefault !== "boolean") {
|
|
864
|
+
return {
|
|
865
|
+
ok: false,
|
|
866
|
+
message: `Invalid request body at isDefault: Expected boolean, received ${typeof record.isDefault}.`
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
isDefault = record.isDefault;
|
|
870
|
+
}
|
|
871
|
+
if (accessToken === void 0 && isDefault === void 0) {
|
|
872
|
+
return {
|
|
873
|
+
ok: false,
|
|
874
|
+
message: "Invalid request body at accessToken: accessToken or isDefault is required."
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
return { ok: true, accessToken, isDefault };
|
|
878
|
+
}
|
|
879
|
+
function dispatchPixel(store, method, pathname, body) {
|
|
880
|
+
if (method === "GET" && pathname === "/v1/pixels") {
|
|
881
|
+
return success(200, {
|
|
882
|
+
pixels: store.listPixels().map(toPublicPixel)
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
const testPixelId = matchPathGroup(pathname, /^\/v1\/pixels\/([^/]+)\/test-events$/);
|
|
886
|
+
if (testPixelId !== void 0) {
|
|
887
|
+
if (method !== "POST") {
|
|
888
|
+
return failure(404, "not_found", "Not found.");
|
|
889
|
+
}
|
|
890
|
+
const pixelId2 = decodePixelId(testPixelId);
|
|
891
|
+
const pixel = pixelId2 ? store.getPixel(pixelId2) : void 0;
|
|
892
|
+
if (!pixel) {
|
|
893
|
+
return failure(404, "not_found", "Pixel not found.");
|
|
894
|
+
}
|
|
895
|
+
return success(200, { status: "sent", pixelId: pixel.id });
|
|
896
|
+
}
|
|
897
|
+
const rawPixelId = matchPathGroup(pathname, /^\/v1\/pixels\/([^/]+)$/);
|
|
898
|
+
if (rawPixelId === void 0) {
|
|
899
|
+
return null;
|
|
900
|
+
}
|
|
901
|
+
const pixelId = decodePixelId(rawPixelId);
|
|
902
|
+
if (!pixelId) {
|
|
903
|
+
return failure(400, "invalid_request", "pixelId is required.");
|
|
904
|
+
}
|
|
905
|
+
if (method === "PUT") {
|
|
906
|
+
const parsed = parsePixelBody(body);
|
|
907
|
+
if (!parsed.ok) {
|
|
908
|
+
return failure(400, "invalid_request", parsed.message);
|
|
909
|
+
}
|
|
910
|
+
const existing = store.getPixel(pixelId);
|
|
911
|
+
if (!existing && parsed.accessToken === void 0) {
|
|
912
|
+
return failure(404, "not_found", "Pixel not found.");
|
|
913
|
+
}
|
|
914
|
+
if (parsed.accessToken !== void 0) {
|
|
915
|
+
const owner = store.findPixelByAccessToken(parsed.accessToken);
|
|
916
|
+
if (owner && owner.id !== pixelId) {
|
|
917
|
+
return failure(
|
|
918
|
+
400,
|
|
919
|
+
"invalid_request",
|
|
920
|
+
"Access token is already in use with another pixel ID"
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
const saved = {
|
|
925
|
+
id: pixelId,
|
|
926
|
+
accessToken: parsed.accessToken ?? existing?.accessToken ?? "",
|
|
927
|
+
isDefault: parsed.isDefault ?? existing?.isDefault ?? false
|
|
928
|
+
};
|
|
929
|
+
store.savePixel(saved);
|
|
930
|
+
return success(200, toPublicPixel(saved));
|
|
931
|
+
}
|
|
932
|
+
if (method === "DELETE") {
|
|
933
|
+
if (!store.getPixel(pixelId)) {
|
|
934
|
+
return failure(404, "not_found", "Pixel not found.");
|
|
935
|
+
}
|
|
936
|
+
store.deletePixel(pixelId);
|
|
937
|
+
return { status: 204, json: null };
|
|
938
|
+
}
|
|
939
|
+
return failure(404, "not_found", "Not found.");
|
|
940
|
+
}
|
|
743
941
|
function isFault(value) {
|
|
744
942
|
return value === "timeout" || value === "conflict" || value === "server_error";
|
|
745
943
|
}
|
|
746
944
|
function dispatchHarness(store, method, pathname, body) {
|
|
747
945
|
if (method === "POST" && pathname === "/__emulator/fault") {
|
|
748
|
-
const
|
|
946
|
+
const record = body && typeof body === "object" ? body : void 0;
|
|
947
|
+
const fault = record?.fault;
|
|
749
948
|
if (!isFault(fault)) {
|
|
750
949
|
return failure(
|
|
751
950
|
400,
|
|
@@ -753,7 +952,14 @@ function dispatchHarness(store, method, pathname, body) {
|
|
|
753
952
|
"fault must be timeout | conflict | server_error."
|
|
754
953
|
);
|
|
755
954
|
}
|
|
756
|
-
|
|
955
|
+
const maxRetries = record?.maxRetries;
|
|
956
|
+
const resolved = resolveFaultResponses(fault, maxRetries);
|
|
957
|
+
if (!resolved.ok) {
|
|
958
|
+
return failure(400, "invalid_request", resolved.message);
|
|
959
|
+
}
|
|
960
|
+
if (!store.armFault(fault, resolved.responses)) {
|
|
961
|
+
return failure(400, "invalid_request", "A fault is already armed.");
|
|
962
|
+
}
|
|
757
963
|
return success(200, { fault });
|
|
758
964
|
}
|
|
759
965
|
if (method === "POST" && pathname === "/__emulator/reset") {
|
|
@@ -880,6 +1086,11 @@ function listen(store, options = {}) {
|
|
|
880
1086
|
return;
|
|
881
1087
|
}
|
|
882
1088
|
const response = dispatch(store, { method, path, headers, body });
|
|
1089
|
+
if (response.status === 204) {
|
|
1090
|
+
res.writeHead(204);
|
|
1091
|
+
res.end();
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
883
1094
|
if (response.hang) {
|
|
884
1095
|
req.on("close", () => {
|
|
885
1096
|
if (!res.writableEnded) {
|
|
@@ -960,8 +1171,14 @@ function createApiEmulator(options = {}) {
|
|
|
960
1171
|
setWallet(balance) {
|
|
961
1172
|
return store.setWalletBalance(balance);
|
|
962
1173
|
},
|
|
963
|
-
failNext(fault) {
|
|
964
|
-
|
|
1174
|
+
failNext(fault, maxRetries) {
|
|
1175
|
+
const resolved2 = resolveFaultResponses(fault, maxRetries);
|
|
1176
|
+
if (!resolved2.ok) {
|
|
1177
|
+
throw new Error(resolved2.message);
|
|
1178
|
+
}
|
|
1179
|
+
if (!store.armFault(fault, resolved2.responses)) {
|
|
1180
|
+
throw new Error("A fault is already armed.");
|
|
1181
|
+
}
|
|
965
1182
|
},
|
|
966
1183
|
transition(campaignId, action, mediaSpendUsd) {
|
|
967
1184
|
return applyTransition(store, campaignId, action, mediaSpendUsd);
|