Skip to content

Commit

Permalink
chore: update tests and types
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Sep 1, 2023
1 parent c2448fd commit 50dd308
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 135 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"arbundles": "^0.9.9",
"arweave": "^1.14.4",
"axios": "^1.4.0",
"elliptic": "^6.5.4",
"jwk-to-pem": "^2.0.5",
"retry-axios": "^3.0.0",
"winston": "^3.10.0"
Expand Down
8 changes: 2 additions & 6 deletions src/common/turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Readable } from 'stream';

import {
Currency,
TurboBalanceResponse,
TurboCountriesResponse,
TurboCurrenciesResponse,
TurboFiatToArResponse,
TurboFileFactory,
TurboPriceResponse,
TurboPrivateClient,
TurboPrivateClientConfiguration,
Expand Down Expand Up @@ -220,10 +219,7 @@ export class TurboAuthenticatedClient implements TurboPrivateClient {
async uploadFiles({
fileStreamGenerator,
bundle = false,
}: {
fileStreamGenerator: (() => Readable)[] | (() => ReadableStream)[];
bundle?: boolean;
}): Promise<TurboUploadDataItemsResponse> {
}: TurboFileFactory): Promise<TurboUploadDataItemsResponse> {
return this.uploadService.uploadFiles({ fileStreamGenerator, bundle });
}

Expand Down
57 changes: 49 additions & 8 deletions src/common/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { JWKInterface } from '../types/arweave.js';
import {
TransactionId,
TurboDataItemSigner,
TurboDataItemVerifier,
TurboFileFactory,
TurboPrivateUploadService,
TurboPrivateUploadServiceConfiguration,
TurboPublicUploadService,
Expand All @@ -41,7 +43,7 @@ export class TurboUnauthenticatedUploadService
implements TurboPublicUploadService
{
protected axios: AxiosInstance;
protected dataItemVerifier: TurboNodeDataItemVerifier;
protected dataItemVerifier: TurboDataItemVerifier;

constructor({
url = 'https://upload.ardrive.dev',
Expand Down Expand Up @@ -72,8 +74,50 @@ export class TurboUnauthenticatedUploadService
throw new Error('One or more data items failed signature validation');
}

// TODO: upload the files
return {} as TurboUploadDataItemsResponse;
const signedDataItems: Readable[] = dataItemGenerator.map((dataItem) =>
dataItem(),
);

// TODO: add p-limit constraint
const uploadPromises = signedDataItems.map((signedDataItem) => {
return this.axios.post<TurboUploadDataItemResponse>(
`/tx`,
signedDataItem,
{
headers: {
'content-type': 'application/octet-stream',
},
},
);
});

// NOTE: our axios config (validateStatus) swallows errors, so failed data items will be ignored
const dataItemResponses = await Promise.all(uploadPromises);
const postedDataItems = dataItemResponses.reduce(
(
postedDataItemsMap: Record<
TransactionId,
Omit<TurboUploadDataItemResponse, 'id'>
>,
dataItemResponse: AxiosResponse<TurboUploadDataItemResponse, 'id'>,
) => {
// handle the fulfilled response
const { status, data } = dataItemResponse;
if (![200, 202].includes(status)) {
// TODO: add to failed data items array
return postedDataItemsMap;
}
const { id, ...dataItemCache } = data;
postedDataItemsMap[id] = dataItemCache;
return postedDataItemsMap;
},
{},
);

return {
ownerAddress: publicKey,
dataItems: postedDataItems,
};
}
}

Expand All @@ -83,7 +127,7 @@ export class TurboAuthenticatedUploadService
protected axios: AxiosInstance;
protected privateKey: JWKInterface | undefined;
protected dataItemSigner: TurboDataItemSigner;
protected dataItemVerifier: TurboNodeDataItemVerifier;
protected dataItemVerifier: TurboDataItemVerifier;

constructor({
url = 'https://upload.ardrive.dev',
Expand Down Expand Up @@ -166,10 +210,7 @@ export class TurboAuthenticatedUploadService
async uploadFiles({
fileStreamGenerator,
bundle = false, // TODO: add bundle param to allow for creating BDI of data items
}: {
fileStreamGenerator: (() => Readable)[];
bundle?: boolean;
}): Promise<TurboUploadDataItemsResponse> {
}: TurboFileFactory): Promise<TurboUploadDataItemsResponse> {
if (!this.privateKey) {
throw new UnauthenticatedRequestError();
}
Expand Down
8 changes: 5 additions & 3 deletions src/node/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { JWKInterface } from '../types/arweave.js';
import {
TurboDataItemSigner,
TurboDataItemVerifier,
TurboFileFactory,
TurboSignedDataItemFactory,
} from '../types/turbo.js';
import { UnauthenticatedRequestError } from '../utils/errors.js';
Expand All @@ -33,7 +34,9 @@ export class TurboNodeDataItemVerifier implements TurboDataItemVerifier {
dataItemGenerator,
signature,
publicKey,
}: TurboSignedDataItemFactory): Promise<boolean> {
}: Omit<TurboSignedDataItemFactory, 'dataItemGenerator'> & {
dataItemGenerator: (() => Readable)[];
}): Promise<boolean> {
const fullKey = {
kty: 'RSA',
e: 'AQAB',
Expand Down Expand Up @@ -80,9 +83,8 @@ export class TurboNodeDataItemSigner implements TurboDataItemSigner {
signDataItems({
fileStreamGenerator,
bundle = false, // TODO: add bundle param to allow for creating BDI of data items
}: {
}: Omit<TurboFileFactory, 'fileStreamGenerator'> & {
fileStreamGenerator: (() => Readable)[];
bundle?: boolean;
}): Promise<Readable>[] {
// TODO: break this into separate classes
if (!this.privateKey) {
Expand Down
8 changes: 4 additions & 4 deletions src/types/turbo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { RetryConfig } from 'retry-axios';
import { PassThrough, Readable } from 'stream';
import { Readable } from 'stream';
import winston from 'winston';

import { JWKInterface } from './arweave.js';
Expand Down Expand Up @@ -111,13 +111,13 @@ export type TurboPrivateClientConfiguration = {
export type TurboFileFactory = {
fileStreamGenerator: (() => Readable)[] | (() => ReadableStream)[];
bundle?: boolean;
// todo: add payload size
// TODO: add payload size
};

// TODO: add web one for ReadableStream
// TODO: add web one for ReadableStream or Buffer depending on how best to implement
export type TurboSignedDataItemFactory = {
dataItemGenerator: (() => Readable)[];
publicKey: string; // TODO add type
publicKey: string; // TODO: add type
signature: Buffer; // TODO: could also be a buffer
};

Expand Down
17 changes: 8 additions & 9 deletions src/web/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { ArweaveSigner, createData } from 'arbundles';
import { AxiosInstance } from 'axios';

import { JWKInterface } from '../types/arweave.js';
import { TurboDataItemSigner } from '../types/turbo.js';
import {
TurboDataItemSigner,
TurboFileFactory,
TurboSignedDataItemFactory,
} from '../types/turbo.js';
import { readableStreamToBuffer } from '../utils/readableStream.js';

export class TurboWebDataItemSigner implements TurboDataItemSigner {
Expand All @@ -33,21 +37,16 @@ export class TurboWebDataItemSigner implements TurboDataItemSigner {
dataItemGenerator,
signature,
publicKey,
}: {
dataItemGenerator: (() => ReadableStream)[];
signature: string;
publicKey: string;
}): boolean {
}: TurboSignedDataItemFactory): boolean {
console.log(dataItemGenerator, signature, publicKey);
throw new Error('Not implemented!');
}

signDataItems({
fileStreamGenerator,
bundle = false,
}: {
}: Omit<TurboFileFactory, 'fileStreamGenerator'> & {
fileStreamGenerator: (() => ReadableStream)[];
bundle?: boolean;
}): Promise<Buffer>[] {
if (bundle) {
throw new Error('Not implemented!');
Expand All @@ -57,7 +56,7 @@ export class TurboWebDataItemSigner implements TurboDataItemSigner {

const signedDataItemPromises = fileStreamGenerator.map(
async (streamGenerator: () => ReadableStream) => {
// Convert the readable stream to a Blob
// Convert the readable stream to a buffer
const buffer = await readableStreamToBuffer({
stream: streamGenerator(),
});
Expand Down
Loading

0 comments on commit 50dd308

Please sign in to comment.