Socket
Socket
Sign inDemoInstall

@mongosh/async-rewriter2

Package Overview
Dependencies
Maintainers
14
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mongosh/async-rewriter2 - npm Package Compare versions

Comparing version 2.1.5 to 2.2.0

281

lib/stages/transform-maybe-await.js

@@ -27,161 +27,156 @@ "use strict";

const babel = __importStar(require("@babel/core"));
exports.default = ({ types: t, }) => {
function asNodeKey(v) {
return v;
function asNodeKey(v) {
return v;
}
const isGeneratedInnerFunction = asNodeKey(Symbol('isGeneratedInnerFunction'));
const isWrappedForOfLoop = asNodeKey(Symbol('isWrappedForOfLoop'));
const isGeneratedHelper = asNodeKey(Symbol('isGeneratedHelper'));
const isOriginalBody = asNodeKey(Symbol('isOriginalBody'));
const isAlwaysSyncFunction = asNodeKey(Symbol('isAlwaysSyncFunction'));
const isExpandedTypeof = asNodeKey(Symbol('isExpandedTypeof'));
const identifierGroupKey = '@@mongosh.identifierGroup';
const syntheticPromiseSymbolTemplate = babel.template.statements `
const SP_IDENTIFIER = Symbol.for("@@mongosh.syntheticPromise");
const SAI_IDENTIFIER = Symbol.for("@@mongosh.syntheticAsyncIterable");
`;
const markSyntheticPromiseTemplate = babel.template.statement `
function MSP_IDENTIFIER(p) {
return Object.defineProperty(p, SP_IDENTIFIER, {
value: true
});
}
`;
const isSyntheticPromiseTemplate = babel.template.statement `
function ISP_IDENTIFIER(p) {
return p && p[SP_IDENTIFIER];
}
`;
const assertNotSyntheticPromiseTemplate = babel.template.statement `
function ANSP_IDENTIFIER(p, s, i = false) {
if (p && p[SP_IDENTIFIER]) {
throw new CUSTOM_ERROR_BUILDER(
'Result of expression "' + s + '" cannot be used in this context',
'SyntheticPromiseInAlwaysSyncContext');
}
const isGeneratedInnerFunction = asNodeKey(Symbol('isGeneratedInnerFunction'));
const isWrappedForOfLoop = asNodeKey(Symbol('isWrappedForOfLoop'));
const isGeneratedHelper = asNodeKey(Symbol('isGeneratedHelper'));
const isOriginalBody = asNodeKey(Symbol('isOriginalBody'));
const isAlwaysSyncFunction = asNodeKey(Symbol('isAlwaysSyncFunction'));
const isExpandedTypeof = asNodeKey(Symbol('isExpandedTypeof'));
const identifierGroupKey = '@@mongosh.identifierGroup';
const syntheticPromiseSymbolTemplate = babel.template.statements(`
const SP_IDENTIFIER = Symbol.for("@@mongosh.syntheticPromise");
const SAI_IDENTIFIER = Symbol.for("@@mongosh.syntheticAsyncIterable");
`);
const markSyntheticPromiseTemplate = babel.template.statement(`
function MSP_IDENTIFIER(p) {
return Object.defineProperty(p, SP_IDENTIFIER, {
value: true
});
if (i && p && p[SAI_IDENTIFIER]) {
throw new CUSTOM_ERROR_BUILDER(
'Result of expression "' + s + '" cannot be iterated in this context',
'SyntheticAsyncIterableInAlwaysSyncContext');
}
`);
const isSyntheticPromiseTemplate = babel.template.statement(`
function ISP_IDENTIFIER(p) {
return p && p[SP_IDENTIFIER];
return p;
}
`;
const adaptAsyncIterableToSyncIterableTemplate = babel.template.statement `
function AAITSI_IDENTIFIER(original) {
if (!original || !original[SAI_IDENTIFIER]) {
return { iterable: original, isSyntheticAsyncIterable: false };
}
`);
const assertNotSyntheticPromiseTemplate = babel.template.statement(`
function ANSP_IDENTIFIER(p, s, i = false) {
if (p && p[SP_IDENTIFIER]) {
throw new CUSTOM_ERROR_BUILDER(
'Result of expression "' + s + '" cannot be used in this context',
'SyntheticPromiseInAlwaysSyncContext');
}
if (i && p && p[SAI_IDENTIFIER]) {
throw new CUSTOM_ERROR_BUILDER(
'Result of expression "' + s + '" cannot be iterated in this context',
'SyntheticAsyncIterableInAlwaysSyncContext');
}
return p;
}
`);
const adaptAsyncIterableToSyncIterableTemplate = babel.template.statement(`
function AAITSI_IDENTIFIER(original) {
if (!original || !original[SAI_IDENTIFIER]) {
return { iterable: original, isSyntheticAsyncIterable: false };
}
const originalIterator = original[Symbol.asyncIterator]();
let next;
let returned;
const originalIterator = original[Symbol.asyncIterator]();
let next;
let returned;
return {
isSyntheticAsyncIterable: true,
iterable: {
[Symbol.iterator]() {
return this;
},
next() {
let _next = next;
next = undefined;
return _next;
},
return(value) {
returned = { value };
return {
value,
done: true
}
},
async expectNext() {
next ??= await originalIterator.next();
},
async syncReturn() {
if (returned) {
await originalIterator.return(returned.value);
}
return {
isSyntheticAsyncIterable: true,
iterable: {
[Symbol.iterator]() {
return this;
},
next() {
let _next = next;
next = undefined;
return _next;
},
return(value) {
returned = { value };
return {
value,
done: true
}
},
async expectNext() {
next ??= await originalIterator.next();
},
async syncReturn() {
if (returned) {
await originalIterator.return(returned.value);
}
}
}
}
`);
const asyncTryCatchWrapperTemplate = babel.template.expression(`
async () => {
try {
ORIGINAL_CODE;
} catch (err) {
if (FUNCTION_STATE_IDENTIFIER === "sync") {
SYNC_RETURN_VALUE_IDENTIFIER = err;
FUNCTION_STATE_IDENTIFIER = "threw";
} else throw err;
} finally {
if (FUNCTION_STATE_IDENTIFIER !== "threw") FUNCTION_STATE_IDENTIFIER = "returned";
}
}
`);
const expressionHolderVariableTemplate = babel.template.statement(`
let EXPRESSION_HOLDER_IDENTIFIER;`);
const wrapperFunctionTemplate = babel.template.statements(`
let FUNCTION_STATE_IDENTIFIER = "sync",
SYNC_RETURN_VALUE_IDENTIFIER;
const ASYNC_RETURN_VALUE_IDENTIFIER = (ASYNC_TRY_CATCH_WRAPPER)();
if (FUNCTION_STATE_IDENTIFIER === "returned")
return SYNC_RETURN_VALUE_IDENTIFIER;
else if (FUNCTION_STATE_IDENTIFIER === "threw")
throw SYNC_RETURN_VALUE_IDENTIFIER;
FUNCTION_STATE_IDENTIFIER = "async";
return MSP_IDENTIFIER(ASYNC_RETURN_VALUE_IDENTIFIER);
`);
const awaitSyntheticPromiseTemplate = babel.template.expression(`(
ORIGINAL_SOURCE,
EXPRESSION_HOLDER = NODE,
ISP_IDENTIFIER(EXPRESSION_HOLDER) ? await EXPRESSION_HOLDER : EXPRESSION_HOLDER
)`, {
allowAwaitOutsideFunction: true,
});
const rethrowTemplate = babel.template.statement(`
}
`;
const asyncTryCatchWrapperTemplate = babel.template.expression `
async () => {
try {
ORIGINAL_CODE;
} catch (err) {
throw err;
if (FUNCTION_STATE_IDENTIFIER === "sync") {
SYNC_RETURN_VALUE_IDENTIFIER = err;
FUNCTION_STATE_IDENTIFIER = "threw";
} else throw err;
} finally {
if (FUNCTION_STATE_IDENTIFIER !== "threw") FUNCTION_STATE_IDENTIFIER = "returned";
}
`);
const forOfLoopTemplate = babel.template.statement(`{
const ITERABLE_INFO = AAITSI_IDENTIFIER(ORIGINAL_ITERABLE);
const ITERABLE_ISAI = (ITERABLE_INFO).isSyntheticAsyncIterable;
const ITERABLE = (ITERABLE_INFO).iterable;
}
`;
const expressionHolderVariableTemplate = babel.template.statement `
let EXPRESSION_HOLDER_IDENTIFIER;`;
const wrapperFunctionTemplate = babel.template.statements `
let FUNCTION_STATE_IDENTIFIER = "sync",
SYNC_RETURN_VALUE_IDENTIFIER;
try {
ITERABLE_ISAI && await (ITERABLE).expectNext();
for (const ITEM of (ORIGINAL_ITERABLE_SOURCE, ITERABLE)) {
ORIGINAL_DECLARATION;
try {
ORIGINAL_BODY;
} finally {
ITERABLE_ISAI && await (ITERABLE).expectNext();
}
const ASYNC_RETURN_VALUE_IDENTIFIER = (ASYNC_TRY_CATCH_WRAPPER)();
if (FUNCTION_STATE_IDENTIFIER === "returned")
return SYNC_RETURN_VALUE_IDENTIFIER;
else if (FUNCTION_STATE_IDENTIFIER === "threw")
throw SYNC_RETURN_VALUE_IDENTIFIER;
FUNCTION_STATE_IDENTIFIER = "async";
return MSP_IDENTIFIER(ASYNC_RETURN_VALUE_IDENTIFIER);
`;
const awaitSyntheticPromiseTemplate = babel.template.expression `(
ORIGINAL_SOURCE,
EXPRESSION_HOLDER = NODE,
ISP_IDENTIFIER(EXPRESSION_HOLDER) ? await EXPRESSION_HOLDER : EXPRESSION_HOLDER
)`;
const rethrowTemplate = babel.template.statement `
try {
ORIGINAL_CODE;
} catch (err) {
throw err;
}
`;
const forOfLoopTemplate = babel.template.statement `{
const ITERABLE_INFO = AAITSI_IDENTIFIER(ORIGINAL_ITERABLE);
const ITERABLE_ISAI = (ITERABLE_INFO).isSyntheticAsyncIterable;
const ITERABLE = (ITERABLE_INFO).iterable;
try {
ITERABLE_ISAI && await (ITERABLE).expectNext();
for (const ITEM of (ORIGINAL_ITERABLE_SOURCE, ITERABLE)) {
ORIGINAL_DECLARATION;
try {
ORIGINAL_BODY;
} finally {
ITERABLE_ISAI && await (ITERABLE).expectNext();
}
} finally {
ITERABLE_ISAI && await (ITERABLE).syncReturn();
}
}`);
const demangleErrorTemplate = babel.template.statement(String.raw `
function DE_IDENTIFIER(err) {
if (Object.prototype.toString.call(err) === '[object Error]' &&
err.message.includes('\ufeff')) {
err.message = err.message.replace(/\(\s*"\ufeff(.+?)\ufeff"\s*,(?:[^\(]|\([^\)]*\))*\)/g, '$1');
}
return err;
} finally {
ITERABLE_ISAI && await (ITERABLE).syncReturn();
}
}`;
const demangleErrorTemplate = babel.template.statement `
function DE_IDENTIFIER(err) {
if (Object.prototype.toString.call(err) === '[object Error]' &&
err.message.includes('\\ufeff')) {
err.message = err.message.replace(/\\(\\s*"\\ufeff(.+?)\\ufeff"\\s*,(?:[^\\(]|\\([^\)]*\\))*\\)/g, (m,o) => o);
}
`, {
placeholderPattern: false,
placeholderWhitelist: new Set(['DE_IDENTIFIER']),
});
const returnValueWrapperTemplate = babel.template.expression(`(
SYNC_RETURN_VALUE_IDENTIFIER = NODE,
FUNCTION_STATE_IDENTIFIER === 'async' ? SYNC_RETURN_VALUE_IDENTIFIER : null
)`);
return err;
}
`;
const returnValueWrapperTemplate = babel.template.expression `(
SYNC_RETURN_VALUE_IDENTIFIER = NODE,
FUNCTION_STATE_IDENTIFIER === 'async' ? SYNC_RETURN_VALUE_IDENTIFIER : null
)`;
exports.default = ({ types: t, }) => {
function getOriginalSourceString({ file }, node, { wrap = true } = {}) {

@@ -188,0 +183,0 @@ var _a, _b;

@@ -27,10 +27,10 @@ "use strict";

const babel = __importStar(require("@babel/core"));
function asNodeKey(v) {
return v;
}
const isGeneratedTryCatch = asNodeKey(Symbol('isGeneratedTryCatch'));
const notUncatchableCheck = babel.template.expression `
(!ERR_IDENTIFIER || !ERR_IDENTIFIER[Symbol.for('@@mongosh.uncatchable')])
`;
exports.default = ({ types: t, }) => {
function asNodeKey(v) {
return v;
}
const isGeneratedTryCatch = asNodeKey(Symbol('isGeneratedTryCatch'));
const notUncatchableCheck = babel.template.expression(`
(!ERR_IDENTIFIER || !ERR_IDENTIFIER[Symbol.for('@@mongosh.uncatchable')])
`);
return {

@@ -37,0 +37,0 @@ visitor: {

{
"name": "@mongosh/async-rewriter2",
"version": "2.1.5",
"version": "2.2.0",
"description": "MongoDB Shell Async Rewriter Package",

@@ -51,5 +51,5 @@ "main": "./lib/index.js",

"devDependencies": {
"@mongodb-js/eslint-config-mongosh": "2.1.5",
"@mongodb-js/eslint-config-mongosh": "2.2.0",
"@mongodb-js/prettier-config-devtools": "^1.0.1",
"@mongodb-js/tsconfig-mongosh": "2.1.5",
"@mongodb-js/tsconfig-mongosh": "2.2.0",
"depcheck": "^1.4.3",

@@ -59,3 +59,3 @@ "eslint": "^7.25.0",

},
"gitHead": "cbfb77a23e88fc09b9334a6680667760b18dec94"
"gitHead": "482c1fa4e9502519952ec6b23930238587801b1b"
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc