Skip to content

Commit 81149df

Browse files
committed
Updated README's client-side examples
1 parent cb0f793 commit 81149df

File tree

6 files changed

+27
-40
lines changed

6 files changed

+27
-40
lines changed

README.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Server-side:
209209
const http = require('http');
210210
const sb = require('structure-bytes');
211211

212-
let type = new sb.DateType();
212+
let type = new sb.DateType;
213213
http.createServer((req, res) => {
214214
sb.httpRespond({req, res, type, value: new Date}, err => {
215215
console.log('Responded');
@@ -218,16 +218,17 @@ http.createServer((req, res) => {
218218
````
219219
Client-side:
220220
````html
221-
<script src = '/structure-bytes/compiled/jquery.js'></script>
222221
<script src = '/structure-bytes/compiled/download.js'></script>
223222
<script>
224223
//'date' specifies the name of the type being transferred so it can be cached
225-
sb.download('date', {
226-
url: '/date',
227-
success: function(value) {
228-
console.log(value.getFullYear()); //2016
229-
}
230-
});
224+
sb.download({
225+
name: 'date',
226+
url: '/',
227+
options: {} //optional options to pass to fetch() (e.g. cookies, headers, etc.)
228+
})
229+
.then(value =>
230+
console.log(value.getFullYear()) //2017
231+
);
231232
</script>
232233
````
233234
### HTTP POST value
@@ -236,34 +237,29 @@ Server-side:
236237
const http = require('http');
237238
const sb = require('structure-bytes');
238239

239-
let type = new sb.DateType();
240240
http.createServer((req, res) => {
241-
sb.readValue({type: new sb.UnsignedIntType, inStream: req}, (err, value) => {
241+
sb.readValue({type: new sb.FlexUnsignedIntType, inStream: req}, (err, value) => {
242242
res.end(String(value));
243243
});
244244
}).listen(80);
245245
````
246246
Client-side:
247247
````html
248-
<script src = '/structure-bytes/compiled/jquery.js'></script>
249248
<script src = '/structure-bytes/compiled/upload.js'></script>
250-
<button>Click me</button>
249+
<button onclick = 'upload()'>Click me</button>
251250
<script>
252251
var clickCount = 0;
253-
$('button').click(function() {
252+
function upload() {
254253
clickCount++;
255254
sb.upload({
256-
type: new sb.UnsignedIntType,
257-
value: clickCount
258-
}, {
255+
type: new sb.FlexUnsignedIntType,
256+
value: clickCount,
259257
url: '/click',
260-
type: 'POST',
261-
dataType: 'text',
262-
success: function(response) {
263-
alert(response); //alerts '1', '2', etc.
264-
}
265-
});
266-
});
258+
options: {method: 'POST'} //options to pass to fetch(); method 'POST' is required
259+
})
260+
.then(response => response.text())
261+
.then(alert);
262+
}
267263
</script>
268264
````
269265

compiled/download.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload-download.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/sha-256.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ const K = new Uint32Array([
1111
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
1212
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
1313
]);
14-
function rightRotate(value, bits) {
15-
return (value >>> bits) | (value << (32 - bits));
16-
}
17-
function pos(value) {
18-
return new Uint32Array([value])[0];
19-
}
14+
const rightRotate = (value, bits) => (value >>> bits) | (value << (32 - bits));
2015
exports.default = (input) => {
2116
const lBytes = input.byteLength;
2217
const l = lBytes * 8; //not using bitwise math in case this overflows a 32-bit integer
23-
assert_1.default(l === pos(l | 0), 'Bit length does not fit in a 32-bit integer');
18+
assert_1.default(l === new Uint32Array([l | 0])[0], 'Bit length does not fit in a 32-bit integer');
2419
const extraBytes = 64 - ((lBytes + 72) & 63);
2520
const messageLength = lBytes + extraBytes + 8;
2621
const message = new ArrayBuffer(messageLength);

lib/sha-256.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ const K = new Uint32Array([
1111
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
1212
])
1313

14-
function rightRotate(value: number, bits: number): number {
15-
return (value >>> bits) | (value << (32 - bits))
16-
}
17-
function pos(value: number): number {
18-
return new Uint32Array([value])[0]
19-
}
14+
const rightRotate = (value: number, bits: number): number =>
15+
(value >>> bits) | (value << (32 - bits))
2016

2117
export default (input: ArrayBuffer): ArrayBuffer => {
2218
const lBytes = input.byteLength
2319
const l = lBytes * 8 //not using bitwise math in case this overflows a 32-bit integer
24-
assert(l === pos(l | 0), 'Bit length does not fit in a 32-bit integer')
20+
assert(l === new Uint32Array([l | 0])[0], 'Bit length does not fit in a 32-bit integer')
2521
const extraBytes = 64 - ((lBytes + 72) & 63)
2622
const messageLength = lBytes + extraBytes + 8
2723
const message = new ArrayBuffer(messageLength)

0 commit comments

Comments
 (0)