edge-mock
Version:
types for testing an developer edge applications
122 lines • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findBoundary = exports.EdgeBody = void 0;
const utils_1 = require("../utils");
const forms_1 = require("../forms");
const Blob_1 = require("./Blob");
const ReadableStream_1 = require("./ReadableStream");
const FormData_1 = require("./FormData");
class EdgeBody {
_formBoundary;
_stream = null;
constructor(content, formBoundary) {
this._formBoundary = formBoundary;
if (content) {
if (typeof content != 'string' && 'getReader' in content) {
this._stream = content;
}
else {
this._stream = new ReadableStream_1.EdgeReadableStream({
start: async (controller) => {
const abv = await this._bodyToArrayBufferView(content);
controller.enqueue(abv);
},
});
}
}
}
get body() {
return this._stream;
}
get bodyUsed() {
return !!this._stream && this._stream.locked;
}
async arrayBuffer() {
this._check_used('arrayBuffer');
if (this._stream) {
const view = await utils_1.rsToArrayBufferView(this._stream);
return view.buffer;
}
else {
return new ArrayBuffer(0);
}
}
async blob() {
this._check_used('blob');
let parts = [];
if (this._stream) {
parts = [await utils_1.rsToArrayBufferView(this._stream)];
}
return new Blob_1.EdgeBlob(parts);
}
async json() {
this._check_used('json');
return JSON.parse(await this._text());
}
async text() {
this._check_used('text');
return await this._text();
}
async formData() {
if (this._formBoundary) {
return forms_1.stringAsFormData(this._formBoundary, await this.text());
}
else {
throw new Error('unable to parse form data, invalid content-type header');
}
}
async _text() {
if (this._stream) {
return await utils_1.rsToString(this._stream);
}
else {
return '';
}
}
_check_used(name) {
if (this._stream?.locked) {
throw new Error(`Failed to execute "${name}": body is already used`);
}
}
async _bodyToArrayBufferView(body) {
if (typeof body == 'string') {
return utils_1.encode(body);
}
else if ('buffer' in body) {
return body;
}
else if ('byteLength' in body) {
return new Uint8Array(body);
}
else if ('arrayBuffer' in body) {
return new Uint8Array(await body.arrayBuffer());
}
else if (body instanceof URLSearchParams) {
return utils_1.encode(body.toString());
}
else if (body instanceof FormData_1.EdgeFormData) {
const [_, form_body] = await forms_1.formDataAsString(body, this._formBoundary);
return utils_1.encode(form_body);
}
else {
throw new TypeError(`${utils_1.getType(body)}s are not supported as body types`);
}
}
}
exports.EdgeBody = EdgeBody;
function findBoundary(headers, content) {
const content_type = headers.get('content-type');
const m_boundary = content_type ? content_type.match(/^multipart\/form-data; ?boundary=(.+)$/i) : null;
if (m_boundary) {
return m_boundary[1];
}
else if (content instanceof FormData_1.EdgeFormData) {
const boundary = forms_1.generateBoundary();
if (!content_type || content_type == 'multipart/form-data') {
headers.set('content-type', `multipart/form-data; boundary=${boundary}`);
}
return boundary;
}
}
exports.findBoundary = findBoundary;
//# sourceMappingURL=Body.js.map