Skip to content

Commit eb389ee

Browse files
committed
Actually build utils for node & web
1 parent fe18df1 commit eb389ee

26 files changed

+885
-50
lines changed

package-lock.json

Lines changed: 535 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/utils/jest.config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
export { default } from '../../jest.config'
1+
import type { Config } from "@jest/types"
2+
import defaultConfig from "../../jest.config"
3+
4+
const config: Config.InitialOptions = {
5+
...defaultConfig,
6+
moduleNameMapper: {
7+
"^@crypto$": "<rootDir>/src/browser/crypto",
8+
"^@md5$": "<rootDir>/src/browser/md5",
9+
},
10+
displayName: "@streamr/utils (browser)",
11+
}
12+
13+
export default config

packages/utils/karma.config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { createKarmaConfig, createWebpackConfig } from '@streamr/browser-test-runner'
2+
import { resolve } from 'path'
23

34
const TEST_PATHS = ['test/**/*.ts']
45

56
export default createKarmaConfig(TEST_PATHS, createWebpackConfig({
67
libraryName: 'utils',
78
fallback: {
89
module: false
9-
}
10+
},
11+
alias: {
12+
'@crypto': resolve(__dirname, 'src/browser/crypto.ts'),
13+
'@md5': resolve(__dirname, 'src/browser/md5.ts'),
14+
os: resolve(__dirname, 'src/browser/os.ts'),
15+
path: 'path-browserify',
16+
},
1017
}))

packages/utils/package.json

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,28 @@
77
"url": "git+https://github.com/streamr-dev/network.git",
88
"directory": "packages/utils"
99
},
10-
"main": "./dist/src/exports.js",
11-
"types": "./dist/src/exports.d.ts",
10+
"types": "./dist/node/src/exports.d.ts",
11+
"exports": {
12+
".": {
13+
"browser": {
14+
"types": "./dist/browser/src/exports.d.ts",
15+
"import": "./dist/browser/src/exports.js",
16+
"require": "./dist/browser/src/exports.cjs"
17+
},
18+
"types": "./dist/node/src/exports.d.ts",
19+
"import": "./dist/node/src/exports.js",
20+
"require": "./dist/node/src/exports.cjs"
21+
}
22+
},
1223
"files": [
13-
"dist",
14-
"!*.tsbuildinfo",
24+
"dist/node/src/exports.*",
25+
"dist/browser/src/exports.*",
1526
"README.md",
1627
"LICENSE"
1728
],
1829
"scripts": {
1930
"build": "tsc -b",
31+
"postbuild": "rollup -c dist/rollup.config.mjs",
2032
"check": "tsc -p tsconfig.jest.json",
2133
"clean": "jest --clearCache || true; rm -rf dist *.tsbuildinfo node_modules/.cache || true",
2234
"eslint": "eslint --cache --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'",
@@ -28,16 +40,26 @@
2840
"dependencies": {
2941
"@noble/curves": "^1.9.7",
3042
"@noble/post-quantum": "^0.4.1",
43+
"buffer": "^6.0.3",
44+
"buffer-shim": "^1.0.1",
3145
"eventemitter3": "^5.0.0",
3246
"lodash": "^4.17.21",
47+
"md5": "^2.3.0",
48+
"path-browserify": "^1.0.1",
3349
"pino": "^10.1.0",
3450
"pino-pretty": "^13.1.2",
51+
"readable-stream": "^4.7.0",
3552
"secp256k1": "^5.0.1",
3653
"sha3": "^2.1.4"
3754
},
3855
"devDependencies": {
56+
"@rollup/plugin-alias": "^6.0.0",
57+
"@rollup/plugin-node-resolve": "^16.0.3",
3958
"@streamr/browser-test-runner": "^0.0.1",
4059
"@types/lodash": "^4.17.21",
41-
"@types/secp256k1": "^4.0.7"
60+
"@types/md5": "^2.3.6",
61+
"@types/secp256k1": "^4.0.7",
62+
"rollup": "^4.53.3",
63+
"rollup-plugin-dts": "^6.3.0"
4264
}
4365
}

