From f09491284e935fa7e1af6cae0870bcb09926f722 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Fri, 14 Jun 2024 11:58:52 +0200 Subject: [PATCH] Create emailregex in components-core instead of using a dedicated package (#895) --- .changeset/great-buses-arrive.md | 5 ++ packages/core/package.json | 1 - packages/core/src/helper/emailRegex.test.ts | 65 +++++++++++++++++++++ packages/core/src/helper/emailRegex.ts | 7 ++- pnpm-lock.yaml | 9 --- 5 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 .changeset/great-buses-arrive.md create mode 100644 packages/core/src/helper/emailRegex.test.ts diff --git a/.changeset/great-buses-arrive.md b/.changeset/great-buses-arrive.md new file mode 100644 index 000000000..0e8d759a4 --- /dev/null +++ b/.changeset/great-buses-arrive.md @@ -0,0 +1,5 @@ +--- +"@livekit/components-core": patch +--- + +Create emailregex in components-core instead of using a dedicated package diff --git a/packages/core/package.json b/packages/core/package.json index 661914d0b..274f1cfa8 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -36,7 +36,6 @@ "typings": "dist/index.d.ts", "dependencies": { "@floating-ui/dom": "1.6.5", - "email-regex": "5.0.0", "loglevel": "1.9.1", "rxjs": "7.8.1" }, diff --git a/packages/core/src/helper/emailRegex.test.ts b/packages/core/src/helper/emailRegex.test.ts new file mode 100644 index 000000000..1640019b3 --- /dev/null +++ b/packages/core/src/helper/emailRegex.test.ts @@ -0,0 +1,65 @@ +import { describe, test } from 'vitest'; +import { createEmailRegExp } from './emailRegex'; + +const fixtures = [ + 'livekit@gmail.com', + 'foo@bar', + 'test@about.museum', + 'test@nominet.org.uk', + 'test.test@livekit.io', + 'test@255.255.255.255', + 'a@livekit.io', + 'test@e.com', + 'test@xn--hxajbheg2az3al.xn--jxalpdlp', + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@livekit.io', + '!#$%&`*+/=?^`{|}~@livekit.io', + 'test@g--a.com', + 'a@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.hij', + '123@livekit.io', + '"\\a"@livekit.io', + '""@livekit.io', + '"test"@livekit.io', + '"\\""@livekit.io', + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklmn@livekit.io', + 'test@iana.co-uk', + 'a@a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v', + 'test@foo-bar.com', + 'foo@x.solutions', + 'foo@[IPv6:2001:db8::2]', +]; + +const fixturesNot = [ + '@', + '@io', + '@livekit.io', + 'test..livekit.io', + 'test@iana..com', + 'test@livekit.io.', + '.test@livekit.io', + 'livekit@livekit@livekit.com', + 'mailto:livekit@gmail.com', + 'foo.example.com', + 'test.@example.com', +]; + +describe('Email regex tests', () => { + test('extract', (t) => { + for (const fixture of fixtures) { + t.expect((createEmailRegExp().exec(`foo ${fixture} bar`) || [])[0]).toBe(fixture); + } + + t.expect(createEmailRegExp().exec('mailto:livekit@gmail.com')?.[0]).toBe('livekit@gmail.com'); + }); + + test('exact', (t) => { + for (const fixture of fixtures) { + t.expect(createEmailRegExp({ exact: true }).test(fixture)).toBeTruthy(); + } + }); + + test('failures', (t) => { + for (const fixture of fixturesNot) { + t.expect(createEmailRegExp({ exact: true }).test(fixture)).toBeFalsy(); + } + }); +}); diff --git a/packages/core/src/helper/emailRegex.ts b/packages/core/src/helper/emailRegex.ts index 505625758..fbd4894b8 100644 --- a/packages/core/src/helper/emailRegex.ts +++ b/packages/core/src/helper/emailRegex.ts @@ -1,3 +1,8 @@ -import createEmailRegExp from 'email-regex'; +// source code adapted from https://github.com/sindresorhus/email-regex due to ESM import incompatibilities when trying to serve a CJS version of components +const regex = '[^\\.\\s@:](?:[^\\s@:]*[^\\s@:\\.])?@[^\\.\\s@]+(?:\\.[^\\.\\s@]+)*'; + +function createEmailRegExp({ exact }: { exact?: boolean } = {}) { + return exact ? new RegExp(`^${regex}$`) : new RegExp(regex, 'g'); +} export { createEmailRegExp }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8fc978f77..3a5437c1e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -157,9 +157,6 @@ importers: '@livekit/protocol': specifier: ^1.16.0 version: 1.16.0 - email-regex: - specifier: 5.0.0 - version: 5.0.0 livekit-client: specifier: ^2.1.5 version: 2.1.5 @@ -4058,10 +4055,6 @@ packages: electron-to-chromium@1.4.726: resolution: {integrity: sha512-xtjfBXn53RORwkbyKvDfTajtnTp0OJoPOIBzXvkNbb7+YYvCHJflba3L7Txyx/6Fov3ov2bGPr/n5MTixmPhdQ==} - email-regex@5.0.0: - resolution: {integrity: sha512-he76Cm8JFxb6OGQHabLBPdsiStgPmJeAEhctmw0uhonUh1pCBsHpI6/rB62s2GNzjBb0YlhIcF/1l9Lp5AfH0Q==} - engines: {node: '>=12'} - emittery@0.13.1: resolution: {integrity: sha512-DeWwawk6r5yR9jFgnDKYt4sLS0LmHJJi3ZOnb5/JdbYwj3nW+FxQnHIjhBKz8YLC7oRNPVM9NQ47I3CVx34eqQ==} engines: {node: '>=12'} @@ -12261,8 +12254,6 @@ snapshots: electron-to-chromium@1.4.726: {} - email-regex@5.0.0: {} - emittery@0.13.1: {} emoji-regex@8.0.0: {}