Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions packages/consumer/src/pkv/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { FetchError, ofetch } from 'ofetch';
import type { PackFile } from 'publint';
import { Result } from 'better-result';
import { join } from 'node:path';
import * as v from 'valibot';

const FileSchema = v.object({
type: v.literal('file'),
name: v.string(),
hash: v.string(),
size: v.number(),
});

interface Directory {
type: 'directory';
name: string;
files: (Directory | v.InferOutput<typeof FileSchema>)[];
}

const DirectorySchema: v.GenericSchema<Directory> = v.object({
type: v.literal('directory'),
name: v.string(),
files: v.array(v.union([v.lazy(() => DirectorySchema), FileSchema])),
});

const ResponseSchema = v.object({
type: v.literal('npm'),
files: v.array(v.union([v.lazy(() => DirectorySchema), FileSchema])),
});

type CreateVFSResult = Result<
PackFile[],
FetchError | v.ValiError<typeof ResponseSchema>
>;

export async function createPackFiles(
name: string,
version: string,
): Promise<CreateVFSResult> {
const raw = await Result.tryPromise(async () => {
return await ofetch(`/npm/${name}@${version}`, {
baseURL: 'https://data.jsdelivr.com/v1/packages',
headers: {
'User-Agent': 'npm.rest (+https://github.com/e18e/npm.rest)',
},
});
});

if (raw.isErr()) {
return Result.err(raw.error);
}

const data = v.safeParse(ResponseSchema, raw.value);

if (!data.success) {
return Result.err(new v.ValiError(data.issues));
}

const files: PackFile[] = [];

function visit(nodes: Directory['files'], prefix: string) {
for (const node of nodes) {
if (node.type === 'directory') {
visit(node.files, join(prefix, node.name));
continue;
}

const path = join(prefix, node.name);

files.push({
name: path,
async data() {
return await ofetch(path, {
baseURL: `https://cdn.jsdelivr.net/npm/${name}@${version}`,
headers: {
'User-Agent':
'npm.rest (+https://github.com/e18e/npm.rest)',
},
responseType: 'arrayBuffer',
});
},
});
}
}

visit(data.output.files, '/');

return Result.ok(files);
}
9 changes: 4 additions & 5 deletions packages/consumer/src/pkv/publint.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { UnpackResult } from '@publint/pack';
import { publint, type PackFile } from 'publint';
import { Result } from 'better-result';
import { publint } from 'publint';

export async function runPublint(tarball: UnpackResult) {
export async function runPublint(files: PackFile[]) {
return await Result.tryPromise(async () => {
return await publint({
pkgDir: tarball.rootDir,
pack: { files: tarball.files },
pkgDir: '/',
pack: { files },
});
});
}
46 changes: 0 additions & 46 deletions packages/consumer/src/pkv/tarball.ts

This file was deleted.

37 changes: 37 additions & 0 deletions patches/publint.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
diff --git a/src/index.d.ts b/src/index.d.ts
index 3b355f94e7e3855bda69c1f842dc57ecf210221d..319b6b9449c954bc111caf10de848884c69d12d1 100644
--- a/src/index.d.ts
+++ b/src/index.d.ts
@@ -133,9 +133,11 @@ export type Message =
| BaseMessage<'IMPORTS_FALLBACK_ARRAY_USE'>
| BaseMessage<'CJS_WITH_ESMODULE_DEFAULT_EXPORT', { filePath?: string }>

+type MaybePromiseFn<T> = T | (() => Promise<T>)
+
export interface PackFile {
name: string
- data: string | ArrayBuffer | Uint8Array
+ data: MaybePromiseFn<string | ArrayBuffer | Uint8Array>
}

export interface Options {
diff --git a/src/shared/vfs-tarball.js b/src/shared/vfs-tarball.js
index b9231ea2afeddc6aab5959949157ca5d0faf9122..eea75c7bc7689ddb895950bc8c74c9de2872dc16 100644
--- a/src/shared/vfs-tarball.js
+++ b/src/shared/vfs-tarball.js
@@ -28,11 +28,12 @@ export function createTarballVfs(files) {
readFile: async (path) => {
const file = files.find((file) => file.name === path)
if (!file) throw new Error(`Unable to read file at path: ${path}`)
- if (typeof file.data === 'string') {
- return file.data
+ const data = typeof file.data === 'function' ? await file.data() : file.data
+ if (typeof data === 'string') {
+ return data
} else {
decoder ??= new TextDecoder()
- return decoder.decode(file.data)
+ return decoder.decode(data)
}
},
}
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ onlyBuiltDependencies:
- esbuild
- workerd
- sharp

patchedDependencies:
publint: patches/publint.patch