Skip to content

Commit d0a03c1

Browse files
refactor(editor): convert source files to ESM and update got to v12
1 parent c04e2f6 commit d0a03c1

4 files changed

Lines changed: 27 additions & 29 deletions

File tree

run/markdown-preview/editor/app.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const express = require('express');
16-
const handlebars = require('handlebars');
17-
const {readFile} = require('fs').promises;
18-
const renderRequest = require('./render.js');
15+
import express from 'express';
16+
import handlebars from 'handlebars';
17+
import { readFile } from 'fs/promises';
18+
import renderRequest from './render.js';
1919

2020
const app = express();
2121
app.use(express.json());
@@ -25,14 +25,13 @@ let markdownDefault, compiledTemplate, renderedHtml;
2525
// Load the template files and serve them with the Editor service.
2626
const buildRenderedHtml = async () => {
2727
try {
28-
markdownDefault = await readFile(__dirname + '/templates/markdown.md');
29-
compiledTemplate = handlebars.compile(
30-
await readFile(__dirname + '/templates/index.html', 'utf8')
31-
);
32-
renderedHtml = compiledTemplate({default: markdownDefault});
28+
markdownDefault = await readFile(new URL('./templates/markdown.md', import.meta.url));
29+
const indexTemplate = await readFile(new URL('./templates/index.html', import.meta.url), 'utf8');
30+
compiledTemplate = handlebars.compile(indexTemplate);
31+
renderedHtml = compiledTemplate({ default: markdownDefault });
3332
return renderedHtml;
3433
} catch (err) {
35-
throw Error('Error loading template: ', err);
34+
throw Error('Error loading template: ' + err);
3635
}
3736
};
3837

@@ -62,7 +61,4 @@ app.post('/render', async (req, res) => {
6261
// [END cloudrun_secure_request_do]
6362

6463
// Exports for testing purposes.
65-
module.exports = {
66-
app,
67-
buildRenderedHtml,
68-
};
64+
export { app, buildRenderedHtml };

run/markdown-preview/editor/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const {app} = require('./app');
16-
const pkg = require('./package.json');
17-
const PORT = parseInt(process.env.PORT) || 8080;
15+
import { app } from './app';
16+
import pkg from './package.json' assert {type: 'json'};
1817

19-
app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));
18+
const PORT = parseInt(process.env.PORT) || 8080;
19+
app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));

run/markdown-preview/editor/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
"dependencies": {
2525
"express": "^4.17.1",
2626
"google-auth-library": "^9.0.0",
27-
"got": "^11.5.0",
27+
"got": "^12.6.1",
2828
"handlebars": "^4.7.6"
2929
},
3030
"devDependencies": {
3131
"c8": "^10.0.0",
3232
"mocha": "^10.0.0",
3333
"supertest": "^7.0.0"
3434
}
35-
}
35+
}

run/markdown-preview/editor/render.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
// limitations under the License.
1414

1515
// [START cloudrun_secure_request]
16-
const {GoogleAuth} = require('google-auth-library');
17-
const got = require('got');
18-
const auth = new GoogleAuth();
16+
import { GoogleAuth } from 'google-auth-library';
17+
import got from 'got';
1918

19+
const auth = new GoogleAuth();
2020
let client, serviceUrl;
2121

2222
// renderRequest creates a new HTTP request with IAM ID Token credential.
2323
// This token is automatically handled by private Cloud Run (fully managed) and Cloud Functions.
2424
const renderRequest = async markdown => {
2525
if (!process.env.EDITOR_UPSTREAM_RENDER_URL)
2626
throw Error('EDITOR_UPSTREAM_RENDER_URL needs to be set.');
27+
2728
serviceUrl = process.env.EDITOR_UPSTREAM_RENDER_URL;
2829

2930
// Build the request to the Renderer receiving service.
@@ -33,15 +34,16 @@ const renderRequest = async markdown => {
3334
'Content-Type': 'text/plain',
3435
},
3536
body: markdown,
36-
timeout: 3000,
37+
timeout: {
38+
request: 3000,
39+
},
3740
};
3841

3942
try {
4043
// [END cloudrun_secure_request]
4144
// If we're in the test environment, use the envvar instead
4245
if (process.env.ID_TOKEN) {
43-
serviceRequestOptions.headers['Authorization'] =
44-
'Bearer ' + process.env.ID_TOKEN;
46+
serviceRequestOptions.headers['Authorization'] = `Bearer ${process.env.ID_TOKEN}`;
4547
} else {
4648
// [START cloudrun_secure_request]
4749
// Create a Google Auth client with the Renderer service url as the target audience.
@@ -60,13 +62,13 @@ const renderRequest = async markdown => {
6062

6163
try {
6264
// serviceResponse converts the Markdown plaintext to HTML.
63-
const serviceResponse = await got(serviceUrl, serviceRequestOptions);
64-
return serviceResponse.body;
65+
const { body } = await got(serviceUrl, serviceRequestOptions);
66+
return body;
6567
} catch (err) {
6668
throw Error('request to rendering service failed: ' + err.message);
6769
}
6870
};
6971

7072
// [END cloudrun_secure_request]
7173

72-
module.exports = renderRequest;
74+
export default renderRequest;

0 commit comments

Comments
 (0)