packages/utils/rollup.config.mts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { defineConfig, type RollupOptions } from 'rollup'
2+
import { dts } from 'rollup-plugin-dts'
3+
import alias, { type Alias } from '@rollup/plugin-alias'
4+
import resolve from '@rollup/plugin-node-resolve'
5+
6+
const nodejsAliases: Alias[] = [
7+
{
8+
find: '@crypto',
9+
replacement: './node/crypto.js',
10+
},
11+
{
12+
find: '@md5',
13+
replacement: './node/md5.js',
14+
},
15+
]
16+
17+
const browserAliases: Alias[] = [
18+
{
19+
find: '@crypto',
20+
replacement: './browser/crypto.js',
21+
},
22+
{
23+
find: '@md5',
24+
replacement: './browser/md5.js',
25+
},
26+
{
27+
find: 'os',
28+
replacement: './browser/os.js',
29+
},
30+
{
31+
find: 'path',
32+
replacement: 'path-browserify',
33+
},
34+
{
35+
find: 'stream',
36+
replacement: 'readable-stream',
37+
},
38+
{
39+
find: /^pino$/,
40+
replacement: 'pino/browser',
41+
},
42+
]
43+
44+
export default defineConfig([
45+
nodejs(),
46+
nodejsTypes(),
47+
browser(),
48+
browserTypes(),
49+
])
50+
51+
function nodejs(): RollupOptions {
52+
return {
53+
input: 'dist/node/src/exports.js',
54+
output: [
55+
{
56+
format: 'es',
57+
file: 'dist/node/src/exports.js',
58+
sourcemap: true,
59+
},
60+
{
61+
format: 'cjs',
62+
file: 'dist/node/src/exports.cjs',
63+
sourcemap: true,
64+
},
65+
],
66+
plugins: [
67+
alias({
68+
entries: nodejsAliases,
69+
}),
70+
resolve({ preferBuiltins: true, resolveOnly: () => false }),
71+
],
72+
}
73+
}
74+
75+
function browser(): RollupOptions {
76+
return {
77+
input: 'dist/browser/src/exports.js',
78+
output: [
79+
{
80+
format: 'es',
81+
file: 'dist/browser/src/exports.js',
82+
sourcemap: true,
83+
},
84+
{
85+
format: 'cjs',
86+
file: 'dist/browser/src/exports.cjs',
87+
sourcemap: true,
88+
},
89+
],
90+
plugins: [
91+
alias({
92+
entries: browserAliases,
93+
}),
94+
resolve({ browser: true, resolveOnly: () => false }),
95+
],
96+
}
97+
}
98+
99+
function nodejsTypes(): RollupOptions {
100+
return {
101+
input: 'dist/node/src/exports.d.ts',
102+
output: [
103+
{
104+
file: 'dist/node/src/exports.d.ts',
105+
},
106+
],
107+
plugins: [
108+
alias({
109+
entries: nodejsAliases,
110+
}),
111+
resolve(),
112+
dts(),
113+
],
114+
}
115+
}
116+
117+
function browserTypes(): RollupOptions {
118+
return {
119+
input: 'dist/browser/src/exports.d.ts',
120+
output: [
121+
{
122+
file: 'dist/browser/src/exports.d.ts',
123+
},
124+
],
125+
plugins: [
126+
alias({
127+
entries: browserAliases,
128+
}),
129+
resolve({ browser: true }),
130+
dts(),
131+
],
132+
}
133+
}

