Socket
Socket
Sign inDemoInstall

packherd

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

packherd - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

dist/src/default-transpile-cache.d.ts

2

dist/src/packherd.d.ts

@@ -6,2 +6,3 @@ /// <reference types="node" />

export * from './types';
export { getSourceMap, getSourceMapAndContent } from './sourcemap-support';
export declare type PackherdOpts = {

@@ -15,4 +16,5 @@ entryFile: string;

bundle: Buffer;
sourceMap: Buffer | undefined;
meta: import("esbuild").Metafile;
warnings: import("esbuild").Message[];
}>;

@@ -16,3 +16,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.packherd = exports.packherdRequire = void 0;
exports.packherd = exports.getSourceMapAndContent = exports.getSourceMap = exports.packherdRequire = void 0;
const path_1 = __importDefault(require("path"));

@@ -26,2 +26,5 @@ const assert_1 = require("assert");

__exportStar(require("./types"), exports);
var sourcemap_support_1 = require("./sourcemap-support");
Object.defineProperty(exports, "getSourceMap", { enumerable: true, get: function () { return sourcemap_support_1.getSourceMap; } });
Object.defineProperty(exports, "getSourceMapAndContent", { enumerable: true, get: function () { return sourcemap_support_1.getSourceMapAndContent; } });
async function packherd(opts) {

@@ -32,3 +35,3 @@ const createBundle = opts.createBundle || create_bundle_1.createBundle;

const { outfile } = utils_1.tmpFilePaths();
const { outputFiles, metafile, warnings } = await createBundle({
const { outputFiles, metafile, sourceMap: sourceMapFile, warnings, } = await createBundle({
outdir: path_1.default.dirname(outfile),

@@ -52,4 +55,10 @@ metafile: true,

: Buffer.from(bundleFile.contents);
const sourceMap = sourceMapFile == null
? undefined
: Buffer.isBuffer(sourceMapFile === null || sourceMapFile === void 0 ? void 0 : sourceMapFile.contents)
? sourceMapFile.contents
: Buffer.from(sourceMapFile.contents);
return {
bundle,
sourceMap,
meta: metafile,

@@ -56,0 +65,0 @@ warnings,

3

dist/src/require.d.ts
import { ModuleLoaderOpts } from './loader';
import type { PackherdTranspileOpts } from './types';
import type { PackherdTranspileOpts, SourceMapLookup } from './types';
export * from './loader';

@@ -7,3 +7,4 @@ export declare type PackherdRequireOpts = ModuleLoaderOpts & {

transpileOpts?: Partial<PackherdTranspileOpts>;
sourceMapLookup?: SourceMapLookup;
};
export declare function packherdRequire(projectBaseDir: string, opts: PackherdRequireOpts): void;

@@ -19,3 +19,5 @@ "use strict";

const benchmark_1 = require("./benchmark");
const default_transpile_cache_1 = require("./default-transpile-cache");
const loader_1 = require("./loader");
const sourcemap_support_1 = require("./sourcemap-support");
const logInfo = debug_1.default('packherd:info');

@@ -30,6 +32,11 @@ const logDebug = debug_1.default('packherd:debug');

function packherdRequire(projectBaseDir, opts) {
var _a;
var _a, _b;
const Module = require('module');
const { supportTS, initTranspileCache, tsconfig } = Object.assign({}, DEFAULT_TRANSPILE_OPTS, opts.transpileOpts);
const diagnostics = (_a = opts.diagnostics) !== null && _a !== void 0 ? _a : false;
const cache = initTranspileCache == null
? new default_transpile_cache_1.DefaultTranspileCache()
: (_b = initTranspileCache(projectBaseDir, {
cacheDir: '/tmp/packherd-cache',
})) !== null && _b !== void 0 ? _b : new default_transpile_cache_1.DefaultTranspileCache();
if (supportTS) {

@@ -39,4 +46,7 @@ logInfo('Enabling TS support');

const { hookTranspileTs } = require('./transpile-ts');
hookTranspileTs(Module, projectBaseDir, logInfo, diagnostics, initTranspileCache, tsconfig);
hookTranspileTs(Module, projectBaseDir, logInfo, diagnostics, cache, opts.sourceMapLookup, tsconfig);
}
else {
sourcemap_support_1.installSourcemapSupport(cache, projectBaseDir, opts.sourceMapLookup);
}
const exportKeysLen = opts.moduleExports != null ? Object.keys(opts.moduleExports).length : 0;

@@ -43,0 +53,0 @@ const definitionKeysLen = opts.moduleDefinitions != null

@@ -1,2 +0,31 @@

import { TranspileCache } from './types';
export declare function installSourcemapSupport(cache: TranspileCache): void;
import type { MapAndSourceContent, SourceMapLookup, TranspileCache, UrlAndMap } from './types';
/**
* Retrieves the sourcemap for the provided bundle uri via the sourcemap support instance.
*
* @param projectBaseDir the root of the project for which the bundled code was generated
* @param bundleUri the path of the generated bundle
* @param cache when provided will be used to look for sourcemaps from transpiled modules
* @param sourceMapLookup when provided will be queried to lookup sourcemaps
*/
export declare function getSourceMap(projectBaseDir: string, bundleUri: string, cache?: TranspileCache, sourceMapLookup?: SourceMapLookup): UrlAndMap;
/**
* Retrieves the sourcemap for the provided bundle uri via the sourcemap support instance
* and extracts the source of the specified @see fileUri when found.
*
* @param projectBaseDir the root of the project for which the bundled code was generated
* @param bundleUri the path of the generated bundle
* @param fileUri the path for the original file we want to extract the source content for
* @param cache when provided will be used to look for sourcemaps from transpiled modules
* @param sourceMapLookup when provided will be queried to lookup sourcemaps
*/
export declare function getSourceMapAndContent(projectBaseDir: string, bundleUri: string, fileUri: string, cache?: TranspileCache, sourceMapLookup?: SourceMapLookup): MapAndSourceContent | undefined;
/**
* Creates an instance of @see SourcemapSupport and installs a hook for
* @see Error.prepareStackTrace in order to map stack traces using the source maps
* it discovers.
*
* @param cache used to look up script content from which to extract source maps
* @param projectBaseDir directory that is the root of relative source map sources
* @param sourceMapLookup: when provided is queried for source maps for a particular URI first
*/
export declare function installSourcemapSupport(cache: TranspileCache, projectBaseDir: string, sourceMapLookup?: SourceMapLookup): void;

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.installSourcemapSupport = void 0;
exports.installSourcemapSupport = exports.getSourceMapAndContent = exports.getSourceMap = void 0;
const debug_1 = __importDefault(require("debug"));

@@ -12,4 +12,9 @@ const path_1 = __importDefault(require("path"));

const convert_source_map_1 = __importDefault(require("convert-source-map"));
const default_transpile_cache_1 = require("./default-transpile-cache");
const logError = debug_1.default('packherd:error');
const logTrace = debug_1.default('packherd:trace');
const INCLUDE_CODE_BEFORE = 2;
const INCLUDE_CODE_AFTER = 2;
const CODE_FRAME_LINE_GUTTER_WIDTH = 4;
const INCLUDE_CODE_FRAMES = process.env.PACKHERD_CODE_FRAMES != null;
const EMPTY_URL_AND_MAP = { url: null, map: null };

@@ -26,10 +31,53 @@ // -----------------

const headerLength = noHeader.test(process.version) ? 0 : 62;
let sourcemapSupport;
// -----------------
// Expose uri to map + content mapping
// -----------------
/**
* Retrieves the sourcemap for the provided bundle uri via the sourcemap support instance.
*
* @param projectBaseDir the root of the project for which the bundled code was generated
* @param bundleUri the path of the generated bundle
* @param cache when provided will be used to look for sourcemaps from transpiled modules
* @param sourceMapLookup when provided will be queried to lookup sourcemaps
*/
function getSourceMap(projectBaseDir, bundleUri, cache = new default_transpile_cache_1.DefaultTranspileCache(), sourceMapLookup) {
const sourcemapSupport = SourcemapSupport.createSingletonInstance(cache, projectBaseDir, sourceMapLookup);
return sourcemapSupport.retrieveSourceMap(bundleUri);
}
exports.getSourceMap = getSourceMap;
/**
* Retrieves the sourcemap for the provided bundle uri via the sourcemap support instance
* and extracts the source of the specified @see fileUri when found.
*
* @param projectBaseDir the root of the project for which the bundled code was generated
* @param bundleUri the path of the generated bundle
* @param fileUri the path for the original file we want to extract the source content for
* @param cache when provided will be used to look for sourcemaps from transpiled modules
* @param sourceMapLookup when provided will be queried to lookup sourcemaps
*/
function getSourceMapAndContent(projectBaseDir, bundleUri, fileUri, cache = new default_transpile_cache_1.DefaultTranspileCache(), sourceMapLookup) {
const { map, url } = getSourceMap(projectBaseDir, bundleUri, cache, sourceMapLookup);
if (map == null || url == null)
return undefined;
const sourceContent = map.sourceContentFor(fileUri, true);
return { map, url, sourceContent };
}
exports.getSourceMapAndContent = getSourceMapAndContent;
// -----------------
// Install
// -----------------
function installSourcemapSupport(cache) {
if ((sourcemapSupport === null || sourcemapSupport === void 0 ? void 0 : sourcemapSupport.prepareStackTrace) === Error.prepareStackTrace)
/**
* Creates an instance of @see SourcemapSupport and installs a hook for
* @see Error.prepareStackTrace in order to map stack traces using the source maps
* it discovers.
*
* @param cache used to look up script content from which to extract source maps
* @param projectBaseDir directory that is the root of relative source map sources
* @param sourceMapLookup: when provided is queried for source maps for a particular URI first
*/
function installSourcemapSupport(cache, projectBaseDir, sourceMapLookup) {
// NOTE: this is a noop if an instance was created previously
const sourcemapSupport = SourcemapSupport.createSingletonInstance(cache, projectBaseDir, sourceMapLookup);
if (Error.prepareStackTrace === sourcemapSupport.prepareStackTrace)
return;
sourcemapSupport = new SourcemapSupport(cache);
Error.prepareStackTrace = sourcemapSupport.prepareStackTrace;

@@ -42,4 +90,6 @@ }

class SourcemapSupport {
constructor(_cache) {
constructor(_cache, _projectBaseDir, _sourceMapLookup) {
this._cache = _cache;
this._projectBaseDir = _projectBaseDir;
this._sourceMapLookup = _sourceMapLookup;
this._sourcemapCache = new Map();

@@ -55,4 +105,13 @@ // This function is part of the V8 stack trace API, for more info see:

const processedStack = [];
let includeCodeFrames = INCLUDE_CODE_FRAMES;
for (let i = stack.length - 1; i >= 0; i--) {
processedStack.push('\n at ' + this.wrapCallSite(stack[i], state));
const c = this.wrapCallSite(stack[i], state, includeCodeFrames);
if (includeCodeFrames) {
// Keep trying to include some code until we succeeded once
includeCodeFrames = c.codeFrames.length === 0;
}
for (const codeFrame of c.codeFrames.reverse()) {
processedStack.push(`\n ${codeFrame}`);
}
processedStack.push('\n at ' + c);
state.nextPos = state.curPos;

@@ -64,3 +123,3 @@ }

}
wrapCallSite(frame, state) {
wrapCallSite(frame, state, includeCodeFrames) {
var _a;

@@ -77,3 +136,3 @@ const script = frame.getFileName();

return frame;
const pos = this.mapSourcePosition({ script, line, column });
const pos = this.mapSourcePosition({ script, line, column }, includeCodeFrames);
state.curPos = pos;

@@ -101,2 +160,3 @@ frame = cloneCallSite(frame);

};
frame.codeFrames = pos.codeFrames;
return frame;

@@ -106,3 +166,3 @@ }

}
mapSourcePosition(pos) {
mapSourcePosition(pos, includeCodeFrames) {
var _a;

@@ -112,4 +172,11 @@ const sourceMap = this.retrieveSourceMap(pos.script);

const origPos = sourceMap.map.originalPositionFor(pos);
if (origPos.source != null)
return origPos;
// Sourcemap lines are 0 based so we adjust them to be 1 based to print correct stack frames
origPos.line++;
const codeFrames = includeCodeFrames
? extractCodeFrames(sourceMap.map, origPos)
: [];
if (origPos.source != null) {
origPos.source = this._ensureFullPath(origPos.source);
return Object.assign(origPos, { codeFrames });
}
}

@@ -123,2 +190,3 @@ // return generated position if we couldn't find the original

name: script,
codeFrames: [],
};

@@ -146,2 +214,26 @@ }

retrieveSourceMap(script) {
// 1. Try to load previosuly cached source map
const fromMemory = this._sourcemapCache.get(script);
if (fromMemory != null) {
logTrace('from memory sourcemap for "%s"', script);
return fromMemory;
}
// 2. Try to look it up via externally provided function
if (this._sourceMapLookup != null) {
const map = this._sourceMapLookup(script);
try {
if (map != null) {
const urlAndMap = { url: script, map: new source_map_js_1.SourceMapConsumer(map) };
this._sourcemapCache.set(script, urlAndMap);
logTrace('Retrieved sourcemap for "%s" from sourcemap lookup', script);
return urlAndMap;
}
}
catch (err) {
logError('Looked up invalid source map "%s"', script);
logError(err);
return EMPTY_URL_AND_MAP;
}
}
// 3. Try to parse a source map out of the script
// Only supporting our own TypeScript modules for now

@@ -151,9 +243,18 @@ if (path_1.default.extname(script) !== '.ts')

logTrace('retrieving sourcemap for %s', script);
const fromMemory = this._sourcemapCache.get(script);
if (fromMemory != null) {
logTrace('from memory sourcemap for %s', script);
return fromMemory;
}
return this.mapFromInlined(script);
}
_ensureFullPath(p) {
return path_1.default.isAbsolute(p) ? p : path_1.default.join(this._projectBaseDir, p);
}
/**
* Creates a [SourcmapSupport] instance unless one was created previously.
* NOTE: that it is impossible for a process to have two instances and the
* parameters the first one was created with will remain active for the process lifetime.
*/
static createSingletonInstance(cache, projectBaseDir, sourceMapLookup) {
if (SourcemapSupport._instance == null) {
SourcemapSupport._instance = new SourcemapSupport(cache, projectBaseDir, sourceMapLookup);
}
return SourcemapSupport._instance;
}
}

@@ -257,2 +358,30 @@ // -----------------

}
function extractCodeFrames(map, pos) {
const sourceContent = map.sourceContentFor(pos.source, true);
if (sourceContent == null)
return [];
// We adjusted lines to be 1 based (see mapSourcePosition)
const lineno = pos.line - 1;
const lines = sourceContent.split('\n');
const beforeStart = Math.max(0, lineno - INCLUDE_CODE_BEFORE);
const beforeEnd = Math.min(lines.length, lineno + 1);
const afterStart = Math.min(lines.length, beforeEnd);
const afterEnd = Math.min(lines.length, afterStart + INCLUDE_CODE_AFTER);
const framesBefore = lines.slice(beforeStart, beforeEnd).map((x, idx) => {
const lineGutter = (beforeStart + idx + 1)
.toString()
.padStart(CODE_FRAME_LINE_GUTTER_WIDTH);
return `${lineGutter}: ${x}`;
});
if (pos.column >= 0) {
framesBefore.push(' '.repeat(CODE_FRAME_LINE_GUTTER_WIDTH + 1 + pos.column) + '^');
}
const framesAfter = lines.slice(afterStart, afterEnd).map((x, idx) => {
const lineGutter = (afterStart + idx + 1)
.toString()
.padStart(CODE_FRAME_LINE_GUTTER_WIDTH);
return `${lineGutter}: ${x}`;
});
return framesBefore.concat(framesAfter);
}
//# sourceMappingURL=sourcemap-support.js.map
/// <reference types="node" />
import type { Debugger } from 'debug';
import { TransformOptions } from 'esbuild';
import type { TranspileCache, InitTranspileCache } from './types';
import type { TranspileCache, SourceMapLookup } from './types';
declare type EnhancedModule = NodeModule & {

@@ -10,5 +10,5 @@ _extensions: Record<string, (mod: EnhancedModule, filename: string) => void>;

};
export declare function transpileTs(fullModuleUri: string, tsconfig?: TransformOptions['tsconfigRaw'], cache?: TranspileCache): string;
export declare function transpileTsCode(fullModuleUri: string, ts: string, tsconfig?: TransformOptions['tsconfigRaw'], cache?: TranspileCache): string;
export declare function hookTranspileTs(Module: EnhancedModule, projectBaseDir: string, log: Debugger, diagnostics: boolean, initCompileCache?: InitTranspileCache, tsconfig?: TransformOptions['tsconfigRaw']): void;
export declare function transpileTs(fullModuleUri: string, cache: TranspileCache, projectBaseDir: string, sourceMapLookup?: SourceMapLookup, tsconfig?: TransformOptions['tsconfigRaw']): string;
export declare function transpileTsCode(fullModuleUri: string, ts: string, cache: TranspileCache, projectBaseDir: string, sourceMapLookup?: SourceMapLookup, tsconfig?: TransformOptions['tsconfigRaw']): string;
export declare function hookTranspileTs(Module: EnhancedModule, projectBaseDir: string, log: Debugger, diagnostics: boolean, cache: TranspileCache, sourceMapLookup?: SourceMapLookup, tsconfig?: TransformOptions['tsconfigRaw']): void;
export {};

@@ -18,3 +18,3 @@ "use strict";

};
function transpileTs(fullModuleUri, tsconfig, cache) {
function transpileTs(fullModuleUri, cache, projectBaseDir, sourceMapLookup, tsconfig) {
const cached = (cache != null && cache.get(fullModuleUri)) || null;

@@ -24,9 +24,9 @@ if (cached != null)

const ts = fs_1.default.readFileSync(fullModuleUri, 'utf8');
return transpileTsCode(fullModuleUri, ts, tsconfig, cache);
return transpileTsCode(fullModuleUri, ts, cache, projectBaseDir, sourceMapLookup, tsconfig);
}
exports.transpileTs = transpileTs;
function transpileTsCode(fullModuleUri, ts, tsconfig, cache) {
if (cache != null) {
sourcemap_support_1.installSourcemapSupport(cache);
}
function transpileTsCode(fullModuleUri, ts, cache, projectBaseDir, sourceMapLookup,
// TODO: consider 'error' for importsNotUsedAsValues (maybe) to add some type checking
tsconfig) {
sourcemap_support_1.installSourcemapSupport(cache, projectBaseDir, sourceMapLookup);
const cached = (cache != null && cache.get(fullModuleUri)) || null;

@@ -46,13 +46,4 @@ if (cached != null)

exports.transpileTsCode = transpileTsCode;
function hookTranspileTs(Module, projectBaseDir, log, diagnostics, initCompileCache, tsconfig) {
const cache = initCompileCache == null
? undefined
: initCompileCache(projectBaseDir, { cacheDir: '/tmp/packherd-cache' });
// If there is no cache we wouldn't know where to store the transpiled JavaScript and thus
// would have to transpile again just to generate sourcemaps.
// In general we expect that during development when sourcemaps are desired for on
// the fly transpiled code, then a cache is used as well.
if (cache != null) {
sourcemap_support_1.installSourcemapSupport(cache);
}
function hookTranspileTs(Module, projectBaseDir, log, diagnostics, cache, sourceMapLookup, tsconfig) {
sourcemap_support_1.installSourcemapSupport(cache, projectBaseDir, sourceMapLookup);
const defaultLoader = Module._extensions['.js'];

@@ -70,3 +61,3 @@ Module._extensions['.ts'] = function (mod, filename) {

log('transpiling %s', path_1.default.relative(projectBaseDir, filename));
const transpiled = transpileTsCode(filename, code, tsconfig, cache);
const transpiled = transpileTsCode(filename, code, cache, projectBaseDir, sourceMapLookup, tsconfig);
const compiled = mod._compile(transpiled, filename);

@@ -73,0 +64,0 @@ return compiled;

/// <reference types="node" />
import { BuildOptions, BuildResult, Metafile, OutputFile, TransformOptions } from 'esbuild';
import type { RawSourceMap, SourceMapConsumer } from 'source-map-js';
declare type NodeRequireFunction = (id: string) => any;

@@ -28,5 +29,9 @@ export declare type ModuleDefinition = (exports: NodeModule['exports'], module: {

};
export declare type CreateBundleSourcemap = {
contents: OutputFile['contents'];
};
export declare type CreateBundleResult = {
warnings: BuildResult['warnings'];
outputFiles: CreateBundleOutputFile[];
sourceMap?: CreateBundleSourcemap;
metafile?: Metafile;

@@ -51,2 +56,12 @@ };

};
export declare type SourceMapLookup = (uri: string) => RawSourceMap | undefined;
export declare type UrlAndMap = {
url: string | null;
map: SourceMapConsumer | null;
};
export declare type MapAndSourceContent = {
url: string;
map: SourceMapConsumer;
sourceContent: string;
};
export {};
{
"name": "packherd",
"version": "0.5.0",
"version": "0.6.0",
"description": "Herds all dependencies reachable from an entry and packs them.",

@@ -5,0 +5,0 @@ "main": "dist/src/packherd.js",

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

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