-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move blob index logic from upload-api to blob-index lib
- Loading branch information
Showing
20 changed files
with
539 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: blob-index | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'packages/blob-index/**' | ||
- 'packages/eslint-config-w3up/**' | ||
- '.github/workflows/blob-index.yml' | ||
- 'pnpm-lock.yaml' | ||
pull_request: | ||
paths: | ||
- 'packages/blob-index/**' | ||
- 'packages/eslint-config-w3up/**' | ||
- '.github/workflows/blob-index.yml' | ||
- 'pnpm-lock.yaml' | ||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./packages/blob-index | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Install | ||
uses: pnpm/action-setup@v2.2.3 | ||
with: | ||
version: 8 | ||
- name: Setup | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
registry-url: https://registry.npmjs.org/ | ||
cache: 'pnpm' | ||
- run: pnpm --filter '@web3-storage/blob-index...' install | ||
- run: pnpm --filter '@web3-storage/blob-index' lint | ||
- run: pnpm --filter '@web3-storage/blob-index' test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"name": "@web3-storage/blob-index", | ||
"description": "blob index library", | ||
"version": "0.0.1", | ||
"homepage": "https://web3.storage", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/w3s-project/w3up.git", | ||
"directory": "packages/blob-index" | ||
}, | ||
"license": "(Apache-2.0 OR MIT)", | ||
"type": "module", | ||
"types": "dist/src/index.d.ts", | ||
"main": "src/index.js", | ||
"files": [ | ||
"src", | ||
"test", | ||
"dist" | ||
], | ||
"exports": { | ||
".": "./dist/src/index.js", | ||
"./types": "./dist/src/types.js" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"types": [ | ||
"dist/src/types" | ||
] | ||
} | ||
}, | ||
"scripts": { | ||
"attw": "attw --pack .", | ||
"build": "tsc --build", | ||
"check": "tsc --build", | ||
"lint": "tsc --build && eslint '**/*.{js,ts}' && prettier --check '**/*.{js,ts,yml,json}' --ignore-path ../../.gitignore", | ||
"test": "mocha --bail --timeout 10s -n no-warnings -n experimental-vm-modules -n experimental-fetch test/**/*.spec.js", | ||
"test-watch": "pnpm build && mocha --bail --timeout 10s --watch --parallel -n no-warnings -n experimental-vm-modules -n experimental-fetch --watch-files src,test" | ||
}, | ||
"dependencies": { | ||
"@ipld/dag-cbor": "^9.0.6", | ||
"carstream": "^2.1.0", | ||
"multiformats": "^13.0.1", | ||
"@ucanto/core": "^10.0.1", | ||
"@ucanto/interface": "^10.0.1", | ||
"@ucanto/server": "^10.0.0", | ||
"uint8arrays": "^5.0.3" | ||
}, | ||
"devDependencies": { | ||
"@ipld/car": "^5.1.1", | ||
"@ipld/dag-ucan": "^3.4.0", | ||
"@types/assert": "^1.5.6", | ||
"@types/mocha": "^10.0.1", | ||
"@ucanto/transport": "^9.1.1", | ||
"@web3-storage/eslint-config-w3up": "workspace:^", | ||
"@web-std/blob": "^3.0.5", | ||
"one-webcrypto": "git://github.com/web3-storage/one-webcrypto", | ||
"mocha": "^10.2.0", | ||
"typescript": "5.2.2" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"@web3-storage/eslint-config-w3up" | ||
], | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
}, | ||
"env": { | ||
"mocha": true | ||
}, | ||
"ignorePatterns": [ | ||
"dist", | ||
"coverage", | ||
"src/types.js" | ||
] | ||
}, | ||
"engines": { | ||
"node": ">=16.15" | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import * as API from './types.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { MultihashDigest } from 'multiformats' | ||
import { ShardedDAGIndex } from '../src/api.js' | ||
|
||
export type { ShardDigest, SliceDigest, ShardedDAGIndex } from '../src/api.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as assert from 'assert' | ||
import * as blobIndex from '../src/sharded-dag-index.js' | ||
import { CAR } from '@ucanto/core' | ||
import { randomCAR } from './util.js' | ||
|
||
describe('blob-index', async () => { | ||
await testBlobIndex(blobIndex, async (name, test) => it(name, test)) | ||
}) | ||
|
||
/** | ||
* @param {typeof blobIndex} blobIndex - blob-index module to test | ||
* @param {import("./test-types.js").TestAdder} test - function to call to add a named test | ||
*/ | ||
async function testBlobIndex(blobIndex, test) { | ||
await test('module is an object', async () => { | ||
assert.equal(typeof blobIndex, 'object') | ||
}) | ||
|
||
await test('from to archive', async () => { | ||
const contentCAR = await randomCAR(32) | ||
const contentCARBytes = new Uint8Array(await contentCAR.arrayBuffer()) | ||
|
||
const index = await blobIndex.fromShardArchives(contentCAR.roots[0], [ | ||
contentCARBytes, | ||
]) | ||
try { | ||
const indexCAR = unwrap(await index.toArchive()) | ||
assert.deepStrictEqual(indexCAR, contentCARBytes) | ||
//const indexCARBytes = new Uint8Array(await indexCAR.arrayBuffer()) | ||
//assert.deepStrictEqual(indexCARBytes, contentCARBytes) | ||
//const indexLink = await CAR.link(indexCAR) | ||
} catch (error) { | ||
if (error != undefined) { | ||
assert.fail(String(error)) | ||
} | ||
} | ||
|
||
assert.notStrictEqual(index.shards.size, 0) | ||
let i = 0 | ||
for (const shard of index.shards.values()) { | ||
assert.notStrictEqual(shard.size, 0) | ||
console.log('shard', i, 'has', shard.size, 'blocks') | ||
i++ | ||
} | ||
}) | ||
} | ||
|
||
const unwrap = ({ ok, error }) => { | ||
if (error) { | ||
throw error | ||
} else { | ||
return /** @type {T} */ (ok) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["src", "test"], | ||
"exclude": ["**/node_modules/**", "dist"], | ||
"references": [], | ||
"typedocOptions": { | ||
"entryPoints": ["./src"] | ||
} | ||
} | ||
|
||
// similar to mocha `it` | ||
export type TestAdder = ( | ||
name: string, | ||
runTest: () => Promise<unknown> | ||
) => Promise<unknown> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { CID } from 'multiformats' | ||
import { webcrypto } from 'one-webcrypto' | ||
import { sha256 } from 'multiformats/hashes/sha2' | ||
import * as CAR from '@ucanto/transport/car' | ||
import * as raw from 'multiformats/codecs/raw' | ||
import { CarWriter } from '@ipld/car' | ||
import { Blob } from '@web-std/blob' | ||
|
||
/** @param {number} size */ | ||
export async function randomBytes(size) { | ||
const bytes = new Uint8Array(size) | ||
while (size) { | ||
const chunk = new Uint8Array(Math.min(size, 65_536)) | ||
webcrypto.getRandomValues(chunk) | ||
|
||
size -= bytes.length | ||
bytes.set(chunk, size) | ||
} | ||
return bytes | ||
} | ||
|
||
/** @param {number} size */ | ||
export async function randomCAR(size) { | ||
const bytes = await randomBytes(size) | ||
const hash = await sha256.digest(bytes) | ||
const root = CID.create(1, raw.code, hash) | ||
|
||
// @ts-expect-error old multiformats in @ipld/car | ||
const { writer, out } = CarWriter.create(root) | ||
writer.put({ cid: root, bytes }) | ||
writer.close() | ||
|
||
const chunks = [] | ||
for await (const chunk of out) { | ||
chunks.push(chunk) | ||
} | ||
const blob = new Blob(chunks) | ||
const cid = await CAR.codec.link(new Uint8Array(await blob.arrayBuffer())) | ||
|
||
return Object.assign(blob, { cid, roots: [root] }) | ||
} | ||
|
||
// eslint-disable-next-line | ||
export async function randomCID() { | ||
const bytes = await randomBytes(10) | ||
const hash = await sha256.digest(bytes) | ||
return CID.create(1, raw.code, hash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["src", "test"], | ||
"exclude": ["**/node_modules/**", "dist"], | ||
"references": [], | ||
"typedocOptions": { | ||
"entryPoints": ["./src"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.