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 +12 -0
- package/dist/cjs/api-emulator.cjs +273 -0
- package/dist/cjs/api-emulator.cjs.map +1 -1
- package/dist/esm/api-emulator.d.ts +10 -1
- package/dist/esm/api-emulator.js +269 -2
- package/dist/esm/api-emulator.js.map +1 -1
- package/dist/esm/chunk-RQQ64IWI.js +14 -0
- package/dist/esm/chunk-RQQ64IWI.js.map +1 -0
- package/dist/esm/index.js +3 -11
- package/dist/esm/index.js.map +1 -1
- package/docs/api-emulator.md +6 -2
- 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,6 +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();
|
|
102
|
+
let pendingFault = null;
|
|
103
|
+
let faultRemaining = 0;
|
|
86
104
|
const store = {
|
|
87
105
|
apiKey: options.apiKey,
|
|
88
106
|
feeRate: options.feeRate,
|
|
@@ -93,6 +111,9 @@ function createEmulatorStore(options) {
|
|
|
93
111
|
updatedAt = nowIso();
|
|
94
112
|
campaigns.clear();
|
|
95
113
|
idempotency.clear();
|
|
114
|
+
pixels.clear();
|
|
115
|
+
pendingFault = null;
|
|
116
|
+
faultRemaining = 0;
|
|
96
117
|
},
|
|
97
118
|
getWallet() {
|
|
98
119
|
return {
|
|
@@ -174,6 +195,52 @@ function createEmulatorStore(options) {
|
|
|
174
195
|
deleteIdempotency(key) {
|
|
175
196
|
idempotency.delete(key);
|
|
176
197
|
},
|
|
198
|
+
armFault(fault, responses) {
|
|
199
|
+
if (pendingFault) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
pendingFault = fault;
|
|
203
|
+
faultRemaining = responses ?? (fault === "server_error" ? SERVER_ERROR_FAULT_RESPONSES : 1);
|
|
204
|
+
return true;
|
|
205
|
+
},
|
|
206
|
+
takeFault() {
|
|
207
|
+
if (!pendingFault || faultRemaining <= 0) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
const fault = pendingFault;
|
|
211
|
+
faultRemaining -= 1;
|
|
212
|
+
if (faultRemaining === 0) {
|
|
213
|
+
pendingFault = null;
|
|
214
|
+
}
|
|
215
|
+
return fault;
|
|
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
|
+
},
|
|
177
244
|
toCampaignDetail(campaign) {
|
|
178
245
|
return {
|
|
179
246
|
campaignId: campaign.campaignId,
|
|
@@ -541,6 +608,20 @@ function dispatch(store, request) {
|
|
|
541
608
|
if (authError) {
|
|
542
609
|
return authError;
|
|
543
610
|
}
|
|
611
|
+
const fault = store.takeFault();
|
|
612
|
+
if (fault === "timeout") {
|
|
613
|
+
return { status: 408, json: null, hang: true };
|
|
614
|
+
}
|
|
615
|
+
if (fault === "conflict") {
|
|
616
|
+
return failure(
|
|
617
|
+
409,
|
|
618
|
+
"idempotency_key_conflict",
|
|
619
|
+
"This Idempotency-Key was already used with a different request body."
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
if (fault === "server_error") {
|
|
623
|
+
return failure(500, "internal_error", "Emulator fault.");
|
|
624
|
+
}
|
|
544
625
|
if (method === "GET" && pathname === "/v1/ping") {
|
|
545
626
|
return success(200, { status: "ok" });
|
|
546
627
|
}
|
|
@@ -558,6 +639,10 @@ function dispatch(store, request) {
|
|
|
558
639
|
]
|
|
559
640
|
});
|
|
560
641
|
}
|
|
642
|
+
const pixelResponse = dispatchPixel(store, method, pathname, request.body);
|
|
643
|
+
if (pixelResponse) {
|
|
644
|
+
return pixelResponse;
|
|
645
|
+
}
|
|
561
646
|
if (method === "GET" && pathname === "/v1/campaigns") {
|
|
562
647
|
const items = store.listCampaigns().map((campaign) => {
|
|
563
648
|
const detail = store.toCampaignDetail(campaign);
|
|
@@ -716,7 +801,167 @@ function handleStopCampaign(store, campaignId, request) {
|
|
|
716
801
|
});
|
|
717
802
|
return success(200, responseBody);
|
|
718
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
|
+
}
|
|
941
|
+
function isFault(value) {
|
|
942
|
+
return value === "timeout" || value === "conflict" || value === "server_error";
|
|
943
|
+
}
|
|
719
944
|
function dispatchHarness(store, method, pathname, body) {
|
|
945
|
+
if (method === "POST" && pathname === "/__emulator/fault") {
|
|
946
|
+
const record = body && typeof body === "object" ? body : void 0;
|
|
947
|
+
const fault = record?.fault;
|
|
948
|
+
if (!isFault(fault)) {
|
|
949
|
+
return failure(
|
|
950
|
+
400,
|
|
951
|
+
"invalid_request",
|
|
952
|
+
"fault must be timeout | conflict | server_error."
|
|
953
|
+
);
|
|
954
|
+
}
|
|
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
|
+
}
|
|
963
|
+
return success(200, { fault });
|
|
964
|
+
}
|
|
720
965
|
if (method === "POST" && pathname === "/__emulator/reset") {
|
|
721
966
|
store.reset();
|
|
722
967
|
return success(200, { ok: true });
|
|
@@ -841,6 +1086,19 @@ function listen(store, options = {}) {
|
|
|
841
1086
|
return;
|
|
842
1087
|
}
|
|
843
1088
|
const response = dispatch(store, { method, path, headers, body });
|
|
1089
|
+
if (response.status === 204) {
|
|
1090
|
+
res.writeHead(204);
|
|
1091
|
+
res.end();
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
if (response.hang) {
|
|
1095
|
+
req.on("close", () => {
|
|
1096
|
+
if (!res.writableEnded) {
|
|
1097
|
+
res.end();
|
|
1098
|
+
}
|
|
1099
|
+
});
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
844
1102
|
res.writeHead(response.status, {
|
|
845
1103
|
"Content-Type": "application/json"
|
|
846
1104
|
});
|
|
@@ -913,6 +1171,15 @@ function createApiEmulator(options = {}) {
|
|
|
913
1171
|
setWallet(balance) {
|
|
914
1172
|
return store.setWalletBalance(balance);
|
|
915
1173
|
},
|
|
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
|
+
}
|
|
1182
|
+
},
|
|
916
1183
|
transition(campaignId, action, mediaSpendUsd) {
|
|
917
1184
|
return applyTransition(store, campaignId, action, mediaSpendUsd);
|
|
918
1185
|
},
|