Skip to content
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

Fix RegExp in expandReferences #875

Merged
merged 3 commits into from
Jan 9, 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
5 changes: 5 additions & 0 deletions .changeset/nasty-pumpkins-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openfn/language-common': patch
---

Ensure that RegExp objects can be safely passed as references
6 changes: 5 additions & 1 deletion packages/common/src/util/references.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ const isStream = value => {
};

function expandReference(state, value) {
if (Buffer.isBuffer(value)) {
if (
Buffer.isBuffer(value) ||
// Note: there is a weird identity thing in the VM where typeof RegExp will be false ¯\_(ツ)_/¯
value?.constructor?.name === 'RegExp'
) {
return value;
}

Expand Down
25 changes: 24 additions & 1 deletion packages/common/test/util/references.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { expect } from 'chai';
import { expandReferences, normalizeOauthConfig } from '../../src/util/index.js';
import {
expandReferences,
normalizeOauthConfig,
} from '../../src/util/index.js';

describe('util expandReferences', () => {
it('should not modify string references', () => {
Expand All @@ -11,6 +14,26 @@ describe('util expandReferences', () => {
expect(resolvedName).to.equal('mulder');
});

it('should ignore regex literals', () => {
const regex = /scully/;
const state = {};

const [resolvedRegex] = expandReferences(state, regex);

expect(resolvedRegex instanceof RegExp).to.be.true;
expect(resolvedRegex.test('scully')).to.be.true;
});

it('should ignore regex instances', () => {
const regex = new RegExp('scully');
const state = {};

const [resolvedRegex] = expandReferences(state, regex);

expect(resolvedRegex instanceof RegExp).to.be.true;
expect(resolvedRegex.test('scully')).to.be.true;
});

it('should expand function references', () => {
const state = { name: 'mulder' };
const name = s => s.name;
Expand Down
3 changes: 2 additions & 1 deletion packages/gmail/src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import unzipper from 'unzipper';
import { google } from 'googleapis';

let gmail = undefined;
let gmail;
let isTesting;

export async function fetchMessages(userId, query, lastPageToken) {
let messagesResponse = null;
Expand Down
Loading