Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit e31d46b

Browse files
committed
feat: add typedef generation
1 parent 787cff3 commit e31d46b

File tree

6 files changed

+143
-28
lines changed

6 files changed

+143
-28
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "ipfs"
3+
}

.github/workflows/typecheck.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
- main
6+
- default
7+
pull_request:
8+
branches:
9+
- '**'
10+
11+
name: Typecheck
12+
jobs:
13+
check:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [12.x]
18+
steps:
19+
- uses: actions/checkout@v1
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v1
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- name: Install dependencies
25+
run: npm install
26+
- name: Typecheck
27+
uses: gozala/[email protected]

package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"main": "src/index.js",
77
"scripts": {
88
"lint": "aegir lint",
9-
"build": "aegir build",
9+
"check": "tsc --noEmit --noErrorTruncation",
10+
"build": "npm run build:js && npm run build:types",
11+
"build:js": "aegir build",
12+
"build:types": "tsc --emitDeclarationOnly --declarationDir dist",
1013
"test": "aegir test",
1114
"test:node": "aegir test --target node",
1215
"test:browser": "aegir test --target browser",
@@ -34,17 +37,24 @@
3437
},
3538
"homepage": "https://github.com/ipld/js-ipld-block#readme",
3639
"devDependencies": {
37-
"aegir": "^25.0.0",
38-
"uint8arrays": "^1.0.0"
40+
"aegir": "^27.0.0",
41+
"uint8arrays": "^1.0.0",
42+
"typescript": "^4.0.3"
3943
},
4044
"dependencies": {
41-
"cids": "^1.0.0",
42-
"class-is": "^1.1.0"
45+
"cids": "^1.0.0"
4346
},
4447
"engines": {
4548
"node": ">=6.0.0",
4649
"npm": ">=3.0.0"
4750
},
51+
"typesVersions": {
52+
"*": {
53+
"*": [
54+
"dist/types/*"
55+
]
56+
}
57+
},
4858
"contributors": [
4959
"David Dias <[email protected]>",
5060
"Volker Mische <[email protected]>",

src/index.js

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
'use strict'
22

33
const CID = require('cids')
4-
const withIs = require('class-is')
4+
5+
const { version } = require('../package.json')
6+
const blockSymbol = Symbol.for('@ipld/js-ipld-block/block')
7+
const readonly = { writable: false, configurable: false, enumerable: true }
58

69
/**
710
* Represents an immutable block of data that is uniquely referenced with a cid.
811
*
912
* @example
1013
* const block = new Block(Uint8Array.from([0, 1, 2, 3]), new CID('...'))
1114
*/
12-
module.exports = class Block {
15+
class Block {
1316
/**
14-
* @constructor
1517
* @param {Uint8Array} data - The data to be stored in the block as a Uint8Array.
1618
* @param {CID} cid - The cid of the data
1719
*/
@@ -24,46 +26,77 @@ module.exports = class Block {
2426
throw new Error('second argument must be a CID')
2527
}
2628

27-
this._data = data
28-
this._cid = cid
29+
this.data = data
30+
this.cid = cid
31+
32+
Object.defineProperties(this, {
33+
data: readonly,
34+
cid: readonly
35+
})
2936
}
3037

3138
/**
3239
* The data of this block.
3340
*
41+
* @deprecated
3442
* @type {Uint8Array}
3543
*/
36-
get data () {
37-
return this._data
38-
}
39-
40-
set data (val) {
41-
throw new Error('Tried to change an immutable block')
44+
get _data () {
45+
deprecateData()
46+
return this.data
4247
}
4348

4449
/**
4550
* The cid of the data this block represents.
4651
*
52+
* @deprecated
4753
* @type {CID}
4854
*/
49-
get cid () {
50-
return this._cid
55+
get _cid () {
56+
deprecateCID()
57+
return this.cid
5158
}
5259

53-
set cid (val) {
54-
throw new Error('Tried to change an immutable block')
60+
get [Symbol.toStringTag] () {
61+
return 'Block'
62+
}
63+
64+
get [blockSymbol] () {
65+
return true
5566
}
5667

57-
// eslint-disable-next-line valid-jsdoc
5868
/**
5969
* Check if the given value is a Block.
70+
*
71+
* @param {any} other
6072
* @returns {other is Block}
6173
*/
62-
static isBlock (other) { // eslint-disable-line no-unused-vars
63-
// implemented by class-is module
74+
static isBlock (other) {
75+
return Boolean(other && other[blockSymbol])
6476
}
6577
}
6678

67-
// to trick the typings engine
68-
// https://github.com/ipld/js-ipld-block/pull/55#discussion_r478845002
69-
module.exports = withIs(module.exports, { className: 'Block', symbolName: '@ipld/js-ipld-block/block' })
79+
/**
80+
* @param {RegExp} range
81+
* @param {string} message
82+
* @returns {() => void}
83+
*/
84+
const deprecate = (range, message) => {
85+
let warned = false
86+
return () => {
87+
if (range.test(version)) {
88+
if (!warned) {
89+
warned = true
90+
// eslint-disable-next-line no-console
91+
console.warn(message)
92+
}
93+
} else {
94+
throw new Error(message)
95+
}
96+
}
97+
}
98+
99+
const deprecateCID = deprecate(/^0\.10/, 'block._cid is deprecated and will be removed in the next major release. Use block.cid instead')
100+
const deprecateData = deprecate(/^0\.10/, 'block._data is deprecated and will be removed in the next major release. Use block.data instead')
101+
102+
module.exports = Block

test/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ describe('block', () => {
3434
expect(
3535
() => { b.data = 'fail' }
3636
).to.throw(
37-
/immutable/
37+
/read only/
3838
)
3939

4040
expect(
4141
() => { b.cid = 'fail' }
4242
).to.throw(
43-
/immutable/
43+
/read only/
4444
)
4545
})
4646
})

tsconfig.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"forceConsistentCasingInFileNames": true,
6+
"noImplicitReturns": false,
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"noFallthroughCasesInSwitch": true,
10+
"noUnusedLocals": true,
11+
"noUnusedParameters": true,
12+
"strictFunctionTypes": false,
13+
"strictNullChecks": true,
14+
"strictPropertyInitialization": true,
15+
"strictBindCallApply": true,
16+
"strict": true,
17+
"alwaysStrict": true,
18+
"esModuleInterop": true,
19+
"target": "ES2018",
20+
"moduleResolution": "node",
21+
"declaration": true,
22+
"declarationMap": true,
23+
"outDir": "dist",
24+
"skipLibCheck": true,
25+
"stripInternal": true,
26+
"resolveJsonModule": true,
27+
"paths": {
28+
"multiformats": [
29+
"src"
30+
]
31+
},
32+
"baseUrl": "."
33+
},
34+
"include": [
35+
"src"
36+
],
37+
"exclude": [
38+
"vendor",
39+
"node_modules"
40+
],
41+
"compileOnSave": false
42+
}

0 commit comments

Comments
 (0)