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

fix(wip): update cloud run markdown sample from cjs to esm 🚧 #3592

Closed
wants to merge 9 commits into 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 @@ -64,7 +70,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 @@ -21,8 +22,8 @@
"dependencies": {
"express": "^4.17.1",
"google-auth-library": "^9.0.0",
"got": "^11.8.0",
"handlebars": "^4.7.6"
"got": "^14.0.0",
"handlebars": "^4.7.8"
},
"devDependencies": {
"c8": "^8.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 @@ -14,8 +14,10 @@

// [START cloudrun_secure_request]
// [START run_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 @@ -34,7 +36,9 @@ const renderRequest = async markdown => {
'Content-Type': 'text/plain',
},
body: markdown,
timeout: 3000,
timeout: {
request: 10000,
},
};

try {
Expand All @@ -61,4 +65,4 @@ const renderRequest = async markdown => {
// [END run_secure_request]
// [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: 5 additions & 4 deletions run/markdown-preview/editor/test/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
// 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');
const {GoogleAuth} = require('google-auth-library');
import assert from 'assert';
import got from 'got';
import {execSync} from 'child_process';
import {GoogleAuth} from 'google-auth-library';

const auth = new GoogleAuth();

describe('End-to-End Tests', () => {
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}`));
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,6 +8,7 @@
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"type": "module",
"engines": {
"node": ">=16.0.0"
},
Expand All @@ -18,13 +19,13 @@
"system-test": "c8 mocha -p -j 2 test/system.test.js --timeout=360000 --exit"
},
"dependencies": {
"express": "^4.17.1",
"markdown-it": "^13.0.0"
"express": "^4.18.2",
"markdown-it": "^14.0.0"
},
"devDependencies": {
"c8": "^8.0.0",
"google-auth-library": "^9.0.0",
"got": "^11.5.0",
"got": "^14.0.0",
"mocha": "^10.0.0",
"sinon": "^16.0.0",
"supertest": "^6.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: 5 additions & 4 deletions run/markdown-preview/renderer/test/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
// 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');
const {GoogleAuth} = require('google-auth-library');
import assert from 'assert';
import got from 'got';
import {execSync} from 'child_process';
import {GoogleAuth} from 'google-auth-library';

const auth = new GoogleAuth();

describe('End-to-End Tests', () => {
Expand Down
Loading