<commit_before>const cache = {};

export function getComponentFromGist ( id ) {
	let cancelled = false;

	if ( !cache[ id ] ) {
		cache[ id ] = fetch( `https://api.github.com/gists/${id}`, { mode: 'cors' })
			.then( r => r.json() )
			.then( gist => {
				const sourceFile = gist.files[ 'component.html' ];
				const source = sourceFile && sourceFile.content;

				const jsonFile = gist.files[ 'data.json' ];
				const json = jsonFile && jsonFile.content;

				return {
					id,
					source,
					json: json || '{}'
				};
			})
			.catch( err => {
				cache[ id ] = null;
				throw err;
			});
	}

	const promise = cache[ id ].then( component => {
		if ( cancelled ) throw new Error( `Request was cancelled` );
		return component;
	});

	promise.cancel = () => {
		cancelled = true;
	};

	return promise;
}

export function saveComponentAsGist ( source, json ) {
	const body = JSON.stringify({
		description: 'Svelte component',
		public: true,
		files: {
			'component.html': {
				content: source
			},
			'data.json': {
				content: json
			}
		}
	});

	return fetch( `https://api.github.com/gists`, { method: 'POST', body })
		.then( r => r.json() )
		.then( gist => gist.id );
}<commit_msg>use XMLHttpRequest instead of fetch fix (#10)<commit_after>const cache = {};

export function getComponentFromGist ( id ) {
	let cancelled = false;

	if ( !cache[ id ] ) {
		cache[ id ] = new Promise( ( resolve, reject ) => {
			const request = new XMLHttpRequest();
			request.open( 'GET', `https://api.github.com/gists/${id}` );
			request.onload = () => resolve( request );
			request.onerror = () => reject( new TypeError('Network request failed') );
			request.send();
		} )
			.then( r => JSON.parse(r.responseText) )
			.then( gist => {
				const sourceFile = gist.files[ 'component.html' ];
				const source = sourceFile && sourceFile.content;

				const jsonFile = gist.files[ 'data.json' ];
				const json = jsonFile && jsonFile.content;

				return {
					id,
					source,
					json: json || '{}'
				};
			})
			.catch( err => {
				cache[ id ] = null;
				throw err;
			});
	}

	const promise = cache[ id ].then( component => {
		if ( cancelled ) throw new Error( `Request was cancelled` );
		return component;
	});

	promise.cancel = () => {
		cancelled = true;
	};

	return promise;
}

export function saveComponentAsGist ( source, json ) {
	const body = JSON.stringify({
		description: 'Svelte component',
		public: true,
		files: {
			'component.html': {
				content: source
			},
			'data.json': {
				content: json
			}
		}
	});

	return new Promise( ( resolve, reject ) => {
		const request = new XMLHttpRequest();
		request.withCredentials = true;
		request.open( 'POST', `https://api.github.com/gists` );
		request.onload = () => resolve( request );
		request.onerror = () => reject( new TypeError('Network request failed') );
		request.send(body);
	} )
		.then( r => JSON.parse(r.responseText) )
		.then( gist => gist.id );
}
{'fraction_non_alphanumeric': 0.16943972835314092, 'fraction_numerical': 0.0006791171477079797, 'mean_word_length': 2.876315789473684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3673969', 'n_tokens_mistral': 1156, 'n_tokens_neox': 984, 'n_words': 249}