poe-code 3.0.285 → 3.0.286
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/package.json
CHANGED
|
@@ -317,8 +317,57 @@ function decodeBase64Body(body) {
|
|
|
317
317
|
if (typeof body !== "string") {
|
|
318
318
|
throw new UserError("Base64 request bodies must be strings.");
|
|
319
319
|
}
|
|
320
|
+
if (!isValidBase64(body)) {
|
|
321
|
+
throw new UserError("Base64 request bodies must contain valid base64 text.");
|
|
322
|
+
}
|
|
320
323
|
return Uint8Array.from(Buffer.from(body, "base64")).buffer;
|
|
321
324
|
}
|
|
325
|
+
function isValidBase64(value) {
|
|
326
|
+
const paddingLength = getBase64PaddingLength(value);
|
|
327
|
+
if (paddingLength === null) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
const unpaddedLength = value.length - paddingLength;
|
|
331
|
+
if (unpaddedLength % 4 === 1) {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
if (paddingLength > 0 && value.length % 4 !== 0) {
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
for (let index = 0; index < unpaddedLength; index += 1) {
|
|
338
|
+
if (!isBase64Character(value[index] ?? "")) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const normalized = value.padEnd(value.length + ((4 - (value.length % 4)) % 4), "=");
|
|
343
|
+
return Buffer.from(value, "base64").toString("base64") === normalized;
|
|
344
|
+
}
|
|
345
|
+
function getBase64PaddingLength(value) {
|
|
346
|
+
let paddingLength = 0;
|
|
347
|
+
for (let index = value.length - 1; index >= 0 && value[index] === "="; index -= 1) {
|
|
348
|
+
paddingLength += 1;
|
|
349
|
+
}
|
|
350
|
+
if (paddingLength > 2) {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
for (let index = 0; index < value.length - paddingLength; index += 1) {
|
|
354
|
+
if (value[index] === "=") {
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return paddingLength;
|
|
359
|
+
}
|
|
360
|
+
function isBase64Character(value) {
|
|
361
|
+
if (value.length !== 1) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
const codePoint = value.charCodeAt(0);
|
|
365
|
+
return ((codePoint >= 0x41 && codePoint <= 0x5a) ||
|
|
366
|
+
(codePoint >= 0x61 && codePoint <= 0x7a) ||
|
|
367
|
+
(codePoint >= 0x30 && codePoint <= 0x39) ||
|
|
368
|
+
value === "+" ||
|
|
369
|
+
value === "/");
|
|
370
|
+
}
|
|
322
371
|
function decodeMultipartBinaryValue(value, fallbackFilename) {
|
|
323
372
|
if (typeof value === "string") {
|
|
324
373
|
return {
|
|
@@ -483,7 +532,12 @@ function parseResponseBody(text, contentType) {
|
|
|
483
532
|
if (!isJsonContentType(contentType)) {
|
|
484
533
|
return text;
|
|
485
534
|
}
|
|
486
|
-
|
|
535
|
+
try {
|
|
536
|
+
return JSON.parse(text);
|
|
537
|
+
}
|
|
538
|
+
catch {
|
|
539
|
+
return text;
|
|
540
|
+
}
|
|
487
541
|
}
|
|
488
542
|
function extractErrorCode(body) {
|
|
489
543
|
if (body === null || typeof body !== "object" || Array.isArray(body)) {
|