@azure/communication-call-automation
Version:
Azure client library for Azure Communication Call Automation services
146 lines (145 loc) • 5.79 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var contentDownloader_exports = {};
__export(contentDownloader_exports, {
ContentDownloaderImpl: () => ContentDownloaderImpl
});
module.exports = __toCommonJS(contentDownloader_exports);
var import_core_rest_pipeline = require("@azure/core-rest-pipeline");
var import_recordingUrlValidator = require("./utli/recordingUrlValidator.js");
class ContentDownloaderImpl {
client;
/**
* Initialize a new instance of the class ContentDownloader class.
* @param client - Reference to the service client
*/
constructor(client) {
this.client = client;
this.addCustomSignUrlPolicy();
}
addCustomSignUrlPolicy() {
const signUrlPolicy = {
name: "CustomSignUrlPolicy",
async sendRequest(request, next) {
if (request.headers.has("OriginalUrl")) {
request.url = `${request.headers.get("OriginalUrl")}`;
const originalRequest = new URL(request.url);
request.headers.set("Host", originalRequest.host);
}
return next(request);
}
};
const pipelineOptions = {};
pipelineOptions.afterPhase = "Sign";
this.client.pipeline.addPolicy(signUrlPolicy, pipelineOptions);
}
/**
* Deletes a recording.
* @param deleteLocationUrl - The recording location url. Required.
*/
async deleteRecording(deleteLocationUrl, options) {
(0, import_recordingUrlValidator.validateRecordingUrl)(deleteLocationUrl, "deleteLocationUrl");
const fileLocation = new URL(deleteLocationUrl);
const endpoint = new URL(this.client.endpoint);
const modifiedUrlForSigning = endpoint.origin + fileLocation.pathname;
const opt = {
url: modifiedUrlForSigning,
method: "DELETE",
headers: (0, import_core_rest_pipeline.createHttpHeaders)(),
body: "",
abortSignal: options?.abortSignal,
tracingOptions: options?.tracingOptions
};
opt.headers?.set("OriginalUrl", deleteLocationUrl);
opt.headers?.set("x-ms-host", endpoint.host);
opt.headers?.set("accept", "application/json");
const req = (0, import_core_rest_pipeline.createPipelineRequest)(opt);
const results = await this.client.sendRequest(req);
if (results.status !== 200) {
if (results.bodyAsText) {
const jsonBody = JSON.parse(results.bodyAsText);
throw { status: jsonBody.status, message: jsonBody.message };
}
throw { status: results.status };
}
}
/**
* Returns a stream with a call recording.
* @param sourceLocationUrl - The source location url. Required.
* @param options - Additional request options contains downloadRecording options.
*/
async download(sourceLocationUrl, options) {
(0, import_recordingUrlValidator.validateRecordingUrl)(sourceLocationUrl, "sourceLocationUrl");
const fileLocation = new URL(sourceLocationUrl);
const endpoint = new URL(this.client.endpoint);
const modifiedUrlForSigning = endpoint.origin + fileLocation.pathname;
const opt = {
url: modifiedUrlForSigning,
method: "GET",
headers: (0, import_core_rest_pipeline.createHttpHeaders)(),
body: "",
streamResponseStatusCodes: /* @__PURE__ */ new Set([200, 206]),
abortSignal: options.abortSignal,
tracingOptions: options?.tracingOptions
};
if (options.length && !options.offset) {
throw new Error("Download offset value must not be empty if length is set.");
}
opt.headers?.set("OriginalUrl", sourceLocationUrl);
opt.headers?.set("x-ms-host", endpoint.host);
opt.headers?.set("accept", "application/json");
if (options.offset !== void 0) {
const rangeHeader = this.buildRangeHeader(options.offset, options.length);
opt.headers?.set("Range", rangeHeader);
}
const req = (0, import_core_rest_pipeline.createPipelineRequest)(opt);
const results = await this.client.sendRequest(req);
if (results.status !== 200 && results.status !== 206) {
if (results.bodyAsText) {
const jsonBody = JSON.parse(results.bodyAsText);
throw { status: jsonBody.status, message: jsonBody.message };
}
throw { status: results.status };
}
return results;
}
/**
* Builds HTTP Range header for partial content requests.
* @param offset - Starting byte position (0-based)
* @param length - Number of bytes to download (optional)
* @returns Formatted Range header value
*
* @example
* buildRangeHeader(1, 100) // "bytes=1-100" (first 100 bytes)
* buildRangeHeader(100, 50) // "bytes=100-149" (50 bytes starting at 100)
* buildRangeHeader(100) // "bytes=100-" (from byte 100 to end)
*/
buildRangeHeader(offset, length) {
if (length !== void 0 && length > 0) {
const endPosition = offset + length - 1;
return `bytes=${offset}-${endPosition}`;
} else {
return `bytes=${offset}-`;
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ContentDownloaderImpl
});
//# sourceMappingURL=contentDownloader.js.map