Skip to content

perf: optimize buffer input processing #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
node-version: [ 22 ]
node-version: [ 8, 22 ]

runs-on: ${{ matrix.os }}
steps:
Expand All @@ -95,14 +95,15 @@ jobs:
- uses: actions/download-artifact@v4
with:
name: build
- name: Fetch deps
run: npm ci

- name: Run all tests
if: matrix.os == 'ubuntu-latest' && matrix.node-version == 22
run: npm run test
run: |
npm ci
npm run test
timeout-minutes: 1

- name: Run units
if: matrix.os != 'ubuntu-latest' && matrix.node-version != 22
run: npm run test:unit
if: matrix.os != 'ubuntu-latest' || matrix.node-version != 22
run: npm run test:smoke:cjs
timeout-minutes: 1
289 changes: 128 additions & 161 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"test:unit": "vitest run --dir src/test/ts --coverage.enabled --coverage.include src/main/ts --coverage.reportsDirectory target/coverage --coverage.thresholds.100",
"test:jsr": "jsr publish --dry-run",
"test:audit": "npm audit",
"test:smoke:cjs": "node ./src/test/smoke/smoke.cjs",
"test:smoke:mjs": "node ./src/test/smoke/smoke.mjs",
"publish:draft": "npm run build && npm publish --no-git-tag-version"
},
"publishConfig": {
Expand Down
4 changes: 3 additions & 1 deletion src/main/ts/envapi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fs from 'node:fs'
import path from 'node:path'
import { TextDecoder } from 'node:util'

const DOTENV = '.env'
const Q1 = '"' // double quote
const Q2 = "'" // single quote
const Q3 = '`' // backtick

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

for (const c of content.toString().replace(/\r\n?/mg, '\n')) {
for (const c of (typeof content === 'string' ? content : decoder.decode(content))) {
if (i) {
if (c === '\n') i = 0
continue
Expand Down
2 changes: 2 additions & 0 deletions src/test/smoke/smoke.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const envapi = require('../../../target/cjs/index.cjs')
envapi.stringify(envapi.parse('foo=bar'))
2 changes: 2 additions & 0 deletions src/test/smoke/smoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import envapi from '../../../target/esm/index.mjs'
envapi.stringify(envapi.parse('foo=bar'))
13 changes: 12 additions & 1 deletion src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MULTILINE = """
long text here, # not-comment
e.g. a private SSH key
"""
ENV=v1\nENV2=v2\n\n\n\t\t ENV3 = 'v"3' \n export ENV4="v\`4"
ENV=v1\nENV2=v2\r\n\n\r\n\t\t ENV3 = 'v"3' \n export ENV4="v\`4"
ENV5="v'5" # comment
ENV6=\`v'"6\`
ENV7=
Expand Down Expand Up @@ -71,6 +71,17 @@ JSONSTR='{"foo": "b a r"}'`
)
})

test('accepts buffer input', () => {
const str = 'FOO=BAR\r\nBAz=QUZ'
const env = {
FOO: 'BAR',
BAz: 'QUZ',
}

assert.deepEqual(parse(Buffer.from(str, 'utf8')), env)
assert.deepEqual(parse(Buffer.from(str, 'ascii')), env)
})

test('throws on invalid input', () => {
assert.throws(() => parse('BRO-KEN=xyz123'))
assert.throws(() => parse('BRO KEN=xyz123'))
Expand Down
Loading