generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.ts
53 lines (51 loc) · 2.21 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { readFile } from 'fs/promises';
import { join } from 'path';
import { Logger } from '../utils/logger.js';
type Function = (...args: any[]) => any;
type ResolveContext = {
conditions: string[];
importAttributes: {};
parentURL: string;
};
type ResolveResponse = {
[key: string]: any;
shortCircuit?: boolean
};
/**
* @summary resolve is a built-in hook that allows you to intercept and modify the
* ResolveResponse of a module specifier. Function that takes in a normal
* import (i.e. an import path), detects a DMI, converts to a DRL and fetches the resource.
* @param specifier either normal code import or DMI (e.g. @drpm/packageName~method~id)
* @param context pre-defined argument required for the resolve hook
* @param defaultResolve pre-defined argument required for the resolve hook
* @returns a promise that resolves to a ResolveResponse object
*/
export async function resolve(
specifier: string,
context: ResolveContext,
defaultResolve: Function
): Promise<ResolveResponse> {
if (specifier.match(/@drpm\/~.*/gi)) {
Logger.log(`DMI detected! Resolving specifier ${specifier}`);
const [name, method, id] = specifier.split('@drpm/')?.[1]?.split('~') ?? [];
if (!(name && method && id)) {
throw new Error(`DrpmHooks: Failed to resolve specifier ${specifier} - invalid DMI format`);
}
const did = !method ? `did:dht:${name}` : `did:${method}:${name}`;
Logger.log('name, method, id, did', name, method, id, did);
const dpkPath = join(process.cwd(), 'node_modules', specifier);
const dpkPackageJson = JSON.parse(await readFile(join(dpkPath, 'package.json'), 'utf8'));
const entryPoint = dpkPackageJson.module || dpkPackageJson.main;
Logger.log(`Resolved path ${dpkPath} to dpk ${name} and entryPoint ${entryPoint}`);
specifier = `${dpkPath}/${entryPoint}`;
Logger.log(`Final specifier=${specifier}`);
}
Logger.log('resolve => specifier, context, defaultResolve', specifier, context, defaultResolve);
return defaultResolve(specifier, context, defaultResolve);
}
export async function load(url: string, context: any, defaultLoad: Function) {
if (url.includes('@drpm')) {
Logger.log('DMI detected! Loading', url);
}
return defaultLoad(url, context, defaultLoad);
}