Skip to content

Commit

Permalink
Remove fs requirement in paima-sdk (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt authored Nov 18, 2023
1 parent 798a7e0 commit ac57618
Show file tree
Hide file tree
Showing 9 changed files with 752 additions and 607 deletions.
1,290 changes: 724 additions & 566 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prebuild": "npx nx run-many --parallel=${NX_PARALLEL:-3} -t prebuild",
"build": "npm run lint:configs && npx nx run-many --parallel=${NX_PARALLEL:-3} -t build",
"test": "npm run lint:configs && npx nx run-many --parallel=${NX_PARALLEL:-3} -t test",
"release:lib": "./wipe.sh && sh ./tools/scripts/bump-version.sh && npm run build && npm run lint && npm run test && read -p 'Enter OTP: ' otp && export NPM_CONFIG_OTP=$otp && npx nx release publish -g paima-sdk && npx nx release publish -g node-sdk",
"release:lib": "./wipe.sh && sh ./tools/scripts/bump-version.sh && npm run build && npm run lint && npm run test && read -p 'Enter OTP: ' otp && export NPM_CONFIG_OTP=$otp && npx nx release publish -g paima-sdk && npx nx release publish -g node-sdk && npx nx release publish -g build-utils",
"release:bin": "./wipe.sh && npm run lint:configs && npx nx run-many --parallel=${NX_PARALLEL:-3} -t release && mkdir -p ./bin && cp -r ./packages/engine/paima-standalone/packaged/@standalone/* ./bin"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ if (
)
throw new Error('Please ensure you have filled out your .env file');

// mock out fs as we can't use it in browser builds
const fsaReplace: esbuild.Plugin = {
name: 'fsa-replace',
setup(build) {
build.onResolve({ filter: /fsa\.js/ }, args => {
const mockFile = args.path.replace('fsa.js', 'fsa_empty.js');
return { path: `${args.resolveDir}/${mockFile}`, namespace: args.namespace };
});
},
};

export const config: esbuild.BuildOptions = {
// JS output from previous compilation step used here instead of index.ts to have more control over the TS build process
// however, that means type definitions will be lost until this is implemented: https://github.com/evanw/esbuild/issues/95#issuecomment-1559710310
Expand All @@ -44,6 +33,6 @@ export const config: esbuild.BuildOptions = {
format: 'esm',
define,
outfile: 'packaged/middleware.js',
plugins: [polyfillNode({}), fsaReplace],
plugins: [polyfillNode({})],
external: ['pg-native'],
};
9 changes: 9 additions & 0 deletions packages/engine/paima-standalone/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
import { config } from 'dotenv';
import { argumentRouter } from './utils/input.js';

import * as fs from 'fs';
import { setLogger } from '@paima/utils';

setLogger(s => {
try {
fs.appendFileSync('./logs.log', `${s}\n`);
} catch (e) {}
});

// Load environment variables
config({ path: `${process.cwd()}/.env.${process.env.NODE_ENV || 'development'}` });

Expand Down
13 changes: 1 addition & 12 deletions packages/paima-sdk/paima-mw-core/esbuildconfig.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ if (
throw new Error('Please ensure you have filled out your .env file');
}

/** @type esbuild.Plugin */
const fsaReplace = {
name: 'fsa-replace',
setup(build) {
build.onResolve({ filter: /fsa\.js/ }, (args) => {
const mockFile = args.path.replace('fsa.js', 'fsa_empty.js');
return { path: `${args.resolveDir}/${mockFile}`, namespace: args.namespace };
});
}
};

const config = {
entryPoints: ['build/index.js'],
bundle: true,
Expand All @@ -50,7 +39,7 @@ const config = {

define,
outfile: 'web/middleware.js',
plugins: [polyfillNode({}), fsaReplace],
plugins: [polyfillNode({})],
external: ['pg-native'],
};

Expand Down
7 changes: 0 additions & 7 deletions packages/paima-sdk/paima-utils/src/fs_access/fsa.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/paima-sdk/paima-utils/src/fs_access/fsa_empty.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/paima-sdk/paima-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import erc165ContractBuild from './artifacts/ERC165Contract.js';
import erc6551RegistryContractBuild from './artifacts/ERC6551RegistryContract.js';
import oldErc6551RegistryContractBuild from './artifacts/OldERC6551RegistryContract.js';
import type * as Contracts from './contract-types/index.js';
import { doLog, logError } from './logging.js';
import { doLog, setLogger, logError } from './logging.js';
import type { Deployment, ErrorCode, ErrorMessageFxn, ErrorMessageMapping } from './types.js';
import {
AddressType,
Expand All @@ -37,6 +37,7 @@ export {
ChainDataExtensionDatumType,
DEFAULT_FUNNEL_TIMEOUT,
logError,
setLogger,
doLog,
};

Expand Down
18 changes: 14 additions & 4 deletions packages/paima-sdk/paima-utils/src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { stringify } from 'flatted';
import * as fsa from './fs_access/fsa.js';

export function logError(error: unknown): void {
doLog(`***ERROR***`);
doLog(error);
doLog(`***`);
}

type LoggerFunc = (str: string) => void;

class LoggerHolder {
static INSTANCE = new LoggerHolder();
log: LoggerFunc = _s => {};
}

export function setLogger(newLogger: LoggerFunc): void {
LoggerHolder.INSTANCE.log = newLogger;
}

// TODO: probably we want to unify this with pushLog
export function doLog(...s: unknown[]): void {
console.log(...s);
Expand All @@ -15,12 +25,12 @@ export function doLog(...s: unknown[]): void {
if (typeof str === 'function') {
continue;
}
fsa.appendToFile(String(str));
LoggerHolder.INSTANCE.log(String(str));
} else if (str instanceof Error) {
fsa.appendToFile(`${str.name}: ${str.message}\nStack: ${str.stack}`);
LoggerHolder.INSTANCE.log(`${str.name}: ${str.message}\nStack: ${str.stack}`);
} else {
try {
fsa.appendToFile(stringify(str));
LoggerHolder.INSTANCE.log(stringify(str));
} catch (e) {
// should not happen, but maybe there is some type that fails for some reason
}
Expand Down

0 comments on commit ac57618

Please sign in to comment.