Skip to content

Commit a057e38

Browse files
perf: optimize buffer input processing (#4)
* refactor: samantic proccesing of buffer * build: add nodejs@7 to ci * refactor: simplify buf decoder, add smoke tests * ci: fix test flow * ci: fix typo --------- Co-authored-by: Anton Golub <[email protected]>
1 parent 6d45387 commit a057e38

File tree

7 files changed

+156
-169
lines changed

7 files changed

+156
-169
lines changed

.github/workflows/ci.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
strategy:
8282
matrix:
8383
os: [ ubuntu-latest, windows-latest ]
84-
node-version: [ 22 ]
84+
node-version: [ 8, 22 ]
8585

8686
runs-on: ${{ matrix.os }}
8787
steps:
@@ -95,14 +95,15 @@ jobs:
9595
- uses: actions/download-artifact@v4
9696
with:
9797
name: build
98-
- name: Fetch deps
99-
run: npm ci
98+
10099
- name: Run all tests
101100
if: matrix.os == 'ubuntu-latest' && matrix.node-version == 22
102-
run: npm run test
101+
run: |
102+
npm ci
103+
npm run test
103104
timeout-minutes: 1
104105

105106
- name: Run units
106-
if: matrix.os != 'ubuntu-latest' && matrix.node-version != 22
107-
run: npm run test:unit
107+
if: matrix.os != 'ubuntu-latest' || matrix.node-version != 22
108+
run: npm run test:smoke:cjs
108109
timeout-minutes: 1

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"test:unit": "vitest run --dir src/test/ts --coverage.enabled --coverage.include src/main/ts --coverage.reportsDirectory target/coverage --coverage.thresholds.100",
3333
"test:jsr": "jsr publish --dry-run",
3434
"test:audit": "npm audit",
35+
"test:smoke:cjs": "node ./src/test/smoke/smoke.cjs",
36+
"test:smoke:mjs": "node ./src/test/smoke/smoke.mjs",
3537
"publish:draft": "npm run build && npm publish --no-git-tag-version"
3638
},
3739
"publishConfig": {

src/main/ts/envapi.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3+
import { TextDecoder } from 'node:util'
34

45
const DOTENV = '.env'
56
const Q1 = '"' // double quote
67
const Q2 = "'" // single quote
78
const Q3 = '`' // backtick
89

10+
const decoder = new TextDecoder()
911
export const parse = (content: string | Buffer): NodeJS.ProcessEnv => {
1012
const kr = /^[a-zA-Z_]+\w*$/
1113
const sr = /\s/
@@ -22,7 +24,7 @@ export const parse = (content: string | Buffer): NodeJS.ProcessEnv => {
2224
}
2325
}
2426

25-
for (const c of content.toString().replace(/\r\n?/mg, '\n')) {
27+
for (const c of (typeof content === 'string' ? content : decoder.decode(content))) {
2628
if (i) {
2729
if (c === '\n') i = 0
2830
continue

src/test/smoke/smoke.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const envapi = require('../../../target/cjs/index.cjs')
2+
envapi.stringify(envapi.parse('foo=bar'))

src/test/smoke/smoke.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import envapi from '../../../target/esm/index.mjs'
2+
envapi.stringify(envapi.parse('foo=bar'))

src/test/ts/index.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ MULTILINE = """
2929
long text here, # not-comment
3030
e.g. a private SSH key
3131
"""
32-
ENV=v1\nENV2=v2\n\n\n\t\t ENV3 = 'v"3' \n export ENV4="v\`4"
32+
ENV=v1\nENV2=v2\r\n\n\r\n\t\t ENV3 = 'v"3' \n export ENV4="v\`4"
3333
ENV5="v'5" # comment
3434
ENV6=\`v'"6\`
3535
ENV7=
@@ -71,6 +71,17 @@ JSONSTR='{"foo": "b a r"}'`
7171
)
7272
})
7373

74+
test('accepts buffer input', () => {
75+
const str = 'FOO=BAR\r\nBAz=QUZ'
76+
const env = {
77+
FOO: 'BAR',
78+
BAz: 'QUZ',
79+
}
80+
81+
assert.deepEqual(parse(Buffer.from(str, 'utf8')), env)
82+
assert.deepEqual(parse(Buffer.from(str, 'ascii')), env)
83+
})
84+
7485
test('throws on invalid input', () => {
7586
assert.throws(() => parse('BRO-KEN=xyz123'))
7687
assert.throws(() => parse('BRO KEN=xyz123'))

0 commit comments

Comments
 (0)