Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix/update markdown preview esm #4043

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
25 changes: 14 additions & 11 deletions run/markdown-preview/editor/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@
// 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')
);
renderedHtml = compiledTemplate({default: markdownDefault});

return renderedHtml;
} catch (err) {
throw Error('Error loading template: ', err);
Expand Down Expand Up @@ -62,7 +68,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: 5 additions & 2 deletions run/markdown-preview/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
// 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'));

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

app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));
5 changes: 3 additions & 2 deletions run/markdown-preview/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"type": "module",
"engines": {
"node": ">=16.0.0"
},
Expand All @@ -23,8 +24,8 @@
"dependencies": {
"express": "^4.17.1",
"google-auth-library": "^9.0.0",
"got": "^11.5.0",
"handlebars": "^4.7.6"
"got": "^14.0.0",
"handlebars": "^4.7.8"
},
"devDependencies": {
"c8": "^10.0.0",
Expand Down
12 changes: 8 additions & 4 deletions run/markdown-preview/editor/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
// 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 +35,9 @@ const renderRequest = async markdown => {
'Content-Type': 'text/plain',
},
body: markdown,
timeout: 3000,
timeout: {
request: 10000,
},
};

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

// [END cloudrun_secure_request]

module.exports = renderRequest;
export default renderRequest;
11 changes: 3 additions & 8 deletions run/markdown-preview/editor/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

'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 +28,6 @@ describe('Editor unit tests', () => {
let template;

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

Expand All @@ -48,7 +44,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
9 changes: 6 additions & 3 deletions run/markdown-preview/editor/test/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
// 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';
import {GoogleAuth} from 'google-auth-library';

const auth = new GoogleAuth();

Check failure on line 20 in run/markdown-preview/editor/test/system.test.js

View workflow job for this annotation

GitHub Actions / lint

'auth' is assigned a value but never used

describe('End-to-End Tests', () => {
// Retrieve Cloud Run service test config
Expand All @@ -41,7 +44,7 @@
'--config ../renderer/test/e2e_test_setup.yaml ' +
`--substitutions _SERVICE=renderer-${SERVICE_NAME},_REGION=${REGION}`;
if (SAMPLE_VERSION) buildRendererCmd += `,_VERSION=${SAMPLE_VERSION}`;
if (SERVICE_ACCOUNT) buildRendererCmd += `,_SERVICE_ACCOUNT=${SERVICE_ACCOUNT}`;

Check failure on line 47 in run/markdown-preview/editor/test/system.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎·····`

console.log('Starting Cloud Build for Renderer service...');
execSync(buildRendererCmd, {cwd: '../renderer'});
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;
7 changes: 5 additions & 2 deletions run/markdown-preview/renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
// 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'));

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

app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));
5 changes: 3 additions & 2 deletions run/markdown-preview/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"type": "module",
"engines": {
"node": ">=16.0.0"
},
Expand All @@ -20,13 +21,13 @@
"system-test": "c8 mocha -p -j 2 test/system.test.js --timeout=360000 --exit"
},
"dependencies": {
"express": "^4.17.1",
"express": "^4.18.2",
"markdown-it": "^14.0.0"
},
"devDependencies": {
"c8": "^10.0.0",
"google-auth-library": "^9.0.0",
"got": "^11.5.0",
"got": "^14.0.0",
"mocha": "^10.0.0",
"sinon": "^18.0.0",
"supertest": "^7.0.0"
Expand Down
11 changes: 4 additions & 7 deletions run/markdown-preview/renderer/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

'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
9 changes: 6 additions & 3 deletions run/markdown-preview/renderer/test/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
// 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';
import {GoogleAuth} from 'google-auth-library';

const auth = new GoogleAuth();

Check failure on line 20 in run/markdown-preview/renderer/test/system.test.js

View workflow job for this annotation

GitHub Actions / lint

'auth' is assigned a value but never used

describe('End-to-End Tests', () => {
// Retrieve Cloud Run service test config
Expand All @@ -33,7 +36,7 @@
const {SERVICE_ACCOUNT} = process.env;
const REGION = 'us-central1';


Check failure on line 39 in run/markdown-preview/renderer/test/system.test.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
let BASE_URL;
before(async () => {
// Deploy service using Cloud Build
Expand All @@ -54,7 +57,7 @@
`--region=${REGION} --format='value(status.url)'`
);
BASE_URL = url.toString('utf-8').trim();
if (!BASE_URL) throw Error('Cloud Run service URL not found');

Check failure on line 60 in run/markdown-preview/renderer/test/system.test.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`

});

Expand Down
Loading