Skip to content

Commit

Permalink
Use custom user-agent for all requests in Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Jul 21, 2024
1 parent 81a79c4 commit 19b99b8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ const downloadScratch2 = async (projectData, zip, options) => {
const fetchAndStoreAsset = async (md5ext, id) => {
progressTarget.fetching(md5ext);
// assetHost will never be undefined here because of parseOptions()
const arrayBuffer = await fetchAsArrayBuffer(options.assetHost.replace('$id', md5ext))
const arrayBuffer = await fetchAsArrayBuffer(options.assetHost.replace('$id', md5ext), {
headers: environment.headers
});
const path = `${id}.${getExtension(md5ext)}`;
progressTarget.fetched(md5ext);
return {
Expand Down Expand Up @@ -407,7 +409,8 @@ const downloadScratch3 = async (projectData, zip, options) => {

// assetHost will never be undefined here because of parseOptions()
const buffer = await fetchAsArrayBuffer(options.assetHost.replace('$id', md5ext), {
signal: options.signal
signal: options.signal,
headers: environment.headers
});

progressTarget.fetched(md5ext);
Expand Down Expand Up @@ -674,7 +677,8 @@ export const getProjectMetadata = async (id, options) => {
for (const url of urls) {
try {
const response = await fetch(url, {
signal: options.signal
signal: options.signal,
headers: environment.headers
});
if (response.status === 404) {
throw new CanNotAccessProjectError(`${id} is unshared or does not exist`);
Expand Down
7 changes: 6 additions & 1 deletion src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const environment = {
* Whether the module is able to directly talk to api.scratch.mit.edu or if a middleman
* is required due to CORS.
*/
canAccessScratchAPI: false
canAccessScratchAPI: false,

/**
* Headers used when fetching remote APIs.
*/
headers: {}
};

export default environment;
3 changes: 3 additions & 0 deletions src/export-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import environment from './environment.js';
import {name, version} from '../package.json';

environment.canAccessScratchAPI = true;
environment.headers['user-agent'] = `SBDL/${version} (+https://www.npmjs.com/package/${name})`;

export * from './downloader.js';
5 changes: 2 additions & 3 deletions src/fetch-with-progress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetch from 'cross-fetch';
import {AbortError, HTTPError} from './errors.js';
import environment from './environment.js';

/**
* @param {stirng} url
Expand Down Expand Up @@ -47,9 +48,7 @@ const fetchAsArrayBufferWithProgress = async (url, progressCallback, abortSignal

// Running in Node.js
const response = await fetch(url, {
headers: {
'user-agent': 'SBDL/1.0 (+https://www.npmjs.com/package/@turbowarp/sbdl)'
},
headers: environment.headers
});
if (response.status !== 200) {
throw new HTTPError(url, response.status);
Expand Down

0 comments on commit 19b99b8

Please sign in to comment.