packages/utils/src/Logger.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,25 @@ const parseBoolean = (value: string | undefined) => {
2020

2121
declare let window: any
2222

23+
function env(key: 'LOG_LEVEL' | 'NOLOG' | 'JEST_WORKER_ID' | 'DISABLE_PRETTY_LOG' | 'LOG_COLORS' | 'STREAMR_APPLICATION_ID'): string | undefined {
24+
return (typeof process === 'undefined' ? {} : process.env)[key]
25+
}
26+
2327
/**
2428
* Disabled when in browser or when environment variable DISABLE_PRETTY_LOG is set to true.
2529
*/
2630
function isPrettyPrintDisabled(): boolean {
27-
return typeof window === 'object' || (parseBoolean(process.env.DISABLE_PRETTY_LOG) ?? false)
31+
return typeof window === 'object' || (parseBoolean(env('DISABLE_PRETTY_LOG')) ?? false)
2832
}
2933

3034
function isJestRunning(): boolean {
31-
return process.env.JEST_WORKER_ID !== undefined
35+
return env('JEST_WORKER_ID') !== undefined
3236
}
3337

3438
const rootLogger = pino({
3539
name: 'rootLogger',
36-
enabled: !process.env.NOLOG,
37-
level: process.env.LOG_LEVEL ?? 'info',
40+
enabled: !env('NOLOG'),
41+
level: env('LOG_LEVEL') ?? 'info',
3842
formatters: {
3943
level: (label) => {
4044
return { level: label } // log level as string instead of number
@@ -43,7 +47,7 @@ const rootLogger = pino({
4347
transport: isPrettyPrintDisabled() ? undefined : {
4448
target: 'pino-pretty',
4549
options: {
46-
colorize: parseBoolean(process.env.LOG_COLORS) ?? true,
50+
colorize: parseBoolean(env('LOG_COLORS')) ?? true,
4751
singleLine: true,
4852
translateTime: 'yyyy-mm-dd"T"HH:MM:ss.l',
4953
ignore: 'pid,hostname',
@@ -95,7 +99,7 @@ export class Logger {
9599
name: Logger.createName(loggerModule),
96100
...contextBindings
97101
}, {
98-
level: process.env.LOG_LEVEL ?? defaultLogLevel
102+
level: env('LOG_LEVEL') ?? defaultLogLevel
99103
})
100104
this.fatal = wrappedMethodCall(this.logger.fatal.bind(this.logger))
101105
this.error = wrappedMethodCall(this.logger.error.bind(this.logger))
@@ -114,7 +118,7 @@ export class Logger {
114118
const parts = parsedPath.dir.split(path.sep)
115119
fileId = parts[parts.length - 1]
116120
}
117-
const longName = without([process.env.STREAMR_APPLICATION_ID, fileId], undefined).join(':')
121+
const longName = without([env('STREAMR_APPLICATION_ID'), fileId], undefined).join(':')
118122
return isPrettyPrintDisabled() ?
119123
longName : padEnd(longName.substring(0, this.NAME_LENGTH), this.NAME_LENGTH, ' ')
120124
}

packages/utils/src/SigningUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { randomBytes } from '@noble/post-quantum/utils'
66
import { p256 } from '@noble/curves/p256'
77
import { areEqualBinaries, binaryToHex } from './binaryUtils'
88
import type { UserIDRaw } from './UserID'
9-
import { getSubtle } from './crossPlatformCrypto'
9+
import { getSubtle } from '@crypto'
1010
import type { webcrypto } from 'crypto'
1111

1212
export const KEY_TYPES = [

packages/utils/src/TheGraphClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class TheGraphClient {
3636
) {
3737
this.serverUrl = opts.serverUrl
3838
this.fetch = opts.fetch
39-
this.logger = opts.logger ?? new Logger(module)
39+
this.logger = opts.logger ?? new Logger('TheGraphClient')
4040
this.indexingState = new IndexingState(
4141
() => this.getIndexBlockNumber(),
4242
opts.indexTimeout ?? 60000,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { webcrypto } from 'crypto'
2+
3+
export function getSubtle(): webcrypto.SubtleCrypto {
4+
if (!crypto.subtle) {
5+
const url =
6+
'https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto'
7+
throw new Error(
8+
`SubtleCrypto not supported. This feature is available only in secure contexts (HTTPS). ${url}`
9+
)
10+
}
11+
12+
return crypto.subtle as webcrypto.SubtleCrypto
13+
}

packages/utils/src/browser/md5.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import md5 from 'md5'
2+
import { hexToBinary } from '../binaryUtils'
3+
4+
export function computeMd5(input: string): Buffer {
5+
return Buffer.from(hexToBinary(md5(input)))
6+
}

0 commit comments

Comments
 (0)