Skip to content

Commit f6a44c5

Browse files
angelcaamaliennae
andauthored
fix(markdown-preview): Upgrade 'got' library and migrate to ES Modules (#4201)
* fix(deps): Upgrade 'got' and refactor modules to ES imports * style: Apply lint fixes and code formatting * style: Apply lint fixes and code formatting --------- Co-authored-by: Jennifer Davis <sigje@google.com>
1 parent ae325ef commit f6a44c5

File tree

11 files changed

+56
-49
lines changed

11 files changed

+56
-49
lines changed

run/markdown-preview/editor/app.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@
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 fs from 'fs';
18+
import renderRequest from './render.js';
1919

2020
const app = express();
2121
app.use(express.json());
2222

2323
let markdownDefault, compiledTemplate, renderedHtml;
2424

2525
// Load the template files and serve them with the Editor service.
26-
const buildRenderedHtml = async () => {
26+
export const buildRenderedHtml = async () => {
27+
const dirname = process.cwd();
2728
try {
28-
markdownDefault = await readFile(__dirname + '/templates/markdown.md');
29+
markdownDefault = await fs.promises.readFile(
30+
dirname + '/templates/markdown.md'
31+
);
2932
compiledTemplate = handlebars.compile(
30-
await readFile(__dirname + '/templates/index.html', 'utf8')
33+
await fs.promises.readFile(dirname + '/templates/index.html', 'utf8')
3134
);
3235
renderedHtml = compiledTemplate({default: markdownDefault});
3336
return renderedHtml;
@@ -62,7 +65,4 @@ app.post('/render', async (req, res) => {
6265
// [END cloudrun_secure_request_do]
6366

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

run/markdown-preview/editor/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
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.js';
16+
import fs from 'fs';
1817

18+
const pkg = JSON.parse(fs.readFileSync('./package.json'));
19+
const PORT = parseInt(process.env.PORT) || 8080;
1920
app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));

run/markdown-preview/editor/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
"type": "git",
1010
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
1111
},
12+
"type": "module",
1213
"engines": {
13-
"node": ">=16.0.0"
14+
"node": ">=20.0.0"
1415
},
1516
"main": "main.js",
1617
"scripts": {
@@ -23,12 +24,12 @@
2324
"dependencies": {
2425
"express": "^4.17.1",
2526
"google-auth-library": "^9.0.0",
26-
"got": "^11.5.0",
27-
"handlebars": "^4.7.6"
27+
"got": "^14.6.5",
28+
"handlebars": "^4.7.8"
2829
},
2930
"devDependencies": {
3031
"c8": "^10.0.0",
31-
"mocha": "^10.0.0",
32+
"mocha": "^11.0.0",
3233
"supertest": "^7.0.0"
3334
}
3435
}

run/markdown-preview/editor/render.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414

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

2020
let client, serviceUrl;
@@ -33,7 +33,9 @@ const renderRequest = async markdown => {
3333
'Content-Type': 'text/plain',
3434
},
3535
body: markdown,
36-
timeout: 3000,
36+
timeout: {
37+
request: 3000,
38+
},
3739
};
3840

3941
try {
@@ -69,4 +71,4 @@ const renderRequest = async markdown => {
6971

7072
// [END cloudrun_secure_request]
7173

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

run/markdown-preview/editor/test/app.test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
'use strict';
1616

17-
const assert = require('assert');
18-
const path = require('path');
19-
const supertest = require('supertest');
17+
import assert from 'assert';
18+
import supertest from 'supertest';
19+
import app, {buildRenderedHtml} from '../app.js';
2020

2121
describe('Editor unit tests', () => {
2222
describe('Initialize app', () => {
2323
it('should successfully load the index page', async () => {
24-
const {app} = require(path.join(__dirname, '..', 'app'));
2524
const request = supertest(app);
2625
await request.get('/').retry(3).expect(200);
2726
});
@@ -31,7 +30,6 @@ describe('Editor unit tests', () => {
3130
let template;
3231

3332
before(async () => {
34-
const {buildRenderedHtml} = require(path.join(__dirname, '..', 'app'));
3533
template = await buildRenderedHtml();
3634
});
3735

@@ -48,7 +46,6 @@ describe('Integration tests', () => {
4846

4947
before(async () => {
5048
process.env.EDITOR_UPSTREAM_RENDER_URL = 'https://www.example.com/';
51-
const {app} = require(path.join(__dirname, '..', 'app'));
5249
request = supertest(app);
5350
});
5451

run/markdown-preview/editor/test/system.test.js

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

15-
const assert = require('assert');
16-
const got = require('got');
17-
const {execSync} = require('child_process');
15+
import assert from 'assert';
16+
import got from 'got';
17+
import {execSync} from 'child_process';
1818

1919
describe('End-to-End Tests', () => {
2020
// Retrieve Cloud Run service test config
@@ -88,7 +88,9 @@ describe('End-to-End Tests', () => {
8888
headers: {
8989
Authorization: `Bearer ${ID_TOKEN.trim()}`,
9090
},
91-
retry: 3,
91+
retry: {
92+
limit: 3,
93+
},
9294
};
9395
const response = await got('', options);
9496
assert.strictEqual(response.statusCode, 200);

run/markdown-preview/renderer/app.js

Lines changed: 3 additions & 3 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 express = require('express');
16-
const MarkdownIt = require('markdown-it');
15+
import express from 'express';
16+
import MarkdownIt from 'markdown-it';
1717

1818
const app = express();
1919
app.use(express.text());
@@ -40,4 +40,4 @@ app.post('/', (req, res) => {
4040
});
4141

4242
// Export for testing purposes.
43-
module.exports = app;
43+
export default app;

run/markdown-preview/renderer/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
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');
15+
import app from './app.js';
16+
import fs from 'fs';
17+
18+
const pkg = JSON.parse(fs.readFileSync('./package.json'));
1719
const PORT = parseInt(process.env.PORT) || 8080;
1820

1921
app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));

run/markdown-preview/renderer/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"type": "git",
99
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
1010
},
11+
"type": "module",
1112
"engines": {
12-
"node": ">=16.0.0"
13+
"node": ">=20.0.0"
1314
},
1415
"main": "index.js",
1516
"scripts": {
@@ -26,8 +27,8 @@
2627
"devDependencies": {
2728
"c8": "^10.0.0",
2829
"google-auth-library": "^9.0.0",
29-
"got": "^11.5.0",
30-
"mocha": "^10.0.0",
30+
"got": "^14.6.5",
31+
"mocha": "^11.0.0",
3132
"sinon": "^18.0.0",
3233
"supertest": "^7.0.0"
3334
}

run/markdown-preview/renderer/test/app.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414

1515
'use strict';
1616

17-
const assert = require('assert');
18-
const path = require('path');
19-
const sinon = require('sinon');
20-
const supertest = require('supertest');
17+
import assert from 'assert';
18+
import sinon from 'sinon';
19+
import supertest from 'supertest';
20+
import app from '../app.js';
2121

2222
let request;
2323

2424
describe('Unit Tests', () => {
2525
before(() => {
26-
const app = require(path.join(__dirname, '..', 'app'));
2726
request = supertest(app);
2827
});
2928

0 commit comments

Comments
 (0)