<commit_before>/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave.
 *
 * The Initial Developer of Weave is the Institute for Visualization
 * and Perception Research at the University of Massachusetts Lowell.
 * Portions created by the Initial Developer are Copyright (C) 2008-2015
 * the Initial Developer. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * ***** END LICENSE BLOCK ***** */

package weavejs.net
{
	import weavejs.WeaveAPI;
	import weavejs.api.core.ILinkableHashMap;
	import weavejs.api.core.ILinkableObject;
	import weavejs.api.net.IURLRequestUtils;
	import weavejs.util.Dictionary2D;
	import weavejs.util.JS;
	import weavejs.util.WeavePromise;

	public class URLRequestUtils implements IURLRequestUtils
	{
		private function byteArrayToDataUri(byteArray:/*Uint8*/Array, mimeType:String):String
		{
			return "data:" + (mimeType || '') + ';base64,' + JS.global.btoa(byteArrayToString(byteArray));
		}
		
		private function byteArrayToString(byteArray:/*Uint8*/Array):String
		{
			var CHUNK_SIZE:int = 8192;
			var n:int = byteArray.length;
			if (n <= CHUNK_SIZE)
				return String.fromCharCode.apply(String, byteArray);
			var strings:Array = [];
			for (var i:int = 0; i < byteArray.length;)
				strings.push(String.fromCharCode.apply(null, byteArray.subarray(i, i += CHUNK_SIZE)));
			return strings.join('');
		}
		
		public function request(relevantContext:Object, urlRequest:URLRequest):WeavePromise
		{
			var responseType:String = urlRequest.responseType || ResponseType.UINT8ARRAY;
			var promise:WeavePromise;
			
			if (urlRequest.url.indexOf(LOCAL_FILE_URL_SCHEME) == 0)
			{
				var fileName:String = urlRequest.url.substr(LOCAL_FILE_URL_SCHEME.length);
				var weaveRoot:ILinkableHashMap = Weave.getRoot(relevantContext as ILinkableObject);
				var cachedPromise:WeavePromise = get_d2d_weaveRoot_fileName_promise(weaveRoot, fileName);
				if (cachedPromise.getResult() == null && cachedPromise.getError() == null)
				{
					if (weaveRoot)
						removeLocalFile(weaveRoot, fileName);
					else
						cachedPromise.setError(new Error(Weave.lang("To request a " + LOCAL_FILE_URL_SCHEME + " URL, the relevantContext must be an ILinkableObject registered under an instance of Weave.")));
				}
				promise = new WeavePromise(relevantContext)
					.setResult(cachedPromise)
					.then(function(byteArray:/*Uint8*/Array):Object {
						switch (responseType) {
							default:
							case ResponseType.TEXT:
								return byteArrayToString(byteArray);<commit_msg>added string cast for url<commit_after>/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave.
 *
 * The Initial Developer of Weave is the Institute for Visualization
 * and Perception Research at the University of Massachusetts Lowell.
 * Portions created by the Initial Developer are Copyright (C) 2008-2015
 * the Initial Developer. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * ***** END LICENSE BLOCK ***** */

package weavejs.net
{
	import weavejs.WeaveAPI;
	import weavejs.api.core.ILinkableHashMap;
	import weavejs.api.core.ILinkableObject;
	import weavejs.api.net.IURLRequestUtils;
	import weavejs.util.Dictionary2D;
	import weavejs.util.JS;
	import weavejs.util.WeavePromise;

	public class URLRequestUtils implements IURLRequestUtils
	{
		private function byteArrayToDataUri(byteArray:/*Uint8*/Array, mimeType:String):String
		{
			return "data:" + (mimeType || '') + ';base64,' + JS.global.btoa(byteArrayToString(byteArray));
		}
		
		private function byteArrayToString(byteArray:/*Uint8*/Array):String
		{
			var CHUNK_SIZE:int = 8192;
			var n:int = byteArray.length;
			if (n <= CHUNK_SIZE)
				return String.fromCharCode.apply(String, byteArray);
			var strings:Array = [];
			for (var i:int = 0; i < byteArray.length;)
				strings.push(String.fromCharCode.apply(null, byteArray.subarray(i, i += CHUNK_SIZE)));
			return strings.join('');
		}
		
		public function request(relevantContext:Object, urlRequest:URLRequest):WeavePromise
		{
			var responseType:String = urlRequest.responseType || ResponseType.UINT8ARRAY;
			var promise:WeavePromise;
			
			if (String(urlRequest.url).indexOf(LOCAL_FILE_URL_SCHEME) == 0)
			{
				var fileName:String = String(urlRequest.url).substr(LOCAL_FILE_URL_SCHEME.length);
				var weaveRoot:ILinkableHashMap = Weave.getRoot(relevantContext as ILinkableObject);
				var cachedPromise:WeavePromise = get_d2d_weaveRoot_fileName_promise(weaveRoot, fileName);
				if (cachedPromise.getResult() == null && cachedPromise.getError() == null)
				{
					if (weaveRoot)
						removeLocalFile(weaveRoot, fileName);
					else
						cachedPromise.setError(new Error(Weave.lang("To request a " + LOCAL_FILE_URL_SCHEME + " URL, the relevantContext must be an ILinkableObject registered under an instance of Weave.")));
				}
				promise = new WeavePromise(relevantContext)
					.setResult(cachedPromise)
					.then(function(byteArray:/*Uint8*/Array):Object {
						switch (responseType) {
							default:
							case ResponseType.TEXT:
								return byteArrayToString(byteArray);
{'fraction_non_alphanumeric': 0.10913799482056974, 'fraction_numerical': 0.009618941916389198, 'mean_word_length': 4.275121951219512, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1874973', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1678, 'n_words': 415}