Exemplo n.º 1
0
		return startup.then(function(main){
			var request = typeof requestOrUrl === "string" ?
				{ url: requestOrUrl } : requestOrUrl;

			// Create the document
			var doc = new document.constructor();

			addCookies(doc, request);

			var serializeFromBody = !!(main.renderAsync ||
									   main.serializeFromBody);
			if(!serializeFromBody) {
				doc.head = doc.createElement("head");
				doc.documentElement.insertBefore(doc.head, doc.body);
			}
			var render = makeRender(main);

			var zonePlugins = [
				ssrGlobalsZone(doc, request, loader),
				canRouteDataZone,
				assetsZone(doc, bundleHelpers),
				responseZone(stream)
			];

			if(typeof XMLHttpRequest !== "undefined") {
				zonePlugins.push(xhrZone);
			}

			if(options.html5shiv) {
				zonePlugins.push(html5shivZone);
			}

			var zone = new Zone({
				plugins: zonePlugins
			});

			return zone.run(function(){

				render(request);

			}).then(function(data){
				var html;
				if(serializeFromBody) {
					html = doc.body.innerHTML;
				} else {
					html = doc.documentElement.outerHTML;
				}

				// Cleanup the dom
				trigger(doc, "removed");

				var dt = cfg.doctype || doctype;
				html = dt + "\n" + html;

				stream.push(html);
				stream.push(null);
			}, function(error){
				stream.emit("error", error);
			});
		});
Exemplo n.º 2
0
		Zone.ignore(function(){
			zone.run(function(){
				d.decrement();
				d.increment();
			})
			.then(function(){
				setTimeout(function(){
					addResult("worked");
				}, 300);
			}, function(err){
				addResult("failed");
			});
		})();
Exemplo n.º 3
0
	it.skip('should fail', function(done) {
		// override funcunit timeout so this test breaks fast.
		F.timeout = 500;

		var myZone = new Zone();

		var promise = myZone.run(function() {
			F('.clickresult').text('clickedz', 'clicked the link');
		});

		promise.then(function() {
			assert(false, '.clickresult text should not be "clickedz"');
		});

		promise.catch(function(error) {
			assert(error, 'F().text should fail');
			done();
		});
	});
Exemplo n.º 4
0
SafeStream.prototype.render = function(){
	var stream = this;
	var request = this.request;
	var response = this.response;

	var zones = [
		requests(request, this.options),
		this.options.domZone(request, response)
	];

	if(this.options.steal !== false) {
		var donejs = require("../zones/donejs");
		zones.push(donejs(this.options.steal, response));
	}

	zones.push(cookies(request, response));

	var timeoutMs = this.options.timeout;
	var timeoutZone = timeout(timeoutMs);
	zones.push(timeoutZone);

	if(this.options.debug) {
		zones.push(debug(timeoutZone));
	}

	var incremental = this.options.strategy === "incremental" &&
		supportsIncremental(request);

	if(incremental) {
		zones.push(pushMutations(request, response, undefined, this.options.streamMap));
		zones.push(pushFetch(response));
		zones.push(pushXHR(response));
	}

	if(this.options.zones) {
		this.options.zones.forEach(zone => zones.push(zone));
	}

	// Make sure the `ready` process is caught, to prevent unhandledRejection
	zones.push({
		created: function(){
			if(this.data.ready) {
				this.data.ready.catch(err => {});
			}
		}
	});

	var zone = new Zone(zones);

	var runFn = this.options.fn || void 0;
	var runPromise = zone.run(runFn);

	if(incremental) {
		var donePromise = runPromise;
		var stylePromise = Promise.resolve(zone.data.initialStylesLoaded);
		runPromise = stylePromise.then(function(){
			// Send the initial HTML.
			var html = doctype + "\n" + zone.data.html;

			stream.push(html);
			stream.push(null);

			return donePromise;
		}).catch(function(err) {
			if(err instanceof TimeoutError) {
				if(zone.data.debugInfo) {
					for(let item of zone.data.debugInfo) {
						console.error(item.stack);
					}
				}
			}
			throw err;
		});
	}
	else {
		runPromise = runPromise.then(null, function(err){
			if(!(err instanceof TimeoutError)) {
				throw err;
			}
			console.error("A timeout of", timeoutMs + "ms", "was exceeded. See https://github.com/donejs/done-ssr#timeout--5000 for more information on timeouts.");
			return zone.data;
		}).then(function(data){
			var html = doctype + "\n" + data.html;

			stream.push(html);
			stream.push(null);
		});
	}

	return runPromise.catch(function(error){
		stream.emit("error", error);
	});
};