Tons of Solutions Engineering work done today for the rest of the CS team! Headway, Howard Hanna, Engels, Brighton, etc. Also completed Datasnippers auth flow and worked on Anthology's script. Cloned Anthology's courses (900..) and will clone Full Story on Monday.
This commit is contained in:
117
Scripts/node_modules/degenerator/README.md
generated
vendored
Normal file
117
Scripts/node_modules/degenerator/README.md
generated
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
degenerator
|
||||
===========
|
||||
### Compiles sync functions into async functions
|
||||
|
||||
Sometimes you need to write sync looking code that's really async under the hood.
|
||||
This module takes a String to one or more synchronous JavaScript functions, and
|
||||
returns a new String that with those JS functions transpiled into `async`
|
||||
functions.
|
||||
|
||||
So this:
|
||||
|
||||
```js
|
||||
function foo() {
|
||||
return a('bar') || b();
|
||||
}
|
||||
```
|
||||
|
||||
Gets compiled into:
|
||||
|
||||
```js
|
||||
async function foo() {
|
||||
return await a('bar') || await b();
|
||||
}
|
||||
```
|
||||
|
||||
With the compiled output code, you can evaluate the code using the `vm` module
|
||||
in Node.js, or save the code to a file and require it, or whatever.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
You must explicitly specify the names of the functions that should be
|
||||
"asyncified". So say we wanted to expose a `get(url)` function that did
|
||||
and HTTP request and returned the response body.
|
||||
|
||||
The user has provided us with this implementation:
|
||||
|
||||
``` js
|
||||
function myFn() {
|
||||
const one = get('https://google.com');
|
||||
const two = get('http://nodejs.org');
|
||||
const three = JSON.parse(get('http://jsonip.org'));
|
||||
return [one, two, three];
|
||||
}
|
||||
```
|
||||
|
||||
Now we can compile this into an asyncronous function, implement the
|
||||
async `get()` function, and finally evaluate it into a real JavaScript function
|
||||
instance with the `vm` module:
|
||||
|
||||
|
||||
```typescript
|
||||
import vm from 'vm';
|
||||
import { degenerator } from 'degenerator';
|
||||
|
||||
// The `get()` function is Promise-based (error handling omitted for brevity)
|
||||
function get(endpoint: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var mod = 0 == endpoint.indexOf('https:') ? require('https') : require('http');
|
||||
var req = mod.get(endpoint);
|
||||
req.on('response', function (res) {
|
||||
var data = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', function (b) { data += b; });
|
||||
res.on('end', function () {
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Convert the JavaScript string provided from the user (assumed to be `str` var)
|
||||
str = degenerator(str, [ 'get' ]);
|
||||
|
||||
// Turn the JS String into a real async function instance
|
||||
const asyncFn = vm.runInNewContext(`(${str})`, { get });
|
||||
|
||||
// Now we can invoke the function asynchronously
|
||||
asyncFn().then((res) => {
|
||||
// Do something with `res`...
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
API
|
||||
---
|
||||
|
||||
### degenerator(code: string, names: Array<string|RegExp>): String
|
||||
|
||||
Returns a "degeneratorified" JavaScript string, with `async`/`await` transplanted.
|
||||
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
11
Scripts/node_modules/degenerator/dist/compile.d.ts
generated
vendored
Normal file
11
Scripts/node_modules/degenerator/dist/compile.d.ts
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
/// <reference types="node" />
|
||||
import type { Context } from 'vm';
|
||||
import type { QuickJSWASMModule } from '@tootallnate/quickjs-emscripten';
|
||||
import type { DegeneratorNames } from './degenerator';
|
||||
export interface CompileOptions {
|
||||
names?: DegeneratorNames;
|
||||
filename?: string;
|
||||
sandbox?: Context;
|
||||
}
|
||||
export declare function compile<R = unknown, A extends unknown[] = []>(qjs: QuickJSWASMModule, code: string, returnName: string, options?: CompileOptions): (...args: A) => Promise<R>;
|
||||
//# sourceMappingURL=compile.d.ts.map
|
||||
1
Scripts/node_modules/degenerator/dist/compile.d.ts.map
generated
vendored
Normal file
1
Scripts/node_modules/degenerator/dist/compile.d.ts.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../src/compile.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAClC,OAAO,KAAK,EAGX,iBAAiB,EACjB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEtD,MAAM,WAAW,cAAc;IAC9B,KAAK,CAAC,EAAE,gBAAgB,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,wBAAgB,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,SAAS,OAAO,EAAE,GAAG,EAAE,EAC5D,GAAG,EAAE,iBAAiB,EACtB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,cAAmB,GAC1B,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CA4E5B"}
|
||||
107
Scripts/node_modules/degenerator/dist/compile.js
generated
vendored
Normal file
107
Scripts/node_modules/degenerator/dist/compile.js
generated
vendored
Normal file
@ -0,0 +1,107 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.compile = void 0;
|
||||
const util_1 = require("util");
|
||||
const degenerator_1 = require("./degenerator");
|
||||
function compile(qjs, code, returnName, options = {}) {
|
||||
const compiled = (0, degenerator_1.degenerator)(code, options.names ?? []);
|
||||
const vm = qjs.newContext();
|
||||
// Add functions to global
|
||||
if (options.sandbox) {
|
||||
for (const [name, value] of Object.entries(options.sandbox)) {
|
||||
if (typeof value !== 'function') {
|
||||
throw new Error(`Expected a "function" for sandbox property \`${name}\`, but got "${typeof value}"`);
|
||||
}
|
||||
const fnHandle = vm.newFunction(name, (...args) => {
|
||||
const result = value(...args.map((arg) => quickJSHandleToHost(vm, arg)));
|
||||
vm.runtime.executePendingJobs();
|
||||
return hostToQuickJSHandle(vm, result);
|
||||
});
|
||||
fnHandle.consume((handle) => vm.setProp(vm.global, name, handle));
|
||||
}
|
||||
}
|
||||
const fnResult = vm.evalCode(`${compiled};${returnName}`, options.filename);
|
||||
const fn = vm.unwrapResult(fnResult);
|
||||
const t = vm.typeof(fn);
|
||||
if (t !== 'function') {
|
||||
throw new Error(`Expected a "function" named \`${returnName}\` to be defined, but got "${t}"`);
|
||||
}
|
||||
const r = async function (...args) {
|
||||
let promiseHandle;
|
||||
let resolvedHandle;
|
||||
try {
|
||||
const result = vm.callFunction(fn, vm.undefined, ...args.map((arg) => hostToQuickJSHandle(vm, arg)));
|
||||
promiseHandle = vm.unwrapResult(result);
|
||||
const resolvedResultP = vm.resolvePromise(promiseHandle);
|
||||
vm.runtime.executePendingJobs();
|
||||
const resolvedResult = await resolvedResultP;
|
||||
resolvedHandle = vm.unwrapResult(resolvedResult);
|
||||
return quickJSHandleToHost(vm, resolvedHandle);
|
||||
}
|
||||
catch (err) {
|
||||
if (err && typeof err === 'object' && 'cause' in err && err.cause) {
|
||||
if (typeof err.cause === 'object' &&
|
||||
'stack' in err.cause &&
|
||||
'name' in err.cause &&
|
||||
'message' in err.cause &&
|
||||
typeof err.cause.stack === 'string' &&
|
||||
typeof err.cause.name === 'string' &&
|
||||
typeof err.cause.message === 'string') {
|
||||
// QuickJS Error `stack` does not include the name +
|
||||
// message, so patch those in to behave more like V8
|
||||
err.cause.stack = `${err.cause.name}: ${err.cause.message}\n${err.cause.stack}`;
|
||||
}
|
||||
throw err.cause;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
finally {
|
||||
promiseHandle?.dispose();
|
||||
resolvedHandle?.dispose();
|
||||
}
|
||||
};
|
||||
Object.defineProperty(r, 'toString', {
|
||||
value: () => compiled,
|
||||
enumerable: false,
|
||||
});
|
||||
return r;
|
||||
}
|
||||
exports.compile = compile;
|
||||
function quickJSHandleToHost(vm, val) {
|
||||
return vm.dump(val);
|
||||
}
|
||||
function hostToQuickJSHandle(vm, val) {
|
||||
if (typeof val === 'undefined') {
|
||||
return vm.undefined;
|
||||
}
|
||||
else if (val === null) {
|
||||
return vm.null;
|
||||
}
|
||||
else if (typeof val === 'string') {
|
||||
return vm.newString(val);
|
||||
}
|
||||
else if (typeof val === 'number') {
|
||||
return vm.newNumber(val);
|
||||
}
|
||||
else if (typeof val === 'bigint') {
|
||||
return vm.newBigInt(val);
|
||||
}
|
||||
else if (typeof val === 'boolean') {
|
||||
return val ? vm.true : vm.false;
|
||||
}
|
||||
else if (util_1.types.isPromise(val)) {
|
||||
const promise = vm.newPromise();
|
||||
promise.settled.then(vm.runtime.executePendingJobs);
|
||||
val.then((r) => {
|
||||
promise.resolve(hostToQuickJSHandle(vm, r));
|
||||
}, (err) => {
|
||||
promise.reject(hostToQuickJSHandle(vm, err));
|
||||
});
|
||||
return promise.handle;
|
||||
}
|
||||
else if (util_1.types.isNativeError(val)) {
|
||||
return vm.newError(val);
|
||||
}
|
||||
throw new Error(`Unsupported value: ${val}`);
|
||||
}
|
||||
//# sourceMappingURL=compile.js.map
|
||||
1
Scripts/node_modules/degenerator/dist/compile.js.map
generated
vendored
Normal file
1
Scripts/node_modules/degenerator/dist/compile.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../src/compile.ts"],"names":[],"mappings":";;;AAAA,+BAA6B;AAC7B,+CAA4C;AAe5C,SAAgB,OAAO,CACtB,GAAsB,EACtB,IAAY,EACZ,UAAkB,EAClB,UAA0B,EAAE;IAE5B,MAAM,QAAQ,GAAG,IAAA,yBAAW,EAAC,IAAI,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAExD,MAAM,EAAE,GAAG,GAAG,CAAC,UAAU,EAAE,CAAC;IAE5B,0BAA0B;IAC1B,IAAI,OAAO,CAAC,OAAO,EAAE;QACpB,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;YAC5D,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;gBAChC,MAAM,IAAI,KAAK,CACd,gDAAgD,IAAI,gBAAgB,OAAO,KAAK,GAAG,CACnF,CAAC;aACF;YACD,MAAM,QAAQ,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE;gBACjD,MAAM,MAAM,GAAG,KAAK,CACnB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,mBAAmB,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAClD,CAAC;gBACF,EAAE,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC;gBAChC,OAAO,mBAAmB,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACxC,CAAC,CAAC,CAAC;YACH,QAAQ,C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degenerator/dist/degenerator.d.ts
generated
vendored
Normal file
12
Scripts/node_modules/degenerator/dist/degenerator.d.ts
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
export type DegeneratorName = string | RegExp;
|
||||
export type DegeneratorNames = DegeneratorName[];
|
||||
/**
|
||||
* Compiles sync JavaScript code into JavaScript with async Functions.
|
||||
*
|
||||
* @param {String} code JavaScript string to convert
|
||||
* @param {Array} names Array of function names to add `await` operators to
|
||||
* @return {String} Converted JavaScript string with async/await injected
|
||||
* @api public
|
||||
*/
|
||||
export declare function degenerator(code: string, _names: DegeneratorNames): string;
|
||||
//# sourceMappingURL=degenerator.d.ts.map
|
||||
1
Scripts/node_modules/degenerator/dist/degenerator.d.ts.map
generated
vendored
Normal file
1
Scripts/node_modules/degenerator/dist/degenerator.d.ts.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"degenerator.d.ts","sourceRoot":"","sources":["../src/degenerator.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,CAAC;AAC9C,MAAM,MAAM,gBAAgB,GAAG,eAAe,EAAE,CAAC;AAEjD;;;;;;;GAOG;AAEH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,MAAM,CAyG1E"}
|
||||
156
Scripts/node_modules/degenerator/dist/degenerator.js
generated
vendored
Normal file
156
Scripts/node_modules/degenerator/dist/degenerator.js
generated
vendored
Normal file
@ -0,0 +1,156 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.degenerator = void 0;
|
||||
const util_1 = require("util");
|
||||
const escodegen_1 = require("escodegen");
|
||||
const esprima_1 = require("esprima");
|
||||
const ast_types_1 = require("ast-types");
|
||||
/**
|
||||
* Compiles sync JavaScript code into JavaScript with async Functions.
|
||||
*
|
||||
* @param {String} code JavaScript string to convert
|
||||
* @param {Array} names Array of function names to add `await` operators to
|
||||
* @return {String} Converted JavaScript string with async/await injected
|
||||
* @api public
|
||||
*/
|
||||
function degenerator(code, _names) {
|
||||
if (!Array.isArray(_names)) {
|
||||
throw new TypeError('an array of async function "names" is required');
|
||||
}
|
||||
// Duplicate the `names` array since it's rude to augment the user args
|
||||
const names = _names.slice(0);
|
||||
const ast = (0, esprima_1.parseScript)(code);
|
||||
// First pass is to find the `function` nodes and turn them into async or
|
||||
// generator functions only if their body includes `CallExpressions` to
|
||||
// function in `names`. We also add the names of the functions to the `names`
|
||||
// array. We'll iterate several time, as every iteration might add new items
|
||||
// to the `names` array, until no new names were added in the iteration.
|
||||
let lastNamesLength = 0;
|
||||
do {
|
||||
lastNamesLength = names.length;
|
||||
(0, ast_types_1.visit)(ast, {
|
||||
visitVariableDeclaration(path) {
|
||||
if (path.node.declarations) {
|
||||
for (let i = 0; i < path.node.declarations.length; i++) {
|
||||
const declaration = path.node.declarations[i];
|
||||
if (ast_types_1.namedTypes.VariableDeclarator.check(declaration) &&
|
||||
ast_types_1.namedTypes.Identifier.check(declaration.init) &&
|
||||
ast_types_1.namedTypes.Identifier.check(declaration.id) &&
|
||||
checkName(declaration.init.name, names) &&
|
||||
!checkName(declaration.id.name, names)) {
|
||||
names.push(declaration.id.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
visitAssignmentExpression(path) {
|
||||
if (ast_types_1.namedTypes.Identifier.check(path.node.left) &&
|
||||
ast_types_1.namedTypes.Identifier.check(path.node.right) &&
|
||||
checkName(path.node.right.name, names) &&
|
||||
!checkName(path.node.left.name, names)) {
|
||||
names.push(path.node.left.name);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
visitFunction(path) {
|
||||
if (path.node.id) {
|
||||
let shouldDegenerate = false;
|
||||
(0, ast_types_1.visit)(path.node, {
|
||||
visitCallExpression(path) {
|
||||
if (checkNames(path.node, names)) {
|
||||
shouldDegenerate = true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
});
|
||||
if (!shouldDegenerate) {
|
||||
return false;
|
||||
}
|
||||
// Got a "function" expression/statement,
|
||||
// convert it into an async function
|
||||
path.node.async = true;
|
||||
// Add function name to `names` array
|
||||
if (!checkName(path.node.id.name, names)) {
|
||||
names.push(path.node.id.name);
|
||||
}
|
||||
}
|
||||
this.traverse(path);
|
||||
},
|
||||
});
|
||||
} while (lastNamesLength !== names.length);
|
||||
// Second pass is for adding `await` statements to any function
|
||||
// invocations that match the given `names` array.
|
||||
(0, ast_types_1.visit)(ast, {
|
||||
visitCallExpression(path) {
|
||||
if (checkNames(path.node, names)) {
|
||||
// A "function invocation" expression,
|
||||
// we need to inject an `AwaitExpression`
|
||||
const delegate = false;
|
||||
const { name, parent: { node: pNode }, } = path;
|
||||
const expr = ast_types_1.builders.awaitExpression(path.node, delegate);
|
||||
if (ast_types_1.namedTypes.CallExpression.check(pNode)) {
|
||||
pNode.arguments[name] = expr;
|
||||
}
|
||||
else {
|
||||
pNode[name] = expr;
|
||||
}
|
||||
}
|
||||
this.traverse(path);
|
||||
},
|
||||
});
|
||||
return (0, escodegen_1.generate)(ast);
|
||||
}
|
||||
exports.degenerator = degenerator;
|
||||
/**
|
||||
* Returns `true` if `node` has a matching name to one of the entries in the
|
||||
* `names` array.
|
||||
*
|
||||
* @param {types.Node} node
|
||||
* @param {Array} names Array of function names to return true for
|
||||
* @return {Boolean}
|
||||
* @api private
|
||||
*/
|
||||
function checkNames({ callee }, names) {
|
||||
let name;
|
||||
if (ast_types_1.namedTypes.Identifier.check(callee)) {
|
||||
name = callee.name;
|
||||
}
|
||||
else if (ast_types_1.namedTypes.MemberExpression.check(callee)) {
|
||||
if (ast_types_1.namedTypes.Identifier.check(callee.object) &&
|
||||
ast_types_1.namedTypes.Identifier.check(callee.property)) {
|
||||
name = `${callee.object.name}.${callee.property.name}`;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (ast_types_1.namedTypes.FunctionExpression.check(callee)) {
|
||||
if (callee.id) {
|
||||
name = callee.id.name;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new Error(`Don't know how to get name for: ${callee.type}`);
|
||||
}
|
||||
return checkName(name, names);
|
||||
}
|
||||
function checkName(name, names) {
|
||||
// now that we have the `name`, check if any entries match in the `names` array
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const n = names[i];
|
||||
if (util_1.types.isRegExp(n)) {
|
||||
if (n.test(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (name === n) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//# sourceMappingURL=degenerator.js.map
|
||||
1
Scripts/node_modules/degenerator/dist/degenerator.js.map
generated
vendored
Normal file
1
Scripts/node_modules/degenerator/dist/degenerator.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"degenerator.js","sourceRoot":"","sources":["../src/degenerator.ts"],"names":[],"mappings":";;;AAAA,+BAA6B;AAC7B,yCAAqC;AACrC,qCAAsC;AACtC,yCAAkE;AAKlE;;;;;;;GAOG;AAEH,SAAgB,WAAW,CAAC,IAAY,EAAE,MAAwB;IACjE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QAC3B,MAAM,IAAI,SAAS,CAAC,gDAAgD,CAAC,CAAC;KACtE;IAED,uEAAuE;IACvE,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAE9B,MAAM,GAAG,GAAG,IAAA,qBAAW,EAAC,IAAI,CAAC,CAAC;IAE9B,yEAAyE;IACzE,uEAAuE;IACvE,6EAA6E;IAC7E,4EAA4E;IAC5E,wEAAwE;IACxE,IAAI,eAAe,GAAG,CAAC,CAAC;IACxB,GAAG;QACF,eAAe,GAAG,KAAK,CAAC,MAAM,CAAC;QAC/B,IAAA,iBAAK,EAAC,GAAG,EAAE;YACV,wBAAwB,CAAC,IAAI;gBAC5B,IAAI,IAAI,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IACD,OAAO,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC/B,CAAC;AAED,SAAS,SAAS,CAAC,IAAY,EAAE,KAAuB;IACvD,+EAA+E;IAC/E,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACtC,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,YAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE;YACtB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBACjB,OAAO,IAAI,CAAC;aACZ;SACD;aAAM,IAAI,IAAI,KAAK,CAAC,EAAE;YACtB,OAAO,IAAI,CAAC;SACZ;KACD;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
|
||||
3
Scripts/node_modules/degenerator/dist/index.d.ts
generated
vendored
Normal file
3
Scripts/node_modules/degenerator/dist/index.d.ts
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './degenerator';
|
||||
export * from './compile';
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
1
Scripts/node_modules/degenerator/dist/index.d.ts.map
generated
vendored
Normal file
1
Scripts/node_modules/degenerator/dist/index.d.ts.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC"}
|
||||
19
Scripts/node_modules/degenerator/dist/index.js
generated
vendored
Normal file
19
Scripts/node_modules/degenerator/dist/index.js
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__exportStar(require("./degenerator"), exports);
|
||||
__exportStar(require("./compile"), exports);
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
Scripts/node_modules/degenerator/dist/index.js.map
generated
vendored
Normal file
1
Scripts/node_modules/degenerator/dist/index.js.map
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,4CAA0B"}
|
||||
42
Scripts/node_modules/degenerator/package.json
generated
vendored
Normal file
42
Scripts/node_modules/degenerator/package.json
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "degenerator",
|
||||
"version": "5.0.1",
|
||||
"description": "Compiles sync functions into async generator functions",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"author": "Nathan Rajlich <nathan@tootallnate.net> (http://n8.io/)",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/TooTallNate/proxy-agents.git",
|
||||
"directory": "packages/degenerator"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 14"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ast-types": "^0.13.4",
|
||||
"escodegen": "^2.1.0",
|
||||
"esprima": "^4.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tootallnate/quickjs-emscripten": "^0.23.0",
|
||||
"@types/escodegen": "^0.0.7",
|
||||
"@types/esprima": "^4.0.3",
|
||||
"@types/jest": "^29.5.2",
|
||||
"@types/node": "^14.18.52",
|
||||
"jest": "^29.5.0",
|
||||
"ts-jest": "^29.1.0",
|
||||
"typescript": "^5.1.6",
|
||||
"tsconfig": "0.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "jest --env node --verbose --bail",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"pack": "node ../../scripts/pack.mjs"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user