auth0
Version:
Auth0 Node.js SDK for the Management API v2.
185 lines (184 loc) • 6.7 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
export function toBinaryUploadRequest(file) {
return __awaiter(this, void 0, void 0, function* () {
const { data, filename, contentLength, contentType } = yield getFileWithMetadata(file);
const request = {
body: data,
headers: {},
};
if (filename) {
request.headers["Content-Disposition"] = `attachment; filename="${filename}"`;
}
if (contentType) {
request.headers["Content-Type"] = contentType;
}
if (contentLength != null) {
request.headers["Content-Length"] = contentLength.toString();
}
return request;
});
}
export function toMultipartDataPart(file) {
return __awaiter(this, void 0, void 0, function* () {
const { data, filename, contentType } = yield getFileWithMetadata(file, {
noSniffFileSize: true,
});
return {
data,
filename,
contentType,
};
});
}
function getFileWithMetadata(file_1) {
return __awaiter(this, arguments, void 0, function* (file, { noSniffFileSize } = {}) {
var _a, _b, _c, _d, _e;
if (isFileLike(file)) {
return getFileWithMetadata({
data: file,
}, { noSniffFileSize });
}
if ("path" in file) {
const fs = yield import("fs");
if (!fs || !fs.createReadStream) {
throw new Error("File path uploads are not supported in this environment.");
}
const data = fs.createReadStream(file.path);
const contentLength = (_a = file.contentLength) !== null && _a !== void 0 ? _a : (noSniffFileSize === true ? undefined : yield tryGetFileSizeFromPath(file.path));
const filename = (_b = file.filename) !== null && _b !== void 0 ? _b : getNameFromPath(file.path);
return {
data,
filename,
contentType: file.contentType,
contentLength,
};
}
if ("data" in file) {
const data = file.data;
const contentLength = (_c = file.contentLength) !== null && _c !== void 0 ? _c : (yield tryGetContentLengthFromFileLike(data, {
noSniffFileSize,
}));
const filename = (_d = file.filename) !== null && _d !== void 0 ? _d : tryGetNameFromFileLike(data);
return {
data,
filename,
contentType: (_e = file.contentType) !== null && _e !== void 0 ? _e : tryGetContentTypeFromFileLike(data),
contentLength,
};
}
throw new Error(`Invalid FileUpload of type ${typeof file}: ${JSON.stringify(file)}`);
});
}
function isFileLike(value) {
return (isBuffer(value) ||
isArrayBufferView(value) ||
isArrayBuffer(value) ||
isUint8Array(value) ||
isBlob(value) ||
isFile(value) ||
isStreamLike(value) ||
isReadableStream(value));
}
function tryGetFileSizeFromPath(path) {
return __awaiter(this, void 0, void 0, function* () {
try {
const fs = yield import("fs");
if (!fs || !fs.promises || !fs.promises.stat) {
return undefined;
}
const fileStat = yield fs.promises.stat(path);
return fileStat.size;
}
catch (_fallbackError) {
return undefined;
}
});
}
function tryGetNameFromFileLike(data) {
if (isNamedValue(data)) {
return data.name;
}
if (isPathedValue(data)) {
return getNameFromPath(data.path.toString());
}
return undefined;
}
function tryGetContentLengthFromFileLike(data_1) {
return __awaiter(this, arguments, void 0, function* (data, { noSniffFileSize } = {}) {
if (isBuffer(data)) {
return data.length;
}
if (isArrayBufferView(data)) {
return data.byteLength;
}
if (isArrayBuffer(data)) {
return data.byteLength;
}
if (isBlob(data)) {
return data.size;
}
if (isFile(data)) {
return data.size;
}
if (noSniffFileSize === true) {
return undefined;
}
if (isPathedValue(data)) {
return yield tryGetFileSizeFromPath(data.path.toString());
}
return undefined;
});
}
function tryGetContentTypeFromFileLike(data) {
if (isBlob(data)) {
return data.type;
}
if (isFile(data)) {
return data.type;
}
return undefined;
}
function getNameFromPath(path) {
const lastForwardSlash = path.lastIndexOf("/");
const lastBackSlash = path.lastIndexOf("\\");
const lastSlashIndex = Math.max(lastForwardSlash, lastBackSlash);
return lastSlashIndex >= 0 ? path.substring(lastSlashIndex + 1) : path;
}
function isNamedValue(value) {
return typeof value === "object" && value != null && "name" in value;
}
function isPathedValue(value) {
return typeof value === "object" && value != null && "path" in value;
}
function isStreamLike(value) {
return typeof value === "object" && value != null && ("read" in value || "pipe" in value);
}
function isReadableStream(value) {
return typeof value === "object" && value != null && "getReader" in value;
}
function isBuffer(value) {
return typeof Buffer !== "undefined" && Buffer.isBuffer && Buffer.isBuffer(value);
}
function isArrayBufferView(value) {
return typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(value);
}
function isArrayBuffer(value) {
return typeof ArrayBuffer !== "undefined" && value instanceof ArrayBuffer;
}
function isUint8Array(value) {
return typeof Uint8Array !== "undefined" && value instanceof Uint8Array;
}
function isBlob(value) {
return typeof Blob !== "undefined" && value instanceof Blob;
}
function isFile(value) {
return typeof File !== "undefined" && value instanceof File;
}