npm package diff
Package: @forge/bundler
Versions: 6.0.0-next.13 - 6.0.0-next.14
File: package/out/stubs/http-sandbox-impl.js
Index: package/out/stubs/http-sandbox-impl.js
===================================================================
--- package/out/stubs/http-sandbox-impl.js
+++ package/out/stubs/http-sandbox-impl.js
@@ -1,185 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.Request = exports.isHttpRequestOptions = void 0;
-const readable_stream_1 = require("readable-stream");
-const api_1 = require("@forge/api");
-const isCallback = (options) => typeof options === 'function';
-const isString = (options) => typeof options === 'string';
-const isURL = (options) => typeof options === 'object' && options.constructor.name === 'URL';
-const isStringOrURL = (options) => isString(options) || isURL(options);
-const isHttpRequestOptions = (options) => !isStringOrURL(options) && !isCallback(options);
-exports.isHttpRequestOptions = isHttpRequestOptions;
-function formatHttpRequestArgsToUrl(options) {
- const protocol = 'https:';
- const auth = options.auth ? `${options.auth}@` : '';
- const host = options.hostname ?? options.host;
- const port = options.port ? `:${options.port}` : '';
- const path = options.path ?? '/';
- return `${protocol}//${auth}${host}${port}${path}`;
-}
-class Request extends readable_stream_1.Writable {
- reqBody;
- chainableOptions;
- constructor(options, callback) {
- super();
- this.reqBody = [];
- this.chainableOptions = {
- headers: {}
- };
- if (callback) {
- this.on('response', callback);
- }
- this.on('finish', this.fetch.bind(this, options));
- }
- setHeader(name, value) {
- this.chainableOptions.headers[name] = value;
- return this;
- }
- setTimeout(value, callback) {
- this.chainableOptions.timeout = value;
- if (callback) {
- this.on('timeout', callback);
- }
- return this;
- }
- async fetch(options) {
- const { url, init } = Request.convertToForgeFetchArgs(options, this.chainableOptions);
- if (init.method && init.method !== 'HEAD' && init.method !== 'GET') {
- init.body = Buffer.concat(this.reqBody);
- }
- try {
- const response = await (0, api_1.fetch)(url, init);
- const content = await response.arrayBuffer();
- const transformedAPIResponse = new Response(content, response);
- this.emit('response', transformedAPIResponse);
- }
- catch (err) {
- if (err.message.startsWith('network timeout')) {
- this.emit('timeout');
- }
- else {
- this.emit('error', err);
- }
- }
- }
- _write(chunk, encoding, callback) {
- try {
- if (Buffer.isEncoding(encoding)) {
- this.reqBody.push(Buffer.from(chunk, encoding));
- }
- else if (Buffer.isBuffer(chunk)) {
- this.reqBody.push(chunk);
- }
- else {
- this.reqBody.push(Buffer.from(chunk));
- }
- callback();
- }
- catch (err) {
- callback(err);
- }
- }
- static urlFromFetchArgs(options) {
- if (isString(options)) {
- return options;
- }
- else if (isURL(options)) {
- return options.toString();
- }
- else {
- try {
- return formatHttpRequestArgsToUrl(options);
- }
- catch (err) {
- console.error('Unable to make request with unrecognised URL options:', options);
- throw err;
- }
- }
- }
- static convertToForgeFetchArgs(options, chainableOptions) {
- const url = Request.urlFromFetchArgs(options);
- if (isStringOrURL(options)) {
- return { url, init: chainableOptions };
- }
- else {
- const reqOpts = {};
- if (options.headers) {
- reqOpts.headers = {};
- for (const header in options.headers) {
- const headerValue = options.headers[header];
- if (headerValue) {
- reqOpts.headers[header] = headerValue.toString();
- }
- }
- }
- if (options.timeout) {
- reqOpts.timeout = options.timeout;
- }
- reqOpts.method = options.method || 'GET';
- return { url, init: { ...chainableOptions, ...reqOpts } };
- }
- }
- abort() {
- }
-}
-exports.Request = Request;
-const HEADERS_DISCARD_DUPLICATES = new Set([
- 'age',
- 'authorization',
- 'content-length',
- 'content-type',
- 'etag',
- 'expires',
- 'from',
- 'host',
- 'if-modified-since',
- 'if-unmodified-since',
- 'last-modified',
- 'location',
- 'max-forwards',
- 'proxy-authorization',
- 'referer',
- 'retry-after',
- 'server',
- 'user-agent'
-]);
-class Response extends readable_stream_1.Readable {
- statusCode;
- statusMessage;
- headers;
- headersDistinct;
- constructor(content, response) {
- super();
- this.statusCode = response.status;
- this.statusMessage = response.statusText;
- this.headers = {};
- this.headersDistinct = {};
- const rawHeaders = response.headers.raw();
- for (const [name, value] of Object.entries(rawHeaders)) {
- if (name === 'content-encoding') {
- continue;
- }
- let distinctValues;
- let overrideHeaderValue;
- if (HEADERS_DISCARD_DUPLICATES.has(name)) {
- const allHeaderValues = value.split(', ');
- overrideHeaderValue = allHeaderValues[0];
- distinctValues = allHeaderValues;
- }
- else if (name === 'set-cookie') {
- distinctValues = [value];
- }
- else if (name === 'cookie') {
- distinctValues = value.split('; ');
- }
- else {
- distinctValues = value.split(', ');
- }
- this.headers[name] = overrideHeaderValue ?? value;
- this.headersDistinct[name] = distinctValues;
- }
- this._read = () => { };
- this.push(Buffer.from(content));
- this.push(null);
- }
-}