Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Abstract Data Types in TypeScript.
Also an exercise in figuring out best practice when distributing a TypeScript project via npm.
Concerns so far:
.npmignore
containing src/*
, Makefile
, etc.?index.js
; would that still be a good idea if it was a big project?If you have a plain JavaScript Node.js project, imports work as usual.
Download the adts
package from the npm registry:
npm install --save adts
Then require and use:
var adts = require('adts');
var primes = new adts.Set([2, 3, 5, 7, 11, 13, 17, 19, 23]);
var even = new adts.Set([2, 4, 6, 8, 10, 12, 14, 16]);
var even_primes = adts.Set.intersection([even, primes]);
console.log('primes that are even: %j', even_primes);
// prints 'primes that are even: ["2"]'
If you have a TypeScript Node.js project, using tsd
and DefinitelyTyped for most of your dependencies, imports are straightforward, but not idiomatic:
Download the adts
package from the npm registry.
npm install --save adts
Assuming tsd
is using the defaults, the aggregate d.ts
file will be in typings/tsd.d.ts
. Open that file, and add the following line:
/// <reference path="../node_modules/adts/adts.d.ts"/>
(The tsd
documentation states that tsd link
will handle that, but tsd link
isn't actually a tsd
command.)
Now importing the aggregate file will import the adts
type declarations along with it.
Suppose you want to use the adts
module in index.ts
:
/// <reference path="typings/tsd.d.ts"/>
import adts = require('adts');
var primes = new adts.Set([2, 3, 5, 7, 11, 13, 17, 19, 23]);
var even = new adts.Set([2, 4, 6, 8, 10, 12, 14, 16]);
var even_primes = adts.Set.intersection([even, primes]);
console.log('primes that are even: %j', even_primes);
Now try to compile:
tsc -m commonjs index.ts
Which produces the error message:
index.ts(4,27): error TS2345: Argument of type 'number[]' is not assignable to
parameter of type 'string[]'. Type 'number' is not assignable to type 'string'.
index.ts(5,25): error TS2345: Argument of type 'number[]' is not assignable to
parameter of type 'string[]'. Type 'number' is not assignable to type 'string'.
It turns out adts.Set
only accepts strings in its constructor, since that's how it stores elements internally.
The plain JS version automatically coerced them to strings, which is fine, but something we need to care about if we're using TypeScript.
Rewrite the sets like so:
var primes = new adts.Set(['2', '3', '5', '7', '11', '13', '17', '19', '23']);
var even = new adts.Set(['2', '4', '6', '8', '10', '12', '14', '16']);
Now recompile (tsc -m commonjs index.ts
) and run:
node index.js
// prints 'primes that are even: ["2"]'
... more?
TypeScript does not make inter-project modularity easy. Here are some features that I'd like tsc
to support.
.d.ts
type declaration for use by 3rd-party modules.As it stands, I'm not sure what the point of the declaration file is, since if you're generating a declaration file that only allows TypeScript-powered imports, you have to TypeScript-import the original modules as well, which tsc
is then obliged to compile.
Fixing this can be pretty simple for a small project: sed 's/declare module adts/declare module "adts"/g'
.d.ts
file.TypeScript (as least v1.1) does not like having multiple external module declarations in one file, e.g., 'module "adts" { ... } module "adts" { } ...'. Otherwise, it's quite happy at merging interfaces and modules, but only reads one external module declaration when there are multiple. (Maybe this is fixed in v1.3?)
.d.ts
file, and expose that module at root level in the generated .js
file(s).In a typical Node.js module, often the only place the module name shows up is in the package.json
and documentation. I like using namespaces, but TypeScript's compiler does not use them in a way that plays nice with Node.js's import idioms.
The Makefile demonstrates how I've hacked together support for these features, at least for this admittedly simplistic project.
index.js
file.adts.d.ts
.
This filename is determined by the Makefile, not the project structure or TypeScript namespaces.src/
, and these will automatically get picked up by the make process.
(But adding files to subdirectories would require further hacks.)Copyright 2014-2015 Christopher Brown. MIT Licensed.
FAQs
Abstract Data Types in TypeScript
The npm package adts receives a total of 9 weekly downloads. As such, adts popularity was classified as not popular.
We found that adts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.