diff --git a/workspaces/cli-shared/package.json b/workspaces/cli-shared/package.json index 90587dab50..9f1944232c 100644 --- a/workspaces/cli-shared/package.json +++ b/workspaces/cli-shared/package.json @@ -23,12 +23,13 @@ "@useoptic/domain-types": "8.3.1", "@useoptic/domain-utilities": "8.3.1", "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", "tree-kill": "^1.2.2", "uuid": "^8.0.0", "shape-hash": "^1.0.6", 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..1921662eb9 --- /dev/null +++ b/workspaces/cli-shared/src/captures/avro/file-system/dump-capture-saver.ts @@ -0,0 +1,92 @@ +import fs from 'fs-extra'; +import path from 'path'; +import { IHttpInteraction } from '@useoptic/domain-types'; +//@ts-ignore +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: any) { + 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); diff --git a/yarn.lock b/yarn.lock index 7981de2a64..1e0479fe87 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8298,6 +8298,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.5.1: version "0.5.2" resolved "https://registry.yarnpkg.com/http-parser-js/-/http-parser-js-0.5.2.tgz#da2e31d237b393aae72ace43882dd7e270a8ff77" @@ -11213,6 +11218,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"