diff --git a/run/markdown-preview/editor/app.js b/run/markdown-preview/editor/app.js index 3cd0f38714..43e9f214aa 100644 --- a/run/markdown-preview/editor/app.js +++ b/run/markdown-preview/editor/app.js @@ -12,10 +12,10 @@ // 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 {readFile} from 'fs/promises'; +import renderRequest from './render.js'; const app = express(); app.use(express.json()); @@ -25,14 +25,18 @@ let markdownDefault, compiledTemplate, renderedHtml; // Load the template files and serve them with the Editor service. const buildRenderedHtml = async () => { try { - markdownDefault = await readFile(__dirname + '/templates/markdown.md'); - compiledTemplate = handlebars.compile( - await readFile(__dirname + '/templates/index.html', 'utf8') + markdownDefault = await readFile( + new URL('./templates/markdown.md', import.meta.url) ); + const indexTemplate = await readFile( + new URL('./templates/index.html', import.meta.url), + 'utf8' + ); + compiledTemplate = handlebars.compile(indexTemplate); renderedHtml = compiledTemplate({default: markdownDefault}); return renderedHtml; } catch (err) { - throw Error('Error loading template: ', err); + throw Error('Error loading template: ' + err); } }; @@ -62,7 +66,4 @@ app.post('/render', async (req, res) => { // [END cloudrun_secure_request_do] // Exports for testing purposes. -module.exports = { - app, - buildRenderedHtml, -}; +export {app, buildRenderedHtml}; diff --git a/run/markdown-preview/editor/index.js b/run/markdown-preview/editor/index.js index 831ee1db01..efcacda07e 100644 --- a/run/markdown-preview/editor/index.js +++ b/run/markdown-preview/editor/index.js @@ -12,8 +12,8 @@ // 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'; +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}`)); diff --git a/run/markdown-preview/editor/package.json b/run/markdown-preview/editor/package.json index 5ffa5dd772..ff414e4cc0 100644 --- a/run/markdown-preview/editor/package.json +++ b/run/markdown-preview/editor/package.json @@ -5,6 +5,7 @@ "private": true, "license": "Apache-2.0", "author": "Google LLC", + "type": "module", "repository": { "type": "git", "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" @@ -23,7 +24,7 @@ "dependencies": { "express": "^4.17.1", "google-auth-library": "^9.0.0", - "got": "^11.5.0", + "got": "^12.6.1", "handlebars": "^4.7.6" }, "devDependencies": { diff --git a/run/markdown-preview/editor/render.js b/run/markdown-preview/editor/render.js index cc75c4c533..43fd267748 100644 --- a/run/markdown-preview/editor/render.js +++ b/run/markdown-preview/editor/render.js @@ -13,10 +13,10 @@ // limitations under the License. // [START cloudrun_secure_request] -const {GoogleAuth} = require('google-auth-library'); -const got = require('got'); -const auth = new GoogleAuth(); +import {GoogleAuth} from 'google-auth-library'; +import got from 'got'; +const auth = new GoogleAuth(); let client, serviceUrl; // renderRequest creates a new HTTP request with IAM ID Token credential. @@ -24,6 +24,7 @@ let client, serviceUrl; const renderRequest = async markdown => { if (!process.env.EDITOR_UPSTREAM_RENDER_URL) throw Error('EDITOR_UPSTREAM_RENDER_URL needs to be set.'); + serviceUrl = process.env.EDITOR_UPSTREAM_RENDER_URL; // Build the request to the Renderer receiving service. @@ -33,7 +34,9 @@ const renderRequest = async markdown => { 'Content-Type': 'text/plain', }, body: markdown, - timeout: 3000, + timeout: { + request: 3000, + }, }; try { @@ -41,7 +44,7 @@ const renderRequest = async markdown => { // If we're in the test environment, use the envvar instead if (process.env.ID_TOKEN) { serviceRequestOptions.headers['Authorization'] = - 'Bearer ' + process.env.ID_TOKEN; + `Bearer ${process.env.ID_TOKEN}`; } else { // [START cloudrun_secure_request] // Create a Google Auth client with the Renderer service url as the target audience. @@ -60,8 +63,8 @@ const renderRequest = async markdown => { try { // serviceResponse converts the Markdown plaintext to HTML. - const serviceResponse = await got(serviceUrl, serviceRequestOptions); - return serviceResponse.body; + const {body} = await got(serviceUrl, serviceRequestOptions); + return body; } catch (err) { throw Error('request to rendering service failed: ' + err.message); } @@ -69,4 +72,4 @@ const renderRequest = async markdown => { // [END cloudrun_secure_request] -module.exports = renderRequest; +export default renderRequest; diff --git a/run/markdown-preview/editor/test/app.test.js b/run/markdown-preview/editor/test/app.test.js index a2c92d5502..6681593b7a 100644 --- a/run/markdown-preview/editor/test/app.test.js +++ b/run/markdown-preview/editor/test/app.test.js @@ -14,24 +14,30 @@ 'use strict'; -const assert = require('assert'); -const path = require('path'); -const supertest = require('supertest'); +import assert from 'assert'; +import path from 'path'; +import {fileURLToPath} from 'url'; +import supertest from 'supertest'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); describe('Editor unit tests', () => { describe('Initialize app', () => { it('should successfully load the index page', async () => { - const {app} = require(path.join(__dirname, '..', 'app')); + const {app} = await import(path.join(__dirname, '..', 'app.js')); const request = supertest(app); await request.get('/').retry(3).expect(200); }); }); - describe('Handlebars compiler', async () => { + describe('Handlebars compiler', () => { let template; before(async () => { - const {buildRenderedHtml} = require(path.join(__dirname, '..', 'app')); + const {buildRenderedHtml} = await import( + path.join(__dirname, '..', 'app.js') + ); template = await buildRenderedHtml(); }); @@ -48,7 +54,7 @@ describe('Integration tests', () => { before(async () => { process.env.EDITOR_UPSTREAM_RENDER_URL = 'https://www.example.com/'; - const {app} = require(path.join(__dirname, '..', 'app')); + const {app} = await import(path.join(__dirname, '..', 'app.js')); request = supertest(app); }); diff --git a/run/markdown-preview/editor/test/system.test.js b/run/markdown-preview/editor/test/system.test.js index f4a9971531..d74c880fc5 100644 --- a/run/markdown-preview/editor/test/system.test.js +++ b/run/markdown-preview/editor/test/system.test.js @@ -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 @@ -27,13 +27,13 @@ describe('End-to-End Tests', () => { console.log('"SERVICE_NAME" env var not found. Defaulting to "editor"'); SERVICE_NAME = 'editor'; } - const {ID_TOKEN} = process.env; - if (!ID_TOKEN) throw Error('ID token not in envvar'); - const {SAMPLE_VERSION} = process.env; - const {SERVICE_ACCOUNT} = process.env; + + const {ID_TOKEN, SAMPLE_VERSION, SERVICE_ACCOUNT} = process.env; const REGION = 'us-central1'; + if (!ID_TOKEN) throw Error('ID token not in envvar'); let BASE_URL; + before(async () => { // Deploy Renderer service let buildRendererCmd = @@ -88,7 +88,7 @@ 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); diff --git a/run/markdown-preview/renderer/app.js b/run/markdown-preview/renderer/app.js index a984b37045..8a7b7de408 100644 --- a/run/markdown-preview/renderer/app.js +++ b/run/markdown-preview/renderer/app.js @@ -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()); @@ -40,4 +40,4 @@ app.post('/', (req, res) => { }); // Export for testing purposes. -module.exports = app; +export default app; diff --git a/run/markdown-preview/renderer/index.js b/run/markdown-preview/renderer/index.js index 43782b14ea..2eebda562e 100644 --- a/run/markdown-preview/renderer/index.js +++ b/run/markdown-preview/renderer/index.js @@ -12,8 +12,8 @@ // 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'; +import pkg from './package.json'; +const PORT = parseInt(process.env.PORT) || 8080; app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`)); diff --git a/run/markdown-preview/renderer/package.json b/run/markdown-preview/renderer/package.json index d947efe077..e464fdb42f 100644 --- a/run/markdown-preview/renderer/package.json +++ b/run/markdown-preview/renderer/package.json @@ -4,6 +4,7 @@ "private": true, "license": "Apache-2.0", "author": "Google LLC", + "type": "module", "repository": { "type": "git", "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" @@ -21,14 +22,14 @@ }, "dependencies": { "express": "^4.17.1", + "got": "^12.6.1", "markdown-it": "^14.0.0" }, "devDependencies": { "c8": "^10.0.0", "google-auth-library": "^9.0.0", - "got": "^11.5.0", "mocha": "^10.0.0", "sinon": "^18.0.0", "supertest": "^7.0.0" } -} +} \ No newline at end of file diff --git a/run/markdown-preview/renderer/test/app.test.js b/run/markdown-preview/renderer/test/app.test.js index e8770795a5..a9b4b4368d 100644 --- a/run/markdown-preview/renderer/test/app.test.js +++ b/run/markdown-preview/renderer/test/app.test.js @@ -14,17 +14,21 @@ 'use strict'; -const assert = require('assert'); -const path = require('path'); -const sinon = require('sinon'); -const supertest = require('supertest'); +import assert from 'assert'; +import path from 'path'; +import sinon from 'sinon'; +import supertest from 'supertest'; +import {fileURLToPath} from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); let request; describe('Unit Tests', () => { - before(() => { - const app = require(path.join(__dirname, '..', 'app')); - request = supertest(app); + before(async () => { + const appModule = await import(path.join(__dirname, '..', 'app.js')); + request = supertest(appModule.default); }); it('should return Bad Request with an invalid type', async () => { diff --git a/run/markdown-preview/renderer/test/system.test.js b/run/markdown-preview/renderer/test/system.test.js index 4b8213b129..864d02cea1 100644 --- a/run/markdown-preview/renderer/test/system.test.js +++ b/run/markdown-preview/renderer/test/system.test.js @@ -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 @@ -22,18 +22,19 @@ describe('End-to-End Tests', () => { if (!GOOGLE_CLOUD_PROJECT) { throw Error('"GOOGLE_CLOUD_PROJECT" env var not found.'); } + let {SERVICE_NAME} = process.env; if (!SERVICE_NAME) { console.log('"SERVICE_NAME" env var not found. Defaulting to "editor"'); SERVICE_NAME = 'renderer'; } - const {ID_TOKEN} = process.env; - if (!ID_TOKEN) throw Error('ID token not in envvar'); - const {SAMPLE_VERSION} = process.env; - const {SERVICE_ACCOUNT} = process.env; + + const {ID_TOKEN, SAMPLE_VERSION, SERVICE_ACCOUNT} = process.env; const REGION = 'us-central1'; + if (!ID_TOKEN) throw Error('ID token not in envvar'); let BASE_URL; + before(async () => { // Deploy service using Cloud Build let buildCmd = @@ -74,7 +75,7 @@ describe('End-to-End Tests', () => { Authorization: `Bearer ${ID_TOKEN.trim()}`, }, method: 'POST', - retry: 3, + retry: {limit: 3}, throwHttpErrors: false, }; const response = await got('', options);