From 17838d183c0900782bcaf6e21493e2cf4b6e7031 Mon Sep 17 00:00:00 2001 From: Dev Doshi Date: Mon, 13 Jul 2020 14:05:13 -0400 Subject: [PATCH 1/2] dumps should be able to be converted to captures --- workspaces/cli-shared/package.json | 8 +- .../avro/file-system/dump-capture-saver.ts | 91 +++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 workspaces/cli-shared/src/captures/avro/file-system/dump-capture-saver.ts diff --git a/workspaces/cli-shared/package.json b/workspaces/cli-shared/package.json index ac98897796..401bc07138 100644 --- a/workspaces/cli-shared/package.json +++ b/workspaces/cli-shared/package.json @@ -22,18 +22,20 @@ "@useoptic/domain-types": "8.1.0", "@useoptic/domain-utilities": "8.1.0", "avsc": "^5.4.21", - "proper-lockfile": "^4.1.1", "bottleneck": "^2.19.5", "colors": "^1.4.0", "fs-extra": "^9.0.0", + "mockttp": "^0.19.2", + "oboe": "^2.1.5", "ora": "^4.0.4", + "proper-lockfile": "^4.1.1", + "shape-hash": "^1.0.6", "tree-kill": "^1.2.2", "uuid": "^8.0.0", - "mockttp": "^0.19.2", - "shape-hash": "^1.0.6", "whatwg-mimetype": "^2.3.0" }, "devDependencies": { + "@types/oboe": "^2.0.29", "@types/whatwg-mimetype": "^2.1.0" }, "files": [ diff --git a/workspaces/cli-shared/src/captures/avro/file-system/dump-capture-saver.ts b/workspaces/cli-shared/src/captures/avro/file-system/dump-capture-saver.ts new file mode 100644 index 0000000000..8203a4b7cf --- /dev/null +++ b/workspaces/cli-shared/src/captures/avro/file-system/dump-capture-saver.ts @@ -0,0 +1,91 @@ +import fs from 'fs-extra'; +import path from 'path'; +import { IHttpInteraction } from '@useoptic/domain-types'; +import oboe from 'oboe'; +import { CaptureSaver } from './capture-saver'; + +async function main(inputFilePath: string, outputBaseDirectory: string) { + console.log({ inputFilePath }); + const input = fs.createReadStream(inputFilePath); + const events: any[] = []; + const captureId = 'ccc'; + const captureBaseDirectory = path.join( + outputBaseDirectory, + '.optic', + 'captures' + ); + const captureSaver = new CaptureSaver({ + captureBaseDirectory, + captureId, + }); + await captureSaver.init(); + await new Promise((resolve, reject) => { + oboe(input) + .on('node', { + // @ts-ignore + 'events.*': function (event: any) { + console.count('event'); + //console.log({ event }); + events.push(event); + }, + 'session.samples.*': function (sample: IHttpInteraction) { + console.count('sample'); + //console.log({ sample }); + captureSaver.save(sample); + }, + }) + .on('done', function () { + console.log('done'); + resolve(); + }) + .on('fail', function (e) { + console.error(e); + reject(e); + }); + }); + + const files = [ + { + location: path.join(outputBaseDirectory, 'optic.yml'), + contents: `name: ${JSON.stringify(path.basename(inputFilePath))}`, + }, + { + location: path.join( + outputBaseDirectory, + '.optic', + 'api', + 'specification.json' + ), + contents: JSON.stringify(events), + }, + { + location: path.join( + outputBaseDirectory, + '.optic', + 'captures', + captureId, + 'optic-capture-state.json' + ), + contents: JSON.stringify({ + captureId, + status: 'completed', + metadata: { + startedAt: new Date().toISOString(), + taskConfig: null, + lastInteraction: null, + }, + }), + }, + ]; + + await Promise.all( + files.map(async (x) => { + const { location, contents } = x; + await fs.ensureDir(path.dirname(location)); + return fs.writeFile(location, contents); + }) + ); +} + +const [, , inputFilePath, outputBaseDirectory] = process.argv; +main(inputFilePath, outputBaseDirectory); From 82727e9e7f88b4efe8216668607ebb19442015c5 Mon Sep 17 00:00:00 2001 From: Aidan Cunniffe Date: Mon, 13 Jul 2020 15:13:43 -0400 Subject: [PATCH 2/2] added utility for turning debug captures into optic projects --- yarn.lock | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/yarn.lock b/yarn.lock index e674fd7f12..2ccd5bdefc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2496,6 +2496,13 @@ resolved "https://registry.yarnpkg.com/@types/npmlog/-/npmlog-4.1.2.tgz#d070fe6a6b78755d1092a3dc492d34c3d8f871c4" integrity sha512-4QQmOF5KlwfxJ5IGXFIudkeLCdMABz03RcUXu+LCb24zmln8QW6aDjuGl4d4XPVLf2j+FnjelHTP7dvceAFbhA== +"@types/oboe@^2.0.29": + version "2.0.29" + resolved "https://registry.yarnpkg.com/@types/oboe/-/oboe-2.0.29.tgz#8bb737ba4afa5308dc647b3dfd2a8ca8d42826ba" + integrity sha512-/20rij3VT0MdlmkKtO9DF7cUpOvshstT4gDlrKRDYXg5P/lqfiRFttGfBcxZqkOm2w4R8JScusn2tT5uN9IEBA== + dependencies: + "@types/node" "*" + "@types/parse-json@^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" @@ -7981,6 +7988,11 @@ http-errors@~1.7.2: statuses ">= 1.5.0 < 2" toidentifier "1.0.0" +http-https@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/http-https/-/http-https-1.0.0.tgz#2f908dd5f1db4068c058cd6e6d4ce392c913389b" + integrity sha1-L5CN1fHbQGjAWM1ubUzjkskTOJs= + "http-parser-js@>=0.4.0 <0.4.11": version "0.4.10" resolved "https://registry.yarnpkg.com/http-parser-js/-/http-parser-js-0.4.10.tgz#92c9c1374c35085f75db359ec56cc257cbb93fa4" @@ -10889,6 +10901,13 @@ object.values@^1.1.0: function-bind "^1.1.1" has "^1.0.3" +oboe@^2.1.5: + version "2.1.5" + resolved "https://registry.yarnpkg.com/oboe/-/oboe-2.1.5.tgz#5554284c543a2266d7a38f17e073821fbde393cd" + integrity sha1-VVQoTFQ6ImbXo48X4HOCH73jk80= + dependencies: + http-https "^1.0.0" + obuf@^1.0.0, obuf@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/obuf/-/obuf-1.1.2.tgz#09bea3343d41859ebd446292d11c9d4db619084e"