-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fei4960.4.examples] Migrate examples (#611)
## Summary: This migrates the various test examples from Khan/render-gateway and adds the infrastructure to run them (i.e. `babel-watch`). Issue: FEI-4960 ## Test plan: `yarn typecheck` `yarn build` `yarn build:types` `yarn --cwd packages/wonder-stuff-render-environment-jsdom example jsdom-simple` `yarn --cwd packages/wonder-stuff-render-server example simple` `yarn --cwd packages/wonder-stuff-render-server example error` `yarn --cwd packages/wonder-stuff-render-server example logging` Author: somewhatabstract Reviewers: somewhatabstract, jeresig Required Reviewers: Approved By: jeresig Checks: ✅ codecov/project, ✅ Test (macos-latest, 16.x), ✅ CodeQL, ✅ Lint, typecheck, and coverage check (ubuntu-latest, 16.x), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 16.x), ✅ gerald, ✅ Analyze (javascript), ⏭ dependabot Pull Request URL: #611
- Loading branch information
1 parent
a13f9d6
commit 7a3ba71
Showing
18 changed files
with
286 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ | |
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
} | ||
}, | ||
"references": [ | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ | |
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
} | ||
}, | ||
"references": [ | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/wonder-stuff-render-environment-jsdom/examples/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Examples | ||
|
||
Each folder in this location must contain a file named `run.ts` that is | ||
responsible for executing the given example. To execute an example, you can | ||
run the following command from the root folder, where `<EXAMPLE>` is the name | ||
of the example folder. | ||
|
||
```shell | ||
yarn example <EXAMPLE> | ||
``` |
10 changes: 10 additions & 0 deletions
10
packages/wonder-stuff-render-environment-jsdom/examples/jsdom-simple/render.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @ts-expect-error This is what we want and I don't get why TS doesn't like it | ||
window["__jsdom_env_register"](() => { | ||
// This is where we can return our result. | ||
return Promise.resolve({ | ||
// @ts-expect-error We know that this does exist off window. | ||
body: `You asked us to render ${window._API.url}`, | ||
status: 200, | ||
headers: {}, | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/wonder-stuff-render-environment-jsdom/examples/jsdom-simple/run.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* This is a simple JSDOM-based server. | ||
*/ | ||
|
||
/** | ||
* NOTE: We import everything from index.js to ensure we're testing the public | ||
* interface of our package. | ||
*/ | ||
import type vm from "vm"; | ||
import {runServer} from "@khanacademy/wonder-stuff-render-server"; | ||
import type { | ||
RenderAPI, | ||
ICloseable, | ||
} from "@khanacademy/wonder-stuff-render-server"; | ||
import * as JSDOM from "../../src/index"; | ||
|
||
async function main() { | ||
const config = new JSDOM.Configuration( | ||
() => Promise.resolve(["http://localhost:8080/render.ts"]), | ||
(url: string, renderAPI: RenderAPI) => | ||
new JSDOM.FileResourceLoader(__dirname), | ||
( | ||
url: string, | ||
fileURLs: ReadonlyArray<string>, | ||
renderAPI: RenderAPI, | ||
vmContext: vm.Context, | ||
): Promise<ICloseable | null | undefined> => { | ||
vmContext._renderAPI = renderAPI; | ||
vmContext._API = { | ||
url, | ||
renderAPI, | ||
fileURLs, | ||
}; | ||
return Promise.resolve(null); | ||
}, | ||
); | ||
const renderEnvironment = new JSDOM.Environment(config); | ||
|
||
runServer({ | ||
name: "DEV_LOCAL", | ||
port: 8080, | ||
host: "127.0.0.1", | ||
renderEnvironment, | ||
}); | ||
} | ||
|
||
main().catch((err) => { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error caught from main setup: ${err}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Examples | ||
|
||
Each folder in this location must contain a file named `run.ts` that is | ||
responsible for executing the given example. To execute an example, you can | ||
run the following command from the root folder, where `<EXAMPLE>` is the name | ||
of the example folder. | ||
|
||
```shell | ||
yarn example <EXAMPLE> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* This is a simple local server for testing what happens if rendering errors. | ||
*/ | ||
|
||
/** | ||
* NOTE: We import everything from index.js to ensure we're testing the public | ||
* interface of this package. | ||
*/ | ||
import {runServer} from "../../src/index"; | ||
import type {RenderAPI, RenderResult} from "../../src/index"; | ||
|
||
async function main() { | ||
const renderEnvironment = { | ||
render: (url: string, renderAPI: RenderAPI): Promise<RenderResult> => | ||
Promise.reject(new Error(`OH NO! We couldn't render ${url}`)), | ||
}; | ||
|
||
runServer({ | ||
name: "DEV_LOCAL", | ||
port: 8080, | ||
host: "127.0.0.1", | ||
renderEnvironment, | ||
}); | ||
} | ||
|
||
main().catch((err) => { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error caught from main setup: ${err}`); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/wonder-stuff-render-server/examples/logging/run.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* This is a simple local server for testing that logging works in a request | ||
* context. | ||
*/ | ||
|
||
/** | ||
* NOTE: We import everything from index.js to ensure we're testing the public | ||
* interface of this package. | ||
*/ | ||
import {runServer} from "../../src/index"; | ||
import type {RenderAPI, RenderResult} from "../../src/index"; | ||
|
||
async function main() { | ||
const renderEnvironment = { | ||
render: (url: string, renderAPI: RenderAPI): Promise<RenderResult> => { | ||
const traceSession = renderAPI.trace( | ||
"LOGGING", | ||
"Testing logging things", | ||
); | ||
try { | ||
renderAPI.logger.silly("A silly log", {with: "metadata"}); | ||
renderAPI.logger.debug("A debug log", {with: "metadata"}); | ||
renderAPI.logger.info("An info log", {with: "metadata"}); | ||
renderAPI.logger.warn("A warning", {with: "metadata"}); | ||
renderAPI.logger.error("An error", {with: "metadata"}); | ||
|
||
return Promise.resolve({ | ||
body: `You asked us to render ${url}`, | ||
status: 200, | ||
headers: {}, | ||
}); | ||
} finally { | ||
traceSession.end({ | ||
with: "metadata", | ||
}); | ||
} | ||
}, | ||
}; | ||
|
||
runServer({ | ||
name: "DEV_LOCAL", | ||
port: 8080, | ||
host: "127.0.0.1", | ||
renderEnvironment, | ||
}); | ||
} | ||
|
||
main().catch((err) => { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error caught from main setup: ${err}`); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/wonder-stuff-render-server/examples/simple/run.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* This is a simple local server for testing this code works. | ||
*/ | ||
|
||
/** | ||
* NOTE: We import everything from index.js to ensure we're testing the public | ||
* interface of this package. | ||
*/ | ||
import {runServer} from "../../src/index"; | ||
import type {RenderAPI, RenderResult} from "../../src/index"; | ||
|
||
async function main() { | ||
const renderEnvironment = { | ||
render: (url: string, renderAPI: RenderAPI): Promise<RenderResult> => | ||
Promise.resolve({ | ||
body: `You asked us to render ${url}`, | ||
status: 200, | ||
headers: {}, | ||
}), | ||
}; | ||
|
||
runServer({ | ||
name: "DEV_LOCAL", | ||
port: 8080, | ||
host: "127.0.0.1", | ||
renderEnvironment, | ||
}); | ||
} | ||
|
||
main().catch((err) => { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error caught from main setup: ${err}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"exclude": [ | ||
"dist" | ||
"dist", "examples" | ||
], | ||
"extends": "../tsconfig-shared.json", | ||
"compilerOptions": { | ||
|
Oops, something went wrong.