mirror of
https://github.com/musix-org/musix-oss
synced 2025-06-17 01:16:00 +00:00
fix
This commit is contained in:
36
node_modules/snekfetch/src/browser.js
generated
vendored
Normal file
36
node_modules/snekfetch/src/browser.js
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
function buildRequest(method, url) {
|
||||
return {
|
||||
method,
|
||||
path: url,
|
||||
redirect: this.options.followRedirects ? 'follow' : 'manual',
|
||||
headers: {},
|
||||
setHeader(name, value) {
|
||||
this.headers[name.toLowerCase()] = value;
|
||||
},
|
||||
getHeader(name) {
|
||||
return this.headers[name.toLowerCase()];
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function finalizeRequest() {
|
||||
this._finalizeRequest();
|
||||
if (this.data)
|
||||
this.request.body = this.data;
|
||||
return window.fetch(this.request.path, this.request)
|
||||
.then((r) => r.text().then((t) => {
|
||||
const headers = {};
|
||||
for (const [k, v] of r.headers.entries())
|
||||
headers[k.toLowerCase()] = v;
|
||||
return { response: r, raw: t, headers };
|
||||
}));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildRequest, finalizeRequest,
|
||||
shouldSendRaw: () => false,
|
||||
METHODS: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH'],
|
||||
STATUS_CODES: {},
|
||||
Extension: Object,
|
||||
FormData: window.FormData,
|
||||
};
|
256
node_modules/snekfetch/src/index.js
generated
vendored
Normal file
256
node_modules/snekfetch/src/index.js
generated
vendored
Normal file
@ -0,0 +1,256 @@
|
||||
const browser = typeof window !== 'undefined';
|
||||
const querystring = require('querystring');
|
||||
const transport = browser ? require('./browser') : require('./node');
|
||||
|
||||
/**
|
||||
* Snekfetch
|
||||
* @extends Stream.Readable
|
||||
* @extends Promise
|
||||
*/
|
||||
class Snekfetch extends transport.Extension {
|
||||
/**
|
||||
* Options to pass to the Snekfetch constructor
|
||||
* @typedef {object} SnekfetchOptions
|
||||
* @memberof Snekfetch
|
||||
* @property {object} [headers] Headers to initialize the request with
|
||||
* @property {object|string|Buffer} [data] Data to initialize the request with
|
||||
* @property {string|Object} [query] Query to intialize the request with
|
||||
* @property {boolean} [followRedirects=true] If the request should follow redirects
|
||||
* @property {object} [qs=querystring] Querystring module to use, any object providing
|
||||
* `stringify` and `parse` for querystrings
|
||||
* @property {number} [version = 1] The http version to use [1 or 2]
|
||||
* @property {external:Agent} [agent] Whether to use an http agent
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create a request.
|
||||
* Usually you'll want to do `Snekfetch#method(url [, options])` instead of
|
||||
* `new Snekfetch(method, url [, options])`
|
||||
* @param {string} method HTTP method
|
||||
* @param {string} url URL
|
||||
* @param {SnekfetchOptions} [opts] Options
|
||||
*/
|
||||
constructor(method, url, opts = {}) {
|
||||
super();
|
||||
this.options = Object.assign({ version: 1, qs: querystring, followRedirects: true }, opts);
|
||||
this.request = transport.buildRequest.call(this, method, url, opts);
|
||||
if (opts.headers)
|
||||
this.set(opts.headers);
|
||||
if (opts.query)
|
||||
this.query(opts.query);
|
||||
if (opts.data)
|
||||
this.send(opts.data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a query param to the request
|
||||
* @param {string|Object} name Name of query param or object to add to query
|
||||
* @param {string} [value] If name is a string value, this will be the value of the query param
|
||||
* @returns {Snekfetch} This request
|
||||
*/
|
||||
query(name, value) {
|
||||
if (!this.request.query)
|
||||
this.request.query = {};
|
||||
if (name !== null && typeof name === 'object') {
|
||||
for (const [k, v] of Object.entries(name))
|
||||
this.query(k, v);
|
||||
} else {
|
||||
this.request.query[name] = value;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a header to the request
|
||||
* @param {string|Object} name Name of query param or object to add to headers
|
||||
* @param {string} [value] If name is a string value, this will be the value of the header
|
||||
* @returns {Snekfetch} This request
|
||||
*/
|
||||
set(name, value) {
|
||||
if (name !== null && typeof name === 'object') {
|
||||
for (const key of Object.keys(name))
|
||||
this.set(key, name[key]);
|
||||
} else {
|
||||
this.request.setHeader(name, value);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a form data object
|
||||
* @param {string} name Name of the form attachment
|
||||
* @param {string|Object|Buffer} data Data for the attachment
|
||||
* @param {string} [filename] Optional filename if form attachment name needs to be overridden
|
||||
* @returns {Snekfetch} This request
|
||||
*/
|
||||
attach(...args) {
|
||||
const form = this.data instanceof transport.FormData ? this.data : this.data = new transport.FormData();
|
||||
if (typeof args[0] === 'object') {
|
||||
for (const [k, v] of Object.entries(args[0]))
|
||||
this.attach(k, v);
|
||||
} else {
|
||||
form.append(...args);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send data with the request
|
||||
* @param {string|Buffer|Object} data Data to send
|
||||
* @returns {Snekfetch} This request
|
||||
*/
|
||||
send(data) {
|
||||
if (data instanceof transport.FormData || transport.shouldSendRaw(data)) {
|
||||
this.data = data;
|
||||
} else if (data !== null && typeof data === 'object') {
|
||||
const header = this.request.getHeader('content-type');
|
||||
let serialize;
|
||||
if (header) {
|
||||
if (header.includes('json'))
|
||||
serialize = JSON.stringify;
|
||||
else if (header.includes('urlencoded'))
|
||||
serialize = this.options.qs.stringify;
|
||||
} else {
|
||||
this.set('Content-Type', 'application/json');
|
||||
serialize = JSON.stringify;
|
||||
}
|
||||
this.data = serialize(data);
|
||||
} else {
|
||||
this.data = data;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
then(resolver, rejector) {
|
||||
if (this._response)
|
||||
return this._response.then(resolver, rejector);
|
||||
// eslint-disable-next-line no-return-assign
|
||||
return this._response = transport.finalizeRequest.call(this)
|
||||
.then(({ response, raw, redirect, headers }) => {
|
||||
if (redirect) {
|
||||
let method = this.request.method;
|
||||
if ([301, 302].includes(response.statusCode)) {
|
||||
if (method !== 'HEAD')
|
||||
method = 'GET';
|
||||
this.data = null;
|
||||
} else if (response.statusCode === 303) {
|
||||
method = 'GET';
|
||||
}
|
||||
|
||||
const redirectHeaders = this.request.getHeaders();
|
||||
delete redirectHeaders.host;
|
||||
return new Snekfetch(method, redirect, {
|
||||
data: this.data,
|
||||
headers: redirectHeaders,
|
||||
version: this.options.version,
|
||||
});
|
||||
}
|
||||
|
||||
const statusCode = response.statusCode || response.status;
|
||||
// forgive me :(
|
||||
const self = this; // eslint-disable-line consistent-this
|
||||
/**
|
||||
* Response from Snekfetch
|
||||
* @typedef {Object} SnekfetchResponse
|
||||
* @memberof Snekfetch
|
||||
* @prop {HTTP.Request} request
|
||||
* @prop {?string|object|Buffer} body Processed response body
|
||||
* @prop {string} text Raw response body
|
||||
* @prop {boolean} ok If the response code is >= 200 and < 300
|
||||
* @prop {number} status HTTP status code
|
||||
* @prop {string} statusText Human readable HTTP status
|
||||
*/
|
||||
const res = {
|
||||
request: this.request,
|
||||
get body() {
|
||||
delete res.body;
|
||||
const type = this.headers['content-type'];
|
||||
if (type && type.includes('application/json')) {
|
||||
try {
|
||||
res.body = JSON.parse(res.text);
|
||||
} catch (err) {
|
||||
res.body = res.text;
|
||||
}
|
||||
} else if (type && type.includes('application/x-www-form-urlencoded')) {
|
||||
res.body = self.options.qs.parse(res.text);
|
||||
} else {
|
||||
res.body = raw;
|
||||
}
|
||||
|
||||
return res.body;
|
||||
},
|
||||
text: raw.toString(),
|
||||
ok: statusCode >= 200 && statusCode < 400,
|
||||
headers: headers || response.headers,
|
||||
status: statusCode,
|
||||
statusText: response.statusText || transport.STATUS_CODES[response.statusCode],
|
||||
};
|
||||
|
||||
if (res.ok) {
|
||||
return res;
|
||||
} else {
|
||||
const err = new Error(`${res.status} ${res.statusText}`.trim());
|
||||
Object.assign(err, res);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
})
|
||||
.then(resolver, rejector);
|
||||
}
|
||||
|
||||
catch(rejector) {
|
||||
return this.then(null, rejector);
|
||||
}
|
||||
|
||||
/**
|
||||
* End the request
|
||||
* @param {Function} [cb] Optional callback to handle the response
|
||||
* @returns {Promise} This request
|
||||
*/
|
||||
end(cb) {
|
||||
return this.then(
|
||||
(res) => cb ? cb(null, res) : res,
|
||||
(err) => cb ? cb(err, err.status ? err : null) : Promise.reject(err)
|
||||
);
|
||||
}
|
||||
|
||||
_finalizeRequest() {
|
||||
if (!this.request)
|
||||
return;
|
||||
|
||||
if (this.request.method !== 'HEAD')
|
||||
this.set('Accept-Encoding', 'gzip, deflate');
|
||||
if (this.data && this.data.getBoundary)
|
||||
this.set('Content-Type', `multipart/form-data; boundary=${this.data.getBoundary()}`);
|
||||
|
||||
if (this.request.query) {
|
||||
const [path, query] = this.request.path.split('?');
|
||||
this.request.path = `${path}?${this.options.qs.stringify(this.request.query)}${query ? `&${query}` : ''}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ((THIS)) request
|
||||
* @dynamic this.METHODS
|
||||
* @method Snekfetch.((THIS)lowerCase)
|
||||
* @param {string} url The url to request
|
||||
* @param {Snekfetch.snekfetchOptions} [opts] Options
|
||||
* @returns {Snekfetch}
|
||||
*/
|
||||
Snekfetch.METHODS = transport.METHODS.concat('BREW').filter((m) => m !== 'M-SEARCH');
|
||||
for (const method of Snekfetch.METHODS) {
|
||||
Snekfetch[method.toLowerCase()] = function runMethod(url, opts) {
|
||||
const Constructor = this.prototype instanceof Snekfetch ? this : Snekfetch;
|
||||
return new Constructor(method, url, opts);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = Snekfetch;
|
||||
|
||||
/**
|
||||
* @external Agent
|
||||
* @see {@link https://nodejs.org/api/http.html#http_class_http_agent}
|
||||
*/
|
51
node_modules/snekfetch/src/node/FormData.js
generated
vendored
Normal file
51
node_modules/snekfetch/src/node/FormData.js
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
const path = require('path');
|
||||
const mime = require('./mime');
|
||||
|
||||
class FormData {
|
||||
constructor() {
|
||||
this.boundary = `--snekfetch--${Math.random().toString().slice(2, 7)}`;
|
||||
this.buffers = [];
|
||||
}
|
||||
|
||||
append(name, data, filename) {
|
||||
if (typeof data === 'undefined')
|
||||
return;
|
||||
let str = `\r\n--${this.boundary}\r\nContent-Disposition: form-data; name="${name}"`;
|
||||
let mimetype = null;
|
||||
if (filename) {
|
||||
str += `; filename="${filename}"`;
|
||||
mimetype = 'application/octet-stream';
|
||||
const extname = path.extname(filename).slice(1);
|
||||
if (extname)
|
||||
mimetype = mime.lookup(extname);
|
||||
}
|
||||
|
||||
if (data instanceof Buffer) {
|
||||
mimetype = mime.buffer(data);
|
||||
} else if (typeof data === 'object') {
|
||||
mimetype = 'application/json';
|
||||
data = Buffer.from(JSON.stringify(data));
|
||||
} else {
|
||||
data = Buffer.from(String(data));
|
||||
}
|
||||
|
||||
if (mimetype)
|
||||
str += `\r\nContent-Type: ${mimetype}`;
|
||||
this.buffers.push(Buffer.from(`${str}\r\n\r\n`));
|
||||
this.buffers.push(data);
|
||||
}
|
||||
|
||||
getBoundary() {
|
||||
return this.boundary;
|
||||
}
|
||||
|
||||
end() {
|
||||
return Buffer.concat([...this.buffers, Buffer.from(`\r\n--${this.boundary}--`)]);
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.buffers.reduce((sum, b) => sum + Buffer.byteLength(b), 0);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FormData;
|
151
node_modules/snekfetch/src/node/index.js
generated
vendored
Normal file
151
node_modules/snekfetch/src/node/index.js
generated
vendored
Normal file
@ -0,0 +1,151 @@
|
||||
const zlib = require('zlib');
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const URL = require('url');
|
||||
const Stream = require('stream');
|
||||
const FormData = require('./FormData');
|
||||
|
||||
const Package = require('../../package.json');
|
||||
|
||||
const transports = {
|
||||
'http:': http,
|
||||
'https:': https,
|
||||
'file:': require('./transports/file'),
|
||||
};
|
||||
|
||||
function buildRequest(method, url) {
|
||||
/* istanbul ignore next */
|
||||
this._read = () => {
|
||||
this.resume();
|
||||
if (this._response)
|
||||
return;
|
||||
this.catch((err) => this.emit('error', err));
|
||||
};
|
||||
|
||||
this.options.lastBuiltUrl = url;
|
||||
|
||||
const options = URL.parse(url);
|
||||
options.encoding = 'utf8';
|
||||
|
||||
if (!options.protocol)
|
||||
throw new Error('URL must have a valid protocol');
|
||||
|
||||
const transport = transports[options.protocol];
|
||||
options.method = method.toUpperCase();
|
||||
|
||||
if (this.options.headers)
|
||||
options.headers = this.options.headers;
|
||||
|
||||
if (this.options.agent)
|
||||
options.agent = this.options.agent;
|
||||
else if (transport.Agent && this.options.followRedirects !== false)
|
||||
options.agent = new transport.Agent({ keepAlive: true });
|
||||
|
||||
if (options.port)
|
||||
options.port = parseInt(options.port);
|
||||
|
||||
this.options._req = options;
|
||||
const request = transport.request(options);
|
||||
if (request.setNoDelay)
|
||||
request.setNoDelay(true);
|
||||
return request;
|
||||
}
|
||||
|
||||
function finalizeRequest() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = this.request;
|
||||
|
||||
let socket;
|
||||
|
||||
const handleError = (err) => {
|
||||
if (!err)
|
||||
err = new Error('Unknown error occured');
|
||||
err.request = request;
|
||||
reject(err);
|
||||
if (socket)
|
||||
socket.removeListener('error', handleError);
|
||||
};
|
||||
|
||||
request.once('abort', handleError);
|
||||
request.once('error', handleError);
|
||||
request.once('socket', (s) => {
|
||||
socket = s;
|
||||
s.once('error', handleError);
|
||||
});
|
||||
|
||||
request.once('response', (response) => {
|
||||
if (socket)
|
||||
socket.removeListener('error', handleError);
|
||||
let stream = response;
|
||||
if (shouldUnzip(response)) {
|
||||
stream = response.pipe(zlib.createUnzip({
|
||||
flush: zlib.Z_SYNC_FLUSH,
|
||||
finishFlush: zlib.Z_SYNC_FLUSH,
|
||||
}));
|
||||
}
|
||||
|
||||
if (this.options.followRedirects !== false && [301, 302, 303, 307, 308].includes(response.statusCode)) {
|
||||
resolve({
|
||||
response,
|
||||
redirect: URL.resolve(this.options.lastBuiltUrl, response.headers.location),
|
||||
});
|
||||
response.destroy();
|
||||
} else {
|
||||
const body = [];
|
||||
|
||||
stream.on('data', (chunk) => {
|
||||
if (!this.push(chunk))
|
||||
this.pause();
|
||||
body.push(chunk);
|
||||
});
|
||||
|
||||
stream.once('end', () => {
|
||||
this.push(null);
|
||||
const raw = Buffer.concat(body);
|
||||
resolve({ response, raw, redirect: false });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (!this.request.getHeader('user-agent'))
|
||||
this.set('User-Agent', `snekfetch/${Package.version} (${Package.homepage})`);
|
||||
|
||||
this._finalizeRequest();
|
||||
let data = this.data;
|
||||
if (data && data.end)
|
||||
data = data.end();
|
||||
if (Array.isArray(data)) {
|
||||
for (const chunk of data)
|
||||
request.write(chunk);
|
||||
request.end();
|
||||
} else if (data instanceof Stream) {
|
||||
data.pipe(request);
|
||||
} else if (data instanceof Buffer) {
|
||||
request.end(data);
|
||||
} else if (data) {
|
||||
request.end(data);
|
||||
} else {
|
||||
request.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function shouldSendRaw(data) {
|
||||
return data instanceof Buffer || data instanceof Stream;
|
||||
}
|
||||
|
||||
function shouldUnzip(res) {
|
||||
if (res.statusCode === 204 || res.statusCode === 304)
|
||||
return false;
|
||||
if (res.headers['content-length'] === '0')
|
||||
return false;
|
||||
return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildRequest, finalizeRequest, shouldSendRaw,
|
||||
METHODS: http.METHODS,
|
||||
STATUS_CODES: http.STATUS_CODES,
|
||||
FormData,
|
||||
Extension: Stream.Readable,
|
||||
};
|
16
node_modules/snekfetch/src/node/mime.js
generated
vendored
Normal file
16
node_modules/snekfetch/src/node/mime.js
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
const mimes = require('./mimes.json');
|
||||
const mimeOfBuffer = require('./mimeOfBuffer.js');
|
||||
|
||||
function lookupMime(ext) {
|
||||
return mimes[ext.replace(/^\./, '')] || mimes.bin;
|
||||
}
|
||||
|
||||
function lookupBuffer(buffer) {
|
||||
const ret = mimeOfBuffer(buffer);
|
||||
return ret ? ret.mime : mimes.bin;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buffer: lookupBuffer,
|
||||
lookup: lookupMime,
|
||||
};
|
546
node_modules/snekfetch/src/node/mimeOfBuffer.js
generated
vendored
Normal file
546
node_modules/snekfetch/src/node/mimeOfBuffer.js
generated
vendored
Normal file
@ -0,0 +1,546 @@
|
||||
/* eslint complexity: 0 */
|
||||
|
||||
// from file-type by @sindresorhus under the MIT license
|
||||
// https://github.com/sindresorhus/file-type
|
||||
|
||||
function mimeOfBuffer(input) {
|
||||
const buf = new Uint8Array(input);
|
||||
|
||||
if (!(buf && buf.length > 1))
|
||||
return null;
|
||||
|
||||
|
||||
if (buf[0] === 0xFF && buf[1] === 0xD8 && buf[2] === 0xFF) {
|
||||
return {
|
||||
ext: 'jpg',
|
||||
mime: 'image/jpeg',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4E && buf[3] === 0x47) {
|
||||
return {
|
||||
ext: 'png',
|
||||
mime: 'image/png',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46) {
|
||||
return {
|
||||
ext: 'gif',
|
||||
mime: 'image/gif',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50) {
|
||||
return {
|
||||
ext: 'webp',
|
||||
mime: 'image/webp',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x46 && buf[1] === 0x4C && buf[2] === 0x49 && buf[3] === 0x46) {
|
||||
return {
|
||||
ext: 'flif',
|
||||
mime: 'image/flif',
|
||||
};
|
||||
}
|
||||
|
||||
// needs to be before `tif` check
|
||||
if (
|
||||
((buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0x2A && buf[3] === 0x0) ||
|
||||
(buf[0] === 0x4D && buf[1] === 0x4D && buf[2] === 0x0 && buf[3] === 0x2A)) && buf[8] === 0x43 && buf[9] === 0x52
|
||||
) {
|
||||
return {
|
||||
ext: 'cr2',
|
||||
mime: 'image/x-canon-cr2',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
(buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0x2A && buf[3] === 0x0) ||
|
||||
(buf[0] === 0x4D && buf[1] === 0x4D && buf[2] === 0x0 && buf[3] === 0x2A)
|
||||
) {
|
||||
return {
|
||||
ext: 'tif',
|
||||
mime: 'image/tiff',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x42 && buf[1] === 0x4D) {
|
||||
return {
|
||||
ext: 'bmp',
|
||||
mime: 'image/bmp',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0xBC) {
|
||||
return {
|
||||
ext: 'jxr',
|
||||
mime: 'image/vnd.ms-photo',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x38 && buf[1] === 0x42 && buf[2] === 0x50 && buf[3] === 0x53) {
|
||||
return {
|
||||
ext: 'psd',
|
||||
mime: 'image/vnd.adobe.photoshop',
|
||||
};
|
||||
}
|
||||
|
||||
// needs to be before `zip` check
|
||||
if (
|
||||
buf[0] === 0x50 && buf[1] === 0x4B && buf[2] === 0x3 && buf[3] === 0x4 && buf[30] === 0x6D && buf[31] === 0x69 &&
|
||||
buf[32] === 0x6D && buf[33] === 0x65 && buf[34] === 0x74 && buf[35] === 0x79 && buf[36] === 0x70 &&
|
||||
buf[37] === 0x65 && buf[38] === 0x61 && buf[39] === 0x70 && buf[40] === 0x70 && buf[41] === 0x6C &&
|
||||
buf[42] === 0x69 && buf[43] === 0x63 && buf[44] === 0x61 && buf[45] === 0x74 && buf[46] === 0x69 &&
|
||||
buf[47] === 0x6F && buf[48] === 0x6E && buf[49] === 0x2F && buf[50] === 0x65 && buf[51] === 0x70 &&
|
||||
buf[52] === 0x75 && buf[53] === 0x62 && buf[54] === 0x2B && buf[55] === 0x7A && buf[56] === 0x69 &&
|
||||
buf[57] === 0x70
|
||||
) {
|
||||
return {
|
||||
ext: 'epub',
|
||||
mime: 'application/epub+zip',
|
||||
};
|
||||
}
|
||||
|
||||
// needs to be before `zip` check
|
||||
// assumes signed .xpi from addons.mozilla.org
|
||||
if (
|
||||
buf[0] === 0x50 && buf[1] === 0x4B && buf[2] === 0x3 && buf[3] === 0x4 && buf[30] === 0x4D && buf[31] === 0x45 &&
|
||||
buf[32] === 0x54 && buf[33] === 0x41 && buf[34] === 0x2D && buf[35] === 0x49 && buf[36] === 0x4E &&
|
||||
buf[37] === 0x46 && buf[38] === 0x2F && buf[39] === 0x6D && buf[40] === 0x6F && buf[41] === 0x7A &&
|
||||
buf[42] === 0x69 && buf[43] === 0x6C && buf[44] === 0x6C && buf[45] === 0x61 && buf[46] === 0x2E &&
|
||||
buf[47] === 0x72 && buf[48] === 0x73 && buf[49] === 0x61
|
||||
) {
|
||||
return {
|
||||
ext: 'xpi',
|
||||
mime: 'application/x-xpinstall',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x50 && buf[1] === 0x4B && (buf[2] === 0x3 || buf[2] === 0x5 || buf[2] === 0x7) &&
|
||||
(buf[3] === 0x4 || buf[3] === 0x6 || buf[3] === 0x8)
|
||||
) {
|
||||
return {
|
||||
ext: 'zip',
|
||||
mime: 'application/zip',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[257] === 0x75 && buf[258] === 0x73 && buf[259] === 0x74 && buf[260] === 0x61 && buf[261] === 0x72) {
|
||||
return {
|
||||
ext: 'tar',
|
||||
mime: 'application/x-tar',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x52 && buf[1] === 0x61 && buf[2] === 0x72 && buf[3] === 0x21 && buf[4] === 0x1A && buf[5] === 0x7 &&
|
||||
(buf[6] === 0x0 || buf[6] === 0x1)
|
||||
) {
|
||||
return {
|
||||
ext: 'rar',
|
||||
mime: 'application/x-rar-compressed',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x1F && buf[1] === 0x8B && buf[2] === 0x8) {
|
||||
return {
|
||||
ext: 'gz',
|
||||
mime: 'application/gzip',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x42 && buf[1] === 0x5A && buf[2] === 0x68) {
|
||||
return {
|
||||
ext: 'bz2',
|
||||
mime: 'application/x-bzip2',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x37 && buf[1] === 0x7A && buf[2] === 0xBC && buf[3] === 0xAF && buf[4] === 0x27 && buf[5] === 0x1C) {
|
||||
return {
|
||||
ext: '7z',
|
||||
mime: 'application/x-7z-compressed',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x78 && buf[1] === 0x01) {
|
||||
return {
|
||||
ext: 'dmg',
|
||||
mime: 'application/x-apple-diskimage',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
(buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && (buf[3] === 0x18 || buf[3] === 0x20) && buf[4] === 0x66 &&
|
||||
buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70) ||
|
||||
(buf[0] === 0x33 && buf[1] === 0x67 && buf[2] === 0x70 && buf[3] === 0x35) ||
|
||||
(buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 && buf[5] === 0x74 &&
|
||||
buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x6D && buf[9] === 0x70 && buf[10] === 0x34 &&
|
||||
buf[11] === 0x32 && buf[16] === 0x6D && buf[17] === 0x70 && buf[18] === 0x34 && buf[19] === 0x31 &&
|
||||
buf[20] === 0x6D && buf[21] === 0x70 && buf[22] === 0x34 && buf[23] === 0x32 && buf[24] === 0x69 &&
|
||||
buf[25] === 0x73 && buf[26] === 0x6F && buf[27] === 0x6D) ||
|
||||
(buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 && buf[5] === 0x74 &&
|
||||
buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x69 && buf[9] === 0x73 && buf[10] === 0x6F &&
|
||||
buf[11] === 0x6D) ||
|
||||
(buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1c && buf[4] === 0x66 && buf[5] === 0x74 &&
|
||||
buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x6D && buf[9] === 0x70 && buf[10] === 0x34 &&
|
||||
buf[11] === 0x32 && buf[12] === 0x0 && buf[13] === 0x0 && buf[14] === 0x0 && buf[15] === 0x0)
|
||||
) {
|
||||
return {
|
||||
ext: 'mp4',
|
||||
mime: 'video/mp4',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 &&
|
||||
buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x4D && buf[9] === 0x34 && buf[10] === 0x56
|
||||
) {
|
||||
return {
|
||||
ext: 'm4v',
|
||||
mime: 'video/x-m4v',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x4D && buf[1] === 0x54 && buf[2] === 0x68 && buf[3] === 0x64) {
|
||||
return {
|
||||
ext: 'mid',
|
||||
mime: 'audio/midi',
|
||||
};
|
||||
}
|
||||
|
||||
// https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska
|
||||
if (buf[0] === 0x1A && buf[1] === 0x45 && buf[2] === 0xDF && buf[3] === 0xA3) {
|
||||
const sliced = buf.subarray(4, 4 + 4096);
|
||||
const idPos = sliced.findIndex((el, i, arr) => arr[i] === 0x42 && arr[i + 1] === 0x82);
|
||||
|
||||
if (idPos >= 0) {
|
||||
const docTypePos = idPos + 3;
|
||||
const findDocType = (type) => Array.from(type).every((c, i) => sliced[docTypePos + i] === c.charCodeAt(0));
|
||||
|
||||
if (findDocType('matroska')) {
|
||||
return {
|
||||
ext: 'mkv',
|
||||
mime: 'video/x-matroska',
|
||||
};
|
||||
}
|
||||
if (findDocType('webm')) {
|
||||
return {
|
||||
ext: 'webm',
|
||||
mime: 'video/webm',
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x14 && buf[4] === 0x66 && buf[5] === 0x74 &&
|
||||
buf[6] === 0x79 && buf[7] === 0x70
|
||||
) {
|
||||
return {
|
||||
ext: 'mov',
|
||||
mime: 'video/quicktime',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x41 && buf[9] === 0x56 &&
|
||||
buf[10] === 0x49
|
||||
) {
|
||||
return {
|
||||
ext: 'avi',
|
||||
mime: 'video/x-msvideo',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x30 && buf[1] === 0x26 && buf[2] === 0xB2 && buf[3] === 0x75 && buf[4] === 0x8E && buf[5] === 0x66 &&
|
||||
buf[6] === 0xCF && buf[7] === 0x11 && buf[8] === 0xA6 && buf[9] === 0xD9
|
||||
) {
|
||||
return {
|
||||
ext: 'wmv',
|
||||
mime: 'video/x-ms-wmv',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x1 && buf[3].toString(16)[0] === 'b') {
|
||||
return {
|
||||
ext: 'mpg',
|
||||
mime: 'video/mpeg',
|
||||
};
|
||||
}
|
||||
|
||||
if ((buf[0] === 0x49 && buf[1] === 0x44 && buf[2] === 0x33) || (buf[0] === 0xFF && buf[1] === 0xfb)) {
|
||||
return {
|
||||
ext: 'mp3',
|
||||
mime: 'audio/mpeg',
|
||||
};
|
||||
}
|
||||
|
||||
if ((buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x4D &&
|
||||
buf[9] === 0x34 && buf[10] === 0x41) || (buf[0] === 0x4D && buf[1] === 0x34 && buf[2] === 0x41 && buf[3] === 0x20)
|
||||
) {
|
||||
return {
|
||||
ext: 'm4a',
|
||||
mime: 'audio/m4a',
|
||||
};
|
||||
}
|
||||
|
||||
// needs to be before `ogg` check
|
||||
if (
|
||||
buf[28] === 0x4F && buf[29] === 0x70 && buf[30] === 0x75 && buf[31] === 0x73 && buf[32] === 0x48 &&
|
||||
buf[33] === 0x65 && buf[34] === 0x61 && buf[35] === 0x64
|
||||
) {
|
||||
return {
|
||||
ext: 'opus',
|
||||
mime: 'audio/opus',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x4F && buf[1] === 0x67 && buf[2] === 0x67 && buf[3] === 0x53) {
|
||||
return {
|
||||
ext: 'ogg',
|
||||
mime: 'audio/ogg',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x66 && buf[1] === 0x4C && buf[2] === 0x61 && buf[3] === 0x43) {
|
||||
return {
|
||||
ext: 'flac',
|
||||
mime: 'audio/x-flac',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x57 && buf[9] === 0x41 &&
|
||||
buf[10] === 0x56 && buf[11] === 0x45
|
||||
) {
|
||||
return {
|
||||
ext: 'wav',
|
||||
mime: 'audio/x-wav',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x23 && buf[1] === 0x21 && buf[2] === 0x41 && buf[3] === 0x4D && buf[4] === 0x52 && buf[5] === 0x0A) {
|
||||
return {
|
||||
ext: 'amr',
|
||||
mime: 'audio/amr',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x25 && buf[1] === 0x50 && buf[2] === 0x44 && buf[3] === 0x46) {
|
||||
return {
|
||||
ext: 'pdf',
|
||||
mime: 'application/pdf',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x4D && buf[1] === 0x5A) {
|
||||
return {
|
||||
ext: 'exe',
|
||||
mime: 'application/x-msdownload',
|
||||
};
|
||||
}
|
||||
|
||||
if ((buf[0] === 0x43 || buf[0] === 0x46) && buf[1] === 0x57 && buf[2] === 0x53) {
|
||||
return {
|
||||
ext: 'swf',
|
||||
mime: 'application/x-shockwave-flash',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x7B && buf[1] === 0x5C && buf[2] === 0x72 && buf[3] === 0x74 && buf[4] === 0x66) {
|
||||
return {
|
||||
ext: 'rtf',
|
||||
mime: 'application/rtf',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
(buf[0] === 0x77 && buf[1] === 0x4F && buf[2] === 0x46 && buf[3] === 0x46) &&
|
||||
(
|
||||
(buf[4] === 0x00 && buf[5] === 0x01 && buf[6] === 0x00 && buf[7] === 0x00) ||
|
||||
(buf[4] === 0x4F && buf[5] === 0x54 && buf[6] === 0x54 && buf[7] === 0x4F)
|
||||
)
|
||||
) {
|
||||
return {
|
||||
ext: 'woff',
|
||||
mime: 'application/font-woff',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
(buf[0] === 0x77 && buf[1] === 0x4F && buf[2] === 0x46 && buf[3] === 0x32) &&
|
||||
(
|
||||
(buf[4] === 0x00 && buf[5] === 0x01 && buf[6] === 0x00 && buf[7] === 0x00) ||
|
||||
(buf[4] === 0x4F && buf[5] === 0x54 && buf[6] === 0x54 && buf[7] === 0x4F)
|
||||
)
|
||||
) {
|
||||
return {
|
||||
ext: 'woff2',
|
||||
mime: 'application/font-woff',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
(buf[34] === 0x4C && buf[35] === 0x50) &&
|
||||
(
|
||||
(buf[8] === 0x00 && buf[9] === 0x00 && buf[10] === 0x01) ||
|
||||
(buf[8] === 0x01 && buf[9] === 0x00 && buf[10] === 0x02) ||
|
||||
(buf[8] === 0x02 && buf[9] === 0x00 && buf[10] === 0x02)
|
||||
)
|
||||
) {
|
||||
return {
|
||||
ext: 'eot',
|
||||
mime: 'application/octet-stream',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x00 && buf[1] === 0x01 && buf[2] === 0x00 && buf[3] === 0x00 && buf[4] === 0x00) {
|
||||
return {
|
||||
ext: 'ttf',
|
||||
mime: 'application/font-sfnt',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x4F && buf[1] === 0x54 && buf[2] === 0x54 && buf[3] === 0x4F && buf[4] === 0x00) {
|
||||
return {
|
||||
ext: 'otf',
|
||||
mime: 'application/font-sfnt',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x00 && buf[1] === 0x00 && buf[2] === 0x01 && buf[3] === 0x00) {
|
||||
return {
|
||||
ext: 'ico',
|
||||
mime: 'image/x-icon',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x46 && buf[1] === 0x4C && buf[2] === 0x56 && buf[3] === 0x01) {
|
||||
return {
|
||||
ext: 'flv',
|
||||
mime: 'video/x-flv',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x25 && buf[1] === 0x21) {
|
||||
return {
|
||||
ext: 'ps',
|
||||
mime: 'application/postscript',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0xFD && buf[1] === 0x37 && buf[2] === 0x7A && buf[3] === 0x58 && buf[4] === 0x5A && buf[5] === 0x00) {
|
||||
return {
|
||||
ext: 'xz',
|
||||
mime: 'application/x-xz',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x53 && buf[1] === 0x51 && buf[2] === 0x4C && buf[3] === 0x69) {
|
||||
return {
|
||||
ext: 'sqlite',
|
||||
mime: 'application/x-sqlite3',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x4E && buf[1] === 0x45 && buf[2] === 0x53 && buf[3] === 0x1A) {
|
||||
return {
|
||||
ext: 'nes',
|
||||
mime: 'application/x-nintendo-nes-rom',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x43 && buf[1] === 0x72 && buf[2] === 0x32 && buf[3] === 0x34) {
|
||||
return {
|
||||
ext: 'crx',
|
||||
mime: 'application/x-google-chrome-extension',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
(buf[0] === 0x4D && buf[1] === 0x53 && buf[2] === 0x43 && buf[3] === 0x46) ||
|
||||
(buf[0] === 0x49 && buf[1] === 0x53 && buf[2] === 0x63 && buf[3] === 0x28)
|
||||
) {
|
||||
return {
|
||||
ext: 'cab',
|
||||
mime: 'application/vnd.ms-cab-compressed',
|
||||
};
|
||||
}
|
||||
|
||||
// needs to be before `ar` check
|
||||
if (
|
||||
buf[0] === 0x21 && buf[1] === 0x3C && buf[2] === 0x61 && buf[3] === 0x72 && buf[4] === 0x63 && buf[5] === 0x68 &&
|
||||
buf[6] === 0x3E && buf[7] === 0x0A && buf[8] === 0x64 && buf[9] === 0x65 && buf[10] === 0x62 && buf[11] === 0x69 &&
|
||||
buf[12] === 0x61 && buf[13] === 0x6E && buf[14] === 0x2D && buf[15] === 0x62 && buf[16] === 0x69 &&
|
||||
buf[17] === 0x6E && buf[18] === 0x61 && buf[19] === 0x72 && buf[20] === 0x79
|
||||
) {
|
||||
return {
|
||||
ext: 'deb',
|
||||
mime: 'application/x-deb',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x21 && buf[1] === 0x3C && buf[2] === 0x61 && buf[3] === 0x72 && buf[4] === 0x63 && buf[5] === 0x68 &&
|
||||
buf[6] === 0x3E
|
||||
) {
|
||||
return {
|
||||
ext: 'ar',
|
||||
mime: 'application/x-unix-archive',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0xED && buf[1] === 0xAB && buf[2] === 0xEE && buf[3] === 0xDB) {
|
||||
return {
|
||||
ext: 'rpm',
|
||||
mime: 'application/x-rpm',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
(buf[0] === 0x1F && buf[1] === 0xA0) ||
|
||||
(buf[0] === 0x1F && buf[1] === 0x9D)
|
||||
) {
|
||||
return {
|
||||
ext: 'Z',
|
||||
mime: 'application/x-compress',
|
||||
};
|
||||
}
|
||||
|
||||
if (buf[0] === 0x4C && buf[1] === 0x5A && buf[2] === 0x49 && buf[3] === 0x50) {
|
||||
return {
|
||||
ext: 'lz',
|
||||
mime: 'application/x-lzip',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0xD0 && buf[1] === 0xCF && buf[2] === 0x11 && buf[3] === 0xE0 && buf[4] === 0xA1 && buf[5] === 0xB1 &&
|
||||
buf[6] === 0x1A && buf[7] === 0xE1
|
||||
) {
|
||||
return {
|
||||
ext: 'msi',
|
||||
mime: 'application/x-msi',
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
buf[0] === 0x06 && buf[1] === 0x0E && buf[2] === 0x2B && buf[3] === 0x34 && buf[4] === 0x02 && buf[5] === 0x05 &&
|
||||
buf[6] === 0x01 && buf[7] === 0x01 && buf[8] === 0x0D && buf[9] === 0x01 && buf[10] === 0x02 && buf[11] === 0x01 &&
|
||||
buf[12] === 0x01 && buf[13] === 0x02
|
||||
) {
|
||||
return {
|
||||
ext: 'mxf',
|
||||
mime: 'application/mxf',
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = mimeOfBuffer;
|
1049
node_modules/snekfetch/src/node/mimes.json
generated
vendored
Normal file
1049
node_modules/snekfetch/src/node/mimes.json
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
24
node_modules/snekfetch/src/node/transports/ResponseStream.js
generated
vendored
Normal file
24
node_modules/snekfetch/src/node/transports/ResponseStream.js
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
const Stream = require('stream');
|
||||
|
||||
class ResponseStream extends Stream.Readable {
|
||||
constructor() {
|
||||
super();
|
||||
this.statusCode = 200;
|
||||
this.status = 'OK';
|
||||
}
|
||||
|
||||
error(code, message) {
|
||||
this.statusCode = code;
|
||||
this.status = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
on(event, handler) {
|
||||
if (['end', 'open'].includes(event))
|
||||
handler();
|
||||
}
|
||||
|
||||
_read() {} // eslint-disable-line no-empty-function
|
||||
}
|
||||
|
||||
module.exports = ResponseStream;
|
113
node_modules/snekfetch/src/node/transports/file.js
generated
vendored
Normal file
113
node_modules/snekfetch/src/node/transports/file.js
generated
vendored
Normal file
@ -0,0 +1,113 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const mime = require('../mime');
|
||||
const EventEmitter = require('events');
|
||||
const ResponseStream = require('./ResponseStream');
|
||||
|
||||
const methods = {
|
||||
GET: (filename, req) => {
|
||||
req.end = () => {
|
||||
const stream = should404(filename) ?
|
||||
new ResponseStream().error(404, `ENOENT: no such file or directory, open '${filename}'`) :
|
||||
fs.createReadStream(filename);
|
||||
req.res = stream;
|
||||
stream.headers = {
|
||||
'content-length': 0,
|
||||
'content-type': mime.lookup(path.extname(filename)),
|
||||
};
|
||||
stream.on('open', () => {
|
||||
req.emit('response', stream);
|
||||
});
|
||||
if (stream instanceof ResponseStream)
|
||||
return;
|
||||
stream.statusCode = 200;
|
||||
stream.on('end', () => {
|
||||
stream.headers['content-length'] = stream.bytesRead;
|
||||
});
|
||||
/* istanbul ignore next */
|
||||
stream.on('error', (err) => {
|
||||
stream.statusCode = 400;
|
||||
stream.status = err.message;
|
||||
});
|
||||
};
|
||||
},
|
||||
POST: (filename, req) => {
|
||||
const chunks = [];
|
||||
/* istanbul ignore next */
|
||||
req.write = (data) => {
|
||||
chunks.push(data);
|
||||
};
|
||||
req.end = (data) => {
|
||||
chunks.push(data);
|
||||
const stream = fs.createWriteStream(filename);
|
||||
const standin = new ResponseStream();
|
||||
req.res = standin;
|
||||
standin.headers = {
|
||||
'content-length': 0,
|
||||
'content-type': mime.lookup(path.extname(filename)),
|
||||
};
|
||||
stream.on('finish', () => {
|
||||
req.emit('response', standin);
|
||||
});
|
||||
stream.on('open', () => {
|
||||
(function write() {
|
||||
const chunk = chunks.shift();
|
||||
if (!chunk)
|
||||
return;
|
||||
/* istanbul ignore next */
|
||||
if (!stream.write(chunk))
|
||||
stream.once('drain', write);
|
||||
else
|
||||
write();
|
||||
}());
|
||||
stream.end();
|
||||
});
|
||||
};
|
||||
},
|
||||
DELETE: (filename, req) => {
|
||||
req.end = () => {
|
||||
const stream = new ResponseStream();
|
||||
req.res = stream;
|
||||
stream.headers = {
|
||||
'content-length': 0,
|
||||
'content-type': mime.lookup(path.extname(filename)),
|
||||
};
|
||||
fs.unlink(filename, (err) => {
|
||||
req.emit('response', err ? stream.error(400, err.message) : stream);
|
||||
});
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
class Req extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this._headers = {};
|
||||
}
|
||||
|
||||
setHeader() {} // eslint-disable-line no-empty-function
|
||||
getHeader() {} // eslint-disable-line no-empty-function
|
||||
}
|
||||
|
||||
function request(options) {
|
||||
const method = methods[options.method];
|
||||
if (!method)
|
||||
throw new Error(`Invalid request method for file: "${options.method}"`);
|
||||
const filename = options.href.replace('file://', '');
|
||||
|
||||
const req = new Req();
|
||||
method(filename, req, options);
|
||||
return req;
|
||||
}
|
||||
|
||||
function should404(p) {
|
||||
try {
|
||||
return fs.lstatSync(p).isDirectory();
|
||||
} catch (err) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
request,
|
||||
};
|
102
node_modules/snekfetch/src/node/transports/http2.js
generated
vendored
Normal file
102
node_modules/snekfetch/src/node/transports/http2.js
generated
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
const http = require('http');
|
||||
const Stream = require('stream');
|
||||
const EventEmitter = require('events');
|
||||
const http2 = (() => {
|
||||
try {
|
||||
const h2 = require('http2');
|
||||
if (!h2.constants)
|
||||
throw new Error('DAMN_YOU_NPM_HTTP2');
|
||||
return h2;
|
||||
} catch (err) {
|
||||
return {
|
||||
constants: {},
|
||||
connect: () => {
|
||||
throw new Error('Please run node with --expose-http2 to use the http2 request transport');
|
||||
},
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
const {
|
||||
HTTP2_HEADER_PATH,
|
||||
HTTP2_HEADER_METHOD,
|
||||
HTTP2_HEADER_STATUS,
|
||||
} = http2.constants;
|
||||
|
||||
class Http2Request extends EventEmitter {
|
||||
constructor(options) {
|
||||
super();
|
||||
this.options = options;
|
||||
this._headers = {
|
||||
[HTTP2_HEADER_PATH]: options.pathname,
|
||||
[HTTP2_HEADER_METHOD]: options.method.toUpperCase(),
|
||||
};
|
||||
}
|
||||
|
||||
setHeader(name, value) {
|
||||
this._headers[name.toLowerCase()] = value;
|
||||
}
|
||||
|
||||
getHeader(name) {
|
||||
return this._headers[name];
|
||||
}
|
||||
|
||||
getHeaders() {
|
||||
return this._headers;
|
||||
}
|
||||
|
||||
get path() {
|
||||
return this._headers[HTTP2_HEADER_PATH];
|
||||
}
|
||||
|
||||
set path(path) {
|
||||
this._headers[HTTP2_HEADER_PATH] = path;
|
||||
}
|
||||
|
||||
end(data) {
|
||||
const options = this.options;
|
||||
const client = http2.connect(`${options.protocol}//${options.hostname}`);
|
||||
const req = client.request(this._headers);
|
||||
const stream = new Stream.PassThrough();
|
||||
|
||||
client.once('error', (e) => this.emit('error', e));
|
||||
client.once('frameError', (e) => this.emit('error', e));
|
||||
|
||||
req.once('error', (e) => this.emit('error', e));
|
||||
|
||||
req.once('response', (headers) => {
|
||||
stream.headers = headers;
|
||||
stream.statusCode = headers[HTTP2_HEADER_STATUS];
|
||||
stream.status = http.STATUS_CODES[stream.statusCode];
|
||||
|
||||
this.emit('response', stream);
|
||||
this.response = stream;
|
||||
|
||||
req.on('data', (chunk) => {
|
||||
if (!stream.push(chunk))
|
||||
req.pause();
|
||||
});
|
||||
|
||||
req.once('end', () => {
|
||||
stream.push(null);
|
||||
client.destroy();
|
||||
});
|
||||
|
||||
stream.once('error', (err) => {
|
||||
stream.statusCode = 400;
|
||||
stream.status = err.message;
|
||||
});
|
||||
});
|
||||
|
||||
req.end(data);
|
||||
|
||||
return req;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function request(options) {
|
||||
return new Http2Request(options);
|
||||
}
|
||||
|
||||
module.exports = { request };
|
9
node_modules/snekfetch/src/qs_mock.js
generated
vendored
Normal file
9
node_modules/snekfetch/src/qs_mock.js
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
exports = {
|
||||
parse: (str) => {
|
||||
const parsed = {};
|
||||
for (const [k, v] of new Window.URLSearchParams(str).entries())
|
||||
parsed[k] = v;
|
||||
return parsed;
|
||||
},
|
||||
stringify: (obj) => new window.URLSearchParams(obj).toString(),
|
||||
};
|
Reference in New Issue
Block a user