本文整理汇总了TypeScript中domain.create函数的典型用法代码示例。如果您正苦于以下问题:TypeScript create函数的具体用法?TypeScript create怎么用?TypeScript create使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('concurrent domain hubs', done => {
const d1 = domain.create();
const d2 = domain.create();
let d1done = false;
let d2done = false;
d1.run(() => {
const hub = getCurrentHub();
hub.getStack().push({ client: 'process' });
expect(hub.getStack()[1]).toEqual({ client: 'process' });
// Just in case so we don't have to worry which one finishes first
// (although it always should be d2)
setTimeout(() => {
d1done = true;
if (d2done) {
done();
}
});
});
d2.run(() => {
const hub = getCurrentHub();
hub.getStack().push({ client: 'local' });
expect(hub.getStack()[1]).toEqual({ client: 'local' });
setTimeout(() => {
d2done = true;
if (d1done) {
done();
}
});
});
});
示例2: sentryRequestMiddleware
return function sentryRequestMiddleware(
req: http.IncomingMessage,
res: http.ServerResponse,
next: (error?: any) => void,
): void {
if (options && options.flushTimeout && options.flushTimeout > 0) {
// tslint:disable-next-line: no-unbound-method
const _end = res.end;
res.end = function(chunk?: any | (() => void), encoding?: string | (() => void), cb?: () => void): void {
flush(options.flushTimeout)
.then(() => {
_end.call(this, chunk, encoding, cb);
})
.catch(e => {
logger.error(e);
});
};
}
const local = domain.create();
local.add(req);
local.add(res);
local.on('error', next);
local.run(() => {
getCurrentHub().configureScope(scope =>
scope.addEventProcessor((event: Event) => parseRequest(event, req, options)),
);
next();
});
};
示例3: _postRequest
/**
* Posts a request to the Akismet API server.
*/
private _postRequest(hostname: string, path: string,
query: { [key: string]: any }, callback: PostRequestCallback): void {
const requestUrl = formatURL({
protocol: this._port === 443 ? "https" : "http",
hostname: hostname,
pathname: path,
port: this._port
});
const options = {
"url": requestUrl,
"form": query,
"headers": {
"content-type": "charset=" + this._charSet,
"user-agent": this._userAgent
}
};
const dom = createDomain();
dom.on("error", (err) => callback(err, 0, ""));
dom.run(() => {
postRequest(options, (err: any, response: Response, body: any) => {
if (err) {
callback(err, 0, "");
} else {
callback(null, response.statusCode, body);
}
});
});
}
示例4: runInContextMiddleware
return function runInContextMiddleware(req, res, next) {
// We want multiple request-context consumers to use the same domain
// context object rather than creating a bunch of nested domains.
// Their namespaces should be sufficient to keep each consumer's
// data separate from the others.
if (domain.active && domain.active.__$cntxt__) {
setContext(namespace, Object.create(null), domain.active);
next();
return;
}
var d = domain.create();
d.add(req);
d.add(res);
d.on('error', handleError);
setContext(namespace, Object.create(null), d);
d.run(next);
function handleError(err) {
res.setHeader('Connection', 'close');
next(err);
}
};
示例5: function
socket.on(namespace + '.' + methodName, function(opts){
var d = domain.create();
d.on('error', function(err){
log.error('error while making command', methodName, namespace, opts);
})
d.run(function(){
var inStream = createInSocketStream(socket, opts._uid);
var outStream = createOutSocketStream(socket, opts._uid);
var infoHandler = createInfoSocketHandler(socket, opts._info_uid);
var commandStream = api[namespace][methodName](opts, infoHandler);
inStream.pipe(commandStream).pipe(outStream);
if(opts.initialData !== undefined){
inStream.write(opts.initialData);
}
})
})
示例6: function
app.post('/', (req, res) => {
var d = domain.create();
var responseHasBeenSent = false;
d.on('error', function(er) {
if (!responseHasBeenSent) {
responseHasBeenSent = true;
console.log(er);
res.status(500).send(er);
}
});
d.run(function() {
var start = Date.now();
fileFetcher.fetchFiles(req.body.file, (allFiles: string[]) => {
console.log('request took ' + (Date.now() - start) + ' ms to complete');
res.status(200).send(allFiles);
});
});
});
示例7: test
test('capture an event in a domain', done => {
const d = domain.create();
const client = new NodeClient({
beforeSend: (event: Event) => {
expect(event.message).toBe('test domain');
expect(event.exception).toBeUndefined();
done();
return null;
},
dsn,
});
d.run(() => {
getCurrentHub().bindClient(client);
expect(getCurrentHub().getClient()).toBe(client);
getCurrentHub().captureEvent({ message: 'test domain' });
});
});