Skip to content

Commit

Permalink
Use/export explicit functions for loading SBVR files
Browse files Browse the repository at this point in the history
This is in preference to using the require loader which is not very
portable when mixing commonjs and esm code

Change-type: patch
  • Loading branch information
Page- committed Dec 30, 2024
1 parent 83eacbb commit ac7e96a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/extended-sbvr-parser/extended-sbvr-parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SBVRParser } from '@balena/sbvr-parser';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Types: string = require('@balena/sbvr-types/Type.sbvr');
import { requireSBVR } from '../server-glue/sbvr-loader';
import { version as sbvrParserVersion } from '@balena/sbvr-parser/package.json';
import { version } from '../config-loader/env';

const Types = requireSBVR('@balena/sbvr-types/Type.sbvr', require);

export const ExtendedSBVRParser = SBVRParser._extend({
initialize() {
SBVRParser.initialize.call(this);
Expand Down
5 changes: 2 additions & 3 deletions src/http-transactions/transactions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { odataNameToSqlName } from '@balena/odata-to-abstract-sql';
// @ts-expect-error b/c TS doesn't know what the result of requiring an sbvr file would be
// eslint-disable-next-line @typescript-eslint/no-var-requires
const transactionModel = require('./transaction.sbvr');
import { requireSBVR } from '../server-glue/sbvr-loader';
const transactionModel = requireSBVR('./transaction.sbvr', require);

/** @type {import('../config-loader/config-loader').Config} */
export const config = {
Expand Down
4 changes: 2 additions & 2 deletions src/migrator/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import type { Config, Model } from '../config-loader/config-loader';

import _ from 'lodash';
import * as sbvrUtils from '../sbvr-api/sbvr-utils';
import { requireSBVR } from '../server-glue/sbvr-loader';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const migrationsModel = require('./migrations.sbvr');
const migrationsModel = requireSBVR('./migrations.sbvr', require);

type ApiRootModel = Model & { apiRoot: string };

Expand Down
4 changes: 2 additions & 2 deletions src/sbvr-api/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ import memoizeWeak from 'memoizee/weak';
import type { Config } from '../config-loader/config-loader';
import type { ODataOptions } from 'pinejs-client-core';
import type { Permission } from './user';
import { requireSBVR } from '../server-glue/sbvr-loader';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const userModel: string = require('./user.sbvr');
const userModel = requireSBVR('./user.sbvr', require);

const DEFAULT_ACTOR_BIND = '@__ACTOR_ID';
const DEFAULT_ACTOR_BIND_REGEX = new RegExp(
Expand Down
4 changes: 2 additions & 2 deletions src/sbvr-api/sbvr-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ import { ExtendedSBVRParser } from '../extended-sbvr-parser/extended-sbvr-parser
import * as asyncMigrator from '../migrator/async';
import * as syncMigrator from '../migrator/sync';
import { generateODataMetadata } from '../odata-metadata/odata-metadata-generator';
import { requireSBVR } from '../server-glue/sbvr-loader';

import type DevModel from './dev';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const devModel = require('./dev.sbvr');
const devModel = requireSBVR('./dev.sbvr', require);
import * as permissions from './permissions';
import {
BadRequestError,
Expand Down
25 changes: 25 additions & 0 deletions src/server-glue/sbvr-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,28 @@ if (!process.browser) {
nodeRequire.extensions['.sbvr'] = (module: NodeModule, filename: string) =>
(module.exports = fs.readFileSync(filename, 'utf8'));
}

/**
*
* @param filePath The module to load
* @param parentUrl Use `import.meta.url`
* @returns The sbvr file contents
*/
export async function importSBVR(filePath: string, meta: ImportMeta) {
return await (
await import('fs')
).promises.readFile(new URL(meta.resolve(filePath)), 'utf8');
}

/**
*
* @param filePath The module to load
* @param parentUrl Use `require`
* @returns The sbvr file contents
*/
export function requireSBVR(filePath: string, require: NodeRequire) {
return (require('fs') as typeof import('fs')).readFileSync(
require.resolve(filePath),
'utf8',
);
}
4 changes: 2 additions & 2 deletions src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { Worker } from './worker';
import type TasksModel from './tasks';
import type { Task } from './tasks';
import type { FromSchema } from 'json-schema-to-ts';
import { requireSBVR } from '../server-glue/sbvr-loader';

export type * from './tasks';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const modelText: string = require('./tasks.sbvr');
const modelText = requireSBVR('./tasks.sbvr', require);

// Create index for polling tasks table
const initSql = `
Expand Down

0 comments on commit ac7e96a

Please sign in to comment.