From 649bbe2f060e2f7570d3cb3b466d446ed02183d6 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 9 Jan 2024 15:15:33 -1000 Subject: [PATCH 1/9] update impl --- webpack.config.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 6d72c663a..3525768c9 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -13,7 +13,6 @@ module.exports = { library: { name: 'AsyncAPIParser', type: 'umd', - export: 'default', }, }, @@ -46,4 +45,4 @@ module.exports = { */ // (require('webpack-bundle-analyzer').BundleAnalyzerPlugin()), ], -}; \ No newline at end of file +}; From f9e72af5d28f8e781712a6a639d302037e15fe2e Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 9 Jan 2024 15:58:48 -1000 Subject: [PATCH 2/9] update config --- .gitignore | 1 + package.json | 5 ++++- test/browser/browser.spec.ts | 4 ++-- webpack.config.js | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 943fd793b..1b328d3dd 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ node_modules /esm /cjs /browser +/browser_new *.tgz diff --git a/package.json b/package.json index 062a724f3..6fbbd8933 100644 --- a/package.json +++ b/package.json @@ -22,17 +22,20 @@ "/esm", "/cjs", "/browser", + "/browser_new", "LICENSE", "README.md" ], "scripts": { - "build": "npm run build:esm && npm run build:cjs && npm run build:browser", + "build": "npm run build:esm && npm run build:cjs && npm run build:browser && npm run build:browser_new", "build:esm": "tsc", "build:cjs": "tsc --project ./tsconfig.cjs.json", "build:browser": "webpack", + "build:browser_new": "webpack --config ./webpack_new.config.js", "test": "npm run test:unit && npm run test:browser", "test:unit": "cross-env CI=true jest --coverage --testPathIgnorePatterns=test/browser/*", "test:browser": "npm run build:browser && cross-env CI=true jest -- ./test/browser/*", + "test:browser_new": "npm run build:browser_new && cross-env CI=true jest -- ./test/browser_new/*", "lint": "eslint --max-warnings 0 --config .eslintrc .", "lint:fix": "eslint --max-warnings 0 --config .eslintrc . --fix", "generate:readme:toc": "markdown-toc -i \"README.md\"", diff --git a/test/browser/browser.spec.ts b/test/browser/browser.spec.ts index 091641e33..29d3af730 100644 --- a/test/browser/browser.spec.ts +++ b/test/browser/browser.spec.ts @@ -44,7 +44,7 @@ describe('Test browser Parser in the node env', function() { afterAll(async function() { await browser.close(); - await server.close(); + server.close(); }); it('should parse spec in the browser', async function() { @@ -58,4 +58,4 @@ describe('Test browser Parser in the node env', function() { const diagnostics = await page.evaluate(element => element && element.textContent, diagnosticsDiv); expect(Number(diagnostics)).toBeGreaterThanOrEqual(0); }, 5000); -}); \ No newline at end of file +}); diff --git a/webpack.config.js b/webpack.config.js index 3525768c9..f3f2a60c8 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -13,6 +13,7 @@ module.exports = { library: { name: 'AsyncAPIParser', type: 'umd', + export: 'default' }, }, From 165ef88d25f08bc9c4e442ae0560520d1b094058 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 9 Jan 2024 16:00:24 -1000 Subject: [PATCH 3/9] update config --- test/browser_new/browser.spec.ts | 61 +++++++++++++++++++++++++++++++ test/browser_new/sample-page.html | 29 +++++++++++++++ webpack_new.config.js | 48 ++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 test/browser_new/browser.spec.ts create mode 100644 test/browser_new/sample-page.html create mode 100644 webpack_new.config.js diff --git a/test/browser_new/browser.spec.ts b/test/browser_new/browser.spec.ts new file mode 100644 index 000000000..74b9dbccd --- /dev/null +++ b/test/browser_new/browser.spec.ts @@ -0,0 +1,61 @@ +import fs from 'fs'; +import http from 'http'; +import path from 'path'; +import url from 'url'; +import puppeteer from 'puppeteer'; + +describe('Test browser Parser in the node env', function() { + let server: http.Server; + let browser: puppeteer.Browser; + let page: puppeteer.Page; + + beforeAll(async function() { + const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); + const htmlPath = path.resolve(__dirname, 'sample-page.html'); + const parserScript = path.resolve(__dirname, '../../browser_new/index.js'); + + console.info('start server'); + server = http.createServer((req, res) => { + res.writeHead(200, { 'content-type': 'text/html' }); + if (req.url === '/') { + return fs.createReadStream(htmlPath).pipe(res); + } else if (req.url === '/parser.js') { + return fs.createReadStream(parserScript).pipe(res); + } + }); + server.listen(8080); + + //use this in case you want to troubleshoot in a real chrome window => browser = await puppeteer.launch({headless: false}); + console.info('starting browser'); + browser = await puppeteer.launch(); + + console.info('opening new page'); + page = await browser.newPage(); + + page.on('console', msg => { + msg.args().forEach((arg, index) => { + console.error(`Browser console content ${index}: ${JSON.stringify(arg.remoteObject().value, null, 2)}`); + }); + }); + + console.info('navigating to localhost'); + await page.goto('http://localhost:8080', { waitUntil: 'networkidle0' }); + }); + + afterAll(async function() { + await browser.close(); + server.close(); + }); + + it('should parse spec in the browser', async function() { + console.info('getting content element'); + const contentDiv = await page.$('#content'); + const content = await page.evaluate(element => element && element.textContent, contentDiv); + expect(content).toEqual('2.0.0'); + + console.info('getting number of warnings'); + const diagnosticsDiv = await page.$('#diagnostics'); + const diagnostics = await page.evaluate(element => element && element.textContent, diagnosticsDiv); + expect(Number(diagnostics)).toBeGreaterThanOrEqual(0); + }, 5000); +}); diff --git a/test/browser_new/sample-page.html b/test/browser_new/sample-page.html new file mode 100644 index 000000000..06bd893fe --- /dev/null +++ b/test/browser_new/sample-page.html @@ -0,0 +1,29 @@ + + + + Sample page + + + + +
+
+ + + + + diff --git a/webpack_new.config.js b/webpack_new.config.js new file mode 100644 index 000000000..318a90428 --- /dev/null +++ b/webpack_new.config.js @@ -0,0 +1,48 @@ +/* eslint-disable */ +const path = require('path'); + +module.exports = { + entry: './src/index.ts', + target: 'web', + mode: 'production', + + output: { + path: path.resolve(__dirname, 'browser_new'), + filename: 'index.js', + globalObject: '(typeof self !== \'undefined\' ? self : this)', + library: { + name: 'AsyncAPIParser', + type: 'umd' + }, + }, + + module: { + rules: [ + { + test: /\.tsx?$/, + loader: 'ts-loader', + exclude: /node_modules/, + options: { + configFile: 'tsconfig.json', + transpileOnly: true, + }, + }, + ], + }, + resolve: { + extensions: ['.ts', '.tsx', '.js'], + fallback: { + fs: false, + path: false, + util: false, + buffer: require.resolve('buffer/'), + } + }, + + plugins: [ + /** + * Uncomment plugin when you wanna see dependency map of bundled package + */ + // (require('webpack-bundle-analyzer').BundleAnalyzerPlugin()), + ], +}; From 7684893ba8f9cfc0a8b1ac1cf1c41b32e4361b8b Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 9 Jan 2024 16:16:21 -1000 Subject: [PATCH 4/9] update config --- package.json | 2 +- test/browser_new/browser.spec.ts | 9 +++++++-- test/browser_new/sample-page.html | 5 ++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 6fbbd8933..526356609 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "test": "npm run test:unit && npm run test:browser", "test:unit": "cross-env CI=true jest --coverage --testPathIgnorePatterns=test/browser/*", "test:browser": "npm run build:browser && cross-env CI=true jest -- ./test/browser/*", - "test:browser_new": "npm run build:browser_new && cross-env CI=true jest -- ./test/browser_new/*", + "test:browser_new": "cross-env CI=true jest -- ./test/browser_new/*", "lint": "eslint --max-warnings 0 --config .eslintrc .", "lint:fix": "eslint --max-warnings 0 --config .eslintrc . --fix", "generate:readme:toc": "markdown-toc -i \"README.md\"", diff --git a/test/browser_new/browser.spec.ts b/test/browser_new/browser.spec.ts index 74b9dbccd..2272c485c 100644 --- a/test/browser_new/browser.spec.ts +++ b/test/browser_new/browser.spec.ts @@ -16,11 +16,16 @@ describe('Test browser Parser in the node env', function() { console.info('start server'); server = http.createServer((req, res) => { - res.writeHead(200, { 'content-type': 'text/html' }); if (req.url === '/') { + res.writeHead(200, { 'content-type': 'text/html' }); return fs.createReadStream(htmlPath).pipe(res); } else if (req.url === '/parser.js') { + res.writeHead(200, { 'content-type': 'text/html' }); return fs.createReadStream(parserScript).pipe(res); + } else if (req.url === '/asyncapi.json') { + res.writeHead(200, { 'content-type': 'application/json' }); + res.write(JSON.stringify({ asyncapi: '2.0.0', info: { title: 'My API', version: '1.0.0' }, channels: { '/test/tester': { subscribe: { operationId: 'subscribeOperation', message: { } } } } })); + res.end(); } }); server.listen(8080); @@ -40,7 +45,7 @@ describe('Test browser Parser in the node env', function() { console.info('navigating to localhost'); await page.goto('http://localhost:8080', { waitUntil: 'networkidle0' }); - }); + }, 5000); afterAll(async function() { await browser.close(); diff --git a/test/browser_new/sample-page.html b/test/browser_new/sample-page.html index 06bd893fe..b03ab84e5 100644 --- a/test/browser_new/sample-page.html +++ b/test/browser_new/sample-page.html @@ -15,9 +15,8 @@ async function parse() { try { const parser = new window.AsyncAPIParser.Parser(); - const spec = '{ "asyncapi": "2.0.0", "info": { "title": "My API", "version": "1.0.0" }, "channels": { "/test/tester": { "subscribe": { "operationId": "subscribeOperation", "message": { } } } } }'; - const { document: parsedDocument, diagnostics } = await parser.parse(spec); - + const result = window.AsyncAPIParser.fromURL(parser, 'http://localhost:8080/asyncapi.json'); + const {document: parsedDocument, diagnostics} = await result.parse(); document.getElementById('content').innerHTML = parsedDocument.version(); document.getElementById('diagnostics').innerHTML = String(diagnostics.length); } catch (error) { From 06c0d2cee5adc962ce188be537d7e915415a7057 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 25 Jun 2024 10:59:42 +0200 Subject: [PATCH 5/9] update browser exposure --- package.json | 5 +-- test/browser/browser.spec.ts | 11 ++++-- test/browser/sample-page.html | 7 ++-- test/browser_new/browser.spec.ts | 66 ------------------------------- test/browser_new/sample-page.html | 28 ------------- webpack.config.js | 3 +- webpack_new.config.js | 48 ---------------------- 7 files changed, 13 insertions(+), 155 deletions(-) delete mode 100644 test/browser_new/browser.spec.ts delete mode 100644 test/browser_new/sample-page.html delete mode 100644 webpack_new.config.js diff --git a/package.json b/package.json index ef9aeeb02..3a12c916f 100644 --- a/package.json +++ b/package.json @@ -22,20 +22,17 @@ "/esm", "/cjs", "/browser", - "/browser_new", "LICENSE", "README.md" ], "scripts": { - "build": "npm run build:esm && npm run build:cjs && npm run build:browser && npm run build:browser_new", + "build": "npm run build:esm && npm run build:cjs && npm run build:browser", "build:esm": "tsc", "build:cjs": "tsc --project ./tsconfig.cjs.json", "build:browser": "webpack", - "build:browser_new": "webpack --config ./webpack_new.config.js", "test": "npm run test:unit && npm run test:browser", "test:unit": "cross-env CI=true jest --coverage --testPathIgnorePatterns=test/browser/*", "test:browser": "npm run build:browser && cross-env CI=true jest -- ./test/browser/*", - "test:browser_new": "cross-env CI=true jest -- ./test/browser_new/*", "lint": "eslint --max-warnings 0 --config .eslintrc .", "lint:fix": "eslint --max-warnings 0 --config .eslintrc . --fix", "generate:readme:toc": "markdown-toc -i \"README.md\"", diff --git a/test/browser/browser.spec.ts b/test/browser/browser.spec.ts index 29d3af730..2272c485c 100644 --- a/test/browser/browser.spec.ts +++ b/test/browser/browser.spec.ts @@ -12,15 +12,20 @@ describe('Test browser Parser in the node env', function() { beforeAll(async function() { const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); const htmlPath = path.resolve(__dirname, 'sample-page.html'); - const parserScript = path.resolve(__dirname, '../../browser/index.js'); + const parserScript = path.resolve(__dirname, '../../browser_new/index.js'); console.info('start server'); server = http.createServer((req, res) => { - res.writeHead(200, { 'content-type': 'text/html' }); if (req.url === '/') { + res.writeHead(200, { 'content-type': 'text/html' }); return fs.createReadStream(htmlPath).pipe(res); } else if (req.url === '/parser.js') { + res.writeHead(200, { 'content-type': 'text/html' }); return fs.createReadStream(parserScript).pipe(res); + } else if (req.url === '/asyncapi.json') { + res.writeHead(200, { 'content-type': 'application/json' }); + res.write(JSON.stringify({ asyncapi: '2.0.0', info: { title: 'My API', version: '1.0.0' }, channels: { '/test/tester': { subscribe: { operationId: 'subscribeOperation', message: { } } } } })); + res.end(); } }); server.listen(8080); @@ -40,7 +45,7 @@ describe('Test browser Parser in the node env', function() { console.info('navigating to localhost'); await page.goto('http://localhost:8080', { waitUntil: 'networkidle0' }); - }); + }, 5000); afterAll(async function() { await browser.close(); diff --git a/test/browser/sample-page.html b/test/browser/sample-page.html index 50e8402b4..b03ab84e5 100644 --- a/test/browser/sample-page.html +++ b/test/browser/sample-page.html @@ -14,10 +14,9 @@ - - diff --git a/webpack.config.js b/webpack.config.js index f3f2a60c8..3d089c44a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -12,8 +12,7 @@ module.exports = { globalObject: '(typeof self !== \'undefined\' ? self : this)', library: { name: 'AsyncAPIParser', - type: 'umd', - export: 'default' + type: 'umd' }, }, diff --git a/webpack_new.config.js b/webpack_new.config.js deleted file mode 100644 index 318a90428..000000000 --- a/webpack_new.config.js +++ /dev/null @@ -1,48 +0,0 @@ -/* eslint-disable */ -const path = require('path'); - -module.exports = { - entry: './src/index.ts', - target: 'web', - mode: 'production', - - output: { - path: path.resolve(__dirname, 'browser_new'), - filename: 'index.js', - globalObject: '(typeof self !== \'undefined\' ? self : this)', - library: { - name: 'AsyncAPIParser', - type: 'umd' - }, - }, - - module: { - rules: [ - { - test: /\.tsx?$/, - loader: 'ts-loader', - exclude: /node_modules/, - options: { - configFile: 'tsconfig.json', - transpileOnly: true, - }, - }, - ], - }, - resolve: { - extensions: ['.ts', '.tsx', '.js'], - fallback: { - fs: false, - path: false, - util: false, - buffer: require.resolve('buffer/'), - } - }, - - plugins: [ - /** - * Uncomment plugin when you wanna see dependency map of bundled package - */ - // (require('webpack-bundle-analyzer').BundleAnalyzerPlugin()), - ], -}; From 25b4ea4f96116f1abe5d58e4871393ba989500bf Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 25 Jun 2024 10:59:59 +0200 Subject: [PATCH 6/9] update browser exposure --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1b328d3dd..943fd793b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,4 @@ node_modules /esm /cjs /browser -/browser_new *.tgz From 0459e6d002796e8aed62f93f1b8f84be75c22d02 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 25 Jun 2024 11:01:05 +0200 Subject: [PATCH 7/9] update browser exposure --- test/browser/browser.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/browser/browser.spec.ts b/test/browser/browser.spec.ts index 2272c485c..c85442af4 100644 --- a/test/browser/browser.spec.ts +++ b/test/browser/browser.spec.ts @@ -12,7 +12,7 @@ describe('Test browser Parser in the node env', function() { beforeAll(async function() { const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); const htmlPath = path.resolve(__dirname, 'sample-page.html'); - const parserScript = path.resolve(__dirname, '../../browser_new/index.js'); + const parserScript = path.resolve(__dirname, '../../browser/index.js'); console.info('start server'); server = http.createServer((req, res) => { From bec5d5d889c1694ad0266674ba2065ed77aade98 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 25 Jun 2024 11:30:20 +0200 Subject: [PATCH 8/9] update browser exposure --- test/browser/browser.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/browser/browser.spec.ts b/test/browser/browser.spec.ts index c85442af4..360bfcdcb 100644 --- a/test/browser/browser.spec.ts +++ b/test/browser/browser.spec.ts @@ -45,7 +45,7 @@ describe('Test browser Parser in the node env', function() { console.info('navigating to localhost'); await page.goto('http://localhost:8080', { waitUntil: 'networkidle0' }); - }, 5000); + }, 10000); afterAll(async function() { await browser.close(); From 6f7d525e3454555e17da2c980f8416afbf11d6de Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 25 Jun 2024 11:41:14 +0200 Subject: [PATCH 9/9] add migration --- docs/migrations/v3-to-v4.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 docs/migrations/v3-to-v4.md diff --git a/docs/migrations/v3-to-v4.md b/docs/migrations/v3-to-v4.md new file mode 100644 index 000000000..58fb5b39f --- /dev/null +++ b/docs/migrations/v3-to-v4.md @@ -0,0 +1,24 @@ +# Migrating from v2 to v3 + +The ONLY thing that changes between v3 and v4 has to do with the standalone browser bundle. + +## New browser bundle + +In the old browser bundle you only had access to the parser. + +```js +const parser = new window.AsyncAPIParser(); +const spec = '{ ... }'; +const { document: parsedDocument, diagnostics } = await parser.parse(spec); +``` + +With the new browser bundle, you have access a bunch of support functions such as `fromURL`, `convertToOldAPI`, `unstringify`, `stringify`. + +```js +const parser = new window.AsyncAPIParser.Parser(); +const spec = '{ ... }'; +const { document: parsedDocument, diagnostics } = await parser.parse(spec); +... +const result = window.AsyncAPIParser.fromURL(parser, 'http://localhost:8080/asyncapi.json'); +const {document: parsedDocument, diagnostics} = await result.parse(); +```