New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

watch-dependency-graph

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

watch-dependency-graph - npm Package Compare versions

Comparing version 2.1.1 to 2.2.0-beta.1

test/bench.js

100

index.js

@@ -6,5 +6,4 @@ const fs = require('fs')

const filewatcher = require('filewatcher')
const acorn = require('acorn-loose')
const walker = require('acorn-walk')
const { transformSync } = require('@babel/core')
const parser = require('@babel/parser')
const traverse = require('@babel/traverse').default

@@ -171,32 +170,80 @@ function emitter () {

const raw = fs.readFileSync(id, 'utf-8')
const { code } = transformSync(raw, {
filename: id,
presets: [require.resolve('@babel/preset-env')]
const ast = parser.parse(raw, {
sourceType: 'module',
plugins: ['jsx', 'typescript', 'dynamicImport']
})
const ast = acorn.parse(code, {
ecmaVersion: 2015,
sourceType: 'module'
const nextIds = []
traverse(ast, {
enter (path) {
if (path.node.type === 'CallExpression') {
const callee = path.get('callee')
const isDynamicImport = callee.isImport()
if (callee.isIdentifier({ name: 'require' }) || isDynamicImport) {
const arg = path.node.arguments[0]
if (arg.type === 'StringLiteral') {
nextIds.push(arg.value)
} else {
nextIds.push(src.slice(arg.start, arg.end))
}
}
} else if (
path.node.type === 'ImportDeclaration' ||
path.node.type === 'ExportNamedDeclaration' ||
path.node.type === 'ExportAllDeclaration'
) {
const { source } = path.node
if (source && source.value) {
nextIds.push(source.value)
}
}
}
})
for (const node of ast.body) {
// get deps of current file
const nextIds = getFileIdsFromAstNode(node, {
parentFileId: id,
const resolvedNextIds = nextIds
.map(moduleId => {
const req = createRequire(id)
let resolved
try {
resolved = req.resolve(moduleId)
} catch (e1) {
try {
resolved = req.resolve(resolveAliases(moduleId, alias))
} catch (e2) {
try {
resolved = require.resolve(moduleId)
} catch (e3) {
if (e1) {
throw e1
}
if (e2) {
throw e2
}
if (e3) {
throw e3
}
}
}
}
// same same, must be built-in module
return resolved === moduleId ? undefined : resolved
})
.filter(Boolean)
// walk each dep
for (const _id of resolvedNextIds) {
walk(_id, {
ids,
tree,
entryPointer,
parentPointer: pointer,
visitedLeaf,
events,
alias
})
// walk each dep
for (const _id of nextIds) {
walk(_id, {
ids,
tree,
entryPointer,
parentPointer: pointer,
visitedLeaf,
events,
alias
})
}
}
} catch (e) {
debug('walk error', e)
// on syntax errors, just watch file and exit walk

@@ -287,2 +334,3 @@ if (e instanceof SyntaxError) return

debug('diff', { addedIds, removedIds })
debug('tree', tree)

@@ -289,0 +337,0 @@ for (const id of addedIds) {

{
"name": "watch-dependency-graph",
"version": "2.1.1",
"version": "2.2.0-beta.1",
"description": "",

@@ -35,7 +35,6 @@ "main": "index.js",

"dependencies": {
"@babel/core": "^7.12.10",
"@babel/parser": "^7.13.15",
"@babel/preset-env": "^7.12.11",
"@babel/traverse": "^7.13.15",
"acorn-jsx": "^5.3.1",
"acorn-loose": "^8.0.1",
"acorn-walk": "^8.0.1",
"debug": "^4.2.0",

@@ -42,0 +41,0 @@ "filewatcher": "^3.0.1"

@@ -846,2 +846,61 @@ const fs = require('fs-extra')

test('all', async () => {
const files = {
a: {
url: './all/a.js',
content: `
import * as a_b from './a_b.js'
import { a_c } from './a_c.js'
const a_d = require('./a_d.js')
let lazy = null
if (true) lazy = import('./a_e.js')
export default ''
`
},
a_b: {
url: './all/a_b.js',
content: `
export const a_b = () => {}
`
},
a_c: {
url: './all/a_c.js',
content: `
export const a_c = () => {}
`
},
a_d: {
url: './all/a_d.js',
content: `
module.exports = { a_d() {} }
`
},
a_e: {
url: './all/a_e.js',
content: `
export const a_e = () => {}
`
}
}
const fsx = fixtures.create(files)
const w = graph({ cwd: fixtures.getRoot() })
w.add([fsx.files.a])
await wait(DELAY)
const tree = w.tree
assert(!!tree[fsx.files.a_b])
assert(!!tree[fsx.files.a_c])
assert(!!tree[fsx.files.a_d])
assert(!!tree[fsx.files.a_e])
w.close()
fsx.cleanup()
})
!(async function () {

@@ -848,0 +907,0 @@ console.time('test')

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