Skip to content

Commit a289f8b

Browse files
changed the way nodejs packages are created
1 parent 773843b commit a289f8b

File tree

10 files changed

+144
-90
lines changed

10 files changed

+144
-90
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
build
2+
node_modules
3+

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*
2+
!js
3+
!build/js/**
4+
!package.json
5+
!README*
6+
!LICENSE*

Makefile

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,9 @@ PYGEN := $(BUILDDIR)/python
4141
# generated Documentation files
4242
DOCGEN := $(BUILDDIR)/doc
4343

44-
# disable protobuf.js per default
45-
PROTOBUFJS := 0
46-
47-
# directory for ProtoBuf.js generated files
48-
PROTOBUFJS_GEN := $(BUILDDIR)/js
49-
50-
# the proto2js compiler
51-
PROTOJS := $(shell which proto2js)
52-
5344
# pkg-config
5445
PKG_CONFIG := $(shell which pkg-config)
5546

56-
# proto2js options - namespace
57-
#PROTOBUFJS_OPT := -commonjs=pb
58-
# http://en.wikipedia.org/wiki/Asynchronous_module_definition
59-
#PROTOBUFJS_OPT := -amd
60-
PROTOBUFJS_OPT := -class
61-
62-
# protobuf namespace; all protos except nanopb.proto
63-
JSNAMESPACE := =pb
64-
6547
# the set of all proto specs generated files depend on
6648
PROTO_SPECS := $(wildcard $(PROTODIR)/*.proto)
6749

@@ -91,10 +73,6 @@ PROTO_CXX_INCS := ${PROTO_SPECS:$(SRCDIR)/%.proto=$(CXXGEN)/%.pb.h}
9173
# generated C++ sources
9274
PROTO_CXX_SRCS := ${PROTO_SPECS:$(SRCDIR)/%.proto=$(CXXGEN)/%.pb.cc}
9375

94-
# generated Javasript sources
95-
PROTO_PROTOBUFJS_SRCS := ${PROTO_SPECS:$(SRCDIR)/%.proto=$(PROTOBUFJS_GEN)/%.js}
96-
97-
9876
# ---- generate dependcy files for .proto files
9977
#
10078
# the list of .d dep files for .proto files:
@@ -106,7 +84,6 @@ PBDEP_OPT :=
10684
#PBDEP_OPT += --debug
10785
PBDEP_OPT += --cgen=$(CXXGEN)
10886
PBDEP_OPT += --pygen=$(PYGEN)
109-
PBDEP_OPT += --jsgen=$(PROTOBUFJS_GEN)
11087
# this path must match the vpath arrangement exactly or the deps will be wrong
11188
# unfortunately there is no way to extract the proto path in the code
11289
# generator plugin
@@ -121,10 +98,6 @@ GENERATED += \
12198
$(PROTO_PY_TARGETS) \
12299
$(PROTO_PY_EXTRAS)
123100

124-
ifeq ($(PROTOBUFJS),1)
125-
GENERATED += $(PROTO_PROTOBUFJS_SRCS) $(PROTOBUFJS_GEN)/nanopb.js
126-
endif
127-
128101
$(OBJDIR)/%.d: $(SRCDIR)/%.proto
129102
$(ECHO) "protoc create dependencies for $<"
130103
@mkdir -p $(OBJDIR)/
@@ -180,32 +153,6 @@ $(DOCGEN)/%.md: $(SRCDIR)/%.proto
180153
--doc_out=markdown,$@:./ \
181154
$<
182155

183-
# ------------- ProtoBuf.js rules ------------
184-
#
185-
# see https://github.com/dcodeIO/ProtoBuf.js
186-
#
187-
# generate Javascript modules from proto files
188-
#=$(filter-out %/butterfly.ngc,$(call GLOB,../nc_files/*))
189-
190-
$(PROTOBUFJS_GEN)/%.js: $(SRCDIR)/%.proto
191-
$(ECHO) $(PROTOJS)" create $@ from $<"
192-
@mkdir -p $(PROTOBUFJS_GEN)
193-
$(Q)$(PROTOJS) $< \
194-
$(PROTOBUFJS_OPT)$(JSNAMESPACE) \
195-
> $@
196-
197-
# everything is namespace pb except nanopb.proto
198-
# # nanopb.proto needs different opts - no namespace argument
199-
ifeq ($(PROTOBUFJS),1)
200-
$(PROTOBUFJS_GEN)/nanopb.js: $(PROTODIR)/nanopb.proto
201-
# $(ECHO) "HALLO"
202-
# $(ECHO) $(PROTOJS)" create $@ from $<"
203-
@mkdir -p $(PROTOBUFJS_GEN)
204-
$(Q)$(PROTOJS) $< \
205-
$(PROTOBUFJS_OPT) \
206-
> $@
207-
endif
208-
209156
# force create of %.proto-dependent files and their deps
210157
Makefile: $(GENERATED) $(PROTO_DEPS)
211158
-include $(PROTO_DEPS)

README.javascript

Lines changed: 0 additions & 37 deletions
This file was deleted.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,37 @@ git subtree merge --prefix=src/machinetalk/proto machinetalk-protobuf/master --s
1818
`````
1919

2020
Now create a PR against the machinekit repo.
21+
22+
## Javascript (NPM/NodeJS)
23+
24+
### Installation
25+
26+
To use machinetalk protobuf definitions in your npm-based projects, use:
27+
28+
```sh
29+
npm install --save machinetalk-protobuf
30+
```
31+
32+
### Usage
33+
34+
See [examples](js/examples). If you want to try these examples, be sure to first run `npm install` in this repository.
35+
36+
#### Encoding
37+
38+
```js
39+
var machinetalkProtobuf = require('machinetalk-protobuf');
40+
var messageContainer = {
41+
type: machinetalkProtobuf.message.ContainerType.MT_PING
42+
};
43+
var encodedMessageContainer = machinetalkProtobuf.message.Container.encode(messageContainer);
44+
```
45+
This results in a buffer that starts with `0x08 0xd2 0x01`.
46+
47+
#### Decoding
48+
49+
```js
50+
var machinetalkProtobuf = require('machinetalk-protobuf');
51+
var encodedBuffer = new Buffer([0x08, 0xd2, 0x01]);
52+
var decodedMessageContainer = machinetalkProtobuf.message.Container.decode(encodedBuffer);
53+
```
54+
This results in a messageContainer like the one defined in [Encoding](#Encoding).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var machinetalkProtobuf = require('../index.js');
2+
3+
var encodedBuffer = new Buffer([0x08, 0xd2, 0x01]);
4+
5+
// Decode the message.
6+
var decodedMessageContainer = machinetalkProtobuf.message.Container.decode(encodedBuffer);
7+
8+
// decodedMessageContainer.type === machinetalkProtobuf.message.ContainerType.MT_PING
9+
console.log(decodedMessageContainer);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var machinetalkProtobuf = require('../index.js');
2+
3+
// Define the message we want to encode.
4+
var messageContainer = {
5+
type: machinetalkProtobuf.message.ContainerType.MT_PING
6+
};
7+
8+
// Encode the message.
9+
var encodedMessageContainer = machinetalkProtobuf.message.Container.encode(messageContainer);
10+
11+
// Strip off excess bytes from the resulting buffer.
12+
var encodedBuffer = encodedMessageContainer.buffer.slice(encodedMessageContainer.offset, encodedMessageContainer.limit);
13+
14+
// Print the buffer.
15+
console.log(encodedBuffer);

js/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var protoNames = require('../build/js/protonames.json');
2+
3+
protoNames.forEach(function(proto) {
4+
module.exports[proto.name] = require('../build/js/' + proto.fullname + '.js').pb;
5+
});

js/scripts/install.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env node
2+
var path = require('path');
3+
var fs = require('fs');
4+
var mkdirp = require('mkdirp');
5+
var child_process = require('child_process');
6+
var sourcePath = path.join(__dirname, '/../../src');
7+
var destinationPath = path.join(__dirname, '/../../build/js');
8+
9+
var namespace = 'machinetalk/protobuf';
10+
11+
// Create list of information for protobuf files.
12+
var protos = fs.readdirSync(path.join(sourcePath, namespace))
13+
.filter(function(filename) {
14+
// Only handle .proto files.
15+
return /\.proto$/.test(filename);
16+
})
17+
.map(function(filename) {
18+
var name = path.basename(filename, '.proto');
19+
var fullname = path.join(namespace, name);
20+
return {
21+
filename: filename,
22+
name: name,
23+
fullname: fullname,
24+
protopath: path.join(sourcePath, namespace, filename),
25+
jspath: path.join(destinationPath, namespace, name + '.js')
26+
};
27+
});
28+
29+
// Generate js file for each proto file
30+
protos.forEach(function(protos) {
31+
mkdirp.sync(path.dirname(protos.jspath));
32+
child_process.execFileSync('pbjs', ['--target', 'commonjs', '--out', protos.jspath, '--path', sourcePath, protos.protopath]);
33+
});
34+
35+
36+
// Generate json file to direct index.js to the generated js files.
37+
var protosjson = protos.map(function(proto) {
38+
return {
39+
name: proto.name,
40+
fullname: proto.fullname
41+
};
42+
});
43+
44+
mkdirp.sync(destinationPath);
45+
fs.writeFileSync(path.join(destinationPath, 'protonames.json'), JSON.stringify(protosjson));

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "machinetalk-protobuf",
3+
"version": "1.0.1",
4+
"description": "Protobuf definitions to communicate with Machinetalk",
5+
"main": "js/index.js",
6+
"scripts": {
7+
"prepublish": "js/scripts/install.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/machinekit/machinetalk-protobuf.git"
12+
},
13+
"author": "Bob van der Linden <[email protected]>",
14+
"keywords": [
15+
"machinetalk",
16+
"machinekit",
17+
"protobuf"
18+
],
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/machinekit/machinetalk-protobuf/issues"
22+
},
23+
"homepage": "https://github.com/machinekit/machinetalk-protobuf#readme",
24+
"dependencies": {
25+
"mkdirp": "0.5.1",
26+
"protobufjs": "5.0.0"
27+
}
28+
}

0 commit comments

Comments
 (0)