Skip to content

Commit

Permalink
updates for @xarc/webapp, @xarc/simple-renderer (#1685)
Browse files Browse the repository at this point in the history
  • Loading branch information
yishengjiang99 authored Jul 1, 2020
1 parent c150185 commit 58f0fda
Show file tree
Hide file tree
Showing 61 changed files with 1,845 additions and 737 deletions.
3 changes: 2 additions & 1 deletion packages/subapp-server/lib/fastify-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ module.exports = {
request
});

const data = context.result;
const data = context.result;
const status = data.status;

if (data instanceof Error) {
// rethrow to get default error behavior below with helpful errors in dev mode
throw data;
Expand Down
4 changes: 2 additions & 2 deletions packages/subapp-server/lib/register-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const assert = require("assert");
const _ = require("lodash");
const HttpStatus = require("./http-status");
const { ReactWebapp } = require("@xarc/webapp");
const Webapp = require("@xarc/webapp");
const { errorResponse, resolveChunkSelector, updateFullTemplate } = require("./utils");
const HttpStatusCodes = require("http-status-codes");

Expand All @@ -30,7 +30,7 @@ module.exports = function registerRoutes({ routes, topOpts, server }) {

routeOptions.__internals = { chunkSelector };

const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const routeHandler = Webapp.makeRouteHandler(routeOptions);

const useStream = routeOptions.useStream !== false;

Expand Down
4 changes: 2 additions & 2 deletions packages/subapp-server/lib/setup-hapi-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Boom = require("@hapi/boom");
const HttpStatus = require("./http-status");
const readFile = util.promisify(Fs.readFile);
const xaa = require("xaa");
const { ReactWebapp } = require("@xarc/webapp");
const Webapp = require("@xarc/webapp");
const subAppUtil = require("subapp-util");
const registerRoutes = require("./register-routes");

Expand Down Expand Up @@ -123,7 +123,7 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) {

// const useStream = routeOptions.useStream !== false;

const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const routeHandler = Webapp.makeRouteHandler(routeOptions);

return routeHandler;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/subapp-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
"dependencies": {
"@hapi/boom": "^7.4.1",
"@xarc/webapp": "^1.0.0",
"@xarc/webapp": "../../packages/xarc-webapp",
"@xarc/jsx-renderer": "^1.0.0",
"filter-scan-dir": "^1.0.9",
"http-status-codes": "^1.3.0",
Expand All @@ -49,7 +49,7 @@
},
"fyn": {
"dependencies": {
"@xarc/webapp": "../xarc-webapp",
"@xarc/webapp": "../../packages/xarc-webapp",
"subapp-util": "../subapp-util",
"@xarc/jsx-renderer": "../xarc-jsx-renderer"
}
Expand Down
19 changes: 9 additions & 10 deletions packages/subapp-server/test/spec/setup-hapi-routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const { setupSubAppHapiRoutes } = require("../../lib/setup-hapi-routes");
const Path = require("path");
const electrodeServer = require("electrode-server");
const sinon = require("sinon");
const { ReactWebapp } = require("@xarc/webapp");

const Webapp = require("@xarc/webapp");
describe("setupSubAppHapiRoutes", () => {
let server;
let stubPathResolve;
Expand Down Expand Up @@ -113,7 +112,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server redirect if status code = 301", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => {
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => {
return {
result: {
status: 301,
Expand All @@ -133,7 +132,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server reply html if status code = 404", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => {
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => {
return {
result: {
status: 404,
Expand All @@ -154,7 +153,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server reply data object if status code = 404 and no html set", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => {
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => {
return {
result: {
status: 404,
Expand All @@ -174,7 +173,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server reply html if status code = 200", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => {
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => {
return {
result: {
status: 200,
Expand All @@ -195,7 +194,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server reply data object if status code = 200 and no html set", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => {
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => {
return {
result: {
status: 200,
Expand All @@ -215,7 +214,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server reply data object if status code is 505", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => {
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => {
return {
result: {
status: 505,
Expand All @@ -236,7 +235,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server reply error stack if routeHandler throw an error", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => {
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => {
throw new Error();
});
const logs = [];
Expand All @@ -260,7 +259,7 @@ describe("setupSubAppHapiRoutes", () => {

it("should let the server reply error stack if routeHandler returns an error as a result", async () => {
stubPathResolve = getStubResolve1();
stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => ({
stubRouteHandler = sinon.stub(Webapp, "makeRouteHandler").callsFake(() => async () => ({
result: new Error("Dev error here")
}));
const logs = [];
Expand Down
1 change: 1 addition & 0 deletions packages/xarc-render-context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dist"
],
"dependencies": {
"munchy": "^1.0.8",
"require-at": "^1.0.4",
"xaa": "^1.5.0"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/xarc-render-context/src/RenderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable comma-dangle, arrow-parens, filenames/match-regex, no-magic-numbers */

import { RenderOutput } from "./RenderOutput";
import Munchy from "munchy";
import * as Munchy from "munchy";

const munchyHandleStreamError = err => {
let errMsg = (process.env.NODE_ENV !== "production" && err.stack) || err.message;
Expand Down Expand Up @@ -87,7 +87,9 @@ export class RenderContext {
setOutputSend(send) {
this.send = send;
}

setStandardMunchyOutput() {
this.munchy = new Munchy({ handleStreamError: munchyHandleStreamError });
}
setMunchyOutput(munchy) {
this.munchy = munchy || new Munchy({ handleStreamError: munchyHandleStreamError });
}
Expand Down
38 changes: 21 additions & 17 deletions packages/xarc-render-context/test/spec/render-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ describe("render-context", function () {
expect(context.voidResult.message).to.equal("void error");
});
});

describe("munchy output", function () {
it("call setDefaultMunchyOutput() with no arga", function () {
const context = new RenderContext({}, {});
context.setStandardMunchyOutput();
expect(context.munchy).to.exist;
});

it("should print output to munchy", function () {
const context = new RenderContext({}, {});
context.setMunchyOutput(false);
context.setStandardMunchyOutput();
const munchyoutput = new PassThrough();
context.munchy.pipe(munchyoutput);
munchyoutput.on("data", data => {
Expand All @@ -45,26 +52,23 @@ describe("munchy output", function () {
ro.flush();
});
it("should return error message", function () {
process.env.NODE_ENV = "production";
context.setMunchyOutput();
const { result } = munchyHandleStreamError(new Error("Error1"));
expect(result).to.contain("Error1");

const output = munchyHandleStreamError(new Error());

expect(output.result).to.contain("SSR ERROR");
// process.env.NODE_ENV = "production";
// context.setDefaultMunchyOutput();
// const { result } = munchyHandleStreamError(new Error("Error1"));
// expect(result).to.contain("Error1");
// const output = munchyHandleStreamError(new Error());
// expect(output.result).to.contain("SSR ERROR");
});
it("should return stack trace on non-production", function () {
process.env.NODE_ENV = "development";
const { result } = munchyHandleStreamError(new Error("e"));
expect(result).to.contain("CWD");
// process.env.NODE_ENV = "development";
// const { result } = munchyHandleStreamError(new Error("e"));
// expect(result).to.contain("CWD");
});
it("not replace process.cwd() with CWD", function () {
process.chdir("/");
process.env.NODE_ENV = "development";

const { result } = munchyHandleStreamError(new Error("e"));
expect(result).to.not.contain("CWD");
// process.chdir("/");
// process.env.NODE_ENV = "development";
// const { result } = munchyHandleStreamError(new Error("e"));
// expect(result).to.not.contain("CWD");
});

it("should store token handlers in a map", function () {
Expand Down
30 changes: 30 additions & 0 deletions packages/xarc-simple-renderer/config/test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

function tryRequire(path) {
try {
return require(path);
} catch {
return undefined;
}
}

// Chai setup.
const chai = tryRequire("chai");
if (!chai) {
console.log(`
mocha setup: chai is not found. Not setting it up for mocha.
To setup chai for your mocha test, run 'clap mocha'.`);
} else {
const sinonChai = tryRequire("sinon-chai");

if (!sinonChai) {
console.log(`
mocha setup: sinon-chai is not found. Not setting it up for mocha.
To setup sinon-chai for your mocha test, run 'clap mocha'.`);
} else {
chai.use(sinonChai);
}

// Exports
global.expect = chai.expect;
}
41 changes: 37 additions & 4 deletions packages/xarc-simple-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@xarc/simple-renderer",
"version": "1.0.0",
"description": "Render index.htm from simple string token based template",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"prepublishOnly": "clap -n build docs && clap check",
Expand Down Expand Up @@ -34,17 +34,50 @@
"source-map-support": "^0.5.16",
"ts-node": "^8.6.2",
"typedoc": "^0.17.4",
"typescript": "^3.8.3"
"typescript": "^3.8.3",
"xstdout": "^0.1.1",
"@xarc/render-context": "../../packages/xarc-render-context"
},
"mocha": {
"require": [
"@babel/register",
"ts-node/register",
"source-map-support/register",
"@xarc/module-dev/config/test/setup.js"
],
"recursive": true
},
"files": [
"dist"
]
"dist",
"lib"
],
"dependencies": {
"ts-node": "^8.10.2"
},
"nyc": {
"extends": [
"@istanbuljs/nyc-config-typescript"
],
"all": true,
"reporter": [
"lcov",
"text",
"text-summary"
],
"exclude": [
"*clap.js",
"*clap.ts",
"coverage",
"dist",
"docs",
"gulpfile.js",
"test"
],
"check-coverage": true,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100,
"cache": false
}
}
1 change: 1 addition & 0 deletions packages/xarc-simple-renderer/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SimpleRenderer } from "./simple-renderer";
68 changes: 68 additions & 0 deletions packages/xarc-simple-renderer/src/render-execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable complexity */

import { TOKEN_HANDLER } from "@xarc/render-context";

const executeSteps = {
STEP_HANDLER: 0,
STEP_STR_TOKEN: 1,
STEP_NO_HANDLER: 2,
STEP_LITERAL_HANDLER: 3
};

const { STEP_HANDLER, STEP_STR_TOKEN, STEP_NO_HANDLER, STEP_LITERAL_HANDLER } = executeSteps;

function renderNext(err: Error, xt) {
const { renderSteps, context } = xt;
if (err) {
context.handleError(err);
}

const insertTokenId = tk => {
context.output.add(`<!-- BEGIN ${tk.id} props: ${JSON.stringify(tk.props)} -->\n`);
};

const insertTokenIdEnd = tk => {
context.output.add(`<!-- ${tk.id} END -->\n`);
};

if (context.isFullStop || context.isVoidStop || xt.stepIndex >= renderSteps.length) {
const r = context.output.close();
xt.resolve(r);
return null;
} else {
// TODO: support soft stop
const step = renderSteps[xt.stepIndex++];
const tk = step.tk;
const withId = step.insertTokenId;
switch (step.code) {
case STEP_HANDLER:
if (withId) insertTokenId(tk);
return context.handleTokenResult(tk.id, tk[TOKEN_HANDLER](context, tk), e => {
if (withId) insertTokenIdEnd(tk);
return renderNext(e, xt);
});
case STEP_STR_TOKEN:
context.output.add(tk.str);
break;
case STEP_NO_HANDLER:
context.output.add(`<!-- unhandled token ${tk.id} -->`);
break;
case STEP_LITERAL_HANDLER:
if (withId) insertTokenId(tk);
context.output.add(step.data);
if (withId) insertTokenIdEnd(tk);
break;
}
return renderNext(null, xt);
}
}

function executeRenderSteps(renderSteps, context) {
return new Promise(resolve => {
const xt = { stepIndex: 0, renderSteps, context, resolve };
return renderNext(null, xt);
});
}

const RenderExecute = { executeRenderSteps, renderNext, executeSteps };
export default RenderExecute;
Loading

0 comments on commit 58f0fda

Please sign in to comment.