From 106bd1dd5885b7d7575ab1c0cd3ab9520a33424c Mon Sep 17 00:00:00 2001 From: ngxingyu Date: Wed, 28 Apr 2021 04:54:12 +0800 Subject: [PATCH] fix: change regex lookbehind to group (#29) fixes runtime errors in safari --- CHANGELOG.md | 32 ++++++++++++++------------- example/lib/main.dart | 2 +- lib/src/dotenv.dart | 2 +- lib/src/parser.dart | 13 ++++++----- pubspec.lock | 50 +++++++++++++++++++++---------------------- pubspec.yaml | 2 +- test/.env | 2 +- test/dotenv_test.dart | 4 ++-- test/parser_test.dart | 6 +++--- 9 files changed, 59 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 154c422..b6a6a9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ Release notes are available on [github][notes]. [pub-semver-readme]: https://pub.dartlang.org/packages/pub_semver [notes]: https://github.com/java-james/flutter_dotenv/releases +### 4.0.0-nullsafety.1 + +- [fix] Remove lookbehind regex to support safari browser + 4.0.0-nullsafety.0 ----- @@ -21,20 +25,20 @@ Release notes are available on [github][notes]. - [new] Migrate to null safety - [new] Create unit test cases for parse - -#### 3.1.0 +### 3.1.0 - [new] Allow merging with a custom map on load -#### 3.0.2 +### 3.0.2 - [chore] Format code with dart fmt -#### 3.0.1 +### 3.0.1 - [docs] Use secure links -#### 3.0.0 +3.0.0 +----- - [new] Merge with Platform.Environment - [new] Throw precise errors @@ -42,19 +46,19 @@ Release notes are available on [github][notes]. - [new] Improved Parsing - [docs] Example project -#### 2.1.0 +### 2.1.0 - [new] Support '=' sign in value -#### 2.0.3 +### 2.0.3 - [fix] Warning when using with flutter_test -#### 2.0.2 +### 2.0.2 - [fix] Flutter 1.9.5 compatibility ensure binding was initialized -#### 2.0.1 +### 2.0.1 - [docs] tweak app description - [fix] increase meta version range @@ -81,16 +85,15 @@ Release notes are available on [github][notes]. - [fix] allow braces with `${var}` substitution [#10][] -0.1.3 ------ +### 0.1.3 - [new] add command-line interface [#7][], [#8][] - [deps] add [args][]@v0.13 [args]: https://pub.dartlang.org/packages/args -0.1.2 ------ +### 0.1.2 + - [new] support variable substitution from `Platform.environment` [#6][] - [deps] drop [logging][] @@ -103,8 +106,7 @@ Release notes are available on [github][notes]. - [fix] whitespace causes quotes not to be stripped -0.1.1 ------ +### 0.1.1 - [deprecated] `Parser` internals will become private. [#3][] - `#unquote`, `#strip`, `#swallow`, `#parseOne`, `#surroundingQuote`, `#interpolate` diff --git a/example/lib/main.dart b/example/lib/main.dart index fbf72c7..6397805 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -3,7 +3,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart' as dotenv; Future main() async { - await dotenv.load(mergeWith: { + await dotenv.load(fileName: "assets/.env", mergeWith: { 'TEST_VAR': '5', }); // mergeWith optional, you can include Platform.environment for Mobile/Desktop app diff --git a/lib/src/dotenv.dart b/lib/src/dotenv.dart index 13e24c0..649ef25 100644 --- a/lib/src/dotenv.dart +++ b/lib/src/dotenv.dart @@ -4,9 +4,9 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart' show rootBundle; import 'package:flutter/widgets.dart'; -import 'package:flutter_dotenv/src/parser.dart'; import 'errors.dart'; +import 'parser.dart'; /// Loads environment variables from a `.env` file. /// diff --git a/lib/src/parser.dart b/lib/src/parser.dart index 783edbd..e6bdabf 100644 --- a/lib/src/parser.dart +++ b/lib/src/parser.dart @@ -6,8 +6,7 @@ class Parser { static final _comment = RegExp(r'''#[^'"]*$'''); static final _commentWithQuotes = RegExp(r'''#.*$'''); static final _surroundQuotes = RegExp(r'''^(["'])(.*?[^\\])\1'''); - static final _bashVar = - RegExp(r'(?<=^|[^\\])(\$)(?:{)?([a-zA-Z_][\w]*)+(?:})?'); + static final _bashVar = RegExp(r'''(\\)?(\$)(?:{)?([a-zA-Z_][\w]*)+(?:})?'''); /// [Parser] methods are pure functions. const Parser(); @@ -52,9 +51,13 @@ class Parser { /// Substitutes $bash_vars in [val] with values from [env]. String interpolate(String val, Map env) => val.replaceAllMapped(_bashVar, (m) { - var k = m.group(2)!; - if (!_has(env, k)) return ''; - return env[k]!; + if ((m.group(1) ?? "") == "\\") { + return m.input.substring(m.start, m.end); + } else { + var k = m.group(3)!; + if (!_has(env, k)) return ''; + return env[k]!; + } }); /// If [val] is wrapped in single or double quotes, returns the quote character. diff --git a/pubspec.lock b/pubspec.lock index 8bb5271..c872358 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -28,28 +28,28 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0-nullsafety.3" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.5" + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" cli_util: dependency: transitive description: @@ -63,14 +63,14 @@ packages: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0-nullsafety.5" + version: "1.15.0" convert: dependency: transitive description: @@ -98,7 +98,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" file: dependency: transitive description: @@ -157,7 +157,7 @@ packages: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.3-nullsafety.3" + version: "0.6.3" logging: dependency: transitive description: @@ -171,14 +171,14 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10-nullsafety.3" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.6" + version: "1.3.0" mime: dependency: transitive description: @@ -220,21 +220,21 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.3" + version: "1.8.0" pedantic: dependency: transitive description: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.10.0-nullsafety.3" + version: "1.11.0" pool: dependency: transitive description: name: pool url: "https://pub.dartlang.org" source: hosted - version: "1.5.0-nullsafety.3" + version: "1.5.0" pub_semver: dependency: transitive description: @@ -281,84 +281,84 @@ packages: name: source_map_stack_trace url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.4" + version: "2.1.0" source_maps: dependency: transitive description: name: source_maps url: "https://pub.dartlang.org" source: hosted - version: "0.10.10-nullsafety.3" + version: "0.10.10" source_span: dependency: transitive description: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.4" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.10.0-nullsafety.6" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" test: dependency: "direct dev" description: name: test url: "https://pub.dartlang.org" source: hosted - version: "1.16.0-nullsafety.17" + version: "1.16.5" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19-nullsafety.6" + version: "0.2.19" test_core: dependency: transitive description: name: test_core url: "https://pub.dartlang.org" source: hosted - version: "0.3.12-nullsafety.15" + version: "0.3.15" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.5" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.5" + version: "2.1.0" vm_service: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b3ce750..6d4758e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: flutter_dotenv -version: 4.0.0-nullsafety.0 +version: 4.0.0-nullsafety.1 description: Easily configure any flutter application with global variables using a `.env` file. author: java-james homepage: https://github.com/java-james/flutter_dotenv diff --git a/test/.env b/test/.env index 407149c..db61d7a 100644 --- a/test/.env +++ b/test/.env @@ -1,6 +1,6 @@ FOO=foo BAR=bar -FOOBAR=\$FOO$BAR +FOOBAR=\$FOO${FOO}$BAR ESCAPED_DOLLAR_SIGN="\$1000" ESCAPED_QUOTE='\'' diff --git a/test/dotenv_test.dart b/test/dotenv_test.dart index a4b6b6a..d204acb 100644 --- a/test/dotenv_test.dart +++ b/test/dotenv_test.dart @@ -7,13 +7,13 @@ void main() { setUp(() { print(Directory.current.toString()); dotenv.testLoad( - fileInput: File('.env') + fileInput: File('test/.env') .readAsStringSync()); //, mergeWith: Platform.environment }); test('able to load .env', () { expect(dotenv.env['FOO'], 'foo'); expect(dotenv.env['BAR'], 'bar'); - expect(dotenv.env['FOOBAR'], '\$FOObar'); + expect(dotenv.env['FOOBAR'], '\$FOOfoobar'); expect(dotenv.env['ESCAPED_DOLLAR_SIGN'], '\$1000'); expect(dotenv.env['ESCAPED_QUOTE'], "'"); expect(dotenv.env['BASIC'], 'basic'); diff --git a/test/parser_test.dart b/test/parser_test.dart index aa1d7f8..8c93200 100644 --- a/test/parser_test.dart +++ b/test/parser_test.dart @@ -1,6 +1,6 @@ import 'dart:math'; -import 'package:flutter_dotenv/src/parser.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:test/test.dart'; const ceil = 100000; @@ -100,8 +100,8 @@ void main() { expect(out, equals({'foo': 'bar'})); }); test('it substitutes known variables into other values', () { - var out = _psr.parse(['foo=bar', r'baz=super$foo']); - expect(out, equals({'foo': 'bar', 'baz': 'superbar'})); + var out = _psr.parse(['foo=bar', r'baz=super$foo${foo}']); + expect(out, equals({'foo': 'bar', 'baz': 'superbarbar'})); }); test('it discards surrounding quotes', () { var out = _psr.parse([r"foo = 'bar'", r'export baz="qux"']);