Skip to content

Commit 8abde6d

Browse files
HCK-9607: browser support (#27)
* chore: declared `lodash` as external resource * chore: eliminated redundant appDependencies.js; followed sonar suggestions * feat: allowed FE features in browser * chore: added `postinstall` hook
1 parent 079df9f commit 8abde6d

19 files changed

+273
-156
lines changed

api/fe.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { generateScript, generateContainerScript } = require('../forward_engineering/api');
2+
3+
module.exports = {
4+
generateScript,
5+
generateContainerScript,
6+
};

esbuild.package.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const esbuild = require('esbuild');
44
const { clean } = require('esbuild-plugin-clean');
5+
const { copy } = require('esbuild-plugin-copy');
56
const { copyFolderFiles, addReleaseFlag } = require('@hackolade/hck-esbuild-plugins-pack');
67
const { EXCLUDED_EXTENSIONS, EXCLUDED_FILES, DEFAULT_RELEASE_FOLDER_PATH } = require('./buildConstants');
78

@@ -11,6 +12,7 @@ const RELEASE_FOLDER_PATH = path.join(DEFAULT_RELEASE_FOLDER_PATH, `${packageDat
1112
esbuild
1213
.build({
1314
entryPoints: [
15+
path.resolve(__dirname, 'api', 'fe.js'),
1416
path.resolve(__dirname, 'api', 're.js'),
1517
path.resolve(__dirname, 'forward_engineering', 'api.js'),
1618
path.resolve(__dirname, 'reverse_engineering', 'api.js'),
@@ -22,10 +24,17 @@ esbuild
2224
outdir: RELEASE_FOLDER_PATH,
2325
minify: true,
2426
logLevel: 'info',
27+
external: ['lodash'],
2528
plugins: [
2629
clean({
2730
patterns: [DEFAULT_RELEASE_FOLDER_PATH],
2831
}),
32+
copy({
33+
assets: {
34+
from: [path.join('node_modules', 'lodash', '**', '*')],
35+
to: [path.join('node_modules', 'lodash')],
36+
},
37+
}),
2938
copyFolderFiles({
3039
fromPath: __dirname,
3140
targetFolderPath: RELEASE_FOLDER_PATH,

forward_engineering/api.js

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
'use strict';
2-
const { generateCollectionScript } = require('./services/protoScriptGenerationService');
3-
const { setDependencies, dependencies } = require('../reverse_engineering/appDependencies');
4-
const RECORD_NAME_STRATEGY = 'RecordNameStrategy';
5-
const TOPIC_RECORD_NAME_STRATEGY = 'TopicRecordNameStrategy';
6-
const protobufjs = require('protobufjs');
7-
const descriptor = require('protobufjs/ext/descriptor');
1+
const _ = require('lodash');
82
const { formatComment } = require('./helpers/utils');
3+
const { prepareScript } = require('./helpers/prepareScript');
4+
const { generateCollectionScript } = require('./services/protoScriptGenerationService');
95

106
const defaultContainerData = [
117
{
@@ -17,11 +13,8 @@ const defaultContainerData = [
1713

1814
module.exports = {
1915
generateContainerScript(data, logger, callback, app) {
20-
setDependencies(app);
21-
const _ = dependencies.lodash;
2216
const containerData = !_.isEmpty(data.containerData) ? data.containerData : defaultContainerData;
2317
try {
24-
const _ = dependencies.lodash;
2518
let preparedData = {
2619
...data,
2720
containerData,
@@ -69,7 +62,7 @@ module.exports = {
6962
]
7063
.filter(row => row !== '')
7164
.join('\n');
72-
callback(null, this.prepareScript(script, preparedData));
65+
callback(null, prepareScript(script, preparedData));
7366
} catch (error) {
7467
const errorObject = {
7568
message: error.message,
@@ -80,12 +73,10 @@ module.exports = {
8073
callback(errorObject);
8174
}
8275
},
76+
8377
generateScript(data, logger, callback, app) {
84-
setDependencies(app);
85-
const _ = dependencies.lodash;
8678
const containerData = !_.isEmpty(data.containerData) ? data.containerData : defaultContainerData;
8779
try {
88-
const _ = dependencies.lodash;
8980
let preparedData = {
9081
...data,
9182
containerData,
@@ -105,7 +96,7 @@ module.exports = {
10596
]
10697
.filter(row => row !== '')
10798
.join('\n');
108-
callback(null, this.prepareScript(script, preparedData));
99+
callback(null, prepareScript(script, preparedData));
109100
} catch (error) {
110101
const errorObject = {
111102
message: error.message,
@@ -115,75 +106,4 @@ module.exports = {
115106
callback(errorObject);
116107
}
117108
},
118-
119-
prepareScript(script, data) {
120-
const _ = dependencies.lodash;
121-
const targetSchemaRegistry = _.get(data, 'options.targetScriptOptions.keyword');
122-
if (targetSchemaRegistry === 'confluentSchemaRegistry') {
123-
return this.getConfluentPostQuery({ data, schema: script });
124-
}
125-
if (targetSchemaRegistry === 'pulsarSchemaRegistry') {
126-
return this.getPulsarPostQuery({ data, schema: script });
127-
}
128-
129-
return script;
130-
},
131-
132-
getPulsarPostQuery({ data, schema }) {
133-
const _ = dependencies.lodash;
134-
const root = protobufjs.parse(schema).root;
135-
const descriptorMsg = root.toDescriptor('proto3');
136-
const buffer = descriptor.FileDescriptorSet.encode(descriptorMsg).finish();
137-
const fileDescriptorSet = buffer.toString('base64');
138-
const descriptorJson = descriptorMsg.toJSON();
139-
const rootMessageTypeName = `${_.get(descriptorJson, 'file[0].package')}.${_.get(descriptorJson, 'file[0].messageType[0].name')}`;
140-
const rootFileDescriptorName = _.get(descriptorJson, 'file[0].name');
141-
const body = {
142-
fileDescriptorSet,
143-
rootMessageTypeName,
144-
rootFileDescriptorName,
145-
};
146-
const bodyObject = {
147-
type: 'PROTOBUF_NATIVE',
148-
data: body,
149-
properties: {},
150-
};
151-
const namespace = _.get(data, 'containerData[0].name', '');
152-
const topic = _.get(data, 'containerData[0].pulsarTopicName', '');
153-
const persistence = _.get(data, 'containerData[0].isNonPersistentTopic', false)
154-
? 'non-persistent'
155-
: 'persistent';
156-
return `POST /${persistence}/${namespace}/${topic}/schema\n\n${JSON.stringify(bodyObject, null, 4)}`;
157-
},
158-
159-
getConfluentPostQuery({ data, schema }) {
160-
const getName = () => {
161-
const _ = dependencies.lodash;
162-
const name = this.getRecordName(data);
163-
164-
const schemaType = _.get(data, 'containerData[0].schemaType');
165-
const containerName = _.get(data, 'containerData[0].name');
166-
const topic = _.get(data, 'modelData[0].schemaTopic');
167-
168-
const typePostfix = schemaType ? `-${schemaType}` : '';
169-
const containerPrefix = containerName ? `${containerName}.` : '';
170-
const topicPrefix = topic ? `${topic}-` : '';
171-
172-
const schemaNameStrategy = _.get(data, 'modelData[0].schemaNameStrategy', '');
173-
switch (schemaNameStrategy) {
174-
case RECORD_NAME_STRATEGY:
175-
return `${containerPrefix}${name}${typePostfix}`;
176-
case TOPIC_RECORD_NAME_STRATEGY:
177-
return `${topicPrefix}${containerPrefix}${name}${typePostfix}`;
178-
default:
179-
return `${name}${typePostfix}`;
180-
}
181-
};
182-
183-
return `POST /subjects/${getName()}/versions\n\n${schema}`;
184-
},
185-
186-
getRecordName(data) {
187-
return data.containerData[0].code || data.containerData[0].name || data.containerData[0].collectionName;
188-
},
189109
};

forward_engineering/helpers/DefinitionsHelper.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const { dependencies } = require('../../reverse_engineering/appDependencies');
2-
1+
const _ = require('lodash');
32
const MODEL_DEFINITION_REF_REGEX = /#model\/definitions\/(.*)/;
43

54
const parseDefinitions = definitions => {
6-
const _ = dependencies.lodash;
75
return Object.entries(_.get(JSON.parse(definitions), 'properties', {}))
86
.map(([key, value]) => ({ title: key, ...value }))
97
.filter(definition => {
@@ -14,7 +12,6 @@ const parseDefinitions = definitions => {
1412
};
1513

1614
const getDefinitionInfo = (definitions, fieldOptions, referenceId) => {
17-
const _ = dependencies.lodash;
1815
const requiredDefinition = getReferencedDefinition(definitions, referenceId);
1916

2017
if (requiredDefinition) {
@@ -30,7 +27,6 @@ const getDefinitionInfo = (definitions, fieldOptions, referenceId) => {
3027
};
3128

3229
const getReferencedDefinition = (definitions, referenceId) => {
33-
const _ = dependencies.lodash;
3430
return definitions.find(definition =>
3531
_.get(definition, 'definitionRefs', []).some(ref => _.last(ref) === referenceId),
3632
);
@@ -71,7 +67,6 @@ const extractDefinitionsFromProperties = (properties = []) => {
7167
};
7268

7369
const convertEntityTypeToValidName = type => {
74-
const _ = dependencies.lodash;
7570
return _.lowerCase(type).split(' ').join('_');
7671
};
7772

forward_engineering/helpers/FieldNumberGenerationHelper.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
const { dependencies } = require('../../reverse_engineering/appDependencies');
1+
const _ = require('lodash');
22

33
const fixFieldNumbers = ({ fields, oneOfFields, oneOfIndex, reservedNumbers }) => {
4-
const _ = dependencies.lodash;
54
const fieldEntries = Object.entries(fields);
65
fieldEntries.splice(oneOfIndex, 0, ...Object.entries(oneOfFields));
76
const checks = getChecksFromReservedNumbers(reservedNumbers);
@@ -74,8 +73,6 @@ const getChecksFromReservedNumbers = reservedNumbers => {
7473
};
7574

7675
const generateSequence = (fieldsNumber, checks) => {
77-
const _ = dependencies.lodash;
78-
7976
const fieldsNumberPositionRange = _.range(1, fieldsNumber + 1);
8077

8178
return fieldsNumberPositionRange.reduce(
@@ -85,7 +82,6 @@ const generateSequence = (fieldsNumber, checks) => {
8582
};
8683

8784
const getNextFieldNumber = (position, usedNumbers, checks) => {
88-
const _ = dependencies.lodash;
8985
const candidates = _.range(position, position + 1000);
9086
const nextNumber = candidates.find(
9187
candidate => !usedNumbers.includes(candidate) && notReservedField(candidate, checks),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const _ = require('lodash');
2+
3+
const RECORD_NAME_STRATEGY = 'RecordNameStrategy';
4+
const TOPIC_RECORD_NAME_STRATEGY = 'TopicRecordNameStrategy';
5+
6+
const getRecordName = data => {
7+
return data.containerData[0].code || data.containerData[0].name || data.containerData[0].collectionName;
8+
};
9+
10+
const getConfluentPostQuery = ({ data, schema }) => {
11+
const getName = () => {
12+
const name = getRecordName(data);
13+
14+
const schemaType = _.get(data, 'containerData[0].schemaType');
15+
const containerName = _.get(data, 'containerData[0].name');
16+
const topic = _.get(data, 'modelData[0].schemaTopic');
17+
18+
const typePostfix = schemaType ? `-${schemaType}` : '';
19+
const containerPrefix = containerName ? `${containerName}.` : '';
20+
const topicPrefix = topic ? `${topic}-` : '';
21+
22+
const schemaNameStrategy = _.get(data, 'modelData[0].schemaNameStrategy', '');
23+
switch (schemaNameStrategy) {
24+
case RECORD_NAME_STRATEGY:
25+
return `${containerPrefix}${name}${typePostfix}`;
26+
case TOPIC_RECORD_NAME_STRATEGY:
27+
return `${topicPrefix}${containerPrefix}${name}${typePostfix}`;
28+
default:
29+
return `${name}${typePostfix}`;
30+
}
31+
};
32+
33+
return `POST /subjects/${getName()}/versions\n\n${schema}`;
34+
};
35+
36+
module.exports = {
37+
getConfluentPostQuery,
38+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const _ = require('lodash');
2+
const protobufjs = require('protobufjs');
3+
const descriptor = require('protobufjs/ext/descriptor');
4+
5+
const getPulsarPostQuery = ({ data, schema }) => {
6+
const root = protobufjs.parse(schema).root;
7+
const descriptorMsg = root.toDescriptor('proto3');
8+
const buffer = descriptor.FileDescriptorSet.encode(descriptorMsg).finish();
9+
const fileDescriptorSet = buffer.toString('base64');
10+
const descriptorJson = descriptorMsg.toJSON();
11+
const rootMessageTypeName = `${_.get(descriptorJson, 'file[0].package')}.${_.get(descriptorJson, 'file[0].messageType[0].name')}`;
12+
const rootFileDescriptorName = _.get(descriptorJson, 'file[0].name');
13+
const body = {
14+
fileDescriptorSet,
15+
rootMessageTypeName,
16+
rootFileDescriptorName,
17+
};
18+
const bodyObject = {
19+
type: 'PROTOBUF_NATIVE',
20+
data: body,
21+
properties: {},
22+
};
23+
const namespace = _.get(data, 'containerData[0].name', '');
24+
const topic = _.get(data, 'containerData[0].pulsarTopicName', '');
25+
const persistence = _.get(data, 'containerData[0].isNonPersistentTopic', false) ? 'non-persistent' : 'persistent';
26+
return `POST /${persistence}/${namespace}/${topic}/schema\n\n${JSON.stringify(bodyObject, null, 4)}`;
27+
};
28+
29+
module.exports = {
30+
getPulsarPostQuery,
31+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const _ = require('lodash');
2+
const { getConfluentPostQuery } = require('./getConfluentPostQuery');
3+
const { getPulsarPostQuery } = require('./getPulsarPostQuery');
4+
5+
const prepareScript = (script, data) => {
6+
const targetSchemaRegistry = _.get(data, 'options.targetScriptOptions.keyword');
7+
if (targetSchemaRegistry === 'confluentSchemaRegistry') {
8+
return getConfluentPostQuery({ data, schema: script });
9+
}
10+
if (targetSchemaRegistry === 'pulsarSchemaRegistry') {
11+
return getPulsarPostQuery({ data, schema: script });
12+
}
13+
14+
return script;
15+
};
16+
17+
module.exports = {
18+
prepareScript,
19+
};

forward_engineering/services/protoScriptGenerationService.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { dependencies } = require('../../reverse_engineering/appDependencies');
1+
const _ = require('lodash');
22
const {
33
parseDefinitions,
44
getDefinitionInfo,
@@ -98,7 +98,6 @@ const getMessageStatement = ({
9898
modelDefinitions,
9999
externalDefinitions,
100100
}) => {
101-
const _ = dependencies.lodash;
102101
if (jsonSchema.$ref) {
103102
const definitionName = jsonSchema.$ref.slice('#model/definitions/'.length);
104103
jsonSchema = modelDefinitions.find(definition => definition.title === definitionName) || jsonSchema;
@@ -162,7 +161,6 @@ const concutFieldsStatements = (fieldsStatements, oneOfStatement, oneOfIndex) =>
162161

163162
const getOneOfStatement = (oneOfMeta, fields, spacePrefix = '') => {
164163
const oneOfName = oneOfMeta?.name || 'one_of';
165-
const _ = dependencies.lodash;
166164
if (_.isEmpty(fields)) {
167165
return '';
168166
}
@@ -206,7 +204,6 @@ const getOptionStatement = (option, spacePrefix) => {
206204
};
207205

208206
const getReservedStatements = (data, spacePrefix) => {
209-
const _ = dependencies.lodash;
210207
const reservedFieldNames = !_.isEmpty(data.reservedFieldNames)
211208
? `${spacePrefix}reserved ${data.reservedFieldNames};`
212209
: ``;
@@ -228,7 +225,6 @@ const getFieldsStatement = ({
228225
externalDefinitions,
229226
oneOfIndex,
230227
}) => {
231-
const _ = dependencies.lodash;
232228
const oneOfFields = Object.entries(
233229
(jsonSchema.oneOf || []).reduce((properties, property) => ({ ...properties, ...property.properties }), {}),
234230
).reduce((oneOfProperties, [key, value]) => ({ ...oneOfProperties, [key]: { ...value, parent: 'oneOf' } }), {});
@@ -303,7 +299,6 @@ const getFieldInfo = ({
303299
externalDefinitions,
304300
}) => {
305301
const getUDT = udt => {
306-
const _ = dependencies.lodash;
307302
return !_.isEmpty(udt) ? udt : 'string';
308303
};
309304
if (isExternalRef) {
@@ -352,8 +347,6 @@ const getValidatedFieldRule = ({ fieldRule, protoVersion }) => {
352347
};
353348

354349
const getFieldOptionsStatement = options => {
355-
const _ = dependencies.lodash;
356-
357350
const stringifiedOptions = (options || [])
358351
.filter(option => option?.optionKey && option?.optionValue)
359352
.filter(option => option.optionKey !== 'allow_alias')

0 commit comments

Comments
 (0)