本文整理匯總了TypeScript中@jonggrang/task.attempt函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript attempt函數的具體用法?TypeScript attempt怎麽用?TypeScript attempt使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了attempt函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it('should respect file key limit', async function () {
const middleware = mutter({ limits: { fieldNameSize: 4 } });
const form = new FormData();
form.append('small0', file('small0.txt'));
const result = await T.toPromise(T.attempt(submitForm(middleware, form)));
assert.ok(isLeft(result));
assert.equal((result.value as any).code, 'LIMIT_FIELD_KEY');
});
示例2: it
it('should report errors from fileFilter', async function () {
const form = new FormData();
const upload = withFilter(skipFileError);
form.append('notme', file('tiny0.json'));
const ectx = await T.toPromise(T.attempt(submitForm(upload, form)));
assert.equal(isLeft(ectx), true);
assert.equal((ectx.value as Error).message, 'Fake error');
});
示例3: sendRsp
function sendRsp(
conn: Z.Connection,
ii: Z.InternalInfo,
status: H.Status,
headers: H.ResponseHeaders,
rsp: Rsp
): T.Task<[P.Maybe<H.Status>, P.Maybe<number>]> {
switch (rsp.tag) {
case RspType.RSPBUFFER:
return conn.writeHead(status, headers)
.chain(() => conn.sendAll(rsp.buffer))
.then(T.pure([P.just(status), P.just(Buffer.byteLength(rsp.buffer))] as [P.Maybe<H.Status>, P.Maybe<number>]));
case RspType.RSPREADABLE:
return conn.writeHead(status, headers)
.chain(_ => conn.sendStream(rsp.readable))
.map(_ => [P.just(status), P.nothing] as [P.Maybe<H.Status>, P.Maybe<number>]);
case RspType.RSPNOBODY:
return conn.writeHead(status, headers)
.then(T.pure([P.just(status), P.nothing] as [P.Maybe<H.Status>, P.Maybe<number>]));
case RspType.RSPFILE:
if (rsp.part != null) {
const part = rsp.part;
return sendRspFile2XX(conn, ii, status, addContentHeadersForFilePart(headers, part),
rsp.path, part.offset, part.byteCount, rsp.isHead);
}
return T.attempt(ii.getFinfo(rsp.path))
.chain(efinfo => {
if (P.isLeft(efinfo)) {
return sendRspFile404(conn, ii, headers);
}
const rspFile = conditionalRequest(efinfo.value, headers, rsp.header);
switch (rspFile.tag) {
case RspFileInfoType.WITHBODY:
return sendRspFile2XX(conn, ii, rspFile.status, rspFile.header, rsp.path,
rspFile.offset, rspFile.length, rsp.isHead);
case RspFileInfoType.WITHOUTBODY:
return sendRsp(conn, ii, rspFile.status, headers, { tag: RspType.RSPNOBODY });
}
});
case RspType.RSPSTREAM:
return conn.writeHead(status, headers)
.chain(_ => rsp.body(buff => conn.sendAll(buff)))
.map(_ => [P.just(status), P.nothing] as [P.Maybe<H.Status>, P.Maybe<number>]);
/* istanbul ignore next */
default:
throw new TypeError('last argument to sendRsp must be Rsp');
}
}
示例4: withFdCache
T.supervise(T.co(function* () {
let fdRef: R.Ref<number> = yield R.newRef(-1);
yield withFdCache(3000, getFd =>
getFd(0)(path.join(__dirname, '..', 'package.json')).chain(fd =>
R.writeRef(fdRef, (fd[0] as any).value))
);
let fd: number = yield R.readRef(fdRef);
return T.attempt(T.node(null, fd, fs.readFile)).chain(mcont => {
assert.ok(isLeft(mcont));
return T.pure(void 0);
});
}))
示例5: fileSystemLookup
export function fileSystemLookup(
etagFn: EtagLookup,
prefix: string,
pieces: Piece[],
weakEtags: boolean
): T.Task<LookupResult> {
const fp = pathFromPieces(prefix, pieces);
return T.attempt(T.node(null, fp, fs.stat))
.chain(estat => {
if (isLeft(estat))
return T.pure({ tag: LookupResultType.LRNOTFOUND } as LookupResult);
const { value: stat } = estat;
if (stat.isFile()) {
return T.pure(mkLookupResult(LookupResultType.LRFILE, mkFile(
stat.size,
(s, h) => W.responseFile(s, h, fp),
pieces.length === 0 ? '' : pieces[pieces.length - 1],
etagFn(stat, weakEtags),
just(stat.mtime)
)));
}
return T.node(null, fp, fs.readdir)
.chain(entries_ => {
const entries = entries_.filter(isVisible);
return T.forIn(entries, frel => {
const fpAbs = path.join(fp, frel);
return T.attempt(T.node(null, fpAbs, fs.stat))
.map(estate => {
if (isLeft(estate)) {
return nothing as Maybe<Either<string, File>>;
}
const stat = estate.value;
if (stat.isDirectory()) {
return just(left(frel)) as Maybe<Either<string, File>>;
}
// file
return just(right(mkFile(
stat.size,
(s, h) => W.responseFile(s, h, fpAbs),
frel,
etagFn(stat, weakEtags),
just(stat.mtime)
))) as Maybe<Either<string, File>>;
});
}).map(xs => mkLookupResult(LookupResultType.LRFOLDER, catMaybes(xs)));
});
});
}
示例6: testRunner
function testRunner(headers: RequestHeaders, fp: string, rsp: RspFileInfoWithBody) {
return T.attempt(getFileInfo(fp))
.chain(efinfo => {
if (isLeft(efinfo)) {
assert.equal(true, false);
return T.pure(false);
}
const { value: finfo } = efinfo;
let hs = S.assign({}, rsp.header, {
'Last-Modified': finfo.date
});
let rsp2 = rspFileInfo(RspFileInfoType.WITHBODY, rsp.status, hs, rsp.offset, rsp.length);
assert.deepEqual(conditionalRequest(finfo, {}, headers), rsp2);
return T.pure(false);
});
}