Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions run/markdown-preview/editor/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const express = require('express');
const handlebars = require('handlebars');
const {readFile} = require('fs').promises;
const renderRequest = require('./render.js');
import express from 'express';
import handlebars from 'handlebars';
import fs from 'fs';
import renderRequest from './render.js';

const app = express();
app.use(express.json());

let markdownDefault, compiledTemplate, renderedHtml;

// Load the template files and serve them with the Editor service.
const buildRenderedHtml = async () => {
export const buildRenderedHtml = async () => {
const dirname = process.cwd();
try {
markdownDefault = await readFile(__dirname + '/templates/markdown.md');
markdownDefault = await fs.promises.readFile(
dirname + '/templates/markdown.md'
);
compiledTemplate = handlebars.compile(
await readFile(__dirname + '/templates/index.html', 'utf8')
await fs.promises.readFile(dirname + '/templates/index.html', 'utf8')
);
Comment on lines +27 to 34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using process.cwd() can be brittle as it depends on the directory from which the Node.js process is started. In ES modules, the recommended way to get the current directory is by using import.meta.url. This makes file path resolution more robust.

You'll also need to add import path from 'path'; and import { fileURLToPath } from 'url'; at the top of the file.

Suggested change
const dirname = process.cwd();
try {
markdownDefault = await readFile(__dirname + '/templates/markdown.md');
markdownDefault = await fs.promises.readFile(
dirname + '/templates/markdown.md'
);
compiledTemplate = handlebars.compile(
await readFile(__dirname + '/templates/index.html', 'utf8')
await fs.promises.readFile(dirname + '/templates/index.html', 'utf8')
);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
try {
markdownDefault = await fs.promises.readFile(
path.join(__dirname, 'templates/markdown.md')
);
compiledTemplate = handlebars.compile(
await fs.promises.readFile(path.join(__dirname, 'templates/index.html'), 'utf8')
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@angelcaamal can you take a look at this recommendation and validate whether it works?

renderedHtml = compiledTemplate({default: markdownDefault});
return renderedHtml;
Expand Down Expand Up @@ -62,7 +65,4 @@ app.post('/render', async (req, res) => {
// [END cloudrun_secure_request_do]

// Exports for testing purposes.
module.exports = {
app,
buildRenderedHtml,
};
export default app;
7 changes: 4 additions & 3 deletions run/markdown-preview/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const {app} = require('./app');
const pkg = require('./package.json');
const PORT = parseInt(process.env.PORT) || 8080;
import app from './app.js';
import fs from 'fs';

const pkg = JSON.parse(fs.readFileSync('./package.json'));
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since you're using Node.js version 20+, you can use import assertions to import JSON files directly. This is cleaner than reading and parsing the file manually, and resolves paths relative to the file, which is more robust than relying on the current working directory.

Suggested change
import fs from 'fs';
const pkg = JSON.parse(fs.readFileSync('./package.json'));
import pkg from './package.json' assert { type: 'json' };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@angelcaamal another one to validate

const PORT = parseInt(process.env.PORT) || 8080;
app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));
9 changes: 5 additions & 4 deletions run/markdown-preview/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"type": "module",
"engines": {
"node": ">=16.0.0"
"node": ">=20.0.0"
},
"main": "main.js",
"scripts": {
Expand All @@ -23,12 +24,12 @@
"dependencies": {
"express": "^4.17.1",
"google-auth-library": "^9.0.0",
"got": "^11.5.0",
"handlebars": "^4.7.6"
"got": "^14.6.5",
"handlebars": "^4.7.8"
},
"devDependencies": {
"c8": "^10.0.0",
"mocha": "^10.0.0",
"mocha": "^11.0.0",
"supertest": "^7.0.0"
}
}
10 changes: 6 additions & 4 deletions run/markdown-preview/editor/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

// [START cloudrun_secure_request]
const {GoogleAuth} = require('google-auth-library');
const got = require('got');
import {GoogleAuth} from 'google-auth-library';
import got from 'got';
const auth = new GoogleAuth();

let client, serviceUrl;
Expand All @@ -33,7 +33,9 @@ const renderRequest = async markdown => {
'Content-Type': 'text/plain',
},
body: markdown,
timeout: 3000,
timeout: {
request: 3000,
},
};

try {
Expand Down Expand Up @@ -69,4 +71,4 @@ const renderRequest = async markdown => {

// [END cloudrun_secure_request]

module.exports = renderRequest;
export default renderRequest;
9 changes: 3 additions & 6 deletions run/markdown-preview/editor/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@

'use strict';

const assert = require('assert');
const path = require('path');
const supertest = require('supertest');
import assert from 'assert';
import supertest from 'supertest';
import app, {buildRenderedHtml} from '../app.js';

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

before(async () => {
const {buildRenderedHtml} = require(path.join(__dirname, '..', 'app'));
template = await buildRenderedHtml();
});

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

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

Expand Down
10 changes: 6 additions & 4 deletions run/markdown-preview/editor/test/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const assert = require('assert');
const got = require('got');
const {execSync} = require('child_process');
import assert from 'assert';
import got from 'got';
import {execSync} from 'child_process';

describe('End-to-End Tests', () => {
// Retrieve Cloud Run service test config
Expand Down Expand Up @@ -88,7 +88,9 @@ describe('End-to-End Tests', () => {
headers: {
Authorization: `Bearer ${ID_TOKEN.trim()}`,
},
retry: 3,
retry: {
limit: 3,
},
};
const response = await got('', options);
assert.strictEqual(response.statusCode, 200);
Expand Down
6 changes: 3 additions & 3 deletions run/markdown-preview/renderer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const express = require('express');
const MarkdownIt = require('markdown-it');
import express from 'express';
import MarkdownIt from 'markdown-it';

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

// Export for testing purposes.
module.exports = app;
export default app;
6 changes: 4 additions & 2 deletions run/markdown-preview/renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const app = require('./app');
const pkg = require('./package.json');
import app from './app.js';
import fs from 'fs';

const pkg = JSON.parse(fs.readFileSync('./package.json'));
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since you're using Node.js version 20+, you can use import assertions to import JSON files directly. This is cleaner than reading and parsing the file manually, and resolves paths relative to the file, which is more robust than relying on the current working directory.

Suggested change
import fs from 'fs';
const pkg = JSON.parse(fs.readFileSync('./package.json'));
import pkg from './package.json' assert { type: 'json' };

const PORT = parseInt(process.env.PORT) || 8080;

app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));
7 changes: 4 additions & 3 deletions run/markdown-preview/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"type": "module",
"engines": {
"node": ">=16.0.0"
"node": ">=20.0.0"
},
"main": "index.js",
"scripts": {
Expand All @@ -26,8 +27,8 @@
"devDependencies": {
"c8": "^10.0.0",
"google-auth-library": "^9.0.0",
"got": "^11.5.0",
"mocha": "^10.0.0",
"got": "^14.6.5",
"mocha": "^11.0.0",
"sinon": "^18.0.0",
"supertest": "^7.0.0"
}
Expand Down
9 changes: 4 additions & 5 deletions run/markdown-preview/renderer/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@

'use strict';

const assert = require('assert');
const path = require('path');
const sinon = require('sinon');
const supertest = require('supertest');
import assert from 'assert';
import sinon from 'sinon';
import supertest from 'supertest';
import app from '../app.js';

let request;

describe('Unit Tests', () => {
before(() => {
const app = require(path.join(__dirname, '..', 'app'));
request = supertest(app);
});

Expand Down
10 changes: 6 additions & 4 deletions run/markdown-preview/renderer/test/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const assert = require('assert');
const got = require('got');
const {execSync} = require('child_process');
import assert from 'assert';
import got from 'got';
import {execSync} from 'child_process';

describe('End-to-End Tests', () => {
// Retrieve Cloud Run service test config
Expand Down Expand Up @@ -74,7 +74,9 @@ describe('End-to-End Tests', () => {
Authorization: `Bearer ${ID_TOKEN.trim()}`,
},
method: 'POST',
retry: 3,
retry: {
limit: 3,
},
throwHttpErrors: false,
};
const response = await got('', options);
Expand Down
Loading