@yopdev/dev-server 2.0.0-alpha.1 → 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,7 +18,7 @@ class Container {
18
18
  .then((started) => this.started = started)
19
19
  .then(() => this.endpointBuilder(this.started.getFirstMappedPort()))
20
20
  .tap((url) => this.LOGGER.debug(url));
21
- this.stop = async () => this.started.stop().then(() => undefined);
21
+ this.stop = async () => (this.started ?? { stop: async () => { this.LOGGER.warn('no container'); } }).stop().then(() => undefined);
22
22
  this.LOGGER = logging_1.LoggerFactory.create(`CONTAINER[${name}]`);
23
23
  const generic = new testcontainers_1.GenericContainer(image);
24
24
  const withEnvironment = environment ? generic.withEnvironment(environment) : generic;
@@ -32,13 +32,14 @@ class Lazy {
32
32
  }
33
33
  class Promised {
34
34
  constructor(configurable) {
35
- this.configurable = configurable;
36
- this.start = async (config) => this.configurable(config)
37
- .then((service) => this.service = service)
38
- .then((service) => this.LOGGER.info('DISCOVERY:%s', service.name))
39
- .then(() => this.service.start(config));
40
- this.stop = async () => this.service.stop();
41
35
  this.name = (0, crypto_1.randomUUID)();
42
36
  this.LOGGER = logging_1.LoggerFactory.create(`PROMISED[${this.name}]`);
37
+ let initialized;
38
+ this.start = async (config) => configurable(config)
39
+ .catch((e) => e?.cleanup?.() ?? Promise.reject(e))
40
+ .then((service) => initialized = service)
41
+ .tap((service) => this.LOGGER.info('DISCOVERY:%s', service.name))
42
+ .then((service) => service.start(config));
43
+ this.stop = async () => (initialized ?? { stop: async () => this.LOGGER.info('not started') }).stop();
43
44
  }
44
45
  }
@@ -10,4 +10,5 @@ export declare class DevServer implements Lifecycle<DevServer> {
10
10
  constructor(name: string, service: Service<any>, config?: Config);
11
11
  start: () => Promise<this>;
12
12
  stop: () => Promise<void>;
13
+ private shutdown;
13
14
  }
@@ -42,12 +42,16 @@ class DevServer {
42
42
  }))))
43
43
  .tap((config) => this.LOGGER.debug('config is %o', config))
44
44
  .then((aws) => this.service.start(aws))
45
+ .catch((e) => this.shutdown()
46
+ .then(() => this.LOGGER.error('error starting service'))
47
+ .then(() => Promise.reject(e)))
45
48
  .then(() => this.LOGGER.info('started'))
46
49
  .then(() => this);
47
- this.stop = () => this.service.stop()
48
- .then(tunnel_1.terminate)
49
- .then(() => this.localStack?.stop())
50
+ this.stop = () => this.shutdown()
50
51
  .then(() => this.LOGGER.info('stopped'));
52
+ this.shutdown = async () => this.service.stop()
53
+ .then(tunnel_1.terminate)
54
+ .then(() => this.localStack?.stop());
51
55
  this.LOGGER = logging_1.LoggerFactory.create(`DEVSERVER[${name}]`);
52
56
  const encodedName = Buffer.from(name).toString('hex').substring(0, 64);
53
57
  this.eventProxyTopic = `EventProxyTopic${encodedName}`;
@@ -20,12 +20,13 @@ class InternalQueue {
20
20
  .then((queue) => this
21
21
  .createConsumer(sqs.client, this.name, queue.url, this.handler(queue), this.visibility)
22
22
  .then(() => queue.url));
23
- this.createConsumer = async (sqs, name, url, handler, visibility) => Promise.resolve(sqs_consumer_1.Consumer.create({
23
+ this.createConsumer = async (sqs, name, url, handler, visibility) => Promise.resolve(() => this.consumer = sqs_consumer_1.Consumer.create({
24
24
  queueUrl: url,
25
25
  waitTimeSeconds: visibility,
26
26
  sqs: sqs,
27
27
  handleMessage: async (message) => handler({ Records: [this.mapper(message)] }),
28
28
  }))
29
+ .then((factory) => factory())
29
30
  .then((consumer) => {
30
31
  consumer.on('error', (err) => {
31
32
  this.LOGGER.error(err, 'failed to handle message');
@@ -33,16 +34,13 @@ class InternalQueue {
33
34
  consumer.on('processing_error', (err) => {
34
35
  this.LOGGER.error(err, 'failed to process message');
35
36
  });
36
- consumer.start();
37
37
  this.LOGGER.info('consumer for %s initialized', name);
38
- this.state = {
39
- consumer: consumer,
40
- visibility: visibility,
41
- };
38
+ return consumer;
42
39
  })
40
+ .then(async (consumer) => consumer.start())
43
41
  .then(() => this.LOGGER.info('started'))
44
42
  .then(() => this);
45
- this.stop = () => (0, stoppable_1.stopConsumer)(this.state.visibility, this.state.consumer);
43
+ this.stop = async () => this.consumer !== undefined ? (0, stoppable_1.stopConsumer)(this.visibility, this.consumer) : this.LOGGER.warn('no consumer');
46
44
  this.LOGGER = logging_1.LoggerFactory.create(`INTERNALQUEUE[${name}]`);
47
45
  }
48
46
  async createQueue(sqs, name) {
@@ -15,6 +15,7 @@ export declare class Service<I> implements Lifecycle<I> {
15
15
  readonly name: string;
16
16
  readonly start: (config: DevServerConfig) => Promise<I>;
17
17
  readonly stop: () => Promise<void>;
18
+ private doStop;
18
19
  private readonly LOGGER;
19
20
  constructor(service: Lifecycle<I>, callback?: Callback<I>);
20
21
  private callbackOrDefault;
@@ -9,14 +9,18 @@ class Service {
9
9
  this.callbackOrDefault = async (instance) => (this.callback ?? this.DEFAULT_CALLBACK)(instance);
10
10
  this.name = service.name;
11
11
  this.LOGGER = logging_1.LoggerFactory.create(`SERVICE[${this.name}]`);
12
+ const stop = async () => service.stop();
12
13
  this.start = (config) => service.start(config)
14
+ .catch((error) => stop()
15
+ .then(() => this.doStop = () => Promise.resolve(this.LOGGER.error(error, 'failed')))
16
+ .then(() => Promise.reject(error)))
13
17
  .then((started) => this
14
18
  .callbackOrDefault(started)
15
19
  .then(() => this.LOGGER.info('started'))
16
20
  .then(() => started));
17
- this.stop = () => service
18
- .stop()
21
+ this.doStop = () => stop()
19
22
  .then(() => this.LOGGER.info('stopped'));
23
+ this.stop = () => this.doStop();
20
24
  }
21
25
  }
22
26
  exports.Service = Service;
package/dist/src/sqs.js CHANGED
@@ -14,7 +14,7 @@ class Sqs {
14
14
  .send(new client_sqs_1.CreateQueueCommand({
15
15
  QueueName: fifo ? `${name}.fifo` : name,
16
16
  Attributes: {
17
- FifoQueue: fifo ? 'true' : 'false',
17
+ FifoQueue: fifo ? 'true' : undefined,
18
18
  }
19
19
  }))
20
20
  .then((create) => this.client
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yopdev/dev-server",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.2",
4
4
  "scripts": {
5
5
  "compile": "tsc",
6
6
  "pretest": "npm run compile",