Skip to content
This repository was archived by the owner on Mar 13, 2021. It is now read-only.

Commit c7bc08c

Browse files
committed
Add typescript sample function
Also add support for transpiled ES module interop. Proper ES Module support is still off on the horizon, but we can handle the common method for ES Modules that have been transpiled to CommonJS Modules.
1 parent 46d5255 commit c7bc08c

File tree

10 files changed

+184
-2
lines changed

10 files changed

+184
-2
lines changed

node-invoker.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ spec:
1818
{{ if .FileExists "package.json" -}}
1919
ENV FUNCTION_URI /functions/
2020
COPY . ${FUNCTION_URI}
21-
RUN (cd ${FUNCTION_URI} && npm install --production)
21+
RUN (cd ${FUNCTION_URI} && npm install --production --unsafe-perm)
2222
{{- else -}}
2323
ENV FUNCTION_URI /functions/{{.Artifact}}
2424
ADD {{.ArtifactBase}} ${FUNCTION_URI}

samples/typescript/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
!.gitignore
3+
!/lib
4+
!package*.json
5+
!tsconfig.json
6+
!README.*

samples/typescript/README.adoc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
= typescript
2+
3+
[horizontal]
4+
*Invoker*:: Node
5+
*Function*:: link:lib/greeter.ts[greeter.ts]
6+
*Input*:: greeter
7+
8+
=== prerequisites
9+
10+
1. riff is deployed
11+
2. the `riff` CLI is on your PATH
12+
3. the node invoker is installed
13+
14+
riff invokers apply -f https://github.com/projectriff/node-function-invoker/raw/master/node-invoker.yaml
15+
16+
4. the working directory is `samples/typescript`
17+
18+
=== how it works
19+
20+
When a node function contains a package.json file, the package dependencies are installed while
21+
building the function into a Docker container. This function installs typescript as a dependency
22+
and then uses a post install script to run the typescript compiler. Once the typescript is
23+
transpiled, the function behavaes as a standard JS function.
24+
25+
=== create the function and its input topic
26+
27+
```
28+
riff create node -i greeter
29+
```
30+
31+
=== publish a message and wait for a reply
32+
33+
```
34+
riff publish -i greeter -d "riff user" -r
35+
```
36+
37+
=== delete the function and its input topic
38+
39+
```
40+
riff delete --all
41+
```

samples/typescript/lib/greeter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
export default function greeter(name: string) : string {
3+
return `Hello ${name}!`;
4+
}

samples/typescript/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/typescript/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@projectriff/typescript-sample",
3+
"version": "1.0.0",
4+
"description": "Typescript greeting sample",
5+
"main": "dist/greeter.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"install": "npm run build",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"author": "Scott Andrews",
12+
"license": "Apache-2.0",
13+
"dependencies": {
14+
"typescript": "^2.8.1"
15+
}
16+
}

samples/typescript/tsconfig.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
// "lib": [], /* Specify library files to be included in the compilation. */
7+
// "allowJs": true, /* Allow javascript files to be compiled. */
8+
// "checkJs": true, /* Report errors in .js files. */
9+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
11+
// "sourceMap": true, /* Generates corresponding '.map' file. */
12+
// "outFile": "./", /* Concatenate and emit output to single file. */
13+
"outDir": "./dist", /* Redirect output structure to the directory. */
14+
"rootDir": "./lib", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
15+
// "removeComments": true, /* Do not emit comments to output. */
16+
// "noEmit": true, /* Do not emit outputs. */
17+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
18+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
19+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
20+
21+
/* Strict Type-Checking Options */
22+
"strict": true, /* Enable all strict type-checking options. */
23+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
24+
// "strictNullChecks": true, /* Enable strict null checks. */
25+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
26+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
27+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
28+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
29+
30+
/* Additional Checks */
31+
// "noUnusedLocals": true, /* Report errors on unused locals. */
32+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
33+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
34+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
35+
36+
/* Module Resolution Options */
37+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
38+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
39+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
40+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
41+
// "typeRoots": [], /* List of folders to include type definitions from. */
42+
// "types": [], /* Type declaration files to be included in compilation. */
43+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
44+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
45+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
46+
47+
/* Source Map Options */
48+
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
49+
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
50+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
51+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
52+
53+
/* Experimental Options */
54+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
55+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
56+
}
57+
}

server.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ const { Message } = require('@projectriff/message');
2424
// must be run before the function is required
2525
Message.install();
2626

27-
const fn = require(FUNCTION_URI);
27+
const fn = (fn => {
28+
if (fn.__esModule && typeof fn.default === 'function') {
29+
// transpiled ES Module interop
30+
return fn.default;
31+
}
32+
return fn;
33+
})(require(FUNCTION_URI));
2834

2935
let grpcServer, httpServer;
3036

spec/server.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ describe('server', () => {
127127
expect(await exitCode).toBe(0);
128128
});
129129

130+
it('runs the echo-esmodule-transpile function', async () => {
131+
const server = createServerProcess('echo-esmodule-transpile');
132+
133+
const exitCode = new Promise(resolve => {
134+
server.on('exit', resolve);
135+
});
136+
137+
await waitForServer();
138+
139+
const { payload, headers } = await requestReplyCall(
140+
Message.builder()
141+
.addHeader('Content-Type', 'text/plain')
142+
.addHeader('Accept', 'text/plain')
143+
.payload('riff')
144+
.build()
145+
);
146+
expect(headers.getValue('Error')).toBeNull();
147+
expect(headers.getValue('Content-Type')).toEqual('text/plain');
148+
expect(payload.toString()).toEqual('riff');
149+
150+
server.kill('SIGINT');
151+
expect(await exitCode).toBe(0);
152+
});
153+
130154
it('runs the uppercase-payload function', async () => {
131155
const server = createServerProcess('uppercase-payload');
132156

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
/*
4+
// ES Module
5+
export default function echo(value) {
6+
return value;
7+
}
8+
*/
9+
10+
// CommonJS Module interop transpile
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
function echo(message) {
13+
return message;
14+
}
15+
exports.default = echo;

0 commit comments

Comments
 (0)